bfin_rotary.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /*
  2. * Rotary counter driver for Analog Devices Blackfin Processors
  3. *
  4. * Copyright 2008-2009 Analog Devices Inc.
  5. * Licensed under the GPL-2 or later.
  6. */
  7. #include <linux/module.h>
  8. #include <linux/version.h>
  9. #include <linux/init.h>
  10. #include <linux/interrupt.h>
  11. #include <linux/irq.h>
  12. #include <linux/pm.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/input.h>
  15. #include <linux/slab.h>
  16. #include <asm/portmux.h>
  17. #include <asm/bfin_rotary.h>
  18. static const u16 per_cnt[] = {
  19. P_CNT_CUD,
  20. P_CNT_CDG,
  21. P_CNT_CZM,
  22. 0
  23. };
  24. struct bfin_rot {
  25. struct input_dev *input;
  26. int irq;
  27. unsigned int up_key;
  28. unsigned int down_key;
  29. unsigned int button_key;
  30. unsigned int rel_code;
  31. unsigned short cnt_config;
  32. unsigned short cnt_imask;
  33. unsigned short cnt_debounce;
  34. };
  35. static void report_key_event(struct input_dev *input, int keycode)
  36. {
  37. /* simulate a press-n-release */
  38. input_report_key(input, keycode, 1);
  39. input_sync(input);
  40. input_report_key(input, keycode, 0);
  41. input_sync(input);
  42. }
  43. static void report_rotary_event(struct bfin_rot *rotary, int delta)
  44. {
  45. struct input_dev *input = rotary->input;
  46. if (rotary->up_key) {
  47. report_key_event(input,
  48. delta > 0 ? rotary->up_key : rotary->down_key);
  49. } else {
  50. input_report_rel(input, rotary->rel_code, delta);
  51. input_sync(input);
  52. }
  53. }
  54. static irqreturn_t bfin_rotary_isr(int irq, void *dev_id)
  55. {
  56. struct platform_device *pdev = dev_id;
  57. struct bfin_rot *rotary = platform_get_drvdata(pdev);
  58. int delta;
  59. switch (bfin_read_CNT_STATUS()) {
  60. case ICII:
  61. break;
  62. case UCII:
  63. case DCII:
  64. delta = bfin_read_CNT_COUNTER();
  65. if (delta)
  66. report_rotary_event(rotary, delta);
  67. break;
  68. case CZMII:
  69. report_key_event(rotary->input, rotary->button_key);
  70. break;
  71. default:
  72. break;
  73. }
  74. bfin_write_CNT_COMMAND(W1LCNT_ZERO); /* Clear COUNTER */
  75. bfin_write_CNT_STATUS(-1); /* Clear STATUS */
  76. return IRQ_HANDLED;
  77. }
  78. static int __devinit bfin_rotary_probe(struct platform_device *pdev)
  79. {
  80. struct bfin_rotary_platform_data *pdata = pdev->dev.platform_data;
  81. struct bfin_rot *rotary;
  82. struct input_dev *input;
  83. int error;
  84. /* Basic validation */
  85. if ((pdata->rotary_up_key && !pdata->rotary_down_key) ||
  86. (!pdata->rotary_up_key && pdata->rotary_down_key)) {
  87. return -EINVAL;
  88. }
  89. error = peripheral_request_list(per_cnt, dev_name(&pdev->dev));
  90. if (error) {
  91. dev_err(&pdev->dev, "requesting peripherals failed\n");
  92. return error;
  93. }
  94. rotary = kzalloc(sizeof(struct bfin_rot), GFP_KERNEL);
  95. input = input_allocate_device();
  96. if (!rotary || !input) {
  97. error = -ENOMEM;
  98. goto out1;
  99. }
  100. rotary->input = input;
  101. rotary->up_key = pdata->rotary_up_key;
  102. rotary->down_key = pdata->rotary_down_key;
  103. rotary->button_key = pdata->rotary_button_key;
  104. rotary->rel_code = pdata->rotary_rel_code;
  105. error = rotary->irq = platform_get_irq(pdev, 0);
  106. if (error < 0)
  107. goto out1;
  108. input->name = pdev->name;
  109. input->phys = "bfin-rotary/input0";
  110. input->dev.parent = &pdev->dev;
  111. input_set_drvdata(input, rotary);
  112. input->id.bustype = BUS_HOST;
  113. input->id.vendor = 0x0001;
  114. input->id.product = 0x0001;
  115. input->id.version = 0x0100;
  116. if (rotary->up_key) {
  117. __set_bit(EV_KEY, input->evbit);
  118. __set_bit(rotary->up_key, input->keybit);
  119. __set_bit(rotary->down_key, input->keybit);
  120. } else {
  121. __set_bit(EV_REL, input->evbit);
  122. __set_bit(rotary->rel_code, input->relbit);
  123. }
  124. if (rotary->button_key) {
  125. __set_bit(EV_KEY, input->evbit);
  126. __set_bit(rotary->button_key, input->keybit);
  127. }
  128. error = request_irq(rotary->irq, bfin_rotary_isr,
  129. 0, dev_name(&pdev->dev), pdev);
  130. if (error) {
  131. dev_err(&pdev->dev,
  132. "unable to claim irq %d; error %d\n",
  133. rotary->irq, error);
  134. goto out1;
  135. }
  136. error = input_register_device(input);
  137. if (error) {
  138. dev_err(&pdev->dev,
  139. "unable to register input device (%d)\n", error);
  140. goto out2;
  141. }
  142. if (pdata->rotary_button_key)
  143. bfin_write_CNT_IMASK(CZMIE);
  144. if (pdata->mode & ROT_DEBE)
  145. bfin_write_CNT_DEBOUNCE(pdata->debounce & DPRESCALE);
  146. if (pdata->mode)
  147. bfin_write_CNT_CONFIG(bfin_read_CNT_CONFIG() |
  148. (pdata->mode & ~CNTE));
  149. bfin_write_CNT_IMASK(bfin_read_CNT_IMASK() | UCIE | DCIE);
  150. bfin_write_CNT_CONFIG(bfin_read_CNT_CONFIG() | CNTE);
  151. platform_set_drvdata(pdev, rotary);
  152. device_init_wakeup(&pdev->dev, 1);
  153. return 0;
  154. out2:
  155. free_irq(rotary->irq, pdev);
  156. out1:
  157. input_free_device(input);
  158. kfree(rotary);
  159. peripheral_free_list(per_cnt);
  160. return error;
  161. }
  162. static int __devexit bfin_rotary_remove(struct platform_device *pdev)
  163. {
  164. struct bfin_rot *rotary = platform_get_drvdata(pdev);
  165. bfin_write_CNT_CONFIG(0);
  166. bfin_write_CNT_IMASK(0);
  167. free_irq(rotary->irq, pdev);
  168. input_unregister_device(rotary->input);
  169. peripheral_free_list(per_cnt);
  170. kfree(rotary);
  171. platform_set_drvdata(pdev, NULL);
  172. return 0;
  173. }
  174. #ifdef CONFIG_PM
  175. static int bfin_rotary_suspend(struct device *dev)
  176. {
  177. struct platform_device *pdev = to_platform_device(dev);
  178. struct bfin_rot *rotary = platform_get_drvdata(pdev);
  179. rotary->cnt_config = bfin_read_CNT_CONFIG();
  180. rotary->cnt_imask = bfin_read_CNT_IMASK();
  181. rotary->cnt_debounce = bfin_read_CNT_DEBOUNCE();
  182. if (device_may_wakeup(&pdev->dev))
  183. enable_irq_wake(rotary->irq);
  184. return 0;
  185. }
  186. static int bfin_rotary_resume(struct device *dev)
  187. {
  188. struct platform_device *pdev = to_platform_device(dev);
  189. struct bfin_rot *rotary = platform_get_drvdata(pdev);
  190. bfin_write_CNT_DEBOUNCE(rotary->cnt_debounce);
  191. bfin_write_CNT_IMASK(rotary->cnt_imask);
  192. bfin_write_CNT_CONFIG(rotary->cnt_config & ~CNTE);
  193. if (device_may_wakeup(&pdev->dev))
  194. disable_irq_wake(rotary->irq);
  195. if (rotary->cnt_config & CNTE)
  196. bfin_write_CNT_CONFIG(rotary->cnt_config);
  197. return 0;
  198. }
  199. static const struct dev_pm_ops bfin_rotary_pm_ops = {
  200. .suspend = bfin_rotary_suspend,
  201. .resume = bfin_rotary_resume,
  202. };
  203. #endif
  204. static struct platform_driver bfin_rotary_device_driver = {
  205. .probe = bfin_rotary_probe,
  206. .remove = __devexit_p(bfin_rotary_remove),
  207. .driver = {
  208. .name = "bfin-rotary",
  209. .owner = THIS_MODULE,
  210. #ifdef CONFIG_PM
  211. .pm = &bfin_rotary_pm_ops,
  212. #endif
  213. },
  214. };
  215. static int __init bfin_rotary_init(void)
  216. {
  217. return platform_driver_register(&bfin_rotary_device_driver);
  218. }
  219. module_init(bfin_rotary_init);
  220. static void __exit bfin_rotary_exit(void)
  221. {
  222. platform_driver_unregister(&bfin_rotary_device_driver);
  223. }
  224. module_exit(bfin_rotary_exit);
  225. MODULE_LICENSE("GPL");
  226. MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
  227. MODULE_DESCRIPTION("Rotary Counter driver for Blackfin Processors");
  228. MODULE_ALIAS("platform:bfin-rotary");