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