bfin_rotary.c 6.0 KB

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