1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * GPIO Testing Device Driver
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>
10 #include <linux/debugfs.h>
11 #include <linux/gpio/consumer.h>
12 #include <linux/gpio/driver.h>
13 #include <linux/init.h>
14 #include <linux/interrupt.h>
15 #include <linux/irq.h>
16 #include <linux/irq_sim.h>
17 #include <linux/irqdomain.h>
18 #include <linux/module.h>
19 #include <linux/platform_device.h>
20 #include <linux/property.h>
21 #include <linux/slab.h>
22 #include <linux/uaccess.h>
26 #define GPIO_MOCKUP_NAME "gpio-mockup"
27 #define GPIO_MOCKUP_MAX_GC 10
29 * We're storing two values per chip: the GPIO base and the number
32 #define GPIO_MOCKUP_MAX_RANGES (GPIO_MOCKUP_MAX_GC * 2)
33 /* Maximum of three properties + the sentinel. */
34 #define GPIO_MOCKUP_MAX_PROP 4
36 #define gpio_mockup_err(...) pr_err(GPIO_MOCKUP_NAME ": " __VA_ARGS__)
39 * struct gpio_pin_status - structure describing a GPIO status
40 * @dir: Configures direction of gpio as "in" or "out"
41 * @value: Configures status of the gpio as 0(low) or 1(high)
43 struct gpio_mockup_line_status {
49 struct gpio_mockup_chip {
51 struct gpio_mockup_line_status *lines;
52 struct irq_domain *irq_sim_domain;
53 struct dentry *dbg_dir;
57 struct gpio_mockup_dbgfs_private {
58 struct gpio_mockup_chip *chip;
59 struct gpio_desc *desc;
63 static int gpio_mockup_ranges[GPIO_MOCKUP_MAX_RANGES];
64 static int gpio_mockup_num_ranges;
65 module_param_array(gpio_mockup_ranges, int, &gpio_mockup_num_ranges, 0400);
67 static bool gpio_mockup_named_lines;
68 module_param_named(gpio_mockup_named_lines,
69 gpio_mockup_named_lines, bool, 0400);
71 static struct dentry *gpio_mockup_dbg_dir;
73 static int gpio_mockup_range_base(unsigned int index)
75 return gpio_mockup_ranges[index * 2];
78 static int gpio_mockup_range_ngpio(unsigned int index)
80 return gpio_mockup_ranges[index * 2 + 1];
83 static int __gpio_mockup_get(struct gpio_mockup_chip *chip,
86 return chip->lines[offset].value;
89 static int gpio_mockup_get(struct gpio_chip *gc, unsigned int offset)
91 struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
94 mutex_lock(&chip->lock);
95 val = __gpio_mockup_get(chip, offset);
96 mutex_unlock(&chip->lock);
101 static int gpio_mockup_get_multiple(struct gpio_chip *gc,
102 unsigned long *mask, unsigned long *bits)
104 struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
105 unsigned int bit, val;
107 mutex_lock(&chip->lock);
108 for_each_set_bit(bit, mask, gc->ngpio) {
109 val = __gpio_mockup_get(chip, bit);
110 __assign_bit(bit, bits, val);
112 mutex_unlock(&chip->lock);
117 static void __gpio_mockup_set(struct gpio_mockup_chip *chip,
118 unsigned int offset, int value)
120 chip->lines[offset].value = !!value;
123 static void gpio_mockup_set(struct gpio_chip *gc,
124 unsigned int offset, int value)
126 struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
128 mutex_lock(&chip->lock);
129 __gpio_mockup_set(chip, offset, value);
130 mutex_unlock(&chip->lock);
133 static void gpio_mockup_set_multiple(struct gpio_chip *gc,
134 unsigned long *mask, unsigned long *bits)
136 struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
139 mutex_lock(&chip->lock);
140 for_each_set_bit(bit, mask, gc->ngpio)
141 __gpio_mockup_set(chip, bit, test_bit(bit, bits));
142 mutex_unlock(&chip->lock);
145 static int gpio_mockup_apply_pull(struct gpio_mockup_chip *chip,
146 unsigned int offset, int value)
148 int curr, irq, irq_type, ret = 0;
149 struct gpio_desc *desc;
150 struct gpio_chip *gc;
153 desc = &gc->gpiodev->descs[offset];
155 mutex_lock(&chip->lock);
157 if (test_bit(FLAG_REQUESTED, &desc->flags) &&
158 !test_bit(FLAG_IS_OUT, &desc->flags)) {
159 curr = __gpio_mockup_get(chip, offset);
163 irq = irq_find_mapping(chip->irq_sim_domain, offset);
166 * This is fine - it just means, nobody is listening
167 * for interrupts on this line, otherwise
168 * irq_create_mapping() would have been called from
169 * the to_irq() callback.
173 irq_type = irq_get_trigger_type(irq);
175 if ((value == 1 && (irq_type & IRQ_TYPE_EDGE_RISING)) ||
176 (value == 0 && (irq_type & IRQ_TYPE_EDGE_FALLING))) {
177 ret = irq_set_irqchip_state(irq, IRQCHIP_STATE_PENDING,
185 /* Change the value unless we're actively driving the line. */
186 if (!test_bit(FLAG_REQUESTED, &desc->flags) ||
187 !test_bit(FLAG_IS_OUT, &desc->flags))
188 __gpio_mockup_set(chip, offset, value);
191 chip->lines[offset].pull = value;
192 mutex_unlock(&chip->lock);
196 static int gpio_mockup_set_config(struct gpio_chip *gc,
197 unsigned int offset, unsigned long config)
199 struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
201 switch (pinconf_to_config_param(config)) {
202 case PIN_CONFIG_BIAS_PULL_UP:
203 return gpio_mockup_apply_pull(chip, offset, 1);
204 case PIN_CONFIG_BIAS_PULL_DOWN:
205 return gpio_mockup_apply_pull(chip, offset, 0);
212 static int gpio_mockup_dirout(struct gpio_chip *gc,
213 unsigned int offset, int value)
215 struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
217 mutex_lock(&chip->lock);
218 chip->lines[offset].dir = GPIO_LINE_DIRECTION_OUT;
219 __gpio_mockup_set(chip, offset, value);
220 mutex_unlock(&chip->lock);
225 static int gpio_mockup_dirin(struct gpio_chip *gc, unsigned int offset)
227 struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
229 mutex_lock(&chip->lock);
230 chip->lines[offset].dir = GPIO_LINE_DIRECTION_IN;
231 mutex_unlock(&chip->lock);
236 static int gpio_mockup_get_direction(struct gpio_chip *gc, unsigned int offset)
238 struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
241 mutex_lock(&chip->lock);
242 direction = chip->lines[offset].dir;
243 mutex_unlock(&chip->lock);
248 static int gpio_mockup_to_irq(struct gpio_chip *gc, unsigned int offset)
250 struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
252 return irq_create_mapping(chip->irq_sim_domain, offset);
255 static void gpio_mockup_free(struct gpio_chip *gc, unsigned int offset)
257 struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
259 __gpio_mockup_set(chip, offset, chip->lines[offset].pull);
262 static ssize_t gpio_mockup_debugfs_read(struct file *file,
263 char __user *usr_buf,
264 size_t size, loff_t *ppos)
266 struct gpio_mockup_dbgfs_private *priv;
267 struct gpio_mockup_chip *chip;
268 struct seq_file *sfile;
269 struct gpio_chip *gc;
276 sfile = file->private_data;
277 priv = sfile->private;
281 val = gpio_mockup_get(gc, priv->offset);
282 cnt = snprintf(buf, sizeof(buf), "%d\n", val);
284 return simple_read_from_buffer(usr_buf, size, ppos, buf, cnt);
287 static ssize_t gpio_mockup_debugfs_write(struct file *file,
288 const char __user *usr_buf,
289 size_t size, loff_t *ppos)
291 struct gpio_mockup_dbgfs_private *priv;
293 struct seq_file *sfile;
298 rv = kstrtoint_from_user(usr_buf, size, 0, &val);
301 if (val != 0 && val != 1)
304 sfile = file->private_data;
305 priv = sfile->private;
306 rv = gpio_mockup_apply_pull(priv->chip, priv->offset, val);
313 static int gpio_mockup_debugfs_open(struct inode *inode, struct file *file)
315 return single_open(file, NULL, inode->i_private);
319 * Each mockup chip is represented by a directory named after the chip's device
320 * name under /sys/kernel/debug/gpio-mockup/. Each line is represented by
321 * a file using the line's offset as the name under the chip's directory.
323 * Reading from the line's file yields the current *value*, writing to the
324 * line's file changes the current *pull*. Default pull for mockup lines is
328 * - when a line pulled down is requested in output mode and driven high, its
329 * value will return to 0 once it's released
330 * - when the line is requested in output mode and driven high, writing 0 to
331 * the corresponding debugfs file will change the pull to down but the
332 * reported value will still be 1 until the line is released
333 * - line requested in input mode always reports the same value as its pull
335 * - when the line is requested in input mode and monitored for events, writing
336 * the same value to the debugfs file will be a noop, while writing the
337 * opposite value will generate a dummy interrupt with an appropriate edge
339 static const struct file_operations gpio_mockup_debugfs_ops = {
340 .owner = THIS_MODULE,
341 .open = gpio_mockup_debugfs_open,
342 .read = gpio_mockup_debugfs_read,
343 .write = gpio_mockup_debugfs_write,
345 .release = single_release,
348 static void gpio_mockup_debugfs_setup(struct device *dev,
349 struct gpio_mockup_chip *chip)
351 struct gpio_mockup_dbgfs_private *priv;
352 struct gpio_chip *gc;
358 devname = dev_name(&gc->gpiodev->dev);
360 chip->dbg_dir = debugfs_create_dir(devname, gpio_mockup_dbg_dir);
362 for (i = 0; i < gc->ngpio; i++) {
363 name = devm_kasprintf(dev, GFP_KERNEL, "%d", i);
367 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
373 priv->desc = &gc->gpiodev->descs[i];
375 debugfs_create_file(name, 0200, chip->dbg_dir, priv,
376 &gpio_mockup_debugfs_ops);
382 static int gpio_mockup_name_lines(struct device *dev,
383 struct gpio_mockup_chip *chip)
385 struct gpio_chip *gc = &chip->gc;
389 names = devm_kcalloc(dev, gc->ngpio, sizeof(char *), GFP_KERNEL);
393 for (i = 0; i < gc->ngpio; i++) {
394 names[i] = devm_kasprintf(dev, GFP_KERNEL,
395 "%s-%d", gc->label, i);
400 gc->names = (const char *const *)names;
405 static void gpio_mockup_dispose_mappings(void *data)
407 struct gpio_mockup_chip *chip = data;
408 struct gpio_chip *gc = &chip->gc;
411 for (i = 0; i < gc->ngpio; i++) {
412 irq = irq_find_mapping(chip->irq_sim_domain, i);
414 irq_dispose_mapping(irq);
418 static int gpio_mockup_probe(struct platform_device *pdev)
420 struct gpio_mockup_chip *chip;
421 struct gpio_chip *gc;
429 rv = device_property_read_u32(dev, "gpio-base", &base);
433 rv = device_property_read_u16(dev, "nr-gpios", &ngpio);
437 rv = device_property_read_string(dev, "chip-name", &name);
441 chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL);
446 name = devm_kasprintf(dev, GFP_KERNEL,
447 "%s-%c", pdev->name, pdev->id + 'A');
452 mutex_init(&chip->lock);
458 gc->owner = THIS_MODULE;
460 gc->get = gpio_mockup_get;
461 gc->set = gpio_mockup_set;
462 gc->get_multiple = gpio_mockup_get_multiple;
463 gc->set_multiple = gpio_mockup_set_multiple;
464 gc->direction_output = gpio_mockup_dirout;
465 gc->direction_input = gpio_mockup_dirin;
466 gc->get_direction = gpio_mockup_get_direction;
467 gc->set_config = gpio_mockup_set_config;
468 gc->to_irq = gpio_mockup_to_irq;
469 gc->free = gpio_mockup_free;
471 chip->lines = devm_kcalloc(dev, gc->ngpio,
472 sizeof(*chip->lines), GFP_KERNEL);
476 for (i = 0; i < gc->ngpio; i++)
477 chip->lines[i].dir = GPIO_LINE_DIRECTION_IN;
479 if (device_property_read_bool(dev, "named-gpio-lines")) {
480 rv = gpio_mockup_name_lines(dev, chip);
485 chip->irq_sim_domain = devm_irq_domain_create_sim(dev, NULL,
487 if (IS_ERR(chip->irq_sim_domain))
488 return PTR_ERR(chip->irq_sim_domain);
490 rv = devm_add_action_or_reset(dev, gpio_mockup_dispose_mappings, chip);
494 rv = devm_gpiochip_add_data(dev, &chip->gc, chip);
498 gpio_mockup_debugfs_setup(dev, chip);
503 static struct platform_driver gpio_mockup_driver = {
505 .name = GPIO_MOCKUP_NAME,
507 .probe = gpio_mockup_probe,
510 static struct platform_device *gpio_mockup_pdevs[GPIO_MOCKUP_MAX_GC];
512 static void gpio_mockup_unregister_pdevs(void)
514 struct platform_device *pdev;
517 for (i = 0; i < GPIO_MOCKUP_MAX_GC; i++) {
518 pdev = gpio_mockup_pdevs[i];
521 platform_device_unregister(pdev);
525 static int __init gpio_mockup_init(void)
527 struct property_entry properties[GPIO_MOCKUP_MAX_PROP];
528 int i, prop, num_chips, err = 0, base;
529 struct platform_device_info pdevinfo;
530 struct platform_device *pdev;
533 if ((gpio_mockup_num_ranges < 2) ||
534 (gpio_mockup_num_ranges % 2) ||
535 (gpio_mockup_num_ranges > GPIO_MOCKUP_MAX_RANGES))
538 /* Each chip is described by two values. */
539 num_chips = gpio_mockup_num_ranges / 2;
542 * The second value in the <base GPIO - number of GPIOS> pair must
543 * always be greater than 0.
545 for (i = 0; i < num_chips; i++) {
546 if (gpio_mockup_range_ngpio(i) < 0)
550 gpio_mockup_dbg_dir = debugfs_create_dir("gpio-mockup", NULL);
552 err = platform_driver_register(&gpio_mockup_driver);
554 gpio_mockup_err("error registering platform driver\n");
558 for (i = 0; i < num_chips; i++) {
559 memset(properties, 0, sizeof(properties));
560 memset(&pdevinfo, 0, sizeof(pdevinfo));
563 base = gpio_mockup_range_base(i);
565 properties[prop++] = PROPERTY_ENTRY_U32("gpio-base",
568 ngpio = base < 0 ? gpio_mockup_range_ngpio(i)
569 : gpio_mockup_range_ngpio(i) - base;
570 properties[prop++] = PROPERTY_ENTRY_U16("nr-gpios", ngpio);
572 if (gpio_mockup_named_lines)
573 properties[prop++] = PROPERTY_ENTRY_BOOL(
576 pdevinfo.name = GPIO_MOCKUP_NAME;
578 pdevinfo.properties = properties;
580 pdev = platform_device_register_full(&pdevinfo);
582 gpio_mockup_err("error registering device");
583 platform_driver_unregister(&gpio_mockup_driver);
584 gpio_mockup_unregister_pdevs();
585 return PTR_ERR(pdev);
588 gpio_mockup_pdevs[i] = pdev;
594 static void __exit gpio_mockup_exit(void)
596 debugfs_remove_recursive(gpio_mockup_dbg_dir);
597 platform_driver_unregister(&gpio_mockup_driver);
598 gpio_mockup_unregister_pdevs();
601 module_init(gpio_mockup_init);
602 module_exit(gpio_mockup_exit);
604 MODULE_AUTHOR("Kamlakant Patel <kamlakant.patel@broadcom.com>");
605 MODULE_AUTHOR("Bamvor Jian Zhang <bamv2005@gmail.com>");
606 MODULE_AUTHOR("Bartosz Golaszewski <brgl@bgdev.pl>");
607 MODULE_DESCRIPTION("GPIO Testing driver");
608 MODULE_LICENSE("GPL v2");