Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 1 | /* |
| 2 | * GPIO Testing Device Driver |
| 3 | * |
| 4 | * Copyright (C) 2014 Kamlakant Patel <kamlakant.patel@broadcom.com> |
| 5 | * Copyright (C) 2015-2016 Bamvor Jian Zhang <bamvor.zhangjian@linaro.org> |
Bartosz Golaszewski | c60c7f4 | 2017-06-09 13:41:31 +0200 | [diff] [blame] | 6 | * Copyright (C) 2017 Bartosz Golaszewski <brgl@bgdev.pl> |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify it |
| 9 | * under the terms of the GNU General Public License as published by the |
| 10 | * Free Software Foundation; either version 2 of the License, or (at your |
| 11 | * option) any later version. |
| 12 | * |
| 13 | */ |
| 14 | |
| 15 | #include <linux/init.h> |
| 16 | #include <linux/module.h> |
| 17 | #include <linux/gpio/driver.h> |
Bartosz Golaszewski | 9202ba2 | 2017-02-06 15:11:08 +0100 | [diff] [blame] | 18 | #include <linux/gpio/consumer.h> |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 19 | #include <linux/platform_device.h> |
Bartosz Golaszewski | 8a68ea0 | 2017-02-06 13:10:37 +0100 | [diff] [blame] | 20 | #include <linux/slab.h> |
Bartosz Golaszewski | e2ff740 | 2017-02-06 15:11:07 +0100 | [diff] [blame] | 21 | #include <linux/interrupt.h> |
| 22 | #include <linux/irq.h> |
Bartosz Golaszewski | b4c495f | 2017-08-14 13:20:56 +0200 | [diff] [blame] | 23 | #include <linux/irq_sim.h> |
Bartosz Golaszewski | 9202ba2 | 2017-02-06 15:11:08 +0100 | [diff] [blame] | 24 | #include <linux/debugfs.h> |
| 25 | #include <linux/uaccess.h> |
| 26 | |
| 27 | #include "gpiolib.h" |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 28 | |
Bartosz Golaszewski | ca409160 | 2017-02-06 13:10:35 +0100 | [diff] [blame] | 29 | #define GPIO_MOCKUP_NAME "gpio-mockup" |
| 30 | #define GPIO_MOCKUP_MAX_GC 10 |
Bartosz Golaszewski | b652336 | 2017-06-09 13:41:28 +0200 | [diff] [blame] | 31 | /* |
| 32 | * We're storing two values per chip: the GPIO base and the number |
| 33 | * of GPIO lines. |
| 34 | */ |
| 35 | #define GPIO_MOCKUP_MAX_RANGES (GPIO_MOCKUP_MAX_GC * 2) |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 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; |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 49 | bool 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]; |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 66 | static int gpio_mockup_params_nr; |
| 67 | module_param_array(gpio_mockup_ranges, int, &gpio_mockup_params_nr, 0400); |
| 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 | ca409160 | 2017-02-06 13:10:35 +0100 | [diff] [blame] | 73 | static const char gpio_mockup_name_start = 'A'; |
Bartosz Golaszewski | 9202ba2 | 2017-02-06 15:11:08 +0100 | [diff] [blame] | 74 | static struct dentry *gpio_mockup_dbg_dir; |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 75 | |
Bartosz Golaszewski | ca409160 | 2017-02-06 13:10:35 +0100 | [diff] [blame] | 76 | 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] | 77 | { |
Bartosz Golaszewski | ca409160 | 2017-02-06 13:10:35 +0100 | [diff] [blame] | 78 | struct gpio_mockup_chip *chip = gpiochip_get_data(gc); |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 79 | |
Bartosz Golaszewski | ca409160 | 2017-02-06 13:10:35 +0100 | [diff] [blame] | 80 | return chip->lines[offset].value; |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 81 | } |
| 82 | |
Bartosz Golaszewski | ca409160 | 2017-02-06 13:10:35 +0100 | [diff] [blame] | 83 | static void gpio_mockup_set(struct gpio_chip *gc, unsigned int offset, |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 84 | int value) |
| 85 | { |
Bartosz Golaszewski | ca409160 | 2017-02-06 13:10:35 +0100 | [diff] [blame] | 86 | struct gpio_mockup_chip *chip = gpiochip_get_data(gc); |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 87 | |
Bartosz Golaszewski | ca409160 | 2017-02-06 13:10:35 +0100 | [diff] [blame] | 88 | chip->lines[offset].value = !!value; |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 89 | } |
| 90 | |
Bartosz Golaszewski | ca409160 | 2017-02-06 13:10:35 +0100 | [diff] [blame] | 91 | static int gpio_mockup_dirout(struct gpio_chip *gc, unsigned int offset, |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 92 | int value) |
| 93 | { |
Bartosz Golaszewski | ca409160 | 2017-02-06 13:10:35 +0100 | [diff] [blame] | 94 | struct gpio_mockup_chip *chip = gpiochip_get_data(gc); |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 95 | |
Bartosz Golaszewski | ca409160 | 2017-02-06 13:10:35 +0100 | [diff] [blame] | 96 | gpio_mockup_set(gc, offset, value); |
Bartosz Golaszewski | c650c00 | 2017-05-25 10:33:39 +0200 | [diff] [blame] | 97 | chip->lines[offset].dir = GPIO_MOCKUP_DIR_OUT; |
Bartosz Golaszewski | ca409160 | 2017-02-06 13:10:35 +0100 | [diff] [blame] | 98 | |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 99 | return 0; |
| 100 | } |
| 101 | |
Bartosz Golaszewski | ca409160 | 2017-02-06 13:10:35 +0100 | [diff] [blame] | 102 | 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] | 103 | { |
Bartosz Golaszewski | ca409160 | 2017-02-06 13:10:35 +0100 | [diff] [blame] | 104 | struct gpio_mockup_chip *chip = gpiochip_get_data(gc); |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 105 | |
Bartosz Golaszewski | c650c00 | 2017-05-25 10:33:39 +0200 | [diff] [blame] | 106 | chip->lines[offset].dir = GPIO_MOCKUP_DIR_IN; |
Bartosz Golaszewski | ca409160 | 2017-02-06 13:10:35 +0100 | [diff] [blame] | 107 | |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 108 | return 0; |
| 109 | } |
| 110 | |
Bartosz Golaszewski | ca409160 | 2017-02-06 13:10:35 +0100 | [diff] [blame] | 111 | 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] | 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 | return chip->lines[offset].dir; |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 116 | } |
| 117 | |
Bartosz Golaszewski | 8a68ea0 | 2017-02-06 13:10:37 +0100 | [diff] [blame] | 118 | static int gpio_mockup_name_lines(struct device *dev, |
| 119 | struct gpio_mockup_chip *chip) |
| 120 | { |
| 121 | struct gpio_chip *gc = &chip->gc; |
| 122 | char **names; |
| 123 | int i; |
| 124 | |
Bartosz Golaszewski | f6ac438 | 2017-06-09 13:41:32 +0200 | [diff] [blame] | 125 | names = devm_kcalloc(dev, gc->ngpio, sizeof(char *), GFP_KERNEL); |
Bartosz Golaszewski | 8a68ea0 | 2017-02-06 13:10:37 +0100 | [diff] [blame] | 126 | if (!names) |
| 127 | return -ENOMEM; |
| 128 | |
| 129 | for (i = 0; i < gc->ngpio; i++) { |
| 130 | names[i] = devm_kasprintf(dev, GFP_KERNEL, |
| 131 | "%s-%d", gc->label, i); |
| 132 | if (!names[i]) |
| 133 | return -ENOMEM; |
| 134 | } |
| 135 | |
| 136 | gc->names = (const char *const *)names; |
| 137 | |
| 138 | return 0; |
| 139 | } |
| 140 | |
Bartosz Golaszewski | b4c495f | 2017-08-14 13:20:56 +0200 | [diff] [blame] | 141 | 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] | 142 | { |
Bartosz Golaszewski | c7f5326 | 2017-05-25 10:33:41 +0200 | [diff] [blame] | 143 | struct gpio_mockup_chip *chip = gpiochip_get_data(gc); |
| 144 | |
Bartosz Golaszewski | b4c495f | 2017-08-14 13:20:56 +0200 | [diff] [blame] | 145 | return irq_sim_irqnum(&chip->irqsim, offset); |
Bartosz Golaszewski | e2ff740 | 2017-02-06 15:11:07 +0100 | [diff] [blame] | 146 | } |
| 147 | |
Bartosz Golaszewski | 9202ba2 | 2017-02-06 15:11:08 +0100 | [diff] [blame] | 148 | static ssize_t gpio_mockup_event_write(struct file *file, |
| 149 | const char __user *usr_buf, |
| 150 | size_t size, loff_t *ppos) |
| 151 | { |
| 152 | struct gpio_mockup_dbgfs_private *priv; |
| 153 | struct gpio_mockup_chip *chip; |
| 154 | struct seq_file *sfile; |
| 155 | struct gpio_desc *desc; |
Bartosz Golaszewski | 6f9b3e7 | 2017-06-09 13:41:25 +0200 | [diff] [blame] | 156 | int rv, val; |
Bartosz Golaszewski | 9202ba2 | 2017-02-06 15:11:08 +0100 | [diff] [blame] | 157 | |
Bartosz Golaszewski | 6f9b3e7 | 2017-06-09 13:41:25 +0200 | [diff] [blame] | 158 | rv = kstrtoint_from_user(usr_buf, size, 0, &val); |
| 159 | if (rv) |
| 160 | return rv; |
| 161 | if (val != 0 && val != 1) |
| 162 | return -EINVAL; |
| 163 | |
Bartosz Golaszewski | 650b57b | 2017-06-09 13:41:26 +0200 | [diff] [blame] | 164 | sfile = file->private_data; |
| 165 | priv = sfile->private; |
| 166 | desc = priv->desc; |
| 167 | chip = priv->chip; |
Bartosz Golaszewski | c7f5326 | 2017-05-25 10:33:41 +0200 | [diff] [blame] | 168 | |
Bartosz Golaszewski | b4c495f | 2017-08-14 13:20:56 +0200 | [diff] [blame] | 169 | gpiod_set_value_cansleep(desc, val); |
| 170 | irq_sim_fire(&chip->irqsim, priv->offset); |
Bartosz Golaszewski | 9202ba2 | 2017-02-06 15:11:08 +0100 | [diff] [blame] | 171 | |
| 172 | return size; |
| 173 | } |
| 174 | |
| 175 | static int gpio_mockup_event_open(struct inode *inode, struct file *file) |
| 176 | { |
| 177 | return single_open(file, NULL, inode->i_private); |
| 178 | } |
| 179 | |
| 180 | static const struct file_operations gpio_mockup_event_ops = { |
| 181 | .owner = THIS_MODULE, |
| 182 | .open = gpio_mockup_event_open, |
| 183 | .write = gpio_mockup_event_write, |
| 184 | .llseek = no_llseek, |
| 185 | }; |
| 186 | |
| 187 | static void gpio_mockup_debugfs_setup(struct device *dev, |
| 188 | struct gpio_mockup_chip *chip) |
| 189 | { |
| 190 | struct gpio_mockup_dbgfs_private *priv; |
| 191 | struct dentry *evfile; |
| 192 | struct gpio_chip *gc; |
| 193 | char *name; |
| 194 | int i; |
| 195 | |
| 196 | gc = &chip->gc; |
| 197 | |
| 198 | chip->dbg_dir = debugfs_create_dir(gc->label, gpio_mockup_dbg_dir); |
| 199 | if (!chip->dbg_dir) |
| 200 | goto err; |
| 201 | |
| 202 | for (i = 0; i < gc->ngpio; i++) { |
| 203 | name = devm_kasprintf(dev, GFP_KERNEL, "%d", i); |
| 204 | if (!name) |
| 205 | goto err; |
| 206 | |
| 207 | priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); |
| 208 | if (!priv) |
| 209 | goto err; |
| 210 | |
| 211 | priv->chip = chip; |
| 212 | priv->offset = i; |
| 213 | priv->desc = &gc->gpiodev->descs[i]; |
| 214 | |
| 215 | evfile = debugfs_create_file(name, 0200, chip->dbg_dir, priv, |
| 216 | &gpio_mockup_event_ops); |
| 217 | if (!evfile) |
| 218 | goto err; |
| 219 | } |
| 220 | |
| 221 | return; |
| 222 | |
| 223 | err: |
| 224 | dev_err(dev, "error creating debugfs directory\n"); |
| 225 | } |
| 226 | |
Bartosz Golaszewski | ca409160 | 2017-02-06 13:10:35 +0100 | [diff] [blame] | 227 | static int gpio_mockup_add(struct device *dev, |
| 228 | struct gpio_mockup_chip *chip, |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 229 | const char *name, int base, int ngpio) |
| 230 | { |
Bartosz Golaszewski | ca409160 | 2017-02-06 13:10:35 +0100 | [diff] [blame] | 231 | struct gpio_chip *gc = &chip->gc; |
Bartosz Golaszewski | 8a68ea0 | 2017-02-06 13:10:37 +0100 | [diff] [blame] | 232 | int ret; |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 233 | |
Bartosz Golaszewski | ca409160 | 2017-02-06 13:10:35 +0100 | [diff] [blame] | 234 | gc->base = base; |
| 235 | gc->ngpio = ngpio; |
| 236 | gc->label = name; |
| 237 | gc->owner = THIS_MODULE; |
| 238 | gc->parent = dev; |
| 239 | gc->get = gpio_mockup_get; |
| 240 | gc->set = gpio_mockup_set; |
| 241 | gc->direction_output = gpio_mockup_dirout; |
| 242 | gc->direction_input = gpio_mockup_dirin; |
| 243 | gc->get_direction = gpio_mockup_get_direction; |
Bartosz Golaszewski | e2ff740 | 2017-02-06 15:11:07 +0100 | [diff] [blame] | 244 | gc->to_irq = gpio_mockup_to_irq; |
Bartosz Golaszewski | ca409160 | 2017-02-06 13:10:35 +0100 | [diff] [blame] | 245 | |
Bartosz Golaszewski | f6ac438 | 2017-06-09 13:41:32 +0200 | [diff] [blame] | 246 | chip->lines = devm_kcalloc(dev, gc->ngpio, |
| 247 | sizeof(*chip->lines), GFP_KERNEL); |
Bartosz Golaszewski | e4ba07b | 2017-02-06 13:10:36 +0100 | [diff] [blame] | 248 | if (!chip->lines) |
| 249 | return -ENOMEM; |
Bartosz Golaszewski | ca409160 | 2017-02-06 13:10:35 +0100 | [diff] [blame] | 250 | |
Bartosz Golaszewski | 8a68ea0 | 2017-02-06 13:10:37 +0100 | [diff] [blame] | 251 | if (gpio_mockup_named_lines) { |
| 252 | ret = gpio_mockup_name_lines(dev, chip); |
| 253 | if (ret) |
| 254 | return ret; |
| 255 | } |
| 256 | |
Bartosz Golaszewski | b4c495f | 2017-08-14 13:20:56 +0200 | [diff] [blame] | 257 | ret = devm_irq_sim_init(dev, &chip->irqsim, gc->ngpio); |
Bartosz Golaszewski | e2ff740 | 2017-02-06 15:11:07 +0100 | [diff] [blame] | 258 | if (ret) |
| 259 | return ret; |
| 260 | |
Bartosz Golaszewski | 9202ba2 | 2017-02-06 15:11:08 +0100 | [diff] [blame] | 261 | ret = devm_gpiochip_add_data(dev, &chip->gc, chip); |
| 262 | if (ret) |
| 263 | return ret; |
| 264 | |
| 265 | if (gpio_mockup_dbg_dir) |
| 266 | gpio_mockup_debugfs_setup(dev, chip); |
| 267 | |
| 268 | return 0; |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 269 | } |
| 270 | |
Bartosz Golaszewski | ca409160 | 2017-02-06 13:10:35 +0100 | [diff] [blame] | 271 | static int gpio_mockup_probe(struct platform_device *pdev) |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 272 | { |
Bartosz Golaszewski | b652336 | 2017-06-09 13:41:28 +0200 | [diff] [blame] | 273 | int ret, i, base, ngpio, num_chips; |
Bartosz Golaszewski | 364ac45 | 2016-12-20 12:28:20 +0100 | [diff] [blame] | 274 | struct device *dev = &pdev->dev; |
Bartosz Golaszewski | b652336 | 2017-06-09 13:41:28 +0200 | [diff] [blame] | 275 | struct gpio_mockup_chip *chips; |
Bartosz Golaszewski | ad6d800 | 2016-12-20 12:28:19 +0100 | [diff] [blame] | 276 | char *chip_name; |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 277 | |
Bartosz Golaszewski | b652336 | 2017-06-09 13:41:28 +0200 | [diff] [blame] | 278 | /* Each chip is described by two values. */ |
| 279 | num_chips = gpio_mockup_params_nr / 2; |
| 280 | |
Bartosz Golaszewski | f6ac438 | 2017-06-09 13:41:32 +0200 | [diff] [blame] | 281 | chips = devm_kcalloc(dev, num_chips, sizeof(*chips), GFP_KERNEL); |
Bartosz Golaszewski | ca409160 | 2017-02-06 13:10:35 +0100 | [diff] [blame] | 282 | if (!chips) |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 283 | return -ENOMEM; |
| 284 | |
Bartosz Golaszewski | ca409160 | 2017-02-06 13:10:35 +0100 | [diff] [blame] | 285 | platform_set_drvdata(pdev, chips); |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 286 | |
Bartosz Golaszewski | b652336 | 2017-06-09 13:41:28 +0200 | [diff] [blame] | 287 | for (i = 0; i < num_chips; i++) { |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 288 | base = gpio_mockup_ranges[i * 2]; |
Bartosz Golaszewski | ca409160 | 2017-02-06 13:10:35 +0100 | [diff] [blame] | 289 | |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 290 | if (base == -1) |
| 291 | ngpio = gpio_mockup_ranges[i * 2 + 1]; |
| 292 | else |
| 293 | ngpio = gpio_mockup_ranges[i * 2 + 1] - base; |
| 294 | |
| 295 | if (ngpio >= 0) { |
Bartosz Golaszewski | ad6d800 | 2016-12-20 12:28:19 +0100 | [diff] [blame] | 296 | chip_name = devm_kasprintf(dev, GFP_KERNEL, |
Bartosz Golaszewski | ca409160 | 2017-02-06 13:10:35 +0100 | [diff] [blame] | 297 | "%s-%c", GPIO_MOCKUP_NAME, |
| 298 | gpio_mockup_name_start + i); |
Bartosz Golaszewski | ad6d800 | 2016-12-20 12:28:19 +0100 | [diff] [blame] | 299 | if (!chip_name) |
| 300 | return -ENOMEM; |
| 301 | |
Bartosz Golaszewski | ca409160 | 2017-02-06 13:10:35 +0100 | [diff] [blame] | 302 | ret = gpio_mockup_add(dev, &chips[i], |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 303 | chip_name, base, ngpio); |
| 304 | } else { |
Bartosz Golaszewski | 4dc9d76 | 2017-06-09 13:41:29 +0200 | [diff] [blame] | 305 | ret = -EINVAL; |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 306 | } |
Bartosz Golaszewski | ca409160 | 2017-02-06 13:10:35 +0100 | [diff] [blame] | 307 | |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 308 | if (ret) { |
Bartosz Golaszewski | ec604f1 | 2017-06-09 13:41:30 +0200 | [diff] [blame] | 309 | dev_err(dev, |
| 310 | "adding gpiochip failed: %d (base: %d, ngpio: %d)\n", |
| 311 | ret, base, base < 0 ? ngpio : base + ngpio); |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 312 | |
| 313 | return ret; |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | return 0; |
| 318 | } |
| 319 | |
Bartosz Golaszewski | ca409160 | 2017-02-06 13:10:35 +0100 | [diff] [blame] | 320 | static struct platform_driver gpio_mockup_driver = { |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 321 | .driver = { |
Bartosz Golaszewski | ca409160 | 2017-02-06 13:10:35 +0100 | [diff] [blame] | 322 | .name = GPIO_MOCKUP_NAME, |
Bartosz Golaszewski | 364ac45 | 2016-12-20 12:28:20 +0100 | [diff] [blame] | 323 | }, |
Bartosz Golaszewski | ca409160 | 2017-02-06 13:10:35 +0100 | [diff] [blame] | 324 | .probe = gpio_mockup_probe, |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 325 | }; |
| 326 | |
Bartosz Golaszewski | f3b4717 | 2017-11-27 11:48:37 +0100 | [diff] [blame] | 327 | static struct platform_device *gpio_mockup_pdev; |
| 328 | |
| 329 | static int __init gpio_mockup_init(void) |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 330 | { |
| 331 | int err; |
| 332 | |
Bartosz Golaszewski | 20c35ac | 2017-11-27 11:48:38 +0100 | [diff] [blame^] | 333 | if (gpio_mockup_params_nr < 2 || (gpio_mockup_params_nr % 2)) |
| 334 | return -EINVAL; |
| 335 | |
Bartosz Golaszewski | 9202ba2 | 2017-02-06 15:11:08 +0100 | [diff] [blame] | 336 | gpio_mockup_dbg_dir = debugfs_create_dir("gpio-mockup-event", NULL); |
| 337 | if (!gpio_mockup_dbg_dir) |
| 338 | pr_err("%s: error creating debugfs directory\n", |
| 339 | GPIO_MOCKUP_NAME); |
| 340 | |
Bartosz Golaszewski | f3b4717 | 2017-11-27 11:48:37 +0100 | [diff] [blame] | 341 | gpio_mockup_pdev = platform_device_alloc(GPIO_MOCKUP_NAME, -1); |
| 342 | if (!gpio_mockup_pdev) |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 343 | return -ENOMEM; |
| 344 | |
Bartosz Golaszewski | f3b4717 | 2017-11-27 11:48:37 +0100 | [diff] [blame] | 345 | err = platform_device_add(gpio_mockup_pdev); |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 346 | if (err) { |
Bartosz Golaszewski | f3b4717 | 2017-11-27 11:48:37 +0100 | [diff] [blame] | 347 | platform_device_put(gpio_mockup_pdev); |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 348 | return err; |
| 349 | } |
| 350 | |
Bartosz Golaszewski | ca409160 | 2017-02-06 13:10:35 +0100 | [diff] [blame] | 351 | err = platform_driver_register(&gpio_mockup_driver); |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 352 | if (err) { |
Bartosz Golaszewski | f3b4717 | 2017-11-27 11:48:37 +0100 | [diff] [blame] | 353 | platform_device_unregister(gpio_mockup_pdev); |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 354 | return err; |
| 355 | } |
| 356 | |
| 357 | return 0; |
| 358 | } |
| 359 | |
Bartosz Golaszewski | f3b4717 | 2017-11-27 11:48:37 +0100 | [diff] [blame] | 360 | static void __exit gpio_mockup_exit(void) |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 361 | { |
Bartosz Golaszewski | 9202ba2 | 2017-02-06 15:11:08 +0100 | [diff] [blame] | 362 | debugfs_remove_recursive(gpio_mockup_dbg_dir); |
Bartosz Golaszewski | ca409160 | 2017-02-06 13:10:35 +0100 | [diff] [blame] | 363 | platform_driver_unregister(&gpio_mockup_driver); |
Bartosz Golaszewski | f3b4717 | 2017-11-27 11:48:37 +0100 | [diff] [blame] | 364 | platform_device_unregister(gpio_mockup_pdev); |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 365 | } |
| 366 | |
Bartosz Golaszewski | f3b4717 | 2017-11-27 11:48:37 +0100 | [diff] [blame] | 367 | module_init(gpio_mockup_init); |
| 368 | module_exit(gpio_mockup_exit); |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 369 | |
| 370 | MODULE_AUTHOR("Kamlakant Patel <kamlakant.patel@broadcom.com>"); |
| 371 | MODULE_AUTHOR("Bamvor Jian Zhang <bamvor.zhangjian@linaro.org>"); |
Bartosz Golaszewski | c60c7f4 | 2017-06-09 13:41:31 +0200 | [diff] [blame] | 372 | MODULE_AUTHOR("Bartosz Golaszewski <brgl@bgdev.pl>"); |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 373 | MODULE_DESCRIPTION("GPIO Testing driver"); |
| 374 | MODULE_LICENSE("GPL v2"); |