0001
0002
0003
0004
0005
0006
0007
0008
0009 #include <linux/bitops.h>
0010 #include <linux/clk.h>
0011 #include <linux/err.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/ioport.h>
0017 #include <linux/irq.h>
0018 #include <linux/platform_device.h>
0019 #include <linux/of.h>
0020 #include <linux/of_device.h>
0021 #include <linux/of_irq.h>
0022 #include <linux/pinctrl/consumer.h>
0023
0024 #define VF610_GPIO_PER_PORT 32
0025
0026 struct fsl_gpio_soc_data {
0027
0028 bool have_paddr;
0029 };
0030
0031 struct vf610_gpio_port {
0032 struct gpio_chip gc;
0033 struct irq_chip ic;
0034 void __iomem *base;
0035 void __iomem *gpio_base;
0036 const struct fsl_gpio_soc_data *sdata;
0037 u8 irqc[VF610_GPIO_PER_PORT];
0038 struct clk *clk_port;
0039 struct clk *clk_gpio;
0040 int irq;
0041 };
0042
0043 #define GPIO_PDOR 0x00
0044 #define GPIO_PSOR 0x04
0045 #define GPIO_PCOR 0x08
0046 #define GPIO_PTOR 0x0c
0047 #define GPIO_PDIR 0x10
0048 #define GPIO_PDDR 0x14
0049
0050 #define PORT_PCR(n) ((n) * 0x4)
0051 #define PORT_PCR_IRQC_OFFSET 16
0052
0053 #define PORT_ISFR 0xa0
0054 #define PORT_DFER 0xc0
0055 #define PORT_DFCR 0xc4
0056 #define PORT_DFWR 0xc8
0057
0058 #define PORT_INT_OFF 0x0
0059 #define PORT_INT_LOGIC_ZERO 0x8
0060 #define PORT_INT_RISING_EDGE 0x9
0061 #define PORT_INT_FALLING_EDGE 0xa
0062 #define PORT_INT_EITHER_EDGE 0xb
0063 #define PORT_INT_LOGIC_ONE 0xc
0064
0065 static const struct fsl_gpio_soc_data imx_data = {
0066 .have_paddr = true,
0067 };
0068
0069 static const struct of_device_id vf610_gpio_dt_ids[] = {
0070 { .compatible = "fsl,vf610-gpio", .data = NULL, },
0071 { .compatible = "fsl,imx7ulp-gpio", .data = &imx_data, },
0072 { }
0073 };
0074
0075 static inline void vf610_gpio_writel(u32 val, void __iomem *reg)
0076 {
0077 writel_relaxed(val, reg);
0078 }
0079
0080 static inline u32 vf610_gpio_readl(void __iomem *reg)
0081 {
0082 return readl_relaxed(reg);
0083 }
0084
0085 static int vf610_gpio_get(struct gpio_chip *gc, unsigned int gpio)
0086 {
0087 struct vf610_gpio_port *port = gpiochip_get_data(gc);
0088 unsigned long mask = BIT(gpio);
0089 unsigned long offset = GPIO_PDIR;
0090
0091 if (port->sdata && port->sdata->have_paddr) {
0092 mask &= vf610_gpio_readl(port->gpio_base + GPIO_PDDR);
0093 if (mask)
0094 offset = GPIO_PDOR;
0095 }
0096
0097 return !!(vf610_gpio_readl(port->gpio_base + offset) & BIT(gpio));
0098 }
0099
0100 static void vf610_gpio_set(struct gpio_chip *gc, unsigned int gpio, int val)
0101 {
0102 struct vf610_gpio_port *port = gpiochip_get_data(gc);
0103 unsigned long mask = BIT(gpio);
0104 unsigned long offset = val ? GPIO_PSOR : GPIO_PCOR;
0105
0106 vf610_gpio_writel(mask, port->gpio_base + offset);
0107 }
0108
0109 static int vf610_gpio_direction_input(struct gpio_chip *chip, unsigned gpio)
0110 {
0111 struct vf610_gpio_port *port = gpiochip_get_data(chip);
0112 unsigned long mask = BIT(gpio);
0113 u32 val;
0114
0115 if (port->sdata && port->sdata->have_paddr) {
0116 val = vf610_gpio_readl(port->gpio_base + GPIO_PDDR);
0117 val &= ~mask;
0118 vf610_gpio_writel(val, port->gpio_base + GPIO_PDDR);
0119 }
0120
0121 return pinctrl_gpio_direction_input(chip->base + gpio);
0122 }
0123
0124 static int vf610_gpio_direction_output(struct gpio_chip *chip, unsigned gpio,
0125 int value)
0126 {
0127 struct vf610_gpio_port *port = gpiochip_get_data(chip);
0128 unsigned long mask = BIT(gpio);
0129 u32 val;
0130
0131 if (port->sdata && port->sdata->have_paddr) {
0132 val = vf610_gpio_readl(port->gpio_base + GPIO_PDDR);
0133 val |= mask;
0134 vf610_gpio_writel(val, port->gpio_base + GPIO_PDDR);
0135 }
0136
0137 vf610_gpio_set(chip, gpio, value);
0138
0139 return pinctrl_gpio_direction_output(chip->base + gpio);
0140 }
0141
0142 static void vf610_gpio_irq_handler(struct irq_desc *desc)
0143 {
0144 struct vf610_gpio_port *port =
0145 gpiochip_get_data(irq_desc_get_handler_data(desc));
0146 struct irq_chip *chip = irq_desc_get_chip(desc);
0147 int pin;
0148 unsigned long irq_isfr;
0149
0150 chained_irq_enter(chip, desc);
0151
0152 irq_isfr = vf610_gpio_readl(port->base + PORT_ISFR);
0153
0154 for_each_set_bit(pin, &irq_isfr, VF610_GPIO_PER_PORT) {
0155 vf610_gpio_writel(BIT(pin), port->base + PORT_ISFR);
0156
0157 generic_handle_domain_irq(port->gc.irq.domain, pin);
0158 }
0159
0160 chained_irq_exit(chip, desc);
0161 }
0162
0163 static void vf610_gpio_irq_ack(struct irq_data *d)
0164 {
0165 struct vf610_gpio_port *port =
0166 gpiochip_get_data(irq_data_get_irq_chip_data(d));
0167 int gpio = d->hwirq;
0168
0169 vf610_gpio_writel(BIT(gpio), port->base + PORT_ISFR);
0170 }
0171
0172 static int vf610_gpio_irq_set_type(struct irq_data *d, u32 type)
0173 {
0174 struct vf610_gpio_port *port =
0175 gpiochip_get_data(irq_data_get_irq_chip_data(d));
0176 u8 irqc;
0177
0178 switch (type) {
0179 case IRQ_TYPE_EDGE_RISING:
0180 irqc = PORT_INT_RISING_EDGE;
0181 break;
0182 case IRQ_TYPE_EDGE_FALLING:
0183 irqc = PORT_INT_FALLING_EDGE;
0184 break;
0185 case IRQ_TYPE_EDGE_BOTH:
0186 irqc = PORT_INT_EITHER_EDGE;
0187 break;
0188 case IRQ_TYPE_LEVEL_LOW:
0189 irqc = PORT_INT_LOGIC_ZERO;
0190 break;
0191 case IRQ_TYPE_LEVEL_HIGH:
0192 irqc = PORT_INT_LOGIC_ONE;
0193 break;
0194 default:
0195 return -EINVAL;
0196 }
0197
0198 port->irqc[d->hwirq] = irqc;
0199
0200 if (type & IRQ_TYPE_LEVEL_MASK)
0201 irq_set_handler_locked(d, handle_level_irq);
0202 else
0203 irq_set_handler_locked(d, handle_edge_irq);
0204
0205 return 0;
0206 }
0207
0208 static void vf610_gpio_irq_mask(struct irq_data *d)
0209 {
0210 struct vf610_gpio_port *port =
0211 gpiochip_get_data(irq_data_get_irq_chip_data(d));
0212 void __iomem *pcr_base = port->base + PORT_PCR(d->hwirq);
0213
0214 vf610_gpio_writel(0, pcr_base);
0215 }
0216
0217 static void vf610_gpio_irq_unmask(struct irq_data *d)
0218 {
0219 struct vf610_gpio_port *port =
0220 gpiochip_get_data(irq_data_get_irq_chip_data(d));
0221 void __iomem *pcr_base = port->base + PORT_PCR(d->hwirq);
0222
0223 vf610_gpio_writel(port->irqc[d->hwirq] << PORT_PCR_IRQC_OFFSET,
0224 pcr_base);
0225 }
0226
0227 static int vf610_gpio_irq_set_wake(struct irq_data *d, u32 enable)
0228 {
0229 struct vf610_gpio_port *port =
0230 gpiochip_get_data(irq_data_get_irq_chip_data(d));
0231
0232 if (enable)
0233 enable_irq_wake(port->irq);
0234 else
0235 disable_irq_wake(port->irq);
0236
0237 return 0;
0238 }
0239
0240 static void vf610_gpio_disable_clk(void *data)
0241 {
0242 clk_disable_unprepare(data);
0243 }
0244
0245 static int vf610_gpio_probe(struct platform_device *pdev)
0246 {
0247 struct device *dev = &pdev->dev;
0248 struct device_node *np = dev->of_node;
0249 struct vf610_gpio_port *port;
0250 struct gpio_chip *gc;
0251 struct gpio_irq_chip *girq;
0252 struct irq_chip *ic;
0253 int i;
0254 int ret;
0255
0256 port = devm_kzalloc(dev, sizeof(*port), GFP_KERNEL);
0257 if (!port)
0258 return -ENOMEM;
0259
0260 port->sdata = of_device_get_match_data(dev);
0261 port->base = devm_platform_ioremap_resource(pdev, 0);
0262 if (IS_ERR(port->base))
0263 return PTR_ERR(port->base);
0264
0265 port->gpio_base = devm_platform_ioremap_resource(pdev, 1);
0266 if (IS_ERR(port->gpio_base))
0267 return PTR_ERR(port->gpio_base);
0268
0269 port->irq = platform_get_irq(pdev, 0);
0270 if (port->irq < 0)
0271 return port->irq;
0272
0273 port->clk_port = devm_clk_get(dev, "port");
0274 ret = PTR_ERR_OR_ZERO(port->clk_port);
0275 if (!ret) {
0276 ret = clk_prepare_enable(port->clk_port);
0277 if (ret)
0278 return ret;
0279 ret = devm_add_action_or_reset(dev, vf610_gpio_disable_clk,
0280 port->clk_port);
0281 if (ret)
0282 return ret;
0283 } else if (ret == -EPROBE_DEFER) {
0284
0285
0286
0287
0288 return ret;
0289 }
0290
0291 port->clk_gpio = devm_clk_get(dev, "gpio");
0292 ret = PTR_ERR_OR_ZERO(port->clk_gpio);
0293 if (!ret) {
0294 ret = clk_prepare_enable(port->clk_gpio);
0295 if (ret)
0296 return ret;
0297 ret = devm_add_action_or_reset(dev, vf610_gpio_disable_clk,
0298 port->clk_gpio);
0299 if (ret)
0300 return ret;
0301 } else if (ret == -EPROBE_DEFER) {
0302 return ret;
0303 }
0304
0305 gc = &port->gc;
0306 gc->parent = dev;
0307 gc->label = "vf610-gpio";
0308 gc->ngpio = VF610_GPIO_PER_PORT;
0309 gc->base = of_alias_get_id(np, "gpio") * VF610_GPIO_PER_PORT;
0310
0311 gc->request = gpiochip_generic_request;
0312 gc->free = gpiochip_generic_free;
0313 gc->direction_input = vf610_gpio_direction_input;
0314 gc->get = vf610_gpio_get;
0315 gc->direction_output = vf610_gpio_direction_output;
0316 gc->set = vf610_gpio_set;
0317
0318 ic = &port->ic;
0319 ic->name = "gpio-vf610";
0320 ic->irq_ack = vf610_gpio_irq_ack;
0321 ic->irq_mask = vf610_gpio_irq_mask;
0322 ic->irq_unmask = vf610_gpio_irq_unmask;
0323 ic->irq_set_type = vf610_gpio_irq_set_type;
0324 ic->irq_set_wake = vf610_gpio_irq_set_wake;
0325
0326
0327 for (i = 0; i < gc->ngpio; i++)
0328 vf610_gpio_writel(0, port->base + PORT_PCR(i));
0329
0330
0331 vf610_gpio_writel(~0, port->base + PORT_ISFR);
0332
0333 girq = &gc->irq;
0334 girq->chip = ic;
0335 girq->parent_handler = vf610_gpio_irq_handler;
0336 girq->num_parents = 1;
0337 girq->parents = devm_kcalloc(&pdev->dev, 1,
0338 sizeof(*girq->parents),
0339 GFP_KERNEL);
0340 if (!girq->parents)
0341 return -ENOMEM;
0342 girq->parents[0] = port->irq;
0343 girq->default_type = IRQ_TYPE_NONE;
0344 girq->handler = handle_edge_irq;
0345
0346 return devm_gpiochip_add_data(dev, gc, port);
0347 }
0348
0349 static struct platform_driver vf610_gpio_driver = {
0350 .driver = {
0351 .name = "gpio-vf610",
0352 .of_match_table = vf610_gpio_dt_ids,
0353 },
0354 .probe = vf610_gpio_probe,
0355 };
0356
0357 builtin_platform_driver(vf610_gpio_driver);