gpio-mockup.c 11.1 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0+
2
3
4
5
/*
 * GPIO Testing Device Driver
 *
 * Copyright (C) 2014  Kamlakant Patel <kamlakant.patel@broadcom.com>
6
 * Copyright (C) 2015-2016  Bamvor Jian Zhang <bamv2005@gmail.com>
7
 * Copyright (C) 2017 Bartosz Golaszewski <brgl@bgdev.pl>
8
9
10
11
12
 */

#include <linux/init.h>
#include <linux/module.h>
#include <linux/gpio/driver.h>
13
#include <linux/gpio/consumer.h>
14
#include <linux/platform_device.h>
15
#include <linux/slab.h>
16
17
#include <linux/interrupt.h>
#include <linux/irq.h>
18
#include <linux/irq_sim.h>
19
20
#include <linux/debugfs.h>
#include <linux/uaccess.h>
21
#include <linux/property.h>
22
23

#include "gpiolib.h"
24

25
#define GPIO_MOCKUP_NAME	"gpio-mockup"
26
#define GPIO_MOCKUP_MAX_GC	10
27
28
29
30
31
/*
 * We're storing two values per chip: the GPIO base and the number
 * of GPIO lines.
 */
#define GPIO_MOCKUP_MAX_RANGES	(GPIO_MOCKUP_MAX_GC * 2)
32
33
/* Maximum of three properties + the sentinel. */
#define GPIO_MOCKUP_MAX_PROP	4
34

35
36
#define gpio_mockup_err(...)	pr_err(GPIO_MOCKUP_NAME ": " __VA_ARGS__)

37
enum {
38
39
	GPIO_MOCKUP_DIR_IN = 0,
	GPIO_MOCKUP_DIR_OUT = 1,
40
41
42
43
44
45
46
};

/*
 * struct gpio_pin_status - structure describing a GPIO status
 * @dir:       Configures direction of gpio as "in" or "out", 0=in, 1=out
 * @value:     Configures status of the gpio as 0(low) or 1(high)
 */
47
48
struct gpio_mockup_line_status {
	int dir;
49
	int value;
50
51
};

52
struct gpio_mockup_chip {
53
	struct gpio_chip gc;
54
	struct gpio_mockup_line_status *lines;
55
	struct irq_sim irqsim;
56
	struct dentry *dbg_dir;
57
	struct mutex lock;
58
59
60
61
62
};

struct gpio_mockup_dbgfs_private {
	struct gpio_mockup_chip *chip;
	struct gpio_desc *desc;
63
	unsigned int offset;
64
65
};

66
static int gpio_mockup_ranges[GPIO_MOCKUP_MAX_RANGES];
67
68
static int gpio_mockup_num_ranges;
module_param_array(gpio_mockup_ranges, int, &gpio_mockup_num_ranges, 0400);
69

70
71
72
73
static bool gpio_mockup_named_lines;
module_param_named(gpio_mockup_named_lines,
		   gpio_mockup_named_lines, bool, 0400);

74
static struct dentry *gpio_mockup_dbg_dir;
75

76
77
78
79
80
81
82
83
84
85
static int gpio_mockup_range_base(unsigned int index)
{
	return gpio_mockup_ranges[index * 2];
}

static int gpio_mockup_range_ngpio(unsigned int index)
{
	return gpio_mockup_ranges[index * 2 + 1];
}

86
87
static int __gpio_mockup_get(struct gpio_mockup_chip *chip,
			     unsigned int offset)
88
{
89
	return chip->lines[offset].value;
90
91
}

92
93
94
95
96
97
static int gpio_mockup_get(struct gpio_chip *gc, unsigned int offset)
{
	struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
	int val;

	mutex_lock(&chip->lock);
98
	val = __gpio_mockup_get(chip, offset);
99
100
101
102
103
	mutex_unlock(&chip->lock);

	return val;
}

104
105
106
107
108
109
110
111
static int gpio_mockup_get_multiple(struct gpio_chip *gc,
				    unsigned long *mask, unsigned long *bits)
{
	struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
	unsigned int bit, val;

	mutex_lock(&chip->lock);
	for_each_set_bit(bit, mask, gc->ngpio) {
112
		val = __gpio_mockup_get(chip, bit);
113
114
115
116
117
118
119
		__assign_bit(bit, bits, val);
	}
	mutex_unlock(&chip->lock);

	return 0;
}

