1 // SPDX-License-Identifier: GPL-2.0-only
3 #include <linux/gpio/driver.h>
4 #include <linux/module.h>
5 #include <linux/platform_device.h>
6 #include <linux/delay.h>
7 #include <asm/mach-rtl838x/mach-rtl83xx.h>
9 /* RTL8231 registers for LED control */
10 #define RTL8231_LED_FUNC0 0x0000
11 #define RTL8231_GPIO_PIN_SEL(gpio) ((0x0002) + ((gpio) >> 4))
12 #define RTL8231_GPIO_DIR(gpio) ((0x0005) + ((gpio) >> 4))
13 #define RTL8231_GPIO_DATA(gpio) ((0x001C) + ((gpio) >> 4))
15 struct rtl8231_gpios
{
22 int ext_gpio_indrt_access
;
25 extern struct mutex smi_lock
;
26 extern struct rtl83xx_soc_info soc_info
;
28 static u32
rtl8231_read(struct rtl8231_gpios
*gpios
, u32 reg
)
31 u8 bus_id
= gpios
->smi_bus_id
;
36 /* Calculate read register address */
37 t
= (bus_id
<< 2) | (reg
<< 7);
39 /* Set execution bit: cleared when operation completed */
41 sw_w32(t
, gpios
->ext_gpio_indrt_access
);
42 do { /* TODO: Return 0x80000000 if timeout */
43 t
= sw_r32(gpios
->ext_gpio_indrt_access
);
45 pr_debug("%s: %x, %x, %x\n", __func__
, bus_id
, reg
, (t
& 0xffff0000) >> 16);
47 return (t
& 0xffff0000) >> 16;
50 static int rtl8231_write(struct rtl8231_gpios
*gpios
, u32 reg
, u32 data
)
53 u8 bus_id
= gpios
->smi_bus_id
;
55 pr_debug("%s: %x, %x, %x\n", __func__
, bus_id
, reg
, data
);
59 t
= (bus_id
<< 2) | (reg
<< 7) | (data
<< 16);
63 /* Set execution bit: cleared when operation completed */
65 sw_w32(t
, gpios
->ext_gpio_indrt_access
);
66 do { /* TODO: Return -1 if timeout */
67 t
= sw_r32(gpios
->ext_gpio_indrt_access
);
73 static u32
rtl8231_read_cached(struct rtl8231_gpios
*gpios
, u32 reg
)
78 if (gpios
->reg_cached
& (1 << reg
))
79 return gpios
->reg_shadow
[reg
];
81 return rtl8231_read(gpios
, reg
);
84 /* Set Direction of the RTL8231 pin:
88 static int rtl8231_pin_dir(struct rtl8231_gpios
*gpios
, u32 gpio
, u32 dir
)
91 int pin_sel_addr
= RTL8231_GPIO_PIN_SEL(gpio
);
92 int pin_dir_addr
= RTL8231_GPIO_DIR(gpio
);
97 pr_info("WARNING: HIGH pin\n");
99 pin_dir_addr
= pin_sel_addr
;
102 v
= rtl8231_read_cached(gpios
, pin_dir_addr
);
103 if (v
& 0x80000000) {
104 pr_err("Error reading RTL8231\n");
108 v
= (v
& ~(1 << dpin
)) | (dir
<< dpin
);
109 rtl8231_write(gpios
, pin_dir_addr
, v
);
110 gpios
->reg_shadow
[pin_dir_addr
] = v
;
111 gpios
->reg_cached
|= 1 << pin_dir_addr
;
115 static int rtl8231_pin_dir_get(struct rtl8231_gpios
*gpios
, u32 gpio
, u32
*dir
)
122 int pin_dir_addr
= RTL8231_GPIO_DIR(gpio
);
126 pin_dir_addr
= RTL8231_GPIO_PIN_SEL(gpio
);
130 v
= rtl8231_read(gpios
, pin_dir_addr
);
138 static int rtl8231_pin_set(struct rtl8231_gpios
*gpios
, u32 gpio
, u32 data
)
140 u32 v
= rtl8231_read(gpios
, RTL8231_GPIO_DATA(gpio
));
142 pr_debug("%s: %d to %d\n", __func__
, gpio
, data
);
143 if (v
& 0x80000000) {
144 pr_err("Error reading RTL8231\n");
147 v
= (v
& ~(1 << (gpio
% 16))) | (data
<< (gpio
% 16));
148 rtl8231_write(gpios
, RTL8231_GPIO_DATA(gpio
), v
);
149 gpios
->reg_shadow
[RTL8231_GPIO_DATA(gpio
)] = v
;
150 gpios
->reg_cached
|= 1 << RTL8231_GPIO_DATA(gpio
);
154 static int rtl8231_pin_get(struct rtl8231_gpios
*gpios
, u32 gpio
, u16
*state
)
156 u32 v
= rtl8231_read(gpios
, RTL8231_GPIO_DATA(gpio
));
158 if (v
& 0x80000000) {
159 pr_err("Error reading RTL8231\n");
167 static int rtl8231_direction_input(struct gpio_chip
*gc
, unsigned int offset
)
170 struct rtl8231_gpios
*gpios
= gpiochip_get_data(gc
);
172 pr_debug("%s: %d\n", __func__
, offset
);
173 mutex_lock(&smi_lock
);
174 err
= rtl8231_pin_dir(gpios
, offset
, 1);
175 mutex_unlock(&smi_lock
);
179 static int rtl8231_direction_output(struct gpio_chip
*gc
, unsigned int offset
, int value
)
182 struct rtl8231_gpios
*gpios
= gpiochip_get_data(gc
);
184 pr_debug("%s: %d\n", __func__
, offset
);
185 mutex_lock(&smi_lock
);
186 err
= rtl8231_pin_dir(gpios
, offset
, 0);
187 mutex_unlock(&smi_lock
);
189 err
= rtl8231_pin_set(gpios
, offset
, value
);
193 static int rtl8231_get_direction(struct gpio_chip
*gc
, unsigned int offset
)
196 struct rtl8231_gpios
*gpios
= gpiochip_get_data(gc
);
198 pr_debug("%s: %d\n", __func__
, offset
);
199 mutex_lock(&smi_lock
);
200 rtl8231_pin_dir_get(gpios
, offset
, &v
);
201 mutex_unlock(&smi_lock
);
205 static int rtl8231_gpio_get(struct gpio_chip
*gc
, unsigned int offset
)
208 struct rtl8231_gpios
*gpios
= gpiochip_get_data(gc
);
210 mutex_lock(&smi_lock
);
211 rtl8231_pin_get(gpios
, offset
, &state
);
212 mutex_unlock(&smi_lock
);
213 if (state
& (1 << (offset
% 16)))
218 void rtl8231_gpio_set(struct gpio_chip
*gc
, unsigned int offset
, int value
)
220 struct rtl8231_gpios
*gpios
= gpiochip_get_data(gc
);
222 rtl8231_pin_set(gpios
, offset
, value
);
225 int rtl8231_init(struct rtl8231_gpios
*gpios
)
227 pr_info("%s called, MDIO bus ID: %d\n", __func__
, gpios
->smi_bus_id
);
229 if (soc_info
.family
== RTL8390_FAMILY_ID
) {
230 sw_w32_mask(0x7 << 18, 0x4 << 18, RTL839X_LED_GLB_CTRL
);
234 /* Enable RTL8231 indirect access mode */
235 sw_w32_mask(0, 1, RTL838X_EXTRA_GPIO_CTRL
);
236 sw_w32_mask(3, 1, RTL838X_DMY_REG5
);
238 /* Enable RTL8231 via GPIO_A1 line
239 rtl838x_w32_mask(0, 1 << RTL838X_GPIO_A1, RTL838X_GPIO_PABC_DIR);
240 rtl838x_w32_mask(0, 1 << RTL838X_GPIO_A1, RTL838X_GPIO_PABC_DATA); */
241 mdelay(50); /* wait 50ms for reset */
243 /*Select GPIO functionality for pins 0-15, 16-31 and 32-37 */
244 rtl8231_write(gpios
, RTL8231_GPIO_PIN_SEL(0), 0xffff);
245 rtl8231_write(gpios
, RTL8231_GPIO_PIN_SEL(16), 0xffff);
247 gpios
->reg_cached
= 0;
252 static const struct of_device_id rtl8231_gpio_of_match
[] = {
253 { .compatible
= "realtek,rtl8231-gpio" },
257 MODULE_DEVICE_TABLE(of
, rtl8231_gpio_of_match
);
259 static int rtl8231_gpio_probe(struct platform_device
*pdev
)
261 struct device
*dev
= &pdev
->dev
;
262 struct device_node
*np
= dev
->of_node
;
263 struct rtl8231_gpios
*gpios
;
267 pr_info("Probing RTL8231 GPIOs\n");
270 dev_err(&pdev
->dev
, "No DT found\n");
274 gpios
= devm_kzalloc(dev
, sizeof(*gpios
), GFP_KERNEL
);
278 gpios
->id
= soc_info
.id
;
279 if (soc_info
.family
== RTL8380_FAMILY_ID
) {
280 gpios
->ext_gpio_indrt_access
= RTL838X_EXT_GPIO_INDRT_ACCESS
;
283 if (soc_info
.family
== RTL8390_FAMILY_ID
) {
284 gpios
->ext_gpio_indrt_access
= RTL839X_EXT_GPIO_INDRT_ACCESS
;
287 if (!of_property_read_u8(np
, "indirect-access-bus-id", &indirect_bus_id
)) {
288 gpios
->smi_bus_id
= indirect_bus_id
;
293 gpios
->gc
.base
= 160;
294 gpios
->gc
.ngpio
= 36;
295 gpios
->gc
.label
= "rtl8231";
296 gpios
->gc
.parent
= dev
;
297 gpios
->gc
.owner
= THIS_MODULE
;
298 gpios
->gc
.can_sleep
= true;
300 gpios
->gc
.direction_input
= rtl8231_direction_input
;
301 gpios
->gc
.direction_output
= rtl8231_direction_output
;
302 gpios
->gc
.set
= rtl8231_gpio_set
;
303 gpios
->gc
.get
= rtl8231_gpio_get
;
304 gpios
->gc
.get_direction
= rtl8231_get_direction
;
306 err
= devm_gpiochip_add_data(dev
, &gpios
->gc
, gpios
);
310 static struct platform_driver rtl8231_gpio_driver
= {
312 .name
= "rtl8231-gpio",
313 .of_match_table
= rtl8231_gpio_of_match
,
315 .probe
= rtl8231_gpio_probe
,
318 module_platform_driver(rtl8231_gpio_driver
);
320 MODULE_DESCRIPTION("Realtek RTL8231 GPIO expansion chip support");
321 MODULE_LICENSE("GPL v2");