Lines Matching +full:gpio +full:- +full:dir
1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * GPIO Testing Device Driver
6 * Copyright (C) 2015-2016 Bamvor Jian Zhang <bamv2005@gmail.com>
15 #include <linux/gpio/driver.h>
31 * We're storing two values per chip: the GPIO base and the number
32 * of GPIO lines.
39 * struct gpio_pin_status - structure describing a GPIO status
40 * @dir: Configures direction of gpio as "in" or "out"
41 * @value: Configures status of the gpio as 0(low) or 1(high)
42 * @pull: Configures the current pull of the GPIO as 0 (pull-down) or
43 * 1 (pull-up)
44 * @requested: Request status of this GPIO
47 int dir; member
89 return chip->lines[offset].value; in __gpio_mockup_get()
97 scoped_guard(mutex, &chip->lock) in gpio_mockup_get()
109 scoped_guard(mutex, &chip->lock) { in gpio_mockup_get_multiple()
110 for_each_set_bit(bit, mask, gc->ngpio) { in gpio_mockup_get_multiple()
122 chip->lines[offset].value = !!value; in __gpio_mockup_set()
130 guard(mutex)(&chip->lock); in gpio_mockup_set()
141 guard(mutex)(&chip->lock); in gpio_mockup_set_multiple()
143 for_each_set_bit(bit, mask, gc->ngpio) in gpio_mockup_set_multiple()
150 struct gpio_mockup_line_status *line = &chip->lines[offset]; in gpio_mockup_apply_pull()
153 guard(mutex)(&chip->lock); in gpio_mockup_apply_pull()
155 if (line->requested && line->dir == GPIO_LINE_DIRECTION_IN) { in gpio_mockup_apply_pull()
160 irq = irq_find_mapping(chip->irq_sim_domain, offset); in gpio_mockup_apply_pull()
163 * This is fine - it just means, nobody is listening in gpio_mockup_apply_pull()
183 if (!line->requested || line->dir == GPIO_LINE_DIRECTION_IN) in gpio_mockup_apply_pull()
187 chip->lines[offset].pull = value; in gpio_mockup_apply_pull()
204 return -ENOTSUPP; in gpio_mockup_set_config()
212 scoped_guard(mutex, &chip->lock) { in gpio_mockup_dirout()
213 chip->lines[offset].dir = GPIO_LINE_DIRECTION_OUT; in gpio_mockup_dirout()
224 scoped_guard(mutex, &chip->lock) in gpio_mockup_dirin()
225 chip->lines[offset].dir = GPIO_LINE_DIRECTION_IN; in gpio_mockup_dirin()
235 scoped_guard(mutex, &chip->lock) in gpio_mockup_get_direction()
236 direction = chip->lines[offset].dir; in gpio_mockup_get_direction()
245 return irq_create_mapping(chip->irq_sim_domain, offset); in gpio_mockup_to_irq()
252 scoped_guard(mutex, &chip->lock) in gpio_mockup_request()
253 chip->lines[offset].requested = true; in gpio_mockup_request()
262 guard(mutex)(&chip->lock); in gpio_mockup_free()
264 chip->lines[offset].requested = false; in gpio_mockup_free()
265 __gpio_mockup_set(chip, offset, chip->lines[offset].pull); in gpio_mockup_free()
282 sfile = file->private_data; in gpio_mockup_debugfs_read()
283 priv = sfile->private; in gpio_mockup_debugfs_read()
284 chip = priv->chip; in gpio_mockup_debugfs_read()
285 gc = &chip->gc; in gpio_mockup_debugfs_read()
287 val = gpio_mockup_get(gc, priv->offset); in gpio_mockup_debugfs_read()
302 return -EINVAL; in gpio_mockup_debugfs_write()
308 return -EINVAL; in gpio_mockup_debugfs_write()
310 sfile = file->private_data; in gpio_mockup_debugfs_write()
311 priv = sfile->private; in gpio_mockup_debugfs_write()
312 rv = gpio_mockup_apply_pull(priv->chip, priv->offset, val); in gpio_mockup_debugfs_write()
321 return single_open(file, NULL, inode->i_private); in gpio_mockup_debugfs_open()
326 * name under /sys/kernel/debug/gpio-mockup/. Each line is represented by
334 * - when a line pulled down is requested in output mode and driven high, its
336 * - when the line is requested in output mode and driven high, writing 0 to
339 * - line requested in input mode always reports the same value as its pull
341 * - when the line is requested in input mode and monitored for events, writing
362 gc = &chip->gc; in gpio_mockup_debugfs_setup()
365 * There can only be a single GPIO device per platform device in in gpio_mockup_debugfs_setup()
366 * gpio-mockup so using device_find_any_child() is OK. in gpio_mockup_debugfs_setup()
373 chip->dbg_dir = debugfs_create_dir(devname, gpio_mockup_dbg_dir); in gpio_mockup_debugfs_setup()
375 for (i = 0; i < gc->ngpio; i++) { in gpio_mockup_debugfs_setup()
384 priv->chip = chip; in gpio_mockup_debugfs_setup()
385 priv->offset = i; in gpio_mockup_debugfs_setup()
387 debugfs_create_file(name, 0600, chip->dbg_dir, priv, in gpio_mockup_debugfs_setup()
396 debugfs_remove_recursive(chip->dbg_dir); in gpio_mockup_debugfs_cleanup()
402 struct gpio_chip *gc = &chip->gc; in gpio_mockup_dispose_mappings()
405 for (i = 0; i < gc->ngpio; i++) { in gpio_mockup_dispose_mappings()
406 irq = irq_find_mapping(chip->irq_sim_domain, i); in gpio_mockup_dispose_mappings()
421 dev = &pdev->dev; in gpio_mockup_probe()
423 rv = device_property_read_u32(dev, "gpio-base", &base); in gpio_mockup_probe()
425 base = -1; in gpio_mockup_probe()
427 rv = device_property_read_u16(dev, "nr-gpios", &ngpio); in gpio_mockup_probe()
431 rv = device_property_read_string(dev, "chip-label", &name); in gpio_mockup_probe()
437 return -ENOMEM; in gpio_mockup_probe()
439 mutex_init(&chip->lock); in gpio_mockup_probe()
441 gc = &chip->gc; in gpio_mockup_probe()
442 gc->base = base; in gpio_mockup_probe()
443 gc->ngpio = ngpio; in gpio_mockup_probe()
444 gc->label = name; in gpio_mockup_probe()
445 gc->owner = THIS_MODULE; in gpio_mockup_probe()
446 gc->parent = dev; in gpio_mockup_probe()
447 gc->get = gpio_mockup_get; in gpio_mockup_probe()
448 gc->set = gpio_mockup_set; in gpio_mockup_probe()
449 gc->get_multiple = gpio_mockup_get_multiple; in gpio_mockup_probe()
450 gc->set_multiple = gpio_mockup_set_multiple; in gpio_mockup_probe()
451 gc->direction_output = gpio_mockup_dirout; in gpio_mockup_probe()
452 gc->direction_input = gpio_mockup_dirin; in gpio_mockup_probe()
453 gc->get_direction = gpio_mockup_get_direction; in gpio_mockup_probe()
454 gc->set_config = gpio_mockup_set_config; in gpio_mockup_probe()
455 gc->to_irq = gpio_mockup_to_irq; in gpio_mockup_probe()
456 gc->request = gpio_mockup_request; in gpio_mockup_probe()
457 gc->free = gpio_mockup_free; in gpio_mockup_probe()
459 chip->lines = devm_kcalloc(dev, gc->ngpio, in gpio_mockup_probe()
460 sizeof(*chip->lines), GFP_KERNEL); in gpio_mockup_probe()
461 if (!chip->lines) in gpio_mockup_probe()
462 return -ENOMEM; in gpio_mockup_probe()
464 for (i = 0; i < gc->ngpio; i++) in gpio_mockup_probe()
465 chip->lines[i].dir = GPIO_LINE_DIRECTION_IN; in gpio_mockup_probe()
467 chip->irq_sim_domain = devm_irq_domain_create_sim(dev, NULL, in gpio_mockup_probe()
468 gc->ngpio); in gpio_mockup_probe()
469 if (IS_ERR(chip->irq_sim_domain)) in gpio_mockup_probe()
470 return PTR_ERR(chip->irq_sim_domain); in gpio_mockup_probe()
476 rv = devm_gpiochip_add_data(dev, &chip->gc, chip); in gpio_mockup_probe()
486 { .compatible = "gpio-mockup", },
493 .name = "gpio-mockup",
512 fwnode = dev_fwnode(&pdev->dev); in gpio_mockup_unregister_pdevs()
532 snprintf(chip_label, sizeof(chip_label), "gpio-mockup-%c", idx + 'A'); in gpio_mockup_register_chip()
533 properties[prop++] = PROPERTY_ENTRY_STRING("chip-label", chip_label); in gpio_mockup_register_chip()
537 properties[prop++] = PROPERTY_ENTRY_U32("gpio-base", base); in gpio_mockup_register_chip()
540 : gpio_mockup_range_ngpio(idx) - base; in gpio_mockup_register_chip()
541 properties[prop++] = PROPERTY_ENTRY_U16("nr-gpios", ngpio); in gpio_mockup_register_chip()
546 return -ENOMEM; in gpio_mockup_register_chip()
549 "gpio-line-names", line_names, ngpio); in gpio_mockup_register_chip()
558 pdevinfo.name = "gpio-mockup"; in gpio_mockup_register_chip()
581 return -EINVAL; in gpio_mockup_init()
587 * The second value in the <base GPIO - number of GPIOS> pair must in gpio_mockup_init()
592 return -EINVAL; in gpio_mockup_init()
595 gpio_mockup_dbg_dir = debugfs_create_dir("gpio-mockup", NULL); in gpio_mockup_init()
630 MODULE_DESCRIPTION("GPIO Testing driver");