mt6360_pmic.c 5.1 KB

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