stmpe-keypad.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) ST-Ericsson SA 2010
  4. *
  5. * Author: Rabin Vincent <rabin.vincent@stericsson.com> for ST-Ericsson
  6. */
  7. #include <linux/module.h>
  8. #include <linux/slab.h>
  9. #include <linux/input.h>
  10. #include <linux/interrupt.h>
  11. #include <linux/of.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/input/matrix_keypad.h>
  14. #include <linux/mfd/stmpe.h>
  15. /* These are at the same addresses in all STMPE variants */
  16. #define STMPE_KPC_COL 0x60
  17. #define STMPE_KPC_ROW_MSB 0x61
  18. #define STMPE_KPC_ROW_LSB 0x62
  19. #define STMPE_KPC_CTRL_MSB 0x63
  20. #define STMPE_KPC_CTRL_LSB 0x64
  21. #define STMPE_KPC_COMBI_KEY_0 0x65
  22. #define STMPE_KPC_COMBI_KEY_1 0x66
  23. #define STMPE_KPC_COMBI_KEY_2 0x67
  24. #define STMPE_KPC_DATA_BYTE0 0x68
  25. #define STMPE_KPC_DATA_BYTE1 0x69
  26. #define STMPE_KPC_DATA_BYTE2 0x6a
  27. #define STMPE_KPC_DATA_BYTE3 0x6b
  28. #define STMPE_KPC_DATA_BYTE4 0x6c
  29. #define STMPE_KPC_CTRL_LSB_SCAN (0x1 << 0)
  30. #define STMPE_KPC_CTRL_LSB_DEBOUNCE (0x7f << 1)
  31. #define STMPE_KPC_CTRL_MSB_SCAN_COUNT (0xf << 4)
  32. #define STMPE_KPC_ROW_MSB_ROWS 0xff
  33. #define STMPE_KPC_DATA_UP (0x1 << 7)
  34. #define STMPE_KPC_DATA_ROW (0xf << 3)
  35. #define STMPE_KPC_DATA_COL (0x7 << 0)
  36. #define STMPE_KPC_DATA_NOKEY_MASK 0x78
  37. #define STMPE_KEYPAD_MAX_DEBOUNCE 127
  38. #define STMPE_KEYPAD_MAX_SCAN_COUNT 15
  39. #define STMPE_KEYPAD_MAX_ROWS 8
  40. #define STMPE_KEYPAD_MAX_COLS 8
  41. #define STMPE_KEYPAD_ROW_SHIFT 3
  42. #define STMPE_KEYPAD_KEYMAP_MAX_SIZE \
  43. (STMPE_KEYPAD_MAX_ROWS * STMPE_KEYPAD_MAX_COLS)
  44. #define STMPE1601_NUM_DATA 5
  45. #define STMPE2401_NUM_DATA 3
  46. #define STMPE2403_NUM_DATA 5
  47. /* Make sure it covers all cases above */
  48. #define MAX_NUM_DATA 5
  49. /**
  50. * struct stmpe_keypad_variant - model-specific attributes
  51. * @auto_increment: whether the KPC_DATA_BYTE register address
  52. * auto-increments on multiple read
  53. * @set_pullup: whether the pins need to have their pull-ups set
  54. * @num_data: number of data bytes
  55. * @num_normal_data: number of normal keys' data bytes
  56. * @max_cols: maximum number of columns supported
  57. * @max_rows: maximum number of rows supported
  58. * @col_gpios: bitmask of gpios which can be used for columns
  59. * @row_gpios: bitmask of gpios which can be used for rows
  60. */
  61. struct stmpe_keypad_variant {
  62. bool auto_increment;
  63. bool set_pullup;
  64. int num_data;
  65. int num_normal_data;
  66. int max_cols;
  67. int max_rows;
  68. unsigned int col_gpios;
  69. unsigned int row_gpios;
  70. };
  71. static const struct stmpe_keypad_variant stmpe_keypad_variants[] = {
  72. [STMPE1601] = {
  73. .auto_increment = true,
  74. .num_data = STMPE1601_NUM_DATA,
  75. .num_normal_data = 3,
  76. .max_cols = 8,
  77. .max_rows = 8,
  78. .col_gpios = 0x000ff, /* GPIO 0 - 7 */
  79. .row_gpios = 0x0ff00, /* GPIO 8 - 15 */
  80. },
  81. [STMPE2401] = {
  82. .auto_increment = false,
  83. .set_pullup = true,
  84. .num_data = STMPE2401_NUM_DATA,
  85. .num_normal_data = 2,
  86. .max_cols = 8,
  87. .max_rows = 12,
  88. .col_gpios = 0x0000ff, /* GPIO 0 - 7*/
  89. .row_gpios = 0x1f7f00, /* GPIO 8-14, 16-20 */
  90. },
  91. [STMPE2403] = {
  92. .auto_increment = true,
  93. .set_pullup = true,
  94. .num_data = STMPE2403_NUM_DATA,
  95. .num_normal_data = 3,
  96. .max_cols = 8,
  97. .max_rows = 12,
  98. .col_gpios = 0x0000ff, /* GPIO 0 - 7*/
  99. .row_gpios = 0x1fef00, /* GPIO 8-14, 16-20 */
  100. },
  101. };
  102. /**
  103. * struct stmpe_keypad - STMPE keypad state container
  104. * @stmpe: pointer to parent STMPE device
  105. * @input: spawned input device
  106. * @variant: STMPE variant
  107. * @debounce_ms: debounce interval, in ms. Maximum is
  108. * %STMPE_KEYPAD_MAX_DEBOUNCE.
  109. * @scan_count: number of key scanning cycles to confirm key data.
  110. * Maximum is %STMPE_KEYPAD_MAX_SCAN_COUNT.
  111. * @no_autorepeat: disable key autorepeat
  112. * @rows: bitmask for the rows
  113. * @cols: bitmask for the columns
  114. * @keymap: the keymap
  115. */
  116. struct stmpe_keypad {
  117. struct stmpe *stmpe;
  118. struct input_dev *input;
  119. const struct stmpe_keypad_variant *variant;
  120. unsigned int debounce_ms;
  121. unsigned int scan_count;
  122. bool no_autorepeat;
  123. unsigned int rows;
  124. unsigned int cols;
  125. unsigned short keymap[STMPE_KEYPAD_KEYMAP_MAX_SIZE];
  126. };
  127. static int stmpe_keypad_read_data(struct stmpe_keypad *keypad, u8 *data)
  128. {
  129. const struct stmpe_keypad_variant *variant = keypad->variant;
  130. struct stmpe *stmpe = keypad->stmpe;
  131. int ret;
  132. int i;
  133. if (variant->auto_increment)
  134. return stmpe_block_read(stmpe, STMPE_KPC_DATA_BYTE0,
  135. variant->num_data, data);
  136. for (i = 0; i < variant->num_data; i++) {
  137. ret = stmpe_reg_read(stmpe, STMPE_KPC_DATA_BYTE0 + i);
  138. if (ret < 0)
  139. return ret;
  140. data[i] = ret;
  141. }
  142. return 0;
  143. }
  144. static irqreturn_t stmpe_keypad_irq(int irq, void *dev)
  145. {
  146. struct stmpe_keypad *keypad = dev;
  147. struct input_dev *input = keypad->input;
  148. const struct stmpe_keypad_variant *variant = keypad->variant;
  149. u8 fifo[MAX_NUM_DATA];
  150. int ret;
  151. int i;
  152. ret = stmpe_keypad_read_data(keypad, fifo);
  153. if (ret < 0)
  154. return IRQ_NONE;
  155. for (i = 0; i < variant->num_normal_data; i++) {
  156. u8 data = fifo[i];
  157. int row = (data & STMPE_KPC_DATA_ROW) >> 3;
  158. int col = data & STMPE_KPC_DATA_COL;
  159. int code = MATRIX_SCAN_CODE(row, col, STMPE_KEYPAD_ROW_SHIFT);
  160. bool up = data & STMPE_KPC_DATA_UP;
  161. if ((data & STMPE_KPC_DATA_NOKEY_MASK)
  162. == STMPE_KPC_DATA_NOKEY_MASK)
  163. continue;
  164. input_event(input, EV_MSC, MSC_SCAN, code);
  165. input_report_key(input, keypad->keymap[code], !up);
  166. input_sync(input);
  167. }
  168. return IRQ_HANDLED;
  169. }
  170. static int stmpe_keypad_altfunc_init(struct stmpe_keypad *keypad)
  171. {
  172. const struct stmpe_keypad_variant *variant = keypad->variant;
  173. unsigned int col_gpios = variant->col_gpios;
  174. unsigned int row_gpios = variant->row_gpios;
  175. struct stmpe *stmpe = keypad->stmpe;
  176. u8 pureg = stmpe->regs[STMPE_IDX_GPPUR_LSB];
  177. unsigned int pins = 0;
  178. unsigned int pu_pins = 0;
  179. int ret;
  180. int i;
  181. /*
  182. * Figure out which pins need to be set to the keypad alternate
  183. * function.
  184. *
  185. * {cols,rows}_gpios are bitmasks of which pins on the chip can be used
  186. * for the keypad.
  187. *
  188. * keypad->{cols,rows} are a bitmask of which pins (of the ones useable
  189. * for the keypad) are used on the board.
  190. */
  191. for (i = 0; i < variant->max_cols; i++) {
  192. int num = __ffs(col_gpios);
  193. if (keypad->cols & (1 << i)) {
  194. pins |= 1 << num;
  195. pu_pins |= 1 << num;
  196. }
  197. col_gpios &= ~(1 << num);
  198. }
  199. for (i = 0; i < variant->max_rows; i++) {
  200. int num = __ffs(row_gpios);
  201. if (keypad->rows & (1 << i))
  202. pins |= 1 << num;
  203. row_gpios &= ~(1 << num);
  204. }
  205. ret = stmpe_set_altfunc(stmpe, pins, STMPE_BLOCK_KEYPAD);
  206. if (ret)
  207. return ret;
  208. /*
  209. * On STMPE24xx, set pin bias to pull-up on all keypad input
  210. * pins (columns), this incidentally happen to be maximum 8 pins
  211. * and placed at GPIO0-7 so only the LSB of the pull up register
  212. * ever needs to be written.
  213. */
  214. if (variant->set_pullup) {
  215. u8 val;
  216. ret = stmpe_reg_read(stmpe, pureg);
  217. if (ret)
  218. return ret;
  219. /* Do not touch unused pins, may be used for GPIO */
  220. val = ret & ~pu_pins;
  221. val |= pu_pins;
  222. ret = stmpe_reg_write(stmpe, pureg, val);
  223. }
  224. return 0;
  225. }
  226. static int stmpe_keypad_chip_init(struct stmpe_keypad *keypad)
  227. {
  228. const struct stmpe_keypad_variant *variant = keypad->variant;
  229. struct stmpe *stmpe = keypad->stmpe;
  230. int ret;
  231. if (keypad->debounce_ms > STMPE_KEYPAD_MAX_DEBOUNCE)
  232. return -EINVAL;
  233. if (keypad->scan_count > STMPE_KEYPAD_MAX_SCAN_COUNT)
  234. return -EINVAL;
  235. ret = stmpe_enable(stmpe, STMPE_BLOCK_KEYPAD);
  236. if (ret < 0)
  237. return ret;
  238. ret = stmpe_keypad_altfunc_init(keypad);
  239. if (ret < 0)
  240. return ret;
  241. ret = stmpe_reg_write(stmpe, STMPE_KPC_COL, keypad->cols);
  242. if (ret < 0)
  243. return ret;
  244. ret = stmpe_reg_write(stmpe, STMPE_KPC_ROW_LSB, keypad->rows);
  245. if (ret < 0)
  246. return ret;
  247. if (variant->max_rows > 8) {
  248. ret = stmpe_set_bits(stmpe, STMPE_KPC_ROW_MSB,
  249. STMPE_KPC_ROW_MSB_ROWS,
  250. keypad->rows >> 8);
  251. if (ret < 0)
  252. return ret;
  253. }
  254. ret = stmpe_set_bits(stmpe, STMPE_KPC_CTRL_MSB,
  255. STMPE_KPC_CTRL_MSB_SCAN_COUNT,
  256. keypad->scan_count << 4);
  257. if (ret < 0)
  258. return ret;
  259. return stmpe_set_bits(stmpe, STMPE_KPC_CTRL_LSB,
  260. STMPE_KPC_CTRL_LSB_SCAN |
  261. STMPE_KPC_CTRL_LSB_DEBOUNCE,
  262. STMPE_KPC_CTRL_LSB_SCAN |
  263. (keypad->debounce_ms << 1));
  264. }
  265. static void stmpe_keypad_fill_used_pins(struct stmpe_keypad *keypad,
  266. u32 used_rows, u32 used_cols)
  267. {
  268. int row, col;
  269. for (row = 0; row < used_rows; row++) {
  270. for (col = 0; col < used_cols; col++) {
  271. int code = MATRIX_SCAN_CODE(row, col,
  272. STMPE_KEYPAD_ROW_SHIFT);
  273. if (keypad->keymap[code] != KEY_RESERVED) {
  274. keypad->rows |= 1 << row;
  275. keypad->cols |= 1 << col;
  276. }
  277. }
  278. }
  279. }
  280. static int stmpe_keypad_probe(struct platform_device *pdev)
  281. {
  282. struct stmpe *stmpe = dev_get_drvdata(pdev->dev.parent);
  283. struct device_node *np = pdev->dev.of_node;
  284. struct stmpe_keypad *keypad;
  285. struct input_dev *input;
  286. u32 rows;
  287. u32 cols;
  288. int error;
  289. int irq;
  290. irq = platform_get_irq(pdev, 0);
  291. if (irq < 0)
  292. return irq;
  293. keypad = devm_kzalloc(&pdev->dev, sizeof(struct stmpe_keypad),
  294. GFP_KERNEL);
  295. if (!keypad)
  296. return -ENOMEM;
  297. keypad->stmpe = stmpe;
  298. keypad->variant = &stmpe_keypad_variants[stmpe->partnum];
  299. of_property_read_u32(np, "debounce-interval", &keypad->debounce_ms);
  300. of_property_read_u32(np, "st,scan-count", &keypad->scan_count);
  301. keypad->no_autorepeat = of_property_read_bool(np, "st,no-autorepeat");
  302. input = devm_input_allocate_device(&pdev->dev);
  303. if (!input)
  304. return -ENOMEM;
  305. input->name = "STMPE keypad";
  306. input->id.bustype = BUS_I2C;
  307. input->dev.parent = &pdev->dev;
  308. error = matrix_keypad_parse_properties(&pdev->dev, &rows, &cols);
  309. if (error)
  310. return error;
  311. error = matrix_keypad_build_keymap(NULL, NULL, rows, cols,
  312. keypad->keymap, input);
  313. if (error)
  314. return error;
  315. input_set_capability(input, EV_MSC, MSC_SCAN);
  316. if (!keypad->no_autorepeat)
  317. __set_bit(EV_REP, input->evbit);
  318. stmpe_keypad_fill_used_pins(keypad, rows, cols);
  319. keypad->input = input;
  320. error = stmpe_keypad_chip_init(keypad);
  321. if (error < 0)
  322. return error;
  323. error = devm_request_threaded_irq(&pdev->dev, irq,
  324. NULL, stmpe_keypad_irq,
  325. IRQF_ONESHOT, "stmpe-keypad", keypad);
  326. if (error) {
  327. dev_err(&pdev->dev, "unable to get irq: %d\n", error);
  328. return error;
  329. }
  330. error = input_register_device(input);
  331. if (error) {
  332. dev_err(&pdev->dev,
  333. "unable to register input device: %d\n", error);
  334. return error;
  335. }
  336. platform_set_drvdata(pdev, keypad);
  337. return 0;
  338. }
  339. static void stmpe_keypad_remove(struct platform_device *pdev)
  340. {
  341. struct stmpe_keypad *keypad = platform_get_drvdata(pdev);
  342. stmpe_disable(keypad->stmpe, STMPE_BLOCK_KEYPAD);
  343. }
  344. static struct platform_driver stmpe_keypad_driver = {
  345. .driver.name = "stmpe-keypad",
  346. .probe = stmpe_keypad_probe,
  347. .remove_new = stmpe_keypad_remove,
  348. };
  349. module_platform_driver(stmpe_keypad_driver);
  350. MODULE_LICENSE("GPL v2");
  351. MODULE_DESCRIPTION("STMPExxxx keypad driver");
  352. MODULE_AUTHOR("Rabin Vincent <rabin.vincent@stericsson.com>");