mt6360_pmic.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 <platform/mt_i2c.h>
  36. #include <printf.h>
  37. #include <debug.h>
  38. #include <string.h>
  39. #define CRC8_TABLE_SIZE (256)
  40. static u8 crc8_table[CRC8_TABLE_SIZE];
  41. struct mt6360_pmic_info {
  42. struct mt_i2c_t i2c;
  43. int i2c_log_level;
  44. };
  45. static struct mt6360_pmic_info g_mpi = {
  46. .i2c = {
  47. .id = I2C5,
  48. .addr = 0x1a,
  49. .mode = FS_MODE,
  50. .speed = 400,
  51. },
  52. .i2c_log_level = INFO,
  53. };
  54. static void crc8_populate_msb(u8 table[CRC8_TABLE_SIZE], u8 polynomial)
  55. {
  56. int i, j;
  57. const u8 msbit = 0x80;
  58. u8 t = msbit;
  59. table[0] = 0;
  60. for (i = 1; i < CRC8_TABLE_SIZE; i *= 2) {
  61. t = (t << 1) ^ (t & msbit ? polynomial : 0);
  62. for (j = 0; j < i; j++)
  63. table[i+j] = table[j]^t;
  64. }
  65. }
  66. static u8 crc8(const u8 table[CRC8_TABLE_SIZE], u8 *pdata, size_t nbytes, u8 crc)
  67. {
  68. while (nbytes-- > 0)
  69. crc = table[(crc ^ *pdata++) & 0xff];
  70. return crc;
  71. }
  72. /* ========================= */
  73. /* I2C operations */
  74. /* ========================= */
  75. static int mt6360_pmic_write_byte(struct mt6360_pmic_info *mpi, u8 addr, u8 data)
  76. {
  77. struct mt_i2c_t *i2c = &mpi->i2c;
  78. int ret = I2C_OK;
  79. int len = 1;
  80. u8 chunk[8] = {0};
  81. if ((addr & 0xc0) != 0) {
  82. dprintf(CRITICAL, "%s: not support addr [%x]\n", __func__, addr);
  83. return -EINVAL;
  84. }
  85. chunk[0] = (i2c->addr & 0x7f) << 1;
  86. chunk[1] = (addr & 0x3f)| ((len - 1) << 6);
  87. memcpy(chunk + 2, &data, len);
  88. chunk[2+len] = crc8(crc8_table, chunk, 2+len, 0);
  89. ret = i2c_write(i2c, chunk+1, len+3);
  90. if (ret != I2C_OK)
  91. dprintf(CRITICAL,
  92. "%s: I2CW[0x%02X] = 0x%02X failed, ret = %d\n",
  93. __func__, addr, data, ret);
  94. else
  95. dprintf(mpi->i2c_log_level, "%s: I2CW[0x%02X] = 0x%02X\n",
  96. __func__, addr, data);
  97. return ret;
  98. }
  99. static int mt6360_pmic_read_byte(struct mt6360_pmic_info *mpi, u8 addr, u8 *data)
  100. {
  101. struct mt_i2c_t *i2c = &mpi->i2c;
  102. int ret = I2C_OK;
  103. int len = 1;
  104. u8 chunk[8] = {0};
  105. u8 temp[2] = {0};
  106. chunk[0] = ((i2c->addr & 0x7f) << 1) + 1;
  107. chunk[1] = (addr & 0x3f) | ((len - 1) << 6);
  108. temp[0] = chunk[1];
  109. ret = i2c_write_read(i2c, &temp[0], 1, len + 1);
  110. if (ret != I2C_OK) {
  111. dprintf(CRITICAL, "%s: I2CR[0x%02X] failed, ret = %d\n",
  112. __func__, addr, ret);
  113. return ret;
  114. }
  115. chunk[2] = temp[0];
  116. chunk[3] = temp[1];
  117. chunk[7] = crc8(crc8_table, chunk, 2 + len, 0);
  118. if (chunk[2+len] != chunk[7]) {
  119. dprintf(CRITICAL, "%s: crc check fail\n", __func__);
  120. return -EINVAL;
  121. }
  122. dprintf(mpi->i2c_log_level, "%s: I2CR[0x%02X] = 0x%02X\n",
  123. __func__, addr, chunk[2]);
  124. memcpy(data, chunk+2, len);
  125. return ret;
  126. }
  127. /* ========================= */
  128. /* GLOBAL operations */
  129. /* ========================= */
  130. int mt6360_pmic_enable_poweroff_seq(bool en)
  131. {
  132. int i, ret = 0;
  133. u8 seq_delay[4] = { 0x06, 0x04, 0x00, 0x02 };
  134. dprintf(CRITICAL, "%s: en = %d\n", __func__, en);
  135. for (i = 0; i < 4; i++) {
  136. ret = mt6360_pmic_write_byte(&g_mpi, 0x07 + i,
  137. en ? seq_delay[i] : 0x00);
  138. if (ret < 0) {
  139. dprintf(CRITICAL,
  140. "%s: set buck(%d) fail\n", __func__, i);
  141. return ret;
  142. }
  143. }
  144. return 0;
  145. }
  146. int mt6360_pmic_i2c_probe(void)
  147. {
  148. dprintf(CRITICAL, "%s: ++\n", __func__);
  149. crc8_populate_msb(crc8_table, 0x7);
  150. dprintf(CRITICAL, "%s: --\n", __func__);
  151. return 0;
  152. }