nspire-keypad.c 6.8 KB

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