Linux Audio

Check our new training course

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