summaryrefslogtreecommitdiff
path: root/drivers/counter/interrupt-cnt.c
blob: 1de4243db488c7baab33f908f48a0efe0928204a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
// SPDX-License-Identifier: GPL-2.0
/*
 * Copyright (c) 2021 Pengutronix, Oleksij Rempel <kernel@pengutronix.de>
 */

#include <linux/counter.h>
#include <linux/gpio/consumer.h>
#include <linux/interrupt.h>
#include <linux/irq.h>
#include <linux/mod_devicetable.h>
#include <linux/module.h>
#include <linux/platform_device.h>

#define INTERRUPT_CNT_NAME "interrupt-cnt"

struct interrupt_cnt_priv {
	atomic_t count;
	struct counter_device counter;
	struct gpio_desc *gpio;
	int irq;
	bool enabled;
	struct counter_signal signals;
	struct counter_synapse synapses;
	struct counter_count cnts;
};

static irqreturn_t interrupt_cnt_isr(int irq, void *dev_id)
{
	struct interrupt_cnt_priv *priv = dev_id;

	atomic_inc(&priv->count);

	return IRQ_HANDLED;
}

static ssize_t interrupt_cnt_enable_read(struct counter_device *counter,
					 struct counter_count *count,
					 void *private, char *buf)
{
	struct interrupt_cnt_priv *priv = counter->priv;

	return sysfs_emit(buf, "%d\n", priv->enabled);
}

static ssize_t interrupt_cnt_enable_write(struct counter_device *counter,
					  struct counter_count *count,
					  void *private, const char *buf,
					  size_t len)
{
	struct interrupt_cnt_priv *priv = counter->priv;
	bool enable;
	ssize_t ret;

	ret = kstrtobool(buf, &enable);
	if (ret)
		return ret;

	if (priv->enabled == enable)
		return len;

	if (enable) {
		priv->enabled = true;
		enable_irq(priv->irq);
	} else {
		disable_irq(priv->irq);
		priv->enabled = false;
	}

	return len;
}

static const struct counter_count_ext interrupt_cnt_ext[] = {
	{
		.name = "enable",
		.read = interrupt_cnt_enable_read,
		.write = interrupt_cnt_enable_write,
	},
};

static const enum counter_synapse_action interrupt_cnt_synapse_actions[] = {
	COUNTER_SYNAPSE_ACTION_RISING_EDGE,
};

static int interrupt_cnt_action_get(struct counter_device *counter,
				    struct counter_count *count,
				    struct counter_synapse *synapse,
				    size_t *action)
{
	*action = 0;

	return 0;
}

static int interrupt_cnt_read(struct counter_device *counter,
			      struct counter_count *count, unsigned long *val)
{
	struct interrupt_cnt_priv *priv = counter->priv;

	*val = atomic_read(&priv->count);

	return 0;
}

static int interrupt_cnt_write(struct counter_device *counter,
			       struct counter_count *count,
			       const unsigned long val)
{
	struct interrupt_cnt_priv *priv = counter->priv;

	if (val != (typeof(priv->count.counter))val)
		return -ERANGE;

	atomic_set(&priv->count, val);

	return 0;
}

static const enum counter_function interrupt_cnt_functions[] = {
	COUNTER_FUNCTION_INCREASE,
};

static int interrupt_cnt_function_get(struct counter_device *counter,
				      struct counter_count *count,
				      size_t *function)
{
	*function = 0;

	return 0;
}

static int interrupt_cnt_signal_read(struct counter_device *counter,
				     struct counter_signal *signal,
				     enum counter_signal_level *level)
{
	struct interrupt_cnt_priv *priv = counter->priv;
	int ret;

	if (!priv->gpio)
		return -EINVAL;

	ret = gpiod_get_value(priv->gpio);
	if (ret < 0)
		return ret;

	*level = ret ? COUNTER_SIGNAL_LEVEL_HIGH : COUNTER_SIGNAL_LEVEL_LOW;

