2 * Atheros AR71XX/AR724X/AR913X GPIO API support
8 * Parts of this file are based on Atheros' 2.6.15/2.6.31 BSP
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License version 2 as published
12 * by the Free Software Foundation.
15 #include <linux/gpio/driver.h>
16 #include <linux/platform_data/gpio-ath79.h>
17 #include <linux/of_device.h>
19 #include <asm/mach-ath79/ar71xx_regs.h>
21 struct ath79_gpio_ctrl {
22 struct gpio_chip chip;
27 static void ath79_gpio_set_value(struct gpio_chip *chip,
28 unsigned gpio, int value)
30 struct ath79_gpio_ctrl *ctrl = gpiochip_get_data(chip);
33 __raw_writel(BIT(gpio), ctrl->base + AR71XX_GPIO_REG_SET);
35 __raw_writel(BIT(gpio), ctrl->base + AR71XX_GPIO_REG_CLEAR);
38 static int ath79_gpio_get_value(struct gpio_chip *chip, unsigned gpio)
40 struct ath79_gpio_ctrl *ctrl = gpiochip_get_data(chip);
42 return (__raw_readl(ctrl->base + AR71XX_GPIO_REG_IN) >> gpio) & 1;
45 static int ath79_gpio_direction_input(struct gpio_chip *chip,
48 struct ath79_gpio_ctrl *ctrl = gpiochip_get_data(chip);
51 spin_lock_irqsave(&ctrl->lock, flags);
54 __raw_readl(ctrl->base + AR71XX_GPIO_REG_OE) & ~BIT(offset),
55 ctrl->base + AR71XX_GPIO_REG_OE);
57 spin_unlock_irqrestore(&ctrl->lock, flags);
62 static int ath79_gpio_direction_output(struct gpio_chip *chip,
63 unsigned offset, int value)
65 struct ath79_gpio_ctrl *ctrl = gpiochip_get_data(chip);
68 spin_lock_irqsave(&ctrl->lock, flags);
71 __raw_writel(BIT(offset), ctrl->base + AR71XX_GPIO_REG_SET);
73 __raw_writel(BIT(offset), ctrl->base + AR71XX_GPIO_REG_CLEAR);
76 __raw_readl(ctrl->base + AR71XX_GPIO_REG_OE) | BIT(offset),
77 ctrl->base + AR71XX_GPIO_REG_OE);
79 spin_unlock_irqrestore(&ctrl->lock, flags);
84 static int ar934x_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
86 struct ath79_gpio_ctrl *ctrl = gpiochip_get_data(chip);
89 spin_lock_irqsave(&ctrl->lock, flags);
92 __raw_readl(ctrl->base + AR71XX_GPIO_REG_OE) | BIT(offset),
93 ctrl->base + AR71XX_GPIO_REG_OE);
95 spin_unlock_irqrestore(&ctrl->lock, flags);
100 static int ar934x_gpio_direction_output(struct gpio_chip *chip, unsigned offset,
103 struct ath79_gpio_ctrl *ctrl = gpiochip_get_data(chip);
106 spin_lock_irqsave(&ctrl->lock, flags);
109 __raw_writel(BIT(offset), ctrl->base + AR71XX_GPIO_REG_SET);
111 __raw_writel(BIT(offset), ctrl->base + AR71XX_GPIO_REG_CLEAR);
114 __raw_readl(ctrl->base + AR71XX_GPIO_REG_OE) & ~BIT(offset),
115 ctrl->base + AR71XX_GPIO_REG_OE);
117 spin_unlock_irqrestore(&ctrl->lock, flags);
122 static const struct gpio_chip ath79_gpio_chip = {
124 .get = ath79_gpio_get_value,
125 .set = ath79_gpio_set_value,
126 .direction_input = ath79_gpio_direction_input,
127 .direction_output = ath79_gpio_direction_output,
131 static const struct of_device_id ath79_gpio_of_match[] = {
132 { .compatible = "qca,ar7100-gpio" },
133 { .compatible = "qca,ar9340-gpio" },
137 static int ath79_gpio_probe(struct platform_device *pdev)
139 struct ath79_gpio_platform_data *pdata = dev_get_platdata(&pdev->dev);
140 struct device_node *np = pdev->dev.of_node;
141 struct ath79_gpio_ctrl *ctrl;
142 struct resource *res;
143 u32 ath79_gpio_count;
147 ctrl = devm_kzalloc(&pdev->dev, sizeof(*ctrl), GFP_KERNEL);
152 err = of_property_read_u32(np, "ngpios", &ath79_gpio_count);
154 dev_err(&pdev->dev, "ngpios property is not valid\n");
157 if (ath79_gpio_count >= 32) {
158 dev_err(&pdev->dev, "ngpios must be less than 32\n");
161 oe_inverted = of_device_is_compatible(np, "qca,ar9340-gpio");
163 ath79_gpio_count = pdata->ngpios;
164 oe_inverted = pdata->oe_inverted;
166 dev_err(&pdev->dev, "No DT node or platform data found\n");
170 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
171 ctrl->base = devm_ioremap_nocache(
172 &pdev->dev, res->start, resource_size(res));
176 spin_lock_init(&ctrl->lock);
177 memcpy(&ctrl->chip, &ath79_gpio_chip, sizeof(ctrl->chip));
178 ctrl->chip.parent = &pdev->dev;
179 ctrl->chip.ngpio = ath79_gpio_count;
181 ctrl->chip.direction_input = ar934x_gpio_direction_input;
182 ctrl->chip.direction_output = ar934x_gpio_direction_output;
185 err = gpiochip_add_data(&ctrl->chip, ctrl);
188 "cannot add AR71xx GPIO chip, error=%d", err);
195 static struct platform_driver ath79_gpio_driver = {
197 .name = "ath79-gpio",
198 .of_match_table = ath79_gpio_of_match,
200 .probe = ath79_gpio_probe,
203 module_platform_driver(ath79_gpio_driver);