Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Xilinx gpio driver for xps/axi_gpio IP.
0004  *
0005  * Copyright 2008 - 2013 Xilinx, Inc.
0006  */
0007 
0008 #include <linux/bitmap.h>
0009 #include <linux/bitops.h>
0010 #include <linux/clk.h>
0011 #include <linux/errno.h>
0012 #include <linux/gpio/driver.h>
0013 #include <linux/init.h>
0014 #include <linux/interrupt.h>
0015 #include <linux/io.h>
0016 #include <linux/irq.h>
0017 #include <linux/module.h>
0018 #include <linux/of_device.h>
0019 #include <linux/of_platform.h>
0020 #include <linux/pm_runtime.h>
0021 #include <linux/slab.h>
0022 
0023 /* Register Offset Definitions */
0024 #define XGPIO_DATA_OFFSET   (0x0)   /* Data register  */
0025 #define XGPIO_TRI_OFFSET    (0x4)   /* I/O direction register  */
0026 
0027 #define XGPIO_CHANNEL0_OFFSET   0x0
0028 #define XGPIO_CHANNEL1_OFFSET   0x8
0029 
0030 #define XGPIO_GIER_OFFSET   0x11c /* Global Interrupt Enable */
0031 #define XGPIO_GIER_IE       BIT(31)
0032 #define XGPIO_IPISR_OFFSET  0x120 /* IP Interrupt Status */
0033 #define XGPIO_IPIER_OFFSET  0x128 /* IP Interrupt Enable */
0034 
0035 /* Read/Write access to the GPIO registers */
0036 #if defined(CONFIG_ARCH_ZYNQ) || defined(CONFIG_X86)
0037 # define xgpio_readreg(offset)      readl(offset)
0038 # define xgpio_writereg(offset, val)    writel(val, offset)
0039 #else
0040 # define xgpio_readreg(offset)      __raw_readl(offset)
0041 # define xgpio_writereg(offset, val)    __raw_writel(val, offset)
0042 #endif
0043 
0044 /**
0045  * struct xgpio_instance - Stores information about GPIO device
0046  * @gc: GPIO chip
0047  * @regs: register block
0048  * @hw_map: GPIO pin mapping on hardware side
0049  * @sw_map: GPIO pin mapping on software side
0050  * @state: GPIO write state shadow register
0051  * @last_irq_read: GPIO read state register from last interrupt
0052  * @dir: GPIO direction shadow register
0053  * @gpio_lock: Lock used for synchronization
0054  * @irq: IRQ used by GPIO device
0055  * @irqchip: IRQ chip
0056  * @enable: GPIO IRQ enable/disable bitfield
0057  * @rising_edge: GPIO IRQ rising edge enable/disable bitfield
0058  * @falling_edge: GPIO IRQ falling edge enable/disable bitfield
0059  * @clk: clock resource for this driver
0060  */
0061 struct xgpio_instance {
0062     struct gpio_chip gc;
0063     void __iomem *regs;
0064     DECLARE_BITMAP(hw_map, 64);
0065     DECLARE_BITMAP(sw_map, 64);
0066     DECLARE_BITMAP(state, 64);
0067     DECLARE_BITMAP(last_irq_read, 64);
0068     DECLARE_BITMAP(dir, 64);
0069     spinlock_t gpio_lock;   /* For serializing operations */
0070     int irq;
0071     struct irq_chip irqchip;
0072     DECLARE_BITMAP(enable, 64);
0073     DECLARE_BITMAP(rising_edge, 64);
0074     DECLARE_BITMAP(falling_edge, 64);
0075     struct clk *clk;
0076 };
0077 
0078 static inline int xgpio_from_bit(struct xgpio_instance *chip, int bit)
0079 {
0080     return bitmap_bitremap(bit, chip->hw_map, chip->sw_map, 64);
0081 }
0082 
0083 static inline int xgpio_to_bit(struct xgpio_instance *chip, int gpio)
0084 {
0085     return bitmap_bitremap(gpio, chip->sw_map, chip->hw_map, 64);
0086 }
0087 
0088 static inline u32 xgpio_get_value32(const unsigned long *map, int bit)
0089 {
0090     const size_t index = BIT_WORD(bit);
0091     const unsigned long offset = (bit % BITS_PER_LONG) & BIT(5);
0092 
0093     return (map[index] >> offset) & 0xFFFFFFFFul;
0094 }
0095 
0096 static inline void xgpio_set_value32(unsigned long *map, int bit, u32 v)
0097 {
0098     const size_t index = BIT_WORD(bit);
0099     const unsigned long offset = (bit % BITS_PER_LONG) & BIT(5);
0100 
0101     map[index] &= ~(0xFFFFFFFFul << offset);
0102     map[index] |= (unsigned long)v << offset;
0103 }
0104 
0105 static inline int xgpio_regoffset(struct xgpio_instance *chip, int ch)
0106 {
0107     switch (ch) {
0108     case 0:
0109         return XGPIO_CHANNEL0_OFFSET;
0110     case 1:
0111         return XGPIO_CHANNEL1_OFFSET;
0112     default:
0113         return -EINVAL;
0114     }
0115 }
0116 
0117 static void xgpio_read_ch(struct xgpio_instance *chip, int reg, int bit, unsigned long *a)
0118 {
0119     void __iomem *addr = chip->regs + reg + xgpio_regoffset(chip, bit / 32);
0120 
0121     xgpio_set_value32(a, bit, xgpio_readreg(addr));
0122 }
0123 
0124 static void xgpio_write_ch(struct xgpio_instance *chip, int reg, int bit, unsigned long *a)
0125 {
0126     void __iomem *addr = chip->regs + reg + xgpio_regoffset(chip, bit / 32);
0127 
0128     xgpio_writereg(addr, xgpio_get_value32(a, bit));
0129 }
0130 
0131 static void xgpio_read_ch_all(struct xgpio_instance *chip, int reg, unsigned long *a)
0132 {
0133     int bit, lastbit = xgpio_to_bit(chip, chip->gc.ngpio - 1);
0134 
0135     for (bit = 0; bit <= lastbit ; bit += 32)
0136         xgpio_read_ch(chip, reg, bit, a);
0137 }
0138 
0139 static void xgpio_write_ch_all(struct xgpio_instance *chip, int reg, unsigned long *a)
0140 {
0141     int bit, lastbit = xgpio_to_bit(chip, chip->gc.ngpio - 1);
0142 
0143     for (bit = 0; bit <= lastbit ; bit += 32)
0144         xgpio_write_ch(chip, reg, bit, a);
0145 }
0146 
0147 /**
0148  * xgpio_get - Read the specified signal of the GPIO device.
0149  * @gc:     Pointer to gpio_chip device structure.
0150  * @gpio:   GPIO signal number.
0151  *
0152  * This function reads the specified signal of the GPIO device.
0153  *
0154  * Return:
0155  * 0 if direction of GPIO signals is set as input otherwise it
0156  * returns negative error value.
0157  */
0158 static int xgpio_get(struct gpio_chip *gc, unsigned int gpio)
0159 {
0160     struct xgpio_instance *chip = gpiochip_get_data(gc);
0161     int bit = xgpio_to_bit(chip, gpio);
0162     DECLARE_BITMAP(state, 64);
0163 
0164     xgpio_read_ch(chip, XGPIO_DATA_OFFSET, bit, state);
0165 
0166     return test_bit(bit, state);
0167 }
0168 
0169 /**
0170  * xgpio_set - Write the specified signal of the GPIO device.
0171  * @gc:     Pointer to gpio_chip device structure.
0172  * @gpio:   GPIO signal number.
0173  * @val:    Value to be written to specified signal.
0174  *
0175  * This function writes the specified value in to the specified signal of the
0176  * GPIO device.
0177  */
0178 static void xgpio_set(struct gpio_chip *gc, unsigned int gpio, int val)
0179 {
0180     unsigned long flags;
0181     struct xgpio_instance *chip = gpiochip_get_data(gc);
0182     int bit = xgpio_to_bit(chip, gpio);
0183 
0184     spin_lock_irqsave(&chip->gpio_lock, flags);
0185 
0186     /* Write to GPIO signal and set its direction to output */
0187     __assign_bit(bit, chip->state, val);
0188 
0189     xgpio_write_ch(chip, XGPIO_DATA_OFFSET, bit, chip->state);
0190 
0191     spin_unlock_irqrestore(&chip->gpio_lock, flags);
0192 }
0193 
0194 /**
0195  * xgpio_set_multiple - Write the specified signals of the GPIO device.
0196  * @gc:     Pointer to gpio_chip device structure.
0197  * @mask:   Mask of the GPIOS to modify.
0198  * @bits:   Value to be wrote on each GPIO
0199  *
0200  * This function writes the specified values into the specified signals of the
0201  * GPIO devices.
0202  */
0203 static void xgpio_set_multiple(struct gpio_chip *gc, unsigned long *mask,
0204                    unsigned long *bits)
0205 {
0206     DECLARE_BITMAP(hw_mask, 64);
0207     DECLARE_BITMAP(hw_bits, 64);
0208     DECLARE_BITMAP(state, 64);
0209     unsigned long flags;
0210     struct xgpio_instance *chip = gpiochip_get_data(gc);
0211 
0212     bitmap_remap(hw_mask, mask, chip->sw_map, chip->hw_map, 64);
0213     bitmap_remap(hw_bits, bits, chip->sw_map, chip->hw_map, 64);
0214 
0215     spin_lock_irqsave(&chip->gpio_lock, flags);
0216 
0217     bitmap_replace(state, chip->state, hw_bits, hw_mask, 64);
0218 
0219     xgpio_write_ch_all(chip, XGPIO_DATA_OFFSET, state);
0220 
0221     bitmap_copy(chip->state, state, 64);
0222 
0223     spin_unlock_irqrestore(&chip->gpio_lock, flags);
0224 }
0225 
0226 /**
0227  * xgpio_dir_in - Set the direction of the specified GPIO signal as input.
0228  * @gc:     Pointer to gpio_chip device structure.
0229  * @gpio:   GPIO signal number.
0230  *
0231  * Return:
0232  * 0 - if direction of GPIO signals is set as input
0233  * otherwise it returns negative error value.
0234  */
0235 static int xgpio_dir_in(struct gpio_chip *gc, unsigned int gpio)
0236 {
0237     unsigned long flags;
0238     struct xgpio_instance *chip = gpiochip_get_data(gc);
0239     int bit = xgpio_to_bit(chip, gpio);
0240 
0241     spin_lock_irqsave(&chip->gpio_lock, flags);
0242 
0243     /* Set the GPIO bit in shadow register and set direction as input */
0244     __set_bit(bit, chip->dir);
0245     xgpio_write_ch(chip, XGPIO_TRI_OFFSET, bit, chip->dir);
0246 
0247     spin_unlock_irqrestore(&chip->gpio_lock, flags);
0248 
0249     return 0;
0250 }
0251 
0252 /**
0253  * xgpio_dir_out - Set the direction of the specified GPIO signal as output.
0254  * @gc:     Pointer to gpio_chip device structure.
0255  * @gpio:   GPIO signal number.
0256  * @val:    Value to be written to specified signal.
0257  *
0258  * This function sets the direction of specified GPIO signal as output.
0259  *
0260  * Return:
0261  * If all GPIO signals of GPIO chip is configured as input then it returns
0262  * error otherwise it returns 0.
0263  */
0264 static int xgpio_dir_out(struct gpio_chip *gc, unsigned int gpio, int val)
0265 {
0266     unsigned long flags;
0267     struct xgpio_instance *chip = gpiochip_get_data(gc);
0268     int bit = xgpio_to_bit(chip, gpio);
0269 
0270     spin_lock_irqsave(&chip->gpio_lock, flags);
0271 
0272     /* Write state of GPIO signal */
0273     __assign_bit(bit, chip->state, val);
0274     xgpio_write_ch(chip, XGPIO_DATA_OFFSET, bit, chip->state);
0275 
0276     /* Clear the GPIO bit in shadow register and set direction as output */
0277     __clear_bit(bit, chip->dir);
0278     xgpio_write_ch(chip, XGPIO_TRI_OFFSET, bit, chip->dir);
0279 
0280     spin_unlock_irqrestore(&chip->gpio_lock, flags);
0281 
0282     return 0;
0283 }
0284 
0285 /**
0286  * xgpio_save_regs - Set initial values of GPIO pins
0287  * @chip: Pointer to GPIO instance
0288  */
0289 static void xgpio_save_regs(struct xgpio_instance *chip)
0290 {
0291     xgpio_write_ch_all(chip, XGPIO_DATA_OFFSET, chip->state);
0292     xgpio_write_ch_all(chip, XGPIO_TRI_OFFSET, chip->dir);
0293 }
0294 
0295 static int xgpio_request(struct gpio_chip *chip, unsigned int offset)
0296 {
0297     int ret;
0298 
0299     ret = pm_runtime_get_sync(chip->parent);
0300     /*
0301      * If the device is already active pm_runtime_get() will return 1 on
0302      * success, but gpio_request still needs to return 0.
0303      */
0304     return ret < 0 ? ret : 0;
0305 }
0306 
0307 static void xgpio_free(struct gpio_chip *chip, unsigned int offset)
0308 {
0309     pm_runtime_put(chip->parent);
0310 }
0311 
0312 static int __maybe_unused xgpio_suspend(struct device *dev)
0313 {
0314     struct xgpio_instance *gpio = dev_get_drvdata(dev);
0315     struct irq_data *data = irq_get_irq_data(gpio->irq);
0316 
0317     if (!data) {
0318         dev_dbg(dev, "IRQ not connected\n");
0319         return pm_runtime_force_suspend(dev);
0320     }
0321 
0322     if (!irqd_is_wakeup_set(data))
0323         return pm_runtime_force_suspend(dev);
0324 
0325     return 0;
0326 }
0327 
0328 /**
0329  * xgpio_remove - Remove method for the GPIO device.
0330  * @pdev: pointer to the platform device
0331  *
0332  * This function remove gpiochips and frees all the allocated resources.
0333  *
0334  * Return: 0 always
0335  */
0336 static int xgpio_remove(struct platform_device *pdev)
0337 {
0338     struct xgpio_instance *gpio = platform_get_drvdata(pdev);
0339 
0340     pm_runtime_get_sync(&pdev->dev);
0341     pm_runtime_put_noidle(&pdev->dev);
0342     pm_runtime_disable(&pdev->dev);
0343     clk_disable_unprepare(gpio->clk);
0344 
0345     return 0;
0346 }
0347 
0348 /**
0349  * xgpio_irq_ack - Acknowledge a child GPIO interrupt.
0350  * @irq_data: per IRQ and chip data passed down to chip functions
0351  * This currently does nothing, but irq_ack is unconditionally called by
0352  * handle_edge_irq and therefore must be defined.
0353  */
0354 static void xgpio_irq_ack(struct irq_data *irq_data)
0355 {
0356 }
0357 
0358 static int __maybe_unused xgpio_resume(struct device *dev)
0359 {
0360     struct xgpio_instance *gpio = dev_get_drvdata(dev);
0361     struct irq_data *data = irq_get_irq_data(gpio->irq);
0362 
0363     if (!data) {
0364         dev_dbg(dev, "IRQ not connected\n");
0365         return pm_runtime_force_resume(dev);
0366     }
0367 
0368     if (!irqd_is_wakeup_set(data))
0369         return pm_runtime_force_resume(dev);
0370 
0371     return 0;
0372 }
0373 
0374 static int __maybe_unused xgpio_runtime_suspend(struct device *dev)
0375 {
0376     struct xgpio_instance *gpio = dev_get_drvdata(dev);
0377 
0378     clk_disable(gpio->clk);
0379 
0380     return 0;
0381 }
0382 
0383 static int __maybe_unused xgpio_runtime_resume(struct device *dev)
0384 {
0385     struct xgpio_instance *gpio = dev_get_drvdata(dev);
0386 
0387     return clk_enable(gpio->clk);
0388 }
0389 
0390 static const struct dev_pm_ops xgpio_dev_pm_ops = {
0391     SET_SYSTEM_SLEEP_PM_OPS(xgpio_suspend, xgpio_resume)
0392     SET_RUNTIME_PM_OPS(xgpio_runtime_suspend,
0393                xgpio_runtime_resume, NULL)
0394 };
0395 
0396 /**
0397  * xgpio_irq_mask - Write the specified signal of the GPIO device.
0398  * @irq_data: per IRQ and chip data passed down to chip functions
0399  */
0400 static void xgpio_irq_mask(struct irq_data *irq_data)
0401 {
0402     unsigned long flags;
0403     struct xgpio_instance *chip = irq_data_get_irq_chip_data(irq_data);
0404     int irq_offset = irqd_to_hwirq(irq_data);
0405     int bit = xgpio_to_bit(chip, irq_offset);
0406     u32 mask = BIT(bit / 32), temp;
0407 
0408     spin_lock_irqsave(&chip->gpio_lock, flags);
0409 
0410     __clear_bit(bit, chip->enable);
0411 
0412     if (xgpio_get_value32(chip->enable, bit) == 0) {
0413         /* Disable per channel interrupt */
0414         temp = xgpio_readreg(chip->regs + XGPIO_IPIER_OFFSET);
0415         temp &= ~mask;
0416         xgpio_writereg(chip->regs + XGPIO_IPIER_OFFSET, temp);
0417     }
0418     spin_unlock_irqrestore(&chip->gpio_lock, flags);
0419 }
0420 
0421 /**
0422  * xgpio_irq_unmask - Write the specified signal of the GPIO device.
0423  * @irq_data: per IRQ and chip data passed down to chip functions
0424  */
0425 static void xgpio_irq_unmask(struct irq_data *irq_data)
0426 {
0427     unsigned long flags;
0428     struct xgpio_instance *chip = irq_data_get_irq_chip_data(irq_data);
0429     int irq_offset = irqd_to_hwirq(irq_data);
0430     int bit = xgpio_to_bit(chip, irq_offset);
0431     u32 old_enable = xgpio_get_value32(chip->enable, bit);
0432     u32 mask = BIT(bit / 32), val;
0433 
0434     spin_lock_irqsave(&chip->gpio_lock, flags);
0435 
0436     __set_bit(bit, chip->enable);
0437 
0438     if (old_enable == 0) {
0439         /* Clear any existing per-channel interrupts */
0440         val = xgpio_readreg(chip->regs + XGPIO_IPISR_OFFSET);
0441         val &= mask;
0442         xgpio_writereg(chip->regs + XGPIO_IPISR_OFFSET, val);
0443 
0444         /* Update GPIO IRQ read data before enabling interrupt*/
0445         xgpio_read_ch(chip, XGPIO_DATA_OFFSET, bit, chip->last_irq_read);
0446 
0447         /* Enable per channel interrupt */
0448         val = xgpio_readreg(chip->regs + XGPIO_IPIER_OFFSET);
0449         val |= mask;
0450         xgpio_writereg(chip->regs + XGPIO_IPIER_OFFSET, val);
0451     }
0452 
0453     spin_unlock_irqrestore(&chip->gpio_lock, flags);
0454 }
0455 
0456 /**
0457  * xgpio_set_irq_type - Write the specified signal of the GPIO device.
0458  * @irq_data: Per IRQ and chip data passed down to chip functions
0459  * @type: Interrupt type that is to be set for the gpio pin
0460  *
0461  * Return:
0462  * 0 if interrupt type is supported otherwise -EINVAL
0463  */
0464 static int xgpio_set_irq_type(struct irq_data *irq_data, unsigned int type)
0465 {
0466     struct xgpio_instance *chip = irq_data_get_irq_chip_data(irq_data);
0467     int irq_offset = irqd_to_hwirq(irq_data);
0468     int bit = xgpio_to_bit(chip, irq_offset);
0469 
0470     /*
0471      * The Xilinx GPIO hardware provides a single interrupt status
0472      * indication for any state change in a given GPIO channel (bank).
0473      * Therefore, only rising edge or falling edge triggers are
0474      * supported.
0475      */
0476     switch (type & IRQ_TYPE_SENSE_MASK) {
0477     case IRQ_TYPE_EDGE_BOTH:
0478         __set_bit(bit, chip->rising_edge);
0479         __set_bit(bit, chip->falling_edge);
0480         break;
0481     case IRQ_TYPE_EDGE_RISING:
0482         __set_bit(bit, chip->rising_edge);
0483         __clear_bit(bit, chip->falling_edge);
0484         break;
0485     case IRQ_TYPE_EDGE_FALLING:
0486         __clear_bit(bit, chip->rising_edge);
0487         __set_bit(bit, chip->falling_edge);
0488         break;
0489     default:
0490         return -EINVAL;
0491     }
0492 
0493     irq_set_handler_locked(irq_data, handle_edge_irq);
0494     return 0;
0495 }
0496 
0497 /**
0498  * xgpio_irqhandler - Gpio interrupt service routine
0499  * @desc: Pointer to interrupt description
0500  */
0501 static void xgpio_irqhandler(struct irq_desc *desc)
0502 {
0503     struct xgpio_instance *chip = irq_desc_get_handler_data(desc);
0504     struct gpio_chip *gc = &chip->gc;
0505     struct irq_chip *irqchip = irq_desc_get_chip(desc);
0506     DECLARE_BITMAP(rising, 64);
0507     DECLARE_BITMAP(falling, 64);
0508     DECLARE_BITMAP(all, 64);
0509     int irq_offset;
0510     u32 status;
0511     u32 bit;
0512 
0513     status = xgpio_readreg(chip->regs + XGPIO_IPISR_OFFSET);
0514     xgpio_writereg(chip->regs + XGPIO_IPISR_OFFSET, status);
0515 
0516     chained_irq_enter(irqchip, desc);
0517 
0518     spin_lock(&chip->gpio_lock);
0519 
0520     xgpio_read_ch_all(chip, XGPIO_DATA_OFFSET, all);
0521 
0522     bitmap_complement(rising, chip->last_irq_read, 64);
0523     bitmap_and(rising, rising, all, 64);
0524     bitmap_and(rising, rising, chip->enable, 64);
0525     bitmap_and(rising, rising, chip->rising_edge, 64);
0526 
0527     bitmap_complement(falling, all, 64);
0528     bitmap_and(falling, falling, chip->last_irq_read, 64);
0529     bitmap_and(falling, falling, chip->enable, 64);
0530     bitmap_and(falling, falling, chip->falling_edge, 64);
0531 
0532     bitmap_copy(chip->last_irq_read, all, 64);
0533     bitmap_or(all, rising, falling, 64);
0534 
0535     spin_unlock(&chip->gpio_lock);
0536 
0537     dev_dbg(gc->parent, "IRQ rising %*pb falling %*pb\n", 64, rising, 64, falling);
0538 
0539     for_each_set_bit(bit, all, 64) {
0540         irq_offset = xgpio_from_bit(chip, bit);
0541         generic_handle_domain_irq(gc->irq.domain, irq_offset);
0542     }
0543 
0544     chained_irq_exit(irqchip, desc);
0545 }
0546 
0547 /**
0548  * xgpio_probe - Probe method for the GPIO device.
0549  * @pdev: pointer to the platform device
0550  *
0551  * Return:
0552  * It returns 0, if the driver is bound to the GPIO device, or
0553  * a negative value if there is an error.
0554  */
0555 static int xgpio_probe(struct platform_device *pdev)
0556 {
0557     struct xgpio_instance *chip;
0558     int status = 0;
0559     struct device_node *np = pdev->dev.of_node;
0560     u32 is_dual = 0;
0561     u32 cells = 2;
0562     u32 width[2];
0563     u32 state[2];
0564     u32 dir[2];
0565     struct gpio_irq_chip *girq;
0566     u32 temp;
0567 
0568     chip = devm_kzalloc(&pdev->dev, sizeof(*chip), GFP_KERNEL);
0569     if (!chip)
0570         return -ENOMEM;
0571 
0572     platform_set_drvdata(pdev, chip);
0573 
0574     /* First, check if the device is dual-channel */
0575     of_property_read_u32(np, "xlnx,is-dual", &is_dual);
0576 
0577     /* Setup defaults */
0578     memset32(width, 0, ARRAY_SIZE(width));
0579     memset32(state, 0, ARRAY_SIZE(state));
0580     memset32(dir, 0xFFFFFFFF, ARRAY_SIZE(dir));
0581 
0582     /* Update GPIO state shadow register with default value */
0583     of_property_read_u32(np, "xlnx,dout-default", &state[0]);
0584     of_property_read_u32(np, "xlnx,dout-default-2", &state[1]);
0585 
0586     bitmap_from_arr32(chip->state, state, 64);
0587 
0588     /* Update GPIO direction shadow register with default value */
0589     of_property_read_u32(np, "xlnx,tri-default", &dir[0]);
0590     of_property_read_u32(np, "xlnx,tri-default-2", &dir[1]);
0591 
0592     bitmap_from_arr32(chip->dir, dir, 64);
0593 
0594     /* Update cells with gpio-cells value */
0595     if (of_property_read_u32(np, "#gpio-cells", &cells))
0596         dev_dbg(&pdev->dev, "Missing gpio-cells property\n");
0597 
0598     if (cells != 2) {
0599         dev_err(&pdev->dev, "#gpio-cells mismatch\n");
0600         return -EINVAL;
0601     }
0602 
0603     /*
0604      * Check device node and parent device node for device width
0605      * and assume default width of 32
0606      */
0607     if (of_property_read_u32(np, "xlnx,gpio-width", &width[0]))
0608         width[0] = 32;
0609 
0610     if (width[0] > 32)
0611         return -EINVAL;
0612 
0613     if (is_dual && of_property_read_u32(np, "xlnx,gpio2-width", &width[1]))
0614         width[1] = 32;
0615 
0616     if (width[1] > 32)
0617         return -EINVAL;
0618 
0619     /* Setup software pin mapping */
0620     bitmap_set(chip->sw_map, 0, width[0] + width[1]);
0621 
0622     /* Setup hardware pin mapping */
0623     bitmap_set(chip->hw_map,  0, width[0]);
0624     bitmap_set(chip->hw_map, 32, width[1]);
0625 
0626     spin_lock_init(&chip->gpio_lock);
0627 
0628     chip->gc.base = -1;
0629     chip->gc.ngpio = bitmap_weight(chip->hw_map, 64);
0630     chip->gc.parent = &pdev->dev;
0631     chip->gc.direction_input = xgpio_dir_in;
0632     chip->gc.direction_output = xgpio_dir_out;
0633     chip->gc.of_gpio_n_cells = cells;
0634     chip->gc.get = xgpio_get;
0635     chip->gc.set = xgpio_set;
0636     chip->gc.request = xgpio_request;
0637     chip->gc.free = xgpio_free;
0638     chip->gc.set_multiple = xgpio_set_multiple;
0639 
0640     chip->gc.label = dev_name(&pdev->dev);
0641 
0642     chip->regs = devm_platform_ioremap_resource(pdev, 0);
0643     if (IS_ERR(chip->regs)) {
0644         dev_err(&pdev->dev, "failed to ioremap memory resource\n");
0645         return PTR_ERR(chip->regs);
0646     }
0647 
0648     chip->clk = devm_clk_get_optional(&pdev->dev, NULL);
0649     if (IS_ERR(chip->clk))
0650         return dev_err_probe(&pdev->dev, PTR_ERR(chip->clk), "input clock not found.\n");
0651 
0652     status = clk_prepare_enable(chip->clk);
0653     if (status < 0) {
0654         dev_err(&pdev->dev, "Failed to prepare clk\n");
0655         return status;
0656     }
0657     pm_runtime_get_noresume(&pdev->dev);
0658     pm_runtime_set_active(&pdev->dev);
0659     pm_runtime_enable(&pdev->dev);
0660 
0661     xgpio_save_regs(chip);
0662 
0663     chip->irq = platform_get_irq_optional(pdev, 0);
0664     if (chip->irq <= 0)
0665         goto skip_irq;
0666 
0667     chip->irqchip.name = "gpio-xilinx";
0668     chip->irqchip.irq_ack = xgpio_irq_ack;
0669     chip->irqchip.irq_mask = xgpio_irq_mask;
0670     chip->irqchip.irq_unmask = xgpio_irq_unmask;
0671     chip->irqchip.irq_set_type = xgpio_set_irq_type;
0672 
0673     /* Disable per-channel interrupts */
0674     xgpio_writereg(chip->regs + XGPIO_IPIER_OFFSET, 0);
0675     /* Clear any existing per-channel interrupts */
0676     temp = xgpio_readreg(chip->regs + XGPIO_IPISR_OFFSET);
0677     xgpio_writereg(chip->regs + XGPIO_IPISR_OFFSET, temp);
0678     /* Enable global interrupts */
0679     xgpio_writereg(chip->regs + XGPIO_GIER_OFFSET, XGPIO_GIER_IE);
0680 
0681     girq = &chip->gc.irq;
0682     girq->chip = &chip->irqchip;
0683     girq->parent_handler = xgpio_irqhandler;
0684     girq->num_parents = 1;
0685     girq->parents = devm_kcalloc(&pdev->dev, 1,
0686                      sizeof(*girq->parents),
0687                      GFP_KERNEL);
0688     if (!girq->parents) {
0689         status = -ENOMEM;
0690         goto err_pm_put;
0691     }
0692     girq->parents[0] = chip->irq;
0693     girq->default_type = IRQ_TYPE_NONE;
0694     girq->handler = handle_bad_irq;
0695 
0696 skip_irq:
0697     status = devm_gpiochip_add_data(&pdev->dev, &chip->gc, chip);
0698     if (status) {
0699         dev_err(&pdev->dev, "failed to add GPIO chip\n");
0700         goto err_pm_put;
0701     }
0702 
0703     pm_runtime_put(&pdev->dev);
0704     return 0;
0705 
0706 err_pm_put:
0707     pm_runtime_disable(&pdev->dev);
0708     pm_runtime_put_noidle(&pdev->dev);
0709     clk_disable_unprepare(chip->clk);
0710     return status;
0711 }
0712 
0713 static const struct of_device_id xgpio_of_match[] = {
0714     { .compatible = "xlnx,xps-gpio-1.00.a", },
0715     { /* end of list */ },
0716 };
0717 
0718 MODULE_DEVICE_TABLE(of, xgpio_of_match);
0719 
0720 static struct platform_driver xgpio_plat_driver = {
0721     .probe      = xgpio_probe,
0722     .remove     = xgpio_remove,
0723     .driver     = {
0724             .name = "gpio-xilinx",
0725             .of_match_table = xgpio_of_match,
0726             .pm = &xgpio_dev_pm_ops,
0727     },
0728 };
0729 
0730 static int __init xgpio_init(void)
0731 {
0732     return platform_driver_register(&xgpio_plat_driver);
0733 }
0734 
0735 subsys_initcall(xgpio_init);
0736 
0737 static void __exit xgpio_exit(void)
0738 {
0739     platform_driver_unregister(&xgpio_plat_driver);
0740 }
0741 module_exit(xgpio_exit);
0742 
0743 MODULE_AUTHOR("Xilinx, Inc.");
0744 MODULE_DESCRIPTION("Xilinx GPIO driver");
0745 MODULE_LICENSE("GPL");