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/module.h>
18 #include <linux/platform_device.h>
19 #include <linux/property.h>
20 #include <linux/slab.h>
21 #include <linux/uaccess.h>
25 #define GPIO_MOCKUP_NAME "gpio-mockup"
26 #define GPIO_MOCKUP_MAX_GC 10
28 * We're storing two values per chip: the GPIO base and the number
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
35 #define gpio_mockup_err(...) pr_err(GPIO_MOCKUP_NAME ": " __VA_ARGS__)
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_sim irqsim
;
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_desc
*desc
;
148 struct gpio_chip
*gc
;
150 int curr
, irq
, irq_type
;
153 desc
= &gc
->gpiodev
->descs
[offset
];
156 mutex_lock(&chip
->lock
);
158 if (test_bit(FLAG_REQUESTED
, &desc
->flags
) &&
159 !test_bit(FLAG_IS_OUT
, &desc
->flags
)) {
160 curr
= __gpio_mockup_get(chip
, offset
);
164 irq
= irq_sim_irqnum(sim
, offset
);
165 irq_type
= irq_get_trigger_type(irq
);
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
);
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
);
178 chip
->lines
[offset
].pull
= value
;
179 mutex_unlock(&chip
->lock
);
183 static int gpio_mockup_set_config(struct gpio_chip
*gc
,
184 unsigned int offset
, unsigned long config
)
186 struct gpio_mockup_chip
*chip
= gpiochip_get_data(gc
);
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);
199 static int gpio_mockup_dirout(struct gpio_chip
*gc
,
200 unsigned int offset
, int value
)
202 struct gpio_mockup_chip
*chip
= gpiochip_get_data(gc
);
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
);
212 static int gpio_mockup_dirin(struct gpio_chip
*gc
, unsigned int offset
)
214 struct gpio_mockup_chip
*chip
= gpiochip_get_data(gc
);
216 mutex_lock(&chip
->lock
);
217 chip
->lines
[offset
].dir
= GPIO_LINE_DIRECTION_IN
;
218 mutex_unlock(&chip
->lock
);
223 static int gpio_mockup_get_direction(struct gpio_chip
*gc
, unsigned int offset
)
225 struct gpio_mockup_chip
*chip
= gpiochip_get_data(gc
);
228 mutex_lock(&chip
->lock
);
229 direction
= chip
->lines
[offset
].dir
;
230 mutex_unlock(&chip
->lock
);
235 static int gpio_mockup_to_irq(struct gpio_chip
*gc
, unsigned int offset
)
237 struct gpio_mockup_chip
*chip
= gpiochip_get_data(gc
);
239 return irq_sim_irqnum(&chip
->irqsim
, offset
);
242 static void gpio_mockup_free(struct gpio_chip
*gc
, unsigned int offset
)
244 struct gpio_mockup_chip
*chip
= gpiochip_get_data(gc
);
246 __gpio_mockup_set(chip
, offset
, chip
->lines
[offset
].pull
);
249 static ssize_t
gpio_mockup_debugfs_read(struct file
*file
,
250 char __user
*usr_buf
,
251 size_t size
, loff_t
*ppos
)
253 struct gpio_mockup_dbgfs_private
*priv
;
254 struct gpio_mockup_chip
*chip
;
255 struct seq_file
*sfile
;
256 struct gpio_chip
*gc
;
263 sfile
= file
->private_data
;
264 priv
= sfile
->private;
268 val
= gpio_mockup_get(gc
, priv
->offset
);
269 cnt
= snprintf(buf
, sizeof(buf
), "%d\n", val
);
271 return simple_read_from_buffer(usr_buf
, size
, ppos
, buf
, cnt
);
274 static ssize_t
gpio_mockup_debugfs_write(struct file
*file
,
275 const char __user
*usr_buf
,
276 size_t size
, loff_t
*ppos
)
278 struct gpio_mockup_dbgfs_private
*priv
;
280 struct seq_file
*sfile
;
285 rv
= kstrtoint_from_user(usr_buf
, size
, 0, &val
);
288 if (val
!= 0 && val
!= 1)
291 sfile
= file
->private_data
;
292 priv
= sfile
->private;
293 rv
= gpio_mockup_apply_pull(priv
->chip
, priv
->offset
, val
);
300 static int gpio_mockup_debugfs_open(struct inode
*inode
, struct file
*file
)
302 return single_open(file
, NULL
, inode
->i_private
);
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.
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
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
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
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
,
332 .release
= single_release
,
335 static void gpio_mockup_debugfs_setup(struct device
*dev
,
336 struct gpio_mockup_chip
*chip
)
338 struct gpio_mockup_dbgfs_private
*priv
;
339 struct gpio_chip
*gc
;
345 devname
= dev_name(&gc
->gpiodev
->dev
);
347 chip
->dbg_dir
= debugfs_create_dir(devname
, gpio_mockup_dbg_dir
);
349 for (i
= 0; i
< gc
->ngpio
; i
++) {
350 name
= devm_kasprintf(dev
, GFP_KERNEL
, "%d", i
);
354 priv
= devm_kzalloc(dev
, sizeof(*priv
), GFP_KERNEL
);
360 priv
->desc
= &gc
->gpiodev
->descs
[i
];
362 debugfs_create_file(name
, 0200, chip
->dbg_dir
, priv
,
363 &gpio_mockup_debugfs_ops
);
369 static int gpio_mockup_name_lines(struct device
*dev
,
370 struct gpio_mockup_chip
*chip
)
372 struct gpio_chip
*gc
= &chip
->gc
;
376 names
= devm_kcalloc(dev
, gc
->ngpio
, sizeof(char *), GFP_KERNEL
);
380 for (i
= 0; i
< gc
->ngpio
; i
++) {
381 names
[i
] = devm_kasprintf(dev
, GFP_KERNEL
,
382 "%s-%d", gc
->label
, i
);
387 gc
->names
= (const char *const *)names
;
392 static int gpio_mockup_probe(struct platform_device
*pdev
)
394 struct gpio_mockup_chip
*chip
;
395 struct gpio_chip
*gc
;
403 rv
= device_property_read_u32(dev
, "gpio-base", &base
);
407 rv
= device_property_read_u16(dev
, "nr-gpios", &ngpio
);
411 rv
= device_property_read_string(dev
, "chip-name", &name
);
415 chip
= devm_kzalloc(dev
, sizeof(*chip
), GFP_KERNEL
);
420 name
= devm_kasprintf(dev
, GFP_KERNEL
,
421 "%s-%c", pdev
->name
, pdev
->id
+ 'A');
426 mutex_init(&chip
->lock
);
432 gc
->owner
= THIS_MODULE
;
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
;
445 chip
->lines
= devm_kcalloc(dev
, gc
->ngpio
,
446 sizeof(*chip
->lines
), GFP_KERNEL
);
450 for (i
= 0; i
< gc
->ngpio
; i
++)
451 chip
->lines
[i
].dir
= GPIO_LINE_DIRECTION_IN
;
453 if (device_property_read_bool(dev
, "named-gpio-lines")) {
454 rv
= gpio_mockup_name_lines(dev
, chip
);
459 rv
= devm_irq_sim_init(dev
, &chip
->irqsim
, gc
->ngpio
);
463 rv
= devm_gpiochip_add_data(dev
, &chip
->gc
, chip
);
467 gpio_mockup_debugfs_setup(dev
, chip
);
472 static struct platform_driver gpio_mockup_driver
= {
474 .name
= GPIO_MOCKUP_NAME
,
476 .probe
= gpio_mockup_probe
,
479 static struct platform_device
*gpio_mockup_pdevs
[GPIO_MOCKUP_MAX_GC
];
481 static void gpio_mockup_unregister_pdevs(void)
483 struct platform_device
*pdev
;
486 for (i
= 0; i
< GPIO_MOCKUP_MAX_GC
; i
++) {
487 pdev
= gpio_mockup_pdevs
[i
];
490 platform_device_unregister(pdev
);
494 static int __init
gpio_mockup_init(void)
496 struct property_entry properties
[GPIO_MOCKUP_MAX_PROP
];
497 int i
, prop
, num_chips
, err
= 0, base
;
498 struct platform_device_info pdevinfo
;
499 struct platform_device
*pdev
;
502 if ((gpio_mockup_num_ranges
< 2) ||
503 (gpio_mockup_num_ranges
% 2) ||
504 (gpio_mockup_num_ranges
> GPIO_MOCKUP_MAX_RANGES
))
507 /* Each chip is described by two values. */
508 num_chips
= gpio_mockup_num_ranges
/ 2;
511 * The second value in the <base GPIO - number of GPIOS> pair must
512 * always be greater than 0.
514 for (i
= 0; i
< num_chips
; i
++) {
515 if (gpio_mockup_range_ngpio(i
) < 0)
519 gpio_mockup_dbg_dir
= debugfs_create_dir("gpio-mockup", NULL
);
521 err
= platform_driver_register(&gpio_mockup_driver
);
523 gpio_mockup_err("error registering platform driver\n");
527 for (i
= 0; i
< num_chips
; i
++) {
528 memset(properties
, 0, sizeof(properties
));
529 memset(&pdevinfo
, 0, sizeof(pdevinfo
));
532 base
= gpio_mockup_range_base(i
);
534 properties
[prop
++] = PROPERTY_ENTRY_U32("gpio-base",
537 ngpio
= base
< 0 ? gpio_mockup_range_ngpio(i
)
538 : gpio_mockup_range_ngpio(i
) - base
;
539 properties
[prop
++] = PROPERTY_ENTRY_U16("nr-gpios", ngpio
);
541 if (gpio_mockup_named_lines
)
542 properties
[prop
++] = PROPERTY_ENTRY_BOOL(
545 pdevinfo
.name
= GPIO_MOCKUP_NAME
;
547 pdevinfo
.properties
= properties
;
549 pdev
= platform_device_register_full(&pdevinfo
);
551 gpio_mockup_err("error registering device");
552 platform_driver_unregister(&gpio_mockup_driver
);
553 gpio_mockup_unregister_pdevs();
554 return PTR_ERR(pdev
);
557 gpio_mockup_pdevs
[i
] = pdev
;
563 static void __exit
gpio_mockup_exit(void)
565 debugfs_remove_recursive(gpio_mockup_dbg_dir
);
566 platform_driver_unregister(&gpio_mockup_driver
);
567 gpio_mockup_unregister_pdevs();
570 module_init(gpio_mockup_init
);
571 module_exit(gpio_mockup_exit
);
573 MODULE_AUTHOR("Kamlakant Patel <kamlakant.patel@broadcom.com>");
574 MODULE_AUTHOR("Bamvor Jian Zhang <bamv2005@gmail.com>");
575 MODULE_AUTHOR("Bartosz Golaszewski <brgl@bgdev.pl>");
576 MODULE_DESCRIPTION("GPIO Testing driver");
577 MODULE_LICENSE("GPL v2");