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