interrupt-cnt.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2021 Pengutronix, Oleksij Rempel <kernel@pengutronix.de>
  4. */
  5. #include <linux/cleanup.h>
  6. #include <linux/counter.h>
  7. #include <linux/gpio/consumer.h>
  8. #include <linux/interrupt.h>
  9. #include <linux/irq.h>
  10. #include <linux/mod_devicetable.h>
  11. #include <linux/module.h>
  12. #include <linux/mutex.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/types.h>
  15. #define INTERRUPT_CNT_NAME "interrupt-cnt"
  16. struct interrupt_cnt_priv {
  17. atomic_long_t count;
  18. struct gpio_desc *gpio;
  19. int irq;
  20. bool enabled;
  21. struct mutex lock;
  22. struct counter_signal signals;
  23. struct counter_synapse synapses;
  24. struct counter_count cnts;
  25. };
  26. static irqreturn_t interrupt_cnt_isr(int irq, void *dev_id)
  27. {
  28. struct counter_device *counter = dev_id;
  29. struct interrupt_cnt_priv *priv = counter_priv(counter);
  30. atomic_long_inc(&priv->count);
  31. counter_push_event(counter, COUNTER_EVENT_CHANGE_OF_STATE, 0);
  32. return IRQ_HANDLED;
  33. }
  34. static int interrupt_cnt_enable_read(struct counter_device *counter,
  35. struct counter_count *count, u8 *enable)
  36. {
  37. struct interrupt_cnt_priv *priv = counter_priv(counter);
  38. guard(mutex)(&priv->lock);
  39. *enable = priv->enabled;
  40. return 0;
  41. }
  42. static int interrupt_cnt_enable_write(struct counter_device *counter,
  43. struct counter_count *count, u8 enable)
  44. {
  45. struct interrupt_cnt_priv *priv = counter_priv(counter);
  46. guard(mutex)(&priv->lock);
  47. if (priv->enabled == enable)
  48. return 0;
  49. if (enable) {
  50. priv->enabled = true;
  51. enable_irq(priv->irq);
  52. } else {
  53. disable_irq(priv->irq);
  54. priv->enabled = false;
  55. }
  56. return 0;
  57. }
  58. static struct counter_comp interrupt_cnt_ext[] = {
  59. COUNTER_COMP_ENABLE(interrupt_cnt_enable_read,
  60. interrupt_cnt_enable_write),
  61. };
  62. static const enum counter_synapse_action interrupt_cnt_synapse_actions[] = {
  63. COUNTER_SYNAPSE_ACTION_RISING_EDGE,
  64. };
  65. static int interrupt_cnt_action_read(struct counter_device *counter,
  66. struct counter_count *count,
  67. struct counter_synapse *synapse,
  68. enum counter_synapse_action *action)
  69. {
  70. *action = COUNTER_SYNAPSE_ACTION_RISING_EDGE;
  71. return 0;
  72. }
  73. static int interrupt_cnt_read(struct counter_device *counter,
  74. struct counter_count *count, u64 *val)
  75. {
  76. struct interrupt_cnt_priv *priv = counter_priv(counter);
  77. *val = atomic_long_read(&priv->count);
  78. return 0;
  79. }
  80. static int interrupt_cnt_write(struct counter_device *counter,
  81. struct counter_count *count, const u64 val)
  82. {
  83. struct interrupt_cnt_priv *priv = counter_priv(counter);
  84. if (val != (typeof(priv->count.counter))val)
  85. return -ERANGE;
  86. atomic_long_set(&priv->count, val);
  87. return 0;
  88. }
  89. static const enum counter_function interrupt_cnt_functions[] = {
  90. COUNTER_FUNCTION_INCREASE,
  91. };
  92. static int interrupt_cnt_function_read(struct counter_device *counter,
  93. struct counter_count *count,
  94. enum counter_function *function)
  95. {
  96. *function = COUNTER_FUNCTION_INCREASE;
  97. return 0;
  98. }
  99. static int interrupt_cnt_signal_read(struct counter_device *counter,
  100. struct counter_signal *signal,
  101. enum counter_signal_level *level)
  102. {
  103. struct interrupt_cnt_priv *priv = counter_priv(counter);
  104. int ret;
  105. if (!priv->gpio)
  106. return -EINVAL;
  107. ret = gpiod_get_value(priv->gpio);
  108. if (ret < 0)
  109. return ret;
  110. *level = ret ? COUNTER_SIGNAL_LEVEL_HIGH : COUNTER_SIGNAL_LEVEL_LOW;
  111. return 0;
  112. }
  113. static int interrupt_cnt_watch_validate(struct counter_device *counter,
  114. const struct counter_watch *watch)
  115. {
  116. if (watch->channel != 0 ||
  117. watch->event != COUNTER_EVENT_CHANGE_OF_STATE)
  118. return -EINVAL;
  119. return 0;
  120. }
  121. static const struct counter_ops interrupt_cnt_ops = {
  122. .action_read = interrupt_cnt_action_read,
  123. .count_read = interrupt_cnt_read,
  124. .count_write = interrupt_cnt_write,
  125. .function_read = interrupt_cnt_function_read,
  126. .signal_read = interrupt_cnt_signal_read,
  127. .watch_validate = interrupt_cnt_watch_validate,
  128. };
  129. static int interrupt_cnt_probe(struct platform_device *pdev)
  130. {
  131. struct device *dev = &pdev->dev;
  132. struct counter_device *counter;
  133. struct interrupt_cnt_priv *priv;
  134. int ret;
  135. counter = devm_counter_alloc(dev, sizeof(*priv));
  136. if (!counter)
  137. return -ENOMEM;
  138. priv = counter_priv(counter);
  139. priv->irq = platform_get_irq_optional(pdev, 0);
  140. if (priv->irq == -ENXIO)
  141. priv->irq = 0;
  142. else if (priv->irq < 0)
  143. return dev_err_probe(dev, priv->irq, "failed to get IRQ\n");
  144. priv->gpio = devm_gpiod_get_optional(dev, NULL, GPIOD_IN);
  145. if (IS_ERR(priv->gpio))
  146. return dev_err_probe(dev, PTR_ERR(priv->gpio), "failed to get GPIO\n");
  147. if (!priv->irq && !priv->gpio) {
  148. dev_err(dev, "IRQ and GPIO are not found. At least one source should be provided\n");
  149. return -ENODEV;
  150. }
  151. if (!priv->irq) {
  152. int irq = gpiod_to_irq(priv->gpio);
  153. if (irq < 0)
  154. return dev_err_probe(dev, irq, "failed to get IRQ from GPIO\n");
  155. priv->irq = irq;
  156. }
  157. priv->signals.name = devm_kasprintf(dev, GFP_KERNEL, "IRQ %d",
  158. priv->irq);
  159. if (!priv->signals.name)
  160. return -ENOMEM;
  161. counter->signals = &priv->signals;
  162. counter->num_signals = 1;
  163. priv->synapses.actions_list = interrupt_cnt_synapse_actions;
  164. priv->synapses.num_actions = ARRAY_SIZE(interrupt_cnt_synapse_actions);
  165. priv->synapses.signal = &priv->signals;
  166. priv->cnts.name = "Channel 0 Count";
  167. priv->cnts.functions_list = interrupt_cnt_functions;
  168. priv->cnts.num_functions = ARRAY_SIZE(interrupt_cnt_functions);
  169. priv->cnts.synapses = &priv->synapses;
  170. priv->cnts.num_synapses = 1;
  171. priv->cnts.ext = interrupt_cnt_ext;
  172. priv->cnts.num_ext = ARRAY_SIZE(interrupt_cnt_ext);
  173. counter->name = dev_name(dev);
  174. counter->parent = dev;
  175. counter->ops = &interrupt_cnt_ops;
  176. counter->counts = &priv->cnts;
  177. counter->num_counts = 1;
  178. irq_set_status_flags(priv->irq, IRQ_NOAUTOEN);
  179. ret = devm_request_irq(dev, priv->irq, interrupt_cnt_isr,
  180. IRQF_TRIGGER_RISING, dev_name(dev), counter);
  181. if (ret)
  182. return ret;
  183. mutex_init(&priv->lock);
  184. ret = devm_counter_add(dev, counter);
  185. if (ret < 0)
  186. return dev_err_probe(dev, ret, "Failed to add counter\n");
  187. return 0;
  188. }
  189. static const struct of_device_id interrupt_cnt_of_match[] = {
  190. { .compatible = "interrupt-counter", },
  191. {}
  192. };
  193. MODULE_DEVICE_TABLE(of, interrupt_cnt_of_match);
  194. static struct platform_driver interrupt_cnt_driver = {
  195. .probe = interrupt_cnt_probe,
  196. .driver = {
  197. .name = INTERRUPT_CNT_NAME,
  198. .of_match_table = interrupt_cnt_of_match,
  199. },
  200. };
  201. module_platform_driver(interrupt_cnt_driver);
  202. MODULE_ALIAS("platform:interrupt-counter");
  203. MODULE_AUTHOR("Oleksij Rempel <o.rempel@pengutronix.de>");
  204. MODULE_DESCRIPTION("Interrupt counter driver");
  205. MODULE_LICENSE("GPL v2");
  206. MODULE_IMPORT_NS("COUNTER");