io_uring: do not always copy iovec in io_req_map_rw()
[linux/fpc-iii.git] / drivers / gpio / gpio-mockup.c
blob7d343bea784afe3c021d22ec66cdd8d796e5ae21
1 // SPDX-License-Identifier: GPL-2.0-or-later
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/debugfs.h>
11 #include <linux/gpio/consumer.h>
12 #include <linux/gpio/driver.h>
13 #include <linux/init.h>
14 #include <linux/interrupt.h>
15 #include <linux/irq.h>
16 #include <linux/irq_sim.h>
17 #include <linux/module.h>
18 #include <linux/platform_device.h>
19 #include <linux/property.h>
20 #include <linux/slab.h>
21 #include <linux/uaccess.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__)
38 * struct gpio_pin_status - structure describing a GPIO status
39 * @dir: Configures direction of gpio as "in" or "out"
40 * @value: Configures status of the gpio as 0(low) or 1(high)
42 struct gpio_mockup_line_status {
43 int dir;
44 int value;
45 int pull;
48 struct gpio_mockup_chip {
49 struct gpio_chip gc;
50 struct gpio_mockup_line_status *lines;
51 struct irq_sim irqsim;
52 struct dentry *dbg_dir;
53 struct mutex lock;
56 struct gpio_mockup_dbgfs_private {
57 struct gpio_mockup_chip *chip;
58 struct gpio_desc *desc;
59 unsigned int offset;
62 static int gpio_mockup_ranges[GPIO_MOCKUP_MAX_RANGES];
63 static int gpio_mockup_num_ranges;
64 module_param_array(gpio_mockup_ranges, int, &gpio_mockup_num_ranges, 0400);
66 static bool gpio_mockup_named_lines;
67 module_param_named(gpio_mockup_named_lines,
68 gpio_mockup_named_lines, bool, 0400);
70 static struct dentry *gpio_mockup_dbg_dir;
72 static int gpio_mockup_range_base(unsigned int index)
74 return gpio_mockup_ranges[index * 2];
77 static int gpio_mockup_range_ngpio(unsigned int index)
79 return gpio_mockup_ranges[index * 2 + 1];
82 static int __gpio_mockup_get(struct gpio_mockup_chip *chip,
83 unsigned int offset)
85 return chip->lines[offset].value;
88 static int gpio_mockup_get(struct gpio_chip *gc, unsigned int offset)
90 struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
91 int val;
93 mutex_lock(&chip->lock);
94 val = __gpio_mockup_get(chip, offset);
95 mutex_unlock(&chip->lock);
97 return val;
100 static int gpio_mockup_get_multiple(struct gpio_chip *gc,
101 unsigned long *mask, unsigned long *bits)
103 struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
104 unsigned int bit, val;
106 mutex_lock(&chip->lock);
107 for_each_set_bit(bit, mask, gc->ngpio) {
108 val = __gpio_mockup_get(chip, bit);
109 __assign_bit(bit, bits, val);
111 mutex_unlock(&chip->lock);
113 return 0;
116 static void __gpio_mockup_set(struct gpio_mockup_chip *chip,
117 unsigned int offset, int value)
119 chip->lines[offset].value = !!value;
122 static void gpio_mockup_set(struct gpio_chip *gc,
123 unsigned int offset, int value)
125 struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
127 mutex_lock(&chip->lock);
128 __gpio_mockup_set(chip, offset, value);
129 mutex_unlock(&chip->lock);
132 static void gpio_mockup_set_multiple(struct gpio_chip *gc,
133 unsigned long *mask, unsigned long *bits)
135 struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
136 unsigned int bit;
138 mutex_lock(&chip->lock);
139 for_each_set_bit(bit, mask, gc->ngpio)
140 __gpio_mockup_set(chip, bit, test_bit(bit, bits));
141 mutex_unlock(&chip->lock);
144 static int gpio_mockup_apply_pull(struct gpio_mockup_chip *chip,
145 unsigned int offset, int value)
147 struct gpio_desc *desc;
148 struct gpio_chip *gc;
149 struct irq_sim *sim;
150 int curr, irq, irq_type;
152 gc = &chip->gc;
153 desc = &gc->gpiodev->descs[offset];
154 sim = &chip->irqsim;
156 mutex_lock(&chip->lock);
158 if (test_bit(FLAG_REQUESTED, &desc->flags) &&
159 !test_bit(FLAG_IS_OUT, &desc->flags)) {
160 curr = __gpio_mockup_get(chip, offset);
161 if (curr == value)
162 goto out;
164 irq = irq_sim_irqnum(sim, offset);
165 irq_type = irq_get_trigger_type(irq);
167 if ((value == 1 && (irq_type & IRQ_TYPE_EDGE_RISING)) ||
168 (value == 0 && (irq_type & IRQ_TYPE_EDGE_FALLING)))
169 irq_sim_fire(sim, offset);
172 /* Change the value unless we're actively driving the line. */
173 if (!test_bit(FLAG_REQUESTED, &desc->flags) ||
174 !test_bit(FLAG_IS_OUT, &desc->flags))
175 __gpio_mockup_set(chip, offset, value);
177 out:
178 chip->lines[offset].pull = value;
179 mutex_unlock(&chip->lock);
180 return 0;
183 static int gpio_mockup_set_config(struct gpio_chip *gc,
184 unsigned int offset, unsigned long config)
186 struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
188 switch (pinconf_to_config_param(config)) {
189 case PIN_CONFIG_BIAS_PULL_UP:
190 return gpio_mockup_apply_pull(chip, offset, 1);
191 case PIN_CONFIG_BIAS_PULL_DOWN:
192 return gpio_mockup_apply_pull(chip, offset, 0);
193 default:
194 break;
196 return -ENOTSUPP;
199 static int gpio_mockup_dirout(struct gpio_chip *gc,
200 unsigned int offset, int value)
202 struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
204 mutex_lock(&chip->lock);
205 chip->lines[offset].dir = GPIO_LINE_DIRECTION_OUT;
206 __gpio_mockup_set(chip, offset, value);
207 mutex_unlock(&chip->lock);
209 return 0;
212 static int gpio_mockup_dirin(struct gpio_chip *gc, unsigned int offset)
214 struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
216 mutex_lock(&chip->lock);
217 chip->lines[offset].dir = GPIO_LINE_DIRECTION_IN;
218 mutex_unlock(&chip->lock);
220 return 0;
223 static int gpio_mockup_get_direction(struct gpio_chip *gc, unsigned int offset)
225 struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
226 int direction;
228 mutex_lock(&chip->lock);
229 direction = chip->lines[offset].dir;
230 mutex_unlock(&chip->lock);
232 return direction;
235 static int gpio_mockup_to_irq(struct gpio_chip *gc, unsigned int offset)
237 struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
239 return irq_sim_irqnum(&chip->irqsim, offset);
242 static void gpio_mockup_free(struct gpio_chip *gc, unsigned int offset)
244 struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
246 __gpio_mockup_set(chip, offset, chip->lines[offset].pull);
249 static ssize_t gpio_mockup_debugfs_read(struct file *file,
250 char __user *usr_buf,
251 size_t size, loff_t *ppos)
253 struct gpio_mockup_dbgfs_private *priv;
254 struct gpio_mockup_chip *chip;
255 struct seq_file *sfile;
256 struct gpio_chip *gc;
257 int val, cnt;
258 char buf[3];
260 if (*ppos != 0)
261 return 0;
263 sfile = file->private_data;
264 priv = sfile->private;
265 chip = priv->chip;
266 gc = &chip->gc;
268 val = gpio_mockup_get(gc, priv->offset);
269 cnt = snprintf(buf, sizeof(buf), "%d\n", val);
271 return simple_read_from_buffer(usr_buf, size, ppos, buf, cnt);
274 static ssize_t gpio_mockup_debugfs_write(struct file *file,
275 const char __user *usr_buf,
276 size_t size, loff_t *ppos)
278 struct gpio_mockup_dbgfs_private *priv;
279 int rv, val;
280 struct seq_file *sfile;
282 if (*ppos != 0)
283 return -EINVAL;
285 rv = kstrtoint_from_user(usr_buf, size, 0, &val);
286 if (rv)
287 return rv;
288 if (val != 0 && val != 1)
289 return -EINVAL;
291 sfile = file->private_data;
292 priv = sfile->private;
293 rv = gpio_mockup_apply_pull(priv->chip, priv->offset, val);
294 if (rv)
295 return rv;
297 return size;
300 static int gpio_mockup_debugfs_open(struct inode *inode, struct file *file)
302 return single_open(file, NULL, inode->i_private);
306 * Each mockup chip is represented by a directory named after the chip's device
307 * name under /sys/kernel/debug/gpio-mockup/. Each line is represented by
308 * a file using the line's offset as the name under the chip's directory.
310 * Reading from the line's file yields the current *value*, writing to the
311 * line's file changes the current *pull*. Default pull for mockup lines is
312 * down.
314 * Examples:
315 * - when a line pulled down is requested in output mode and driven high, its
316 * value will return to 0 once it's released
317 * - when the line is requested in output mode and driven high, writing 0 to
318 * the corresponding debugfs file will change the pull to down but the
319 * reported value will still be 1 until the line is released
320 * - line requested in input mode always reports the same value as its pull
321 * configuration
322 * - when the line is requested in input mode and monitored for events, writing
323 * the same value to the debugfs file will be a noop, while writing the
324 * opposite value will generate a dummy interrupt with an appropriate edge
326 static const struct file_operations gpio_mockup_debugfs_ops = {
327 .owner = THIS_MODULE,
328 .open = gpio_mockup_debugfs_open,
329 .read = gpio_mockup_debugfs_read,
330 .write = gpio_mockup_debugfs_write,
331 .llseek = no_llseek,
332 .release = single_release,
335 static void gpio_mockup_debugfs_setup(struct device *dev,
336 struct gpio_mockup_chip *chip)
338 struct gpio_mockup_dbgfs_private *priv;
339 struct gpio_chip *gc;
340 const char *devname;
341 char *name;
342 int i;
344 gc = &chip->gc;
345 devname = dev_name(&gc->gpiodev->dev);
347 chip->dbg_dir = debugfs_create_dir(devname, gpio_mockup_dbg_dir);
349 for (i = 0; i < gc->ngpio; i++) {
350 name = devm_kasprintf(dev, GFP_KERNEL, "%d", i);
351 if (!name)
352 return;
354 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
355 if (!priv)
356 return;
358 priv->chip = chip;
359 priv->offset = i;
360 priv->desc = &gc->gpiodev->descs[i];
362 debugfs_create_file(name, 0200, chip->dbg_dir, priv,
363 &gpio_mockup_debugfs_ops);
366 return;
369 static int gpio_mockup_name_lines(struct device *dev,
370 struct gpio_mockup_chip *chip)
372 struct gpio_chip *gc = &chip->gc;
373 char **names;
374 int i;
376 names = devm_kcalloc(dev, gc->ngpio, sizeof(char *), GFP_KERNEL);
377 if (!names)
378 return -ENOMEM;
380 for (i = 0; i < gc->ngpio; i++) {
381 names[i] = devm_kasprintf(dev, GFP_KERNEL,
382 "%s-%d", gc->label, i);
383 if (!names[i])
384 return -ENOMEM;
387 gc->names = (const char *const *)names;
389 return 0;
392 static int gpio_mockup_probe(struct platform_device *pdev)
394 struct gpio_mockup_chip *chip;
395 struct gpio_chip *gc;
396 struct device *dev;
397 const char *name;
398 int rv, base, i;
399 u16 ngpio;
401 dev = &pdev->dev;
403 rv = device_property_read_u32(dev, "gpio-base", &base);
404 if (rv)
405 base = -1;
407 rv = device_property_read_u16(dev, "nr-gpios", &ngpio);
408 if (rv)
409 return rv;
411 rv = device_property_read_string(dev, "chip-name", &name);
412 if (rv)
413 name = NULL;
415 chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL);
416 if (!chip)
417 return -ENOMEM;
419 if (!name) {
420 name = devm_kasprintf(dev, GFP_KERNEL,
421 "%s-%c", pdev->name, pdev->id + 'A');
422 if (!name)
423 return -ENOMEM;
426 mutex_init(&chip->lock);
428 gc = &chip->gc;
429 gc->base = base;
430 gc->ngpio = ngpio;
431 gc->label = name;
432 gc->owner = THIS_MODULE;
433 gc->parent = dev;
434 gc->get = gpio_mockup_get;
435 gc->set = gpio_mockup_set;
436 gc->get_multiple = gpio_mockup_get_multiple;
437 gc->set_multiple = gpio_mockup_set_multiple;
438 gc->direction_output = gpio_mockup_dirout;
439 gc->direction_input = gpio_mockup_dirin;
440 gc->get_direction = gpio_mockup_get_direction;
441 gc->set_config = gpio_mockup_set_config;
442 gc->to_irq = gpio_mockup_to_irq;
443 gc->free = gpio_mockup_free;
445 chip->lines = devm_kcalloc(dev, gc->ngpio,
446 sizeof(*chip->lines), GFP_KERNEL);
447 if (!chip->lines)
448 return -ENOMEM;
450 for (i = 0; i < gc->ngpio; i++)
451 chip->lines[i].dir = GPIO_LINE_DIRECTION_IN;
453 if (device_property_read_bool(dev, "named-gpio-lines")) {
454 rv = gpio_mockup_name_lines(dev, chip);
455 if (rv)
456 return rv;
459 rv = devm_irq_sim_init(dev, &chip->irqsim, gc->ngpio);
460 if (rv < 0)
461 return rv;
463 rv = devm_gpiochip_add_data(dev, &chip->gc, chip);
464 if (rv)
465 return rv;
467 gpio_mockup_debugfs_setup(dev, chip);
469 return 0;
472 static struct platform_driver gpio_mockup_driver = {
473 .driver = {
474 .name = GPIO_MOCKUP_NAME,
476 .probe = gpio_mockup_probe,
479 static struct platform_device *gpio_mockup_pdevs[GPIO_MOCKUP_MAX_GC];
481 static void gpio_mockup_unregister_pdevs(void)
483 struct platform_device *pdev;
484 int i;
486 for (i = 0; i < GPIO_MOCKUP_MAX_GC; i++) {
487 pdev = gpio_mockup_pdevs[i];
489 if (pdev)
490 platform_device_unregister(pdev);
494 static int __init gpio_mockup_init(void)
496 struct property_entry properties[GPIO_MOCKUP_MAX_PROP];
497 int i, prop, num_chips, err = 0, base;
498 struct platform_device_info pdevinfo;
499 struct platform_device *pdev;
500 u16 ngpio;
502 if ((gpio_mockup_num_ranges < 2) ||
503 (gpio_mockup_num_ranges % 2) ||
504 (gpio_mockup_num_ranges > GPIO_MOCKUP_MAX_RANGES))
505 return -EINVAL;
507 /* Each chip is described by two values. */
508 num_chips = gpio_mockup_num_ranges / 2;
511 * The second value in the <base GPIO - number of GPIOS> pair must
512 * always be greater than 0.
514 for (i = 0; i < num_chips; i++) {
515 if (gpio_mockup_range_ngpio(i) < 0)
516 return -EINVAL;
519 gpio_mockup_dbg_dir = debugfs_create_dir("gpio-mockup", NULL);
521 err = platform_driver_register(&gpio_mockup_driver);
522 if (err) {
523 gpio_mockup_err("error registering platform driver\n");
524 return err;
527 for (i = 0; i < num_chips; i++) {
528 memset(properties, 0, sizeof(properties));
529 memset(&pdevinfo, 0, sizeof(pdevinfo));
530 prop = 0;
532 base = gpio_mockup_range_base(i);
533 if (base >= 0)
534 properties[prop++] = PROPERTY_ENTRY_U32("gpio-base",
535 base);
537 ngpio = base < 0 ? gpio_mockup_range_ngpio(i)
538 : gpio_mockup_range_ngpio(i) - base;
539 properties[prop++] = PROPERTY_ENTRY_U16("nr-gpios", ngpio);
541 if (gpio_mockup_named_lines)
542 properties[prop++] = PROPERTY_ENTRY_BOOL(
543 "named-gpio-lines");
545 pdevinfo.name = GPIO_MOCKUP_NAME;
546 pdevinfo.id = i;
547 pdevinfo.properties = properties;
549 pdev = platform_device_register_full(&pdevinfo);
550 if (IS_ERR(pdev)) {
551 gpio_mockup_err("error registering device");
552 platform_driver_unregister(&gpio_mockup_driver);
553 gpio_mockup_unregister_pdevs();
554 return PTR_ERR(pdev);
557 gpio_mockup_pdevs[i] = pdev;
560 return 0;
563 static void __exit gpio_mockup_exit(void)
565 debugfs_remove_recursive(gpio_mockup_dbg_dir);
566 platform_driver_unregister(&gpio_mockup_driver);
567 gpio_mockup_unregister_pdevs();
570 module_init(gpio_mockup_init);
571 module_exit(gpio_mockup_exit);
573 MODULE_AUTHOR("Kamlakant Patel <kamlakant.patel@broadcom.com>");
574 MODULE_AUTHOR("Bamvor Jian Zhang <bamv2005@gmail.com>");
575 MODULE_AUTHOR("Bartosz Golaszewski <brgl@bgdev.pl>");
576 MODULE_DESCRIPTION("GPIO Testing driver");
577 MODULE_LICENSE("GPL v2");