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> |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify it |
| 8 | * under the terms of the GNU General Public License as published by the |
| 9 | * Free Software Foundation; either version 2 of the License, or (at your |
| 10 | * option) any later version. |
| 11 | * |
| 12 | */ |
| 13 | |
| 14 | #include <linux/init.h> |
| 15 | #include <linux/module.h> |
| 16 | #include <linux/gpio/driver.h> |
Bartosz Golaszewski | 9202ba2 | 2017-02-06 15:11:08 +0100 | [diff] [blame] | 17 | #include <linux/gpio/consumer.h> |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 18 | #include <linux/platform_device.h> |
Bartosz Golaszewski | 8a68ea0 | 2017-02-06 13:10:37 +0100 | [diff] [blame] | 19 | #include <linux/slab.h> |
Bartosz Golaszewski | e2ff740 | 2017-02-06 15:11:07 +0100 | [diff] [blame] | 20 | #include <linux/interrupt.h> |
| 21 | #include <linux/irq.h> |
| 22 | #include <linux/irq_work.h> |
Bartosz Golaszewski | 9202ba2 | 2017-02-06 15:11:08 +0100 | [diff] [blame] | 23 | #include <linux/debugfs.h> |
| 24 | #include <linux/uaccess.h> |
| 25 | |
| 26 | #include "gpiolib.h" |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 27 | |
Bartosz Golaszewski | ca409160 | 2017-02-06 13:10:35 +0100 | [diff] [blame] | 28 | #define GPIO_MOCKUP_NAME "gpio-mockup" |
| 29 | #define GPIO_MOCKUP_MAX_GC 10 |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 30 | |
Bartosz Golaszewski | ca409160 | 2017-02-06 13:10:35 +0100 | [diff] [blame] | 31 | enum { |
Bartosz Golaszewski | c650c00 | 2017-05-25 10:33:39 +0200 | [diff] [blame] | 32 | GPIO_MOCKUP_DIR_OUT = 0, |
| 33 | GPIO_MOCKUP_DIR_IN = 1, |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 34 | }; |
| 35 | |
| 36 | /* |
| 37 | * struct gpio_pin_status - structure describing a GPIO status |
| 38 | * @dir: Configures direction of gpio as "in" or "out", 0=in, 1=out |
| 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; |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 43 | bool value; |
| 44 | }; |
| 45 | |
Bartosz Golaszewski | e2ff740 | 2017-02-06 15:11:07 +0100 | [diff] [blame] | 46 | struct gpio_mockup_irq_context { |
| 47 | struct irq_work work; |
| 48 | int irq; |
| 49 | }; |
| 50 | |
Bartosz Golaszewski | ca409160 | 2017-02-06 13:10:35 +0100 | [diff] [blame] | 51 | struct gpio_mockup_chip { |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 52 | struct gpio_chip gc; |
Bartosz Golaszewski | ca409160 | 2017-02-06 13:10:35 +0100 | [diff] [blame] | 53 | struct gpio_mockup_line_status *lines; |
Bartosz Golaszewski | e2ff740 | 2017-02-06 15:11:07 +0100 | [diff] [blame] | 54 | struct gpio_mockup_irq_context irq_ctx; |
Bartosz Golaszewski | 9202ba2 | 2017-02-06 15:11:08 +0100 | [diff] [blame] | 55 | struct dentry *dbg_dir; |
| 56 | }; |
| 57 | |
| 58 | struct gpio_mockup_dbgfs_private { |
| 59 | struct gpio_mockup_chip *chip; |
| 60 | struct gpio_desc *desc; |
| 61 | int offset; |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 62 | }; |
| 63 | |
Bartosz Golaszewski | ca409160 | 2017-02-06 13:10:35 +0100 | [diff] [blame] | 64 | static int gpio_mockup_ranges[GPIO_MOCKUP_MAX_GC << 1]; |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 65 | static int gpio_mockup_params_nr; |
| 66 | module_param_array(gpio_mockup_ranges, int, &gpio_mockup_params_nr, 0400); |
| 67 | |
Bartosz Golaszewski | 8a68ea0 | 2017-02-06 13:10:37 +0100 | [diff] [blame] | 68 | static bool gpio_mockup_named_lines; |
| 69 | module_param_named(gpio_mockup_named_lines, |
| 70 | gpio_mockup_named_lines, bool, 0400); |
| 71 | |
Bartosz Golaszewski | ca409160 | 2017-02-06 13:10:35 +0100 | [diff] [blame] | 72 | static const char gpio_mockup_name_start = 'A'; |
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 | ca409160 | 2017-02-06 13:10:35 +0100 | [diff] [blame] | 75 | 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] | 76 | { |
Bartosz Golaszewski | ca409160 | 2017-02-06 13:10:35 +0100 | [diff] [blame] | 77 | struct gpio_mockup_chip *chip = gpiochip_get_data(gc); |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 78 | |
Bartosz Golaszewski | ca409160 | 2017-02-06 13:10:35 +0100 | [diff] [blame] | 79 | return chip->lines[offset].value; |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 80 | } |
| 81 | |
Bartosz Golaszewski | ca409160 | 2017-02-06 13:10:35 +0100 | [diff] [blame] | 82 | 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] | 83 | int value) |
| 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 | chip->lines[offset].value = !!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 int gpio_mockup_dirout(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 | gpio_mockup_set(gc, offset, value); |
Bartosz Golaszewski | c650c00 | 2017-05-25 10:33:39 +0200 | [diff] [blame] | 96 | chip->lines[offset].dir = GPIO_MOCKUP_DIR_OUT; |
Bartosz Golaszewski | ca409160 | 2017-02-06 13:10:35 +0100 | [diff] [blame] | 97 | |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 98 | return 0; |
| 99 | } |
| 100 | |
Bartosz Golaszewski | ca409160 | 2017-02-06 13:10:35 +0100 | [diff] [blame] | 101 | 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] | 102 | { |
Bartosz Golaszewski | ca409160 | 2017-02-06 13:10:35 +0100 | [diff] [blame] | 103 | struct gpio_mockup_chip *chip = gpiochip_get_data(gc); |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 104 | |
Bartosz Golaszewski | c650c00 | 2017-05-25 10:33:39 +0200 | [diff] [blame] | 105 | chip->lines[offset].dir = GPIO_MOCKUP_DIR_IN; |
Bartosz Golaszewski | ca409160 | 2017-02-06 13:10:35 +0100 | [diff] [blame] | 106 | |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 107 | return 0; |
| 108 | } |
| 109 | |
Bartosz Golaszewski | ca409160 | 2017-02-06 13:10:35 +0100 | [diff] [blame] | 110 | 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] | 111 | { |
Bartosz Golaszewski | ca409160 | 2017-02-06 13:10:35 +0100 | [diff] [blame] | 112 | struct gpio_mockup_chip *chip = gpiochip_get_data(gc); |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 113 | |
Bartosz Golaszewski | ca409160 | 2017-02-06 13:10:35 +0100 | [diff] [blame] | 114 | return chip->lines[offset].dir; |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 115 | } |
| 116 | |
Bartosz Golaszewski | 8a68ea0 | 2017-02-06 13:10:37 +0100 | [diff] [blame] | 117 | static int gpio_mockup_name_lines(struct device *dev, |
| 118 | struct gpio_mockup_chip *chip) |
| 119 | { |
| 120 | struct gpio_chip *gc = &chip->gc; |
| 121 | char **names; |
| 122 | int i; |
| 123 | |
| 124 | names = devm_kzalloc(dev, sizeof(char *) * gc->ngpio, GFP_KERNEL); |
| 125 | if (!names) |
| 126 | return -ENOMEM; |
| 127 | |
| 128 | for (i = 0; i < gc->ngpio; i++) { |
| 129 | names[i] = devm_kasprintf(dev, GFP_KERNEL, |
| 130 | "%s-%d", gc->label, i); |
| 131 | if (!names[i]) |
| 132 | return -ENOMEM; |
| 133 | } |
| 134 | |
| 135 | gc->names = (const char *const *)names; |
| 136 | |
| 137 | return 0; |
| 138 | } |
| 139 | |
Bartosz Golaszewski | e2ff740 | 2017-02-06 15:11:07 +0100 | [diff] [blame] | 140 | static int gpio_mockup_to_irq(struct gpio_chip *chip, unsigned int offset) |
| 141 | { |
| 142 | return chip->irq_base + offset; |
| 143 | } |
| 144 | |
| 145 | /* |
| 146 | * While we should generally support irqmask and irqunmask, this driver is |
| 147 | * for testing purposes only so we don't care. |
| 148 | */ |
| 149 | static void gpio_mockup_irqmask(struct irq_data *d) { } |
| 150 | static void gpio_mockup_irqunmask(struct irq_data *d) { } |
| 151 | |
| 152 | static struct irq_chip gpio_mockup_irqchip = { |
| 153 | .name = GPIO_MOCKUP_NAME, |
| 154 | .irq_mask = gpio_mockup_irqmask, |
| 155 | .irq_unmask = gpio_mockup_irqunmask, |
| 156 | }; |
| 157 | |
| 158 | static void gpio_mockup_handle_irq(struct irq_work *work) |
| 159 | { |
| 160 | struct gpio_mockup_irq_context *irq_ctx; |
| 161 | |
| 162 | irq_ctx = container_of(work, struct gpio_mockup_irq_context, work); |
| 163 | handle_simple_irq(irq_to_desc(irq_ctx->irq)); |
| 164 | } |
| 165 | |
| 166 | static int gpio_mockup_irqchip_setup(struct device *dev, |
| 167 | struct gpio_mockup_chip *chip) |
| 168 | { |
| 169 | struct gpio_chip *gc = &chip->gc; |
| 170 | int irq_base, i; |
| 171 | |
Bartosz Golaszewski | 62a7ca0 | 2017-03-04 17:23:29 +0100 | [diff] [blame] | 172 | irq_base = devm_irq_alloc_descs(dev, -1, 0, gc->ngpio, 0); |
Bartosz Golaszewski | e2ff740 | 2017-02-06 15:11:07 +0100 | [diff] [blame] | 173 | if (irq_base < 0) |
| 174 | return irq_base; |
| 175 | |
| 176 | gc->irq_base = irq_base; |
| 177 | gc->irqchip = &gpio_mockup_irqchip; |
| 178 | |
| 179 | for (i = 0; i < gc->ngpio; i++) { |
| 180 | irq_set_chip(irq_base + i, gc->irqchip); |
| 181 | irq_set_handler(irq_base + i, &handle_simple_irq); |
| 182 | irq_modify_status(irq_base + i, |
| 183 | IRQ_NOREQUEST | IRQ_NOAUTOEN, IRQ_NOPROBE); |
| 184 | } |
| 185 | |
| 186 | init_irq_work(&chip->irq_ctx.work, gpio_mockup_handle_irq); |
| 187 | |
| 188 | return 0; |
| 189 | } |
| 190 | |
Bartosz Golaszewski | 9202ba2 | 2017-02-06 15:11:08 +0100 | [diff] [blame] | 191 | static ssize_t gpio_mockup_event_write(struct file *file, |
| 192 | const char __user *usr_buf, |
| 193 | size_t size, loff_t *ppos) |
| 194 | { |
| 195 | struct gpio_mockup_dbgfs_private *priv; |
| 196 | struct gpio_mockup_chip *chip; |
| 197 | struct seq_file *sfile; |
| 198 | struct gpio_desc *desc; |
| 199 | struct gpio_chip *gc; |
Dan Carpenter | fa6256d | 2017-02-15 02:02:06 +0300 | [diff] [blame] | 200 | int val; |
Bartosz Golaszewski | 9202ba2 | 2017-02-06 15:11:08 +0100 | [diff] [blame] | 201 | char buf; |
| 202 | |
| 203 | sfile = file->private_data; |
| 204 | priv = sfile->private; |
| 205 | desc = priv->desc; |
| 206 | chip = priv->chip; |
| 207 | gc = &chip->gc; |
| 208 | |
Dan Carpenter | fa6256d | 2017-02-15 02:02:06 +0300 | [diff] [blame] | 209 | if (copy_from_user(&buf, usr_buf, 1)) |
| 210 | return -EFAULT; |
Bartosz Golaszewski | 9202ba2 | 2017-02-06 15:11:08 +0100 | [diff] [blame] | 211 | |
| 212 | if (buf == '0') |
| 213 | val = 0; |
| 214 | else if (buf == '1') |
| 215 | val = 1; |
| 216 | else |
| 217 | return -EINVAL; |
| 218 | |
| 219 | gpiod_set_value_cansleep(desc, val); |
| 220 | priv->chip->irq_ctx.irq = gc->irq_base + priv->offset; |
| 221 | irq_work_queue(&priv->chip->irq_ctx.work); |
| 222 | |
| 223 | return size; |
| 224 | } |
| 225 | |
| 226 | static int gpio_mockup_event_open(struct inode *inode, struct file *file) |
| 227 | { |
| 228 | return single_open(file, NULL, inode->i_private); |
| 229 | } |
| 230 | |
| 231 | static const struct file_operations gpio_mockup_event_ops = { |
| 232 | .owner = THIS_MODULE, |
| 233 | .open = gpio_mockup_event_open, |
| 234 | .write = gpio_mockup_event_write, |
| 235 | .llseek = no_llseek, |
| 236 | }; |
| 237 | |
| 238 | static void gpio_mockup_debugfs_setup(struct device *dev, |
| 239 | struct gpio_mockup_chip *chip) |
| 240 | { |
| 241 | struct gpio_mockup_dbgfs_private *priv; |
| 242 | struct dentry *evfile; |
| 243 | struct gpio_chip *gc; |
| 244 | char *name; |
| 245 | int i; |
| 246 | |
| 247 | gc = &chip->gc; |
| 248 | |
| 249 | chip->dbg_dir = debugfs_create_dir(gc->label, gpio_mockup_dbg_dir); |
| 250 | if (!chip->dbg_dir) |
| 251 | goto err; |
| 252 | |
| 253 | for (i = 0; i < gc->ngpio; i++) { |
| 254 | name = devm_kasprintf(dev, GFP_KERNEL, "%d", i); |
| 255 | if (!name) |
| 256 | goto err; |
| 257 | |
| 258 | priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); |
| 259 | if (!priv) |
| 260 | goto err; |
| 261 | |
| 262 | priv->chip = chip; |
| 263 | priv->offset = i; |
| 264 | priv->desc = &gc->gpiodev->descs[i]; |
| 265 | |
| 266 | evfile = debugfs_create_file(name, 0200, chip->dbg_dir, priv, |
| 267 | &gpio_mockup_event_ops); |
| 268 | if (!evfile) |
| 269 | goto err; |
| 270 | } |
| 271 | |
| 272 | return; |
| 273 | |
| 274 | err: |
| 275 | dev_err(dev, "error creating debugfs directory\n"); |
| 276 | } |
| 277 | |
Bartosz Golaszewski | ca409160 | 2017-02-06 13:10:35 +0100 | [diff] [blame] | 278 | static int gpio_mockup_add(struct device *dev, |
| 279 | struct gpio_mockup_chip *chip, |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 280 | const char *name, int base, int ngpio) |
| 281 | { |
Bartosz Golaszewski | ca409160 | 2017-02-06 13:10:35 +0100 | [diff] [blame] | 282 | struct gpio_chip *gc = &chip->gc; |
Bartosz Golaszewski | 8a68ea0 | 2017-02-06 13:10:37 +0100 | [diff] [blame] | 283 | int ret; |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 284 | |
Bartosz Golaszewski | ca409160 | 2017-02-06 13:10:35 +0100 | [diff] [blame] | 285 | gc->base = base; |
| 286 | gc->ngpio = ngpio; |
| 287 | gc->label = name; |
| 288 | gc->owner = THIS_MODULE; |
| 289 | gc->parent = dev; |
| 290 | gc->get = gpio_mockup_get; |
| 291 | gc->set = gpio_mockup_set; |
| 292 | gc->direction_output = gpio_mockup_dirout; |
| 293 | gc->direction_input = gpio_mockup_dirin; |
| 294 | gc->get_direction = gpio_mockup_get_direction; |
Bartosz Golaszewski | e2ff740 | 2017-02-06 15:11:07 +0100 | [diff] [blame] | 295 | gc->to_irq = gpio_mockup_to_irq; |
Bartosz Golaszewski | ca409160 | 2017-02-06 13:10:35 +0100 | [diff] [blame] | 296 | |
| 297 | chip->lines = devm_kzalloc(dev, sizeof(*chip->lines) * gc->ngpio, |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 298 | GFP_KERNEL); |
Bartosz Golaszewski | e4ba07b | 2017-02-06 13:10:36 +0100 | [diff] [blame] | 299 | if (!chip->lines) |
| 300 | return -ENOMEM; |
Bartosz Golaszewski | ca409160 | 2017-02-06 13:10:35 +0100 | [diff] [blame] | 301 | |
Bartosz Golaszewski | 8a68ea0 | 2017-02-06 13:10:37 +0100 | [diff] [blame] | 302 | if (gpio_mockup_named_lines) { |
| 303 | ret = gpio_mockup_name_lines(dev, chip); |
| 304 | if (ret) |
| 305 | return ret; |
| 306 | } |
| 307 | |
Bartosz Golaszewski | e2ff740 | 2017-02-06 15:11:07 +0100 | [diff] [blame] | 308 | ret = gpio_mockup_irqchip_setup(dev, chip); |
| 309 | if (ret) |
| 310 | return ret; |
| 311 | |
Bartosz Golaszewski | 9202ba2 | 2017-02-06 15:11:08 +0100 | [diff] [blame] | 312 | ret = devm_gpiochip_add_data(dev, &chip->gc, chip); |
| 313 | if (ret) |
| 314 | return ret; |
| 315 | |
| 316 | if (gpio_mockup_dbg_dir) |
| 317 | gpio_mockup_debugfs_setup(dev, chip); |
| 318 | |
| 319 | return 0; |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 320 | } |
| 321 | |
Bartosz Golaszewski | ca409160 | 2017-02-06 13:10:35 +0100 | [diff] [blame] | 322 | static int gpio_mockup_probe(struct platform_device *pdev) |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 323 | { |
Bartosz Golaszewski | ca409160 | 2017-02-06 13:10:35 +0100 | [diff] [blame] | 324 | struct gpio_mockup_chip *chips; |
Bartosz Golaszewski | 364ac45 | 2016-12-20 12:28:20 +0100 | [diff] [blame] | 325 | struct device *dev = &pdev->dev; |
| 326 | int ret, i, base, ngpio; |
Bartosz Golaszewski | ad6d800 | 2016-12-20 12:28:19 +0100 | [diff] [blame] | 327 | char *chip_name; |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 328 | |
| 329 | if (gpio_mockup_params_nr < 2) |
| 330 | return -EINVAL; |
| 331 | |
Bartosz Golaszewski | ca409160 | 2017-02-06 13:10:35 +0100 | [diff] [blame] | 332 | chips = devm_kzalloc(dev, |
| 333 | sizeof(*chips) * (gpio_mockup_params_nr >> 1), |
| 334 | GFP_KERNEL); |
| 335 | if (!chips) |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 336 | return -ENOMEM; |
| 337 | |
Bartosz Golaszewski | ca409160 | 2017-02-06 13:10:35 +0100 | [diff] [blame] | 338 | platform_set_drvdata(pdev, chips); |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 339 | |
| 340 | for (i = 0; i < gpio_mockup_params_nr >> 1; i++) { |
| 341 | base = gpio_mockup_ranges[i * 2]; |
Bartosz Golaszewski | ca409160 | 2017-02-06 13:10:35 +0100 | [diff] [blame] | 342 | |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 343 | if (base == -1) |
| 344 | ngpio = gpio_mockup_ranges[i * 2 + 1]; |
| 345 | else |
| 346 | ngpio = gpio_mockup_ranges[i * 2 + 1] - base; |
| 347 | |
| 348 | if (ngpio >= 0) { |
Bartosz Golaszewski | ad6d800 | 2016-12-20 12:28:19 +0100 | [diff] [blame] | 349 | chip_name = devm_kasprintf(dev, GFP_KERNEL, |
Bartosz Golaszewski | ca409160 | 2017-02-06 13:10:35 +0100 | [diff] [blame] | 350 | "%s-%c", GPIO_MOCKUP_NAME, |
| 351 | gpio_mockup_name_start + i); |
Bartosz Golaszewski | ad6d800 | 2016-12-20 12:28:19 +0100 | [diff] [blame] | 352 | if (!chip_name) |
| 353 | return -ENOMEM; |
| 354 | |
Bartosz Golaszewski | ca409160 | 2017-02-06 13:10:35 +0100 | [diff] [blame] | 355 | ret = gpio_mockup_add(dev, &chips[i], |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 356 | chip_name, base, ngpio); |
| 357 | } else { |
| 358 | ret = -1; |
| 359 | } |
Bartosz Golaszewski | ca409160 | 2017-02-06 13:10:35 +0100 | [diff] [blame] | 360 | |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 361 | if (ret) { |
Bartosz Golaszewski | e4ba07b | 2017-02-06 13:10:36 +0100 | [diff] [blame] | 362 | dev_err(dev, "gpio<%d..%d> add failed\n", |
| 363 | base, base < 0 ? ngpio : base + ngpio); |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 364 | |
| 365 | return ret; |
| 366 | } |
| 367 | } |
| 368 | |
| 369 | return 0; |
| 370 | } |
| 371 | |
Bartosz Golaszewski | ca409160 | 2017-02-06 13:10:35 +0100 | [diff] [blame] | 372 | static struct platform_driver gpio_mockup_driver = { |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 373 | .driver = { |
Bartosz Golaszewski | ca409160 | 2017-02-06 13:10:35 +0100 | [diff] [blame] | 374 | .name = GPIO_MOCKUP_NAME, |
Bartosz Golaszewski | 364ac45 | 2016-12-20 12:28:20 +0100 | [diff] [blame] | 375 | }, |
Bartosz Golaszewski | ca409160 | 2017-02-06 13:10:35 +0100 | [diff] [blame] | 376 | .probe = gpio_mockup_probe, |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 377 | }; |
| 378 | |
| 379 | static struct platform_device *pdev; |
| 380 | static int __init mock_device_init(void) |
| 381 | { |
| 382 | int err; |
| 383 | |
Bartosz Golaszewski | 9202ba2 | 2017-02-06 15:11:08 +0100 | [diff] [blame] | 384 | gpio_mockup_dbg_dir = debugfs_create_dir("gpio-mockup-event", NULL); |
| 385 | if (!gpio_mockup_dbg_dir) |
| 386 | pr_err("%s: error creating debugfs directory\n", |
| 387 | GPIO_MOCKUP_NAME); |
| 388 | |
Bartosz Golaszewski | ca409160 | 2017-02-06 13:10:35 +0100 | [diff] [blame] | 389 | pdev = platform_device_alloc(GPIO_MOCKUP_NAME, -1); |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 390 | if (!pdev) |
| 391 | return -ENOMEM; |
| 392 | |
| 393 | err = platform_device_add(pdev); |
| 394 | if (err) { |
| 395 | platform_device_put(pdev); |
| 396 | return err; |
| 397 | } |
| 398 | |
Bartosz Golaszewski | ca409160 | 2017-02-06 13:10:35 +0100 | [diff] [blame] | 399 | err = platform_driver_register(&gpio_mockup_driver); |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 400 | if (err) { |
| 401 | platform_device_unregister(pdev); |
| 402 | return err; |
| 403 | } |
| 404 | |
| 405 | return 0; |
| 406 | } |
| 407 | |
| 408 | static void __exit mock_device_exit(void) |
| 409 | { |
Bartosz Golaszewski | 9202ba2 | 2017-02-06 15:11:08 +0100 | [diff] [blame] | 410 | debugfs_remove_recursive(gpio_mockup_dbg_dir); |
Bartosz Golaszewski | ca409160 | 2017-02-06 13:10:35 +0100 | [diff] [blame] | 411 | platform_driver_unregister(&gpio_mockup_driver); |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 412 | platform_device_unregister(pdev); |
| 413 | } |
| 414 | |
| 415 | module_init(mock_device_init); |
| 416 | module_exit(mock_device_exit); |
| 417 | |
| 418 | MODULE_AUTHOR("Kamlakant Patel <kamlakant.patel@broadcom.com>"); |
| 419 | MODULE_AUTHOR("Bamvor Jian Zhang <bamvor.zhangjian@linaro.org>"); |
| 420 | MODULE_DESCRIPTION("GPIO Testing driver"); |
| 421 | MODULE_LICENSE("GPL v2"); |