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>
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)
44 struct gpio_chip chip
;
48 u32 set_dr_val
[XGENE_MAX_GPIO_BANKS
];
52 static inline struct xgene_gpio
*to_xgene_gpio(struct gpio_chip
*chip
)
54 return container_of(chip
, struct xgene_gpio
, chip
);
57 static int xgene_gpio_get(struct gpio_chip
*gc
, unsigned int offset
)
59 struct xgene_gpio
*chip
= to_xgene_gpio(gc
);
60 unsigned long bank_offset
;
63 bank_offset
= GPIO_DATA_OFFSET
+ GPIO_BANK_OFFSET(offset
);
64 bit_offset
= GPIO_BIT_OFFSET(offset
);
65 return !!(ioread32(chip
->base
+ bank_offset
) & BIT(bit_offset
));
68 static void __xgene_gpio_set(struct gpio_chip
*gc
, unsigned int offset
, int val
)
70 struct xgene_gpio
*chip
= to_xgene_gpio(gc
);
71 unsigned long bank_offset
;
72 u32 setval
, bit_offset
;
74 bank_offset
= GPIO_SET_DR_OFFSET
+ GPIO_BANK_OFFSET(offset
);
75 bit_offset
= GPIO_BIT_OFFSET(offset
) + XGENE_GPIOS_PER_BANK
;
77 setval
= ioread32(chip
->base
+ bank_offset
);
79 setval
|= BIT(bit_offset
);
81 setval
&= ~BIT(bit_offset
);
82 iowrite32(setval
, chip
->base
+ bank_offset
);
85 static void xgene_gpio_set(struct gpio_chip
*gc
, unsigned int offset
, int val
)
87 struct xgene_gpio
*chip
= to_xgene_gpio(gc
);
90 spin_lock_irqsave(&chip
->lock
, flags
);
91 __xgene_gpio_set(gc
, offset
, val
);
92 spin_unlock_irqrestore(&chip
->lock
, flags
);
95 static int xgene_gpio_dir_in(struct gpio_chip
*gc
, unsigned int offset
)
97 struct xgene_gpio
*chip
= to_xgene_gpio(gc
);
98 unsigned long flags
, bank_offset
;
99 u32 dirval
, bit_offset
;
101 bank_offset
= GPIO_SET_DR_OFFSET
+ GPIO_BANK_OFFSET(offset
);
102 bit_offset
= GPIO_BIT_OFFSET(offset
);
104 spin_lock_irqsave(&chip
->lock
, flags
);
106 dirval
= ioread32(chip
->base
+ bank_offset
);
107 dirval
|= BIT(bit_offset
);
108 iowrite32(dirval
, chip
->base
+ bank_offset
);
110 spin_unlock_irqrestore(&chip
->lock
, flags
);
115 static int xgene_gpio_dir_out(struct gpio_chip
*gc
,
116 unsigned int offset
, int val
)
118 struct xgene_gpio
*chip
= to_xgene_gpio(gc
);
119 unsigned long flags
, bank_offset
;
120 u32 dirval
, bit_offset
;
122 bank_offset
= GPIO_SET_DR_OFFSET
+ GPIO_BANK_OFFSET(offset
);
123 bit_offset
= GPIO_BIT_OFFSET(offset
);
125 spin_lock_irqsave(&chip
->lock
, flags
);
127 dirval
= ioread32(chip
->base
+ bank_offset
);
128 dirval
&= ~BIT(bit_offset
);
129 iowrite32(dirval
, chip
->base
+ bank_offset
);
130 __xgene_gpio_set(gc
, offset
, val
);
132 spin_unlock_irqrestore(&chip
->lock
, flags
);
138 static int xgene_gpio_suspend(struct device
*dev
)
140 struct xgene_gpio
*gpio
= dev_get_drvdata(dev
);
141 unsigned long bank_offset
;
144 for (bank
= 0; bank
< XGENE_MAX_GPIO_BANKS
; bank
++) {
145 bank_offset
= GPIO_SET_DR_OFFSET
+ bank
* GPIO_BANK_STRIDE
;
146 gpio
->set_dr_val
[bank
] = ioread32(gpio
->base
+ bank_offset
);
151 static int xgene_gpio_resume(struct device
*dev
)
153 struct xgene_gpio
*gpio
= dev_get_drvdata(dev
);
154 unsigned long bank_offset
;
157 for (bank
= 0; bank
< XGENE_MAX_GPIO_BANKS
; bank
++) {
158 bank_offset
= GPIO_SET_DR_OFFSET
+ bank
* GPIO_BANK_STRIDE
;
159 iowrite32(gpio
->set_dr_val
[bank
], gpio
->base
+ bank_offset
);
164 static SIMPLE_DEV_PM_OPS(xgene_gpio_pm
, xgene_gpio_suspend
, xgene_gpio_resume
);
165 #define XGENE_GPIO_PM_OPS (&xgene_gpio_pm)
167 #define XGENE_GPIO_PM_OPS NULL
170 static int xgene_gpio_probe(struct platform_device
*pdev
)
172 struct resource
*res
;
173 struct xgene_gpio
*gpio
;
176 gpio
= devm_kzalloc(&pdev
->dev
, sizeof(*gpio
), GFP_KERNEL
);
182 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
183 gpio
->base
= devm_ioremap_nocache(&pdev
->dev
, res
->start
,
190 gpio
->chip
.ngpio
= XGENE_MAX_GPIOS
;
192 spin_lock_init(&gpio
->lock
);
193 gpio
->chip
.dev
= &pdev
->dev
;
194 gpio
->chip
.direction_input
= xgene_gpio_dir_in
;
195 gpio
->chip
.direction_output
= xgene_gpio_dir_out
;
196 gpio
->chip
.get
= xgene_gpio_get
;
197 gpio
->chip
.set
= xgene_gpio_set
;
198 gpio
->chip
.label
= dev_name(&pdev
->dev
);
199 gpio
->chip
.base
= -1;
201 platform_set_drvdata(pdev
, gpio
);
203 err
= gpiochip_add(&gpio
->chip
);
206 "failed to register gpiochip.\n");
210 dev_info(&pdev
->dev
, "X-Gene GPIO driver registered.\n");
213 dev_err(&pdev
->dev
, "X-Gene GPIO driver registration failed.\n");
217 static int xgene_gpio_remove(struct platform_device
*pdev
)
219 struct xgene_gpio
*gpio
= platform_get_drvdata(pdev
);
221 gpiochip_remove(&gpio
->chip
);
226 static const struct of_device_id xgene_gpio_of_match
[] = {
227 { .compatible
= "apm,xgene-gpio", },
230 MODULE_DEVICE_TABLE(of
, xgene_gpio_of_match
);
233 static struct platform_driver xgene_gpio_driver
= {
235 .name
= "xgene-gpio",
236 .owner
= THIS_MODULE
,
237 .of_match_table
= xgene_gpio_of_match
,
238 .pm
= XGENE_GPIO_PM_OPS
,
240 .probe
= xgene_gpio_probe
,
241 .remove
= xgene_gpio_remove
,
244 module_platform_driver(xgene_gpio_driver
);
246 MODULE_AUTHOR("Feng Kan <fkan@apm.com>");
247 MODULE_DESCRIPTION("APM X-Gene GPIO driver");
248 MODULE_LICENSE("GPL");