blob: 031f60f5307c937d02918e12c90c1f36c9e49e4b [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001// SPDX-License-Identifier: GPL-2.0-only
2
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>
8
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))
14
15#define USEC_TIMEOUT 5000
16
17struct rtl8231_gpios {
18 struct gpio_chip gc;
19 struct device *dev;
20 u32 id;
21 int smi_bus_id;
22 u16 reg_shadow[0x20];
23 u32 reg_cached;
24 int ext_gpio_indrt_access;
25};
26
27extern struct mutex smi_lock;
28extern struct rtl83xx_soc_info soc_info;
29
30static u32 rtl8231_read(struct rtl8231_gpios *gpios, u32 reg)
31{
32 u32 t = 0, n = 0;
33 u8 bus_id = gpios->smi_bus_id;
34
35 reg &= 0x1f;
36 bus_id &= 0x1f;
37
38 /* Calculate read register address */
39 t = (bus_id << 2) | (reg << 7);
40
41 /* Set execution bit: cleared when operation completed */
42 t |= 1;
43
44 // Start execution
45 sw_w32(t, gpios->ext_gpio_indrt_access);
46 do {
47 udelay(1);
48 t = sw_r32(gpios->ext_gpio_indrt_access);
49 n++;
50 } while ((t & 1) && (n < USEC_TIMEOUT));
51
52 if (n >= USEC_TIMEOUT)
53 return 0x80000000;
54
55 pr_debug("%s: %x, %x, %x\n", __func__, bus_id, reg, (t & 0xffff0000) >> 16);
56
57 return (t & 0xffff0000) >> 16;
58}
59
60static int rtl8231_write(struct rtl8231_gpios *gpios, u32 reg, u32 data)
61{
62 u32 t = 0, n = 0;
63 u8 bus_id = gpios->smi_bus_id;
64
65 pr_debug("%s: %x, %x, %x\n", __func__, bus_id, reg, data);
66 reg &= 0x1f;
67 bus_id &= 0x1f;
68
69 t = (bus_id << 2) | (reg << 7) | (data << 16);
70 /* Set write bit */
71 t |= 2;
72
73 /* Set execution bit: cleared when operation completed */
74 t |= 1;
75
76 // Start execution
77 sw_w32(t, gpios->ext_gpio_indrt_access);
78 do {
79 udelay(1);
80 t = sw_r32(gpios->ext_gpio_indrt_access);
81 } while ((t & 1) && (n < USEC_TIMEOUT));
82
83 if (n >= USEC_TIMEOUT)
84 return -1;
85
86 return 0;
87}
88
89static u32 rtl8231_read_cached(struct rtl8231_gpios *gpios, u32 reg)
90{
91 if (reg > 0x1f)
92 return 0;
93
94 if (gpios->reg_cached & (1 << reg))
95 return gpios->reg_shadow[reg];
96
97 return rtl8231_read(gpios, reg);
98}
99
100/* Set Direction of the RTL8231 pin:
101 * dir 1: input
102 * dir 0: output
103 */
104static int rtl8231_pin_dir(struct rtl8231_gpios *gpios, u32 gpio, u32 dir)
105{
106 u32 v;
107 int pin_sel_addr = RTL8231_GPIO_PIN_SEL(gpio);
108 int pin_dir_addr = RTL8231_GPIO_DIR(gpio);
109 int pin = gpio % 16;
110 int dpin = pin;
111
112 if (gpio > 31) {
113 pr_debug("WARNING: HIGH pin\n");
114 dpin = pin << 5;
115 pin_dir_addr = pin_sel_addr;
116 }
117
118 v = rtl8231_read_cached(gpios, pin_dir_addr);
119 if (v & 0x80000000) {
120 pr_err("Error reading RTL8231\n");
121 return -1;
122 }
123
124 v = (v & ~(1 << dpin)) | (dir << dpin);
125 rtl8231_write(gpios, pin_dir_addr, v);
126 gpios->reg_shadow[pin_dir_addr] = v;
127 gpios->reg_cached |= 1 << pin_dir_addr;
128 return 0;
129}
130
131static int rtl8231_pin_dir_get(struct rtl8231_gpios *gpios, u32 gpio, u32 *dir)
132{
133 /* dir 1: input
134 * dir 0: output
135 */
136
137 u32 v;
138 int pin_dir_addr = RTL8231_GPIO_DIR(gpio);
139 int pin = gpio % 16;
140
141 if (gpio > 31) {
142 pin_dir_addr = RTL8231_GPIO_PIN_SEL(gpio);
143 pin = pin << 5;
144 }
145
146 v = rtl8231_read(gpios, pin_dir_addr);
147 if (v & (1 << pin))
148 *dir = 1;
149 else
150 *dir = 0;
151 return 0;
152}
153
154static int rtl8231_pin_set(struct rtl8231_gpios *gpios, u32 gpio, u32 data)
155{
156 u32 v = rtl8231_read(gpios, RTL8231_GPIO_DATA(gpio));
157
158 pr_debug("%s: %d to %d\n", __func__, gpio, data);
159 if (v & 0x80000000) {
160 pr_err("Error reading RTL8231\n");
161 return -1;
162 }
163 v = (v & ~(1 << (gpio % 16))) | (data << (gpio % 16));
164 rtl8231_write(gpios, RTL8231_GPIO_DATA(gpio), v);
165 gpios->reg_shadow[RTL8231_GPIO_DATA(gpio)] = v;
166 gpios->reg_cached |= 1 << RTL8231_GPIO_DATA(gpio);
167 return 0;
168}
169
170static int rtl8231_pin_get(struct rtl8231_gpios *gpios, u32 gpio, u16 *state)
171{
172 u32 v = rtl8231_read(gpios, RTL8231_GPIO_DATA(gpio));
173
174 if (v & 0x80000000) {
175 pr_err("Error reading RTL8231\n");
176 return -1;
177 }
178
179 *state = v & 0xffff;
180 return 0;
181}
182
183static int rtl8231_direction_input(struct gpio_chip *gc, unsigned int offset)
184{
185 int err;
186 struct rtl8231_gpios *gpios = gpiochip_get_data(gc);
187
188 pr_debug("%s: %d\n", __func__, offset);
189 mutex_lock(&smi_lock);
190 err = rtl8231_pin_dir(gpios, offset, 1);
191 mutex_unlock(&smi_lock);
192 return err;
193}
194
195static int rtl8231_direction_output(struct gpio_chip *gc, unsigned int offset, int value)
196{
197 int err;
198 struct rtl8231_gpios *gpios = gpiochip_get_data(gc);
199
200 pr_debug("%s: %d\n", __func__, offset);
201 mutex_lock(&smi_lock);
202 err = rtl8231_pin_dir(gpios, offset, 0);
203 mutex_unlock(&smi_lock);
204 if (!err)
205 err = rtl8231_pin_set(gpios, offset, value);
206 return err;
207}
208
209static int rtl8231_get_direction(struct gpio_chip *gc, unsigned int offset)
210{
211 u32 v = 0;
212 struct rtl8231_gpios *gpios = gpiochip_get_data(gc);
213
214 pr_debug("%s: %d\n", __func__, offset);
215 mutex_lock(&smi_lock);
216 rtl8231_pin_dir_get(gpios, offset, &v);
217 mutex_unlock(&smi_lock);
218 return v;
219}
220
221static int rtl8231_gpio_get(struct gpio_chip *gc, unsigned int offset)
222{
223 u16 state = 0;
224 struct rtl8231_gpios *gpios = gpiochip_get_data(gc);
225
226 mutex_lock(&smi_lock);
227 rtl8231_pin_get(gpios, offset, &state);
228 mutex_unlock(&smi_lock);
229 if (state & (1 << (offset % 16)))
230 return 1;
231 return 0;
232}
233
234void rtl8231_gpio_set(struct gpio_chip *gc, unsigned int offset, int value)
235{
236 struct rtl8231_gpios *gpios = gpiochip_get_data(gc);
237
238 rtl8231_pin_set(gpios, offset, value);
239}
240
241int rtl8231_init(struct rtl8231_gpios *gpios)
242{
243 pr_info("%s called, MDIO bus ID: %d\n", __func__, gpios->smi_bus_id);
244
245 gpios->reg_cached = 0;
246
247 if (soc_info.family == RTL8390_FAMILY_ID) {
248 // RTL8390: Enable external gpio in global led control register
249 sw_w32_mask(0x7 << 18, 0x4 << 18, RTL839X_LED_GLB_CTRL);
250 } else if (soc_info.family == RTL8380_FAMILY_ID) {
251 // RTL8380: Enable RTL8231 indirect access mode
252 sw_w32_mask(0, 1, RTL838X_EXTRA_GPIO_CTRL);
253 sw_w32_mask(3, 1, RTL838X_DMY_REG5);
254 }
255
256 /*Select GPIO functionality for pins 0-15, 16-31 and 32-37 */
257 rtl8231_write(gpios, RTL8231_GPIO_PIN_SEL(0), 0xffff);
258 rtl8231_write(gpios, RTL8231_GPIO_PIN_SEL(16), 0xffff);
259
260 return 0;
261}
262
263static const struct of_device_id rtl8231_gpio_of_match[] = {
264 { .compatible = "realtek,rtl8231-gpio" },
265 {},
266};
267
268MODULE_DEVICE_TABLE(of, rtl8231_gpio_of_match);
269
270static int rtl8231_gpio_probe(struct platform_device *pdev)
271{
272 struct device *dev = &pdev->dev;
273 struct device_node *np = dev->of_node;
274 struct rtl8231_gpios *gpios;
275 int err;
276 u32 indirect_bus_id;
277
278 pr_info("Probing RTL8231 GPIOs\n");
279
280 if (!np) {
281 dev_err(&pdev->dev, "No DT found\n");
282 return -EINVAL;
283 }
284
285 gpios = devm_kzalloc(dev, sizeof(*gpios), GFP_KERNEL);
286 if (!gpios)
287 return -ENOMEM;
288
289 gpios->id = soc_info.id;
290 if (soc_info.family == RTL8380_FAMILY_ID) {
291 gpios->ext_gpio_indrt_access = RTL838X_EXT_GPIO_INDRT_ACCESS;
292 }
293
294 if (soc_info.family == RTL8390_FAMILY_ID) {
295 gpios->ext_gpio_indrt_access = RTL839X_EXT_GPIO_INDRT_ACCESS;
296 }
297
298 /*
299 * We use a default MDIO bus ID for the 8231 of 0, which can be overriden
300 * by the indirect-access-bus-id property in the dts.
301 */
302 gpios->smi_bus_id = 0;
303 of_property_read_u32(np, "indirect-access-bus-id", &indirect_bus_id);
304 gpios->smi_bus_id = indirect_bus_id;
305
306 rtl8231_init(gpios);
307
308 gpios->dev = dev;
309 gpios->gc.base = 160;
310 gpios->gc.ngpio = 36;
311 gpios->gc.label = "rtl8231";
312 gpios->gc.parent = dev;
313 gpios->gc.owner = THIS_MODULE;
314 gpios->gc.can_sleep = true;
315
316 gpios->gc.direction_input = rtl8231_direction_input;
317 gpios->gc.direction_output = rtl8231_direction_output;
318 gpios->gc.set = rtl8231_gpio_set;
319 gpios->gc.get = rtl8231_gpio_get;
320 gpios->gc.get_direction = rtl8231_get_direction;
321
322 err = devm_gpiochip_add_data(dev, &gpios->gc, gpios);
323 return err;
324}
325
326static struct platform_driver rtl8231_gpio_driver = {
327 .driver = {
328 .name = "rtl8231-gpio",
329 .of_match_table = rtl8231_gpio_of_match,
330 },
331 .probe = rtl8231_gpio_probe,
332};
333
334module_platform_driver(rtl8231_gpio_driver);
335
336MODULE_DESCRIPTION("Realtek RTL8231 GPIO expansion chip support");
337MODULE_LICENSE("GPL v2");