sh_keysc.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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. [SH_KEYSC_MODE_6] = { 5, 7, 7 },
  33. };
  34. struct sh_keysc_priv {
  35. void __iomem *iomem_base;
  36. struct clk *clk;
  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. char clk_name[8];
  139. int i;
  140. int irq, error;
  141. if (!pdev->dev.platform_data) {
  142. dev_err(&pdev->dev, "no platform data defined\n");
  143. error = -EINVAL;
  144. goto err0;
  145. }
  146. error = -ENXIO;
  147. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  148. if (res == NULL) {
  149. dev_err(&pdev->dev, "failed to get I/O memory\n");
  150. goto err0;
  151. }
  152. irq = platform_get_irq(pdev, 0);
  153. if (irq < 0) {
  154. dev_err(&pdev->dev, "failed to get irq\n");
  155. goto err0;
  156. }
  157. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  158. if (priv == NULL) {
  159. dev_err(&pdev->dev, "failed to allocate driver data\n");
  160. error = -ENOMEM;
  161. goto err0;
  162. }
  163. platform_set_drvdata(pdev, priv);
  164. memcpy(&priv->pdata, pdev->dev.platform_data, sizeof(priv->pdata));
  165. pdata = &priv->pdata;
  166. priv->iomem_base = ioremap_nocache(res->start, resource_size(res));
  167. if (priv->iomem_base == NULL) {
  168. dev_err(&pdev->dev, "failed to remap I/O memory\n");
  169. error = -ENXIO;
  170. goto err1;
  171. }
  172. snprintf(clk_name, sizeof(clk_name), "keysc%d", pdev->id);
  173. priv->clk = clk_get(&pdev->dev, clk_name);
  174. if (IS_ERR(priv->clk)) {
  175. dev_err(&pdev->dev, "cannot get clock \"%s\"\n", clk_name);
  176. error = PTR_ERR(priv->clk);
  177. goto err2;
  178. }
  179. priv->input = input_allocate_device();
  180. if (!priv->input) {
  181. dev_err(&pdev->dev, "failed to allocate input device\n");
  182. error = -ENOMEM;
  183. goto err3;
  184. }
  185. input = priv->input;
  186. input->evbit[0] = BIT_MASK(EV_KEY);
  187. input->name = pdev->name;
  188. input->phys = "sh-keysc-keys/input0";
  189. input->dev.parent = &pdev->dev;
  190. input->id.bustype = BUS_HOST;
  191. input->id.vendor = 0x0001;
  192. input->id.product = 0x0001;
  193. input->id.version = 0x0100;
  194. input->keycode = pdata->keycodes;
  195. input->keycodesize = sizeof(pdata->keycodes[0]);
  196. input->keycodemax = ARRAY_SIZE(pdata->keycodes);
  197. error = request_irq(irq, sh_keysc_isr, 0, pdev->name, pdev);
  198. if (error) {
  199. dev_err(&pdev->dev, "failed to request IRQ\n");
  200. goto err4;
  201. }
  202. for (i = 0; i < SH_KEYSC_MAXKEYS; i++)
  203. __set_bit(pdata->keycodes[i], input->keybit);
  204. __clear_bit(KEY_RESERVED, input->keybit);
  205. error = input_register_device(input);
  206. if (error) {
  207. dev_err(&pdev->dev, "failed to register input device\n");
  208. goto err5;
  209. }
  210. clk_enable(priv->clk);
  211. sh_keysc_write(priv, KYCR1, (sh_keysc_mode[pdata->mode].kymd << 8) |
  212. pdata->scan_timing);
  213. sh_keysc_level_mode(priv, 0);
  214. device_init_wakeup(&pdev->dev, 1);
  215. return 0;
  216. err5:
  217. free_irq(irq, pdev);
  218. err4:
  219. input_free_device(input);
  220. err3:
  221. clk_put(priv->clk);
  222. err2:
  223. iounmap(priv->iomem_base);
  224. err1:
  225. platform_set_drvdata(pdev, NULL);
  226. kfree(priv);
  227. err0:
  228. return error;
  229. }
  230. static int __devexit sh_keysc_remove(struct platform_device *pdev)
  231. {
  232. struct sh_keysc_priv *priv = platform_get_drvdata(pdev);
  233. sh_keysc_write(priv, KYCR2, KYCR2_IRQ_DISABLED);
  234. input_unregister_device(priv->input);
  235. free_irq(platform_get_irq(pdev, 0), pdev);
  236. iounmap(priv->iomem_base);
  237. clk_disable(priv->clk);
  238. clk_put(priv->clk);
  239. platform_set_drvdata(pdev, NULL);
  240. kfree(priv);
  241. return 0;
  242. }
  243. static int sh_keysc_suspend(struct device *dev)
  244. {
  245. struct platform_device *pdev = to_platform_device(dev);
  246. struct sh_keysc_priv *priv = platform_get_drvdata(pdev);
  247. int irq = platform_get_irq(pdev, 0);
  248. unsigned short value;
  249. value = sh_keysc_read(priv, KYCR1);
  250. if (device_may_wakeup(dev)) {
  251. value |= 0x80;
  252. enable_irq_wake(irq);
  253. } else {
  254. value &= ~0x80;
  255. }
  256. sh_keysc_write(priv, KYCR1, value);
  257. return 0;
  258. }
  259. static int sh_keysc_resume(struct device *dev)
  260. {
  261. struct platform_device *pdev = to_platform_device(dev);
  262. int irq = platform_get_irq(pdev, 0);
  263. if (device_may_wakeup(dev))
  264. disable_irq_wake(irq);
  265. return 0;
  266. }
  267. static const struct dev_pm_ops sh_keysc_dev_pm_ops = {
  268. .suspend = sh_keysc_suspend,
  269. .resume = sh_keysc_resume,
  270. };
  271. struct platform_driver sh_keysc_device_driver = {
  272. .probe = sh_keysc_probe,
  273. .remove = __devexit_p(sh_keysc_remove),
  274. .driver = {
  275. .name = "sh_keysc",
  276. .pm = &sh_keysc_dev_pm_ops,
  277. }
  278. };
  279. static int __init sh_keysc_init(void)
  280. {
  281. return platform_driver_register(&sh_keysc_device_driver);
  282. }
  283. static void __exit sh_keysc_exit(void)
  284. {
  285. platform_driver_unregister(&sh_keysc_device_driver);
  286. }
  287. module_init(sh_keysc_init);
  288. module_exit(sh_keysc_exit);
  289. MODULE_AUTHOR("Magnus Damm");
  290. MODULE_DESCRIPTION("SuperH KEYSC Keypad Driver");
  291. MODULE_LICENSE("GPL");