blob: 77d4686923b9b527e4a1c46375299c5e9baab396 [file] [log] [blame]
/*****************************************************************************
* Copyright Statement:
* --------------------
* This software is protected by Copyright and the information contained
* herein is confidential. The software may not be copied and the information
* contained herein may not be used or disclosed except with the written
* permission of MediaTek Inc. (C) 2017
*
* BY OPENING THIS FILE, BUYER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
* THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
* RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO BUYER ON
* AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT.
* NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE
* SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR
* SUPPLIED WITH THE MEDIATEK SOFTWARE, AND BUYER AGREES TO LOOK ONLY TO SUCH
* THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. MEDIATEK SHALL ALSO
* NOT BE RESPONSIBLE FOR ANY MEDIATEK SOFTWARE RELEASES MADE TO BUYER'S
* SPECIFICATION OR TO CONFORM TO A PARTICULAR STANDARD OR OPEN FORUM.
*
* BUYER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S ENTIRE AND CUMULATIVE
* LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER WILL BE,
* AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE AT ISSUE,
* OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY BUYER TO
* MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
*
* THE TRANSACTION CONTEMPLATED HEREUNDER SHALL BE CONSTRUED IN ACCORDANCE
* WITH THE LAWS OF THE STATE OF CALIFORNIA, USA, EXCLUDING ITS CONFLICT OF
* LAWS PRINCIPLES. ANY DISPUTES, CONTROVERSIES OR CLAIMS ARISING THEREOF AND
* RELATED THERETO SHALL BE SETTLED BY ARBITRATION IN SAN FRANCISCO, CA, UNDER
* THE RULES OF THE INTERNATIONAL CHAMBER OF COMMERCE (ICC).
*
*****************************************************************************/
/*******************************************************************************
*
* Filename:
* ---------
* tia_adc.c
*
* Project:
* --------
* VMOLY
*
* Description:
* ------------
* TIA (Thermal Information Acquisition) driver - adc
*
* Author:
* -------
* -------
*
*============================================================================
* HISTORY
* Below this line, this part is controlled by PVCS VM. DO NOT MODIFY!!
*------------------------------------------------------------------------------
* removed!
* removed!
* removed!
*------------------------------------------------------------------------------
* Upper this line, this part is controlled by PVCS VM. DO NOT MODIFY!!
*============================================================================
****************************************************************************/
#include "dcl.h"
#include "tia.h"
const kal_uint32 tia_adc_rc2k_map[TIA_ADC_RC_MAX+1] = {100, 30, 400, 0};
// NCP15WF104F03RC, NCP03WF104F05RL
static const kal_uint32 tia_adc_tbl[TIA_ADC_TMP_CNT] =
{
0x0043183F, 0x003E73CA, 0x003A2985, 0x00363205, 0x00328684, // -40~-36 'C
0x002F20D7, 0x002BFB5C, 0x002910EE, 0x00265CD9, 0x0023DAD2, // -35~-31 'C
0x002186E9, 0x001F5D86, 0x001D5B5C, 0x001B7D65, 0x0019C0DD, // -30~-26 'C
0x00182339, 0x0016A15C, 0x00153A09, 0x0013EB45, 0x0012B33B, // -25~-21 'C
0x0011903D, 0x001080BF, 0x000F8355, 0x000E96B0, 0x000DB999, // -20~-16 'C
0x000CEAF3, 0x000C29B7, 0x000B74F2, 0x000ACBC4, 0x000A2D5C, // -15~-11 'C
0x000998FC, 0x00090DBE, 0x00088B3B, 0x000810DA, 0x00079E0C, // -10~ -6 'C
0x00073250, 0x0006CD4C, 0x00066E68, 0x0006153C, 0x0005C169, // -5~ -1 'C
0x00057294, 0x00052856, 0x0004E27A, 0x0004A0B7, 0x000462CA, // 0~ 4 'C
0x00042874, 0x0003F17A, 0x0003BDA8, 0x00038CC9, 0x00035EAF, // 5~ 9 'C
0x0003332E, 0x00030A1C, 0x0002E354, 0x0002BEB2, 0x00029C14, // 10~ 14 'C
0x00027B5B, 0x00025C66, 0x00023F20, 0x0002236E, 0x00020938, // 15~ 19 'C
0x0001F068, 0x0001D8EA, 0x0001C2A8, 0x0001AD92, 0x00019994, // 20~ 24 'C
0x000186A0, 0x000174A6, 0x00016398, 0x00015369, 0x0001440C, // 25~ 29 'C
0x00013576, 0x0001279B, 0x00011A72, 0x00010DF0, 0x0001020D, // 30~ 34 'C
0x0000F6BF, 0x0000EBFF, 0x0000E1C5, 0x0000D80A, 0x0000CEC6, // 35~ 39 'C
0x0000C5F5, 0x0000BD90, 0x0000B592, 0x0000ADF5, 0x0000A6B3, // 40~ 44 'C
0x00009FC8, 0x0000992D, 0x000092E1, 0x00008CDF, 0x00008723, // 45~ 49 'C
0x000081AB, 0x00007C73, 0x00007778, 0x000072B6, 0x00006E2B, // 50~ 54 'C
0x000069D3, 0x000065AC, 0x000061B5, 0x00005DEA, 0x00005A49, // 55~ 59 'C
0x000056D0, 0x0000537E, 0x00005051, 0x00004D46, 0x00004A5C, // 60~ 64 'C
0x00004793, 0x000044E8, 0x0000425A, 0x00003FE8, 0x00003D90, // 65~ 69 'C
0x00003B50, 0x00003927, 0x00003715, 0x00003518, 0x00003330, // 70~ 74 'C
0x0000315B, 0x00002F9B, 0x00002DED, 0x00002C50, 0x00002AC3, // 75~ 79 'C
0x00002946, 0x000027D8, 0x00002678, 0x00002526, 0x000023E1, // 80~ 84 'C
0x000022A9, 0x0000217C, 0x0000205B, 0x00001F46, 0x00001E3A, // 85~ 89 'C
0x00001D39, 0x00001C42, 0x00001B55, 0x00001A71, 0x00001994, // 90~ 94 'C
0x000018C1, 0x000017F4, 0x0000172E, 0x00001670, 0x000015B9, // 95~ 99 'C
0x00001508, 0x0000145E, 0x000013BB, 0x0000131D, 0x00001285, // 100~104 'C
0x000011F2, 0x00001165, 0x000010DC, 0x00001058, 0x00000FD9, // 105~109 'C
0x00000F5E, 0x00000EE8, 0x00000E75, 0x00000E07, 0x00000D9C, // 110~114 'C
0x00000D34, 0x00000CD1, 0x00000C71, 0x00000C15, 0x00000BBB, // 115~119 'C
0x00000B64, 0x00000B10, 0x00000ABF, 0x00000A70, 0x00000A24, // 120~124 'C
0x000009DA // 125 'C
};
static struct {
kal_int32 efuse_V18; // 7 bits 2's complement
kal_int32 efuse_Rinb; // 16 bits 2's complement
kal_uint32 V18; // /10^4 V
kal_uint32 Rinb; // *10 ohm
} tia_adc_cal_par;
kal_uint32 tia_adc_to_ohm(tia_adc_rc_type_e rc, kal_uint32 adc)
{
// V = /10^4 volt
// R = *10 ohm || range
// Vin = 1.9 - code / 16384 => ~19000 /10^4 V
// V18 = AUXADC_EFUSE_VAUX18[6:0] * 0.0005 + 1.84 => ~18720 /10^4 V
// Rint = {100K, 30K, 400K} => ~40000 *10 ohm
// Rinb = RG_AUXADC_SDMADC_RIN_BUFFER[15:0] * 10 + 10^6 => ~132767 *10 ohm
// a = (Rint/2 + Rinb) / (Rint + Rinb)
// Rprl = Rint * Rinb / (Rint + Rinb)
// Rntc = Vin * Rprl / (a * V18 - Vin)
// = Vin * Rprl / {[(Rint/2 + Rinb) / (Rint + Rinb)] * V18 - Vin}
// = Vin * Rprl / [(Rint/2 + Rinb) * V18 - (Rint + Rinb) * Vin] * (Rint + Rinb)
const kal_uint32 Rints[] = {10000, 3000, 40000};
kal_int32 T32i;
kal_uint32 T32u, T32u2, Vin, Rint, Rprl, Rntc;
// Vin = 1.9 - code / 16384
T32i = 19000 - adc * 10000 / 16384;
Vin = (T32i < 0)? 0: T32i;
// Rprl = Rint * Rinb / (Rint + Rinb)
Rint = Rints[rc];
Rprl = ((kal_uint64) Rint * tia_adc_cal_par.Rinb) / (Rint + tia_adc_cal_par.Rinb);
// T32u - T32u2 = [(Rint/2 + Rinb) * V18 - (Rint + Rinb) * Vin]
T32u = (Rint / 2 + tia_adc_cal_par.Rinb) * tia_adc_cal_par.V18;
T32u2 = (Rint + tia_adc_cal_par.Rinb) * Vin;
if (T32u <= T32u2) {
Rntc = 0xFFFFFFFF;
} else {
// Rntc = Vin * Rprl / [(Rint/2 + Rinb) * V18 - (Rint + Rinb) * Vin] * (Rint + Rinb)
Rntc = ((kal_uint64) (Vin * Rprl) * (Rint + tia_adc_cal_par.Rinb) * 10/*ohm*/) / (T32u - T32u2);
}
return Rntc;
}
kal_int32 tia_adc_to_tmp(tia_adc_rc_type_e rc, kal_uint32 adc)
{
kal_int32 l = 0, r = sizeof(tia_adc_tbl)/sizeof(tia_adc_tbl[0])-1, m;
kal_uint32 ohm;
if (rc < TIA_ADC_RC_MAX) {
ohm = tia_adc_to_ohm(rc, adc);
} else if (rc == TIA_ADC_RC_FAKE) {
ohm = adc;
} else {
return TIA_ADC_TMP_ERR;
}
if (ohm > tia_adc_tbl[0]) {
return TIA_ADC_TMP_MIN * 10;
}
if (ohm < tia_adc_tbl[r]) {
return TIA_ADC_TMP_MAX * 10;
}
// binary search with inversed order
while (l <= r) {
m = l + (r - l) / 2;
if (tia_adc_tbl[m] == ohm) {
return (m + TIA_ADC_TMP_MIN) * 10;
}
if (tia_adc_tbl[m] < ohm) {
r = m - 1;
} else {
l = m + 1;
}
}
m = (((tia_adc_tbl[r] - ohm) * 100) / (tia_adc_tbl[r] - tia_adc_tbl[l]) + 5/*rounding*/) / 10;
return (r + TIA_ADC_TMP_MIN) * 10 + m;
}
void tia_adc_init(void)
{
kal_uint32 V18 = 0 , Rinb = 0;
#if defined(CHIP10992)
{
DCL_HANDLE hdl;
SPMI_CTRL_EXT_REGISTER_READL cmd = {.type=DCL_MAIN_PMIC, .len=1};
hdl = DclSPMI_Open(DCL_SPMI, FLAGS_NONE);
cmd.addr = 0x11f8; // AUXADC_DIG_3_ELR54 = AUXADC_EFUSE_VAUX18
if (DclSPMI_Control(hdl, EXT_REGISTER_READL, (DCL_CTRL_DATA_T *)&cmd) == STATUS_OK) {
V18 = cmd.value & 0x7F;
}
cmd.addr = 0x11d2; // AUXADC_DIG_3_ELR16 = EFUSE_GAIN_CH12_TRIM_L
if (DclSPMI_Control(hdl, EXT_REGISTER_READL, (DCL_CTRL_DATA_T *)&cmd) == STATUS_OK) {
Rinb |= (cmd.value & 0xFF) << 0;
}
cmd.addr = 0x11d4; // AUXADC_DIG_3_ELR18 = EFUSE_OFFSET_CH12_TRIM_L
if (DclSPMI_Control(hdl, EXT_REGISTER_READL, (DCL_CTRL_DATA_T *)&cmd) == STATUS_OK) {
Rinb |= (cmd.value & 0xFF) << 8;
}
DclSPMI_Close(hdl);
}
#endif
// V18 = AUXADC_EFUSE_VAUX18[6:0] * 0.0005 + 1.84
tia_adc_cal_par.efuse_V18 = (V18 & (1<<6))? (signed char) (V18 | (1<<7)): V18;
tia_adc_cal_par.V18 = tia_adc_cal_par.efuse_V18 * 5 + 18400;
// Rinb = RG_AUXADC_SDMADC_RIN_BUFFER[15:0] * 10 + 10^6
tia_adc_cal_par.efuse_Rinb = (kal_int16) Rinb;
tia_adc_cal_par.Rinb = tia_adc_cal_par.efuse_Rinb * 1 + 100000;
}
void tia_adc_dbg_efuse(kal_int32 *V18, kal_int32 *Rinb)
{
if (V18) *V18 = tia_adc_cal_par.efuse_V18;
if (Rinb) *Rinb = tia_adc_cal_par.efuse_Rinb;
MD_TRC(TIA_MSG_AUXADC_EFUSE, tia_adc_cal_par.efuse_V18, tia_adc_cal_par.efuse_Rinb);
}