blob: f1a9c0544e3f7f5fc63c8f87298c5630c3254e4b [file] [log] [blame]
Bartosz Golaszewski058de342018-04-26 22:12:36 +02001// SPDX-License-Identifier: GPL-2.0+
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +02002/*
3 * GPIO Testing Device Driver
4 *
5 * Copyright (C) 2014 Kamlakant Patel <kamlakant.patel@broadcom.com>
Linus Walleij4f9a4cd2018-03-06 09:04:15 +01006 * Copyright (C) 2015-2016 Bamvor Jian Zhang <bamv2005@gmail.com>
Bartosz Golaszewskic60c7f42017-06-09 13:41:31 +02007 * Copyright (C) 2017 Bartosz Golaszewski <brgl@bgdev.pl>
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +02008 */
9
10#include <linux/init.h>
11#include <linux/module.h>
12#include <linux/gpio/driver.h>
Bartosz Golaszewski9202ba22017-02-06 15:11:08 +010013#include <linux/gpio/consumer.h>
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +020014#include <linux/platform_device.h>
Bartosz Golaszewski8a68ea02017-02-06 13:10:37 +010015#include <linux/slab.h>
Bartosz Golaszewskie2ff7402017-02-06 15:11:07 +010016#include <linux/interrupt.h>
17#include <linux/irq.h>
Bartosz Golaszewskib4c495f2017-08-14 13:20:56 +020018#include <linux/irq_sim.h>
Bartosz Golaszewski9202ba22017-02-06 15:11:08 +010019#include <linux/debugfs.h>
20#include <linux/uaccess.h>
Bartosz Golaszewski3ea47b42018-09-25 13:29:46 +020021#include <linux/property.h>
Bartosz Golaszewski9202ba22017-02-06 15:11:08 +010022
23#include "gpiolib.h"
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +020024
Bartosz Golaszewskica4091602017-02-06 13:10:35 +010025#define GPIO_MOCKUP_NAME "gpio-mockup"
Bartosz Golaszewskib9576d02017-11-27 11:48:42 +010026#define GPIO_MOCKUP_MAX_GC 10
Bartosz Golaszewskib6523362017-06-09 13:41:28 +020027/*
28 * We're storing two values per chip: the GPIO base and the number
29 * of GPIO lines.
30 */
31#define GPIO_MOCKUP_MAX_RANGES (GPIO_MOCKUP_MAX_GC * 2)
Bartosz Golaszewski3ea47b42018-09-25 13:29:46 +020032/* Maximum of three properties + the sentinel. */
33#define GPIO_MOCKUP_MAX_PROP 4
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +020034
Bartosz Golaszewskif1bec992017-11-27 11:48:41 +010035#define gpio_mockup_err(...) pr_err(GPIO_MOCKUP_NAME ": " __VA_ARGS__)
36
Bartosz Golaszewskica4091602017-02-06 13:10:35 +010037enum {
Bartosz Golaszewskibff466b2018-11-08 17:52:53 +010038 GPIO_MOCKUP_DIR_IN = 0,
39 GPIO_MOCKUP_DIR_OUT = 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;
Bartosz Golaszewski5b7908c2017-11-27 11:48:46 +010049 int value;
Bartosz Golaszewski2a9e2742019-01-17 16:30:27 +010050 int pull;
Bartosz Golaszewskie2ff7402017-02-06 15:11:07 +010051};
52
Bartosz Golaszewskica4091602017-02-06 13:10:35 +010053struct gpio_mockup_chip {
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +020054 struct gpio_chip gc;
Bartosz Golaszewskica4091602017-02-06 13:10:35 +010055 struct gpio_mockup_line_status *lines;
Bartosz Golaszewskib4c495f2017-08-14 13:20:56 +020056 struct irq_sim irqsim;
Bartosz Golaszewski9202ba22017-02-06 15:11:08 +010057 struct dentry *dbg_dir;
Bartosz Golaszewski92124922018-10-31 14:55:47 +010058 struct mutex lock;
Bartosz Golaszewski9202ba22017-02-06 15:11:08 +010059};
60
61struct gpio_mockup_dbgfs_private {
62 struct gpio_mockup_chip *chip;
63 struct gpio_desc *desc;
Bartosz Golaszewski833366682019-01-18 18:08:59 +010064 unsigned int offset;
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +020065};
66
Bartosz Golaszewskib6523362017-06-09 13:41:28 +020067static int gpio_mockup_ranges[GPIO_MOCKUP_MAX_RANGES];
Bartosz Golaszewskie63a0062017-11-27 11:48:53 +010068static int gpio_mockup_num_ranges;
69module_param_array(gpio_mockup_ranges, int, &gpio_mockup_num_ranges, 0400);
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +020070
Bartosz Golaszewski8a68ea02017-02-06 13:10:37 +010071static bool gpio_mockup_named_lines;
72module_param_named(gpio_mockup_named_lines,
73 gpio_mockup_named_lines, bool, 0400);
74
Bartosz Golaszewski9202ba22017-02-06 15:11:08 +010075static struct dentry *gpio_mockup_dbg_dir;
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +020076
Bartosz Golaszewskicd9835f2017-11-27 11:48:54 +010077static int gpio_mockup_range_base(unsigned int index)
78{
79 return gpio_mockup_ranges[index * 2];
80}
81
82static int gpio_mockup_range_ngpio(unsigned int index)
83{
84 return gpio_mockup_ranges[index * 2 + 1];
85}
86
Bartosz Golaszewskie09313c2019-01-23 09:34:15 +010087static int __gpio_mockup_get(struct gpio_mockup_chip *chip,
88 unsigned int offset)
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +020089{
Bartosz Golaszewskica4091602017-02-06 13:10:35 +010090 return chip->lines[offset].value;
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +020091}
92
Bartosz Golaszewski92124922018-10-31 14:55:47 +010093static int gpio_mockup_get(struct gpio_chip *gc, unsigned int offset)
94{
95 struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
96 int val;
97
98 mutex_lock(&chip->lock);
Bartosz Golaszewskie09313c2019-01-23 09:34:15 +010099 val = __gpio_mockup_get(chip, offset);
Bartosz Golaszewski92124922018-10-31 14:55:47 +0100100 mutex_unlock(&chip->lock);
101
102 return val;
103}
104
Bartosz Golaszewskicbf1e092018-10-31 15:01:40 +0100105static int gpio_mockup_get_multiple(struct gpio_chip *gc,
106 unsigned long *mask, unsigned long *bits)
107{
108 struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
109 unsigned int bit, val;
110
111 mutex_lock(&chip->lock);
112 for_each_set_bit(bit, mask, gc->ngpio) {
Bartosz Golaszewskie09313c2019-01-23 09:34:15 +0100113 val = __gpio_mockup_get(chip, bit);
Bartosz Golaszewskicbf1e092018-10-31 15:01:40 +0100114 __assign_bit(bit, bits, val);
115 }
116 mutex_unlock(&chip->lock);
117
118 return 0;
119}
120
Bartosz Golaszewskie09313c2019-01-23 09:34:15 +0100121static void __gpio_mockup_set(struct gpio_mockup_chip *chip,
Bartosz Golaszewski92124922018-10-31 14:55:47 +0100122 unsigned int offset, int value)
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +0200123{
Bartosz Golaszewskica4091602017-02-06 13:10:35 +0100124 chip->lines[offset].value = !!value;
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +0200125}
126
Bartosz Golaszewski92124922018-10-31 14:55:47 +0100127static void gpio_mockup_set(struct gpio_chip *gc,
128 unsigned int offset, int value)
129{
130 struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
131
132 mutex_lock(&chip->lock);
Bartosz Golaszewskie09313c2019-01-23 09:34:15 +0100133 __gpio_mockup_set(chip, offset, value);
Bartosz Golaszewski92124922018-10-31 14:55:47 +0100134 mutex_unlock(&chip->lock);
135}
136
Bartosz Golaszewskic47bee92017-11-27 11:48:51 +0100137static void gpio_mockup_set_multiple(struct gpio_chip *gc,
138 unsigned long *mask, unsigned long *bits)
139{
Bartosz Golaszewski92124922018-10-31 14:55:47 +0100140 struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
Bartosz Golaszewskic47bee92017-11-27 11:48:51 +0100141 unsigned int bit;
142
Bartosz Golaszewski92124922018-10-31 14:55:47 +0100143 mutex_lock(&chip->lock);
Bartosz Golaszewskic47bee92017-11-27 11:48:51 +0100144 for_each_set_bit(bit, mask, gc->ngpio)
Bartosz Golaszewskie09313c2019-01-23 09:34:15 +0100145 __gpio_mockup_set(chip, bit, test_bit(bit, bits));
Bartosz Golaszewski92124922018-10-31 14:55:47 +0100146 mutex_unlock(&chip->lock);
Bartosz Golaszewskic47bee92017-11-27 11:48:51 +0100147}
148
Bartosz Golaszewski2a9d7422017-11-27 11:48:50 +0100149static int gpio_mockup_dirout(struct gpio_chip *gc,
150 unsigned int offset, int value)
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +0200151{
Bartosz Golaszewskica4091602017-02-06 13:10:35 +0100152 struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +0200153
Bartosz Golaszewski92124922018-10-31 14:55:47 +0100154 mutex_lock(&chip->lock);
Bartosz Golaszewskic650c002017-05-25 10:33:39 +0200155 chip->lines[offset].dir = GPIO_MOCKUP_DIR_OUT;
Bartosz Golaszewskie09313c2019-01-23 09:34:15 +0100156 __gpio_mockup_set(chip, offset, value);
Bartosz Golaszewski92124922018-10-31 14:55:47 +0100157 mutex_unlock(&chip->lock);
Bartosz Golaszewskica4091602017-02-06 13:10:35 +0100158
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +0200159 return 0;
160}
161
Bartosz Golaszewskica4091602017-02-06 13:10:35 +0100162static int gpio_mockup_dirin(struct gpio_chip *gc, unsigned int offset)
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +0200163{
Bartosz Golaszewskica4091602017-02-06 13:10:35 +0100164 struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +0200165
Bartosz Golaszewski92124922018-10-31 14:55:47 +0100166 mutex_lock(&chip->lock);
Bartosz Golaszewskic650c002017-05-25 10:33:39 +0200167 chip->lines[offset].dir = GPIO_MOCKUP_DIR_IN;
Bartosz Golaszewski92124922018-10-31 14:55:47 +0100168 mutex_unlock(&chip->lock);
Bartosz Golaszewskica4091602017-02-06 13:10:35 +0100169
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +0200170 return 0;
171}
172
Bartosz Golaszewskica4091602017-02-06 13:10:35 +0100173static int gpio_mockup_get_direction(struct gpio_chip *gc, unsigned int offset)
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +0200174{
Bartosz Golaszewskica4091602017-02-06 13:10:35 +0100175 struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
Bartosz Golaszewski92124922018-10-31 14:55:47 +0100176 int direction;
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +0200177
Bartosz Golaszewski92124922018-10-31 14:55:47 +0100178 mutex_lock(&chip->lock);
179 direction = !chip->lines[offset].dir;
180 mutex_unlock(&chip->lock);
181
182 return direction;
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +0200183}
184
Bartosz Golaszewskib4c495f2017-08-14 13:20:56 +0200185static int gpio_mockup_to_irq(struct gpio_chip *gc, unsigned int offset)
Bartosz Golaszewskie2ff7402017-02-06 15:11:07 +0100186{
Bartosz Golaszewskic7f53262017-05-25 10:33:41 +0200187 struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
188
Bartosz Golaszewskib4c495f2017-08-14 13:20:56 +0200189 return irq_sim_irqnum(&chip->irqsim, offset);
Bartosz Golaszewskie2ff7402017-02-06 15:11:07 +0100190}
191
Bartosz Golaszewski2a9e2742019-01-17 16:30:27 +0100192static void gpio_mockup_free(struct gpio_chip *gc, unsigned int offset)
193{
194 struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
195
196 __gpio_mockup_set(chip, offset, chip->lines[offset].pull);
197}
198
199static ssize_t gpio_mockup_debugfs_read(struct file *file,
200 char __user *usr_buf,
201 size_t size, loff_t *ppos)
Bartosz Golaszewski9202ba22017-02-06 15:11:08 +0100202{
203 struct gpio_mockup_dbgfs_private *priv;
204 struct gpio_mockup_chip *chip;
205 struct seq_file *sfile;
Bartosz Golaszewski2a9e2742019-01-17 16:30:27 +0100206 struct gpio_chip *gc;
Bartosz Golaszewskice9fb532019-03-28 11:38:06 +0100207 int val, cnt;
Bartosz Golaszewski2a9e2742019-01-17 16:30:27 +0100208 char buf[3];
Bartosz Golaszewski25833032019-03-22 18:27:12 +0100209
Bartosz Golaszewski2a9e2742019-01-17 16:30:27 +0100210 if (*ppos != 0)
211 return 0;
212
213 sfile = file->private_data;
214 priv = sfile->private;
215 chip = priv->chip;
216 gc = &chip->gc;
217
218 val = gpio_mockup_get(gc, priv->offset);
Bartosz Golaszewski25833032019-03-22 18:27:12 +0100219 cnt = snprintf(buf, sizeof(buf), "%d\n", val);
Bartosz Golaszewski2a9e2742019-01-17 16:30:27 +0100220
Bartosz Golaszewskice9fb532019-03-28 11:38:06 +0100221 return simple_read_from_buffer(usr_buf, size, ppos, buf, cnt);
Bartosz Golaszewski2a9e2742019-01-17 16:30:27 +0100222}
223
224static ssize_t gpio_mockup_debugfs_write(struct file *file,
225 const char __user *usr_buf,
226 size_t size, loff_t *ppos)
227{
228 struct gpio_mockup_dbgfs_private *priv;
229 int rv, val, curr, irq, irq_type;
230 struct gpio_mockup_chip *chip;
231 struct seq_file *sfile;
Bartosz Golaszewski9202ba22017-02-06 15:11:08 +0100232 struct gpio_desc *desc;
Bartosz Golaszewski2a9e2742019-01-17 16:30:27 +0100233 struct gpio_chip *gc;
234 struct irq_sim *sim;
235
236 if (*ppos != 0)
237 return -EINVAL;
Bartosz Golaszewski9202ba22017-02-06 15:11:08 +0100238
Bartosz Golaszewski6f9b3e72017-06-09 13:41:25 +0200239 rv = kstrtoint_from_user(usr_buf, size, 0, &val);
240 if (rv)
241 return rv;
242 if (val != 0 && val != 1)
243 return -EINVAL;
244
Bartosz Golaszewski650b57b2017-06-09 13:41:26 +0200245 sfile = file->private_data;
246 priv = sfile->private;
Bartosz Golaszewski650b57b2017-06-09 13:41:26 +0200247 chip = priv->chip;
Bartosz Golaszewski2a9e2742019-01-17 16:30:27 +0100248 gc = &chip->gc;
249 desc = &gc->gpiodev->descs[priv->offset];
250 sim = &chip->irqsim;
Bartosz Golaszewskic7f53262017-05-25 10:33:41 +0200251
Bartosz Golaszewski2a9e2742019-01-17 16:30:27 +0100252 mutex_lock(&chip->lock);
253
254 if (test_bit(FLAG_REQUESTED, &desc->flags) &&
255 !test_bit(FLAG_IS_OUT, &desc->flags)) {
256 curr = __gpio_mockup_get(chip, priv->offset);
257 if (curr == val)
258 goto out;
259
260 irq = irq_sim_irqnum(sim, priv->offset);
261 irq_type = irq_get_trigger_type(irq);
262
263 if ((val == 1 && (irq_type & IRQ_TYPE_EDGE_RISING)) ||
264 (val == 0 && (irq_type & IRQ_TYPE_EDGE_FALLING)))
265 irq_sim_fire(sim, priv->offset);
266 }
267
268 /* Change the value unless we're actively driving the line. */
269 if (!test_bit(FLAG_REQUESTED, &desc->flags) ||
270 !test_bit(FLAG_IS_OUT, &desc->flags))
271 __gpio_mockup_set(chip, priv->offset, val);
272
273out:
274 chip->lines[priv->offset].pull = val;
275 mutex_unlock(&chip->lock);
Bartosz Golaszewski9202ba22017-02-06 15:11:08 +0100276
277 return size;
278}
279
Bartosz Golaszewski2a9e2742019-01-17 16:30:27 +0100280static int gpio_mockup_debugfs_open(struct inode *inode, struct file *file)
Bartosz Golaszewski9202ba22017-02-06 15:11:08 +0100281{
282 return single_open(file, NULL, inode->i_private);
283}
284
Bartosz Golaszewski2a9e2742019-01-17 16:30:27 +0100285/*
286 * Each mockup chip is represented by a directory named after the chip's device
287 * name under /sys/kernel/debug/gpio-mockup/. Each line is represented by
288 * a file using the line's offset as the name under the chip's directory.
289 *
290 * Reading from the line's file yields the current *value*, writing to the
291 * line's file changes the current *pull*. Default pull for mockup lines is
292 * down.
293 *
294 * Examples:
295 * - when a line pulled down is requested in output mode and driven high, its
296 * value will return to 0 once it's released
297 * - when the line is requested in output mode and driven high, writing 0 to
298 * the corresponding debugfs file will change the pull to down but the
299 * reported value will still be 1 until the line is released
300 * - line requested in input mode always reports the same value as its pull
301 * configuration
302 * - when the line is requested in input mode and monitored for events, writing
303 * the same value to the debugfs file will be a noop, while writing the
304 * opposite value will generate a dummy interrupt with an appropriate edge
305 */
306static const struct file_operations gpio_mockup_debugfs_ops = {
Bartosz Golaszewski9202ba22017-02-06 15:11:08 +0100307 .owner = THIS_MODULE,
Bartosz Golaszewski2a9e2742019-01-17 16:30:27 +0100308 .open = gpio_mockup_debugfs_open,
309 .read = gpio_mockup_debugfs_read,
310 .write = gpio_mockup_debugfs_write,
Bartosz Golaszewski9202ba22017-02-06 15:11:08 +0100311 .llseek = no_llseek,
312};
313
314static void gpio_mockup_debugfs_setup(struct device *dev,
315 struct gpio_mockup_chip *chip)
316{
317 struct gpio_mockup_dbgfs_private *priv;
Bartosz Golaszewski9202ba22017-02-06 15:11:08 +0100318 struct gpio_chip *gc;
Bartosz Golaszewskica8792a2017-11-27 11:48:45 +0100319 const char *devname;
Bartosz Golaszewski9202ba22017-02-06 15:11:08 +0100320 char *name;
321 int i;
322
323 gc = &chip->gc;
Bartosz Golaszewskica8792a2017-11-27 11:48:45 +0100324 devname = dev_name(&gc->gpiodev->dev);
Bartosz Golaszewski9202ba22017-02-06 15:11:08 +0100325
Bartosz Golaszewskica8792a2017-11-27 11:48:45 +0100326 chip->dbg_dir = debugfs_create_dir(devname, gpio_mockup_dbg_dir);
Bartosz Golaszewski9202ba22017-02-06 15:11:08 +0100327
328 for (i = 0; i < gc->ngpio; i++) {
329 name = devm_kasprintf(dev, GFP_KERNEL, "%d", i);
330 if (!name)
Greg Kroah-Hartmanf360dcd2019-06-26 10:45:57 +0200331 return;
Bartosz Golaszewski9202ba22017-02-06 15:11:08 +0100332
333 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
334 if (!priv)
Greg Kroah-Hartmanf360dcd2019-06-26 10:45:57 +0200335 return;
Bartosz Golaszewski9202ba22017-02-06 15:11:08 +0100336
337 priv->chip = chip;
338 priv->offset = i;
339 priv->desc = &gc->gpiodev->descs[i];
340
Greg Kroah-Hartmanf360dcd2019-06-26 10:45:57 +0200341 debugfs_create_file(name, 0200, chip->dbg_dir, priv,
342 &gpio_mockup_debugfs_ops);
Bartosz Golaszewski9202ba22017-02-06 15:11:08 +0100343 }
344
345 return;
Bartosz Golaszewski9202ba22017-02-06 15:11:08 +0100346}
347
Bartosz Golaszewski1cea4732017-11-27 11:48:47 +0100348static int gpio_mockup_name_lines(struct device *dev,
349 struct gpio_mockup_chip *chip)
350{
351 struct gpio_chip *gc = &chip->gc;
352 char **names;
353 int i;
354
355 names = devm_kcalloc(dev, gc->ngpio, sizeof(char *), GFP_KERNEL);
356 if (!names)
357 return -ENOMEM;
358
359 for (i = 0; i < gc->ngpio; i++) {
360 names[i] = devm_kasprintf(dev, GFP_KERNEL,
361 "%s-%d", gc->label, i);
362 if (!names[i])
363 return -ENOMEM;
364 }
365
366 gc->names = (const char *const *)names;
367
368 return 0;
369}
370
Bartosz Golaszewskica4091602017-02-06 13:10:35 +0100371static int gpio_mockup_probe(struct platform_device *pdev)
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +0200372{
Bartosz Golaszewski8a39f592017-11-27 11:48:40 +0100373 struct gpio_mockup_chip *chip;
Bartosz Golaszewski6d974d32017-11-27 11:48:43 +0100374 struct gpio_chip *gc;
Bartosz Golaszewski8a39f592017-11-27 11:48:40 +0100375 struct device *dev;
Bartosz Golaszewski3ea47b42018-09-25 13:29:46 +0200376 const char *name;
377 int rv, base;
378 u16 ngpio;
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +0200379
Bartosz Golaszewski8a39f592017-11-27 11:48:40 +0100380 dev = &pdev->dev;
Bartosz Golaszewski3ea47b42018-09-25 13:29:46 +0200381
382 rv = device_property_read_u32(dev, "gpio-base", &base);
383 if (rv)
384 base = -1;
385
386 rv = device_property_read_u16(dev, "nr-gpios", &ngpio);
387 if (rv)
388 return rv;
389
390 rv = device_property_read_string(dev, "chip-name", &name);
391 if (rv)
392 name = NULL;
Bartosz Golaszewskib6523362017-06-09 13:41:28 +0200393
Bartosz Golaszewski8a39f592017-11-27 11:48:40 +0100394 chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL);
395 if (!chip)
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +0200396 return -ENOMEM;
397
Bartosz Golaszewski3ea47b42018-09-25 13:29:46 +0200398 if (!name) {
399 name = devm_kasprintf(dev, GFP_KERNEL,
400 "%s-%c", pdev->name, pdev->id + 'A');
401 if (!name)
402 return -ENOMEM;
403 }
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +0200404
Bartosz Golaszewski92124922018-10-31 14:55:47 +0100405 mutex_init(&chip->lock);
406
Bartosz Golaszewski6d974d32017-11-27 11:48:43 +0100407 gc = &chip->gc;
408 gc->base = base;
409 gc->ngpio = ngpio;
410 gc->label = name;
411 gc->owner = THIS_MODULE;
412 gc->parent = dev;
413 gc->get = gpio_mockup_get;
414 gc->set = gpio_mockup_set;
Bartosz Golaszewskicbf1e092018-10-31 15:01:40 +0100415 gc->get_multiple = gpio_mockup_get_multiple;
Bartosz Golaszewskic47bee92017-11-27 11:48:51 +0100416 gc->set_multiple = gpio_mockup_set_multiple;
Bartosz Golaszewski6d974d32017-11-27 11:48:43 +0100417 gc->direction_output = gpio_mockup_dirout;
418 gc->direction_input = gpio_mockup_dirin;
419 gc->get_direction = gpio_mockup_get_direction;
420 gc->to_irq = gpio_mockup_to_irq;
Bartosz Golaszewski2a9e2742019-01-17 16:30:27 +0100421 gc->free = gpio_mockup_free;
Bartosz Golaszewski6d974d32017-11-27 11:48:43 +0100422
423 chip->lines = devm_kcalloc(dev, gc->ngpio,
424 sizeof(*chip->lines), GFP_KERNEL);
425 if (!chip->lines)
426 return -ENOMEM;
427
Bartosz Golaszewski3ea47b42018-09-25 13:29:46 +0200428 if (device_property_read_bool(dev, "named-gpio-lines")) {
Bartosz Golaszewski6d974d32017-11-27 11:48:43 +0100429 rv = gpio_mockup_name_lines(dev, chip);
430 if (rv)
431 return rv;
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +0200432 }
433
Bartosz Golaszewski6d974d32017-11-27 11:48:43 +0100434 rv = devm_irq_sim_init(dev, &chip->irqsim, gc->ngpio);
Bartosz Golaszewski1e0dca62017-11-27 11:48:52 +0100435 if (rv < 0)
Bartosz Golaszewski6d974d32017-11-27 11:48:43 +0100436 return rv;
437
438 rv = devm_gpiochip_add_data(dev, &chip->gc, chip);
439 if (rv)
440 return rv;
441
Greg Kroah-Hartmanf360dcd2019-06-26 10:45:57 +0200442 gpio_mockup_debugfs_setup(dev, chip);
Bartosz Golaszewski6d974d32017-11-27 11:48:43 +0100443
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +0200444 return 0;
445}
446
Bartosz Golaszewskica4091602017-02-06 13:10:35 +0100447static struct platform_driver gpio_mockup_driver = {
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +0200448 .driver = {
Bartosz Golaszewskica4091602017-02-06 13:10:35 +0100449 .name = GPIO_MOCKUP_NAME,
Bartosz Golaszewski364ac452016-12-20 12:28:20 +0100450 },
Bartosz Golaszewskica4091602017-02-06 13:10:35 +0100451 .probe = gpio_mockup_probe,
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +0200452};
453
Bartosz Golaszewski8a39f592017-11-27 11:48:40 +0100454static struct platform_device *gpio_mockup_pdevs[GPIO_MOCKUP_MAX_GC];
455
456static void gpio_mockup_unregister_pdevs(void)
457{
458 struct platform_device *pdev;
459 int i;
460
461 for (i = 0; i < GPIO_MOCKUP_MAX_GC; i++) {
462 pdev = gpio_mockup_pdevs[i];
463
464 if (pdev)
465 platform_device_unregister(pdev);
466 }
467}
Bartosz Golaszewskif3b47172017-11-27 11:48:37 +0100468
469static int __init gpio_mockup_init(void)
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +0200470{
Bartosz Golaszewski3ea47b42018-09-25 13:29:46 +0200471 struct property_entry properties[GPIO_MOCKUP_MAX_PROP];
472 int i, prop, num_chips, err = 0, base;
473 struct platform_device_info pdevinfo;
Bartosz Golaszewski8a39f592017-11-27 11:48:40 +0100474 struct platform_device *pdev;
Bartosz Golaszewski3ea47b42018-09-25 13:29:46 +0200475 u16 ngpio;
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +0200476
Bartosz Golaszewskie63a0062017-11-27 11:48:53 +0100477 if ((gpio_mockup_num_ranges < 2) ||
478 (gpio_mockup_num_ranges % 2) ||
479 (gpio_mockup_num_ranges > GPIO_MOCKUP_MAX_RANGES))
Bartosz Golaszewski20c35ac2017-11-27 11:48:38 +0100480 return -EINVAL;
481
Bartosz Golaszewski8a39f592017-11-27 11:48:40 +0100482 /* Each chip is described by two values. */
Bartosz Golaszewskie63a0062017-11-27 11:48:53 +0100483 num_chips = gpio_mockup_num_ranges / 2;
Bartosz Golaszewski8a39f592017-11-27 11:48:40 +0100484
Bartosz Golaszewskifa869632017-11-27 11:48:49 +0100485 /*
486 * The second value in the <base GPIO - number of GPIOS> pair must
487 * always be greater than 0.
488 */
489 for (i = 0; i < num_chips; i++) {
Bartosz Golaszewskicd9835f2017-11-27 11:48:54 +0100490 if (gpio_mockup_range_ngpio(i) < 0)
Bartosz Golaszewskifa869632017-11-27 11:48:49 +0100491 return -EINVAL;
492 }
493
Bartosz Golaszewski2a9e2742019-01-17 16:30:27 +0100494 gpio_mockup_dbg_dir = debugfs_create_dir("gpio-mockup", NULL);
Bartosz Golaszewski9202ba22017-02-06 15:11:08 +0100495
Bartosz Golaszewski8a39f592017-11-27 11:48:40 +0100496 err = platform_driver_register(&gpio_mockup_driver);
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +0200497 if (err) {
Bartosz Golaszewskif1bec992017-11-27 11:48:41 +0100498 gpio_mockup_err("error registering platform driver\n");
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +0200499 return err;
500 }
501
Bartosz Golaszewski8a39f592017-11-27 11:48:40 +0100502 for (i = 0; i < num_chips; i++) {
Bartosz Golaszewski3ea47b42018-09-25 13:29:46 +0200503 memset(properties, 0, sizeof(properties));
504 memset(&pdevinfo, 0, sizeof(pdevinfo));
505 prop = 0;
Bartosz Golaszewski8a39f592017-11-27 11:48:40 +0100506
Bartosz Golaszewski3ea47b42018-09-25 13:29:46 +0200507 base = gpio_mockup_range_base(i);
508 if (base >= 0)
509 properties[prop++] = PROPERTY_ENTRY_U32("gpio-base",
510 base);
511
512 ngpio = base < 0 ? gpio_mockup_range_ngpio(i)
513 : gpio_mockup_range_ngpio(i) - base;
514 properties[prop++] = PROPERTY_ENTRY_U16("nr-gpios", ngpio);
515
516 if (gpio_mockup_named_lines)
517 properties[prop++] = PROPERTY_ENTRY_BOOL(
518 "named-gpio-lines");
519
520 pdevinfo.name = GPIO_MOCKUP_NAME;
521 pdevinfo.id = i;
522 pdevinfo.properties = properties;
523
524 pdev = platform_device_register_full(&pdevinfo);
Bartosz Golaszewskic4b54e12017-12-06 17:30:02 +0100525 if (IS_ERR(pdev)) {
Bartosz Golaszewskif1bec992017-11-27 11:48:41 +0100526 gpio_mockup_err("error registering device");
Bartosz Golaszewski8a39f592017-11-27 11:48:40 +0100527 platform_driver_unregister(&gpio_mockup_driver);
528 gpio_mockup_unregister_pdevs();
Bartosz Golaszewskic4b54e12017-12-06 17:30:02 +0100529 return PTR_ERR(pdev);
Bartosz Golaszewski8a39f592017-11-27 11:48:40 +0100530 }
531
532 gpio_mockup_pdevs[i] = pdev;
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +0200533 }
534
535 return 0;
536}
537
Bartosz Golaszewskif3b47172017-11-27 11:48:37 +0100538static void __exit gpio_mockup_exit(void)
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +0200539{
Bartosz Golaszewski9202ba22017-02-06 15:11:08 +0100540 debugfs_remove_recursive(gpio_mockup_dbg_dir);
Bartosz Golaszewskica4091602017-02-06 13:10:35 +0100541 platform_driver_unregister(&gpio_mockup_driver);
Bartosz Golaszewski8a39f592017-11-27 11:48:40 +0100542 gpio_mockup_unregister_pdevs();
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +0200543}
544
Bartosz Golaszewskif3b47172017-11-27 11:48:37 +0100545module_init(gpio_mockup_init);
546module_exit(gpio_mockup_exit);
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +0200547
548MODULE_AUTHOR("Kamlakant Patel <kamlakant.patel@broadcom.com>");
Linus Walleij4f9a4cd2018-03-06 09:04:15 +0100549MODULE_AUTHOR("Bamvor Jian Zhang <bamv2005@gmail.com>");
Bartosz Golaszewskic60c7f42017-06-09 13:41:31 +0200550MODULE_AUTHOR("Bartosz Golaszewski <brgl@bgdev.pl>");
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +0200551MODULE_DESCRIPTION("GPIO Testing driver");
552MODULE_LICENSE("GPL v2");