blob: b70f3b0c30b1d9e42a8017dbb16fb30223eee91b [file] [log] [blame]
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +02001/*
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>
Bartosz Golaszewskic60c7f42017-06-09 13:41:31 +02006 * Copyright (C) 2017 Bartosz Golaszewski <brgl@bgdev.pl>
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +02007 *
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>
Bartosz Golaszewski9202ba22017-02-06 15:11:08 +010018#include <linux/gpio/consumer.h>
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +020019#include <linux/platform_device.h>
Bartosz Golaszewski8a68ea02017-02-06 13:10:37 +010020#include <linux/slab.h>
Bartosz Golaszewskie2ff7402017-02-06 15:11:07 +010021#include <linux/interrupt.h>
22#include <linux/irq.h>
Bartosz Golaszewskib4c495f2017-08-14 13:20:56 +020023#include <linux/irq_sim.h>
Bartosz Golaszewski9202ba22017-02-06 15:11:08 +010024#include <linux/debugfs.h>
25#include <linux/uaccess.h>
26
27#include "gpiolib.h"
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +020028
Bartosz Golaszewskica4091602017-02-06 13:10:35 +010029#define GPIO_MOCKUP_NAME "gpio-mockup"
30#define GPIO_MOCKUP_MAX_GC 10
Bartosz Golaszewskib6523362017-06-09 13:41:28 +020031/*
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)
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +020036
Bartosz Golaszewskica4091602017-02-06 13:10:35 +010037enum {
Bartosz Golaszewskic650c002017-05-25 10:33:39 +020038 GPIO_MOCKUP_DIR_OUT = 0,
39 GPIO_MOCKUP_DIR_IN = 1,
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +020040};
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 */
Bartosz Golaszewskica4091602017-02-06 13:10:35 +010047struct gpio_mockup_line_status {
48 int dir;
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +020049 bool value;
Bartosz Golaszewskie2ff7402017-02-06 15:11:07 +010050};
51
Bartosz Golaszewskica4091602017-02-06 13:10:35 +010052struct gpio_mockup_chip {
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +020053 struct gpio_chip gc;
Bartosz Golaszewskica4091602017-02-06 13:10:35 +010054 struct gpio_mockup_line_status *lines;
Bartosz Golaszewskib4c495f2017-08-14 13:20:56 +020055 struct irq_sim irqsim;
Bartosz Golaszewski9202ba22017-02-06 15:11:08 +010056 struct dentry *dbg_dir;
57};
58
59struct gpio_mockup_dbgfs_private {
60 struct gpio_mockup_chip *chip;
61 struct gpio_desc *desc;
62 int offset;
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +020063};
64
Bartosz Golaszewskib6523362017-06-09 13:41:28 +020065static int gpio_mockup_ranges[GPIO_MOCKUP_MAX_RANGES];
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +020066static int gpio_mockup_params_nr;
67module_param_array(gpio_mockup_ranges, int, &gpio_mockup_params_nr, 0400);
68
Bartosz Golaszewski8a68ea02017-02-06 13:10:37 +010069static bool gpio_mockup_named_lines;
70module_param_named(gpio_mockup_named_lines,
71 gpio_mockup_named_lines, bool, 0400);
72
Bartosz Golaszewskica4091602017-02-06 13:10:35 +010073static const char gpio_mockup_name_start = 'A';
Bartosz Golaszewski9202ba22017-02-06 15:11:08 +010074static struct dentry *gpio_mockup_dbg_dir;
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +020075
Bartosz Golaszewskica4091602017-02-06 13:10:35 +010076static int gpio_mockup_get(struct gpio_chip *gc, unsigned int offset)
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +020077{
Bartosz Golaszewskica4091602017-02-06 13:10:35 +010078 struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +020079
Bartosz Golaszewskica4091602017-02-06 13:10:35 +010080 return chip->lines[offset].value;
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +020081}
82
Bartosz Golaszewskica4091602017-02-06 13:10:35 +010083static void gpio_mockup_set(struct gpio_chip *gc, unsigned int offset,
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +020084 int value)
85{
Bartosz Golaszewskica4091602017-02-06 13:10:35 +010086 struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +020087
Bartosz Golaszewskica4091602017-02-06 13:10:35 +010088 chip->lines[offset].value = !!value;
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +020089}
90
Bartosz Golaszewskica4091602017-02-06 13:10:35 +010091static int gpio_mockup_dirout(struct gpio_chip *gc, unsigned int offset,
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +020092 int value)
93{
Bartosz Golaszewskica4091602017-02-06 13:10:35 +010094 struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +020095
Bartosz Golaszewskica4091602017-02-06 13:10:35 +010096 gpio_mockup_set(gc, offset, value);
Bartosz Golaszewskic650c002017-05-25 10:33:39 +020097 chip->lines[offset].dir = GPIO_MOCKUP_DIR_OUT;
Bartosz Golaszewskica4091602017-02-06 13:10:35 +010098
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +020099 return 0;
100}
101
Bartosz Golaszewskica4091602017-02-06 13:10:35 +0100102static int gpio_mockup_dirin(struct gpio_chip *gc, unsigned int offset)
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +0200103{
Bartosz Golaszewskica4091602017-02-06 13:10:35 +0100104 struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +0200105
Bartosz Golaszewskic650c002017-05-25 10:33:39 +0200106 chip->lines[offset].dir = GPIO_MOCKUP_DIR_IN;
Bartosz Golaszewskica4091602017-02-06 13:10:35 +0100107
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +0200108 return 0;
109}
110
Bartosz Golaszewskica4091602017-02-06 13:10:35 +0100111static int gpio_mockup_get_direction(struct gpio_chip *gc, unsigned int offset)
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +0200112{
Bartosz Golaszewskica4091602017-02-06 13:10:35 +0100113 struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +0200114
Bartosz Golaszewskica4091602017-02-06 13:10:35 +0100115 return chip->lines[offset].dir;
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +0200116}
117
Bartosz Golaszewski8a68ea02017-02-06 13:10:37 +0100118static 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
Bartosz Golaszewskif6ac4382017-06-09 13:41:32 +0200125 names = devm_kcalloc(dev, gc->ngpio, sizeof(char *), GFP_KERNEL);
Bartosz Golaszewski8a68ea02017-02-06 13:10:37 +0100126 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
Bartosz Golaszewskib4c495f2017-08-14 13:20:56 +0200141static int gpio_mockup_to_irq(struct gpio_chip *gc, unsigned int offset)
Bartosz Golaszewskie2ff7402017-02-06 15:11:07 +0100142{
Bartosz Golaszewskic7f53262017-05-25 10:33:41 +0200143 struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
144
Bartosz Golaszewskib4c495f2017-08-14 13:20:56 +0200145 return irq_sim_irqnum(&chip->irqsim, offset);
Bartosz Golaszewskie2ff7402017-02-06 15:11:07 +0100146}
147
Bartosz Golaszewski9202ba22017-02-06 15:11:08 +0100148static 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;
Bartosz Golaszewski6f9b3e72017-06-09 13:41:25 +0200156 int rv, val;
Bartosz Golaszewski9202ba22017-02-06 15:11:08 +0100157
Bartosz Golaszewski6f9b3e72017-06-09 13:41:25 +0200158 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
Bartosz Golaszewski650b57b2017-06-09 13:41:26 +0200164 sfile = file->private_data;
165 priv = sfile->private;
166 desc = priv->desc;
167 chip = priv->chip;
Bartosz Golaszewskic7f53262017-05-25 10:33:41 +0200168
Bartosz Golaszewskib4c495f2017-08-14 13:20:56 +0200169 gpiod_set_value_cansleep(desc, val);
170 irq_sim_fire(&chip->irqsim, priv->offset);
Bartosz Golaszewski9202ba22017-02-06 15:11:08 +0100171
172 return size;
173}
174
175static int gpio_mockup_event_open(struct inode *inode, struct file *file)
176{
177 return single_open(file, NULL, inode->i_private);
178}
179
180static 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
187static 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
223err:
224 dev_err(dev, "error creating debugfs directory\n");
225}
226
Bartosz Golaszewskica4091602017-02-06 13:10:35 +0100227static int gpio_mockup_add(struct device *dev,
228 struct gpio_mockup_chip *chip,
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +0200229 const char *name, int base, int ngpio)
230{
Bartosz Golaszewskica4091602017-02-06 13:10:35 +0100231 struct gpio_chip *gc = &chip->gc;
Bartosz Golaszewski8a68ea02017-02-06 13:10:37 +0100232 int ret;
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +0200233
Bartosz Golaszewskica4091602017-02-06 13:10:35 +0100234 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;
Bartosz Golaszewskie2ff7402017-02-06 15:11:07 +0100244 gc->to_irq = gpio_mockup_to_irq;
Bartosz Golaszewskica4091602017-02-06 13:10:35 +0100245
Bartosz Golaszewskif6ac4382017-06-09 13:41:32 +0200246 chip->lines = devm_kcalloc(dev, gc->ngpio,
247 sizeof(*chip->lines), GFP_KERNEL);
Bartosz Golaszewskie4ba07b2017-02-06 13:10:36 +0100248 if (!chip->lines)
249 return -ENOMEM;
Bartosz Golaszewskica4091602017-02-06 13:10:35 +0100250
Bartosz Golaszewski8a68ea02017-02-06 13:10:37 +0100251 if (gpio_mockup_named_lines) {
252 ret = gpio_mockup_name_lines(dev, chip);
253 if (ret)
254 return ret;
255 }
256
Bartosz Golaszewskib4c495f2017-08-14 13:20:56 +0200257 ret = devm_irq_sim_init(dev, &chip->irqsim, gc->ngpio);
Bartosz Golaszewskie2ff7402017-02-06 15:11:07 +0100258 if (ret)
259 return ret;
260
Bartosz Golaszewski9202ba22017-02-06 15:11:08 +0100261 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;
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +0200269}
270
Bartosz Golaszewskica4091602017-02-06 13:10:35 +0100271static int gpio_mockup_probe(struct platform_device *pdev)
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +0200272{
Bartosz Golaszewskib6523362017-06-09 13:41:28 +0200273 int ret, i, base, ngpio, num_chips;
Bartosz Golaszewski364ac452016-12-20 12:28:20 +0100274 struct device *dev = &pdev->dev;
Bartosz Golaszewskib6523362017-06-09 13:41:28 +0200275 struct gpio_mockup_chip *chips;
Bartosz Golaszewskiad6d8002016-12-20 12:28:19 +0100276 char *chip_name;
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +0200277
Bartosz Golaszewskib6523362017-06-09 13:41:28 +0200278 /* Each chip is described by two values. */
279 num_chips = gpio_mockup_params_nr / 2;
280
Bartosz Golaszewskif6ac4382017-06-09 13:41:32 +0200281 chips = devm_kcalloc(dev, num_chips, sizeof(*chips), GFP_KERNEL);
Bartosz Golaszewskica4091602017-02-06 13:10:35 +0100282 if (!chips)
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +0200283 return -ENOMEM;
284
Bartosz Golaszewskica4091602017-02-06 13:10:35 +0100285 platform_set_drvdata(pdev, chips);
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +0200286
Bartosz Golaszewskib6523362017-06-09 13:41:28 +0200287 for (i = 0; i < num_chips; i++) {
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +0200288 base = gpio_mockup_ranges[i * 2];
Bartosz Golaszewskica4091602017-02-06 13:10:35 +0100289
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +0200290 if (base == -1)
291 ngpio = gpio_mockup_ranges[i * 2 + 1];
292 else
293 ngpio = gpio_mockup_ranges[i * 2 + 1] - base;
294
295 if (ngpio >= 0) {
Bartosz Golaszewskiad6d8002016-12-20 12:28:19 +0100296 chip_name = devm_kasprintf(dev, GFP_KERNEL,
Bartosz Golaszewskica4091602017-02-06 13:10:35 +0100297 "%s-%c", GPIO_MOCKUP_NAME,
298 gpio_mockup_name_start + i);
Bartosz Golaszewskiad6d8002016-12-20 12:28:19 +0100299 if (!chip_name)
300 return -ENOMEM;
301
Bartosz Golaszewskica4091602017-02-06 13:10:35 +0100302 ret = gpio_mockup_add(dev, &chips[i],
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +0200303 chip_name, base, ngpio);
304 } else {
Bartosz Golaszewski4dc9d762017-06-09 13:41:29 +0200305 ret = -EINVAL;
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +0200306 }
Bartosz Golaszewskica4091602017-02-06 13:10:35 +0100307
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +0200308 if (ret) {
Bartosz Golaszewskiec604f12017-06-09 13:41:30 +0200309 dev_err(dev,
310 "adding gpiochip failed: %d (base: %d, ngpio: %d)\n",
311 ret, base, base < 0 ? ngpio : base + ngpio);
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +0200312
313 return ret;
314 }
315 }
316
317 return 0;
318}
319
Bartosz Golaszewskica4091602017-02-06 13:10:35 +0100320static struct platform_driver gpio_mockup_driver = {
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +0200321 .driver = {
Bartosz Golaszewskica4091602017-02-06 13:10:35 +0100322 .name = GPIO_MOCKUP_NAME,
Bartosz Golaszewski364ac452016-12-20 12:28:20 +0100323 },
Bartosz Golaszewskica4091602017-02-06 13:10:35 +0100324 .probe = gpio_mockup_probe,
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +0200325};
326
Bartosz Golaszewskif3b47172017-11-27 11:48:37 +0100327static struct platform_device *gpio_mockup_pdev;
328
329static int __init gpio_mockup_init(void)
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +0200330{
331 int err;
332
Bartosz Golaszewski20c35ac2017-11-27 11:48:38 +0100333 if (gpio_mockup_params_nr < 2 || (gpio_mockup_params_nr % 2))
334 return -EINVAL;
335
Bartosz Golaszewski9202ba22017-02-06 15:11:08 +0100336 gpio_mockup_dbg_dir = debugfs_create_dir("gpio-mockup-event", NULL);
337 if (!gpio_mockup_dbg_dir)
338 pr_err("%s: error creating debugfs directory\n",
339 GPIO_MOCKUP_NAME);
340
Bartosz Golaszewskif3b47172017-11-27 11:48:37 +0100341 gpio_mockup_pdev = platform_device_alloc(GPIO_MOCKUP_NAME, -1);
342 if (!gpio_mockup_pdev)
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +0200343 return -ENOMEM;
344
Bartosz Golaszewskif3b47172017-11-27 11:48:37 +0100345 err = platform_device_add(gpio_mockup_pdev);
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +0200346 if (err) {
Bartosz Golaszewskif3b47172017-11-27 11:48:37 +0100347 platform_device_put(gpio_mockup_pdev);
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +0200348 return err;
349 }
350
Bartosz Golaszewskica4091602017-02-06 13:10:35 +0100351 err = platform_driver_register(&gpio_mockup_driver);
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +0200352 if (err) {
Bartosz Golaszewskif3b47172017-11-27 11:48:37 +0100353 platform_device_unregister(gpio_mockup_pdev);
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +0200354 return err;
355 }
356
357 return 0;
358}
359
Bartosz Golaszewskif3b47172017-11-27 11:48:37 +0100360static void __exit gpio_mockup_exit(void)
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +0200361{
Bartosz Golaszewski9202ba22017-02-06 15:11:08 +0100362 debugfs_remove_recursive(gpio_mockup_dbg_dir);
Bartosz Golaszewskica4091602017-02-06 13:10:35 +0100363 platform_driver_unregister(&gpio_mockup_driver);
Bartosz Golaszewskif3b47172017-11-27 11:48:37 +0100364 platform_device_unregister(gpio_mockup_pdev);
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +0200365}
366
Bartosz Golaszewskif3b47172017-11-27 11:48:37 +0100367module_init(gpio_mockup_init);
368module_exit(gpio_mockup_exit);
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +0200369
370MODULE_AUTHOR("Kamlakant Patel <kamlakant.patel@broadcom.com>");
371MODULE_AUTHOR("Bamvor Jian Zhang <bamvor.zhangjian@linaro.org>");
Bartosz Golaszewskic60c7f42017-06-09 13:41:31 +0200372MODULE_AUTHOR("Bartosz Golaszewski <brgl@bgdev.pl>");
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +0200373MODULE_DESCRIPTION("GPIO Testing driver");
374MODULE_LICENSE("GPL v2");