tcpc_subpmic.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /* Copyright Statement:
  2. *
  3. * This software/firmware and related documentation ("MediaTek Software") are
  4. * protected under relevant copyright laws. The information contained herein
  5. * is confidential and proprietary to MediaTek Inc. and/or its licensors.
  6. * Without the prior written permission of MediaTek inc. and/or its licensors,
  7. * any reproduction, modification, use or disclosure of MediaTek Software,
  8. * and information contained herein, in whole or in part, shall be strictly prohibited.
  9. */
  10. /* MediaTek Inc. (C) 2019. All rights reserved.
  11. *
  12. * BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
  13. * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
  14. * RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER ON
  15. * AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES,
  16. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT.
  18. * NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE
  19. * SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR
  20. * SUPPLIED WITH THE MEDIATEK SOFTWARE, AND RECEIVER AGREES TO LOOK ONLY TO SUCH
  21. * THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. RECEIVER EXPRESSLY ACKNOWLEDGES
  22. * THAT IT IS RECEIVER'S SOLE RESPONSIBILITY TO OBTAIN FROM ANY THIRD PARTY ALL PROPER LICENSES
  23. * CONTAINED IN MEDIATEK SOFTWARE. MEDIATEK SHALL ALSO NOT BE RESPONSIBLE FOR ANY MEDIATEK
  24. * SOFTWARE RELEASES MADE TO RECEIVER'S SPECIFICATION OR TO CONFORM TO A PARTICULAR
  25. * STANDARD OR OPEN FORUM. RECEIVER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S ENTIRE AND
  26. * CUMULATIVE LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER WILL BE,
  27. * AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE AT ISSUE,
  28. * OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY RECEIVER TO
  29. * MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
  30. *
  31. * The following software/firmware and/or related documentation ("MediaTek Software")
  32. * have been modified by MediaTek Inc. All revisions are subject to any receiver\'s
  33. * applicable license agreements with MediaTek Inc.
  34. */
  35. #include <platform/mt_typedefs.h>
  36. #include <platform/errno.h>
  37. #include <platform/mt_i2c.h>
  38. #include <platform/timer.h>
  39. #include <printf.h>
  40. #include <debug.h>
  41. #include <string.h>
  42. #include "tcpc_subpmic.h"
  43. #if defined(TYPEC_MT6370)
  44. #define MT6370_REG_IDLE_CTRL (0x9B)
  45. #elif defined(TYPEC_MT6360)
  46. #define MT6360_REG_MODE_CTRL2 (0x8F)
  47. #endif
  48. struct subpmic_typec_info {
  49. struct mt_i2c_t i2c;
  50. int i2c_log_level;
  51. };
  52. static struct subpmic_typec_info g_sti = {
  53. .i2c = {
  54. .id = I2C5,
  55. .addr = 0x4E,
  56. .mode = HS_MODE,
  57. .speed = 3400,
  58. .pushpull = true,
  59. },
  60. .i2c_log_level = INFO,
  61. };
  62. static int subpmic_typec_write_byte(struct subpmic_typec_info *sti, u8 addr,
  63. u8 data)
  64. {
  65. struct mt_i2c_t *i2c = &sti->i2c;
  66. u8 write_buf[2] = {addr, data};
  67. int ret = I2C_OK;
  68. ret = i2c_write(i2c, write_buf, 2);
  69. if (ret != I2C_OK)
  70. dprintf(CRITICAL,
  71. "%s: I2CW[0x%02X] = 0x%02X failed, code = %d\n",
  72. __func__, addr, data, ret);
  73. else
  74. dprintf(sti->i2c_log_level, "%s: I2CW[0x%02X] = 0x%02X\n",
  75. __func__, addr, data);
  76. return ret;
  77. }
  78. static int subpmic_typec_read_byte(struct subpmic_typec_info *sti, u8 addr,
  79. u8 *data)
  80. {
  81. struct mt_i2c_t *i2c = &sti->i2c;
  82. u8 rdata = addr;
  83. int ret = I2C_OK;
  84. ret = i2c_write_read(i2c, &rdata, 1, 1);
  85. if (ret != I2C_OK)
  86. dprintf(CRITICAL, "%s: I2CR[0x%02X] failed, code = %d\n",
  87. __func__, addr, ret);
  88. else {
  89. dprintf(sti->i2c_log_level, "%s: I2CR[0x%02X] = 0x%02X\n",
  90. __func__, addr, rdata);
  91. *data = rdata;
  92. }
  93. return ret;
  94. }
  95. static int subpmic_typec_update_bits(struct subpmic_typec_info *sti, u8 addr,
  96. u8 mask, u8 data)
  97. {
  98. u8 rdata = 0;
  99. int ret = 0;
  100. ret = subpmic_typec_read_byte(sti, addr, &rdata);
  101. if (ret != I2C_OK)
  102. return ret;
  103. rdata &= ~mask;
  104. rdata |= (data & mask);
  105. return subpmic_typec_write_byte(sti, addr, rdata);
  106. }
  107. static inline int subpmic_typec_set_bit(struct subpmic_typec_info *sti, u8 addr,
  108. u8 mask)
  109. {
  110. return subpmic_typec_update_bits(sti, addr, mask, mask);
  111. }
  112. static inline int subpmic_typec_clr_bit(struct subpmic_typec_info *sti, u8 addr,
  113. u8 mask)
  114. {
  115. return subpmic_typec_update_bits(sti, addr, mask, 0);
  116. }
  117. int pd_reset_adapter(void)
  118. {
  119. int ret = 0;
  120. dprintf(CRITICAL, "%s: ++\n", __func__);
  121. #if defined(TYPEC_MT6370)
  122. /* CK_300K from 320K, SHIPPING off, AUTOIDLE enable, TIMEOUT = 32ms */
  123. ret = subpmic_typec_write_byte(&g_sti, MT6370_REG_IDLE_CTRL, 0x3A);
  124. #elif defined(TYPEC_MT6360)
  125. /* SHIPPING off, AUTOIDLE off */
  126. ret = subpmic_typec_write_byte(&g_sti, MT6360_REG_MODE_CTRL2, 0x22);
  127. #endif
  128. if (ret < 0) {
  129. dprintf(CRITICAL, "%s: shipping mode off fail\n", __func__);
  130. goto out;
  131. }
  132. /* Set CC (open, open) */
  133. ret = subpmic_typec_write_byte(&g_sti, 0x1A, 0x0F);
  134. if (ret < 0) {
  135. dprintf(CRITICAL, "%s: set cc open fail\n", __func__);
  136. goto out;
  137. }
  138. mdelay(300);
  139. /* SHIPPING on */
  140. #if defined(TYPEC_MT6370)
  141. ret = subpmic_typec_write_byte(&g_sti, MT6370_REG_IDLE_CTRL, 0xC0);
  142. #elif defined(TYPEC_MT6360)
  143. ret = subpmic_typec_write_byte(&g_sti, MT6360_REG_MODE_CTRL2, 0x52);
  144. #endif
  145. if (ret < 0)
  146. dprintf(CRITICAL, "%s: shipping mode on fail\n", __func__);
  147. out:
  148. dprintf(CRITICAL, "%s: --\n", __func__);
  149. return ret;
  150. }