Bartosz Golaszewski | 058de34 | 2018-04-26 22:12:36 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
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 | |
| 10 | #include <linux/init.h> |
| 11 | #include <linux/module.h> |
| 12 | #include <linux/gpio/driver.h> |
Bartosz Golaszewski | 9202ba2 | 2017-02-06 15:11:08 +0100 | [diff] [blame] | 13 | #include <linux/gpio/consumer.h> |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 14 | #include <linux/platform_device.h> |
Bartosz Golaszewski | 8a68ea0 | 2017-02-06 13:10:37 +0100 | [diff] [blame] | 15 | #include <linux/slab.h> |
Bartosz Golaszewski | e2ff740 | 2017-02-06 15:11:07 +0100 | [diff] [blame] | 16 | #include <linux/interrupt.h> |
| 17 | #include <linux/irq.h> |
Bartosz Golaszewski | b4c495f | 2017-08-14 13:20:56 +0200 | [diff] [blame] | 18 | #include <linux/irq_sim.h> |
Bartosz Golaszewski | 9202ba2 | 2017-02-06 15:11:08 +0100 | [diff] [blame] | 19 | #include <linux/debugfs.h> |
| 20 | #include <linux/uaccess.h> |
Bartosz Golaszewski | 3ea47b4 | 2018-09-25 13:29:46 +0200 | [diff] [blame] | 21 | #include <linux/property.h> |
Bartosz Golaszewski | 9202ba2 | 2017-02-06 15:11:08 +0100 | [diff] [blame] | 22 | |
| 23 | #include "gpiolib.h" |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 24 | |
Bartosz Golaszewski | ca409160 | 2017-02-06 13:10:35 +0100 | [diff] [blame] | 25 | #define GPIO_MOCKUP_NAME "gpio-mockup" |
Bartosz Golaszewski | b9576d0 | 2017-11-27 11:48:42 +0100 | [diff] [blame] | 26 | #define GPIO_MOCKUP_MAX_GC 10 |
Bartosz Golaszewski | b652336 | 2017-06-09 13:41:28 +0200 | [diff] [blame] | 27 | /* |
| 28 | * We're storing two values per chip: the GPIO base and the number |
| 29 | * of GPIO lines. |
| 30 | */ |
| 31 | #define GPIO_MOCKUP_MAX_RANGES (GPIO_MOCKUP_MAX_GC * 2) |
Bartosz Golaszewski | 3ea47b4 | 2018-09-25 13:29:46 +0200 | [diff] [blame] | 32 | /* Maximum of three properties + the sentinel. */ |
| 33 | #define GPIO_MOCKUP_MAX_PROP 4 |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 34 | |
Bartosz Golaszewski | f1bec99 | 2017-11-27 11:48:41 +0100 | [diff] [blame] | 35 | #define gpio_mockup_err(...) pr_err(GPIO_MOCKUP_NAME ": " __VA_ARGS__) |
| 36 | |
Bartosz Golaszewski | ca409160 | 2017-02-06 13:10:35 +0100 | [diff] [blame] | 37 | enum { |
Bartosz Golaszewski | c650c00 | 2017-05-25 10:33:39 +0200 | [diff] [blame] | 38 | GPIO_MOCKUP_DIR_OUT = 0, |
| 39 | GPIO_MOCKUP_DIR_IN = 1, |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 40 | }; |
| 41 | |
| 42 | /* |
| 43 | * struct gpio_pin_status - structure describing a GPIO status |
| 44 | * @dir: Configures direction of gpio as "in" or "out", 0=in, 1=out |
| 45 | * @value: Configures status of the gpio as 0(low) or 1(high) |
| 46 | */ |
Bartosz Golaszewski | ca409160 | 2017-02-06 13:10:35 +0100 | [diff] [blame] | 47 | struct gpio_mockup_line_status { |
| 48 | int dir; |
Bartosz Golaszewski | 5b7908c | 2017-11-27 11:48:46 +0100 | [diff] [blame] | 49 | int value; |
Bartosz Golaszewski | e2ff740 | 2017-02-06 15:11:07 +0100 | [diff] [blame] | 50 | }; |
| 51 | |
Bartosz Golaszewski | ca409160 | 2017-02-06 13:10:35 +0100 | [diff] [blame] | 52 | struct gpio_mockup_chip { |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 53 | struct gpio_chip gc; |
Bartosz Golaszewski | ca409160 | 2017-02-06 13:10:35 +0100 | [diff] [blame] | 54 | struct gpio_mockup_line_status *lines; |
Bartosz Golaszewski | b4c495f | 2017-08-14 13:20:56 +0200 | [diff] [blame] | 55 | struct irq_sim irqsim; |
Bartosz Golaszewski | 9202ba2 | 2017-02-06 15:11:08 +0100 | [diff] [blame] | 56 | struct dentry *dbg_dir; |
| 57 | }; |
| 58 | |
| 59 | struct gpio_mockup_dbgfs_private { |
| 60 | struct gpio_mockup_chip *chip; |
| 61 | struct gpio_desc *desc; |
| 62 | int offset; |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 63 | }; |
| 64 | |
Bartosz Golaszewski | b652336 | 2017-06-09 13:41:28 +0200 | [diff] [blame] | 65 | static int gpio_mockup_ranges[GPIO_MOCKUP_MAX_RANGES]; |
Bartosz Golaszewski | e63a006 | 2017-11-27 11:48:53 +0100 | [diff] [blame] | 66 | static int gpio_mockup_num_ranges; |
| 67 | 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] | 68 | |
Bartosz Golaszewski | 8a68ea0 | 2017-02-06 13:10:37 +0100 | [diff] [blame] | 69 | static bool gpio_mockup_named_lines; |
| 70 | module_param_named(gpio_mockup_named_lines, |
| 71 | gpio_mockup_named_lines, bool, 0400); |
| 72 | |
Bartosz Golaszewski | 9202ba2 | 2017-02-06 15:11:08 +0100 | [diff] [blame] | 73 | static struct dentry *gpio_mockup_dbg_dir; |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 74 | |
Bartosz Golaszewski | cd9835f | 2017-11-27 11:48:54 +0100 | [diff] [blame] | 75 | static int gpio_mockup_range_base(unsigned int index) |
| 76 | { |
| 77 | return gpio_mockup_ranges[index * 2]; |
| 78 | } |
| 79 | |
| 80 | static int gpio_mockup_range_ngpio(unsigned int index) |
| 81 | { |
| 82 | return gpio_mockup_ranges[index * 2 + 1]; |
| 83 | } |
| 84 | |
Bartosz Golaszewski | ca409160 | 2017-02-06 13:10:35 +0100 | [diff] [blame] | 85 | static int gpio_mockup_get(struct gpio_chip *gc, unsigned int offset) |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 86 | { |
Bartosz Golaszewski | ca409160 | 2017-02-06 13:10:35 +0100 | [diff] [blame] | 87 | struct gpio_mockup_chip *chip = gpiochip_get_data(gc); |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 88 | |
Bartosz Golaszewski | ca409160 | 2017-02-06 13:10:35 +0100 | [diff] [blame] | 89 | return chip->lines[offset].value; |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 90 | } |
| 91 | |
Bartosz Golaszewski | 2a9d742 | 2017-11-27 11:48:50 +0100 | [diff] [blame] | 92 | static void gpio_mockup_set(struct gpio_chip *gc, |
| 93 | unsigned int offset, int value) |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 94 | { |
Bartosz Golaszewski | ca409160 | 2017-02-06 13:10:35 +0100 | [diff] [blame] | 95 | struct gpio_mockup_chip *chip = gpiochip_get_data(gc); |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 96 | |
Bartosz Golaszewski | ca409160 | 2017-02-06 13:10:35 +0100 | [diff] [blame] | 97 | chip->lines[offset].value = !!value; |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 98 | } |
| 99 | |
Bartosz Golaszewski | c47bee9 | 2017-11-27 11:48:51 +0100 | [diff] [blame] | 100 | static void gpio_mockup_set_multiple(struct gpio_chip *gc, |
| 101 | unsigned long *mask, unsigned long *bits) |
| 102 | { |
| 103 | unsigned int bit; |
| 104 | |
| 105 | for_each_set_bit(bit, mask, gc->ngpio) |
| 106 | gpio_mockup_set(gc, bit, test_bit(bit, bits)); |
| 107 | |
| 108 | } |
| 109 | |
Bartosz Golaszewski | 2a9d742 | 2017-11-27 11:48:50 +0100 | [diff] [blame] | 110 | static int gpio_mockup_dirout(struct gpio_chip *gc, |
| 111 | unsigned int offset, int value) |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 112 | { |
Bartosz Golaszewski | ca409160 | 2017-02-06 13:10:35 +0100 | [diff] [blame] | 113 | struct gpio_mockup_chip *chip = gpiochip_get_data(gc); |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 114 | |
Bartosz Golaszewski | ca409160 | 2017-02-06 13:10:35 +0100 | [diff] [blame] | 115 | gpio_mockup_set(gc, offset, value); |
Bartosz Golaszewski | c650c00 | 2017-05-25 10:33:39 +0200 | [diff] [blame] | 116 | chip->lines[offset].dir = GPIO_MOCKUP_DIR_OUT; |
Bartosz Golaszewski | ca409160 | 2017-02-06 13:10:35 +0100 | [diff] [blame] | 117 | |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 118 | return 0; |
| 119 | } |
| 120 | |
Bartosz Golaszewski | ca409160 | 2017-02-06 13:10:35 +0100 | [diff] [blame] | 121 | 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] | 122 | { |
Bartosz Golaszewski | ca409160 | 2017-02-06 13:10:35 +0100 | [diff] [blame] | 123 | struct gpio_mockup_chip *chip = gpiochip_get_data(gc); |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 124 | |
Bartosz Golaszewski | c650c00 | 2017-05-25 10:33:39 +0200 | [diff] [blame] | 125 | chip->lines[offset].dir = GPIO_MOCKUP_DIR_IN; |
Bartosz Golaszewski | ca409160 | 2017-02-06 13:10:35 +0100 | [diff] [blame] | 126 | |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 127 | return 0; |
| 128 | } |
| 129 | |
Bartosz Golaszewski | ca409160 | 2017-02-06 13:10:35 +0100 | [diff] [blame] | 130 | 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] | 131 | { |
Bartosz Golaszewski | ca409160 | 2017-02-06 13:10:35 +0100 | [diff] [blame] | 132 | struct gpio_mockup_chip *chip = gpiochip_get_data(gc); |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 133 | |
Bartosz Golaszewski | ca409160 | 2017-02-06 13:10:35 +0100 | [diff] [blame] | 134 | return chip->lines[offset].dir; |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 135 | } |
| 136 | |
Bartosz Golaszewski | b4c495f | 2017-08-14 13:20:56 +0200 | [diff] [blame] | 137 | 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] | 138 | { |
Bartosz Golaszewski | c7f5326 | 2017-05-25 10:33:41 +0200 | [diff] [blame] | 139 | struct gpio_mockup_chip *chip = gpiochip_get_data(gc); |
| 140 | |
Bartosz Golaszewski | b4c495f | 2017-08-14 13:20:56 +0200 | [diff] [blame] | 141 | return irq_sim_irqnum(&chip->irqsim, offset); |
Bartosz Golaszewski | e2ff740 | 2017-02-06 15:11:07 +0100 | [diff] [blame] | 142 | } |
| 143 | |
Bartosz Golaszewski | 9202ba2 | 2017-02-06 15:11:08 +0100 | [diff] [blame] | 144 | static ssize_t gpio_mockup_event_write(struct file *file, |
| 145 | const char __user *usr_buf, |
| 146 | size_t size, loff_t *ppos) |
| 147 | { |
| 148 | struct gpio_mockup_dbgfs_private *priv; |
| 149 | struct gpio_mockup_chip *chip; |
| 150 | struct seq_file *sfile; |
| 151 | struct gpio_desc *desc; |
Bartosz Golaszewski | 6f9b3e7 | 2017-06-09 13:41:25 +0200 | [diff] [blame] | 152 | int rv, val; |
Bartosz Golaszewski | 9202ba2 | 2017-02-06 15:11:08 +0100 | [diff] [blame] | 153 | |
Bartosz Golaszewski | 6f9b3e7 | 2017-06-09 13:41:25 +0200 | [diff] [blame] | 154 | rv = kstrtoint_from_user(usr_buf, size, 0, &val); |
| 155 | if (rv) |
| 156 | return rv; |
| 157 | if (val != 0 && val != 1) |
| 158 | return -EINVAL; |
| 159 | |
Bartosz Golaszewski | 650b57b | 2017-06-09 13:41:26 +0200 | [diff] [blame] | 160 | sfile = file->private_data; |
| 161 | priv = sfile->private; |
| 162 | desc = priv->desc; |
| 163 | chip = priv->chip; |
Bartosz Golaszewski | c7f5326 | 2017-05-25 10:33:41 +0200 | [diff] [blame] | 164 | |
Bartosz Golaszewski | b4c495f | 2017-08-14 13:20:56 +0200 | [diff] [blame] | 165 | gpiod_set_value_cansleep(desc, val); |
| 166 | irq_sim_fire(&chip->irqsim, priv->offset); |
Bartosz Golaszewski | 9202ba2 | 2017-02-06 15:11:08 +0100 | [diff] [blame] | 167 | |
| 168 | return size; |
| 169 | } |
| 170 | |
| 171 | static int gpio_mockup_event_open(struct inode *inode, struct file *file) |
| 172 | { |
| 173 | return single_open(file, NULL, inode->i_private); |
| 174 | } |
| 175 | |
| 176 | static const struct file_operations gpio_mockup_event_ops = { |
| 177 | .owner = THIS_MODULE, |
| 178 | .open = gpio_mockup_event_open, |
| 179 | .write = gpio_mockup_event_write, |
| 180 | .llseek = no_llseek, |
| 181 | }; |
| 182 | |
| 183 | static void gpio_mockup_debugfs_setup(struct device *dev, |
| 184 | struct gpio_mockup_chip *chip) |
| 185 | { |
| 186 | struct gpio_mockup_dbgfs_private *priv; |
Bartosz Golaszewski | ca8792a | 2017-11-27 11:48:45 +0100 | [diff] [blame] | 187 | struct dentry *evfile, *link; |
Bartosz Golaszewski | 9202ba2 | 2017-02-06 15:11:08 +0100 | [diff] [blame] | 188 | struct gpio_chip *gc; |
Bartosz Golaszewski | ca8792a | 2017-11-27 11:48:45 +0100 | [diff] [blame] | 189 | const char *devname; |
Bartosz Golaszewski | 9202ba2 | 2017-02-06 15:11:08 +0100 | [diff] [blame] | 190 | char *name; |
| 191 | int i; |
| 192 | |
| 193 | gc = &chip->gc; |
Bartosz Golaszewski | ca8792a | 2017-11-27 11:48:45 +0100 | [diff] [blame] | 194 | devname = dev_name(&gc->gpiodev->dev); |
Bartosz Golaszewski | 9202ba2 | 2017-02-06 15:11:08 +0100 | [diff] [blame] | 195 | |
Bartosz Golaszewski | ca8792a | 2017-11-27 11:48:45 +0100 | [diff] [blame] | 196 | chip->dbg_dir = debugfs_create_dir(devname, gpio_mockup_dbg_dir); |
Bartosz Golaszewski | 46526c1 | 2017-11-27 11:48:48 +0100 | [diff] [blame] | 197 | if (IS_ERR_OR_NULL(chip->dbg_dir)) |
Bartosz Golaszewski | 9202ba2 | 2017-02-06 15:11:08 +0100 | [diff] [blame] | 198 | goto err; |
| 199 | |
Bartosz Golaszewski | ca8792a | 2017-11-27 11:48:45 +0100 | [diff] [blame] | 200 | link = debugfs_create_symlink(gc->label, gpio_mockup_dbg_dir, devname); |
Bartosz Golaszewski | 46526c1 | 2017-11-27 11:48:48 +0100 | [diff] [blame] | 201 | if (IS_ERR_OR_NULL(link)) |
Bartosz Golaszewski | ca8792a | 2017-11-27 11:48:45 +0100 | [diff] [blame] | 202 | goto err; |
| 203 | |
Bartosz Golaszewski | 9202ba2 | 2017-02-06 15:11:08 +0100 | [diff] [blame] | 204 | for (i = 0; i < gc->ngpio; i++) { |
| 205 | name = devm_kasprintf(dev, GFP_KERNEL, "%d", i); |
| 206 | if (!name) |
| 207 | goto err; |
| 208 | |
| 209 | priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); |
| 210 | if (!priv) |
| 211 | goto err; |
| 212 | |
| 213 | priv->chip = chip; |
| 214 | priv->offset = i; |
| 215 | priv->desc = &gc->gpiodev->descs[i]; |
| 216 | |
| 217 | evfile = debugfs_create_file(name, 0200, chip->dbg_dir, priv, |
| 218 | &gpio_mockup_event_ops); |
Bartosz Golaszewski | 46526c1 | 2017-11-27 11:48:48 +0100 | [diff] [blame] | 219 | if (IS_ERR_OR_NULL(evfile)) |
Bartosz Golaszewski | 9202ba2 | 2017-02-06 15:11:08 +0100 | [diff] [blame] | 220 | goto err; |
| 221 | } |
| 222 | |
| 223 | return; |
| 224 | |
| 225 | err: |
Bartosz Golaszewski | 46526c1 | 2017-11-27 11:48:48 +0100 | [diff] [blame] | 226 | dev_err(dev, "error creating debugfs event files\n"); |
Bartosz Golaszewski | 9202ba2 | 2017-02-06 15:11:08 +0100 | [diff] [blame] | 227 | } |
| 228 | |
Bartosz Golaszewski | 1cea473 | 2017-11-27 11:48:47 +0100 | [diff] [blame] | 229 | static int gpio_mockup_name_lines(struct device *dev, |
| 230 | struct gpio_mockup_chip *chip) |
| 231 | { |
| 232 | struct gpio_chip *gc = &chip->gc; |
| 233 | char **names; |
| 234 | int i; |
| 235 | |
| 236 | names = devm_kcalloc(dev, gc->ngpio, sizeof(char *), GFP_KERNEL); |
| 237 | if (!names) |
| 238 | return -ENOMEM; |
| 239 | |
| 240 | for (i = 0; i < gc->ngpio; i++) { |
| 241 | names[i] = devm_kasprintf(dev, GFP_KERNEL, |
| 242 | "%s-%d", gc->label, i); |
| 243 | if (!names[i]) |
| 244 | return -ENOMEM; |
| 245 | } |
| 246 | |
| 247 | gc->names = (const char *const *)names; |
| 248 | |
| 249 | return 0; |
| 250 | } |
| 251 | |
Bartosz Golaszewski | ca409160 | 2017-02-06 13:10:35 +0100 | [diff] [blame] | 252 | static int gpio_mockup_probe(struct platform_device *pdev) |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 253 | { |
Bartosz Golaszewski | 8a39f59 | 2017-11-27 11:48:40 +0100 | [diff] [blame] | 254 | struct gpio_mockup_chip *chip; |
Bartosz Golaszewski | 6d974d3 | 2017-11-27 11:48:43 +0100 | [diff] [blame] | 255 | struct gpio_chip *gc; |
Bartosz Golaszewski | 8a39f59 | 2017-11-27 11:48:40 +0100 | [diff] [blame] | 256 | struct device *dev; |
Bartosz Golaszewski | 3ea47b4 | 2018-09-25 13:29:46 +0200 | [diff] [blame] | 257 | const char *name; |
| 258 | int rv, base; |
| 259 | u16 ngpio; |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 260 | |
Bartosz Golaszewski | 8a39f59 | 2017-11-27 11:48:40 +0100 | [diff] [blame] | 261 | dev = &pdev->dev; |
Bartosz Golaszewski | 3ea47b4 | 2018-09-25 13:29:46 +0200 | [diff] [blame] | 262 | |
| 263 | rv = device_property_read_u32(dev, "gpio-base", &base); |
| 264 | if (rv) |
| 265 | base = -1; |
| 266 | |
| 267 | rv = device_property_read_u16(dev, "nr-gpios", &ngpio); |
| 268 | if (rv) |
| 269 | return rv; |
| 270 | |
| 271 | rv = device_property_read_string(dev, "chip-name", &name); |
| 272 | if (rv) |
| 273 | name = NULL; |
Bartosz Golaszewski | b652336 | 2017-06-09 13:41:28 +0200 | [diff] [blame] | 274 | |
Bartosz Golaszewski | 8a39f59 | 2017-11-27 11:48:40 +0100 | [diff] [blame] | 275 | chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL); |
| 276 | if (!chip) |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 277 | return -ENOMEM; |
| 278 | |
Bartosz Golaszewski | 3ea47b4 | 2018-09-25 13:29:46 +0200 | [diff] [blame] | 279 | if (!name) { |
| 280 | name = devm_kasprintf(dev, GFP_KERNEL, |
| 281 | "%s-%c", pdev->name, pdev->id + 'A'); |
| 282 | if (!name) |
| 283 | return -ENOMEM; |
| 284 | } |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 285 | |
Bartosz Golaszewski | 6d974d3 | 2017-11-27 11:48:43 +0100 | [diff] [blame] | 286 | gc = &chip->gc; |
| 287 | gc->base = base; |
| 288 | gc->ngpio = ngpio; |
| 289 | gc->label = name; |
| 290 | gc->owner = THIS_MODULE; |
| 291 | gc->parent = dev; |
| 292 | gc->get = gpio_mockup_get; |
| 293 | gc->set = gpio_mockup_set; |
Bartosz Golaszewski | c47bee9 | 2017-11-27 11:48:51 +0100 | [diff] [blame] | 294 | gc->set_multiple = gpio_mockup_set_multiple; |
Bartosz Golaszewski | 6d974d3 | 2017-11-27 11:48:43 +0100 | [diff] [blame] | 295 | gc->direction_output = gpio_mockup_dirout; |
| 296 | gc->direction_input = gpio_mockup_dirin; |
| 297 | gc->get_direction = gpio_mockup_get_direction; |
| 298 | gc->to_irq = gpio_mockup_to_irq; |
| 299 | |
| 300 | chip->lines = devm_kcalloc(dev, gc->ngpio, |
| 301 | sizeof(*chip->lines), GFP_KERNEL); |
| 302 | if (!chip->lines) |
| 303 | return -ENOMEM; |
| 304 | |
Bartosz Golaszewski | 3ea47b4 | 2018-09-25 13:29:46 +0200 | [diff] [blame] | 305 | if (device_property_read_bool(dev, "named-gpio-lines")) { |
Bartosz Golaszewski | 6d974d3 | 2017-11-27 11:48:43 +0100 | [diff] [blame] | 306 | rv = gpio_mockup_name_lines(dev, chip); |
| 307 | if (rv) |
| 308 | return rv; |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 309 | } |
| 310 | |
Bartosz Golaszewski | 6d974d3 | 2017-11-27 11:48:43 +0100 | [diff] [blame] | 311 | rv = devm_irq_sim_init(dev, &chip->irqsim, gc->ngpio); |
Bartosz Golaszewski | 1e0dca6 | 2017-11-27 11:48:52 +0100 | [diff] [blame] | 312 | if (rv < 0) |
Bartosz Golaszewski | 6d974d3 | 2017-11-27 11:48:43 +0100 | [diff] [blame] | 313 | return rv; |
| 314 | |
| 315 | rv = devm_gpiochip_add_data(dev, &chip->gc, chip); |
| 316 | if (rv) |
| 317 | return rv; |
| 318 | |
Bartosz Golaszewski | 6cb9215 | 2018-03-04 13:45:51 +0100 | [diff] [blame] | 319 | if (!IS_ERR_OR_NULL(gpio_mockup_dbg_dir)) |
Bartosz Golaszewski | 6d974d3 | 2017-11-27 11:48:43 +0100 | [diff] [blame] | 320 | gpio_mockup_debugfs_setup(dev, chip); |
| 321 | |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 322 | return 0; |
| 323 | } |
| 324 | |
Bartosz Golaszewski | ca409160 | 2017-02-06 13:10:35 +0100 | [diff] [blame] | 325 | static struct platform_driver gpio_mockup_driver = { |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 326 | .driver = { |
Bartosz Golaszewski | ca409160 | 2017-02-06 13:10:35 +0100 | [diff] [blame] | 327 | .name = GPIO_MOCKUP_NAME, |
Bartosz Golaszewski | 364ac45 | 2016-12-20 12:28:20 +0100 | [diff] [blame] | 328 | }, |
Bartosz Golaszewski | ca409160 | 2017-02-06 13:10:35 +0100 | [diff] [blame] | 329 | .probe = gpio_mockup_probe, |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 330 | }; |
| 331 | |
Bartosz Golaszewski | 8a39f59 | 2017-11-27 11:48:40 +0100 | [diff] [blame] | 332 | static struct platform_device *gpio_mockup_pdevs[GPIO_MOCKUP_MAX_GC]; |
| 333 | |
| 334 | static void gpio_mockup_unregister_pdevs(void) |
| 335 | { |
| 336 | struct platform_device *pdev; |
| 337 | int i; |
| 338 | |
| 339 | for (i = 0; i < GPIO_MOCKUP_MAX_GC; i++) { |
| 340 | pdev = gpio_mockup_pdevs[i]; |
| 341 | |
| 342 | if (pdev) |
| 343 | platform_device_unregister(pdev); |
| 344 | } |
| 345 | } |
Bartosz Golaszewski | f3b4717 | 2017-11-27 11:48:37 +0100 | [diff] [blame] | 346 | |
| 347 | static int __init gpio_mockup_init(void) |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 348 | { |
Bartosz Golaszewski | 3ea47b4 | 2018-09-25 13:29:46 +0200 | [diff] [blame] | 349 | struct property_entry properties[GPIO_MOCKUP_MAX_PROP]; |
| 350 | int i, prop, num_chips, err = 0, base; |
| 351 | struct platform_device_info pdevinfo; |
Bartosz Golaszewski | 8a39f59 | 2017-11-27 11:48:40 +0100 | [diff] [blame] | 352 | struct platform_device *pdev; |
Bartosz Golaszewski | 3ea47b4 | 2018-09-25 13:29:46 +0200 | [diff] [blame] | 353 | u16 ngpio; |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 354 | |
Bartosz Golaszewski | e63a006 | 2017-11-27 11:48:53 +0100 | [diff] [blame] | 355 | if ((gpio_mockup_num_ranges < 2) || |
| 356 | (gpio_mockup_num_ranges % 2) || |
| 357 | (gpio_mockup_num_ranges > GPIO_MOCKUP_MAX_RANGES)) |
Bartosz Golaszewski | 20c35ac | 2017-11-27 11:48:38 +0100 | [diff] [blame] | 358 | return -EINVAL; |
| 359 | |
Bartosz Golaszewski | 8a39f59 | 2017-11-27 11:48:40 +0100 | [diff] [blame] | 360 | /* Each chip is described by two values. */ |
Bartosz Golaszewski | e63a006 | 2017-11-27 11:48:53 +0100 | [diff] [blame] | 361 | num_chips = gpio_mockup_num_ranges / 2; |
Bartosz Golaszewski | 8a39f59 | 2017-11-27 11:48:40 +0100 | [diff] [blame] | 362 | |
Bartosz Golaszewski | fa86963 | 2017-11-27 11:48:49 +0100 | [diff] [blame] | 363 | /* |
| 364 | * The second value in the <base GPIO - number of GPIOS> pair must |
| 365 | * always be greater than 0. |
| 366 | */ |
| 367 | for (i = 0; i < num_chips; i++) { |
Bartosz Golaszewski | cd9835f | 2017-11-27 11:48:54 +0100 | [diff] [blame] | 368 | if (gpio_mockup_range_ngpio(i) < 0) |
Bartosz Golaszewski | fa86963 | 2017-11-27 11:48:49 +0100 | [diff] [blame] | 369 | return -EINVAL; |
| 370 | } |
| 371 | |
Bartosz Golaszewski | 9202ba2 | 2017-02-06 15:11:08 +0100 | [diff] [blame] | 372 | gpio_mockup_dbg_dir = debugfs_create_dir("gpio-mockup-event", NULL); |
Bartosz Golaszewski | 46526c1 | 2017-11-27 11:48:48 +0100 | [diff] [blame] | 373 | if (IS_ERR_OR_NULL(gpio_mockup_dbg_dir)) |
Bartosz Golaszewski | f1bec99 | 2017-11-27 11:48:41 +0100 | [diff] [blame] | 374 | gpio_mockup_err("error creating debugfs directory\n"); |
Bartosz Golaszewski | 9202ba2 | 2017-02-06 15:11:08 +0100 | [diff] [blame] | 375 | |
Bartosz Golaszewski | 8a39f59 | 2017-11-27 11:48:40 +0100 | [diff] [blame] | 376 | err = platform_driver_register(&gpio_mockup_driver); |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 377 | if (err) { |
Bartosz Golaszewski | f1bec99 | 2017-11-27 11:48:41 +0100 | [diff] [blame] | 378 | gpio_mockup_err("error registering platform driver\n"); |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 379 | return err; |
| 380 | } |
| 381 | |
Bartosz Golaszewski | 8a39f59 | 2017-11-27 11:48:40 +0100 | [diff] [blame] | 382 | for (i = 0; i < num_chips; i++) { |
Bartosz Golaszewski | 3ea47b4 | 2018-09-25 13:29:46 +0200 | [diff] [blame] | 383 | memset(properties, 0, sizeof(properties)); |
| 384 | memset(&pdevinfo, 0, sizeof(pdevinfo)); |
| 385 | prop = 0; |
Bartosz Golaszewski | 8a39f59 | 2017-11-27 11:48:40 +0100 | [diff] [blame] | 386 | |
Bartosz Golaszewski | 3ea47b4 | 2018-09-25 13:29:46 +0200 | [diff] [blame] | 387 | base = gpio_mockup_range_base(i); |
| 388 | if (base >= 0) |
| 389 | properties[prop++] = PROPERTY_ENTRY_U32("gpio-base", |
| 390 | base); |
| 391 | |
| 392 | ngpio = base < 0 ? gpio_mockup_range_ngpio(i) |
| 393 | : gpio_mockup_range_ngpio(i) - base; |
| 394 | properties[prop++] = PROPERTY_ENTRY_U16("nr-gpios", ngpio); |
| 395 | |
| 396 | if (gpio_mockup_named_lines) |
| 397 | properties[prop++] = PROPERTY_ENTRY_BOOL( |
| 398 | "named-gpio-lines"); |
| 399 | |
| 400 | pdevinfo.name = GPIO_MOCKUP_NAME; |
| 401 | pdevinfo.id = i; |
| 402 | pdevinfo.properties = properties; |
| 403 | |
| 404 | pdev = platform_device_register_full(&pdevinfo); |
Bartosz Golaszewski | c4b54e1 | 2017-12-06 17:30:02 +0100 | [diff] [blame] | 405 | if (IS_ERR(pdev)) { |
Bartosz Golaszewski | f1bec99 | 2017-11-27 11:48:41 +0100 | [diff] [blame] | 406 | gpio_mockup_err("error registering device"); |
Bartosz Golaszewski | 8a39f59 | 2017-11-27 11:48:40 +0100 | [diff] [blame] | 407 | platform_driver_unregister(&gpio_mockup_driver); |
| 408 | gpio_mockup_unregister_pdevs(); |
Bartosz Golaszewski | c4b54e1 | 2017-12-06 17:30:02 +0100 | [diff] [blame] | 409 | return PTR_ERR(pdev); |
Bartosz Golaszewski | 8a39f59 | 2017-11-27 11:48:40 +0100 | [diff] [blame] | 410 | } |
| 411 | |
| 412 | gpio_mockup_pdevs[i] = pdev; |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | return 0; |
| 416 | } |
| 417 | |
Bartosz Golaszewski | f3b4717 | 2017-11-27 11:48:37 +0100 | [diff] [blame] | 418 | static void __exit gpio_mockup_exit(void) |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 419 | { |
Bartosz Golaszewski | 9202ba2 | 2017-02-06 15:11:08 +0100 | [diff] [blame] | 420 | debugfs_remove_recursive(gpio_mockup_dbg_dir); |
Bartosz Golaszewski | ca409160 | 2017-02-06 13:10:35 +0100 | [diff] [blame] | 421 | platform_driver_unregister(&gpio_mockup_driver); |
Bartosz Golaszewski | 8a39f59 | 2017-11-27 11:48:40 +0100 | [diff] [blame] | 422 | gpio_mockup_unregister_pdevs(); |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 423 | } |
| 424 | |
Bartosz Golaszewski | f3b4717 | 2017-11-27 11:48:37 +0100 | [diff] [blame] | 425 | module_init(gpio_mockup_init); |
| 426 | module_exit(gpio_mockup_exit); |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 427 | |
| 428 | MODULE_AUTHOR("Kamlakant Patel <kamlakant.patel@broadcom.com>"); |
Linus Walleij | 4f9a4cd | 2018-03-06 09:04:15 +0100 | [diff] [blame] | 429 | MODULE_AUTHOR("Bamvor Jian Zhang <bamv2005@gmail.com>"); |
Bartosz Golaszewski | c60c7f4 | 2017-06-09 13:41:31 +0200 | [diff] [blame] | 430 | MODULE_AUTHOR("Bartosz Golaszewski <brgl@bgdev.pl>"); |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 431 | MODULE_DESCRIPTION("GPIO Testing driver"); |
| 432 | MODULE_LICENSE("GPL v2"); |