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