lpc32xx-keys.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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. int err;
  120. err = matrix_keypad_parse_of_params(dev, &rows, &columns);
  121. if (err)
  122. return err;
  123. if (rows != columns) {
  124. dev_err(dev, "rows and columns must be equal!\n");
  125. return -EINVAL;
  126. }
  127. kscandat->matrix_sz = rows;
  128. kscandat->row_shift = get_count_order(columns);
  129. of_property_read_u32(np, "nxp,debounce-delay-ms", &kscandat->deb_clks);
  130. of_property_read_u32(np, "nxp,scan-delay-ms", &kscandat->scan_delay);
  131. if (!kscandat->deb_clks || !kscandat->scan_delay) {
  132. dev_err(dev, "debounce or scan delay not specified\n");
  133. return -EINVAL;
  134. }
  135. return 0;
  136. }
  137. static int lpc32xx_kscan_probe(struct platform_device *pdev)
  138. {
  139. struct lpc32xx_kscan_drv *kscandat;
  140. struct input_dev *input;
  141. struct resource *res;
  142. size_t keymap_size;
  143. int error;
  144. int irq;
  145. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  146. if (!res) {
  147. dev_err(&pdev->dev, "failed to get platform I/O memory\n");
  148. return -EINVAL;
  149. }
  150. irq = platform_get_irq(pdev, 0);
  151. if (irq < 0 || irq >= NR_IRQS) {
  152. dev_err(&pdev->dev, "failed to get platform irq\n");
  153. return -EINVAL;
  154. }
  155. kscandat = kzalloc(sizeof(struct lpc32xx_kscan_drv), GFP_KERNEL);
  156. if (!kscandat) {
  157. dev_err(&pdev->dev, "failed to allocate memory\n");
  158. return -ENOMEM;
  159. }
  160. error = lpc32xx_parse_dt(&pdev->dev, kscandat);
  161. if (error) {
  162. dev_err(&pdev->dev, "failed to parse device tree\n");
  163. goto err_free_mem;
  164. }
  165. keymap_size = sizeof(kscandat->keymap[0]) *
  166. (kscandat->matrix_sz << kscandat->row_shift);
  167. kscandat->keymap = kzalloc(keymap_size, GFP_KERNEL);
  168. if (!kscandat->keymap) {
  169. dev_err(&pdev->dev, "could not allocate memory for keymap\n");
  170. error = -ENOMEM;
  171. goto err_free_mem;
  172. }
  173. kscandat->input = input = input_allocate_device();
  174. if (!input) {
  175. dev_err(&pdev->dev, "failed to allocate input device\n");
  176. error = -ENOMEM;
  177. goto err_free_keymap;
  178. }
  179. /* Setup key input */
  180. input->name = pdev->name;
  181. input->phys = "lpc32xx/input0";
  182. input->id.vendor = 0x0001;
  183. input->id.product = 0x0001;
  184. input->id.version = 0x0100;
  185. input->open = lpc32xx_kscan_open;
  186. input->close = lpc32xx_kscan_close;
  187. input->dev.parent = &pdev->dev;
  188. input_set_capability(input, EV_MSC, MSC_SCAN);
  189. error = matrix_keypad_build_keymap(NULL, NULL,
  190. kscandat->matrix_sz,
  191. kscandat->matrix_sz,
  192. kscandat->keymap, kscandat->input);
  193. if (error) {
  194. dev_err(&pdev->dev, "failed to build keymap\n");
  195. goto err_free_input;
  196. }
  197. input_set_drvdata(kscandat->input, kscandat);
  198. kscandat->iores = request_mem_region(res->start, resource_size(res),
  199. pdev->name);
  200. if (!kscandat->iores) {
  201. dev_err(&pdev->dev, "failed to request I/O memory\n");
  202. error = -EBUSY;
  203. goto err_free_input;
  204. }
  205. kscandat->kscan_base = ioremap(kscandat->iores->start,
  206. resource_size(kscandat->iores));
  207. if (!kscandat->kscan_base) {
  208. dev_err(&pdev->dev, "failed to remap I/O memory\n");
  209. error = -EBUSY;
  210. goto err_release_memregion;
  211. }
  212. /* Get the key scanner clock */
  213. kscandat->clk = clk_get(&pdev->dev, NULL);
  214. if (IS_ERR(kscandat->clk)) {
  215. dev_err(&pdev->dev, "failed to get clock\n");
  216. error = PTR_ERR(kscandat->clk);
  217. goto err_unmap;
  218. }
  219. /* Configure the key scanner */
  220. error = clk_prepare_enable(kscandat->clk);
  221. if (error)
  222. goto err_clk_put;
  223. writel(kscandat->deb_clks, LPC32XX_KS_DEB(kscandat->kscan_base));
  224. writel(kscandat->scan_delay, LPC32XX_KS_SCAN_CTL(kscandat->kscan_base));
  225. writel(LPC32XX_KSCAN_FTST_USE32K_CLK,
  226. LPC32XX_KS_FAST_TST(kscandat->kscan_base));
  227. writel(kscandat->matrix_sz,
  228. LPC32XX_KS_MATRIX_DIM(kscandat->kscan_base));
  229. writel(1, LPC32XX_KS_IRQ(kscandat->kscan_base));
  230. clk_disable_unprepare(kscandat->clk);
  231. error = request_irq(irq, lpc32xx_kscan_irq, 0, pdev->name, kscandat);
  232. if (error) {
  233. dev_err(&pdev->dev, "failed to request irq\n");
  234. goto err_clk_put;
  235. }
  236. error = input_register_device(kscandat->input);
  237. if (error) {
  238. dev_err(&pdev->dev, "failed to register input device\n");
  239. goto err_free_irq;
  240. }
  241. platform_set_drvdata(pdev, kscandat);
  242. return 0;
  243. err_free_irq:
  244. free_irq(irq, kscandat);
  245. err_clk_put:
  246. clk_put(kscandat->clk);
  247. err_unmap:
  248. iounmap(kscandat->kscan_base);
  249. err_release_memregion:
  250. release_mem_region(kscandat->iores->start,
  251. resource_size(kscandat->iores));
  252. err_free_input:
  253. input_free_device(kscandat->input);
  254. err_free_keymap:
  255. kfree(kscandat->keymap);
  256. err_free_mem:
  257. kfree(kscandat);
  258. return error;
  259. }
  260. static int lpc32xx_kscan_remove(struct platform_device *pdev)
  261. {
  262. struct lpc32xx_kscan_drv *kscandat = platform_get_drvdata(pdev);
  263. free_irq(platform_get_irq(pdev, 0), kscandat);
  264. clk_put(kscandat->clk);
  265. iounmap(kscandat->kscan_base);
  266. release_mem_region(kscandat->iores->start,
  267. resource_size(kscandat->iores));
  268. input_unregister_device(kscandat->input);
  269. kfree(kscandat->keymap);
  270. kfree(kscandat);
  271. return 0;
  272. }
  273. #ifdef CONFIG_PM_SLEEP
  274. static int lpc32xx_kscan_suspend(struct device *dev)
  275. {
  276. struct platform_device *pdev = to_platform_device(dev);
  277. struct lpc32xx_kscan_drv *kscandat = platform_get_drvdata(pdev);
  278. struct input_dev *input = kscandat->input;
  279. mutex_lock(&input->mutex);
  280. if (input->users) {
  281. /* Clear IRQ and disable clock */
  282. writel(1, LPC32XX_KS_IRQ(kscandat->kscan_base));
  283. clk_disable_unprepare(kscandat->clk);
  284. }
  285. mutex_unlock(&input->mutex);
  286. return 0;
  287. }
  288. static int lpc32xx_kscan_resume(struct device *dev)
  289. {
  290. struct platform_device *pdev = to_platform_device(dev);
  291. struct lpc32xx_kscan_drv *kscandat = platform_get_drvdata(pdev);
  292. struct input_dev *input = kscandat->input;
  293. int retval = 0;
  294. mutex_lock(&input->mutex);
  295. if (input->users) {
  296. /* Enable clock and clear IRQ */
  297. retval = clk_prepare_enable(kscandat->clk);
  298. if (retval == 0)
  299. writel(1, LPC32XX_KS_IRQ(kscandat->kscan_base));
  300. }
  301. mutex_unlock(&input->mutex);
  302. return retval;
  303. }
  304. #endif
  305. static SIMPLE_DEV_PM_OPS(lpc32xx_kscan_pm_ops, lpc32xx_kscan_suspend,
  306. lpc32xx_kscan_resume);
  307. static const struct of_device_id lpc32xx_kscan_match[] = {
  308. { .compatible = "nxp,lpc3220-key" },
  309. {},
  310. };
  311. MODULE_DEVICE_TABLE(of, lpc32xx_kscan_match);
  312. static struct platform_driver lpc32xx_kscan_driver = {
  313. .probe = lpc32xx_kscan_probe,
  314. .remove = lpc32xx_kscan_remove,
  315. .driver = {
  316. .name = DRV_NAME,
  317. .owner = THIS_MODULE,
  318. .pm = &lpc32xx_kscan_pm_ops,
  319. .of_match_table = of_match_ptr(lpc32xx_kscan_match),
  320. }
  321. };
  322. module_platform_driver(lpc32xx_kscan_driver);
  323. MODULE_LICENSE("GPL");
  324. MODULE_AUTHOR("Kevin Wells <kevin.wells@nxp.com>");
  325. MODULE_AUTHOR("Roland Stigge <stigge@antcom.de>");
  326. MODULE_DESCRIPTION("Key scanner driver for LPC32XX devices");