120
static void __gpio_mockup_set(struct gpio_mockup_chip *chip,
121
			      unsigned int offset, int value)
122
{
123
	chip->lines[offset].value = !!value;
124
125
}

126
127
128
129
130
131
static void gpio_mockup_set(struct gpio_chip *gc,
			   unsigned int offset, int value)
{
	struct gpio_mockup_chip *chip = gpiochip_get_data(gc);

	mutex_lock(&chip->lock);
132
	__gpio_mockup_set(chip, offset, value);
133
134
135
	mutex_unlock(&chip->lock);
}

136
137
138
static void gpio_mockup_set_multiple(struct gpio_chip *gc,
				     unsigned long *mask, unsigned long *bits)
{
139
	struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
140
141
	unsigned int bit;

142
	mutex_lock(&chip->lock);
143
	for_each_set_bit(bit, mask, gc->ngpio)
144
		__gpio_mockup_set(chip, bit, test_bit(bit, bits));
145
	mutex_unlock(&chip->lock);
146
147
}

148
149
static int gpio_mockup_dirout(struct gpio_chip *gc,
			      unsigned int offset, int value)
150
{
151
152
	struct gpio_mockup_chip *chip = gpiochip_get_data(gc);

153
	mutex_lock(&chip->lock);
154
	chip->lines[offset].dir = GPIO_MOCKUP_DIR_OUT;
155
	__gpio_mockup_set(chip, offset, value);
156
	mutex_unlock(&chip->lock);
157
158
159
160

	return 0;
}

161
static int gpio_mockup_dirin(struct gpio_chip *gc, unsigned int offset)
162
{
163
164
	struct gpio_mockup_chip *chip = gpiochip_get_data(gc);

165
	mutex_lock(&chip->lock);
166
	chip->lines[offset].dir = GPIO_MOCKUP_DIR_IN;
167
	mutex_unlock(&chip->lock);
168
169
170
171

	return 0;
}

172
static int gpio_mockup_get_direction(struct gpio_chip *gc, unsigned int offset)
173
{
174
	struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
175
	int direction;
176

177
178
179
180
181
	mutex_lock(&chip->lock);
	direction = !chip->lines[offset].dir;
	mutex_unlock(&chip->lock);

	return direction;
182
183
}

184
static int gpio_mockup_to_irq(struct gpio_chip *gc, unsigned int offset)
185
186
187
{
	struct gpio_mockup_chip *chip = gpiochip_get_data(gc);

188
	return irq_sim_irqnum(&chip->irqsim, offset);
189
190
}

191
192
193
194
195
196
197
198
static ssize_t gpio_mockup_event_write(struct file *file,
				       const char __user *usr_buf,
				       size_t size, loff_t *ppos)
{
	struct gpio_mockup_dbgfs_private *priv;
	struct gpio_mockup_chip *chip;
	struct seq_file *sfile;
	struct gpio_desc *desc;
199
	int rv, val;
200

201
202
203
204
205
206
	rv = kstrtoint_from_user(usr_buf, size, 0, &val);
	if (rv)
		return rv;
	if (val != 0 && val != 1)
		return -EINVAL;

207
208
209
210
	sfile = file->private_data;
	priv = sfile->private;
	desc = priv->desc;
	chip = priv->chip;
211

212
213
	gpiod_set_value_cansleep(desc, val);
	irq_sim_fire(&chip->irqsim, priv->offset);
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233

	return size;
}

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

static const struct file_operations gpio_mockup_event_ops = {
	.owner = THIS_MODULE,
	.open = gpio_mockup_event_open,
	.write = gpio_mockup_event_write,
	.llseek = no_llseek,
};

static void gpio_mockup_debugfs_setup(struct device *dev,
				      struct gpio_mockup_chip *chip)
{
	struct gpio_mockup_dbgfs_private *priv;
234
	struct dentry *evfile;
235
	struct gpio_chip *gc;
236
	const char *devname;
237
238
239
240
	char *name;
	int i;

