xref: /openbmc/linux/drivers/gpio/gpio-mockup.c (revision 6aa7de05)
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 <bamvor.zhangjian@linaro.org>
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 enum {
38 	GPIO_MOCKUP_DIR_OUT = 0,
39 	GPIO_MOCKUP_DIR_IN = 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  */
47 struct gpio_mockup_line_status {
48 	int dir;
49 	bool value;
50 };
51 
52 struct gpio_mockup_chip {
53 	struct gpio_chip gc;
54 	struct gpio_mockup_line_status *lines;
55 	struct irq_sim irqsim;
56 	struct dentry *dbg_dir;
57 };
58 
59 struct gpio_mockup_dbgfs_private {
60 	struct gpio_mockup_chip *chip;
61 	struct gpio_desc *desc;
62 	int offset;
63 };
64 
65 static int gpio_mockup_ranges[GPIO_MOCKUP_MAX_RANGES];
66 static int gpio_mockup_params_nr;
67 module_param_array(gpio_mockup_ranges, int, &gpio_mockup_params_nr, 0400);
68 
69 static bool gpio_mockup_named_lines;
70 module_param_named(gpio_mockup_named_lines,
71 		   gpio_mockup_named_lines, bool, 0400);
72 
73 static const char gpio_mockup_name_start = 'A';
74 static struct dentry *gpio_mockup_dbg_dir;
75 
76 static int gpio_mockup_get(struct gpio_chip *gc, unsigned int offset)
77 {
78 	struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
79 
80 	return chip->lines[offset].value;
81 }
82 
83 static void gpio_mockup_set(struct gpio_chip *gc, unsigned int offset,
84 			    int value)
85 {
86 	struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
87 
88 	chip->lines[offset].value = !!value;
89 }
90 
91 static int gpio_mockup_dirout(struct gpio_chip *gc, unsigned int offset,
92 			      int value)
93 {
94 	struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
95 
96 	gpio_mockup_set(gc, offset, value);
97 	chip->lines[offset].dir = GPIO_MOCKUP_DIR_OUT;
98 
99 	return 0;
100 }
101 
102 static int gpio_mockup_dirin(struct gpio_chip *gc, unsigned int offset)
103 {
104 	struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
105 
106 	chip->lines[offset].dir = GPIO_MOCKUP_DIR_IN;
107 
108 	return 0;
109 }
110 
111 static int gpio_mockup_get_direction(struct gpio_chip *gc, unsigned int offset)
112 {
113 	struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
114 
115 	return chip->lines[offset].dir;
116 }
117 
118 static int gpio_mockup_name_lines(struct device *dev,
119 				  struct gpio_mockup_chip *chip)
120 {
121 	struct gpio_chip *gc = &chip->gc;
122 	char **names;
123 	int i;
124 
125 	names = devm_kcalloc(dev, gc->ngpio, sizeof(char *), GFP_KERNEL);
126 	if (!names)
127 		return -ENOMEM;
128 
129 	for (i = 0; i < gc->ngpio; i++) {
130 		names[i] = devm_kasprintf(dev, GFP_KERNEL,
131 					  "%s-%d", gc->label, i);
132 		if (!names[i])
133 			return -ENOMEM;
134 	}
135 
136 	gc->names = (const char *const *)names;
137 
138 	return 0;
139 }
140 
141 static int gpio_mockup_to_irq(struct gpio_chip *gc, unsigned int offset)
142 {
143 	struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
144 
145 	return irq_sim_irqnum(&chip->irqsim, offset);
146 }
147 
148 static ssize_t gpio_mockup_event_write(struct file *file,
149 				       const char __user *usr_buf,
150 				       size_t size, loff_t *ppos)
151 {
152 	struct gpio_mockup_dbgfs_private *priv;
153 	struct gpio_mockup_chip *chip;
154 	struct seq_file *sfile;
155 	struct gpio_desc *desc;
156 	int rv, val;
157 
158 	rv = kstrtoint_from_user(usr_buf, size, 0, &val);
159 	if (rv)
160 		return rv;
161 	if (val != 0 && val != 1)
162 		return -EINVAL;
163 
164 	sfile = file->private_data;
165 	priv = sfile->private;
166 	desc = priv->desc;
167 	chip = priv->chip;
168 
169 	gpiod_set_value_cansleep(desc, val);
170 	irq_sim_fire(&chip->irqsim, priv->offset);
171 
172 	return size;
173 }
174 
175 static int gpio_mockup_event_open(struct inode *inode, struct file *file)
176 {
177 	return single_open(file, NULL, inode->i_private);
178 }
179 
180 static const struct file_operations gpio_mockup_event_ops = {
181 	.owner = THIS_MODULE,
182 	.open = gpio_mockup_event_open,
183 	.write = gpio_mockup_event_write,
184 	.llseek = no_llseek,
185 };
186 
187 static void gpio_mockup_debugfs_setup(struct device *dev,
188 				      struct gpio_mockup_chip *chip)
189 {
190 	struct gpio_mockup_dbgfs_private *priv;
191 	struct dentry *evfile;
192 	struct gpio_chip *gc;
193 	char *name;
194 	int i;
195 
196 	gc = &chip->gc;
197 
198 	chip->dbg_dir = debugfs_create_dir(gc->label, gpio_mockup_dbg_dir);
199 	if (!chip->dbg_dir)
200 		goto err;
201 
202 	for (i = 0; i < gc->ngpio; i++) {
203 		name = devm_kasprintf(dev, GFP_KERNEL, "%d", i);
204 		if (!name)
205 			goto err;
206 
207 		priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
208 		if (!priv)
209 			goto err;
210 
211 		priv->chip = chip;
212 		priv->offset = i;
213 		priv->desc = &gc->gpiodev->descs[i];
214 
215 		evfile = debugfs_create_file(name, 0200, chip->dbg_dir, priv,
216 					     &gpio_mockup_event_ops);
217 		if (!evfile)
218 			goto err;
219 	}
220 
221 	return;
222 
223 err:
224 	dev_err(dev, "error creating debugfs directory\n");
225 }
226 
227 static int gpio_mockup_add(struct device *dev,
228 			   struct gpio_mockup_chip *chip,
229 			   const char *name, int base, int ngpio)
230 {
231 	struct gpio_chip *gc = &chip->gc;
232 	int ret;
233 
234 	gc->base = base;
235 	gc->ngpio = ngpio;
236 	gc->label = name;
237 	gc->owner = THIS_MODULE;
238 	gc->parent = dev;
239 	gc->get = gpio_mockup_get;
240 	gc->set = gpio_mockup_set;
241 	gc->direction_output = gpio_mockup_dirout;
242 	gc->direction_input = gpio_mockup_dirin;
243 	gc->get_direction = gpio_mockup_get_direction;
244 	gc->to_irq = gpio_mockup_to_irq;
245 
246 	chip->lines = devm_kcalloc(dev, gc->ngpio,
247 				   sizeof(*chip->lines), GFP_KERNEL);
248 	if (!chip->lines)
249 		return -ENOMEM;
250 
251 	if (gpio_mockup_named_lines) {
252 		ret = gpio_mockup_name_lines(dev, chip);
253 		if (ret)
254 			return ret;
255 	}
256 
257 	ret = devm_irq_sim_init(dev, &chip->irqsim, gc->ngpio);
258 	if (ret)
259 		return ret;
260 
261 	ret = devm_gpiochip_add_data(dev, &chip->gc, chip);
262 	if (ret)
263 		return ret;
264 
265 	if (gpio_mockup_dbg_dir)
266 		gpio_mockup_debugfs_setup(dev, chip);
267 
268 	return 0;
269 }
270 
271 static int gpio_mockup_probe(struct platform_device *pdev)
272 {
273 	int ret, i, base, ngpio, num_chips;
274 	struct device *dev = &pdev->dev;
275 	struct gpio_mockup_chip *chips;
276 	char *chip_name;
277 
278 	if (gpio_mockup_params_nr < 2 || (gpio_mockup_params_nr % 2))
279 		return -EINVAL;
280 
281 	/* Each chip is described by two values. */
282 	num_chips = gpio_mockup_params_nr / 2;
283 
284 	chips = devm_kcalloc(dev, num_chips, sizeof(*chips), GFP_KERNEL);
285 	if (!chips)
286 		return -ENOMEM;
287 
288 	platform_set_drvdata(pdev, chips);
289 
290 	for (i = 0; i < num_chips; i++) {
291 		base = gpio_mockup_ranges[i * 2];
292 
293 		if (base == -1)
294 			ngpio = gpio_mockup_ranges[i * 2 + 1];
295 		else
296 			ngpio = gpio_mockup_ranges[i * 2 + 1] - base;
297 
298 		if (ngpio >= 0) {
299 			chip_name = devm_kasprintf(dev, GFP_KERNEL,
300 						   "%s-%c", GPIO_MOCKUP_NAME,
301 						   gpio_mockup_name_start + i);
302 			if (!chip_name)
303 				return -ENOMEM;
304 
305 			ret = gpio_mockup_add(dev, &chips[i],
306 					      chip_name, base, ngpio);
307 		} else {
308 			ret = -EINVAL;
309 		}
310 
311 		if (ret) {
312 			dev_err(dev,
313 				"adding gpiochip failed: %d (base: %d, ngpio: %d)\n",
314 				ret, base, base < 0 ? ngpio : base + ngpio);
315 
316 			return ret;
317 		}
318 	}
319 
320 	return 0;
321 }
322 
323 static struct platform_driver gpio_mockup_driver = {
324 	.driver = {
325 		.name = GPIO_MOCKUP_NAME,
326 	},
327 	.probe = gpio_mockup_probe,
328 };
329 
330 static struct platform_device *pdev;
331 static int __init mock_device_init(void)
332 {
333 	int err;
334 
335 	gpio_mockup_dbg_dir = debugfs_create_dir("gpio-mockup-event", NULL);
336 	if (!gpio_mockup_dbg_dir)
337 		pr_err("%s: error creating debugfs directory\n",
338 		       GPIO_MOCKUP_NAME);
339 
340 	pdev = platform_device_alloc(GPIO_MOCKUP_NAME, -1);
341 	if (!pdev)
342 		return -ENOMEM;
343 
344 	err = platform_device_add(pdev);
345 	if (err) {
346 		platform_device_put(pdev);
347 		return err;
348 	}
349 
350 	err = platform_driver_register(&gpio_mockup_driver);
351 	if (err) {
352 		platform_device_unregister(pdev);
353 		return err;
354 	}
355 
356 	return 0;
357 }
358 
359 static void __exit mock_device_exit(void)
360 {
361 	debugfs_remove_recursive(gpio_mockup_dbg_dir);
362 	platform_driver_unregister(&gpio_mockup_driver);
363 	platform_device_unregister(pdev);
364 }
365 
366 module_init(mock_device_init);
367 module_exit(mock_device_exit);
368 
369 MODULE_AUTHOR("Kamlakant Patel <kamlakant.patel@broadcom.com>");
370 MODULE_AUTHOR("Bamvor Jian Zhang <bamvor.zhangjian@linaro.org>");
371 MODULE_AUTHOR("Bartosz Golaszewski <brgl@bgdev.pl>");
372 MODULE_DESCRIPTION("GPIO Testing driver");
373 MODULE_LICENSE("GPL v2");
374