nomadik-ske-keypad.c 11 KB

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