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