1
2
3
4
5
6
7
8
9
10
11
12
13
14
15#include <linux/bitops.h>
16#include <linux/init.h>
17#include <linux/errno.h>
18#include <linux/module.h>
19#include <linux/of_device.h>
20#include <linux/of_platform.h>
21#include <linux/of_gpio.h>
22#include <linux/io.h>
23#include <linux/gpio.h>
24#include <linux/slab.h>
25
26
27#define XGPIO_DATA_OFFSET (0x0)
28#define XGPIO_TRI_OFFSET (0x4)
29
30#define XGPIO_CHANNEL_OFFSET 0x8
31
32
33#if defined(CONFIG_ARCH_ZYNQ) || defined(CONFIG_X86)
34# define xgpio_readreg(offset) readl(offset)
35# define xgpio_writereg(offset, val) writel(val, offset)
36#else
37# define xgpio_readreg(offset) __raw_readl(offset)
38# define xgpio_writereg(offset, val) __raw_writel(val, offset)
39#endif
40
41
42
43
44
45
46
47
48
49struct xgpio_instance {
50 struct of_mm_gpio_chip mmchip;
51 unsigned int gpio_width[2];
52 u32 gpio_state[2];
53 u32 gpio_dir[2];
54 spinlock_t gpio_lock[2];
55};
56
57static inline int xgpio_index(struct xgpio_instance *chip, int gpio)
58{
59 if (gpio >= chip->gpio_width[0])
60 return 1;
61
62 return 0;
63}
64
65static inline int xgpio_regoffset(struct xgpio_instance *chip, int gpio)
66{
67 if (xgpio_index(chip, gpio))
68 return XGPIO_CHANNEL_OFFSET;
69
70 return 0;
71}
72
73static inline int xgpio_offset(struct xgpio_instance *chip, int gpio)
74{
75 if (xgpio_index(chip, gpio))
76 return gpio - chip->gpio_width[0];
77
78 return gpio;
79}
80
81
82
83
84
85
86
87
88
89
90
91
92static int xgpio_get(struct gpio_chip *gc, unsigned int gpio)
93{
94 struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
95 struct xgpio_instance *chip = gpiochip_get_data(gc);
96 u32 val;
97
98 val = xgpio_readreg(mm_gc->regs + XGPIO_DATA_OFFSET +
99 xgpio_regoffset(chip, gpio));
100
101 return !!(val & BIT(xgpio_offset(chip, gpio)));
102}
103
104
105
106
107
108
109
110
111
112
113static void xgpio_set(struct gpio_chip *gc, unsigned int gpio, int val)
114{
115 unsigned long flags;
116 struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
117 struct xgpio_instance *chip = gpiochip_get_data(gc);
118 int index = xgpio_index(chip, gpio);
119 int offset = xgpio_offset(chip, gpio);
120
121 spin_lock_irqsave(&chip->gpio_lock[index], flags);
122
123
124 if (val)
125 chip->gpio_state[index] |= BIT(offset);
126 else
127 chip->gpio_state[index] &= ~BIT(offset);
128
129 xgpio_writereg(mm_gc->regs + XGPIO_DATA_OFFSET +
130 xgpio_regoffset(chip, gpio), chip->gpio_state[index]);
131
132 spin_unlock_irqrestore(&chip->gpio_lock[index], flags);
133}
134
135
136
137
138
139
140
141
142
143
144static void xgpio_set_multiple(struct gpio_chip *gc, unsigned long *mask,
145 unsigned long *bits)
146{
147 unsigned long flags;
148 struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
149 struct xgpio_instance *chip = gpiochip_get_data(gc);
150 int index = xgpio_index(chip, 0);
151 int offset, i;
152
153 spin_lock_irqsave(&chip->gpio_lock[index], flags);
154
155
156 for (i = 0; i < gc->ngpio; i++) {
157 if (*mask == 0)
158 break;
159 if (index != xgpio_index(chip, i)) {
160 xgpio_writereg(mm_gc->regs + XGPIO_DATA_OFFSET +
161 xgpio_regoffset(chip, i),
162 chip->gpio_state[index]);
163 spin_unlock_irqrestore(&chip->gpio_lock[index], flags);
164 index = xgpio_index(chip, i);
165 spin_lock_irqsave(&chip->gpio_lock[index], flags);
166 }
167 if (__test_and_clear_bit(i, mask)) {
168 offset = xgpio_offset(chip, i);
169 if (test_bit(i, bits))
170 chip->gpio_state[index] |= BIT(offset);
171 else
172 chip->gpio_state[index] &= ~BIT(offset);
173 }
174 }
175
176 xgpio_writereg(mm_gc->regs + XGPIO_DATA_OFFSET +
177 xgpio_regoffset(chip, i), chip->gpio_state[index]);
178
179 spin_unlock_irqrestore(&chip->gpio_lock[index], flags);
180}
181
182
183
184
185
186
187
188
189
190
191static int xgpio_dir_in(struct gpio_chip *gc, unsigned int gpio)
192{
193 unsigned long flags;
194 struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
195 struct xgpio_instance *chip = gpiochip_get_data(gc);
196 int index = xgpio_index(chip, gpio);
197 int offset = xgpio_offset(chip, gpio);
198
199 spin_lock_irqsave(&chip->gpio_lock[index], flags);
200
201
202 chip->gpio_dir[index] |= BIT(offset);
203 xgpio_writereg(mm_gc->regs + XGPIO_TRI_OFFSET +
204 xgpio_regoffset(chip, gpio), chip->gpio_dir[index]);
205
206 spin_unlock_irqrestore(&chip->gpio_lock[index], flags);
207
208 return 0;
209}
210
211
212
213
214
215
216
217
218
219
220
221
222
223static int xgpio_dir_out(struct gpio_chip *gc, unsigned int gpio, int val)
224{
225 unsigned long flags;
226 struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
227 struct xgpio_instance *chip = gpiochip_get_data(gc);
228 int index = xgpio_index(chip, gpio);
229 int offset = xgpio_offset(chip, gpio);
230
231 spin_lock_irqsave(&chip->gpio_lock[index], flags);
232
233
234 if (val)
235 chip->gpio_state[index] |= BIT(offset);
236 else
237 chip->gpio_state[index] &= ~BIT(offset);
238 xgpio_writereg(mm_gc->regs + XGPIO_DATA_OFFSET +
239 xgpio_regoffset(chip, gpio), chip->gpio_state[index]);
240
241
242 chip->gpio_dir[index] &= ~BIT(offset);
243 xgpio_writereg(mm_gc->regs + XGPIO_TRI_OFFSET +
244 xgpio_regoffset(chip, gpio), chip->gpio_dir[index]);
245
246 spin_unlock_irqrestore(&chip->gpio_lock[index], flags);
247
248 return 0;
249}
250
251
252
253
254
255static void xgpio_save_regs(struct of_mm_gpio_chip *mm_gc)
256{
257 struct xgpio_instance *chip =
258 container_of(mm_gc, struct xgpio_instance, mmchip);
259
260 xgpio_writereg(mm_gc->regs + XGPIO_DATA_OFFSET, chip->gpio_state[0]);
261 xgpio_writereg(mm_gc->regs + XGPIO_TRI_OFFSET, chip->gpio_dir[0]);
262
263 if (!chip->gpio_width[1])
264 return;
265
266 xgpio_writereg(mm_gc->regs + XGPIO_DATA_OFFSET + XGPIO_CHANNEL_OFFSET,
267 chip->gpio_state[1]);
268 xgpio_writereg(mm_gc->regs + XGPIO_TRI_OFFSET + XGPIO_CHANNEL_OFFSET,
269 chip->gpio_dir[1]);
270}
271
272
273
274
275
276
277
278
279
280static int xgpio_remove(struct platform_device *pdev)
281{
282 struct xgpio_instance *chip = platform_get_drvdata(pdev);
283
284 of_mm_gpiochip_remove(&chip->mmchip);
285
286 return 0;
287}
288
289
290
291
292
293
294
295
296
297static int xgpio_probe(struct platform_device *pdev)
298{
299 struct xgpio_instance *chip;
300 int status = 0;
301 struct device_node *np = pdev->dev.of_node;
302 u32 is_dual;
303
304 chip = devm_kzalloc(&pdev->dev, sizeof(*chip), GFP_KERNEL);
305 if (!chip)
306 return -ENOMEM;
307
308 platform_set_drvdata(pdev, chip);
309
310
311 of_property_read_u32(np, "xlnx,dout-default", &chip->gpio_state[0]);
312
313
314 if (of_property_read_u32(np, "xlnx,tri-default", &chip->gpio_dir[0]))
315 chip->gpio_dir[0] = 0xFFFFFFFF;
316
317
318
319
320
321 if (of_property_read_u32(np, "xlnx,gpio-width", &chip->gpio_width[0]))
322 chip->gpio_width[0] = 32;
323
324 spin_lock_init(&chip->gpio_lock[0]);
325
326 if (of_property_read_u32(np, "xlnx,is-dual", &is_dual))
327 is_dual = 0;
328
329 if (is_dual) {
330
331 of_property_read_u32(np, "xlnx,dout-default-2",
332 &chip->gpio_state[1]);
333
334
335 if (of_property_read_u32(np, "xlnx,tri-default-2",
336 &chip->gpio_dir[1]))
337 chip->gpio_dir[1] = 0xFFFFFFFF;
338
339
340
341
342
343 if (of_property_read_u32(np, "xlnx,gpio2-width",
344 &chip->gpio_width[1]))
345 chip->gpio_width[1] = 32;
346
347 spin_lock_init(&chip->gpio_lock[1]);
348 }
349
350 chip->mmchip.gc.ngpio = chip->gpio_width[0] + chip->gpio_width[1];
351 chip->mmchip.gc.parent = &pdev->dev;
352 chip->mmchip.gc.direction_input = xgpio_dir_in;
353 chip->mmchip.gc.direction_output = xgpio_dir_out;
354 chip->mmchip.gc.get = xgpio_get;
355 chip->mmchip.gc.set = xgpio_set;
356 chip->mmchip.gc.set_multiple = xgpio_set_multiple;
357
358 chip->mmchip.save_regs = xgpio_save_regs;
359
360
361 status = of_mm_gpiochip_add_data(np, &chip->mmchip, chip);
362 if (status) {
363 pr_err("%s: error in probe function with status %d\n",
364 np->full_name, status);
365 return status;
366 }
367
368 return 0;
369}
370
371static const struct of_device_id xgpio_of_match[] = {
372 { .compatible = "xlnx,xps-gpio-1.00.a", },
373 { },
374};
375
376MODULE_DEVICE_TABLE(of, xgpio_of_match);
377
378static struct platform_driver xgpio_plat_driver = {
379 .probe = xgpio_probe,
380 .remove = xgpio_remove,
381 .driver = {
382 .name = "gpio-xilinx",
383 .of_match_table = xgpio_of_match,
384 },
385};
386
387static int __init xgpio_init(void)
388{
389 return platform_driver_register(&xgpio_plat_driver);
390}
391
392subsys_initcall(xgpio_init);
393
394static void __exit xgpio_exit(void)
395{
396 platform_driver_unregister(&xgpio_plat_driver);
397}
398module_exit(xgpio_exit);
399
400MODULE_AUTHOR("Xilinx, Inc.");
401MODULE_DESCRIPTION("Xilinx GPIO driver");
402MODULE_LICENSE("GPL");
403