1 // SPDX-License-Identifier: GPL-2.0+
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/init.h>
11 #include <linux/module.h>
12 #include <linux/gpio/driver.h>
13 #include <linux/gpio/consumer.h>
14 #include <linux/platform_device.h>
15 #include <linux/slab.h>
16 #include <linux/interrupt.h>
17 #include <linux/irq.h>
18 #include <linux/irq_sim.h>
19 #include <linux/debugfs.h>
20 #include <linux/uaccess.h>
21 #include <linux/property.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 GPIO_MOCKUP_DIR_IN
= 0,
39 GPIO_MOCKUP_DIR_OUT
= 1,
43 * struct gpio_pin_status - structure describing a GPIO status
44 * @dir: Configures direction of gpio as "in" or "out", 0=in, 1=out
45 * @value: Configures status of the gpio as 0(low) or 1(high)
47 struct gpio_mockup_line_status
{
53 struct gpio_mockup_chip
{
55 struct gpio_mockup_line_status
*lines
;
56 struct irq_sim irqsim
;
57 struct dentry
*dbg_dir
;
61 struct gpio_mockup_dbgfs_private
{
62 struct gpio_mockup_chip
*chip
;
63 struct gpio_desc
*desc
;
67 static int gpio_mockup_ranges
[GPIO_MOCKUP_MAX_RANGES
];
68 static int gpio_mockup_num_ranges
;
69 module_param_array(gpio_mockup_ranges
, int, &gpio_mockup_num_ranges
, 0400);
71 static bool gpio_mockup_named_lines
;
72 module_param_named(gpio_mockup_named_lines
,
73 gpio_mockup_named_lines
, bool, 0400);
75 static struct dentry
*gpio_mockup_dbg_dir
;
77 static int gpio_mockup_range_base(unsigned int index
)
79 return gpio_mockup_ranges
[index
* 2];
82 static int gpio_mockup_range_ngpio(unsigned int index
)
84 return gpio_mockup_ranges
[index
* 2 + 1];
87 static int __gpio_mockup_get(struct gpio_mockup_chip
*chip
,
90 return chip
->lines
[offset
].value
;
93 static int gpio_mockup_get(struct gpio_chip
*gc
, unsigned int offset
)
95 struct gpio_mockup_chip
*chip
= gpiochip_get_data(gc
);
98 mutex_lock(&chip
->lock
);
99 val
= __gpio_mockup_get(chip
, offset
);
100 mutex_unlock(&chip
->lock
);
105 static int gpio_mockup_get_multiple(struct gpio_chip
*gc
,
106 unsigned long *mask
, unsigned long *bits
)
108 struct gpio_mockup_chip
*chip
= gpiochip_get_data(gc
);
109 unsigned int bit
, val
;
111 mutex_lock(&chip
->lock
);
112 for_each_set_bit(bit
, mask
, gc
->ngpio
) {
113 val
= __gpio_mockup_get(chip
, bit
);
114 __assign_bit(bit
, bits
, val
);
116 mutex_unlock(&chip
->lock
);
121 static void __gpio_mockup_set(struct gpio_mockup_chip
*chip
,
122 unsigned int offset
, int value
)
124 chip
->lines
[offset
].value
= !!value
;
127 static void gpio_mockup_set(struct gpio_chip
*gc
,
128 unsigned int offset
, int value
)
130 struct gpio_mockup_chip
*chip
= gpiochip_get_data(gc
);
132 mutex_lock(&chip
->lock
);
133 __gpio_mockup_set(chip
, offset
, value
);
134 mutex_unlock(&chip
->lock
);
137 static void gpio_mockup_set_multiple(struct gpio_chip
*gc
,
138 unsigned long *mask
, unsigned long *bits
)
140 struct gpio_mockup_chip
*chip
= gpiochip_get_data(gc
);
143 mutex_lock(&chip
->lock
);
144 for_each_set_bit(bit
, mask
, gc
->ngpio
)
145 __gpio_mockup_set(chip
, bit
, test_bit(bit
, bits
));
146 mutex_unlock(&chip
->lock
);
149 static int gpio_mockup_dirout(struct gpio_chip
*gc
,
150 unsigned int offset
, int value
)
152 struct gpio_mockup_chip
*chip
= gpiochip_get_data(gc
);
154 mutex_lock(&chip
->lock
);
155 chip
->lines
[offset
].dir
= GPIO_MOCKUP_DIR_OUT
;
156 __gpio_mockup_set(chip
, offset
, value
);
157 mutex_unlock(&chip
->lock
);
162 static int gpio_mockup_dirin(struct gpio_chip
*gc
, unsigned int offset
)
164 struct gpio_mockup_chip
*chip
= gpiochip_get_data(gc
);
166 mutex_lock(&chip
->lock
);
167 chip
->lines
[offset
].dir
= GPIO_MOCKUP_DIR_IN
;
168 mutex_unlock(&chip
->lock
);
173 static int gpio_mockup_get_direction(struct gpio_chip
*gc
, unsigned int offset
)
175 struct gpio_mockup_chip
*chip
= gpiochip_get_data(gc
);
178 mutex_lock(&chip
->lock
);
179 direction
= !chip
->lines
[offset
].dir
;
180 mutex_unlock(&chip
->lock
);
185 static int gpio_mockup_to_irq(struct gpio_chip
*gc
, unsigned int offset
)
187 struct gpio_mockup_chip
*chip
= gpiochip_get_data(gc
);
189 return irq_sim_irqnum(&chip
->irqsim
, offset
);
192 static void gpio_mockup_free(struct gpio_chip
*gc
, unsigned int offset
)
194 struct gpio_mockup_chip
*chip
= gpiochip_get_data(gc
);
196 __gpio_mockup_set(chip
, offset
, chip
->lines
[offset
].pull
);
199 static ssize_t
gpio_mockup_debugfs_read(struct file
*file
,
200 char __user
*usr_buf
,
201 size_t size
, loff_t
*ppos
)
203 struct gpio_mockup_dbgfs_private
*priv
;
204 struct gpio_mockup_chip
*chip
;
205 struct seq_file
*sfile
;
206 struct gpio_chip
*gc
;
213 sfile
= file
->private_data
;
214 priv
= sfile
->private;
218 val
= gpio_mockup_get(gc
, priv
->offset
);
219 cnt
= snprintf(buf
, sizeof(buf
), "%d\n", val
);
221 return simple_read_from_buffer(usr_buf
, size
, ppos
, buf
, cnt
);
224 static ssize_t
gpio_mockup_debugfs_write(struct file
*file
,
225 const char __user
*usr_buf
,
226 size_t size
, loff_t
*ppos
)
228 struct gpio_mockup_dbgfs_private
*priv
;
229 int rv
, val
, curr
, irq
, irq_type
;
230 struct gpio_mockup_chip
*chip
;
231 struct seq_file
*sfile
;
232 struct gpio_desc
*desc
;
233 struct gpio_chip
*gc
;
239 rv
= kstrtoint_from_user(usr_buf
, size
, 0, &val
);
242 if (val
!= 0 && val
!= 1)
245 sfile
= file
->private_data
;
246 priv
= sfile
->private;
249 desc
= &gc
->gpiodev
->descs
[priv
->offset
];
252 mutex_lock(&chip
->lock
);
254 if (test_bit(FLAG_REQUESTED
, &desc
->flags
) &&
255 !test_bit(FLAG_IS_OUT
, &desc
->flags
)) {
256 curr
= __gpio_mockup_get(chip
, priv
->offset
);
260 irq
= irq_sim_irqnum(sim
, priv
->offset
);
261 irq_type
= irq_get_trigger_type(irq
);
263 if ((val
== 1 && (irq_type
& IRQ_TYPE_EDGE_RISING
)) ||
264 (val
== 0 && (irq_type
& IRQ_TYPE_EDGE_FALLING
)))
265 irq_sim_fire(sim
, priv
->offset
);
268 /* Change the value unless we're actively driving the line. */
269 if (!test_bit(FLAG_REQUESTED
, &desc
->flags
) ||
270 !test_bit(FLAG_IS_OUT
, &desc
->flags
))
271 __gpio_mockup_set(chip
, priv
->offset
, val
);
274 chip
->lines
[priv
->offset
].pull
= val
;
275 mutex_unlock(&chip
->lock
);
280 static int gpio_mockup_debugfs_open(struct inode
*inode
, struct file
*file
)
282 return single_open(file
, NULL
, inode
->i_private
);
286 * Each mockup chip is represented by a directory named after the chip's device
287 * name under /sys/kernel/debug/gpio-mockup/. Each line is represented by
288 * a file using the line's offset as the name under the chip's directory.
290 * Reading from the line's file yields the current *value*, writing to the
291 * line's file changes the current *pull*. Default pull for mockup lines is
295 * - when a line pulled down is requested in output mode and driven high, its
296 * value will return to 0 once it's released
297 * - when the line is requested in output mode and driven high, writing 0 to
298 * the corresponding debugfs file will change the pull to down but the
299 * reported value will still be 1 until the line is released
300 * - line requested in input mode always reports the same value as its pull
302 * - when the line is requested in input mode and monitored for events, writing
303 * the same value to the debugfs file will be a noop, while writing the
304 * opposite value will generate a dummy interrupt with an appropriate edge
306 static const struct file_operations gpio_mockup_debugfs_ops
= {
307 .owner
= THIS_MODULE
,
308 .open
= gpio_mockup_debugfs_open
,
309 .read
= gpio_mockup_debugfs_read
,
310 .write
= gpio_mockup_debugfs_write
,
314 static void gpio_mockup_debugfs_setup(struct device
*dev
,
315 struct gpio_mockup_chip
*chip
)
317 struct gpio_mockup_dbgfs_private
*priv
;
318 struct dentry
*evfile
;
319 struct gpio_chip
*gc
;
325 devname
= dev_name(&gc
->gpiodev
->dev
);
327 chip
->dbg_dir
= debugfs_create_dir(devname
, gpio_mockup_dbg_dir
);
328 if (IS_ERR_OR_NULL(chip
->dbg_dir
))
331 for (i
= 0; i
< gc
->ngpio
; i
++) {
332 name
= devm_kasprintf(dev
, GFP_KERNEL
, "%d", i
);
336 priv
= devm_kzalloc(dev
, sizeof(*priv
), GFP_KERNEL
);
342 priv
->desc
= &gc
->gpiodev
->descs
[i
];
344 evfile
= debugfs_create_file(name
, 0200, chip
->dbg_dir
, priv
,
345 &gpio_mockup_debugfs_ops
);
346 if (IS_ERR_OR_NULL(evfile
))
353 dev_err(dev
, "error creating debugfs files\n");
356 static int gpio_mockup_name_lines(struct device
*dev
,
357 struct gpio_mockup_chip
*chip
)
359 struct gpio_chip
*gc
= &chip
->gc
;
363 names
= devm_kcalloc(dev
, gc
->ngpio
, sizeof(char *), GFP_KERNEL
);
367 for (i
= 0; i
< gc
->ngpio
; i
++) {
368 names
[i
] = devm_kasprintf(dev
, GFP_KERNEL
,
369 "%s-%d", gc
->label
, i
);
374 gc
->names
= (const char *const *)names
;
379 static int gpio_mockup_probe(struct platform_device
*pdev
)
381 struct gpio_mockup_chip
*chip
;
382 struct gpio_chip
*gc
;
390 rv
= device_property_read_u32(dev
, "gpio-base", &base
);
394 rv
= device_property_read_u16(dev
, "nr-gpios", &ngpio
);
398 rv
= device_property_read_string(dev
, "chip-name", &name
);
402 chip
= devm_kzalloc(dev
, sizeof(*chip
), GFP_KERNEL
);
407 name
= devm_kasprintf(dev
, GFP_KERNEL
,
408 "%s-%c", pdev
->name
, pdev
->id
+ 'A');
413 mutex_init(&chip
->lock
);
419 gc
->owner
= THIS_MODULE
;
421 gc
->get
= gpio_mockup_get
;
422 gc
->set
= gpio_mockup_set
;
423 gc
->get_multiple
= gpio_mockup_get_multiple
;
424 gc
->set_multiple
= gpio_mockup_set_multiple
;
425 gc
->direction_output
= gpio_mockup_dirout
;
426 gc
->direction_input
= gpio_mockup_dirin
;
427 gc
->get_direction
= gpio_mockup_get_direction
;
428 gc
->to_irq
= gpio_mockup_to_irq
;
429 gc
->free
= gpio_mockup_free
;
431 chip
->lines
= devm_kcalloc(dev
, gc
->ngpio
,
432 sizeof(*chip
->lines
), GFP_KERNEL
);
436 if (device_property_read_bool(dev
, "named-gpio-lines")) {
437 rv
= gpio_mockup_name_lines(dev
, chip
);
442 rv
= devm_irq_sim_init(dev
, &chip
->irqsim
, gc
->ngpio
);
446 rv
= devm_gpiochip_add_data(dev
, &chip
->gc
, chip
);
450 if (!IS_ERR_OR_NULL(gpio_mockup_dbg_dir
))
451 gpio_mockup_debugfs_setup(dev
, chip
);
456 static struct platform_driver gpio_mockup_driver
= {
458 .name
= GPIO_MOCKUP_NAME
,
460 .probe
= gpio_mockup_probe
,
463 static struct platform_device
*gpio_mockup_pdevs
[GPIO_MOCKUP_MAX_GC
];
465 static void gpio_mockup_unregister_pdevs(void)
467 struct platform_device
*pdev
;
470 for (i
= 0; i
< GPIO_MOCKUP_MAX_GC
; i
++) {
471 pdev
= gpio_mockup_pdevs
[i
];
474 platform_device_unregister(pdev
);
478 static int __init
gpio_mockup_init(void)
480 struct property_entry properties
[GPIO_MOCKUP_MAX_PROP
];
481 int i
, prop
, num_chips
, err
= 0, base
;
482 struct platform_device_info pdevinfo
;
483 struct platform_device
*pdev
;
486 if ((gpio_mockup_num_ranges
< 2) ||
487 (gpio_mockup_num_ranges
% 2) ||
488 (gpio_mockup_num_ranges
> GPIO_MOCKUP_MAX_RANGES
))
491 /* Each chip is described by two values. */
492 num_chips
= gpio_mockup_num_ranges
/ 2;
495 * The second value in the <base GPIO - number of GPIOS> pair must
496 * always be greater than 0.
498 for (i
= 0; i
< num_chips
; i
++) {
499 if (gpio_mockup_range_ngpio(i
) < 0)
503 gpio_mockup_dbg_dir
= debugfs_create_dir("gpio-mockup", NULL
);
504 if (IS_ERR_OR_NULL(gpio_mockup_dbg_dir
))
505 gpio_mockup_err("error creating debugfs directory\n");
507 err
= platform_driver_register(&gpio_mockup_driver
);
509 gpio_mockup_err("error registering platform driver\n");
513 for (i
= 0; i
< num_chips
; i
++) {
514 memset(properties
, 0, sizeof(properties
));
515 memset(&pdevinfo
, 0, sizeof(pdevinfo
));
518 base
= gpio_mockup_range_base(i
);
520 properties
[prop
++] = PROPERTY_ENTRY_U32("gpio-base",
523 ngpio
= base
< 0 ? gpio_mockup_range_ngpio(i
)
524 : gpio_mockup_range_ngpio(i
) - base
;
525 properties
[prop
++] = PROPERTY_ENTRY_U16("nr-gpios", ngpio
);
527 if (gpio_mockup_named_lines
)
528 properties
[prop
++] = PROPERTY_ENTRY_BOOL(
531 pdevinfo
.name
= GPIO_MOCKUP_NAME
;
533 pdevinfo
.properties
= properties
;
535 pdev
= platform_device_register_full(&pdevinfo
);
537 gpio_mockup_err("error registering device");
538 platform_driver_unregister(&gpio_mockup_driver
);
539 gpio_mockup_unregister_pdevs();
540 return PTR_ERR(pdev
);
543 gpio_mockup_pdevs
[i
] = pdev
;
549 static void __exit
gpio_mockup_exit(void)
551 debugfs_remove_recursive(gpio_mockup_dbg_dir
);
552 platform_driver_unregister(&gpio_mockup_driver
);
553 gpio_mockup_unregister_pdevs();
556 module_init(gpio_mockup_init
);
557 module_exit(gpio_mockup_exit
);
559 MODULE_AUTHOR("Kamlakant Patel <kamlakant.patel@broadcom.com>");
560 MODULE_AUTHOR("Bamvor Jian Zhang <bamv2005@gmail.com>");
561 MODULE_AUTHOR("Bartosz Golaszewski <brgl@bgdev.pl>");
562 MODULE_DESCRIPTION("GPIO Testing driver");
563 MODULE_LICENSE("GPL v2");