tnetv107x-keypad.c 8.1 KB

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