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-rtl838x.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))
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
21 #define MDC_WAIT { int i; for (i = 0; i < 2; i++); }
22 #define I2C_WAIT { int i; for (i = 0; i < 5; i++); }
24 struct rtl838x_gpios
{
41 extern struct mutex smi_lock
;
43 u32
rtl838x_rtl8231_read(u8 bus_id
, u32 reg
)
50 /* Calculate read register address */
51 t
= (bus_id
<< 2) | (reg
<< 7);
53 mutex_lock(&smi_lock
);
54 /* Set execution bit: cleared when operation completed */
56 sw_w32(t
, RTL838X_EXT_GPIO_INDRT_ACCESS
);
57 do { /* TODO: Return 0x80000000 if timeout */
58 t
= sw_r32(RTL838X_EXT_GPIO_INDRT_ACCESS
);
60 pr_debug("%s: %x, %x, %x\n", __func__
, bus_id
, reg
, (t
& 0xffff0000) >> 16);
62 mutex_unlock(&smi_lock
);
63 return (t
& 0xffff0000) >> 16;
66 int rtl838x_rtl8231_write(u8 bus_id
, u32 reg
, u32 data
)
70 pr_debug("%s: %x, %x, %x\n", __func__
, bus_id
, reg
, data
);
75 mutex_lock(&smi_lock
);
76 t
= (bus_id
<< 2) | (reg
<< 7) | (data
<< 16);
80 /* Set execution bit: cleared when operation completed */
82 sw_w32(t
, RTL838X_EXT_GPIO_INDRT_ACCESS
);
83 do { /* TODO: Return -1 if timeout */
84 t
= sw_r32(RTL838X_EXT_GPIO_INDRT_ACCESS
);
87 mutex_unlock(&smi_lock
);
91 static int rtl8231_pin_dir(u8 bus_id
, u32 gpio
, u32 dir
)
98 int pin_sel_addr
= RTL8231_GPIO_PIN_SEL(gpio
);
99 int pin_dir_addr
= RTL8231_GPIO_DIR(gpio
);
105 pin_dir_addr
= pin_sel_addr
;
108 /* Select GPIO function for pin */
109 v
= rtl838x_rtl8231_read(bus_id
, pin_sel_addr
);
110 if (v
& 0x80000000) {
111 pr_err("Error reading RTL8231\n");
114 rtl838x_rtl8231_write(bus_id
, pin_sel_addr
, v
| (1 << pin
));
116 v
= rtl838x_rtl8231_read(bus_id
, pin_dir_addr
);
117 if (v
& 0x80000000) {
118 pr_err("Error reading RTL8231\n");
121 rtl838x_rtl8231_write(bus_id
, pin_dir_addr
,
122 (v
& ~(1 << dpin
)) | (dir
<< dpin
));
126 static int rtl8231_pin_dir_get(u8 bus_id
, u32 gpio
, u32
*dir
)
133 int pin_dir_addr
= RTL8231_GPIO_DIR(gpio
);
137 pin_dir_addr
= RTL8231_GPIO_PIN_SEL(gpio
);
141 v
= rtl838x_rtl8231_read(bus_id
, pin_dir_addr
);
149 static int rtl8231_pin_set(u8 bus_id
, u32 gpio
, u32 data
)
151 u32 v
= rtl838x_rtl8231_read(bus_id
, RTL8231_GPIO_DATA(gpio
));
153 if (v
& 0x80000000) {
154 pr_err("Error reading RTL8231\n");
157 rtl838x_rtl8231_write(bus_id
, RTL8231_GPIO_DATA(gpio
),
158 (v
& ~(1 << (gpio
% 16))) | (data
<< (gpio
% 16)));
162 static int rtl8231_pin_get(u8 bus_id
, u32 gpio
, u16
*state
)
164 u32 v
= rtl838x_rtl8231_read(bus_id
, RTL8231_GPIO_DATA(gpio
));
166 if (v
& 0x80000000) {
167 pr_err("Error reading RTL8231\n");
175 static int rtl838x_direction_input(struct gpio_chip
*gc
, unsigned int offset
)
177 struct rtl838x_gpios
*gpios
= gpiochip_get_data(gc
);
179 pr_debug("%s: %d\n", __func__
, offset
);
182 rtl838x_w32_mask(1 << offset
, 0, RTL838X_GPIO_PABC_DIR
);
186 /* Internal LED driver does not support input */
187 if (offset
>= 32 && offset
< 64)
190 if (offset
>= 64 && offset
< 100 && gpios
->bus_id
>= 0)
191 return rtl8231_pin_dir(gpios
->bus_id
, offset
- 64, 1);
196 static int rtl838x_direction_output(struct gpio_chip
*gc
, unsigned int offset
, int value
)
198 struct rtl838x_gpios
*gpios
= gpiochip_get_data(gc
);
200 pr_debug("%s: %d\n", __func__
, offset
);
202 rtl838x_w32_mask(0, 1 << offset
, RTL838X_GPIO_PABC_DIR
);
204 /* LED for PWR and SYS driver is direction output by default */
205 if (offset
>= 32 && offset
< 64)
208 if (offset
>= 64 && offset
< 100 && gpios
->bus_id
>= 0)
209 return rtl8231_pin_dir(gpios
->bus_id
, offset
- 64, 0);
213 static int rtl838x_get_direction(struct gpio_chip
*gc
, unsigned int offset
)
216 struct rtl838x_gpios
*gpios
= gpiochip_get_data(gc
);
218 pr_debug("%s: %d\n", __func__
, offset
);
220 v
= rtl838x_r32(RTL838X_GPIO_PABC_DIR
);
221 if (v
& (1 << offset
))
226 /* LED driver for PWR and SYS is direction output by default */
227 if (offset
>= 32 && offset
< 64)
230 if (offset
>= 64 && offset
< 100 && gpios
->bus_id
>= 0) {
231 rtl8231_pin_dir_get(gpios
->bus_id
, offset
- 64, &v
);
238 static int rtl838x_gpio_get(struct gpio_chip
*gc
, unsigned int offset
)
243 struct rtl838x_gpios
*gpios
= gpiochip_get_data(gc
);
245 pr_debug("%s: %d\n", __func__
, offset
);
247 /* Internal GPIO of the RTL8380 */
249 v
= rtl838x_r32(RTL838X_GPIO_PABC_DATA
);
250 if (v
& (1 << offset
))
255 /* LED driver for PWR and SYS */
256 if (offset
>= 32 && offset
< 64) {
257 v
= sw_r32(RTL838X_LED_GLB_CTRL
);
258 if (v
& (1 << (offset
-32)))
263 /* Indirect access GPIO with RTL8231 */
264 if (offset
>= 64 && offset
< 100 && gpios
->bus_id
>= 0) {
265 rtl8231_pin_get(gpios
->bus_id
, offset
- 64, &state
);
266 if (state
& (1 << (offset
% 16)))
271 bit
= (offset
- 100) % 32;
272 if (offset
>= 100 && offset
< 132) {
273 if (sw_r32(RTL838X_LED1_SW_P_EN_CTRL
) & (1 << bit
))
277 if (offset
>= 132 && offset
< 164) {
278 if (sw_r32(RTL838X_LED1_SW_P_EN_CTRL
) & (1 << bit
))
282 if (offset
>= 164 && offset
< 196) {
283 if (sw_r32(RTL838X_LED1_SW_P_EN_CTRL
) & (1 << bit
))
290 void rtl838x_gpio_set(struct gpio_chip
*gc
, unsigned int offset
, int value
)
293 struct rtl838x_gpios
*gpios
= gpiochip_get_data(gc
);
295 pr_debug("rtl838x_set: %d, value: %d\n", offset
, value
);
296 /* Internal GPIO of the RTL8380 */
299 rtl838x_w32_mask(0, 1 << offset
, RTL838X_GPIO_PABC_DATA
);
301 rtl838x_w32_mask(1 << offset
, 0, RTL838X_GPIO_PABC_DATA
);
304 /* LED driver for PWR and SYS */
305 if (offset
>= 32 && offset
< 64) {
308 sw_w32_mask(0, 1 << bit
, RTL838X_LED_GLB_CTRL
);
310 sw_w32_mask(1 << bit
, 0, RTL838X_LED_GLB_CTRL
);
314 /* Indirect access GPIO with RTL8231 */
315 if (offset
>= 64 && offset
< 100 && gpios
->bus_id
>= 0) {
316 rtl8231_pin_set(gpios
->bus_id
, offset
- 64, value
);
320 bit
= (offset
- 100) % 32;
322 if (offset
>= 100 && offset
< 132
323 && offset
>= (100 + gpios
->min_led
)
324 && offset
< (100 + gpios
->min_led
+ gpios
->num_leds
)) {
326 sw_w32_mask(7, 5, RTL838X_LED_SW_P_CTRL(bit
));
328 sw_w32_mask(7, 0, RTL838X_LED_SW_P_CTRL(bit
));
330 if (offset
>= 132 && offset
< 164
331 && offset
>= (132 + gpios
->min_led
)
332 && offset
< (132 + gpios
->min_led
+ gpios
->num_leds
)) {
334 sw_w32_mask(7 << 3, 5 << 3, RTL838X_LED_SW_P_CTRL(bit
));
336 sw_w32_mask(7 << 3, 0, RTL838X_LED_SW_P_CTRL(bit
));
338 if (offset
>= 164 && offset
< 196
339 && offset
>= (164 + gpios
->min_led
)
340 && offset
< (164 + gpios
->min_led
+ gpios
->num_leds
)) {
342 sw_w32_mask(7 << 6, 5 << 6, RTL838X_LED_SW_P_CTRL(bit
));
344 sw_w32_mask(7 << 6, 0, RTL838X_LED_SW_P_CTRL(bit
));
346 __asm__
volatile ("sync");
349 int rtl8231_init(struct rtl838x_gpios
*gpios
)
352 u8 bus_id
= gpios
->bus_id
;
354 pr_info("%s called\n", __func__
);
356 /* Enable RTL8231 indirect access mode */
357 sw_w32_mask(0, 1, RTL838X_EXTRA_GPIO_CTRL
);
358 sw_w32_mask(3, 1, RTL838X_DMY_REG5
);
360 /* Enable RTL8231 via GPIO_A1 line */
361 rtl838x_w32_mask(0, 1 << RTL838X_GPIO_A1
, RTL838X_GPIO_PABC_DIR
);
362 rtl838x_w32_mask(0, 1 << RTL838X_GPIO_A1
, RTL838X_GPIO_PABC_DATA
);
363 mdelay(50); /* wait 50ms for reset */
365 /*Select GPIO functionality for pins 0-15, 16-31 and 32-37 */
366 rtl838x_rtl8231_write(bus_id
, RTL8231_GPIO_PIN_SEL(0), 0xffff);
367 rtl838x_rtl8231_write(bus_id
, RTL8231_GPIO_PIN_SEL(16), 0xffff);
368 rtl838x_rtl8231_write(bus_id
, RTL8231_GPIO_PIN_SEL2
, 0x03ff);
370 v
= rtl838x_rtl8231_read(bus_id
, RTL8231_LED_FUNC0
);
371 pr_info("RTL8231 led function now: %x\n", v
);
376 static void smi_write_bit(struct rtl838x_gpios
*gpios
, u32 bit
)
379 rtl838x_w32_mask(0, 1 << gpios
->smi_data
, RTL838X_GPIO_PABC_DATA
);
381 rtl838x_w32_mask(1 << gpios
->smi_data
, 0, RTL838X_GPIO_PABC_DATA
);
384 rtl838x_w32_mask(1 << gpios
->smi_clock
, 0, RTL838X_GPIO_PABC_DATA
);
386 rtl838x_w32_mask(0, 1 << gpios
->smi_clock
, RTL838X_GPIO_PABC_DATA
);
389 static int smi_read_bit(struct rtl838x_gpios
*gpios
)
394 rtl838x_w32_mask(1 << gpios
->smi_clock
, 0, RTL838X_GPIO_PABC_DATA
);
396 rtl838x_w32_mask(0, 1 << gpios
->smi_clock
, RTL838X_GPIO_PABC_DATA
);
398 v
= rtl838x_r32(RTL838X_GPIO_PABC_DATA
);
399 if (v
& (1 << gpios
->smi_data
))
404 /* Tri-state of MDIO line */
405 static void smi_z(struct rtl838x_gpios
*gpios
)
407 /* MDIO pin to input */
408 rtl838x_w32_mask(1 << gpios
->smi_data
, 0, RTL838X_GPIO_PABC_DIR
);
410 rtl838x_w32_mask(1 << gpios
->smi_clock
, 0, RTL838X_GPIO_PABC_DATA
);
412 rtl838x_w32_mask(0, 1 << gpios
->smi_clock
, RTL838X_GPIO_PABC_DATA
);
415 static void smi_write_bits(struct rtl838x_gpios
*gpios
, u32 data
, int len
)
419 smi_write_bit(gpios
, data
& (1 << len
));
423 static void smi_read_bits(struct rtl838x_gpios
*gpios
, int len
, u32
*data
)
430 v
|= smi_read_bit(gpios
);
435 /* Bit-banged verson of SMI write access, caller must hold smi_lock */
436 int rtl8380_smi_write(struct rtl838x_gpios
*gpios
, u16 reg
, u32 data
)
438 u16 bus_id
= gpios
->bus_id
;
440 /* Set clock and data pins on RTL838X to output */
441 rtl838x_w32_mask(0, 1 << gpios
->smi_clock
, RTL838X_GPIO_PABC_DIR
);
442 rtl838x_w32_mask(0, 1 << gpios
->smi_data
, RTL838X_GPIO_PABC_DIR
);
444 /* Write start bits */
445 smi_write_bits(gpios
, 0xffffffff, 32);
447 smi_write_bits(gpios
, 0x5, 4); /* ST and write OP */
449 smi_write_bits(gpios
, bus_id
, 5); /* 5 bits: phy address */
450 smi_write_bits(gpios
, reg
, 5); /* 5 bits: register address */
452 smi_write_bits(gpios
, 0x2, 2); /* TURNAROUND */
454 smi_write_bits(gpios
, data
, 16); /* 16 bits: data*/
461 /* Bit-banged verson of SMI read access, caller must hold smi_lock */
462 int rtl8380_smi_read(struct rtl838x_gpios
*gpios
, u16 reg
, u32
*data
)
464 u16 bus_id
= gpios
->bus_id
;
466 /* Set clock and data pins on RTL838X to output */
467 rtl838x_w32_mask(0, 1 << gpios
->smi_clock
, RTL838X_GPIO_PABC_DIR
);
468 rtl838x_w32_mask(0, 1 << gpios
->smi_data
, RTL838X_GPIO_PABC_DIR
);
470 /* Write start bits */
471 smi_write_bits(gpios
, 0xffffffff, 32);
473 smi_write_bits(gpios
, 0x6, 4); /* ST and read OP */
475 smi_write_bits(gpios
, bus_id
, 5); /* 5 bits: phy address */
476 smi_write_bits(gpios
, reg
, 5); /* 5 bits: register address */
478 smi_z(gpios
); /* TURNAROUND */
480 smi_read_bits(gpios
, 16, data
);
484 static void i2c_pin_set(struct rtl838x_gpios
*gpios
, int pin
, u32 data
)
488 rtl8380_smi_read(gpios
, RTL8231_GPIO_DATA(pin
), &v
);
490 v
&= ~(1 << (pin
% 16));
492 v
|= (1 << (pin
% 16));
493 rtl8380_smi_write(gpios
, RTL8231_GPIO_DATA(pin
), v
);
496 static void i2c_pin_get(struct rtl838x_gpios
*gpios
, int pin
, u32
*data
)
500 rtl8380_smi_read(gpios
, RTL8231_GPIO_DATA(pin
), &v
);
501 if (v
& (1 << (pin
% 16))) {
508 static void i2c_pin_dir(struct rtl838x_gpios
*gpios
, int pin
, u16 direction
)
512 rtl8380_smi_read(gpios
, RTL8231_GPIO_DIR(pin
), &v
);
513 if (direction
) // Output
514 v
&= ~(1 << (pin
% 16));
516 v
|= (1 << (pin
% 16));
517 rtl8380_smi_write(gpios
, RTL8231_GPIO_DIR(pin
), v
);
520 static void i2c_start(struct rtl838x_gpios
*gpios
)
522 i2c_pin_dir(gpios
, gpios
->i2c_sda
, 0); /* Output */
523 i2c_pin_dir(gpios
, gpios
->i2c_sdc
, 0); /* Output */
525 i2c_pin_set(gpios
, gpios
->i2c_sdc
, 1);
527 i2c_pin_set(gpios
, gpios
->i2c_sda
, 1);
529 i2c_pin_set(gpios
, gpios
->i2c_sda
, 0);
531 i2c_pin_set(gpios
, gpios
->i2c_sdc
, 0);
535 static void i2c_stop(struct rtl838x_gpios
*gpios
)
538 i2c_pin_set(gpios
, gpios
->i2c_sdc
, 1);
539 i2c_pin_set(gpios
, gpios
->i2c_sda
, 0);
542 i2c_pin_set(gpios
, gpios
->i2c_sda
, 1);
544 i2c_pin_set(gpios
, gpios
->i2c_sdc
, 0);
546 i2c_pin_dir(gpios
, gpios
->i2c_sda
, 1); /* Input */
547 i2c_pin_dir(gpios
, gpios
->i2c_sdc
, 1); /* Input */
550 static void i2c_read_bits(struct rtl838x_gpios
*gpios
, int len
, u32
*data
)
557 i2c_pin_set(gpios
, gpios
->i2c_sdc
, 1);
559 i2c_pin_get(gpios
, gpios
->i2c_sda
, &t
);
561 i2c_pin_set(gpios
, gpios
->i2c_sdc
, 0);
567 static void i2c_write_bits(struct rtl838x_gpios
*gpios
, u32 data
, int len
)
571 i2c_pin_set(gpios
, gpios
->i2c_sda
, data
& (1 << len
));
573 i2c_pin_set(gpios
, gpios
->i2c_sdc
, 1);
575 i2c_pin_set(gpios
, gpios
->i2c_sdc
, 0);
580 /* This initializes direct external GPIOs via the RTL8231 */
581 int rtl8380_rtl8321_init(struct rtl838x_gpios
*gpios
)
584 int mdc
= gpios
->smi_clock
;
585 int mdio
= gpios
->smi_data
;
587 pr_info("Configuring SMI: Clock %d, Data %d\n", mdc
, mdio
);
588 sw_w32_mask(0, 0x2, RTL838X_IO_DRIVING_ABILITY_CTRL
);
590 /* Enter simulated GPIO mode */
591 sw_w32_mask(1, 0, RTL838X_EXTRA_GPIO_CTRL
);
593 /* MDIO clock to 2.6MHz */
594 sw_w32_mask(0x3 << 8, 0, RTL838X_EXTRA_GPIO_CTRL
);
596 /* Configure SMI clock and data GPIO pins */
597 rtl838x_w32_mask((1 << mdc
) | (1 << mdio
), 0, RTL838X_GPIO_PABC_CNR
);
598 rtl838x_w32_mask(0, (1 << mdc
) | (1 << mdio
), RTL838X_GPIO_PABC_DIR
);
600 rtl8380_smi_write(gpios
, RTL8231_GPIO_PIN_SEL0
, 0xffff);
601 rtl8380_smi_write(gpios
, RTL8231_GPIO_PIN_SEL1
, 0xffff);
602 rtl8380_smi_read(gpios
, RTL8231_GPIO_PIN_SEL2
, &v
);
604 rtl8380_smi_write(gpios
, RTL8231_GPIO_PIN_SEL2
, v
);
606 rtl8380_smi_write(gpios
, RTL8231_GPIO_IO_SEL0
, 0xffff);
607 rtl8380_smi_write(gpios
, RTL8231_GPIO_IO_SEL1
, 0xffff);
608 rtl8380_smi_read(gpios
, RTL8231_GPIO_IO_SEL2
, &v
);
610 rtl8380_smi_write(gpios
, RTL8231_GPIO_PIN_SEL2
, v
);
615 void rtl8380_led_test(u32 mask
)
618 u32 mode_sel
= sw_r32(RTL838X_LED_MODE_SEL
);
619 u32 led_gbl
= sw_r32(RTL838X_LED_GLB_CTRL
);
620 u32 led_p_en
= sw_r32(RTL838X_LED_P_EN_CTRL
);
622 /* 2 Leds for ports 0-23 and 24-27, 3 would be 0x7 */
623 sw_w32_mask(0x3f, 0x3 | (0x3 << 3), RTL838X_LED_GLB_CTRL
);
624 /* Enable all leds */
625 sw_w32(0xFFFFFFF, RTL838X_LED_P_EN_CTRL
);
627 /* Enable software control of all leds */
628 sw_w32(0xFFFFFFF, RTL838X_LED_SW_CTRL
);
629 sw_w32(0xFFFFFFF, RTL838X_LED0_SW_P_EN_CTRL
);
630 sw_w32(0xFFFFFFF, RTL838X_LED1_SW_P_EN_CTRL
);
631 sw_w32(0x0000000, RTL838X_LED2_SW_P_EN_CTRL
);
633 for (i
= 0; i
< 28; i
++) {
635 sw_w32(5 | (5 << 3) | (5 << 6),
636 RTL838X_LED_SW_P_CTRL(i
));
640 sw_w32(led_p_en
, RTL838X_LED_P_EN_CTRL
);
641 /* Disable software control of all leds */
642 sw_w32(0x0000000, RTL838X_LED_SW_CTRL
);
643 sw_w32(0x0000000, RTL838X_LED0_SW_P_EN_CTRL
);
644 sw_w32(0x0000000, RTL838X_LED1_SW_P_EN_CTRL
);
645 sw_w32(0x0000000, RTL838X_LED2_SW_P_EN_CTRL
);
647 sw_w32(led_gbl
, RTL838X_LED_GLB_CTRL
);
648 sw_w32(mode_sel
, RTL838X_LED_MODE_SEL
);
651 void take_port_leds(struct rtl838x_gpios
*gpios
)
653 int leds_per_port
= gpios
->leds_per_port
;
654 int mode
= gpios
->led_mode
;
656 pr_info("%s, %d, %x\n", __func__
, leds_per_port
, mode
);
657 pr_debug("Bootloader settings: %x %x %x\n",
658 sw_r32(RTL838X_LED0_SW_P_EN_CTRL
),
659 sw_r32(RTL838X_LED1_SW_P_EN_CTRL
),
660 sw_r32(RTL838X_LED2_SW_P_EN_CTRL
)
663 pr_debug("led glb: %x, sel %x\n",
664 sw_r32(RTL838X_LED_GLB_CTRL
), sw_r32(RTL838X_LED_MODE_SEL
));
665 pr_debug("RTL838X_LED_P_EN_CTRL: %x", sw_r32(RTL838X_LED_P_EN_CTRL
));
666 pr_debug("RTL838X_LED_MODE_CTRL: %x", sw_r32(RTL838X_LED_MODE_CTRL
));
668 sw_w32_mask(3, 0, RTL838X_LED_MODE_SEL
);
669 sw_w32(mode
, RTL838X_LED_MODE_CTRL
);
671 /* Enable software control of all leds */
672 sw_w32(0xFFFFFFF, RTL838X_LED_SW_CTRL
);
673 sw_w32(0xFFFFFFF, RTL838X_LED_P_EN_CTRL
);
675 sw_w32(0x0000000, RTL838X_LED0_SW_P_EN_CTRL
);
676 sw_w32(0x0000000, RTL838X_LED1_SW_P_EN_CTRL
);
677 sw_w32(0x0000000, RTL838X_LED2_SW_P_EN_CTRL
);
679 sw_w32_mask(0x3f, 0, RTL838X_LED_GLB_CTRL
);
680 switch (leds_per_port
) {
682 sw_w32_mask(0, 0x7 | (0x7 << 3), RTL838X_LED_GLB_CTRL
);
683 sw_w32(0xFFFFFFF, RTL838X_LED2_SW_P_EN_CTRL
);
686 sw_w32_mask(0, 0x3 | (0x3 << 3), RTL838X_LED_GLB_CTRL
);
687 sw_w32(0xFFFFFFF, RTL838X_LED1_SW_P_EN_CTRL
);
690 sw_w32_mask(0, 0x1 | (0x1 << 3), RTL838X_LED_GLB_CTRL
);
691 sw_w32(0xFFFFFFF, RTL838X_LED0_SW_P_EN_CTRL
);
694 pr_err("No LEDS configured for software control\n");
698 static const struct of_device_id rtl838x_gpio_of_match
[] = {
699 { .compatible
= "realtek,rtl838x-gpio" },
703 MODULE_DEVICE_TABLE(of
, rtl838x_gpio_of_match
);
705 static int rtl838x_gpio_probe(struct platform_device
*pdev
)
707 struct device
*dev
= &pdev
->dev
;
708 struct device_node
*np
= dev
->of_node
;
709 struct rtl838x_gpios
*gpios
;
713 pr_info("Probing RTL838X GPIOs\n");
716 dev_err(&pdev
->dev
, "No DT found\n");
720 gpios
= devm_kzalloc(dev
, sizeof(*gpios
), GFP_KERNEL
);
724 gpios
->id
= sw_r32(RTL838X_MODEL_NAME_INFO
) >> 16;
728 pr_debug("Found RTL8332M GPIO\n");
731 pr_debug("Found RTL8380M GPIO\n");
734 pr_debug("Found RTL8381M GPIO\n");
737 pr_debug("Found RTL8382M GPIO\n");
740 pr_err("Unknown GPIO chip id (%04x)\n", gpios
->id
);
747 * 32-63, LED control register
748 * 64-99: external RTL8231
749 * 100-131: PORT-LED 0
750 * 132-163: PORT-LED 1
751 * 164-195: PORT-LED 2
753 gpios
->gc
.ngpio
= 196;
754 gpios
->gc
.label
= "rtl838x";
755 gpios
->gc
.parent
= dev
;
756 gpios
->gc
.owner
= THIS_MODULE
;
757 gpios
->gc
.can_sleep
= true;
761 gpios
->gc
.direction_input
= rtl838x_direction_input
;
762 gpios
->gc
.direction_output
= rtl838x_direction_output
;
763 gpios
->gc
.set
= rtl838x_gpio_set
;
764 gpios
->gc
.get
= rtl838x_gpio_get
;
765 gpios
->gc
.get_direction
= rtl838x_get_direction
;
767 if (!of_property_read_u8(np
, "indirect-access-bus-id", &indirect_bus_id
)) {
768 gpios
->bus_id
= indirect_bus_id
;
771 if (!of_property_read_u8(np
, "smi-bus-id", &indirect_bus_id
)) {
772 gpios
->bus_id
= indirect_bus_id
;
773 gpios
->smi_clock
= RTL838X_GPIO_A2
;
774 gpios
->smi_data
= RTL838X_GPIO_A3
;
777 rtl8380_rtl8321_init(gpios
);
780 if (of_property_read_bool(np
, "take-port-leds")) {
781 if (of_property_read_u32(np
, "leds-per-port", &gpios
->leds_per_port
))
782 gpios
->leds_per_port
= 2;
783 if (of_property_read_u32(np
, "led-mode", &gpios
->led_mode
))
784 gpios
->led_mode
= (0x1ea << 15) | 0x1ea;
785 if (of_property_read_u32(np
, "num-leds", &gpios
->num_leds
))
786 gpios
->num_leds
= 32;
787 if (of_property_read_u32(np
, "min-led", &gpios
->min_led
))
789 take_port_leds(gpios
);
792 err
= devm_gpiochip_add_data(dev
, &gpios
->gc
, gpios
);
797 static struct platform_driver rtl838x_gpio_driver
= {
799 .name
= "rtl838x-gpio",
800 .of_match_table
= rtl838x_gpio_of_match
,
802 .probe
= rtl838x_gpio_probe
,
805 module_platform_driver(rtl838x_gpio_driver
);
807 MODULE_DESCRIPTION("Realtek RTL838X GPIO API support");
808 MODULE_LICENSE("GPL v2");