	gc = &chip->gc;
241
	devname = dev_name(&gc->gpiodev->dev);
242

243
	chip->dbg_dir = debugfs_create_dir(devname, gpio_mockup_dbg_dir);
244
	if (IS_ERR_OR_NULL(chip->dbg_dir))
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
		goto err;

	for (i = 0; i < gc->ngpio; i++) {
		name = devm_kasprintf(dev, GFP_KERNEL, "%d", i);
		if (!name)
			goto err;

		priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
		if (!priv)
			goto err;

		priv->chip = chip;
		priv->offset = i;
		priv->desc = &gc->gpiodev->descs[i];

		evfile = debugfs_create_file(name, 0200, chip->dbg_dir, priv,
					     &gpio_mockup_event_ops);
262
		if (IS_ERR_OR_NULL(evfile))
263
264
265
266
267
268
			goto err;
	}

	return;

err:
269
	dev_err(dev, "error creating debugfs event files\n");
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
static int gpio_mockup_name_lines(struct device *dev,
				  struct gpio_mockup_chip *chip)
{
	struct gpio_chip *gc = &chip->gc;
	char **names;
	int i;

	names = devm_kcalloc(dev, gc->ngpio, sizeof(char *), GFP_KERNEL);
	if (!names)
		return -ENOMEM;

	for (i = 0; i < gc->ngpio; i++) {
		names[i] = devm_kasprintf(dev, GFP_KERNEL,
					  "%s-%d", gc->label, i);
		if (!names[i])
			return -ENOMEM;
	}

	gc->names = (const char *const *)names;

