omap4-keypad.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. /*
  2. * OMAP4 Keypad Driver
  3. *
  4. * Copyright (C) 2010 Texas Instruments
  5. *
  6. * Author: Abraham Arce <x0066660@ti.com>
  7. * Initial Code: Syed Rafiuddin <rafiuddin.syed@ti.com>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  22. */
  23. #include <linux/module.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/platform_device.h>
  26. #include <linux/errno.h>
  27. #include <linux/io.h>
  28. #include <linux/of.h>
  29. #include <linux/input.h>
  30. #include <linux/input/matrix_keypad.h>
  31. #include <linux/slab.h>
  32. #include <linux/pm_runtime.h>
  33. /* OMAP4 registers */
  34. #define OMAP4_KBD_REVISION 0x00
  35. #define OMAP4_KBD_SYSCONFIG 0x10
  36. #define OMAP4_KBD_SYSSTATUS 0x14
  37. #define OMAP4_KBD_IRQSTATUS 0x18
  38. #define OMAP4_KBD_IRQENABLE 0x1C
  39. #define OMAP4_KBD_WAKEUPENABLE 0x20
  40. #define OMAP4_KBD_PENDING 0x24
  41. #define OMAP4_KBD_CTRL 0x28
  42. #define OMAP4_KBD_DEBOUNCINGTIME 0x2C
  43. #define OMAP4_KBD_LONGKEYTIME 0x30
  44. #define OMAP4_KBD_TIMEOUT 0x34
  45. #define OMAP4_KBD_STATEMACHINE 0x38
  46. #define OMAP4_KBD_ROWINPUTS 0x3C
  47. #define OMAP4_KBD_COLUMNOUTPUTS 0x40
  48. #define OMAP4_KBD_FULLCODE31_0 0x44
  49. #define OMAP4_KBD_FULLCODE63_32 0x48
  50. /* OMAP4 bit definitions */
  51. #define OMAP4_DEF_IRQENABLE_EVENTEN BIT(0)
  52. #define OMAP4_DEF_IRQENABLE_LONGKEY BIT(1)
  53. #define OMAP4_DEF_WUP_EVENT_ENA BIT(0)
  54. #define OMAP4_DEF_WUP_LONG_KEY_ENA BIT(1)
  55. #define OMAP4_DEF_CTRL_NOSOFTMODE BIT(1)
  56. #define OMAP4_DEF_CTRL_PTV_SHIFT 2
  57. /* OMAP4 values */
  58. #define OMAP4_VAL_IRQDISABLE 0x0
  59. /*
  60. * Errata i689: If a key is released for a time shorter than debounce time,
  61. * the keyboard will idle and never detect the key release. The workaround
  62. * is to use at least a 12ms debounce time. See omap5432 TRM chapter
  63. * "26.4.6.2 Keyboard Controller Timer" for more information.
  64. */
  65. #define OMAP4_KEYPAD_PTV_DIV_128 0x6
  66. #define OMAP4_KEYPAD_DEBOUNCINGTIME_MS(dbms, ptv) \
  67. ((((dbms) * 1000) / ((1 << ((ptv) + 1)) * (1000000 / 32768))) - 1)
  68. #define OMAP4_VAL_DEBOUNCINGTIME_16MS \
  69. OMAP4_KEYPAD_DEBOUNCINGTIME_MS(16, OMAP4_KEYPAD_PTV_DIV_128)
  70. enum {
  71. KBD_REVISION_OMAP4 = 0,
  72. KBD_REVISION_OMAP5,
  73. };
  74. struct omap4_keypad {
  75. struct input_dev *input;
  76. void __iomem *base;
  77. bool irq_wake_enabled;
  78. unsigned int irq;
  79. unsigned int rows;
  80. unsigned int cols;
  81. u32 reg_offset;
  82. u32 irqreg_offset;
  83. unsigned int row_shift;
  84. bool no_autorepeat;
  85. unsigned char key_state[8];
  86. unsigned short *keymap;
  87. };
  88. static int kbd_readl(struct omap4_keypad *keypad_data, u32 offset)
  89. {
  90. return __raw_readl(keypad_data->base +
  91. keypad_data->reg_offset + offset);
  92. }
  93. static void kbd_writel(struct omap4_keypad *keypad_data, u32 offset, u32 value)
  94. {
  95. __raw_writel(value,
  96. keypad_data->base + keypad_data->reg_offset + offset);
  97. }
  98. static int kbd_read_irqreg(struct omap4_keypad *keypad_data, u32 offset)
  99. {
  100. return __raw_readl(keypad_data->base +
  101. keypad_data->irqreg_offset + offset);
  102. }
  103. static void kbd_write_irqreg(struct omap4_keypad *keypad_data,
  104. u32 offset, u32 value)
  105. {
  106. __raw_writel(value,
  107. keypad_data->base + keypad_data->irqreg_offset + offset);
  108. }
  109. /* Interrupt handlers */
  110. static irqreturn_t omap4_keypad_irq_handler(int irq, void *dev_id)
  111. {
  112. struct omap4_keypad *keypad_data = dev_id;
  113. if (kbd_read_irqreg(keypad_data, OMAP4_KBD_IRQSTATUS))
  114. return IRQ_WAKE_THREAD;
  115. return IRQ_NONE;
  116. }
  117. static irqreturn_t omap4_keypad_irq_thread_fn(int irq, void *dev_id)
  118. {
  119. struct omap4_keypad *keypad_data = dev_id;
  120. struct input_dev *input_dev = keypad_data->input;
  121. unsigned char key_state[ARRAY_SIZE(keypad_data->key_state)];
  122. unsigned int col, row, code, changed;
  123. u32 *new_state = (u32 *) key_state;
  124. *new_state = kbd_readl(keypad_data, OMAP4_KBD_FULLCODE31_0);
  125. *(new_state + 1) = kbd_readl(keypad_data, OMAP4_KBD_FULLCODE63_32);
  126. for (row = 0; row < keypad_data->rows; row++) {
  127. changed = key_state[row] ^ keypad_data->key_state[row];
  128. if (!changed)
  129. continue;
  130. for (col = 0; col < keypad_data->cols; col++) {
  131. if (changed & (1 << col)) {
  132. code = MATRIX_SCAN_CODE(row, col,
  133. keypad_data->row_shift);
  134. input_event(input_dev, EV_MSC, MSC_SCAN, code);
  135. input_report_key(input_dev,
  136. keypad_data->keymap[code],
  137. key_state[row] & (1 << col));
  138. }
  139. }
  140. }
  141. input_sync(input_dev);
  142. memcpy(keypad_data->key_state, key_state,
  143. sizeof(keypad_data->key_state));
  144. /* clear pending interrupts */
  145. kbd_write_irqreg(keypad_data, OMAP4_KBD_IRQSTATUS,
  146. kbd_read_irqreg(keypad_data, OMAP4_KBD_IRQSTATUS));
  147. return IRQ_HANDLED;
  148. }
  149. static int omap4_keypad_open(struct input_dev *input)
  150. {
  151. struct omap4_keypad *keypad_data = input_get_drvdata(input);
  152. pm_runtime_get_sync(input->dev.parent);
  153. disable_irq(keypad_data->irq);
  154. kbd_writel(keypad_data, OMAP4_KBD_CTRL,
  155. OMAP4_DEF_CTRL_NOSOFTMODE |
  156. (OMAP4_KEYPAD_PTV_DIV_128 << OMAP4_DEF_CTRL_PTV_SHIFT));
  157. kbd_writel(keypad_data, OMAP4_KBD_DEBOUNCINGTIME,
  158. OMAP4_VAL_DEBOUNCINGTIME_16MS);
  159. /* clear pending interrupts */
  160. kbd_write_irqreg(keypad_data, OMAP4_KBD_IRQSTATUS,
  161. kbd_read_irqreg(keypad_data, OMAP4_KBD_IRQSTATUS));
  162. kbd_write_irqreg(keypad_data, OMAP4_KBD_IRQENABLE,
  163. OMAP4_DEF_IRQENABLE_EVENTEN |
  164. OMAP4_DEF_IRQENABLE_LONGKEY);
  165. kbd_writel(keypad_data, OMAP4_KBD_WAKEUPENABLE,
  166. OMAP4_DEF_WUP_EVENT_ENA | OMAP4_DEF_WUP_LONG_KEY_ENA);
  167. enable_irq(keypad_data->irq);
  168. return 0;
  169. }
  170. static void omap4_keypad_close(struct input_dev *input)
  171. {
  172. struct omap4_keypad *keypad_data = input_get_drvdata(input);
  173. disable_irq(keypad_data->irq);
  174. /* Disable interrupts and wake-up events */
  175. kbd_write_irqreg(keypad_data, OMAP4_KBD_IRQENABLE,
  176. OMAP4_VAL_IRQDISABLE);
  177. kbd_writel(keypad_data, OMAP4_KBD_WAKEUPENABLE, 0);
  178. /* clear pending interrupts */
  179. kbd_write_irqreg(keypad_data, OMAP4_KBD_IRQSTATUS,
  180. kbd_read_irqreg(keypad_data, OMAP4_KBD_IRQSTATUS));
  181. enable_irq(keypad_data->irq);
  182. pm_runtime_put_sync(input->dev.parent);
  183. }
  184. static int omap4_keypad_parse_dt(struct device *dev,
  185. struct omap4_keypad *keypad_data)
  186. {
  187. struct device_node *np = dev->of_node;
  188. int err;
  189. err = matrix_keypad_parse_of_params(dev, &keypad_data->rows,
  190. &keypad_data->cols);
  191. if (err)
  192. return err;
  193. if (of_get_property(np, "linux,input-no-autorepeat", NULL))
  194. keypad_data->no_autorepeat = true;
  195. return 0;
  196. }
  197. static int omap4_keypad_probe(struct platform_device *pdev)
  198. {
  199. struct omap4_keypad *keypad_data;
  200. struct input_dev *input_dev;
  201. struct resource *res;
  202. unsigned int max_keys;
  203. int rev;
  204. int irq;
  205. int error;
  206. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  207. if (!res) {
  208. dev_err(&pdev->dev, "no base address specified\n");
  209. return -EINVAL;
  210. }
  211. irq = platform_get_irq(pdev, 0);
  212. if (!irq) {
  213. dev_err(&pdev->dev, "no keyboard irq assigned\n");
  214. return -EINVAL;
  215. }
  216. keypad_data = kzalloc(sizeof(struct omap4_keypad), GFP_KERNEL);
  217. if (!keypad_data) {
  218. dev_err(&pdev->dev, "keypad_data memory allocation failed\n");
  219. return -ENOMEM;
  220. }
  221. keypad_data->irq = irq;
  222. error = omap4_keypad_parse_dt(&pdev->dev, keypad_data);
  223. if (error)
  224. goto err_free_keypad;
  225. res = request_mem_region(res->start, resource_size(res), pdev->name);
  226. if (!res) {
  227. dev_err(&pdev->dev, "can't request mem region\n");
  228. error = -EBUSY;
  229. goto err_free_keypad;
  230. }
  231. keypad_data->base = ioremap(res->start, resource_size(res));
  232. if (!keypad_data->base) {
  233. dev_err(&pdev->dev, "can't ioremap mem resource\n");
  234. error = -ENOMEM;
  235. goto err_release_mem;
  236. }
  237. /*
  238. * Enable clocks for the keypad module so that we can read
  239. * revision register.
  240. */
  241. pm_runtime_enable(&pdev->dev);
  242. error = pm_runtime_get_sync(&pdev->dev);
  243. if (error) {
  244. dev_err(&pdev->dev, "pm_runtime_get_sync() failed\n");
  245. goto err_unmap;
  246. }
  247. rev = __raw_readl(keypad_data->base + OMAP4_KBD_REVISION);
  248. rev &= 0x03 << 30;
  249. rev >>= 30;
  250. switch (rev) {
  251. case KBD_REVISION_OMAP4:
  252. keypad_data->reg_offset = 0x00;
  253. keypad_data->irqreg_offset = 0x00;
  254. break;
  255. case KBD_REVISION_OMAP5:
  256. keypad_data->reg_offset = 0x10;
  257. keypad_data->irqreg_offset = 0x0c;
  258. break;
  259. default:
  260. dev_err(&pdev->dev,
  261. "Keypad reports unsupported revision %d", rev);
  262. error = -EINVAL;
  263. goto err_pm_put_sync;
  264. }
  265. /* input device allocation */
  266. keypad_data->input = input_dev = input_allocate_device();
  267. if (!input_dev) {
  268. error = -ENOMEM;
  269. goto err_pm_put_sync;
  270. }
  271. input_dev->name = pdev->name;
  272. input_dev->dev.parent = &pdev->dev;
  273. input_dev->id.bustype = BUS_HOST;
  274. input_dev->id.vendor = 0x0001;
  275. input_dev->id.product = 0x0001;
  276. input_dev->id.version = 0x0001;
  277. input_dev->open = omap4_keypad_open;
  278. input_dev->close = omap4_keypad_close;
  279. input_set_capability(input_dev, EV_MSC, MSC_SCAN);
  280. if (!keypad_data->no_autorepeat)
  281. __set_bit(EV_REP, input_dev->evbit);
  282. input_set_drvdata(input_dev, keypad_data);
  283. keypad_data->row_shift = get_count_order(keypad_data->cols);
  284. max_keys = keypad_data->rows << keypad_data->row_shift;
  285. keypad_data->keymap = kzalloc(max_keys * sizeof(keypad_data->keymap[0]),
  286. GFP_KERNEL);
  287. if (!keypad_data->keymap) {
  288. dev_err(&pdev->dev, "Not enough memory for keymap\n");
  289. error = -ENOMEM;
  290. goto err_free_input;
  291. }
  292. error = matrix_keypad_build_keymap(NULL, NULL,
  293. keypad_data->rows, keypad_data->cols,
  294. keypad_data->keymap, input_dev);
  295. if (error) {
  296. dev_err(&pdev->dev, "failed to build keymap\n");
  297. goto err_free_keymap;
  298. }
  299. error = request_threaded_irq(keypad_data->irq, omap4_keypad_irq_handler,
  300. omap4_keypad_irq_thread_fn, IRQF_ONESHOT,
  301. "omap4-keypad", keypad_data);
  302. if (error) {
  303. dev_err(&pdev->dev, "failed to register interrupt\n");
  304. goto err_free_input;
  305. }
  306. device_init_wakeup(&pdev->dev, true);
  307. pm_runtime_put_sync(&pdev->dev);
  308. error = input_register_device(keypad_data->input);
  309. if (error < 0) {
  310. dev_err(&pdev->dev, "failed to register input device\n");
  311. goto err_pm_disable;
  312. }
  313. platform_set_drvdata(pdev, keypad_data);
  314. return 0;
  315. err_pm_disable:
  316. pm_runtime_disable(&pdev->dev);
  317. device_init_wakeup(&pdev->dev, false);
  318. free_irq(keypad_data->irq, keypad_data);
  319. err_free_keymap:
  320. kfree(keypad_data->keymap);
  321. err_free_input:
  322. input_free_device(input_dev);
  323. err_pm_put_sync:
  324. pm_runtime_put_sync(&pdev->dev);
  325. err_unmap:
  326. iounmap(keypad_data->base);
  327. err_release_mem:
  328. release_mem_region(res->start, resource_size(res));
  329. err_free_keypad:
  330. kfree(keypad_data);
  331. return error;
  332. }
  333. static int omap4_keypad_remove(struct platform_device *pdev)
  334. {
  335. struct omap4_keypad *keypad_data = platform_get_drvdata(pdev);
  336. struct resource *res;
  337. free_irq(keypad_data->irq, keypad_data);
  338. pm_runtime_disable(&pdev->dev);
  339. device_init_wakeup(&pdev->dev, false);
  340. input_unregister_device(keypad_data->input);
  341. iounmap(keypad_data->base);
  342. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  343. release_mem_region(res->start, resource_size(res));
  344. kfree(keypad_data->keymap);
  345. kfree(keypad_data);
  346. return 0;
  347. }
  348. static const struct of_device_id omap_keypad_dt_match[] = {
  349. { .compatible = "ti,omap4-keypad" },
  350. {},
  351. };
  352. MODULE_DEVICE_TABLE(of, omap_keypad_dt_match);
  353. #ifdef CONFIG_PM_SLEEP
  354. static int omap4_keypad_suspend(struct device *dev)
  355. {
  356. struct platform_device *pdev = to_platform_device(dev);
  357. struct omap4_keypad *keypad_data = platform_get_drvdata(pdev);
  358. int error;
  359. if (device_may_wakeup(&pdev->dev)) {
  360. error = enable_irq_wake(keypad_data->irq);
  361. if (!error)
  362. keypad_data->irq_wake_enabled = true;
  363. }
  364. return 0;
  365. }
  366. static int omap4_keypad_resume(struct device *dev)
  367. {
  368. struct platform_device *pdev = to_platform_device(dev);
  369. struct omap4_keypad *keypad_data = platform_get_drvdata(pdev);
  370. if (device_may_wakeup(&pdev->dev) && keypad_data->irq_wake_enabled) {
  371. disable_irq_wake(keypad_data->irq);
  372. keypad_data->irq_wake_enabled = false;
  373. }
  374. return 0;
  375. }
  376. #endif
  377. static SIMPLE_DEV_PM_OPS(omap4_keypad_pm_ops,
  378. omap4_keypad_suspend, omap4_keypad_resume);
  379. static struct platform_driver omap4_keypad_driver = {
  380. .probe = omap4_keypad_probe,
  381. .remove = omap4_keypad_remove,
  382. .driver = {
  383. .name = "omap4-keypad",
  384. .pm = &omap4_keypad_pm_ops,
  385. .of_match_table = omap_keypad_dt_match,
  386. },
  387. };
  388. module_platform_driver(omap4_keypad_driver);
  389. MODULE_AUTHOR("Texas Instruments");
  390. MODULE_DESCRIPTION("OMAP4 Keypad Driver");
  391. MODULE_LICENSE("GPL");
  392. MODULE_ALIAS("platform:omap4-keypad");