tracing/snapshot: Resize spare buffer if size changed
[linux/fpc-iii.git] / drivers / gpio / gpio-mockup.c
blob945bd13e5e7918c014a1d44792c0d67393f50629
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * GPIO Testing Device Driver
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 */
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>
22 #include "gpiolib.h"
24 #define GPIO_MOCKUP_NAME "gpio-mockup"
25 #define GPIO_MOCKUP_MAX_GC 10
27 * We're storing two values per chip: the GPIO base and the number
28 * of GPIO lines.
30 #define GPIO_MOCKUP_MAX_RANGES (GPIO_MOCKUP_MAX_GC * 2)
32 #define gpio_mockup_err(...) pr_err(GPIO_MOCKUP_NAME ": " __VA_ARGS__)
34 enum {
35 GPIO_MOCKUP_DIR_IN = 0,
36 GPIO_MOCKUP_DIR_OUT = 1,
40 * struct gpio_pin_status - structure describing a GPIO status
41 * @dir: Configures direction of gpio as "in" or "out", 0=in, 1=out
42 * @value: Configures status of the gpio as 0(low) or 1(high)
44 struct gpio_mockup_line_status {
45 int dir;
46 int value;
49 struct gpio_mockup_chip {
50 struct gpio_chip gc;
51 struct gpio_mockup_line_status *lines;
52 struct irq_sim irqsim;
53 struct dentry *dbg_dir;
56 struct gpio_mockup_dbgfs_private {
57 struct gpio_mockup_chip *chip;
58 struct gpio_desc *desc;
59 int offset;
62 struct gpio_mockup_platform_data {
63 int base;
64 int ngpio;
65 int index;
66 bool named_lines;
69 static int gpio_mockup_ranges[GPIO_MOCKUP_MAX_RANGES];
70 static int gpio_mockup_num_ranges;
71 module_param_array(gpio_mockup_ranges, int, &gpio_mockup_num_ranges, 0400);
73 static bool gpio_mockup_named_lines;
74 module_param_named(gpio_mockup_named_lines,
75 gpio_mockup_named_lines, bool, 0400);
77 static struct dentry *gpio_mockup_dbg_dir;
79 static int gpio_mockup_range_base(unsigned int index)
81 return gpio_mockup_ranges[index * 2];
84 static int gpio_mockup_range_ngpio(unsigned int index)
86 return gpio_mockup_ranges[index * 2 + 1];
89 static int gpio_mockup_get(struct gpio_chip *gc, unsigned int offset)
91 struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
93 return chip->lines[offset].value;
96 static void gpio_mockup_set(struct gpio_chip *gc,
97 unsigned int offset, int value)
99 struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
101 chip->lines[offset].value = !!value;
104 static void gpio_mockup_set_multiple(struct gpio_chip *gc,
105 unsigned long *mask, unsigned long *bits)
107 unsigned int bit;
109 for_each_set_bit(bit, mask, gc->ngpio)
110 gpio_mockup_set(gc, bit, test_bit(bit, bits));
114 static int gpio_mockup_dirout(struct gpio_chip *gc,
115 unsigned int offset, int value)
117 struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
119 gpio_mockup_set(gc, offset, value);
120 chip->lines[offset].dir = GPIO_MOCKUP_DIR_OUT;
122 return 0;
125 static int gpio_mockup_dirin(struct gpio_chip *gc, unsigned int offset)
127 struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
129 chip->lines[offset].dir = GPIO_MOCKUP_DIR_IN;
131 return 0;
134 static int gpio_mockup_get_direction(struct gpio_chip *gc, unsigned int offset)
136 struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
138 return !chip->lines[offset].dir;
141 static int gpio_mockup_to_irq(struct gpio_chip *gc, unsigned int offset)
143 struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
145 return irq_sim_irqnum(&chip->irqsim, offset);
148 static ssize_t gpio_mockup_event_write(struct file *file,
149 const char __user *usr_buf,
150 size_t size, loff_t *ppos)
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;
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;
164 sfile = file->private_data;
165 priv = sfile->private;
166 desc = priv->desc;
167 chip = priv->chip;
169 gpiod_set_value_cansleep(desc, val);
170 irq_sim_fire(&chip->irqsim, priv->offset);
172 return size;
175 static int gpio_mockup_event_open(struct inode *inode, struct file *file)
177 return single_open(file, NULL, inode->i_private);
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,
187 static void gpio_mockup_debugfs_setup(struct device *dev,
188 struct gpio_mockup_chip *chip)
190 struct gpio_mockup_dbgfs_private *priv;
191 struct dentry *evfile, *link;
192 struct gpio_chip *gc;
193 const char *devname;
194 char *name;
195 int i;
197 gc = &chip->gc;
198 devname = dev_name(&gc->gpiodev->dev);
200 chip->dbg_dir = debugfs_create_dir(devname, gpio_mockup_dbg_dir);
201 if (IS_ERR_OR_NULL(chip->dbg_dir))
202 goto err;
204 link = debugfs_create_symlink(gc->label, gpio_mockup_dbg_dir, devname);
205 if (IS_ERR_OR_NULL(link))
206 goto err;
208 for (i = 0; i < gc->ngpio; i++) {
209 name = devm_kasprintf(dev, GFP_KERNEL, "%d", i);
210 if (!name)
211 goto err;
213 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
214 if (!priv)
215 goto err;
217 priv->chip = chip;
218 priv->offset = i;
219 priv->desc = &gc->gpiodev->descs[i];
221 evfile = debugfs_create_file(name, 0200, chip->dbg_dir, priv,
222 &gpio_mockup_event_ops);
223 if (IS_ERR_OR_NULL(evfile))
224 goto err;
227 return;
229 err:
230 dev_err(dev, "error creating debugfs event files\n");
233 static int gpio_mockup_name_lines(struct device *dev,
234 struct gpio_mockup_chip *chip)
236 struct gpio_chip *gc = &chip->gc;
237 char **names;
238 int i;
240 names = devm_kcalloc(dev, gc->ngpio, sizeof(char *), GFP_KERNEL);
241 if (!names)
242 return -ENOMEM;
244 for (i = 0; i < gc->ngpio; i++) {
245 names[i] = devm_kasprintf(dev, GFP_KERNEL,
246 "%s-%d", gc->label, i);
247 if (!names[i])
248 return -ENOMEM;
251 gc->names = (const char *const *)names;
253 return 0;
256 static int gpio_mockup_probe(struct platform_device *pdev)
258 struct gpio_mockup_platform_data *pdata;
259 struct gpio_mockup_chip *chip;
260 struct gpio_chip *gc;
261 int rv, base, ngpio;
262 struct device *dev;
263 char *name;
265 dev = &pdev->dev;
266 pdata = dev_get_platdata(dev);
267 base = pdata->base;
268 ngpio = pdata->ngpio;
270 chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL);
271 if (!chip)
272 return -ENOMEM;
274 name = devm_kasprintf(dev, GFP_KERNEL, "%s-%c",
275 pdev->name, pdata->index);
276 if (!name)
277 return -ENOMEM;
279 gc = &chip->gc;
280 gc->base = base;
281 gc->ngpio = ngpio;
282 gc->label = name;
283 gc->owner = THIS_MODULE;
284 gc->parent = dev;
285 gc->get = gpio_mockup_get;
286 gc->set = gpio_mockup_set;
287 gc->set_multiple = gpio_mockup_set_multiple;
288 gc->direction_output = gpio_mockup_dirout;
289 gc->direction_input = gpio_mockup_dirin;
290 gc->get_direction = gpio_mockup_get_direction;
291 gc->to_irq = gpio_mockup_to_irq;
293 chip->lines = devm_kcalloc(dev, gc->ngpio,
294 sizeof(*chip->lines), GFP_KERNEL);
295 if (!chip->lines)
296 return -ENOMEM;
298 if (pdata->named_lines) {
299 rv = gpio_mockup_name_lines(dev, chip);
300 if (rv)
301 return rv;
304 rv = devm_irq_sim_init(dev, &chip->irqsim, gc->ngpio);
305 if (rv < 0)
306 return rv;
308 rv = devm_gpiochip_add_data(dev, &chip->gc, chip);
309 if (rv)
310 return rv;
312 if (!IS_ERR_OR_NULL(gpio_mockup_dbg_dir))
313 gpio_mockup_debugfs_setup(dev, chip);
315 return 0;
318 static struct platform_driver gpio_mockup_driver = {
319 .driver = {
320 .name = GPIO_MOCKUP_NAME,
322 .probe = gpio_mockup_probe,
325 static struct platform_device *gpio_mockup_pdevs[GPIO_MOCKUP_MAX_GC];
327 static void gpio_mockup_unregister_pdevs(void)
329 struct platform_device *pdev;
330 int i;
332 for (i = 0; i < GPIO_MOCKUP_MAX_GC; i++) {
333 pdev = gpio_mockup_pdevs[i];
335 if (pdev)
336 platform_device_unregister(pdev);
340 static int __init gpio_mockup_init(void)
342 int i, num_chips, err = 0, index = 'A';
343 struct gpio_mockup_platform_data pdata;
344 struct platform_device *pdev;
346 if ((gpio_mockup_num_ranges < 2) ||
347 (gpio_mockup_num_ranges % 2) ||
348 (gpio_mockup_num_ranges > GPIO_MOCKUP_MAX_RANGES))
349 return -EINVAL;
351 /* Each chip is described by two values. */
352 num_chips = gpio_mockup_num_ranges / 2;
355 * The second value in the <base GPIO - number of GPIOS> pair must
356 * always be greater than 0.
358 for (i = 0; i < num_chips; i++) {
359 if (gpio_mockup_range_ngpio(i) < 0)
360 return -EINVAL;
363 gpio_mockup_dbg_dir = debugfs_create_dir("gpio-mockup-event", NULL);
364 if (IS_ERR_OR_NULL(gpio_mockup_dbg_dir))
365 gpio_mockup_err("error creating debugfs directory\n");
367 err = platform_driver_register(&gpio_mockup_driver);
368 if (err) {
369 gpio_mockup_err("error registering platform driver\n");
370 return err;
373 for (i = 0; i < num_chips; i++) {
374 pdata.index = index++;
375 pdata.base = gpio_mockup_range_base(i);
376 pdata.ngpio = pdata.base < 0
377 ? gpio_mockup_range_ngpio(i)
378 : gpio_mockup_range_ngpio(i) - pdata.base;
379 pdata.named_lines = gpio_mockup_named_lines;
381 pdev = platform_device_register_resndata(NULL,
382 GPIO_MOCKUP_NAME,
383 i, NULL, 0, &pdata,
384 sizeof(pdata));
385 if (IS_ERR(pdev)) {
386 gpio_mockup_err("error registering device");
387 platform_driver_unregister(&gpio_mockup_driver);
388 gpio_mockup_unregister_pdevs();
389 return PTR_ERR(pdev);
392 gpio_mockup_pdevs[i] = pdev;
395 return 0;
398 static void __exit gpio_mockup_exit(void)
400 debugfs_remove_recursive(gpio_mockup_dbg_dir);
401 platform_driver_unregister(&gpio_mockup_driver);
402 gpio_mockup_unregister_pdevs();
405 module_init(gpio_mockup_init);
406 module_exit(gpio_mockup_exit);
408 MODULE_AUTHOR("Kamlakant Patel <kamlakant.patel@broadcom.com>");
409 MODULE_AUTHOR("Bamvor Jian Zhang <bamv2005@gmail.com>");
410 MODULE_AUTHOR("Bartosz Golaszewski <brgl@bgdev.pl>");
411 MODULE_DESCRIPTION("GPIO Testing driver");
412 MODULE_LICENSE("GPL v2");