xref: /openbmc/linux/drivers/gpio/gpio-mockup.c (revision be709d48)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * GPIO Testing Device Driver
4  *
5  * 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 #include <linux/init.h>
11 #include <linux/module.h>
12 #include <linux/gpio/driver.h>
13 #include <linux/gpio/consumer.h>
14 #include <linux/platform_device.h>
15 #include <linux/slab.h>
16 #include <linux/interrupt.h>
17 #include <linux/irq.h>
18 #include <linux/irq_sim.h>
19 #include <linux/debugfs.h>
20 #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  * We're storing two values per chip: the GPIO base and the number
29  * of GPIO lines.
30  */
31 #define GPIO_MOCKUP_MAX_RANGES	(GPIO_MOCKUP_MAX_GC * 2)
32 /* Maximum of three properties + the sentinel. */
33 #define GPIO_MOCKUP_MAX_PROP	4
34 
35 #define gpio_mockup_err(...)	pr_err(GPIO_MOCKUP_NAME ": " __VA_ARGS__)
36 
37 enum {
38 	GPIO_MOCKUP_DIR_IN = 0,
39 	GPIO_MOCKUP_DIR_OUT = 1,
40 };
41 
42 /*
43  * struct gpio_pin_status - structure describing a GPIO status
44  * @dir:       Configures direction of gpio as "in" or "out", 0=in, 1=out
45  * @value:     Configures status of the gpio as 0(low) or 1(high)
46  */
47 struct gpio_mockup_line_status {
48 	int dir;
49 	int value;
50 	int pull;
51 };
52 
53 struct gpio_mockup_chip {
54 	struct gpio_chip gc;
55 	struct gpio_mockup_line_status *lines;
56 	struct irq_sim irqsim;
57 	struct dentry *dbg_dir;
58 	struct mutex lock;
59 };
60 
61 struct gpio_mockup_dbgfs_private {
62 	struct gpio_mockup_chip *chip;
63 	struct gpio_desc *desc;
64 	unsigned int offset;
65 };
66 
67 static int gpio_mockup_ranges[GPIO_MOCKUP_MAX_RANGES];
68 static int gpio_mockup_num_ranges;
69 module_param_array(gpio_mockup_ranges, int, &gpio_mockup_num_ranges, 0400);
70 
71 static bool gpio_mockup_named_lines;
72 module_param_named(gpio_mockup_named_lines,
73 		   gpio_mockup_named_lines, bool, 0400);
74 
75 static struct dentry *gpio_mockup_dbg_dir;
76 
77 static int gpio_mockup_range_base(unsigned int index)
78 {
79 	return gpio_mockup_ranges[index * 2];
80 }
81 
82 static int gpio_mockup_range_ngpio(unsigned int index)
83 {
84 	return gpio_mockup_ranges[index * 2 + 1];
85 }
86 
87 static int __gpio_mockup_get(struct gpio_mockup_chip *chip,
88 			     unsigned int offset)
89 {
90 	return chip->lines[offset].value;
91 }
92 
93 static int gpio_mockup_get(struct gpio_chip *gc, unsigned int offset)
94 {
95 	struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
96 	int val;
97 
98 	mutex_lock(&chip->lock);
99 	val = __gpio_mockup_get(chip, offset);
100 	mutex_unlock(&chip->lock);
101 
102 	return val;
103 }
104 
105 static int gpio_mockup_get_multiple(struct gpio_chip *gc,
106 				    unsigned long *mask, unsigned long *bits)
107 {
108 	struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
109 	unsigned int bit, val;
110 
111 	mutex_lock(&chip->lock);
112 	for_each_set_bit(bit, mask, gc->ngpio) {
113 		val = __gpio_mockup_get(chip, bit);
114 		__assign_bit(bit, bits, val);
115 	}
116 	mutex_unlock(&chip->lock);
117 
118 	return 0;
119 }
120 
121 static void __gpio_mockup_set(struct gpio_mockup_chip *chip,
122 			      unsigned int offset, int value)
123 {
124 	chip->lines[offset].value = !!value;
125 }
126 
127 static void gpio_mockup_set(struct gpio_chip *gc,
128 			   unsigned int offset, int value)
129 {
130 	struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
131 
132 	mutex_lock(&chip->lock);
133 	__gpio_mockup_set(chip, offset, value);
134 	mutex_unlock(&chip->lock);
135 }
136 
137 static void gpio_mockup_set_multiple(struct gpio_chip *gc,
138 				     unsigned long *mask, unsigned long *bits)
139 {
140 	struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
141 	unsigned int bit;
142 
143 	mutex_lock(&chip->lock);
144 	for_each_set_bit(bit, mask, gc->ngpio)
145 		__gpio_mockup_set(chip, bit, test_bit(bit, bits));
146 	mutex_unlock(&chip->lock);
147 }
148 
149 static int gpio_mockup_dirout(struct gpio_chip *gc,
150 			      unsigned int offset, int value)
151 {
152 	struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
153 
154 	mutex_lock(&chip->lock);
155 	chip->lines[offset].dir = GPIO_MOCKUP_DIR_OUT;
156 	__gpio_mockup_set(chip, offset, value);
157 	mutex_unlock(&chip->lock);
158 
159 	return 0;
160 }
161 
162 static int gpio_mockup_dirin(struct gpio_chip *gc, unsigned int offset)
163 {
164 	struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
165 
166 	mutex_lock(&chip->lock);
167 	chip->lines[offset].dir = GPIO_MOCKUP_DIR_IN;
168 	mutex_unlock(&chip->lock);
169 
170 	return 0;
171 }
172 
173 static int gpio_mockup_get_direction(struct gpio_chip *gc, unsigned int offset)
174 {
175 	struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
176 	int direction;
177 
178 	mutex_lock(&chip->lock);
179 	direction = !chip->lines[offset].dir;
180 	mutex_unlock(&chip->lock);
181 
182 	return direction;
183 }
184 
185 static int gpio_mockup_to_irq(struct gpio_chip *gc, unsigned int offset)
186 {
187 	struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
188 
189 	return irq_sim_irqnum(&chip->irqsim, offset);
190 }
191 
192 static void gpio_mockup_free(struct gpio_chip *gc, unsigned int offset)
193 {
194 	struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
195 
196 	__gpio_mockup_set(chip, offset, chip->lines[offset].pull);
197 }
198 
199 static ssize_t gpio_mockup_debugfs_read(struct file *file,
200 					char __user *usr_buf,
201 					size_t size, loff_t *ppos)
202 {
203 	struct gpio_mockup_dbgfs_private *priv;
204 	struct gpio_mockup_chip *chip;
205 	struct seq_file *sfile;
206 	struct gpio_chip *gc;
207 	char buf[3];
208 	int val, rv;
209 
210 	if (*ppos != 0)
211 		return 0;
212 
213 	sfile = file->private_data;
214 	priv = sfile->private;
215 	chip = priv->chip;
216 	gc = &chip->gc;
217 
218 	val = gpio_mockup_get(gc, priv->offset);
219 	snprintf(buf, sizeof(buf), "%d\n", val);
220 
221 	rv = copy_to_user(usr_buf, buf, sizeof(buf));
222 	if (rv)
223 		return rv;
224 
225 	return sizeof(buf) - 1;
226 }
227 
228 static ssize_t gpio_mockup_debugfs_write(struct file *file,
229 					 const char __user *usr_buf,
230 					 size_t size, loff_t *ppos)
231 {
232 	struct gpio_mockup_dbgfs_private *priv;
233 	int rv, val, curr, irq, irq_type;
234 	struct gpio_mockup_chip *chip;
235 	struct seq_file *sfile;
236 	struct gpio_desc *desc;
237 	struct gpio_chip *gc;
238 	struct irq_sim *sim;
239 
240 	if (*ppos != 0)
241 		return -EINVAL;
242 
243 	rv = kstrtoint_from_user(usr_buf, size, 0, &val);
244 	if (rv)
245 		return rv;
246 	if (val != 0 && val != 1)
247 		return -EINVAL;
248 
249 	sfile = file->private_data;
250 	priv = sfile->private;
251 	chip = priv->chip;
252 	gc = &chip->gc;
253 	desc = &gc->gpiodev->descs[priv->offset];
254 	sim = &chip->irqsim;
255 
256 	mutex_lock(&chip->lock);
257 
258 	if (test_bit(FLAG_REQUESTED, &desc->flags) &&
259 	    !test_bit(FLAG_IS_OUT, &desc->flags)) {
260 		curr = __gpio_mockup_get(chip, priv->offset);
261 		if (curr == val)
262 			goto out;
263 
264 		irq = irq_sim_irqnum(sim, priv->offset);
265 		irq_type = irq_get_trigger_type(irq);
266 
267 		if ((val == 1 && (irq_type & IRQ_TYPE_EDGE_RISING)) ||
268 		    (val == 0 && (irq_type & IRQ_TYPE_EDGE_FALLING)))
269 			irq_sim_fire(sim, priv->offset);
270 	}
271 
272 	/* Change the value unless we're actively driving the line. */
273 	if (!test_bit(FLAG_REQUESTED, &desc->flags) ||
274 	    !test_bit(FLAG_IS_OUT, &desc->flags))
275 		__gpio_mockup_set(chip, priv->offset, val);
276 
277 out:
278 	chip->lines[priv->offset].pull = val;
279 	mutex_unlock(&chip->lock);
280 
281 	return size;
282 }
283 
284 static int gpio_mockup_debugfs_open(struct inode *inode, struct file *file)
285 {
286 	return single_open(file, NULL, inode->i_private);
287 }
288 
289 /*
290  * Each mockup chip is represented by a directory named after the chip's device
291  * name under /sys/kernel/debug/gpio-mockup/. Each line is represented by
292  * a file using the line's offset as the name under the chip's directory.
293  *
294  * Reading from the line's file yields the current *value*, writing to the
295  * line's file changes the current *pull*. Default pull for mockup lines is
296  * down.
297  *
298  * Examples:
299  * - when a line pulled down is requested in output mode and driven high, its
300  *   value will return to 0 once it's released
301  * - when the line is requested in output mode and driven high, writing 0 to
302  *   the corresponding debugfs file will change the pull to down but the
303  *   reported value will still be 1 until the line is released
304  * - line requested in input mode always reports the same value as its pull
305  *   configuration
306  * - when the line is requested in input mode and monitored for events, writing
307  *   the same value to the debugfs file will be a noop, while writing the
308  *   opposite value will generate a dummy interrupt with an appropriate edge
309  */
310 static const struct file_operations gpio_mockup_debugfs_ops = {
311 	.owner = THIS_MODULE,
312 	.open = gpio_mockup_debugfs_open,
313 	.read = gpio_mockup_debugfs_read,
314 	.write = gpio_mockup_debugfs_write,
315 	.llseek = no_llseek,
316 };
317 
318 static void gpio_mockup_debugfs_setup(struct device *dev,
319 				      struct gpio_mockup_chip *chip)
320 {
321 	struct gpio_mockup_dbgfs_private *priv;
322 	struct dentry *evfile;
323 	struct gpio_chip *gc;
324 	const char *devname;
325 	char *name;
326 	int i;
327 
328 	gc = &chip->gc;
329 	devname = dev_name(&gc->gpiodev->dev);
330 
331 	chip->dbg_dir = debugfs_create_dir(devname, gpio_mockup_dbg_dir);
332 	if (IS_ERR_OR_NULL(chip->dbg_dir))
333 		goto err;
334 
335 	for (i = 0; i < gc->ngpio; i++) {
336 		name = devm_kasprintf(dev, GFP_KERNEL, "%d", i);
337 		if (!name)
338 			goto err;
339 
340 		priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
341 		if (!priv)
342 			goto err;
343 
344 		priv->chip = chip;
345 		priv->offset = i;
346 		priv->desc = &gc->gpiodev->descs[i];
347 
348 		evfile = debugfs_create_file(name, 0200, chip->dbg_dir, priv,
349 					     &gpio_mockup_debugfs_ops);
350 		if (IS_ERR_OR_NULL(evfile))
351 			goto err;
352 	}
353 
354 	return;
355 
356 err:
357 	dev_err(dev, "error creating debugfs files\n");
358 }
359 
360 static int gpio_mockup_name_lines(struct device *dev,
361 				  struct gpio_mockup_chip *chip)
362 {
363 	struct gpio_chip *gc = &chip->gc;
364 	char **names;
365 	int i;
366 
367 	names = devm_kcalloc(dev, gc->ngpio, sizeof(char *), GFP_KERNEL);
368 	if (!names)
369 		return -ENOMEM;
370 
371 	for (i = 0; i < gc->ngpio; i++) {
372 		names[i] = devm_kasprintf(dev, GFP_KERNEL,
373 					  "%s-%d", gc->label, i);
374 		if (!names[i])
375 			return -ENOMEM;
376 	}
377 
378 	gc->names = (const char *const *)names;
379 
380 	return 0;
381 }
382 
383 static int gpio_mockup_probe(struct platform_device *pdev)
384 {
385 	struct gpio_mockup_chip *chip;
386 	struct gpio_chip *gc;
387 	struct device *dev;
388 	const char *name;
389 	int rv, base;
390 	u16 ngpio;
391 
392 	dev = &pdev->dev;
393 
394 	rv = device_property_read_u32(dev, "gpio-base", &base);
395 	if (rv)
396 		base = -1;
397 
398 	rv = device_property_read_u16(dev, "nr-gpios", &ngpio);
399 	if (rv)
400 		return rv;
401 
402 	rv = device_property_read_string(dev, "chip-name", &name);
403 	if (rv)
404 		name = NULL;
405 
406 	chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL);
407 	if (!chip)
408 		return -ENOMEM;
409 
410 	if (!name) {
411 		name = devm_kasprintf(dev, GFP_KERNEL,
412 				      "%s-%c", pdev->name, pdev->id + 'A');
413 		if (!name)
414 			return -ENOMEM;
415 	}
416 
417 	mutex_init(&chip->lock);
418 
419 	gc = &chip->gc;
420 	gc->base = base;
421 	gc->ngpio = ngpio;
422 	gc->label = name;
423 	gc->owner = THIS_MODULE;
424 	gc->parent = dev;
425 	gc->get = gpio_mockup_get;
426 	gc->set = gpio_mockup_set;
427 	gc->get_multiple = gpio_mockup_get_multiple;
428 	gc->set_multiple = gpio_mockup_set_multiple;
429 	gc->direction_output = gpio_mockup_dirout;
430 	gc->direction_input = gpio_mockup_dirin;
431 	gc->get_direction = gpio_mockup_get_direction;
432 	gc->to_irq = gpio_mockup_to_irq;
433 	gc->free = gpio_mockup_free;
434 
435 	chip->lines = devm_kcalloc(dev, gc->ngpio,
436 				   sizeof(*chip->lines), GFP_KERNEL);
437 	if (!chip->lines)
438 		return -ENOMEM;
439 
440 	if (device_property_read_bool(dev, "named-gpio-lines")) {
441 		rv = gpio_mockup_name_lines(dev, chip);
442 		if (rv)
443 			return rv;
444 	}
445 
446 	rv = devm_irq_sim_init(dev, &chip->irqsim, gc->ngpio);
447 	if (rv < 0)
448 		return rv;
449 
450 	rv = devm_gpiochip_add_data(dev, &chip->gc, chip);
451 	if (rv)
452 		return rv;
453 
454 	if (!IS_ERR_OR_NULL(gpio_mockup_dbg_dir))
455 		gpio_mockup_debugfs_setup(dev, chip);
456 
457 	return 0;
458 }
459 
460 static struct platform_driver gpio_mockup_driver = {
461 	.driver = {
462 		.name = GPIO_MOCKUP_NAME,
463 	},
464 	.probe = gpio_mockup_probe,
465 };
466 
467 static struct platform_device *gpio_mockup_pdevs[GPIO_MOCKUP_MAX_GC];
468 
469 static void gpio_mockup_unregister_pdevs(void)
470 {
471 	struct platform_device *pdev;
472 	int i;
473 
474 	for (i = 0; i < GPIO_MOCKUP_MAX_GC; i++) {
475 		pdev = gpio_mockup_pdevs[i];
476 
477 		if (pdev)
478 			platform_device_unregister(pdev);
479 	}
480 }
481 
482 static int __init gpio_mockup_init(void)
483 {
484 	struct property_entry properties[GPIO_MOCKUP_MAX_PROP];
485 	int i, prop, num_chips, err = 0, base;
486 	struct platform_device_info pdevinfo;
487 	struct platform_device *pdev;
488 	u16 ngpio;
489 
490 	if ((gpio_mockup_num_ranges < 2) ||
491 	    (gpio_mockup_num_ranges % 2) ||
492 	    (gpio_mockup_num_ranges > GPIO_MOCKUP_MAX_RANGES))
493 		return -EINVAL;
494 
495 	/* Each chip is described by two values. */
496 	num_chips = gpio_mockup_num_ranges / 2;
497 
498 	/*
499 	 * The second value in the <base GPIO - number of GPIOS> pair must
500 	 * always be greater than 0.
501 	 */
502 	for (i = 0; i < num_chips; i++) {
503 		if (gpio_mockup_range_ngpio(i) < 0)
504 			return -EINVAL;
505 	}
506 
507 	gpio_mockup_dbg_dir = debugfs_create_dir("gpio-mockup", NULL);
508 	if (IS_ERR_OR_NULL(gpio_mockup_dbg_dir))
509 		gpio_mockup_err("error creating debugfs directory\n");
510 
511 	err = platform_driver_register(&gpio_mockup_driver);
512 	if (err) {
513 		gpio_mockup_err("error registering platform driver\n");
514 		return err;
515 	}
516 
517 	for (i = 0; i < num_chips; i++) {
518 		memset(properties, 0, sizeof(properties));
519 		memset(&pdevinfo, 0, sizeof(pdevinfo));
520 		prop = 0;
521 
522 		base = gpio_mockup_range_base(i);
523 		if (base >= 0)
524 			properties[prop++] = PROPERTY_ENTRY_U32("gpio-base",
525 								base);
526 
527 		ngpio = base < 0 ? gpio_mockup_range_ngpio(i)
528 				 : gpio_mockup_range_ngpio(i) - base;
529 		properties[prop++] = PROPERTY_ENTRY_U16("nr-gpios", ngpio);
530 
531 		if (gpio_mockup_named_lines)
532 			properties[prop++] = PROPERTY_ENTRY_BOOL(
533 						"named-gpio-lines");
534 
535 		pdevinfo.name = GPIO_MOCKUP_NAME;
536 		pdevinfo.id = i;
537 		pdevinfo.properties = properties;
538 
539 		pdev = platform_device_register_full(&pdevinfo);
540 		if (IS_ERR(pdev)) {
541 			gpio_mockup_err("error registering device");
542 			platform_driver_unregister(&gpio_mockup_driver);
543 			gpio_mockup_unregister_pdevs();
544 			return PTR_ERR(pdev);
545 		}
546 
547 		gpio_mockup_pdevs[i] = pdev;
548 	}
549 
550 	return 0;
551 }
552 
553 static void __exit gpio_mockup_exit(void)
554 {
555 	debugfs_remove_recursive(gpio_mockup_dbg_dir);
556 	platform_driver_unregister(&gpio_mockup_driver);
557 	gpio_mockup_unregister_pdevs();
558 }
559 
560 module_init(gpio_mockup_init);
561 module_exit(gpio_mockup_exit);
562 
563 MODULE_AUTHOR("Kamlakant Patel <kamlakant.patel@broadcom.com>");
564 MODULE_AUTHOR("Bamvor Jian Zhang <bamv2005@gmail.com>");
565 MODULE_AUTHOR("Bartosz Golaszewski <brgl@bgdev.pl>");
566 MODULE_DESCRIPTION("GPIO Testing driver");
567 MODULE_LICENSE("GPL v2");
568