	return 0;
}

static const struct counter_ops interrupt_cnt_ops = {
	.action_get = interrupt_cnt_action_get,
	.count_read = interrupt_cnt_read,
	.count_write = interrupt_cnt_write,
	.function_get = interrupt_cnt_function_get,
	.signal_read  = interrupt_cnt_signal_read,
};

static int interrupt_cnt_probe(struct platform_device *pdev)
{
	struct device *dev = &pdev->dev;
	struct interrupt_cnt_priv *priv;
	int ret;

	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
	if (!priv)
		return -ENOMEM;

	priv->irq = platform_get_irq_optional(pdev,  0);
	if (priv->irq == -ENXIO)
		priv->irq = 0;
	else if (priv->irq < 0)
		return dev_err_probe(dev, priv->irq, "failed to get IRQ\n");

	priv->gpio = devm_gpiod_get_optional(dev, NULL, GPIOD_IN);
	if (IS_ERR(priv->gpio))
		return dev_err_probe(dev, PTR_ERR(priv->gpio), "failed to get GPIO\n");

	if (!priv->irq && !priv->gpio) {
		dev_err(dev, "IRQ and GPIO are not found. At least one source should be provided\n");
		return -ENODEV;
	}

	if (!priv->irq) {
		int irq = gpiod_to_irq(priv->gpio);

		if (irq < 0)
			return dev_err_probe(dev, irq, "failed to get IRQ from GPIO\n");

		priv->irq = irq;
	}

	priv->signals.name = devm_kasprintf(dev, GFP_KERNEL, "IRQ %d",
					    priv->irq);
	if (!priv->signals.name)
		return -ENOMEM;

	priv->counter.signals = &priv->signals;
	priv->counter.num_signals = 1;

	priv->synapses.actions_list = interrupt_cnt_synapse_actions;
	priv->synapses.num_actions = ARRAY_SIZE(interrupt_cnt_synapse_actions);
	priv->synapses.signal = &priv->signals;

	priv->cnts.name = "Channel 0 Count";
	priv->cnts.functions_list = interrupt_cnt_functions;
	priv->cnts.num_functions = ARRAY_SIZE(interrupt_cnt_functions);
	priv->cnts.synapses = &priv->synapses;
	priv->cnts.num_synapses = 1;
	priv->cnts.ext = interrupt_cnt_ext;
	priv->cnts.num_ext = ARRAY_SIZE(interrupt_cnt_ext);

	priv->counter.priv = priv;
	priv->counter.name = dev_name(dev);
	priv->counter.parent = dev;
	priv->counter.ops = &interrupt_cnt_ops;
	priv->counter.counts = &priv->cnts;
	priv->counter.num_counts = 1;

	irq_set_status_flags(priv->irq, IRQ_NOAUTOEN);
	ret = devm_request_irq(dev, priv->irq, interrupt_cnt_isr,
			       IRQF_TRIGGER_RISING | IRQF_NO_THREAD,
			       dev_name(dev), priv);
	if (ret)
		return ret;

	return devm_counter_register(dev, &priv->counter);
}

static const struct of_device_id interrupt_cnt_of_match[] = {
	{ .compatible = "interrupt-counter", },
	{}
};
MODULE_DEVICE_TABLE(of, interrupt_cnt_of_match);

static struct platform_driver interrupt_cnt_driver = {
	.probe = interrupt_cnt_probe,
	.driver = {
		.name = INTERRUPT_CNT_NAME,
		.of_match_table = interrupt_cnt_of_match,
	},
};
module_platform_driver(interrupt_cnt_driver);

MODULE_ALIAS("platform:interrupt-counter");
MODULE_AUTHOR("Oleksij Rempel <o.rempel@pengutronix.de>");
MODULE_DESCRIPTION("Interrupt counter driver");
MODULE_LICENSE("GPL v2");