blob: 876027fdefc9543e4835839cbe2939a8671c8918 [file] [log] [blame]
Olivier Deprez157378f2022-04-04 15:47:50 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002/*
3 * GPIO Testing Device Driver
4 *
5 * Copyright (C) 2014 Kamlakant Patel <kamlakant.patel@broadcom.com>
6 * Copyright (C) 2015-2016 Bamvor Jian Zhang <bamv2005@gmail.com>
7 * Copyright (C) 2017 Bartosz Golaszewski <brgl@bgdev.pl>
8 */
9
Olivier Deprez157378f2022-04-04 15:47:50 +020010#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11
12#include <linux/debugfs.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000013#include <linux/gpio/driver.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000014#include <linux/interrupt.h>
15#include <linux/irq.h>
16#include <linux/irq_sim.h>
Olivier Deprez157378f2022-04-04 15:47:50 +020017#include <linux/irqdomain.h>
18#include <linux/module.h>
19#include <linux/platform_device.h>
David Brazdil0f672f62019-12-10 10:32:29 +000020#include <linux/property.h>
Olivier Deprez157378f2022-04-04 15:47:50 +020021#include <linux/slab.h>
22#include <linux/string_helpers.h>
23#include <linux/uaccess.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000024
25#include "gpiolib.h"
26
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000027#define GPIO_MOCKUP_MAX_GC 10
28/*
29 * We're storing two values per chip: the GPIO base and the number
30 * of GPIO lines.
31 */
32#define GPIO_MOCKUP_MAX_RANGES (GPIO_MOCKUP_MAX_GC * 2)
Olivier Deprez157378f2022-04-04 15:47:50 +020033/* Maximum of four properties + the sentinel. */
34#define GPIO_MOCKUP_MAX_PROP 5
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000035
36/*
37 * struct gpio_pin_status - structure describing a GPIO status
Olivier Deprez157378f2022-04-04 15:47:50 +020038 * @dir: Configures direction of gpio as "in" or "out"
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000039 * @value: Configures status of the gpio as 0(low) or 1(high)
40 */
41struct gpio_mockup_line_status {
42 int dir;
43 int value;
David Brazdil0f672f62019-12-10 10:32:29 +000044 int pull;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000045};
46
47struct gpio_mockup_chip {
48 struct gpio_chip gc;
49 struct gpio_mockup_line_status *lines;
Olivier Deprez157378f2022-04-04 15:47:50 +020050 struct irq_domain *irq_sim_domain;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000051 struct dentry *dbg_dir;
David Brazdil0f672f62019-12-10 10:32:29 +000052 struct mutex lock;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000053};
54
55struct gpio_mockup_dbgfs_private {
56 struct gpio_mockup_chip *chip;
57 struct gpio_desc *desc;
David Brazdil0f672f62019-12-10 10:32:29 +000058 unsigned int offset;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000059};
60
61static int gpio_mockup_ranges[GPIO_MOCKUP_MAX_RANGES];
62static int gpio_mockup_num_ranges;
63module_param_array(gpio_mockup_ranges, int, &gpio_mockup_num_ranges, 0400);
64
65static bool gpio_mockup_named_lines;
66module_param_named(gpio_mockup_named_lines,
67 gpio_mockup_named_lines, bool, 0400);
68
69static struct dentry *gpio_mockup_dbg_dir;
70
71static int gpio_mockup_range_base(unsigned int index)
72{
73 return gpio_mockup_ranges[index * 2];
74}
75
76static int gpio_mockup_range_ngpio(unsigned int index)
77{
78 return gpio_mockup_ranges[index * 2 + 1];
79}
80
David Brazdil0f672f62019-12-10 10:32:29 +000081static int __gpio_mockup_get(struct gpio_mockup_chip *chip,
82 unsigned int offset)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000083{
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000084 return chip->lines[offset].value;
85}
86
David Brazdil0f672f62019-12-10 10:32:29 +000087static int gpio_mockup_get(struct gpio_chip *gc, unsigned int offset)
88{
89 struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
90 int val;
91
92 mutex_lock(&chip->lock);
93 val = __gpio_mockup_get(chip, offset);
94 mutex_unlock(&chip->lock);
95
96 return val;
97}
98
99static int gpio_mockup_get_multiple(struct gpio_chip *gc,
100 unsigned long *mask, unsigned long *bits)
101{
102 struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
103 unsigned int bit, val;
104
105 mutex_lock(&chip->lock);
106 for_each_set_bit(bit, mask, gc->ngpio) {
107 val = __gpio_mockup_get(chip, bit);
108 __assign_bit(bit, bits, val);
109 }
110 mutex_unlock(&chip->lock);
111
112 return 0;
113}
114
115static void __gpio_mockup_set(struct gpio_mockup_chip *chip,
116 unsigned int offset, int value)
117{
118 chip->lines[offset].value = !!value;
119}
120
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000121static void gpio_mockup_set(struct gpio_chip *gc,
David Brazdil0f672f62019-12-10 10:32:29 +0000122 unsigned int offset, int value)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000123{
124 struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
125
David Brazdil0f672f62019-12-10 10:32:29 +0000126 mutex_lock(&chip->lock);
127 __gpio_mockup_set(chip, offset, value);
128 mutex_unlock(&chip->lock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000129}
130
131static void gpio_mockup_set_multiple(struct gpio_chip *gc,
132 unsigned long *mask, unsigned long *bits)
133{
David Brazdil0f672f62019-12-10 10:32:29 +0000134 struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000135 unsigned int bit;
136
David Brazdil0f672f62019-12-10 10:32:29 +0000137 mutex_lock(&chip->lock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000138 for_each_set_bit(bit, mask, gc->ngpio)
David Brazdil0f672f62019-12-10 10:32:29 +0000139 __gpio_mockup_set(chip, bit, test_bit(bit, bits));
140 mutex_unlock(&chip->lock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000141}
142
Olivier Deprez157378f2022-04-04 15:47:50 +0200143static int gpio_mockup_apply_pull(struct gpio_mockup_chip *chip,
144 unsigned int offset, int value)
145{
146 int curr, irq, irq_type, ret = 0;
147 struct gpio_desc *desc;
148 struct gpio_chip *gc;
149
150 gc = &chip->gc;
151 desc = &gc->gpiodev->descs[offset];
152
153 mutex_lock(&chip->lock);
154
155 if (test_bit(FLAG_REQUESTED, &desc->flags) &&
156 !test_bit(FLAG_IS_OUT, &desc->flags)) {
157 curr = __gpio_mockup_get(chip, offset);
158 if (curr == value)
159 goto out;
160
161 irq = irq_find_mapping(chip->irq_sim_domain, offset);
162 if (!irq)
163 /*
164 * This is fine - it just means, nobody is listening
165 * for interrupts on this line, otherwise
166 * irq_create_mapping() would have been called from
167 * the to_irq() callback.
168 */
169 goto set_value;
170
171 irq_type = irq_get_trigger_type(irq);
172
173 if ((value == 1 && (irq_type & IRQ_TYPE_EDGE_RISING)) ||
174 (value == 0 && (irq_type & IRQ_TYPE_EDGE_FALLING))) {
175 ret = irq_set_irqchip_state(irq, IRQCHIP_STATE_PENDING,
176 true);
177 if (ret)
178 goto out;
179 }
180 }
181
182set_value:
183 /* Change the value unless we're actively driving the line. */
184 if (!test_bit(FLAG_REQUESTED, &desc->flags) ||
185 !test_bit(FLAG_IS_OUT, &desc->flags))
186 __gpio_mockup_set(chip, offset, value);
187
188out:
189 chip->lines[offset].pull = value;
190 mutex_unlock(&chip->lock);
191 return ret;
192}
193
194static int gpio_mockup_set_config(struct gpio_chip *gc,
195 unsigned int offset, unsigned long config)
196{
197 struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
198
199 switch (pinconf_to_config_param(config)) {
200 case PIN_CONFIG_BIAS_PULL_UP:
201 return gpio_mockup_apply_pull(chip, offset, 1);
202 case PIN_CONFIG_BIAS_PULL_DOWN:
203 return gpio_mockup_apply_pull(chip, offset, 0);
204 default:
205 break;
206 }
207 return -ENOTSUPP;
208}
209
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000210static int gpio_mockup_dirout(struct gpio_chip *gc,
211 unsigned int offset, int value)
212{
213 struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
214
David Brazdil0f672f62019-12-10 10:32:29 +0000215 mutex_lock(&chip->lock);
Olivier Deprez157378f2022-04-04 15:47:50 +0200216 chip->lines[offset].dir = GPIO_LINE_DIRECTION_OUT;
David Brazdil0f672f62019-12-10 10:32:29 +0000217 __gpio_mockup_set(chip, offset, value);
218 mutex_unlock(&chip->lock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000219
220 return 0;
221}
222
223static int gpio_mockup_dirin(struct gpio_chip *gc, unsigned int offset)
224{
225 struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
226
David Brazdil0f672f62019-12-10 10:32:29 +0000227 mutex_lock(&chip->lock);
Olivier Deprez157378f2022-04-04 15:47:50 +0200228 chip->lines[offset].dir = GPIO_LINE_DIRECTION_IN;
David Brazdil0f672f62019-12-10 10:32:29 +0000229 mutex_unlock(&chip->lock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000230
231 return 0;
232}
233
234static int gpio_mockup_get_direction(struct gpio_chip *gc, unsigned int offset)
235{
236 struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
David Brazdil0f672f62019-12-10 10:32:29 +0000237 int direction;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000238
David Brazdil0f672f62019-12-10 10:32:29 +0000239 mutex_lock(&chip->lock);
Olivier Deprez157378f2022-04-04 15:47:50 +0200240 direction = chip->lines[offset].dir;
David Brazdil0f672f62019-12-10 10:32:29 +0000241 mutex_unlock(&chip->lock);
242
243 return direction;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000244}
245
246static int gpio_mockup_to_irq(struct gpio_chip *gc, unsigned int offset)
247{
248 struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
249
Olivier Deprez157378f2022-04-04 15:47:50 +0200250 return irq_create_mapping(chip->irq_sim_domain, offset);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000251}
252
David Brazdil0f672f62019-12-10 10:32:29 +0000253static void gpio_mockup_free(struct gpio_chip *gc, unsigned int offset)
254{
255 struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
256
257 __gpio_mockup_set(chip, offset, chip->lines[offset].pull);
258}
259
260static ssize_t gpio_mockup_debugfs_read(struct file *file,
261 char __user *usr_buf,
262 size_t size, loff_t *ppos)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000263{
264 struct gpio_mockup_dbgfs_private *priv;
265 struct gpio_mockup_chip *chip;
266 struct seq_file *sfile;
David Brazdil0f672f62019-12-10 10:32:29 +0000267 struct gpio_chip *gc;
268 int val, cnt;
269 char buf[3];
270
271 if (*ppos != 0)
272 return 0;
273
274 sfile = file->private_data;
275 priv = sfile->private;
276 chip = priv->chip;
277 gc = &chip->gc;
278
279 val = gpio_mockup_get(gc, priv->offset);
280 cnt = snprintf(buf, sizeof(buf), "%d\n", val);
281
282 return simple_read_from_buffer(usr_buf, size, ppos, buf, cnt);
283}
284
285static ssize_t gpio_mockup_debugfs_write(struct file *file,
286 const char __user *usr_buf,
287 size_t size, loff_t *ppos)
288{
289 struct gpio_mockup_dbgfs_private *priv;
Olivier Deprez157378f2022-04-04 15:47:50 +0200290 int rv, val;
David Brazdil0f672f62019-12-10 10:32:29 +0000291 struct seq_file *sfile;
David Brazdil0f672f62019-12-10 10:32:29 +0000292
293 if (*ppos != 0)
294 return -EINVAL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000295
296 rv = kstrtoint_from_user(usr_buf, size, 0, &val);
297 if (rv)
298 return rv;
299 if (val != 0 && val != 1)
300 return -EINVAL;
301
302 sfile = file->private_data;
303 priv = sfile->private;
Olivier Deprez157378f2022-04-04 15:47:50 +0200304 rv = gpio_mockup_apply_pull(priv->chip, priv->offset, val);
305 if (rv)
306 return rv;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000307
308 return size;
309}
310
David Brazdil0f672f62019-12-10 10:32:29 +0000311static int gpio_mockup_debugfs_open(struct inode *inode, struct file *file)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000312{
313 return single_open(file, NULL, inode->i_private);
314}
315
David Brazdil0f672f62019-12-10 10:32:29 +0000316/*
317 * Each mockup chip is represented by a directory named after the chip's device
318 * name under /sys/kernel/debug/gpio-mockup/. Each line is represented by
319 * a file using the line's offset as the name under the chip's directory.
320 *
321 * Reading from the line's file yields the current *value*, writing to the
322 * line's file changes the current *pull*. Default pull for mockup lines is
323 * down.
324 *
325 * Examples:
326 * - when a line pulled down is requested in output mode and driven high, its
327 * value will return to 0 once it's released
328 * - when the line is requested in output mode and driven high, writing 0 to
329 * the corresponding debugfs file will change the pull to down but the
330 * reported value will still be 1 until the line is released
331 * - line requested in input mode always reports the same value as its pull
332 * configuration
333 * - when the line is requested in input mode and monitored for events, writing
334 * the same value to the debugfs file will be a noop, while writing the
335 * opposite value will generate a dummy interrupt with an appropriate edge
336 */
337static const struct file_operations gpio_mockup_debugfs_ops = {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000338 .owner = THIS_MODULE,
David Brazdil0f672f62019-12-10 10:32:29 +0000339 .open = gpio_mockup_debugfs_open,
340 .read = gpio_mockup_debugfs_read,
341 .write = gpio_mockup_debugfs_write,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000342 .llseek = no_llseek,
David Brazdil0f672f62019-12-10 10:32:29 +0000343 .release = single_release,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000344};
345
346static void gpio_mockup_debugfs_setup(struct device *dev,
347 struct gpio_mockup_chip *chip)
348{
349 struct gpio_mockup_dbgfs_private *priv;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000350 struct gpio_chip *gc;
351 const char *devname;
352 char *name;
353 int i;
354
355 gc = &chip->gc;
356 devname = dev_name(&gc->gpiodev->dev);
357
358 chip->dbg_dir = debugfs_create_dir(devname, gpio_mockup_dbg_dir);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000359
360 for (i = 0; i < gc->ngpio; i++) {
361 name = devm_kasprintf(dev, GFP_KERNEL, "%d", i);
362 if (!name)
David Brazdil0f672f62019-12-10 10:32:29 +0000363 return;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000364
365 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
366 if (!priv)
David Brazdil0f672f62019-12-10 10:32:29 +0000367 return;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000368
369 priv->chip = chip;
370 priv->offset = i;
371 priv->desc = &gc->gpiodev->descs[i];
372
David Brazdil0f672f62019-12-10 10:32:29 +0000373 debugfs_create_file(name, 0200, chip->dbg_dir, priv,
374 &gpio_mockup_debugfs_ops);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000375 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000376}
377
Olivier Deprez92d4c212022-12-06 15:05:30 +0100378static void gpio_mockup_debugfs_cleanup(void *data)
379{
380 struct gpio_mockup_chip *chip = data;
381
382 debugfs_remove_recursive(chip->dbg_dir);
383}
384
Olivier Deprez157378f2022-04-04 15:47:50 +0200385static void gpio_mockup_dispose_mappings(void *data)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000386{
Olivier Deprez157378f2022-04-04 15:47:50 +0200387 struct gpio_mockup_chip *chip = data;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000388 struct gpio_chip *gc = &chip->gc;
Olivier Deprez157378f2022-04-04 15:47:50 +0200389 int i, irq;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000390
391 for (i = 0; i < gc->ngpio; i++) {
Olivier Deprez157378f2022-04-04 15:47:50 +0200392 irq = irq_find_mapping(chip->irq_sim_domain, i);
393 if (irq)
394 irq_dispose_mapping(irq);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000395 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000396}
397
398static int gpio_mockup_probe(struct platform_device *pdev)
399{
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000400 struct gpio_mockup_chip *chip;
401 struct gpio_chip *gc;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000402 struct device *dev;
David Brazdil0f672f62019-12-10 10:32:29 +0000403 const char *name;
Olivier Deprez157378f2022-04-04 15:47:50 +0200404 int rv, base, i;
David Brazdil0f672f62019-12-10 10:32:29 +0000405 u16 ngpio;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000406
407 dev = &pdev->dev;
David Brazdil0f672f62019-12-10 10:32:29 +0000408
409 rv = device_property_read_u32(dev, "gpio-base", &base);
410 if (rv)
411 base = -1;
412
413 rv = device_property_read_u16(dev, "nr-gpios", &ngpio);
414 if (rv)
415 return rv;
416
Olivier Deprez157378f2022-04-04 15:47:50 +0200417 rv = device_property_read_string(dev, "chip-label", &name);
David Brazdil0f672f62019-12-10 10:32:29 +0000418 if (rv)
Olivier Deprez157378f2022-04-04 15:47:50 +0200419 name = dev_name(dev);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000420
421 chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL);
422 if (!chip)
423 return -ENOMEM;
424
David Brazdil0f672f62019-12-10 10:32:29 +0000425 mutex_init(&chip->lock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000426
427 gc = &chip->gc;
428 gc->base = base;
429 gc->ngpio = ngpio;
430 gc->label = name;
431 gc->owner = THIS_MODULE;
432 gc->parent = dev;
433 gc->get = gpio_mockup_get;
434 gc->set = gpio_mockup_set;
David Brazdil0f672f62019-12-10 10:32:29 +0000435 gc->get_multiple = gpio_mockup_get_multiple;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000436 gc->set_multiple = gpio_mockup_set_multiple;
437 gc->direction_output = gpio_mockup_dirout;
438 gc->direction_input = gpio_mockup_dirin;
439 gc->get_direction = gpio_mockup_get_direction;
Olivier Deprez157378f2022-04-04 15:47:50 +0200440 gc->set_config = gpio_mockup_set_config;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000441 gc->to_irq = gpio_mockup_to_irq;
David Brazdil0f672f62019-12-10 10:32:29 +0000442 gc->free = gpio_mockup_free;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000443
444 chip->lines = devm_kcalloc(dev, gc->ngpio,
445 sizeof(*chip->lines), GFP_KERNEL);
446 if (!chip->lines)
447 return -ENOMEM;
448
Olivier Deprez157378f2022-04-04 15:47:50 +0200449 for (i = 0; i < gc->ngpio; i++)
450 chip->lines[i].dir = GPIO_LINE_DIRECTION_IN;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000451
Olivier Deprez157378f2022-04-04 15:47:50 +0200452 chip->irq_sim_domain = devm_irq_domain_create_sim(dev, NULL,
453 gc->ngpio);
454 if (IS_ERR(chip->irq_sim_domain))
455 return PTR_ERR(chip->irq_sim_domain);
456
457 rv = devm_add_action_or_reset(dev, gpio_mockup_dispose_mappings, chip);
458 if (rv)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000459 return rv;
460
461 rv = devm_gpiochip_add_data(dev, &chip->gc, chip);
462 if (rv)
463 return rv;
464
David Brazdil0f672f62019-12-10 10:32:29 +0000465 gpio_mockup_debugfs_setup(dev, chip);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000466
Olivier Deprez92d4c212022-12-06 15:05:30 +0100467 return devm_add_action_or_reset(dev, gpio_mockup_debugfs_cleanup, chip);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000468}
469
470static struct platform_driver gpio_mockup_driver = {
471 .driver = {
Olivier Deprez157378f2022-04-04 15:47:50 +0200472 .name = "gpio-mockup",
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000473 },
474 .probe = gpio_mockup_probe,
475};
476
477static struct platform_device *gpio_mockup_pdevs[GPIO_MOCKUP_MAX_GC];
478
479static void gpio_mockup_unregister_pdevs(void)
480{
481 struct platform_device *pdev;
482 int i;
483
484 for (i = 0; i < GPIO_MOCKUP_MAX_GC; i++) {
485 pdev = gpio_mockup_pdevs[i];
486
487 if (pdev)
488 platform_device_unregister(pdev);
489 }
490}
491
Olivier Deprez157378f2022-04-04 15:47:50 +0200492static __init char **gpio_mockup_make_line_names(const char *label,
493 unsigned int num_lines)
494{
495 unsigned int i;
496 char **names;
497
498 names = kcalloc(num_lines + 1, sizeof(char *), GFP_KERNEL);
499 if (!names)
500 return NULL;
501
502 for (i = 0; i < num_lines; i++) {
503 names[i] = kasprintf(GFP_KERNEL, "%s-%u", label, i);
504 if (!names[i]) {
505 kfree_strarray(names, i);
506 return NULL;
507 }
508 }
509
510 return names;
511}
512
513static int __init gpio_mockup_register_chip(int idx)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000514{
David Brazdil0f672f62019-12-10 10:32:29 +0000515 struct property_entry properties[GPIO_MOCKUP_MAX_PROP];
David Brazdil0f672f62019-12-10 10:32:29 +0000516 struct platform_device_info pdevinfo;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000517 struct platform_device *pdev;
Olivier Deprez157378f2022-04-04 15:47:50 +0200518 char **line_names = NULL;
519 char chip_label[32];
520 int prop = 0, base;
David Brazdil0f672f62019-12-10 10:32:29 +0000521 u16 ngpio;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000522
Olivier Deprez157378f2022-04-04 15:47:50 +0200523 memset(properties, 0, sizeof(properties));
524 memset(&pdevinfo, 0, sizeof(pdevinfo));
525
526 snprintf(chip_label, sizeof(chip_label), "gpio-mockup-%c", idx + 'A');
527 properties[prop++] = PROPERTY_ENTRY_STRING("chip-label", chip_label);
528
529 base = gpio_mockup_range_base(idx);
530 if (base >= 0)
531 properties[prop++] = PROPERTY_ENTRY_U32("gpio-base", base);
532
533 ngpio = base < 0 ? gpio_mockup_range_ngpio(idx)
534 : gpio_mockup_range_ngpio(idx) - base;
535 properties[prop++] = PROPERTY_ENTRY_U16("nr-gpios", ngpio);
536
537 if (gpio_mockup_named_lines) {
538 line_names = gpio_mockup_make_line_names(chip_label, ngpio);
539 if (!line_names)
540 return -ENOMEM;
541
542 properties[prop++] = PROPERTY_ENTRY_STRING_ARRAY_LEN(
543 "gpio-line-names", line_names, ngpio);
544 }
545
546 pdevinfo.name = "gpio-mockup";
547 pdevinfo.id = idx;
548 pdevinfo.properties = properties;
549
550 pdev = platform_device_register_full(&pdevinfo);
551 kfree_strarray(line_names, ngpio);
552 if (IS_ERR(pdev)) {
553 pr_err("error registering device");
554 return PTR_ERR(pdev);
555 }
556
557 gpio_mockup_pdevs[idx] = pdev;
558
559 return 0;
560}
561
562static int __init gpio_mockup_init(void)
563{
564 int i, num_chips, err;
565
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000566 if ((gpio_mockup_num_ranges < 2) ||
567 (gpio_mockup_num_ranges % 2) ||
568 (gpio_mockup_num_ranges > GPIO_MOCKUP_MAX_RANGES))
569 return -EINVAL;
570
571 /* Each chip is described by two values. */
572 num_chips = gpio_mockup_num_ranges / 2;
573
574 /*
575 * The second value in the <base GPIO - number of GPIOS> pair must
576 * always be greater than 0.
577 */
578 for (i = 0; i < num_chips; i++) {
579 if (gpio_mockup_range_ngpio(i) < 0)
580 return -EINVAL;
581 }
582
David Brazdil0f672f62019-12-10 10:32:29 +0000583 gpio_mockup_dbg_dir = debugfs_create_dir("gpio-mockup", NULL);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000584
585 err = platform_driver_register(&gpio_mockup_driver);
586 if (err) {
Olivier Deprez157378f2022-04-04 15:47:50 +0200587 pr_err("error registering platform driver\n");
Olivier Deprez0e641232021-09-23 10:07:05 +0200588 debugfs_remove_recursive(gpio_mockup_dbg_dir);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000589 return err;
590 }
591
592 for (i = 0; i < num_chips; i++) {
Olivier Deprez157378f2022-04-04 15:47:50 +0200593 err = gpio_mockup_register_chip(i);
594 if (err) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000595 platform_driver_unregister(&gpio_mockup_driver);
596 gpio_mockup_unregister_pdevs();
Olivier Deprez0e641232021-09-23 10:07:05 +0200597 debugfs_remove_recursive(gpio_mockup_dbg_dir);
Olivier Deprez157378f2022-04-04 15:47:50 +0200598 return err;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000599 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000600 }
601
602 return 0;
603}
604
605static void __exit gpio_mockup_exit(void)
606{
Olivier Deprez92d4c212022-12-06 15:05:30 +0100607 gpio_mockup_unregister_pdevs();
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000608 debugfs_remove_recursive(gpio_mockup_dbg_dir);
609 platform_driver_unregister(&gpio_mockup_driver);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000610}
611
612module_init(gpio_mockup_init);
613module_exit(gpio_mockup_exit);
614
615MODULE_AUTHOR("Kamlakant Patel <kamlakant.patel@broadcom.com>");
616MODULE_AUTHOR("Bamvor Jian Zhang <bamv2005@gmail.com>");
617MODULE_AUTHOR("Bartosz Golaszewski <brgl@bgdev.pl>");
618MODULE_DESCRIPTION("GPIO Testing driver");
619MODULE_LICENSE("GPL v2");