blob: 10f6bf6f16602224ebff5dfc96990160302a41ee [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>
17#include <linux/platform_device.h>
18
19#define GPIO_NAME "gpio-mockup"
20#define MAX_GC 10
21
22enum direction {
23 OUT,
24 IN
25};
26
27/*
28 * struct gpio_pin_status - structure describing a GPIO status
29 * @dir: Configures direction of gpio as "in" or "out", 0=in, 1=out
30 * @value: Configures status of the gpio as 0(low) or 1(high)
31 */
32struct gpio_pin_status {
33 enum direction dir;
34 bool value;
35};
36
37struct mockup_gpio_controller {
38 struct gpio_chip gc;
39 struct gpio_pin_status *stats;
40};
41
42static int gpio_mockup_ranges[MAX_GC << 1];
43static int gpio_mockup_params_nr;
44module_param_array(gpio_mockup_ranges, int, &gpio_mockup_params_nr, 0400);
45
Bartosz Golaszewskie53c6022016-12-20 12:28:18 +010046static const char pins_name_start = 'A';
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +020047
48static int mockup_gpio_get(struct gpio_chip *gc, unsigned int offset)
49{
50 struct mockup_gpio_controller *cntr = gpiochip_get_data(gc);
51
52 return cntr->stats[offset].value;
53}
54
55static void mockup_gpio_set(struct gpio_chip *gc, unsigned int offset,
56 int value)
57{
58 struct mockup_gpio_controller *cntr = gpiochip_get_data(gc);
59
60 cntr->stats[offset].value = !!value;
61}
62
63static int mockup_gpio_dirout(struct gpio_chip *gc, unsigned int offset,
64 int value)
65{
66 struct mockup_gpio_controller *cntr = gpiochip_get_data(gc);
67
68 mockup_gpio_set(gc, offset, value);
69 cntr->stats[offset].dir = OUT;
70 return 0;
71}
72
73static int mockup_gpio_dirin(struct gpio_chip *gc, unsigned int offset)
74{
75 struct mockup_gpio_controller *cntr = gpiochip_get_data(gc);
76
77 cntr->stats[offset].dir = IN;
78 return 0;
79}
80
81static int mockup_gpio_get_direction(struct gpio_chip *gc, unsigned int offset)
82{
83 struct mockup_gpio_controller *cntr = gpiochip_get_data(gc);
84
85 return cntr->stats[offset].dir;
86}
87
88static int mockup_gpio_add(struct device *dev,
89 struct mockup_gpio_controller *cntr,
90 const char *name, int base, int ngpio)
91{
92 int ret;
93
94 cntr->gc.base = base;
95 cntr->gc.ngpio = ngpio;
96 cntr->gc.label = name;
97 cntr->gc.owner = THIS_MODULE;
98 cntr->gc.parent = dev;
99 cntr->gc.get = mockup_gpio_get;
100 cntr->gc.set = mockup_gpio_set;
101 cntr->gc.direction_output = mockup_gpio_dirout;
102 cntr->gc.direction_input = mockup_gpio_dirin;
103 cntr->gc.get_direction = mockup_gpio_get_direction;
104 cntr->stats = devm_kzalloc(dev, sizeof(*cntr->stats) * cntr->gc.ngpio,
105 GFP_KERNEL);
106 if (!cntr->stats) {
107 ret = -ENOMEM;
108 goto err;
109 }
110 ret = devm_gpiochip_add_data(dev, &cntr->gc, cntr);
111 if (ret)
112 goto err;
113
114 dev_info(dev, "gpio<%d..%d> add successful!", base, base + ngpio);
115 return 0;
116err:
117 dev_err(dev, "gpio<%d..%d> add failed!", base, base + ngpio);
118 return ret;
119}
120
121static int mockup_gpio_probe(struct platform_device *pdev)
122{
123 struct device *dev = &pdev->dev;
124 struct mockup_gpio_controller *cntr;
125 int ret;
126 int i;
127 int base;
128 int ngpio;
Bartosz Golaszewskiad6d8002016-12-20 12:28:19 +0100129 char *chip_name;
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +0200130
131 if (gpio_mockup_params_nr < 2)
132 return -EINVAL;
133
134 cntr = devm_kzalloc(dev, sizeof(*cntr) * (gpio_mockup_params_nr >> 1),
135 GFP_KERNEL);
136 if (!cntr)
137 return -ENOMEM;
138
139 platform_set_drvdata(pdev, cntr);
140
141 for (i = 0; i < gpio_mockup_params_nr >> 1; i++) {
142 base = gpio_mockup_ranges[i * 2];
143 if (base == -1)
144 ngpio = gpio_mockup_ranges[i * 2 + 1];
145 else
146 ngpio = gpio_mockup_ranges[i * 2 + 1] - base;
147
148 if (ngpio >= 0) {
Bartosz Golaszewskiad6d8002016-12-20 12:28:19 +0100149 chip_name = devm_kasprintf(dev, GFP_KERNEL,
150 "%s-%c", GPIO_NAME,
151 pins_name_start + i);
152 if (!chip_name)
153 return -ENOMEM;
154
Bamvor Jian Zhang0f98dd12016-08-31 11:45:46 +0200155 ret = mockup_gpio_add(dev, &cntr[i],
156 chip_name, base, ngpio);
157 } else {
158 ret = -1;
159 }
160 if (ret) {
161 if (base < 0)
162 dev_err(dev, "gpio<%d..%d> add failed\n",
163 base, ngpio);
164 else
165 dev_err(dev, "gpio<%d..%d> add failed\n",
166 base, base + ngpio);
167
168 return ret;
169 }
170 }
171
172 return 0;
173}
174
175static struct platform_driver mockup_gpio_driver = {
176 .driver = {
177 .name = GPIO_NAME,
178 },
179 .probe = mockup_gpio_probe,
180};
181
182static struct platform_device *pdev;
183static int __init mock_device_init(void)
184{
185 int err;
186
187 pdev = platform_device_alloc(GPIO_NAME, -1);
188 if (!pdev)
189 return -ENOMEM;
190
191 err = platform_device_add(pdev);
192 if (err) {
193 platform_device_put(pdev);
194 return err;
195 }
196
197 err = platform_driver_register(&mockup_gpio_driver);
198 if (err) {
199 platform_device_unregister(pdev);
200 return err;
201 }
202
203 return 0;
204}
205
206static void __exit mock_device_exit(void)
207{
208 platform_driver_unregister(&mockup_gpio_driver);
209 platform_device_unregister(pdev);
210}
211
212module_init(mock_device_init);
213module_exit(mock_device_exit);
214
215MODULE_AUTHOR("Kamlakant Patel <kamlakant.patel@broadcom.com>");
216MODULE_AUTHOR("Bamvor Jian Zhang <bamvor.zhangjian@linaro.org>");
217MODULE_DESCRIPTION("GPIO Testing driver");
218MODULE_LICENSE("GPL v2");