nspire-keypad.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2013 Daniel Tang <tangrs@tangrs.id.au>
  4. */
  5. #include <linux/input/matrix_keypad.h>
  6. #include <linux/platform_device.h>
  7. #include <linux/interrupt.h>
  8. #include <linux/io.h>
  9. #include <linux/delay.h>
  10. #include <linux/input.h>
  11. #include <linux/slab.h>
  12. #include <linux/clk.h>
  13. #include <linux/module.h>
  14. #include <linux/of.h>
  15. #define KEYPAD_SCAN_MODE 0x00
  16. #define KEYPAD_CNTL 0x04
  17. #define KEYPAD_INT 0x08
  18. #define KEYPAD_INTMSK 0x0C
  19. #define KEYPAD_DATA 0x10
  20. #define KEYPAD_GPIO 0x30
  21. #define KEYPAD_UNKNOWN_INT 0x40
  22. #define KEYPAD_UNKNOWN_INT_STS 0x44
  23. #define KEYPAD_BITMASK_COLS 11
  24. #define KEYPAD_BITMASK_ROWS 8
  25. struct nspire_keypad {
  26. void __iomem *reg_base;
  27. u32 int_mask;
  28. struct input_dev *input;
  29. struct clk *clk;
  30. struct matrix_keymap_data *keymap;
  31. int row_shift;
  32. /* Maximum delay estimated assuming 33MHz APB */
  33. u32 scan_interval; /* In microseconds (~2000us max) */
  34. u32 row_delay; /* In microseconds (~500us max) */
  35. u16 state[KEYPAD_BITMASK_ROWS];
  36. bool active_low;
  37. };
  38. static irqreturn_t nspire_keypad_irq(int irq, void *dev_id)
  39. {
  40. struct nspire_keypad *keypad = dev_id;
  41. struct input_dev *input = keypad->input;
  42. unsigned short *keymap = input->keycode;
  43. unsigned int code;
  44. int row, col;
  45. u32 int_sts;
  46. u16 state[8];
  47. u16 bits, changed;
  48. int_sts = readl(keypad->reg_base + KEYPAD_INT) & keypad->int_mask;
  49. if (!int_sts)
  50. return IRQ_NONE;
  51. memcpy_fromio(state, keypad->reg_base + KEYPAD_DATA, sizeof(state));
  52. for (row = 0; row < KEYPAD_BITMASK_ROWS; row++) {
  53. bits = state[row];
  54. if (keypad->active_low)
  55. bits = ~bits;
  56. changed = bits ^ keypad->state[row];
  57. if (!changed)
  58. continue;
  59. keypad->state[row] = bits;
  60. for (col = 0; col < KEYPAD_BITMASK_COLS; col++) {
  61. if (!(changed & (1U << col)))
  62. continue;
  63. code = MATRIX_SCAN_CODE(row, col, keypad->row_shift);
  64. input_event(input, EV_MSC, MSC_SCAN, code);
  65. input_report_key(input, keymap[code],
  66. bits & (1U << col));
  67. }
  68. }
  69. input_sync(input);
  70. writel(0x3, keypad->reg_base + KEYPAD_INT);
  71. return IRQ_HANDLED;
  72. }
  73. static int nspire_keypad_open(struct input_dev *input)
  74. {
  75. struct nspire_keypad *keypad = input_get_drvdata(input);
  76. unsigned long val = 0, cycles_per_us, delay_cycles, row_delay_cycles;
  77. int error;
  78. error = clk_prepare_enable(keypad->clk);
  79. if (error)
  80. return error;
  81. cycles_per_us = (clk_get_rate(keypad->clk) / 1000000);
  82. if (cycles_per_us == 0)
  83. cycles_per_us = 1;
  84. delay_cycles = cycles_per_us * keypad->scan_interval;
  85. WARN_ON(delay_cycles >= (1 << 16)); /* Overflow */
  86. delay_cycles &= 0xffff;
  87. row_delay_cycles = cycles_per_us * keypad->row_delay;
  88. WARN_ON(row_delay_cycles >= (1 << 14)); /* Overflow */
  89. row_delay_cycles &= 0x3fff;
  90. val |= 3 << 0; /* Set scan mode to 3 (continuous scan) */
  91. val |= row_delay_cycles << 2; /* Delay between scanning each row */
  92. val |= delay_cycles << 16; /* Delay between scans */
  93. writel(val, keypad->reg_base + KEYPAD_SCAN_MODE);
  94. val = (KEYPAD_BITMASK_ROWS & 0xff) | (KEYPAD_BITMASK_COLS & 0xff)<<8;
  95. writel(val, keypad->reg_base + KEYPAD_CNTL);
  96. /* Enable interrupts */
  97. keypad->int_mask = 1 << 1;
  98. writel(keypad->int_mask, keypad->reg_base + KEYPAD_INTMSK);
  99. return 0;
  100. }
  101. static void nspire_keypad_close(struct input_dev *input)
  102. {
  103. struct nspire_keypad *keypad = input_get_drvdata(input);
  104. /* Disable interrupts */
  105. writel(0, keypad->reg_base + KEYPAD_INTMSK);
  106. /* Acknowledge existing interrupts */
  107. writel(~0, keypad->reg_base + KEYPAD_INT);
  108. clk_disable_unprepare(keypad->clk);
  109. }
  110. static int nspire_keypad_probe(struct platform_device *pdev)
  111. {
  112. const struct device_node *of_node = pdev->dev.of_node;
  113. struct nspire_keypad *keypad;
  114. struct input_dev *input;
  115. struct resource *res;
  116. int irq;
  117. int error;
  118. irq = platform_get_irq(pdev, 0);
  119. if (irq < 0)
  120. return -EINVAL;
  121. keypad = devm_kzalloc(&pdev->dev, sizeof(struct nspire_keypad),
  122. GFP_KERNEL);
  123. if (!keypad) {
  124. dev_err(&pdev->dev, "failed to allocate keypad memory\n");
  125. return -ENOMEM;
  126. }
  127. keypad->row_shift = get_count_order(KEYPAD_BITMASK_COLS);
  128. error = of_property_read_u32(of_node, "scan-interval",
  129. &keypad->scan_interval);
  130. if (error) {
  131. dev_err(&pdev->dev, "failed to get scan-interval\n");
  132. return error;
  133. }
  134. error = of_property_read_u32(of_node, "row-delay",
  135. &keypad->row_delay);
  136. if (error) {
  137. dev_err(&pdev->dev, "failed to get row-delay\n");
  138. return error;
  139. }
  140. keypad->active_low = of_property_read_bool(of_node, "active-low");
  141. keypad->clk = devm_clk_get(&pdev->dev, NULL);
  142. if (IS_ERR(keypad->clk)) {
  143. dev_err(&pdev->dev, "unable to get clock\n");
  144. return PTR_ERR(keypad->clk);
  145. }
  146. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  147. keypad->reg_base = devm_ioremap_resource(&pdev->dev, res);
  148. if (IS_ERR(keypad->reg_base))
  149. return PTR_ERR(keypad->reg_base);
  150. keypad->input = input = devm_input_allocate_device(&pdev->dev);
  151. if (!input) {
  152. dev_err(&pdev->dev, "failed to allocate input device\n");
  153. return -ENOMEM;
  154. }
  155. error = clk_prepare_enable(keypad->clk);
  156. if (error) {
  157. dev_err(&pdev->dev, "failed to enable clock\n");
  158. return error;
  159. }
  160. /* Disable interrupts */
  161. writel(0, keypad->reg_base + KEYPAD_INTMSK);
  162. /* Acknowledge existing interrupts */
  163. writel(~0, keypad->reg_base + KEYPAD_INT);
  164. /* Disable GPIO interrupts to prevent hanging on touchpad */
  165. /* Possibly used to detect touchpad events */
  166. writel(0, keypad->reg_base + KEYPAD_UNKNOWN_INT);
  167. /* Acknowledge existing GPIO interrupts */
  168. writel(~0, keypad->reg_base + KEYPAD_UNKNOWN_INT_STS);
  169. clk_disable_unprepare(keypad->clk);
  170. input_set_drvdata(input, keypad);
  171. input->id.bustype = BUS_HOST;
  172. input->name = "nspire-keypad";
  173. input->open = nspire_keypad_open;
  174. input->close = nspire_keypad_close;
  175. __set_bit(EV_KEY, input->evbit);
  176. __set_bit(EV_REP, input->evbit);
  177. input_set_capability(input, EV_MSC, MSC_SCAN);
  178. error = matrix_keypad_build_keymap(NULL, NULL,
  179. KEYPAD_BITMASK_ROWS,
  180. KEYPAD_BITMASK_COLS,
  181. NULL, input);
  182. if (error) {
  183. dev_err(&pdev->dev, "building keymap failed\n");
  184. return error;
  185. }
  186. error = devm_request_irq(&pdev->dev, irq, nspire_keypad_irq, 0,
  187. "nspire_keypad", keypad);
  188. if (error) {
  189. dev_err(&pdev->dev, "allocate irq %d failed\n", irq);
  190. return error;
  191. }
  192. error = input_register_device(input);
  193. if (error) {
  194. dev_err(&pdev->dev,
  195. "unable to register input device: %d\n", error);
  196. return error;
  197. }
  198. dev_dbg(&pdev->dev,
  199. "TI-NSPIRE keypad at %pR (scan_interval=%uus, row_delay=%uus%s)\n",
  200. res, keypad->row_delay, keypad->scan_interval,
  201. keypad->active_low ? ", active_low" : "");
  202. return 0;
  203. }
  204. static const struct of_device_id nspire_keypad_dt_match[] = {
  205. { .compatible = "ti,nspire-keypad" },
  206. { },
  207. };
  208. MODULE_DEVICE_TABLE(of, nspire_keypad_dt_match);
  209. static struct platform_driver nspire_keypad_driver = {
  210. .driver = {
  211. .name = "nspire-keypad",
  212. .of_match_table = nspire_keypad_dt_match,
  213. },
  214. .probe = nspire_keypad_probe,
  215. };
  216. module_platform_driver(nspire_keypad_driver);
  217. MODULE_LICENSE("GPL");
  218. MODULE_DESCRIPTION("TI-NSPIRE Keypad Driver");