bfin_rotary.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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/io.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 <linux/platform_data/bfin_rotary.h>
  16. #include <asm/portmux.h>
  17. #define CNT_CONFIG_OFF 0 /* CNT Config Offset */
  18. #define CNT_IMASK_OFF 4 /* CNT Interrupt Mask Offset */
  19. #define CNT_STATUS_OFF 8 /* CNT Status Offset */
  20. #define CNT_COMMAND_OFF 12 /* CNT Command Offset */
  21. #define CNT_DEBOUNCE_OFF 16 /* CNT Debounce Offset */
  22. #define CNT_COUNTER_OFF 20 /* CNT Counter Offset */
  23. #define CNT_MAX_OFF 24 /* CNT Maximum Count Offset */
  24. #define CNT_MIN_OFF 28 /* CNT Minimum Count Offset */
  25. struct bfin_rot {
  26. struct input_dev *input;
  27. void __iomem *base;
  28. int irq;
  29. unsigned int up_key;
  30. unsigned int down_key;
  31. unsigned int button_key;
  32. unsigned int rel_code;
  33. unsigned short mode;
  34. unsigned short debounce;
  35. unsigned short cnt_config;
  36. unsigned short cnt_imask;
  37. unsigned short cnt_debounce;
  38. };
  39. static void report_key_event(struct input_dev *input, int keycode)
  40. {
  41. /* simulate a press-n-release */
  42. input_report_key(input, keycode, 1);
  43. input_sync(input);
  44. input_report_key(input, keycode, 0);
  45. input_sync(input);
  46. }
  47. static void report_rotary_event(struct bfin_rot *rotary, int delta)
  48. {
  49. struct input_dev *input = rotary->input;
  50. if (rotary->up_key) {
  51. report_key_event(input,
  52. delta > 0 ? rotary->up_key : rotary->down_key);
  53. } else {
  54. input_report_rel(input, rotary->rel_code, delta);
  55. input_sync(input);
  56. }
  57. }
  58. static irqreturn_t bfin_rotary_isr(int irq, void *dev_id)
  59. {
  60. struct bfin_rot *rotary = dev_id;
  61. int delta;
  62. switch (readw(rotary->base + CNT_STATUS_OFF)) {
  63. case ICII:
  64. break;
  65. case UCII:
  66. case DCII:
  67. delta = readl(rotary->base + CNT_COUNTER_OFF);
  68. if (delta)
  69. report_rotary_event(rotary, delta);
  70. break;
  71. case CZMII:
  72. report_key_event(rotary->input, rotary->button_key);
  73. break;
  74. default:
  75. break;
  76. }
  77. writew(W1LCNT_ZERO, rotary->base + CNT_COMMAND_OFF); /* Clear COUNTER */
  78. writew(-1, rotary->base + CNT_STATUS_OFF); /* Clear STATUS */
  79. return IRQ_HANDLED;
  80. }
  81. static int bfin_rotary_open(struct input_dev *input)
  82. {
  83. struct bfin_rot *rotary = input_get_drvdata(input);
  84. unsigned short val;
  85. if (rotary->mode & ROT_DEBE)
  86. writew(rotary->debounce & DPRESCALE,
  87. rotary->base + CNT_DEBOUNCE_OFF);
  88. writew(rotary->mode & ~CNTE, rotary->base + CNT_CONFIG_OFF);
  89. val = UCIE | DCIE;
  90. if (rotary->button_key)
  91. val |= CZMIE;
  92. writew(val, rotary->base + CNT_IMASK_OFF);
  93. writew(rotary->mode | CNTE, rotary->base + CNT_CONFIG_OFF);
  94. return 0;
  95. }
  96. static void bfin_rotary_close(struct input_dev *input)
  97. {
  98. struct bfin_rot *rotary = input_get_drvdata(input);
  99. writew(0, rotary->base + CNT_CONFIG_OFF);
  100. writew(0, rotary->base + CNT_IMASK_OFF);
  101. }
  102. static void bfin_rotary_free_action(void *data)
  103. {
  104. peripheral_free_list(data);
  105. }
  106. static int bfin_rotary_probe(struct platform_device *pdev)
  107. {
  108. struct device *dev = &pdev->dev;
  109. const struct bfin_rotary_platform_data *pdata = dev_get_platdata(dev);
  110. struct bfin_rot *rotary;
  111. struct resource *res;
  112. struct input_dev *input;
  113. int error;
  114. /* Basic validation */
  115. if ((pdata->rotary_up_key && !pdata->rotary_down_key) ||
  116. (!pdata->rotary_up_key && pdata->rotary_down_key)) {
  117. return -EINVAL;
  118. }
  119. if (pdata->pin_list) {
  120. error = peripheral_request_list(pdata->pin_list,
  121. dev_name(&pdev->dev));
  122. if (error) {
  123. dev_err(dev, "requesting peripherals failed: %d\n",
  124. error);
  125. return error;
  126. }
  127. error = devm_add_action(dev, bfin_rotary_free_action,
  128. pdata->pin_list);
  129. if (error) {
  130. dev_err(dev, "setting cleanup action failed: %d\n",
  131. error);
  132. peripheral_free_list(pdata->pin_list);
  133. return error;
  134. }
  135. }
  136. rotary = devm_kzalloc(dev, sizeof(struct bfin_rot), GFP_KERNEL);
  137. if (!rotary)
  138. return -ENOMEM;
  139. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  140. rotary->base = devm_ioremap_resource(dev, res);
  141. if (IS_ERR(rotary->base))
  142. return PTR_ERR(rotary->base);
  143. input = devm_input_allocate_device(dev);
  144. if (!input)
  145. return -ENOMEM;
  146. rotary->input = input;
  147. rotary->up_key = pdata->rotary_up_key;
  148. rotary->down_key = pdata->rotary_down_key;
  149. rotary->button_key = pdata->rotary_button_key;
  150. rotary->rel_code = pdata->rotary_rel_code;
  151. rotary->mode = pdata->mode;
  152. rotary->debounce = pdata->debounce;
  153. input->name = pdev->name;
  154. input->phys = "bfin-rotary/input0";
  155. input->dev.parent = &pdev->dev;
  156. input_set_drvdata(input, rotary);
  157. input->id.bustype = BUS_HOST;
  158. input->id.vendor = 0x0001;
  159. input->id.product = 0x0001;
  160. input->id.version = 0x0100;
  161. input->open = bfin_rotary_open;
  162. input->close = bfin_rotary_close;
  163. if (rotary->up_key) {
  164. __set_bit(EV_KEY, input->evbit);
  165. __set_bit(rotary->up_key, input->keybit);
  166. __set_bit(rotary->down_key, input->keybit);
  167. } else {
  168. __set_bit(EV_REL, input->evbit);
  169. __set_bit(rotary->rel_code, input->relbit);
  170. }
  171. if (rotary->button_key) {
  172. __set_bit(EV_KEY, input->evbit);
  173. __set_bit(rotary->button_key, input->keybit);
  174. }
  175. /* Quiesce the device before requesting irq */
  176. bfin_rotary_close(input);
  177. rotary->irq = platform_get_irq(pdev, 0);
  178. if (rotary->irq < 0) {
  179. dev_err(dev, "No rotary IRQ specified\n");
  180. return -ENOENT;
  181. }
  182. error = devm_request_irq(dev, rotary->irq, bfin_rotary_isr,
  183. 0, dev_name(dev), rotary);
  184. if (error) {
  185. dev_err(dev, "unable to claim irq %d; error %d\n",
  186. rotary->irq, error);
  187. return error;
  188. }
  189. error = input_register_device(input);
  190. if (error) {
  191. dev_err(dev, "unable to register input device (%d)\n", error);
  192. return error;
  193. }
  194. platform_set_drvdata(pdev, rotary);
  195. device_init_wakeup(&pdev->dev, 1);
  196. return 0;
  197. }
  198. static int __maybe_unused bfin_rotary_suspend(struct device *dev)
  199. {
  200. struct platform_device *pdev = to_platform_device(dev);
  201. struct bfin_rot *rotary = platform_get_drvdata(pdev);
  202. rotary->cnt_config = readw(rotary->base + CNT_CONFIG_OFF);
  203. rotary->cnt_imask = readw(rotary->base + CNT_IMASK_OFF);
  204. rotary->cnt_debounce = readw(rotary->base + CNT_DEBOUNCE_OFF);
  205. if (device_may_wakeup(&pdev->dev))
  206. enable_irq_wake(rotary->irq);
  207. return 0;
  208. }
  209. static int __maybe_unused bfin_rotary_resume(struct device *dev)
  210. {
  211. struct platform_device *pdev = to_platform_device(dev);
  212. struct bfin_rot *rotary = platform_get_drvdata(pdev);
  213. writew(rotary->cnt_debounce, rotary->base + CNT_DEBOUNCE_OFF);
  214. writew(rotary->cnt_imask, rotary->base + CNT_IMASK_OFF);
  215. writew(rotary->cnt_config & ~CNTE, rotary->base + CNT_CONFIG_OFF);
  216. if (device_may_wakeup(&pdev->dev))
  217. disable_irq_wake(rotary->irq);
  218. if (rotary->cnt_config & CNTE)
  219. writew(rotary->cnt_config, rotary->base + CNT_CONFIG_OFF);
  220. return 0;
  221. }
  222. static SIMPLE_DEV_PM_OPS(bfin_rotary_pm_ops,
  223. bfin_rotary_suspend, bfin_rotary_resume);
  224. static struct platform_driver bfin_rotary_device_driver = {
  225. .probe = bfin_rotary_probe,
  226. .driver = {
  227. .name = "bfin-rotary",
  228. .pm = &bfin_rotary_pm_ops,
  229. },
  230. };
  231. module_platform_driver(bfin_rotary_device_driver);
  232. MODULE_LICENSE("GPL");
  233. MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
  234. MODULE_DESCRIPTION("Rotary Counter driver for Blackfin Processors");
  235. MODULE_ALIAS("platform:bfin-rotary");