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 #define USEC_TIMEOUT 5000
17 struct rtl8231_gpios
{
24 int ext_gpio_indrt_access
;
27 extern struct mutex smi_lock
;
28 extern struct rtl83xx_soc_info soc_info
;
30 static u32
rtl8231_read(struct rtl8231_gpios
*gpios
, u32 reg
)
33 u8 bus_id
= gpios
->smi_bus_id
;
38 /* Calculate read register address */
39 t
= (bus_id
<< 2) | (reg
<< 7);
41 /* Set execution bit: cleared when operation completed */
45 sw_w32(t
, gpios
->ext_gpio_indrt_access
);
48 t
= sw_r32(gpios
->ext_gpio_indrt_access
);
50 } while ((t
& 1) && (n
< USEC_TIMEOUT
));
52 if (n
>= USEC_TIMEOUT
)
55 pr_debug("%s: %x, %x, %x\n", __func__
, bus_id
, reg
, (t
& 0xffff0000) >> 16);
57 return (t
& 0xffff0000) >> 16;
60 static int rtl8231_write(struct rtl8231_gpios
*gpios
, u32 reg
, u32 data
)
63 u8 bus_id
= gpios
->smi_bus_id
;
65 pr_debug("%s: %x, %x, %x\n", __func__
, bus_id
, reg
, data
);
69 t
= (bus_id
<< 2) | (reg
<< 7) | (data
<< 16);
73 /* Set execution bit: cleared when operation completed */
77 sw_w32(t
, gpios
->ext_gpio_indrt_access
);
80 t
= sw_r32(gpios
->ext_gpio_indrt_access
);
81 } while ((t
& 1) && (n
< USEC_TIMEOUT
));
83 if (n
>= USEC_TIMEOUT
)
89 static u32
rtl8231_read_cached(struct rtl8231_gpios
*gpios
, u32 reg
)
94 if (gpios
->reg_cached
& (1 << reg
))
95 return gpios
->reg_shadow
[reg
];
97 return rtl8231_read(gpios
, reg
);
100 /* Set Direction of the RTL8231 pin:
104 static int rtl8231_pin_dir(struct rtl8231_gpios
*gpios
, u32 gpio
, u32 dir
)
107 int pin_sel_addr
= RTL8231_GPIO_PIN_SEL(gpio
);
108 int pin_dir_addr
= RTL8231_GPIO_DIR(gpio
);
109 int dpin
= gpio
% 16;
112 pr_debug("WARNING: HIGH pin\n");
114 pin_dir_addr
= pin_sel_addr
;
117 v
= rtl8231_read_cached(gpios
, pin_dir_addr
);
118 if (v
& 0x80000000) {
119 pr_err("Error reading RTL8231\n");
123 v
= (v
& ~(1 << dpin
)) | (dir
<< dpin
);
124 rtl8231_write(gpios
, pin_dir_addr
, v
);
125 gpios
->reg_shadow
[pin_dir_addr
] = v
;
126 gpios
->reg_cached
|= 1 << pin_dir_addr
;
130 static int rtl8231_pin_dir_get(struct rtl8231_gpios
*gpios
, u32 gpio
, u32
*dir
)
137 int pin_dir_addr
= RTL8231_GPIO_DIR(gpio
);
141 pin_dir_addr
= RTL8231_GPIO_PIN_SEL(gpio
);
145 v
= rtl8231_read(gpios
, pin_dir_addr
);
153 static int rtl8231_pin_set(struct rtl8231_gpios
*gpios
, u32 gpio
, u32 data
)
155 u32 v
= rtl8231_read(gpios
, RTL8231_GPIO_DATA(gpio
));
157 pr_debug("%s: %d to %d\n", __func__
, gpio
, data
);
158 if (v
& 0x80000000) {
159 pr_err("Error reading RTL8231\n");
162 v
= (v
& ~(1 << (gpio
% 16))) | (data
<< (gpio
% 16));
163 rtl8231_write(gpios
, RTL8231_GPIO_DATA(gpio
), v
);
164 gpios
->reg_shadow
[RTL8231_GPIO_DATA(gpio
)] = v
;
165 gpios
->reg_cached
|= 1 << RTL8231_GPIO_DATA(gpio
);
169 static int rtl8231_pin_get(struct rtl8231_gpios
*gpios
, u32 gpio
, u16
*state
)
171 u32 v
= rtl8231_read(gpios
, RTL8231_GPIO_DATA(gpio
));
173 if (v
& 0x80000000) {
174 pr_err("Error reading RTL8231\n");
182 static int rtl8231_direction_input(struct gpio_chip
*gc
, unsigned int offset
)
185 struct rtl8231_gpios
*gpios
= gpiochip_get_data(gc
);
187 pr_debug("%s: %d\n", __func__
, offset
);
188 mutex_lock(&smi_lock
);
189 err
= rtl8231_pin_dir(gpios
, offset
, 1);
190 mutex_unlock(&smi_lock
);
194 static int rtl8231_direction_output(struct gpio_chip
*gc
, unsigned int offset
, int value
)
197 struct rtl8231_gpios
*gpios
= gpiochip_get_data(gc
);
199 pr_debug("%s: %d\n", __func__
, offset
);
200 mutex_lock(&smi_lock
);
201 err
= rtl8231_pin_dir(gpios
, offset
, 0);
202 mutex_unlock(&smi_lock
);
204 err
= rtl8231_pin_set(gpios
, offset
, value
);
208 static int rtl8231_get_direction(struct gpio_chip
*gc
, unsigned int offset
)
211 struct rtl8231_gpios
*gpios
= gpiochip_get_data(gc
);
213 pr_debug("%s: %d\n", __func__
, offset
);
214 mutex_lock(&smi_lock
);
215 rtl8231_pin_dir_get(gpios
, offset
, &v
);
216 mutex_unlock(&smi_lock
);
220 static int rtl8231_gpio_get(struct gpio_chip
*gc
, unsigned int offset
)
223 struct rtl8231_gpios
*gpios
= gpiochip_get_data(gc
);
225 mutex_lock(&smi_lock
);
226 rtl8231_pin_get(gpios
, offset
, &state
);
227 mutex_unlock(&smi_lock
);
228 if (state
& (1 << (offset
% 16)))
233 void rtl8231_gpio_set(struct gpio_chip
*gc
, unsigned int offset
, int value
)
235 struct rtl8231_gpios
*gpios
= gpiochip_get_data(gc
);
237 rtl8231_pin_set(gpios
, offset
, value
);
240 int rtl8231_init(struct rtl8231_gpios
*gpios
)
244 pr_info("%s called, MDIO bus ID: %d\n", __func__
, gpios
->smi_bus_id
);
246 gpios
->reg_cached
= 0;
248 if (soc_info
.family
== RTL8390_FAMILY_ID
) {
249 // RTL8390: Enable external gpio in global led control register
250 sw_w32_mask(0x7 << 18, 0x4 << 18, RTL839X_LED_GLB_CTRL
);
251 } else if (soc_info
.family
== RTL8380_FAMILY_ID
) {
252 // RTL8380: Enable RTL8231 indirect access mode
253 sw_w32_mask(0, 1, RTL838X_EXTRA_GPIO_CTRL
);
254 sw_w32_mask(3, 1, RTL838X_DMY_REG5
);
257 /* Select GPIO functionality for pins 0-34 */
258 rtl8231_write(gpios
, RTL8231_GPIO_PIN_SEL(0), 0xffff);
259 rtl8231_write(gpios
, RTL8231_GPIO_PIN_SEL(16), 0xffff);
260 v
= rtl8231_read(gpios
, RTL8231_GPIO_PIN_SEL(32));
261 rtl8231_write(gpios
, RTL8231_GPIO_PIN_SEL(32), v
| 0x7);
266 static const struct of_device_id rtl8231_gpio_of_match
[] = {
267 { .compatible
= "realtek,rtl8231-gpio" },
271 MODULE_DEVICE_TABLE(of
, rtl8231_gpio_of_match
);
273 static int rtl8231_gpio_probe(struct platform_device
*pdev
)
275 struct device
*dev
= &pdev
->dev
;
276 struct device_node
*np
= dev
->of_node
;
277 struct rtl8231_gpios
*gpios
;
281 pr_info("Probing RTL8231 GPIOs\n");
284 dev_err(&pdev
->dev
, "No DT found\n");
288 gpios
= devm_kzalloc(dev
, sizeof(*gpios
), GFP_KERNEL
);
292 gpios
->id
= soc_info
.id
;
293 if (soc_info
.family
== RTL8380_FAMILY_ID
) {
294 gpios
->ext_gpio_indrt_access
= RTL838X_EXT_GPIO_INDRT_ACCESS
;
297 if (soc_info
.family
== RTL8390_FAMILY_ID
) {
298 gpios
->ext_gpio_indrt_access
= RTL839X_EXT_GPIO_INDRT_ACCESS
;
302 * We use a default MDIO bus ID for the 8231 of 0, which can be overriden
303 * by the indirect-access-bus-id property in the dts.
305 gpios
->smi_bus_id
= 0;
306 of_property_read_u32(np
, "indirect-access-bus-id", &indirect_bus_id
);
307 gpios
->smi_bus_id
= indirect_bus_id
;
312 gpios
->gc
.base
= 160;
313 gpios
->gc
.ngpio
= 36;
314 gpios
->gc
.label
= "rtl8231";
315 gpios
->gc
.parent
= dev
;
316 gpios
->gc
.owner
= THIS_MODULE
;
317 gpios
->gc
.can_sleep
= true;
319 gpios
->gc
.direction_input
= rtl8231_direction_input
;
320 gpios
->gc
.direction_output
= rtl8231_direction_output
;
321 gpios
->gc
.set
= rtl8231_gpio_set
;
322 gpios
->gc
.get
= rtl8231_gpio_get
;
323 gpios
->gc
.get_direction
= rtl8231_get_direction
;
325 err
= devm_gpiochip_add_data(dev
, &gpios
->gc
, gpios
);
329 static struct platform_driver rtl8231_gpio_driver
= {
331 .name
= "rtl8231-gpio",
332 .of_match_table
= rtl8231_gpio_of_match
,
334 .probe
= rtl8231_gpio_probe
,
337 module_platform_driver(rtl8231_gpio_driver
);
339 MODULE_DESCRIPTION("Realtek RTL8231 GPIO expansion chip support");
340 MODULE_LICENSE("GPL v2");