omap4-keypad.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  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/slab.h>
  31. #include <linux/pm_runtime.h>
  32. #include <linux/platform_data/omap4-keypad.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. #define OMAP4_VAL_DEBOUNCINGTIME 0x7
  60. #define OMAP4_VAL_PVT 0x7
  61. enum {
  62. KBD_REVISION_OMAP4 = 0,
  63. KBD_REVISION_OMAP5,
  64. };
  65. struct omap4_keypad {
  66. struct input_dev *input;
  67. void __iomem *base;
  68. bool irq_wake_enabled;
  69. unsigned int irq;
  70. unsigned int rows;
  71. unsigned int cols;
  72. u32 reg_offset;
  73. u32 irqreg_offset;
  74. unsigned int row_shift;
  75. bool no_autorepeat;
  76. unsigned char key_state[8];
  77. unsigned short *keymap;
  78. };
  79. static int kbd_readl(struct omap4_keypad *keypad_data, u32 offset)
  80. {
  81. return __raw_readl(keypad_data->base +
  82. keypad_data->reg_offset + offset);
  83. }
  84. static void kbd_writel(struct omap4_keypad *keypad_data, u32 offset, u32 value)
  85. {
  86. __raw_writel(value,
  87. keypad_data->base + keypad_data->reg_offset + offset);
  88. }
  89. static int kbd_read_irqreg(struct omap4_keypad *keypad_data, u32 offset)
  90. {
  91. return __raw_readl(keypad_data->base +
  92. keypad_data->irqreg_offset + offset);
  93. }
  94. static void kbd_write_irqreg(struct omap4_keypad *keypad_data,
  95. u32 offset, u32 value)
  96. {
  97. __raw_writel(value,
  98. keypad_data->base + keypad_data->irqreg_offset + offset);
  99. }
  100. /* Interrupt handlers */
  101. static irqreturn_t omap4_keypad_irq_handler(int irq, void *dev_id)
  102. {
  103. struct omap4_keypad *keypad_data = dev_id;
  104. if (kbd_read_irqreg(keypad_data, OMAP4_KBD_IRQSTATUS)) {
  105. /* Disable interrupts */
  106. kbd_write_irqreg(keypad_data, OMAP4_KBD_IRQENABLE,
  107. OMAP4_VAL_IRQDISABLE);
  108. return IRQ_WAKE_THREAD;
  109. }
  110. return IRQ_NONE;
  111. }
  112. static irqreturn_t omap4_keypad_irq_thread_fn(int irq, void *dev_id)
  113. {
  114. struct omap4_keypad *keypad_data = dev_id;
  115. struct input_dev *input_dev = keypad_data->input;
  116. unsigned char key_state[ARRAY_SIZE(keypad_data->key_state)];
  117. unsigned int col, row, code, changed;
  118. u32 *new_state = (u32 *) key_state;
  119. *new_state = kbd_readl(keypad_data, OMAP4_KBD_FULLCODE31_0);
  120. *(new_state + 1) = kbd_readl(keypad_data, OMAP4_KBD_FULLCODE63_32);
  121. for (row = 0; row < keypad_data->rows; row++) {
  122. changed = key_state[row] ^ keypad_data->key_state[row];
  123. if (!changed)
  124. continue;
  125. for (col = 0; col < keypad_data->cols; col++) {
  126. if (changed & (1 << col)) {
  127. code = MATRIX_SCAN_CODE(row, col,
  128. keypad_data->row_shift);
  129. input_event(input_dev, EV_MSC, MSC_SCAN, code);
  130. input_report_key(input_dev,
  131. keypad_data->keymap[code],
  132. key_state[row] & (1 << col));
  133. }
  134. }
  135. }
  136. input_sync(input_dev);
  137. memcpy(keypad_data->key_state, key_state,
  138. sizeof(keypad_data->key_state));
  139. /* clear pending interrupts */
  140. kbd_write_irqreg(keypad_data, OMAP4_KBD_IRQSTATUS,
  141. kbd_read_irqreg(keypad_data, OMAP4_KBD_IRQSTATUS));
  142. /* enable interrupts */
  143. kbd_write_irqreg(keypad_data, OMAP4_KBD_IRQENABLE,
  144. OMAP4_DEF_IRQENABLE_EVENTEN |
  145. OMAP4_DEF_IRQENABLE_LONGKEY);
  146. return IRQ_HANDLED;
  147. }
  148. static int omap4_keypad_open(struct input_dev *input)
  149. {
  150. struct omap4_keypad *keypad_data = input_get_drvdata(input);
  151. pm_runtime_get_sync(input->dev.parent);
  152. disable_irq(keypad_data->irq);
  153. kbd_writel(keypad_data, OMAP4_KBD_CTRL,
  154. OMAP4_DEF_CTRL_NOSOFTMODE |
  155. (OMAP4_VAL_PVT << OMAP4_DEF_CTRL_PTV_SHIFT));
  156. kbd_writel(keypad_data, OMAP4_KBD_DEBOUNCINGTIME,
  157. OMAP4_VAL_DEBOUNCINGTIME);
  158. /* clear pending interrupts */
  159. kbd_write_irqreg(keypad_data, OMAP4_KBD_IRQSTATUS,
  160. kbd_read_irqreg(keypad_data, OMAP4_KBD_IRQSTATUS));
  161. kbd_write_irqreg(keypad_data, OMAP4_KBD_IRQENABLE,
  162. OMAP4_DEF_IRQENABLE_EVENTEN |
  163. OMAP4_DEF_IRQENABLE_LONGKEY);
  164. kbd_writel(keypad_data, OMAP4_KBD_WAKEUPENABLE,
  165. OMAP4_DEF_WUP_EVENT_ENA | OMAP4_DEF_WUP_LONG_KEY_ENA);
  166. enable_irq(keypad_data->irq);
  167. return 0;
  168. }
  169. static void omap4_keypad_close(struct input_dev *input)
  170. {
  171. struct omap4_keypad *keypad_data = input_get_drvdata(input);
  172. disable_irq(keypad_data->irq);
  173. /* Disable interrupts */
  174. kbd_write_irqreg(keypad_data, OMAP4_KBD_IRQENABLE,
  175. OMAP4_VAL_IRQDISABLE);
  176. /* clear pending interrupts */
  177. kbd_write_irqreg(keypad_data, OMAP4_KBD_IRQSTATUS,
  178. kbd_read_irqreg(keypad_data, OMAP4_KBD_IRQSTATUS));
  179. enable_irq(keypad_data->irq);
  180. pm_runtime_put_sync(input->dev.parent);
  181. }
  182. #ifdef CONFIG_OF
  183. static int omap4_keypad_parse_dt(struct device *dev,
  184. struct omap4_keypad *keypad_data)
  185. {
  186. struct device_node *np = dev->of_node;
  187. int err;
  188. err = matrix_keypad_parse_of_params(dev, &keypad_data->rows,
  189. &keypad_data->cols);
  190. if (err)
  191. return err;
  192. if (of_get_property(np, "linux,input-no-autorepeat", NULL))
  193. keypad_data->no_autorepeat = true;
  194. return 0;
  195. }
  196. #else
  197. static inline int omap4_keypad_parse_dt(struct device *dev,
  198. struct omap4_keypad *keypad_data)
  199. {
  200. return -ENOSYS;
  201. }
  202. #endif
  203. static int omap4_keypad_probe(struct platform_device *pdev)
  204. {
  205. const struct omap4_keypad_platform_data *pdata =
  206. dev_get_platdata(&pdev->dev);
  207. const struct matrix_keymap_data *keymap_data =
  208. pdata ? pdata->keymap_data : NULL;
  209. struct omap4_keypad *keypad_data;
  210. struct input_dev *input_dev;
  211. struct resource *res;
  212. unsigned int max_keys;
  213. int rev;
  214. int irq;
  215. int error;
  216. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  217. if (!res) {
  218. dev_err(&pdev->dev, "no base address specified\n");
  219. return -EINVAL;
  220. }
  221. irq = platform_get_irq(pdev, 0);
  222. if (!irq) {
  223. dev_err(&pdev->dev, "no keyboard irq assigned\n");
  224. return -EINVAL;
  225. }
  226. keypad_data = kzalloc(sizeof(struct omap4_keypad), GFP_KERNEL);
  227. if (!keypad_data) {
  228. dev_err(&pdev->dev, "keypad_data memory allocation failed\n");
  229. return -ENOMEM;
  230. }
  231. keypad_data->irq = irq;
  232. if (pdata) {
  233. keypad_data->rows = pdata->rows;
  234. keypad_data->cols = pdata->cols;
  235. } else {
  236. error = omap4_keypad_parse_dt(&pdev->dev, keypad_data);
  237. if (error)
  238. return error;
  239. }
  240. res = request_mem_region(res->start, resource_size(res), pdev->name);
  241. if (!res) {
  242. dev_err(&pdev->dev, "can't request mem region\n");
  243. error = -EBUSY;
  244. goto err_free_keypad;
  245. }
  246. keypad_data->base = ioremap(res->start, resource_size(res));
  247. if (!keypad_data->base) {
  248. dev_err(&pdev->dev, "can't ioremap mem resource\n");
  249. error = -ENOMEM;
  250. goto err_release_mem;
  251. }
  252. /*
  253. * Enable clocks for the keypad module so that we can read
  254. * revision register.
  255. */
  256. pm_runtime_enable(&pdev->dev);
  257. error = pm_runtime_get_sync(&pdev->dev);
  258. if (error) {
  259. dev_err(&pdev->dev, "pm_runtime_get_sync() failed\n");
  260. goto err_unmap;
  261. }
  262. rev = __raw_readl(keypad_data->base + OMAP4_KBD_REVISION);
  263. rev &= 0x03 << 30;
  264. rev >>= 30;
  265. switch (rev) {
  266. case KBD_REVISION_OMAP4:
  267. keypad_data->reg_offset = 0x00;
  268. keypad_data->irqreg_offset = 0x00;
  269. break;
  270. case KBD_REVISION_OMAP5:
  271. keypad_data->reg_offset = 0x10;
  272. keypad_data->irqreg_offset = 0x0c;
  273. break;
  274. default:
  275. dev_err(&pdev->dev,
  276. "Keypad reports unsupported revision %d", rev);
  277. error = -EINVAL;
  278. goto err_pm_put_sync;
  279. }
  280. /* input device allocation */
  281. keypad_data->input = input_dev = input_allocate_device();
  282. if (!input_dev) {
  283. error = -ENOMEM;
  284. goto err_pm_put_sync;
  285. }
  286. input_dev->name = pdev->name;
  287. input_dev->dev.parent = &pdev->dev;
  288. input_dev->id.bustype = BUS_HOST;
  289. input_dev->id.vendor = 0x0001;
  290. input_dev->id.product = 0x0001;
  291. input_dev->id.version = 0x0001;
  292. input_dev->open = omap4_keypad_open;
  293. input_dev->close = omap4_keypad_close;
  294. input_set_capability(input_dev, EV_MSC, MSC_SCAN);
  295. if (!keypad_data->no_autorepeat)
  296. __set_bit(EV_REP, input_dev->evbit);
  297. input_set_drvdata(input_dev, keypad_data);
  298. keypad_data->row_shift = get_count_order(keypad_data->cols);
  299. max_keys = keypad_data->rows << keypad_data->row_shift;
  300. keypad_data->keymap = kzalloc(max_keys * sizeof(keypad_data->keymap[0]),
  301. GFP_KERNEL);
  302. if (!keypad_data->keymap) {
  303. dev_err(&pdev->dev, "Not enough memory for keymap\n");
  304. error = -ENOMEM;
  305. goto err_free_input;
  306. }
  307. error = matrix_keypad_build_keymap(keymap_data, NULL,
  308. keypad_data->rows, keypad_data->cols,
  309. keypad_data->keymap, input_dev);
  310. if (error) {
  311. dev_err(&pdev->dev, "failed to build keymap\n");
  312. goto err_free_keymap;
  313. }
  314. error = request_threaded_irq(keypad_data->irq, omap4_keypad_irq_handler,
  315. omap4_keypad_irq_thread_fn, 0,
  316. "omap4-keypad", keypad_data);
  317. if (error) {
  318. dev_err(&pdev->dev, "failed to register interrupt\n");
  319. goto err_free_input;
  320. }
  321. device_init_wakeup(&pdev->dev, true);
  322. pm_runtime_put_sync(&pdev->dev);
  323. error = input_register_device(keypad_data->input);
  324. if (error < 0) {
  325. dev_err(&pdev->dev, "failed to register input device\n");
  326. goto err_pm_disable;
  327. }
  328. platform_set_drvdata(pdev, keypad_data);
  329. return 0;
  330. err_pm_disable:
  331. pm_runtime_disable(&pdev->dev);
  332. device_init_wakeup(&pdev->dev, false);
  333. free_irq(keypad_data->irq, keypad_data);
  334. err_free_keymap:
  335. kfree(keypad_data->keymap);
  336. err_free_input:
  337. input_free_device(input_dev);
  338. err_pm_put_sync:
  339. pm_runtime_put_sync(&pdev->dev);
  340. err_unmap:
  341. iounmap(keypad_data->base);
  342. err_release_mem:
  343. release_mem_region(res->start, resource_size(res));
  344. err_free_keypad:
  345. kfree(keypad_data);
  346. return error;
  347. }
  348. static int omap4_keypad_remove(struct platform_device *pdev)
  349. {
  350. struct omap4_keypad *keypad_data = platform_get_drvdata(pdev);
  351. struct resource *res;
  352. free_irq(keypad_data->irq, keypad_data);
  353. pm_runtime_disable(&pdev->dev);
  354. device_init_wakeup(&pdev->dev, false);
  355. input_unregister_device(keypad_data->input);
  356. iounmap(keypad_data->base);
  357. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  358. release_mem_region(res->start, resource_size(res));
  359. kfree(keypad_data->keymap);
  360. kfree(keypad_data);
  361. return 0;
  362. }
  363. #ifdef CONFIG_OF
  364. static const struct of_device_id omap_keypad_dt_match[] = {
  365. { .compatible = "ti,omap4-keypad" },
  366. {},
  367. };
  368. MODULE_DEVICE_TABLE(of, omap_keypad_dt_match);
  369. #endif
  370. #ifdef CONFIG_PM_SLEEP
  371. static int omap4_keypad_suspend(struct device *dev)
  372. {
  373. struct platform_device *pdev = to_platform_device(dev);
  374. struct omap4_keypad *keypad_data = platform_get_drvdata(pdev);
  375. int error;
  376. if (device_may_wakeup(&pdev->dev)) {
  377. error = enable_irq_wake(keypad_data->irq);
  378. if (!error)
  379. keypad_data->irq_wake_enabled = true;
  380. }
  381. return 0;
  382. }
  383. static int omap4_keypad_resume(struct device *dev)
  384. {
  385. struct platform_device *pdev = to_platform_device(dev);
  386. struct omap4_keypad *keypad_data = platform_get_drvdata(pdev);
  387. if (device_may_wakeup(&pdev->dev) && keypad_data->irq_wake_enabled) {
  388. disable_irq_wake(keypad_data->irq);
  389. keypad_data->irq_wake_enabled = false;
  390. }
  391. return 0;
  392. }
  393. #endif
  394. static SIMPLE_DEV_PM_OPS(omap4_keypad_pm_ops,
  395. omap4_keypad_suspend, omap4_keypad_resume);
  396. static struct platform_driver omap4_keypad_driver = {
  397. .probe = omap4_keypad_probe,
  398. .remove = omap4_keypad_remove,
  399. .driver = {
  400. .name = "omap4-keypad",
  401. .owner = THIS_MODULE,
  402. .pm = &omap4_keypad_pm_ops,
  403. .of_match_table = of_match_ptr(omap_keypad_dt_match),
  404. },
  405. };
  406. module_platform_driver(omap4_keypad_driver);
  407. MODULE_AUTHOR("Texas Instruments");
  408. MODULE_DESCRIPTION("OMAP4 Keypad Driver");
  409. MODULE_LICENSE("GPL");
  410. MODULE_ALIAS("platform:omap4-keypad");