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/cleanup.h>
13 #include <linux/debugfs.h>
14 #include <linux/device.h>
15 #include <linux/gpio/driver.h>
16 #include <linux/interrupt.h>
17 #include <linux/irq.h>
18 #include <linux/irq_sim.h>
19 #include <linux/irqdomain.h>
20 #include <linux/mod_devicetable.h>
21 #include <linux/module.h>
22 #include <linux/platform_device.h>
23 #include <linux/property.h>
24 #include <linux/seq_file.h>
25 #include <linux/slab.h>
26 #include <linux/string_helpers.h>
27 #include <linux/uaccess.h>
29 #define GPIO_MOCKUP_MAX_GC 10
31 * We're storing two values per chip: the GPIO base and the number
34 #define GPIO_MOCKUP_MAX_RANGES (GPIO_MOCKUP_MAX_GC * 2)
35 /* Maximum of four properties + the sentinel. */
36 #define GPIO_MOCKUP_MAX_PROP 5
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)
42 * @pull: Configures the current pull of the GPIO as 0 (pull-down) or
44 * @requested: Request status of this GPIO
46 struct gpio_mockup_line_status
{
53 struct gpio_mockup_chip
{
55 struct gpio_mockup_line_status
*lines
;
56 struct irq_domain
*irq_sim_domain
;
57 struct dentry
*dbg_dir
;
61 struct gpio_mockup_dbgfs_private
{
62 struct gpio_mockup_chip
*chip
;
66 static int gpio_mockup_ranges
[GPIO_MOCKUP_MAX_RANGES
];
67 static int gpio_mockup_num_ranges
;
68 module_param_array(gpio_mockup_ranges
, int, &gpio_mockup_num_ranges
, 0400);
70 static bool gpio_mockup_named_lines
;
71 module_param_named(gpio_mockup_named_lines
,
72 gpio_mockup_named_lines
, bool, 0400);
74 static struct dentry
*gpio_mockup_dbg_dir
;
76 static int gpio_mockup_range_base(unsigned int index
)
78 return gpio_mockup_ranges
[index
* 2];
81 static int gpio_mockup_range_ngpio(unsigned int index
)
83 return gpio_mockup_ranges
[index
* 2 + 1];
86 static int __gpio_mockup_get(struct gpio_mockup_chip
*chip
,
89 return chip
->lines
[offset
].value
;
92 static int gpio_mockup_get(struct gpio_chip
*gc
, unsigned int offset
)
94 struct gpio_mockup_chip
*chip
= gpiochip_get_data(gc
);
97 scoped_guard(mutex
, &chip
->lock
)
98 val
= __gpio_mockup_get(chip
, offset
);
103 static int gpio_mockup_get_multiple(struct gpio_chip
*gc
,
104 unsigned long *mask
, unsigned long *bits
)
106 struct gpio_mockup_chip
*chip
= gpiochip_get_data(gc
);
107 unsigned int bit
, val
;
109 scoped_guard(mutex
, &chip
->lock
) {
110 for_each_set_bit(bit
, mask
, gc
->ngpio
) {
111 val
= __gpio_mockup_get(chip
, bit
);
112 __assign_bit(bit
, bits
, val
);
119 static void __gpio_mockup_set(struct gpio_mockup_chip
*chip
,
120 unsigned int offset
, int value
)
122 chip
->lines
[offset
].value
= !!value
;
125 static void gpio_mockup_set(struct gpio_chip
*gc
,
126 unsigned int offset
, int value
)
128 struct gpio_mockup_chip
*chip
= gpiochip_get_data(gc
);
130 guard(mutex
)(&chip
->lock
);
132 __gpio_mockup_set(chip
, offset
, value
);
135 static void gpio_mockup_set_multiple(struct gpio_chip
*gc
,
136 unsigned long *mask
, unsigned long *bits
)
138 struct gpio_mockup_chip
*chip
= gpiochip_get_data(gc
);
141 guard(mutex
)(&chip
->lock
);
143 for_each_set_bit(bit
, mask
, gc
->ngpio
)
144 __gpio_mockup_set(chip
, bit
, test_bit(bit
, bits
));
147 static int gpio_mockup_apply_pull(struct gpio_mockup_chip
*chip
,
148 unsigned int offset
, int value
)
150 struct gpio_mockup_line_status
*line
= &chip
->lines
[offset
];
151 int curr
, irq
, irq_type
, ret
= 0;
153 guard(mutex
)(&chip
->lock
);
155 if (line
->requested
&& line
->dir
== GPIO_LINE_DIRECTION_IN
) {
156 curr
= __gpio_mockup_get(chip
, offset
);
160 irq
= irq_find_mapping(chip
->irq_sim_domain
, offset
);
163 * This is fine - it just means, nobody is listening
164 * for interrupts on this line, otherwise
165 * irq_create_mapping() would have been called from
166 * the to_irq() callback.
170 irq_type
= irq_get_trigger_type(irq
);
172 if ((value
== 1 && (irq_type
& IRQ_TYPE_EDGE_RISING
)) ||
173 (value
== 0 && (irq_type
& IRQ_TYPE_EDGE_FALLING
))) {
174 ret
= irq_set_irqchip_state(irq
, IRQCHIP_STATE_PENDING
,
182 /* Change the value unless we're actively driving the line. */
183 if (!line
->requested
|| line
->dir
== GPIO_LINE_DIRECTION_IN
)
184 __gpio_mockup_set(chip
, offset
, value
);
187 chip
->lines
[offset
].pull
= value
;
191 static int gpio_mockup_set_config(struct gpio_chip
*gc
,
192 unsigned int offset
, unsigned long config
)
194 struct gpio_mockup_chip
*chip
= gpiochip_get_data(gc
);
196 switch (pinconf_to_config_param(config
)) {
197 case PIN_CONFIG_BIAS_PULL_UP
:
198 return gpio_mockup_apply_pull(chip
, offset
, 1);
199 case PIN_CONFIG_BIAS_PULL_DOWN
:
200 return gpio_mockup_apply_pull(chip
, offset
, 0);
207 static int gpio_mockup_dirout(struct gpio_chip
*gc
,
208 unsigned int offset
, int value
)
210 struct gpio_mockup_chip
*chip
= gpiochip_get_data(gc
);
212 scoped_guard(mutex
, &chip
->lock
) {
213 chip
->lines
[offset
].dir
= GPIO_LINE_DIRECTION_OUT
;
214 __gpio_mockup_set(chip
, offset
, value
);
220 static int gpio_mockup_dirin(struct gpio_chip
*gc
, unsigned int offset
)
222 struct gpio_mockup_chip
*chip
= gpiochip_get_data(gc
);
224 scoped_guard(mutex
, &chip
->lock
)
225 chip
->lines
[offset
].dir
= GPIO_LINE_DIRECTION_IN
;
230 static int gpio_mockup_get_direction(struct gpio_chip
*gc
, unsigned int offset
)
232 struct gpio_mockup_chip
*chip
= gpiochip_get_data(gc
);
235 scoped_guard(mutex
, &chip
->lock
)
236 direction
= chip
->lines
[offset
].dir
;
241 static int gpio_mockup_to_irq(struct gpio_chip
*gc
, unsigned int offset
)
243 struct gpio_mockup_chip
*chip
= gpiochip_get_data(gc
);
245 return irq_create_mapping(chip
->irq_sim_domain
, offset
);
248 static int gpio_mockup_request(struct gpio_chip
*gc
, unsigned int offset
)
250 struct gpio_mockup_chip
*chip
= gpiochip_get_data(gc
);
252 scoped_guard(mutex
, &chip
->lock
)
253 chip
->lines
[offset
].requested
= true;
258 static void gpio_mockup_free(struct gpio_chip
*gc
, unsigned int offset
)
260 struct gpio_mockup_chip
*chip
= gpiochip_get_data(gc
);
262 guard(mutex
)(&chip
->lock
);
264 chip
->lines
[offset
].requested
= false;
265 __gpio_mockup_set(chip
, offset
, chip
->lines
[offset
].pull
);
268 static ssize_t
gpio_mockup_debugfs_read(struct file
*file
,
269 char __user
*usr_buf
,
270 size_t size
, loff_t
*ppos
)
272 struct gpio_mockup_dbgfs_private
*priv
;
273 struct gpio_mockup_chip
*chip
;
274 struct seq_file
*sfile
;
275 struct gpio_chip
*gc
;
282 sfile
= file
->private_data
;
283 priv
= sfile
->private;
287 val
= gpio_mockup_get(gc
, priv
->offset
);
288 cnt
= snprintf(buf
, sizeof(buf
), "%d\n", val
);
290 return simple_read_from_buffer(usr_buf
, size
, ppos
, buf
, cnt
);
293 static ssize_t
gpio_mockup_debugfs_write(struct file
*file
,
294 const char __user
*usr_buf
,
295 size_t size
, loff_t
*ppos
)
297 struct gpio_mockup_dbgfs_private
*priv
;
299 struct seq_file
*sfile
;
304 rv
= kstrtoint_from_user(usr_buf
, size
, 0, &val
);
307 if (val
!= 0 && val
!= 1)
310 sfile
= file
->private_data
;
311 priv
= sfile
->private;
312 rv
= gpio_mockup_apply_pull(priv
->chip
, priv
->offset
, val
);
319 static int gpio_mockup_debugfs_open(struct inode
*inode
, struct file
*file
)
321 return single_open(file
, NULL
, inode
->i_private
);
325 * Each mockup chip is represented by a directory named after the chip's device
326 * name under /sys/kernel/debug/gpio-mockup/. Each line is represented by
327 * a file using the line's offset as the name under the chip's directory.
329 * Reading from the line's file yields the current *value*, writing to the
330 * line's file changes the current *pull*. Default pull for mockup lines is
334 * - when a line pulled down is requested in output mode and driven high, its
335 * value will return to 0 once it's released
336 * - when the line is requested in output mode and driven high, writing 0 to
337 * the corresponding debugfs file will change the pull to down but the
338 * reported value will still be 1 until the line is released
339 * - line requested in input mode always reports the same value as its pull
341 * - when the line is requested in input mode and monitored for events, writing
342 * the same value to the debugfs file will be a noop, while writing the
343 * opposite value will generate a dummy interrupt with an appropriate edge
345 static const struct file_operations gpio_mockup_debugfs_ops
= {
346 .owner
= THIS_MODULE
,
347 .open
= gpio_mockup_debugfs_open
,
348 .read
= gpio_mockup_debugfs_read
,
349 .write
= gpio_mockup_debugfs_write
,
350 .release
= single_release
,
353 static void gpio_mockup_debugfs_setup(struct device
*dev
,
354 struct gpio_mockup_chip
*chip
)
356 struct gpio_mockup_dbgfs_private
*priv
;
357 struct gpio_chip
*gc
;
365 * There can only be a single GPIO device per platform device in
366 * gpio-mockup so using device_find_any_child() is OK.
368 struct device
*child
__free(put_device
) = device_find_any_child(dev
);
372 devname
= dev_name(child
);
373 chip
->dbg_dir
= debugfs_create_dir(devname
, gpio_mockup_dbg_dir
);
375 for (i
= 0; i
< gc
->ngpio
; i
++) {
376 name
= devm_kasprintf(dev
, GFP_KERNEL
, "%d", i
);
380 priv
= devm_kzalloc(dev
, sizeof(*priv
), GFP_KERNEL
);
387 debugfs_create_file(name
, 0600, chip
->dbg_dir
, priv
,
388 &gpio_mockup_debugfs_ops
);
392 static void gpio_mockup_debugfs_cleanup(void *data
)
394 struct gpio_mockup_chip
*chip
= data
;
396 debugfs_remove_recursive(chip
->dbg_dir
);
399 static void gpio_mockup_dispose_mappings(void *data
)
401 struct gpio_mockup_chip
*chip
= data
;
402 struct gpio_chip
*gc
= &chip
->gc
;
405 for (i
= 0; i
< gc
->ngpio
; i
++) {
406 irq
= irq_find_mapping(chip
->irq_sim_domain
, i
);
408 irq_dispose_mapping(irq
);
412 static int gpio_mockup_probe(struct platform_device
*pdev
)
414 struct gpio_mockup_chip
*chip
;
415 struct gpio_chip
*gc
;
423 rv
= device_property_read_u32(dev
, "gpio-base", &base
);
427 rv
= device_property_read_u16(dev
, "nr-gpios", &ngpio
);
431 rv
= device_property_read_string(dev
, "chip-label", &name
);
433 name
= dev_name(dev
);
435 chip
= devm_kzalloc(dev
, sizeof(*chip
), GFP_KERNEL
);
439 mutex_init(&chip
->lock
);
445 gc
->owner
= THIS_MODULE
;
447 gc
->get
= gpio_mockup_get
;
448 gc
->set
= gpio_mockup_set
;
449 gc
->get_multiple
= gpio_mockup_get_multiple
;
450 gc
->set_multiple
= gpio_mockup_set_multiple
;
451 gc
->direction_output
= gpio_mockup_dirout
;
452 gc
->direction_input
= gpio_mockup_dirin
;
453 gc
->get_direction
= gpio_mockup_get_direction
;
454 gc
->set_config
= gpio_mockup_set_config
;
455 gc
->to_irq
= gpio_mockup_to_irq
;
456 gc
->request
= gpio_mockup_request
;
457 gc
->free
= gpio_mockup_free
;
459 chip
->lines
= devm_kcalloc(dev
, gc
->ngpio
,
460 sizeof(*chip
->lines
), GFP_KERNEL
);
464 for (i
= 0; i
< gc
->ngpio
; i
++)
465 chip
->lines
[i
].dir
= GPIO_LINE_DIRECTION_IN
;
467 chip
->irq_sim_domain
= devm_irq_domain_create_sim(dev
, NULL
,
469 if (IS_ERR(chip
->irq_sim_domain
))
470 return PTR_ERR(chip
->irq_sim_domain
);
472 rv
= devm_add_action_or_reset(dev
, gpio_mockup_dispose_mappings
, chip
);
476 rv
= devm_gpiochip_add_data(dev
, &chip
->gc
, chip
);
480 gpio_mockup_debugfs_setup(dev
, chip
);
482 return devm_add_action_or_reset(dev
, gpio_mockup_debugfs_cleanup
, chip
);
485 static const struct of_device_id gpio_mockup_of_match
[] = {
486 { .compatible
= "gpio-mockup", },
489 MODULE_DEVICE_TABLE(of
, gpio_mockup_of_match
);
491 static struct platform_driver gpio_mockup_driver
= {
493 .name
= "gpio-mockup",
494 .of_match_table
= gpio_mockup_of_match
,
496 .probe
= gpio_mockup_probe
,
499 static struct platform_device
*gpio_mockup_pdevs
[GPIO_MOCKUP_MAX_GC
];
501 static void gpio_mockup_unregister_pdevs(void)
503 struct platform_device
*pdev
;
504 struct fwnode_handle
*fwnode
;
507 for (i
= 0; i
< GPIO_MOCKUP_MAX_GC
; i
++) {
508 pdev
= gpio_mockup_pdevs
[i
];
512 fwnode
= dev_fwnode(&pdev
->dev
);
513 platform_device_unregister(pdev
);
514 fwnode_remove_software_node(fwnode
);
518 static int __init
gpio_mockup_register_chip(int idx
)
520 struct property_entry properties
[GPIO_MOCKUP_MAX_PROP
];
521 struct platform_device_info pdevinfo
;
522 struct platform_device
*pdev
;
523 struct fwnode_handle
*fwnode
;
524 char **line_names
= NULL
;
529 memset(properties
, 0, sizeof(properties
));
530 memset(&pdevinfo
, 0, sizeof(pdevinfo
));
532 snprintf(chip_label
, sizeof(chip_label
), "gpio-mockup-%c", idx
+ 'A');
533 properties
[prop
++] = PROPERTY_ENTRY_STRING("chip-label", chip_label
);
535 base
= gpio_mockup_range_base(idx
);
537 properties
[prop
++] = PROPERTY_ENTRY_U32("gpio-base", base
);
539 ngpio
= base
< 0 ? gpio_mockup_range_ngpio(idx
)
540 : gpio_mockup_range_ngpio(idx
) - base
;
541 properties
[prop
++] = PROPERTY_ENTRY_U16("nr-gpios", ngpio
);
543 if (gpio_mockup_named_lines
) {
544 line_names
= kasprintf_strarray(GFP_KERNEL
, chip_label
, ngpio
);
548 properties
[prop
++] = PROPERTY_ENTRY_STRING_ARRAY_LEN(
549 "gpio-line-names", line_names
, ngpio
);
552 fwnode
= fwnode_create_software_node(properties
, NULL
);
553 if (IS_ERR(fwnode
)) {
554 kfree_strarray(line_names
, ngpio
);
555 return PTR_ERR(fwnode
);
558 pdevinfo
.name
= "gpio-mockup";
560 pdevinfo
.fwnode
= fwnode
;
562 pdev
= platform_device_register_full(&pdevinfo
);
563 kfree_strarray(line_names
, ngpio
);
565 fwnode_remove_software_node(fwnode
);
566 pr_err("error registering device");
567 return PTR_ERR(pdev
);
570 gpio_mockup_pdevs
[idx
] = pdev
;
575 static int __init
gpio_mockup_init(void)
577 int i
, num_chips
, err
;
579 if ((gpio_mockup_num_ranges
% 2) ||
580 (gpio_mockup_num_ranges
> GPIO_MOCKUP_MAX_RANGES
))
583 /* Each chip is described by two values. */
584 num_chips
= gpio_mockup_num_ranges
/ 2;
587 * The second value in the <base GPIO - number of GPIOS> pair must
588 * always be greater than 0.
590 for (i
= 0; i
< num_chips
; i
++) {
591 if (gpio_mockup_range_ngpio(i
) < 0)
595 gpio_mockup_dbg_dir
= debugfs_create_dir("gpio-mockup", NULL
);
597 err
= platform_driver_register(&gpio_mockup_driver
);
599 pr_err("error registering platform driver\n");
600 debugfs_remove_recursive(gpio_mockup_dbg_dir
);
604 for (i
= 0; i
< num_chips
; i
++) {
605 err
= gpio_mockup_register_chip(i
);
607 platform_driver_unregister(&gpio_mockup_driver
);
608 gpio_mockup_unregister_pdevs();
609 debugfs_remove_recursive(gpio_mockup_dbg_dir
);
617 static void __exit
gpio_mockup_exit(void)
619 gpio_mockup_unregister_pdevs();
620 debugfs_remove_recursive(gpio_mockup_dbg_dir
);
621 platform_driver_unregister(&gpio_mockup_driver
);
624 module_init(gpio_mockup_init
);
625 module_exit(gpio_mockup_exit
);
627 MODULE_AUTHOR("Kamlakant Patel <kamlakant.patel@broadcom.com>");
628 MODULE_AUTHOR("Bamvor Jian Zhang <bamv2005@gmail.com>");
629 MODULE_AUTHOR("Bartosz Golaszewski <brgl@bgdev.pl>");
630 MODULE_DESCRIPTION("GPIO Testing driver");
631 MODULE_LICENSE("GPL v2");