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>
24 #define GPIO_MOCKUP_NAME "gpio-mockup"
25 #define GPIO_MOCKUP_MAX_GC 10
27 * We're storing two values per chip: the GPIO base and the number
30 #define GPIO_MOCKUP_MAX_RANGES (GPIO_MOCKUP_MAX_GC * 2)
32 #define gpio_mockup_err(...) pr_err(GPIO_MOCKUP_NAME ": " __VA_ARGS__)
35 GPIO_MOCKUP_DIR_IN
= 0,
36 GPIO_MOCKUP_DIR_OUT
= 1,
40 * struct gpio_pin_status - structure describing a GPIO status
41 * @dir: Configures direction of gpio as "in" or "out", 0=in, 1=out
42 * @value: Configures status of the gpio as 0(low) or 1(high)
44 struct gpio_mockup_line_status
{
49 struct gpio_mockup_chip
{
51 struct gpio_mockup_line_status
*lines
;
52 struct irq_sim irqsim
;
53 struct dentry
*dbg_dir
;
56 struct gpio_mockup_dbgfs_private
{
57 struct gpio_mockup_chip
*chip
;
58 struct gpio_desc
*desc
;
62 struct gpio_mockup_platform_data
{
69 static int gpio_mockup_ranges
[GPIO_MOCKUP_MAX_RANGES
];
70 static int gpio_mockup_num_ranges
;
71 module_param_array(gpio_mockup_ranges
, int, &gpio_mockup_num_ranges
, 0400);
73 static bool gpio_mockup_named_lines
;
74 module_param_named(gpio_mockup_named_lines
,
75 gpio_mockup_named_lines
, bool, 0400);
77 static struct dentry
*gpio_mockup_dbg_dir
;
79 static int gpio_mockup_range_base(unsigned int index
)
81 return gpio_mockup_ranges
[index
* 2];
84 static int gpio_mockup_range_ngpio(unsigned int index
)
86 return gpio_mockup_ranges
[index
* 2 + 1];
89 static int gpio_mockup_get(struct gpio_chip
*gc
, unsigned int offset
)
91 struct gpio_mockup_chip
*chip
= gpiochip_get_data(gc
);
93 return chip
->lines
[offset
].value
;
96 static void gpio_mockup_set(struct gpio_chip
*gc
,
97 unsigned int offset
, int value
)
99 struct gpio_mockup_chip
*chip
= gpiochip_get_data(gc
);
101 chip
->lines
[offset
].value
= !!value
;
104 static void gpio_mockup_set_multiple(struct gpio_chip
*gc
,
105 unsigned long *mask
, unsigned long *bits
)
109 for_each_set_bit(bit
, mask
, gc
->ngpio
)
110 gpio_mockup_set(gc
, bit
, test_bit(bit
, bits
));
114 static int gpio_mockup_dirout(struct gpio_chip
*gc
,
115 unsigned int offset
, int value
)
117 struct gpio_mockup_chip
*chip
= gpiochip_get_data(gc
);
119 gpio_mockup_set(gc
, offset
, value
);
120 chip
->lines
[offset
].dir
= GPIO_MOCKUP_DIR_OUT
;
125 static int gpio_mockup_dirin(struct gpio_chip
*gc
, unsigned int offset
)
127 struct gpio_mockup_chip
*chip
= gpiochip_get_data(gc
);
129 chip
->lines
[offset
].dir
= GPIO_MOCKUP_DIR_IN
;
134 static int gpio_mockup_get_direction(struct gpio_chip
*gc
, unsigned int offset
)
136 struct gpio_mockup_chip
*chip
= gpiochip_get_data(gc
);
138 return !chip
->lines
[offset
].dir
;
141 static int gpio_mockup_to_irq(struct gpio_chip
*gc
, unsigned int offset
)
143 struct gpio_mockup_chip
*chip
= gpiochip_get_data(gc
);
145 return irq_sim_irqnum(&chip
->irqsim
, offset
);
148 static ssize_t
gpio_mockup_event_write(struct file
*file
,
149 const char __user
*usr_buf
,
150 size_t size
, loff_t
*ppos
)
152 struct gpio_mockup_dbgfs_private
*priv
;
153 struct gpio_mockup_chip
*chip
;
154 struct seq_file
*sfile
;
155 struct gpio_desc
*desc
;
158 rv
= kstrtoint_from_user(usr_buf
, size
, 0, &val
);
161 if (val
!= 0 && val
!= 1)
164 sfile
= file
->private_data
;
165 priv
= sfile
->private;
169 gpiod_set_value_cansleep(desc
, val
);
170 irq_sim_fire(&chip
->irqsim
, priv
->offset
);
175 static int gpio_mockup_event_open(struct inode
*inode
, struct file
*file
)
177 return single_open(file
, NULL
, inode
->i_private
);
180 static const struct file_operations gpio_mockup_event_ops
= {
181 .owner
= THIS_MODULE
,
182 .open
= gpio_mockup_event_open
,
183 .write
= gpio_mockup_event_write
,
187 static void gpio_mockup_debugfs_setup(struct device
*dev
,
188 struct gpio_mockup_chip
*chip
)
190 struct gpio_mockup_dbgfs_private
*priv
;
191 struct dentry
*evfile
, *link
;
192 struct gpio_chip
*gc
;
198 devname
= dev_name(&gc
->gpiodev
->dev
);
200 chip
->dbg_dir
= debugfs_create_dir(devname
, gpio_mockup_dbg_dir
);
201 if (IS_ERR_OR_NULL(chip
->dbg_dir
))
204 link
= debugfs_create_symlink(gc
->label
, gpio_mockup_dbg_dir
, devname
);
205 if (IS_ERR_OR_NULL(link
))
208 for (i
= 0; i
< gc
->ngpio
; i
++) {
209 name
= devm_kasprintf(dev
, GFP_KERNEL
, "%d", i
);
213 priv
= devm_kzalloc(dev
, sizeof(*priv
), GFP_KERNEL
);
219 priv
->desc
= &gc
->gpiodev
->descs
[i
];
221 evfile
= debugfs_create_file(name
, 0200, chip
->dbg_dir
, priv
,
222 &gpio_mockup_event_ops
);
223 if (IS_ERR_OR_NULL(evfile
))
230 dev_err(dev
, "error creating debugfs event files\n");
233 static int gpio_mockup_name_lines(struct device
*dev
,
234 struct gpio_mockup_chip
*chip
)
236 struct gpio_chip
*gc
= &chip
->gc
;
240 names
= devm_kcalloc(dev
, gc
->ngpio
, sizeof(char *), GFP_KERNEL
);
244 for (i
= 0; i
< gc
->ngpio
; i
++) {
245 names
[i
] = devm_kasprintf(dev
, GFP_KERNEL
,
246 "%s-%d", gc
->label
, i
);
251 gc
->names
= (const char *const *)names
;
256 static int gpio_mockup_probe(struct platform_device
*pdev
)
258 struct gpio_mockup_platform_data
*pdata
;
259 struct gpio_mockup_chip
*chip
;
260 struct gpio_chip
*gc
;
266 pdata
= dev_get_platdata(dev
);
268 ngpio
= pdata
->ngpio
;
270 chip
= devm_kzalloc(dev
, sizeof(*chip
), GFP_KERNEL
);
274 name
= devm_kasprintf(dev
, GFP_KERNEL
, "%s-%c",
275 pdev
->name
, pdata
->index
);
283 gc
->owner
= THIS_MODULE
;
285 gc
->get
= gpio_mockup_get
;
286 gc
->set
= gpio_mockup_set
;
287 gc
->set_multiple
= gpio_mockup_set_multiple
;
288 gc
->direction_output
= gpio_mockup_dirout
;
289 gc
->direction_input
= gpio_mockup_dirin
;
290 gc
->get_direction
= gpio_mockup_get_direction
;
291 gc
->to_irq
= gpio_mockup_to_irq
;
293 chip
->lines
= devm_kcalloc(dev
, gc
->ngpio
,
294 sizeof(*chip
->lines
), GFP_KERNEL
);
298 if (pdata
->named_lines
) {
299 rv
= gpio_mockup_name_lines(dev
, chip
);
304 rv
= devm_irq_sim_init(dev
, &chip
->irqsim
, gc
->ngpio
);
308 rv
= devm_gpiochip_add_data(dev
, &chip
->gc
, chip
);
312 if (!IS_ERR_OR_NULL(gpio_mockup_dbg_dir
))
313 gpio_mockup_debugfs_setup(dev
, chip
);
318 static struct platform_driver gpio_mockup_driver
= {
320 .name
= GPIO_MOCKUP_NAME
,
322 .probe
= gpio_mockup_probe
,
325 static struct platform_device
*gpio_mockup_pdevs
[GPIO_MOCKUP_MAX_GC
];
327 static void gpio_mockup_unregister_pdevs(void)
329 struct platform_device
*pdev
;
332 for (i
= 0; i
< GPIO_MOCKUP_MAX_GC
; i
++) {
333 pdev
= gpio_mockup_pdevs
[i
];
336 platform_device_unregister(pdev
);
340 static int __init
gpio_mockup_init(void)
342 int i
, num_chips
, err
= 0, index
= 'A';
343 struct gpio_mockup_platform_data pdata
;
344 struct platform_device
*pdev
;
346 if ((gpio_mockup_num_ranges
< 2) ||
347 (gpio_mockup_num_ranges
% 2) ||
348 (gpio_mockup_num_ranges
> GPIO_MOCKUP_MAX_RANGES
))
351 /* Each chip is described by two values. */
352 num_chips
= gpio_mockup_num_ranges
/ 2;
355 * The second value in the <base GPIO - number of GPIOS> pair must
356 * always be greater than 0.
358 for (i
= 0; i
< num_chips
; i
++) {
359 if (gpio_mockup_range_ngpio(i
) < 0)
363 gpio_mockup_dbg_dir
= debugfs_create_dir("gpio-mockup-event", NULL
);
364 if (IS_ERR_OR_NULL(gpio_mockup_dbg_dir
))
365 gpio_mockup_err("error creating debugfs directory\n");
367 err
= platform_driver_register(&gpio_mockup_driver
);
369 gpio_mockup_err("error registering platform driver\n");
373 for (i
= 0; i
< num_chips
; i
++) {
374 pdata
.index
= index
++;
375 pdata
.base
= gpio_mockup_range_base(i
);
376 pdata
.ngpio
= pdata
.base
< 0
377 ? gpio_mockup_range_ngpio(i
)
378 : gpio_mockup_range_ngpio(i
) - pdata
.base
;
379 pdata
.named_lines
= gpio_mockup_named_lines
;
381 pdev
= platform_device_register_resndata(NULL
,
386 gpio_mockup_err("error registering device");
387 platform_driver_unregister(&gpio_mockup_driver
);
388 gpio_mockup_unregister_pdevs();
389 return PTR_ERR(pdev
);
392 gpio_mockup_pdevs
[i
] = pdev
;
398 static void __exit
gpio_mockup_exit(void)
400 debugfs_remove_recursive(gpio_mockup_dbg_dir
);
401 platform_driver_unregister(&gpio_mockup_driver
);
402 gpio_mockup_unregister_pdevs();
405 module_init(gpio_mockup_init
);
406 module_exit(gpio_mockup_exit
);
408 MODULE_AUTHOR("Kamlakant Patel <kamlakant.patel@broadcom.com>");
409 MODULE_AUTHOR("Bamvor Jian Zhang <bamv2005@gmail.com>");
410 MODULE_AUTHOR("Bartosz Golaszewski <brgl@bgdev.pl>");
411 MODULE_DESCRIPTION("GPIO Testing driver");
412 MODULE_LICENSE("GPL v2");