bfin_rotary.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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(dev));
  122. if (error) {
  123. dev_err(dev, "requesting peripherals failed: %d\n",
  124. error);
  125. return error;
  126. }
  127. error = devm_add_action_or_reset(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. return error;
  133. }
  134. }
  135. rotary = devm_kzalloc(dev, sizeof(struct bfin_rot), GFP_KERNEL);
  136. if (!rotary)
  137. return -ENOMEM;
  138. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  139. rotary->base = devm_ioremap_resource(dev, res);
  140. if (IS_ERR(rotary->base))
  141. return PTR_ERR(rotary->base);
  142. input = devm_input_allocate_device(dev);
  143. if (!input)
  144. return -ENOMEM;
  145. rotary->input = input;
  146. rotary->up_key = pdata->rotary_up_key;
  147. rotary->down_key = pdata->rotary_down_key;
  148. rotary->button_key = pdata->rotary_button_key;
  149. rotary->rel_code = pdata->rotary_rel_code;
  150. rotary->mode = pdata->mode;
  151. rotary->debounce = pdata->debounce;
  152. input->name = pdev->name;
  153. input->phys = "bfin-rotary/input0";
  154. input->dev.parent = dev;
  155. input_set_drvdata(input, rotary);
  156. input->id.bustype = BUS_HOST;
  157. input->id.vendor = 0x0001;
  158. input->id.product = 0x0001;
  159. input->id.version = 0x0100;
  160. input->open = bfin_rotary_open;
  161. input->close = bfin_rotary_close;
  162. if (rotary->up_key) {
  163. __set_bit(EV_KEY, input->evbit);
  164. __set_bit(rotary->up_key, input->keybit);
  165. __set_bit(rotary->down_key, input->keybit);
  166. } else {
  167. __set_bit(EV_REL, input->evbit);
  168. __set_bit(rotary->rel_code, input->relbit);
  169. }
  170. if (rotary->button_key) {
  171. __set_bit(EV_KEY, input->evbit);
  172. __set_bit(rotary->button_key, input->keybit);
  173. }
  174. /* Quiesce the device before requesting irq */
  175. bfin_rotary_close(input);
  176. rotary->irq = platform_get_irq(pdev, 0);
  177. if (rotary->irq < 0) {
  178. dev_err(dev, "No rotary IRQ specified\n");
  179. return -ENOENT;
  180. }
  181. error = devm_request_irq(dev, rotary->irq, bfin_rotary_isr,
  182. 0, dev_name(dev), rotary);
  183. if (error) {
  184. dev_err(dev, "unable to claim irq %d; error %d\n",
  185. rotary->irq, error);
  186. return error;
  187. }
  188. error = input_register_device(input);
  189. if (error) {
  190. dev_err(dev, "unable to register input device (%d)\n", error);
  191. return error;
  192. }
  193. platform_set_drvdata(pdev, rotary);
  194. device_init_wakeup(dev, 1);
  195. return 0;
  196. }
  197. static int __maybe_unused bfin_rotary_suspend(struct device *dev)
  198. {
  199. struct platform_device *pdev = to_platform_device(dev);
  200. struct bfin_rot *rotary = platform_get_drvdata(pdev);
  201. rotary->cnt_config = readw(rotary->base + CNT_CONFIG_OFF);
  202. rotary->cnt_imask = readw(rotary->base + CNT_IMASK_OFF);
  203. rotary->cnt_debounce = readw(rotary->base + CNT_DEBOUNCE_OFF);
  204. if (device_may_wakeup(&pdev->dev))
  205. enable_irq_wake(rotary->irq);
  206. return 0;
  207. }
  208. static int __maybe_unused bfin_rotary_resume(struct device *dev)
  209. {
  210. struct platform_device *pdev = to_platform_device(dev);
  211. struct bfin_rot *rotary = platform_get_drvdata(pdev);
  212. writew(rotary->cnt_debounce, rotary->base + CNT_DEBOUNCE_OFF);
  213. writew(rotary->cnt_imask, rotary->base + CNT_IMASK_OFF);
  214. writew(rotary->cnt_config & ~CNTE, rotary->base + CNT_CONFIG_OFF);
  215. if (device_may_wakeup(&pdev->dev))
  216. disable_irq_wake(rotary->irq);
  217. if (rotary->cnt_config & CNTE)
  218. writew(rotary->cnt_config, rotary->base + CNT_CONFIG_OFF);
  219. return 0;
  220. }
  221. static SIMPLE_DEV_PM_OPS(bfin_rotary_pm_ops,
  222. bfin_rotary_suspend, bfin_rotary_resume);
  223. static struct platform_driver bfin_rotary_device_driver = {
  224. .probe = bfin_rotary_probe,
  225. .driver = {
  226. .name = "bfin-rotary",
  227. .pm = &bfin_rotary_pm_ops,
  228. },
  229. };
  230. module_platform_driver(bfin_rotary_device_driver);
  231. MODULE_LICENSE("GPL");
  232. MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
  233. MODULE_DESCRIPTION("Rotary Counter driver for Blackfin Processors");
  234. MODULE_ALIAS("platform:bfin-rotary");