omap4-keypad.c 11 KB

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