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