sh_keysc.c 7.7 KB

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