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