lpc32xx-keys.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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. void __iomem *kscan_base;
  60. unsigned int irq;
  61. u32 matrix_sz; /* Size of matrix in XxY, ie. 3 = 3x3 */
  62. u32 deb_clks; /* Debounce clocks (based on 32KHz clock) */
  63. u32 scan_delay; /* Scan delay (based on 32KHz clock) */
  64. unsigned short *keymap; /* Pointer to key map for the scan matrix */
  65. unsigned int row_shift;
  66. u8 lastkeystates[8];
  67. };
  68. static void lpc32xx_mod_states(struct lpc32xx_kscan_drv *kscandat, int col)
  69. {
  70. struct input_dev *input = kscandat->input;
  71. unsigned row, changed, scancode, keycode;
  72. u8 key;
  73. key = readl(LPC32XX_KS_DATA(kscandat->kscan_base, col));
  74. changed = key ^ kscandat->lastkeystates[col];
  75. kscandat->lastkeystates[col] = key;
  76. for (row = 0; changed; row++, changed >>= 1) {
  77. if (changed & 1) {
  78. /* Key state changed, signal an event */
  79. scancode = MATRIX_SCAN_CODE(row, col,
  80. kscandat->row_shift);
  81. keycode = kscandat->keymap[scancode];
  82. input_event(input, EV_MSC, MSC_SCAN, scancode);
  83. input_report_key(input, keycode, key & (1 << row));
  84. }
  85. }
  86. }
  87. static irqreturn_t lpc32xx_kscan_irq(int irq, void *dev_id)
  88. {
  89. struct lpc32xx_kscan_drv *kscandat = dev_id;
  90. int i;
  91. for (i = 0; i < kscandat->matrix_sz; i++)
  92. lpc32xx_mod_states(kscandat, i);
  93. writel(1, LPC32XX_KS_IRQ(kscandat->kscan_base));
  94. input_sync(kscandat->input);
  95. return IRQ_HANDLED;
  96. }
  97. static int lpc32xx_kscan_open(struct input_dev *dev)
  98. {
  99. struct lpc32xx_kscan_drv *kscandat = input_get_drvdata(dev);
  100. int error;
  101. error = clk_prepare_enable(kscandat->clk);
  102. if (error)
  103. return error;
  104. writel(1, LPC32XX_KS_IRQ(kscandat->kscan_base));
  105. return 0;
  106. }
  107. static void lpc32xx_kscan_close(struct input_dev *dev)
  108. {
  109. struct lpc32xx_kscan_drv *kscandat = input_get_drvdata(dev);
  110. writel(1, LPC32XX_KS_IRQ(kscandat->kscan_base));
  111. clk_disable_unprepare(kscandat->clk);
  112. }
  113. static int lpc32xx_parse_dt(struct device *dev,
  114. struct lpc32xx_kscan_drv *kscandat)
  115. {
  116. struct device_node *np = dev->of_node;
  117. u32 rows = 0, columns = 0;
  118. int err;
  119. err = matrix_keypad_parse_of_params(dev, &rows, &columns);
  120. if (err)
  121. return err;
  122. if (rows != columns) {
  123. dev_err(dev, "rows and columns must 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 = devm_kzalloc(&pdev->dev, sizeof(*kscandat),
  155. GFP_KERNEL);
  156. if (!kscandat)
  157. return -ENOMEM;
  158. error = lpc32xx_parse_dt(&pdev->dev, kscandat);
  159. if (error) {
  160. dev_err(&pdev->dev, "failed to parse device tree\n");
  161. return error;
  162. }
  163. keymap_size = sizeof(kscandat->keymap[0]) *
  164. (kscandat->matrix_sz << kscandat->row_shift);
  165. kscandat->keymap = devm_kzalloc(&pdev->dev, keymap_size, GFP_KERNEL);
  166. if (!kscandat->keymap)
  167. return -ENOMEM;
  168. kscandat->input = input = devm_input_allocate_device(&pdev->dev);
  169. if (!input) {
  170. dev_err(&pdev->dev, "failed to allocate input device\n");
  171. return -ENOMEM;
  172. }
  173. /* Setup key input */
  174. input->name = pdev->name;
  175. input->phys = "lpc32xx/input0";
  176. input->id.vendor = 0x0001;
  177. input->id.product = 0x0001;
  178. input->id.version = 0x0100;
  179. input->open = lpc32xx_kscan_open;
  180. input->close = lpc32xx_kscan_close;
  181. input->dev.parent = &pdev->dev;
  182. input_set_capability(input, EV_MSC, MSC_SCAN);
  183. error = matrix_keypad_build_keymap(NULL, NULL,
  184. kscandat->matrix_sz,
  185. kscandat->matrix_sz,
  186. kscandat->keymap, kscandat->input);
  187. if (error) {
  188. dev_err(&pdev->dev, "failed to build keymap\n");
  189. return error;
  190. }
  191. input_set_drvdata(kscandat->input, kscandat);
  192. kscandat->kscan_base = devm_ioremap_resource(&pdev->dev, res);
  193. if (IS_ERR(kscandat->kscan_base))
  194. return PTR_ERR(kscandat->kscan_base);
  195. /* Get the key scanner clock */
  196. kscandat->clk = devm_clk_get(&pdev->dev, NULL);
  197. if (IS_ERR(kscandat->clk)) {
  198. dev_err(&pdev->dev, "failed to get clock\n");
  199. return PTR_ERR(kscandat->clk);
  200. }
  201. /* Configure the key scanner */
  202. error = clk_prepare_enable(kscandat->clk);
  203. if (error)
  204. return error;
  205. writel(kscandat->deb_clks, LPC32XX_KS_DEB(kscandat->kscan_base));
  206. writel(kscandat->scan_delay, LPC32XX_KS_SCAN_CTL(kscandat->kscan_base));
  207. writel(LPC32XX_KSCAN_FTST_USE32K_CLK,
  208. LPC32XX_KS_FAST_TST(kscandat->kscan_base));
  209. writel(kscandat->matrix_sz,
  210. LPC32XX_KS_MATRIX_DIM(kscandat->kscan_base));
  211. writel(1, LPC32XX_KS_IRQ(kscandat->kscan_base));
  212. clk_disable_unprepare(kscandat->clk);
  213. error = devm_request_irq(&pdev->dev, irq, lpc32xx_kscan_irq, 0,
  214. pdev->name, kscandat);
  215. if (error) {
  216. dev_err(&pdev->dev, "failed to request irq\n");
  217. return error;
  218. }
  219. error = input_register_device(kscandat->input);
  220. if (error) {
  221. dev_err(&pdev->dev, "failed to register input device\n");
  222. return error;
  223. }
  224. platform_set_drvdata(pdev, kscandat);
  225. return 0;
  226. }
  227. #ifdef CONFIG_PM_SLEEP
  228. static int lpc32xx_kscan_suspend(struct device *dev)
  229. {
  230. struct platform_device *pdev = to_platform_device(dev);
  231. struct lpc32xx_kscan_drv *kscandat = platform_get_drvdata(pdev);
  232. struct input_dev *input = kscandat->input;
  233. mutex_lock(&input->mutex);
  234. if (input->users) {
  235. /* Clear IRQ and disable clock */
  236. writel(1, LPC32XX_KS_IRQ(kscandat->kscan_base));
  237. clk_disable_unprepare(kscandat->clk);
  238. }
  239. mutex_unlock(&input->mutex);
  240. return 0;
  241. }
  242. static int lpc32xx_kscan_resume(struct device *dev)
  243. {
  244. struct platform_device *pdev = to_platform_device(dev);
  245. struct lpc32xx_kscan_drv *kscandat = platform_get_drvdata(pdev);
  246. struct input_dev *input = kscandat->input;
  247. int retval = 0;
  248. mutex_lock(&input->mutex);
  249. if (input->users) {
  250. /* Enable clock and clear IRQ */
  251. retval = clk_prepare_enable(kscandat->clk);
  252. if (retval == 0)
  253. writel(1, LPC32XX_KS_IRQ(kscandat->kscan_base));
  254. }
  255. mutex_unlock(&input->mutex);
  256. return retval;
  257. }
  258. #endif
  259. static SIMPLE_DEV_PM_OPS(lpc32xx_kscan_pm_ops, lpc32xx_kscan_suspend,
  260. lpc32xx_kscan_resume);
  261. static const struct of_device_id lpc32xx_kscan_match[] = {
  262. { .compatible = "nxp,lpc3220-key" },
  263. {},
  264. };
  265. MODULE_DEVICE_TABLE(of, lpc32xx_kscan_match);
  266. static struct platform_driver lpc32xx_kscan_driver = {
  267. .probe = lpc32xx_kscan_probe,
  268. .driver = {
  269. .name = DRV_NAME,
  270. .pm = &lpc32xx_kscan_pm_ops,
  271. .of_match_table = lpc32xx_kscan_match,
  272. }
  273. };
  274. module_platform_driver(lpc32xx_kscan_driver);
  275. MODULE_LICENSE("GPL");
  276. MODULE_AUTHOR("Kevin Wells <kevin.wells@nxp.com>");
  277. MODULE_AUTHOR("Roland Stigge <stigge@antcom.de>");
  278. MODULE_DESCRIPTION("Key scanner driver for LPC32XX devices");