1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * GPIO Testing Device Driver
10 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12 #include <linux/debugfs.h>
13 #include <linux/gpio/driver.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/mod_devicetable.h>
19 #include <linux/module.h>
20 #include <linux/platform_device.h>
21 #include <linux/property.h>
22 #include <linux/slab.h>
23 #include <linux/string_helpers.h>
24 #include <linux/uaccess.h>
28 #define GPIO_MOCKUP_MAX_GC 10
30 * We're storing two values per chip: the GPIO base and the number
33 #define GPIO_MOCKUP_MAX_RANGES (GPIO_MOCKUP_MAX_GC * 2)
34 /* Maximum of four properties + the sentinel. */
35 #define GPIO_MOCKUP_MAX_PROP 5
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)
42 struct gpio_mockup_line_status {
48 struct gpio_mockup_chip {
50 struct gpio_mockup_line_status *lines;
51 struct irq_domain *irq_sim_domain;
52 struct dentry *dbg_dir;
56 struct gpio_mockup_dbgfs_private {
57 struct gpio_mockup_chip *chip;
58 struct gpio_desc *desc;
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);
66 static bool gpio_mockup_named_lines;
67 module_param_named(gpio_mockup_named_lines,
68 gpio_mockup_named_lines, bool, 0400);
70 static struct dentry *gpio_mockup_dbg_dir;
72 static int gpio_mockup_range_base(unsigned int index)
74 return gpio_mockup_ranges[index * 2];
77 static int gpio_mockup_range_ngpio(unsigned int index)
79 return gpio_mockup_ranges[index * 2 + 1];
82 static int __gpio_mockup_get(struct gpio_mockup_chip *chip,
85 return chip->lines[offset].value;
88 static int gpio_mockup_get(struct gpio_chip *gc, unsigned int offset)
90 struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
93 mutex_lock(&chip->lock);
94 val = __gpio_mockup_get(chip, offset);
95 mutex_unlock(&chip->lock);
100 static int gpio_mockup_get_multiple(struct gpio_chip *gc,
101 unsigned long *mask, unsigned long *bits)
103 struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
104 unsigned int bit, val;
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);
111 mutex_unlock(&chip->lock);
116 static void __gpio_mockup_set(struct gpio_mockup_chip *chip,
117 unsigned int offset, int value)
119 chip->lines[offset].value = !!value;
122 static void gpio_mockup_set(struct gpio_chip *gc,
123 unsigned int offset, int value)
125 struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
127 mutex_lock(&chip->lock);
128 __gpio_mockup_set(chip, offset, value);
129 mutex_unlock(&chip->lock);
132 static void gpio_mockup_set_multiple(struct gpio_chip *gc,
133 unsigned long *mask, unsigned long *bits)
135 struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
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);
144 static int gpio_mockup_apply_pull(struct gpio_mockup_chip *chip,
145 unsigned int offset, int value)
147 struct gpio_chip *gc = &chip->gc;
148 struct gpio_desc *desc = gpiochip_get_desc(gc, offset);
149 int curr, irq, irq_type, ret = 0;
151 mutex_lock(&chip->lock);
153 if (test_bit(FLAG_REQUESTED, &desc->flags) &&
154 !test_bit(FLAG_IS_OUT, &desc->flags)) {
155 curr = __gpio_mockup_get(chip, offset);
159 irq = irq_find_mapping(chip->irq_sim_domain, offset);
162 * This is fine - it just means, nobody is listening
163 * for interrupts on this line, otherwise
164 * irq_create_mapping() would have been called from
165 * the to_irq() callback.
169 irq_type = irq_get_trigger_type(irq);
171 if ((value == 1 && (irq_type & IRQ_TYPE_EDGE_RISING)) ||
172 (value == 0 && (irq_type & IRQ_TYPE_EDGE_FALLING))) {
173 ret = irq_set_irqchip_state(irq, IRQCHIP_STATE_PENDING,
181 /* Change the value unless we're actively driving the line. */
182 if (!test_bit(FLAG_REQUESTED, &desc->flags) ||
183 !test_bit(FLAG_IS_OUT, &desc->flags))
184 __gpio_mockup_set(chip, offset, value);
187 chip->lines[offset].pull = value;
188 mutex_unlock(&chip->lock);
192 static int gpio_mockup_set_config(struct gpio_chip *gc,
193 unsigned int offset, unsigned long config)
195 struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
197 switch (pinconf_to_config_param(config)) {
198 case PIN_CONFIG_BIAS_PULL_UP:
199 return gpio_mockup_apply_pull(chip, offset, 1);
200 case PIN_CONFIG_BIAS_PULL_DOWN:
201 return gpio_mockup_apply_pull(chip, offset, 0);
208 static int gpio_mockup_dirout(struct gpio_chip *gc,
209 unsigned int offset, int value)
211 struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
213 mutex_lock(&chip->lock);
214 chip->lines[offset].dir = GPIO_LINE_DIRECTION_OUT;
215 __gpio_mockup_set(chip, offset, value);
216 mutex_unlock(&chip->lock);
221 static int gpio_mockup_dirin(struct gpio_chip *gc, unsigned int offset)
223 struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
225 mutex_lock(&chip->lock);
226 chip->lines[offset].dir = GPIO_LINE_DIRECTION_IN;
227 mutex_unlock(&chip->lock);
232 static int gpio_mockup_get_direction(struct gpio_chip *gc, unsigned int offset)
234 struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
237 mutex_lock(&chip->lock);
238 direction = chip->lines[offset].dir;
239 mutex_unlock(&chip->lock);
244 static int gpio_mockup_to_irq(struct gpio_chip *gc, unsigned int offset)
246 struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
248 return irq_create_mapping(chip->irq_sim_domain, offset);
251 static void gpio_mockup_free(struct gpio_chip *gc, unsigned int offset)
253 struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
255 __gpio_mockup_set(chip, offset, chip->lines[offset].pull);
258 static ssize_t gpio_mockup_debugfs_read(struct file *file,
259 char __user *usr_buf,
260 size_t size, loff_t *ppos)
262 struct gpio_mockup_dbgfs_private *priv;
263 struct gpio_mockup_chip *chip;
264 struct seq_file *sfile;
265 struct gpio_chip *gc;
272 sfile = file->private_data;
273 priv = sfile->private;
277 val = gpio_mockup_get(gc, priv->offset);
278 cnt = snprintf(buf, sizeof(buf), "%d\n", val);
280 return simple_read_from_buffer(usr_buf, size, ppos, buf, cnt);
283 static ssize_t gpio_mockup_debugfs_write(struct file *file,
284 const char __user *usr_buf,
285 size_t size, loff_t *ppos)
287 struct gpio_mockup_dbgfs_private *priv;
289 struct seq_file *sfile;
294 rv = kstrtoint_from_user(usr_buf, size, 0, &val);
297 if (val != 0 && val != 1)
300 sfile = file->private_data;
301 priv = sfile->private;
302 rv = gpio_mockup_apply_pull(priv->chip, priv->offset, val);
309 static int gpio_mockup_debugfs_open(struct inode *inode, struct file *file)
311 return single_open(file, NULL, inode->i_private);
315 * Each mockup chip is represented by a directory named after the chip's device
316 * name under /sys/kernel/debug/gpio-mockup/. Each line is represented by
317 * a file using the line's offset as the name under the chip's directory.
319 * Reading from the line's file yields the current *value*, writing to the
320 * line's file changes the current *pull*. Default pull for mockup lines is
324 * - when a line pulled down is requested in output mode and driven high, its
325 * value will return to 0 once it's released
326 * - when the line is requested in output mode and driven high, writing 0 to
327 * the corresponding debugfs file will change the pull to down but the
328 * reported value will still be 1 until the line is released
329 * - line requested in input mode always reports the same value as its pull
331 * - when the line is requested in input mode and monitored for events, writing
332 * the same value to the debugfs file will be a noop, while writing the
333 * opposite value will generate a dummy interrupt with an appropriate edge
335 static const struct file_operations gpio_mockup_debugfs_ops = {
336 .owner = THIS_MODULE,
337 .open = gpio_mockup_debugfs_open,
338 .read = gpio_mockup_debugfs_read,
339 .write = gpio_mockup_debugfs_write,
341 .release = single_release,
344 static void gpio_mockup_debugfs_setup(struct device *dev,
345 struct gpio_mockup_chip *chip)
347 struct gpio_mockup_dbgfs_private *priv;
348 struct gpio_chip *gc;
354 devname = dev_name(&gc->gpiodev->dev);
356 chip->dbg_dir = debugfs_create_dir(devname, gpio_mockup_dbg_dir);
358 for (i = 0; i < gc->ngpio; i++) {
359 name = devm_kasprintf(dev, GFP_KERNEL, "%d", i);
363 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
369 priv->desc = gpiochip_get_desc(gc, i);
371 debugfs_create_file(name, 0200, chip->dbg_dir, priv,
372 &gpio_mockup_debugfs_ops);
376 static void gpio_mockup_debugfs_cleanup(void *data)
378 struct gpio_mockup_chip *chip = data;
380 debugfs_remove_recursive(chip->dbg_dir);
383 static void gpio_mockup_dispose_mappings(void *data)
385 struct gpio_mockup_chip *chip = data;
386 struct gpio_chip *gc = &chip->gc;
389 for (i = 0; i < gc->ngpio; i++) {
390 irq = irq_find_mapping(chip->irq_sim_domain, i);
392 irq_dispose_mapping(irq);
396 static int gpio_mockup_probe(struct platform_device *pdev)
398 struct gpio_mockup_chip *chip;
399 struct gpio_chip *gc;
407 rv = device_property_read_u32(dev, "gpio-base", &base);
411 rv = device_property_read_u16(dev, "nr-gpios", &ngpio);
415 rv = device_property_read_string(dev, "chip-label", &name);
417 name = dev_name(dev);
419 chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL);
423 mutex_init(&chip->lock);
429 gc->owner = THIS_MODULE;
431 gc->get = gpio_mockup_get;
432 gc->set = gpio_mockup_set;
433 gc->get_multiple = gpio_mockup_get_multiple;
434 gc->set_multiple = gpio_mockup_set_multiple;
435 gc->direction_output = gpio_mockup_dirout;
436 gc->direction_input = gpio_mockup_dirin;
437 gc->get_direction = gpio_mockup_get_direction;
438 gc->set_config = gpio_mockup_set_config;
439 gc->to_irq = gpio_mockup_to_irq;
440 gc->free = gpio_mockup_free;
442 chip->lines = devm_kcalloc(dev, gc->ngpio,
443 sizeof(*chip->lines), GFP_KERNEL);
447 for (i = 0; i < gc->ngpio; i++)
448 chip->lines[i].dir = GPIO_LINE_DIRECTION_IN;
450 chip->irq_sim_domain = devm_irq_domain_create_sim(dev, NULL,
452 if (IS_ERR(chip->irq_sim_domain))
453 return PTR_ERR(chip->irq_sim_domain);
455 rv = devm_add_action_or_reset(dev, gpio_mockup_dispose_mappings, chip);
459 rv = devm_gpiochip_add_data(dev, &chip->gc, chip);
463 gpio_mockup_debugfs_setup(dev, chip);
465 return devm_add_action_or_reset(dev, gpio_mockup_debugfs_cleanup, chip);
468 static const struct of_device_id gpio_mockup_of_match[] = {
469 { .compatible = "gpio-mockup", },
472 MODULE_DEVICE_TABLE(of, gpio_mockup_of_match);
474 static struct platform_driver gpio_mockup_driver = {
476 .name = "gpio-mockup",
477 .of_match_table = gpio_mockup_of_match,
479 .probe = gpio_mockup_probe,
482 static struct platform_device *gpio_mockup_pdevs[GPIO_MOCKUP_MAX_GC];
484 static void gpio_mockup_unregister_pdevs(void)
486 struct platform_device *pdev;
487 struct fwnode_handle *fwnode;
490 for (i = 0; i < GPIO_MOCKUP_MAX_GC; i++) {
491 pdev = gpio_mockup_pdevs[i];
495 fwnode = dev_fwnode(&pdev->dev);
496 platform_device_unregister(pdev);
497 fwnode_remove_software_node(fwnode);
501 static int __init gpio_mockup_register_chip(int idx)
503 struct property_entry properties[GPIO_MOCKUP_MAX_PROP];
504 struct platform_device_info pdevinfo;
505 struct platform_device *pdev;
506 struct fwnode_handle *fwnode;
507 char **line_names = NULL;
512 memset(properties, 0, sizeof(properties));
513 memset(&pdevinfo, 0, sizeof(pdevinfo));
515 snprintf(chip_label, sizeof(chip_label), "gpio-mockup-%c", idx + 'A');
516 properties[prop++] = PROPERTY_ENTRY_STRING("chip-label", chip_label);
518 base = gpio_mockup_range_base(idx);
520 properties[prop++] = PROPERTY_ENTRY_U32("gpio-base", base);
522 ngpio = base < 0 ? gpio_mockup_range_ngpio(idx)
523 : gpio_mockup_range_ngpio(idx) - base;
524 properties[prop++] = PROPERTY_ENTRY_U16("nr-gpios", ngpio);
526 if (gpio_mockup_named_lines) {
527 line_names = kasprintf_strarray(GFP_KERNEL, chip_label, ngpio);
531 properties[prop++] = PROPERTY_ENTRY_STRING_ARRAY_LEN(
532 "gpio-line-names", line_names, ngpio);
535 fwnode = fwnode_create_software_node(properties, NULL);
536 if (IS_ERR(fwnode)) {
537 kfree_strarray(line_names, ngpio);
538 return PTR_ERR(fwnode);
541 pdevinfo.name = "gpio-mockup";
543 pdevinfo.fwnode = fwnode;
545 pdev = platform_device_register_full(&pdevinfo);
546 kfree_strarray(line_names, ngpio);
548 fwnode_remove_software_node(fwnode);
549 pr_err("error registering device");
550 return PTR_ERR(pdev);
553 gpio_mockup_pdevs[idx] = pdev;
558 static int __init gpio_mockup_init(void)
560 int i, num_chips, err;
562 if ((gpio_mockup_num_ranges % 2) ||
563 (gpio_mockup_num_ranges > GPIO_MOCKUP_MAX_RANGES))
566 /* Each chip is described by two values. */
567 num_chips = gpio_mockup_num_ranges / 2;
570 * The second value in the <base GPIO - number of GPIOS> pair must
571 * always be greater than 0.
573 for (i = 0; i < num_chips; i++) {
574 if (gpio_mockup_range_ngpio(i) < 0)
578 gpio_mockup_dbg_dir = debugfs_create_dir("gpio-mockup", NULL);
580 err = platform_driver_register(&gpio_mockup_driver);
582 pr_err("error registering platform driver\n");
583 debugfs_remove_recursive(gpio_mockup_dbg_dir);
587 for (i = 0; i < num_chips; i++) {
588 err = gpio_mockup_register_chip(i);
590 platform_driver_unregister(&gpio_mockup_driver);
591 gpio_mockup_unregister_pdevs();
592 debugfs_remove_recursive(gpio_mockup_dbg_dir);
600 static void __exit gpio_mockup_exit(void)
602 gpio_mockup_unregister_pdevs();
603 debugfs_remove_recursive(gpio_mockup_dbg_dir);
604 platform_driver_unregister(&gpio_mockup_driver);
607 module_init(gpio_mockup_init);
608 module_exit(gpio_mockup_exit);
613 MODULE_DESCRIPTION("GPIO Testing driver");
614 MODULE_LICENSE("GPL v2");