rotary_encoder.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. /*
  2. * rotary_encoder.c
  3. *
  4. * (c) 2009 Daniel Mack <daniel@caiaq.de>
  5. * Copyright (C) 2011 Johan Hovold <jhovold@gmail.com>
  6. *
  7. * state machine code inspired by code from Tim Ruetz
  8. *
  9. * A generic driver for rotary encoders connected to GPIO lines.
  10. * See file:Documentation/input/rotary-encoder.txt for more information
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License version 2 as
  14. * published by the Free Software Foundation.
  15. */
  16. #include <linux/kernel.h>
  17. #include <linux/module.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/input.h>
  20. #include <linux/device.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/gpio/consumer.h>
  23. #include <linux/slab.h>
  24. #include <linux/of.h>
  25. #include <linux/pm.h>
  26. #include <linux/property.h>
  27. #define DRV_NAME "rotary-encoder"
  28. enum rotary_encoder_encoding {
  29. ROTENC_GRAY,
  30. ROTENC_BINARY,
  31. };
  32. struct rotary_encoder {
  33. struct input_dev *input;
  34. struct mutex access_mutex;
  35. u32 steps;
  36. u32 axis;
  37. bool relative_axis;
  38. bool rollover;
  39. enum rotary_encoder_encoding encoding;
  40. unsigned int pos;
  41. struct gpio_descs *gpios;
  42. unsigned int *irq;
  43. bool armed;
  44. signed char dir; /* 1 - clockwise, -1 - CCW */
  45. unsigned int last_stable;
  46. };
  47. static unsigned int rotary_encoder_get_state(struct rotary_encoder *encoder)
  48. {
  49. int i;
  50. unsigned int ret = 0;
  51. for (i = 0; i < encoder->gpios->ndescs; ++i) {
  52. int val = gpiod_get_value_cansleep(encoder->gpios->desc[i]);
  53. /* convert from gray encoding to normal */
  54. if (encoder->encoding == ROTENC_GRAY && ret & 1)
  55. val = !val;
  56. ret = ret << 1 | val;
  57. }
  58. return ret & 3;
  59. }
  60. static void rotary_encoder_report_event(struct rotary_encoder *encoder)
  61. {
  62. if (encoder->relative_axis) {
  63. input_report_rel(encoder->input,
  64. encoder->axis, encoder->dir);
  65. } else {
  66. unsigned int pos = encoder->pos;
  67. if (encoder->dir < 0) {
  68. /* turning counter-clockwise */
  69. if (encoder->rollover)
  70. pos += encoder->steps;
  71. if (pos)
  72. pos--;
  73. } else {
  74. /* turning clockwise */
  75. if (encoder->rollover || pos < encoder->steps)
  76. pos++;
  77. }
  78. if (encoder->rollover)
  79. pos %= encoder->steps;
  80. encoder->pos = pos;
  81. input_report_abs(encoder->input, encoder->axis, encoder->pos);
  82. }
  83. input_sync(encoder->input);
  84. }
  85. static irqreturn_t rotary_encoder_irq(int irq, void *dev_id)
  86. {
  87. struct rotary_encoder *encoder = dev_id;
  88. unsigned int state;
  89. mutex_lock(&encoder->access_mutex);
  90. state = rotary_encoder_get_state(encoder);
  91. switch (state) {
  92. case 0x0:
  93. if (encoder->armed) {
  94. rotary_encoder_report_event(encoder);
  95. encoder->armed = false;
  96. }
  97. break;
  98. case 0x1:
  99. case 0x3:
  100. if (encoder->armed)
  101. encoder->dir = 2 - state;
  102. break;
  103. case 0x2:
  104. encoder->armed = true;
  105. break;
  106. }
  107. mutex_unlock(&encoder->access_mutex);
  108. return IRQ_HANDLED;
  109. }
  110. static irqreturn_t rotary_encoder_half_period_irq(int irq, void *dev_id)
  111. {
  112. struct rotary_encoder *encoder = dev_id;
  113. unsigned int state;
  114. mutex_lock(&encoder->access_mutex);
  115. state = rotary_encoder_get_state(encoder);
  116. if (state & 1) {
  117. encoder->dir = ((encoder->last_stable - state + 1) % 4) - 1;
  118. } else {
  119. if (state != encoder->last_stable) {
  120. rotary_encoder_report_event(encoder);
  121. encoder->last_stable = state;
  122. }
  123. }
  124. mutex_unlock(&encoder->access_mutex);
  125. return IRQ_HANDLED;
  126. }
  127. static irqreturn_t rotary_encoder_quarter_period_irq(int irq, void *dev_id)
  128. {
  129. struct rotary_encoder *encoder = dev_id;
  130. unsigned int state;
  131. mutex_lock(&encoder->access_mutex);
  132. state = rotary_encoder_get_state(encoder);
  133. if ((encoder->last_stable + 1) % 4 == state)
  134. encoder->dir = 1;
  135. else if (encoder->last_stable == (state + 1) % 4)
  136. encoder->dir = -1;
  137. else
  138. goto out;
  139. rotary_encoder_report_event(encoder);
  140. out:
  141. encoder->last_stable = state;
  142. mutex_unlock(&encoder->access_mutex);
  143. return IRQ_HANDLED;
  144. }
  145. static int rotary_encoder_probe(struct platform_device *pdev)
  146. {
  147. struct device *dev = &pdev->dev;
  148. struct rotary_encoder *encoder;
  149. struct input_dev *input;
  150. irq_handler_t handler;
  151. u32 steps_per_period;
  152. unsigned int i;
  153. int err;
  154. encoder = devm_kzalloc(dev, sizeof(struct rotary_encoder), GFP_KERNEL);
  155. if (!encoder)
  156. return -ENOMEM;
  157. mutex_init(&encoder->access_mutex);
  158. device_property_read_u32(dev, "rotary-encoder,steps", &encoder->steps);
  159. err = device_property_read_u32(dev, "rotary-encoder,steps-per-period",
  160. &steps_per_period);
  161. if (err) {
  162. /*
  163. * The 'half-period' property has been deprecated, you must
  164. * use 'steps-per-period' and set an appropriate value, but
  165. * we still need to parse it to maintain compatibility. If
  166. * neither property is present we fall back to the one step
  167. * per period behavior.
  168. */
  169. steps_per_period = device_property_read_bool(dev,
  170. "rotary-encoder,half-period") ? 2 : 1;
  171. }
  172. encoder->rollover =
  173. device_property_read_bool(dev, "rotary-encoder,rollover");
  174. if (!device_property_present(dev, "rotary-encoder,encoding") ||
  175. !device_property_match_string(dev, "rotary-encoder,encoding",
  176. "gray")) {
  177. dev_info(dev, "gray");
  178. encoder->encoding = ROTENC_GRAY;
  179. } else if (!device_property_match_string(dev, "rotary-encoder,encoding",
  180. "binary")) {
  181. dev_info(dev, "binary");
  182. encoder->encoding = ROTENC_BINARY;
  183. } else {
  184. dev_err(dev, "unknown encoding setting\n");
  185. return -EINVAL;
  186. }
  187. device_property_read_u32(dev, "linux,axis", &encoder->axis);
  188. encoder->relative_axis =
  189. device_property_read_bool(dev, "rotary-encoder,relative-axis");
  190. encoder->gpios = devm_gpiod_get_array(dev, NULL, GPIOD_IN);
  191. if (IS_ERR(encoder->gpios)) {
  192. dev_err(dev, "unable to get gpios\n");
  193. return PTR_ERR(encoder->gpios);
  194. }
  195. if (encoder->gpios->ndescs < 2) {
  196. dev_err(dev, "not enough gpios found\n");
  197. return -EINVAL;
  198. }
  199. input = devm_input_allocate_device(dev);
  200. if (!input)
  201. return -ENOMEM;
  202. encoder->input = input;
  203. input->name = pdev->name;
  204. input->id.bustype = BUS_HOST;
  205. input->dev.parent = dev;
  206. if (encoder->relative_axis)
  207. input_set_capability(input, EV_REL, encoder->axis);
  208. else
  209. input_set_abs_params(input,
  210. encoder->axis, 0, encoder->steps, 0, 1);
  211. switch (steps_per_period >> (encoder->gpios->ndescs - 2)) {
  212. case 4:
  213. handler = &rotary_encoder_quarter_period_irq;
  214. encoder->last_stable = rotary_encoder_get_state(encoder);
  215. break;
  216. case 2:
  217. handler = &rotary_encoder_half_period_irq;
  218. encoder->last_stable = rotary_encoder_get_state(encoder);
  219. break;
  220. case 1:
  221. handler = &rotary_encoder_irq;
  222. break;
  223. default:
  224. dev_err(dev, "'%d' is not a valid steps-per-period value\n",
  225. steps_per_period);
  226. return -EINVAL;
  227. }
  228. encoder->irq =
  229. devm_kzalloc(dev,
  230. sizeof(*encoder->irq) * encoder->gpios->ndescs,
  231. GFP_KERNEL);
  232. if (!encoder->irq)
  233. return -ENOMEM;
  234. for (i = 0; i < encoder->gpios->ndescs; ++i) {
  235. encoder->irq[i] = gpiod_to_irq(encoder->gpios->desc[i]);
  236. err = devm_request_threaded_irq(dev, encoder->irq[i],
  237. NULL, handler,
  238. IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING |
  239. IRQF_ONESHOT,
  240. DRV_NAME, encoder);
  241. if (err) {
  242. dev_err(dev, "unable to request IRQ %d (gpio#%d)\n",
  243. encoder->irq[i], i);
  244. return err;
  245. }
  246. }
  247. err = input_register_device(input);
  248. if (err) {
  249. dev_err(dev, "failed to register input device\n");
  250. return err;
  251. }
  252. device_init_wakeup(dev,
  253. device_property_read_bool(dev, "wakeup-source"));
  254. platform_set_drvdata(pdev, encoder);
  255. return 0;
  256. }
  257. static int __maybe_unused rotary_encoder_suspend(struct device *dev)
  258. {
  259. struct rotary_encoder *encoder = dev_get_drvdata(dev);
  260. unsigned int i;
  261. if (device_may_wakeup(dev)) {
  262. for (i = 0; i < encoder->gpios->ndescs; ++i)
  263. enable_irq_wake(encoder->irq[i]);
  264. }
  265. return 0;
  266. }
  267. static int __maybe_unused rotary_encoder_resume(struct device *dev)
  268. {
  269. struct rotary_encoder *encoder = dev_get_drvdata(dev);
  270. unsigned int i;
  271. if (device_may_wakeup(dev)) {
  272. for (i = 0; i < encoder->gpios->ndescs; ++i)
  273. disable_irq_wake(encoder->irq[i]);
  274. }
  275. return 0;
  276. }
  277. static SIMPLE_DEV_PM_OPS(rotary_encoder_pm_ops,
  278. rotary_encoder_suspend, rotary_encoder_resume);
  279. #ifdef CONFIG_OF
  280. static const struct of_device_id rotary_encoder_of_match[] = {
  281. { .compatible = "rotary-encoder", },
  282. { },
  283. };
  284. MODULE_DEVICE_TABLE(of, rotary_encoder_of_match);
  285. #endif
  286. static struct platform_driver rotary_encoder_driver = {
  287. .probe = rotary_encoder_probe,
  288. .driver = {
  289. .name = DRV_NAME,
  290. .pm = &rotary_encoder_pm_ops,
  291. .of_match_table = of_match_ptr(rotary_encoder_of_match),
  292. }
  293. };
  294. module_platform_driver(rotary_encoder_driver);
  295. MODULE_ALIAS("platform:" DRV_NAME);
  296. MODULE_DESCRIPTION("GPIO rotary encoder driver");
  297. MODULE_AUTHOR("Daniel Mack <daniel@caiaq.de>, Johan Hovold");
  298. MODULE_LICENSE("GPL v2");