gpio-rtl838x.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  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-rtl83xx.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. struct rtl838x_gpios {
  13. struct gpio_chip gc;
  14. u32 id;
  15. struct device *dev;
  16. int irq;
  17. int num_leds;
  18. int min_led;
  19. int leds_per_port;
  20. u32 led_mode;
  21. int led_glb_ctrl;
  22. int led_sw_ctrl;
  23. int (*led_sw_p_ctrl)(int port);
  24. int (*led_sw_p_en_ctrl)(int port);
  25. int (*ext_gpio_dir)(int i);
  26. int (*ext_gpio_data)(int i);
  27. };
  28. inline int rtl838x_ext_gpio_dir(int i)
  29. {
  30. return RTL838X_EXT_GPIO_DIR + ((i >>5) << 2);
  31. }
  32. inline int rtl839x_ext_gpio_dir(int i)
  33. {
  34. return RTL839X_EXT_GPIO_DIR + ((i >>5) << 2);
  35. }
  36. inline int rtl838x_ext_gpio_data(int i)
  37. {
  38. return RTL838X_EXT_GPIO_DATA + ((i >>5) << 2);
  39. }
  40. inline int rtl839x_ext_gpio_data(int i)
  41. {
  42. return RTL839X_EXT_GPIO_DATA + ((i >>5) << 2);
  43. }
  44. inline int rtl838x_led_sw_p_ctrl(int p)
  45. {
  46. return RTL838X_LED_SW_P_CTRL + (p << 2);
  47. }
  48. inline int rtl839x_led_sw_p_ctrl(int p)
  49. {
  50. return RTL839X_LED_SW_P_CTRL + (p << 2);
  51. }
  52. inline int rtl838x_led_sw_p_en_ctrl(int p)
  53. {
  54. return RTL838X_LED_SW_P_EN_CTRL + ((p / 10) << 2);
  55. }
  56. inline int rtl839x_led_sw_p_en_ctrl(int p)
  57. {
  58. return RTL839X_LED_SW_P_EN_CTRL + ((p / 10) << 2);
  59. }
  60. extern struct mutex smi_lock;
  61. extern struct rtl83xx_soc_info soc_info;
  62. void rtl838x_gpio_set(struct gpio_chip *gc, unsigned int offset, int value)
  63. {
  64. int bit;
  65. struct rtl838x_gpios *gpios = gpiochip_get_data(gc);
  66. pr_debug("rtl838x_set: %d, value: %d\n", offset, value);
  67. /* Internal GPIO of the RTL8380 */
  68. if (offset < 32) {
  69. if (value)
  70. rtl83xx_w32_mask(0, BIT(offset), RTL838X_GPIO_PABC_DATA);
  71. else
  72. rtl83xx_w32_mask(BIT(offset), 0, RTL838X_GPIO_PABC_DATA);
  73. }
  74. /* LED driver for PWR and SYS */
  75. if (offset >= 32 && offset < 64) {
  76. bit = offset - 32;
  77. if (value)
  78. sw_w32_mask(0, BIT(bit), gpios->led_glb_ctrl);
  79. else
  80. sw_w32_mask(BIT(bit), 0, gpios->led_glb_ctrl);
  81. return;
  82. }
  83. bit = (offset - 64) % 32;
  84. /* First Port-LED */
  85. if (offset >= 64 && offset < 96
  86. && offset >= (64 + gpios->min_led)
  87. && offset < (64 + gpios->min_led + gpios->num_leds)) {
  88. if (value)
  89. sw_w32_mask(7, 5, gpios->led_sw_p_ctrl(bit));
  90. else
  91. sw_w32_mask(7, 0, gpios->led_sw_p_ctrl(bit));
  92. }
  93. if (offset >= 96 && offset < 128
  94. && offset >= (96 + gpios->min_led)
  95. && offset < (96 + gpios->min_led + gpios->num_leds)) {
  96. if (value)
  97. sw_w32_mask(7 << 3, 5 << 3, gpios->led_sw_p_ctrl(bit));
  98. else
  99. sw_w32_mask(7 << 3, 0, gpios->led_sw_p_ctrl(bit));
  100. }
  101. if (offset >= 128 && offset < 160
  102. && offset >= (128 + gpios->min_led)
  103. && offset < (128 + gpios->min_led + gpios->num_leds)) {
  104. if (value)
  105. sw_w32_mask(7 << 6, 5 << 6, gpios->led_sw_p_ctrl(bit));
  106. else
  107. sw_w32_mask(7 << 6, 0, gpios->led_sw_p_ctrl(bit));
  108. }
  109. __asm__ volatile ("sync");
  110. }
  111. void rtl930x_gpio_set(struct gpio_chip *gc, unsigned int offset, int value)
  112. {
  113. pr_debug("rtl838x_set: %d, value: %d\n", offset, value);
  114. /* Internal GPIO of the RTL9300 */
  115. if (value)
  116. rtl83xx_w32_mask(0, BIT(offset), RTL930X_GPIO_PABCD_DAT);
  117. else
  118. rtl83xx_w32_mask(BIT(offset), 0, RTL930X_GPIO_PABCD_DAT);
  119. }
  120. static int rtl838x_direction_input(struct gpio_chip *gc, unsigned int offset)
  121. {
  122. pr_debug("%s: %d\n", __func__, offset);
  123. if (offset < 32) {
  124. rtl83xx_w32_mask(BIT(offset), 0, RTL838X_GPIO_PABC_DIR);
  125. return 0;
  126. }
  127. /* Internal LED driver does not support input */
  128. return -ENOTSUPP;
  129. }
  130. static int rtl930x_direction_input(struct gpio_chip *gc, unsigned int offset)
  131. {
  132. pr_debug("%s: %d\n", __func__, offset);
  133. rtl83xx_w32_mask(BIT(offset), 0, RTL930X_GPIO_PABCD_DIR);
  134. return 0;
  135. }
  136. static int rtl838x_direction_output(struct gpio_chip *gc, unsigned int offset, int value)
  137. {
  138. pr_debug("%s: %d\n", __func__, offset);
  139. if (offset < 32)
  140. rtl83xx_w32_mask(0, BIT(offset), RTL838X_GPIO_PABC_DIR);
  141. rtl930x_gpio_set(gc, offset, value);
  142. /* LED for PWR and SYS driver is direction output by default */
  143. return 0;
  144. }
  145. static int rtl930x_direction_output(struct gpio_chip *gc, unsigned int offset, int value)
  146. {
  147. pr_debug("%s: %d\n", __func__, offset);
  148. rtl83xx_w32_mask(0, BIT(offset), RTL930X_GPIO_PABCD_DIR);
  149. rtl930x_gpio_set(gc, offset, value);
  150. /* LED for PWR and SYS driver is direction output by default */
  151. return 0;
  152. }
  153. static int rtl838x_get_direction(struct gpio_chip *gc, unsigned int offset)
  154. {
  155. u32 v = 0;
  156. pr_debug("%s: %d\n", __func__, offset);
  157. if (offset < 32) {
  158. v = rtl83xx_r32(RTL838X_GPIO_PABC_DIR);
  159. if (v & BIT(offset))
  160. return 0;
  161. return 1;
  162. }
  163. /* LED driver for PWR and SYS is direction output by default */
  164. if (offset >= 32 && offset < 64)
  165. return 0;
  166. return 0;
  167. }
  168. static int rtl930x_get_direction(struct gpio_chip *gc, unsigned int offset)
  169. {
  170. u32 v = 0;
  171. v = rtl83xx_r32(RTL930X_GPIO_PABCD_DIR);
  172. if (v & BIT(offset))
  173. return 0;
  174. return 1;
  175. }
  176. static int rtl838x_gpio_get(struct gpio_chip *gc, unsigned int offset)
  177. {
  178. u32 v;
  179. struct rtl838x_gpios *gpios = gpiochip_get_data(gc);
  180. pr_debug("%s: %d\n", __func__, offset);
  181. /* Internal GPIO of the RTL8380 */
  182. if (offset < 32) {
  183. v = rtl83xx_r32(RTL838X_GPIO_PABC_DATA);
  184. if (v & BIT(offset))
  185. return 1;
  186. return 0;
  187. }
  188. /* LED driver for PWR and SYS */
  189. if (offset >= 32 && offset < 64) {
  190. v = sw_r32(gpios->led_glb_ctrl);
  191. if (v & BIT(offset-32))
  192. return 1;
  193. return 0;
  194. }
  195. return 0;
  196. }
  197. static int rtl930x_gpio_get(struct gpio_chip *gc, unsigned int offset)
  198. {
  199. u32 v = rtl83xx_r32(RTL930X_GPIO_PABCD_DAT);
  200. if (v & BIT(offset))
  201. return 1;
  202. return 0;
  203. }
  204. void rtl8380_led_test(struct rtl838x_gpios *gpios, u32 mask)
  205. {
  206. int i;
  207. u32 led_gbl = sw_r32(gpios->led_glb_ctrl);
  208. u32 mode_sel, led_p_en;
  209. if (soc_info.family == RTL8380_FAMILY_ID) {
  210. mode_sel = sw_r32(RTL838X_LED_MODE_SEL);
  211. led_p_en = sw_r32(RTL838X_LED_P_EN_CTRL);
  212. }
  213. /* 2 Leds for ports 0-23 and 24-27, 3 would be 0x7 */
  214. sw_w32_mask(0x3f, 0x3 | (0x3 << 3), gpios->led_glb_ctrl);
  215. if(soc_info.family == RTL8380_FAMILY_ID) {
  216. /* Enable all leds */
  217. sw_w32(0xFFFFFFF, RTL838X_LED_P_EN_CTRL);
  218. }
  219. /* Enable software control of all leds */
  220. sw_w32(0xFFFFFFF, gpios->led_sw_ctrl);
  221. sw_w32(0xFFFFFFF, gpios->led_sw_p_en_ctrl(0));
  222. sw_w32(0xFFFFFFF, gpios->led_sw_p_en_ctrl(10));
  223. sw_w32(0x0000000, gpios->led_sw_p_en_ctrl(20));
  224. for (i = 0; i < 28; i++) {
  225. if (mask & BIT(i))
  226. sw_w32(5 | (5 << 3) | (5 << 6), gpios->led_sw_p_ctrl(i));
  227. }
  228. msleep(3000);
  229. if (soc_info.family == RTL8380_FAMILY_ID)
  230. sw_w32(led_p_en, RTL838X_LED_P_EN_CTRL);
  231. /* Disable software control of all leds */
  232. sw_w32(0x0000000, gpios->led_sw_ctrl);
  233. sw_w32(0x0000000, gpios->led_sw_p_en_ctrl(0));
  234. sw_w32(0x0000000, gpios->led_sw_p_en_ctrl(10));
  235. sw_w32(0x0000000, gpios->led_sw_p_en_ctrl(20));
  236. sw_w32(led_gbl, gpios->led_glb_ctrl);
  237. if (soc_info.family == RTL8380_FAMILY_ID)
  238. sw_w32(mode_sel, RTL838X_LED_MODE_SEL);
  239. }
  240. void take_port_leds(struct rtl838x_gpios *gpios)
  241. {
  242. int leds_per_port = gpios->leds_per_port;
  243. int mode = gpios->led_mode;
  244. pr_info("%s, %d, %x\n", __func__, leds_per_port, mode);
  245. pr_debug("Bootloader settings: %x %x %x\n",
  246. sw_r32(gpios->led_sw_p_en_ctrl(0)),
  247. sw_r32(gpios->led_sw_p_en_ctrl(10)),
  248. sw_r32(gpios->led_sw_p_en_ctrl(20))
  249. );
  250. if (soc_info.family == RTL8380_FAMILY_ID) {
  251. pr_debug("led glb: %x, sel %x\n",
  252. sw_r32(gpios->led_glb_ctrl), sw_r32(RTL838X_LED_MODE_SEL));
  253. pr_debug("RTL838X_LED_P_EN_CTRL: %x", sw_r32(RTL838X_LED_P_EN_CTRL));
  254. pr_debug("RTL838X_LED_MODE_CTRL: %x", sw_r32(RTL838X_LED_MODE_CTRL));
  255. sw_w32_mask(3, 0, RTL838X_LED_MODE_SEL);
  256. sw_w32(mode, RTL838X_LED_MODE_CTRL);
  257. }
  258. /* Enable software control of all leds */
  259. sw_w32(0xFFFFFFF, gpios->led_sw_ctrl);
  260. if (soc_info.family == RTL8380_FAMILY_ID)
  261. sw_w32(0xFFFFFFF, RTL838X_LED_P_EN_CTRL);
  262. sw_w32(0x0000000, gpios->led_sw_p_en_ctrl(0));
  263. sw_w32(0x0000000, gpios->led_sw_p_en_ctrl(10));
  264. sw_w32(0x0000000, gpios->led_sw_p_en_ctrl(20));
  265. sw_w32_mask(0x3f, 0, gpios->led_glb_ctrl);
  266. switch (leds_per_port) {
  267. case 3:
  268. sw_w32_mask(0, 0x7 | (0x7 << 3), gpios->led_glb_ctrl);
  269. sw_w32(0xFFFFFFF, gpios->led_sw_p_en_ctrl(20));
  270. /* FALLTHRU */
  271. case 2:
  272. sw_w32_mask(0, 0x3 | (0x3 << 3), gpios->led_glb_ctrl);
  273. sw_w32(0xFFFFFFF, gpios->led_sw_p_en_ctrl(10));
  274. /* FALLTHRU */
  275. case 1:
  276. sw_w32_mask(0, 0x1 | (0x1 << 3), gpios->led_glb_ctrl);
  277. sw_w32(0xFFFFFFF, gpios->led_sw_p_en_ctrl(0));
  278. break;
  279. default:
  280. pr_err("No LEDS configured for software control\n");
  281. }
  282. }
  283. static const struct of_device_id rtl838x_gpio_of_match[] = {
  284. { .compatible = "realtek,rtl838x-gpio" },
  285. {},
  286. };
  287. MODULE_DEVICE_TABLE(of, rtl838x_gpio_of_match);
  288. static int rtl838x_gpio_probe(struct platform_device *pdev)
  289. {
  290. struct device *dev = &pdev->dev;
  291. struct device_node *np = dev->of_node;
  292. struct rtl838x_gpios *gpios;
  293. int err;
  294. pr_info("Probing RTL838X GPIOs for %08x\n", soc_info.id);
  295. if (!np) {
  296. dev_err(&pdev->dev, "No DT found\n");
  297. return -EINVAL;
  298. }
  299. gpios = devm_kzalloc(dev, sizeof(*gpios), GFP_KERNEL);
  300. if (!gpios)
  301. return -ENOMEM;
  302. gpios->id = soc_info.id;
  303. switch (gpios->id) {
  304. case 0x8332:
  305. pr_debug("Found RTL8332M GPIO\n");
  306. break;
  307. case 0x8380:
  308. pr_debug("Found RTL8380M GPIO\n");
  309. break;
  310. case 0x8381:
  311. pr_debug("Found RTL8381M GPIO\n");
  312. break;
  313. case 0x8382:
  314. pr_debug("Found RTL8382M GPIO\n");
  315. break;
  316. case 0x8391:
  317. pr_debug("Found RTL8391 GPIO\n");
  318. break;
  319. case 0x8393:
  320. pr_debug("Found RTL8393 GPIO\n");
  321. break;
  322. case 0x9302:
  323. pr_info("Found RTL9302 GPIO\n");
  324. break;
  325. default:
  326. pr_err("Unknown GPIO chip id (%04x)\n", gpios->id);
  327. return -ENODEV;
  328. }
  329. if (soc_info.family == RTL8380_FAMILY_ID) {
  330. gpios->led_glb_ctrl = RTL838X_LED_GLB_CTRL;
  331. gpios->led_sw_ctrl = RTL838X_LED_SW_CTRL;
  332. gpios->led_sw_p_ctrl = rtl838x_led_sw_p_ctrl;
  333. gpios->led_sw_p_en_ctrl = rtl838x_led_sw_p_en_ctrl;
  334. gpios->ext_gpio_dir = rtl838x_ext_gpio_dir;
  335. gpios->ext_gpio_data = rtl838x_ext_gpio_data;
  336. gpios->irq = 31;
  337. }
  338. if (soc_info.family == RTL8390_FAMILY_ID) {
  339. gpios->led_glb_ctrl = RTL839X_LED_GLB_CTRL;
  340. gpios->led_sw_ctrl = RTL839X_LED_SW_CTRL;
  341. gpios->led_sw_p_ctrl = rtl839x_led_sw_p_ctrl;
  342. gpios->led_sw_p_en_ctrl = rtl839x_led_sw_p_en_ctrl;
  343. gpios->ext_gpio_dir = rtl839x_ext_gpio_dir;
  344. gpios->ext_gpio_data = rtl839x_ext_gpio_data;
  345. gpios->irq = 31;
  346. }
  347. if (soc_info.family == RTL9300_FAMILY_ID) {
  348. gpios->irq = 13;
  349. }
  350. gpios->gc.label = "rtl838x";
  351. gpios->gc.parent = dev;
  352. gpios->gc.owner = THIS_MODULE;
  353. gpios->gc.can_sleep = true;
  354. gpios->dev = dev;
  355. gpios->gc.base = 0;
  356. if (soc_info.family != RTL9300_FAMILY_ID) {
  357. /* 0-31: internal
  358. * 32-63, LED control register
  359. * 64-95: PORT-LED 0
  360. * 96-127: PORT-LED 1
  361. * 128-159: PORT-LED 2
  362. */
  363. gpios->gc.ngpio = 160;
  364. gpios->gc.direction_input = rtl838x_direction_input;
  365. gpios->gc.direction_output = rtl838x_direction_output;
  366. gpios->gc.set = rtl838x_gpio_set;
  367. gpios->gc.get = rtl838x_gpio_get;
  368. gpios->gc.get_direction = rtl838x_get_direction;
  369. } else {
  370. gpios->gc.ngpio = 32;
  371. gpios->gc.direction_input = rtl930x_direction_input;
  372. gpios->gc.direction_output = rtl930x_direction_output;
  373. gpios->gc.set = rtl930x_gpio_set;
  374. gpios->gc.get = rtl930x_gpio_get;
  375. gpios->gc.get_direction = rtl930x_get_direction;
  376. }
  377. if (of_property_read_bool(np, "take-port-leds")) {
  378. pr_info("A1\n");
  379. if (of_property_read_u32(np, "leds-per-port", &gpios->leds_per_port))
  380. gpios->leds_per_port = 2;
  381. if (of_property_read_u32(np, "led-mode", &gpios->led_mode))
  382. gpios->led_mode = (0x1ea << 15) | 0x1ea;
  383. if (of_property_read_u32(np, "num-leds", &gpios->num_leds))
  384. gpios->num_leds = 32;
  385. if (of_property_read_u32(np, "min-led", &gpios->min_led))
  386. gpios->min_led = 0;
  387. take_port_leds(gpios);
  388. }
  389. err = devm_gpiochip_add_data(dev, &gpios->gc, gpios);
  390. return err;
  391. }
  392. static struct platform_driver rtl838x_gpio_driver = {
  393. .driver = {
  394. .name = "rtl838x-gpio",
  395. .of_match_table = rtl838x_gpio_of_match,
  396. },
  397. .probe = rtl838x_gpio_probe,
  398. };
  399. module_platform_driver(rtl838x_gpio_driver);
  400. MODULE_DESCRIPTION("Realtek RTL838X GPIO API support");
  401. MODULE_LICENSE("GPL v2");