lpc32xx-keys.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. /*
  2. * NXP LPC32xx SoC Key Scan Interface
  3. *
  4. * Authors:
  5. * Kevin Wells <kevin.wells@nxp.com>
  6. * Roland Stigge <stigge@antcom.de>
  7. *
  8. * Copyright (C) 2010 NXP Semiconductors
  9. * Copyright (C) 2012 Roland Stigge
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. *
  22. * This controller supports square key matrices from 1x1 up to 8x8
  23. */
  24. #include <linux/module.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/slab.h>
  27. #include <linux/irq.h>
  28. #include <linux/pm.h>
  29. #include <linux/platform_device.h>
  30. #include <linux/input.h>
  31. #include <linux/clk.h>
  32. #include <linux/io.h>
  33. #include <linux/of.h>
  34. #include <linux/input/matrix_keypad.h>
  35. #define DRV_NAME "lpc32xx_keys"
  36. /*
  37. * Key scanner register offsets
  38. */
  39. #define LPC32XX_KS_DEB(x) ((x) + 0x00)
  40. #define LPC32XX_KS_STATE_COND(x) ((x) + 0x04)
  41. #define LPC32XX_KS_IRQ(x) ((x) + 0x08)
  42. #define LPC32XX_KS_SCAN_CTL(x) ((x) + 0x0C)
  43. #define LPC32XX_KS_FAST_TST(x) ((x) + 0x10)
  44. #define LPC32XX_KS_MATRIX_DIM(x) ((x) + 0x14) /* 1..8 */
  45. #define LPC32XX_KS_DATA(x, y) ((x) + 0x40 + ((y) << 2))
  46. #define LPC32XX_KSCAN_DEB_NUM_DEB_PASS(n) ((n) & 0xFF)
  47. #define LPC32XX_KSCAN_SCOND_IN_IDLE 0x0
  48. #define LPC32XX_KSCAN_SCOND_IN_SCANONCE 0x1
  49. #define LPC32XX_KSCAN_SCOND_IN_IRQGEN 0x2
  50. #define LPC32XX_KSCAN_SCOND_IN_SCAN_MATRIX 0x3
  51. #define LPC32XX_KSCAN_IRQ_PENDING_CLR 0x1
  52. #define LPC32XX_KSCAN_SCTRL_SCAN_DELAY(n) ((n) & 0xFF)
  53. #define LPC32XX_KSCAN_FTST_FORCESCANONCE 0x1
  54. #define LPC32XX_KSCAN_FTST_USE32K_CLK 0x2
  55. #define LPC32XX_KSCAN_MSEL_SELECT(n) ((n) & 0xF)
  56. struct lpc32xx_kscan_drv {
  57. struct input_dev *input;
  58. struct clk *clk;
  59. struct resource *iores;
  60. void __iomem *kscan_base;
  61. unsigned int irq;
  62. u32 matrix_sz; /* Size of matrix in XxY, ie. 3 = 3x3 */
  63. u32 deb_clks; /* Debounce clocks (based on 32KHz clock) */
  64. u32 scan_delay; /* Scan delay (based on 32KHz clock) */
  65. unsigned short *keymap; /* Pointer to key map for the scan matrix */
  66. unsigned int row_shift;
  67. u8 lastkeystates[8];
  68. };
  69. static void lpc32xx_mod_states(struct lpc32xx_kscan_drv *kscandat, int col)
  70. {
  71. struct input_dev *input = kscandat->input;
  72. unsigned row, changed, scancode, keycode;
  73. u8 key;
  74. key = readl(LPC32XX_KS_DATA(kscandat->kscan_base, col));
  75. changed = key ^ kscandat->lastkeystates[col];
  76. kscandat->lastkeystates[col] = key;
  77. for (row = 0; changed; row++, changed >>= 1) {
  78. if (changed & 1) {
  79. /* Key state changed, signal an event */
  80. scancode = MATRIX_SCAN_CODE(row, col,
  81. kscandat->row_shift);
  82. keycode = kscandat->keymap[scancode];
  83. input_event(input, EV_MSC, MSC_SCAN, scancode);
  84. input_report_key(input, keycode, key & (1 << row));
  85. }
  86. }
  87. }
  88. static irqreturn_t lpc32xx_kscan_irq(int irq, void *dev_id)
  89. {
  90. struct lpc32xx_kscan_drv *kscandat = dev_id;
  91. int i;
  92. for (i = 0; i < kscandat->matrix_sz; i++)
  93. lpc32xx_mod_states(kscandat, i);
  94. writel(1, LPC32XX_KS_IRQ(kscandat->kscan_base));
  95. input_sync(kscandat->input);
  96. return IRQ_HANDLED;
  97. }
  98. static int lpc32xx_kscan_open(struct input_dev *dev)
  99. {
  100. struct lpc32xx_kscan_drv *kscandat = input_get_drvdata(dev);
  101. int error;
  102. error = clk_prepare_enable(kscandat->clk);
  103. if (error)
  104. return error;
  105. writel(1, LPC32XX_KS_IRQ(kscandat->kscan_base));
  106. return 0;
  107. }
  108. static void lpc32xx_kscan_close(struct input_dev *dev)
  109. {
  110. struct lpc32xx_kscan_drv *kscandat = input_get_drvdata(dev);
  111. writel(1, LPC32XX_KS_IRQ(kscandat->kscan_base));
  112. clk_disable_unprepare(kscandat->clk);
  113. }
  114. static int lpc32xx_parse_dt(struct device *dev,
  115. struct lpc32xx_kscan_drv *kscandat)
  116. {
  117. struct device_node *np = dev->of_node;
  118. u32 rows = 0, columns = 0;
  119. of_property_read_u32(np, "keypad,num-rows", &rows);
  120. of_property_read_u32(np, "keypad,num-columns", &columns);
  121. if (!rows || rows != columns) {
  122. dev_err(dev,
  123. "rows and columns must be specified and be equal!\n");
  124. return -EINVAL;
  125. }
  126. kscandat->matrix_sz = rows;
  127. kscandat->row_shift = get_count_order(columns);
  128. of_property_read_u32(np, "nxp,debounce-delay-ms", &kscandat->deb_clks);
  129. of_property_read_u32(np, "nxp,scan-delay-ms", &kscandat->scan_delay);
  130. if (!kscandat->deb_clks || !kscandat->scan_delay) {
  131. dev_err(dev, "debounce or scan delay not specified\n");
  132. return -EINVAL;
  133. }
  134. return 0;
  135. }
  136. static int lpc32xx_kscan_probe(struct platform_device *pdev)
  137. {
  138. struct lpc32xx_kscan_drv *kscandat;
  139. struct input_dev *input;
  140. struct resource *res;
  141. size_t keymap_size;
  142. int error;
  143. int irq;
  144. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  145. if (!res) {
  146. dev_err(&pdev->dev, "failed to get platform I/O memory\n");
  147. return -EINVAL;
  148. }
  149. irq = platform_get_irq(pdev, 0);
  150. if (irq < 0 || irq >= NR_IRQS) {
  151. dev_err(&pdev->dev, "failed to get platform irq\n");
  152. return -EINVAL;
  153. }
  154. kscandat = kzalloc(sizeof(struct lpc32xx_kscan_drv), GFP_KERNEL);
  155. if (!kscandat) {
  156. dev_err(&pdev->dev, "failed to allocate memory\n");
  157. return -ENOMEM;
  158. }
  159. error = lpc32xx_parse_dt(&pdev->dev, kscandat);
  160. if (error) {
  161. dev_err(&pdev->dev, "failed to parse device tree\n");
  162. goto err_free_mem;
  163. }
  164. keymap_size = sizeof(kscandat->keymap[0]) *
  165. (kscandat->matrix_sz << kscandat->row_shift);
  166. kscandat->keymap = kzalloc(keymap_size, GFP_KERNEL);
  167. if (!kscandat->keymap) {
  168. dev_err(&pdev->dev, "could not allocate memory for keymap\n");
  169. error = -ENOMEM;
  170. goto err_free_mem;
  171. }
  172. kscandat->input = input = input_allocate_device();
  173. if (!input) {
  174. dev_err(&pdev->dev, "failed to allocate input device\n");
  175. error = -ENOMEM;
  176. goto err_free_keymap;
  177. }
  178. /* Setup key input */
  179. input->name = pdev->name;
  180. input->phys = "lpc32xx/input0";
  181. input->id.vendor = 0x0001;
  182. input->id.product = 0x0001;
  183. input->id.version = 0x0100;
  184. input->open = lpc32xx_kscan_open;
  185. input->close = lpc32xx_kscan_close;
  186. input->dev.parent = &pdev->dev;
  187. input_set_capability(input, EV_MSC, MSC_SCAN);
  188. error = matrix_keypad_build_keymap(NULL, NULL,
  189. kscandat->matrix_sz,
  190. kscandat->matrix_sz,
  191. kscandat->keymap, kscandat->input);
  192. if (error) {
  193. dev_err(&pdev->dev, "failed to build keymap\n");
  194. goto err_free_input;
  195. }
  196. input_set_drvdata(kscandat->input, kscandat);
  197. kscandat->iores = request_mem_region(res->start, resource_size(res),
  198. pdev->name);
  199. if (!kscandat->iores) {
  200. dev_err(&pdev->dev, "failed to request I/O memory\n");
  201. error = -EBUSY;
  202. goto err_free_input;
  203. }
  204. kscandat->kscan_base = ioremap(kscandat->iores->start,
  205. resource_size(kscandat->iores));
  206. if (!kscandat->kscan_base) {
  207. dev_err(&pdev->dev, "failed to remap I/O memory\n");
  208. error = -EBUSY;
  209. goto err_release_memregion;
  210. }
  211. /* Get the key scanner clock */
  212. kscandat->clk = clk_get(&pdev->dev, NULL);
  213. if (IS_ERR(kscandat->clk)) {
  214. dev_err(&pdev->dev, "failed to get clock\n");
  215. error = PTR_ERR(kscandat->clk);
  216. goto err_unmap;
  217. }
  218. /* Configure the key scanner */
  219. error = clk_prepare_enable(kscandat->clk);
  220. if (error)
  221. goto err_clk_put;
  222. writel(kscandat->deb_clks, LPC32XX_KS_DEB(kscandat->kscan_base));
  223. writel(kscandat->scan_delay, LPC32XX_KS_SCAN_CTL(kscandat->kscan_base));
  224. writel(LPC32XX_KSCAN_FTST_USE32K_CLK,
  225. LPC32XX_KS_FAST_TST(kscandat->kscan_base));
  226. writel(kscandat->matrix_sz,
  227. LPC32XX_KS_MATRIX_DIM(kscandat->kscan_base));
  228. writel(1, LPC32XX_KS_IRQ(kscandat->kscan_base));
  229. clk_disable_unprepare(kscandat->clk);
  230. error = request_irq(irq, lpc32xx_kscan_irq, 0, pdev->name, kscandat);
  231. if (error) {
  232. dev_err(&pdev->dev, "failed to request irq\n");
  233. goto err_clk_put;
  234. }
  235. error = input_register_device(kscandat->input);
  236. if (error) {
  237. dev_err(&pdev->dev, "failed to register input device\n");
  238. goto err_free_irq;
  239. }
  240. platform_set_drvdata(pdev, kscandat);
  241. return 0;
  242. err_free_irq:
  243. free_irq(irq, kscandat);
  244. err_clk_put:
  245. clk_put(kscandat->clk);
  246. err_unmap:
  247. iounmap(kscandat->kscan_base);
  248. err_release_memregion:
  249. release_mem_region(kscandat->iores->start,
  250. resource_size(kscandat->iores));
  251. err_free_input:
  252. input_free_device(kscandat->input);
  253. err_free_keymap:
  254. kfree(kscandat->keymap);
  255. err_free_mem:
  256. kfree(kscandat);
  257. return error;
  258. }
  259. static int lpc32xx_kscan_remove(struct platform_device *pdev)
  260. {
  261. struct lpc32xx_kscan_drv *kscandat = platform_get_drvdata(pdev);
  262. free_irq(platform_get_irq(pdev, 0), kscandat);
  263. clk_put(kscandat->clk);
  264. iounmap(kscandat->kscan_base);
  265. release_mem_region(kscandat->iores->start,
  266. resource_size(kscandat->iores));
  267. input_unregister_device(kscandat->input);
  268. kfree(kscandat->keymap);
  269. kfree(kscandat);
  270. return 0;
  271. }
  272. #ifdef CONFIG_PM_SLEEP
  273. static int lpc32xx_kscan_suspend(struct device *dev)
  274. {
  275. struct platform_device *pdev = to_platform_device(dev);
  276. struct lpc32xx_kscan_drv *kscandat = platform_get_drvdata(pdev);
  277. struct input_dev *input = kscandat->input;
  278. mutex_lock(&input->mutex);
  279. if (input->users) {
  280. /* Clear IRQ and disable clock */
  281. writel(1, LPC32XX_KS_IRQ(kscandat->kscan_base));
  282. clk_disable_unprepare(kscandat->clk);
  283. }
  284. mutex_unlock(&input->mutex);
  285. return 0;
  286. }
  287. static int lpc32xx_kscan_resume(struct device *dev)
  288. {
  289. struct platform_device *pdev = to_platform_device(dev);
  290. struct lpc32xx_kscan_drv *kscandat = platform_get_drvdata(pdev);
  291. struct input_dev *input = kscandat->input;
  292. int retval = 0;
  293. mutex_lock(&input->mutex);
  294. if (input->users) {
  295. /* Enable clock and clear IRQ */
  296. retval = clk_prepare_enable(kscandat->clk);
  297. if (retval == 0)
  298. writel(1, LPC32XX_KS_IRQ(kscandat->kscan_base));
  299. }
  300. mutex_unlock(&input->mutex);
  301. return retval;
  302. }
  303. #endif
  304. static SIMPLE_DEV_PM_OPS(lpc32xx_kscan_pm_ops, lpc32xx_kscan_suspend,
  305. lpc32xx_kscan_resume);
  306. static const struct of_device_id lpc32xx_kscan_match[] = {
  307. { .compatible = "nxp,lpc3220-key" },
  308. {},
  309. };
  310. MODULE_DEVICE_TABLE(of, lpc32xx_kscan_match);
  311. static struct platform_driver lpc32xx_kscan_driver = {
  312. .probe = lpc32xx_kscan_probe,
  313. .remove = lpc32xx_kscan_remove,
  314. .driver = {
  315. .name = DRV_NAME,
  316. .owner = THIS_MODULE,
  317. .pm = &lpc32xx_kscan_pm_ops,
  318. .of_match_table = of_match_ptr(lpc32xx_kscan_match),
  319. }
  320. };
  321. module_platform_driver(lpc32xx_kscan_driver);
  322. MODULE_LICENSE("GPL");
  323. MODULE_AUTHOR("Kevin Wells <kevin.wells@nxp.com>");
  324. MODULE_AUTHOR("Roland Stigge <stigge@antcom.de>");
  325. MODULE_DESCRIPTION("Key scanner driver for LPC32XX devices");