^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * GPIO Testing Device Driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) 2014 Kamlakant Patel <kamlakant.patel@broadcom.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Copyright (C) 2015-2016 Bamvor Jian Zhang <bamv2005@gmail.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Copyright (C) 2017 Bartosz Golaszewski <brgl@bgdev.pl>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/debugfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/gpio/driver.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/interrupt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/irq.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/irq_sim.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/irqdomain.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/property.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include <linux/string_helpers.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #include "gpiolib.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #define GPIO_MOCKUP_MAX_GC 10
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) * We're storing two values per chip: the GPIO base and the number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) * of GPIO lines.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) #define GPIO_MOCKUP_MAX_RANGES (GPIO_MOCKUP_MAX_GC * 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) /* Maximum of four properties + the sentinel. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) #define GPIO_MOCKUP_MAX_PROP 5
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) * struct gpio_pin_status - structure describing a GPIO status
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) * @dir: Configures direction of gpio as "in" or "out"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) * @value: Configures status of the gpio as 0(low) or 1(high)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) struct gpio_mockup_line_status {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) int dir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) int value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) int pull;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) struct gpio_mockup_chip {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) struct gpio_chip gc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) struct gpio_mockup_line_status *lines;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) struct irq_domain *irq_sim_domain;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) struct dentry *dbg_dir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) struct mutex lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) struct gpio_mockup_dbgfs_private {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) struct gpio_mockup_chip *chip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) struct gpio_desc *desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) unsigned int offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) static int gpio_mockup_ranges[GPIO_MOCKUP_MAX_RANGES];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) static int gpio_mockup_num_ranges;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) module_param_array(gpio_mockup_ranges, int, &gpio_mockup_num_ranges, 0400);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) static bool gpio_mockup_named_lines;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) module_param_named(gpio_mockup_named_lines,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) gpio_mockup_named_lines, bool, 0400);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) static struct dentry *gpio_mockup_dbg_dir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) static int gpio_mockup_range_base(unsigned int index)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) return gpio_mockup_ranges[index * 2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) static int gpio_mockup_range_ngpio(unsigned int index)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) return gpio_mockup_ranges[index * 2 + 1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) static int __gpio_mockup_get(struct gpio_mockup_chip *chip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) unsigned int offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) return chip->lines[offset].value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) static int gpio_mockup_get(struct gpio_chip *gc, unsigned int offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) int val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) mutex_lock(&chip->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) val = __gpio_mockup_get(chip, offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) mutex_unlock(&chip->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) return val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) static int gpio_mockup_get_multiple(struct gpio_chip *gc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) unsigned long *mask, unsigned long *bits)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) unsigned int bit, val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) mutex_lock(&chip->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) for_each_set_bit(bit, mask, gc->ngpio) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) val = __gpio_mockup_get(chip, bit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) __assign_bit(bit, bits, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) mutex_unlock(&chip->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) static void __gpio_mockup_set(struct gpio_mockup_chip *chip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) unsigned int offset, int value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) chip->lines[offset].value = !!value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) static void gpio_mockup_set(struct gpio_chip *gc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) unsigned int offset, int value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) mutex_lock(&chip->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) __gpio_mockup_set(chip, offset, value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) mutex_unlock(&chip->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) static void gpio_mockup_set_multiple(struct gpio_chip *gc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) unsigned long *mask, unsigned long *bits)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) unsigned int bit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) mutex_lock(&chip->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) for_each_set_bit(bit, mask, gc->ngpio)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) __gpio_mockup_set(chip, bit, test_bit(bit, bits));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) mutex_unlock(&chip->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) static int gpio_mockup_apply_pull(struct gpio_mockup_chip *chip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) unsigned int offset, int value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) int curr, irq, irq_type, ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) struct gpio_desc *desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) struct gpio_chip *gc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) gc = &chip->gc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) desc = &gc->gpiodev->descs[offset];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) mutex_lock(&chip->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) if (test_bit(FLAG_REQUESTED, &desc->flags) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) !test_bit(FLAG_IS_OUT, &desc->flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) curr = __gpio_mockup_get(chip, offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) if (curr == value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) irq = irq_find_mapping(chip->irq_sim_domain, offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) if (!irq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) * This is fine - it just means, nobody is listening
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) * for interrupts on this line, otherwise
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) * irq_create_mapping() would have been called from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) * the to_irq() callback.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) goto set_value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) irq_type = irq_get_trigger_type(irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) if ((value == 1 && (irq_type & IRQ_TYPE_EDGE_RISING)) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) (value == 0 && (irq_type & IRQ_TYPE_EDGE_FALLING))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) ret = irq_set_irqchip_state(irq, IRQCHIP_STATE_PENDING,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) set_value:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) /* Change the value unless we're actively driving the line. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) if (!test_bit(FLAG_REQUESTED, &desc->flags) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) !test_bit(FLAG_IS_OUT, &desc->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) __gpio_mockup_set(chip, offset, value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) chip->lines[offset].pull = value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) mutex_unlock(&chip->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) static int gpio_mockup_set_config(struct gpio_chip *gc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) unsigned int offset, unsigned long config)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) switch (pinconf_to_config_param(config)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) case PIN_CONFIG_BIAS_PULL_UP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) return gpio_mockup_apply_pull(chip, offset, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) case PIN_CONFIG_BIAS_PULL_DOWN:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) return gpio_mockup_apply_pull(chip, offset, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) return -ENOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) static int gpio_mockup_dirout(struct gpio_chip *gc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) unsigned int offset, int value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) mutex_lock(&chip->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) chip->lines[offset].dir = GPIO_LINE_DIRECTION_OUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) __gpio_mockup_set(chip, offset, value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) mutex_unlock(&chip->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) static int gpio_mockup_dirin(struct gpio_chip *gc, unsigned int offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) mutex_lock(&chip->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) chip->lines[offset].dir = GPIO_LINE_DIRECTION_IN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) mutex_unlock(&chip->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) static int gpio_mockup_get_direction(struct gpio_chip *gc, unsigned int offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) int direction;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) mutex_lock(&chip->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) direction = chip->lines[offset].dir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) mutex_unlock(&chip->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) return direction;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) static int gpio_mockup_to_irq(struct gpio_chip *gc, unsigned int offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) return irq_create_mapping(chip->irq_sim_domain, offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) static void gpio_mockup_free(struct gpio_chip *gc, unsigned int offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) __gpio_mockup_set(chip, offset, chip->lines[offset].pull);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) static ssize_t gpio_mockup_debugfs_read(struct file *file,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) char __user *usr_buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) size_t size, loff_t *ppos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) struct gpio_mockup_dbgfs_private *priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) struct gpio_mockup_chip *chip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) struct seq_file *sfile;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) struct gpio_chip *gc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) int val, cnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) char buf[3];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) if (*ppos != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) sfile = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) priv = sfile->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) chip = priv->chip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) gc = &chip->gc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) val = gpio_mockup_get(gc, priv->offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) cnt = snprintf(buf, sizeof(buf), "%d\n", val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) return simple_read_from_buffer(usr_buf, size, ppos, buf, cnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) static ssize_t gpio_mockup_debugfs_write(struct file *file,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) const char __user *usr_buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) size_t size, loff_t *ppos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) struct gpio_mockup_dbgfs_private *priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) int rv, val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) struct seq_file *sfile;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) if (*ppos != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) rv = kstrtoint_from_user(usr_buf, size, 0, &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) if (rv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) return rv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) if (val != 0 && val != 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) sfile = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) priv = sfile->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) rv = gpio_mockup_apply_pull(priv->chip, priv->offset, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) if (rv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) return rv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) return size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) static int gpio_mockup_debugfs_open(struct inode *inode, struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) return single_open(file, NULL, inode->i_private);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) * Each mockup chip is represented by a directory named after the chip's device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) * name under /sys/kernel/debug/gpio-mockup/. Each line is represented by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) * a file using the line's offset as the name under the chip's directory.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) * Reading from the line's file yields the current *value*, writing to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) * line's file changes the current *pull*. Default pull for mockup lines is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) * down.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) * Examples:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) * - when a line pulled down is requested in output mode and driven high, its
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) * value will return to 0 once it's released
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) * - when the line is requested in output mode and driven high, writing 0 to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) * the corresponding debugfs file will change the pull to down but the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) * reported value will still be 1 until the line is released
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) * - line requested in input mode always reports the same value as its pull
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) * configuration
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) * - when the line is requested in input mode and monitored for events, writing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) * the same value to the debugfs file will be a noop, while writing the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) * opposite value will generate a dummy interrupt with an appropriate edge
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) static const struct file_operations gpio_mockup_debugfs_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) .owner = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) .open = gpio_mockup_debugfs_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) .read = gpio_mockup_debugfs_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) .write = gpio_mockup_debugfs_write,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) .llseek = no_llseek,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) .release = single_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) static void gpio_mockup_debugfs_setup(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) struct gpio_mockup_chip *chip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) struct gpio_mockup_dbgfs_private *priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) struct gpio_chip *gc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) const char *devname;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) char *name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) gc = &chip->gc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) devname = dev_name(&gc->gpiodev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) chip->dbg_dir = debugfs_create_dir(devname, gpio_mockup_dbg_dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) for (i = 0; i < gc->ngpio; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) name = devm_kasprintf(dev, GFP_KERNEL, "%d", i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) if (!name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) if (!priv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) priv->chip = chip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) priv->offset = i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) priv->desc = &gc->gpiodev->descs[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) debugfs_create_file(name, 0200, chip->dbg_dir, priv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) &gpio_mockup_debugfs_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) static void gpio_mockup_dispose_mappings(void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) struct gpio_mockup_chip *chip = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) struct gpio_chip *gc = &chip->gc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) int i, irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) for (i = 0; i < gc->ngpio; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) irq = irq_find_mapping(chip->irq_sim_domain, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) if (irq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) irq_dispose_mapping(irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) static int gpio_mockup_probe(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) struct gpio_mockup_chip *chip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) struct gpio_chip *gc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) struct device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) const char *name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) int rv, base, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) u16 ngpio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) dev = &pdev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) rv = device_property_read_u32(dev, "gpio-base", &base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) if (rv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) base = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) rv = device_property_read_u16(dev, "nr-gpios", &ngpio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) if (rv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) return rv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) rv = device_property_read_string(dev, "chip-label", &name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) if (rv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) name = dev_name(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) if (!chip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) mutex_init(&chip->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) gc = &chip->gc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) gc->base = base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) gc->ngpio = ngpio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) gc->label = name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) gc->owner = THIS_MODULE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) gc->parent = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) gc->get = gpio_mockup_get;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) gc->set = gpio_mockup_set;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) gc->get_multiple = gpio_mockup_get_multiple;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) gc->set_multiple = gpio_mockup_set_multiple;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) gc->direction_output = gpio_mockup_dirout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) gc->direction_input = gpio_mockup_dirin;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) gc->get_direction = gpio_mockup_get_direction;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) gc->set_config = gpio_mockup_set_config;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) gc->to_irq = gpio_mockup_to_irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) gc->free = gpio_mockup_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) chip->lines = devm_kcalloc(dev, gc->ngpio,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) sizeof(*chip->lines), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) if (!chip->lines)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) for (i = 0; i < gc->ngpio; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) chip->lines[i].dir = GPIO_LINE_DIRECTION_IN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) chip->irq_sim_domain = devm_irq_domain_create_sim(dev, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) gc->ngpio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) if (IS_ERR(chip->irq_sim_domain))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) return PTR_ERR(chip->irq_sim_domain);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) rv = devm_add_action_or_reset(dev, gpio_mockup_dispose_mappings, chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) if (rv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) return rv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) rv = devm_gpiochip_add_data(dev, &chip->gc, chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) if (rv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) return rv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) gpio_mockup_debugfs_setup(dev, chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) static struct platform_driver gpio_mockup_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) .driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) .name = "gpio-mockup",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) .probe = gpio_mockup_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) static struct platform_device *gpio_mockup_pdevs[GPIO_MOCKUP_MAX_GC];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) static void gpio_mockup_unregister_pdevs(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) struct platform_device *pdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) for (i = 0; i < GPIO_MOCKUP_MAX_GC; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) pdev = gpio_mockup_pdevs[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) if (pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) platform_device_unregister(pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) static __init char **gpio_mockup_make_line_names(const char *label,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) unsigned int num_lines)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) char **names;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) names = kcalloc(num_lines + 1, sizeof(char *), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) if (!names)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) for (i = 0; i < num_lines; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) names[i] = kasprintf(GFP_KERNEL, "%s-%u", label, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) if (!names[i]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) kfree_strarray(names, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) return names;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) static int __init gpio_mockup_register_chip(int idx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) struct property_entry properties[GPIO_MOCKUP_MAX_PROP];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) struct platform_device_info pdevinfo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) struct platform_device *pdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) char **line_names = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) char chip_label[32];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) int prop = 0, base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) u16 ngpio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) memset(properties, 0, sizeof(properties));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) memset(&pdevinfo, 0, sizeof(pdevinfo));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) snprintf(chip_label, sizeof(chip_label), "gpio-mockup-%c", idx + 'A');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) properties[prop++] = PROPERTY_ENTRY_STRING("chip-label", chip_label);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) base = gpio_mockup_range_base(idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) if (base >= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) properties[prop++] = PROPERTY_ENTRY_U32("gpio-base", base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) ngpio = base < 0 ? gpio_mockup_range_ngpio(idx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) : gpio_mockup_range_ngpio(idx) - base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) properties[prop++] = PROPERTY_ENTRY_U16("nr-gpios", ngpio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) if (gpio_mockup_named_lines) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) line_names = gpio_mockup_make_line_names(chip_label, ngpio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) if (!line_names)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) properties[prop++] = PROPERTY_ENTRY_STRING_ARRAY_LEN(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) "gpio-line-names", line_names, ngpio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) pdevinfo.name = "gpio-mockup";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) pdevinfo.id = idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) pdevinfo.properties = properties;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) pdev = platform_device_register_full(&pdevinfo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) kfree_strarray(line_names, ngpio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) if (IS_ERR(pdev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) pr_err("error registering device");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) return PTR_ERR(pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) gpio_mockup_pdevs[idx] = pdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) static int __init gpio_mockup_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) int i, num_chips, err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) if ((gpio_mockup_num_ranges < 2) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) (gpio_mockup_num_ranges % 2) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) (gpio_mockup_num_ranges > GPIO_MOCKUP_MAX_RANGES))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) /* Each chip is described by two values. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) num_chips = gpio_mockup_num_ranges / 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) * The second value in the <base GPIO - number of GPIOS> pair must
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) * always be greater than 0.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) for (i = 0; i < num_chips; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) if (gpio_mockup_range_ngpio(i) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) gpio_mockup_dbg_dir = debugfs_create_dir("gpio-mockup", NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) err = platform_driver_register(&gpio_mockup_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) pr_err("error registering platform driver\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) debugfs_remove_recursive(gpio_mockup_dbg_dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) for (i = 0; i < num_chips; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) err = gpio_mockup_register_chip(i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) platform_driver_unregister(&gpio_mockup_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) gpio_mockup_unregister_pdevs();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) debugfs_remove_recursive(gpio_mockup_dbg_dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) static void __exit gpio_mockup_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) debugfs_remove_recursive(gpio_mockup_dbg_dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) platform_driver_unregister(&gpio_mockup_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) gpio_mockup_unregister_pdevs();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) module_init(gpio_mockup_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) module_exit(gpio_mockup_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) MODULE_AUTHOR("Kamlakant Patel <kamlakant.patel@broadcom.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) MODULE_AUTHOR("Bamvor Jian Zhang <bamv2005@gmail.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) MODULE_AUTHOR("Bartosz Golaszewski <brgl@bgdev.pl>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) MODULE_DESCRIPTION("GPIO Testing driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) MODULE_LICENSE("GPL v2");