bfin_rotary.c 6.1 KB

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