sh_keysc.c 8.4 KB

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