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" |
Bartosz Golaszewski | b9576d0 | 2017-11-27 11:48:42 +0100 | [diff] [blame^] | 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 | f1bec99 | 2017-11-27 11:48:41 +0100 | [diff] [blame] | 37 | #define gpio_mockup_err(...) pr_err(GPIO_MOCKUP_NAME ": " __VA_ARGS__) |
| 38 | |
Bartosz Golaszewski | ca409160 | 2017-02-06 13:10:35 +0100 | [diff] [blame] | 39 | enum { |
Bartosz Golaszewski | c650c00 | 2017-05-25 10:33:39 +0200 | [diff] [blame] | 40 | GPIO_MOCKUP_DIR_OUT = 0, |
| 41 | GPIO_MOCKUP_DIR_IN = 1, |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 42 | }; |
| 43 | |
| 44 | /* |
| 45 | * struct gpio_pin_status - structure describing a GPIO status |
| 46 | * @dir: Configures direction of gpio as "in" or "out", 0=in, 1=out |
| 47 | * @value: Configures status of the gpio as 0(low) or 1(high) |
| 48 | */ |
Bartosz Golaszewski | ca409160 | 2017-02-06 13:10:35 +0100 | [diff] [blame] | 49 | struct gpio_mockup_line_status { |
| 50 | int dir; |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 51 | bool value; |
Bartosz Golaszewski | e2ff740 | 2017-02-06 15:11:07 +0100 | [diff] [blame] | 52 | }; |
| 53 | |
Bartosz Golaszewski | ca409160 | 2017-02-06 13:10:35 +0100 | [diff] [blame] | 54 | struct gpio_mockup_chip { |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 55 | struct gpio_chip gc; |
Bartosz Golaszewski | ca409160 | 2017-02-06 13:10:35 +0100 | [diff] [blame] | 56 | struct gpio_mockup_line_status *lines; |
Bartosz Golaszewski | b4c495f | 2017-08-14 13:20:56 +0200 | [diff] [blame] | 57 | struct irq_sim irqsim; |
Bartosz Golaszewski | 9202ba2 | 2017-02-06 15:11:08 +0100 | [diff] [blame] | 58 | struct dentry *dbg_dir; |
| 59 | }; |
| 60 | |
| 61 | struct gpio_mockup_dbgfs_private { |
| 62 | struct gpio_mockup_chip *chip; |
| 63 | struct gpio_desc *desc; |
| 64 | int offset; |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 65 | }; |
| 66 | |
Bartosz Golaszewski | 8a39f59 | 2017-11-27 11:48:40 +0100 | [diff] [blame] | 67 | struct gpio_mockup_platform_data { |
| 68 | int base; |
| 69 | int ngpio; |
| 70 | int index; |
| 71 | }; |
| 72 | |
Bartosz Golaszewski | b652336 | 2017-06-09 13:41:28 +0200 | [diff] [blame] | 73 | static int gpio_mockup_ranges[GPIO_MOCKUP_MAX_RANGES]; |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 74 | static int gpio_mockup_params_nr; |
| 75 | module_param_array(gpio_mockup_ranges, int, &gpio_mockup_params_nr, 0400); |
| 76 | |
Bartosz Golaszewski | 8a68ea0 | 2017-02-06 13:10:37 +0100 | [diff] [blame] | 77 | static bool gpio_mockup_named_lines; |
| 78 | module_param_named(gpio_mockup_named_lines, |
| 79 | gpio_mockup_named_lines, bool, 0400); |
| 80 | |
Bartosz Golaszewski | 9202ba2 | 2017-02-06 15:11:08 +0100 | [diff] [blame] | 81 | static struct dentry *gpio_mockup_dbg_dir; |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 82 | |
Bartosz Golaszewski | ca409160 | 2017-02-06 13:10:35 +0100 | [diff] [blame] | 83 | 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] | 84 | { |
Bartosz Golaszewski | ca409160 | 2017-02-06 13:10:35 +0100 | [diff] [blame] | 85 | struct gpio_mockup_chip *chip = gpiochip_get_data(gc); |
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 | return chip->lines[offset].value; |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 88 | } |
| 89 | |
Bartosz Golaszewski | ca409160 | 2017-02-06 13:10:35 +0100 | [diff] [blame] | 90 | 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] | 91 | int value) |
| 92 | { |
Bartosz Golaszewski | ca409160 | 2017-02-06 13:10:35 +0100 | [diff] [blame] | 93 | struct gpio_mockup_chip *chip = gpiochip_get_data(gc); |
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 | chip->lines[offset].value = !!value; |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 96 | } |
| 97 | |
Bartosz Golaszewski | ca409160 | 2017-02-06 13:10:35 +0100 | [diff] [blame] | 98 | 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] | 99 | int value) |
| 100 | { |
Bartosz Golaszewski | ca409160 | 2017-02-06 13:10:35 +0100 | [diff] [blame] | 101 | struct gpio_mockup_chip *chip = gpiochip_get_data(gc); |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 102 | |
Bartosz Golaszewski | ca409160 | 2017-02-06 13:10:35 +0100 | [diff] [blame] | 103 | gpio_mockup_set(gc, offset, value); |
Bartosz Golaszewski | c650c00 | 2017-05-25 10:33:39 +0200 | [diff] [blame] | 104 | chip->lines[offset].dir = GPIO_MOCKUP_DIR_OUT; |
Bartosz Golaszewski | ca409160 | 2017-02-06 13:10:35 +0100 | [diff] [blame] | 105 | |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 106 | return 0; |
| 107 | } |
| 108 | |
Bartosz Golaszewski | ca409160 | 2017-02-06 13:10:35 +0100 | [diff] [blame] | 109 | 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] | 110 | { |
Bartosz Golaszewski | ca409160 | 2017-02-06 13:10:35 +0100 | [diff] [blame] | 111 | struct gpio_mockup_chip *chip = gpiochip_get_data(gc); |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 112 | |
Bartosz Golaszewski | c650c00 | 2017-05-25 10:33:39 +0200 | [diff] [blame] | 113 | chip->lines[offset].dir = GPIO_MOCKUP_DIR_IN; |
Bartosz Golaszewski | ca409160 | 2017-02-06 13:10:35 +0100 | [diff] [blame] | 114 | |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 115 | return 0; |
| 116 | } |
| 117 | |
Bartosz Golaszewski | ca409160 | 2017-02-06 13:10:35 +0100 | [diff] [blame] | 118 | 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] | 119 | { |
Bartosz Golaszewski | ca409160 | 2017-02-06 13:10:35 +0100 | [diff] [blame] | 120 | struct gpio_mockup_chip *chip = gpiochip_get_data(gc); |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 121 | |
Bartosz Golaszewski | ca409160 | 2017-02-06 13:10:35 +0100 | [diff] [blame] | 122 | return chip->lines[offset].dir; |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 123 | } |
| 124 | |
Bartosz Golaszewski | 8a68ea0 | 2017-02-06 13:10:37 +0100 | [diff] [blame] | 125 | static int gpio_mockup_name_lines(struct device *dev, |
| 126 | struct gpio_mockup_chip *chip) |
| 127 | { |
| 128 | struct gpio_chip *gc = &chip->gc; |
| 129 | char **names; |
| 130 | int i; |
| 131 | |
Bartosz Golaszewski | f6ac438 | 2017-06-09 13:41:32 +0200 | [diff] [blame] | 132 | names = devm_kcalloc(dev, gc->ngpio, sizeof(char *), GFP_KERNEL); |
Bartosz Golaszewski | 8a68ea0 | 2017-02-06 13:10:37 +0100 | [diff] [blame] | 133 | if (!names) |
| 134 | return -ENOMEM; |
| 135 | |
| 136 | for (i = 0; i < gc->ngpio; i++) { |
| 137 | names[i] = devm_kasprintf(dev, GFP_KERNEL, |
| 138 | "%s-%d", gc->label, i); |
| 139 | if (!names[i]) |
| 140 | return -ENOMEM; |
| 141 | } |
| 142 | |
| 143 | gc->names = (const char *const *)names; |
| 144 | |
| 145 | return 0; |
| 146 | } |
| 147 | |
Bartosz Golaszewski | b4c495f | 2017-08-14 13:20:56 +0200 | [diff] [blame] | 148 | 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] | 149 | { |
Bartosz Golaszewski | c7f5326 | 2017-05-25 10:33:41 +0200 | [diff] [blame] | 150 | struct gpio_mockup_chip *chip = gpiochip_get_data(gc); |
| 151 | |
Bartosz Golaszewski | b4c495f | 2017-08-14 13:20:56 +0200 | [diff] [blame] | 152 | return irq_sim_irqnum(&chip->irqsim, offset); |
Bartosz Golaszewski | e2ff740 | 2017-02-06 15:11:07 +0100 | [diff] [blame] | 153 | } |
| 154 | |
Bartosz Golaszewski | 9202ba2 | 2017-02-06 15:11:08 +0100 | [diff] [blame] | 155 | static ssize_t gpio_mockup_event_write(struct file *file, |
| 156 | const char __user *usr_buf, |
| 157 | size_t size, loff_t *ppos) |
| 158 | { |
| 159 | struct gpio_mockup_dbgfs_private *priv; |
| 160 | struct gpio_mockup_chip *chip; |
| 161 | struct seq_file *sfile; |
| 162 | struct gpio_desc *desc; |
Bartosz Golaszewski | 6f9b3e7 | 2017-06-09 13:41:25 +0200 | [diff] [blame] | 163 | int rv, val; |
Bartosz Golaszewski | 9202ba2 | 2017-02-06 15:11:08 +0100 | [diff] [blame] | 164 | |
Bartosz Golaszewski | 6f9b3e7 | 2017-06-09 13:41:25 +0200 | [diff] [blame] | 165 | rv = kstrtoint_from_user(usr_buf, size, 0, &val); |
| 166 | if (rv) |
| 167 | return rv; |
| 168 | if (val != 0 && val != 1) |
| 169 | return -EINVAL; |
| 170 | |
Bartosz Golaszewski | 650b57b | 2017-06-09 13:41:26 +0200 | [diff] [blame] | 171 | sfile = file->private_data; |
| 172 | priv = sfile->private; |
| 173 | desc = priv->desc; |
| 174 | chip = priv->chip; |
Bartosz Golaszewski | c7f5326 | 2017-05-25 10:33:41 +0200 | [diff] [blame] | 175 | |
Bartosz Golaszewski | b4c495f | 2017-08-14 13:20:56 +0200 | [diff] [blame] | 176 | gpiod_set_value_cansleep(desc, val); |
| 177 | irq_sim_fire(&chip->irqsim, priv->offset); |
Bartosz Golaszewski | 9202ba2 | 2017-02-06 15:11:08 +0100 | [diff] [blame] | 178 | |
| 179 | return size; |
| 180 | } |
| 181 | |
| 182 | static int gpio_mockup_event_open(struct inode *inode, struct file *file) |
| 183 | { |
| 184 | return single_open(file, NULL, inode->i_private); |
| 185 | } |
| 186 | |
| 187 | static const struct file_operations gpio_mockup_event_ops = { |
| 188 | .owner = THIS_MODULE, |
| 189 | .open = gpio_mockup_event_open, |
| 190 | .write = gpio_mockup_event_write, |
| 191 | .llseek = no_llseek, |
| 192 | }; |
| 193 | |
| 194 | static void gpio_mockup_debugfs_setup(struct device *dev, |
| 195 | struct gpio_mockup_chip *chip) |
| 196 | { |
| 197 | struct gpio_mockup_dbgfs_private *priv; |
| 198 | struct dentry *evfile; |
| 199 | struct gpio_chip *gc; |
| 200 | char *name; |
| 201 | int i; |
| 202 | |
| 203 | gc = &chip->gc; |
| 204 | |
| 205 | chip->dbg_dir = debugfs_create_dir(gc->label, gpio_mockup_dbg_dir); |
| 206 | if (!chip->dbg_dir) |
| 207 | goto err; |
| 208 | |
| 209 | for (i = 0; i < gc->ngpio; i++) { |
| 210 | name = devm_kasprintf(dev, GFP_KERNEL, "%d", i); |
| 211 | if (!name) |
| 212 | goto err; |
| 213 | |
| 214 | priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); |
| 215 | if (!priv) |
| 216 | goto err; |
| 217 | |
| 218 | priv->chip = chip; |
| 219 | priv->offset = i; |
| 220 | priv->desc = &gc->gpiodev->descs[i]; |
| 221 | |
| 222 | evfile = debugfs_create_file(name, 0200, chip->dbg_dir, priv, |
| 223 | &gpio_mockup_event_ops); |
| 224 | if (!evfile) |
| 225 | goto err; |
| 226 | } |
| 227 | |
| 228 | return; |
| 229 | |
| 230 | err: |
| 231 | dev_err(dev, "error creating debugfs directory\n"); |
| 232 | } |
| 233 | |
Bartosz Golaszewski | ca409160 | 2017-02-06 13:10:35 +0100 | [diff] [blame] | 234 | static int gpio_mockup_add(struct device *dev, |
| 235 | struct gpio_mockup_chip *chip, |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 236 | const char *name, int base, int ngpio) |
| 237 | { |
Bartosz Golaszewski | ca409160 | 2017-02-06 13:10:35 +0100 | [diff] [blame] | 238 | struct gpio_chip *gc = &chip->gc; |
Bartosz Golaszewski | 8a68ea0 | 2017-02-06 13:10:37 +0100 | [diff] [blame] | 239 | int ret; |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 240 | |
Bartosz Golaszewski | ca409160 | 2017-02-06 13:10:35 +0100 | [diff] [blame] | 241 | gc->base = base; |
| 242 | gc->ngpio = ngpio; |
| 243 | gc->label = name; |
| 244 | gc->owner = THIS_MODULE; |
| 245 | gc->parent = dev; |
| 246 | gc->get = gpio_mockup_get; |
| 247 | gc->set = gpio_mockup_set; |
| 248 | gc->direction_output = gpio_mockup_dirout; |
| 249 | gc->direction_input = gpio_mockup_dirin; |
| 250 | gc->get_direction = gpio_mockup_get_direction; |
Bartosz Golaszewski | e2ff740 | 2017-02-06 15:11:07 +0100 | [diff] [blame] | 251 | gc->to_irq = gpio_mockup_to_irq; |
Bartosz Golaszewski | ca409160 | 2017-02-06 13:10:35 +0100 | [diff] [blame] | 252 | |
Bartosz Golaszewski | f6ac438 | 2017-06-09 13:41:32 +0200 | [diff] [blame] | 253 | chip->lines = devm_kcalloc(dev, gc->ngpio, |
| 254 | sizeof(*chip->lines), GFP_KERNEL); |
Bartosz Golaszewski | e4ba07b | 2017-02-06 13:10:36 +0100 | [diff] [blame] | 255 | if (!chip->lines) |
| 256 | return -ENOMEM; |
Bartosz Golaszewski | ca409160 | 2017-02-06 13:10:35 +0100 | [diff] [blame] | 257 | |
Bartosz Golaszewski | 8a68ea0 | 2017-02-06 13:10:37 +0100 | [diff] [blame] | 258 | if (gpio_mockup_named_lines) { |
| 259 | ret = gpio_mockup_name_lines(dev, chip); |
| 260 | if (ret) |
| 261 | return ret; |
| 262 | } |
| 263 | |
Bartosz Golaszewski | b4c495f | 2017-08-14 13:20:56 +0200 | [diff] [blame] | 264 | ret = devm_irq_sim_init(dev, &chip->irqsim, gc->ngpio); |
Bartosz Golaszewski | e2ff740 | 2017-02-06 15:11:07 +0100 | [diff] [blame] | 265 | if (ret) |
| 266 | return ret; |
| 267 | |
Bartosz Golaszewski | 9202ba2 | 2017-02-06 15:11:08 +0100 | [diff] [blame] | 268 | ret = devm_gpiochip_add_data(dev, &chip->gc, chip); |
| 269 | if (ret) |
| 270 | return ret; |
| 271 | |
| 272 | if (gpio_mockup_dbg_dir) |
| 273 | gpio_mockup_debugfs_setup(dev, chip); |
| 274 | |
| 275 | return 0; |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 276 | } |
| 277 | |
Bartosz Golaszewski | ca409160 | 2017-02-06 13:10:35 +0100 | [diff] [blame] | 278 | static int gpio_mockup_probe(struct platform_device *pdev) |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 279 | { |
Bartosz Golaszewski | 8a39f59 | 2017-11-27 11:48:40 +0100 | [diff] [blame] | 280 | struct gpio_mockup_platform_data *pdata; |
| 281 | struct gpio_mockup_chip *chip; |
| 282 | int rv, base, ngpio; |
| 283 | struct device *dev; |
| 284 | char *name; |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 285 | |
Bartosz Golaszewski | 8a39f59 | 2017-11-27 11:48:40 +0100 | [diff] [blame] | 286 | dev = &pdev->dev; |
| 287 | pdata = dev_get_platdata(dev); |
| 288 | base = pdata->base; |
| 289 | ngpio = pdata->ngpio; |
Bartosz Golaszewski | b652336 | 2017-06-09 13:41:28 +0200 | [diff] [blame] | 290 | |
Bartosz Golaszewski | 8a39f59 | 2017-11-27 11:48:40 +0100 | [diff] [blame] | 291 | chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL); |
| 292 | if (!chip) |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 293 | return -ENOMEM; |
| 294 | |
Bartosz Golaszewski | 8a39f59 | 2017-11-27 11:48:40 +0100 | [diff] [blame] | 295 | name = devm_kasprintf(dev, GFP_KERNEL, "%s-%c", |
| 296 | pdev->name, pdata->index); |
| 297 | if (!name) |
| 298 | return -ENOMEM; |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 299 | |
Bartosz Golaszewski | 8a39f59 | 2017-11-27 11:48:40 +0100 | [diff] [blame] | 300 | rv = gpio_mockup_add(dev, chip, name, base, ngpio); |
| 301 | if (rv) { |
| 302 | dev_err(dev, |
| 303 | "adding gpiochip failed (base: %d, ngpio: %d)\n", |
| 304 | base, base < 0 ? ngpio : base + ngpio); |
| 305 | return rv; |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | return 0; |
| 309 | } |
| 310 | |
Bartosz Golaszewski | ca409160 | 2017-02-06 13:10:35 +0100 | [diff] [blame] | 311 | static struct platform_driver gpio_mockup_driver = { |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 312 | .driver = { |
Bartosz Golaszewski | ca409160 | 2017-02-06 13:10:35 +0100 | [diff] [blame] | 313 | .name = GPIO_MOCKUP_NAME, |
Bartosz Golaszewski | 364ac45 | 2016-12-20 12:28:20 +0100 | [diff] [blame] | 314 | }, |
Bartosz Golaszewski | ca409160 | 2017-02-06 13:10:35 +0100 | [diff] [blame] | 315 | .probe = gpio_mockup_probe, |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 316 | }; |
| 317 | |
Bartosz Golaszewski | 8a39f59 | 2017-11-27 11:48:40 +0100 | [diff] [blame] | 318 | static struct platform_device *gpio_mockup_pdevs[GPIO_MOCKUP_MAX_GC]; |
| 319 | |
| 320 | static void gpio_mockup_unregister_pdevs(void) |
| 321 | { |
| 322 | struct platform_device *pdev; |
| 323 | int i; |
| 324 | |
| 325 | for (i = 0; i < GPIO_MOCKUP_MAX_GC; i++) { |
| 326 | pdev = gpio_mockup_pdevs[i]; |
| 327 | |
| 328 | if (pdev) |
| 329 | platform_device_unregister(pdev); |
| 330 | } |
| 331 | } |
Bartosz Golaszewski | f3b4717 | 2017-11-27 11:48:37 +0100 | [diff] [blame] | 332 | |
| 333 | static int __init gpio_mockup_init(void) |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 334 | { |
Bartosz Golaszewski | 8a39f59 | 2017-11-27 11:48:40 +0100 | [diff] [blame] | 335 | int i, num_chips, err = 0, index = 'A'; |
| 336 | struct gpio_mockup_platform_data pdata; |
| 337 | struct platform_device *pdev; |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 338 | |
Bartosz Golaszewski | b447a75 | 2017-11-27 11:48:39 +0100 | [diff] [blame] | 339 | if ((gpio_mockup_params_nr < 2) || |
| 340 | (gpio_mockup_params_nr % 2) || |
| 341 | (gpio_mockup_params_nr > GPIO_MOCKUP_MAX_RANGES)) |
Bartosz Golaszewski | 20c35ac | 2017-11-27 11:48:38 +0100 | [diff] [blame] | 342 | return -EINVAL; |
| 343 | |
Bartosz Golaszewski | 8a39f59 | 2017-11-27 11:48:40 +0100 | [diff] [blame] | 344 | /* Each chip is described by two values. */ |
| 345 | num_chips = gpio_mockup_params_nr / 2; |
| 346 | |
Bartosz Golaszewski | 9202ba2 | 2017-02-06 15:11:08 +0100 | [diff] [blame] | 347 | gpio_mockup_dbg_dir = debugfs_create_dir("gpio-mockup-event", NULL); |
| 348 | if (!gpio_mockup_dbg_dir) |
Bartosz Golaszewski | f1bec99 | 2017-11-27 11:48:41 +0100 | [diff] [blame] | 349 | gpio_mockup_err("error creating debugfs directory\n"); |
Bartosz Golaszewski | 9202ba2 | 2017-02-06 15:11:08 +0100 | [diff] [blame] | 350 | |
Bartosz Golaszewski | 8a39f59 | 2017-11-27 11:48:40 +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 | f1bec99 | 2017-11-27 11:48:41 +0100 | [diff] [blame] | 353 | gpio_mockup_err("error registering platform driver\n"); |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 354 | return err; |
| 355 | } |
| 356 | |
Bartosz Golaszewski | 8a39f59 | 2017-11-27 11:48:40 +0100 | [diff] [blame] | 357 | for (i = 0; i < num_chips; i++) { |
| 358 | pdata.index = index++; |
| 359 | pdata.base = gpio_mockup_ranges[i * 2]; |
| 360 | pdata.ngpio = pdata.base < 0 |
| 361 | ? gpio_mockup_ranges[i * 2 + 1] |
| 362 | : gpio_mockup_ranges[i * 2 + 1] - pdata.base; |
| 363 | |
| 364 | pdev = platform_device_register_resndata(NULL, |
| 365 | GPIO_MOCKUP_NAME, |
| 366 | i, NULL, 0, &pdata, |
| 367 | sizeof(pdata)); |
| 368 | if (!pdev) { |
Bartosz Golaszewski | f1bec99 | 2017-11-27 11:48:41 +0100 | [diff] [blame] | 369 | gpio_mockup_err("error registering device"); |
Bartosz Golaszewski | 8a39f59 | 2017-11-27 11:48:40 +0100 | [diff] [blame] | 370 | platform_driver_unregister(&gpio_mockup_driver); |
| 371 | gpio_mockup_unregister_pdevs(); |
| 372 | return -ENOMEM; |
| 373 | } |
| 374 | |
| 375 | gpio_mockup_pdevs[i] = pdev; |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | return 0; |
| 379 | } |
| 380 | |
Bartosz Golaszewski | f3b4717 | 2017-11-27 11:48:37 +0100 | [diff] [blame] | 381 | static void __exit gpio_mockup_exit(void) |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 382 | { |
Bartosz Golaszewski | 9202ba2 | 2017-02-06 15:11:08 +0100 | [diff] [blame] | 383 | debugfs_remove_recursive(gpio_mockup_dbg_dir); |
Bartosz Golaszewski | ca409160 | 2017-02-06 13:10:35 +0100 | [diff] [blame] | 384 | platform_driver_unregister(&gpio_mockup_driver); |
Bartosz Golaszewski | 8a39f59 | 2017-11-27 11:48:40 +0100 | [diff] [blame] | 385 | gpio_mockup_unregister_pdevs(); |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 386 | } |
| 387 | |
Bartosz Golaszewski | f3b4717 | 2017-11-27 11:48:37 +0100 | [diff] [blame] | 388 | module_init(gpio_mockup_init); |
| 389 | module_exit(gpio_mockup_exit); |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 390 | |
| 391 | MODULE_AUTHOR("Kamlakant Patel <kamlakant.patel@broadcom.com>"); |
| 392 | MODULE_AUTHOR("Bamvor Jian Zhang <bamvor.zhangjian@linaro.org>"); |
Bartosz Golaszewski | c60c7f4 | 2017-06-09 13:41:31 +0200 | [diff] [blame] | 393 | MODULE_AUTHOR("Bartosz Golaszewski <brgl@bgdev.pl>"); |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 394 | MODULE_DESCRIPTION("GPIO Testing driver"); |
| 395 | MODULE_LICENSE("GPL v2"); |