Linux 5.2
[linux-2.6/linux-2.6-arm.git] / drivers / gpio / gpio-mb86s7x.c
blob9bfff171f9fef3c073f9bfda74d0025fe64cd4e1
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * linux/drivers/gpio/gpio-mb86s7x.c
5 * Copyright (C) 2015 Fujitsu Semiconductor Limited
6 * Copyright (C) 2015 Linaro Ltd.
7 */
9 #include <linux/io.h>
10 #include <linux/init.h>
11 #include <linux/clk.h>
12 #include <linux/module.h>
13 #include <linux/err.h>
14 #include <linux/errno.h>
15 #include <linux/ioport.h>
16 #include <linux/of_device.h>
17 #include <linux/gpio/driver.h>
18 #include <linux/platform_device.h>
19 #include <linux/spinlock.h>
20 #include <linux/slab.h>
23 * Only first 8bits of a register correspond to each pin,
24 * so there are 4 registers for 32 pins.
26 #define PDR(x) (0x0 + x / 8 * 4)
27 #define DDR(x) (0x10 + x / 8 * 4)
28 #define PFR(x) (0x20 + x / 8 * 4)
30 #define OFFSET(x) BIT((x) % 8)
32 struct mb86s70_gpio_chip {
33 struct gpio_chip gc;
34 void __iomem *base;
35 struct clk *clk;
36 spinlock_t lock;
39 static int mb86s70_gpio_request(struct gpio_chip *gc, unsigned gpio)
41 struct mb86s70_gpio_chip *gchip = gpiochip_get_data(gc);
42 unsigned long flags;
43 u32 val;
45 spin_lock_irqsave(&gchip->lock, flags);
47 val = readl(gchip->base + PFR(gpio));
48 val &= ~OFFSET(gpio);
49 writel(val, gchip->base + PFR(gpio));
51 spin_unlock_irqrestore(&gchip->lock, flags);
53 return 0;
56 static void mb86s70_gpio_free(struct gpio_chip *gc, unsigned gpio)
58 struct mb86s70_gpio_chip *gchip = gpiochip_get_data(gc);
59 unsigned long flags;
60 u32 val;
62 spin_lock_irqsave(&gchip->lock, flags);
64 val = readl(gchip->base + PFR(gpio));
65 val |= OFFSET(gpio);
66 writel(val, gchip->base + PFR(gpio));
68 spin_unlock_irqrestore(&gchip->lock, flags);
71 static int mb86s70_gpio_direction_input(struct gpio_chip *gc, unsigned gpio)
73 struct mb86s70_gpio_chip *gchip = gpiochip_get_data(gc);
74 unsigned long flags;
75 unsigned char val;
77 spin_lock_irqsave(&gchip->lock, flags);
79 val = readl(gchip->base + DDR(gpio));
80 val &= ~OFFSET(gpio);
81 writel(val, gchip->base + DDR(gpio));
83 spin_unlock_irqrestore(&gchip->lock, flags);
85 return 0;
88 static int mb86s70_gpio_direction_output(struct gpio_chip *gc,
89 unsigned gpio, int value)
91 struct mb86s70_gpio_chip *gchip = gpiochip_get_data(gc);
92 unsigned long flags;
93 unsigned char val;
95 spin_lock_irqsave(&gchip->lock, flags);
97 val = readl(gchip->base + PDR(gpio));
98 if (value)
99 val |= OFFSET(gpio);
100 else
101 val &= ~OFFSET(gpio);
102 writel(val, gchip->base + PDR(gpio));
104 val = readl(gchip->base + DDR(gpio));
105 val |= OFFSET(gpio);
106 writel(val, gchip->base + DDR(gpio));
108 spin_unlock_irqrestore(&gchip->lock, flags);
110 return 0;
113 static int mb86s70_gpio_get(struct gpio_chip *gc, unsigned gpio)
115 struct mb86s70_gpio_chip *gchip = gpiochip_get_data(gc);
117 return !!(readl(gchip->base + PDR(gpio)) & OFFSET(gpio));
120 static void mb86s70_gpio_set(struct gpio_chip *gc, unsigned gpio, int value)
122 struct mb86s70_gpio_chip *gchip = gpiochip_get_data(gc);
123 unsigned long flags;
124 unsigned char val;
126 spin_lock_irqsave(&gchip->lock, flags);
128 val = readl(gchip->base + PDR(gpio));
129 if (value)
130 val |= OFFSET(gpio);
131 else
132 val &= ~OFFSET(gpio);
133 writel(val, gchip->base + PDR(gpio));
135 spin_unlock_irqrestore(&gchip->lock, flags);
138 static int mb86s70_gpio_probe(struct platform_device *pdev)
140 struct mb86s70_gpio_chip *gchip;
141 int ret;
143 gchip = devm_kzalloc(&pdev->dev, sizeof(*gchip), GFP_KERNEL);
144 if (gchip == NULL)
145 return -ENOMEM;
147 platform_set_drvdata(pdev, gchip);
149 gchip->base = devm_platform_ioremap_resource(pdev, 0);
150 if (IS_ERR(gchip->base))
151 return PTR_ERR(gchip->base);
153 gchip->clk = devm_clk_get(&pdev->dev, NULL);
154 if (IS_ERR(gchip->clk))
155 return PTR_ERR(gchip->clk);
157 ret = clk_prepare_enable(gchip->clk);
158 if (ret)
159 return ret;
161 spin_lock_init(&gchip->lock);
163 gchip->gc.direction_output = mb86s70_gpio_direction_output;
164 gchip->gc.direction_input = mb86s70_gpio_direction_input;
165 gchip->gc.request = mb86s70_gpio_request;
166 gchip->gc.free = mb86s70_gpio_free;
167 gchip->gc.get = mb86s70_gpio_get;
168 gchip->gc.set = mb86s70_gpio_set;
169 gchip->gc.label = dev_name(&pdev->dev);
170 gchip->gc.ngpio = 32;
171 gchip->gc.owner = THIS_MODULE;
172 gchip->gc.parent = &pdev->dev;
173 gchip->gc.base = -1;
175 ret = gpiochip_add_data(&gchip->gc, gchip);
176 if (ret) {
177 dev_err(&pdev->dev, "couldn't register gpio driver\n");
178 clk_disable_unprepare(gchip->clk);
181 return ret;
184 static int mb86s70_gpio_remove(struct platform_device *pdev)
186 struct mb86s70_gpio_chip *gchip = platform_get_drvdata(pdev);
188 gpiochip_remove(&gchip->gc);
189 clk_disable_unprepare(gchip->clk);
191 return 0;
194 static const struct of_device_id mb86s70_gpio_dt_ids[] = {
195 { .compatible = "fujitsu,mb86s70-gpio" },
196 { /* sentinel */ }
198 MODULE_DEVICE_TABLE(of, mb86s70_gpio_dt_ids);
200 static struct platform_driver mb86s70_gpio_driver = {
201 .driver = {
202 .name = "mb86s70-gpio",
203 .of_match_table = mb86s70_gpio_dt_ids,
205 .probe = mb86s70_gpio_probe,
206 .remove = mb86s70_gpio_remove,
208 module_platform_driver(mb86s70_gpio_driver);
210 MODULE_DESCRIPTION("MB86S7x GPIO Driver");
211 MODULE_ALIAS("platform:mb86s70-gpio");
212 MODULE_LICENSE("GPL");