mt6362.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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,
  9. * shall be strictly prohibited.
  10. */
  11. /* MediaTek Inc. (C) 2015. All rights reserved.
  12. *
  13. * BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
  14. * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
  15. * RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER ON
  16. * AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES,
  17. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT.
  19. * NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE
  20. * SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR
  21. * SUPPLIED WITH THE MEDIATEK SOFTWARE, AND RECEIVER AGREES TO LOOK ONLY TO SUCH
  22. * THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. RECEIVER EXPRESSLY
  23. * ACKNOWLEDGES THAT IT IS RECEIVER'S SOLE RESPONSIBILITY TO OBTAIN FROM ANY
  24. * THIRD PARTY ALL PROPER LICENSES CONTAINED IN MEDIATEK SOFTWARE. MEDIATEK SHALL
  25. * ALSO NOT BE RESPONSIBLE FOR ANY MEDIATEK SOFTWARE RELEASES MADE TO RECEIVER'S
  26. * SPECIFICATION OR TO CONFORM TO A PARTICULAR STANDARD OR OPEN FORUM.
  27. * RECEIVER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S ENTIRE AND CUMULATIVE
  28. * LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER WILL BE,
  29. * AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE AT ISSUE,
  30. * OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY RECEIVER TO
  31. * MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
  32. */
  33. #include <platform/mt_typedefs.h>
  34. #include <platform/errno.h>
  35. #include <printf.h>
  36. #include <debug.h>
  37. #include <string.h>
  38. #include <platform/spmi.h>
  39. #include <platform/spmi_sw.h>
  40. #include <mtk_subpmic.h>
  41. #define U32_MAX 0xFFFFFFFF
  42. #define MT6362_REG_BUCK1_SEQOFFDLY (0x107)
  43. struct mt6362_data {
  44. struct spmi_device *sdev;
  45. u32 last_access_addr;
  46. u32 last_access_tick;
  47. };
  48. static struct mt6362_data g_data;
  49. static int mt6362_write_byte(struct mt6362_data *mtdata, u32 addr, u8 data)
  50. {
  51. struct spmi_device *sdev = mtdata->sdev;
  52. int ret;
  53. ret = spmi_ext_register_writel(sdev, addr, &data, 1);
  54. if (ret < 0)
  55. return ret;
  56. mtdata->last_access_addr = addr;
  57. mtdata->last_access_tick = gpt4_get_current_tick();
  58. return 0;
  59. }
  60. static int mt6362_read_byte(struct mt6362_data *mtdata, u32 addr, u8 *data)
  61. {
  62. struct spmi_device *sdev = mtdata->sdev;
  63. u32 avail_access_tick, current_tick, rest_tick;
  64. if (addr == mtdata->last_access_addr) {
  65. avail_access_tick =
  66. mtdata->last_access_tick + gpt4_time2tick_us(3);
  67. current_tick = gpt4_get_current_tick();
  68. if (avail_access_tick > current_tick) {
  69. rest_tick = avail_access_tick - current_tick;
  70. udelay(gpt4_tick2time_us(rest_tick));
  71. }
  72. }
  73. return spmi_ext_register_readl(sdev, addr, data, 1);
  74. }
  75. int mtk_subpmic_read_interface(u32 addr, u8 *data, u8 mask, u8 shift)
  76. {
  77. int ret;
  78. u8 rdata = 0;
  79. ret = mt6362_read_byte(&g_data, addr, &rdata);
  80. if (ret < 0) {
  81. dprintf(CRITICAL, "%s: fail(%d), addr:0x%x\n",
  82. __func__, ret, addr);
  83. return ret;
  84. }
  85. rdata &= (mask << shift);
  86. *data = (rdata >> shift);
  87. return 0;
  88. }
  89. int mtk_subpmic_config_interface(u32 addr, u8 data, u8 mask, u8 shift)
  90. {
  91. int ret;
  92. u8 org = 0;
  93. ret = mt6362_read_byte(&g_data, addr, &org);
  94. if (ret < 0) {
  95. dprintf(CRITICAL, "%s: fail(%d), addr:0x%x\n",
  96. __func__, ret, addr);
  97. return ret;
  98. }
  99. org &= ~(mask << shift);
  100. org |= (data << shift);
  101. return mt6362_write_byte(&g_data, addr, org);
  102. }
  103. int mtk_subpmic_enable_poweroff_sequence(bool en)
  104. {
  105. int ret;
  106. unsigned int i;
  107. u8 seq_delay[9] = {0x24, 0x24, 0x04, 0x22, 0x00, 0x00,
  108. 0x00, 0x02, 0x04};
  109. dprintf(CRITICAL, "%s: en = %d\n", __func__, en);
  110. /* Set sub-pmic buck1/2/3/4/5/6&ldo7/6/4 power off seuqence */
  111. for (i = 0; i < ARRAY_SIZE(seq_delay); i++) {
  112. ret = mt6362_write_byte(&g_data,
  113. MT6362_REG_BUCK1_SEQOFFDLY + i,
  114. en ? seq_delay[i] : 0x00);
  115. if (ret < 0) {
  116. dprintf(CRITICAL,
  117. "%s: set seq(%d) fail\n", __func__, i);
  118. return ret;
  119. }
  120. }
  121. return ret;
  122. }
  123. int mtk_subpmic_init(void)
  124. {
  125. int ret = 0;
  126. dprintf(CRITICAL, "%s\n", __func__);
  127. g_data.sdev = get_spmi_device(SPMI_MASTER_0, SPMI_SLAVE_9);
  128. if (!g_data.sdev) {
  129. dprintf(CRITICAL, "%s: get spmi device fail\n", __func__);
  130. return -ENODEV;
  131. }
  132. g_data.last_access_addr = U32_MAX;
  133. return ret;
  134. }