xref: /openbmc/linux/drivers/gpio/gpio-mockup.c (revision 2583303debb7acc77295b77901916d08a4c743c2)
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 	int val, rv, cnt;
208 	char buf[3];
209 
210 
211 	if (*ppos != 0)
212 		return 0;
213 
214 	sfile = file->private_data;
215 	priv = sfile->private;
216 	chip = priv->chip;
217 	gc = &chip->gc;
218 
219 	val = gpio_mockup_get(gc, priv->offset);
220 	cnt = snprintf(buf, sizeof(buf), "%d\n", val);
221 
222 	rv = copy_to_user(usr_buf, buf, cnt);
223 	if (rv)
224 		return rv;
225 
226 	*ppos += cnt;
227 	return cnt;
228 }
229 
230 static ssize_t gpio_mockup_debugfs_write(struct file *file,
231 					 const char __user *usr_buf,
232 					 size_t size, loff_t *ppos)
233 {
234 	struct gpio_mockup_dbgfs_private *priv;
235 	int rv, val, curr, irq, irq_type;
236 	struct gpio_mockup_chip *chip;
237 	struct seq_file *sfile;
238 	struct gpio_desc *desc;
239 	struct gpio_chip *gc;
240 	struct irq_sim *sim;
241 
242 	if (*ppos != 0)
243 		return -EINVAL;
244 
245 	rv = kstrtoint_from_user(usr_buf, size, 0, &val);
246 	if (rv)
247 		return rv;
248 	if (val != 0 && val != 1)
249 		return -EINVAL;
250 
251 	sfile = file->private_data;
252 	priv = sfile->private;
253 	chip = priv->chip;
254 	gc = &chip->gc;
255 	desc = &gc->gpiodev->descs[priv->offset];
256 	sim = &chip->irqsim;
257 
258 	mutex_lock(&chip->lock);
259 
260 	if (test_bit(FLAG_REQUESTED, &desc->flags) &&
261 	    !test_bit(FLAG_IS_OUT, &desc->flags)) {
262 		curr = __gpio_mockup_get(chip, priv->offset);
263 		if (curr == val)
264 			goto out;
265 
266 		irq = irq_sim_irqnum(sim, priv->offset);
267 		irq_type = irq_get_trigger_type(irq);
268 
269 		if ((val == 1 && (irq_type & IRQ_TYPE_EDGE_RISING)) ||
270 		    (val == 0 && (irq_type & IRQ_TYPE_EDGE_FALLING)))
271 			irq_sim_fire(sim, priv->offset);
272 	}
273 
274 	/* Change the value unless we're actively driving the line. */
275 	if (!test_bit(FLAG_REQUESTED, &desc->flags) ||
276 	    !test_bit(FLAG_IS_OUT, &desc->flags))
277 		__gpio_mockup_set(chip, priv->offset, val);
278 
279 out:
280 	chip->lines[priv->offset].pull = val;
281 	mutex_unlock(&chip->lock);
282 
283 	return size;
284 }
285 
286 static int gpio_mockup_debugfs_open(struct inode *inode, struct file *file)
287 {
288 	return single_open(file, NULL, inode->i_private);
289 }
290 
291 /*
292  * Each mockup chip is represented by a directory named after the chip's device
293  * name under /sys/kernel/debug/gpio-mockup/. Each line is represented by
294  * a file using the line's offset as the name under the chip's directory.
295  *
296  * Reading from the line's file yields the current *value*, writing to the
297  * line's file changes the current *pull*. Default pull for mockup lines is
298  * down.
299  *
300  * Examples:
301  * - when a line pulled down is requested in output mode and driven high, its
302  *   value will return to 0 once it's released
303  * - when the line is requested in output mode and driven high, writing 0 to
304  *   the corresponding debugfs file will change the pull to down but the
305  *   reported value will still be 1 until the line is released
306  * - line requested in input mode always reports the same value as its pull
307  *   configuration
308  * - when the line is requested in input mode and monitored for events, writing
309  *   the same value to the debugfs file will be a noop, while writing the
310  *   opposite value will generate a dummy interrupt with an appropriate edge
311  */
312 static const struct file_operations gpio_mockup_debugfs_ops = {
313 	.owner = THIS_MODULE,
314 	.open = gpio_mockup_debugfs_open,
315 	.read = gpio_mockup_debugfs_read,
316 	.write = gpio_mockup_debugfs_write,
317 	.llseek = no_llseek,
318 };
319 
320 static void gpio_mockup_debugfs_setup(struct device *dev,
321 				      struct gpio_mockup_chip *chip)
322 {
323 	struct gpio_mockup_dbgfs_private *priv;
324 	struct dentry *evfile;
325 	struct gpio_chip *gc;
326 	const char *devname;
327 	char *name;
328 	int i;
329 
330 	gc = &chip->gc;
331 	devname = dev_name(&gc->gpiodev->dev);
332 
333 	chip->dbg_dir = debugfs_create_dir(devname, gpio_mockup_dbg_dir);
334 	if (IS_ERR_OR_NULL(chip->dbg_dir))
335 		goto err;
336 
337 	for (i = 0; i < gc->ngpio; i++) {
338 		name = devm_kasprintf(dev, GFP_KERNEL, "%d", i);
339 		if (!name)
340 			goto err;
341 
342 		priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
343 		if (!priv)
344 			goto err;
345 
346 		priv->chip = chip;
347 		priv->offset = i;
348 		priv->desc = &gc->gpiodev->descs[i];
349 
350 		evfile = debugfs_create_file(name, 0200, chip->dbg_dir, priv,
351 					     &gpio_mockup_debugfs_ops);
352 		if (IS_ERR_OR_NULL(evfile))
353 			goto err;
354 	}
355 
356 	return;
357 
358 err:
359 	dev_err(dev, "error creating debugfs files\n");
360 }
361 
362 static int gpio_mockup_name_lines(struct device *dev,
363 				  struct gpio_mockup_chip *chip)
364 {
365 	struct gpio_chip *gc = &chip->gc;
366 	char **names;
367 	int i;
368 
369 	names = devm_kcalloc(dev, gc->ngpio, sizeof(char *), GFP_KERNEL);
370 	if (!names)
371 		return -ENOMEM;
372 
373 	for (i = 0; i < gc->ngpio; i++) {
374 		names[i] = devm_kasprintf(dev, GFP_KERNEL,
375 					  "%s-%d", gc->label, i);
376 		if (!names[i])
377 			return -ENOMEM;
378 	}
379 
380 	gc->names = (const char *const *)names;
381 
382 	return 0;
383 }
384 
385 static int gpio_mockup_probe(struct platform_device *pdev)
386 {
387 	struct gpio_mockup_chip *chip;
388 	struct gpio_chip *gc;
389 	struct device *dev;
390 	const char *name;
391 	int rv, base;
392 	u16 ngpio;
393 
394 	dev = &pdev->dev;
395 
396 	rv = device_property_read_u32(dev, "gpio-base", &base);
397 	if (rv)
398 		base = -1;
399 
400 	rv = device_property_read_u16(dev, "nr-gpios", &ngpio);
401 	if (rv)
402 		return rv;
403 
404 	rv = device_property_read_string(dev, "chip-name", &name);
405 	if (rv)
406 		name = NULL;
407 
408 	chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL);
409 	if (!chip)
410 		return -ENOMEM;
411 
412 	if (!name) {
413 		name = devm_kasprintf(dev, GFP_KERNEL,
414 				      "%s-%c", pdev->name, pdev->id + 'A');
415 		if (!name)
416 			return -ENOMEM;
417 	}
418 
419 	mutex_init(&chip->lock);
420 
421 	gc = &chip->gc;
422 	gc->base = base;
423 	gc->ngpio = ngpio;
424 	gc->label = name;
425 	gc->owner = THIS_MODULE;
426 	gc->parent = dev;
427 	gc->get = gpio_mockup_get;
428 	gc->set = gpio_mockup_set;
429 	gc->get_multiple = gpio_mockup_get_multiple;
430 	gc->set_multiple = gpio_mockup_set_multiple;
431 	gc->direction_output = gpio_mockup_dirout;
432 	gc->direction_input = gpio_mockup_dirin;
433 	gc->get_direction = gpio_mockup_get_direction;
434 	gc->to_irq = gpio_mockup_to_irq;
435 	gc->free = gpio_mockup_free;
436 
437 	chip->lines = devm_kcalloc(dev, gc->ngpio,
438 				   sizeof(*chip->lines), GFP_KERNEL);
439 	if (!chip->lines)
440 		return -ENOMEM;
441 
442 	if (device_property_read_bool(dev, "named-gpio-lines")) {
443 		rv = gpio_mockup_name_lines(dev, chip);
444 		if (rv)
445 			return rv;
446 	}
447 
448 	rv = devm_irq_sim_init(dev, &chip->irqsim, gc->ngpio);
449 	if (rv < 0)
450 		return rv;
451 
452 	rv = devm_gpiochip_add_data(dev, &chip->gc, chip);
453 	if (rv)
454 		return rv;
455 
456 	if (!IS_ERR_OR_NULL(gpio_mockup_dbg_dir))
457 		gpio_mockup_debugfs_setup(dev, chip);
458 
459 	return 0;
460 }
461 
462 static struct platform_driver gpio_mockup_driver = {
463 	.driver = {
464 		.name = GPIO_MOCKUP_NAME,
465 	},
466 	.probe = gpio_mockup_probe,
467 };
468 
469 static struct platform_device *gpio_mockup_pdevs[GPIO_MOCKUP_MAX_GC];
470 
471 static void gpio_mockup_unregister_pdevs(void)
472 {
473 	struct platform_device *pdev;
474 	int i;
475 
476 	for (i = 0; i < GPIO_MOCKUP_MAX_GC; i++) {
477 		pdev = gpio_mockup_pdevs[i];
478 
479 		if (pdev)
480 			platform_device_unregister(pdev);
481 	}
482 }
483 
484 static int __init gpio_mockup_init(void)
485 {
486 	struct property_entry properties[GPIO_MOCKUP_MAX_PROP];
487 	int i, prop, num_chips, err = 0, base;
488 	struct platform_device_info pdevinfo;
489 	struct platform_device *pdev;
490 	u16 ngpio;
491 
492 	if ((gpio_mockup_num_ranges < 2) ||
493 	    (gpio_mockup_num_ranges % 2) ||
494 	    (gpio_mockup_num_ranges > GPIO_MOCKUP_MAX_RANGES))
495 		return -EINVAL;
496 
497 	/* Each chip is described by two values. */
498 	num_chips = gpio_mockup_num_ranges / 2;
499 
500 	/*
501 	 * The second value in the <base GPIO - number of GPIOS> pair must
502 	 * always be greater than 0.
503 	 */
504 	for (i = 0; i < num_chips; i++) {
505 		if (gpio_mockup_range_ngpio(i) < 0)
506 			return -EINVAL;
507 	}
508 
509 	gpio_mockup_dbg_dir = debugfs_create_dir("gpio-mockup", NULL);
510 	if (IS_ERR_OR_NULL(gpio_mockup_dbg_dir))
511 		gpio_mockup_err("error creating debugfs directory\n");
512 
513 	err = platform_driver_register(&gpio_mockup_driver);
514 	if (err) {
515 		gpio_mockup_err("error registering platform driver\n");
516 		return err;
517 	}
518 
519 	for (i = 0; i < num_chips; i++) {
520 		memset(properties, 0, sizeof(properties));
521 		memset(&pdevinfo, 0, sizeof(pdevinfo));
522 		prop = 0;
523 
524 		base = gpio_mockup_range_base(i);
525 		if (base >= 0)
526 			properties[prop++] = PROPERTY_ENTRY_U32("gpio-base",
527 								base);
528 
529 		ngpio = base < 0 ? gpio_mockup_range_ngpio(i)
530 				 : gpio_mockup_range_ngpio(i) - base;
531 		properties[prop++] = PROPERTY_ENTRY_U16("nr-gpios", ngpio);
532 
533 		if (gpio_mockup_named_lines)
534 			properties[prop++] = PROPERTY_ENTRY_BOOL(
535 						"named-gpio-lines");
536 
537 		pdevinfo.name = GPIO_MOCKUP_NAME;
538 		pdevinfo.id = i;
539 		pdevinfo.properties = properties;
540 
541 		pdev = platform_device_register_full(&pdevinfo);
542 		if (IS_ERR(pdev)) {
543 			gpio_mockup_err("error registering device");
544 			platform_driver_unregister(&gpio_mockup_driver);
545 			gpio_mockup_unregister_pdevs();
546 			return PTR_ERR(pdev);
547 		}
548 
549 		gpio_mockup_pdevs[i] = pdev;
550 	}
551 
552 	return 0;
553 }
554 
555 static void __exit gpio_mockup_exit(void)
556 {
557 	debugfs_remove_recursive(gpio_mockup_dbg_dir);
558 	platform_driver_unregister(&gpio_mockup_driver);
559 	gpio_mockup_unregister_pdevs();
560 }
561 
562 module_init(gpio_mockup_init);
563 module_exit(gpio_mockup_exit);
564 
565 MODULE_AUTHOR("Kamlakant Patel <kamlakant.patel@broadcom.com>");
566 MODULE_AUTHOR("Bamvor Jian Zhang <bamv2005@gmail.com>");
567 MODULE_AUTHOR("Bartosz Golaszewski <brgl@bgdev.pl>");
568 MODULE_DESCRIPTION("GPIO Testing driver");
569 MODULE_LICENSE("GPL v2");
570