kernel: bump 5.4 to 5.4.80
[openwrt/staging/rmilecki.git] / target / linux / rtl838x / files-5.4 / drivers / gpio / gpio-rtl838x.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-rtl838x.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 #define RTL8231_GPIO_PIN_SEL0 0x0002
15 #define RTL8231_GPIO_PIN_SEL1 0x0003
16 #define RTL8231_GPIO_PIN_SEL2 0x0004
17 #define RTL8231_GPIO_IO_SEL0 0x0005
18 #define RTL8231_GPIO_IO_SEL1 0x0006
19 #define RTL8231_GPIO_IO_SEL2 0x0007
20
21 #define MDC_WAIT { int i; for (i = 0; i < 2; i++); }
22 #define I2C_WAIT { int i; for (i = 0; i < 5; i++); }
23
24 struct rtl838x_gpios {
25 struct gpio_chip gc;
26 u32 id;
27 struct device *dev;
28 int irq;
29 int bus_id;
30 int num_leds;
31 int min_led;
32 int leds_per_port;
33 u32 led_mode;
34 u16 rtl8381_phy_id;
35 int smi_clock;
36 int smi_data;
37 int i2c_sda;
38 int i2c_sdc;
39 };
40
41 u32 rtl838x_rtl8231_read(u8 bus_id, u32 reg)
42 {
43 u32 t = 0;
44
45 reg &= 0x1f;
46 bus_id &= 0x1f;
47
48 /* Calculate read register address */
49 t = (bus_id << 2) | (reg << 7);
50
51 mutex_lock(&smi_lock);
52 /* Set execution bit: cleared when operation completed */
53 t |= 1;
54 sw_w32(t, RTL838X_EXT_GPIO_INDRT_ACCESS);
55 do { /* TODO: Return 0x80000000 if timeout */
56 t = sw_r32(RTL838X_EXT_GPIO_INDRT_ACCESS);
57 } while (t & 1);
58 pr_debug("%s: %x, %x, %x\n", __func__, bus_id, reg, (t & 0xffff0000) >> 16);
59
60 mutex_unlock(&smi_lock);
61 return (t & 0xffff0000) >> 16;
62 }
63
64 int rtl838x_rtl8231_write(u8 bus_id, u32 reg, u32 data)
65 {
66 u32 t = 0;
67
68 pr_debug("%s: %x, %x, %x\n", __func__, bus_id, reg, data);
69 data &= 0xffff;
70 reg &= 0x1f;
71 bus_id &= 0x1f;
72
73 mutex_lock(&smi_lock);
74 t = (bus_id << 2) | (reg << 7) | (data << 16);
75 /* Set write bit */
76 t |= 2;
77
78 /* Set execution bit: cleared when operation completed */
79 t |= 1;
80 sw_w32(t, RTL838X_EXT_GPIO_INDRT_ACCESS);
81 do { /* TODO: Return -1 if timeout */
82 t = sw_r32(RTL838X_EXT_GPIO_INDRT_ACCESS);
83 } while (t & 1);
84
85 mutex_unlock(&smi_lock);
86 return 0;
87 }
88
89 static int rtl8231_pin_dir(u8 bus_id, u32 gpio, u32 dir)
90 {
91 /* dir 1: input
92 * dir 0: output
93 */
94
95 u32 v;
96 int pin_sel_addr = RTL8231_GPIO_PIN_SEL(gpio);
97 int pin_dir_addr = RTL8231_GPIO_DIR(gpio);
98 int pin = gpio % 16;
99 int dpin = pin;
100
101 if (gpio > 31) {
102 dpin = pin << 5;
103 pin_dir_addr = pin_sel_addr;
104 }
105
106 /* Select GPIO function for pin */
107 v = rtl838x_rtl8231_read(bus_id, pin_sel_addr);
108 if (v & 0x80000000) {
109 pr_err("Error reading RTL8231\n");
110 return -1;
111 }
112 rtl838x_rtl8231_write(bus_id, pin_sel_addr, v | (1 << pin));
113
114 v = rtl838x_rtl8231_read(bus_id, pin_dir_addr);
115 if (v & 0x80000000) {
116 pr_err("Error reading RTL8231\n");
117 return -1;
118 }
119 rtl838x_rtl8231_write(bus_id, pin_dir_addr,
120 (v & ~(1 << dpin)) | (dir << dpin));
121 return 0;
122 }
123
124 static int rtl8231_pin_dir_get(u8 bus_id, u32 gpio, u32 *dir)
125 {
126 /* dir 1: input
127 * dir 0: output
128 */
129
130 u32 v;
131 int pin_dir_addr = RTL8231_GPIO_DIR(gpio);
132 int pin = gpio % 16;
133
134 if (gpio > 31) {
135 pin_dir_addr = RTL8231_GPIO_PIN_SEL(gpio);
136 pin = pin << 5;
137 }
138
139 v = rtl838x_rtl8231_read(bus_id, pin_dir_addr);
140 if (v & (1 << pin))
141 *dir = 1;
142 else
143 *dir = 0;
144 return 0;
145 }
146
147 static int rtl8231_pin_set(u8 bus_id, u32 gpio, u32 data)
148 {
149 u32 v = rtl838x_rtl8231_read(bus_id, RTL8231_GPIO_DATA(gpio));
150
151 if (v & 0x80000000) {
152 pr_err("Error reading RTL8231\n");
153 return -1;
154 }
155 rtl838x_rtl8231_write(bus_id, RTL8231_GPIO_DATA(gpio),
156 (v & ~(1 << (gpio % 16))) | (data << (gpio % 16)));
157 return 0;
158 }
159
160 static int rtl8231_pin_get(u8 bus_id, u32 gpio, u16 *state)
161 {
162 u32 v = rtl838x_rtl8231_read(bus_id, RTL8231_GPIO_DATA(gpio));
163
164 if (v & 0x80000000) {
165 pr_err("Error reading RTL8231\n");
166 return -1;
167 }
168
169 *state = v & 0xffff;
170 return 0;
171 }
172
173 static int rtl838x_direction_input(struct gpio_chip *gc, unsigned int offset)
174 {
175 struct rtl838x_gpios *gpios = gpiochip_get_data(gc);
176
177 pr_debug("%s: %d\n", __func__, offset);
178
179 if (offset < 32) {
180 rtl838x_w32_mask(1 << offset, 0, RTL838X_GPIO_PABC_DIR);
181 return 0;
182 }
183
184 /* Internal LED driver does not support input */
185 if (offset >= 32 && offset < 64)
186 return -ENOTSUPP;
187
188 if (offset >= 64 && offset < 100 && gpios->bus_id >= 0)
189 return rtl8231_pin_dir(gpios->bus_id, offset - 64, 1);
190
191 return -ENOTSUPP;
192 }
193
194 static int rtl838x_direction_output(struct gpio_chip *gc, unsigned int offset, int value)
195 {
196 struct rtl838x_gpios *gpios = gpiochip_get_data(gc);
197
198 pr_debug("%s: %d\n", __func__, offset);
199 if (offset < 32)
200 rtl838x_w32_mask(0, 1 << offset, RTL838X_GPIO_PABC_DIR);
201
202 /* LED for PWR and SYS driver is direction output by default */
203 if (offset >= 32 && offset < 64)
204 return 0;
205
206 if (offset >= 64 && offset < 100 && gpios->bus_id >= 0)
207 return rtl8231_pin_dir(gpios->bus_id, offset - 64, 0);
208 return 0;
209 }
210
211 static int rtl838x_get_direction(struct gpio_chip *gc, unsigned int offset)
212 {
213 u32 v = 0;
214 struct rtl838x_gpios *gpios = gpiochip_get_data(gc);
215
216 pr_debug("%s: %d\n", __func__, offset);
217 if (offset < 32) {
218 v = rtl838x_r32(RTL838X_GPIO_PABC_DIR);
219 if (v & (1 << offset))
220 return 0;
221 return 1;
222 }
223
224 /* LED driver for PWR and SYS is direction output by default */
225 if (offset >= 32 && offset < 64)
226 return 0;
227
228 if (offset >= 64 && offset < 100 && gpios->bus_id >= 0) {
229 rtl8231_pin_dir_get(gpios->bus_id, offset - 64, &v);
230 return v;
231 }
232
233 return 0;
234 }
235
236 static int rtl838x_gpio_get(struct gpio_chip *gc, unsigned int offset)
237 {
238 u32 v;
239 u16 state = 0;
240 int bit;
241 struct rtl838x_gpios *gpios = gpiochip_get_data(gc);
242
243 pr_debug("%s: %d\n", __func__, offset);
244
245 /* Internal GPIO of the RTL8380 */
246 if (offset < 32) {
247 v = rtl838x_r32(RTL838X_GPIO_PABC_DATA);
248 if (v & (1 << offset))
249 return 1;
250 return 0;
251 }
252
253 /* LED driver for PWR and SYS */
254 if (offset >= 32 && offset < 64) {
255 v = sw_r32(RTL838X_LED_GLB_CTRL);
256 if (v & (1 << (offset-32)))
257 return 1;
258 return 0;
259 }
260
261 /* Indirect access GPIO with RTL8231 */
262 if (offset >= 64 && offset < 100 && gpios->bus_id >= 0) {
263 rtl8231_pin_get(gpios->bus_id, offset - 64, &state);
264 if (state & (1 << (offset % 16)))
265 return 1;
266 return 0;
267 }
268
269 bit = (offset - 100) % 32;
270 if (offset >= 100 && offset < 132) {
271 if (sw_r32(RTL838X_LED1_SW_P_EN_CTRL) & (1 << bit))
272 return 1;
273 return 0;
274 }
275 if (offset >= 132 && offset < 164) {
276 if (sw_r32(RTL838X_LED1_SW_P_EN_CTRL) & (1 << bit))
277 return 1;
278 return 0;
279 }
280 if (offset >= 164 && offset < 196) {
281 if (sw_r32(RTL838X_LED1_SW_P_EN_CTRL) & (1 << bit))
282 return 1;
283 return 0;
284 }
285 return 0;
286 }
287
288 void rtl838x_gpio_set(struct gpio_chip *gc, unsigned int offset, int value)
289 {
290 int bit;
291 struct rtl838x_gpios *gpios = gpiochip_get_data(gc);
292
293 pr_debug("rtl838x_set: %d, value: %d\n", offset, value);
294 /* Internal GPIO of the RTL8380 */
295 if (offset < 32) {
296 if (value)
297 rtl838x_w32_mask(0, 1 << offset, RTL838X_GPIO_PABC_DATA);
298 else
299 rtl838x_w32_mask(1 << offset, 0, RTL838X_GPIO_PABC_DATA);
300 }
301
302 /* LED driver for PWR and SYS */
303 if (offset >= 32 && offset < 64) {
304 bit = offset - 32;
305 if (value)
306 sw_w32_mask(0, 1 << bit, RTL838X_LED_GLB_CTRL);
307 else
308 sw_w32_mask(1 << bit, 0, RTL838X_LED_GLB_CTRL);
309 return;
310 }
311
312 /* Indirect access GPIO with RTL8231 */
313 if (offset >= 64 && offset < 100 && gpios->bus_id >= 0) {
314 rtl8231_pin_set(gpios->bus_id, offset - 64, value);
315 return;
316 }
317
318 bit = (offset - 100) % 32;
319 /* First Port-LED */
320 if (offset >= 100 && offset < 132
321 && offset >= (100 + gpios->min_led)
322 && offset < (100 + gpios->min_led + gpios->num_leds)) {
323 if (value)
324 sw_w32_mask(7, 5, RTL838X_LED_SW_P_CTRL(bit));
325 else
326 sw_w32_mask(7, 0, RTL838X_LED_SW_P_CTRL(bit));
327 }
328 if (offset >= 132 && offset < 164
329 && offset >= (132 + gpios->min_led)
330 && offset < (132 + gpios->min_led + gpios->num_leds)) {
331 if (value)
332 sw_w32_mask(7 << 3, 5 << 3, RTL838X_LED_SW_P_CTRL(bit));
333 else
334 sw_w32_mask(7 << 3, 0, RTL838X_LED_SW_P_CTRL(bit));
335 }
336 if (offset >= 164 && offset < 196
337 && offset >= (164 + gpios->min_led)
338 && offset < (164 + gpios->min_led + gpios->num_leds)) {
339 if (value)
340 sw_w32_mask(7 << 6, 5 << 6, RTL838X_LED_SW_P_CTRL(bit));
341 else
342 sw_w32_mask(7 << 6, 0, RTL838X_LED_SW_P_CTRL(bit));
343 }
344 __asm__ volatile ("sync");
345 }
346
347 int rtl8231_init(struct rtl838x_gpios *gpios)
348 {
349 uint32_t v;
350 u8 bus_id = gpios->bus_id;
351
352 pr_info("%s called\n", __func__);
353
354 /* Enable RTL8231 indirect access mode */
355 sw_w32_mask(0, 1, RTL838X_EXTRA_GPIO_CTRL);
356 sw_w32_mask(3, 1, RTL838X_DMY_REG5);
357
358 /* Enable RTL8231 via GPIO_A1 line */
359 rtl838x_w32_mask(0, 1 << RTL838X_GPIO_A1, RTL838X_GPIO_PABC_DIR);
360 rtl838x_w32_mask(0, 1 << RTL838X_GPIO_A1, RTL838X_GPIO_PABC_DATA);
361 mdelay(50); /* wait 50ms for reset */
362
363 /*Select GPIO functionality for pins 0-15, 16-31 and 32-37 */
364 rtl838x_rtl8231_write(bus_id, RTL8231_GPIO_PIN_SEL(0), 0xffff);
365 rtl838x_rtl8231_write(bus_id, RTL8231_GPIO_PIN_SEL(16), 0xffff);
366 rtl838x_rtl8231_write(bus_id, RTL8231_GPIO_PIN_SEL2, 0x03ff);
367
368 v = rtl838x_rtl8231_read(bus_id, RTL8231_LED_FUNC0);
369 pr_info("RTL8231 led function now: %x\n", v);
370
371 return 0;
372 }
373
374 static void smi_write_bit(struct rtl838x_gpios *gpios, u32 bit)
375 {
376 if (bit)
377 rtl838x_w32_mask(0, 1 << gpios->smi_data, RTL838X_GPIO_PABC_DATA);
378 else
379 rtl838x_w32_mask(1 << gpios->smi_data, 0, RTL838X_GPIO_PABC_DATA);
380
381 MDC_WAIT;
382 rtl838x_w32_mask(1 << gpios->smi_clock, 0, RTL838X_GPIO_PABC_DATA);
383 MDC_WAIT;
384 rtl838x_w32_mask(0, 1 << gpios->smi_clock, RTL838X_GPIO_PABC_DATA);
385 }
386
387 static int smi_read_bit(struct rtl838x_gpios *gpios)
388 {
389 u32 v;
390
391 MDC_WAIT;
392 rtl838x_w32_mask(1 << gpios->smi_clock, 0, RTL838X_GPIO_PABC_DATA);
393 MDC_WAIT;
394 rtl838x_w32_mask(0, 1 << gpios->smi_clock, RTL838X_GPIO_PABC_DATA);
395
396 v = rtl838x_r32(RTL838X_GPIO_PABC_DATA);
397 if (v & (1 << gpios->smi_data))
398 return 1;
399 return 0;
400 }
401
402 /* Tri-state of MDIO line */
403 static void smi_z(struct rtl838x_gpios *gpios)
404 {
405 /* MDIO pin to input */
406 rtl838x_w32_mask(1 << gpios->smi_data, 0, RTL838X_GPIO_PABC_DIR);
407 MDC_WAIT;
408 rtl838x_w32_mask(1 << gpios->smi_clock, 0, RTL838X_GPIO_PABC_DATA);
409 MDC_WAIT;
410 rtl838x_w32_mask(0, 1 << gpios->smi_clock, RTL838X_GPIO_PABC_DATA);
411 }
412
413 static void smi_write_bits(struct rtl838x_gpios *gpios, u32 data, int len)
414 {
415 while (len) {
416 len--;
417 smi_write_bit(gpios, data & (1 << len));
418 }
419 }
420
421 static void smi_read_bits(struct rtl838x_gpios *gpios, int len, u32 *data)
422 {
423 u32 v = 0;
424
425 while (len) {
426 len--;
427 v <<= 1;
428 v |= smi_read_bit(gpios);
429 }
430 *data = v;
431 }
432
433 /* Bit-banged verson of SMI write access, caller must hold smi_lock */
434 int rtl8380_smi_write(struct rtl838x_gpios *gpios, u16 reg, u32 data)
435 {
436 u16 bus_id = gpios->bus_id;
437
438 /* Set clock and data pins on RTL838X to output */
439 rtl838x_w32_mask(0, 1 << gpios->smi_clock, RTL838X_GPIO_PABC_DIR);
440 rtl838x_w32_mask(0, 1 << gpios->smi_data, RTL838X_GPIO_PABC_DIR);
441
442 /* Write start bits */
443 smi_write_bits(gpios, 0xffffffff, 32);
444
445 smi_write_bits(gpios, 0x5, 4); /* ST and write OP */
446
447 smi_write_bits(gpios, bus_id, 5); /* 5 bits: phy address */
448 smi_write_bits(gpios, reg, 5); /* 5 bits: register address */
449
450 smi_write_bits(gpios, 0x2, 2); /* TURNAROUND */
451
452 smi_write_bits(gpios, data, 16); /* 16 bits: data*/
453
454 smi_z(gpios);
455
456 return 0;
457 }
458
459 /* Bit-banged verson of SMI read access, caller must hold smi_lock */
460 int rtl8380_smi_read(struct rtl838x_gpios *gpios, u16 reg, u32 *data)
461 {
462 u16 bus_id = gpios->bus_id;
463
464 /* Set clock and data pins on RTL838X to output */
465 rtl838x_w32_mask(0, 1 << gpios->smi_clock, RTL838X_GPIO_PABC_DIR);
466 rtl838x_w32_mask(0, 1 << gpios->smi_data, RTL838X_GPIO_PABC_DIR);
467
468 /* Write start bits */
469 smi_write_bits(gpios, 0xffffffff, 32);
470
471 smi_write_bits(gpios, 0x6, 4); /* ST and read OP */
472
473 smi_write_bits(gpios, bus_id, 5); /* 5 bits: phy address */
474 smi_write_bits(gpios, reg, 5); /* 5 bits: register address */
475
476 smi_z(gpios); /* TURNAROUND */
477
478 smi_read_bits(gpios, 16, data);
479 return 0;
480 }
481
482 static void i2c_pin_set(struct rtl838x_gpios *gpios, int pin, u32 data)
483 {
484 u32 v;
485
486 rtl8380_smi_read(gpios, RTL8231_GPIO_DATA(pin), &v);
487 if (!data)
488 v &= ~(1 << (pin % 16));
489 else
490 v |= (1 << (pin % 16));
491 rtl8380_smi_write(gpios, RTL8231_GPIO_DATA(pin), v);
492 }
493
494 static void i2c_pin_get(struct rtl838x_gpios *gpios, int pin, u32 *data)
495 {
496 u32 v;
497
498 rtl8380_smi_read(gpios, RTL8231_GPIO_DATA(pin), &v);
499 if (v & (1 << (pin % 16))) {
500 *data = 1;
501 return;
502 }
503 *data = 0;
504 }
505
506 static void i2c_pin_dir(struct rtl838x_gpios *gpios, int pin, u16 direction)
507 {
508 u32 v;
509
510 rtl8380_smi_read(gpios, RTL8231_GPIO_DIR(pin), &v);
511 if (direction) // Output
512 v &= ~(1 << (pin % 16));
513 else
514 v |= (1 << (pin % 16));
515 rtl8380_smi_write(gpios, RTL8231_GPIO_DIR(pin), v);
516 }
517
518 static void i2c_start(struct rtl838x_gpios *gpios)
519 {
520 i2c_pin_dir(gpios, gpios->i2c_sda, 0); /* Output */
521 i2c_pin_dir(gpios, gpios->i2c_sdc, 0); /* Output */
522 I2C_WAIT;
523 i2c_pin_set(gpios, gpios->i2c_sdc, 1);
524 I2C_WAIT;
525 i2c_pin_set(gpios, gpios->i2c_sda, 1);
526 I2C_WAIT;
527 i2c_pin_set(gpios, gpios->i2c_sda, 0);
528 I2C_WAIT;
529 i2c_pin_set(gpios, gpios->i2c_sdc, 0);
530 I2C_WAIT;
531 }
532
533 static void i2c_stop(struct rtl838x_gpios *gpios)
534 {
535 I2C_WAIT;
536 i2c_pin_set(gpios, gpios->i2c_sdc, 1);
537 i2c_pin_set(gpios, gpios->i2c_sda, 0);
538 I2C_WAIT;
539
540 i2c_pin_set(gpios, gpios->i2c_sda, 1);
541 I2C_WAIT;
542 i2c_pin_set(gpios, gpios->i2c_sdc, 0);
543
544 i2c_pin_dir(gpios, gpios->i2c_sda, 1); /* Input */
545 i2c_pin_dir(gpios, gpios->i2c_sdc, 1); /* Input */
546 }
547
548 static void i2c_read_bits(struct rtl838x_gpios *gpios, int len, u32 *data)
549 {
550 u32 v = 0, t;
551
552 while (len) {
553 len--;
554 v <<= 1;
555 i2c_pin_set(gpios, gpios->i2c_sdc, 1);
556 I2C_WAIT;
557 i2c_pin_get(gpios, gpios->i2c_sda, &t);
558 v |= t;
559 i2c_pin_set(gpios, gpios->i2c_sdc, 0);
560 I2C_WAIT;
561 }
562 *data = v;
563 }
564
565 static void i2c_write_bits(struct rtl838x_gpios *gpios, u32 data, int len)
566 {
567 while (len) {
568 len--;
569 i2c_pin_set(gpios, gpios->i2c_sda, data & (1 << len));
570 I2C_WAIT;
571 i2c_pin_set(gpios, gpios->i2c_sdc, 1);
572 I2C_WAIT;
573 i2c_pin_set(gpios, gpios->i2c_sdc, 0);
574 I2C_WAIT;
575 }
576 }
577
578 /* This initializes direct external GPIOs via the RTL8231 */
579 int rtl8380_rtl8321_init(struct rtl838x_gpios *gpios)
580 {
581 u32 v;
582 int mdc = gpios->smi_clock;
583 int mdio = gpios->smi_data;
584
585 pr_info("Configuring SMI: Clock %d, Data %d\n", mdc, mdio);
586 sw_w32_mask(0, 0x2, RTL838X_IO_DRIVING_ABILITY_CTRL);
587
588 /* Enter simulated GPIO mode */
589 sw_w32_mask(1, 0, RTL838X_EXTRA_GPIO_CTRL);
590
591 /* MDIO clock to 2.6MHz */
592 sw_w32_mask(0x3 << 8, 0, RTL838X_EXTRA_GPIO_CTRL);
593
594 /* Configure SMI clock and data GPIO pins */
595 rtl838x_w32_mask((1 << mdc) | (1 << mdio), 0, RTL838X_GPIO_PABC_CNR);
596 rtl838x_w32_mask(0, (1 << mdc) | (1 << mdio), RTL838X_GPIO_PABC_DIR);
597
598 rtl8380_smi_write(gpios, RTL8231_GPIO_PIN_SEL0, 0xffff);
599 rtl8380_smi_write(gpios, RTL8231_GPIO_PIN_SEL1, 0xffff);
600 rtl8380_smi_read(gpios, RTL8231_GPIO_PIN_SEL2, &v);
601 v |= 0x1f;
602 rtl8380_smi_write(gpios, RTL8231_GPIO_PIN_SEL2, v);
603
604 rtl8380_smi_write(gpios, RTL8231_GPIO_IO_SEL0, 0xffff);
605 rtl8380_smi_write(gpios, RTL8231_GPIO_IO_SEL1, 0xffff);
606 rtl8380_smi_read(gpios, RTL8231_GPIO_IO_SEL2, &v);
607 v |= 0x1f << 5;
608 rtl8380_smi_write(gpios, RTL8231_GPIO_PIN_SEL2, v);
609
610 return 0;
611 }
612
613 void rtl8380_led_test(u32 mask)
614 {
615 int i;
616 u32 mode_sel = sw_r32(RTL838X_LED_MODE_SEL);
617 u32 led_gbl = sw_r32(RTL838X_LED_GLB_CTRL);
618 u32 led_p_en = sw_r32(RTL838X_LED_P_EN_CTRL);
619
620 /* 2 Leds for ports 0-23 and 24-27, 3 would be 0x7 */
621 sw_w32_mask(0x3f, 0x3 | (0x3 << 3), RTL838X_LED_GLB_CTRL);
622 /* Enable all leds */
623 sw_w32(0xFFFFFFF, RTL838X_LED_P_EN_CTRL);
624
625 /* Enable software control of all leds */
626 sw_w32(0xFFFFFFF, RTL838X_LED_SW_CTRL);
627 sw_w32(0xFFFFFFF, RTL838X_LED0_SW_P_EN_CTRL);
628 sw_w32(0xFFFFFFF, RTL838X_LED1_SW_P_EN_CTRL);
629 sw_w32(0x0000000, RTL838X_LED2_SW_P_EN_CTRL);
630
631 for (i = 0; i < 28; i++) {
632 if (mask & (1 << i))
633 sw_w32(5 | (5 << 3) | (5 << 6),
634 RTL838X_LED_SW_P_CTRL(i));
635 }
636 msleep(3000);
637
638 sw_w32(led_p_en, RTL838X_LED_P_EN_CTRL);
639 /* Disable software control of all leds */
640 sw_w32(0x0000000, RTL838X_LED_SW_CTRL);
641 sw_w32(0x0000000, RTL838X_LED0_SW_P_EN_CTRL);
642 sw_w32(0x0000000, RTL838X_LED1_SW_P_EN_CTRL);
643 sw_w32(0x0000000, RTL838X_LED2_SW_P_EN_CTRL);
644
645 sw_w32(led_gbl, RTL838X_LED_GLB_CTRL);
646 sw_w32(mode_sel, RTL838X_LED_MODE_SEL);
647 }
648
649 void take_port_leds(struct rtl838x_gpios *gpios)
650 {
651 int leds_per_port = gpios->leds_per_port;
652 int mode = gpios->led_mode;
653
654 pr_info("%s, %d, %x\n", __func__, leds_per_port, mode);
655 pr_debug("Bootloader settings: %x %x %x\n",
656 sw_r32(RTL838X_LED0_SW_P_EN_CTRL),
657 sw_r32(RTL838X_LED1_SW_P_EN_CTRL),
658 sw_r32(RTL838X_LED2_SW_P_EN_CTRL)
659 );
660
661 pr_debug("led glb: %x, sel %x\n",
662 sw_r32(RTL838X_LED_GLB_CTRL), sw_r32(RTL838X_LED_MODE_SEL));
663 pr_debug("RTL838X_LED_P_EN_CTRL: %x", sw_r32(RTL838X_LED_P_EN_CTRL));
664 pr_debug("RTL838X_LED_MODE_CTRL: %x", sw_r32(RTL838X_LED_MODE_CTRL));
665
666 sw_w32_mask(3, 0, RTL838X_LED_MODE_SEL);
667 sw_w32(mode, RTL838X_LED_MODE_CTRL);
668
669 /* Enable software control of all leds */
670 sw_w32(0xFFFFFFF, RTL838X_LED_SW_CTRL);
671 sw_w32(0xFFFFFFF, RTL838X_LED_P_EN_CTRL);
672
673 sw_w32(0x0000000, RTL838X_LED0_SW_P_EN_CTRL);
674 sw_w32(0x0000000, RTL838X_LED1_SW_P_EN_CTRL);
675 sw_w32(0x0000000, RTL838X_LED2_SW_P_EN_CTRL);
676
677 sw_w32_mask(0x3f, 0, RTL838X_LED_GLB_CTRL);
678 switch (leds_per_port) {
679 case 3:
680 sw_w32_mask(0, 0x7 | (0x7 << 3), RTL838X_LED_GLB_CTRL);
681 sw_w32(0xFFFFFFF, RTL838X_LED2_SW_P_EN_CTRL);
682 /* FALLTHRU */
683 case 2:
684 sw_w32_mask(0, 0x3 | (0x3 << 3), RTL838X_LED_GLB_CTRL);
685 sw_w32(0xFFFFFFF, RTL838X_LED1_SW_P_EN_CTRL);
686 /* FALLTHRU */
687 case 1:
688 sw_w32_mask(0, 0x1 | (0x1 << 3), RTL838X_LED_GLB_CTRL);
689 sw_w32(0xFFFFFFF, RTL838X_LED0_SW_P_EN_CTRL);
690 break;
691 default:
692 pr_err("No LEDS configured for software control\n");
693 }
694 }
695
696 static const struct of_device_id rtl838x_gpio_of_match[] = {
697 { .compatible = "realtek,rtl838x-gpio" },
698 {},
699 };
700
701 MODULE_DEVICE_TABLE(of, rtl838x_gpio_of_match);
702
703 static int rtl838x_gpio_probe(struct platform_device *pdev)
704 {
705 struct device *dev = &pdev->dev;
706 struct device_node *np = dev->of_node;
707 struct rtl838x_gpios *gpios;
708 int err;
709 u8 indirect_bus_id;
710
711 pr_info("Probing RTL838X GPIOs\n");
712
713 if (!np) {
714 dev_err(&pdev->dev, "No DT found\n");
715 return -EINVAL;
716 }
717
718 gpios = devm_kzalloc(dev, sizeof(*gpios), GFP_KERNEL);
719 if (!gpios)
720 return -ENOMEM;
721
722 gpios->id = sw_r32(RTL838X_MODEL_NAME_INFO) >> 16;
723
724 switch (gpios->id) {
725 case 0x8332:
726 pr_debug("Found RTL8332M GPIO\n");
727 break;
728 case 0x8380:
729 pr_debug("Found RTL8380M GPIO\n");
730 break;
731 case 0x8381:
732 pr_debug("Found RTL8381M GPIO\n");
733 break;
734 case 0x8382:
735 pr_debug("Found RTL8382M GPIO\n");
736 break;
737 default:
738 pr_err("Unknown GPIO chip id (%04x)\n", gpios->id);
739 return -ENODEV;
740 }
741
742 gpios->dev = dev;
743 gpios->gc.base = 0;
744 /* 0-31: internal
745 * 32-63, LED control register
746 * 64-99: external RTL8231
747 * 100-131: PORT-LED 0
748 * 132-163: PORT-LED 1
749 * 164-195: PORT-LED 2
750 */
751 gpios->gc.ngpio = 196;
752 gpios->gc.label = "rtl838x";
753 gpios->gc.parent = dev;
754 gpios->gc.owner = THIS_MODULE;
755 gpios->gc.can_sleep = true;
756 gpios->bus_id = -1;
757 gpios->irq = 31;
758
759 gpios->gc.direction_input = rtl838x_direction_input;
760 gpios->gc.direction_output = rtl838x_direction_output;
761 gpios->gc.set = rtl838x_gpio_set;
762 gpios->gc.get = rtl838x_gpio_get;
763 gpios->gc.get_direction = rtl838x_get_direction;
764
765 if (!of_property_read_u8(np, "indirect-access-bus-id", &indirect_bus_id)) {
766 gpios->bus_id = indirect_bus_id;
767 rtl8231_init(gpios);
768 }
769 if (!of_property_read_u8(np, "smi-bus-id", &indirect_bus_id)) {
770 gpios->bus_id = indirect_bus_id;
771 gpios->smi_clock = RTL838X_GPIO_A2;
772 gpios->smi_data = RTL838X_GPIO_A3;
773 gpios->i2c_sda = 1;
774 gpios->i2c_sdc = 2;
775 rtl8380_rtl8321_init(gpios);
776 }
777
778 if (of_property_read_bool(np, "take-port-leds")) {
779 if (of_property_read_u32(np, "leds-per-port", &gpios->leds_per_port))
780 gpios->leds_per_port = 2;
781 if (of_property_read_u32(np, "led-mode", &gpios->led_mode))
782 gpios->led_mode = (0x1ea << 15) | 0x1ea;
783 if (of_property_read_u32(np, "num-leds", &gpios->num_leds))
784 gpios->num_leds = 32;
785 if (of_property_read_u32(np, "min-led", &gpios->min_led))
786 gpios->min_led = 0;
787 take_port_leds(gpios);
788 }
789
790 err = devm_gpiochip_add_data(dev, &gpios->gc, gpios);
791 return err;
792 }
793
794
795 static struct platform_driver rtl838x_gpio_driver = {
796 .driver = {
797 .name = "rtl838x-gpio",
798 .of_match_table = rtl838x_gpio_of_match,
799 },
800 .probe = rtl838x_gpio_probe,
801 };
802
803 module_platform_driver(rtl838x_gpio_driver);
804
805 MODULE_DESCRIPTION("Realtek RTL838X GPIO API support");
806 MODULE_LICENSE("GPL v2");