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