1 // SPDX-License-Identifier: GPL-2.0-only
3 * ZTE ZX296702 GPIO driver
5 * Author: Jun Nie <jun.nie@linaro.org>
7 * Copyright (C) 2015 Linaro Ltd.
9 #include <linux/bitops.h>
10 #include <linux/device.h>
11 #include <linux/errno.h>
12 #include <linux/gpio/driver.h>
13 #include <linux/irqchip/chained_irq.h>
14 #include <linux/init.h>
16 #include <linux/pinctrl/consumer.h>
17 #include <linux/platform_device.h>
19 #include <linux/slab.h>
20 #include <linux/spinlock.h>
22 #define ZX_GPIO_DIR 0x00
23 #define ZX_GPIO_IVE 0x04
24 #define ZX_GPIO_IV 0x08
25 #define ZX_GPIO_IEP 0x0C
26 #define ZX_GPIO_IEN 0x10
27 #define ZX_GPIO_DI 0x14
28 #define ZX_GPIO_DO1 0x18
29 #define ZX_GPIO_DO0 0x1C
30 #define ZX_GPIO_DO 0x20
32 #define ZX_GPIO_IM 0x28
33 #define ZX_GPIO_IE 0x2C
35 #define ZX_GPIO_MIS 0x30
36 #define ZX_GPIO_IC 0x34
47 static int zx_direction_input(struct gpio_chip
*gc
, unsigned offset
)
49 struct zx_gpio
*chip
= gpiochip_get_data(gc
);
53 if (offset
>= gc
->ngpio
)
56 raw_spin_lock_irqsave(&chip
->lock
, flags
);
57 gpiodir
= readw_relaxed(chip
->base
+ ZX_GPIO_DIR
);
58 gpiodir
&= ~BIT(offset
);
59 writew_relaxed(gpiodir
, chip
->base
+ ZX_GPIO_DIR
);
60 raw_spin_unlock_irqrestore(&chip
->lock
, flags
);
65 static int zx_direction_output(struct gpio_chip
*gc
, unsigned offset
,
68 struct zx_gpio
*chip
= gpiochip_get_data(gc
);
72 if (offset
>= gc
->ngpio
)
75 raw_spin_lock_irqsave(&chip
->lock
, flags
);
76 gpiodir
= readw_relaxed(chip
->base
+ ZX_GPIO_DIR
);
77 gpiodir
|= BIT(offset
);
78 writew_relaxed(gpiodir
, chip
->base
+ ZX_GPIO_DIR
);
81 writew_relaxed(BIT(offset
), chip
->base
+ ZX_GPIO_DO1
);
83 writew_relaxed(BIT(offset
), chip
->base
+ ZX_GPIO_DO0
);
84 raw_spin_unlock_irqrestore(&chip
->lock
, flags
);
89 static int zx_get_value(struct gpio_chip
*gc
, unsigned offset
)
91 struct zx_gpio
*chip
= gpiochip_get_data(gc
);
93 return !!(readw_relaxed(chip
->base
+ ZX_GPIO_DI
) & BIT(offset
));
96 static void zx_set_value(struct gpio_chip
*gc
, unsigned offset
, int value
)
98 struct zx_gpio
*chip
= gpiochip_get_data(gc
);
101 writew_relaxed(BIT(offset
), chip
->base
+ ZX_GPIO_DO1
);
103 writew_relaxed(BIT(offset
), chip
->base
+ ZX_GPIO_DO0
);
106 static int zx_irq_type(struct irq_data
*d
, unsigned trigger
)
108 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
109 struct zx_gpio
*chip
= gpiochip_get_data(gc
);
110 int offset
= irqd_to_hwirq(d
);
112 u16 gpiois
, gpioi_epos
, gpioi_eneg
, gpioiev
;
113 u16 bit
= BIT(offset
);
115 if (offset
< 0 || offset
>= ZX_GPIO_NR
)
118 raw_spin_lock_irqsave(&chip
->lock
, flags
);
120 gpioiev
= readw_relaxed(chip
->base
+ ZX_GPIO_IV
);
121 gpiois
= readw_relaxed(chip
->base
+ ZX_GPIO_IVE
);
122 gpioi_epos
= readw_relaxed(chip
->base
+ ZX_GPIO_IEP
);
123 gpioi_eneg
= readw_relaxed(chip
->base
+ ZX_GPIO_IEN
);
125 if (trigger
& (IRQ_TYPE_LEVEL_HIGH
| IRQ_TYPE_LEVEL_LOW
)) {
127 if (trigger
& IRQ_TYPE_LEVEL_HIGH
)
134 if ((trigger
& IRQ_TYPE_EDGE_BOTH
) == IRQ_TYPE_EDGE_BOTH
) {
138 if (trigger
& IRQ_TYPE_EDGE_RISING
) {
141 } else if (trigger
& IRQ_TYPE_EDGE_FALLING
) {
147 writew_relaxed(gpiois
, chip
->base
+ ZX_GPIO_IVE
);
148 writew_relaxed(gpioi_epos
, chip
->base
+ ZX_GPIO_IEP
);
149 writew_relaxed(gpioi_eneg
, chip
->base
+ ZX_GPIO_IEN
);
150 writew_relaxed(gpioiev
, chip
->base
+ ZX_GPIO_IV
);
151 raw_spin_unlock_irqrestore(&chip
->lock
, flags
);
156 static void zx_irq_handler(struct irq_desc
*desc
)
158 unsigned long pending
;
160 struct gpio_chip
*gc
= irq_desc_get_handler_data(desc
);
161 struct zx_gpio
*chip
= gpiochip_get_data(gc
);
162 struct irq_chip
*irqchip
= irq_desc_get_chip(desc
);
164 chained_irq_enter(irqchip
, desc
);
166 pending
= readw_relaxed(chip
->base
+ ZX_GPIO_MIS
);
167 writew_relaxed(pending
, chip
->base
+ ZX_GPIO_IC
);
169 for_each_set_bit(offset
, &pending
, ZX_GPIO_NR
)
170 generic_handle_irq(irq_find_mapping(gc
->irq
.domain
,
174 chained_irq_exit(irqchip
, desc
);
177 static void zx_irq_mask(struct irq_data
*d
)
179 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
180 struct zx_gpio
*chip
= gpiochip_get_data(gc
);
181 u16 mask
= BIT(irqd_to_hwirq(d
) % ZX_GPIO_NR
);
184 raw_spin_lock(&chip
->lock
);
185 gpioie
= readw_relaxed(chip
->base
+ ZX_GPIO_IM
) | mask
;
186 writew_relaxed(gpioie
, chip
->base
+ ZX_GPIO_IM
);
187 gpioie
= readw_relaxed(chip
->base
+ ZX_GPIO_IE
) & ~mask
;
188 writew_relaxed(gpioie
, chip
->base
+ ZX_GPIO_IE
);
189 raw_spin_unlock(&chip
->lock
);
192 static void zx_irq_unmask(struct irq_data
*d
)
194 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
195 struct zx_gpio
*chip
= gpiochip_get_data(gc
);
196 u16 mask
= BIT(irqd_to_hwirq(d
) % ZX_GPIO_NR
);
199 raw_spin_lock(&chip
->lock
);
200 gpioie
= readw_relaxed(chip
->base
+ ZX_GPIO_IM
) & ~mask
;
201 writew_relaxed(gpioie
, chip
->base
+ ZX_GPIO_IM
);
202 gpioie
= readw_relaxed(chip
->base
+ ZX_GPIO_IE
) | mask
;
203 writew_relaxed(gpioie
, chip
->base
+ ZX_GPIO_IE
);
204 raw_spin_unlock(&chip
->lock
);
207 static struct irq_chip zx_irqchip
= {
209 .irq_mask
= zx_irq_mask
,
210 .irq_unmask
= zx_irq_unmask
,
211 .irq_set_type
= zx_irq_type
,
214 static int zx_gpio_probe(struct platform_device
*pdev
)
216 struct device
*dev
= &pdev
->dev
;
217 struct zx_gpio
*chip
;
218 struct gpio_irq_chip
*girq
;
221 chip
= devm_kzalloc(dev
, sizeof(*chip
), GFP_KERNEL
);
225 chip
->base
= devm_platform_ioremap_resource(pdev
, 0);
226 if (IS_ERR(chip
->base
))
227 return PTR_ERR(chip
->base
);
229 id
= of_alias_get_id(dev
->of_node
, "gpio");
231 raw_spin_lock_init(&chip
->lock
);
232 chip
->gc
.request
= gpiochip_generic_request
;
233 chip
->gc
.free
= gpiochip_generic_free
;
234 chip
->gc
.direction_input
= zx_direction_input
;
235 chip
->gc
.direction_output
= zx_direction_output
;
236 chip
->gc
.get
= zx_get_value
;
237 chip
->gc
.set
= zx_set_value
;
238 chip
->gc
.base
= ZX_GPIO_NR
* id
;
239 chip
->gc
.ngpio
= ZX_GPIO_NR
;
240 chip
->gc
.label
= dev_name(dev
);
241 chip
->gc
.parent
= dev
;
242 chip
->gc
.owner
= THIS_MODULE
;
247 writew_relaxed(0xffff, chip
->base
+ ZX_GPIO_IM
);
248 writew_relaxed(0, chip
->base
+ ZX_GPIO_IE
);
249 irq
= platform_get_irq(pdev
, 0);
252 girq
= &chip
->gc
.irq
;
253 girq
->chip
= &zx_irqchip
;
254 girq
->parent_handler
= zx_irq_handler
;
255 girq
->num_parents
= 1;
256 girq
->parents
= devm_kcalloc(&pdev
->dev
, 1,
257 sizeof(*girq
->parents
),
261 girq
->parents
[0] = irq
;
262 girq
->default_type
= IRQ_TYPE_NONE
;
263 girq
->handler
= handle_simple_irq
;
265 ret
= gpiochip_add_data(&chip
->gc
, chip
);
269 platform_set_drvdata(pdev
, chip
);
270 dev_info(dev
, "ZX GPIO chip registered\n");
275 static const struct of_device_id zx_gpio_match
[] = {
277 .compatible
= "zte,zx296702-gpio",
282 static struct platform_driver zx_gpio_driver
= {
283 .probe
= zx_gpio_probe
,
286 .of_match_table
= of_match_ptr(zx_gpio_match
),
289 builtin_platform_driver(zx_gpio_driver
)