Linux Audio

Check our new training course

Open-source upstreaming

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