nomadik-ske-keypad.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) ST-Ericsson SA 2010
  4. *
  5. * Author: Naveen Kumar G <[email protected]> for ST-Ericsson
  6. * Author: Sundar Iyer <[email protected]> for ST-Ericsson
  7. *
  8. * Keypad controller driver for the SKE (Scroll Key Encoder) module used in
  9. * the Nomadik 8815 and Ux500 platforms.
  10. */
  11. #include <linux/platform_device.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/spinlock.h>
  14. #include <linux/io.h>
  15. #include <linux/delay.h>
  16. #include <linux/input.h>
  17. #include <linux/slab.h>
  18. #include <linux/clk.h>
  19. #include <linux/module.h>
  20. #include <linux/platform_data/keypad-nomadik-ske.h>
  21. /* SKE_CR bits */
  22. #define SKE_KPMLT (0x1 << 6)
  23. #define SKE_KPCN (0x7 << 3)
  24. #define SKE_KPASEN (0x1 << 2)
  25. #define SKE_KPASON (0x1 << 7)
  26. /* SKE_IMSC bits */
  27. #define SKE_KPIMA (0x1 << 2)
  28. /* SKE_ICR bits */
  29. #define SKE_KPICS (0x1 << 3)
  30. #define SKE_KPICA (0x1 << 2)
  31. /* SKE_RIS bits */
  32. #define SKE_KPRISA (0x1 << 2)
  33. #define SKE_KEYPAD_ROW_SHIFT 3
  34. #define SKE_KPD_NUM_ROWS 8
  35. #define SKE_KPD_NUM_COLS 8
  36. /* keypad auto scan registers */
  37. #define SKE_ASR0 0x20
  38. #define SKE_ASR1 0x24
  39. #define SKE_ASR2 0x28
  40. #define SKE_ASR3 0x2C
  41. #define SKE_NUM_ASRX_REGISTERS (4)
  42. #define KEY_PRESSED_DELAY 10
  43. /**
  44. * struct ske_keypad - data structure used by keypad driver
  45. * @irq: irq no
  46. * @reg_base: ske registers base address
  47. * @input: pointer to input device object
  48. * @board: keypad platform device
  49. * @keymap: matrix scan code table for keycodes
  50. * @clk: clock structure pointer
  51. * @pclk: clock structure pointer
  52. * @ske_keypad_lock: spinlock protecting the keypad read/writes
  53. */
  54. struct ske_keypad {
  55. int irq;
  56. void __iomem *reg_base;
  57. struct input_dev *input;
  58. const struct ske_keypad_platform_data *board;
  59. unsigned short keymap[SKE_KPD_NUM_ROWS * SKE_KPD_NUM_COLS];
  60. struct clk *clk;
  61. struct clk *pclk;
  62. spinlock_t ske_keypad_lock;
  63. };
  64. static void ske_keypad_set_bits(struct ske_keypad *keypad, u16 addr,
  65. u8 mask, u8 data)
  66. {
  67. u32 ret;
  68. spin_lock(&keypad->ske_keypad_lock);
  69. ret = readl(keypad->reg_base + addr);
  70. ret &= ~mask;
  71. ret |= data;
  72. writel(ret, keypad->reg_base + addr);
  73. spin_unlock(&keypad->ske_keypad_lock);
  74. }
  75. /*
  76. * ske_keypad_chip_init: init keypad controller configuration
  77. *
  78. * Enable Multi key press detection, auto scan mode
  79. */
  80. static int __init ske_keypad_chip_init(struct ske_keypad *keypad)
  81. {
  82. u32 value;
  83. int timeout = keypad->board->debounce_ms;
  84. /* check SKE_RIS to be 0 */
  85. while ((readl(keypad->reg_base + SKE_RIS) != 0x00000000) && timeout--)
  86. cpu_relax();
  87. if (timeout == -1)
  88. return -EINVAL;
  89. /*
  90. * set debounce value
  91. * keypad dbounce is configured in DBCR[15:8]
  92. * dbounce value in steps of 32/32.768 ms
  93. */
  94. spin_lock(&keypad->ske_keypad_lock);
  95. value = readl(keypad->reg_base + SKE_DBCR);
  96. value = value & 0xff;
  97. value |= ((keypad->board->debounce_ms * 32000)/32768) << 8;
  98. writel(value, keypad->reg_base + SKE_DBCR);
  99. spin_unlock(&keypad->ske_keypad_lock);
  100. /* enable multi key detection */
  101. ske_keypad_set_bits(keypad, SKE_CR, 0x0, SKE_KPMLT);
  102. /*
  103. * set up the number of columns
  104. * KPCN[5:3] defines no. of keypad columns to be auto scanned
  105. */
  106. value = (keypad->board->kcol - 1) << 3;
  107. ske_keypad_set_bits(keypad, SKE_CR, SKE_KPCN, value);
  108. /* clear keypad interrupt for auto(and pending SW) scans */
  109. ske_keypad_set_bits(keypad, SKE_ICR, 0x0, SKE_KPICA | SKE_KPICS);
  110. /* un-mask keypad interrupts */
  111. ske_keypad_set_bits(keypad, SKE_IMSC, 0x0, SKE_KPIMA);
  112. /* enable automatic scan */
  113. ske_keypad_set_bits(keypad, SKE_CR, 0x0, SKE_KPASEN);
  114. return 0;
  115. }
  116. static void ske_keypad_report(struct ske_keypad *keypad, u8 status, int col)
  117. {
  118. int row = 0, code, pos;
  119. struct input_dev *input = keypad->input;
  120. u32 ske_ris;
  121. int key_pressed;
  122. int num_of_rows;
  123. /* find out the row */
  124. num_of_rows = hweight8(status);
  125. do {
  126. pos = __ffs(status);
  127. row = pos;
  128. status &= ~(1 << pos);
  129. code = MATRIX_SCAN_CODE(row, col, SKE_KEYPAD_ROW_SHIFT);
  130. ske_ris = readl(keypad->reg_base + SKE_RIS);
  131. key_pressed = ske_ris & SKE_KPRISA;
  132. input_event(input, EV_MSC, MSC_SCAN, code);
  133. input_report_key(input, keypad->keymap[code], key_pressed);
  134. input_sync(input);
  135. num_of_rows--;
  136. } while (num_of_rows);
  137. }
  138. static void ske_keypad_read_data(struct ske_keypad *keypad)
  139. {
  140. u8 status;
  141. int col = 0;
  142. int ske_asr, i;
  143. /*
  144. * Read the auto scan registers
  145. *
  146. * Each SKE_ASRx (x=0 to x=3) contains two row values.
  147. * lower byte contains row value for column 2*x,
  148. * upper byte contains row value for column 2*x + 1
  149. */
  150. for (i = 0; i < SKE_NUM_ASRX_REGISTERS; i++) {
  151. ske_asr = readl(keypad->reg_base + SKE_ASR0 + (4 * i));
  152. if (!ske_asr)
  153. continue;
  154. /* now that ASRx is zero, find out the coloumn x and row y */
  155. status = ske_asr & 0xff;
  156. if (status) {
  157. col = i * 2;
  158. ske_keypad_report(keypad, status, col);
  159. }
  160. status = (ske_asr & 0xff00) >> 8;
  161. if (status) {
  162. col = (i * 2) + 1;
  163. ske_keypad_report(keypad, status, col);
  164. }
  165. }
  166. }
  167. static irqreturn_t ske_keypad_irq(int irq, void *dev_id)
  168. {
  169. struct ske_keypad *keypad = dev_id;
  170. int timeout = keypad->board->debounce_ms;
  171. /* disable auto scan interrupt; mask the interrupt generated */
  172. ske_keypad_set_bits(keypad, SKE_IMSC, ~SKE_KPIMA, 0x0);
  173. ske_keypad_set_bits(keypad, SKE_ICR, 0x0, SKE_KPICA);
  174. while ((readl(keypad->reg_base + SKE_CR) & SKE_KPASON) && --timeout)
  175. cpu_relax();
  176. /* SKEx registers are stable and can be read */
  177. ske_keypad_read_data(keypad);
  178. /* wait until raw interrupt is clear */
  179. while ((readl(keypad->reg_base + SKE_RIS)) && --timeout)
  180. msleep(KEY_PRESSED_DELAY);
  181. /* enable auto scan interrupts */
  182. ske_keypad_set_bits(keypad, SKE_IMSC, 0x0, SKE_KPIMA);
  183. return IRQ_HANDLED;
  184. }
  185. static int __init ske_keypad_probe(struct platform_device *pdev)
  186. {
  187. const struct ske_keypad_platform_data *plat =
  188. dev_get_platdata(&pdev->dev);
  189. struct ske_keypad *keypad;
  190. struct input_dev *input;
  191. struct resource *res;
  192. int irq;
  193. int error;
  194. if (!plat) {
  195. dev_err(&pdev->dev, "invalid keypad platform data\n");
  196. return -EINVAL;
  197. }
  198. irq = platform_get_irq(pdev, 0);
  199. if (irq < 0)
  200. return -EINVAL;
  201. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  202. if (!res) {
  203. dev_err(&pdev->dev, "missing platform resources\n");
  204. return -EINVAL;
  205. }
  206. keypad = kzalloc(sizeof(struct ske_keypad), GFP_KERNEL);
  207. input = input_allocate_device();
  208. if (!keypad || !input) {
  209. dev_err(&pdev->dev, "failed to allocate keypad memory\n");
  210. error = -ENOMEM;
  211. goto err_free_mem;
  212. }
  213. keypad->irq = irq;
  214. keypad->board = plat;
  215. keypad->input = input;
  216. spin_lock_init(&keypad->ske_keypad_lock);
  217. if (!request_mem_region(res->start, resource_size(res), pdev->name)) {
  218. dev_err(&pdev->dev, "failed to request I/O memory\n");
  219. error = -EBUSY;
  220. goto err_free_mem;
  221. }
  222. keypad->reg_base = ioremap(res->start, resource_size(res));
  223. if (!keypad->reg_base) {
  224. dev_err(&pdev->dev, "failed to remap I/O memory\n");
  225. error = -ENXIO;
  226. goto err_free_mem_region;
  227. }
  228. keypad->pclk = clk_get(&pdev->dev, "apb_pclk");
  229. if (IS_ERR(keypad->pclk)) {
  230. dev_err(&pdev->dev, "failed to get pclk\n");
  231. error = PTR_ERR(keypad->pclk);
  232. goto err_iounmap;
  233. }
  234. keypad->clk = clk_get(&pdev->dev, NULL);
  235. if (IS_ERR(keypad->clk)) {
  236. dev_err(&pdev->dev, "failed to get clk\n");
  237. error = PTR_ERR(keypad->clk);
  238. goto err_pclk;
  239. }
  240. input->id.bustype = BUS_HOST;
  241. input->name = "ux500-ske-keypad";
  242. input->dev.parent = &pdev->dev;
  243. error = matrix_keypad_build_keymap(plat->keymap_data, NULL,
  244. SKE_KPD_NUM_ROWS, SKE_KPD_NUM_COLS,
  245. keypad->keymap, input);
  246. if (error) {
  247. dev_err(&pdev->dev, "Failed to build keymap\n");
  248. goto err_clk;
  249. }
  250. input_set_capability(input, EV_MSC, MSC_SCAN);
  251. if (!plat->no_autorepeat)
  252. __set_bit(EV_REP, input->evbit);
  253. error = clk_prepare_enable(keypad->pclk);
  254. if (error) {
  255. dev_err(&pdev->dev, "Failed to prepare/enable pclk\n");
  256. goto err_clk;
  257. }
  258. error = clk_prepare_enable(keypad->clk);
  259. if (error) {
  260. dev_err(&pdev->dev, "Failed to prepare/enable clk\n");
  261. goto err_pclk_disable;
  262. }
  263. /* go through board initialization helpers */
  264. if (keypad->board->init)
  265. keypad->board->init();
  266. error = ske_keypad_chip_init(keypad);
  267. if (error) {
  268. dev_err(&pdev->dev, "unable to init keypad hardware\n");
  269. goto err_clk_disable;
  270. }
  271. error = request_threaded_irq(keypad->irq, NULL, ske_keypad_irq,
  272. IRQF_ONESHOT, "ske-keypad", keypad);
  273. if (error) {
  274. dev_err(&pdev->dev, "allocate irq %d failed\n", keypad->irq);
  275. goto err_clk_disable;
  276. }
  277. error = input_register_device(input);
  278. if (error) {
  279. dev_err(&pdev->dev,
  280. "unable to register input device: %d\n", error);
  281. goto err_free_irq;
  282. }
  283. if (plat->wakeup_enable)
  284. device_init_wakeup(&pdev->dev, true);
  285. platform_set_drvdata(pdev, keypad);
  286. return 0;
  287. err_free_irq:
  288. free_irq(keypad->irq, keypad);
  289. err_clk_disable:
  290. clk_disable_unprepare(keypad->clk);
  291. err_pclk_disable:
  292. clk_disable_unprepare(keypad->pclk);
  293. err_clk:
  294. clk_put(keypad->clk);
  295. err_pclk:
  296. clk_put(keypad->pclk);
  297. err_iounmap:
  298. iounmap(keypad->reg_base);
  299. err_free_mem_region:
  300. release_mem_region(res->start, resource_size(res));
  301. err_free_mem:
  302. input_free_device(input);
  303. kfree(keypad);
  304. return error;
  305. }
  306. static int ske_keypad_remove(struct platform_device *pdev)
  307. {
  308. struct ske_keypad *keypad = platform_get_drvdata(pdev);
  309. struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  310. free_irq(keypad->irq, keypad);
  311. input_unregister_device(keypad->input);
  312. clk_disable_unprepare(keypad->clk);
  313. clk_put(keypad->clk);
  314. if (keypad->board->exit)
  315. keypad->board->exit();
  316. iounmap(keypad->reg_base);
  317. release_mem_region(res->start, resource_size(res));
  318. kfree(keypad);
  319. return 0;
  320. }
  321. #ifdef CONFIG_PM_SLEEP
  322. static int ske_keypad_suspend(struct device *dev)
  323. {
  324. struct platform_device *pdev = to_platform_device(dev);
  325. struct ske_keypad *keypad = platform_get_drvdata(pdev);
  326. int irq = platform_get_irq(pdev, 0);
  327. if (device_may_wakeup(dev))
  328. enable_irq_wake(irq);
  329. else
  330. ske_keypad_set_bits(keypad, SKE_IMSC, ~SKE_KPIMA, 0x0);
  331. return 0;
  332. }
  333. static int ske_keypad_resume(struct device *dev)
  334. {
  335. struct platform_device *pdev = to_platform_device(dev);
  336. struct ske_keypad *keypad = platform_get_drvdata(pdev);
  337. int irq = platform_get_irq(pdev, 0);
  338. if (device_may_wakeup(dev))
  339. disable_irq_wake(irq);
  340. else
  341. ske_keypad_set_bits(keypad, SKE_IMSC, 0x0, SKE_KPIMA);
  342. return 0;
  343. }
  344. #endif
  345. static SIMPLE_DEV_PM_OPS(ske_keypad_dev_pm_ops,
  346. ske_keypad_suspend, ske_keypad_resume);
  347. static struct platform_driver ske_keypad_driver = {
  348. .driver = {
  349. .name = "nmk-ske-keypad",
  350. .pm = &ske_keypad_dev_pm_ops,
  351. },
  352. .remove = ske_keypad_remove,
  353. };
  354. module_platform_driver_probe(ske_keypad_driver, ske_keypad_probe);
  355. MODULE_LICENSE("GPL v2");
  356. MODULE_AUTHOR("Naveen Kumar <[email protected]> / Sundar Iyer <[email protected]>");
  357. MODULE_DESCRIPTION("Nomadik Scroll-Key-Encoder Keypad Driver");
  358. MODULE_ALIAS("platform:nomadik-ske-keypad");