blob: 3eb94f3740d1423337c8175ad4d279df8155a370 [file] [log] [blame]
Bartosz Golaszewskif2f67982020-01-14 16:02:52 +01001// SPDX-License-Identifier: GPL-2.0-or-later
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
Bartosz Golaszewski726a4452020-01-14 16:02:53 +010010#include <linux/debugfs.h>
Bartosz Golaszewski9202ba22017-02-06 15:11:08 +010011#include <linux/gpio/consumer.h>
Bartosz Golaszewski726a4452020-01-14 16:02:53 +010012#include <linux/gpio/driver.h>
13#include <linux/init.h>
Bartosz Golaszewskie2ff7402017-02-06 15:11:07 +010014#include <linux/interrupt.h>
15#include <linux/irq.h>
Bartosz Golaszewskib4c495f2017-08-14 13:20:56 +020016#include <linux/irq_sim.h>
Bartosz Golaszewski726a4452020-01-14 16:02:53 +010017#include <linux/module.h>
18#include <linux/platform_device.h>
Bartosz Golaszewski3ea47b42018-09-25 13:29:46 +020019#include <linux/property.h>
Bartosz Golaszewski726a4452020-01-14 16:02:53 +010020#include <linux/slab.h>
21#include <linux/uaccess.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
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +020037/*
38 * struct gpio_pin_status - structure describing a GPIO status
Matti Vaittinene42615e2019-11-06 10:54:12 +020039 * @dir: Configures direction of gpio as "in" or "out"
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +020040 * @value: Configures status of the gpio as 0(low) or 1(high)
41 */
Bartosz Golaszewskica4091602017-02-06 13:10:35 +010042struct gpio_mockup_line_status {
43 int dir;
Bartosz Golaszewski5b7908c2017-11-27 11:48:46 +010044 int value;
Bartosz Golaszewski2a9e2742019-01-17 16:30:27 +010045 int pull;
Bartosz Golaszewskie2ff7402017-02-06 15:11:07 +010046};
47
Bartosz Golaszewskica4091602017-02-06 13:10:35 +010048struct gpio_mockup_chip {
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +020049 struct gpio_chip gc;
Bartosz Golaszewskica4091602017-02-06 13:10:35 +010050 struct gpio_mockup_line_status *lines;
Bartosz Golaszewskib4c495f2017-08-14 13:20:56 +020051 struct irq_sim irqsim;
Bartosz Golaszewski9202ba22017-02-06 15:11:08 +010052 struct dentry *dbg_dir;
Bartosz Golaszewski92124922018-10-31 14:55:47 +010053 struct mutex lock;
Bartosz Golaszewski9202ba22017-02-06 15:11:08 +010054};
55
56struct gpio_mockup_dbgfs_private {
57 struct gpio_mockup_chip *chip;
58 struct gpio_desc *desc;
Bartosz Golaszewski833366682019-01-18 18:08:59 +010059 unsigned int offset;
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +020060};
61
Bartosz Golaszewskib6523362017-06-09 13:41:28 +020062static int gpio_mockup_ranges[GPIO_MOCKUP_MAX_RANGES];
Bartosz Golaszewskie63a0062017-11-27 11:48:53 +010063static int gpio_mockup_num_ranges;
64module_param_array(gpio_mockup_ranges, int, &gpio_mockup_num_ranges, 0400);
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +020065
Bartosz Golaszewski8a68ea02017-02-06 13:10:37 +010066static bool gpio_mockup_named_lines;
67module_param_named(gpio_mockup_named_lines,
68 gpio_mockup_named_lines, bool, 0400);
69
Bartosz Golaszewski9202ba22017-02-06 15:11:08 +010070static struct dentry *gpio_mockup_dbg_dir;
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +020071
Bartosz Golaszewskicd9835f2017-11-27 11:48:54 +010072static int gpio_mockup_range_base(unsigned int index)
73{
74 return gpio_mockup_ranges[index * 2];
75}
76
77static int gpio_mockup_range_ngpio(unsigned int index)
78{
79 return gpio_mockup_ranges[index * 2 + 1];
80}
81
Bartosz Golaszewskie09313c2019-01-23 09:34:15 +010082static int __gpio_mockup_get(struct gpio_mockup_chip *chip,
83 unsigned int offset)
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +020084{
Bartosz Golaszewskica4091602017-02-06 13:10:35 +010085 return chip->lines[offset].value;
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +020086}
87
Bartosz Golaszewski92124922018-10-31 14:55:47 +010088static int gpio_mockup_get(struct gpio_chip *gc, unsigned int offset)
89{
90 struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
91 int val;
92
93 mutex_lock(&chip->lock);
Bartosz Golaszewskie09313c2019-01-23 09:34:15 +010094 val = __gpio_mockup_get(chip, offset);
Bartosz Golaszewski92124922018-10-31 14:55:47 +010095 mutex_unlock(&chip->lock);
96
97 return val;
98}
99
Bartosz Golaszewskicbf1e092018-10-31 15:01:40 +0100100static int gpio_mockup_get_multiple(struct gpio_chip *gc,
101 unsigned long *mask, unsigned long *bits)
102{
103 struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
104 unsigned int bit, val;
105
106 mutex_lock(&chip->lock);
107 for_each_set_bit(bit, mask, gc->ngpio) {
Bartosz Golaszewskie09313c2019-01-23 09:34:15 +0100108 val = __gpio_mockup_get(chip, bit);
Bartosz Golaszewskicbf1e092018-10-31 15:01:40 +0100109 __assign_bit(bit, bits, val);
110 }
111 mutex_unlock(&chip->lock);
112
113 return 0;
114}
115
Bartosz Golaszewskie09313c2019-01-23 09:34:15 +0100116static void __gpio_mockup_set(struct gpio_mockup_chip *chip,
Bartosz Golaszewski92124922018-10-31 14:55:47 +0100117 unsigned int offset, int value)
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +0200118{
Bartosz Golaszewskica4091602017-02-06 13:10:35 +0100119 chip->lines[offset].value = !!value;
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +0200120}
121
Bartosz Golaszewski92124922018-10-31 14:55:47 +0100122static void gpio_mockup_set(struct gpio_chip *gc,
123 unsigned int offset, int value)
124{
125 struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
126
127 mutex_lock(&chip->lock);
Bartosz Golaszewskie09313c2019-01-23 09:34:15 +0100128 __gpio_mockup_set(chip, offset, value);
Bartosz Golaszewski92124922018-10-31 14:55:47 +0100129 mutex_unlock(&chip->lock);
130}
131
Bartosz Golaszewskic47bee92017-11-27 11:48:51 +0100132static void gpio_mockup_set_multiple(struct gpio_chip *gc,
133 unsigned long *mask, unsigned long *bits)
134{
Bartosz Golaszewski92124922018-10-31 14:55:47 +0100135 struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
Bartosz Golaszewskic47bee92017-11-27 11:48:51 +0100136 unsigned int bit;
137
Bartosz Golaszewski92124922018-10-31 14:55:47 +0100138 mutex_lock(&chip->lock);
Bartosz Golaszewskic47bee92017-11-27 11:48:51 +0100139 for_each_set_bit(bit, mask, gc->ngpio)
Bartosz Golaszewskie09313c2019-01-23 09:34:15 +0100140 __gpio_mockup_set(chip, bit, test_bit(bit, bits));
Bartosz Golaszewski92124922018-10-31 14:55:47 +0100141 mutex_unlock(&chip->lock);
Bartosz Golaszewskic47bee92017-11-27 11:48:51 +0100142}
143
Kent Gibson64e71122019-11-05 10:04:27 +0800144static int gpio_mockup_apply_pull(struct gpio_mockup_chip *chip,
145 unsigned int offset, int value)
146{
147 struct gpio_desc *desc;
148 struct gpio_chip *gc;
149 struct irq_sim *sim;
150 int curr, irq, irq_type;
151
152 gc = &chip->gc;
153 desc = &gc->gpiodev->descs[offset];
154 sim = &chip->irqsim;
155
156 mutex_lock(&chip->lock);
157
158 if (test_bit(FLAG_REQUESTED, &desc->flags) &&
Bartosz Golaszewskiee8598a2019-12-16 17:37:10 +0100159 !test_bit(FLAG_IS_OUT, &desc->flags)) {
Kent Gibson64e71122019-11-05 10:04:27 +0800160 curr = __gpio_mockup_get(chip, offset);
161 if (curr == value)
162 goto out;
163
164 irq = irq_sim_irqnum(sim, offset);
165 irq_type = irq_get_trigger_type(irq);
166
167 if ((value == 1 && (irq_type & IRQ_TYPE_EDGE_RISING)) ||
Bartosz Golaszewskiee8598a2019-12-16 17:37:10 +0100168 (value == 0 && (irq_type & IRQ_TYPE_EDGE_FALLING)))
Kent Gibson64e71122019-11-05 10:04:27 +0800169 irq_sim_fire(sim, offset);
170 }
171
172 /* Change the value unless we're actively driving the line. */
173 if (!test_bit(FLAG_REQUESTED, &desc->flags) ||
Bartosz Golaszewskif6e51bb2020-02-10 16:50:59 +0100174 !test_bit(FLAG_IS_OUT, &desc->flags))
Kent Gibson64e71122019-11-05 10:04:27 +0800175 __gpio_mockup_set(chip, offset, value);
176
177out:
178 chip->lines[offset].pull = value;
179 mutex_unlock(&chip->lock);
180 return 0;
181}
182
183static int gpio_mockup_set_config(struct gpio_chip *gc,
184 unsigned int offset, unsigned long config)
185{
186 struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
187
188 switch (pinconf_to_config_param(config)) {
189 case PIN_CONFIG_BIAS_PULL_UP:
190 return gpio_mockup_apply_pull(chip, offset, 1);
191 case PIN_CONFIG_BIAS_PULL_DOWN:
192 return gpio_mockup_apply_pull(chip, offset, 0);
193 default:
194 break;
195 }
196 return -ENOTSUPP;
197}
198
Bartosz Golaszewski2a9d7422017-11-27 11:48:50 +0100199static int gpio_mockup_dirout(struct gpio_chip *gc,
200 unsigned int offset, int value)
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +0200201{
Bartosz Golaszewskica4091602017-02-06 13:10:35 +0100202 struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +0200203
Bartosz Golaszewski92124922018-10-31 14:55:47 +0100204 mutex_lock(&chip->lock);
Matti Vaittinene42615e2019-11-06 10:54:12 +0200205 chip->lines[offset].dir = GPIO_LINE_DIRECTION_OUT;
Bartosz Golaszewskie09313c2019-01-23 09:34:15 +0100206 __gpio_mockup_set(chip, offset, value);
Bartosz Golaszewski92124922018-10-31 14:55:47 +0100207 mutex_unlock(&chip->lock);
Bartosz Golaszewskica4091602017-02-06 13:10:35 +0100208
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +0200209 return 0;
210}
211
Bartosz Golaszewskica4091602017-02-06 13:10:35 +0100212static int gpio_mockup_dirin(struct gpio_chip *gc, unsigned int offset)
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +0200213{
Bartosz Golaszewskica4091602017-02-06 13:10:35 +0100214 struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +0200215
Bartosz Golaszewski92124922018-10-31 14:55:47 +0100216 mutex_lock(&chip->lock);
Matti Vaittinene42615e2019-11-06 10:54:12 +0200217 chip->lines[offset].dir = GPIO_LINE_DIRECTION_IN;
Bartosz Golaszewski92124922018-10-31 14:55:47 +0100218 mutex_unlock(&chip->lock);
Bartosz Golaszewskica4091602017-02-06 13:10:35 +0100219
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +0200220 return 0;
221}
222
Bartosz Golaszewskica4091602017-02-06 13:10:35 +0100223static int gpio_mockup_get_direction(struct gpio_chip *gc, unsigned int offset)
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +0200224{
Bartosz Golaszewskica4091602017-02-06 13:10:35 +0100225 struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
Bartosz Golaszewski92124922018-10-31 14:55:47 +0100226 int direction;
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +0200227
Bartosz Golaszewski92124922018-10-31 14:55:47 +0100228 mutex_lock(&chip->lock);
Kent Gibsonbc7bc682019-12-11 08:46:31 +0800229 direction = chip->lines[offset].dir;
Bartosz Golaszewski92124922018-10-31 14:55:47 +0100230 mutex_unlock(&chip->lock);
231
232 return direction;
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +0200233}
234
Bartosz Golaszewskib4c495f2017-08-14 13:20:56 +0200235static int gpio_mockup_to_irq(struct gpio_chip *gc, unsigned int offset)
Bartosz Golaszewskie2ff7402017-02-06 15:11:07 +0100236{
Bartosz Golaszewskic7f5326fb2017-05-25 10:33:41 +0200237 struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
238
Bartosz Golaszewskib4c495f2017-08-14 13:20:56 +0200239 return irq_sim_irqnum(&chip->irqsim, offset);
Bartosz Golaszewskie2ff7402017-02-06 15:11:07 +0100240}
241
Bartosz Golaszewski2a9e2742019-01-17 16:30:27 +0100242static void gpio_mockup_free(struct gpio_chip *gc, unsigned int offset)
243{
244 struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
245
246 __gpio_mockup_set(chip, offset, chip->lines[offset].pull);
247}
248
249static ssize_t gpio_mockup_debugfs_read(struct file *file,
250 char __user *usr_buf,
251 size_t size, loff_t *ppos)
Bartosz Golaszewski9202ba22017-02-06 15:11:08 +0100252{
253 struct gpio_mockup_dbgfs_private *priv;
254 struct gpio_mockup_chip *chip;
255 struct seq_file *sfile;
Bartosz Golaszewski2a9e2742019-01-17 16:30:27 +0100256 struct gpio_chip *gc;
Bartosz Golaszewskice9fb532019-03-28 11:38:06 +0100257 int val, cnt;
Bartosz Golaszewski2a9e2742019-01-17 16:30:27 +0100258 char buf[3];
Bartosz Golaszewski25833032019-03-22 18:27:12 +0100259
Bartosz Golaszewski2a9e2742019-01-17 16:30:27 +0100260 if (*ppos != 0)
261 return 0;
262
263 sfile = file->private_data;
264 priv = sfile->private;
265 chip = priv->chip;
266 gc = &chip->gc;
267
268 val = gpio_mockup_get(gc, priv->offset);
Bartosz Golaszewski25833032019-03-22 18:27:12 +0100269 cnt = snprintf(buf, sizeof(buf), "%d\n", val);
Bartosz Golaszewski2a9e2742019-01-17 16:30:27 +0100270
Bartosz Golaszewskice9fb532019-03-28 11:38:06 +0100271 return simple_read_from_buffer(usr_buf, size, ppos, buf, cnt);
Bartosz Golaszewski2a9e2742019-01-17 16:30:27 +0100272}
273
274static ssize_t gpio_mockup_debugfs_write(struct file *file,
275 const char __user *usr_buf,
276 size_t size, loff_t *ppos)
277{
278 struct gpio_mockup_dbgfs_private *priv;
Kent Gibson64e71122019-11-05 10:04:27 +0800279 int rv, val;
Bartosz Golaszewski2a9e2742019-01-17 16:30:27 +0100280 struct seq_file *sfile;
Bartosz Golaszewski2a9e2742019-01-17 16:30:27 +0100281
282 if (*ppos != 0)
283 return -EINVAL;
Bartosz Golaszewski9202ba22017-02-06 15:11:08 +0100284
Bartosz Golaszewski6f9b3e72017-06-09 13:41:25 +0200285 rv = kstrtoint_from_user(usr_buf, size, 0, &val);
286 if (rv)
287 return rv;
288 if (val != 0 && val != 1)
289 return -EINVAL;
290
Bartosz Golaszewski650b57b2017-06-09 13:41:26 +0200291 sfile = file->private_data;
292 priv = sfile->private;
Kent Gibson64e71122019-11-05 10:04:27 +0800293 rv = gpio_mockup_apply_pull(priv->chip, priv->offset, val);
294 if (rv)
295 return rv;
Bartosz Golaszewski9202ba22017-02-06 15:11:08 +0100296
297 return size;
298}
299
Bartosz Golaszewski2a9e2742019-01-17 16:30:27 +0100300static int gpio_mockup_debugfs_open(struct inode *inode, struct file *file)
Bartosz Golaszewski9202ba22017-02-06 15:11:08 +0100301{
302 return single_open(file, NULL, inode->i_private);
303}
304
Bartosz Golaszewski2a9e2742019-01-17 16:30:27 +0100305/*
306 * Each mockup chip is represented by a directory named after the chip's device
307 * name under /sys/kernel/debug/gpio-mockup/. Each line is represented by
308 * a file using the line's offset as the name under the chip's directory.
309 *
310 * Reading from the line's file yields the current *value*, writing to the
311 * line's file changes the current *pull*. Default pull for mockup lines is
312 * down.
313 *
314 * Examples:
315 * - when a line pulled down is requested in output mode and driven high, its
316 * value will return to 0 once it's released
317 * - when the line is requested in output mode and driven high, writing 0 to
318 * the corresponding debugfs file will change the pull to down but the
319 * reported value will still be 1 until the line is released
320 * - line requested in input mode always reports the same value as its pull
321 * configuration
322 * - when the line is requested in input mode and monitored for events, writing
323 * the same value to the debugfs file will be a noop, while writing the
324 * opposite value will generate a dummy interrupt with an appropriate edge
325 */
326static const struct file_operations gpio_mockup_debugfs_ops = {
Bartosz Golaszewski9202ba22017-02-06 15:11:08 +0100327 .owner = THIS_MODULE,
Bartosz Golaszewski2a9e2742019-01-17 16:30:27 +0100328 .open = gpio_mockup_debugfs_open,
329 .read = gpio_mockup_debugfs_read,
330 .write = gpio_mockup_debugfs_write,
Bartosz Golaszewski9202ba22017-02-06 15:11:08 +0100331 .llseek = no_llseek,
Wei Yongjun59929d32019-09-04 14:18:34 +0000332 .release = single_release,
Bartosz Golaszewski9202ba22017-02-06 15:11:08 +0100333};
334
335static void gpio_mockup_debugfs_setup(struct device *dev,
336 struct gpio_mockup_chip *chip)
337{
338 struct gpio_mockup_dbgfs_private *priv;
Bartosz Golaszewski9202ba22017-02-06 15:11:08 +0100339 struct gpio_chip *gc;
Bartosz Golaszewskica8792a2017-11-27 11:48:45 +0100340 const char *devname;
Bartosz Golaszewski9202ba22017-02-06 15:11:08 +0100341 char *name;
342 int i;
343
344 gc = &chip->gc;
Bartosz Golaszewskica8792a2017-11-27 11:48:45 +0100345 devname = dev_name(&gc->gpiodev->dev);
Bartosz Golaszewski9202ba22017-02-06 15:11:08 +0100346
Bartosz Golaszewskica8792a2017-11-27 11:48:45 +0100347 chip->dbg_dir = debugfs_create_dir(devname, gpio_mockup_dbg_dir);
Bartosz Golaszewski9202ba22017-02-06 15:11:08 +0100348
349 for (i = 0; i < gc->ngpio; i++) {
350 name = devm_kasprintf(dev, GFP_KERNEL, "%d", i);
351 if (!name)
Greg Kroah-Hartmanf360dcd2019-06-26 10:45:57 +0200352 return;
Bartosz Golaszewski9202ba22017-02-06 15:11:08 +0100353
354 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
355 if (!priv)
Greg Kroah-Hartmanf360dcd2019-06-26 10:45:57 +0200356 return;
Bartosz Golaszewski9202ba22017-02-06 15:11:08 +0100357
358 priv->chip = chip;
359 priv->offset = i;
360 priv->desc = &gc->gpiodev->descs[i];
361
Greg Kroah-Hartmanf360dcd2019-06-26 10:45:57 +0200362 debugfs_create_file(name, 0200, chip->dbg_dir, priv,
363 &gpio_mockup_debugfs_ops);
Bartosz Golaszewski9202ba22017-02-06 15:11:08 +0100364 }
365
366 return;
Bartosz Golaszewski9202ba22017-02-06 15:11:08 +0100367}
368
Bartosz Golaszewski1cea4732017-11-27 11:48:47 +0100369static int gpio_mockup_name_lines(struct device *dev,
370 struct gpio_mockup_chip *chip)
371{
372 struct gpio_chip *gc = &chip->gc;
373 char **names;
374 int i;
375
376 names = devm_kcalloc(dev, gc->ngpio, sizeof(char *), GFP_KERNEL);
377 if (!names)
378 return -ENOMEM;
379
380 for (i = 0; i < gc->ngpio; i++) {
381 names[i] = devm_kasprintf(dev, GFP_KERNEL,
382 "%s-%d", gc->label, i);
383 if (!names[i])
384 return -ENOMEM;
385 }
386
387 gc->names = (const char *const *)names;
388
389 return 0;
390}
391
Bartosz Golaszewskica4091602017-02-06 13:10:35 +0100392static int gpio_mockup_probe(struct platform_device *pdev)
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +0200393{
Bartosz Golaszewski8a39f592017-11-27 11:48:40 +0100394 struct gpio_mockup_chip *chip;
Bartosz Golaszewski6d974d32017-11-27 11:48:43 +0100395 struct gpio_chip *gc;
Bartosz Golaszewski8a39f592017-11-27 11:48:40 +0100396 struct device *dev;
Bartosz Golaszewski3ea47b42018-09-25 13:29:46 +0200397 const char *name;
Kent Gibsonbc7bc682019-12-11 08:46:31 +0800398 int rv, base, i;
Bartosz Golaszewski3ea47b42018-09-25 13:29:46 +0200399 u16 ngpio;
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +0200400
Bartosz Golaszewski8a39f592017-11-27 11:48:40 +0100401 dev = &pdev->dev;
Bartosz Golaszewski3ea47b42018-09-25 13:29:46 +0200402
403 rv = device_property_read_u32(dev, "gpio-base", &base);
404 if (rv)
405 base = -1;
406
407 rv = device_property_read_u16(dev, "nr-gpios", &ngpio);
408 if (rv)
409 return rv;
410
411 rv = device_property_read_string(dev, "chip-name", &name);
412 if (rv)
413 name = NULL;
Bartosz Golaszewskib6523362017-06-09 13:41:28 +0200414
Bartosz Golaszewski8a39f592017-11-27 11:48:40 +0100415 chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL);
416 if (!chip)
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +0200417 return -ENOMEM;
418
Bartosz Golaszewski3ea47b42018-09-25 13:29:46 +0200419 if (!name) {
420 name = devm_kasprintf(dev, GFP_KERNEL,
421 "%s-%c", pdev->name, pdev->id + 'A');
422 if (!name)
423 return -ENOMEM;
424 }
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +0200425
Bartosz Golaszewski92124922018-10-31 14:55:47 +0100426 mutex_init(&chip->lock);
427
Bartosz Golaszewski6d974d32017-11-27 11:48:43 +0100428 gc = &chip->gc;
429 gc->base = base;
430 gc->ngpio = ngpio;
431 gc->label = name;
432 gc->owner = THIS_MODULE;
433 gc->parent = dev;
434 gc->get = gpio_mockup_get;
435 gc->set = gpio_mockup_set;
Bartosz Golaszewskicbf1e092018-10-31 15:01:40 +0100436 gc->get_multiple = gpio_mockup_get_multiple;
Bartosz Golaszewskic47bee92017-11-27 11:48:51 +0100437 gc->set_multiple = gpio_mockup_set_multiple;
Bartosz Golaszewski6d974d32017-11-27 11:48:43 +0100438 gc->direction_output = gpio_mockup_dirout;
439 gc->direction_input = gpio_mockup_dirin;
440 gc->get_direction = gpio_mockup_get_direction;
Kent Gibson64e71122019-11-05 10:04:27 +0800441 gc->set_config = gpio_mockup_set_config;
Bartosz Golaszewski6d974d32017-11-27 11:48:43 +0100442 gc->to_irq = gpio_mockup_to_irq;
Bartosz Golaszewski2a9e2742019-01-17 16:30:27 +0100443 gc->free = gpio_mockup_free;
Bartosz Golaszewski6d974d32017-11-27 11:48:43 +0100444
445 chip->lines = devm_kcalloc(dev, gc->ngpio,
446 sizeof(*chip->lines), GFP_KERNEL);
447 if (!chip->lines)
448 return -ENOMEM;
449
Kent Gibsonbc7bc682019-12-11 08:46:31 +0800450 for (i = 0; i < gc->ngpio; i++)
451 chip->lines[i].dir = GPIO_LINE_DIRECTION_IN;
452
Bartosz Golaszewski3ea47b42018-09-25 13:29:46 +0200453 if (device_property_read_bool(dev, "named-gpio-lines")) {
Bartosz Golaszewski6d974d32017-11-27 11:48:43 +0100454 rv = gpio_mockup_name_lines(dev, chip);
455 if (rv)
456 return rv;
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +0200457 }
458
Bartosz Golaszewski6d974d32017-11-27 11:48:43 +0100459 rv = devm_irq_sim_init(dev, &chip->irqsim, gc->ngpio);
Bartosz Golaszewski1e0dca62017-11-27 11:48:52 +0100460 if (rv < 0)
Bartosz Golaszewski6d974d32017-11-27 11:48:43 +0100461 return rv;
462
463 rv = devm_gpiochip_add_data(dev, &chip->gc, chip);
464 if (rv)
465 return rv;
466
Greg Kroah-Hartmanf360dcd2019-06-26 10:45:57 +0200467 gpio_mockup_debugfs_setup(dev, chip);
Bartosz Golaszewski6d974d32017-11-27 11:48:43 +0100468
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +0200469 return 0;
470}
471
Bartosz Golaszewskica4091602017-02-06 13:10:35 +0100472static struct platform_driver gpio_mockup_driver = {
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +0200473 .driver = {
Bartosz Golaszewskica4091602017-02-06 13:10:35 +0100474 .name = GPIO_MOCKUP_NAME,
Bartosz Golaszewski364ac452016-12-20 12:28:20 +0100475 },
Bartosz Golaszewskica4091602017-02-06 13:10:35 +0100476 .probe = gpio_mockup_probe,
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +0200477};
478
Bartosz Golaszewski8a39f592017-11-27 11:48:40 +0100479static struct platform_device *gpio_mockup_pdevs[GPIO_MOCKUP_MAX_GC];
480
481static void gpio_mockup_unregister_pdevs(void)
482{
483 struct platform_device *pdev;
484 int i;
485
486 for (i = 0; i < GPIO_MOCKUP_MAX_GC; i++) {
487 pdev = gpio_mockup_pdevs[i];
488
489 if (pdev)
490 platform_device_unregister(pdev);
491 }
492}
Bartosz Golaszewskif3b47172017-11-27 11:48:37 +0100493
494static int __init gpio_mockup_init(void)
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +0200495{
Bartosz Golaszewski3ea47b42018-09-25 13:29:46 +0200496 struct property_entry properties[GPIO_MOCKUP_MAX_PROP];
497 int i, prop, num_chips, err = 0, base;
498 struct platform_device_info pdevinfo;
Bartosz Golaszewski8a39f592017-11-27 11:48:40 +0100499 struct platform_device *pdev;
Bartosz Golaszewski3ea47b42018-09-25 13:29:46 +0200500 u16 ngpio;
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +0200501
Bartosz Golaszewskie63a0062017-11-27 11:48:53 +0100502 if ((gpio_mockup_num_ranges < 2) ||
503 (gpio_mockup_num_ranges % 2) ||
504 (gpio_mockup_num_ranges > GPIO_MOCKUP_MAX_RANGES))
Bartosz Golaszewski20c35ac2017-11-27 11:48:38 +0100505 return -EINVAL;
506
Bartosz Golaszewski8a39f592017-11-27 11:48:40 +0100507 /* Each chip is described by two values. */
Bartosz Golaszewskie63a0062017-11-27 11:48:53 +0100508 num_chips = gpio_mockup_num_ranges / 2;
Bartosz Golaszewski8a39f592017-11-27 11:48:40 +0100509
Bartosz Golaszewskifa869632017-11-27 11:48:49 +0100510 /*
511 * The second value in the <base GPIO - number of GPIOS> pair must
512 * always be greater than 0.
513 */
514 for (i = 0; i < num_chips; i++) {
Bartosz Golaszewskicd9835f2017-11-27 11:48:54 +0100515 if (gpio_mockup_range_ngpio(i) < 0)
Bartosz Golaszewskifa869632017-11-27 11:48:49 +0100516 return -EINVAL;
517 }
518
Bartosz Golaszewski2a9e2742019-01-17 16:30:27 +0100519 gpio_mockup_dbg_dir = debugfs_create_dir("gpio-mockup", NULL);
Bartosz Golaszewski9202ba22017-02-06 15:11:08 +0100520
Bartosz Golaszewski8a39f592017-11-27 11:48:40 +0100521 err = platform_driver_register(&gpio_mockup_driver);
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +0200522 if (err) {
Bartosz Golaszewskif1bec992017-11-27 11:48:41 +0100523 gpio_mockup_err("error registering platform driver\n");
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +0200524 return err;
525 }
526
Bartosz Golaszewski8a39f592017-11-27 11:48:40 +0100527 for (i = 0; i < num_chips; i++) {
Bartosz Golaszewski3ea47b42018-09-25 13:29:46 +0200528 memset(properties, 0, sizeof(properties));
529 memset(&pdevinfo, 0, sizeof(pdevinfo));
530 prop = 0;
Bartosz Golaszewski8a39f592017-11-27 11:48:40 +0100531
Bartosz Golaszewski3ea47b42018-09-25 13:29:46 +0200532 base = gpio_mockup_range_base(i);
533 if (base >= 0)
534 properties[prop++] = PROPERTY_ENTRY_U32("gpio-base",
535 base);
536
537 ngpio = base < 0 ? gpio_mockup_range_ngpio(i)
538 : gpio_mockup_range_ngpio(i) - base;
539 properties[prop++] = PROPERTY_ENTRY_U16("nr-gpios", ngpio);
540
541 if (gpio_mockup_named_lines)
542 properties[prop++] = PROPERTY_ENTRY_BOOL(
543 "named-gpio-lines");
544
545 pdevinfo.name = GPIO_MOCKUP_NAME;
546 pdevinfo.id = i;
547 pdevinfo.properties = properties;
548
549 pdev = platform_device_register_full(&pdevinfo);
Bartosz Golaszewskic4b54e12017-12-06 17:30:02 +0100550 if (IS_ERR(pdev)) {
Bartosz Golaszewskif1bec992017-11-27 11:48:41 +0100551 gpio_mockup_err("error registering device");
Bartosz Golaszewski8a39f592017-11-27 11:48:40 +0100552 platform_driver_unregister(&gpio_mockup_driver);
553 gpio_mockup_unregister_pdevs();
Bartosz Golaszewskic4b54e12017-12-06 17:30:02 +0100554 return PTR_ERR(pdev);
Bartosz Golaszewski8a39f592017-11-27 11:48:40 +0100555 }
556
557 gpio_mockup_pdevs[i] = pdev;
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +0200558 }
559
560 return 0;
561}
562
Bartosz Golaszewskif3b47172017-11-27 11:48:37 +0100563static void __exit gpio_mockup_exit(void)
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +0200564{
Bartosz Golaszewski9202ba22017-02-06 15:11:08 +0100565 debugfs_remove_recursive(gpio_mockup_dbg_dir);
Bartosz Golaszewskica4091602017-02-06 13:10:35 +0100566 platform_driver_unregister(&gpio_mockup_driver);
Bartosz Golaszewski8a39f592017-11-27 11:48:40 +0100567 gpio_mockup_unregister_pdevs();
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +0200568}
569
Bartosz Golaszewskif3b47172017-11-27 11:48:37 +0100570module_init(gpio_mockup_init);
571module_exit(gpio_mockup_exit);
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +0200572
573MODULE_AUTHOR("Kamlakant Patel <kamlakant.patel@broadcom.com>");
Linus Walleij4f9a4cd2018-03-06 09:04:15 +0100574MODULE_AUTHOR("Bamvor Jian Zhang <bamv2005@gmail.com>");
Bartosz Golaszewskic60c7f42017-06-09 13:41:31 +0200575MODULE_AUTHOR("Bartosz Golaszewski <brgl@bgdev.pl>");
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +0200576MODULE_DESCRIPTION("GPIO Testing driver");
577MODULE_LICENSE("GPL v2");