2
0

gpio-rtl838x.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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. static int rtl838x_direction_input(struct gpio_chip *gc, unsigned int offset)
  112. {
  113. pr_debug("%s: %d\n", __func__, offset);
  114. if (offset < 32) {
  115. rtl83xx_w32_mask(BIT(offset), 0, RTL838X_GPIO_PABC_DIR);
  116. return 0;
  117. }
  118. /* Internal LED driver does not support input */
  119. return -ENOTSUPP;
  120. }
  121. static int rtl838x_direction_output(struct gpio_chip *gc, unsigned int offset, int value)
  122. {
  123. pr_debug("%s: %d\n", __func__, offset);
  124. if (offset < 32)
  125. rtl83xx_w32_mask(0, BIT(offset), RTL838X_GPIO_PABC_DIR);
  126. rtl838x_gpio_set(gc, offset, value);
  127. /* LED for PWR and SYS driver is direction output by default */
  128. return 0;
  129. }
  130. static int rtl838x_get_direction(struct gpio_chip *gc, unsigned int offset)
  131. {
  132. u32 v = 0;
  133. pr_debug("%s: %d\n", __func__, offset);
  134. if (offset < 32) {
  135. v = rtl83xx_r32(RTL838X_GPIO_PABC_DIR);
  136. if (v & BIT(offset))
  137. return 0;
  138. return 1;
  139. }
  140. /* LED driver for PWR and SYS is direction output by default */
  141. if (offset >= 32 && offset < 64)
  142. return 0;
  143. return 0;
  144. }
  145. static int rtl838x_gpio_get(struct gpio_chip *gc, unsigned int offset)
  146. {
  147. u32 v;
  148. struct rtl838x_gpios *gpios = gpiochip_get_data(gc);
  149. pr_debug("%s: %d\n", __func__, offset);
  150. /* Internal GPIO of the RTL8380 */
  151. if (offset < 32) {
  152. v = rtl83xx_r32(RTL838X_GPIO_PABC_DATA);
  153. if (v & BIT(offset))
  154. return 1;
  155. return 0;
  156. }
  157. /* LED driver for PWR and SYS */
  158. if (offset >= 32 && offset < 64) {
  159. v = sw_r32(gpios->led_glb_ctrl);
  160. if (v & BIT(offset-32))
  161. return 1;
  162. return 0;
  163. }
  164. /* BUG:
  165. bit = (offset - 64) % 32;
  166. if (offset >= 64 && offset < 96) {
  167. if (sw_r32(RTL838X_LED1_SW_P_EN_CTRL) & BIT(bit))
  168. return 1;
  169. return 0;
  170. }
  171. if (offset >= 96 && offset < 128) {
  172. if (sw_r32(RTL838X_LED1_SW_P_EN_CTRL) & BIT(bit))
  173. return 1;
  174. return 0;
  175. }
  176. if (offset >= 128 && offset < 160) {
  177. if (sw_r32(RTL838X_LED1_SW_P_EN_CTRL) & BIT(bit))
  178. return 1;
  179. return 0;
  180. }
  181. */
  182. return 0;
  183. }
  184. void rtl8380_led_test(struct rtl838x_gpios *gpios, u32 mask)
  185. {
  186. int i;
  187. u32 led_gbl = sw_r32(gpios->led_glb_ctrl);
  188. u32 mode_sel, led_p_en;
  189. if (soc_info.family == RTL8380_FAMILY_ID) {
  190. mode_sel = sw_r32(RTL838X_LED_MODE_SEL);
  191. led_p_en = sw_r32(RTL838X_LED_P_EN_CTRL);
  192. }
  193. /* 2 Leds for ports 0-23 and 24-27, 3 would be 0x7 */
  194. sw_w32_mask(0x3f, 0x3 | (0x3 << 3), gpios->led_glb_ctrl);
  195. if(soc_info.family == RTL8380_FAMILY_ID) {
  196. /* Enable all leds */
  197. sw_w32(0xFFFFFFF, RTL838X_LED_P_EN_CTRL);
  198. }
  199. /* Enable software control of all leds */
  200. sw_w32(0xFFFFFFF, gpios->led_sw_ctrl);
  201. sw_w32(0xFFFFFFF, gpios->led_sw_p_en_ctrl(0));
  202. sw_w32(0xFFFFFFF, gpios->led_sw_p_en_ctrl(10));
  203. sw_w32(0x0000000, gpios->led_sw_p_en_ctrl(20));
  204. for (i = 0; i < 28; i++) {
  205. if (mask & BIT(i))
  206. sw_w32(5 | (5 << 3) | (5 << 6), gpios->led_sw_p_ctrl(i));
  207. }
  208. msleep(3000);
  209. if (soc_info.family == RTL8380_FAMILY_ID)
  210. sw_w32(led_p_en, RTL838X_LED_P_EN_CTRL);
  211. /* Disable software control of all leds */
  212. sw_w32(0x0000000, gpios->led_sw_ctrl);
  213. sw_w32(0x0000000, gpios->led_sw_p_en_ctrl(0));
  214. sw_w32(0x0000000, gpios->led_sw_p_en_ctrl(10));
  215. sw_w32(0x0000000, gpios->led_sw_p_en_ctrl(20));
  216. sw_w32(led_gbl, gpios->led_glb_ctrl);
  217. if (soc_info.family == RTL8380_FAMILY_ID)
  218. sw_w32(mode_sel, RTL838X_LED_MODE_SEL);
  219. }
  220. void take_port_leds(struct rtl838x_gpios *gpios)
  221. {
  222. int leds_per_port = gpios->leds_per_port;
  223. int mode = gpios->led_mode;
  224. pr_info("%s, %d, %x\n", __func__, leds_per_port, mode);
  225. pr_debug("Bootloader settings: %x %x %x\n",
  226. sw_r32(gpios->led_sw_p_en_ctrl(0)),
  227. sw_r32(gpios->led_sw_p_en_ctrl(10)),
  228. sw_r32(gpios->led_sw_p_en_ctrl(20))
  229. );
  230. if (soc_info.family == RTL8380_FAMILY_ID) {
  231. pr_debug("led glb: %x, sel %x\n",
  232. sw_r32(gpios->led_glb_ctrl), sw_r32(RTL838X_LED_MODE_SEL));
  233. pr_debug("RTL838X_LED_P_EN_CTRL: %x", sw_r32(RTL838X_LED_P_EN_CTRL));
  234. pr_debug("RTL838X_LED_MODE_CTRL: %x", sw_r32(RTL838X_LED_MODE_CTRL));
  235. sw_w32_mask(3, 0, RTL838X_LED_MODE_SEL);
  236. sw_w32(mode, RTL838X_LED_MODE_CTRL);
  237. }
  238. /* Enable software control of all leds */
  239. sw_w32(0xFFFFFFF, gpios->led_sw_ctrl);
  240. if (soc_info.family == RTL8380_FAMILY_ID)
  241. sw_w32(0xFFFFFFF, RTL838X_LED_P_EN_CTRL);
  242. sw_w32(0x0000000, gpios->led_sw_p_en_ctrl(0));
  243. sw_w32(0x0000000, gpios->led_sw_p_en_ctrl(10));
  244. sw_w32(0x0000000, gpios->led_sw_p_en_ctrl(20));
  245. sw_w32_mask(0x3f, 0, gpios->led_glb_ctrl);
  246. switch (leds_per_port) {
  247. case 3:
  248. sw_w32_mask(0, 0x7 | (0x7 << 3), gpios->led_glb_ctrl);
  249. sw_w32(0xFFFFFFF, gpios->led_sw_p_en_ctrl(20));
  250. /* FALLTHRU */
  251. case 2:
  252. sw_w32_mask(0, 0x3 | (0x3 << 3), gpios->led_glb_ctrl);
  253. sw_w32(0xFFFFFFF, gpios->led_sw_p_en_ctrl(10));
  254. /* FALLTHRU */
  255. case 1:
  256. sw_w32_mask(0, 0x1 | (0x1 << 3), gpios->led_glb_ctrl);
  257. sw_w32(0xFFFFFFF, gpios->led_sw_p_en_ctrl(0));
  258. break;
  259. default:
  260. pr_err("No LEDS configured for software control\n");
  261. }
  262. }
  263. static const struct of_device_id rtl838x_gpio_of_match[] = {
  264. { .compatible = "realtek,rtl838x-gpio" },
  265. {},
  266. };
  267. MODULE_DEVICE_TABLE(of, rtl838x_gpio_of_match);
  268. static int rtl838x_gpio_probe(struct platform_device *pdev)
  269. {
  270. struct device *dev = &pdev->dev;
  271. struct device_node *np = dev->of_node;
  272. struct rtl838x_gpios *gpios;
  273. int err;
  274. pr_info("Probing RTL838X GPIOs\n");
  275. if (!np) {
  276. dev_err(&pdev->dev, "No DT found\n");
  277. return -EINVAL;
  278. }
  279. gpios = devm_kzalloc(dev, sizeof(*gpios), GFP_KERNEL);
  280. if (!gpios)
  281. return -ENOMEM;
  282. gpios->id = soc_info.id;
  283. switch (gpios->id) {
  284. case 0x8332:
  285. pr_debug("Found RTL8332M GPIO\n");
  286. break;
  287. case 0x8380:
  288. pr_debug("Found RTL8380M GPIO\n");
  289. break;
  290. case 0x8381:
  291. pr_debug("Found RTL8381M GPIO\n");
  292. break;
  293. case 0x8382:
  294. pr_debug("Found RTL8382M GPIO\n");
  295. break;
  296. case 0x8391:
  297. pr_debug("Found RTL8391 GPIO\n");
  298. break;
  299. case 0x8393:
  300. pr_debug("Found RTL8393 GPIO\n");
  301. break;
  302. default:
  303. pr_err("Unknown GPIO chip id (%04x)\n", gpios->id);
  304. return -ENODEV;
  305. }
  306. if (soc_info.family == RTL8380_FAMILY_ID) {
  307. gpios->led_glb_ctrl = gpios->led_glb_ctrl;
  308. gpios->led_sw_ctrl = RTL838X_LED_SW_CTRL;
  309. gpios->led_sw_p_ctrl = rtl838x_led_sw_p_ctrl;
  310. gpios->led_sw_p_en_ctrl = rtl838x_led_sw_p_en_ctrl;
  311. gpios->ext_gpio_dir = rtl838x_ext_gpio_dir;
  312. gpios->ext_gpio_data = rtl838x_ext_gpio_data;
  313. }
  314. if (soc_info.family == RTL8390_FAMILY_ID) {
  315. gpios->led_glb_ctrl = RTL839X_LED_GLB_CTRL;
  316. gpios->led_sw_ctrl = RTL839X_LED_SW_CTRL;
  317. gpios->led_sw_p_ctrl = rtl839x_led_sw_p_ctrl;
  318. gpios->led_sw_p_en_ctrl = rtl839x_led_sw_p_en_ctrl;
  319. gpios->ext_gpio_dir = rtl839x_ext_gpio_dir;
  320. gpios->ext_gpio_data = rtl839x_ext_gpio_data;
  321. }
  322. gpios->dev = dev;
  323. gpios->gc.base = 0;
  324. /* 0-31: internal
  325. * 32-63, LED control register
  326. * 64-95: PORT-LED 0
  327. * 96-127: PORT-LED 1
  328. * 128-159: PORT-LED 2
  329. */
  330. gpios->gc.ngpio = 160;
  331. gpios->gc.label = "rtl838x";
  332. gpios->gc.parent = dev;
  333. gpios->gc.owner = THIS_MODULE;
  334. gpios->gc.can_sleep = true;
  335. gpios->irq = 31;
  336. gpios->gc.direction_input = rtl838x_direction_input;
  337. gpios->gc.direction_output = rtl838x_direction_output;
  338. gpios->gc.set = rtl838x_gpio_set;
  339. gpios->gc.get = rtl838x_gpio_get;
  340. gpios->gc.get_direction = rtl838x_get_direction;
  341. if (of_property_read_bool(np, "take-port-leds")) {
  342. if (of_property_read_u32(np, "leds-per-port", &gpios->leds_per_port))
  343. gpios->leds_per_port = 2;
  344. if (of_property_read_u32(np, "led-mode", &gpios->led_mode))
  345. gpios->led_mode = (0x1ea << 15) | 0x1ea;
  346. if (of_property_read_u32(np, "num-leds", &gpios->num_leds))
  347. gpios->num_leds = 32;
  348. if (of_property_read_u32(np, "min-led", &gpios->min_led))
  349. gpios->min_led = 0;
  350. take_port_leds(gpios);
  351. }
  352. err = devm_gpiochip_add_data(dev, &gpios->gc, gpios);
  353. return err;
  354. }
  355. static struct platform_driver rtl838x_gpio_driver = {
  356. .driver = {
  357. .name = "rtl838x-gpio",
  358. .of_match_table = rtl838x_gpio_of_match,
  359. },
  360. .probe = rtl838x_gpio_probe,
  361. };
  362. module_platform_driver(rtl838x_gpio_driver);
  363. MODULE_DESCRIPTION("Realtek RTL838X GPIO API support");
  364. MODULE_LICENSE("GPL v2");