Linux 4.2.1
[linux/fpc-iii.git] / drivers / gpio / gpio-xgene.c
blob18a8182d4fec291f569233935ad0472517aefdb8
1 /*
2 * AppliedMicro X-Gene SoC GPIO Driver
4 * Copyright (c) 2014, Applied Micro Circuits Corporation
5 * Author: Feng Kan <fkan@apm.com>.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include <linux/module.h>
21 #include <linux/kernel.h>
22 #include <linux/init.h>
23 #include <linux/io.h>
24 #include <linux/spinlock.h>
25 #include <linux/platform_device.h>
26 #include <linux/gpio/driver.h>
27 #include <linux/types.h>
28 #include <linux/bitops.h>
30 #define GPIO_SET_DR_OFFSET 0x0C
31 #define GPIO_DATA_OFFSET 0x14
32 #define GPIO_BANK_STRIDE 0x0C
34 #define XGENE_GPIOS_PER_BANK 16
35 #define XGENE_MAX_GPIO_BANKS 3
36 #define XGENE_MAX_GPIOS (XGENE_GPIOS_PER_BANK * XGENE_MAX_GPIO_BANKS)
38 #define GPIO_BIT_OFFSET(x) (x % XGENE_GPIOS_PER_BANK)
39 #define GPIO_BANK_OFFSET(x) ((x / XGENE_GPIOS_PER_BANK) * GPIO_BANK_STRIDE)
41 struct xgene_gpio {
42 struct gpio_chip chip;
43 void __iomem *base;
44 spinlock_t lock;
45 #ifdef CONFIG_PM
46 u32 set_dr_val[XGENE_MAX_GPIO_BANKS];
47 #endif
50 static inline struct xgene_gpio *to_xgene_gpio(struct gpio_chip *chip)
52 return container_of(chip, struct xgene_gpio, chip);
55 static int xgene_gpio_get(struct gpio_chip *gc, unsigned int offset)
57 struct xgene_gpio *chip = to_xgene_gpio(gc);
58 unsigned long bank_offset;
59 u32 bit_offset;
61 bank_offset = GPIO_DATA_OFFSET + GPIO_BANK_OFFSET(offset);
62 bit_offset = GPIO_BIT_OFFSET(offset);
63 return !!(ioread32(chip->base + bank_offset) & BIT(bit_offset));
66 static void __xgene_gpio_set(struct gpio_chip *gc, unsigned int offset, int val)
68 struct xgene_gpio *chip = to_xgene_gpio(gc);
69 unsigned long bank_offset;
70 u32 setval, bit_offset;
72 bank_offset = GPIO_SET_DR_OFFSET + GPIO_BANK_OFFSET(offset);
73 bit_offset = GPIO_BIT_OFFSET(offset) + XGENE_GPIOS_PER_BANK;
75 setval = ioread32(chip->base + bank_offset);
76 if (val)
77 setval |= BIT(bit_offset);
78 else
79 setval &= ~BIT(bit_offset);
80 iowrite32(setval, chip->base + bank_offset);
83 static void xgene_gpio_set(struct gpio_chip *gc, unsigned int offset, int val)
85 struct xgene_gpio *chip = to_xgene_gpio(gc);
86 unsigned long flags;
88 spin_lock_irqsave(&chip->lock, flags);
89 __xgene_gpio_set(gc, offset, val);
90 spin_unlock_irqrestore(&chip->lock, flags);
93 static int xgene_gpio_dir_in(struct gpio_chip *gc, unsigned int offset)
95 struct xgene_gpio *chip = to_xgene_gpio(gc);
96 unsigned long flags, bank_offset;
97 u32 dirval, bit_offset;
99 bank_offset = GPIO_SET_DR_OFFSET + GPIO_BANK_OFFSET(offset);
100 bit_offset = GPIO_BIT_OFFSET(offset);
102 spin_lock_irqsave(&chip->lock, flags);
104 dirval = ioread32(chip->base + bank_offset);
105 dirval |= BIT(bit_offset);
106 iowrite32(dirval, chip->base + bank_offset);
108 spin_unlock_irqrestore(&chip->lock, flags);
110 return 0;
113 static int xgene_gpio_dir_out(struct gpio_chip *gc,
114 unsigned int offset, int val)
116 struct xgene_gpio *chip = to_xgene_gpio(gc);
117 unsigned long flags, bank_offset;
118 u32 dirval, bit_offset;
120 bank_offset = GPIO_SET_DR_OFFSET + GPIO_BANK_OFFSET(offset);
121 bit_offset = GPIO_BIT_OFFSET(offset);
123 spin_lock_irqsave(&chip->lock, flags);
125 dirval = ioread32(chip->base + bank_offset);
126 dirval &= ~BIT(bit_offset);
127 iowrite32(dirval, chip->base + bank_offset);
128 __xgene_gpio_set(gc, offset, val);
130 spin_unlock_irqrestore(&chip->lock, flags);
132 return 0;
135 #ifdef CONFIG_PM
136 static int xgene_gpio_suspend(struct device *dev)
138 struct xgene_gpio *gpio = dev_get_drvdata(dev);
139 unsigned long bank_offset;
140 unsigned int bank;
142 for (bank = 0; bank < XGENE_MAX_GPIO_BANKS; bank++) {
143 bank_offset = GPIO_SET_DR_OFFSET + bank * GPIO_BANK_STRIDE;
144 gpio->set_dr_val[bank] = ioread32(gpio->base + bank_offset);
146 return 0;
149 static int xgene_gpio_resume(struct device *dev)
151 struct xgene_gpio *gpio = dev_get_drvdata(dev);
152 unsigned long bank_offset;
153 unsigned int bank;
155 for (bank = 0; bank < XGENE_MAX_GPIO_BANKS; bank++) {
156 bank_offset = GPIO_SET_DR_OFFSET + bank * GPIO_BANK_STRIDE;
157 iowrite32(gpio->set_dr_val[bank], gpio->base + bank_offset);
159 return 0;
162 static SIMPLE_DEV_PM_OPS(xgene_gpio_pm, xgene_gpio_suspend, xgene_gpio_resume);
163 #define XGENE_GPIO_PM_OPS (&xgene_gpio_pm)
164 #else
165 #define XGENE_GPIO_PM_OPS NULL
166 #endif
168 static int xgene_gpio_probe(struct platform_device *pdev)
170 struct resource *res;
171 struct xgene_gpio *gpio;
172 int err = 0;
174 gpio = devm_kzalloc(&pdev->dev, sizeof(*gpio), GFP_KERNEL);
175 if (!gpio) {
176 err = -ENOMEM;
177 goto err;
180 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
181 gpio->base = devm_ioremap_nocache(&pdev->dev, res->start,
182 resource_size(res));
183 if (!gpio->base) {
184 err = -ENOMEM;
185 goto err;
188 gpio->chip.ngpio = XGENE_MAX_GPIOS;
190 spin_lock_init(&gpio->lock);
191 gpio->chip.dev = &pdev->dev;
192 gpio->chip.direction_input = xgene_gpio_dir_in;
193 gpio->chip.direction_output = xgene_gpio_dir_out;
194 gpio->chip.get = xgene_gpio_get;
195 gpio->chip.set = xgene_gpio_set;
196 gpio->chip.label = dev_name(&pdev->dev);
197 gpio->chip.base = -1;
199 platform_set_drvdata(pdev, gpio);
201 err = gpiochip_add(&gpio->chip);
202 if (err) {
203 dev_err(&pdev->dev,
204 "failed to register gpiochip.\n");
205 goto err;
208 dev_info(&pdev->dev, "X-Gene GPIO driver registered.\n");
209 return 0;
210 err:
211 dev_err(&pdev->dev, "X-Gene GPIO driver registration failed.\n");
212 return err;
215 static int xgene_gpio_remove(struct platform_device *pdev)
217 struct xgene_gpio *gpio = platform_get_drvdata(pdev);
219 gpiochip_remove(&gpio->chip);
220 return 0;
223 static const struct of_device_id xgene_gpio_of_match[] = {
224 { .compatible = "apm,xgene-gpio", },
227 MODULE_DEVICE_TABLE(of, xgene_gpio_of_match);
229 static struct platform_driver xgene_gpio_driver = {
230 .driver = {
231 .name = "xgene-gpio",
232 .of_match_table = xgene_gpio_of_match,
233 .pm = XGENE_GPIO_PM_OPS,
235 .probe = xgene_gpio_probe,
236 .remove = xgene_gpio_remove,
239 module_platform_driver(xgene_gpio_driver);
241 MODULE_AUTHOR("Feng Kan <fkan@apm.com>");
242 MODULE_DESCRIPTION("APM X-Gene GPIO driver");
243 MODULE_LICENSE("GPL");