1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
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
10#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11
12#include <linux/cleanup.h>
13#include <linux/debugfs.h>
14#include <linux/device.h>
15#include <linux/gpio/driver.h>
16#include <linux/interrupt.h>
17#include <linux/irq.h>
18#include <linux/irq_sim.h>
19#include <linux/irqdomain.h>
20#include <linux/mod_devicetable.h>
21#include <linux/module.h>
22#include <linux/platform_device.h>
23#include <linux/property.h>
24#include <linux/seq_file.h>
25#include <linux/slab.h>
26#include <linux/string_helpers.h>
27#include <linux/uaccess.h>
28
29#define GPIO_MOCKUP_MAX_GC 10
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)
35/* Maximum of four properties + the sentinel. */
36#define GPIO_MOCKUP_MAX_PROP 5
37
38/*
39 * struct gpio_pin_status - structure describing a GPIO status
40 * @dir: Configures direction of gpio as "in" or "out"
41 * @value: Configures status of the gpio as 0(low) or 1(high)
42 * @pull: Configures the current pull of the GPIO as 0 (pull-down) or
43 * 1 (pull-up)
44 * @requested: Request status of this GPIO
45 */
46struct gpio_mockup_line_status {
47 int dir;
48 int value;
49 int pull;
50 bool requested;
51};
52
53struct gpio_mockup_chip {
54 struct gpio_chip gc;
55 struct gpio_mockup_line_status *lines;
56 struct irq_domain *irq_sim_domain;
57 struct dentry *dbg_dir;
58 struct mutex lock;
59};
60
61struct gpio_mockup_dbgfs_private {
62 struct gpio_mockup_chip *chip;
63 unsigned int offset;
64};
65
66static int gpio_mockup_ranges[GPIO_MOCKUP_MAX_RANGES];
67static int gpio_mockup_num_ranges;
68module_param_array(gpio_mockup_ranges, int, &gpio_mockup_num_ranges, 0400);
69
70static bool gpio_mockup_named_lines;
71module_param_named(gpio_mockup_named_lines,
72 gpio_mockup_named_lines, bool, 0400);
73
74static struct dentry *gpio_mockup_dbg_dir;
75
76static int gpio_mockup_range_base(unsigned int index)
77{
78 return gpio_mockup_ranges[index * 2];
79}
80
81static int gpio_mockup_range_ngpio(unsigned int index)
82{
83 return gpio_mockup_ranges[index * 2 + 1];
84}
85
86static int __gpio_mockup_get(struct gpio_mockup_chip *chip,
87 unsigned int offset)
88{
89 return chip->lines[offset].value;
90}
91
92static 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
97 scoped_guard(mutex, &chip->lock)
98 val = __gpio_mockup_get(chip, offset);
99
100 return val;
101}
102
103static 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
109 scoped_guard(mutex, &chip->lock) {
110 for_each_set_bit(bit, mask, gc->ngpio) {
111 val = __gpio_mockup_get(chip, offset: bit);
112 __assign_bit(nr: bit, addr: bits, value: val);
113 }
114 }
115
116 return 0;
117}
118
119static void __gpio_mockup_set(struct gpio_mockup_chip *chip,
120 unsigned int offset, int value)
121{
122 chip->lines[offset].value = !!value;
123}
124
125static 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
130 guard(mutex)(T: &chip->lock);
131
132 __gpio_mockup_set(chip, offset, value);
133}
134
135static void gpio_mockup_set_multiple(struct gpio_chip *gc,
136 unsigned long *mask, unsigned long *bits)
137{
138 struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
139 unsigned int bit;
140
141 guard(mutex)(T: &chip->lock);
142
143 for_each_set_bit(bit, mask, gc->ngpio)
144 __gpio_mockup_set(chip, offset: bit, test_bit(bit, bits));
145}
146
147static int gpio_mockup_apply_pull(struct gpio_mockup_chip *chip,
148 unsigned int offset, int value)
149{
150 struct gpio_mockup_line_status *line = &chip->lines[offset];
151 int curr, irq, irq_type, ret = 0;
152
153 guard(mutex)(T: &chip->lock);
154
155 if (line->requested && line->dir == GPIO_LINE_DIRECTION_IN) {
156 curr = __gpio_mockup_get(chip, offset);
157 if (curr == value)
158 goto out;
159
160 irq = irq_find_mapping(domain: chip->irq_sim_domain, hwirq: 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
170 irq_type = irq_get_trigger_type(irq);
171
172 if ((value == 1 && (irq_type & IRQ_TYPE_EDGE_RISING)) ||
173 (value == 0 && (irq_type & IRQ_TYPE_EDGE_FALLING))) {
174 ret = irq_set_irqchip_state(irq, which: IRQCHIP_STATE_PENDING,
175 state: true);
176 if (ret)
177 goto out;
178 }
179 }
180
181set_value:
182 /* Change the value unless we're actively driving the line. */
183 if (!line->requested || line->dir == GPIO_LINE_DIRECTION_IN)
184 __gpio_mockup_set(chip, offset, value);
185
186out:
187 chip->lines[offset].pull = value;
188 return ret;
189}
190
191static 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, value: 1);
199 case PIN_CONFIG_BIAS_PULL_DOWN:
200 return gpio_mockup_apply_pull(chip, offset, value: 0);
201 default:
202 break;
203 }
204 return -ENOTSUPP;
205}
206
207static int gpio_mockup_dirout(struct gpio_chip *gc,
208 unsigned int offset, int value)
209{
210 struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
211
212 scoped_guard(mutex, &chip->lock) {
213 chip->lines[offset].dir = GPIO_LINE_DIRECTION_OUT;
214 __gpio_mockup_set(chip, offset, value);
215 }
216
217 return 0;
218}
219
220static int gpio_mockup_dirin(struct gpio_chip *gc, unsigned int offset)
221{
222 struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
223
224 scoped_guard(mutex, &chip->lock)
225 chip->lines[offset].dir = GPIO_LINE_DIRECTION_IN;
226
227 return 0;
228}
229
230static int gpio_mockup_get_direction(struct gpio_chip *gc, unsigned int offset)
231{
232 struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
233 int direction;
234
235 scoped_guard(mutex, &chip->lock)
236 direction = chip->lines[offset].dir;
237
238 return direction;
239}
240
241static int gpio_mockup_to_irq(struct gpio_chip *gc, unsigned int offset)
242{
243 struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
244
245 return irq_create_mapping(host: chip->irq_sim_domain, hwirq: offset);
246}
247
248static 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
258static void gpio_mockup_free(struct gpio_chip *gc, unsigned int offset)
259{
260 struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
261
262 guard(mutex)(T: &chip->lock);
263
264 chip->lines[offset].requested = false;
265 __gpio_mockup_set(chip, offset, value: chip->lines[offset].pull);
266}
267
268static ssize_t gpio_mockup_debugfs_read(struct file *file,
269 char __user *usr_buf,
270 size_t size, loff_t *ppos)
271{
272 struct gpio_mockup_dbgfs_private *priv;
273 struct gpio_mockup_chip *chip;
274 struct seq_file *sfile;
275 struct gpio_chip *gc;
276 int val, cnt;
277 char buf[3];
278
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, offset: priv->offset);
288 cnt = snprintf(buf, size: sizeof(buf), fmt: "%d\n", val);
289
290 return simple_read_from_buffer(to: usr_buf, count: size, ppos, from: buf, available: cnt);
291}
292
293static 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;
298 int rv, val;
299 struct seq_file *sfile;
300
301 if (*ppos != 0)
302 return -EINVAL;
303
304 rv = kstrtoint_from_user(s: usr_buf, count: size, base: 0, res: &val);
305 if (rv)
306 return rv;
307 if (val != 0 && val != 1)
308 return -EINVAL;
309
310 sfile = file->private_data;
311 priv = sfile->private;
312 rv = gpio_mockup_apply_pull(chip: priv->chip, offset: priv->offset, value: val);
313 if (rv)
314 return rv;
315
316 return size;
317}
318
319static int gpio_mockup_debugfs_open(struct inode *inode, struct file *file)
320{
321 return single_open(file, NULL, inode->i_private);
322}
323
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 */
345static const struct file_operations gpio_mockup_debugfs_ops = {
346 .owner = THIS_MODULE,
347 .open = gpio_mockup_debugfs_open,
348 .read = gpio_mockup_debugfs_read,
349 .write = gpio_mockup_debugfs_write,
350 .llseek = no_llseek,
351 .release = single_release,
352};
353
354static void gpio_mockup_debugfs_setup(struct device *dev,
355 struct gpio_mockup_chip *chip)
356{
357 struct device *child __free(put_device) = NULL;
358 struct gpio_mockup_dbgfs_private *priv;
359 struct gpio_chip *gc;
360 const char *devname;
361 char *name;
362 int i;
363
364 gc = &chip->gc;
365
366 /*
367 * There can only be a single GPIO device per platform device in
368 * gpio-mockup so using device_find_any_child() is OK.
369 */
370 child = device_find_any_child(parent: dev);
371 if (!child)
372 return;
373
374 devname = dev_name(dev: child);
375 chip->dbg_dir = debugfs_create_dir(name: devname, parent: gpio_mockup_dbg_dir);
376
377 for (i = 0; i < gc->ngpio; i++) {
378 name = devm_kasprintf(dev, GFP_KERNEL, fmt: "%d", i);
379 if (!name)
380 return;
381
382 priv = devm_kzalloc(dev, size: sizeof(*priv), GFP_KERNEL);
383 if (!priv)
384 return;
385
386 priv->chip = chip;
387 priv->offset = i;
388
389 debugfs_create_file(name, mode: 0600, parent: chip->dbg_dir, data: priv,
390 fops: &gpio_mockup_debugfs_ops);
391 }
392}
393
394static void gpio_mockup_debugfs_cleanup(void *data)
395{
396 struct gpio_mockup_chip *chip = data;
397
398 debugfs_remove_recursive(dentry: chip->dbg_dir);
399}
400
401static void gpio_mockup_dispose_mappings(void *data)
402{
403 struct gpio_mockup_chip *chip = data;
404 struct gpio_chip *gc = &chip->gc;
405 int i, irq;
406
407 for (i = 0; i < gc->ngpio; i++) {
408 irq = irq_find_mapping(domain: chip->irq_sim_domain, hwirq: i);
409 if (irq)
410 irq_dispose_mapping(virq: irq);
411 }
412}
413
414static int gpio_mockup_probe(struct platform_device *pdev)
415{
416 struct gpio_mockup_chip *chip;
417 struct gpio_chip *gc;
418 struct device *dev;
419 const char *name;
420 int rv, base, i;
421 u16 ngpio;
422
423 dev = &pdev->dev;
424
425 rv = device_property_read_u32(dev, propname: "gpio-base", val: &base);
426 if (rv)
427 base = -1;
428
429 rv = device_property_read_u16(dev, propname: "nr-gpios", val: &ngpio);
430 if (rv)
431 return rv;
432
433 rv = device_property_read_string(dev, propname: "chip-label", val: &name);
434 if (rv)
435 name = dev_name(dev);
436
437 chip = devm_kzalloc(dev, size: sizeof(*chip), GFP_KERNEL);
438 if (!chip)
439 return -ENOMEM;
440
441 mutex_init(&chip->lock);
442
443 gc = &chip->gc;
444 gc->base = base;
445 gc->ngpio = ngpio;
446 gc->label = name;
447 gc->owner = THIS_MODULE;
448 gc->parent = dev;
449 gc->get = gpio_mockup_get;
450 gc->set = gpio_mockup_set;
451 gc->get_multiple = gpio_mockup_get_multiple;
452 gc->set_multiple = gpio_mockup_set_multiple;
453 gc->direction_output = gpio_mockup_dirout;
454 gc->direction_input = gpio_mockup_dirin;
455 gc->get_direction = gpio_mockup_get_direction;
456 gc->set_config = gpio_mockup_set_config;
457 gc->to_irq = gpio_mockup_to_irq;
458 gc->request = gpio_mockup_request;
459 gc->free = gpio_mockup_free;
460
461 chip->lines = devm_kcalloc(dev, n: gc->ngpio,
462 size: sizeof(*chip->lines), GFP_KERNEL);
463 if (!chip->lines)
464 return -ENOMEM;
465
466 for (i = 0; i < gc->ngpio; i++)
467 chip->lines[i].dir = GPIO_LINE_DIRECTION_IN;
468
469 chip->irq_sim_domain = devm_irq_domain_create_sim(dev, NULL,
470 num_irqs: gc->ngpio);
471 if (IS_ERR(ptr: chip->irq_sim_domain))
472 return PTR_ERR(ptr: chip->irq_sim_domain);
473
474 rv = devm_add_action_or_reset(dev, gpio_mockup_dispose_mappings, chip);
475 if (rv)
476 return rv;
477
478 rv = devm_gpiochip_add_data(dev, &chip->gc, chip);
479 if (rv)
480 return rv;
481
482 gpio_mockup_debugfs_setup(dev, chip);
483
484 return devm_add_action_or_reset(dev, gpio_mockup_debugfs_cleanup, chip);
485}
486
487static const struct of_device_id gpio_mockup_of_match[] = {
488 { .compatible = "gpio-mockup", },
489 {},
490};
491MODULE_DEVICE_TABLE(of, gpio_mockup_of_match);
492
493static struct platform_driver gpio_mockup_driver = {
494 .driver = {
495 .name = "gpio-mockup",
496 .of_match_table = gpio_mockup_of_match,
497 },
498 .probe = gpio_mockup_probe,
499};
500
501static struct platform_device *gpio_mockup_pdevs[GPIO_MOCKUP_MAX_GC];
502
503static void gpio_mockup_unregister_pdevs(void)
504{
505 struct platform_device *pdev;
506 struct fwnode_handle *fwnode;
507 int i;
508
509 for (i = 0; i < GPIO_MOCKUP_MAX_GC; i++) {
510 pdev = gpio_mockup_pdevs[i];
511 if (!pdev)
512 continue;
513
514 fwnode = dev_fwnode(&pdev->dev);
515 platform_device_unregister(pdev);
516 fwnode_remove_software_node(fwnode);
517 }
518}
519
520static int __init gpio_mockup_register_chip(int idx)
521{
522 struct property_entry properties[GPIO_MOCKUP_MAX_PROP];
523 struct platform_device_info pdevinfo;
524 struct platform_device *pdev;
525 struct fwnode_handle *fwnode;
526 char **line_names = NULL;
527 char chip_label[32];
528 int prop = 0, base;
529 u16 ngpio;
530
531 memset(properties, 0, sizeof(properties));
532 memset(&pdevinfo, 0, sizeof(pdevinfo));
533
534 snprintf(buf: chip_label, size: sizeof(chip_label), fmt: "gpio-mockup-%c", idx + 'A');
535 properties[prop++] = PROPERTY_ENTRY_STRING("chip-label", chip_label);
536
537 base = gpio_mockup_range_base(index: idx);
538 if (base >= 0)
539 properties[prop++] = PROPERTY_ENTRY_U32("gpio-base", base);
540
541 ngpio = base < 0 ? gpio_mockup_range_ngpio(index: idx)
542 : gpio_mockup_range_ngpio(index: idx) - base;
543 properties[prop++] = PROPERTY_ENTRY_U16("nr-gpios", ngpio);
544
545 if (gpio_mockup_named_lines) {
546 line_names = kasprintf_strarray(GFP_KERNEL, prefix: chip_label, n: ngpio);
547 if (!line_names)
548 return -ENOMEM;
549
550 properties[prop++] = PROPERTY_ENTRY_STRING_ARRAY_LEN(
551 "gpio-line-names", line_names, ngpio);
552 }
553
554 fwnode = fwnode_create_software_node(properties, NULL);
555 if (IS_ERR(ptr: fwnode)) {
556 kfree_strarray(array: line_names, n: ngpio);
557 return PTR_ERR(ptr: fwnode);
558 }
559
560 pdevinfo.name = "gpio-mockup";
561 pdevinfo.id = idx;
562 pdevinfo.fwnode = fwnode;
563
564 pdev = platform_device_register_full(pdevinfo: &pdevinfo);
565 kfree_strarray(array: line_names, n: ngpio);
566 if (IS_ERR(ptr: pdev)) {
567 fwnode_remove_software_node(fwnode);
568 pr_err("error registering device");
569 return PTR_ERR(ptr: pdev);
570 }
571
572 gpio_mockup_pdevs[idx] = pdev;
573
574 return 0;
575}
576
577static int __init gpio_mockup_init(void)
578{
579 int i, num_chips, err;
580
581 if ((gpio_mockup_num_ranges % 2) ||
582 (gpio_mockup_num_ranges > GPIO_MOCKUP_MAX_RANGES))
583 return -EINVAL;
584
585 /* Each chip is described by two values. */
586 num_chips = gpio_mockup_num_ranges / 2;
587
588 /*
589 * The second value in the <base GPIO - number of GPIOS> pair must
590 * always be greater than 0.
591 */
592 for (i = 0; i < num_chips; i++) {
593 if (gpio_mockup_range_ngpio(index: i) < 0)
594 return -EINVAL;
595 }
596
597 gpio_mockup_dbg_dir = debugfs_create_dir(name: "gpio-mockup", NULL);
598
599 err = platform_driver_register(&gpio_mockup_driver);
600 if (err) {
601 pr_err("error registering platform driver\n");
602 debugfs_remove_recursive(dentry: gpio_mockup_dbg_dir);
603 return err;
604 }
605
606 for (i = 0; i < num_chips; i++) {
607 err = gpio_mockup_register_chip(idx: i);
608 if (err) {
609 platform_driver_unregister(&gpio_mockup_driver);
610 gpio_mockup_unregister_pdevs();
611 debugfs_remove_recursive(dentry: gpio_mockup_dbg_dir);
612 return err;
613 }
614 }
615
616 return 0;
617}
618
619static void __exit gpio_mockup_exit(void)
620{
621 gpio_mockup_unregister_pdevs();
622 debugfs_remove_recursive(dentry: gpio_mockup_dbg_dir);
623 platform_driver_unregister(&gpio_mockup_driver);
624}
625
626module_init(gpio_mockup_init);
627module_exit(gpio_mockup_exit);
628
629MODULE_AUTHOR("Kamlakant Patel <kamlakant.patel@broadcom.com>");
630MODULE_AUTHOR("Bamvor Jian Zhang <bamv2005@gmail.com>");
631MODULE_AUTHOR("Bartosz Golaszewski <brgl@bgdev.pl>");
632MODULE_DESCRIPTION("GPIO Testing driver");
633MODULE_LICENSE("GPL v2");
634

source code of linux/drivers/gpio/gpio-mockup.c