blob: d78e8e081f5a3aa69f8e199dfc2c414a20e2ea1b [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>
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version.
11 *
12 */
13
14#include <linux/init.h>
15#include <linux/module.h>
16#include <linux/gpio/driver.h>
Bartosz Golaszewski9202ba22017-02-06 15:11:08 +010017#include <linux/gpio/consumer.h>
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +020018#include <linux/platform_device.h>
Bartosz Golaszewski8a68ea02017-02-06 13:10:37 +010019#include <linux/slab.h>
Bartosz Golaszewskie2ff7402017-02-06 15:11:07 +010020#include <linux/interrupt.h>
21#include <linux/irq.h>
22#include <linux/irq_work.h>
Bartosz Golaszewski9202ba22017-02-06 15:11:08 +010023#include <linux/debugfs.h>
24#include <linux/uaccess.h>
25
26#include "gpiolib.h"
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +020027
Bartosz Golaszewskica4091602017-02-06 13:10:35 +010028#define GPIO_MOCKUP_NAME "gpio-mockup"
29#define GPIO_MOCKUP_MAX_GC 10
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +020030
Bartosz Golaszewskica4091602017-02-06 13:10:35 +010031enum {
Bartosz Golaszewskic650c002017-05-25 10:33:39 +020032 GPIO_MOCKUP_DIR_OUT = 0,
33 GPIO_MOCKUP_DIR_IN = 1,
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +020034};
35
36/*
37 * struct gpio_pin_status - structure describing a GPIO status
38 * @dir: Configures direction of gpio as "in" or "out", 0=in, 1=out
39 * @value: Configures status of the gpio as 0(low) or 1(high)
40 */
Bartosz Golaszewskica4091602017-02-06 13:10:35 +010041struct gpio_mockup_line_status {
42 int dir;
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +020043 bool value;
Bartosz Golaszewskic7f5326fb2017-05-25 10:33:41 +020044 bool irq_enabled;
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +020045};
46
Bartosz Golaszewskie2ff7402017-02-06 15:11:07 +010047struct gpio_mockup_irq_context {
48 struct irq_work work;
49 int irq;
50};
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 Golaszewskie2ff7402017-02-06 15:11:07 +010055 struct gpio_mockup_irq_context irq_ctx;
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 Golaszewskica4091602017-02-06 13:10:35 +010065static int gpio_mockup_ranges[GPIO_MOCKUP_MAX_GC << 1];
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
125 names = devm_kzalloc(dev, sizeof(char *) * gc->ngpio, 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
Bartosz Golaszewskie2ff7402017-02-06 15:11:07 +0100141static int gpio_mockup_to_irq(struct gpio_chip *chip, unsigned int offset)
142{
143 return chip->irq_base + offset;
144}
145
Bartosz Golaszewskic7f5326fb2017-05-25 10:33:41 +0200146static void gpio_mockup_irqmask(struct irq_data *data)
147{
148 struct gpio_chip *gc = irq_data_get_irq_chip_data(data);
149 struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
150
151 chip->lines[data->irq - gc->irq_base].irq_enabled = false;
152}
153
154static void gpio_mockup_irqunmask(struct irq_data *data)
155{
156 struct gpio_chip *gc = irq_data_get_irq_chip_data(data);
157 struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
158
159 chip->lines[data->irq - gc->irq_base].irq_enabled = true;
160}
Bartosz Golaszewskie2ff7402017-02-06 15:11:07 +0100161
162static struct irq_chip gpio_mockup_irqchip = {
163 .name = GPIO_MOCKUP_NAME,
164 .irq_mask = gpio_mockup_irqmask,
165 .irq_unmask = gpio_mockup_irqunmask,
166};
167
168static void gpio_mockup_handle_irq(struct irq_work *work)
169{
170 struct gpio_mockup_irq_context *irq_ctx;
171
172 irq_ctx = container_of(work, struct gpio_mockup_irq_context, work);
173 handle_simple_irq(irq_to_desc(irq_ctx->irq));
174}
175
176static int gpio_mockup_irqchip_setup(struct device *dev,
177 struct gpio_mockup_chip *chip)
178{
179 struct gpio_chip *gc = &chip->gc;
180 int irq_base, i;
181
Bartosz Golaszewski62a7ca02017-03-04 17:23:29 +0100182 irq_base = devm_irq_alloc_descs(dev, -1, 0, gc->ngpio, 0);
Bartosz Golaszewskie2ff7402017-02-06 15:11:07 +0100183 if (irq_base < 0)
184 return irq_base;
185
186 gc->irq_base = irq_base;
187 gc->irqchip = &gpio_mockup_irqchip;
188
189 for (i = 0; i < gc->ngpio; i++) {
190 irq_set_chip(irq_base + i, gc->irqchip);
Bartosz Golaszewskic7f5326fb2017-05-25 10:33:41 +0200191 irq_set_chip_data(irq_base + i, gc);
Bartosz Golaszewskie2ff7402017-02-06 15:11:07 +0100192 irq_set_handler(irq_base + i, &handle_simple_irq);
193 irq_modify_status(irq_base + i,
194 IRQ_NOREQUEST | IRQ_NOAUTOEN, IRQ_NOPROBE);
195 }
196
197 init_irq_work(&chip->irq_ctx.work, gpio_mockup_handle_irq);
198
199 return 0;
200}
201
Bartosz Golaszewski9202ba22017-02-06 15:11:08 +0100202static ssize_t gpio_mockup_event_write(struct file *file,
203 const char __user *usr_buf,
204 size_t size, loff_t *ppos)
205{
206 struct gpio_mockup_dbgfs_private *priv;
207 struct gpio_mockup_chip *chip;
208 struct seq_file *sfile;
209 struct gpio_desc *desc;
210 struct gpio_chip *gc;
Bartosz Golaszewski6f9b3e72017-06-09 13:41:25 +0200211 int rv, val;
Bartosz Golaszewski9202ba22017-02-06 15:11:08 +0100212
Bartosz Golaszewski6f9b3e72017-06-09 13:41:25 +0200213 rv = kstrtoint_from_user(usr_buf, size, 0, &val);
214 if (rv)
215 return rv;
216 if (val != 0 && val != 1)
217 return -EINVAL;
218
Bartosz Golaszewski650b57b2017-06-09 13:41:26 +0200219 sfile = file->private_data;
220 priv = sfile->private;
221 desc = priv->desc;
222 chip = priv->chip;
223 gc = &chip->gc;
Bartosz Golaszewskic7f5326fb2017-05-25 10:33:41 +0200224
Bartosz Golaszewski650b57b2017-06-09 13:41:26 +0200225 if (chip->lines[priv->offset].irq_enabled) {
226 gpiod_set_value_cansleep(desc, val);
227 priv->chip->irq_ctx.irq = gc->irq_base + priv->offset;
228 irq_work_queue(&priv->chip->irq_ctx.work);
229 }
Bartosz Golaszewski9202ba22017-02-06 15:11:08 +0100230
231 return size;
232}
233
234static int gpio_mockup_event_open(struct inode *inode, struct file *file)
235{
236 return single_open(file, NULL, inode->i_private);
237}
238
239static const struct file_operations gpio_mockup_event_ops = {
240 .owner = THIS_MODULE,
241 .open = gpio_mockup_event_open,
242 .write = gpio_mockup_event_write,
243 .llseek = no_llseek,
244};
245
246static void gpio_mockup_debugfs_setup(struct device *dev,
247 struct gpio_mockup_chip *chip)
248{
249 struct gpio_mockup_dbgfs_private *priv;
250 struct dentry *evfile;
251 struct gpio_chip *gc;
252 char *name;
253 int i;
254
255 gc = &chip->gc;
256
257 chip->dbg_dir = debugfs_create_dir(gc->label, gpio_mockup_dbg_dir);
258 if (!chip->dbg_dir)
259 goto err;
260
261 for (i = 0; i < gc->ngpio; i++) {
262 name = devm_kasprintf(dev, GFP_KERNEL, "%d", i);
263 if (!name)
264 goto err;
265
266 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
267 if (!priv)
268 goto err;
269
270 priv->chip = chip;
271 priv->offset = i;
272 priv->desc = &gc->gpiodev->descs[i];
273
274 evfile = debugfs_create_file(name, 0200, chip->dbg_dir, priv,
275 &gpio_mockup_event_ops);
276 if (!evfile)
277 goto err;
278 }
279
280 return;
281
282err:
283 dev_err(dev, "error creating debugfs directory\n");
284}
285
Bartosz Golaszewskica4091602017-02-06 13:10:35 +0100286static int gpio_mockup_add(struct device *dev,
287 struct gpio_mockup_chip *chip,
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +0200288 const char *name, int base, int ngpio)
289{
Bartosz Golaszewskica4091602017-02-06 13:10:35 +0100290 struct gpio_chip *gc = &chip->gc;
Bartosz Golaszewski8a68ea02017-02-06 13:10:37 +0100291 int ret;
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +0200292
Bartosz Golaszewskica4091602017-02-06 13:10:35 +0100293 gc->base = base;
294 gc->ngpio = ngpio;
295 gc->label = name;
296 gc->owner = THIS_MODULE;
297 gc->parent = dev;
298 gc->get = gpio_mockup_get;
299 gc->set = gpio_mockup_set;
300 gc->direction_output = gpio_mockup_dirout;
301 gc->direction_input = gpio_mockup_dirin;
302 gc->get_direction = gpio_mockup_get_direction;
Bartosz Golaszewskie2ff7402017-02-06 15:11:07 +0100303 gc->to_irq = gpio_mockup_to_irq;
Bartosz Golaszewskica4091602017-02-06 13:10:35 +0100304
305 chip->lines = devm_kzalloc(dev, sizeof(*chip->lines) * gc->ngpio,
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +0200306 GFP_KERNEL);
Bartosz Golaszewskie4ba07b2017-02-06 13:10:36 +0100307 if (!chip->lines)
308 return -ENOMEM;
Bartosz Golaszewskica4091602017-02-06 13:10:35 +0100309
Bartosz Golaszewski8a68ea02017-02-06 13:10:37 +0100310 if (gpio_mockup_named_lines) {
311 ret = gpio_mockup_name_lines(dev, chip);
312 if (ret)
313 return ret;
314 }
315
Bartosz Golaszewskie2ff7402017-02-06 15:11:07 +0100316 ret = gpio_mockup_irqchip_setup(dev, chip);
317 if (ret)
318 return ret;
319
Bartosz Golaszewski9202ba22017-02-06 15:11:08 +0100320 ret = devm_gpiochip_add_data(dev, &chip->gc, chip);
321 if (ret)
322 return ret;
323
324 if (gpio_mockup_dbg_dir)
325 gpio_mockup_debugfs_setup(dev, chip);
326
327 return 0;
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +0200328}
329
Bartosz Golaszewskica4091602017-02-06 13:10:35 +0100330static int gpio_mockup_probe(struct platform_device *pdev)
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +0200331{
Bartosz Golaszewskica4091602017-02-06 13:10:35 +0100332 struct gpio_mockup_chip *chips;
Bartosz Golaszewski364ac452016-12-20 12:28:20 +0100333 struct device *dev = &pdev->dev;
334 int ret, i, base, ngpio;
Bartosz Golaszewskiad6d8002016-12-20 12:28:19 +0100335 char *chip_name;
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +0200336
337 if (gpio_mockup_params_nr < 2)
338 return -EINVAL;
339
Bartosz Golaszewskica4091602017-02-06 13:10:35 +0100340 chips = devm_kzalloc(dev,
341 sizeof(*chips) * (gpio_mockup_params_nr >> 1),
342 GFP_KERNEL);
343 if (!chips)
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +0200344 return -ENOMEM;
345
Bartosz Golaszewskica4091602017-02-06 13:10:35 +0100346 platform_set_drvdata(pdev, chips);
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +0200347
348 for (i = 0; i < gpio_mockup_params_nr >> 1; i++) {
349 base = gpio_mockup_ranges[i * 2];
Bartosz Golaszewskica4091602017-02-06 13:10:35 +0100350
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +0200351 if (base == -1)
352 ngpio = gpio_mockup_ranges[i * 2 + 1];
353 else
354 ngpio = gpio_mockup_ranges[i * 2 + 1] - base;
355
356 if (ngpio >= 0) {
Bartosz Golaszewskiad6d8002016-12-20 12:28:19 +0100357 chip_name = devm_kasprintf(dev, GFP_KERNEL,
Bartosz Golaszewskica4091602017-02-06 13:10:35 +0100358 "%s-%c", GPIO_MOCKUP_NAME,
359 gpio_mockup_name_start + i);
Bartosz Golaszewskiad6d8002016-12-20 12:28:19 +0100360 if (!chip_name)
361 return -ENOMEM;
362
Bartosz Golaszewskica4091602017-02-06 13:10:35 +0100363 ret = gpio_mockup_add(dev, &chips[i],
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +0200364 chip_name, base, ngpio);
365 } else {
366 ret = -1;
367 }
Bartosz Golaszewskica4091602017-02-06 13:10:35 +0100368
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +0200369 if (ret) {
Bartosz Golaszewskie4ba07b2017-02-06 13:10:36 +0100370 dev_err(dev, "gpio<%d..%d> add failed\n",
371 base, base < 0 ? ngpio : base + ngpio);
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +0200372
373 return ret;
374 }
375 }
376
377 return 0;
378}
379
Bartosz Golaszewskica4091602017-02-06 13:10:35 +0100380static struct platform_driver gpio_mockup_driver = {
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +0200381 .driver = {
Bartosz Golaszewskica4091602017-02-06 13:10:35 +0100382 .name = GPIO_MOCKUP_NAME,
Bartosz Golaszewski364ac452016-12-20 12:28:20 +0100383 },
Bartosz Golaszewskica4091602017-02-06 13:10:35 +0100384 .probe = gpio_mockup_probe,
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +0200385};
386
387static struct platform_device *pdev;
388static int __init mock_device_init(void)
389{
390 int err;
391
Bartosz Golaszewski9202ba22017-02-06 15:11:08 +0100392 gpio_mockup_dbg_dir = debugfs_create_dir("gpio-mockup-event", NULL);
393 if (!gpio_mockup_dbg_dir)
394 pr_err("%s: error creating debugfs directory\n",
395 GPIO_MOCKUP_NAME);
396
Bartosz Golaszewskica4091602017-02-06 13:10:35 +0100397 pdev = platform_device_alloc(GPIO_MOCKUP_NAME, -1);
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +0200398 if (!pdev)
399 return -ENOMEM;
400
401 err = platform_device_add(pdev);
402 if (err) {
403 platform_device_put(pdev);
404 return err;
405 }
406
Bartosz Golaszewskica4091602017-02-06 13:10:35 +0100407 err = platform_driver_register(&gpio_mockup_driver);
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +0200408 if (err) {
409 platform_device_unregister(pdev);
410 return err;
411 }
412
413 return 0;
414}
415
416static void __exit mock_device_exit(void)
417{
Bartosz Golaszewski9202ba22017-02-06 15:11:08 +0100418 debugfs_remove_recursive(gpio_mockup_dbg_dir);
Bartosz Golaszewskica4091602017-02-06 13:10:35 +0100419 platform_driver_unregister(&gpio_mockup_driver);
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +0200420 platform_device_unregister(pdev);
421}
422
423module_init(mock_device_init);
424module_exit(mock_device_exit);
425
426MODULE_AUTHOR("Kamlakant Patel <kamlakant.patel@broadcom.com>");
427MODULE_AUTHOR("Bamvor Jian Zhang <bamvor.zhangjian@linaro.org>");
428MODULE_DESCRIPTION("GPIO Testing driver");
429MODULE_LICENSE("GPL v2");