2 * GPIO Testing Device Driver
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version.
15 #include <linux/init.h>
16 #include <linux/module.h>
17 #include <linux/gpio/driver.h>
18 #include <linux/gpio/consumer.h>
19 #include <linux/platform_device.h>
20 #include <linux/slab.h>
21 #include <linux/interrupt.h>
22 #include <linux/irq.h>
23 #include <linux/irq_sim.h>
24 #include <linux/debugfs.h>
25 #include <linux/uaccess.h>
29 #define GPIO_MOCKUP_NAME "gpio-mockup"
30 #define GPIO_MOCKUP_MAX_GC 10
32 * We're storing two values per chip: the GPIO base and the number
35 #define GPIO_MOCKUP_MAX_RANGES (GPIO_MOCKUP_MAX_GC * 2)
38 GPIO_MOCKUP_DIR_OUT = 0,
39 GPIO_MOCKUP_DIR_IN = 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_params_nr;
67 module_param_array(gpio_mockup_ranges, int, &gpio_mockup_params_nr, 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 const char gpio_mockup_name_start = 'A';
74 static struct dentry *gpio_mockup_dbg_dir;
76 static int gpio_mockup_get(struct gpio_chip *gc, unsigned int offset)
78 struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
80 return chip->lines[offset].value;
83 static void gpio_mockup_set(struct gpio_chip *gc, unsigned int offset,
86 struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
88 chip->lines[offset].value = !!value;
91 static int gpio_mockup_dirout(struct gpio_chip *gc, unsigned int offset,
94 struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
96 gpio_mockup_set(gc, offset, value);
97 chip->lines[offset].dir = GPIO_MOCKUP_DIR_OUT;
102 static int gpio_mockup_dirin(struct gpio_chip *gc, unsigned int offset)
104 struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
106 chip->lines[offset].dir = GPIO_MOCKUP_DIR_IN;
111 static int gpio_mockup_get_direction(struct gpio_chip *gc, unsigned int offset)
113 struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
115 return chip->lines[offset].dir;
118 static int gpio_mockup_name_lines(struct device *dev,
119 struct gpio_mockup_chip *chip)
121 struct gpio_chip *gc = &chip->gc;
125 names = devm_kcalloc(dev, gc->ngpio, sizeof(char *), GFP_KERNEL);
129 for (i = 0; i < gc->ngpio; i++) {
130 names[i] = devm_kasprintf(dev, GFP_KERNEL,
131 "%s-%d", gc->label, i);
136 gc->names = (const char *const *)names;
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;
192 struct gpio_chip *gc;
198 chip->dbg_dir = debugfs_create_dir(gc->label, gpio_mockup_dbg_dir);
202 for (i = 0; i < gc->ngpio; i++) {
203 name = devm_kasprintf(dev, GFP_KERNEL, "%d", i);
207 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
213 priv->desc = &gc->gpiodev->descs[i];
215 evfile = debugfs_create_file(name, 0200, chip->dbg_dir, priv,
216 &gpio_mockup_event_ops);
224 dev_err(dev, "error creating debugfs directory\n");
227 static int gpio_mockup_add(struct device *dev,
228 struct gpio_mockup_chip *chip,
229 const char *name, int base, int ngpio)
231 struct gpio_chip *gc = &chip->gc;
237 gc->owner = THIS_MODULE;
239 gc->get = gpio_mockup_get;
240 gc->set = gpio_mockup_set;
241 gc->direction_output = gpio_mockup_dirout;
242 gc->direction_input = gpio_mockup_dirin;
243 gc->get_direction = gpio_mockup_get_direction;
244 gc->to_irq = gpio_mockup_to_irq;
246 chip->lines = devm_kcalloc(dev, gc->ngpio,
247 sizeof(*chip->lines), GFP_KERNEL);
251 if (gpio_mockup_named_lines) {
252 ret = gpio_mockup_name_lines(dev, chip);
257 ret = devm_irq_sim_init(dev, &chip->irqsim, gc->ngpio);
261 ret = devm_gpiochip_add_data(dev, &chip->gc, chip);
265 if (gpio_mockup_dbg_dir)
266 gpio_mockup_debugfs_setup(dev, chip);
271 static int gpio_mockup_probe(struct platform_device *pdev)
273 int ret, i, base, ngpio, num_chips;
274 struct device *dev = &pdev->dev;
275 struct gpio_mockup_chip *chips;
278 if (gpio_mockup_params_nr < 2 || (gpio_mockup_params_nr % 2))
281 /* Each chip is described by two values. */
282 num_chips = gpio_mockup_params_nr / 2;
284 chips = devm_kcalloc(dev, num_chips, sizeof(*chips), GFP_KERNEL);
288 platform_set_drvdata(pdev, chips);
290 for (i = 0; i < num_chips; i++) {
291 base = gpio_mockup_ranges[i * 2];
294 ngpio = gpio_mockup_ranges[i * 2 + 1];
296 ngpio = gpio_mockup_ranges[i * 2 + 1] - base;
299 chip_name = devm_kasprintf(dev, GFP_KERNEL,
300 "%s-%c", GPIO_MOCKUP_NAME,
301 gpio_mockup_name_start + i);
305 ret = gpio_mockup_add(dev, &chips[i],
306 chip_name, base, ngpio);
313 "adding gpiochip failed: %d (base: %d, ngpio: %d)\n",
314 ret, base, base < 0 ? ngpio : base + ngpio);
323 static struct platform_driver gpio_mockup_driver = {
325 .name = GPIO_MOCKUP_NAME,
327 .probe = gpio_mockup_probe,
330 static struct platform_device *pdev;
331 static int __init mock_device_init(void)
335 gpio_mockup_dbg_dir = debugfs_create_dir("gpio-mockup-event", NULL);
336 if (!gpio_mockup_dbg_dir)
337 pr_err("%s: error creating debugfs directory\n",
340 pdev = platform_device_alloc(GPIO_MOCKUP_NAME, -1);
344 err = platform_device_add(pdev);
346 platform_device_put(pdev);
350 err = platform_driver_register(&gpio_mockup_driver);
352 platform_device_unregister(pdev);
359 static void __exit mock_device_exit(void)
361 debugfs_remove_recursive(gpio_mockup_dbg_dir);
362 platform_driver_unregister(&gpio_mockup_driver);
363 platform_device_unregister(pdev);
366 module_init(mock_device_init);
367 module_exit(mock_device_exit);
372 MODULE_DESCRIPTION("GPIO Testing driver");
373 MODULE_LICENSE("GPL v2");