stmpe-keypad.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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/init.h>
  9. #include <linux/slab.h>
  10. #include <linux/input.h>
  11. #include <linux/interrupt.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_SIZE \
  43. (STMPE_KEYPAD_MAX_ROWS * STMPE_KEYPAD_MAX_COLS)
  44. /**
  45. * struct stmpe_keypad_variant - model-specific attributes
  46. * @auto_increment: whether the KPC_DATA_BYTE register address
  47. * auto-increments on multiple read
  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. int num_data;
  58. int num_normal_data;
  59. int max_cols;
  60. int max_rows;
  61. unsigned int col_gpios;
  62. unsigned int row_gpios;
  63. };
  64. static const struct stmpe_keypad_variant stmpe_keypad_variants[] = {
  65. [STMPE1601] = {
  66. .auto_increment = true,
  67. .num_data = 5,
  68. .num_normal_data = 3,
  69. .max_cols = 8,
  70. .max_rows = 8,
  71. .col_gpios = 0x000ff, /* GPIO 0 - 7 */
  72. .row_gpios = 0x0ff00, /* GPIO 8 - 15 */
  73. },
  74. [STMPE2401] = {
  75. .auto_increment = false,
  76. .num_data = 3,
  77. .num_normal_data = 2,
  78. .max_cols = 8,
  79. .max_rows = 12,
  80. .col_gpios = 0x0000ff, /* GPIO 0 - 7*/
  81. .row_gpios = 0x1fef00, /* GPIO 8-14, 16-20 */
  82. },
  83. [STMPE2403] = {
  84. .auto_increment = true,
  85. .num_data = 5,
  86. .num_normal_data = 3,
  87. .max_cols = 8,
  88. .max_rows = 12,
  89. .col_gpios = 0x0000ff, /* GPIO 0 - 7*/
  90. .row_gpios = 0x1fef00, /* GPIO 8-14, 16-20 */
  91. },
  92. };
  93. struct stmpe_keypad {
  94. struct stmpe *stmpe;
  95. struct input_dev *input;
  96. const struct stmpe_keypad_variant *variant;
  97. const struct stmpe_keypad_platform_data *plat;
  98. unsigned int rows;
  99. unsigned int cols;
  100. unsigned short keymap[STMPE_KEYPAD_KEYMAP_SIZE];
  101. };
  102. static int stmpe_keypad_read_data(struct stmpe_keypad *keypad, u8 *data)
  103. {
  104. const struct stmpe_keypad_variant *variant = keypad->variant;
  105. struct stmpe *stmpe = keypad->stmpe;
  106. int ret;
  107. int i;
  108. if (variant->auto_increment)
  109. return stmpe_block_read(stmpe, STMPE_KPC_DATA_BYTE0,
  110. variant->num_data, data);
  111. for (i = 0; i < variant->num_data; i++) {
  112. ret = stmpe_reg_read(stmpe, STMPE_KPC_DATA_BYTE0 + i);
  113. if (ret < 0)
  114. return ret;
  115. data[i] = ret;
  116. }
  117. return 0;
  118. }
  119. static irqreturn_t stmpe_keypad_irq(int irq, void *dev)
  120. {
  121. struct stmpe_keypad *keypad = dev;
  122. struct input_dev *input = keypad->input;
  123. const struct stmpe_keypad_variant *variant = keypad->variant;
  124. u8 fifo[variant->num_data];
  125. int ret;
  126. int i;
  127. ret = stmpe_keypad_read_data(keypad, fifo);
  128. if (ret < 0)
  129. return IRQ_NONE;
  130. for (i = 0; i < variant->num_normal_data; i++) {
  131. u8 data = fifo[i];
  132. int row = (data & STMPE_KPC_DATA_ROW) >> 3;
  133. int col = data & STMPE_KPC_DATA_COL;
  134. int code = MATRIX_SCAN_CODE(row, col, STMPE_KEYPAD_ROW_SHIFT);
  135. bool up = data & STMPE_KPC_DATA_UP;
  136. if ((data & STMPE_KPC_DATA_NOKEY_MASK)
  137. == STMPE_KPC_DATA_NOKEY_MASK)
  138. continue;
  139. input_event(input, EV_MSC, MSC_SCAN, code);
  140. input_report_key(input, keypad->keymap[code], !up);
  141. input_sync(input);
  142. }
  143. return IRQ_HANDLED;
  144. }
  145. static int __devinit stmpe_keypad_altfunc_init(struct stmpe_keypad *keypad)
  146. {
  147. const struct stmpe_keypad_variant *variant = keypad->variant;
  148. unsigned int col_gpios = variant->col_gpios;
  149. unsigned int row_gpios = variant->row_gpios;
  150. struct stmpe *stmpe = keypad->stmpe;
  151. unsigned int pins = 0;
  152. int i;
  153. /*
  154. * Figure out which pins need to be set to the keypad alternate
  155. * function.
  156. *
  157. * {cols,rows}_gpios are bitmasks of which pins on the chip can be used
  158. * for the keypad.
  159. *
  160. * keypad->{cols,rows} are a bitmask of which pins (of the ones useable
  161. * for the keypad) are used on the board.
  162. */
  163. for (i = 0; i < variant->max_cols; i++) {
  164. int num = __ffs(col_gpios);
  165. if (keypad->cols & (1 << i))
  166. pins |= 1 << num;
  167. col_gpios &= ~(1 << num);
  168. }
  169. for (i = 0; i < variant->max_rows; i++) {
  170. int num = __ffs(row_gpios);
  171. if (keypad->rows & (1 << i))
  172. pins |= 1 << num;
  173. row_gpios &= ~(1 << num);
  174. }
  175. return stmpe_set_altfunc(stmpe, pins, STMPE_BLOCK_KEYPAD);
  176. }
  177. static int __devinit stmpe_keypad_chip_init(struct stmpe_keypad *keypad)
  178. {
  179. const struct stmpe_keypad_platform_data *plat = keypad->plat;
  180. const struct stmpe_keypad_variant *variant = keypad->variant;
  181. struct stmpe *stmpe = keypad->stmpe;
  182. int ret;
  183. if (plat->debounce_ms > STMPE_KEYPAD_MAX_DEBOUNCE)
  184. return -EINVAL;
  185. if (plat->scan_count > STMPE_KEYPAD_MAX_SCAN_COUNT)
  186. return -EINVAL;
  187. ret = stmpe_enable(stmpe, STMPE_BLOCK_KEYPAD);
  188. if (ret < 0)
  189. return ret;
  190. ret = stmpe_keypad_altfunc_init(keypad);
  191. if (ret < 0)
  192. return ret;
  193. ret = stmpe_reg_write(stmpe, STMPE_KPC_COL, keypad->cols);
  194. if (ret < 0)
  195. return ret;
  196. ret = stmpe_reg_write(stmpe, STMPE_KPC_ROW_LSB, keypad->rows);
  197. if (ret < 0)
  198. return ret;
  199. if (variant->max_rows > 8) {
  200. ret = stmpe_set_bits(stmpe, STMPE_KPC_ROW_MSB,
  201. STMPE_KPC_ROW_MSB_ROWS,
  202. keypad->rows >> 8);
  203. if (ret < 0)
  204. return ret;
  205. }
  206. ret = stmpe_set_bits(stmpe, STMPE_KPC_CTRL_MSB,
  207. STMPE_KPC_CTRL_MSB_SCAN_COUNT,
  208. plat->scan_count << 4);
  209. if (ret < 0)
  210. return ret;
  211. return stmpe_set_bits(stmpe, STMPE_KPC_CTRL_LSB,
  212. STMPE_KPC_CTRL_LSB_SCAN |
  213. STMPE_KPC_CTRL_LSB_DEBOUNCE,
  214. STMPE_KPC_CTRL_LSB_SCAN |
  215. (plat->debounce_ms << 1));
  216. }
  217. static int __devinit stmpe_keypad_probe(struct platform_device *pdev)
  218. {
  219. struct stmpe *stmpe = dev_get_drvdata(pdev->dev.parent);
  220. struct stmpe_keypad_platform_data *plat;
  221. struct stmpe_keypad *keypad;
  222. struct input_dev *input;
  223. int ret;
  224. int irq;
  225. int i;
  226. plat = stmpe->pdata->keypad;
  227. if (!plat)
  228. return -ENODEV;
  229. irq = platform_get_irq(pdev, 0);
  230. if (irq < 0)
  231. return irq;
  232. keypad = kzalloc(sizeof(struct stmpe_keypad), GFP_KERNEL);
  233. if (!keypad)
  234. return -ENOMEM;
  235. input = input_allocate_device();
  236. if (!input) {
  237. ret = -ENOMEM;
  238. goto out_freekeypad;
  239. }
  240. input->name = "STMPE keypad";
  241. input->id.bustype = BUS_I2C;
  242. input->dev.parent = &pdev->dev;
  243. input_set_capability(input, EV_MSC, MSC_SCAN);
  244. __set_bit(EV_KEY, input->evbit);
  245. if (!plat->no_autorepeat)
  246. __set_bit(EV_REP, input->evbit);
  247. input->keycode = keypad->keymap;
  248. input->keycodesize = sizeof(keypad->keymap[0]);
  249. input->keycodemax = ARRAY_SIZE(keypad->keymap);
  250. matrix_keypad_build_keymap(plat->keymap_data, STMPE_KEYPAD_ROW_SHIFT,
  251. input->keycode, input->keybit);
  252. for (i = 0; i < plat->keymap_data->keymap_size; i++) {
  253. unsigned int key = plat->keymap_data->keymap[i];
  254. keypad->cols |= 1 << KEY_COL(key);
  255. keypad->rows |= 1 << KEY_ROW(key);
  256. }
  257. keypad->stmpe = stmpe;
  258. keypad->plat = plat;
  259. keypad->input = input;
  260. keypad->variant = &stmpe_keypad_variants[stmpe->partnum];
  261. ret = stmpe_keypad_chip_init(keypad);
  262. if (ret < 0)
  263. goto out_freeinput;
  264. ret = input_register_device(input);
  265. if (ret) {
  266. dev_err(&pdev->dev,
  267. "unable to register input device: %d\n", ret);
  268. goto out_freeinput;
  269. }
  270. ret = request_threaded_irq(irq, NULL, stmpe_keypad_irq, IRQF_ONESHOT,
  271. "stmpe-keypad", keypad);
  272. if (ret) {
  273. dev_err(&pdev->dev, "unable to get irq: %d\n", ret);
  274. goto out_unregisterinput;
  275. }
  276. platform_set_drvdata(pdev, keypad);
  277. return 0;
  278. out_unregisterinput:
  279. input_unregister_device(input);
  280. input = NULL;
  281. out_freeinput:
  282. input_free_device(input);
  283. out_freekeypad:
  284. kfree(keypad);
  285. return ret;
  286. }
  287. static int __devexit stmpe_keypad_remove(struct platform_device *pdev)
  288. {
  289. struct stmpe_keypad *keypad = platform_get_drvdata(pdev);
  290. struct stmpe *stmpe = keypad->stmpe;
  291. int irq = platform_get_irq(pdev, 0);
  292. stmpe_disable(stmpe, STMPE_BLOCK_KEYPAD);
  293. free_irq(irq, keypad);
  294. input_unregister_device(keypad->input);
  295. platform_set_drvdata(pdev, NULL);
  296. kfree(keypad);
  297. return 0;
  298. }
  299. static struct platform_driver stmpe_keypad_driver = {
  300. .driver.name = "stmpe-keypad",
  301. .driver.owner = THIS_MODULE,
  302. .probe = stmpe_keypad_probe,
  303. .remove = __devexit_p(stmpe_keypad_remove),
  304. };
  305. module_platform_driver(stmpe_keypad_driver);
  306. MODULE_LICENSE("GPL v2");
  307. MODULE_DESCRIPTION("STMPExxxx keypad driver");
  308. MODULE_AUTHOR("Rabin Vincent <rabin.vincent@stericsson.com>");