Commit | Line | Data |
---|---|---|
f2f67983 | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
0f98dd1b BJZ |
2 | /* |
3 | * GPIO Testing Device Driver | |
4 | * | |
5 | * Copyright (C) 2014 Kamlakant Patel <kamlakant.patel@broadcom.com> | |
4f9a4cd6 | 6 | * Copyright (C) 2015-2016 Bamvor Jian Zhang <bamv2005@gmail.com> |
c60c7f4c | 7 | * Copyright (C) 2017 Bartosz Golaszewski <brgl@bgdev.pl> |
0f98dd1b BJZ |
8 | */ |
9 | ||
56f6cb35 BG |
10 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
11 | ||
726a4453 | 12 | #include <linux/debugfs.h> |
726a4453 | 13 | #include <linux/gpio/driver.h> |
e2ff7408 BG |
14 | #include <linux/interrupt.h> |
15 | #include <linux/irq.h> | |
b4c495f0 | 16 | #include <linux/irq_sim.h> |
337cbeb2 | 17 | #include <linux/irqdomain.h> |
43ddebdd | 18 | #include <linux/mod_devicetable.h> |
726a4453 BG |
19 | #include <linux/module.h> |
20 | #include <linux/platform_device.h> | |
3ea47b44 | 21 | #include <linux/property.h> |
5cedd3c2 | 22 | #include <linux/seq_file.h> |
726a4453 | 23 | #include <linux/slab.h> |
582be05e | 24 | #include <linux/string_helpers.h> |
726a4453 | 25 | #include <linux/uaccess.h> |
9202ba23 BG |
26 | |
27 | #include "gpiolib.h" | |
0f98dd1b | 28 | |
b9576d03 | 29 | #define GPIO_MOCKUP_MAX_GC 10 |
b652336d BG |
30 | /* |
31 | * We're storing two values per chip: the GPIO base and the number | |
32 | * of GPIO lines. | |
33 | */ | |
34 | #define GPIO_MOCKUP_MAX_RANGES (GPIO_MOCKUP_MAX_GC * 2) | |
383bb2de BG |
35 | /* Maximum of four properties + the sentinel. */ |
36 | #define GPIO_MOCKUP_MAX_PROP 5 | |
0f98dd1b | 37 | |
0f98dd1b BJZ |
38 | /* |
39 | * struct gpio_pin_status - structure describing a GPIO status | |
e42615ec | 40 | * @dir: Configures direction of gpio as "in" or "out" |
0f98dd1b BJZ |
41 | * @value: Configures status of the gpio as 0(low) or 1(high) |
42 | */ | |
ca409160 BG |
43 | struct gpio_mockup_line_status { |
44 | int dir; | |
5b7908c0 | 45 | int value; |
2a9e2740 | 46 | int pull; |
e2ff7408 BG |
47 | }; |
48 | ||
ca409160 | 49 | struct gpio_mockup_chip { |
0f98dd1b | 50 | struct gpio_chip gc; |
ca409160 | 51 | struct gpio_mockup_line_status *lines; |
337cbeb2 | 52 | struct irq_domain *irq_sim_domain; |
9202ba23 | 53 | struct dentry *dbg_dir; |
9212492f | 54 | struct mutex lock; |
9202ba23 BG |
55 | }; |
56 | ||
57 | struct gpio_mockup_dbgfs_private { | |
58 | struct gpio_mockup_chip *chip; | |
59 | struct gpio_desc *desc; | |
83336668 | 60 | unsigned int offset; |
0f98dd1b BJZ |
61 | }; |
62 | ||
b652336d | 63 | static int gpio_mockup_ranges[GPIO_MOCKUP_MAX_RANGES]; |
e63a006f BG |
64 | static int gpio_mockup_num_ranges; |
65 | module_param_array(gpio_mockup_ranges, int, &gpio_mockup_num_ranges, 0400); | |
0f98dd1b | 66 | |
8a68ea00 BG |
67 | static bool gpio_mockup_named_lines; |
68 | module_param_named(gpio_mockup_named_lines, | |
69 | gpio_mockup_named_lines, bool, 0400); | |
70 | ||
9202ba23 | 71 | static struct dentry *gpio_mockup_dbg_dir; |
0f98dd1b | 72 | |
cd9835f1 BG |
73 | static int gpio_mockup_range_base(unsigned int index) |
74 | { | |
75 | return gpio_mockup_ranges[index * 2]; | |
76 | } | |
77 | ||
78 | static int gpio_mockup_range_ngpio(unsigned int index) | |
79 | { | |
80 | return gpio_mockup_ranges[index * 2 + 1]; | |
81 | } | |
82 | ||
e09313ce BG |
83 | static int __gpio_mockup_get(struct gpio_mockup_chip *chip, |
84 | unsigned int offset) | |
0f98dd1b | 85 | { |
ca409160 | 86 | return chip->lines[offset].value; |
0f98dd1b BJZ |
87 | } |
88 | ||
9212492f BG |
89 | static int gpio_mockup_get(struct gpio_chip *gc, unsigned int offset) |
90 | { | |
91 | struct gpio_mockup_chip *chip = gpiochip_get_data(gc); | |
92 | int val; | |
93 | ||
94 | mutex_lock(&chip->lock); | |
e09313ce | 95 | val = __gpio_mockup_get(chip, offset); |
9212492f BG |
96 | mutex_unlock(&chip->lock); |
97 | ||
98 | return val; | |
99 | } | |
100 | ||
cbf1e092 BG |
101 | static int gpio_mockup_get_multiple(struct gpio_chip *gc, |
102 | unsigned long *mask, unsigned long *bits) | |
103 | { | |
104 | struct gpio_mockup_chip *chip = gpiochip_get_data(gc); | |
105 | unsigned int bit, val; | |
106 | ||
107 | mutex_lock(&chip->lock); | |
108 | for_each_set_bit(bit, mask, gc->ngpio) { | |
e09313ce | 109 | val = __gpio_mockup_get(chip, bit); |
cbf1e092 BG |
110 | __assign_bit(bit, bits, val); |
111 | } | |
112 | mutex_unlock(&chip->lock); | |
113 | ||
114 | return 0; | |
115 | } | |
116 | ||
e09313ce | 117 | static void __gpio_mockup_set(struct gpio_mockup_chip *chip, |
9212492f | 118 | unsigned int offset, int value) |
0f98dd1b | 119 | { |
ca409160 | 120 | chip->lines[offset].value = !!value; |
0f98dd1b BJZ |
121 | } |
122 | ||
9212492f BG |
123 | static void gpio_mockup_set(struct gpio_chip *gc, |
124 | unsigned int offset, int value) | |
125 | { | |
126 | struct gpio_mockup_chip *chip = gpiochip_get_data(gc); | |
127 | ||
128 | mutex_lock(&chip->lock); | |
e09313ce | 129 | __gpio_mockup_set(chip, offset, value); |
9212492f BG |
130 | mutex_unlock(&chip->lock); |
131 | } | |
132 | ||
c47bee95 BG |
133 | static void gpio_mockup_set_multiple(struct gpio_chip *gc, |
134 | unsigned long *mask, unsigned long *bits) | |
135 | { | |
9212492f | 136 | struct gpio_mockup_chip *chip = gpiochip_get_data(gc); |
c47bee95 BG |
137 | unsigned int bit; |
138 | ||
9212492f | 139 | mutex_lock(&chip->lock); |
c47bee95 | 140 | for_each_set_bit(bit, mask, gc->ngpio) |
e09313ce | 141 | __gpio_mockup_set(chip, bit, test_bit(bit, bits)); |
9212492f | 142 | mutex_unlock(&chip->lock); |
c47bee95 BG |
143 | } |
144 | ||
64e7112e KG |
145 | static int gpio_mockup_apply_pull(struct gpio_mockup_chip *chip, |
146 | unsigned int offset, int value) | |
147 | { | |
e7d8fde4 AS |
148 | struct gpio_chip *gc = &chip->gc; |
149 | struct gpio_desc *desc = gpiochip_get_desc(gc, offset); | |
337cbeb2 | 150 | int curr, irq, irq_type, ret = 0; |
64e7112e KG |
151 | |
152 | mutex_lock(&chip->lock); | |
153 | ||
154 | if (test_bit(FLAG_REQUESTED, &desc->flags) && | |
ee8598ae | 155 | !test_bit(FLAG_IS_OUT, &desc->flags)) { |
64e7112e KG |
156 | curr = __gpio_mockup_get(chip, offset); |
157 | if (curr == value) | |
158 | goto out; | |
159 | ||
337cbeb2 BG |
160 | irq = irq_find_mapping(chip->irq_sim_domain, offset); |
161 | if (!irq) | |
162 | /* | |
163 | * This is fine - it just means, nobody is listening | |
164 | * for interrupts on this line, otherwise | |
165 | * irq_create_mapping() would have been called from | |
166 | * the to_irq() callback. | |
167 | */ | |
168 | goto set_value; | |
169 | ||
64e7112e KG |
170 | irq_type = irq_get_trigger_type(irq); |
171 | ||
172 | if ((value == 1 && (irq_type & IRQ_TYPE_EDGE_RISING)) || | |
337cbeb2 BG |
173 | (value == 0 && (irq_type & IRQ_TYPE_EDGE_FALLING))) { |
174 | ret = irq_set_irqchip_state(irq, IRQCHIP_STATE_PENDING, | |
175 | true); | |
176 | if (ret) | |
177 | goto out; | |
178 | } | |
64e7112e KG |
179 | } |
180 | ||
337cbeb2 | 181 | set_value: |
64e7112e KG |
182 | /* Change the value unless we're actively driving the line. */ |
183 | if (!test_bit(FLAG_REQUESTED, &desc->flags) || | |
f6e51bb3 | 184 | !test_bit(FLAG_IS_OUT, &desc->flags)) |
64e7112e KG |
185 | __gpio_mockup_set(chip, offset, value); |
186 | ||
187 | out: | |
188 | chip->lines[offset].pull = value; | |
189 | mutex_unlock(&chip->lock); | |
337cbeb2 | 190 | return ret; |
64e7112e KG |
191 | } |
192 | ||
193 | static int gpio_mockup_set_config(struct gpio_chip *gc, | |
194 | unsigned int offset, unsigned long config) | |
195 | { | |
196 | struct gpio_mockup_chip *chip = gpiochip_get_data(gc); | |
197 | ||
198 | switch (pinconf_to_config_param(config)) { | |
199 | case PIN_CONFIG_BIAS_PULL_UP: | |
200 | return gpio_mockup_apply_pull(chip, offset, 1); | |
201 | case PIN_CONFIG_BIAS_PULL_DOWN: | |
202 | return gpio_mockup_apply_pull(chip, offset, 0); | |
203 | default: | |
204 | break; | |
205 | } | |
206 | return -ENOTSUPP; | |
207 | } | |
208 | ||
2a9d742c BG |
209 | static int gpio_mockup_dirout(struct gpio_chip *gc, |
210 | unsigned int offset, int value) | |
0f98dd1b | 211 | { |
ca409160 BG |
212 | struct gpio_mockup_chip *chip = gpiochip_get_data(gc); |
213 | ||
9212492f | 214 | mutex_lock(&chip->lock); |
e42615ec | 215 | chip->lines[offset].dir = GPIO_LINE_DIRECTION_OUT; |
e09313ce | 216 | __gpio_mockup_set(chip, offset, value); |
9212492f | 217 | mutex_unlock(&chip->lock); |
0f98dd1b | 218 | |
0f98dd1b BJZ |
219 | return 0; |
220 | } | |
221 | ||
ca409160 | 222 | static int gpio_mockup_dirin(struct gpio_chip *gc, unsigned int offset) |
0f98dd1b | 223 | { |
ca409160 BG |
224 | struct gpio_mockup_chip *chip = gpiochip_get_data(gc); |
225 | ||
9212492f | 226 | mutex_lock(&chip->lock); |
e42615ec | 227 | chip->lines[offset].dir = GPIO_LINE_DIRECTION_IN; |
9212492f | 228 | mutex_unlock(&chip->lock); |
0f98dd1b | 229 | |
0f98dd1b BJZ |
230 | return 0; |
231 | } | |
232 | ||
ca409160 | 233 | static int gpio_mockup_get_direction(struct gpio_chip *gc, unsigned int offset) |
0f98dd1b | 234 | { |
ca409160 | 235 | struct gpio_mockup_chip *chip = gpiochip_get_data(gc); |
9212492f | 236 | int direction; |
0f98dd1b | 237 | |
9212492f | 238 | mutex_lock(&chip->lock); |
bc7bc688 | 239 | direction = chip->lines[offset].dir; |
9212492f BG |
240 | mutex_unlock(&chip->lock); |
241 | ||
242 | return direction; | |
0f98dd1b BJZ |
243 | } |
244 | ||
b4c495f0 | 245 | static int gpio_mockup_to_irq(struct gpio_chip *gc, unsigned int offset) |
c7f5326f | 246 | { |
c7f5326f BG |
247 | struct gpio_mockup_chip *chip = gpiochip_get_data(gc); |
248 | ||
337cbeb2 | 249 | return irq_create_mapping(chip->irq_sim_domain, offset); |
e2ff7408 BG |
250 | } |
251 | ||
2a9e2740 BG |
252 | static void gpio_mockup_free(struct gpio_chip *gc, unsigned int offset) |
253 | { | |
254 | struct gpio_mockup_chip *chip = gpiochip_get_data(gc); | |
255 | ||
256 | __gpio_mockup_set(chip, offset, chip->lines[offset].pull); | |
257 | } | |
258 | ||
259 | static ssize_t gpio_mockup_debugfs_read(struct file *file, | |
260 | char __user *usr_buf, | |
261 | size_t size, loff_t *ppos) | |
9202ba23 BG |
262 | { |
263 | struct gpio_mockup_dbgfs_private *priv; | |
264 | struct gpio_mockup_chip *chip; | |
265 | struct seq_file *sfile; | |
2a9e2740 | 266 | struct gpio_chip *gc; |
ce9fb53c | 267 | int val, cnt; |
2a9e2740 | 268 | char buf[3]; |
2583303d | 269 | |
2a9e2740 BG |
270 | if (*ppos != 0) |
271 | return 0; | |
272 | ||
273 | sfile = file->private_data; | |
274 | priv = sfile->private; | |
275 | chip = priv->chip; | |
276 | gc = &chip->gc; | |
277 | ||
278 | val = gpio_mockup_get(gc, priv->offset); | |
2583303d | 279 | cnt = snprintf(buf, sizeof(buf), "%d\n", val); |
2a9e2740 | 280 | |
ce9fb53c | 281 | return simple_read_from_buffer(usr_buf, size, ppos, buf, cnt); |
2a9e2740 BG |
282 | } |
283 | ||
284 | static ssize_t gpio_mockup_debugfs_write(struct file *file, | |
285 | const char __user *usr_buf, | |
286 | size_t size, loff_t *ppos) | |
287 | { | |
288 | struct gpio_mockup_dbgfs_private *priv; | |
64e7112e | 289 | int rv, val; |
2a9e2740 | 290 | struct seq_file *sfile; |
2a9e2740 BG |
291 | |
292 | if (*ppos != 0) | |
293 | return -EINVAL; | |
9202ba23 | 294 | |
6f9b3e77 BG |
295 | rv = kstrtoint_from_user(usr_buf, size, 0, &val); |
296 | if (rv) | |
297 | return rv; | |
298 | if (val != 0 && val != 1) | |
299 | return -EINVAL; | |
300 | ||
650b57b0 BG |
301 | sfile = file->private_data; |
302 | priv = sfile->private; | |
64e7112e KG |
303 | rv = gpio_mockup_apply_pull(priv->chip, priv->offset, val); |
304 | if (rv) | |
305 | return rv; | |
9202ba23 BG |
306 | |
307 | return size; | |
308 | } | |
309 | ||
2a9e2740 | 310 | static int gpio_mockup_debugfs_open(struct inode *inode, struct file *file) |
9202ba23 BG |
311 | { |
312 | return single_open(file, NULL, inode->i_private); | |
313 | } | |
314 | ||
2a9e2740 BG |
315 | /* |
316 | * Each mockup chip is represented by a directory named after the chip's device | |
317 | * name under /sys/kernel/debug/gpio-mockup/. Each line is represented by | |
318 | * a file using the line's offset as the name under the chip's directory. | |
319 | * | |
320 | * Reading from the line's file yields the current *value*, writing to the | |
321 | * line's file changes the current *pull*. Default pull for mockup lines is | |
322 | * down. | |
323 | * | |
324 | * Examples: | |
325 | * - when a line pulled down is requested in output mode and driven high, its | |
326 | * value will return to 0 once it's released | |
327 | * - when the line is requested in output mode and driven high, writing 0 to | |
328 | * the corresponding debugfs file will change the pull to down but the | |
329 | * reported value will still be 1 until the line is released | |
330 | * - line requested in input mode always reports the same value as its pull | |
331 | * configuration | |
332 | * - when the line is requested in input mode and monitored for events, writing | |
333 | * the same value to the debugfs file will be a noop, while writing the | |
334 | * opposite value will generate a dummy interrupt with an appropriate edge | |
335 | */ | |
336 | static const struct file_operations gpio_mockup_debugfs_ops = { | |
9202ba23 | 337 | .owner = THIS_MODULE, |
2a9e2740 BG |
338 | .open = gpio_mockup_debugfs_open, |
339 | .read = gpio_mockup_debugfs_read, | |
340 | .write = gpio_mockup_debugfs_write, | |
9202ba23 | 341 | .llseek = no_llseek, |
59929d3a | 342 | .release = single_release, |
9202ba23 BG |
343 | }; |
344 | ||
345 | static void gpio_mockup_debugfs_setup(struct device *dev, | |
346 | struct gpio_mockup_chip *chip) | |
347 | { | |
348 | struct gpio_mockup_dbgfs_private *priv; | |
9202ba23 | 349 | struct gpio_chip *gc; |
ca8792af | 350 | const char *devname; |
9202ba23 BG |
351 | char *name; |
352 | int i; | |
353 | ||
354 | gc = &chip->gc; | |
ca8792af | 355 | devname = dev_name(&gc->gpiodev->dev); |
9202ba23 | 356 | |
ca8792af | 357 | chip->dbg_dir = debugfs_create_dir(devname, gpio_mockup_dbg_dir); |
9202ba23 BG |
358 | |
359 | for (i = 0; i < gc->ngpio; i++) { | |
360 | name = devm_kasprintf(dev, GFP_KERNEL, "%d", i); | |
361 | if (!name) | |
f360dcd4 | 362 | return; |
9202ba23 BG |
363 | |
364 | priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); | |
365 | if (!priv) | |
f360dcd4 | 366 | return; |
9202ba23 BG |
367 | |
368 | priv->chip = chip; | |
369 | priv->offset = i; | |
e7d8fde4 | 370 | priv->desc = gpiochip_get_desc(gc, i); |
9202ba23 | 371 | |
f360dcd4 GKH |
372 | debugfs_create_file(name, 0200, chip->dbg_dir, priv, |
373 | &gpio_mockup_debugfs_ops); | |
9202ba23 | 374 | } |
9202ba23 BG |
375 | } |
376 | ||
303e6da9 WY |
377 | static void gpio_mockup_debugfs_cleanup(void *data) |
378 | { | |
379 | struct gpio_mockup_chip *chip = data; | |
380 | ||
381 | debugfs_remove_recursive(chip->dbg_dir); | |
382 | } | |
383 | ||
337cbeb2 BG |
384 | static void gpio_mockup_dispose_mappings(void *data) |
385 | { | |
386 | struct gpio_mockup_chip *chip = data; | |
387 | struct gpio_chip *gc = &chip->gc; | |
388 | int i, irq; | |
389 | ||
390 | for (i = 0; i < gc->ngpio; i++) { | |
391 | irq = irq_find_mapping(chip->irq_sim_domain, i); | |
392 | if (irq) | |
393 | irq_dispose_mapping(irq); | |
394 | } | |
395 | } | |
396 | ||
6d974d32 | 397 | static int gpio_mockup_probe(struct platform_device *pdev) |
0f98dd1b | 398 | { |
6d974d32 BG |
399 | struct gpio_mockup_chip *chip; |
400 | struct gpio_chip *gc; | |
6d974d32 | 401 | struct device *dev; |
3ea47b44 | 402 | const char *name; |
bc7bc688 | 403 | int rv, base, i; |
3ea47b44 | 404 | u16 ngpio; |
6d974d32 BG |
405 | |
406 | dev = &pdev->dev; | |
3ea47b44 BG |
407 | |
408 | rv = device_property_read_u32(dev, "gpio-base", &base); | |
409 | if (rv) | |
410 | base = -1; | |
411 | ||
412 | rv = device_property_read_u16(dev, "nr-gpios", &ngpio); | |
413 | if (rv) | |
414 | return rv; | |
415 | ||
148c2560 | 416 | rv = device_property_read_string(dev, "chip-label", &name); |
3ea47b44 | 417 | if (rv) |
148c2560 | 418 | name = dev_name(dev); |
6d974d32 BG |
419 | |
420 | chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL); | |
421 | if (!chip) | |
422 | return -ENOMEM; | |
423 | ||
9212492f BG |
424 | mutex_init(&chip->lock); |
425 | ||
6d974d32 | 426 | gc = &chip->gc; |
ca409160 BG |
427 | gc->base = base; |
428 | gc->ngpio = ngpio; | |
429 | gc->label = name; | |
430 | gc->owner = THIS_MODULE; | |
431 | gc->parent = dev; | |
432 | gc->get = gpio_mockup_get; | |
433 | gc->set = gpio_mockup_set; | |
cbf1e092 | 434 | gc->get_multiple = gpio_mockup_get_multiple; |
c47bee95 | 435 | gc->set_multiple = gpio_mockup_set_multiple; |
ca409160 BG |
436 | gc->direction_output = gpio_mockup_dirout; |
437 | gc->direction_input = gpio_mockup_dirin; | |
438 | gc->get_direction = gpio_mockup_get_direction; | |
64e7112e | 439 | gc->set_config = gpio_mockup_set_config; |
e2ff7408 | 440 | gc->to_irq = gpio_mockup_to_irq; |
2a9e2740 | 441 | gc->free = gpio_mockup_free; |
ca409160 | 442 | |
f6ac438e BG |
443 | chip->lines = devm_kcalloc(dev, gc->ngpio, |
444 | sizeof(*chip->lines), GFP_KERNEL); | |
e4ba07bf BG |
445 | if (!chip->lines) |
446 | return -ENOMEM; | |
ca409160 | 447 | |
bc7bc688 KG |
448 | for (i = 0; i < gc->ngpio; i++) |
449 | chip->lines[i].dir = GPIO_LINE_DIRECTION_IN; | |
450 | ||
337cbeb2 BG |
451 | chip->irq_sim_domain = devm_irq_domain_create_sim(dev, NULL, |
452 | gc->ngpio); | |
453 | if (IS_ERR(chip->irq_sim_domain)) | |
454 | return PTR_ERR(chip->irq_sim_domain); | |
455 | ||
456 | rv = devm_add_action_or_reset(dev, gpio_mockup_dispose_mappings, chip); | |
457 | if (rv) | |
6d974d32 | 458 | return rv; |
e2ff7408 | 459 | |
6d974d32 BG |
460 | rv = devm_gpiochip_add_data(dev, &chip->gc, chip); |
461 | if (rv) | |
462 | return rv; | |
9202ba23 | 463 | |
f360dcd4 | 464 | gpio_mockup_debugfs_setup(dev, chip); |
9202ba23 | 465 | |
303e6da9 | 466 | return devm_add_action_or_reset(dev, gpio_mockup_debugfs_cleanup, chip); |
0f98dd1b BJZ |
467 | } |
468 | ||
43ddebdd VW |
469 | static const struct of_device_id gpio_mockup_of_match[] = { |
470 | { .compatible = "gpio-mockup", }, | |
471 | {}, | |
472 | }; | |
473 | MODULE_DEVICE_TABLE(of, gpio_mockup_of_match); | |
474 | ||
ca409160 | 475 | static struct platform_driver gpio_mockup_driver = { |
0f98dd1b | 476 | .driver = { |
25f00066 | 477 | .name = "gpio-mockup", |
43ddebdd | 478 | .of_match_table = gpio_mockup_of_match, |
364ac456 | 479 | }, |
ca409160 | 480 | .probe = gpio_mockup_probe, |
0f98dd1b BJZ |
481 | }; |
482 | ||
8a39f597 BG |
483 | static struct platform_device *gpio_mockup_pdevs[GPIO_MOCKUP_MAX_GC]; |
484 | ||
485 | static void gpio_mockup_unregister_pdevs(void) | |
486 | { | |
6fda593f AS |
487 | struct platform_device *pdev; |
488 | struct fwnode_handle *fwnode; | |
8a39f597 BG |
489 | int i; |
490 | ||
6fda593f AS |
491 | for (i = 0; i < GPIO_MOCKUP_MAX_GC; i++) { |
492 | pdev = gpio_mockup_pdevs[i]; | |
493 | if (!pdev) | |
494 | continue; | |
495 | ||
496 | fwnode = dev_fwnode(&pdev->dev); | |
497 | platform_device_unregister(pdev); | |
498 | fwnode_remove_software_node(fwnode); | |
499 | } | |
8a39f597 | 500 | } |
f3b47170 | 501 | |
42e9acc6 | 502 | static int __init gpio_mockup_register_chip(int idx) |
0f98dd1b | 503 | { |
3ea47b44 | 504 | struct property_entry properties[GPIO_MOCKUP_MAX_PROP]; |
3ea47b44 | 505 | struct platform_device_info pdevinfo; |
8a39f597 | 506 | struct platform_device *pdev; |
6fda593f | 507 | struct fwnode_handle *fwnode; |
42e9acc6 | 508 | char **line_names = NULL; |
148c2560 | 509 | char chip_label[32]; |
42e9acc6 | 510 | int prop = 0, base; |
3ea47b44 | 511 | u16 ngpio; |
0f98dd1b | 512 | |
42e9acc6 BG |
513 | memset(properties, 0, sizeof(properties)); |
514 | memset(&pdevinfo, 0, sizeof(pdevinfo)); | |
515 | ||
516 | snprintf(chip_label, sizeof(chip_label), "gpio-mockup-%c", idx + 'A'); | |
517 | properties[prop++] = PROPERTY_ENTRY_STRING("chip-label", chip_label); | |
518 | ||
519 | base = gpio_mockup_range_base(idx); | |
520 | if (base >= 0) | |
521 | properties[prop++] = PROPERTY_ENTRY_U32("gpio-base", base); | |
522 | ||
523 | ngpio = base < 0 ? gpio_mockup_range_ngpio(idx) | |
524 | : gpio_mockup_range_ngpio(idx) - base; | |
525 | properties[prop++] = PROPERTY_ENTRY_U16("nr-gpios", ngpio); | |
526 | ||
527 | if (gpio_mockup_named_lines) { | |
f7c151d8 | 528 | line_names = kasprintf_strarray(GFP_KERNEL, chip_label, ngpio); |
42e9acc6 BG |
529 | if (!line_names) |
530 | return -ENOMEM; | |
531 | ||
532 | properties[prop++] = PROPERTY_ENTRY_STRING_ARRAY_LEN( | |
533 | "gpio-line-names", line_names, ngpio); | |
534 | } | |
535 | ||
6fda593f | 536 | fwnode = fwnode_create_software_node(properties, NULL); |
02743c40 AS |
537 | if (IS_ERR(fwnode)) { |
538 | kfree_strarray(line_names, ngpio); | |
6fda593f | 539 | return PTR_ERR(fwnode); |
02743c40 | 540 | } |
6fda593f | 541 | |
42e9acc6 BG |
542 | pdevinfo.name = "gpio-mockup"; |
543 | pdevinfo.id = idx; | |
6fda593f | 544 | pdevinfo.fwnode = fwnode; |
42e9acc6 BG |
545 | |
546 | pdev = platform_device_register_full(&pdevinfo); | |
547 | kfree_strarray(line_names, ngpio); | |
548 | if (IS_ERR(pdev)) { | |
6fda593f | 549 | fwnode_remove_software_node(fwnode); |
42e9acc6 BG |
550 | pr_err("error registering device"); |
551 | return PTR_ERR(pdev); | |
552 | } | |
553 | ||
554 | gpio_mockup_pdevs[idx] = pdev; | |
555 | ||
556 | return 0; | |
557 | } | |
558 | ||
559 | static int __init gpio_mockup_init(void) | |
560 | { | |
561 | int i, num_chips, err; | |
562 | ||
43ddebdd | 563 | if ((gpio_mockup_num_ranges % 2) || |
e63a006f | 564 | (gpio_mockup_num_ranges > GPIO_MOCKUP_MAX_RANGES)) |
20c35ac4 BG |
565 | return -EINVAL; |
566 | ||
8a39f597 | 567 | /* Each chip is described by two values. */ |
e63a006f | 568 | num_chips = gpio_mockup_num_ranges / 2; |
8a39f597 | 569 | |
fa86963a BG |
570 | /* |
571 | * The second value in the <base GPIO - number of GPIOS> pair must | |
572 | * always be greater than 0. | |
573 | */ | |
574 | for (i = 0; i < num_chips; i++) { | |
cd9835f1 | 575 | if (gpio_mockup_range_ngpio(i) < 0) |
fa86963a BG |
576 | return -EINVAL; |
577 | } | |
578 | ||
2a9e2740 | 579 | gpio_mockup_dbg_dir = debugfs_create_dir("gpio-mockup", NULL); |
9202ba23 | 580 | |
8a39f597 | 581 | err = platform_driver_register(&gpio_mockup_driver); |
0f98dd1b | 582 | if (err) { |
56f6cb35 | 583 | pr_err("error registering platform driver\n"); |
e0ab949f | 584 | debugfs_remove_recursive(gpio_mockup_dbg_dir); |
0f98dd1b BJZ |
585 | return err; |
586 | } | |
587 | ||
8a39f597 | 588 | for (i = 0; i < num_chips; i++) { |
42e9acc6 BG |
589 | err = gpio_mockup_register_chip(i); |
590 | if (err) { | |
8a39f597 BG |
591 | platform_driver_unregister(&gpio_mockup_driver); |
592 | gpio_mockup_unregister_pdevs(); | |
e0ab949f | 593 | debugfs_remove_recursive(gpio_mockup_dbg_dir); |
42e9acc6 | 594 | return err; |
8a39f597 | 595 | } |
0f98dd1b BJZ |
596 | } |
597 | ||
598 | return 0; | |
599 | } | |
600 | ||
f3b47170 | 601 | static void __exit gpio_mockup_exit(void) |
0f98dd1b | 602 | { |
b7df41a6 | 603 | gpio_mockup_unregister_pdevs(); |
9202ba23 | 604 | debugfs_remove_recursive(gpio_mockup_dbg_dir); |
ca409160 | 605 | platform_driver_unregister(&gpio_mockup_driver); |
0f98dd1b BJZ |
606 | } |
607 | ||
f3b47170 BG |
608 | module_init(gpio_mockup_init); |
609 | module_exit(gpio_mockup_exit); | |
0f98dd1b BJZ |
610 | |
611 | MODULE_AUTHOR("Kamlakant Patel <kamlakant.patel@broadcom.com>"); | |
4f9a4cd6 | 612 | MODULE_AUTHOR("Bamvor Jian Zhang <bamv2005@gmail.com>"); |
c60c7f4c | 613 | MODULE_AUTHOR("Bartosz Golaszewski <brgl@bgdev.pl>"); |
0f98dd1b BJZ |
614 | MODULE_DESCRIPTION("GPIO Testing driver"); |
615 | MODULE_LICENSE("GPL v2"); |