	return 0;
}

295
static int gpio_mockup_probe(struct platform_device *pdev)
296
{
297
298
299
	struct gpio_mockup_chip *chip;
	struct gpio_chip *gc;
	struct device *dev;
300
301
302
	const char *name;
	int rv, base;
	u16 ngpio;
303
304

	dev = &pdev->dev;
305
306
307
308
309
310
311
312
313
314
315
316

	rv = device_property_read_u32(dev, "gpio-base", &base);
	if (rv)
		base = -1;

	rv = device_property_read_u16(dev, "nr-gpios", &ngpio);
	if (rv)
		return rv;

	rv = device_property_read_string(dev, "chip-name", &name);
	if (rv)
		name = NULL;
317
318
319
320
321

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

322
323
324
325
326
327
	if (!name) {
		name = devm_kasprintf(dev, GFP_KERNEL,
				      "%s-%c", pdev->name, pdev->id + 'A');
		if (!name)
			return -ENOMEM;
	}
328

329
330
	mutex_init(&chip->lock);

331
	gc = &chip->gc;
332
333
334
335
336
337
338
	gc->base = base;
	gc->ngpio = ngpio;
	gc->label = name;
	gc->owner = THIS_MODULE;
	gc->parent = dev;
	gc->get = gpio_mockup_get;
	gc->set = gpio_mockup_set;
339
	gc->get_multiple = gpio_mockup_get_multiple;
340
	gc->set_multiple = gpio_mockup_set_multiple;
341
342
343
	gc->direction_output = gpio_mockup_dirout;
	gc->direction_input = gpio_mockup_dirin;
	gc->get_direction = gpio_mockup_get_direction;
344
	gc->to_irq = gpio_mockup_to_irq;
345

346
347
	chip->lines = devm_kcalloc(dev, gc->ngpio,
				   sizeof(*chip->lines), GFP_KERNEL);
348
349
	if (!chip->lines)
		return -ENOMEM;
350

351
	if (device_property_read_bool(dev, "named-gpio-lines")) {
352
353
354
		rv = gpio_mockup_name_lines(dev, chip);
		if (rv)
			return rv;
355
356
	}

357
	rv = devm_irq_sim_init(dev, &chip->irqsim, gc->ngpio);
358
	if (rv < 0)
359
		return rv;
360

361
362
363
	rv = devm_gpiochip_add_data(dev, &chip->gc, chip);
	if (rv)
		return rv;
364

365
	if (!IS_ERR_OR_NULL(gpio_mockup_dbg_dir))
366
367
368
		gpio_mockup_debugfs_setup(dev, chip);

	return 0;
369
370
}

371
static struct platform_driver gpio_mockup_driver = {
372
	.driver = {
373
		.name = GPIO_MOCKUP_NAME,
374
	},
375
	.probe = gpio_mockup_probe,
376
377
};

378
379
380
381
382
383
384
385
386
387
388
389
390
391
static struct platform_device *gpio_mockup_pdevs[GPIO_MOCKUP_MAX_GC];

static void gpio_mockup_unregister_pdevs(void)
{
	struct platform_device *pdev;
	int i;

	for (i = 0; i < GPIO_MOCKUP_MAX_GC; i++) {
		pdev = gpio_mockup_pdevs[i];

		if (pdev)
			platform_device_unregister(pdev);
	}
}
392
393

static int __init gpio_mockup_init(void)
394
{
395
396
397
	struct property_entry properties[GPIO_MOCKUP_MAX_PROP];
	int i, prop, num_chips, err = 0, base;
	struct platform_device_info pdevinfo;
398
	struct platform_device *pdev;
399
	u16 ngpio;
400

401
402
403
	if ((gpio_mockup_num_ranges < 2) ||
	    (gpio_mockup_num_ranges % 2) ||
	    (gpio_mockup_num_ranges > GPIO_MOCKUP_MAX_RANGES))
404
405
		return -EINVAL;

406
	/* Each chip is described by two values. */
407
	num_chips = gpio_mockup_num_ranges / 2;
408

409
410
411
412
413
	/*
	 * The second value in the <base GPIO - number of GPIOS> pair must
	 * always be greater than 0.
	 */
	for (i = 0; i < num_chips; i++) {
414
		if (gpio_mockup_range_ngpio(i) < 0)
415
416
417
			return -EINVAL;
	}

418
	gpio_mockup_dbg_dir = debugfs_create_dir("gpio-mockup-event", NULL);
419
	if (IS_ERR_OR_NULL(gpio_mockup_dbg_dir))
420
		gpio_mockup_err("error creating debugfs directory\n");
421

422
	err = platform_driver_register(&gpio_mockup_driver);
423
	if (err) {
424
		gpio_mockup_err("error registering platform driver\n");
425
426
427
		return err;
	}

428
	for (i = 0; i < num_chips; i++) {
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
		memset(properties, 0, sizeof(properties));
		memset(&pdevinfo, 0, sizeof(pdevinfo));
		prop = 0;

		base = gpio_mockup_range_base(i);
		if (base >= 0)
			properties[prop++] = PROPERTY_ENTRY_U32("gpio-base",
								base);

		ngpio = base < 0 ? gpio_mockup_range_ngpio(i)
				 : gpio_mockup_range_ngpio(i) - base;
		properties[prop++] = PROPERTY_ENTRY_U16("nr-gpios", ngpio);

		if (gpio_mockup_named_lines)
			properties[prop++] = PROPERTY_ENTRY_BOOL(
						"named-gpio-lines");

		pdevinfo.name = GPIO_MOCKUP_NAME;
		pdevinfo.id = i;
		pdevinfo.properties = properties;

		pdev = platform_device_register_full(&pdevinfo);
451
		if (IS_ERR(pdev)) {
452
			gpio_mockup_err("error registering device");
453
454
			platform_driver_unregister(&gpio_mockup_driver);
			gpio_mockup_unregister_pdevs();
455
			return PTR_ERR(pdev);
456
457
458
		}

		gpio_mockup_pdevs[i] = pdev;
459
460
461
462
463
	}

	return 0;
}

464
static void __exit gpio_mockup_exit(void)
465
{
466
	debugfs_remove_recursive(gpio_mockup_dbg_dir);
467
	platform_driver_unregister(&gpio_mockup_driver);
468
	gpio_mockup_unregister_pdevs();
469
470
}

471
472
module_init(gpio_mockup_init);
module_exit(gpio_mockup_exit);
473
474

MODULE_AUTHOR("Kamlakant Patel <kamlakant.patel@broadcom.com>");
475
MODULE_AUTHOR("Bamvor Jian Zhang <bamv2005@gmail.com>");
476
MODULE_AUTHOR("Bartosz Golaszewski <brgl@bgdev.pl>");
477
478
MODULE_DESCRIPTION("GPIO Testing driver");
MODULE_LICENSE("GPL v2");