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