Merge tag 'tag-chrome-platform-for-v4.20' of git://git.kernel.org/pub/scm/linux/kerne...
[linux/fpc-iii.git] / drivers / gpio / gpio-mockup.c
blob8269cffc2967f772ba2da14e566fd136bb68da56
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
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>
8 */
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>
23 #include "gpiolib.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
29 * of GPIO lines.
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__)
37 enum {
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 {
48 int dir;
49 int value;
52 struct gpio_mockup_chip {
53 struct gpio_chip gc;
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;
62 int offset;
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)
103 unsigned int bit;
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;
118 return 0;
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;
127 return 0;
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;
152 int rv, val;
154 rv = kstrtoint_from_user(usr_buf, size, 0, &val);
155 if (rv)
156 return rv;
157 if (val != 0 && val != 1)
158 return -EINVAL;
160 sfile = file->private_data;
161 priv = sfile->private;
162 desc = priv->desc;
163 chip = priv->chip;
165 gpiod_set_value_cansleep(desc, val);
166 irq_sim_fire(&chip->irqsim, priv->offset);
168 return size;
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,
180 .llseek = no_llseek,
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;
189 const char *devname;
190 char *name;
191 int i;
193 gc = &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))
198 goto err;
200 link = debugfs_create_symlink(gc->label, gpio_mockup_dbg_dir, devname);
201 if (IS_ERR_OR_NULL(link))
202 goto err;
204 for (i = 0; i < gc->ngpio; i++) {
205 name = devm_kasprintf(dev, GFP_KERNEL, "%d", i);
206 if (!name)
207 goto err;
209 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
210 if (!priv)
211 goto err;
213 priv->chip = chip;
214 priv->offset = i;
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))
220 goto err;
223 return;
225 err:
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;
233 char **names;
234 int i;
236 names = devm_kcalloc(dev, gc->ngpio, sizeof(char *), GFP_KERNEL);
237 if (!names)
238 return -ENOMEM;
240 for (i = 0; i < gc->ngpio; i++) {
241 names[i] = devm_kasprintf(dev, GFP_KERNEL,
242 "%s-%d", gc->label, i);
243 if (!names[i])
244 return -ENOMEM;
247 gc->names = (const char *const *)names;
249 return 0;
252 static int gpio_mockup_probe(struct platform_device *pdev)
254 struct gpio_mockup_chip *chip;
255 struct gpio_chip *gc;
256 struct device *dev;
257 const char *name;
258 int rv, base;
259 u16 ngpio;
261 dev = &pdev->dev;
263 rv = device_property_read_u32(dev, "gpio-base", &base);
264 if (rv)
265 base = -1;
267 rv = device_property_read_u16(dev, "nr-gpios", &ngpio);
268 if (rv)
269 return rv;
271 rv = device_property_read_string(dev, "chip-name", &name);
272 if (rv)
273 name = NULL;
275 chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL);
276 if (!chip)
277 return -ENOMEM;
279 if (!name) {
280 name = devm_kasprintf(dev, GFP_KERNEL,
281 "%s-%c", pdev->name, pdev->id + 'A');
282 if (!name)
283 return -ENOMEM;
286 gc = &chip->gc;
287 gc->base = base;
288 gc->ngpio = ngpio;
289 gc->label = name;
290 gc->owner = THIS_MODULE;
291 gc->parent = dev;
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);
302 if (!chip->lines)
303 return -ENOMEM;
305 if (device_property_read_bool(dev, "named-gpio-lines")) {
306 rv = gpio_mockup_name_lines(dev, chip);
307 if (rv)
308 return rv;
311 rv = devm_irq_sim_init(dev, &chip->irqsim, gc->ngpio);
312 if (rv < 0)
313 return rv;
315 rv = devm_gpiochip_add_data(dev, &chip->gc, chip);
316 if (rv)
317 return rv;
319 if (!IS_ERR_OR_NULL(gpio_mockup_dbg_dir))
320 gpio_mockup_debugfs_setup(dev, chip);
322 return 0;
325 static struct platform_driver gpio_mockup_driver = {
326 .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;
337 int i;
339 for (i = 0; i < GPIO_MOCKUP_MAX_GC; i++) {
340 pdev = gpio_mockup_pdevs[i];
342 if (pdev)
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;
353 u16 ngpio;
355 if ((gpio_mockup_num_ranges < 2) ||
356 (gpio_mockup_num_ranges % 2) ||
357 (gpio_mockup_num_ranges > GPIO_MOCKUP_MAX_RANGES))
358 return -EINVAL;
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)
369 return -EINVAL;
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);
377 if (err) {
378 gpio_mockup_err("error registering platform driver\n");
379 return err;
382 for (i = 0; i < num_chips; i++) {
383 memset(properties, 0, sizeof(properties));
384 memset(&pdevinfo, 0, sizeof(pdevinfo));
385 prop = 0;
387 base = gpio_mockup_range_base(i);
388 if (base >= 0)
389 properties[prop++] = PROPERTY_ENTRY_U32("gpio-base",
390 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(
398 "named-gpio-lines");
400 pdevinfo.name = GPIO_MOCKUP_NAME;
401 pdevinfo.id = i;
402 pdevinfo.properties = properties;
404 pdev = platform_device_register_full(&pdevinfo);
405 if (IS_ERR(pdev)) {
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;
415 return 0;
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);
428 MODULE_AUTHOR("Kamlakant Patel <kamlakant.patel@broadcom.com>");
429 MODULE_AUTHOR("Bamvor Jian Zhang <bamv2005@gmail.com>");
430 MODULE_AUTHOR("Bartosz Golaszewski <brgl@bgdev.pl>");
431 MODULE_DESCRIPTION("GPIO Testing driver");
432 MODULE_LICENSE("GPL v2");