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; |
Bartosz Golaszewski | c7f5326fb | 2017-05-25 10:33:41 +0200 | [diff] [blame] | 44 | bool irq_enabled; |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 45 | }; |
| 46 | |
Bartosz Golaszewski | e2ff740 | 2017-02-06 15:11:07 +0100 | [diff] [blame] | 47 | struct gpio_mockup_irq_context { |
| 48 | struct irq_work work; |
| 49 | int irq; |
| 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 | e2ff740 | 2017-02-06 15:11:07 +0100 | [diff] [blame] | 55 | struct gpio_mockup_irq_context irq_ctx; |
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 | ca409160 | 2017-02-06 13:10:35 +0100 | [diff] [blame] | 65 | static int gpio_mockup_ranges[GPIO_MOCKUP_MAX_GC << 1]; |
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 | |
| 125 | names = devm_kzalloc(dev, sizeof(char *) * gc->ngpio, GFP_KERNEL); |
| 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 | e2ff740 | 2017-02-06 15:11:07 +0100 | [diff] [blame] | 141 | static int gpio_mockup_to_irq(struct gpio_chip *chip, unsigned int offset) |
| 142 | { |
| 143 | return chip->irq_base + offset; |
| 144 | } |
| 145 | |
Bartosz Golaszewski | c7f5326fb | 2017-05-25 10:33:41 +0200 | [diff] [blame] | 146 | static void gpio_mockup_irqmask(struct irq_data *data) |
| 147 | { |
| 148 | struct gpio_chip *gc = irq_data_get_irq_chip_data(data); |
| 149 | struct gpio_mockup_chip *chip = gpiochip_get_data(gc); |
| 150 | |
| 151 | chip->lines[data->irq - gc->irq_base].irq_enabled = false; |
| 152 | } |
| 153 | |
| 154 | static void gpio_mockup_irqunmask(struct irq_data *data) |
| 155 | { |
| 156 | struct gpio_chip *gc = irq_data_get_irq_chip_data(data); |
| 157 | struct gpio_mockup_chip *chip = gpiochip_get_data(gc); |
| 158 | |
| 159 | chip->lines[data->irq - gc->irq_base].irq_enabled = true; |
| 160 | } |
Bartosz Golaszewski | e2ff740 | 2017-02-06 15:11:07 +0100 | [diff] [blame] | 161 | |
| 162 | static struct irq_chip gpio_mockup_irqchip = { |
| 163 | .name = GPIO_MOCKUP_NAME, |
| 164 | .irq_mask = gpio_mockup_irqmask, |
| 165 | .irq_unmask = gpio_mockup_irqunmask, |
| 166 | }; |
| 167 | |
| 168 | static void gpio_mockup_handle_irq(struct irq_work *work) |
| 169 | { |
| 170 | struct gpio_mockup_irq_context *irq_ctx; |
| 171 | |
| 172 | irq_ctx = container_of(work, struct gpio_mockup_irq_context, work); |
| 173 | handle_simple_irq(irq_to_desc(irq_ctx->irq)); |
| 174 | } |
| 175 | |
| 176 | static int gpio_mockup_irqchip_setup(struct device *dev, |
| 177 | struct gpio_mockup_chip *chip) |
| 178 | { |
| 179 | struct gpio_chip *gc = &chip->gc; |
| 180 | int irq_base, i; |
| 181 | |
Bartosz Golaszewski | 62a7ca0 | 2017-03-04 17:23:29 +0100 | [diff] [blame] | 182 | irq_base = devm_irq_alloc_descs(dev, -1, 0, gc->ngpio, 0); |
Bartosz Golaszewski | e2ff740 | 2017-02-06 15:11:07 +0100 | [diff] [blame] | 183 | if (irq_base < 0) |
| 184 | return irq_base; |
| 185 | |
| 186 | gc->irq_base = irq_base; |
| 187 | gc->irqchip = &gpio_mockup_irqchip; |
| 188 | |
| 189 | for (i = 0; i < gc->ngpio; i++) { |
| 190 | irq_set_chip(irq_base + i, gc->irqchip); |
Bartosz Golaszewski | c7f5326fb | 2017-05-25 10:33:41 +0200 | [diff] [blame] | 191 | irq_set_chip_data(irq_base + i, gc); |
Bartosz Golaszewski | e2ff740 | 2017-02-06 15:11:07 +0100 | [diff] [blame] | 192 | irq_set_handler(irq_base + i, &handle_simple_irq); |
| 193 | irq_modify_status(irq_base + i, |
| 194 | IRQ_NOREQUEST | IRQ_NOAUTOEN, IRQ_NOPROBE); |
| 195 | } |
| 196 | |
| 197 | init_irq_work(&chip->irq_ctx.work, gpio_mockup_handle_irq); |
| 198 | |
| 199 | return 0; |
| 200 | } |
| 201 | |
Bartosz Golaszewski | 9202ba2 | 2017-02-06 15:11:08 +0100 | [diff] [blame] | 202 | static ssize_t gpio_mockup_event_write(struct file *file, |
| 203 | const char __user *usr_buf, |
| 204 | size_t size, loff_t *ppos) |
| 205 | { |
| 206 | struct gpio_mockup_dbgfs_private *priv; |
| 207 | struct gpio_mockup_chip *chip; |
| 208 | struct seq_file *sfile; |
| 209 | struct gpio_desc *desc; |
| 210 | struct gpio_chip *gc; |
Bartosz Golaszewski | 6f9b3e7 | 2017-06-09 13:41:25 +0200 | [diff] [blame] | 211 | int rv, val; |
Bartosz Golaszewski | 9202ba2 | 2017-02-06 15:11:08 +0100 | [diff] [blame] | 212 | |
Bartosz Golaszewski | 6f9b3e7 | 2017-06-09 13:41:25 +0200 | [diff] [blame] | 213 | rv = kstrtoint_from_user(usr_buf, size, 0, &val); |
| 214 | if (rv) |
| 215 | return rv; |
| 216 | if (val != 0 && val != 1) |
| 217 | return -EINVAL; |
| 218 | |
Bartosz Golaszewski | 650b57b | 2017-06-09 13:41:26 +0200 | [diff] [blame^] | 219 | sfile = file->private_data; |
| 220 | priv = sfile->private; |
| 221 | desc = priv->desc; |
| 222 | chip = priv->chip; |
| 223 | gc = &chip->gc; |
Bartosz Golaszewski | c7f5326fb | 2017-05-25 10:33:41 +0200 | [diff] [blame] | 224 | |
Bartosz Golaszewski | 650b57b | 2017-06-09 13:41:26 +0200 | [diff] [blame^] | 225 | if (chip->lines[priv->offset].irq_enabled) { |
| 226 | gpiod_set_value_cansleep(desc, val); |
| 227 | priv->chip->irq_ctx.irq = gc->irq_base + priv->offset; |
| 228 | irq_work_queue(&priv->chip->irq_ctx.work); |
| 229 | } |
Bartosz Golaszewski | 9202ba2 | 2017-02-06 15:11:08 +0100 | [diff] [blame] | 230 | |
| 231 | return size; |
| 232 | } |
| 233 | |
| 234 | static int gpio_mockup_event_open(struct inode *inode, struct file *file) |
| 235 | { |
| 236 | return single_open(file, NULL, inode->i_private); |
| 237 | } |
| 238 | |
| 239 | static const struct file_operations gpio_mockup_event_ops = { |
| 240 | .owner = THIS_MODULE, |
| 241 | .open = gpio_mockup_event_open, |
| 242 | .write = gpio_mockup_event_write, |
| 243 | .llseek = no_llseek, |
| 244 | }; |
| 245 | |
| 246 | static void gpio_mockup_debugfs_setup(struct device *dev, |
| 247 | struct gpio_mockup_chip *chip) |
| 248 | { |
| 249 | struct gpio_mockup_dbgfs_private *priv; |
| 250 | struct dentry *evfile; |
| 251 | struct gpio_chip *gc; |
| 252 | char *name; |
| 253 | int i; |
| 254 | |
| 255 | gc = &chip->gc; |
| 256 | |
| 257 | chip->dbg_dir = debugfs_create_dir(gc->label, gpio_mockup_dbg_dir); |
| 258 | if (!chip->dbg_dir) |
| 259 | goto err; |
| 260 | |
| 261 | for (i = 0; i < gc->ngpio; i++) { |
| 262 | name = devm_kasprintf(dev, GFP_KERNEL, "%d", i); |
| 263 | if (!name) |
| 264 | goto err; |
| 265 | |
| 266 | priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); |
| 267 | if (!priv) |
| 268 | goto err; |
| 269 | |
| 270 | priv->chip = chip; |
| 271 | priv->offset = i; |
| 272 | priv->desc = &gc->gpiodev->descs[i]; |
| 273 | |
| 274 | evfile = debugfs_create_file(name, 0200, chip->dbg_dir, priv, |
| 275 | &gpio_mockup_event_ops); |
| 276 | if (!evfile) |
| 277 | goto err; |
| 278 | } |
| 279 | |
| 280 | return; |
| 281 | |
| 282 | err: |
| 283 | dev_err(dev, "error creating debugfs directory\n"); |
| 284 | } |
| 285 | |
Bartosz Golaszewski | ca409160 | 2017-02-06 13:10:35 +0100 | [diff] [blame] | 286 | static int gpio_mockup_add(struct device *dev, |
| 287 | struct gpio_mockup_chip *chip, |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 288 | const char *name, int base, int ngpio) |
| 289 | { |
Bartosz Golaszewski | ca409160 | 2017-02-06 13:10:35 +0100 | [diff] [blame] | 290 | struct gpio_chip *gc = &chip->gc; |
Bartosz Golaszewski | 8a68ea0 | 2017-02-06 13:10:37 +0100 | [diff] [blame] | 291 | int ret; |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 292 | |
Bartosz Golaszewski | ca409160 | 2017-02-06 13:10:35 +0100 | [diff] [blame] | 293 | gc->base = base; |
| 294 | gc->ngpio = ngpio; |
| 295 | gc->label = name; |
| 296 | gc->owner = THIS_MODULE; |
| 297 | gc->parent = dev; |
| 298 | gc->get = gpio_mockup_get; |
| 299 | gc->set = gpio_mockup_set; |
| 300 | gc->direction_output = gpio_mockup_dirout; |
| 301 | gc->direction_input = gpio_mockup_dirin; |
| 302 | gc->get_direction = gpio_mockup_get_direction; |
Bartosz Golaszewski | e2ff740 | 2017-02-06 15:11:07 +0100 | [diff] [blame] | 303 | gc->to_irq = gpio_mockup_to_irq; |
Bartosz Golaszewski | ca409160 | 2017-02-06 13:10:35 +0100 | [diff] [blame] | 304 | |
| 305 | chip->lines = devm_kzalloc(dev, sizeof(*chip->lines) * gc->ngpio, |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 306 | GFP_KERNEL); |
Bartosz Golaszewski | e4ba07b | 2017-02-06 13:10:36 +0100 | [diff] [blame] | 307 | if (!chip->lines) |
| 308 | return -ENOMEM; |
Bartosz Golaszewski | ca409160 | 2017-02-06 13:10:35 +0100 | [diff] [blame] | 309 | |
Bartosz Golaszewski | 8a68ea0 | 2017-02-06 13:10:37 +0100 | [diff] [blame] | 310 | if (gpio_mockup_named_lines) { |
| 311 | ret = gpio_mockup_name_lines(dev, chip); |
| 312 | if (ret) |
| 313 | return ret; |
| 314 | } |
| 315 | |
Bartosz Golaszewski | e2ff740 | 2017-02-06 15:11:07 +0100 | [diff] [blame] | 316 | ret = gpio_mockup_irqchip_setup(dev, chip); |
| 317 | if (ret) |
| 318 | return ret; |
| 319 | |
Bartosz Golaszewski | 9202ba2 | 2017-02-06 15:11:08 +0100 | [diff] [blame] | 320 | ret = devm_gpiochip_add_data(dev, &chip->gc, chip); |
| 321 | if (ret) |
| 322 | return ret; |
| 323 | |
| 324 | if (gpio_mockup_dbg_dir) |
| 325 | gpio_mockup_debugfs_setup(dev, chip); |
| 326 | |
| 327 | return 0; |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 328 | } |
| 329 | |
Bartosz Golaszewski | ca409160 | 2017-02-06 13:10:35 +0100 | [diff] [blame] | 330 | static int gpio_mockup_probe(struct platform_device *pdev) |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 331 | { |
Bartosz Golaszewski | ca409160 | 2017-02-06 13:10:35 +0100 | [diff] [blame] | 332 | struct gpio_mockup_chip *chips; |
Bartosz Golaszewski | 364ac45 | 2016-12-20 12:28:20 +0100 | [diff] [blame] | 333 | struct device *dev = &pdev->dev; |
| 334 | int ret, i, base, ngpio; |
Bartosz Golaszewski | ad6d800 | 2016-12-20 12:28:19 +0100 | [diff] [blame] | 335 | char *chip_name; |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 336 | |
| 337 | if (gpio_mockup_params_nr < 2) |
| 338 | return -EINVAL; |
| 339 | |
Bartosz Golaszewski | ca409160 | 2017-02-06 13:10:35 +0100 | [diff] [blame] | 340 | chips = devm_kzalloc(dev, |
| 341 | sizeof(*chips) * (gpio_mockup_params_nr >> 1), |
| 342 | GFP_KERNEL); |
| 343 | if (!chips) |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 344 | return -ENOMEM; |
| 345 | |
Bartosz Golaszewski | ca409160 | 2017-02-06 13:10:35 +0100 | [diff] [blame] | 346 | platform_set_drvdata(pdev, chips); |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 347 | |
| 348 | for (i = 0; i < gpio_mockup_params_nr >> 1; i++) { |
| 349 | base = gpio_mockup_ranges[i * 2]; |
Bartosz Golaszewski | ca409160 | 2017-02-06 13:10:35 +0100 | [diff] [blame] | 350 | |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 351 | if (base == -1) |
| 352 | ngpio = gpio_mockup_ranges[i * 2 + 1]; |
| 353 | else |
| 354 | ngpio = gpio_mockup_ranges[i * 2 + 1] - base; |
| 355 | |
| 356 | if (ngpio >= 0) { |
Bartosz Golaszewski | ad6d800 | 2016-12-20 12:28:19 +0100 | [diff] [blame] | 357 | chip_name = devm_kasprintf(dev, GFP_KERNEL, |
Bartosz Golaszewski | ca409160 | 2017-02-06 13:10:35 +0100 | [diff] [blame] | 358 | "%s-%c", GPIO_MOCKUP_NAME, |
| 359 | gpio_mockup_name_start + i); |
Bartosz Golaszewski | ad6d800 | 2016-12-20 12:28:19 +0100 | [diff] [blame] | 360 | if (!chip_name) |
| 361 | return -ENOMEM; |
| 362 | |
Bartosz Golaszewski | ca409160 | 2017-02-06 13:10:35 +0100 | [diff] [blame] | 363 | ret = gpio_mockup_add(dev, &chips[i], |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 364 | chip_name, base, ngpio); |
| 365 | } else { |
| 366 | ret = -1; |
| 367 | } |
Bartosz Golaszewski | ca409160 | 2017-02-06 13:10:35 +0100 | [diff] [blame] | 368 | |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 369 | if (ret) { |
Bartosz Golaszewski | e4ba07b | 2017-02-06 13:10:36 +0100 | [diff] [blame] | 370 | dev_err(dev, "gpio<%d..%d> add failed\n", |
| 371 | base, base < 0 ? ngpio : base + ngpio); |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 372 | |
| 373 | return ret; |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | return 0; |
| 378 | } |
| 379 | |
Bartosz Golaszewski | ca409160 | 2017-02-06 13:10:35 +0100 | [diff] [blame] | 380 | static struct platform_driver gpio_mockup_driver = { |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 381 | .driver = { |
Bartosz Golaszewski | ca409160 | 2017-02-06 13:10:35 +0100 | [diff] [blame] | 382 | .name = GPIO_MOCKUP_NAME, |
Bartosz Golaszewski | 364ac45 | 2016-12-20 12:28:20 +0100 | [diff] [blame] | 383 | }, |
Bartosz Golaszewski | ca409160 | 2017-02-06 13:10:35 +0100 | [diff] [blame] | 384 | .probe = gpio_mockup_probe, |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 385 | }; |
| 386 | |
| 387 | static struct platform_device *pdev; |
| 388 | static int __init mock_device_init(void) |
| 389 | { |
| 390 | int err; |
| 391 | |
Bartosz Golaszewski | 9202ba2 | 2017-02-06 15:11:08 +0100 | [diff] [blame] | 392 | gpio_mockup_dbg_dir = debugfs_create_dir("gpio-mockup-event", NULL); |
| 393 | if (!gpio_mockup_dbg_dir) |
| 394 | pr_err("%s: error creating debugfs directory\n", |
| 395 | GPIO_MOCKUP_NAME); |
| 396 | |
Bartosz Golaszewski | ca409160 | 2017-02-06 13:10:35 +0100 | [diff] [blame] | 397 | pdev = platform_device_alloc(GPIO_MOCKUP_NAME, -1); |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 398 | if (!pdev) |
| 399 | return -ENOMEM; |
| 400 | |
| 401 | err = platform_device_add(pdev); |
| 402 | if (err) { |
| 403 | platform_device_put(pdev); |
| 404 | return err; |
| 405 | } |
| 406 | |
Bartosz Golaszewski | ca409160 | 2017-02-06 13:10:35 +0100 | [diff] [blame] | 407 | err = platform_driver_register(&gpio_mockup_driver); |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 408 | if (err) { |
| 409 | platform_device_unregister(pdev); |
| 410 | return err; |
| 411 | } |
| 412 | |
| 413 | return 0; |
| 414 | } |
| 415 | |
| 416 | static void __exit mock_device_exit(void) |
| 417 | { |
Bartosz Golaszewski | 9202ba2 | 2017-02-06 15:11:08 +0100 | [diff] [blame] | 418 | debugfs_remove_recursive(gpio_mockup_dbg_dir); |
Bartosz Golaszewski | ca409160 | 2017-02-06 13:10:35 +0100 | [diff] [blame] | 419 | platform_driver_unregister(&gpio_mockup_driver); |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 420 | platform_device_unregister(pdev); |
| 421 | } |
| 422 | |
| 423 | module_init(mock_device_init); |
| 424 | module_exit(mock_device_exit); |
| 425 | |
| 426 | MODULE_AUTHOR("Kamlakant Patel <kamlakant.patel@broadcom.com>"); |
| 427 | MODULE_AUTHOR("Bamvor Jian Zhang <bamvor.zhangjian@linaro.org>"); |
| 428 | MODULE_DESCRIPTION("GPIO Testing driver"); |
| 429 | MODULE_LICENSE("GPL v2"); |