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