blob: 9e99702470c2790a687362e91a1ceafe8f4e76e4 [file] [log] [blame]
Oleksij Rempela55ebd42021-03-01 09:04:01 +01001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright (c) 2021 Pengutronix, Oleksij Rempel <kernel@pengutronix.de>
4 */
5
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/platform_device.h>
William Breathitt Grayaaec1a02021-08-27 12:47:47 +090013#include <linux/types.h>
Oleksij Rempela55ebd42021-03-01 09:04:01 +010014
15#define INTERRUPT_CNT_NAME "interrupt-cnt"
16
17struct interrupt_cnt_priv {
18 atomic_t count;
Oleksij Rempela55ebd42021-03-01 09:04:01 +010019 struct gpio_desc *gpio;
20 int irq;
21 bool enabled;
22 struct counter_signal signals;
23 struct counter_synapse synapses;
24 struct counter_count cnts;
25};
26
27static irqreturn_t interrupt_cnt_isr(int irq, void *dev_id)
28{
29 struct interrupt_cnt_priv *priv = dev_id;
30
31 atomic_inc(&priv->count);
32
33 return IRQ_HANDLED;
34}
35
William Breathitt Grayaaec1a02021-08-27 12:47:47 +090036static int interrupt_cnt_enable_read(struct counter_device *counter,
37 struct counter_count *count, u8 *enable)
Oleksij Rempela55ebd42021-03-01 09:04:01 +010038{
Uwe Kleine-König63f0e2b2021-12-30 16:02:43 +010039 struct interrupt_cnt_priv *priv = counter_priv(counter);
Oleksij Rempela55ebd42021-03-01 09:04:01 +010040
William Breathitt Grayaaec1a02021-08-27 12:47:47 +090041 *enable = priv->enabled;
42
43 return 0;
Oleksij Rempela55ebd42021-03-01 09:04:01 +010044}
45
William Breathitt Grayaaec1a02021-08-27 12:47:47 +090046static int interrupt_cnt_enable_write(struct counter_device *counter,
47 struct counter_count *count, u8 enable)
Oleksij Rempela55ebd42021-03-01 09:04:01 +010048{
Uwe Kleine-König63f0e2b2021-12-30 16:02:43 +010049 struct interrupt_cnt_priv *priv = counter_priv(counter);
Oleksij Rempela55ebd42021-03-01 09:04:01 +010050
51 if (priv->enabled == enable)
William Breathitt Grayaaec1a02021-08-27 12:47:47 +090052 return 0;
Oleksij Rempela55ebd42021-03-01 09:04:01 +010053
54 if (enable) {
55 priv->enabled = true;
56 enable_irq(priv->irq);
57 } else {
58 disable_irq(priv->irq);
59 priv->enabled = false;
60 }
61
William Breathitt Grayaaec1a02021-08-27 12:47:47 +090062 return 0;
Oleksij Rempela55ebd42021-03-01 09:04:01 +010063}
64
William Breathitt Grayaaec1a02021-08-27 12:47:47 +090065static struct counter_comp interrupt_cnt_ext[] = {
66 COUNTER_COMP_ENABLE(interrupt_cnt_enable_read,
67 interrupt_cnt_enable_write),
Oleksij Rempela55ebd42021-03-01 09:04:01 +010068};
69
William Breathitt Graybc849572021-06-10 10:36:42 +090070static const enum counter_synapse_action interrupt_cnt_synapse_actions[] = {
Oleksij Rempela55ebd42021-03-01 09:04:01 +010071 COUNTER_SYNAPSE_ACTION_RISING_EDGE,
72};
73
William Breathitt Grayaaec1a02021-08-27 12:47:47 +090074static int interrupt_cnt_action_read(struct counter_device *counter,
75 struct counter_count *count,
76 struct counter_synapse *synapse,
77 enum counter_synapse_action *action)
Oleksij Rempela55ebd42021-03-01 09:04:01 +010078{
William Breathitt Grayaaec1a02021-08-27 12:47:47 +090079 *action = COUNTER_SYNAPSE_ACTION_RISING_EDGE;
Oleksij Rempela55ebd42021-03-01 09:04:01 +010080
81 return 0;
82}
83
84static int interrupt_cnt_read(struct counter_device *counter,
William Breathitt Grayaaec1a02021-08-27 12:47:47 +090085 struct counter_count *count, u64 *val)
Oleksij Rempela55ebd42021-03-01 09:04:01 +010086{
Uwe Kleine-König63f0e2b2021-12-30 16:02:43 +010087 struct interrupt_cnt_priv *priv = counter_priv(counter);
Oleksij Rempela55ebd42021-03-01 09:04:01 +010088
89 *val = atomic_read(&priv->count);
90
91 return 0;
92}
93
94static int interrupt_cnt_write(struct counter_device *counter,
William Breathitt Grayaaec1a02021-08-27 12:47:47 +090095 struct counter_count *count, const u64 val)
Oleksij Rempela55ebd42021-03-01 09:04:01 +010096{
Uwe Kleine-König63f0e2b2021-12-30 16:02:43 +010097 struct interrupt_cnt_priv *priv = counter_priv(counter);
Oleksij Rempela55ebd42021-03-01 09:04:01 +010098
William Breathitt Graye2ff3192021-08-03 21:06:13 +090099 if (val != (typeof(priv->count.counter))val)
100 return -ERANGE;
101
Oleksij Rempela55ebd42021-03-01 09:04:01 +0100102 atomic_set(&priv->count, val);
103
104 return 0;
105}
106
William Breathitt Gray394a0152021-08-03 21:06:15 +0900107static const enum counter_function interrupt_cnt_functions[] = {
108 COUNTER_FUNCTION_INCREASE,
Oleksij Rempela55ebd42021-03-01 09:04:01 +0100109};
110
William Breathitt Grayaaec1a02021-08-27 12:47:47 +0900111static int interrupt_cnt_function_read(struct counter_device *counter,
112 struct counter_count *count,
113 enum counter_function *function)
Oleksij Rempela55ebd42021-03-01 09:04:01 +0100114{
William Breathitt Grayaaec1a02021-08-27 12:47:47 +0900115 *function = COUNTER_FUNCTION_INCREASE;
Oleksij Rempela55ebd42021-03-01 09:04:01 +0100116
117 return 0;
118}
119
120static int interrupt_cnt_signal_read(struct counter_device *counter,
121 struct counter_signal *signal,
William Breathitt Gray493b9382021-08-03 21:06:14 +0900122 enum counter_signal_level *level)
Oleksij Rempela55ebd42021-03-01 09:04:01 +0100123{
Uwe Kleine-König63f0e2b2021-12-30 16:02:43 +0100124 struct interrupt_cnt_priv *priv = counter_priv(counter);
Oleksij Rempela55ebd42021-03-01 09:04:01 +0100125 int ret;
126
127 if (!priv->gpio)
128 return -EINVAL;
129
130 ret = gpiod_get_value(priv->gpio);
131 if (ret < 0)
132 return ret;
133
William Breathitt Gray493b9382021-08-03 21:06:14 +0900134 *level = ret ? COUNTER_SIGNAL_LEVEL_HIGH : COUNTER_SIGNAL_LEVEL_LOW;
Oleksij Rempela55ebd42021-03-01 09:04:01 +0100135
136 return 0;
137}
138
139static const struct counter_ops interrupt_cnt_ops = {
William Breathitt Grayaaec1a02021-08-27 12:47:47 +0900140 .action_read = interrupt_cnt_action_read,
Oleksij Rempela55ebd42021-03-01 09:04:01 +0100141 .count_read = interrupt_cnt_read,
142 .count_write = interrupt_cnt_write,
William Breathitt Grayaaec1a02021-08-27 12:47:47 +0900143 .function_read = interrupt_cnt_function_read,
Oleksij Rempela55ebd42021-03-01 09:04:01 +0100144 .signal_read = interrupt_cnt_signal_read,
145};
146
147static int interrupt_cnt_probe(struct platform_device *pdev)
148{
149 struct device *dev = &pdev->dev;
Uwe Kleine-Königaefc7e12021-12-30 16:02:53 +0100150 struct counter_device *counter;
Oleksij Rempela55ebd42021-03-01 09:04:01 +0100151 struct interrupt_cnt_priv *priv;
152 int ret;
153
Uwe Kleine-Königaefc7e12021-12-30 16:02:53 +0100154 counter = devm_counter_alloc(dev, sizeof(*priv));
155 if (!counter)
Oleksij Rempela55ebd42021-03-01 09:04:01 +0100156 return -ENOMEM;
Uwe Kleine-Königaefc7e12021-12-30 16:02:53 +0100157 priv = counter_priv(counter);
Oleksij Rempela55ebd42021-03-01 09:04:01 +0100158
159 priv->irq = platform_get_irq_optional(pdev, 0);
160 if (priv->irq == -ENXIO)
161 priv->irq = 0;
162 else if (priv->irq < 0)
163 return dev_err_probe(dev, priv->irq, "failed to get IRQ\n");
164
165 priv->gpio = devm_gpiod_get_optional(dev, NULL, GPIOD_IN);
166 if (IS_ERR(priv->gpio))
167 return dev_err_probe(dev, PTR_ERR(priv->gpio), "failed to get GPIO\n");
168
169 if (!priv->irq && !priv->gpio) {
170 dev_err(dev, "IRQ and GPIO are not found. At least one source should be provided\n");
171 return -ENODEV;
172 }
173
174 if (!priv->irq) {
175 int irq = gpiod_to_irq(priv->gpio);
176
177 if (irq < 0)
178 return dev_err_probe(dev, irq, "failed to get IRQ from GPIO\n");
179
180 priv->irq = irq;
181 }
182
183 priv->signals.name = devm_kasprintf(dev, GFP_KERNEL, "IRQ %d",
184 priv->irq);
185 if (!priv->signals.name)
186 return -ENOMEM;
187
Uwe Kleine-Königaefc7e12021-12-30 16:02:53 +0100188 counter->signals = &priv->signals;
189 counter->num_signals = 1;
Oleksij Rempela55ebd42021-03-01 09:04:01 +0100190
William Breathitt Graybc849572021-06-10 10:36:42 +0900191 priv->synapses.actions_list = interrupt_cnt_synapse_actions;
192 priv->synapses.num_actions = ARRAY_SIZE(interrupt_cnt_synapse_actions);
Oleksij Rempela55ebd42021-03-01 09:04:01 +0100193 priv->synapses.signal = &priv->signals;
194
195 priv->cnts.name = "Channel 0 Count";
196 priv->cnts.functions_list = interrupt_cnt_functions;
197 priv->cnts.num_functions = ARRAY_SIZE(interrupt_cnt_functions);
198 priv->cnts.synapses = &priv->synapses;
199 priv->cnts.num_synapses = 1;
200 priv->cnts.ext = interrupt_cnt_ext;
201 priv->cnts.num_ext = ARRAY_SIZE(interrupt_cnt_ext);
202
Uwe Kleine-Königaefc7e12021-12-30 16:02:53 +0100203 counter->name = dev_name(dev);
204 counter->parent = dev;
205 counter->ops = &interrupt_cnt_ops;
206 counter->counts = &priv->cnts;
207 counter->num_counts = 1;
Oleksij Rempela55ebd42021-03-01 09:04:01 +0100208
209 irq_set_status_flags(priv->irq, IRQ_NOAUTOEN);
210 ret = devm_request_irq(dev, priv->irq, interrupt_cnt_isr,
211 IRQF_TRIGGER_RISING | IRQF_NO_THREAD,
212 dev_name(dev), priv);
213 if (ret)
214 return ret;
215
Uwe Kleine-Königaefc7e12021-12-30 16:02:53 +0100216 ret = devm_counter_add(dev, counter);
217 if (ret < 0)
218 return dev_err_probe(dev, ret, "Failed to add counter\n");
219
220 return 0;
Oleksij Rempela55ebd42021-03-01 09:04:01 +0100221}
222
223static const struct of_device_id interrupt_cnt_of_match[] = {
224 { .compatible = "interrupt-counter", },
225 {}
226};
227MODULE_DEVICE_TABLE(of, interrupt_cnt_of_match);
228
229static struct platform_driver interrupt_cnt_driver = {
230 .probe = interrupt_cnt_probe,
231 .driver = {
232 .name = INTERRUPT_CNT_NAME,
233 .of_match_table = interrupt_cnt_of_match,
234 },
235};
236module_platform_driver(interrupt_cnt_driver);
237
238MODULE_ALIAS("platform:interrupt-counter");
239MODULE_AUTHOR("Oleksij Rempel <o.rempel@pengutronix.de>");
240MODULE_DESCRIPTION("Interrupt counter driver");
241MODULE_LICENSE("GPL v2");