2 * linux/drivers/gpio/gpio-mb86s7x.c
4 * Copyright (C) 2015 Fujitsu Semiconductor Limited
5 * Copyright (C) 2015 Linaro Ltd.
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, version 2 of the License.
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.
18 #include <linux/init.h>
19 #include <linux/clk.h>
20 #include <linux/module.h>
21 #include <linux/err.h>
22 #include <linux/errno.h>
23 #include <linux/ioport.h>
24 #include <linux/of_device.h>
25 #include <linux/gpio/driver.h>
26 #include <linux/platform_device.h>
27 #include <linux/spinlock.h>
28 #include <linux/slab.h>
31 * Only first 8bits of a register correspond to each pin,
32 * so there are 4 registers for 32 pins.
34 #define PDR(x) (0x0 + x / 8 * 4)
35 #define DDR(x) (0x10 + x / 8 * 4)
36 #define PFR(x) (0x20 + x / 8 * 4)
38 #define OFFSET(x) BIT((x) % 8)
40 struct mb86s70_gpio_chip
{
47 static inline struct mb86s70_gpio_chip
*chip_to_mb86s70(struct gpio_chip
*gc
)
49 return container_of(gc
, struct mb86s70_gpio_chip
, gc
);
52 static int mb86s70_gpio_request(struct gpio_chip
*gc
, unsigned gpio
)
54 struct mb86s70_gpio_chip
*gchip
= chip_to_mb86s70(gc
);
58 spin_lock_irqsave(&gchip
->lock
, flags
);
60 val
= readl(gchip
->base
+ PFR(gpio
));
62 writel(val
, gchip
->base
+ PFR(gpio
));
64 spin_unlock_irqrestore(&gchip
->lock
, flags
);
69 static void mb86s70_gpio_free(struct gpio_chip
*gc
, unsigned gpio
)
71 struct mb86s70_gpio_chip
*gchip
= chip_to_mb86s70(gc
);
75 spin_lock_irqsave(&gchip
->lock
, flags
);
77 val
= readl(gchip
->base
+ PFR(gpio
));
79 writel(val
, gchip
->base
+ PFR(gpio
));
81 spin_unlock_irqrestore(&gchip
->lock
, flags
);
84 static int mb86s70_gpio_direction_input(struct gpio_chip
*gc
, unsigned gpio
)
86 struct mb86s70_gpio_chip
*gchip
= chip_to_mb86s70(gc
);
90 spin_lock_irqsave(&gchip
->lock
, flags
);
92 val
= readl(gchip
->base
+ DDR(gpio
));
94 writel(val
, gchip
->base
+ DDR(gpio
));
96 spin_unlock_irqrestore(&gchip
->lock
, flags
);
101 static int mb86s70_gpio_direction_output(struct gpio_chip
*gc
,
102 unsigned gpio
, int value
)
104 struct mb86s70_gpio_chip
*gchip
= chip_to_mb86s70(gc
);
108 spin_lock_irqsave(&gchip
->lock
, flags
);
110 val
= readl(gchip
->base
+ PDR(gpio
));
114 val
&= ~OFFSET(gpio
);
115 writel(val
, gchip
->base
+ PDR(gpio
));
117 val
= readl(gchip
->base
+ DDR(gpio
));
119 writel(val
, gchip
->base
+ DDR(gpio
));
121 spin_unlock_irqrestore(&gchip
->lock
, flags
);
126 static int mb86s70_gpio_get(struct gpio_chip
*gc
, unsigned gpio
)
128 struct mb86s70_gpio_chip
*gchip
= chip_to_mb86s70(gc
);
130 return !!(readl(gchip
->base
+ PDR(gpio
)) & OFFSET(gpio
));
133 static void mb86s70_gpio_set(struct gpio_chip
*gc
, unsigned gpio
, int value
)
135 struct mb86s70_gpio_chip
*gchip
= chip_to_mb86s70(gc
);
139 spin_lock_irqsave(&gchip
->lock
, flags
);
141 val
= readl(gchip
->base
+ PDR(gpio
));
145 val
&= ~OFFSET(gpio
);
146 writel(val
, gchip
->base
+ PDR(gpio
));
148 spin_unlock_irqrestore(&gchip
->lock
, flags
);
151 static int mb86s70_gpio_probe(struct platform_device
*pdev
)
153 struct mb86s70_gpio_chip
*gchip
;
154 struct resource
*res
;
157 gchip
= devm_kzalloc(&pdev
->dev
, sizeof(*gchip
), GFP_KERNEL
);
161 platform_set_drvdata(pdev
, gchip
);
163 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
164 gchip
->base
= devm_ioremap_resource(&pdev
->dev
, res
);
165 if (IS_ERR(gchip
->base
))
166 return PTR_ERR(gchip
->base
);
168 gchip
->clk
= devm_clk_get(&pdev
->dev
, NULL
);
169 if (IS_ERR(gchip
->clk
))
170 return PTR_ERR(gchip
->clk
);
172 clk_prepare_enable(gchip
->clk
);
174 spin_lock_init(&gchip
->lock
);
176 gchip
->gc
.direction_output
= mb86s70_gpio_direction_output
;
177 gchip
->gc
.direction_input
= mb86s70_gpio_direction_input
;
178 gchip
->gc
.request
= mb86s70_gpio_request
;
179 gchip
->gc
.free
= mb86s70_gpio_free
;
180 gchip
->gc
.get
= mb86s70_gpio_get
;
181 gchip
->gc
.set
= mb86s70_gpio_set
;
182 gchip
->gc
.label
= dev_name(&pdev
->dev
);
183 gchip
->gc
.ngpio
= 32;
184 gchip
->gc
.owner
= THIS_MODULE
;
185 gchip
->gc
.dev
= &pdev
->dev
;
188 platform_set_drvdata(pdev
, gchip
);
190 ret
= gpiochip_add(&gchip
->gc
);
192 dev_err(&pdev
->dev
, "couldn't register gpio driver\n");
193 clk_disable_unprepare(gchip
->clk
);
199 static int mb86s70_gpio_remove(struct platform_device
*pdev
)
201 struct mb86s70_gpio_chip
*gchip
= platform_get_drvdata(pdev
);
203 gpiochip_remove(&gchip
->gc
);
204 clk_disable_unprepare(gchip
->clk
);
209 static const struct of_device_id mb86s70_gpio_dt_ids
[] = {
210 { .compatible
= "fujitsu,mb86s70-gpio" },
213 MODULE_DEVICE_TABLE(of
, mb86s70_gpio_dt_ids
);
215 static struct platform_driver mb86s70_gpio_driver
= {
217 .name
= "mb86s70-gpio",
218 .of_match_table
= mb86s70_gpio_dt_ids
,
220 .probe
= mb86s70_gpio_probe
,
221 .remove
= mb86s70_gpio_remove
,
224 static int __init
mb86s70_gpio_init(void)
226 return platform_driver_register(&mb86s70_gpio_driver
);
228 module_init(mb86s70_gpio_init
);
230 MODULE_DESCRIPTION("MB86S7x GPIO Driver");
231 MODULE_ALIAS("platform:mb86s70-gpio");
232 MODULE_LICENSE("GPL");