gpio-rtl838x.c 20 KB

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