stmpe-keypad.c 10 KB

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