aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/gpio/gpio-zynq.c
diff options
context:
space:
mode:
authorHarini Katakam <harini.katakam@xilinx.com>2014-07-08 07:02:35 -0400
committerLinus Walleij <linus.walleij@linaro.org>2014-07-10 05:11:41 -0400
commit3242ba117e9bd29eb748afd6f6fbfbd6dd7cfe52 (patch)
treeac01c36057b3aa43eab356ede0d4f507e056d0fc /drivers/gpio/gpio-zynq.c
parent85ea29ac1c4838a720e6fcde2805ddb33451f21b (diff)
gpio: Add driver for Zynq GPIO controller
Add support for GPIO controller used by Xilinx Zynq. v3 changes: - Use linux/gpio/driver.h instead of linux/gpio.h - Make irq a local variable in probe v2 changes: - convert to pm_runtime_force_(suspend|resume) - add pm_runtime_set_active in probe() - also (un)prepare clocks when they are dis-/enabled - add some missing calls to pm_runtime_get() - use pm_runtime_put() instead of sync variant - remove gpio chip in driver remove() - remove redundant type casts - directly use IO helpers - use BIT macro to set/clear bits - migrate to GPIOLIB_IRQCHIP Signed-off-by: Harini Katakam <harinik@xilinx.com> Signed-off-by: Soren Brinkmann <soren.brinkmann@xilinx.com> Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
Diffstat (limited to 'drivers/gpio/gpio-zynq.c')
-rw-r--r--drivers/gpio/gpio-zynq.c649
1 files changed, 649 insertions, 0 deletions
diff --git a/drivers/gpio/gpio-zynq.c b/drivers/gpio/gpio-zynq.c
new file mode 100644
index 000000000000..c0c53fdbf07d
--- /dev/null
+++ b/drivers/gpio/gpio-zynq.c
@@ -0,0 +1,649 @@
1/*
2 * Xilinx Zynq GPIO device driver
3 *
4 * Copyright (C) 2009 - 2014 Xilinx, Inc.
5 *
6 * This program is free software; you can redistribute it and/or modify it under
7 * the terms of the GNU General Public License as published by the Free Software
8 * Foundation; either version 2 of the License, or (at your option) any later
9 * version.
10 */
11
12#include <linux/bitops.h>
13#include <linux/clk.h>
14#include <linux/gpio/driver.h>
15#include <linux/init.h>
16#include <linux/interrupt.h>
17#include <linux/io.h>
18#include <linux/module.h>
19#include <linux/platform_device.h>
20#include <linux/pm_runtime.h>
21
22#define DRIVER_NAME "zynq-gpio"
23
24/* Maximum banks */
25#define ZYNQ_GPIO_MAX_BANK 4
26
27#define ZYNQ_GPIO_BANK0_NGPIO 32
28#define ZYNQ_GPIO_BANK1_NGPIO 22
29#define ZYNQ_GPIO_BANK2_NGPIO 32
30#define ZYNQ_GPIO_BANK3_NGPIO 32
31
32#define ZYNQ_GPIO_NR_GPIOS (ZYNQ_GPIO_BANK0_NGPIO + \
33 ZYNQ_GPIO_BANK1_NGPIO + \
34 ZYNQ_GPIO_BANK2_NGPIO + \
35 ZYNQ_GPIO_BANK3_NGPIO)
36
37#define ZYNQ_GPIO_BANK0_PIN_MIN 0
38#define ZYNQ_GPIO_BANK0_PIN_MAX (ZYNQ_GPIO_BANK0_PIN_MIN + \
39 ZYNQ_GPIO_BANK0_NGPIO - 1)
40#define ZYNQ_GPIO_BANK1_PIN_MIN (ZYNQ_GPIO_BANK0_PIN_MAX + 1)
41#define ZYNQ_GPIO_BANK1_PIN_MAX (ZYNQ_GPIO_BANK1_PIN_MIN + \
42 ZYNQ_GPIO_BANK1_NGPIO - 1)
43#define ZYNQ_GPIO_BANK2_PIN_MIN (ZYNQ_GPIO_BANK1_PIN_MAX + 1)
44#define ZYNQ_GPIO_BANK2_PIN_MAX (ZYNQ_GPIO_BANK2_PIN_MIN + \
45 ZYNQ_GPIO_BANK2_NGPIO - 1)
46#define ZYNQ_GPIO_BANK3_PIN_MIN (ZYNQ_GPIO_BANK2_PIN_MAX + 1)
47#define ZYNQ_GPIO_BANK3_PIN_MAX (ZYNQ_GPIO_BANK3_PIN_MIN + \
48 ZYNQ_GPIO_BANK3_NGPIO - 1)
49
50
51/* Register offsets for the GPIO device */
52/* LSW Mask & Data -WO */
53#define ZYNQ_GPIO_DATA_LSW_OFFSET(BANK) (0x000 + (8 * BANK))
54/* MSW Mask & Data -WO */
55#define ZYNQ_GPIO_DATA_MSW_OFFSET(BANK) (0x004 + (8 * BANK))
56/* Data Register-RW */
57#define ZYNQ_GPIO_DATA_RO_OFFSET(BANK) (0x060 + (4 * BANK))
58/* Direction mode reg-RW */
59#define ZYNQ_GPIO_DIRM_OFFSET(BANK) (0x204 + (0x40 * BANK))
60/* Output enable reg-RW */
61#define ZYNQ_GPIO_OUTEN_OFFSET(BANK) (0x208 + (0x40 * BANK))
62/* Interrupt mask reg-RO */
63#define ZYNQ_GPIO_INTMASK_OFFSET(BANK) (0x20C + (0x40 * BANK))
64/* Interrupt enable reg-WO */
65#define ZYNQ_GPIO_INTEN_OFFSET(BANK) (0x210 + (0x40 * BANK))
66/* Interrupt disable reg-WO */
67#define ZYNQ_GPIO_INTDIS_OFFSET(BANK) (0x214 + (0x40 * BANK))
68/* Interrupt status reg-RO */
69#define ZYNQ_GPIO_INTSTS_OFFSET(BANK) (0x218 + (0x40 * BANK))
70/* Interrupt type reg-RW */
71#define ZYNQ_GPIO_INTTYPE_OFFSET(BANK) (0x21C + (0x40 * BANK))
72/* Interrupt polarity reg-RW */
73#define ZYNQ_GPIO_INTPOL_OFFSET(BANK) (0x220 + (0x40 * BANK))
74/* Interrupt on any, reg-RW */
75#define ZYNQ_GPIO_INTANY_OFFSET(BANK) (0x224 + (0x40 * BANK))
76
77/* Disable all interrupts mask */
78#define ZYNQ_GPIO_IXR_DISABLE_ALL 0xFFFFFFFF
79
80/* Mid pin number of a bank */
81#define ZYNQ_GPIO_MID_PIN_NUM 16
82
83/* GPIO upper 16 bit mask */
84#define ZYNQ_GPIO_UPPER_MASK 0xFFFF0000
85
86/**
87 * struct zynq_gpio - gpio device private data structure
88 * @chip: instance of the gpio_chip
89 * @base_addr: base address of the GPIO device
90 * @clk: clock resource for this controller
91 */
92struct zynq_gpio {
93 struct gpio_chip chip;
94 void __iomem *base_addr;
95 struct clk *clk;
96};
97
98/**
99 * zynq_gpio_get_bank_pin - Get the bank number and pin number within that bank
100 * for a given pin in the GPIO device
101 * @pin_num: gpio pin number within the device
102 * @bank_num: an output parameter used to return the bank number of the gpio
103 * pin
104 * @bank_pin_num: an output parameter used to return pin number within a bank
105 * for the given gpio pin
106 *
107 * Returns the bank number and pin offset within the bank.
108 */
109static inline void zynq_gpio_get_bank_pin(unsigned int pin_num,
110 unsigned int *bank_num,
111 unsigned int *bank_pin_num)
112{
113 switch (pin_num) {
114 case ZYNQ_GPIO_BANK0_PIN_MIN ... ZYNQ_GPIO_BANK0_PIN_MAX:
115 *bank_num = 0;
116 *bank_pin_num = pin_num;
117 break;
118 case ZYNQ_GPIO_BANK1_PIN_MIN ... ZYNQ_GPIO_BANK1_PIN_MAX:
119 *bank_num = 1;
120 *bank_pin_num = pin_num - ZYNQ_GPIO_BANK1_PIN_MIN;
121 break;
122 case ZYNQ_GPIO_BANK2_PIN_MIN ... ZYNQ_GPIO_BANK2_PIN_MAX:
123 *bank_num = 2;
124 *bank_pin_num = pin_num - ZYNQ_GPIO_BANK2_PIN_MIN;
125 break;
126 case ZYNQ_GPIO_BANK3_PIN_MIN ... ZYNQ_GPIO_BANK3_PIN_MAX:
127 *bank_num = 3;
128 *bank_pin_num = pin_num - ZYNQ_GPIO_BANK3_PIN_MIN;
129 break;
130 default:
131 WARN(true, "invalid GPIO pin number: %u", pin_num);
132 *bank_num = 0;
133 *bank_pin_num = 0;
134 break;
135 }
136}
137
138/**
139 * zynq_gpio_get_value - Get the state of the specified pin of GPIO device
140 * @chip: gpio_chip instance to be worked on
141 * @pin: gpio pin number within the device
142 *
143 * This function reads the state of the specified pin of the GPIO device.
144 *
145 * Return: 0 if the pin is low, 1 if pin is high.
146 */
147static int zynq_gpio_get_value(struct gpio_chip *chip, unsigned int pin)
148{
149 u32 data;
150 unsigned int bank_num, bank_pin_num;
151 struct zynq_gpio *gpio = container_of(chip, struct zynq_gpio, chip);
152
153 zynq_gpio_get_bank_pin(pin, &bank_num, &bank_pin_num);
154
155 data = readl_relaxed(gpio->base_addr +
156 ZYNQ_GPIO_DATA_RO_OFFSET(bank_num));
157
158 return (data >> bank_pin_num) & 1;
159}
160
161/**
162 * zynq_gpio_set_value - Modify the state of the pin with specified value
163 * @chip: gpio_chip instance to be worked on
164 * @pin: gpio pin number within the device
165 * @state: value used to modify the state of the specified pin
166 *
167 * This function calculates the register offset (i.e to lower 16 bits or
168 * upper 16 bits) based on the given pin number and sets the state of a
169 * gpio pin to the specified value. The state is either 0 or non-zero.
170 */
171static void zynq_gpio_set_value(struct gpio_chip *chip, unsigned int pin,
172 int state)
173{
174 unsigned int reg_offset, bank_num, bank_pin_num;
175 struct zynq_gpio *gpio = container_of(chip, struct zynq_gpio, chip);
176
177 zynq_gpio_get_bank_pin(pin, &bank_num, &bank_pin_num);
178
179 if (bank_pin_num >= ZYNQ_GPIO_MID_PIN_NUM) {
180 /* only 16 data bits in bit maskable reg */
181 bank_pin_num -= ZYNQ_GPIO_MID_PIN_NUM;
182 reg_offset = ZYNQ_GPIO_DATA_MSW_OFFSET(bank_num);
183 } else {
184 reg_offset = ZYNQ_GPIO_DATA_LSW_OFFSET(bank_num);
185 }
186
187 /*
188 * get the 32 bit value to be written to the mask/data register where
189 * the upper 16 bits is the mask and lower 16 bits is the data
190 */
191 state = !!state;
192 state = ~(1 << (bank_pin_num + ZYNQ_GPIO_MID_PIN_NUM)) &
193 ((state << bank_pin_num) | ZYNQ_GPIO_UPPER_MASK);
194
195 writel_relaxed(state, gpio->base_addr + reg_offset);
196}
197
198/**
199 * zynq_gpio_dir_in - Set the direction of the specified GPIO pin as input
200 * @chip: gpio_chip instance to be worked on
201 * @pin: gpio pin number within the device
202 *
203 * This function uses the read-modify-write sequence to set the direction of
204 * the gpio pin as input.
205 *
206 * Return: 0 always
207 */
208static int zynq_gpio_dir_in(struct gpio_chip *chip, unsigned int pin)
209{
210 u32 reg;
211 unsigned int bank_num, bank_pin_num;
212 struct zynq_gpio *gpio = container_of(chip, struct zynq_gpio, chip);
213
214 zynq_gpio_get_bank_pin(pin, &bank_num, &bank_pin_num);
215
216 /* bank 0 pins 7 and 8 are special and cannot be used as inputs */
217 if (bank_num == 0 && (bank_pin_num == 7 || bank_pin_num == 8))
218 return -EINVAL;
219
220 /* clear the bit in direction mode reg to set the pin as input */
221 reg = readl_relaxed(gpio->base_addr + ZYNQ_GPIO_DIRM_OFFSET(bank_num));
222 reg &= ~BIT(bank_pin_num);
223 writel_relaxed(reg, gpio->base_addr + ZYNQ_GPIO_DIRM_OFFSET(bank_num));
224
225 return 0;
226}
227
228/**
229 * zynq_gpio_dir_out - Set the direction of the specified GPIO pin as output
230 * @chip: gpio_chip instance to be worked on
231 * @pin: gpio pin number within the device
232 * @state: value to be written to specified pin
233 *
234 * This function sets the direction of specified GPIO pin as output, configures
235 * the Output Enable register for the pin and uses zynq_gpio_set to set
236 * the state of the pin to the value specified.
237 *
238 * Return: 0 always
239 */
240static int zynq_gpio_dir_out(struct gpio_chip *chip, unsigned int pin,
241 int state)
242{
243 u32 reg;
244 unsigned int bank_num, bank_pin_num;
245 struct zynq_gpio *gpio = container_of(chip, struct zynq_gpio, chip);
246
247 zynq_gpio_get_bank_pin(pin, &bank_num, &bank_pin_num);
248
249 /* set the GPIO pin as output */
250 reg = readl_relaxed(gpio->base_addr + ZYNQ_GPIO_DIRM_OFFSET(bank_num));
251 reg |= BIT(bank_pin_num);
252 writel_relaxed(reg, gpio->base_addr + ZYNQ_GPIO_DIRM_OFFSET(bank_num));
253
254 /* configure the output enable reg for the pin */
255 reg = readl_relaxed(gpio->base_addr + ZYNQ_GPIO_OUTEN_OFFSET(bank_num));
256 reg |= BIT(bank_pin_num);
257 writel_relaxed(reg, gpio->base_addr + ZYNQ_GPIO_OUTEN_OFFSET(bank_num));
258
259 /* set the state of the pin */
260 zynq_gpio_set_value(chip, pin, state);
261 return 0;
262}
263
264/**
265 * zynq_gpio_irq_mask - Disable the interrupts for a gpio pin
266 * @irq_data: per irq and chip data passed down to chip functions
267 *
268 * This function calculates gpio pin number from irq number and sets the
269 * bit in the Interrupt Disable register of the corresponding bank to disable
270 * interrupts for that pin.
271 */
272static void zynq_gpio_irq_mask(struct irq_data *irq_data)
273{
274 unsigned int device_pin_num, bank_num, bank_pin_num;
275 struct zynq_gpio *gpio = irq_data_get_irq_chip_data(irq_data);
276
277 device_pin_num = irq_data->hwirq;
278 zynq_gpio_get_bank_pin(device_pin_num, &bank_num, &bank_pin_num);
279 writel_relaxed(BIT(bank_pin_num),
280 gpio->base_addr + ZYNQ_GPIO_INTDIS_OFFSET(bank_num));
281}
282
283/**
284 * zynq_gpio_irq_unmask - Enable the interrupts for a gpio pin
285 * @irq_data: irq data containing irq number of gpio pin for the interrupt
286 * to enable
287 *
288 * This function calculates the gpio pin number from irq number and sets the
289 * bit in the Interrupt Enable register of the corresponding bank to enable
290 * interrupts for that pin.
291 */
292static void zynq_gpio_irq_unmask(struct irq_data *irq_data)
293{
294 unsigned int device_pin_num, bank_num, bank_pin_num;
295 struct zynq_gpio *gpio = irq_data_get_irq_chip_data(irq_data);
296
297 device_pin_num = irq_data->hwirq;
298 zynq_gpio_get_bank_pin(device_pin_num, &bank_num, &bank_pin_num);
299 writel_relaxed(BIT(bank_pin_num),
300 gpio->base_addr + ZYNQ_GPIO_INTEN_OFFSET(bank_num));
301}
302
303/**
304 * zynq_gpio_set_irq_type - Set the irq type for a gpio pin
305 * @irq_data: irq data containing irq number of gpio pin
306 * @type: interrupt type that is to be set for the gpio pin
307 *
308 * This function gets the gpio pin number and its bank from the gpio pin number
309 * and configures the INT_TYPE, INT_POLARITY and INT_ANY registers.
310 *
311 * Return: 0, negative error otherwise.
312 * TYPE-EDGE_RISING, INT_TYPE - 1, INT_POLARITY - 1, INT_ANY - 0;
313 * TYPE-EDGE_FALLING, INT_TYPE - 1, INT_POLARITY - 0, INT_ANY - 0;
314 * TYPE-EDGE_BOTH, INT_TYPE - 1, INT_POLARITY - NA, INT_ANY - 1;
315 * TYPE-LEVEL_HIGH, INT_TYPE - 0, INT_POLARITY - 1, INT_ANY - NA;
316 * TYPE-LEVEL_LOW, INT_TYPE - 0, INT_POLARITY - 0, INT_ANY - NA
317 */
318static int zynq_gpio_set_irq_type(struct irq_data *irq_data, unsigned int type)
319{
320 u32 int_type, int_pol, int_any;
321 unsigned int device_pin_num, bank_num, bank_pin_num;
322 struct zynq_gpio *gpio = irq_data_get_irq_chip_data(irq_data);
323
324 device_pin_num = irq_data->hwirq;
325 zynq_gpio_get_bank_pin(device_pin_num, &bank_num, &bank_pin_num);
326
327 int_type = readl_relaxed(gpio->base_addr +
328 ZYNQ_GPIO_INTTYPE_OFFSET(bank_num));
329 int_pol = readl_relaxed(gpio->base_addr +
330 ZYNQ_GPIO_INTPOL_OFFSET(bank_num));
331 int_any = readl_relaxed(gpio->base_addr +
332 ZYNQ_GPIO_INTANY_OFFSET(bank_num));
333
334 /*
335 * based on the type requested, configure the INT_TYPE, INT_POLARITY
336 * and INT_ANY registers
337 */
338 switch (type) {
339 case IRQ_TYPE_EDGE_RISING:
340 int_type |= BIT(bank_pin_num);
341 int_pol |= BIT(bank_pin_num);
342 int_any &= ~BIT(bank_pin_num);
343 break;
344 case IRQ_TYPE_EDGE_FALLING:
345 int_type |= BIT(bank_pin_num);
346 int_pol &= ~BIT(bank_pin_num);
347 int_any &= ~BIT(bank_pin_num);
348 break;
349 case IRQ_TYPE_EDGE_BOTH:
350 int_type |= BIT(bank_pin_num);
351 int_any |= BIT(bank_pin_num);
352 break;
353 case IRQ_TYPE_LEVEL_HIGH:
354 int_type &= ~BIT(bank_pin_num);
355 int_pol |= BIT(bank_pin_num);
356 break;
357 case IRQ_TYPE_LEVEL_LOW:
358 int_type &= ~BIT(bank_pin_num);
359 int_pol &= ~BIT(bank_pin_num);
360 break;
361 default:
362 return -EINVAL;
363 }
364
365 writel_relaxed(int_type,
366 gpio->base_addr + ZYNQ_GPIO_INTTYPE_OFFSET(bank_num));
367 writel_relaxed(int_pol,
368 gpio->base_addr + ZYNQ_GPIO_INTPOL_OFFSET(bank_num));
369 writel_relaxed(int_any,
370 gpio->base_addr + ZYNQ_GPIO_INTANY_OFFSET(bank_num));
371 return 0;
372}
373
374static int zynq_gpio_set_wake(struct irq_data *data, unsigned int on)
375{
376 if (on)
377 zynq_gpio_irq_unmask(data);
378 else
379 zynq_gpio_irq_mask(data);
380
381 return 0;
382}
383
384/* irq chip descriptor */
385static struct irq_chip zynq_gpio_irqchip = {
386 .name = DRIVER_NAME,
387 .irq_mask = zynq_gpio_irq_mask,
388 .irq_unmask = zynq_gpio_irq_unmask,
389 .irq_set_type = zynq_gpio_set_irq_type,
390 .irq_set_wake = zynq_gpio_set_wake,
391};
392
393/**
394 * zynq_gpio_irqhandler - IRQ handler for the gpio banks of a gpio device
395 * @irq: irq number of the gpio bank where interrupt has occurred
396 * @desc: irq descriptor instance of the 'irq'
397 *
398 * This function reads the Interrupt Status Register of each bank to get the
399 * gpio pin number which has triggered an interrupt. It then acks the triggered
400 * interrupt and calls the pin specific handler set by the higher layer
401 * application for that pin.
402 * Note: A bug is reported if no handler is set for the gpio pin.
403 */
404static void zynq_gpio_irqhandler(unsigned int irq, struct irq_desc *desc)
405{
406 u32 int_sts, int_enb;
407 unsigned int bank_num;
408 struct zynq_gpio *gpio = irq_get_handler_data(irq);
409 struct irq_chip *irqchip = irq_desc_get_chip(desc);
410
411 chained_irq_enter(irqchip, desc);
412
413 for (bank_num = 0; bank_num < ZYNQ_GPIO_MAX_BANK; bank_num++) {
414 int_sts = readl_relaxed(gpio->base_addr +
415 ZYNQ_GPIO_INTSTS_OFFSET(bank_num));
416 int_enb = readl_relaxed(gpio->base_addr +
417 ZYNQ_GPIO_INTMASK_OFFSET(bank_num));
418 int_sts &= ~int_enb;
419 if (int_sts) {
420 int offset;
421 unsigned long pending = int_sts;
422
423 for_each_set_bit(offset, &pending, 32) {
424 unsigned int gpio_irq =
425 irq_find_mapping(gpio->chip.irqdomain,
426 offset);
427 generic_handle_irq(gpio_irq);
428 }
429
430 /* clear IRQ in HW */
431 writel_relaxed(int_sts, gpio->base_addr +
432 ZYNQ_GPIO_INTSTS_OFFSET(bank_num));
433 }
434 }
435
436 chained_irq_exit(irqchip, desc);
437}
438
439static int __maybe_unused zynq_gpio_suspend(struct device *dev)
440{
441 if (!device_may_wakeup(dev))
442 return pm_runtime_force_suspend(dev);
443
444 return 0;
445}
446
447static int __maybe_unused zynq_gpio_resume(struct device *dev)
448{
449 if (!device_may_wakeup(dev))
450 return pm_runtime_force_resume(dev);
451
452 return 0;
453}
454
455static int __maybe_unused zynq_gpio_runtime_suspend(struct device *dev)
456{
457 struct platform_device *pdev = to_platform_device(dev);
458 struct zynq_gpio *gpio = platform_get_drvdata(pdev);
459
460 clk_disable_unprepare(gpio->clk);
461
462 return 0;
463}
464
465static int __maybe_unused zynq_gpio_runtime_resume(struct device *dev)
466{
467 struct platform_device *pdev = to_platform_device(dev);
468 struct zynq_gpio *gpio = platform_get_drvdata(pdev);
469
470 return clk_prepare_enable(gpio->clk);
471}
472
473static int zynq_gpio_request(struct gpio_chip *chip, unsigned offset)
474{
475 int ret;
476
477 ret = pm_runtime_get_sync(chip->dev);
478
479 /*
480 * If the device is already active pm_runtime_get() will return 1 on
481 * success, but gpio_request still needs to return 0.
482 */
483 return ret < 0 ? ret : 0;
484}
485
486static void zynq_gpio_free(struct gpio_chip *chip, unsigned offset)
487{
488 pm_runtime_put(chip->dev);
489}
490
491static const struct dev_pm_ops zynq_gpio_dev_pm_ops = {
492 SET_SYSTEM_SLEEP_PM_OPS(zynq_gpio_suspend, zynq_gpio_resume)
493 SET_PM_RUNTIME_PM_OPS(zynq_gpio_runtime_suspend,
494 zynq_gpio_runtime_resume, NULL)
495};
496
497/**
498 * zynq_gpio_probe - Initialization method for a zynq_gpio device
499 * @pdev: platform device instance
500 *
501 * This function allocates memory resources for the gpio device and registers
502 * all the banks of the device. It will also set up interrupts for the gpio
503 * pins.
504 * Note: Interrupts are disabled for all the banks during initialization.
505 *
506 * Return: 0 on success, negative error otherwise.
507 */
508static int zynq_gpio_probe(struct platform_device *pdev)
509{
510 int ret, bank_num, irq;
511 struct zynq_gpio *gpio;
512 struct gpio_chip *chip;
513 struct resource *res;
514
515 gpio = devm_kzalloc(&pdev->dev, sizeof(*gpio), GFP_KERNEL);
516 if (!gpio)
517 return -ENOMEM;
518
519 platform_set_drvdata(pdev, gpio);
520
521 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
522 gpio->base_addr = devm_ioremap_resource(&pdev->dev, res);
523 if (IS_ERR(gpio->base_addr))
524 return PTR_ERR(gpio->base_addr);
525
526 irq = platform_get_irq(pdev, 0);
527 if (irq < 0) {
528 dev_err(&pdev->dev, "invalid IRQ\n");
529 return irq;
530 }
531
532 /* configure the gpio chip */
533 chip = &gpio->chip;
534 chip->label = "zynq_gpio";
535 chip->owner = THIS_MODULE;
536 chip->dev = &pdev->dev;
537 chip->get = zynq_gpio_get_value;
538 chip->set = zynq_gpio_set_value;
539 chip->request = zynq_gpio_request;
540 chip->free = zynq_gpio_free;
541 chip->direction_input = zynq_gpio_dir_in;
542 chip->direction_output = zynq_gpio_dir_out;
543 chip->base = -1;
544 chip->ngpio = ZYNQ_GPIO_NR_GPIOS;
545
546 /* Enable GPIO clock */
547 gpio->clk = devm_clk_get(&pdev->dev, NULL);
548 if (IS_ERR(gpio->clk)) {
549 dev_err(&pdev->dev, "input clock not found.\n");
550 return PTR_ERR(gpio->clk);
551 }
552 ret = clk_prepare_enable(gpio->clk);
553 if (ret) {
554 dev_err(&pdev->dev, "Unable to enable clock.\n");
555 return ret;
556 }
557
558 /* report a bug if gpio chip registration fails */
559 ret = gpiochip_add(chip);
560 if (ret) {
561 dev_err(&pdev->dev, "Failed to add gpio chip\n");
562 goto err_disable_clk;
563 }
564
565 /* disable interrupts for all banks */
566 for (bank_num = 0; bank_num < ZYNQ_GPIO_MAX_BANK; bank_num++)
567 writel_relaxed(ZYNQ_GPIO_IXR_DISABLE_ALL, gpio->base_addr +
568 ZYNQ_GPIO_INTDIS_OFFSET(bank_num));
569
570 ret = gpiochip_irqchip_add(chip, &zynq_gpio_irqchip, 0,
571 handle_simple_irq, IRQ_TYPE_NONE);
572 if (ret) {
573 dev_err(&pdev->dev, "Failed to add irq chip\n");
574 goto err_rm_gpiochip;
575 }
576
577 gpiochip_set_chained_irqchip(chip, &zynq_gpio_irqchip, irq,
578 zynq_gpio_irqhandler);
579
580 pm_runtime_set_active(&pdev->dev);
581 pm_runtime_enable(&pdev->dev);
582
583 device_set_wakeup_capable(&pdev->dev, 1);
584
585 return 0;
586
587err_rm_gpiochip:
588 if (gpiochip_remove(chip))
589 dev_err(&pdev->dev, "Failed to remove gpio chip\n");
590err_disable_clk:
591 clk_disable_unprepare(gpio->clk);
592
593 return ret;
594}
595
596/**
597 * zynq_gpio_remove - Driver removal function
598 * @pdev: platform device instance
599 *
600 * Return: 0 always
601 */
602static int zynq_gpio_remove(struct platform_device *pdev)
603{
604 int ret;
605 struct zynq_gpio *gpio = platform_get_drvdata(pdev);
606
607 pm_runtime_get_sync(&pdev->dev);
608
609 ret = gpiochip_remove(&gpio->chip);
610 if (ret) {
611 dev_err(&pdev->dev, "Failed to remove gpio chip\n");
612 return ret;
613 }
614 clk_disable_unprepare(gpio->clk);
615 device_set_wakeup_capable(&pdev->dev, 0);
616 return 0;
617}
618
619static struct of_device_id zynq_gpio_of_match[] = {
620 { .compatible = "xlnx,zynq-gpio-1.0", },
621 { /* end of table */ }
622};
623MODULE_DEVICE_TABLE(of, zynq_gpio_of_match);
624
625static struct platform_driver zynq_gpio_driver = {
626 .driver = {
627 .name = DRIVER_NAME,
628 .owner = THIS_MODULE,
629 .pm = &zynq_gpio_dev_pm_ops,
630 .of_match_table = zynq_gpio_of_match,
631 },
632 .probe = zynq_gpio_probe,
633 .remove = zynq_gpio_remove,
634};
635
636/**
637 * zynq_gpio_init - Initial driver registration call
638 *
639 * Return: value from platform_driver_register
640 */
641static int __init zynq_gpio_init(void)
642{
643 return platform_driver_register(&zynq_gpio_driver);
644}
645postcore_initcall(zynq_gpio_init);
646
647MODULE_AUTHOR("Xilinx Inc.");
648MODULE_DESCRIPTION("Zynq GPIO driver");
649MODULE_LICENSE("GPL");