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 #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/module.h>
19 #include <linux/platform_device.h>
20 #include <linux/property.h>
21 #include <linux/slab.h>
22 #include <linux/string_helpers.h>
23 #include <linux/uaccess.h>
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 four properties + the sentinel. */
34 #define GPIO_MOCKUP_MAX_PROP 5
37 * struct gpio_pin_status - structure describing a GPIO status
38 * @dir: Configures direction of gpio as "in" or "out"
39 * @value: Configures status of the gpio as 0(low) or 1(high)
41 struct gpio_mockup_line_status
{
47 struct gpio_mockup_chip
{
49 struct gpio_mockup_line_status
*lines
;
50 struct irq_domain
*irq_sim_domain
;
51 struct dentry
*dbg_dir
;
55 struct gpio_mockup_dbgfs_private
{
56 struct gpio_mockup_chip
*chip
;
57 struct gpio_desc
*desc
;
61 static int gpio_mockup_ranges
[GPIO_MOCKUP_MAX_RANGES
];
62 static int gpio_mockup_num_ranges
;
63 module_param_array(gpio_mockup_ranges
, int, &gpio_mockup_num_ranges
, 0400);
65 static bool gpio_mockup_named_lines
;
66 module_param_named(gpio_mockup_named_lines
,
67 gpio_mockup_named_lines
, bool, 0400);
69 static struct dentry
*gpio_mockup_dbg_dir
;
71 static int gpio_mockup_range_base(unsigned int index
)
73 return gpio_mockup_ranges
[index
* 2];
76 static int gpio_mockup_range_ngpio(unsigned int index
)
78 return gpio_mockup_ranges
[index
* 2 + 1];
81 static int __gpio_mockup_get(struct gpio_mockup_chip
*chip
,
84 return chip
->lines
[offset
].value
;
87 static int gpio_mockup_get(struct gpio_chip
*gc
, unsigned int offset
)
89 struct gpio_mockup_chip
*chip
= gpiochip_get_data(gc
);
92 mutex_lock(&chip
->lock
);
93 val
= __gpio_mockup_get(chip
, offset
);
94 mutex_unlock(&chip
->lock
);
99 static int gpio_mockup_get_multiple(struct gpio_chip
*gc
,
100 unsigned long *mask
, unsigned long *bits
)
102 struct gpio_mockup_chip
*chip
= gpiochip_get_data(gc
);
103 unsigned int bit
, val
;
105 mutex_lock(&chip
->lock
);
106 for_each_set_bit(bit
, mask
, gc
->ngpio
) {
107 val
= __gpio_mockup_get(chip
, bit
);
108 __assign_bit(bit
, bits
, val
);
110 mutex_unlock(&chip
->lock
);
115 static void __gpio_mockup_set(struct gpio_mockup_chip
*chip
,
116 unsigned int offset
, int value
)
118 chip
->lines
[offset
].value
= !!value
;
121 static void gpio_mockup_set(struct gpio_chip
*gc
,
122 unsigned int offset
, int value
)
124 struct gpio_mockup_chip
*chip
= gpiochip_get_data(gc
);
126 mutex_lock(&chip
->lock
);
127 __gpio_mockup_set(chip
, offset
, value
);
128 mutex_unlock(&chip
->lock
);
131 static void gpio_mockup_set_multiple(struct gpio_chip
*gc
,
132 unsigned long *mask
, unsigned long *bits
)
134 struct gpio_mockup_chip
*chip
= gpiochip_get_data(gc
);
137 mutex_lock(&chip
->lock
);
138 for_each_set_bit(bit
, mask
, gc
->ngpio
)
139 __gpio_mockup_set(chip
, bit
, test_bit(bit
, bits
));
140 mutex_unlock(&chip
->lock
);
143 static int gpio_mockup_apply_pull(struct gpio_mockup_chip
*chip
,
144 unsigned int offset
, int value
)
146 int curr
, irq
, irq_type
, ret
= 0;
147 struct gpio_desc
*desc
;
148 struct gpio_chip
*gc
;
151 desc
= &gc
->gpiodev
->descs
[offset
];
153 mutex_lock(&chip
->lock
);
155 if (test_bit(FLAG_REQUESTED
, &desc
->flags
) &&
156 !test_bit(FLAG_IS_OUT
, &desc
->flags
)) {
157 curr
= __gpio_mockup_get(chip
, offset
);
161 irq
= irq_find_mapping(chip
->irq_sim_domain
, offset
);
164 * This is fine - it just means, nobody is listening
165 * for interrupts on this line, otherwise
166 * irq_create_mapping() would have been called from
167 * the to_irq() callback.
171 irq_type
= irq_get_trigger_type(irq
);
173 if ((value
== 1 && (irq_type
& IRQ_TYPE_EDGE_RISING
)) ||
174 (value
== 0 && (irq_type
& IRQ_TYPE_EDGE_FALLING
))) {
175 ret
= irq_set_irqchip_state(irq
, IRQCHIP_STATE_PENDING
,
183 /* Change the value unless we're actively driving the line. */
184 if (!test_bit(FLAG_REQUESTED
, &desc
->flags
) ||
185 !test_bit(FLAG_IS_OUT
, &desc
->flags
))
186 __gpio_mockup_set(chip
, offset
, value
);
189 chip
->lines
[offset
].pull
= value
;
190 mutex_unlock(&chip
->lock
);
194 static int gpio_mockup_set_config(struct gpio_chip
*gc
,
195 unsigned int offset
, unsigned long config
)
197 struct gpio_mockup_chip
*chip
= gpiochip_get_data(gc
);
199 switch (pinconf_to_config_param(config
)) {
200 case PIN_CONFIG_BIAS_PULL_UP
:
201 return gpio_mockup_apply_pull(chip
, offset
, 1);
202 case PIN_CONFIG_BIAS_PULL_DOWN
:
203 return gpio_mockup_apply_pull(chip
, offset
, 0);
210 static int gpio_mockup_dirout(struct gpio_chip
*gc
,
211 unsigned int offset
, int value
)
213 struct gpio_mockup_chip
*chip
= gpiochip_get_data(gc
);
215 mutex_lock(&chip
->lock
);
216 chip
->lines
[offset
].dir
= GPIO_LINE_DIRECTION_OUT
;
217 __gpio_mockup_set(chip
, offset
, value
);
218 mutex_unlock(&chip
->lock
);
223 static int gpio_mockup_dirin(struct gpio_chip
*gc
, unsigned int offset
)
225 struct gpio_mockup_chip
*chip
= gpiochip_get_data(gc
);
227 mutex_lock(&chip
->lock
);
228 chip
->lines
[offset
].dir
= GPIO_LINE_DIRECTION_IN
;
229 mutex_unlock(&chip
->lock
);
234 static int gpio_mockup_get_direction(struct gpio_chip
*gc
, unsigned int offset
)
236 struct gpio_mockup_chip
*chip
= gpiochip_get_data(gc
);
239 mutex_lock(&chip
->lock
);
240 direction
= chip
->lines
[offset
].dir
;
241 mutex_unlock(&chip
->lock
);
246 static int gpio_mockup_to_irq(struct gpio_chip
*gc
, unsigned int offset
)
248 struct gpio_mockup_chip
*chip
= gpiochip_get_data(gc
);
250 return irq_create_mapping(chip
->irq_sim_domain
, offset
);
253 static void gpio_mockup_free(struct gpio_chip
*gc
, unsigned int offset
)
255 struct gpio_mockup_chip
*chip
= gpiochip_get_data(gc
);
257 __gpio_mockup_set(chip
, offset
, chip
->lines
[offset
].pull
);
260 static ssize_t
gpio_mockup_debugfs_read(struct file
*file
,
261 char __user
*usr_buf
,
262 size_t size
, loff_t
*ppos
)
264 struct gpio_mockup_dbgfs_private
*priv
;
265 struct gpio_mockup_chip
*chip
;
266 struct seq_file
*sfile
;
267 struct gpio_chip
*gc
;
274 sfile
= file
->private_data
;
275 priv
= sfile
->private;
279 val
= gpio_mockup_get(gc
, priv
->offset
);
280 cnt
= snprintf(buf
, sizeof(buf
), "%d\n", val
);
282 return simple_read_from_buffer(usr_buf
, size
, ppos
, buf
, cnt
);
285 static ssize_t
gpio_mockup_debugfs_write(struct file
*file
,
286 const char __user
*usr_buf
,
287 size_t size
, loff_t
*ppos
)
289 struct gpio_mockup_dbgfs_private
*priv
;
291 struct seq_file
*sfile
;
296 rv
= kstrtoint_from_user(usr_buf
, size
, 0, &val
);
299 if (val
!= 0 && val
!= 1)
302 sfile
= file
->private_data
;
303 priv
= sfile
->private;
304 rv
= gpio_mockup_apply_pull(priv
->chip
, priv
->offset
, val
);
311 static int gpio_mockup_debugfs_open(struct inode
*inode
, struct file
*file
)
313 return single_open(file
, NULL
, inode
->i_private
);
317 * Each mockup chip is represented by a directory named after the chip's device
318 * name under /sys/kernel/debug/gpio-mockup/. Each line is represented by
319 * a file using the line's offset as the name under the chip's directory.
321 * Reading from the line's file yields the current *value*, writing to the
322 * line's file changes the current *pull*. Default pull for mockup lines is
326 * - when a line pulled down is requested in output mode and driven high, its
327 * value will return to 0 once it's released
328 * - when the line is requested in output mode and driven high, writing 0 to
329 * the corresponding debugfs file will change the pull to down but the
330 * reported value will still be 1 until the line is released
331 * - line requested in input mode always reports the same value as its pull
333 * - when the line is requested in input mode and monitored for events, writing
334 * the same value to the debugfs file will be a noop, while writing the
335 * opposite value will generate a dummy interrupt with an appropriate edge
337 static const struct file_operations gpio_mockup_debugfs_ops
= {
338 .owner
= THIS_MODULE
,
339 .open
= gpio_mockup_debugfs_open
,
340 .read
= gpio_mockup_debugfs_read
,
341 .write
= gpio_mockup_debugfs_write
,
343 .release
= single_release
,
346 static void gpio_mockup_debugfs_setup(struct device
*dev
,
347 struct gpio_mockup_chip
*chip
)
349 struct gpio_mockup_dbgfs_private
*priv
;
350 struct gpio_chip
*gc
;
356 devname
= dev_name(&gc
->gpiodev
->dev
);
358 chip
->dbg_dir
= debugfs_create_dir(devname
, gpio_mockup_dbg_dir
);
360 for (i
= 0; i
< gc
->ngpio
; i
++) {
361 name
= devm_kasprintf(dev
, GFP_KERNEL
, "%d", i
);
365 priv
= devm_kzalloc(dev
, sizeof(*priv
), GFP_KERNEL
);
371 priv
->desc
= &gc
->gpiodev
->descs
[i
];
373 debugfs_create_file(name
, 0200, chip
->dbg_dir
, priv
,
374 &gpio_mockup_debugfs_ops
);
378 static void gpio_mockup_dispose_mappings(void *data
)
380 struct gpio_mockup_chip
*chip
= data
;
381 struct gpio_chip
*gc
= &chip
->gc
;
384 for (i
= 0; i
< gc
->ngpio
; i
++) {
385 irq
= irq_find_mapping(chip
->irq_sim_domain
, i
);
387 irq_dispose_mapping(irq
);
391 static int gpio_mockup_probe(struct platform_device
*pdev
)
393 struct gpio_mockup_chip
*chip
;
394 struct gpio_chip
*gc
;
402 rv
= device_property_read_u32(dev
, "gpio-base", &base
);
406 rv
= device_property_read_u16(dev
, "nr-gpios", &ngpio
);
410 rv
= device_property_read_string(dev
, "chip-label", &name
);
412 name
= dev_name(dev
);
414 chip
= devm_kzalloc(dev
, sizeof(*chip
), GFP_KERNEL
);
418 mutex_init(&chip
->lock
);
424 gc
->owner
= THIS_MODULE
;
426 gc
->get
= gpio_mockup_get
;
427 gc
->set
= gpio_mockup_set
;
428 gc
->get_multiple
= gpio_mockup_get_multiple
;
429 gc
->set_multiple
= gpio_mockup_set_multiple
;
430 gc
->direction_output
= gpio_mockup_dirout
;
431 gc
->direction_input
= gpio_mockup_dirin
;
432 gc
->get_direction
= gpio_mockup_get_direction
;
433 gc
->set_config
= gpio_mockup_set_config
;
434 gc
->to_irq
= gpio_mockup_to_irq
;
435 gc
->free
= gpio_mockup_free
;
437 chip
->lines
= devm_kcalloc(dev
, gc
->ngpio
,
438 sizeof(*chip
->lines
), GFP_KERNEL
);
442 for (i
= 0; i
< gc
->ngpio
; i
++)
443 chip
->lines
[i
].dir
= GPIO_LINE_DIRECTION_IN
;
445 chip
->irq_sim_domain
= devm_irq_domain_create_sim(dev
, NULL
,
447 if (IS_ERR(chip
->irq_sim_domain
))
448 return PTR_ERR(chip
->irq_sim_domain
);
450 rv
= devm_add_action_or_reset(dev
, gpio_mockup_dispose_mappings
, chip
);
454 rv
= devm_gpiochip_add_data(dev
, &chip
->gc
, chip
);
458 gpio_mockup_debugfs_setup(dev
, chip
);
463 static struct platform_driver gpio_mockup_driver
= {
465 .name
= "gpio-mockup",
467 .probe
= gpio_mockup_probe
,
470 static struct platform_device
*gpio_mockup_pdevs
[GPIO_MOCKUP_MAX_GC
];
472 static void gpio_mockup_unregister_pdevs(void)
474 struct platform_device
*pdev
;
477 for (i
= 0; i
< GPIO_MOCKUP_MAX_GC
; i
++) {
478 pdev
= gpio_mockup_pdevs
[i
];
481 platform_device_unregister(pdev
);
485 static __init
char **gpio_mockup_make_line_names(const char *label
,
486 unsigned int num_lines
)
491 names
= kcalloc(num_lines
+ 1, sizeof(char *), GFP_KERNEL
);
495 for (i
= 0; i
< num_lines
; i
++) {
496 names
[i
] = kasprintf(GFP_KERNEL
, "%s-%u", label
, i
);
498 kfree_strarray(names
, i
);
506 static int __init
gpio_mockup_register_chip(int idx
)
508 struct property_entry properties
[GPIO_MOCKUP_MAX_PROP
];
509 struct platform_device_info pdevinfo
;
510 struct platform_device
*pdev
;
511 char **line_names
= NULL
;
516 memset(properties
, 0, sizeof(properties
));
517 memset(&pdevinfo
, 0, sizeof(pdevinfo
));
519 snprintf(chip_label
, sizeof(chip_label
), "gpio-mockup-%c", idx
+ 'A');
520 properties
[prop
++] = PROPERTY_ENTRY_STRING("chip-label", chip_label
);
522 base
= gpio_mockup_range_base(idx
);
524 properties
[prop
++] = PROPERTY_ENTRY_U32("gpio-base", base
);
526 ngpio
= base
< 0 ? gpio_mockup_range_ngpio(idx
)
527 : gpio_mockup_range_ngpio(idx
) - base
;
528 properties
[prop
++] = PROPERTY_ENTRY_U16("nr-gpios", ngpio
);
530 if (gpio_mockup_named_lines
) {
531 line_names
= gpio_mockup_make_line_names(chip_label
, ngpio
);
535 properties
[prop
++] = PROPERTY_ENTRY_STRING_ARRAY_LEN(
536 "gpio-line-names", line_names
, ngpio
);
539 pdevinfo
.name
= "gpio-mockup";
541 pdevinfo
.properties
= properties
;
543 pdev
= platform_device_register_full(&pdevinfo
);
544 kfree_strarray(line_names
, ngpio
);
546 pr_err("error registering device");
547 return PTR_ERR(pdev
);
550 gpio_mockup_pdevs
[idx
] = pdev
;
555 static int __init
gpio_mockup_init(void)
557 int i
, num_chips
, err
;
559 if ((gpio_mockup_num_ranges
< 2) ||
560 (gpio_mockup_num_ranges
% 2) ||
561 (gpio_mockup_num_ranges
> GPIO_MOCKUP_MAX_RANGES
))
564 /* Each chip is described by two values. */
565 num_chips
= gpio_mockup_num_ranges
/ 2;
568 * The second value in the <base GPIO - number of GPIOS> pair must
569 * always be greater than 0.
571 for (i
= 0; i
< num_chips
; i
++) {
572 if (gpio_mockup_range_ngpio(i
) < 0)
576 gpio_mockup_dbg_dir
= debugfs_create_dir("gpio-mockup", NULL
);
578 err
= platform_driver_register(&gpio_mockup_driver
);
580 pr_err("error registering platform driver\n");
581 debugfs_remove_recursive(gpio_mockup_dbg_dir
);
585 for (i
= 0; i
< num_chips
; i
++) {
586 err
= gpio_mockup_register_chip(i
);
588 platform_driver_unregister(&gpio_mockup_driver
);
589 gpio_mockup_unregister_pdevs();
590 debugfs_remove_recursive(gpio_mockup_dbg_dir
);
598 static void __exit
gpio_mockup_exit(void)
600 debugfs_remove_recursive(gpio_mockup_dbg_dir
);
601 platform_driver_unregister(&gpio_mockup_driver
);
602 gpio_mockup_unregister_pdevs();
605 module_init(gpio_mockup_init
);
606 module_exit(gpio_mockup_exit
);
608 MODULE_AUTHOR("Kamlakant Patel <kamlakant.patel@broadcom.com>");
609 MODULE_AUTHOR("Bamvor Jian Zhang <bamv2005@gmail.com>");
610 MODULE_AUTHOR("Bartosz Golaszewski <brgl@bgdev.pl>");
611 MODULE_DESCRIPTION("GPIO Testing driver");
612 MODULE_LICENSE("GPL v2");