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.
17 #include <linux/acpi.h>
19 #include <linux/init.h>
20 #include <linux/clk.h>
21 #include <linux/module.h>
22 #include <linux/err.h>
23 #include <linux/errno.h>
24 #include <linux/ioport.h>
25 #include <linux/of_device.h>
26 #include <linux/gpio/driver.h>
27 #include <linux/platform_device.h>
28 #include <linux/spinlock.h>
29 #include <linux/slab.h>
34 * Only first 8bits of a register correspond to each pin,
35 * so there are 4 registers for 32 pins.
37 #define PDR(x) (0x0 + x / 8 * 4)
38 #define DDR(x) (0x10 + x / 8 * 4)
39 #define PFR(x) (0x20 + x / 8 * 4)
41 #define OFFSET(x) BIT((x) % 8)
43 struct mb86s70_gpio_chip {
50 static int mb86s70_gpio_request(struct gpio_chip *gc, unsigned gpio)
52 struct mb86s70_gpio_chip *gchip = gpiochip_get_data(gc);
56 spin_lock_irqsave(&gchip->lock, flags);
58 val = readl(gchip->base + PFR(gpio));
60 writel(val, gchip->base + PFR(gpio));
62 spin_unlock_irqrestore(&gchip->lock, flags);
67 static void mb86s70_gpio_free(struct gpio_chip *gc, unsigned gpio)
69 struct mb86s70_gpio_chip *gchip = gpiochip_get_data(gc);
73 spin_lock_irqsave(&gchip->lock, flags);
75 val = readl(gchip->base + PFR(gpio));
77 writel(val, gchip->base + PFR(gpio));
79 spin_unlock_irqrestore(&gchip->lock, flags);
82 static int mb86s70_gpio_direction_input(struct gpio_chip *gc, unsigned gpio)
84 struct mb86s70_gpio_chip *gchip = gpiochip_get_data(gc);
88 spin_lock_irqsave(&gchip->lock, flags);
90 val = readl(gchip->base + DDR(gpio));
92 writel(val, gchip->base + DDR(gpio));
94 spin_unlock_irqrestore(&gchip->lock, flags);
99 static int mb86s70_gpio_direction_output(struct gpio_chip *gc,
100 unsigned gpio, int value)
102 struct mb86s70_gpio_chip *gchip = gpiochip_get_data(gc);
106 spin_lock_irqsave(&gchip->lock, flags);
108 val = readl(gchip->base + PDR(gpio));
112 val &= ~OFFSET(gpio);
113 writel(val, gchip->base + PDR(gpio));
115 val = readl(gchip->base + DDR(gpio));
117 writel(val, gchip->base + DDR(gpio));
119 spin_unlock_irqrestore(&gchip->lock, flags);
124 static int mb86s70_gpio_get(struct gpio_chip *gc, unsigned gpio)
126 struct mb86s70_gpio_chip *gchip = gpiochip_get_data(gc);
128 return !!(readl(gchip->base + PDR(gpio)) & OFFSET(gpio));
131 static void mb86s70_gpio_set(struct gpio_chip *gc, unsigned gpio, int value)
133 struct mb86s70_gpio_chip *gchip = gpiochip_get_data(gc);
137 spin_lock_irqsave(&gchip->lock, flags);
139 val = readl(gchip->base + PDR(gpio));
143 val &= ~OFFSET(gpio);
144 writel(val, gchip->base + PDR(gpio));
146 spin_unlock_irqrestore(&gchip->lock, flags);
149 static int mb86s70_gpio_to_irq(struct gpio_chip *gc, unsigned int offset)
153 for (index = 0;; index++) {
154 irq = platform_get_irq(to_platform_device(gc->parent), index);
157 if (irq_get_irq_data(irq)->hwirq == offset)
163 static int mb86s70_gpio_probe(struct platform_device *pdev)
165 struct mb86s70_gpio_chip *gchip;
168 gchip = devm_kzalloc(&pdev->dev, sizeof(*gchip), GFP_KERNEL);
172 platform_set_drvdata(pdev, gchip);
174 gchip->base = devm_platform_ioremap_resource(pdev, 0);
175 if (IS_ERR(gchip->base))
176 return PTR_ERR(gchip->base);
178 if (!has_acpi_companion(&pdev->dev)) {
179 gchip->clk = devm_clk_get(&pdev->dev, NULL);
180 if (IS_ERR(gchip->clk))
181 return PTR_ERR(gchip->clk);
183 ret = clk_prepare_enable(gchip->clk);
188 spin_lock_init(&gchip->lock);
190 gchip->gc.direction_output = mb86s70_gpio_direction_output;
191 gchip->gc.direction_input = mb86s70_gpio_direction_input;
192 gchip->gc.request = mb86s70_gpio_request;
193 gchip->gc.free = mb86s70_gpio_free;
194 gchip->gc.get = mb86s70_gpio_get;
195 gchip->gc.set = mb86s70_gpio_set;
196 gchip->gc.label = dev_name(&pdev->dev);
197 gchip->gc.ngpio = 32;
198 gchip->gc.owner = THIS_MODULE;
199 gchip->gc.parent = &pdev->dev;
202 if (has_acpi_companion(&pdev->dev))
203 gchip->gc.to_irq = mb86s70_gpio_to_irq;
205 ret = gpiochip_add_data(&gchip->gc, gchip);
207 dev_err(&pdev->dev, "couldn't register gpio driver\n");
208 clk_disable_unprepare(gchip->clk);
212 if (has_acpi_companion(&pdev->dev))
213 acpi_gpiochip_request_interrupts(&gchip->gc);
218 static int mb86s70_gpio_remove(struct platform_device *pdev)
220 struct mb86s70_gpio_chip *gchip = platform_get_drvdata(pdev);
222 if (has_acpi_companion(&pdev->dev))
223 acpi_gpiochip_free_interrupts(&gchip->gc);
224 gpiochip_remove(&gchip->gc);
225 clk_disable_unprepare(gchip->clk);
230 static const struct of_device_id mb86s70_gpio_dt_ids[] = {
231 { .compatible = "fujitsu,mb86s70-gpio" },
234 MODULE_DEVICE_TABLE(of, mb86s70_gpio_dt_ids);
237 static const struct acpi_device_id mb86s70_gpio_acpi_ids[] = {
241 MODULE_DEVICE_TABLE(acpi, mb86s70_gpio_acpi_ids);
244 static struct platform_driver mb86s70_gpio_driver = {
246 .name = "mb86s70-gpio",
247 .of_match_table = mb86s70_gpio_dt_ids,
248 .acpi_match_table = ACPI_PTR(mb86s70_gpio_acpi_ids),
250 .probe = mb86s70_gpio_probe,
251 .remove = mb86s70_gpio_remove,
253 module_platform_driver(mb86s70_gpio_driver);
255 MODULE_DESCRIPTION("MB86S7x GPIO Driver");
256 MODULE_ALIAS("platform:mb86s70-gpio");
257 MODULE_LICENSE("GPL");