Gabor Juhos | 6eae43c | 2011-01-04 21:28:15 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Atheros AR71XX/AR724X/AR913X GPIO API support |
| 3 | * |
Gabor Juhos | 5b5b544 | 2012-03-14 10:45:23 +0100 | [diff] [blame] | 4 | * Copyright (C) 2010-2011 Jaiganesh Narayanan <jnarayanan@atheros.com> |
| 5 | * Copyright (C) 2008-2011 Gabor Juhos <juhosg@openwrt.org> |
Gabor Juhos | 6eae43c | 2011-01-04 21:28:15 +0100 | [diff] [blame] | 6 | * Copyright (C) 2008 Imre Kaloz <kaloz@openwrt.org> |
| 7 | * |
Gabor Juhos | 5b5b544 | 2012-03-14 10:45:23 +0100 | [diff] [blame] | 8 | * Parts of this file are based on Atheros' 2.6.15/2.6.31 BSP |
| 9 | * |
Gabor Juhos | 6eae43c | 2011-01-04 21:28:15 +0100 | [diff] [blame] | 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. |
| 13 | */ |
| 14 | |
Alban Bedel | 49a5bd8 | 2015-09-01 11:38:02 +0200 | [diff] [blame] | 15 | #include <linux/gpio/driver.h> |
Alban Bedel | 2ddf3a7 | 2015-05-31 02:18:24 +0200 | [diff] [blame] | 16 | #include <linux/platform_data/gpio-ath79.h> |
| 17 | #include <linux/of_device.h> |
Gabor Juhos | 6eae43c | 2011-01-04 21:28:15 +0100 | [diff] [blame] | 18 | |
| 19 | #include <asm/mach-ath79/ar71xx_regs.h> |
Gabor Juhos | 6eae43c | 2011-01-04 21:28:15 +0100 | [diff] [blame] | 20 | |
Alban Bedel | 49a5bd8 | 2015-09-01 11:38:02 +0200 | [diff] [blame] | 21 | struct ath79_gpio_ctrl { |
| 22 | struct gpio_chip chip; |
| 23 | void __iomem *base; |
| 24 | spinlock_t lock; |
| 25 | }; |
Gabor Juhos | 6eae43c | 2011-01-04 21:28:15 +0100 | [diff] [blame] | 26 | |
Gabor Juhos | 6eae43c | 2011-01-04 21:28:15 +0100 | [diff] [blame] | 27 | static void ath79_gpio_set_value(struct gpio_chip *chip, |
Alban Bedel | 49a5bd8 | 2015-09-01 11:38:02 +0200 | [diff] [blame] | 28 | unsigned gpio, int value) |
Gabor Juhos | 6eae43c | 2011-01-04 21:28:15 +0100 | [diff] [blame] | 29 | { |
Linus Walleij | b4e97a6 | 2015-12-04 15:36:14 +0100 | [diff] [blame] | 30 | struct ath79_gpio_ctrl *ctrl = gpiochip_get_data(chip); |
Alban Bedel | 49a5bd8 | 2015-09-01 11:38:02 +0200 | [diff] [blame] | 31 | |
| 32 | if (value) |
| 33 | __raw_writel(BIT(gpio), ctrl->base + AR71XX_GPIO_REG_SET); |
| 34 | else |
| 35 | __raw_writel(BIT(gpio), ctrl->base + AR71XX_GPIO_REG_CLEAR); |
| 36 | } |
| 37 | |
| 38 | static int ath79_gpio_get_value(struct gpio_chip *chip, unsigned gpio) |
| 39 | { |
Linus Walleij | b4e97a6 | 2015-12-04 15:36:14 +0100 | [diff] [blame] | 40 | struct ath79_gpio_ctrl *ctrl = gpiochip_get_data(chip); |
Alban Bedel | 49a5bd8 | 2015-09-01 11:38:02 +0200 | [diff] [blame] | 41 | |
| 42 | return (__raw_readl(ctrl->base + AR71XX_GPIO_REG_IN) >> gpio) & 1; |
Gabor Juhos | 6eae43c | 2011-01-04 21:28:15 +0100 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | static int ath79_gpio_direction_input(struct gpio_chip *chip, |
| 46 | unsigned offset) |
| 47 | { |
Linus Walleij | b4e97a6 | 2015-12-04 15:36:14 +0100 | [diff] [blame] | 48 | struct ath79_gpio_ctrl *ctrl = gpiochip_get_data(chip); |
Gabor Juhos | 6eae43c | 2011-01-04 21:28:15 +0100 | [diff] [blame] | 49 | unsigned long flags; |
| 50 | |
Alban Bedel | 49a5bd8 | 2015-09-01 11:38:02 +0200 | [diff] [blame] | 51 | spin_lock_irqsave(&ctrl->lock, flags); |
Gabor Juhos | 6eae43c | 2011-01-04 21:28:15 +0100 | [diff] [blame] | 52 | |
Alban Bedel | 49a5bd8 | 2015-09-01 11:38:02 +0200 | [diff] [blame] | 53 | __raw_writel( |
| 54 | __raw_readl(ctrl->base + AR71XX_GPIO_REG_OE) & ~BIT(offset), |
| 55 | ctrl->base + AR71XX_GPIO_REG_OE); |
Gabor Juhos | 6eae43c | 2011-01-04 21:28:15 +0100 | [diff] [blame] | 56 | |
Alban Bedel | 49a5bd8 | 2015-09-01 11:38:02 +0200 | [diff] [blame] | 57 | spin_unlock_irqrestore(&ctrl->lock, flags); |
Gabor Juhos | 6eae43c | 2011-01-04 21:28:15 +0100 | [diff] [blame] | 58 | |
| 59 | return 0; |
| 60 | } |
| 61 | |
| 62 | static int ath79_gpio_direction_output(struct gpio_chip *chip, |
| 63 | unsigned offset, int value) |
| 64 | { |
Linus Walleij | b4e97a6 | 2015-12-04 15:36:14 +0100 | [diff] [blame] | 65 | struct ath79_gpio_ctrl *ctrl = gpiochip_get_data(chip); |
Gabor Juhos | 6eae43c | 2011-01-04 21:28:15 +0100 | [diff] [blame] | 66 | unsigned long flags; |
| 67 | |
Alban Bedel | 49a5bd8 | 2015-09-01 11:38:02 +0200 | [diff] [blame] | 68 | spin_lock_irqsave(&ctrl->lock, flags); |
Gabor Juhos | 6eae43c | 2011-01-04 21:28:15 +0100 | [diff] [blame] | 69 | |
| 70 | if (value) |
Alban Bedel | 49a5bd8 | 2015-09-01 11:38:02 +0200 | [diff] [blame] | 71 | __raw_writel(BIT(offset), ctrl->base + AR71XX_GPIO_REG_SET); |
Gabor Juhos | 6eae43c | 2011-01-04 21:28:15 +0100 | [diff] [blame] | 72 | else |
Alban Bedel | 49a5bd8 | 2015-09-01 11:38:02 +0200 | [diff] [blame] | 73 | __raw_writel(BIT(offset), ctrl->base + AR71XX_GPIO_REG_CLEAR); |
Gabor Juhos | 6eae43c | 2011-01-04 21:28:15 +0100 | [diff] [blame] | 74 | |
Alban Bedel | 49a5bd8 | 2015-09-01 11:38:02 +0200 | [diff] [blame] | 75 | __raw_writel( |
| 76 | __raw_readl(ctrl->base + AR71XX_GPIO_REG_OE) | BIT(offset), |
| 77 | ctrl->base + AR71XX_GPIO_REG_OE); |
Gabor Juhos | 6eae43c | 2011-01-04 21:28:15 +0100 | [diff] [blame] | 78 | |
Alban Bedel | 49a5bd8 | 2015-09-01 11:38:02 +0200 | [diff] [blame] | 79 | spin_unlock_irqrestore(&ctrl->lock, flags); |
Gabor Juhos | 6eae43c | 2011-01-04 21:28:15 +0100 | [diff] [blame] | 80 | |
| 81 | return 0; |
| 82 | } |
| 83 | |
Gabor Juhos | 5b5b544 | 2012-03-14 10:45:23 +0100 | [diff] [blame] | 84 | static int ar934x_gpio_direction_input(struct gpio_chip *chip, unsigned offset) |
| 85 | { |
Linus Walleij | b4e97a6 | 2015-12-04 15:36:14 +0100 | [diff] [blame] | 86 | struct ath79_gpio_ctrl *ctrl = gpiochip_get_data(chip); |
Gabor Juhos | 5b5b544 | 2012-03-14 10:45:23 +0100 | [diff] [blame] | 87 | unsigned long flags; |
| 88 | |
Alban Bedel | 49a5bd8 | 2015-09-01 11:38:02 +0200 | [diff] [blame] | 89 | spin_lock_irqsave(&ctrl->lock, flags); |
Gabor Juhos | 5b5b544 | 2012-03-14 10:45:23 +0100 | [diff] [blame] | 90 | |
Alban Bedel | 49a5bd8 | 2015-09-01 11:38:02 +0200 | [diff] [blame] | 91 | __raw_writel( |
| 92 | __raw_readl(ctrl->base + AR71XX_GPIO_REG_OE) | BIT(offset), |
| 93 | ctrl->base + AR71XX_GPIO_REG_OE); |
Gabor Juhos | 5b5b544 | 2012-03-14 10:45:23 +0100 | [diff] [blame] | 94 | |
Alban Bedel | 49a5bd8 | 2015-09-01 11:38:02 +0200 | [diff] [blame] | 95 | spin_unlock_irqrestore(&ctrl->lock, flags); |
Gabor Juhos | 5b5b544 | 2012-03-14 10:45:23 +0100 | [diff] [blame] | 96 | |
| 97 | return 0; |
| 98 | } |
| 99 | |
| 100 | static int ar934x_gpio_direction_output(struct gpio_chip *chip, unsigned offset, |
| 101 | int value) |
| 102 | { |
Linus Walleij | b4e97a6 | 2015-12-04 15:36:14 +0100 | [diff] [blame] | 103 | struct ath79_gpio_ctrl *ctrl = gpiochip_get_data(chip); |
Gabor Juhos | 5b5b544 | 2012-03-14 10:45:23 +0100 | [diff] [blame] | 104 | unsigned long flags; |
| 105 | |
Alban Bedel | 49a5bd8 | 2015-09-01 11:38:02 +0200 | [diff] [blame] | 106 | spin_lock_irqsave(&ctrl->lock, flags); |
Gabor Juhos | 5b5b544 | 2012-03-14 10:45:23 +0100 | [diff] [blame] | 107 | |
| 108 | if (value) |
Alban Bedel | 49a5bd8 | 2015-09-01 11:38:02 +0200 | [diff] [blame] | 109 | __raw_writel(BIT(offset), ctrl->base + AR71XX_GPIO_REG_SET); |
Gabor Juhos | 5b5b544 | 2012-03-14 10:45:23 +0100 | [diff] [blame] | 110 | else |
Alban Bedel | 49a5bd8 | 2015-09-01 11:38:02 +0200 | [diff] [blame] | 111 | __raw_writel(BIT(offset), ctrl->base + AR71XX_GPIO_REG_CLEAR); |
Gabor Juhos | 5b5b544 | 2012-03-14 10:45:23 +0100 | [diff] [blame] | 112 | |
Alban Bedel | 49a5bd8 | 2015-09-01 11:38:02 +0200 | [diff] [blame] | 113 | __raw_writel( |
Axel Lin | 3a57e74 | 2015-12-08 23:01:07 +0800 | [diff] [blame] | 114 | __raw_readl(ctrl->base + AR71XX_GPIO_REG_OE) & ~BIT(offset), |
Alban Bedel | 49a5bd8 | 2015-09-01 11:38:02 +0200 | [diff] [blame] | 115 | ctrl->base + AR71XX_GPIO_REG_OE); |
Gabor Juhos | 5b5b544 | 2012-03-14 10:45:23 +0100 | [diff] [blame] | 116 | |
Alban Bedel | 49a5bd8 | 2015-09-01 11:38:02 +0200 | [diff] [blame] | 117 | spin_unlock_irqrestore(&ctrl->lock, flags); |
Gabor Juhos | 5b5b544 | 2012-03-14 10:45:23 +0100 | [diff] [blame] | 118 | |
| 119 | return 0; |
| 120 | } |
| 121 | |
Alban Bedel | 49a5bd8 | 2015-09-01 11:38:02 +0200 | [diff] [blame] | 122 | static const struct gpio_chip ath79_gpio_chip = { |
Gabor Juhos | 6eae43c | 2011-01-04 21:28:15 +0100 | [diff] [blame] | 123 | .label = "ath79", |
| 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, |
| 128 | .base = 0, |
| 129 | }; |
| 130 | |
Alban Bedel | 2ddf3a7 | 2015-05-31 02:18:24 +0200 | [diff] [blame] | 131 | static const struct of_device_id ath79_gpio_of_match[] = { |
| 132 | { .compatible = "qca,ar7100-gpio" }, |
| 133 | { .compatible = "qca,ar9340-gpio" }, |
| 134 | {}, |
| 135 | }; |
| 136 | |
| 137 | static int ath79_gpio_probe(struct platform_device *pdev) |
Gabor Juhos | 6eae43c | 2011-01-04 21:28:15 +0100 | [diff] [blame] | 138 | { |
Nizam Haider | ab128af | 2015-11-23 20:53:18 +0530 | [diff] [blame] | 139 | struct ath79_gpio_platform_data *pdata = dev_get_platdata(&pdev->dev); |
Alban Bedel | 2ddf3a7 | 2015-05-31 02:18:24 +0200 | [diff] [blame] | 140 | struct device_node *np = pdev->dev.of_node; |
Alban Bedel | 49a5bd8 | 2015-09-01 11:38:02 +0200 | [diff] [blame] | 141 | struct ath79_gpio_ctrl *ctrl; |
Alban Bedel | 2ddf3a7 | 2015-05-31 02:18:24 +0200 | [diff] [blame] | 142 | struct resource *res; |
Alban Bedel | 49a5bd8 | 2015-09-01 11:38:02 +0200 | [diff] [blame] | 143 | u32 ath79_gpio_count; |
Alban Bedel | 2ddf3a7 | 2015-05-31 02:18:24 +0200 | [diff] [blame] | 144 | bool oe_inverted; |
Gabor Juhos | 6eae43c | 2011-01-04 21:28:15 +0100 | [diff] [blame] | 145 | int err; |
| 146 | |
Alban Bedel | 49a5bd8 | 2015-09-01 11:38:02 +0200 | [diff] [blame] | 147 | ctrl = devm_kzalloc(&pdev->dev, sizeof(*ctrl), GFP_KERNEL); |
| 148 | if (!ctrl) |
| 149 | return -ENOMEM; |
| 150 | |
Alban Bedel | 2ddf3a7 | 2015-05-31 02:18:24 +0200 | [diff] [blame] | 151 | if (np) { |
| 152 | err = of_property_read_u32(np, "ngpios", &ath79_gpio_count); |
| 153 | if (err) { |
| 154 | dev_err(&pdev->dev, "ngpios property is not valid\n"); |
| 155 | return err; |
| 156 | } |
| 157 | if (ath79_gpio_count >= 32) { |
| 158 | dev_err(&pdev->dev, "ngpios must be less than 32\n"); |
| 159 | return -EINVAL; |
| 160 | } |
| 161 | oe_inverted = of_device_is_compatible(np, "qca,ar9340-gpio"); |
| 162 | } else if (pdata) { |
| 163 | ath79_gpio_count = pdata->ngpios; |
| 164 | oe_inverted = pdata->oe_inverted; |
| 165 | } else { |
| 166 | dev_err(&pdev->dev, "No DT node or platform data found\n"); |
| 167 | return -EINVAL; |
| 168 | } |
Gabor Juhos | 6eae43c | 2011-01-04 21:28:15 +0100 | [diff] [blame] | 169 | |
Alban Bedel | 2ddf3a7 | 2015-05-31 02:18:24 +0200 | [diff] [blame] | 170 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
Alban Bedel | 49a5bd8 | 2015-09-01 11:38:02 +0200 | [diff] [blame] | 171 | ctrl->base = devm_ioremap_nocache( |
Alban Bedel | 2ddf3a7 | 2015-05-31 02:18:24 +0200 | [diff] [blame] | 172 | &pdev->dev, res->start, resource_size(res)); |
Alban Bedel | 49a5bd8 | 2015-09-01 11:38:02 +0200 | [diff] [blame] | 173 | if (!ctrl->base) |
Alban Bedel | 2ddf3a7 | 2015-05-31 02:18:24 +0200 | [diff] [blame] | 174 | return -ENOMEM; |
| 175 | |
Alban Bedel | 49a5bd8 | 2015-09-01 11:38:02 +0200 | [diff] [blame] | 176 | spin_lock_init(&ctrl->lock); |
| 177 | memcpy(&ctrl->chip, &ath79_gpio_chip, sizeof(ctrl->chip)); |
Linus Walleij | 58383c7 | 2015-11-04 09:56:26 +0100 | [diff] [blame] | 178 | ctrl->chip.parent = &pdev->dev; |
Alban Bedel | 49a5bd8 | 2015-09-01 11:38:02 +0200 | [diff] [blame] | 179 | ctrl->chip.ngpio = ath79_gpio_count; |
Alban Bedel | 2ddf3a7 | 2015-05-31 02:18:24 +0200 | [diff] [blame] | 180 | if (oe_inverted) { |
Alban Bedel | 49a5bd8 | 2015-09-01 11:38:02 +0200 | [diff] [blame] | 181 | ctrl->chip.direction_input = ar934x_gpio_direction_input; |
| 182 | ctrl->chip.direction_output = ar934x_gpio_direction_output; |
Gabor Juhos | 5b5b544 | 2012-03-14 10:45:23 +0100 | [diff] [blame] | 183 | } |
Gabor Juhos | 6eae43c | 2011-01-04 21:28:15 +0100 | [diff] [blame] | 184 | |
Linus Walleij | b4e97a6 | 2015-12-04 15:36:14 +0100 | [diff] [blame] | 185 | err = gpiochip_add_data(&ctrl->chip, ctrl); |
Alban Bedel | 2ddf3a7 | 2015-05-31 02:18:24 +0200 | [diff] [blame] | 186 | if (err) { |
| 187 | dev_err(&pdev->dev, |
| 188 | "cannot add AR71xx GPIO chip, error=%d", err); |
| 189 | return err; |
| 190 | } |
| 191 | |
| 192 | return 0; |
Gabor Juhos | 6eae43c | 2011-01-04 21:28:15 +0100 | [diff] [blame] | 193 | } |
| 194 | |
Alban Bedel | 2ddf3a7 | 2015-05-31 02:18:24 +0200 | [diff] [blame] | 195 | static struct platform_driver ath79_gpio_driver = { |
| 196 | .driver = { |
| 197 | .name = "ath79-gpio", |
| 198 | .of_match_table = ath79_gpio_of_match, |
| 199 | }, |
| 200 | .probe = ath79_gpio_probe, |
| 201 | }; |
| 202 | |
| 203 | module_platform_driver(ath79_gpio_driver); |