1 // SPDX-License-Identifier: GPL-2.0-only
3 * linux/drivers/gpio/gpio-mb86s7x.c
5 * Copyright (C) 2015 Fujitsu Semiconductor Limited
6 * Copyright (C) 2015 Linaro Ltd.
9 #include <linux/acpi.h>
11 #include <linux/init.h>
12 #include <linux/clk.h>
13 #include <linux/module.h>
14 #include <linux/err.h>
15 #include <linux/errno.h>
16 #include <linux/ioport.h>
17 #include <linux/of_device.h>
18 #include <linux/gpio/driver.h>
19 #include <linux/platform_device.h>
20 #include <linux/spinlock.h>
21 #include <linux/slab.h>
24 #include "gpiolib-acpi.h"
27 * Only first 8bits of a register correspond to each pin,
28 * so there are 4 registers for 32 pins.
30 #define PDR(x) (0x0 + x / 8 * 4)
31 #define DDR(x) (0x10 + x / 8 * 4)
32 #define PFR(x) (0x20 + x / 8 * 4)
34 #define OFFSET(x) BIT((x) % 8)
36 struct mb86s70_gpio_chip {
43 static int mb86s70_gpio_request(struct gpio_chip *gc, unsigned gpio)
45 struct mb86s70_gpio_chip *gchip = gpiochip_get_data(gc);
49 spin_lock_irqsave(&gchip->lock, flags);
51 val = readl(gchip->base + PFR(gpio));
53 writel(val, gchip->base + PFR(gpio));
55 spin_unlock_irqrestore(&gchip->lock, flags);
60 static void mb86s70_gpio_free(struct gpio_chip *gc, unsigned gpio)
62 struct mb86s70_gpio_chip *gchip = gpiochip_get_data(gc);
66 spin_lock_irqsave(&gchip->lock, flags);
68 val = readl(gchip->base + PFR(gpio));
70 writel(val, gchip->base + PFR(gpio));
72 spin_unlock_irqrestore(&gchip->lock, flags);
75 static int mb86s70_gpio_direction_input(struct gpio_chip *gc, unsigned gpio)
77 struct mb86s70_gpio_chip *gchip = gpiochip_get_data(gc);
81 spin_lock_irqsave(&gchip->lock, flags);
83 val = readl(gchip->base + DDR(gpio));
85 writel(val, gchip->base + DDR(gpio));
87 spin_unlock_irqrestore(&gchip->lock, flags);
92 static int mb86s70_gpio_direction_output(struct gpio_chip *gc,
93 unsigned gpio, int value)
95 struct mb86s70_gpio_chip *gchip = gpiochip_get_data(gc);
99 spin_lock_irqsave(&gchip->lock, flags);
101 val = readl(gchip->base + PDR(gpio));
105 val &= ~OFFSET(gpio);
106 writel(val, gchip->base + PDR(gpio));
108 val = readl(gchip->base + DDR(gpio));
110 writel(val, gchip->base + DDR(gpio));
112 spin_unlock_irqrestore(&gchip->lock, flags);
117 static int mb86s70_gpio_get(struct gpio_chip *gc, unsigned gpio)
119 struct mb86s70_gpio_chip *gchip = gpiochip_get_data(gc);
121 return !!(readl(gchip->base + PDR(gpio)) & OFFSET(gpio));
124 static void mb86s70_gpio_set(struct gpio_chip *gc, unsigned gpio, int value)
126 struct mb86s70_gpio_chip *gchip = gpiochip_get_data(gc);
130 spin_lock_irqsave(&gchip->lock, flags);
132 val = readl(gchip->base + PDR(gpio));
136 val &= ~OFFSET(gpio);
137 writel(val, gchip->base + PDR(gpio));
139 spin_unlock_irqrestore(&gchip->lock, flags);
142 static int mb86s70_gpio_to_irq(struct gpio_chip *gc, unsigned int offset)
146 for (index = 0;; index++) {
147 irq = platform_get_irq(to_platform_device(gc->parent), index);
152 if (irq_get_irq_data(irq)->hwirq == offset)
158 static int mb86s70_gpio_probe(struct platform_device *pdev)
160 struct mb86s70_gpio_chip *gchip;
163 gchip = devm_kzalloc(&pdev->dev, sizeof(*gchip), GFP_KERNEL);
167 platform_set_drvdata(pdev, gchip);
169 gchip->base = devm_platform_ioremap_resource(pdev, 0);
170 if (IS_ERR(gchip->base))
171 return PTR_ERR(gchip->base);
173 gchip->clk = devm_clk_get_optional(&pdev->dev, NULL);
174 if (IS_ERR(gchip->clk))
175 return PTR_ERR(gchip->clk);
177 ret = clk_prepare_enable(gchip->clk);
181 spin_lock_init(&gchip->lock);
183 gchip->gc.direction_output = mb86s70_gpio_direction_output;
184 gchip->gc.direction_input = mb86s70_gpio_direction_input;
185 gchip->gc.request = mb86s70_gpio_request;
186 gchip->gc.free = mb86s70_gpio_free;
187 gchip->gc.get = mb86s70_gpio_get;
188 gchip->gc.set = mb86s70_gpio_set;
189 gchip->gc.to_irq = mb86s70_gpio_to_irq;
190 gchip->gc.label = dev_name(&pdev->dev);
191 gchip->gc.ngpio = 32;
192 gchip->gc.owner = THIS_MODULE;
193 gchip->gc.parent = &pdev->dev;
196 ret = gpiochip_add_data(&gchip->gc, gchip);
198 dev_err(&pdev->dev, "couldn't register gpio driver\n");
199 clk_disable_unprepare(gchip->clk);
203 acpi_gpiochip_request_interrupts(&gchip->gc);
208 static int mb86s70_gpio_remove(struct platform_device *pdev)
210 struct mb86s70_gpio_chip *gchip = platform_get_drvdata(pdev);
212 acpi_gpiochip_free_interrupts(&gchip->gc);
213 gpiochip_remove(&gchip->gc);
214 clk_disable_unprepare(gchip->clk);
219 static const struct of_device_id mb86s70_gpio_dt_ids[] = {
220 { .compatible = "fujitsu,mb86s70-gpio" },
223 MODULE_DEVICE_TABLE(of, mb86s70_gpio_dt_ids);
226 static const struct acpi_device_id mb86s70_gpio_acpi_ids[] = {
230 MODULE_DEVICE_TABLE(acpi, mb86s70_gpio_acpi_ids);
233 static struct platform_driver mb86s70_gpio_driver = {
235 .name = "mb86s70-gpio",
236 .of_match_table = mb86s70_gpio_dt_ids,
237 .acpi_match_table = ACPI_PTR(mb86s70_gpio_acpi_ids),
239 .probe = mb86s70_gpio_probe,
240 .remove = mb86s70_gpio_remove,
242 module_platform_driver(mb86s70_gpio_driver);
244 MODULE_DESCRIPTION("MB86S7x GPIO Driver");
245 MODULE_ALIAS("platform:mb86s70-gpio");
246 MODULE_LICENSE("GPL");