1 // SPDX-License-Identifier: GPL-2.0+
3 * GPIO Testing Device Driver
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 {
52 struct gpio_mockup_chip {
54 struct gpio_mockup_line_status *lines;
55 struct irq_sim irqsim;
56 struct dentry *dbg_dir;
59 struct gpio_mockup_dbgfs_private {
60 struct gpio_mockup_chip *chip;
61 struct gpio_desc *desc;
65 static int gpio_mockup_ranges[GPIO_MOCKUP_MAX_RANGES];
66 static int gpio_mockup_num_ranges;
67 module_param_array(gpio_mockup_ranges, int, &gpio_mockup_num_ranges, 0400);
69 static bool gpio_mockup_named_lines;
70 module_param_named(gpio_mockup_named_lines,
71 gpio_mockup_named_lines, bool, 0400);
73 static struct dentry *gpio_mockup_dbg_dir;
75 static int gpio_mockup_range_base(unsigned int index)
77 return gpio_mockup_ranges[index * 2];
80 static int gpio_mockup_range_ngpio(unsigned int index)
82 return gpio_mockup_ranges[index * 2 + 1];
85 static int gpio_mockup_get(struct gpio_chip *gc, unsigned int offset)
87 struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
89 return chip->lines[offset].value;
92 static void gpio_mockup_set(struct gpio_chip *gc,
93 unsigned int offset, int value)
95 struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
97 chip->lines[offset].value = !!value;
100 static void gpio_mockup_set_multiple(struct gpio_chip *gc,
101 unsigned long *mask, unsigned long *bits)
105 for_each_set_bit(bit, mask, gc->ngpio)
106 gpio_mockup_set(gc, bit, test_bit(bit, bits));
110 static int gpio_mockup_dirout(struct gpio_chip *gc,
111 unsigned int offset, int value)
113 struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
115 gpio_mockup_set(gc, offset, value);
116 chip->lines[offset].dir = GPIO_MOCKUP_DIR_OUT;
121 static int gpio_mockup_dirin(struct gpio_chip *gc, unsigned int offset)
123 struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
125 chip->lines[offset].dir = GPIO_MOCKUP_DIR_IN;
130 static int gpio_mockup_get_direction(struct gpio_chip *gc, unsigned int offset)
132 struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
134 return !chip->lines[offset].dir;
137 static int gpio_mockup_to_irq(struct gpio_chip *gc, unsigned int offset)
139 struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
141 return irq_sim_irqnum(&chip->irqsim, offset);
144 static ssize_t gpio_mockup_event_write(struct file *file,
145 const char __user *usr_buf,
146 size_t size, loff_t *ppos)
148 struct gpio_mockup_dbgfs_private *priv;
149 struct gpio_mockup_chip *chip;
150 struct seq_file *sfile;
151 struct gpio_desc *desc;
154 rv = kstrtoint_from_user(usr_buf, size, 0, &val);
157 if (val != 0 && val != 1)
160 sfile = file->private_data;
161 priv = sfile->private;
165 gpiod_set_value_cansleep(desc, val);
166 irq_sim_fire(&chip->irqsim, priv->offset);
171 static int gpio_mockup_event_open(struct inode *inode, struct file *file)
173 return single_open(file, NULL, inode->i_private);
176 static const struct file_operations gpio_mockup_event_ops = {
177 .owner = THIS_MODULE,
178 .open = gpio_mockup_event_open,
179 .write = gpio_mockup_event_write,
183 static void gpio_mockup_debugfs_setup(struct device *dev,
184 struct gpio_mockup_chip *chip)
186 struct gpio_mockup_dbgfs_private *priv;
187 struct dentry *evfile, *link;
188 struct gpio_chip *gc;
194 devname = dev_name(&gc->gpiodev->dev);
196 chip->dbg_dir = debugfs_create_dir(devname, gpio_mockup_dbg_dir);
197 if (IS_ERR_OR_NULL(chip->dbg_dir))
200 link = debugfs_create_symlink(gc->label, gpio_mockup_dbg_dir, devname);
201 if (IS_ERR_OR_NULL(link))
204 for (i = 0; i < gc->ngpio; i++) {
205 name = devm_kasprintf(dev, GFP_KERNEL, "%d", i);
209 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
215 priv->desc = &gc->gpiodev->descs[i];
217 evfile = debugfs_create_file(name, 0200, chip->dbg_dir, priv,
218 &gpio_mockup_event_ops);
219 if (IS_ERR_OR_NULL(evfile))
226 dev_err(dev, "error creating debugfs event files\n");
229 static int gpio_mockup_name_lines(struct device *dev,
230 struct gpio_mockup_chip *chip)
232 struct gpio_chip *gc = &chip->gc;
236 names = devm_kcalloc(dev, gc->ngpio, sizeof(char *), GFP_KERNEL);
240 for (i = 0; i < gc->ngpio; i++) {
241 names[i] = devm_kasprintf(dev, GFP_KERNEL,
242 "%s-%d", gc->label, i);
247 gc->names = (const char *const *)names;
252 static int gpio_mockup_probe(struct platform_device *pdev)
254 struct gpio_mockup_chip *chip;
255 struct gpio_chip *gc;
263 rv = device_property_read_u32(dev, "gpio-base", &base);
267 rv = device_property_read_u16(dev, "nr-gpios", &ngpio);
271 rv = device_property_read_string(dev, "chip-name", &name);
275 chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL);
280 name = devm_kasprintf(dev, GFP_KERNEL,
281 "%s-%c", pdev->name, pdev->id + 'A');
290 gc->owner = THIS_MODULE;
292 gc->get = gpio_mockup_get;
293 gc->set = gpio_mockup_set;
294 gc->set_multiple = gpio_mockup_set_multiple;
295 gc->direction_output = gpio_mockup_dirout;
296 gc->direction_input = gpio_mockup_dirin;
297 gc->get_direction = gpio_mockup_get_direction;
298 gc->to_irq = gpio_mockup_to_irq;
300 chip->lines = devm_kcalloc(dev, gc->ngpio,
301 sizeof(*chip->lines), GFP_KERNEL);
305 if (device_property_read_bool(dev, "named-gpio-lines")) {
306 rv = gpio_mockup_name_lines(dev, chip);
311 rv = devm_irq_sim_init(dev, &chip->irqsim, gc->ngpio);
315 rv = devm_gpiochip_add_data(dev, &chip->gc, chip);
319 if (!IS_ERR_OR_NULL(gpio_mockup_dbg_dir))
320 gpio_mockup_debugfs_setup(dev, chip);
325 static struct platform_driver gpio_mockup_driver = {
327 .name = GPIO_MOCKUP_NAME,
329 .probe = gpio_mockup_probe,
332 static struct platform_device *gpio_mockup_pdevs[GPIO_MOCKUP_MAX_GC];
334 static void gpio_mockup_unregister_pdevs(void)
336 struct platform_device *pdev;
339 for (i = 0; i < GPIO_MOCKUP_MAX_GC; i++) {
340 pdev = gpio_mockup_pdevs[i];
343 platform_device_unregister(pdev);
347 static int __init gpio_mockup_init(void)
349 struct property_entry properties[GPIO_MOCKUP_MAX_PROP];
350 int i, prop, num_chips, err = 0, base;
351 struct platform_device_info pdevinfo;
352 struct platform_device *pdev;
355 if ((gpio_mockup_num_ranges < 2) ||
356 (gpio_mockup_num_ranges % 2) ||
357 (gpio_mockup_num_ranges > GPIO_MOCKUP_MAX_RANGES))
360 /* Each chip is described by two values. */
361 num_chips = gpio_mockup_num_ranges / 2;
364 * The second value in the <base GPIO - number of GPIOS> pair must
365 * always be greater than 0.
367 for (i = 0; i < num_chips; i++) {
368 if (gpio_mockup_range_ngpio(i) < 0)
372 gpio_mockup_dbg_dir = debugfs_create_dir("gpio-mockup-event", NULL);
373 if (IS_ERR_OR_NULL(gpio_mockup_dbg_dir))
374 gpio_mockup_err("error creating debugfs directory\n");
376 err = platform_driver_register(&gpio_mockup_driver);
378 gpio_mockup_err("error registering platform driver\n");
382 for (i = 0; i < num_chips; i++) {
383 memset(properties, 0, sizeof(properties));
384 memset(&pdevinfo, 0, sizeof(pdevinfo));
387 base = gpio_mockup_range_base(i);
389 properties[prop++] = PROPERTY_ENTRY_U32("gpio-base",
392 ngpio = base < 0 ? gpio_mockup_range_ngpio(i)
393 : gpio_mockup_range_ngpio(i) - base;
394 properties[prop++] = PROPERTY_ENTRY_U16("nr-gpios", ngpio);
396 if (gpio_mockup_named_lines)
397 properties[prop++] = PROPERTY_ENTRY_BOOL(
400 pdevinfo.name = GPIO_MOCKUP_NAME;
402 pdevinfo.properties = properties;
404 pdev = platform_device_register_full(&pdevinfo);
406 gpio_mockup_err("error registering device");
407 platform_driver_unregister(&gpio_mockup_driver);
408 gpio_mockup_unregister_pdevs();
409 return PTR_ERR(pdev);
412 gpio_mockup_pdevs[i] = pdev;
418 static void __exit gpio_mockup_exit(void)
420 debugfs_remove_recursive(gpio_mockup_dbg_dir);
421 platform_driver_unregister(&gpio_mockup_driver);
422 gpio_mockup_unregister_pdevs();
425 module_init(gpio_mockup_init);
426 module_exit(gpio_mockup_exit);
431 MODULE_DESCRIPTION("GPIO Testing driver");
432 MODULE_LICENSE("GPL v2");