stmpe-keypad.c 11 KB

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