interrupt-cnt.c 6.3 KB

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