tnetv107x-keypad.c 8.3 KB

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