2 * GPIO controller in LSI ZEVIO SoCs.
4 * Author: Fabian Vogt <fabian@ritter-vogt.de>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
11 #include <linux/spinlock.h>
12 #include <linux/errno.h>
13 #include <linux/module.h>
14 #include <linux/bitops.h>
16 #include <linux/of_device.h>
17 #include <linux/of_gpio.h>
18 #include <linux/slab.h>
19 #include <linux/gpio.h>
23 * This chip has four gpio sections, each controls 8 GPIOs.
24 * Bit 0 in section 0 is GPIO 0, bit 2 in section 1 is GPIO 10.
25 * Disclaimer: Reverse engineered!
26 * For more information refer to:
27 * http://hackspire.unsads.com/wiki/index.php/Memory-mapped_I/O_ports#90000000_-_General_Purpose_I.2FO_.28GPIO.29
29 * 0x00-0x3F: Section 0
30 * +0x00: Masked interrupt status (read-only)
31 * +0x04: R: Interrupt status W: Reset interrupt status
32 * +0x08: R: Interrupt mask W: Mask interrupt
33 * +0x0C: W: Unmask interrupt (write-only)
34 * +0x10: Direction: I/O=1/0
36 * +0x18: Input (read-only)
37 * +0x20: R: Level interrupt W: Set as level interrupt
38 * 0x40-0x7F: Section 1
39 * 0x80-0xBF: Section 2
40 * 0xC0-0xFF: Section 3
43 #define ZEVIO_GPIO_SECTION_SIZE 0x40
45 /* Offsets to various registers */
46 #define ZEVIO_GPIO_INT_MASKED_STATUS 0x00
47 #define ZEVIO_GPIO_INT_STATUS 0x04
48 #define ZEVIO_GPIO_INT_UNMASK 0x08
49 #define ZEVIO_GPIO_INT_MASK 0x0C
50 #define ZEVIO_GPIO_DIRECTION 0x10
51 #define ZEVIO_GPIO_OUTPUT 0x14
52 #define ZEVIO_GPIO_INPUT 0x18
53 #define ZEVIO_GPIO_INT_STICKY 0x20
55 #define to_zevio_gpio(chip) container_of(to_of_mm_gpio_chip(chip), \
56 struct zevio_gpio, chip)
58 /* Bit number of GPIO in its section */
59 #define ZEVIO_GPIO_BIT(gpio) (gpio&7)
63 struct of_mm_gpio_chip chip
;
66 static inline u32
zevio_gpio_port_get(struct zevio_gpio
*c
, unsigned pin
,
69 unsigned section_offset
= ((pin
>> 3) & 3)*ZEVIO_GPIO_SECTION_SIZE
;
70 return readl(IOMEM(c
->chip
.regs
+ section_offset
+ port_offset
));
73 static inline void zevio_gpio_port_set(struct zevio_gpio
*c
, unsigned pin
,
74 unsigned port_offset
, u32 val
)
76 unsigned section_offset
= ((pin
>> 3) & 3)*ZEVIO_GPIO_SECTION_SIZE
;
77 writel(val
, IOMEM(c
->chip
.regs
+ section_offset
+ port_offset
));
80 /* Functions for struct gpio_chip */
81 static int zevio_gpio_get(struct gpio_chip
*chip
, unsigned pin
)
83 struct zevio_gpio
*controller
= to_zevio_gpio(chip
);
85 /* Only reading allowed, so no spinlock needed */
86 u32 val
= zevio_gpio_port_get(controller
, pin
, ZEVIO_GPIO_INPUT
);
88 return (val
>> ZEVIO_GPIO_BIT(pin
)) & 0x1;
91 static void zevio_gpio_set(struct gpio_chip
*chip
, unsigned pin
, int value
)
93 struct zevio_gpio
*controller
= to_zevio_gpio(chip
);
96 spin_lock(&controller
->lock
);
97 val
= zevio_gpio_port_get(controller
, pin
, ZEVIO_GPIO_OUTPUT
);
99 val
|= BIT(ZEVIO_GPIO_BIT(pin
));
101 val
&= ~BIT(ZEVIO_GPIO_BIT(pin
));
103 zevio_gpio_port_set(controller
, pin
, ZEVIO_GPIO_OUTPUT
, val
);
104 spin_unlock(&controller
->lock
);
107 static int zevio_gpio_direction_input(struct gpio_chip
*chip
, unsigned pin
)
109 struct zevio_gpio
*controller
= to_zevio_gpio(chip
);
112 spin_lock(&controller
->lock
);
114 val
= zevio_gpio_port_get(controller
, pin
, ZEVIO_GPIO_DIRECTION
);
115 val
|= BIT(ZEVIO_GPIO_BIT(pin
));
116 zevio_gpio_port_set(controller
, pin
, ZEVIO_GPIO_DIRECTION
, val
);
118 spin_unlock(&controller
->lock
);
123 static int zevio_gpio_direction_output(struct gpio_chip
*chip
,
124 unsigned pin
, int value
)
126 struct zevio_gpio
*controller
= to_zevio_gpio(chip
);
129 spin_lock(&controller
->lock
);
130 val
= zevio_gpio_port_get(controller
, pin
, ZEVIO_GPIO_OUTPUT
);
132 val
|= BIT(ZEVIO_GPIO_BIT(pin
));
134 val
&= ~BIT(ZEVIO_GPIO_BIT(pin
));
136 zevio_gpio_port_set(controller
, pin
, ZEVIO_GPIO_OUTPUT
, val
);
137 val
= zevio_gpio_port_get(controller
, pin
, ZEVIO_GPIO_DIRECTION
);
138 val
&= ~BIT(ZEVIO_GPIO_BIT(pin
));
139 zevio_gpio_port_set(controller
, pin
, ZEVIO_GPIO_DIRECTION
, val
);
141 spin_unlock(&controller
->lock
);
146 static int zevio_gpio_to_irq(struct gpio_chip
*chip
, unsigned pin
)
149 * TODO: Implement IRQs.
150 * Not implemented yet due to weird lockups
156 static struct gpio_chip zevio_gpio_chip
= {
157 .direction_input
= zevio_gpio_direction_input
,
158 .direction_output
= zevio_gpio_direction_output
,
159 .set
= zevio_gpio_set
,
160 .get
= zevio_gpio_get
,
161 .to_irq
= zevio_gpio_to_irq
,
163 .owner
= THIS_MODULE
,
165 .of_gpio_n_cells
= 2,
169 static int zevio_gpio_probe(struct platform_device
*pdev
)
171 struct zevio_gpio
*controller
;
174 controller
= devm_kzalloc(&pdev
->dev
, sizeof(*controller
), GFP_KERNEL
);
176 dev_err(&pdev
->dev
, "not enough free memory\n");
180 /* Copy our reference */
181 controller
->chip
.gc
= zevio_gpio_chip
;
182 controller
->chip
.gc
.dev
= &pdev
->dev
;
184 status
= of_mm_gpiochip_add(pdev
->dev
.of_node
, &(controller
->chip
));
186 dev_err(&pdev
->dev
, "failed to add gpiochip: %d\n", status
);
190 spin_lock_init(&controller
->lock
);
192 /* Disable interrupts, they only cause errors */
193 for (i
= 0; i
< controller
->chip
.gc
.ngpio
; i
+= 8)
194 zevio_gpio_port_set(controller
, i
, ZEVIO_GPIO_INT_MASK
, 0xFF);
196 dev_dbg(controller
->chip
.gc
.dev
, "ZEVIO GPIO controller set up!\n");
201 static struct of_device_id zevio_gpio_of_match
[] = {
202 { .compatible
= "lsi,zevio-gpio", },
206 MODULE_DEVICE_TABLE(of
, zevio_gpio_of_match
);
208 static struct platform_driver zevio_gpio_driver
= {
210 .name
= "gpio-zevio",
211 .owner
= THIS_MODULE
,
212 .of_match_table
= of_match_ptr(zevio_gpio_of_match
),
214 .probe
= zevio_gpio_probe
,
216 module_platform_driver(zevio_gpio_driver
);
218 MODULE_LICENSE("GPL");
219 MODULE_AUTHOR("Fabian Vogt <fabian@ritter-vogt.de>");
220 MODULE_DESCRIPTION("LSI ZEVIO SoC GPIO driver");