blob: 8269cffc2967f772ba2da14e566fd136bb68da56 [file] [log] [blame]
Bartosz Golaszewski058de342018-04-26 22:12:36 +02001// SPDX-License-Identifier: GPL-2.0+
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +02002/*
3 * GPIO Testing Device Driver
4 *
5 * Copyright (C) 2014 Kamlakant Patel <kamlakant.patel@broadcom.com>
Linus Walleij4f9a4cd2018-03-06 09:04:15 +01006 * Copyright (C) 2015-2016 Bamvor Jian Zhang <bamv2005@gmail.com>
Bartosz Golaszewskic60c7f42017-06-09 13:41:31 +02007 * Copyright (C) 2017 Bartosz Golaszewski <brgl@bgdev.pl>
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +02008 */
9
10#include <linux/init.h>
11#include <linux/module.h>
12#include <linux/gpio/driver.h>
Bartosz Golaszewski9202ba22017-02-06 15:11:08 +010013#include <linux/gpio/consumer.h>
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +020014#include <linux/platform_device.h>
Bartosz Golaszewski8a68ea02017-02-06 13:10:37 +010015#include <linux/slab.h>
Bartosz Golaszewskie2ff7402017-02-06 15:11:07 +010016#include <linux/interrupt.h>
17#include <linux/irq.h>
Bartosz Golaszewskib4c495f2017-08-14 13:20:56 +020018#include <linux/irq_sim.h>
Bartosz Golaszewski9202ba22017-02-06 15:11:08 +010019#include <linux/debugfs.h>
20#include <linux/uaccess.h>
Bartosz Golaszewski3ea47b42018-09-25 13:29:46 +020021#include <linux/property.h>
Bartosz Golaszewski9202ba22017-02-06 15:11:08 +010022
23#include "gpiolib.h"
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +020024
Bartosz Golaszewskica4091602017-02-06 13:10:35 +010025#define GPIO_MOCKUP_NAME "gpio-mockup"
Bartosz Golaszewskib9576d02017-11-27 11:48:42 +010026#define GPIO_MOCKUP_MAX_GC 10
Bartosz Golaszewskib6523362017-06-09 13:41:28 +020027/*
28 * We're storing two values per chip: the GPIO base and the number
29 * of GPIO lines.
30 */
31#define GPIO_MOCKUP_MAX_RANGES (GPIO_MOCKUP_MAX_GC * 2)
Bartosz Golaszewski3ea47b42018-09-25 13:29:46 +020032/* Maximum of three properties + the sentinel. */
33#define GPIO_MOCKUP_MAX_PROP 4
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +020034
Bartosz Golaszewskif1bec992017-11-27 11:48:41 +010035#define gpio_mockup_err(...) pr_err(GPIO_MOCKUP_NAME ": " __VA_ARGS__)
36
Bartosz Golaszewskica4091602017-02-06 13:10:35 +010037enum {
Bartosz Golaszewskic650c002017-05-25 10:33:39 +020038 GPIO_MOCKUP_DIR_OUT = 0,
39 GPIO_MOCKUP_DIR_IN = 1,
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +020040};
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 Golaszewskica4091602017-02-06 13:10:35 +010047struct gpio_mockup_line_status {
48 int dir;
Bartosz Golaszewski5b7908c2017-11-27 11:48:46 +010049 int value;
Bartosz Golaszewskie2ff7402017-02-06 15:11:07 +010050};
51
Bartosz Golaszewskica4091602017-02-06 13:10:35 +010052struct gpio_mockup_chip {
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +020053 struct gpio_chip gc;
Bartosz Golaszewskica4091602017-02-06 13:10:35 +010054 struct gpio_mockup_line_status *lines;
Bartosz Golaszewskib4c495f2017-08-14 13:20:56 +020055 struct irq_sim irqsim;
Bartosz Golaszewski9202ba22017-02-06 15:11:08 +010056 struct dentry *dbg_dir;
57};
58
59struct gpio_mockup_dbgfs_private {
60 struct gpio_mockup_chip *chip;
61 struct gpio_desc *desc;
62 int offset;
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +020063};
64
Bartosz Golaszewskib6523362017-06-09 13:41:28 +020065static int gpio_mockup_ranges[GPIO_MOCKUP_MAX_RANGES];
Bartosz Golaszewskie63a0062017-11-27 11:48:53 +010066static int gpio_mockup_num_ranges;
67module_param_array(gpio_mockup_ranges, int, &gpio_mockup_num_ranges, 0400);
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +020068
Bartosz Golaszewski8a68ea02017-02-06 13:10:37 +010069static bool gpio_mockup_named_lines;
70module_param_named(gpio_mockup_named_lines,
71 gpio_mockup_named_lines, bool, 0400);
72
Bartosz Golaszewski9202ba22017-02-06 15:11:08 +010073static struct dentry *gpio_mockup_dbg_dir;
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +020074
Bartosz Golaszewskicd9835f2017-11-27 11:48:54 +010075static int gpio_mockup_range_base(unsigned int index)
76{
77 return gpio_mockup_ranges[index * 2];
78}
79
80static int gpio_mockup_range_ngpio(unsigned int index)
81{
82 return gpio_mockup_ranges[index * 2 + 1];
83}
84
Bartosz Golaszewskica4091602017-02-06 13:10:35 +010085static int gpio_mockup_get(struct gpio_chip *gc, unsigned int offset)
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +020086{
Bartosz Golaszewskica4091602017-02-06 13:10:35 +010087 struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +020088
Bartosz Golaszewskica4091602017-02-06 13:10:35 +010089 return chip->lines[offset].value;
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +020090}
91
Bartosz Golaszewski2a9d7422017-11-27 11:48:50 +010092static void gpio_mockup_set(struct gpio_chip *gc,
93 unsigned int offset, int value)
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +020094{
Bartosz Golaszewskica4091602017-02-06 13:10:35 +010095 struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +020096
Bartosz Golaszewskica4091602017-02-06 13:10:35 +010097 chip->lines[offset].value = !!value;
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +020098}
99
Bartosz Golaszewskic47bee92017-11-27 11:48:51 +0100100static void gpio_mockup_set_multiple(struct gpio_chip *gc,
101 unsigned long *mask, unsigned long *bits)
102{
103 unsigned int bit;
104
105 for_each_set_bit(bit, mask, gc->ngpio)
106 gpio_mockup_set(gc, bit, test_bit(bit, bits));
107
108}
109
Bartosz Golaszewski2a9d7422017-11-27 11:48:50 +0100110static int gpio_mockup_dirout(struct gpio_chip *gc,
111 unsigned int offset, int value)
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +0200112{
Bartosz Golaszewskica4091602017-02-06 13:10:35 +0100113 struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +0200114
Bartosz Golaszewskica4091602017-02-06 13:10:35 +0100115 gpio_mockup_set(gc, offset, value);
Bartosz Golaszewskic650c002017-05-25 10:33:39 +0200116 chip->lines[offset].dir = GPIO_MOCKUP_DIR_OUT;
Bartosz Golaszewskica4091602017-02-06 13:10:35 +0100117
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +0200118 return 0;
119}
120
Bartosz Golaszewskica4091602017-02-06 13:10:35 +0100121static int gpio_mockup_dirin(struct gpio_chip *gc, unsigned int offset)
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +0200122{
Bartosz Golaszewskica4091602017-02-06 13:10:35 +0100123 struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +0200124
Bartosz Golaszewskic650c002017-05-25 10:33:39 +0200125 chip->lines[offset].dir = GPIO_MOCKUP_DIR_IN;
Bartosz Golaszewskica4091602017-02-06 13:10:35 +0100126
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +0200127 return 0;
128}
129
Bartosz Golaszewskica4091602017-02-06 13:10:35 +0100130static int gpio_mockup_get_direction(struct gpio_chip *gc, unsigned int offset)
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +0200131{
Bartosz Golaszewskica4091602017-02-06 13:10:35 +0100132 struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +0200133
Bartosz Golaszewskica4091602017-02-06 13:10:35 +0100134 return chip->lines[offset].dir;
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +0200135}
136
Bartosz Golaszewskib4c495f2017-08-14 13:20:56 +0200137static int gpio_mockup_to_irq(struct gpio_chip *gc, unsigned int offset)
Bartosz Golaszewskie2ff7402017-02-06 15:11:07 +0100138{
Bartosz Golaszewskic7f53262017-05-25 10:33:41 +0200139 struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
140
Bartosz Golaszewskib4c495f2017-08-14 13:20:56 +0200141 return irq_sim_irqnum(&chip->irqsim, offset);
Bartosz Golaszewskie2ff7402017-02-06 15:11:07 +0100142}
143
Bartosz Golaszewski9202ba22017-02-06 15:11:08 +0100144static ssize_t gpio_mockup_event_write(struct file *file,
145 const char __user *usr_buf,
146 size_t size, loff_t *ppos)
147{
148 struct gpio_mockup_dbgfs_private *priv;
149 struct gpio_mockup_chip *chip;
150 struct seq_file *sfile;
151 struct gpio_desc *desc;
Bartosz Golaszewski6f9b3e72017-06-09 13:41:25 +0200152 int rv, val;
Bartosz Golaszewski9202ba22017-02-06 15:11:08 +0100153
Bartosz Golaszewski6f9b3e72017-06-09 13:41:25 +0200154 rv = kstrtoint_from_user(usr_buf, size, 0, &val);
155 if (rv)
156 return rv;
157 if (val != 0 && val != 1)
158 return -EINVAL;
159
Bartosz Golaszewski650b57b2017-06-09 13:41:26 +0200160 sfile = file->private_data;
161 priv = sfile->private;
162 desc = priv->desc;
163 chip = priv->chip;
Bartosz Golaszewskic7f53262017-05-25 10:33:41 +0200164
Bartosz Golaszewskib4c495f2017-08-14 13:20:56 +0200165 gpiod_set_value_cansleep(desc, val);
166 irq_sim_fire(&chip->irqsim, priv->offset);
Bartosz Golaszewski9202ba22017-02-06 15:11:08 +0100167
168 return size;
169}
170
171static int gpio_mockup_event_open(struct inode *inode, struct file *file)
172{
173 return single_open(file, NULL, inode->i_private);
174}
175
176static const struct file_operations gpio_mockup_event_ops = {
177 .owner = THIS_MODULE,
178 .open = gpio_mockup_event_open,
179 .write = gpio_mockup_event_write,
180 .llseek = no_llseek,
181};
182
183static void gpio_mockup_debugfs_setup(struct device *dev,
184 struct gpio_mockup_chip *chip)
185{
186 struct gpio_mockup_dbgfs_private *priv;
Bartosz Golaszewskica8792a2017-11-27 11:48:45 +0100187 struct dentry *evfile, *link;
Bartosz Golaszewski9202ba22017-02-06 15:11:08 +0100188 struct gpio_chip *gc;
Bartosz Golaszewskica8792a2017-11-27 11:48:45 +0100189 const char *devname;
Bartosz Golaszewski9202ba22017-02-06 15:11:08 +0100190 char *name;
191 int i;
192
193 gc = &chip->gc;
Bartosz Golaszewskica8792a2017-11-27 11:48:45 +0100194 devname = dev_name(&gc->gpiodev->dev);
Bartosz Golaszewski9202ba22017-02-06 15:11:08 +0100195
Bartosz Golaszewskica8792a2017-11-27 11:48:45 +0100196 chip->dbg_dir = debugfs_create_dir(devname, gpio_mockup_dbg_dir);
Bartosz Golaszewski46526c12017-11-27 11:48:48 +0100197 if (IS_ERR_OR_NULL(chip->dbg_dir))
Bartosz Golaszewski9202ba22017-02-06 15:11:08 +0100198 goto err;
199
Bartosz Golaszewskica8792a2017-11-27 11:48:45 +0100200 link = debugfs_create_symlink(gc->label, gpio_mockup_dbg_dir, devname);
Bartosz Golaszewski46526c12017-11-27 11:48:48 +0100201 if (IS_ERR_OR_NULL(link))
Bartosz Golaszewskica8792a2017-11-27 11:48:45 +0100202 goto err;
203
Bartosz Golaszewski9202ba22017-02-06 15:11:08 +0100204 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);
Bartosz Golaszewski46526c12017-11-27 11:48:48 +0100219 if (IS_ERR_OR_NULL(evfile))
Bartosz Golaszewski9202ba22017-02-06 15:11:08 +0100220 goto err;
221 }
222
223 return;
224
225err:
Bartosz Golaszewski46526c12017-11-27 11:48:48 +0100226 dev_err(dev, "error creating debugfs event files\n");
Bartosz Golaszewski9202ba22017-02-06 15:11:08 +0100227}
228
Bartosz Golaszewski1cea4732017-11-27 11:48:47 +0100229static int gpio_mockup_name_lines(struct device *dev,
230 struct gpio_mockup_chip *chip)
231{
232 struct gpio_chip *gc = &chip->gc;
233 char **names;
234 int i;
235
236 names = devm_kcalloc(dev, gc->ngpio, sizeof(char *), GFP_KERNEL);
237 if (!names)
238 return -ENOMEM;
239
240 for (i = 0; i < gc->ngpio; i++) {
241 names[i] = devm_kasprintf(dev, GFP_KERNEL,
242 "%s-%d", gc->label, i);
243 if (!names[i])
244 return -ENOMEM;
245 }
246
247 gc->names = (const char *const *)names;
248
249 return 0;
250}
251
Bartosz Golaszewskica4091602017-02-06 13:10:35 +0100252static int gpio_mockup_probe(struct platform_device *pdev)
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +0200253{
Bartosz Golaszewski8a39f592017-11-27 11:48:40 +0100254 struct gpio_mockup_chip *chip;
Bartosz Golaszewski6d974d32017-11-27 11:48:43 +0100255 struct gpio_chip *gc;
Bartosz Golaszewski8a39f592017-11-27 11:48:40 +0100256 struct device *dev;
Bartosz Golaszewski3ea47b42018-09-25 13:29:46 +0200257 const char *name;
258 int rv, base;
259 u16 ngpio;
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +0200260
Bartosz Golaszewski8a39f592017-11-27 11:48:40 +0100261 dev = &pdev->dev;
Bartosz Golaszewski3ea47b42018-09-25 13:29:46 +0200262
263 rv = device_property_read_u32(dev, "gpio-base", &base);
264 if (rv)
265 base = -1;
266
267 rv = device_property_read_u16(dev, "nr-gpios", &ngpio);
268 if (rv)
269 return rv;
270
271 rv = device_property_read_string(dev, "chip-name", &name);
272 if (rv)
273 name = NULL;
Bartosz Golaszewskib6523362017-06-09 13:41:28 +0200274
Bartosz Golaszewski8a39f592017-11-27 11:48:40 +0100275 chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL);
276 if (!chip)
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +0200277 return -ENOMEM;
278
Bartosz Golaszewski3ea47b42018-09-25 13:29:46 +0200279 if (!name) {
280 name = devm_kasprintf(dev, GFP_KERNEL,
281 "%s-%c", pdev->name, pdev->id + 'A');
282 if (!name)
283 return -ENOMEM;
284 }
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +0200285
Bartosz Golaszewski6d974d32017-11-27 11:48:43 +0100286 gc = &chip->gc;
287 gc->base = base;
288 gc->ngpio = ngpio;
289 gc->label = name;
290 gc->owner = THIS_MODULE;
291 gc->parent = dev;
292 gc->get = gpio_mockup_get;
293 gc->set = gpio_mockup_set;
Bartosz Golaszewskic47bee92017-11-27 11:48:51 +0100294 gc->set_multiple = gpio_mockup_set_multiple;
Bartosz Golaszewski6d974d32017-11-27 11:48:43 +0100295 gc->direction_output = gpio_mockup_dirout;
296 gc->direction_input = gpio_mockup_dirin;
297 gc->get_direction = gpio_mockup_get_direction;
298 gc->to_irq = gpio_mockup_to_irq;
299
300 chip->lines = devm_kcalloc(dev, gc->ngpio,
301 sizeof(*chip->lines), GFP_KERNEL);
302 if (!chip->lines)
303 return -ENOMEM;
304
Bartosz Golaszewski3ea47b42018-09-25 13:29:46 +0200305 if (device_property_read_bool(dev, "named-gpio-lines")) {
Bartosz Golaszewski6d974d32017-11-27 11:48:43 +0100306 rv = gpio_mockup_name_lines(dev, chip);
307 if (rv)
308 return rv;
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +0200309 }
310
Bartosz Golaszewski6d974d32017-11-27 11:48:43 +0100311 rv = devm_irq_sim_init(dev, &chip->irqsim, gc->ngpio);
Bartosz Golaszewski1e0dca62017-11-27 11:48:52 +0100312 if (rv < 0)
Bartosz Golaszewski6d974d32017-11-27 11:48:43 +0100313 return rv;
314
315 rv = devm_gpiochip_add_data(dev, &chip->gc, chip);
316 if (rv)
317 return rv;
318
Bartosz Golaszewski6cb92152018-03-04 13:45:51 +0100319 if (!IS_ERR_OR_NULL(gpio_mockup_dbg_dir))
Bartosz Golaszewski6d974d32017-11-27 11:48:43 +0100320 gpio_mockup_debugfs_setup(dev, chip);
321
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +0200322 return 0;
323}
324
Bartosz Golaszewskica4091602017-02-06 13:10:35 +0100325static struct platform_driver gpio_mockup_driver = {
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +0200326 .driver = {
Bartosz Golaszewskica4091602017-02-06 13:10:35 +0100327 .name = GPIO_MOCKUP_NAME,
Bartosz Golaszewski364ac452016-12-20 12:28:20 +0100328 },
Bartosz Golaszewskica4091602017-02-06 13:10:35 +0100329 .probe = gpio_mockup_probe,
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +0200330};
331
Bartosz Golaszewski8a39f592017-11-27 11:48:40 +0100332static struct platform_device *gpio_mockup_pdevs[GPIO_MOCKUP_MAX_GC];
333
334static void gpio_mockup_unregister_pdevs(void)
335{
336 struct platform_device *pdev;
337 int i;
338
339 for (i = 0; i < GPIO_MOCKUP_MAX_GC; i++) {
340 pdev = gpio_mockup_pdevs[i];
341
342 if (pdev)
343 platform_device_unregister(pdev);
344 }
345}
Bartosz Golaszewskif3b47172017-11-27 11:48:37 +0100346
347static int __init gpio_mockup_init(void)
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +0200348{
Bartosz Golaszewski3ea47b42018-09-25 13:29:46 +0200349 struct property_entry properties[GPIO_MOCKUP_MAX_PROP];
350 int i, prop, num_chips, err = 0, base;
351 struct platform_device_info pdevinfo;
Bartosz Golaszewski8a39f592017-11-27 11:48:40 +0100352 struct platform_device *pdev;
Bartosz Golaszewski3ea47b42018-09-25 13:29:46 +0200353 u16 ngpio;
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +0200354
Bartosz Golaszewskie63a0062017-11-27 11:48:53 +0100355 if ((gpio_mockup_num_ranges < 2) ||
356 (gpio_mockup_num_ranges % 2) ||
357 (gpio_mockup_num_ranges > GPIO_MOCKUP_MAX_RANGES))
Bartosz Golaszewski20c35ac2017-11-27 11:48:38 +0100358 return -EINVAL;
359
Bartosz Golaszewski8a39f592017-11-27 11:48:40 +0100360 /* Each chip is described by two values. */
Bartosz Golaszewskie63a0062017-11-27 11:48:53 +0100361 num_chips = gpio_mockup_num_ranges / 2;
Bartosz Golaszewski8a39f592017-11-27 11:48:40 +0100362
Bartosz Golaszewskifa869632017-11-27 11:48:49 +0100363 /*
364 * The second value in the <base GPIO - number of GPIOS> pair must
365 * always be greater than 0.
366 */
367 for (i = 0; i < num_chips; i++) {
Bartosz Golaszewskicd9835f2017-11-27 11:48:54 +0100368 if (gpio_mockup_range_ngpio(i) < 0)
Bartosz Golaszewskifa869632017-11-27 11:48:49 +0100369 return -EINVAL;
370 }
371
Bartosz Golaszewski9202ba22017-02-06 15:11:08 +0100372 gpio_mockup_dbg_dir = debugfs_create_dir("gpio-mockup-event", NULL);
Bartosz Golaszewski46526c12017-11-27 11:48:48 +0100373 if (IS_ERR_OR_NULL(gpio_mockup_dbg_dir))
Bartosz Golaszewskif1bec992017-11-27 11:48:41 +0100374 gpio_mockup_err("error creating debugfs directory\n");
Bartosz Golaszewski9202ba22017-02-06 15:11:08 +0100375
Bartosz Golaszewski8a39f592017-11-27 11:48:40 +0100376 err = platform_driver_register(&gpio_mockup_driver);
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +0200377 if (err) {
Bartosz Golaszewskif1bec992017-11-27 11:48:41 +0100378 gpio_mockup_err("error registering platform driver\n");
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +0200379 return err;
380 }
381
Bartosz Golaszewski8a39f592017-11-27 11:48:40 +0100382 for (i = 0; i < num_chips; i++) {
Bartosz Golaszewski3ea47b42018-09-25 13:29:46 +0200383 memset(properties, 0, sizeof(properties));
384 memset(&pdevinfo, 0, sizeof(pdevinfo));
385 prop = 0;
Bartosz Golaszewski8a39f592017-11-27 11:48:40 +0100386
Bartosz Golaszewski3ea47b42018-09-25 13:29:46 +0200387 base = gpio_mockup_range_base(i);
388 if (base >= 0)
389 properties[prop++] = PROPERTY_ENTRY_U32("gpio-base",
390 base);
391
392 ngpio = base < 0 ? gpio_mockup_range_ngpio(i)
393 : gpio_mockup_range_ngpio(i) - base;
394 properties[prop++] = PROPERTY_ENTRY_U16("nr-gpios", ngpio);
395
396 if (gpio_mockup_named_lines)
397 properties[prop++] = PROPERTY_ENTRY_BOOL(
398 "named-gpio-lines");
399
400 pdevinfo.name = GPIO_MOCKUP_NAME;
401 pdevinfo.id = i;
402 pdevinfo.properties = properties;
403
404 pdev = platform_device_register_full(&pdevinfo);
Bartosz Golaszewskic4b54e12017-12-06 17:30:02 +0100405 if (IS_ERR(pdev)) {
Bartosz Golaszewskif1bec992017-11-27 11:48:41 +0100406 gpio_mockup_err("error registering device");
Bartosz Golaszewski8a39f592017-11-27 11:48:40 +0100407 platform_driver_unregister(&gpio_mockup_driver);
408 gpio_mockup_unregister_pdevs();
Bartosz Golaszewskic4b54e12017-12-06 17:30:02 +0100409 return PTR_ERR(pdev);
Bartosz Golaszewski8a39f592017-11-27 11:48:40 +0100410 }
411
412 gpio_mockup_pdevs[i] = pdev;
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +0200413 }
414
415 return 0;
416}
417
Bartosz Golaszewskif3b47172017-11-27 11:48:37 +0100418static void __exit gpio_mockup_exit(void)
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +0200419{
Bartosz Golaszewski9202ba22017-02-06 15:11:08 +0100420 debugfs_remove_recursive(gpio_mockup_dbg_dir);
Bartosz Golaszewskica4091602017-02-06 13:10:35 +0100421 platform_driver_unregister(&gpio_mockup_driver);
Bartosz Golaszewski8a39f592017-11-27 11:48:40 +0100422 gpio_mockup_unregister_pdevs();
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +0200423}
424
Bartosz Golaszewskif3b47172017-11-27 11:48:37 +0100425module_init(gpio_mockup_init);
426module_exit(gpio_mockup_exit);
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +0200427
428MODULE_AUTHOR("Kamlakant Patel <kamlakant.patel@broadcom.com>");
Linus Walleij4f9a4cd2018-03-06 09:04:15 +0100429MODULE_AUTHOR("Bamvor Jian Zhang <bamv2005@gmail.com>");
Bartosz Golaszewskic60c7f42017-06-09 13:41:31 +0200430MODULE_AUTHOR("Bartosz Golaszewski <brgl@bgdev.pl>");
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +0200431MODULE_DESCRIPTION("GPIO Testing driver");
432MODULE_LICENSE("GPL v2");