summaryrefslogtreecommitdiff
path: root/drivers/gpio/gpio-sloppy-logic-analyzer.c
blob: aed6d1f6cfc308884a7003262cca96f81c679ff7 (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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
// SPDX-License-Identifier: GPL-2.0-only
/*
 * Sloppy logic analyzer using GPIOs (to be run on an isolated CPU)
 *
 * Use the 'gpio-sloppy-logic-analyzer' script in the 'tools/gpio' folder for
 * easier usage and further documentation. Note that this is a last resort
 * analyzer which can be affected by latencies and non-deterministic code
 * paths. However, for e.g. remote development, it may be useful to get a first
 * view and aid further debugging.
 *
 * Copyright (C) Wolfram Sang <wsa@sang-engineering.com>
 * Copyright (C) Renesas Electronics Corporation
 */

#include <linux/ctype.h>
#include <linux/debugfs.h>
#include <linux/delay.h>
#include <linux/device.h>
#include <linux/err.h>
#include <linux/gpio/consumer.h>
#include <linux/init.h>
#include <linux/ktime.h>
#include <linux/mod_devicetable.h>
#include <linux/module.h>
#include <linux/mutex.h>
#include <linux/platform_device.h>
#include <linux/property.h>
#include <linux/slab.h>
#include <linux/sizes.h>
#include <linux/timekeeping.h>
#include <linux/types.h>
#include <linux/vmalloc.h>

#define GPIO_LA_NAME "gpio-sloppy-logic-analyzer"
#define GPIO_LA_DEFAULT_BUF_SIZE SZ_256K
/* can be increased but then we need to extend the u8 buffers */
#define GPIO_LA_MAX_PROBES 8
#define GPIO_LA_NUM_TESTS 1024

struct gpio_la_poll_priv {
	struct mutex blob_lock; /* serialize access to the blob (data) */
	u32 buf_idx;
	struct gpio_descs *descs;
	unsigned long delay_ns;
	unsigned long acq_delay;
	struct debugfs_blob_wrapper blob;
	struct dentry *debug_dir;
	struct dentry *blob_dent;
	struct debugfs_blob_wrapper meta;
	struct device *dev;
	unsigned int trig_len;
	u8 *trig_data;
};

static struct dentry *gpio_la_poll_debug_dir;

static __always_inline int gpio_la_get_array(struct gpio_descs *d, unsigned long *sptr)
{
	int ret;

	ret = gpiod_get_array_value(d->ndescs, d->desc, d->info, sptr);
	if (ret == 0 && fatal_signal_pending(current))
		ret = -EINTR;

	return ret;
}

static int fops_capture_set(void *data, u64 val)
{
	struct gpio_la_poll_priv *priv = data;
	u8 *la_buf = priv->blob.data;
	unsigned long state = 0; /* zeroed because GPIO arrays are bitfields */
	unsigned long delay;
	ktime_t start_time;
	unsigned int i;
	int ret;

	if (!val)
		return 0;

	if (!la_buf)
		return -ENOMEM;

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

	mutex_lock(&priv->blob_lock);
	if (priv->blob_dent) {
		debugfs_remove(priv->blob_dent);
		priv->blob_dent = NULL;
	}

	priv->buf_idx = 0;

	local_irq_disable();
	preempt_disable_notrace();

	/* Measure delay of reading GPIOs */
	start_time = ktime_get();
	for (i = 0; i < GPIO_LA_NUM_TESTS; i++) {
		ret = gpio_la_get_array(priv->descs, &state);
		if (ret)
			goto out;
	}

	priv->acq_delay = ktime_sub(ktime_get(), start_time) / GPIO_LA_NUM_TESTS;
	if (priv->delay_ns < priv->acq_delay) {
		ret = -ERANGE;
		goto out;
	}

	delay = priv->delay_ns - priv->acq_delay;

	/* Wait for triggers */
	for (i = 0; i < priv->trig_len; i += 2) {
		do {
			ret = gpio_la_get_array(priv->descs, &state);
			if (ret)
				goto out;

			ndelay(delay);
		} while ((state & priv->trig_data[i]) != priv->trig_data[i + 1]);
	}

	/* With triggers, final state is also the first sample */
	if (priv->trig_len)
		la_buf[priv->buf_idx++] = state;

	/* Sample */
	while (priv->buf_idx < priv->blob.size) {
		ret = gpio_la_get_array(priv->descs, &state);
		if (ret)
			goto out;

		la_buf[priv->buf_idx++] = state;
		ndelay(delay);
	}
out:
	preempt_enable_notrace();
	local_irq_enable();
	if (ret)
		dev_err(priv->dev, "couldn't read GPIOs: %d\n", ret);

	kfree(priv->trig_data);
	priv->trig_data = NULL;
	priv->trig_len = 0;

	priv->blob_dent = debugfs_create_blob("sample_data", 0400, priv->debug_dir, &priv->blob);
	mutex_unlock(&priv->blob_lock);

	return ret;
}
DEFINE_DEBUGFS_ATTRIBUTE(fops_capture, NULL, fops_capture_set, "%llu\n");

static int fops_buf_size_get(void *data, u64 *val)
{
	struct gpio_la_poll_priv *priv = data;

	*val = priv->blob.size;

	return 0;
}

static int fops_buf_size_set(void *data, u64 val)
{
	struct gpio_la_poll_priv *priv = data;
	int ret = 0;
	void *p;

	if (!val)
		return -EINVAL;

	mutex_lock(&priv->blob_lock);

	vfree(priv->blob.data);
	p = vzalloc(val);
	if (!p) {
		val = 0;
		ret = -ENOMEM;
	}

	priv->blob.data = p;
	priv->blob.size = val;

	mutex_unlock(&priv->blob_lock);
	return ret;
}
DEFINE_DEBUGFS_ATTRIBUTE(fops_buf_size, fops_buf_size_get, fops_buf_size_set, "%llu\n");

static int trigger_open(struct inode *inode, struct file *file)
{
	return single_open(file, NULL, inode->i_private);
}

static ssize_t trigger_write(struct file *file, const char __user *ubuf,
			     size_t count, loff_t *offset)
{
	struct seq_file *m = file->private_data;
	struct gpio_la_poll_priv *priv = m->private;
	char *buf;

	/* upper limit is arbitrary but should be less than PAGE_SIZE */
	if (count > 2048 || count & 1)
		return -EINVAL;

	buf = memdup_user(ubuf, count);
	if (IS_ERR(buf))
		return PTR_ERR(buf);

	priv->trig_data = buf;
	priv->trig_len = count;

	return count;
}

static const struct file_operations fops_trigger = {
	.owner = THIS_MODULE,
	.open = trigger_open,
	.write = trigger_write,
	.llseek = no_llseek,
	.release = single_release,
};

static int gpio_la_poll_probe(struct platform_device *pdev)
{
	struct gpio_la_poll_priv *priv;
	struct device *dev = &pdev->dev;
	const char *devname = dev_name(dev);
	const char *gpio_names[GPIO_LA_MAX_PROBES];
	char *meta = NULL;
	unsigned int i, meta_len = 0;
	int ret;

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

	devm_mutex_init(dev, &priv->blob_lock);

	fops_buf_size_set(priv, GPIO_LA_DEFAULT_BUF_SIZE);

	priv->descs = devm_gpiod_get_array(dev, "probe", GPIOD_IN);
	if (IS_ERR(priv->descs))
		return PTR_ERR(priv->descs);

	/* artificial limit to keep 1 byte per sample for now */
	if (priv->descs->ndescs > GPIO_LA_MAX_PROBES)
		return -EFBIG;

	ret = device_property_read_string_array(dev, "probe-names", gpio_names,
						priv->descs->ndescs);
	if (ret >= 0 && ret != priv->descs->ndescs)
		ret = -EBADR;
	if (ret < 0)
		return dev_err_probe(dev, ret, "error naming the GPIOs");

	for (i = 0; i < priv->descs->ndescs; i++) {
		unsigned int add_len;
		char *new_meta, *consumer_name;

		if (gpiod_cansleep(priv->descs->desc[i]))
			return -EREMOTE;

		consumer_name = kasprintf(GFP_KERNEL, "%s: %s", devname, gpio_names[i]);
		if (!consumer_name)
			return -ENOMEM;
		gpiod_set_consumer_name(priv->descs->desc[i], consumer_name);
		kfree(consumer_name);

		/* '10' is length of 'probe00=\n\0' */
		add_len = strlen(gpio_names[i]) + 10;

		new_meta = devm_krealloc(dev, meta, meta_len + add_len, GFP_KERNEL);
		if (!new_meta)
			return -ENOMEM;

		meta = new_meta;
		meta_len += snprintf(meta + meta_len, add_len, "probe%02u=%s\n",
				     i + 1, gpio_names[i]);
	}

	platform_set_drvdata(pdev, priv);
	priv->dev = dev;

	priv->meta.data = meta;
	priv->meta.size = meta_len;
	priv->debug_dir = debugfs_create_dir(devname, gpio_la_poll_debug_dir);
	debugfs_create_blob("meta_data", 0400, priv->debug_dir, &priv->meta);
	debugfs_create_ulong("delay_ns", 0600, priv->debug_dir, &priv->delay_ns);
	debugfs_create_ulong("delay_ns_acquisition", 0400, priv->debug_dir, &priv->acq_delay);
	debugfs_create_file_unsafe("buf_size", 0600, priv->debug_dir, priv, &fops_buf_size);
	debugfs_create_file_unsafe("capture", 0200, priv->debug_dir, priv, &fops_capture);
	debugfs_create_file_unsafe("trigger", 0200, priv->debug_dir, priv, &fops_trigger);

	return 0;
}

static void gpio_la_poll_remove(struct platform_device *pdev)
{
	struct gpio_la_poll_priv *priv = platform_get_drvdata(pdev);

	mutex_lock(&priv->blob_lock);
	debugfs_remove_recursive(priv->debug_dir);
	mutex_unlock(&priv->blob_lock);
}

static const struct of_device_id gpio_la_poll_of_match[] = {
	{ .compatible = GPIO_LA_NAME },
	{ }
};
MODULE_DEVICE_TABLE(of, gpio_la_poll_of_match);

static struct platform_driver gpio_la_poll_device_driver = {
	.probe = gpio_la_poll_probe,
	.remove_new = gpio_la_poll_remove,
	.driver = {
		.name = GPIO_LA_NAME,
		.of_match_table = gpio_la_poll_of_match,
	}
};

static int __init gpio_la_poll_init(void)
{
	gpio_la_poll_debug_dir = debugfs_create_dir(GPIO_LA_NAME, NULL);

	return platform_driver_register(&gpio_la_poll_device_driver);
}
/*
 * Non-strict pin controllers can read GPIOs while being muxed to something else.
 * To support that, we need to claim GPIOs before further pinmuxing happens. So,
 * we probe early using 'late_initcall'
 */
late_initcall(gpio_la_poll_init);

static void __exit gpio_la_poll_exit(void)
{
	platform_driver_unregister(&gpio_la_poll_device_driver);
	debugfs_remove_recursive(gpio_la_poll_debug_dir);
}
module_exit(gpio_la_poll_exit);

MODULE_AUTHOR("Wolfram Sang <wsa@sang-engineering.com>");
MODULE_DESCRIPTION("Sloppy logic analyzer using GPIOs");
MODULE_LICENSE("GPL");