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/mod_devicetable.h>
14 #include <linux/module.h>
15 #include <linux/err.h>
16 #include <linux/errno.h>
17 #include <linux/ioport.h>
18 #include <linux/gpio/driver.h>
19 #include <linux/platform_device.h>
20 #include <linux/spinlock.h>
21 #include <linux/slab.h>
23 #include "gpiolib-acpi.h"
26 * Only first 8bits of a register correspond to each pin,
27 * so there are 4 registers for 32 pins.
29 #define PDR(x) (0x0 + x / 8 * 4)
30 #define DDR(x) (0x10 + x / 8 * 4)
31 #define PFR(x) (0x20 + x / 8 * 4)
33 #define OFFSET(x) BIT((x) % 8)
35 struct mb86s70_gpio_chip
{
41 static int mb86s70_gpio_request(struct gpio_chip
*gc
, unsigned gpio
)
43 struct mb86s70_gpio_chip
*gchip
= gpiochip_get_data(gc
);
47 spin_lock_irqsave(&gchip
->lock
, flags
);
49 val
= readl(gchip
->base
+ PFR(gpio
));
51 writel(val
, gchip
->base
+ PFR(gpio
));
53 spin_unlock_irqrestore(&gchip
->lock
, flags
);
58 static void mb86s70_gpio_free(struct gpio_chip
*gc
, unsigned gpio
)
60 struct mb86s70_gpio_chip
*gchip
= gpiochip_get_data(gc
);
64 spin_lock_irqsave(&gchip
->lock
, flags
);
66 val
= readl(gchip
->base
+ PFR(gpio
));
68 writel(val
, gchip
->base
+ PFR(gpio
));
70 spin_unlock_irqrestore(&gchip
->lock
, flags
);
73 static int mb86s70_gpio_direction_input(struct gpio_chip
*gc
, unsigned gpio
)
75 struct mb86s70_gpio_chip
*gchip
= gpiochip_get_data(gc
);
79 spin_lock_irqsave(&gchip
->lock
, flags
);
81 val
= readl(gchip
->base
+ DDR(gpio
));
83 writel(val
, gchip
->base
+ DDR(gpio
));
85 spin_unlock_irqrestore(&gchip
->lock
, flags
);
90 static int mb86s70_gpio_direction_output(struct gpio_chip
*gc
,
91 unsigned gpio
, int value
)
93 struct mb86s70_gpio_chip
*gchip
= gpiochip_get_data(gc
);
97 spin_lock_irqsave(&gchip
->lock
, flags
);
99 val
= readl(gchip
->base
+ PDR(gpio
));
103 val
&= ~OFFSET(gpio
);
104 writel(val
, gchip
->base
+ PDR(gpio
));
106 val
= readl(gchip
->base
+ DDR(gpio
));
108 writel(val
, gchip
->base
+ DDR(gpio
));
110 spin_unlock_irqrestore(&gchip
->lock
, flags
);
115 static int mb86s70_gpio_get(struct gpio_chip
*gc
, unsigned gpio
)
117 struct mb86s70_gpio_chip
*gchip
= gpiochip_get_data(gc
);
119 return !!(readl(gchip
->base
+ PDR(gpio
)) & OFFSET(gpio
));
122 static void mb86s70_gpio_set(struct gpio_chip
*gc
, unsigned gpio
, int value
)
124 struct mb86s70_gpio_chip
*gchip
= gpiochip_get_data(gc
);
128 spin_lock_irqsave(&gchip
->lock
, flags
);
130 val
= readl(gchip
->base
+ PDR(gpio
));
134 val
&= ~OFFSET(gpio
);
135 writel(val
, gchip
->base
+ PDR(gpio
));
137 spin_unlock_irqrestore(&gchip
->lock
, flags
);
140 static int mb86s70_gpio_to_irq(struct gpio_chip
*gc
, unsigned int offset
)
144 for (index
= 0;; index
++) {
145 irq
= platform_get_irq(to_platform_device(gc
->parent
), index
);
148 if (irq_get_irq_data(irq
)->hwirq
== offset
)
154 static int mb86s70_gpio_probe(struct platform_device
*pdev
)
156 struct mb86s70_gpio_chip
*gchip
;
160 gchip
= devm_kzalloc(&pdev
->dev
, sizeof(*gchip
), GFP_KERNEL
);
164 platform_set_drvdata(pdev
, gchip
);
166 gchip
->base
= devm_platform_ioremap_resource(pdev
, 0);
167 if (IS_ERR(gchip
->base
))
168 return PTR_ERR(gchip
->base
);
170 clk
= devm_clk_get_optional_enabled(&pdev
->dev
, NULL
);
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
.to_irq
= mb86s70_gpio_to_irq
;
183 gchip
->gc
.label
= dev_name(&pdev
->dev
);
184 gchip
->gc
.ngpio
= 32;
185 gchip
->gc
.owner
= THIS_MODULE
;
186 gchip
->gc
.parent
= &pdev
->dev
;
189 ret
= gpiochip_add_data(&gchip
->gc
, gchip
);
191 return dev_err_probe(&pdev
->dev
, ret
,
192 "couldn't register gpio driver\n");
194 acpi_gpiochip_request_interrupts(&gchip
->gc
);
199 static void mb86s70_gpio_remove(struct platform_device
*pdev
)
201 struct mb86s70_gpio_chip
*gchip
= platform_get_drvdata(pdev
);
203 acpi_gpiochip_free_interrupts(&gchip
->gc
);
204 gpiochip_remove(&gchip
->gc
);
207 static const struct of_device_id mb86s70_gpio_dt_ids
[] = {
208 { .compatible
= "fujitsu,mb86s70-gpio" },
211 MODULE_DEVICE_TABLE(of
, mb86s70_gpio_dt_ids
);
214 static const struct acpi_device_id mb86s70_gpio_acpi_ids
[] = {
218 MODULE_DEVICE_TABLE(acpi
, mb86s70_gpio_acpi_ids
);
221 static struct platform_driver mb86s70_gpio_driver
= {
223 .name
= "mb86s70-gpio",
224 .of_match_table
= mb86s70_gpio_dt_ids
,
225 .acpi_match_table
= ACPI_PTR(mb86s70_gpio_acpi_ids
),
227 .probe
= mb86s70_gpio_probe
,
228 .remove
= mb86s70_gpio_remove
,
230 module_platform_driver(mb86s70_gpio_driver
);
232 MODULE_DESCRIPTION("MB86S7x GPIO Driver");
233 MODULE_ALIAS("platform:mb86s70-gpio");
234 MODULE_LICENSE("GPL");