sh_keysc.c 8.3 KB

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