2 * GPIO Testing Device Driver
4 * Copyright (C) 2014 Kamlakant Patel <kamlakant.patel@broadcom.com>
5 * Copyright (C) 2015-2016 Bamvor Jian Zhang <bamvor.zhangjian@linaro.org>
6 * Copyright (C) 2017 Bartosz Golaszewski <brgl@bgdev.pl>
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)
37 #define gpio_mockup_err(...) pr_err(GPIO_MOCKUP_NAME ": " __VA_ARGS__)
40 GPIO_MOCKUP_DIR_OUT = 0,
41 GPIO_MOCKUP_DIR_IN = 1,
45 * struct gpio_pin_status - structure describing a GPIO status
46 * @dir: Configures direction of gpio as "in" or "out", 0=in, 1=out
47 * @value: Configures status of the gpio as 0(low) or 1(high)
49 struct gpio_mockup_line_status {
54 struct gpio_mockup_chip {
56 struct gpio_mockup_line_status *lines;
57 struct irq_sim irqsim;
58 struct dentry *dbg_dir;
61 struct gpio_mockup_dbgfs_private {
62 struct gpio_mockup_chip *chip;
63 struct gpio_desc *desc;
67 struct gpio_mockup_platform_data {
74 static int gpio_mockup_ranges[GPIO_MOCKUP_MAX_RANGES];
75 static int gpio_mockup_num_ranges;
76 module_param_array(gpio_mockup_ranges, int, &gpio_mockup_num_ranges, 0400);
78 static bool gpio_mockup_named_lines;
79 module_param_named(gpio_mockup_named_lines,
80 gpio_mockup_named_lines, bool, 0400);
82 static struct dentry *gpio_mockup_dbg_dir;
84 static int gpio_mockup_range_base(unsigned int index)
86 return gpio_mockup_ranges[index * 2];
89 static int gpio_mockup_range_ngpio(unsigned int index)
91 return gpio_mockup_ranges[index * 2 + 1];
94 static int gpio_mockup_get(struct gpio_chip *gc, unsigned int offset)
96 struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
98 return chip->lines[offset].value;
101 static void gpio_mockup_set(struct gpio_chip *gc,
102 unsigned int offset, int value)
104 struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
106 chip->lines[offset].value = !!value;
109 static void gpio_mockup_set_multiple(struct gpio_chip *gc,
110 unsigned long *mask, unsigned long *bits)
114 for_each_set_bit(bit, mask, gc->ngpio)
115 gpio_mockup_set(gc, bit, test_bit(bit, bits));
119 static int gpio_mockup_dirout(struct gpio_chip *gc,
120 unsigned int offset, int value)
122 struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
124 gpio_mockup_set(gc, offset, value);
125 chip->lines[offset].dir = GPIO_MOCKUP_DIR_OUT;
130 static int gpio_mockup_dirin(struct gpio_chip *gc, unsigned int offset)
132 struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
134 chip->lines[offset].dir = GPIO_MOCKUP_DIR_IN;
139 static int gpio_mockup_get_direction(struct gpio_chip *gc, unsigned int offset)
141 struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
143 return chip->lines[offset].dir;
146 static int gpio_mockup_to_irq(struct gpio_chip *gc, unsigned int offset)
148 struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
150 return irq_sim_irqnum(&chip->irqsim, offset);
153 static ssize_t gpio_mockup_event_write(struct file *file,
154 const char __user *usr_buf,
155 size_t size, loff_t *ppos)
157 struct gpio_mockup_dbgfs_private *priv;
158 struct gpio_mockup_chip *chip;
159 struct seq_file *sfile;
160 struct gpio_desc *desc;
163 rv = kstrtoint_from_user(usr_buf, size, 0, &val);
166 if (val != 0 && val != 1)
169 sfile = file->private_data;
170 priv = sfile->private;
174 gpiod_set_value_cansleep(desc, val);
175 irq_sim_fire(&chip->irqsim, priv->offset);
180 static int gpio_mockup_event_open(struct inode *inode, struct file *file)
182 return single_open(file, NULL, inode->i_private);
185 static const struct file_operations gpio_mockup_event_ops = {
186 .owner = THIS_MODULE,
187 .open = gpio_mockup_event_open,
188 .write = gpio_mockup_event_write,
192 static void gpio_mockup_debugfs_setup(struct device *dev,
193 struct gpio_mockup_chip *chip)
195 struct gpio_mockup_dbgfs_private *priv;
196 struct dentry *evfile, *link;
197 struct gpio_chip *gc;
203 devname = dev_name(&gc->gpiodev->dev);
205 chip->dbg_dir = debugfs_create_dir(devname, gpio_mockup_dbg_dir);
206 if (IS_ERR_OR_NULL(chip->dbg_dir))
209 link = debugfs_create_symlink(gc->label, gpio_mockup_dbg_dir, devname);
210 if (IS_ERR_OR_NULL(link))
213 for (i = 0; i < gc->ngpio; i++) {
214 name = devm_kasprintf(dev, GFP_KERNEL, "%d", i);
218 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
224 priv->desc = &gc->gpiodev->descs[i];
226 evfile = debugfs_create_file(name, 0200, chip->dbg_dir, priv,
227 &gpio_mockup_event_ops);
228 if (IS_ERR_OR_NULL(evfile))
235 dev_err(dev, "error creating debugfs event files\n");
238 static int gpio_mockup_name_lines(struct device *dev,
239 struct gpio_mockup_chip *chip)
241 struct gpio_chip *gc = &chip->gc;
245 names = devm_kcalloc(dev, gc->ngpio, sizeof(char *), GFP_KERNEL);
249 for (i = 0; i < gc->ngpio; i++) {
250 names[i] = devm_kasprintf(dev, GFP_KERNEL,
251 "%s-%d", gc->label, i);
256 gc->names = (const char *const *)names;
261 static int gpio_mockup_probe(struct platform_device *pdev)
263 struct gpio_mockup_platform_data *pdata;
264 struct gpio_mockup_chip *chip;
265 struct gpio_chip *gc;
271 pdata = dev_get_platdata(dev);
273 ngpio = pdata->ngpio;
275 chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL);
279 name = devm_kasprintf(dev, GFP_KERNEL, "%s-%c",
280 pdev->name, pdata->index);
288 gc->owner = THIS_MODULE;
290 gc->get = gpio_mockup_get;
291 gc->set = gpio_mockup_set;
292 gc->set_multiple = gpio_mockup_set_multiple;
293 gc->direction_output = gpio_mockup_dirout;
294 gc->direction_input = gpio_mockup_dirin;
295 gc->get_direction = gpio_mockup_get_direction;
296 gc->to_irq = gpio_mockup_to_irq;
298 chip->lines = devm_kcalloc(dev, gc->ngpio,
299 sizeof(*chip->lines), GFP_KERNEL);
303 if (pdata->named_lines) {
304 rv = gpio_mockup_name_lines(dev, chip);
309 rv = devm_irq_sim_init(dev, &chip->irqsim, gc->ngpio);
313 rv = devm_gpiochip_add_data(dev, &chip->gc, chip);
317 if (gpio_mockup_dbg_dir)
318 gpio_mockup_debugfs_setup(dev, chip);
323 static struct platform_driver gpio_mockup_driver = {
325 .name = GPIO_MOCKUP_NAME,
327 .probe = gpio_mockup_probe,
330 static struct platform_device *gpio_mockup_pdevs[GPIO_MOCKUP_MAX_GC];
332 static void gpio_mockup_unregister_pdevs(void)
334 struct platform_device *pdev;
337 for (i = 0; i < GPIO_MOCKUP_MAX_GC; i++) {
338 pdev = gpio_mockup_pdevs[i];
341 platform_device_unregister(pdev);
345 static int __init gpio_mockup_init(void)
347 int i, num_chips, err = 0, index = 'A';
348 struct gpio_mockup_platform_data pdata;
349 struct platform_device *pdev;
351 if ((gpio_mockup_num_ranges < 2) ||
352 (gpio_mockup_num_ranges % 2) ||
353 (gpio_mockup_num_ranges > GPIO_MOCKUP_MAX_RANGES))
356 /* Each chip is described by two values. */
357 num_chips = gpio_mockup_num_ranges / 2;
360 * The second value in the <base GPIO - number of GPIOS> pair must
361 * always be greater than 0.
363 for (i = 0; i < num_chips; i++) {
364 if (gpio_mockup_range_ngpio(i) < 0)
368 gpio_mockup_dbg_dir = debugfs_create_dir("gpio-mockup-event", NULL);
369 if (IS_ERR_OR_NULL(gpio_mockup_dbg_dir))
370 gpio_mockup_err("error creating debugfs directory\n");
372 err = platform_driver_register(&gpio_mockup_driver);
374 gpio_mockup_err("error registering platform driver\n");
378 for (i = 0; i < num_chips; i++) {
379 pdata.index = index++;
380 pdata.base = gpio_mockup_range_base(i);
381 pdata.ngpio = pdata.base < 0
382 ? gpio_mockup_range_ngpio(i)
383 : gpio_mockup_range_ngpio(i) - pdata.base;
384 pdata.named_lines = gpio_mockup_named_lines;
386 pdev = platform_device_register_resndata(NULL,
391 gpio_mockup_err("error registering device");
392 platform_driver_unregister(&gpio_mockup_driver);
393 gpio_mockup_unregister_pdevs();
394 return PTR_ERR(pdev);
397 gpio_mockup_pdevs[i] = pdev;
403 static void __exit gpio_mockup_exit(void)
405 debugfs_remove_recursive(gpio_mockup_dbg_dir);
406 platform_driver_unregister(&gpio_mockup_driver);
407 gpio_mockup_unregister_pdevs();
410 module_init(gpio_mockup_init);
411 module_exit(gpio_mockup_exit);
413 MODULE_AUTHOR("Kamlakant Patel <kamlakant.patel@broadcom.com>");
414 MODULE_AUTHOR("Bamvor Jian Zhang <bamvor.zhangjian@linaro.org>");
415 MODULE_AUTHOR("Bartosz Golaszewski <brgl@bgdev.pl>");
416 MODULE_DESCRIPTION("GPIO Testing driver");
417 MODULE_LICENSE("GPL v2");