]> rtime.felk.cvut.cz Git - zynq/linux.git/blob - drivers/gpio/gpio-xilinx.c
d0f0a0ec09d8a28c3e9647714b557740c4de7be9
[zynq/linux.git] / drivers / gpio / gpio-xilinx.c
1 /*
2  * Xilinx gpio driver for xps/axi_gpio IP.
3  *
4  * Copyright 2008 - 2013 Xilinx, Inc.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2
8  * as published by the Free Software Foundation.
9  *
10  * You should have received a copy of the GNU General Public License
11  * along with this program; if not, write to the Free Software
12  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
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_irq.h>
21 #include <linux/of_platform.h>
22 #include <linux/of_gpio.h>
23 #include <linux/interrupt.h>
24 #include <linux/io.h>
25 #include <linux/irq.h>
26 #include <linux/irqchip/chained_irq.h>
27 #include <linux/irqdomain.h>
28 #include <linux/gpio.h>
29 #include <linux/slab.h>
30 #include <linux/pm_runtime.h>
31 #include <linux/clk.h>
32
33 /* Register Offset Definitions */
34 #define XGPIO_DATA_OFFSET       0x0 /* Data register */
35 #define XGPIO_TRI_OFFSET        0x4 /* I/O direction register */
36 #define XGPIO_GIER_OFFSET       0x11c /* Global Interrupt Enable */
37 #define XGPIO_GIER_IE           BIT(31)
38
39 #define XGPIO_IPISR_OFFSET      0x120 /* IP Interrupt Status */
40 #define XGPIO_IPIER_OFFSET      0x128 /* IP Interrupt Enable */
41
42 #define XGPIO_CHANNEL_OFFSET    0x8
43
44 /* Read/Write access to the GPIO registers */
45 #if defined(CONFIG_ARCH_ZYNQ) || defined(CONFIG_ARM64)
46 # define xgpio_readreg(offset)          readl(offset)
47 # define xgpio_writereg(offset, val)    writel(val, offset)
48 #else
49 # define xgpio_readreg(offset)          __raw_readl(offset)
50 # define xgpio_writereg(offset, val)    __raw_writel(val, offset)
51 #endif
52
53 /**
54  * struct xgpio_instance - Stores information about GPIO device
55  * @mmchip: OF GPIO chip for memory mapped banks
56  * @gpio_state: GPIO state shadow register
57  * @gpio_dir: GPIO direction shadow register
58  * @offset: GPIO channel offset
59  * @irq_base: GPIO channel irq base address
60  * @irq_enable: GPIO irq enable/disable bitfield
61  * @no_init: No intitialisation at probe
62  * @gpio_lock: Lock used for synchronization
63  * @irq_domain: irq_domain of the controller
64  * @clk: clock resource for this driver
65  */
66 struct xgpio_instance {
67         struct of_mm_gpio_chip mmchip;
68         u32 gpio_state;
69         u32 gpio_dir;
70         u32 offset;
71         int irq_base;
72         u32 irq_enable;
73         bool no_init;
74         spinlock_t gpio_lock;
75         struct irq_domain *irq_domain;
76         struct clk *clk;
77 };
78
79 /**
80  * xgpio_get - Read the specified signal of the GPIO device.
81  * @gc:     Pointer to gpio_chip device structure.
82  * @gpio:   GPIO signal number.
83  *
84  * This function reads the specified signal of the GPIO device.
85  *
86  * Return:
87  * 0 if direction of GPIO signals is set as input otherwise it
88  * returns negative error value.
89  */
90 static int xgpio_get(struct gpio_chip *gc, unsigned int gpio)
91 {
92         struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
93         struct xgpio_instance *chip =
94             container_of(mm_gc, struct xgpio_instance, mmchip);
95
96         void __iomem *regs = mm_gc->regs + chip->offset;
97
98         return !!(xgpio_readreg(regs + XGPIO_DATA_OFFSET) & BIT(gpio));
99 }
100
101 /**
102  * xgpio_set - Write the specified signal of the GPIO device.
103  * @gc:     Pointer to gpio_chip device structure.
104  * @gpio:   GPIO signal number.
105  * @val:    Value to be written to specified signal.
106  *
107  * This function writes the specified value in to the specified signal of the
108  * GPIO device.
109  */
110 static void xgpio_set(struct gpio_chip *gc, unsigned int gpio, int val)
111 {
112         unsigned long flags;
113         struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
114         struct xgpio_instance *chip =
115             container_of(mm_gc, struct xgpio_instance, mmchip);
116         void __iomem *regs = mm_gc->regs;
117
118         spin_lock_irqsave(&chip->gpio_lock, flags);
119
120         /* Write to GPIO signal and set its direction to output */
121         if (val)
122                 chip->gpio_state |= BIT(gpio);
123         else
124                 chip->gpio_state &= ~BIT(gpio);
125
126         xgpio_writereg(regs + chip->offset + XGPIO_DATA_OFFSET,
127                                                          chip->gpio_state);
128
129         spin_unlock_irqrestore(&chip->gpio_lock, flags);
130 }
131
132 /**
133  * xgpio_set_multiple - Write the specified signals of the GPIO device.
134  * @gc:     Pointer to gpio_chip device structure.
135  * @mask:   Mask of the GPIOS to modify.
136  * @bits:   Value to be wrote on each GPIO
137  *
138  * This function writes the specified values into the specified signals of the
139  * GPIO devices.
140  */
141 static void xgpio_set_multiple(struct gpio_chip *gc, unsigned long *mask,
142                                unsigned long *bits)
143 {
144         unsigned long flags;
145         struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
146         struct xgpio_instance *chip =
147             container_of(mm_gc, struct xgpio_instance, mmchip);
148         void __iomem *regs = mm_gc->regs;
149         int i;
150
151         spin_lock_irqsave(&chip->gpio_lock, flags);
152
153         /* Write to GPIO signals */
154         for (i = 0; i < gc->ngpio; i++) {
155                 if (*mask == 0)
156                         break;
157                 if (__test_and_clear_bit(i, mask)) {
158                         if (test_bit(i, bits))
159                                 chip->gpio_state |= BIT(i);
160                         else
161                                 chip->gpio_state &= ~BIT(i);
162                 }
163         }
164
165         xgpio_writereg(regs + chip->offset + XGPIO_DATA_OFFSET,
166                        chip->gpio_state);
167
168         spin_unlock_irqrestore(&chip->gpio_lock, flags);
169 }
170
171 /**
172  * xgpio_dir_in - Set the direction of the specified GPIO signal as input.
173  * @gc:     Pointer to gpio_chip device structure.
174  * @gpio:   GPIO signal number.
175  *
176  * This function sets the direction of specified GPIO signal as input.
177  *
178  * Return:
179  * 0 - if direction of GPIO signals is set as input
180  * otherwise it returns negative error value.
181  */
182 static int xgpio_dir_in(struct gpio_chip *gc, unsigned int gpio)
183 {
184         unsigned long flags;
185         struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
186         struct xgpio_instance *chip =
187             container_of(mm_gc, struct xgpio_instance, mmchip);
188         void __iomem *regs = mm_gc->regs;
189
190         spin_lock_irqsave(&chip->gpio_lock, flags);
191
192         /* Set the GPIO bit in shadow register and set direction as input */
193         chip->gpio_dir |= BIT(gpio);
194         xgpio_writereg(regs + chip->offset + XGPIO_TRI_OFFSET, chip->gpio_dir);
195
196         spin_unlock_irqrestore(&chip->gpio_lock, flags);
197
198         return 0;
199 }
200
201 /**
202  * xgpio_dir_out - Set the direction of the specified GPIO signal as output.
203  * @gc:     Pointer to gpio_chip device structure.
204  * @gpio:   GPIO signal number.
205  * @val:    Value to be written to specified signal.
206  *
207  * This function sets the direction of specified GPIO signal as output.
208  *
209  * Return:
210  * If all GPIO signals of GPIO chip is configured as input then it returns
211  * error otherwise it returns 0.
212  */
213 static int xgpio_dir_out(struct gpio_chip *gc, unsigned int gpio, int val)
214 {
215         unsigned long flags;
216         struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
217         struct xgpio_instance *chip =
218             container_of(mm_gc, struct xgpio_instance, mmchip);
219         void __iomem *regs = mm_gc->regs;
220
221         spin_lock_irqsave(&chip->gpio_lock, flags);
222
223         /* Write state of GPIO signal */
224         if (val)
225                 chip->gpio_state |= BIT(gpio);
226         else
227                 chip->gpio_state &= ~BIT(gpio);
228         xgpio_writereg(regs + chip->offset + XGPIO_DATA_OFFSET,
229                        chip->gpio_state);
230
231         /* Clear the GPIO bit in shadow register and set direction as output */
232         chip->gpio_dir &= ~BIT(gpio);
233         xgpio_writereg(regs + chip->offset + XGPIO_TRI_OFFSET, chip->gpio_dir);
234
235         spin_unlock_irqrestore(&chip->gpio_lock, flags);
236
237         return 0;
238 }
239
240 /**
241  * xgpio_save_regs - Set initial values of GPIO pins
242  * @mm_gc: Pointer to memory mapped GPIO chip structure
243  */
244 static void xgpio_save_regs(struct of_mm_gpio_chip *mm_gc)
245 {
246         struct xgpio_instance *chip =
247             container_of(mm_gc, struct xgpio_instance, mmchip);
248         if (chip->no_init) {
249                 chip->gpio_state = xgpio_readreg(mm_gc->regs +
250                                                  XGPIO_DATA_OFFSET);
251                 chip->gpio_dir = xgpio_readreg(mm_gc->regs + XGPIO_TRI_OFFSET);
252         } else {
253                 xgpio_writereg(mm_gc->regs + chip->offset + XGPIO_DATA_OFFSET,
254                                chip->gpio_state);
255                 xgpio_writereg(mm_gc->regs + chip->offset + XGPIO_TRI_OFFSET,
256                                chip->gpio_dir);
257         }
258 }
259
260 /**
261  * xgpio_xlate - Translate gpio_spec to the GPIO number and flags
262  * @gc: Pointer to gpio_chip device structure.
263  * @gpiospec:  gpio specifier as found in the device tree
264  * @flags: A flags pointer based on binding
265  *
266  * Return:
267  * irq number otherwise -EINVAL
268  */
269 static int xgpio_xlate(struct gpio_chip *gc,
270                        const struct of_phandle_args *gpiospec, u32 *flags)
271 {
272         struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
273         struct xgpio_instance *chip = container_of(mm_gc, struct xgpio_instance,
274                                                    mmchip);
275         if (gc->of_gpio_n_cells == 3 && flags)
276                 *flags = gpiospec->args[2];
277
278         if (gpiospec->args[1] == chip->offset)
279                 return gpiospec->args[0];
280
281         return -EINVAL;
282 }
283
284 /**
285  * xgpio_irq_mask - Write the specified signal of the GPIO device.
286  * @irq_data: per irq and chip data passed down to chip functions
287  */
288 static void xgpio_irq_mask(struct irq_data *irq_data)
289 {
290         unsigned long flags;
291         struct xgpio_instance *chip = irq_data_get_irq_chip_data(irq_data);
292         struct of_mm_gpio_chip *mm_gc = &chip->mmchip;
293         u32 offset = irq_data->irq - chip->irq_base;
294         u32 temp;
295
296         pr_debug("%s: Disable %d irq, irq_enable_mask 0x%x\n",
297                 __func__, offset, chip->irq_enable);
298
299         spin_lock_irqsave(&chip->gpio_lock, flags);
300
301         chip->irq_enable &= ~BIT(offset);
302
303         if (!chip->irq_enable) {
304                 /* Enable per channel interrupt */
305                 temp = xgpio_readreg(mm_gc->regs + XGPIO_IPIER_OFFSET);
306                 temp &= chip->offset / XGPIO_CHANNEL_OFFSET + 1;
307                 xgpio_writereg(mm_gc->regs + XGPIO_IPIER_OFFSET, temp);
308
309                 /* Disable global interrupt if channel interrupts are unused */
310                 temp = xgpio_readreg(mm_gc->regs + XGPIO_IPIER_OFFSET);
311                 if (!temp)
312                         xgpio_writereg(mm_gc->regs + XGPIO_GIER_OFFSET,
313                                        ~XGPIO_GIER_IE);
314
315         }
316         spin_unlock_irqrestore(&chip->gpio_lock, flags);
317 }
318
319 /**
320  * xgpio_irq_unmask - Write the specified signal of the GPIO device.
321  * @irq_data: per irq and chip data passed down to chip functions
322  */
323 static void xgpio_irq_unmask(struct irq_data *irq_data)
324 {
325         unsigned long flags;
326         struct xgpio_instance *chip = irq_data_get_irq_chip_data(irq_data);
327         struct of_mm_gpio_chip *mm_gc = &chip->mmchip;
328         u32 offset = irq_data->irq - chip->irq_base;
329         u32 temp;
330
331         pr_debug("%s: Enable %d irq, irq_enable_mask 0x%x\n",
332                 __func__, offset, chip->irq_enable);
333
334         /* Setup pin as input */
335         xgpio_dir_in(&mm_gc->gc, offset);
336
337         spin_lock_irqsave(&chip->gpio_lock, flags);
338
339         chip->irq_enable |= BIT(offset);
340
341         if (chip->irq_enable) {
342
343                 /* Enable per channel interrupt */
344                 temp = xgpio_readreg(mm_gc->regs + XGPIO_IPIER_OFFSET);
345                 temp |= chip->offset / XGPIO_CHANNEL_OFFSET + 1;
346                 xgpio_writereg(mm_gc->regs + XGPIO_IPIER_OFFSET, temp);
347
348                 /* Enable global interrupts */
349                 xgpio_writereg(mm_gc->regs + XGPIO_GIER_OFFSET, XGPIO_GIER_IE);
350         }
351
352         spin_unlock_irqrestore(&chip->gpio_lock, flags);
353 }
354
355 /**
356  * xgpio_set_irq_type - Write the specified signal of the GPIO device.
357  * @irq_data: Per irq and chip data passed down to chip functions
358  * @type: Interrupt type that is to be set for the gpio pin
359  *
360  * Return:
361  * 0 if interrupt type is supported otherwise otherwise -EINVAL
362  */
363 static int xgpio_set_irq_type(struct irq_data *irq_data, unsigned int type)
364 {
365         /* Only rising edge case is supported now */
366         if (type & IRQ_TYPE_EDGE_RISING)
367                 return 0;
368
369         return -EINVAL;
370 }
371
372 /* irq chip descriptor */
373 static struct irq_chip xgpio_irqchip = {
374         .name           = "xgpio",
375         .irq_mask       = xgpio_irq_mask,
376         .irq_unmask     = xgpio_irq_unmask,
377         .irq_set_type   = xgpio_set_irq_type,
378 };
379
380 /**
381  * xgpio_to_irq - Find out gpio to Linux irq mapping
382  * @gc: Pointer to gpio_chip device structure.
383  * @offset: Gpio pin offset
384  *
385  * Return:
386  * irq number otherwise -EINVAL
387  */
388 static int xgpio_to_irq(struct gpio_chip *gc, unsigned int offset)
389 {
390         struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
391         struct xgpio_instance *chip = container_of(mm_gc, struct xgpio_instance,
392                                                    mmchip);
393
394         return irq_find_mapping(chip->irq_domain, offset);
395 }
396
397 /**
398  * xgpio_irqhandler - Gpio interrupt service routine
399  * @desc: Pointer to interrupt description
400  */
401 static void xgpio_irqhandler(struct irq_desc *desc)
402 {
403         unsigned int irq = irq_desc_get_irq(desc);
404
405         struct xgpio_instance *chip = (struct xgpio_instance *)
406                                                 irq_get_handler_data(irq);
407         struct of_mm_gpio_chip *mm_gc = &chip->mmchip;
408         struct irq_chip *irqchip = irq_desc_get_chip(desc);
409         int offset;
410         unsigned long val;
411
412         chained_irq_enter(irqchip, desc);
413
414         val = xgpio_readreg(mm_gc->regs + chip->offset);
415         /* Only rising edge is supported */
416         val &= chip->irq_enable;
417
418         for_each_set_bit(offset, &val, chip->mmchip.gc.ngpio) {
419                 generic_handle_irq(chip->irq_base + offset);
420         }
421
422         xgpio_writereg(mm_gc->regs + XGPIO_IPISR_OFFSET,
423                        chip->offset / XGPIO_CHANNEL_OFFSET + 1);
424
425         chained_irq_exit(irqchip, desc);
426 }
427
428 static struct lock_class_key gpio_lock_class;
429
430 /**
431  * xgpio_irq_setup - Allocate irq for gpio and setup appropriate functions
432  * @np: Device node of the GPIO chip
433  * @chip: Pointer to private gpio channel structure
434  *
435  * Return:
436  * 0 if success, otherwise -1
437  */
438 static int xgpio_irq_setup(struct device_node *np, struct xgpio_instance *chip)
439 {
440         u32 pin_num;
441         struct resource res;
442
443         int ret = of_irq_to_resource(np, 0, &res);
444
445         if (!ret) {
446                 pr_info("GPIO IRQ not connected\n");
447                 return 0;
448         }
449
450         chip->mmchip.gc.to_irq = xgpio_to_irq;
451
452         chip->irq_base = irq_alloc_descs(-1, 0, chip->mmchip.gc.ngpio, 0);
453         if (chip->irq_base < 0) {
454                 pr_err("Couldn't allocate IRQ numbers\n");
455                 return -1;
456         }
457         chip->irq_domain = irq_domain_add_legacy(np, chip->mmchip.gc.ngpio,
458                                                  chip->irq_base, 0,
459                                                  &irq_domain_simple_ops, NULL);
460
461         /*
462          * set the irq chip, handler and irq chip data for callbacks for
463          * each pin
464          */
465         for (pin_num = 0; pin_num < chip->mmchip.gc.ngpio; pin_num++) {
466                 u32 gpio_irq = irq_find_mapping(chip->irq_domain, pin_num);
467
468                 irq_set_lockdep_class(gpio_irq, &gpio_lock_class);
469                 pr_debug("IRQ Base: %d, Pin %d = IRQ %d\n",
470                         chip->irq_base, pin_num, gpio_irq);
471                 irq_set_chip_and_handler(gpio_irq, &xgpio_irqchip,
472                                          handle_simple_irq);
473                 irq_set_chip_data(gpio_irq, (void *)chip);
474         }
475         irq_set_handler_data(res.start, (void *)chip);
476         irq_set_chained_handler(res.start, xgpio_irqhandler);
477
478         return 0;
479 }
480
481 static int xgpio_request(struct gpio_chip *chip, unsigned int offset)
482 {
483         int ret = pm_runtime_get_sync(chip->parent);
484
485         /*
486          * If the device is already active pm_runtime_get() will return 1 on
487          * success, but gpio_request still needs to return 0.
488          */
489         return ret < 0 ? ret : 0;
490 }
491
492 static void xgpio_free(struct gpio_chip *chip, unsigned int offset)
493 {
494         pm_runtime_put(chip->parent);
495 }
496
497 static int __maybe_unused xgpio_suspend(struct device *dev)
498 {
499         struct platform_device *pdev = to_platform_device(dev);
500         int irq;
501         struct irq_data *data;
502
503         irq = platform_get_irq(pdev, 0);
504         if (irq <= 0) {
505                 dev_dbg(dev, "failed to get IRQ\n");
506                 return 0;
507         }
508
509         data = irq_get_irq_data(irq);
510         if (!irqd_is_wakeup_set(data))
511                 return pm_runtime_force_suspend(dev);
512
513         return 0;
514 }
515
516 static int __maybe_unused xgpio_resume(struct device *dev)
517 {
518         struct platform_device *pdev = to_platform_device(dev);
519         int irq;
520         struct irq_data *data;
521
522
523         irq = platform_get_irq(pdev, 0);
524         if (irq <= 0) {
525                 dev_dbg(dev, "failed to get IRQ\n");
526                 return 0;
527         }
528
529         data = irq_get_irq_data(irq);
530         if (!irqd_is_wakeup_set(data))
531                 return pm_runtime_force_resume(dev);
532
533         return 0;
534 }
535
536 static int __maybe_unused xgpio_runtime_suspend(struct device *dev)
537 {
538         struct platform_device *pdev = to_platform_device(dev);
539         struct xgpio_instance *gpio = platform_get_drvdata(pdev);
540
541         clk_disable(gpio->clk);
542
543         return 0;
544 }
545
546 static int __maybe_unused xgpio_runtime_resume(struct device *dev)
547 {
548         struct platform_device *pdev = to_platform_device(dev);
549         struct xgpio_instance *gpio = platform_get_drvdata(pdev);
550
551         return clk_enable(gpio->clk);
552 }
553
554 static const struct dev_pm_ops xgpio_dev_pm_ops = {
555         SET_SYSTEM_SLEEP_PM_OPS(xgpio_suspend, xgpio_resume)
556         SET_RUNTIME_PM_OPS(xgpio_runtime_suspend,
557                         xgpio_runtime_resume, NULL)
558 };
559
560 /**
561  * xgpio_remove - Remove method for the GPIO device.
562  * @pdev: pointer to the platform device
563  *
564  * This function remove gpiochips and frees all the allocated resources.
565  *
566  * Return: 0 always
567  */
568 static int xgpio_remove(struct platform_device *pdev)
569 {
570         struct xgpio_instance *chip = platform_get_drvdata(pdev);
571
572         of_mm_gpiochip_remove(&chip->mmchip);
573         clk_disable_unprepare(chip->clk);
574         pm_runtime_disable(&pdev->dev);
575
576         return 0;
577 }
578
579 /**
580  * xgpio_of_probe - Probe method for the GPIO device.
581  * @pdev:       platform device instance
582  *
583  * This function probes the GPIO device in the device tree. It initializes the
584  * driver data structure.
585  *
586  * Return:
587  * It returns 0, if the driver is bound to the GPIO device, or
588  * a negative value if there is an error.
589  */
590 static int xgpio_of_probe(struct platform_device *pdev)
591 {
592         struct device_node *np = pdev->dev.of_node;
593         struct xgpio_instance *chip;
594         int status = 0;
595         const u32 *tree_info;
596         u32 ngpio;
597         u32 cells = 2;
598
599         chip = devm_kzalloc(&pdev->dev, sizeof(*chip), GFP_KERNEL);
600         if (!chip)
601                 return -ENOMEM;
602
603         /* Update GPIO state shadow register with default value */
604         of_property_read_u32(np, "xlnx,dout-default", &chip->gpio_state);
605
606         /* By default, all pins are inputs */
607         chip->gpio_dir = 0xFFFFFFFF;
608
609         /* Update GPIO direction shadow register with default value */
610         of_property_read_u32(np, "xlnx,tri-default", &chip->gpio_dir);
611
612         chip->no_init = of_property_read_bool(np, "xlnx,no-init");
613
614         /* Update cells with gpio-cells value */
615         of_property_read_u32(np, "#gpio-cells", &cells);
616
617         /*
618          * Check device node and parent device node for device width
619          * and assume default width of 32
620          */
621         if (of_property_read_u32(np, "xlnx,gpio-width", &ngpio))
622                 ngpio = 32;
623         chip->mmchip.gc.ngpio = (u16)ngpio;
624
625         spin_lock_init(&chip->gpio_lock);
626
627         chip->mmchip.gc.parent = &pdev->dev;
628         chip->mmchip.gc.owner = THIS_MODULE;
629         chip->mmchip.gc.of_xlate = xgpio_xlate;
630         chip->mmchip.gc.of_gpio_n_cells = cells;
631         chip->mmchip.gc.direction_input = xgpio_dir_in;
632         chip->mmchip.gc.direction_output = xgpio_dir_out;
633         chip->mmchip.gc.get = xgpio_get;
634         chip->mmchip.gc.set = xgpio_set;
635         chip->mmchip.gc.request = xgpio_request;
636         chip->mmchip.gc.free = xgpio_free;
637         chip->mmchip.gc.set_multiple = xgpio_set_multiple;
638
639         chip->mmchip.save_regs = xgpio_save_regs;
640
641         platform_set_drvdata(pdev, chip);
642
643         chip->clk = devm_clk_get(&pdev->dev, "s_axi_aclk");
644         if (IS_ERR(chip->clk)) {
645                 if ((PTR_ERR(chip->clk) != -ENOENT) ||
646                                 (PTR_ERR(chip->clk) != -EPROBE_DEFER)) {
647                         dev_err(&pdev->dev, "Input clock not found\n");
648                         return PTR_ERR(chip->clk);
649                 }
650
651                 /*
652                  * Clock framework support is optional, continue on
653                  * anyways if we don't find a matching clock.
654                  */
655                 chip->clk = NULL;
656         }
657
658         status = clk_prepare_enable(chip->clk);
659         if (status < 0) {
660                 dev_err(&pdev->dev, "Failed to prepare clk\n");
661                 return status;
662         }
663
664         pm_runtime_enable(&pdev->dev);
665         status = pm_runtime_get_sync(&pdev->dev);
666         if (status < 0)
667                 goto err_unprepare_clk;
668
669         /* Call the OF gpio helper to setup and register the GPIO device */
670         status = of_mm_gpiochip_add(np, &chip->mmchip);
671         if (status) {
672                 pr_err("%pOF: error in probe function with status %d\n",
673                        np, status);
674                 goto err_pm_put;
675         }
676
677         status = xgpio_irq_setup(np, chip);
678         if (status) {
679                 pr_err("%s: GPIO IRQ initialization failed %d\n",
680                        np->full_name, status);
681                 goto err_pm_put;
682         }
683
684         pr_info("XGpio: %s: registered, base is %d\n", np->full_name,
685                                                         chip->mmchip.gc.base);
686
687         tree_info = of_get_property(np, "xlnx,is-dual", NULL);
688         if (tree_info && be32_to_cpup(tree_info)) {
689                 chip = devm_kzalloc(&pdev->dev, sizeof(*chip), GFP_KERNEL);
690                 if (!chip)
691                         return -ENOMEM;
692
693                 /* Add dual channel offset */
694                 chip->offset = XGPIO_CHANNEL_OFFSET;
695
696                 /* Update GPIO state shadow register with default value */
697                 of_property_read_u32(np, "xlnx,dout-default-2",
698                                      &chip->gpio_state);
699
700                 /* By default, all pins are inputs */
701                 chip->gpio_dir = 0xFFFFFFFF;
702
703                 /* Update GPIO direction shadow register with default value */
704                 of_property_read_u32(np, "xlnx,tri-default-2", &chip->gpio_dir);
705
706                 /*
707                  * Check device node and parent device node for device width
708                  * and assume default width of 32
709                  */
710                 if (of_property_read_u32(np, "xlnx,gpio2-width", &ngpio))
711                         ngpio = 32;
712                 chip->mmchip.gc.ngpio = (u16)ngpio;
713
714                 spin_lock_init(&chip->gpio_lock);
715
716                 chip->mmchip.gc.parent = &pdev->dev;
717                 chip->mmchip.gc.owner = THIS_MODULE;
718                 chip->mmchip.gc.of_xlate = xgpio_xlate;
719                 chip->mmchip.gc.of_gpio_n_cells = cells;
720                 chip->mmchip.gc.direction_input = xgpio_dir_in;
721                 chip->mmchip.gc.direction_output = xgpio_dir_out;
722                 chip->mmchip.gc.get = xgpio_get;
723                 chip->mmchip.gc.set = xgpio_set;
724                 chip->mmchip.gc.request = xgpio_request;
725                 chip->mmchip.gc.free = xgpio_free;
726                 chip->mmchip.gc.set_multiple = xgpio_set_multiple;
727
728                 chip->mmchip.save_regs = xgpio_save_regs;
729
730                 status = xgpio_irq_setup(np, chip);
731                 if (status) {
732                         pr_err("%s: GPIO IRQ initialization failed %d\n",
733                               np->full_name, status);
734                         goto err_pm_put;
735                 }
736
737                 /* Call the OF gpio helper to setup and register the GPIO dev */
738                 status = of_mm_gpiochip_add(np, &chip->mmchip);
739                 if (status) {
740                         pr_err("%s: error in probe function with status %d\n",
741                                np->full_name, status);
742                         goto err_pm_put;
743                 }
744                 pr_info("XGpio: %s: dual channel registered, base is %d\n",
745                                         np->full_name, chip->mmchip.gc.base);
746         }
747
748         pm_runtime_put(&pdev->dev);
749         return 0;
750
751 err_pm_put:
752         pm_runtime_put(&pdev->dev);
753 err_unprepare_clk:
754         pm_runtime_disable(&pdev->dev);
755         clk_disable_unprepare(chip->clk);
756         return status;
757 }
758
759 static const struct of_device_id xgpio_of_match[] = {
760         { .compatible = "xlnx,xps-gpio-1.00.a", },
761         { /* end of list */ },
762 };
763 MODULE_DEVICE_TABLE(of, xgpio_of_match);
764
765 static struct platform_driver xilinx_gpio_driver = {
766         .probe = xgpio_of_probe,
767         .remove = xgpio_remove,
768         .driver = {
769                 .name = "xilinx-gpio",
770                 .of_match_table = xgpio_of_match,
771                 .pm = &xgpio_dev_pm_ops,
772         },
773 };
774
775 static int __init xgpio_init(void)
776 {
777         return platform_driver_register(&xilinx_gpio_driver);
778 }
779
780 /* Make sure we get initialized before anyone else tries to use us */
781 subsys_initcall(xgpio_init);
782
783 static void __exit xgpio_exit(void)
784 {
785         platform_driver_unregister(&xilinx_gpio_driver);
786 }
787 module_exit(xgpio_exit);
788
789 MODULE_AUTHOR("Xilinx, Inc.");
790 MODULE_DESCRIPTION("Xilinx GPIO driver");
791 MODULE_LICENSE("GPL");