xref: /openbmc/linux/drivers/gpio/gpio-mockup.c (revision 1ac731c529cd4d6adbce134754b51ff7d822b145)
1f2f67983SBartosz Golaszewski // SPDX-License-Identifier: GPL-2.0-or-later
20f98dd1bSBamvor Jian Zhang /*
30f98dd1bSBamvor Jian Zhang  * GPIO Testing Device Driver
40f98dd1bSBamvor Jian Zhang  *
50f98dd1bSBamvor Jian Zhang  * Copyright (C) 2014  Kamlakant Patel <kamlakant.patel@broadcom.com>
64f9a4cd6SLinus Walleij  * Copyright (C) 2015-2016  Bamvor Jian Zhang <bamv2005@gmail.com>
7c60c7f4cSBartosz Golaszewski  * Copyright (C) 2017 Bartosz Golaszewski <brgl@bgdev.pl>
80f98dd1bSBamvor Jian Zhang  */
90f98dd1bSBamvor Jian Zhang 
1056f6cb35SBartosz Golaszewski #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
1156f6cb35SBartosz Golaszewski 
12726a4453SBartosz Golaszewski #include <linux/debugfs.h>
13726a4453SBartosz Golaszewski #include <linux/gpio/driver.h>
14e2ff7408SBartosz Golaszewski #include <linux/interrupt.h>
15e2ff7408SBartosz Golaszewski #include <linux/irq.h>
16b4c495f0SBartosz Golaszewski #include <linux/irq_sim.h>
17337cbeb2SBartosz Golaszewski #include <linux/irqdomain.h>
1843ddebddSVincent Whitchurch #include <linux/mod_devicetable.h>
19726a4453SBartosz Golaszewski #include <linux/module.h>
20726a4453SBartosz Golaszewski #include <linux/platform_device.h>
213ea47b44SBartosz Golaszewski #include <linux/property.h>
225cedd3c2SAndy Shevchenko #include <linux/seq_file.h>
23726a4453SBartosz Golaszewski #include <linux/slab.h>
24582be05eSBartosz Golaszewski #include <linux/string_helpers.h>
25726a4453SBartosz Golaszewski #include <linux/uaccess.h>
269202ba23SBartosz Golaszewski 
279202ba23SBartosz Golaszewski #include "gpiolib.h"
280f98dd1bSBamvor Jian Zhang 
29ca409160SBartosz Golaszewski #define GPIO_MOCKUP_MAX_GC	10
30b652336dSBartosz Golaszewski /*
31b652336dSBartosz Golaszewski  * We're storing two values per chip: the GPIO base and the number
32b652336dSBartosz Golaszewski  * of GPIO lines.
33b652336dSBartosz Golaszewski  */
34b652336dSBartosz Golaszewski #define GPIO_MOCKUP_MAX_RANGES	(GPIO_MOCKUP_MAX_GC * 2)
35383bb2deSBartosz Golaszewski /* Maximum of four properties + the sentinel. */
36383bb2deSBartosz Golaszewski #define GPIO_MOCKUP_MAX_PROP	5
370f98dd1bSBamvor Jian Zhang 
380f98dd1bSBamvor Jian Zhang /*
390f98dd1bSBamvor Jian Zhang  * struct gpio_pin_status - structure describing a GPIO status
40e42615ecSMatti Vaittinen  * @dir:       Configures direction of gpio as "in" or "out"
410f98dd1bSBamvor Jian Zhang  * @value:     Configures status of the gpio as 0(low) or 1(high)
420f98dd1bSBamvor Jian Zhang  */
43ca409160SBartosz Golaszewski struct gpio_mockup_line_status {
44ca409160SBartosz Golaszewski 	int dir;
455b7908c0SBartosz Golaszewski 	int value;
462a9e2740SBartosz Golaszewski 	int pull;
47e2ff7408SBartosz Golaszewski };
48e2ff7408SBartosz Golaszewski 
49ca409160SBartosz Golaszewski struct gpio_mockup_chip {
500f98dd1bSBamvor Jian Zhang 	struct gpio_chip gc;
51ca409160SBartosz Golaszewski 	struct gpio_mockup_line_status *lines;
52337cbeb2SBartosz Golaszewski 	struct irq_domain *irq_sim_domain;
539202ba23SBartosz Golaszewski 	struct dentry *dbg_dir;
549212492fSBartosz Golaszewski 	struct mutex lock;
559202ba23SBartosz Golaszewski };
569202ba23SBartosz Golaszewski 
579202ba23SBartosz Golaszewski struct gpio_mockup_dbgfs_private {
589202ba23SBartosz Golaszewski 	struct gpio_mockup_chip *chip;
599202ba23SBartosz Golaszewski 	struct gpio_desc *desc;
6083336668SBartosz Golaszewski 	unsigned int offset;
610f98dd1bSBamvor Jian Zhang };
620f98dd1bSBamvor Jian Zhang 
63b652336dSBartosz Golaszewski static int gpio_mockup_ranges[GPIO_MOCKUP_MAX_RANGES];
64e63a006fSBartosz Golaszewski static int gpio_mockup_num_ranges;
65e63a006fSBartosz Golaszewski module_param_array(gpio_mockup_ranges, int, &gpio_mockup_num_ranges, 0400);
660f98dd1bSBamvor Jian Zhang 
678a68ea00SBartosz Golaszewski static bool gpio_mockup_named_lines;
688a68ea00SBartosz Golaszewski module_param_named(gpio_mockup_named_lines,
698a68ea00SBartosz Golaszewski 		   gpio_mockup_named_lines, bool, 0400);
708a68ea00SBartosz Golaszewski 
719202ba23SBartosz Golaszewski static struct dentry *gpio_mockup_dbg_dir;
720f98dd1bSBamvor Jian Zhang 
gpio_mockup_range_base(unsigned int index)73cd9835f1SBartosz Golaszewski static int gpio_mockup_range_base(unsigned int index)
74cd9835f1SBartosz Golaszewski {
75cd9835f1SBartosz Golaszewski 	return gpio_mockup_ranges[index * 2];
76cd9835f1SBartosz Golaszewski }
77cd9835f1SBartosz Golaszewski 
gpio_mockup_range_ngpio(unsigned int index)78cd9835f1SBartosz Golaszewski static int gpio_mockup_range_ngpio(unsigned int index)
79cd9835f1SBartosz Golaszewski {
80cd9835f1SBartosz Golaszewski 	return gpio_mockup_ranges[index * 2 + 1];
81cd9835f1SBartosz Golaszewski }
82cd9835f1SBartosz Golaszewski 
__gpio_mockup_get(struct gpio_mockup_chip * chip,unsigned int offset)83e09313ceSBartosz Golaszewski static int __gpio_mockup_get(struct gpio_mockup_chip *chip,
84e09313ceSBartosz Golaszewski 			     unsigned int offset)
850f98dd1bSBamvor Jian Zhang {
86ca409160SBartosz Golaszewski 	return chip->lines[offset].value;
870f98dd1bSBamvor Jian Zhang }
880f98dd1bSBamvor Jian Zhang 
gpio_mockup_get(struct gpio_chip * gc,unsigned int offset)899212492fSBartosz Golaszewski static int gpio_mockup_get(struct gpio_chip *gc, unsigned int offset)
909212492fSBartosz Golaszewski {
919212492fSBartosz Golaszewski 	struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
929212492fSBartosz Golaszewski 	int val;
939212492fSBartosz Golaszewski 
949212492fSBartosz Golaszewski 	mutex_lock(&chip->lock);
95e09313ceSBartosz Golaszewski 	val = __gpio_mockup_get(chip, offset);
969212492fSBartosz Golaszewski 	mutex_unlock(&chip->lock);
979212492fSBartosz Golaszewski 
989212492fSBartosz Golaszewski 	return val;
999212492fSBartosz Golaszewski }
1009212492fSBartosz Golaszewski 
gpio_mockup_get_multiple(struct gpio_chip * gc,unsigned long * mask,unsigned long * bits)101cbf1e092SBartosz Golaszewski static int gpio_mockup_get_multiple(struct gpio_chip *gc,
102cbf1e092SBartosz Golaszewski 				    unsigned long *mask, unsigned long *bits)
103cbf1e092SBartosz Golaszewski {
104cbf1e092SBartosz Golaszewski 	struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
105cbf1e092SBartosz Golaszewski 	unsigned int bit, val;
106cbf1e092SBartosz Golaszewski 
107cbf1e092SBartosz Golaszewski 	mutex_lock(&chip->lock);
108cbf1e092SBartosz Golaszewski 	for_each_set_bit(bit, mask, gc->ngpio) {
109e09313ceSBartosz Golaszewski 		val = __gpio_mockup_get(chip, bit);
110cbf1e092SBartosz Golaszewski 		__assign_bit(bit, bits, val);
111cbf1e092SBartosz Golaszewski 	}
112cbf1e092SBartosz Golaszewski 	mutex_unlock(&chip->lock);
113cbf1e092SBartosz Golaszewski 
114cbf1e092SBartosz Golaszewski 	return 0;
115cbf1e092SBartosz Golaszewski }
116cbf1e092SBartosz Golaszewski 
__gpio_mockup_set(struct gpio_mockup_chip * chip,unsigned int offset,int value)117e09313ceSBartosz Golaszewski static void __gpio_mockup_set(struct gpio_mockup_chip *chip,
1182a9d742cSBartosz Golaszewski 			      unsigned int offset, int value)
1190f98dd1bSBamvor Jian Zhang {
120ca409160SBartosz Golaszewski 	chip->lines[offset].value = !!value;
1210f98dd1bSBamvor Jian Zhang }
1220f98dd1bSBamvor Jian Zhang 
gpio_mockup_set(struct gpio_chip * gc,unsigned int offset,int value)1239212492fSBartosz Golaszewski static void gpio_mockup_set(struct gpio_chip *gc,
1249212492fSBartosz Golaszewski 			   unsigned int offset, int value)
1259212492fSBartosz Golaszewski {
1269212492fSBartosz Golaszewski 	struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
1279212492fSBartosz Golaszewski 
1289212492fSBartosz Golaszewski 	mutex_lock(&chip->lock);
129e09313ceSBartosz Golaszewski 	__gpio_mockup_set(chip, offset, value);
1309212492fSBartosz Golaszewski 	mutex_unlock(&chip->lock);
1319212492fSBartosz Golaszewski }
1329212492fSBartosz Golaszewski 
gpio_mockup_set_multiple(struct gpio_chip * gc,unsigned long * mask,unsigned long * bits)133c47bee95SBartosz Golaszewski static void gpio_mockup_set_multiple(struct gpio_chip *gc,
134c47bee95SBartosz Golaszewski 				     unsigned long *mask, unsigned long *bits)
135c47bee95SBartosz Golaszewski {
1369212492fSBartosz Golaszewski 	struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
137c47bee95SBartosz Golaszewski 	unsigned int bit;
138c47bee95SBartosz Golaszewski 
1399212492fSBartosz Golaszewski 	mutex_lock(&chip->lock);
140c47bee95SBartosz Golaszewski 	for_each_set_bit(bit, mask, gc->ngpio)
141e09313ceSBartosz Golaszewski 		__gpio_mockup_set(chip, bit, test_bit(bit, bits));
1429212492fSBartosz Golaszewski 	mutex_unlock(&chip->lock);
143c47bee95SBartosz Golaszewski }
144c47bee95SBartosz Golaszewski 
gpio_mockup_apply_pull(struct gpio_mockup_chip * chip,unsigned int offset,int value)14564e7112eSKent Gibson static int gpio_mockup_apply_pull(struct gpio_mockup_chip *chip,
14664e7112eSKent Gibson 				  unsigned int offset, int value)
14764e7112eSKent Gibson {
148e7d8fde4SAndy Shevchenko 	struct gpio_chip *gc = &chip->gc;
149e7d8fde4SAndy Shevchenko 	struct gpio_desc *desc = gpiochip_get_desc(gc, offset);
150337cbeb2SBartosz Golaszewski 	int curr, irq, irq_type, ret = 0;
15164e7112eSKent Gibson 
15264e7112eSKent Gibson 	mutex_lock(&chip->lock);
15364e7112eSKent Gibson 
15464e7112eSKent Gibson 	if (test_bit(FLAG_REQUESTED, &desc->flags) &&
15564e7112eSKent Gibson 	    !test_bit(FLAG_IS_OUT, &desc->flags)) {
15664e7112eSKent Gibson 		curr = __gpio_mockup_get(chip, offset);
15764e7112eSKent Gibson 		if (curr == value)
15864e7112eSKent Gibson 			goto out;
15964e7112eSKent Gibson 
160337cbeb2SBartosz Golaszewski 		irq = irq_find_mapping(chip->irq_sim_domain, offset);
161337cbeb2SBartosz Golaszewski 		if (!irq)
162337cbeb2SBartosz Golaszewski 			/*
163337cbeb2SBartosz Golaszewski 			 * This is fine - it just means, nobody is listening
164337cbeb2SBartosz Golaszewski 			 * for interrupts on this line, otherwise
165337cbeb2SBartosz Golaszewski 			 * irq_create_mapping() would have been called from
166337cbeb2SBartosz Golaszewski 			 * the to_irq() callback.
167337cbeb2SBartosz Golaszewski 			 */
168337cbeb2SBartosz Golaszewski 			goto set_value;
169337cbeb2SBartosz Golaszewski 
17064e7112eSKent Gibson 		irq_type = irq_get_trigger_type(irq);
17164e7112eSKent Gibson 
17264e7112eSKent Gibson 		if ((value == 1 && (irq_type & IRQ_TYPE_EDGE_RISING)) ||
173337cbeb2SBartosz Golaszewski 		    (value == 0 && (irq_type & IRQ_TYPE_EDGE_FALLING))) {
174337cbeb2SBartosz Golaszewski 			ret = irq_set_irqchip_state(irq, IRQCHIP_STATE_PENDING,
175337cbeb2SBartosz Golaszewski 						    true);
176337cbeb2SBartosz Golaszewski 			if (ret)
177337cbeb2SBartosz Golaszewski 				goto out;
178337cbeb2SBartosz Golaszewski 		}
17964e7112eSKent Gibson 	}
18064e7112eSKent Gibson 
181337cbeb2SBartosz Golaszewski set_value:
18264e7112eSKent Gibson 	/* Change the value unless we're actively driving the line. */
18364e7112eSKent Gibson 	if (!test_bit(FLAG_REQUESTED, &desc->flags) ||
18464e7112eSKent Gibson 	    !test_bit(FLAG_IS_OUT, &desc->flags))
18564e7112eSKent Gibson 		__gpio_mockup_set(chip, offset, value);
18664e7112eSKent Gibson 
18764e7112eSKent Gibson out:
18864e7112eSKent Gibson 	chip->lines[offset].pull = value;
18964e7112eSKent Gibson 	mutex_unlock(&chip->lock);
190337cbeb2SBartosz Golaszewski 	return ret;
19164e7112eSKent Gibson }
19264e7112eSKent Gibson 
gpio_mockup_set_config(struct gpio_chip * gc,unsigned int offset,unsigned long config)19364e7112eSKent Gibson static int gpio_mockup_set_config(struct gpio_chip *gc,
19464e7112eSKent Gibson 				  unsigned int offset, unsigned long config)
19564e7112eSKent Gibson {
19664e7112eSKent Gibson 	struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
19764e7112eSKent Gibson 
19864e7112eSKent Gibson 	switch (pinconf_to_config_param(config)) {
19964e7112eSKent Gibson 	case PIN_CONFIG_BIAS_PULL_UP:
20064e7112eSKent Gibson 		return gpio_mockup_apply_pull(chip, offset, 1);
20164e7112eSKent Gibson 	case PIN_CONFIG_BIAS_PULL_DOWN:
20264e7112eSKent Gibson 		return gpio_mockup_apply_pull(chip, offset, 0);
20364e7112eSKent Gibson 	default:
20464e7112eSKent Gibson 		break;
20564e7112eSKent Gibson 	}
20664e7112eSKent Gibson 	return -ENOTSUPP;
20764e7112eSKent Gibson }
20864e7112eSKent Gibson 
gpio_mockup_dirout(struct gpio_chip * gc,unsigned int offset,int value)2092a9d742cSBartosz Golaszewski static int gpio_mockup_dirout(struct gpio_chip *gc,
2102a9d742cSBartosz Golaszewski 			      unsigned int offset, int value)
2110f98dd1bSBamvor Jian Zhang {
212ca409160SBartosz Golaszewski 	struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
2130f98dd1bSBamvor Jian Zhang 
2149212492fSBartosz Golaszewski 	mutex_lock(&chip->lock);
215e42615ecSMatti Vaittinen 	chip->lines[offset].dir = GPIO_LINE_DIRECTION_OUT;
216e09313ceSBartosz Golaszewski 	__gpio_mockup_set(chip, offset, value);
2179212492fSBartosz Golaszewski 	mutex_unlock(&chip->lock);
218ca409160SBartosz Golaszewski 
2190f98dd1bSBamvor Jian Zhang 	return 0;
2200f98dd1bSBamvor Jian Zhang }
2210f98dd1bSBamvor Jian Zhang 
gpio_mockup_dirin(struct gpio_chip * gc,unsigned int offset)222ca409160SBartosz Golaszewski static int gpio_mockup_dirin(struct gpio_chip *gc, unsigned int offset)
2230f98dd1bSBamvor Jian Zhang {
224ca409160SBartosz Golaszewski 	struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
2250f98dd1bSBamvor Jian Zhang 
2269212492fSBartosz Golaszewski 	mutex_lock(&chip->lock);
227e42615ecSMatti Vaittinen 	chip->lines[offset].dir = GPIO_LINE_DIRECTION_IN;
2289212492fSBartosz Golaszewski 	mutex_unlock(&chip->lock);
229ca409160SBartosz Golaszewski 
2300f98dd1bSBamvor Jian Zhang 	return 0;
2310f98dd1bSBamvor Jian Zhang }
2320f98dd1bSBamvor Jian Zhang 
gpio_mockup_get_direction(struct gpio_chip * gc,unsigned int offset)233ca409160SBartosz Golaszewski static int gpio_mockup_get_direction(struct gpio_chip *gc, unsigned int offset)
2340f98dd1bSBamvor Jian Zhang {
235ca409160SBartosz Golaszewski 	struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
2369212492fSBartosz Golaszewski 	int direction;
2370f98dd1bSBamvor Jian Zhang 
2389212492fSBartosz Golaszewski 	mutex_lock(&chip->lock);
239bc7bc688SKent Gibson 	direction = chip->lines[offset].dir;
2409212492fSBartosz Golaszewski 	mutex_unlock(&chip->lock);
2419212492fSBartosz Golaszewski 
2429212492fSBartosz Golaszewski 	return direction;
2430f98dd1bSBamvor Jian Zhang }
2440f98dd1bSBamvor Jian Zhang 
gpio_mockup_to_irq(struct gpio_chip * gc,unsigned int offset)245b4c495f0SBartosz Golaszewski static int gpio_mockup_to_irq(struct gpio_chip *gc, unsigned int offset)
246e2ff7408SBartosz Golaszewski {
247c7f5326fSBartosz Golaszewski 	struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
248c7f5326fSBartosz Golaszewski 
249337cbeb2SBartosz Golaszewski 	return irq_create_mapping(chip->irq_sim_domain, offset);
250e2ff7408SBartosz Golaszewski }
251e2ff7408SBartosz Golaszewski 
gpio_mockup_free(struct gpio_chip * gc,unsigned int offset)2522a9e2740SBartosz Golaszewski static void gpio_mockup_free(struct gpio_chip *gc, unsigned int offset)
2532a9e2740SBartosz Golaszewski {
2542a9e2740SBartosz Golaszewski 	struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
2552a9e2740SBartosz Golaszewski 
2562a9e2740SBartosz Golaszewski 	__gpio_mockup_set(chip, offset, chip->lines[offset].pull);
2572a9e2740SBartosz Golaszewski }
2582a9e2740SBartosz Golaszewski 
gpio_mockup_debugfs_read(struct file * file,char __user * usr_buf,size_t size,loff_t * ppos)2592a9e2740SBartosz Golaszewski static ssize_t gpio_mockup_debugfs_read(struct file *file,
2602a9e2740SBartosz Golaszewski 					char __user *usr_buf,
2619202ba23SBartosz Golaszewski 					size_t size, loff_t *ppos)
2629202ba23SBartosz Golaszewski {
2639202ba23SBartosz Golaszewski 	struct gpio_mockup_dbgfs_private *priv;
2649202ba23SBartosz Golaszewski 	struct gpio_mockup_chip *chip;
2659202ba23SBartosz Golaszewski 	struct seq_file *sfile;
2662a9e2740SBartosz Golaszewski 	struct gpio_chip *gc;
267ce9fb53cSBartosz Golaszewski 	int val, cnt;
2682a9e2740SBartosz Golaszewski 	char buf[3];
2692583303dSBartosz Golaszewski 
2702a9e2740SBartosz Golaszewski 	if (*ppos != 0)
2712a9e2740SBartosz Golaszewski 		return 0;
2722a9e2740SBartosz Golaszewski 
2732a9e2740SBartosz Golaszewski 	sfile = file->private_data;
2742a9e2740SBartosz Golaszewski 	priv = sfile->private;
2752a9e2740SBartosz Golaszewski 	chip = priv->chip;
2762a9e2740SBartosz Golaszewski 	gc = &chip->gc;
2772a9e2740SBartosz Golaszewski 
2782a9e2740SBartosz Golaszewski 	val = gpio_mockup_get(gc, priv->offset);
2792583303dSBartosz Golaszewski 	cnt = snprintf(buf, sizeof(buf), "%d\n", val);
2802a9e2740SBartosz Golaszewski 
281ce9fb53cSBartosz Golaszewski 	return simple_read_from_buffer(usr_buf, size, ppos, buf, cnt);
2822a9e2740SBartosz Golaszewski }
2832a9e2740SBartosz Golaszewski 
gpio_mockup_debugfs_write(struct file * file,const char __user * usr_buf,size_t size,loff_t * ppos)2842a9e2740SBartosz Golaszewski static ssize_t gpio_mockup_debugfs_write(struct file *file,
2852a9e2740SBartosz Golaszewski 					 const char __user *usr_buf,
2862a9e2740SBartosz Golaszewski 					 size_t size, loff_t *ppos)
2872a9e2740SBartosz Golaszewski {
2882a9e2740SBartosz Golaszewski 	struct gpio_mockup_dbgfs_private *priv;
28964e7112eSKent Gibson 	int rv, val;
2902a9e2740SBartosz Golaszewski 	struct seq_file *sfile;
2912a9e2740SBartosz Golaszewski 
2922a9e2740SBartosz Golaszewski 	if (*ppos != 0)
2932a9e2740SBartosz Golaszewski 		return -EINVAL;
2949202ba23SBartosz Golaszewski 
2956f9b3e77SBartosz Golaszewski 	rv = kstrtoint_from_user(usr_buf, size, 0, &val);
2966f9b3e77SBartosz Golaszewski 	if (rv)
2976f9b3e77SBartosz Golaszewski 		return rv;
2986f9b3e77SBartosz Golaszewski 	if (val != 0 && val != 1)
2996f9b3e77SBartosz Golaszewski 		return -EINVAL;
3006f9b3e77SBartosz Golaszewski 
301650b57b0SBartosz Golaszewski 	sfile = file->private_data;
302650b57b0SBartosz Golaszewski 	priv = sfile->private;
30364e7112eSKent Gibson 	rv = gpio_mockup_apply_pull(priv->chip, priv->offset, val);
30464e7112eSKent Gibson 	if (rv)
30564e7112eSKent Gibson 		return rv;
3069202ba23SBartosz Golaszewski 
3079202ba23SBartosz Golaszewski 	return size;
3089202ba23SBartosz Golaszewski }
3099202ba23SBartosz Golaszewski 
gpio_mockup_debugfs_open(struct inode * inode,struct file * file)3102a9e2740SBartosz Golaszewski static int gpio_mockup_debugfs_open(struct inode *inode, struct file *file)
3119202ba23SBartosz Golaszewski {
3129202ba23SBartosz Golaszewski 	return single_open(file, NULL, inode->i_private);
3139202ba23SBartosz Golaszewski }
3149202ba23SBartosz Golaszewski 
3152a9e2740SBartosz Golaszewski /*
3162a9e2740SBartosz Golaszewski  * Each mockup chip is represented by a directory named after the chip's device
3172a9e2740SBartosz Golaszewski  * name under /sys/kernel/debug/gpio-mockup/. Each line is represented by
3182a9e2740SBartosz Golaszewski  * a file using the line's offset as the name under the chip's directory.
3192a9e2740SBartosz Golaszewski  *
3202a9e2740SBartosz Golaszewski  * Reading from the line's file yields the current *value*, writing to the
3212a9e2740SBartosz Golaszewski  * line's file changes the current *pull*. Default pull for mockup lines is
3222a9e2740SBartosz Golaszewski  * down.
3232a9e2740SBartosz Golaszewski  *
3242a9e2740SBartosz Golaszewski  * Examples:
3252a9e2740SBartosz Golaszewski  * - when a line pulled down is requested in output mode and driven high, its
3262a9e2740SBartosz Golaszewski  *   value will return to 0 once it's released
3272a9e2740SBartosz Golaszewski  * - when the line is requested in output mode and driven high, writing 0 to
3282a9e2740SBartosz Golaszewski  *   the corresponding debugfs file will change the pull to down but the
3292a9e2740SBartosz Golaszewski  *   reported value will still be 1 until the line is released
3302a9e2740SBartosz Golaszewski  * - line requested in input mode always reports the same value as its pull
3312a9e2740SBartosz Golaszewski  *   configuration
3322a9e2740SBartosz Golaszewski  * - when the line is requested in input mode and monitored for events, writing
3332a9e2740SBartosz Golaszewski  *   the same value to the debugfs file will be a noop, while writing the
3342a9e2740SBartosz Golaszewski  *   opposite value will generate a dummy interrupt with an appropriate edge
3352a9e2740SBartosz Golaszewski  */
3362a9e2740SBartosz Golaszewski static const struct file_operations gpio_mockup_debugfs_ops = {
3379202ba23SBartosz Golaszewski 	.owner = THIS_MODULE,
3382a9e2740SBartosz Golaszewski 	.open = gpio_mockup_debugfs_open,
3392a9e2740SBartosz Golaszewski 	.read = gpio_mockup_debugfs_read,
3402a9e2740SBartosz Golaszewski 	.write = gpio_mockup_debugfs_write,
3419202ba23SBartosz Golaszewski 	.llseek = no_llseek,
34259929d3aSWei Yongjun 	.release = single_release,
3439202ba23SBartosz Golaszewski };
3449202ba23SBartosz Golaszewski 
gpio_mockup_debugfs_setup(struct device * dev,struct gpio_mockup_chip * chip)3459202ba23SBartosz Golaszewski static void gpio_mockup_debugfs_setup(struct device *dev,
3469202ba23SBartosz Golaszewski 				      struct gpio_mockup_chip *chip)
3479202ba23SBartosz Golaszewski {
3489202ba23SBartosz Golaszewski 	struct gpio_mockup_dbgfs_private *priv;
3499202ba23SBartosz Golaszewski 	struct gpio_chip *gc;
350ca8792afSBartosz Golaszewski 	const char *devname;
3519202ba23SBartosz Golaszewski 	char *name;
3529202ba23SBartosz Golaszewski 	int i;
3539202ba23SBartosz Golaszewski 
3549202ba23SBartosz Golaszewski 	gc = &chip->gc;
355ca8792afSBartosz Golaszewski 	devname = dev_name(&gc->gpiodev->dev);
3569202ba23SBartosz Golaszewski 
357ca8792afSBartosz Golaszewski 	chip->dbg_dir = debugfs_create_dir(devname, gpio_mockup_dbg_dir);
3589202ba23SBartosz Golaszewski 
3599202ba23SBartosz Golaszewski 	for (i = 0; i < gc->ngpio; i++) {
3609202ba23SBartosz Golaszewski 		name = devm_kasprintf(dev, GFP_KERNEL, "%d", i);
3619202ba23SBartosz Golaszewski 		if (!name)
362f360dcd4SGreg Kroah-Hartman 			return;
3639202ba23SBartosz Golaszewski 
3649202ba23SBartosz Golaszewski 		priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
3659202ba23SBartosz Golaszewski 		if (!priv)
366f360dcd4SGreg Kroah-Hartman 			return;
3679202ba23SBartosz Golaszewski 
3689202ba23SBartosz Golaszewski 		priv->chip = chip;
3699202ba23SBartosz Golaszewski 		priv->offset = i;
370e7d8fde4SAndy Shevchenko 		priv->desc = gpiochip_get_desc(gc, i);
3719202ba23SBartosz Golaszewski 
372*0a1bb16eSZev Weiss 		debugfs_create_file(name, 0600, chip->dbg_dir, priv,
3732a9e2740SBartosz Golaszewski 				    &gpio_mockup_debugfs_ops);
3749202ba23SBartosz Golaszewski 	}
3759202ba23SBartosz Golaszewski }
3769202ba23SBartosz Golaszewski 
gpio_mockup_debugfs_cleanup(void * data)377303e6da9SWei Yongjun static void gpio_mockup_debugfs_cleanup(void *data)
378303e6da9SWei Yongjun {
379303e6da9SWei Yongjun 	struct gpio_mockup_chip *chip = data;
380303e6da9SWei Yongjun 
381303e6da9SWei Yongjun 	debugfs_remove_recursive(chip->dbg_dir);
382303e6da9SWei Yongjun }
383303e6da9SWei Yongjun 
gpio_mockup_dispose_mappings(void * data)384337cbeb2SBartosz Golaszewski static void gpio_mockup_dispose_mappings(void *data)
385337cbeb2SBartosz Golaszewski {
386337cbeb2SBartosz Golaszewski 	struct gpio_mockup_chip *chip = data;
387337cbeb2SBartosz Golaszewski 	struct gpio_chip *gc = &chip->gc;
388337cbeb2SBartosz Golaszewski 	int i, irq;
389337cbeb2SBartosz Golaszewski 
390337cbeb2SBartosz Golaszewski 	for (i = 0; i < gc->ngpio; i++) {
391337cbeb2SBartosz Golaszewski 		irq = irq_find_mapping(chip->irq_sim_domain, i);
392337cbeb2SBartosz Golaszewski 		if (irq)
393337cbeb2SBartosz Golaszewski 			irq_dispose_mapping(irq);
394337cbeb2SBartosz Golaszewski 	}
395337cbeb2SBartosz Golaszewski }
396337cbeb2SBartosz Golaszewski 
gpio_mockup_probe(struct platform_device * pdev)397ca409160SBartosz Golaszewski static int gpio_mockup_probe(struct platform_device *pdev)
3980f98dd1bSBamvor Jian Zhang {
3998a39f597SBartosz Golaszewski 	struct gpio_mockup_chip *chip;
4006d974d32SBartosz Golaszewski 	struct gpio_chip *gc;
4018a39f597SBartosz Golaszewski 	struct device *dev;
4023ea47b44SBartosz Golaszewski 	const char *name;
403bc7bc688SKent Gibson 	int rv, base, i;
4043ea47b44SBartosz Golaszewski 	u16 ngpio;
4050f98dd1bSBamvor Jian Zhang 
4068a39f597SBartosz Golaszewski 	dev = &pdev->dev;
4073ea47b44SBartosz Golaszewski 
4083ea47b44SBartosz Golaszewski 	rv = device_property_read_u32(dev, "gpio-base", &base);
4093ea47b44SBartosz Golaszewski 	if (rv)
4103ea47b44SBartosz Golaszewski 		base = -1;
4113ea47b44SBartosz Golaszewski 
4123ea47b44SBartosz Golaszewski 	rv = device_property_read_u16(dev, "nr-gpios", &ngpio);
4133ea47b44SBartosz Golaszewski 	if (rv)
4143ea47b44SBartosz Golaszewski 		return rv;
4153ea47b44SBartosz Golaszewski 
416148c2560SBartosz Golaszewski 	rv = device_property_read_string(dev, "chip-label", &name);
4173ea47b44SBartosz Golaszewski 	if (rv)
418148c2560SBartosz Golaszewski 		name = dev_name(dev);
419b652336dSBartosz Golaszewski 
4208a39f597SBartosz Golaszewski 	chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL);
4218a39f597SBartosz Golaszewski 	if (!chip)
4220f98dd1bSBamvor Jian Zhang 		return -ENOMEM;
4230f98dd1bSBamvor Jian Zhang 
4249212492fSBartosz Golaszewski 	mutex_init(&chip->lock);
4259212492fSBartosz Golaszewski 
4266d974d32SBartosz Golaszewski 	gc = &chip->gc;
4276d974d32SBartosz Golaszewski 	gc->base = base;
4286d974d32SBartosz Golaszewski 	gc->ngpio = ngpio;
4296d974d32SBartosz Golaszewski 	gc->label = name;
4306d974d32SBartosz Golaszewski 	gc->owner = THIS_MODULE;
4316d974d32SBartosz Golaszewski 	gc->parent = dev;
4326d974d32SBartosz Golaszewski 	gc->get = gpio_mockup_get;
4336d974d32SBartosz Golaszewski 	gc->set = gpio_mockup_set;
434cbf1e092SBartosz Golaszewski 	gc->get_multiple = gpio_mockup_get_multiple;
435c47bee95SBartosz Golaszewski 	gc->set_multiple = gpio_mockup_set_multiple;
4366d974d32SBartosz Golaszewski 	gc->direction_output = gpio_mockup_dirout;
4376d974d32SBartosz Golaszewski 	gc->direction_input = gpio_mockup_dirin;
4386d974d32SBartosz Golaszewski 	gc->get_direction = gpio_mockup_get_direction;
43964e7112eSKent Gibson 	gc->set_config = gpio_mockup_set_config;
4406d974d32SBartosz Golaszewski 	gc->to_irq = gpio_mockup_to_irq;
4412a9e2740SBartosz Golaszewski 	gc->free = gpio_mockup_free;
4426d974d32SBartosz Golaszewski 
4436d974d32SBartosz Golaszewski 	chip->lines = devm_kcalloc(dev, gc->ngpio,
4446d974d32SBartosz Golaszewski 				   sizeof(*chip->lines), GFP_KERNEL);
4456d974d32SBartosz Golaszewski 	if (!chip->lines)
4466d974d32SBartosz Golaszewski 		return -ENOMEM;
4476d974d32SBartosz Golaszewski 
448bc7bc688SKent Gibson 	for (i = 0; i < gc->ngpio; i++)
449bc7bc688SKent Gibson 		chip->lines[i].dir = GPIO_LINE_DIRECTION_IN;
450bc7bc688SKent Gibson 
451337cbeb2SBartosz Golaszewski 	chip->irq_sim_domain = devm_irq_domain_create_sim(dev, NULL,
452337cbeb2SBartosz Golaszewski 							  gc->ngpio);
453337cbeb2SBartosz Golaszewski 	if (IS_ERR(chip->irq_sim_domain))
454337cbeb2SBartosz Golaszewski 		return PTR_ERR(chip->irq_sim_domain);
455337cbeb2SBartosz Golaszewski 
456337cbeb2SBartosz Golaszewski 	rv = devm_add_action_or_reset(dev, gpio_mockup_dispose_mappings, chip);
457337cbeb2SBartosz Golaszewski 	if (rv)
4586d974d32SBartosz Golaszewski 		return rv;
4596d974d32SBartosz Golaszewski 
4606d974d32SBartosz Golaszewski 	rv = devm_gpiochip_add_data(dev, &chip->gc, chip);
4616d974d32SBartosz Golaszewski 	if (rv)
4626d974d32SBartosz Golaszewski 		return rv;
4636d974d32SBartosz Golaszewski 
4646d974d32SBartosz Golaszewski 	gpio_mockup_debugfs_setup(dev, chip);
4656d974d32SBartosz Golaszewski 
466303e6da9SWei Yongjun 	return devm_add_action_or_reset(dev, gpio_mockup_debugfs_cleanup, chip);
4670f98dd1bSBamvor Jian Zhang }
4680f98dd1bSBamvor Jian Zhang 
46943ddebddSVincent Whitchurch static const struct of_device_id gpio_mockup_of_match[] = {
47043ddebddSVincent Whitchurch 	{ .compatible = "gpio-mockup", },
47143ddebddSVincent Whitchurch 	{},
47243ddebddSVincent Whitchurch };
47343ddebddSVincent Whitchurch MODULE_DEVICE_TABLE(of, gpio_mockup_of_match);
47443ddebddSVincent Whitchurch 
475ca409160SBartosz Golaszewski static struct platform_driver gpio_mockup_driver = {
4760f98dd1bSBamvor Jian Zhang 	.driver = {
47725f00066SBartosz Golaszewski 		.name = "gpio-mockup",
47843ddebddSVincent Whitchurch 		.of_match_table = gpio_mockup_of_match,
4790f98dd1bSBamvor Jian Zhang 	},
480ca409160SBartosz Golaszewski 	.probe = gpio_mockup_probe,
4810f98dd1bSBamvor Jian Zhang };
4820f98dd1bSBamvor Jian Zhang 
4838a39f597SBartosz Golaszewski static struct platform_device *gpio_mockup_pdevs[GPIO_MOCKUP_MAX_GC];
4848a39f597SBartosz Golaszewski 
gpio_mockup_unregister_pdevs(void)4858a39f597SBartosz Golaszewski static void gpio_mockup_unregister_pdevs(void)
4868a39f597SBartosz Golaszewski {
4876fda593fSAndy Shevchenko 	struct platform_device *pdev;
4886fda593fSAndy Shevchenko 	struct fwnode_handle *fwnode;
4898a39f597SBartosz Golaszewski 	int i;
4908a39f597SBartosz Golaszewski 
4916fda593fSAndy Shevchenko 	for (i = 0; i < GPIO_MOCKUP_MAX_GC; i++) {
4926fda593fSAndy Shevchenko 		pdev = gpio_mockup_pdevs[i];
4936fda593fSAndy Shevchenko 		if (!pdev)
4946fda593fSAndy Shevchenko 			continue;
4956fda593fSAndy Shevchenko 
4966fda593fSAndy Shevchenko 		fwnode = dev_fwnode(&pdev->dev);
4976fda593fSAndy Shevchenko 		platform_device_unregister(pdev);
4986fda593fSAndy Shevchenko 		fwnode_remove_software_node(fwnode);
4996fda593fSAndy Shevchenko 	}
5008a39f597SBartosz Golaszewski }
501f3b47170SBartosz Golaszewski 
gpio_mockup_register_chip(int idx)50242e9acc6SBartosz Golaszewski static int __init gpio_mockup_register_chip(int idx)
5030f98dd1bSBamvor Jian Zhang {
5043ea47b44SBartosz Golaszewski 	struct property_entry properties[GPIO_MOCKUP_MAX_PROP];
5053ea47b44SBartosz Golaszewski 	struct platform_device_info pdevinfo;
5068a39f597SBartosz Golaszewski 	struct platform_device *pdev;
5076fda593fSAndy Shevchenko 	struct fwnode_handle *fwnode;
50842e9acc6SBartosz Golaszewski 	char **line_names = NULL;
509148c2560SBartosz Golaszewski 	char chip_label[32];
51042e9acc6SBartosz Golaszewski 	int prop = 0, base;
5113ea47b44SBartosz Golaszewski 	u16 ngpio;
5120f98dd1bSBamvor Jian Zhang 
51342e9acc6SBartosz Golaszewski 	memset(properties, 0, sizeof(properties));
51442e9acc6SBartosz Golaszewski 	memset(&pdevinfo, 0, sizeof(pdevinfo));
51542e9acc6SBartosz Golaszewski 
51642e9acc6SBartosz Golaszewski 	snprintf(chip_label, sizeof(chip_label), "gpio-mockup-%c", idx + 'A');
51742e9acc6SBartosz Golaszewski 	properties[prop++] = PROPERTY_ENTRY_STRING("chip-label", chip_label);
51842e9acc6SBartosz Golaszewski 
51942e9acc6SBartosz Golaszewski 	base = gpio_mockup_range_base(idx);
52042e9acc6SBartosz Golaszewski 	if (base >= 0)
52142e9acc6SBartosz Golaszewski 		properties[prop++] = PROPERTY_ENTRY_U32("gpio-base", base);
52242e9acc6SBartosz Golaszewski 
52342e9acc6SBartosz Golaszewski 	ngpio = base < 0 ? gpio_mockup_range_ngpio(idx)
52442e9acc6SBartosz Golaszewski 			 : gpio_mockup_range_ngpio(idx) - base;
52542e9acc6SBartosz Golaszewski 	properties[prop++] = PROPERTY_ENTRY_U16("nr-gpios", ngpio);
52642e9acc6SBartosz Golaszewski 
52742e9acc6SBartosz Golaszewski 	if (gpio_mockup_named_lines) {
528f7c151d8SAndy Shevchenko 		line_names = kasprintf_strarray(GFP_KERNEL, chip_label, ngpio);
52942e9acc6SBartosz Golaszewski 		if (!line_names)
53042e9acc6SBartosz Golaszewski 			return -ENOMEM;
53142e9acc6SBartosz Golaszewski 
53242e9acc6SBartosz Golaszewski 		properties[prop++] = PROPERTY_ENTRY_STRING_ARRAY_LEN(
53342e9acc6SBartosz Golaszewski 					"gpio-line-names", line_names, ngpio);
53442e9acc6SBartosz Golaszewski 	}
53542e9acc6SBartosz Golaszewski 
5366fda593fSAndy Shevchenko 	fwnode = fwnode_create_software_node(properties, NULL);
53702743c40SAndy Shevchenko 	if (IS_ERR(fwnode)) {
53802743c40SAndy Shevchenko 		kfree_strarray(line_names, ngpio);
5396fda593fSAndy Shevchenko 		return PTR_ERR(fwnode);
54002743c40SAndy Shevchenko 	}
5416fda593fSAndy Shevchenko 
54242e9acc6SBartosz Golaszewski 	pdevinfo.name = "gpio-mockup";
54342e9acc6SBartosz Golaszewski 	pdevinfo.id = idx;
5446fda593fSAndy Shevchenko 	pdevinfo.fwnode = fwnode;
54542e9acc6SBartosz Golaszewski 
54642e9acc6SBartosz Golaszewski 	pdev = platform_device_register_full(&pdevinfo);
54742e9acc6SBartosz Golaszewski 	kfree_strarray(line_names, ngpio);
54842e9acc6SBartosz Golaszewski 	if (IS_ERR(pdev)) {
5496fda593fSAndy Shevchenko 		fwnode_remove_software_node(fwnode);
55042e9acc6SBartosz Golaszewski 		pr_err("error registering device");
55142e9acc6SBartosz Golaszewski 		return PTR_ERR(pdev);
55242e9acc6SBartosz Golaszewski 	}
55342e9acc6SBartosz Golaszewski 
55442e9acc6SBartosz Golaszewski 	gpio_mockup_pdevs[idx] = pdev;
55542e9acc6SBartosz Golaszewski 
55642e9acc6SBartosz Golaszewski 	return 0;
55742e9acc6SBartosz Golaszewski }
55842e9acc6SBartosz Golaszewski 
gpio_mockup_init(void)55942e9acc6SBartosz Golaszewski static int __init gpio_mockup_init(void)
56042e9acc6SBartosz Golaszewski {
56142e9acc6SBartosz Golaszewski 	int i, num_chips, err;
56242e9acc6SBartosz Golaszewski 
56343ddebddSVincent Whitchurch 	if ((gpio_mockup_num_ranges % 2) ||
564e63a006fSBartosz Golaszewski 	    (gpio_mockup_num_ranges > GPIO_MOCKUP_MAX_RANGES))
56520c35ac4SBartosz Golaszewski 		return -EINVAL;
56620c35ac4SBartosz Golaszewski 
5678a39f597SBartosz Golaszewski 	/* Each chip is described by two values. */
568e63a006fSBartosz Golaszewski 	num_chips = gpio_mockup_num_ranges / 2;
5698a39f597SBartosz Golaszewski 
570fa86963aSBartosz Golaszewski 	/*
571fa86963aSBartosz Golaszewski 	 * The second value in the <base GPIO - number of GPIOS> pair must
572fa86963aSBartosz Golaszewski 	 * always be greater than 0.
573fa86963aSBartosz Golaszewski 	 */
574fa86963aSBartosz Golaszewski 	for (i = 0; i < num_chips; i++) {
575cd9835f1SBartosz Golaszewski 		if (gpio_mockup_range_ngpio(i) < 0)
576fa86963aSBartosz Golaszewski 			return -EINVAL;
577fa86963aSBartosz Golaszewski 	}
578fa86963aSBartosz Golaszewski 
5792a9e2740SBartosz Golaszewski 	gpio_mockup_dbg_dir = debugfs_create_dir("gpio-mockup", NULL);
5809202ba23SBartosz Golaszewski 
5818a39f597SBartosz Golaszewski 	err = platform_driver_register(&gpio_mockup_driver);
5820f98dd1bSBamvor Jian Zhang 	if (err) {
58356f6cb35SBartosz Golaszewski 		pr_err("error registering platform driver\n");
584e0ab949fSBartosz Golaszewski 		debugfs_remove_recursive(gpio_mockup_dbg_dir);
5850f98dd1bSBamvor Jian Zhang 		return err;
5860f98dd1bSBamvor Jian Zhang 	}
5870f98dd1bSBamvor Jian Zhang 
5888a39f597SBartosz Golaszewski 	for (i = 0; i < num_chips; i++) {
58942e9acc6SBartosz Golaszewski 		err = gpio_mockup_register_chip(i);
59042e9acc6SBartosz Golaszewski 		if (err) {
5918a39f597SBartosz Golaszewski 			platform_driver_unregister(&gpio_mockup_driver);
5928a39f597SBartosz Golaszewski 			gpio_mockup_unregister_pdevs();
593e0ab949fSBartosz Golaszewski 			debugfs_remove_recursive(gpio_mockup_dbg_dir);
59442e9acc6SBartosz Golaszewski 			return err;
5958a39f597SBartosz Golaszewski 		}
5960f98dd1bSBamvor Jian Zhang 	}
5970f98dd1bSBamvor Jian Zhang 
5980f98dd1bSBamvor Jian Zhang 	return 0;
5990f98dd1bSBamvor Jian Zhang }
6000f98dd1bSBamvor Jian Zhang 
gpio_mockup_exit(void)601f3b47170SBartosz Golaszewski static void __exit gpio_mockup_exit(void)
6020f98dd1bSBamvor Jian Zhang {
603b7df41a6SBartosz Golaszewski 	gpio_mockup_unregister_pdevs();
6049202ba23SBartosz Golaszewski 	debugfs_remove_recursive(gpio_mockup_dbg_dir);
605ca409160SBartosz Golaszewski 	platform_driver_unregister(&gpio_mockup_driver);
6060f98dd1bSBamvor Jian Zhang }
6070f98dd1bSBamvor Jian Zhang 
608f3b47170SBartosz Golaszewski module_init(gpio_mockup_init);
609f3b47170SBartosz Golaszewski module_exit(gpio_mockup_exit);
6100f98dd1bSBamvor Jian Zhang 
6110f98dd1bSBamvor Jian Zhang MODULE_AUTHOR("Kamlakant Patel <kamlakant.patel@broadcom.com>");
6124f9a4cd6SLinus Walleij MODULE_AUTHOR("Bamvor Jian Zhang <bamv2005@gmail.com>");
613c60c7f4cSBartosz Golaszewski MODULE_AUTHOR("Bartosz Golaszewski <brgl@bgdev.pl>");
6140f98dd1bSBamvor Jian Zhang MODULE_DESCRIPTION("GPIO Testing driver");
6150f98dd1bSBamvor Jian Zhang MODULE_LICENSE("GPL v2");
616