tnetv107x-keypad.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. /*
  2. * Texas Instruments TNETV107X Keypad Driver
  3. *
  4. * Copyright (C) 2010 Texas Instruments
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation version 2.
  9. *
  10. * This program is distributed "as is" WITHOUT ANY WARRANTY of any
  11. * kind, whether express or implied; without even the implied warranty
  12. * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #include <linux/kernel.h>
  16. #include <linux/errno.h>
  17. #include <linux/input.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/slab.h>
  21. #include <linux/delay.h>
  22. #include <linux/io.h>
  23. #include <linux/clk.h>
  24. #include <linux/input/matrix_keypad.h>
  25. #define BITS(x) (BIT(x) - 1)
  26. #define KEYPAD_ROWS 9
  27. #define KEYPAD_COLS 9
  28. #define DEBOUNCE_MIN 0x400ul
  29. #define DEBOUNCE_MAX 0x3ffffffful
  30. struct keypad_regs {
  31. u32 rev;
  32. u32 mode;
  33. u32 mask;
  34. u32 pol;
  35. u32 dclock;
  36. u32 rclock;
  37. u32 stable_cnt;
  38. u32 in_en;
  39. u32 out;
  40. u32 out_en;
  41. u32 in;
  42. u32 lock;
  43. u32 pres[3];
  44. };
  45. #define keypad_read(kp, reg) __raw_readl(&(kp)->regs->reg)
  46. #define keypad_write(kp, reg, val) __raw_writel(val, &(kp)->regs->reg)
  47. struct keypad_data {
  48. struct input_dev *input_dev;
  49. struct resource *res;
  50. struct keypad_regs __iomem *regs;
  51. struct clk *clk;
  52. struct device *dev;
  53. spinlock_t lock;
  54. u32 irq_press;
  55. u32 irq_release;
  56. int rows, cols, row_shift;
  57. int debounce_ms, active_low;
  58. u32 prev_keys[3];
  59. unsigned short keycodes[];
  60. };
  61. static irqreturn_t keypad_irq(int irq, void *data)
  62. {
  63. struct keypad_data *kp = data;
  64. int i, bit, val, row, col, code;
  65. unsigned long flags;
  66. u32 curr_keys[3];
  67. u32 change;
  68. spin_lock_irqsave(&kp->lock, flags);
  69. memset(curr_keys, 0, sizeof(curr_keys));
  70. if (irq == kp->irq_press)
  71. for (i = 0; i < 3; i++)
  72. curr_keys[i] = keypad_read(kp, pres[i]);
  73. for (i = 0; i < 3; i++) {
  74. change = curr_keys[i] ^ kp->prev_keys[i];
  75. while (change) {
  76. bit = fls(change) - 1;
  77. change ^= BIT(bit);
  78. val = curr_keys[i] & BIT(bit);
  79. bit += i * 32;
  80. row = bit / KEYPAD_COLS;
  81. col = bit % KEYPAD_COLS;
  82. code = MATRIX_SCAN_CODE(row, col, kp->row_shift);
  83. input_event(kp->input_dev, EV_MSC, MSC_SCAN, code);
  84. input_report_key(kp->input_dev, kp->keycodes[code],
  85. val);
  86. }
  87. }
  88. input_sync(kp->input_dev);
  89. memcpy(kp->prev_keys, curr_keys, sizeof(curr_keys));
  90. if (irq == kp->irq_press)
  91. keypad_write(kp, lock, 0); /* Allow hardware updates */
  92. spin_unlock_irqrestore(&kp->lock, flags);
  93. return IRQ_HANDLED;
  94. }
  95. static int keypad_start(struct input_dev *dev)
  96. {
  97. struct keypad_data *kp = input_get_drvdata(dev);
  98. unsigned long mask, debounce, clk_rate_khz;
  99. unsigned long flags;
  100. clk_enable(kp->clk);
  101. clk_rate_khz = clk_get_rate(kp->clk) / 1000;
  102. spin_lock_irqsave(&kp->lock, flags);
  103. /* Initialize device registers */
  104. keypad_write(kp, mode, 0);
  105. mask = BITS(kp->rows) << KEYPAD_COLS;
  106. mask |= BITS(kp->cols);
  107. keypad_write(kp, mask, ~mask);
  108. keypad_write(kp, pol, kp->active_low ? 0 : 0x3ffff);
  109. keypad_write(kp, stable_cnt, 3);
  110. debounce = kp->debounce_ms * clk_rate_khz;
  111. debounce = clamp(debounce, DEBOUNCE_MIN, DEBOUNCE_MAX);
  112. keypad_write(kp, dclock, debounce);
  113. keypad_write(kp, rclock, 4 * debounce);
  114. keypad_write(kp, in_en, 1);
  115. spin_unlock_irqrestore(&kp->lock, flags);
  116. return 0;
  117. }
  118. static void keypad_stop(struct input_dev *dev)
  119. {
  120. struct keypad_data *kp = input_get_drvdata(dev);
  121. synchronize_irq(kp->irq_press);
  122. synchronize_irq(kp->irq_release);
  123. clk_disable(kp->clk);
  124. }
  125. static int __devinit keypad_probe(struct platform_device *pdev)
  126. {
  127. const struct matrix_keypad_platform_data *pdata;
  128. const struct matrix_keymap_data *keymap_data;
  129. struct device *dev = &pdev->dev;
  130. struct keypad_data *kp;
  131. int error = 0, sz, row_shift;
  132. u32 rev = 0;
  133. pdata = pdev->dev.platform_data;
  134. if (!pdata) {
  135. dev_err(dev, "cannot find device data\n");
  136. return -EINVAL;
  137. }
  138. keymap_data = pdata->keymap_data;
  139. if (!keymap_data) {
  140. dev_err(dev, "cannot find keymap data\n");
  141. return -EINVAL;
  142. }
  143. row_shift = get_count_order(pdata->num_col_gpios);
  144. sz = offsetof(struct keypad_data, keycodes);
  145. sz += (pdata->num_row_gpios << row_shift) * sizeof(kp->keycodes[0]);
  146. kp = kzalloc(sz, GFP_KERNEL);
  147. if (!kp) {
  148. dev_err(dev, "cannot allocate device info\n");
  149. return -ENOMEM;
  150. }
  151. kp->dev = dev;
  152. kp->rows = pdata->num_row_gpios;
  153. kp->cols = pdata->num_col_gpios;
  154. kp->row_shift = row_shift;
  155. platform_set_drvdata(pdev, kp);
  156. spin_lock_init(&kp->lock);
  157. kp->irq_press = platform_get_irq_byname(pdev, "press");
  158. kp->irq_release = platform_get_irq_byname(pdev, "release");
  159. if (kp->irq_press < 0 || kp->irq_release < 0) {
  160. dev_err(dev, "cannot determine device interrupts\n");
  161. error = -ENODEV;
  162. goto error_res;
  163. }
  164. kp->res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  165. if (!kp->res) {
  166. dev_err(dev, "cannot determine register area\n");
  167. error = -ENODEV;
  168. goto error_res;
  169. }
  170. if (!request_mem_region(kp->res->start, resource_size(kp->res),
  171. pdev->name)) {
  172. dev_err(dev, "cannot claim register memory\n");
  173. kp->res = NULL;
  174. error = -EINVAL;
  175. goto error_res;
  176. }
  177. kp->regs = ioremap(kp->res->start, resource_size(kp->res));
  178. if (!kp->regs) {
  179. dev_err(dev, "cannot map register memory\n");
  180. error = -ENOMEM;
  181. goto error_map;
  182. }
  183. kp->clk = clk_get(dev, NULL);
  184. if (!kp->clk) {
  185. dev_err(dev, "cannot claim device clock\n");
  186. error = -EINVAL;
  187. goto error_clk;
  188. }
  189. error = request_threaded_irq(kp->irq_press, NULL, keypad_irq, 0,
  190. dev_name(dev), kp);
  191. if (error < 0) {
  192. dev_err(kp->dev, "Could not allocate keypad press key irq\n");
  193. goto error_irq_press;
  194. }
  195. error = request_threaded_irq(kp->irq_release, NULL, keypad_irq, 0,
  196. dev_name(dev), kp);
  197. if (error < 0) {
  198. dev_err(kp->dev, "Could not allocate keypad release key irq\n");
  199. goto error_irq_release;
  200. }
  201. kp->input_dev = input_allocate_device();
  202. if (!kp->input_dev) {
  203. dev_err(dev, "cannot allocate input device\n");
  204. error = -ENOMEM;
  205. goto error_input;
  206. }
  207. input_set_drvdata(kp->input_dev, kp);
  208. kp->input_dev->name = pdev->name;
  209. kp->input_dev->dev.parent = &pdev->dev;
  210. kp->input_dev->open = keypad_start;
  211. kp->input_dev->close = keypad_stop;
  212. kp->input_dev->evbit[0] = BIT_MASK(EV_KEY);
  213. if (!pdata->no_autorepeat)
  214. kp->input_dev->evbit[0] |= BIT_MASK(EV_REP);
  215. clk_enable(kp->clk);
  216. rev = keypad_read(kp, rev);
  217. kp->input_dev->id.bustype = BUS_HOST;
  218. kp->input_dev->id.product = ((rev >> 8) & 0x07);
  219. kp->input_dev->id.version = ((rev >> 16) & 0xfff);
  220. clk_disable(kp->clk);
  221. kp->input_dev->keycode = kp->keycodes;
  222. kp->input_dev->keycodesize = sizeof(kp->keycodes[0]);
  223. kp->input_dev->keycodemax = kp->rows << kp->row_shift;
  224. matrix_keypad_build_keymap(keymap_data, kp->row_shift, kp->keycodes,
  225. kp->input_dev->keybit);
  226. input_set_capability(kp->input_dev, EV_MSC, MSC_SCAN);
  227. error = input_register_device(kp->input_dev);
  228. if (error < 0) {
  229. dev_err(dev, "Could not register input device\n");
  230. goto error_reg;
  231. }
  232. return 0;
  233. error_reg:
  234. input_free_device(kp->input_dev);
  235. error_input:
  236. free_irq(kp->irq_release, kp);
  237. error_irq_release:
  238. free_irq(kp->irq_press, kp);
  239. error_irq_press:
  240. clk_put(kp->clk);
  241. error_clk:
  242. iounmap(kp->regs);
  243. error_map:
  244. release_mem_region(kp->res->start, resource_size(kp->res));
  245. error_res:
  246. platform_set_drvdata(pdev, NULL);
  247. kfree(kp);
  248. return error;
  249. }
  250. static int __devexit keypad_remove(struct platform_device *pdev)
  251. {
  252. struct keypad_data *kp = platform_get_drvdata(pdev);
  253. free_irq(kp->irq_press, kp);
  254. free_irq(kp->irq_release, kp);
  255. input_unregister_device(kp->input_dev);
  256. clk_put(kp->clk);
  257. iounmap(kp->regs);
  258. release_mem_region(kp->res->start, resource_size(kp->res));
  259. platform_set_drvdata(pdev, NULL);
  260. kfree(kp);
  261. return 0;
  262. }
  263. static struct platform_driver keypad_driver = {
  264. .probe = keypad_probe,
  265. .remove = __devexit_p(keypad_remove),
  266. .driver.name = "tnetv107x-keypad",
  267. .driver.owner = THIS_MODULE,
  268. };
  269. static int __init keypad_init(void)
  270. {
  271. return platform_driver_register(&keypad_driver);
  272. }
  273. static void __exit keypad_exit(void)
  274. {
  275. platform_driver_unregister(&keypad_driver);
  276. }
  277. module_init(keypad_init);
  278. module_exit(keypad_exit);
  279. MODULE_AUTHOR("Cyril Chemparathy");
  280. MODULE_DESCRIPTION("TNETV107X Keypad Driver");
  281. MODULE_ALIAS("platform: tnetv107x-keypad");
  282. MODULE_LICENSE("GPL");