1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2021 Pengutronix, Oleksij Rempel <kernel@pengutronix.de>
6 #include <linux/counter.h>
7 #include <linux/gpio/consumer.h>
8 #include <linux/interrupt.h>
10 #include <linux/mod_devicetable.h>
11 #include <linux/module.h>
12 #include <linux/platform_device.h>
14 #define INTERRUPT_CNT_NAME "interrupt-cnt"
16 struct interrupt_cnt_priv {
18 struct counter_device counter;
19 struct gpio_desc *gpio;
22 struct counter_signal signals;
23 struct counter_synapse synapses;
24 struct counter_count cnts;
27 static irqreturn_t interrupt_cnt_isr(int irq, void *dev_id)
29 struct interrupt_cnt_priv *priv = dev_id;
31 atomic_inc(&priv->count);
36 static ssize_t interrupt_cnt_enable_read(struct counter_device *counter,
37 struct counter_count *count,
38 void *private, char *buf)
40 struct interrupt_cnt_priv *priv = counter->priv;
42 return sysfs_emit(buf, "%d\n", priv->enabled);
45 static ssize_t interrupt_cnt_enable_write(struct counter_device *counter,
46 struct counter_count *count,
47 void *private, const char *buf,
50 struct interrupt_cnt_priv *priv = counter->priv;
54 ret = kstrtobool(buf, &enable);
58 if (priv->enabled == enable)
63 enable_irq(priv->irq);
65 disable_irq(priv->irq);
66 priv->enabled = false;
72 static const struct counter_count_ext interrupt_cnt_ext[] = {
75 .read = interrupt_cnt_enable_read,
76 .write = interrupt_cnt_enable_write,
80 static enum counter_synapse_action interrupt_cnt_synapse_actionss[] = {
81 COUNTER_SYNAPSE_ACTION_RISING_EDGE,
84 static int interrupt_cnt_action_get(struct counter_device *counter,
85 struct counter_count *count,
86 struct counter_synapse *synapse,
94 static int interrupt_cnt_read(struct counter_device *counter,
95 struct counter_count *count, unsigned long *val)
97 struct interrupt_cnt_priv *priv = counter->priv;
99 *val = atomic_read(&priv->count);
104 static int interrupt_cnt_write(struct counter_device *counter,
105 struct counter_count *count,
106 const unsigned long val)
108 struct interrupt_cnt_priv *priv = counter->priv;
110 atomic_set(&priv->count, val);
115 static enum counter_count_function interrupt_cnt_functions[] = {
116 COUNTER_COUNT_FUNCTION_INCREASE,
119 static int interrupt_cnt_function_get(struct counter_device *counter,
120 struct counter_count *count,
128 static int interrupt_cnt_signal_read(struct counter_device *counter,
129 struct counter_signal *signal,
130 enum counter_signal_value *val)
132 struct interrupt_cnt_priv *priv = counter->priv;
138 ret = gpiod_get_value(priv->gpio);
142 *val = ret ? COUNTER_SIGNAL_HIGH : COUNTER_SIGNAL_LOW;
147 static const struct counter_ops interrupt_cnt_ops = {
148 .action_get = interrupt_cnt_action_get,
149 .count_read = interrupt_cnt_read,
150 .count_write = interrupt_cnt_write,
151 .function_get = interrupt_cnt_function_get,
152 .signal_read = interrupt_cnt_signal_read,
155 static int interrupt_cnt_probe(struct platform_device *pdev)
157 struct device *dev = &pdev->dev;
158 struct interrupt_cnt_priv *priv;
161 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
165 priv->irq = platform_get_irq_optional(pdev, 0);
166 if (priv->irq == -ENXIO)
168 else if (priv->irq < 0)
169 return dev_err_probe(dev, priv->irq, "failed to get IRQ\n");
171 priv->gpio = devm_gpiod_get_optional(dev, NULL, GPIOD_IN);
172 if (IS_ERR(priv->gpio))
173 return dev_err_probe(dev, PTR_ERR(priv->gpio), "failed to get GPIO\n");
175 if (!priv->irq && !priv->gpio) {
176 dev_err(dev, "IRQ and GPIO are not found. At least one source should be provided\n");
181 int irq = gpiod_to_irq(priv->gpio);
184 return dev_err_probe(dev, irq, "failed to get IRQ from GPIO\n");
189 priv->signals.name = devm_kasprintf(dev, GFP_KERNEL, "IRQ %d",
191 if (!priv->signals.name)
194 priv->counter.signals = &priv->signals;
195 priv->counter.num_signals = 1;
197 priv->synapses.actions_list = interrupt_cnt_synapse_actionss;
198 priv->synapses.num_actions = ARRAY_SIZE(interrupt_cnt_synapse_actionss);
199 priv->synapses.signal = &priv->signals;
201 priv->cnts.name = "Channel 0 Count";
202 priv->cnts.functions_list = interrupt_cnt_functions;
203 priv->cnts.num_functions = ARRAY_SIZE(interrupt_cnt_functions);
204 priv->cnts.synapses = &priv->synapses;
205 priv->cnts.num_synapses = 1;
206 priv->cnts.ext = interrupt_cnt_ext;
207 priv->cnts.num_ext = ARRAY_SIZE(interrupt_cnt_ext);
209 priv->counter.priv = priv;
210 priv->counter.name = dev_name(dev);
211 priv->counter.parent = dev;
212 priv->counter.ops = &interrupt_cnt_ops;
213 priv->counter.counts = &priv->cnts;
214 priv->counter.num_counts = 1;
216 irq_set_status_flags(priv->irq, IRQ_NOAUTOEN);
217 ret = devm_request_irq(dev, priv->irq, interrupt_cnt_isr,
218 IRQF_TRIGGER_RISING | IRQF_NO_THREAD,
219 dev_name(dev), priv);
223 return devm_counter_register(dev, &priv->counter);
226 static const struct of_device_id interrupt_cnt_of_match[] = {
227 { .compatible = "interrupt-counter", },
230 MODULE_DEVICE_TABLE(of, interrupt_cnt_of_match);
232 static struct platform_driver interrupt_cnt_driver = {
233 .probe = interrupt_cnt_probe,
235 .name = INTERRUPT_CNT_NAME,
236 .of_match_table = interrupt_cnt_of_match,
239 module_platform_driver(interrupt_cnt_driver);
241 MODULE_ALIAS("platform:interrupt-counter");
242 MODULE_AUTHOR("Oleksij Rempel <o.rempel@pengutronix.de>");
243 MODULE_DESCRIPTION("Interrupt counter driver");
244 MODULE_LICENSE("GPL v2");