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" |
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 | ca409160 | 2017-02-06 13:10:35 +0100 | [diff] [blame] | 84 | 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] | 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 | return chip->lines[offset].value; |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 89 | } |
| 90 | |
Bartosz Golaszewski | 2a9d742 | 2017-11-27 11:48:50 +0100 | [diff] [blame] | 91 | static void gpio_mockup_set(struct gpio_chip *gc, |
| 92 | unsigned int offset, int value) |
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 | 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 | chip->lines[offset].value = !!value; |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 97 | } |
| 98 | |
Bartosz Golaszewski | c47bee9 | 2017-11-27 11:48:51 +0100 | [diff] [blame] | 99 | static void gpio_mockup_set_multiple(struct gpio_chip *gc, |
| 100 | unsigned long *mask, unsigned long *bits) |
| 101 | { |
| 102 | unsigned int bit; |
| 103 | |
| 104 | for_each_set_bit(bit, mask, gc->ngpio) |
| 105 | gpio_mockup_set(gc, bit, test_bit(bit, bits)); |
| 106 | |
| 107 | } |
| 108 | |
Bartosz Golaszewski | 2a9d742 | 2017-11-27 11:48:50 +0100 | [diff] [blame] | 109 | static int gpio_mockup_dirout(struct gpio_chip *gc, |
| 110 | unsigned int offset, int value) |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 111 | { |
Bartosz Golaszewski | ca409160 | 2017-02-06 13:10:35 +0100 | [diff] [blame] | 112 | struct gpio_mockup_chip *chip = gpiochip_get_data(gc); |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 113 | |
Bartosz Golaszewski | ca409160 | 2017-02-06 13:10:35 +0100 | [diff] [blame] | 114 | gpio_mockup_set(gc, offset, value); |
Bartosz Golaszewski | c650c00 | 2017-05-25 10:33:39 +0200 | [diff] [blame] | 115 | chip->lines[offset].dir = GPIO_MOCKUP_DIR_OUT; |
Bartosz Golaszewski | ca409160 | 2017-02-06 13:10:35 +0100 | [diff] [blame] | 116 | |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 117 | return 0; |
| 118 | } |
| 119 | |
Bartosz Golaszewski | ca409160 | 2017-02-06 13:10:35 +0100 | [diff] [blame] | 120 | 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] | 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 | c650c00 | 2017-05-25 10:33:39 +0200 | [diff] [blame] | 124 | chip->lines[offset].dir = GPIO_MOCKUP_DIR_IN; |
Bartosz Golaszewski | ca409160 | 2017-02-06 13:10:35 +0100 | [diff] [blame] | 125 | |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 126 | return 0; |
| 127 | } |
| 128 | |
Bartosz Golaszewski | ca409160 | 2017-02-06 13:10:35 +0100 | [diff] [blame] | 129 | 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] | 130 | { |
Bartosz Golaszewski | ca409160 | 2017-02-06 13:10:35 +0100 | [diff] [blame] | 131 | struct gpio_mockup_chip *chip = gpiochip_get_data(gc); |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 132 | |
Bartosz Golaszewski | ca409160 | 2017-02-06 13:10:35 +0100 | [diff] [blame] | 133 | return chip->lines[offset].dir; |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 134 | } |
| 135 | |
Bartosz Golaszewski | b4c495f | 2017-08-14 13:20:56 +0200 | [diff] [blame] | 136 | 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] | 137 | { |
Bartosz Golaszewski | c7f5326fb | 2017-05-25 10:33:41 +0200 | [diff] [blame] | 138 | struct gpio_mockup_chip *chip = gpiochip_get_data(gc); |
| 139 | |
Bartosz Golaszewski | b4c495f | 2017-08-14 13:20:56 +0200 | [diff] [blame] | 140 | return irq_sim_irqnum(&chip->irqsim, offset); |
Bartosz Golaszewski | e2ff740 | 2017-02-06 15:11:07 +0100 | [diff] [blame] | 141 | } |
| 142 | |
Bartosz Golaszewski | 9202ba2 | 2017-02-06 15:11:08 +0100 | [diff] [blame] | 143 | static ssize_t gpio_mockup_event_write(struct file *file, |
| 144 | const char __user *usr_buf, |
| 145 | size_t size, loff_t *ppos) |
| 146 | { |
| 147 | struct gpio_mockup_dbgfs_private *priv; |
| 148 | struct gpio_mockup_chip *chip; |
| 149 | struct seq_file *sfile; |
| 150 | struct gpio_desc *desc; |
Bartosz Golaszewski | 6f9b3e7 | 2017-06-09 13:41:25 +0200 | [diff] [blame] | 151 | int rv, val; |
Bartosz Golaszewski | 9202ba2 | 2017-02-06 15:11:08 +0100 | [diff] [blame] | 152 | |
Bartosz Golaszewski | 6f9b3e7 | 2017-06-09 13:41:25 +0200 | [diff] [blame] | 153 | rv = kstrtoint_from_user(usr_buf, size, 0, &val); |
| 154 | if (rv) |
| 155 | return rv; |
| 156 | if (val != 0 && val != 1) |
| 157 | return -EINVAL; |
| 158 | |
Bartosz Golaszewski | 650b57b | 2017-06-09 13:41:26 +0200 | [diff] [blame] | 159 | sfile = file->private_data; |
| 160 | priv = sfile->private; |
| 161 | desc = priv->desc; |
| 162 | chip = priv->chip; |
Bartosz Golaszewski | c7f5326fb | 2017-05-25 10:33:41 +0200 | [diff] [blame] | 163 | |
Bartosz Golaszewski | b4c495f | 2017-08-14 13:20:56 +0200 | [diff] [blame] | 164 | gpiod_set_value_cansleep(desc, val); |
| 165 | irq_sim_fire(&chip->irqsim, priv->offset); |
Bartosz Golaszewski | 9202ba2 | 2017-02-06 15:11:08 +0100 | [diff] [blame] | 166 | |
| 167 | return size; |
| 168 | } |
| 169 | |
| 170 | static int gpio_mockup_event_open(struct inode *inode, struct file *file) |
| 171 | { |
| 172 | return single_open(file, NULL, inode->i_private); |
| 173 | } |
| 174 | |
| 175 | static const struct file_operations gpio_mockup_event_ops = { |
| 176 | .owner = THIS_MODULE, |
| 177 | .open = gpio_mockup_event_open, |
| 178 | .write = gpio_mockup_event_write, |
| 179 | .llseek = no_llseek, |
| 180 | }; |
| 181 | |
| 182 | static void gpio_mockup_debugfs_setup(struct device *dev, |
| 183 | struct gpio_mockup_chip *chip) |
| 184 | { |
| 185 | struct gpio_mockup_dbgfs_private *priv; |
Bartosz Golaszewski | ca8792a | 2017-11-27 11:48:45 +0100 | [diff] [blame] | 186 | struct dentry *evfile, *link; |
Bartosz Golaszewski | 9202ba2 | 2017-02-06 15:11:08 +0100 | [diff] [blame] | 187 | struct gpio_chip *gc; |
Bartosz Golaszewski | ca8792a | 2017-11-27 11:48:45 +0100 | [diff] [blame] | 188 | const char *devname; |
Bartosz Golaszewski | 9202ba2 | 2017-02-06 15:11:08 +0100 | [diff] [blame] | 189 | char *name; |
| 190 | int i; |
| 191 | |
| 192 | gc = &chip->gc; |
Bartosz Golaszewski | ca8792a | 2017-11-27 11:48:45 +0100 | [diff] [blame] | 193 | devname = dev_name(&gc->gpiodev->dev); |
Bartosz Golaszewski | 9202ba2 | 2017-02-06 15:11:08 +0100 | [diff] [blame] | 194 | |
Bartosz Golaszewski | ca8792a | 2017-11-27 11:48:45 +0100 | [diff] [blame] | 195 | chip->dbg_dir = debugfs_create_dir(devname, gpio_mockup_dbg_dir); |
Bartosz Golaszewski | 46526c1 | 2017-11-27 11:48:48 +0100 | [diff] [blame] | 196 | if (IS_ERR_OR_NULL(chip->dbg_dir)) |
Bartosz Golaszewski | 9202ba2 | 2017-02-06 15:11:08 +0100 | [diff] [blame] | 197 | goto err; |
| 198 | |
Bartosz Golaszewski | ca8792a | 2017-11-27 11:48:45 +0100 | [diff] [blame] | 199 | link = debugfs_create_symlink(gc->label, gpio_mockup_dbg_dir, devname); |
Bartosz Golaszewski | 46526c1 | 2017-11-27 11:48:48 +0100 | [diff] [blame] | 200 | if (IS_ERR_OR_NULL(link)) |
Bartosz Golaszewski | ca8792a | 2017-11-27 11:48:45 +0100 | [diff] [blame] | 201 | goto err; |
| 202 | |
Bartosz Golaszewski | 9202ba2 | 2017-02-06 15:11:08 +0100 | [diff] [blame] | 203 | for (i = 0; i < gc->ngpio; i++) { |
| 204 | name = devm_kasprintf(dev, GFP_KERNEL, "%d", i); |
| 205 | if (!name) |
| 206 | goto err; |
| 207 | |
| 208 | priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); |
| 209 | if (!priv) |
| 210 | goto err; |
| 211 | |
| 212 | priv->chip = chip; |
| 213 | priv->offset = i; |
| 214 | priv->desc = &gc->gpiodev->descs[i]; |
| 215 | |
| 216 | evfile = debugfs_create_file(name, 0200, chip->dbg_dir, priv, |
| 217 | &gpio_mockup_event_ops); |
Bartosz Golaszewski | 46526c1 | 2017-11-27 11:48:48 +0100 | [diff] [blame] | 218 | if (IS_ERR_OR_NULL(evfile)) |
Bartosz Golaszewski | 9202ba2 | 2017-02-06 15:11:08 +0100 | [diff] [blame] | 219 | goto err; |
| 220 | } |
| 221 | |
| 222 | return; |
| 223 | |
| 224 | err: |
Bartosz Golaszewski | 46526c1 | 2017-11-27 11:48:48 +0100 | [diff] [blame] | 225 | dev_err(dev, "error creating debugfs event files\n"); |
Bartosz Golaszewski | 9202ba2 | 2017-02-06 15:11:08 +0100 | [diff] [blame] | 226 | } |
| 227 | |
Bartosz Golaszewski | 1cea473 | 2017-11-27 11:48:47 +0100 | [diff] [blame] | 228 | static int gpio_mockup_name_lines(struct device *dev, |
| 229 | struct gpio_mockup_chip *chip) |
| 230 | { |
| 231 | struct gpio_chip *gc = &chip->gc; |
| 232 | char **names; |
| 233 | int i; |
| 234 | |
| 235 | names = devm_kcalloc(dev, gc->ngpio, sizeof(char *), GFP_KERNEL); |
| 236 | if (!names) |
| 237 | return -ENOMEM; |
| 238 | |
| 239 | for (i = 0; i < gc->ngpio; i++) { |
| 240 | names[i] = devm_kasprintf(dev, GFP_KERNEL, |
| 241 | "%s-%d", gc->label, i); |
| 242 | if (!names[i]) |
| 243 | return -ENOMEM; |
| 244 | } |
| 245 | |
| 246 | gc->names = (const char *const *)names; |
| 247 | |
| 248 | return 0; |
| 249 | } |
| 250 | |
Bartosz Golaszewski | ca409160 | 2017-02-06 13:10:35 +0100 | [diff] [blame] | 251 | static int gpio_mockup_probe(struct platform_device *pdev) |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 252 | { |
Bartosz Golaszewski | 8a39f59 | 2017-11-27 11:48:40 +0100 | [diff] [blame] | 253 | struct gpio_mockup_platform_data *pdata; |
| 254 | struct gpio_mockup_chip *chip; |
Bartosz Golaszewski | 6d974d3 | 2017-11-27 11:48:43 +0100 | [diff] [blame] | 255 | struct gpio_chip *gc; |
Bartosz Golaszewski | 8a39f59 | 2017-11-27 11:48:40 +0100 | [diff] [blame] | 256 | int rv, base, ngpio; |
| 257 | struct device *dev; |
| 258 | char *name; |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 259 | |
Bartosz Golaszewski | 8a39f59 | 2017-11-27 11:48:40 +0100 | [diff] [blame] | 260 | dev = &pdev->dev; |
| 261 | pdata = dev_get_platdata(dev); |
| 262 | base = pdata->base; |
| 263 | ngpio = pdata->ngpio; |
Bartosz Golaszewski | b652336 | 2017-06-09 13:41:28 +0200 | [diff] [blame] | 264 | |
Bartosz Golaszewski | 8a39f59 | 2017-11-27 11:48:40 +0100 | [diff] [blame] | 265 | chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL); |
| 266 | if (!chip) |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 267 | return -ENOMEM; |
| 268 | |
Bartosz Golaszewski | 8a39f59 | 2017-11-27 11:48:40 +0100 | [diff] [blame] | 269 | name = devm_kasprintf(dev, GFP_KERNEL, "%s-%c", |
| 270 | pdev->name, pdata->index); |
| 271 | if (!name) |
| 272 | return -ENOMEM; |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 273 | |
Bartosz Golaszewski | 6d974d3 | 2017-11-27 11:48:43 +0100 | [diff] [blame] | 274 | gc = &chip->gc; |
| 275 | gc->base = base; |
| 276 | gc->ngpio = ngpio; |
| 277 | gc->label = name; |
| 278 | gc->owner = THIS_MODULE; |
| 279 | gc->parent = dev; |
| 280 | gc->get = gpio_mockup_get; |
| 281 | gc->set = gpio_mockup_set; |
Bartosz Golaszewski | c47bee9 | 2017-11-27 11:48:51 +0100 | [diff] [blame] | 282 | gc->set_multiple = gpio_mockup_set_multiple; |
Bartosz Golaszewski | 6d974d3 | 2017-11-27 11:48:43 +0100 | [diff] [blame] | 283 | gc->direction_output = gpio_mockup_dirout; |
| 284 | gc->direction_input = gpio_mockup_dirin; |
| 285 | gc->get_direction = gpio_mockup_get_direction; |
| 286 | gc->to_irq = gpio_mockup_to_irq; |
| 287 | |
| 288 | chip->lines = devm_kcalloc(dev, gc->ngpio, |
| 289 | sizeof(*chip->lines), GFP_KERNEL); |
| 290 | if (!chip->lines) |
| 291 | return -ENOMEM; |
| 292 | |
Bartosz Golaszewski | c3196a7 | 2017-11-27 11:48:44 +0100 | [diff] [blame] | 293 | if (pdata->named_lines) { |
Bartosz Golaszewski | 6d974d3 | 2017-11-27 11:48:43 +0100 | [diff] [blame] | 294 | rv = gpio_mockup_name_lines(dev, chip); |
| 295 | if (rv) |
| 296 | return rv; |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 297 | } |
| 298 | |
Bartosz Golaszewski | 6d974d3 | 2017-11-27 11:48:43 +0100 | [diff] [blame] | 299 | rv = devm_irq_sim_init(dev, &chip->irqsim, gc->ngpio); |
Bartosz Golaszewski | 1e0dca6 | 2017-11-27 11:48:52 +0100 | [diff] [blame] | 300 | if (rv < 0) |
Bartosz Golaszewski | 6d974d3 | 2017-11-27 11:48:43 +0100 | [diff] [blame] | 301 | return rv; |
| 302 | |
| 303 | rv = devm_gpiochip_add_data(dev, &chip->gc, chip); |
| 304 | if (rv) |
| 305 | return rv; |
| 306 | |
| 307 | if (gpio_mockup_dbg_dir) |
| 308 | gpio_mockup_debugfs_setup(dev, chip); |
| 309 | |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 310 | return 0; |
| 311 | } |
| 312 | |
Bartosz Golaszewski | ca409160 | 2017-02-06 13:10:35 +0100 | [diff] [blame] | 313 | static struct platform_driver gpio_mockup_driver = { |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 314 | .driver = { |
Bartosz Golaszewski | ca409160 | 2017-02-06 13:10:35 +0100 | [diff] [blame] | 315 | .name = GPIO_MOCKUP_NAME, |
Bartosz Golaszewski | 364ac45 | 2016-12-20 12:28:20 +0100 | [diff] [blame] | 316 | }, |
Bartosz Golaszewski | ca409160 | 2017-02-06 13:10:35 +0100 | [diff] [blame] | 317 | .probe = gpio_mockup_probe, |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 318 | }; |
| 319 | |
Bartosz Golaszewski | 8a39f59 | 2017-11-27 11:48:40 +0100 | [diff] [blame] | 320 | static struct platform_device *gpio_mockup_pdevs[GPIO_MOCKUP_MAX_GC]; |
| 321 | |
| 322 | static void gpio_mockup_unregister_pdevs(void) |
| 323 | { |
| 324 | struct platform_device *pdev; |
| 325 | int i; |
| 326 | |
| 327 | for (i = 0; i < GPIO_MOCKUP_MAX_GC; i++) { |
| 328 | pdev = gpio_mockup_pdevs[i]; |
| 329 | |
| 330 | if (pdev) |
| 331 | platform_device_unregister(pdev); |
| 332 | } |
| 333 | } |
Bartosz Golaszewski | f3b4717 | 2017-11-27 11:48:37 +0100 | [diff] [blame] | 334 | |
| 335 | static int __init gpio_mockup_init(void) |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 336 | { |
Bartosz Golaszewski | 8a39f59 | 2017-11-27 11:48:40 +0100 | [diff] [blame] | 337 | int i, num_chips, err = 0, index = 'A'; |
| 338 | struct gpio_mockup_platform_data pdata; |
| 339 | struct platform_device *pdev; |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 340 | |
Bartosz Golaszewski | e63a006 | 2017-11-27 11:48:53 +0100 | [diff] [blame^] | 341 | if ((gpio_mockup_num_ranges < 2) || |
| 342 | (gpio_mockup_num_ranges % 2) || |
| 343 | (gpio_mockup_num_ranges > GPIO_MOCKUP_MAX_RANGES)) |
Bartosz Golaszewski | 20c35ac | 2017-11-27 11:48:38 +0100 | [diff] [blame] | 344 | return -EINVAL; |
| 345 | |
Bartosz Golaszewski | 8a39f59 | 2017-11-27 11:48:40 +0100 | [diff] [blame] | 346 | /* Each chip is described by two values. */ |
Bartosz Golaszewski | e63a006 | 2017-11-27 11:48:53 +0100 | [diff] [blame^] | 347 | num_chips = gpio_mockup_num_ranges / 2; |
Bartosz Golaszewski | 8a39f59 | 2017-11-27 11:48:40 +0100 | [diff] [blame] | 348 | |
Bartosz Golaszewski | fa86963 | 2017-11-27 11:48:49 +0100 | [diff] [blame] | 349 | /* |
| 350 | * The second value in the <base GPIO - number of GPIOS> pair must |
| 351 | * always be greater than 0. |
| 352 | */ |
| 353 | for (i = 0; i < num_chips; i++) { |
| 354 | if (gpio_mockup_ranges[i * 2 + 1] < 0) |
| 355 | return -EINVAL; |
| 356 | } |
| 357 | |
Bartosz Golaszewski | 9202ba2 | 2017-02-06 15:11:08 +0100 | [diff] [blame] | 358 | gpio_mockup_dbg_dir = debugfs_create_dir("gpio-mockup-event", NULL); |
Bartosz Golaszewski | 46526c1 | 2017-11-27 11:48:48 +0100 | [diff] [blame] | 359 | if (IS_ERR_OR_NULL(gpio_mockup_dbg_dir)) |
Bartosz Golaszewski | f1bec99 | 2017-11-27 11:48:41 +0100 | [diff] [blame] | 360 | gpio_mockup_err("error creating debugfs directory\n"); |
Bartosz Golaszewski | 9202ba2 | 2017-02-06 15:11:08 +0100 | [diff] [blame] | 361 | |
Bartosz Golaszewski | 8a39f59 | 2017-11-27 11:48:40 +0100 | [diff] [blame] | 362 | err = platform_driver_register(&gpio_mockup_driver); |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 363 | if (err) { |
Bartosz Golaszewski | f1bec99 | 2017-11-27 11:48:41 +0100 | [diff] [blame] | 364 | gpio_mockup_err("error registering platform driver\n"); |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 365 | return err; |
| 366 | } |
| 367 | |
Bartosz Golaszewski | 8a39f59 | 2017-11-27 11:48:40 +0100 | [diff] [blame] | 368 | for (i = 0; i < num_chips; i++) { |
| 369 | pdata.index = index++; |
| 370 | pdata.base = gpio_mockup_ranges[i * 2]; |
| 371 | pdata.ngpio = pdata.base < 0 |
| 372 | ? gpio_mockup_ranges[i * 2 + 1] |
| 373 | : gpio_mockup_ranges[i * 2 + 1] - pdata.base; |
Bartosz Golaszewski | c3196a7 | 2017-11-27 11:48:44 +0100 | [diff] [blame] | 374 | pdata.named_lines = gpio_mockup_named_lines; |
Bartosz Golaszewski | 8a39f59 | 2017-11-27 11:48:40 +0100 | [diff] [blame] | 375 | |
| 376 | pdev = platform_device_register_resndata(NULL, |
| 377 | GPIO_MOCKUP_NAME, |
| 378 | i, NULL, 0, &pdata, |
| 379 | sizeof(pdata)); |
| 380 | if (!pdev) { |
Bartosz Golaszewski | f1bec99 | 2017-11-27 11:48:41 +0100 | [diff] [blame] | 381 | gpio_mockup_err("error registering device"); |
Bartosz Golaszewski | 8a39f59 | 2017-11-27 11:48:40 +0100 | [diff] [blame] | 382 | platform_driver_unregister(&gpio_mockup_driver); |
| 383 | gpio_mockup_unregister_pdevs(); |
| 384 | return -ENOMEM; |
| 385 | } |
| 386 | |
| 387 | gpio_mockup_pdevs[i] = pdev; |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | return 0; |
| 391 | } |
| 392 | |
Bartosz Golaszewski | f3b4717 | 2017-11-27 11:48:37 +0100 | [diff] [blame] | 393 | static void __exit gpio_mockup_exit(void) |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 394 | { |
Bartosz Golaszewski | 9202ba2 | 2017-02-06 15:11:08 +0100 | [diff] [blame] | 395 | debugfs_remove_recursive(gpio_mockup_dbg_dir); |
Bartosz Golaszewski | ca409160 | 2017-02-06 13:10:35 +0100 | [diff] [blame] | 396 | platform_driver_unregister(&gpio_mockup_driver); |
Bartosz Golaszewski | 8a39f59 | 2017-11-27 11:48:40 +0100 | [diff] [blame] | 397 | gpio_mockup_unregister_pdevs(); |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 398 | } |
| 399 | |
Bartosz Golaszewski | f3b4717 | 2017-11-27 11:48:37 +0100 | [diff] [blame] | 400 | module_init(gpio_mockup_init); |
| 401 | module_exit(gpio_mockup_exit); |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 402 | |
| 403 | MODULE_AUTHOR("Kamlakant Patel <kamlakant.patel@broadcom.com>"); |
| 404 | MODULE_AUTHOR("Bamvor Jian Zhang <bamvor.zhangjian@linaro.org>"); |
Bartosz Golaszewski | c60c7f4 | 2017-06-09 13:41:31 +0200 | [diff] [blame] | 405 | MODULE_AUTHOR("Bartosz Golaszewski <brgl@bgdev.pl>"); |
Bamvor Jian Zhang | 0f98dd1 | 2016-08-31 11:45:46 +0200 | [diff] [blame] | 406 | MODULE_DESCRIPTION("GPIO Testing driver"); |
| 407 | MODULE_LICENSE("GPL v2"); |