extcon-max14526.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (c) 2022 Svyatoslav Ryhel <clamor95@gmail.com>
  4. */
  5. #include <common.h>
  6. #include <dm.h>
  7. #include <i2c.h>
  8. #include <linux/delay.h>
  9. #include <linux/err.h>
  10. #include <log.h>
  11. #include <extcon.h>
  12. #include <asm/gpio.h>
  13. #define CONTROL_1 0x01
  14. #define SW_CONTROL 0x03
  15. #define ID_200 0x10
  16. #define ADC_EN 0x02
  17. #define CP_EN 0x01
  18. #define DP_USB 0x00
  19. #define DP_UART 0x08
  20. #define DP_AUDIO 0x10
  21. #define DP_OPEN 0x38
  22. #define DM_USB 0x00
  23. #define DM_UART 0x01
  24. #define DM_AUDIO 0x02
  25. #define DM_OPEN 0x07
  26. #define AP_USB BIT(0)
  27. #define CP_USB BIT(1)
  28. #define CP_UART BIT(2)
  29. struct max14526_priv {
  30. struct gpio_desc usif_gpio;
  31. struct gpio_desc dp2t_gpio;
  32. struct gpio_desc ifx_usb_vbus_gpio;
  33. };
  34. static void max14526_set_mode(struct udevice *dev, int mode)
  35. {
  36. struct max14526_priv *priv = dev_get_priv(dev);
  37. int ret;
  38. if ((mode & AP_USB) || (mode & CP_USB)) {
  39. /* Connect CP UART signals to AP */
  40. ret = dm_gpio_set_value(&priv->usif_gpio, 0);
  41. if (ret)
  42. log_debug("cp-uart > ap failed (%d)\n", ret);
  43. }
  44. if (mode & CP_UART) {
  45. /* Connect CP UART signals to DP2T */
  46. ret = dm_gpio_set_value(&priv->usif_gpio, 1);
  47. if (ret)
  48. log_debug("cp-uart > dp2t failed (%d)\n", ret);
  49. }
  50. if (mode & CP_USB) {
  51. /* Connect CP USB to MUIC UART */
  52. ret = dm_gpio_set_value(&priv->ifx_usb_vbus_gpio, 1);
  53. if (ret)
  54. log_debug("usb-vbus-gpio enable failed (%d)\n", ret);
  55. ret = dm_gpio_set_value(&priv->dp2t_gpio, 1);
  56. if (ret)
  57. log_debug("cp-usb > muic-uart failed (%d)\n", ret);
  58. }
  59. if ((mode & AP_USB) || (mode & CP_UART)) {
  60. /* Connect CP UART to MUIC UART */
  61. ret = dm_gpio_set_value(&priv->dp2t_gpio, 0);
  62. if (ret)
  63. log_debug("cp-uart > muic-uart failed (%d)\n", ret);
  64. }
  65. if (mode & AP_USB) {
  66. /* Enables USB Path */
  67. ret = dm_i2c_reg_write(dev, SW_CONTROL, DP_USB | DM_USB);
  68. if (ret)
  69. log_debug("USB path set failed: %d\n", ret);
  70. }
  71. if ((mode & CP_USB) || (mode & CP_UART)) {
  72. /* Enables UART Path */
  73. ret = dm_i2c_reg_write(dev, SW_CONTROL, DP_UART | DM_UART);
  74. if (ret)
  75. log_debug("UART path set failed: %d\n", ret);
  76. }
  77. /* Enables 200K, Charger Pump, and ADC */
  78. ret = dm_i2c_reg_write(dev, CONTROL_1, ID_200 | ADC_EN | CP_EN);
  79. if (ret)
  80. log_debug("200K, Charger Pump, and ADC set failed: %d\n", ret);
  81. }
  82. static int max14526_probe(struct udevice *dev)
  83. {
  84. struct max14526_priv *priv = dev_get_priv(dev);
  85. int ret, mode = 0;
  86. ret = gpio_request_by_name(dev, "usif-gpios", 0,
  87. &priv->usif_gpio, GPIOD_IS_OUT);
  88. if (ret) {
  89. log_err("could not decode usif-gpios (%d)\n", ret);
  90. return ret;
  91. }
  92. ret = gpio_request_by_name(dev, "dp2t-gpios", 0,
  93. &priv->dp2t_gpio, GPIOD_IS_OUT);
  94. if (ret) {
  95. log_err("could not decode dp2t-gpios (%d)\n", ret);
  96. return ret;
  97. }
  98. if (dev_read_bool(dev, "maxim,ap-usb"))
  99. mode |= AP_USB;
  100. if (dev_read_bool(dev, "maxim,cp-usb")) {
  101. mode |= CP_USB;
  102. ret = gpio_request_by_name(dev, "usb-vbus-gpios", 0,
  103. &priv->ifx_usb_vbus_gpio, GPIOD_IS_OUT);
  104. if (ret) {
  105. log_err("could not decode usb-vbus-gpios (%d)\n", ret);
  106. return ret;
  107. }
  108. }
  109. if (dev_read_bool(dev, "maxim,cp-uart"))
  110. mode |= CP_UART;
  111. max14526_set_mode(dev, mode);
  112. return 0;
  113. }
  114. static const struct udevice_id max14526_ids[] = {
  115. { .compatible = "maxim,max14526-muic" },
  116. { }
  117. };
  118. U_BOOT_DRIVER(extcon_max14526) = {
  119. .name = "extcon_max14526",
  120. .id = UCLASS_EXTCON,
  121. .of_match = max14526_ids,
  122. .probe = max14526_probe,
  123. .priv_auto = sizeof(struct max14526_priv),
  124. };