sh_keysc.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. /*
  2. * SuperH KEYSC Keypad Driver
  3. *
  4. * Copyright (C) 2008 Magnus Damm
  5. *
  6. * Based on gpio_keys.c, Copyright 2005 Phil Blundell
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #include <linux/kernel.h>
  13. #include <linux/module.h>
  14. #include <linux/init.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/irq.h>
  17. #include <linux/delay.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/input.h>
  20. #include <linux/input/sh_keysc.h>
  21. #include <linux/bitmap.h>
  22. #include <linux/pm_runtime.h>
  23. #include <linux/io.h>
  24. #include <linux/slab.h>
  25. static const struct {
  26. unsigned char kymd, keyout, keyin;
  27. } sh_keysc_mode[] = {
  28. [SH_KEYSC_MODE_1] = { 0, 6, 5 },
  29. [SH_KEYSC_MODE_2] = { 1, 5, 6 },
  30. [SH_KEYSC_MODE_3] = { 2, 4, 7 },
  31. [SH_KEYSC_MODE_4] = { 3, 6, 6 },
  32. [SH_KEYSC_MODE_5] = { 4, 6, 7 },
  33. [SH_KEYSC_MODE_6] = { 5, 8, 8 },
  34. };
  35. struct sh_keysc_priv {
  36. void __iomem *iomem_base;
  37. DECLARE_BITMAP(last_keys, SH_KEYSC_MAXKEYS);
  38. struct input_dev *input;
  39. struct sh_keysc_info pdata;
  40. };
  41. #define KYCR1 0
  42. #define KYCR2 1
  43. #define KYINDR 2
  44. #define KYOUTDR 3
  45. #define KYCR2_IRQ_LEVEL 0x10
  46. #define KYCR2_IRQ_DISABLED 0x00
  47. static unsigned long sh_keysc_read(struct sh_keysc_priv *p, int reg_nr)
  48. {
  49. return ioread16(p->iomem_base + (reg_nr << 2));
  50. }
  51. static void sh_keysc_write(struct sh_keysc_priv *p, int reg_nr,
  52. unsigned long value)
  53. {
  54. iowrite16(value, p->iomem_base + (reg_nr << 2));
  55. }
  56. static void sh_keysc_level_mode(struct sh_keysc_priv *p,
  57. unsigned long keys_set)
  58. {
  59. struct sh_keysc_info *pdata = &p->pdata;
  60. sh_keysc_write(p, KYOUTDR, 0);
  61. sh_keysc_write(p, KYCR2, KYCR2_IRQ_LEVEL | (keys_set << 8));
  62. if (pdata->kycr2_delay)
  63. udelay(pdata->kycr2_delay);
  64. }
  65. static void sh_keysc_map_dbg(struct device *dev, unsigned long *map,
  66. const char *str)
  67. {
  68. int k;
  69. for (k = 0; k < BITS_TO_LONGS(SH_KEYSC_MAXKEYS); k++)
  70. dev_dbg(dev, "%s[%d] 0x%lx\n", str, k, map[k]);
  71. }
  72. static irqreturn_t sh_keysc_isr(int irq, void *dev_id)
  73. {
  74. struct platform_device *pdev = dev_id;
  75. struct sh_keysc_priv *priv = platform_get_drvdata(pdev);
  76. struct sh_keysc_info *pdata = &priv->pdata;
  77. int keyout_nr = sh_keysc_mode[pdata->mode].keyout;
  78. int keyin_nr = sh_keysc_mode[pdata->mode].keyin;
  79. DECLARE_BITMAP(keys, SH_KEYSC_MAXKEYS);
  80. DECLARE_BITMAP(keys0, SH_KEYSC_MAXKEYS);
  81. DECLARE_BITMAP(keys1, SH_KEYSC_MAXKEYS);
  82. unsigned char keyin_set, tmp;
  83. int i, k, n;
  84. dev_dbg(&pdev->dev, "isr!\n");
  85. bitmap_fill(keys1, SH_KEYSC_MAXKEYS);
  86. bitmap_zero(keys0, SH_KEYSC_MAXKEYS);
  87. do {
  88. bitmap_zero(keys, SH_KEYSC_MAXKEYS);
  89. keyin_set = 0;
  90. sh_keysc_write(priv, KYCR2, KYCR2_IRQ_DISABLED);
  91. for (i = 0; i < keyout_nr; i++) {
  92. n = keyin_nr * i;
  93. /* drive one KEYOUT pin low, read KEYIN pins */
  94. sh_keysc_write(priv, KYOUTDR, 0xffff ^ (3 << (i * 2)));
  95. udelay(pdata->delay);
  96. tmp = sh_keysc_read(priv, KYINDR);
  97. /* set bit if key press has been detected */
  98. for (k = 0; k < keyin_nr; k++) {
  99. if (tmp & (1 << k))
  100. __set_bit(n + k, keys);
  101. }
  102. /* keep track of which KEYIN bits that have been set */
  103. keyin_set |= tmp ^ ((1 << keyin_nr) - 1);
  104. }
  105. sh_keysc_level_mode(priv, keyin_set);
  106. bitmap_complement(keys, keys, SH_KEYSC_MAXKEYS);
  107. bitmap_and(keys1, keys1, keys, SH_KEYSC_MAXKEYS);
  108. bitmap_or(keys0, keys0, keys, SH_KEYSC_MAXKEYS);
  109. sh_keysc_map_dbg(&pdev->dev, keys, "keys");
  110. } while (sh_keysc_read(priv, KYCR2) & 0x01);
  111. sh_keysc_map_dbg(&pdev->dev, priv->last_keys, "last_keys");
  112. sh_keysc_map_dbg(&pdev->dev, keys0, "keys0");
  113. sh_keysc_map_dbg(&pdev->dev, keys1, "keys1");
  114. for (i = 0; i < SH_KEYSC_MAXKEYS; i++) {
  115. k = pdata->keycodes[i];
  116. if (!k)
  117. continue;
  118. if (test_bit(i, keys0) == test_bit(i, priv->last_keys))
  119. continue;
  120. if (test_bit(i, keys1) || test_bit(i, keys0)) {
  121. input_event(priv->input, EV_KEY, k, 1);
  122. __set_bit(i, priv->last_keys);
  123. }
  124. if (!test_bit(i, keys1)) {
  125. input_event(priv->input, EV_KEY, k, 0);
  126. __clear_bit(i, priv->last_keys);
  127. }
  128. }
  129. input_sync(priv->input);
  130. return IRQ_HANDLED;
  131. }
  132. static int __devinit sh_keysc_probe(struct platform_device *pdev)
  133. {
  134. struct sh_keysc_priv *priv;
  135. struct sh_keysc_info *pdata;
  136. struct resource *res;
  137. struct input_dev *input;
  138. int i;
  139. int irq, error;
  140. if (!pdev->dev.platform_data) {
  141. dev_err(&pdev->dev, "no platform data defined\n");
  142. error = -EINVAL;
  143. goto err0;
  144. }
  145. error = -ENXIO;
  146. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  147. if (res == NULL) {
  148. dev_err(&pdev->dev, "failed to get I/O memory\n");
  149. goto err0;
  150. }
  151. irq = platform_get_irq(pdev, 0);
  152. if (irq < 0) {
  153. dev_err(&pdev->dev, "failed to get irq\n");
  154. goto err0;
  155. }
  156. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  157. if (priv == NULL) {
  158. dev_err(&pdev->dev, "failed to allocate driver data\n");
  159. error = -ENOMEM;
  160. goto err0;
  161. }
  162. platform_set_drvdata(pdev, priv);
  163. memcpy(&priv->pdata, pdev->dev.platform_data, sizeof(priv->pdata));
  164. pdata = &priv->pdata;
  165. priv->iomem_base = ioremap_nocache(res->start, resource_size(res));
  166. if (priv->iomem_base == NULL) {
  167. dev_err(&pdev->dev, "failed to remap I/O memory\n");
  168. error = -ENXIO;
  169. goto err1;
  170. }
  171. priv->input = input_allocate_device();
  172. if (!priv->input) {
  173. dev_err(&pdev->dev, "failed to allocate input device\n");
  174. error = -ENOMEM;
  175. goto err2;
  176. }
  177. input = priv->input;
  178. input->evbit[0] = BIT_MASK(EV_KEY);
  179. input->name = pdev->name;
  180. input->phys = "sh-keysc-keys/input0";
  181. input->dev.parent = &pdev->dev;
  182. input->id.bustype = BUS_HOST;
  183. input->id.vendor = 0x0001;
  184. input->id.product = 0x0001;
  185. input->id.version = 0x0100;
  186. input->keycode = pdata->keycodes;
  187. input->keycodesize = sizeof(pdata->keycodes[0]);
  188. input->keycodemax = ARRAY_SIZE(pdata->keycodes);
  189. error = request_threaded_irq(irq, NULL, sh_keysc_isr, IRQF_ONESHOT,
  190. dev_name(&pdev->dev), pdev);
  191. if (error) {
  192. dev_err(&pdev->dev, "failed to request IRQ\n");
  193. goto err3;
  194. }
  195. for (i = 0; i < SH_KEYSC_MAXKEYS; i++)
  196. __set_bit(pdata->keycodes[i], input->keybit);
  197. __clear_bit(KEY_RESERVED, input->keybit);
  198. error = input_register_device(input);
  199. if (error) {
  200. dev_err(&pdev->dev, "failed to register input device\n");
  201. goto err4;
  202. }
  203. pm_runtime_enable(&pdev->dev);
  204. pm_runtime_get_sync(&pdev->dev);
  205. sh_keysc_write(priv, KYCR1, (sh_keysc_mode[pdata->mode].kymd << 8) |
  206. pdata->scan_timing);
  207. sh_keysc_level_mode(priv, 0);
  208. device_init_wakeup(&pdev->dev, 1);
  209. return 0;
  210. err4:
  211. free_irq(irq, pdev);
  212. err3:
  213. input_free_device(input);
  214. err2:
  215. iounmap(priv->iomem_base);
  216. err1:
  217. platform_set_drvdata(pdev, NULL);
  218. kfree(priv);
  219. err0:
  220. return error;
  221. }
  222. static int __devexit sh_keysc_remove(struct platform_device *pdev)
  223. {
  224. struct sh_keysc_priv *priv = platform_get_drvdata(pdev);
  225. sh_keysc_write(priv, KYCR2, KYCR2_IRQ_DISABLED);
  226. input_unregister_device(priv->input);
  227. free_irq(platform_get_irq(pdev, 0), pdev);
  228. iounmap(priv->iomem_base);
  229. pm_runtime_put_sync(&pdev->dev);
  230. pm_runtime_disable(&pdev->dev);
  231. platform_set_drvdata(pdev, NULL);
  232. kfree(priv);
  233. return 0;
  234. }
  235. #ifdef CONFIG_PM_SLEEP
  236. static int sh_keysc_suspend(struct device *dev)
  237. {
  238. struct platform_device *pdev = to_platform_device(dev);
  239. struct sh_keysc_priv *priv = platform_get_drvdata(pdev);
  240. int irq = platform_get_irq(pdev, 0);
  241. unsigned short value;
  242. value = sh_keysc_read(priv, KYCR1);
  243. if (device_may_wakeup(dev)) {
  244. sh_keysc_write(priv, KYCR1, value | 0x80);
  245. enable_irq_wake(irq);
  246. } else {
  247. sh_keysc_write(priv, KYCR1, value & ~0x80);
  248. pm_runtime_put_sync(dev);
  249. }
  250. return 0;
  251. }
  252. static int sh_keysc_resume(struct device *dev)
  253. {
  254. struct platform_device *pdev = to_platform_device(dev);
  255. int irq = platform_get_irq(pdev, 0);
  256. if (device_may_wakeup(dev))
  257. disable_irq_wake(irq);
  258. else
  259. pm_runtime_get_sync(dev);
  260. return 0;
  261. }
  262. #endif
  263. static SIMPLE_DEV_PM_OPS(sh_keysc_dev_pm_ops,
  264. sh_keysc_suspend, sh_keysc_resume);
  265. static struct platform_driver sh_keysc_device_driver = {
  266. .probe = sh_keysc_probe,
  267. .remove = __devexit_p(sh_keysc_remove),
  268. .driver = {
  269. .name = "sh_keysc",
  270. .pm = &sh_keysc_dev_pm_ops,
  271. }
  272. };
  273. static int __init sh_keysc_init(void)
  274. {
  275. return platform_driver_register(&sh_keysc_device_driver);
  276. }
  277. static void __exit sh_keysc_exit(void)
  278. {
  279. platform_driver_unregister(&sh_keysc_device_driver);
  280. }
  281. module_init(sh_keysc_init);
  282. module_exit(sh_keysc_exit);
  283. MODULE_AUTHOR("Magnus Damm");
  284. MODULE_DESCRIPTION("SuperH KEYSC Keypad Driver");
  285. MODULE_LICENSE("GPL");