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