rjw | 6c1fd8f | 2022-11-30 14:33:01 +0800 | [diff] [blame] | 1 | /***************************************************************************** |
| 2 | * Copyright Statement: |
| 3 | * -------------------- |
| 4 | * This software is protected by Copyright and the information contained |
| 5 | * herein is confidential. The software may not be copied and the information |
| 6 | * contained herein may not be used or disclosed except with the written |
| 7 | * permission of MediaTek Inc. (C) 2005 |
| 8 | * |
| 9 | * BY OPENING THIS FILE, BUYER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES |
| 10 | * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE") |
| 11 | * RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO BUYER ON |
| 12 | * AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES, |
| 13 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF |
| 14 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT. |
| 15 | * NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE |
| 16 | * SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR |
| 17 | * SUPPLIED WITH THE MEDIATEK SOFTWARE, AND BUYER AGREES TO LOOK ONLY TO SUCH |
| 18 | * THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. MEDIATEK SHALL ALSO |
| 19 | * NOT BE RESPONSIBLE FOR ANY MEDIATEK SOFTWARE RELEASES MADE TO BUYER'S |
| 20 | * SPECIFICATION OR TO CONFORM TO A PARTICULAR STANDARD OR OPEN FORUM. |
| 21 | * |
| 22 | * BUYER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S ENTIRE AND CUMULATIVE |
| 23 | * LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER WILL BE, |
| 24 | * AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE AT ISSUE, |
| 25 | * OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY BUYER TO |
| 26 | * MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE. |
| 27 | * |
| 28 | * THE TRANSACTION CONTEMPLATED HEREUNDER SHALL BE CONSTRUED IN ACCORDANCE |
| 29 | * WITH THE LAWS OF THE STATE OF CALIFORNIA, USA, EXCLUDING ITS CONFLICT OF |
| 30 | * LAWS PRINCIPLES. ANY DISPUTES, CONTROVERSIES OR CLAIMS ARISING THEREOF AND |
| 31 | * RELATED THERETO SHALL BE SETTLED BY ARBITRATION IN SAN FRANCISCO, CA, UNDER |
| 32 | * THE RULES OF THE INTERNATIONAL CHAMBER OF COMMERCE (ICC). |
| 33 | * |
| 34 | *****************************************************************************/ |
| 35 | |
| 36 | /***************************************************************************** |
| 37 | * |
| 38 | * Filename: |
| 39 | * --------- |
| 40 | * adc.c |
| 41 | * |
| 42 | * Project: |
| 43 | * -------- |
| 44 | * Maui_Software |
| 45 | * |
| 46 | * Description: |
| 47 | * ------------ |
| 48 | * This Module defines the auxiliary ADC driver and adaption layer. |
| 49 | * |
| 50 | * Author: |
| 51 | * ------- |
| 52 | * ------- |
| 53 | * ------- |
| 54 | * |
| 55 | *============================================================================ |
| 56 | * HISTORY |
| 57 | * Below this line, this part is controlled by PVCS VM. DO NOT MODIFY!! |
| 58 | *------------------------------------------------------------------------------ |
| 59 | * removed! |
| 60 | * removed! |
| 61 | * removed! |
| 62 | * |
| 63 | * removed! |
| 64 | * removed! |
| 65 | * removed! |
| 66 | * removed! |
| 67 | * |
| 68 | * removed! |
| 69 | * removed! |
| 70 | * removed! |
| 71 | * |
| 72 | * removed! |
| 73 | * removed! |
| 74 | * removed! |
| 75 | * |
| 76 | * removed! |
| 77 | * removed! |
| 78 | * removed! |
| 79 | * |
| 80 | * removed! |
| 81 | * removed! |
| 82 | * removed! |
| 83 | * |
| 84 | * removed! |
| 85 | * removed! |
| 86 | * removed! |
| 87 | * |
| 88 | * removed! |
| 89 | * removed! |
| 90 | * removed! |
| 91 | * removed! |
| 92 | * removed! |
| 93 | * removed! |
| 94 | * removed! |
| 95 | * removed! |
| 96 | * removed! |
| 97 | * removed! |
| 98 | * removed! |
| 99 | * |
| 100 | * removed! |
| 101 | * removed! |
| 102 | * removed! |
| 103 | * |
| 104 | * removed! |
| 105 | * removed! |
| 106 | * removed! |
| 107 | * |
| 108 | * removed! |
| 109 | * removed! |
| 110 | * removed! |
| 111 | * |
| 112 | * removed! |
| 113 | * removed! |
| 114 | * removed! |
| 115 | * |
| 116 | * removed! |
| 117 | * removed! |
| 118 | * removed! |
| 119 | * |
| 120 | * removed! |
| 121 | * removed! |
| 122 | * removed! |
| 123 | * |
| 124 | * removed! |
| 125 | * removed! |
| 126 | * removed! |
| 127 | * |
| 128 | * removed! |
| 129 | * removed! |
| 130 | * removed! |
| 131 | * |
| 132 | * removed! |
| 133 | * removed! |
| 134 | * removed! |
| 135 | * |
| 136 | * removed! |
| 137 | * removed! |
| 138 | * removed! |
| 139 | * |
| 140 | * removed! |
| 141 | * removed! |
| 142 | * removed! |
| 143 | * |
| 144 | * removed! |
| 145 | * removed! |
| 146 | * removed! |
| 147 | * |
| 148 | * removed! |
| 149 | * removed! |
| 150 | * removed! |
| 151 | * |
| 152 | * removed! |
| 153 | * removed! |
| 154 | * removed! |
| 155 | * |
| 156 | * removed! |
| 157 | * removed! |
| 158 | * removed! |
| 159 | * |
| 160 | * removed! |
| 161 | * removed! |
| 162 | * removed! |
| 163 | * |
| 164 | * removed! |
| 165 | * removed! |
| 166 | * removed! |
| 167 | * |
| 168 | * removed! |
| 169 | * removed! |
| 170 | * removed! |
| 171 | * |
| 172 | * removed! |
| 173 | * removed! |
| 174 | * removed! |
| 175 | * |
| 176 | * removed! |
| 177 | * removed! |
| 178 | * removed! |
| 179 | * |
| 180 | * removed! |
| 181 | * removed! |
| 182 | * removed! |
| 183 | * |
| 184 | * removed! |
| 185 | * removed! |
| 186 | * removed! |
| 187 | * |
| 188 | * removed! |
| 189 | * removed! |
| 190 | * removed! |
| 191 | * |
| 192 | * removed! |
| 193 | * removed! |
| 194 | * removed! |
| 195 | * |
| 196 | * removed! |
| 197 | * removed! |
| 198 | * removed! |
| 199 | * |
| 200 | * removed! |
| 201 | * removed! |
| 202 | * removed! |
| 203 | * |
| 204 | * removed! |
| 205 | * removed! |
| 206 | * removed! |
| 207 | * |
| 208 | * removed! |
| 209 | * removed! |
| 210 | * removed! |
| 211 | * |
| 212 | * removed! |
| 213 | * removed! |
| 214 | * removed! |
| 215 | * |
| 216 | * removed! |
| 217 | * removed! |
| 218 | * removed! |
| 219 | * |
| 220 | * removed! |
| 221 | * removed! |
| 222 | * removed! |
| 223 | * |
| 224 | * removed! |
| 225 | * removed! |
| 226 | * removed! |
| 227 | * |
| 228 | * removed! |
| 229 | * removed! |
| 230 | * removed! |
| 231 | * |
| 232 | * removed! |
| 233 | * removed! |
| 234 | * removed! |
| 235 | * |
| 236 | * removed! |
| 237 | * removed! |
| 238 | * removed! |
| 239 | * |
| 240 | * removed! |
| 241 | * removed! |
| 242 | * removed! |
| 243 | * |
| 244 | * removed! |
| 245 | * removed! |
| 246 | * removed! |
| 247 | * |
| 248 | * removed! |
| 249 | * removed! |
| 250 | * removed! |
| 251 | * |
| 252 | * removed! |
| 253 | * removed! |
| 254 | * removed! |
| 255 | * |
| 256 | * removed! |
| 257 | * removed! |
| 258 | * removed! |
| 259 | * |
| 260 | * removed! |
| 261 | * removed! |
| 262 | * removed! |
| 263 | * |
| 264 | * removed! |
| 265 | * removed! |
| 266 | * removed! |
| 267 | * |
| 268 | * removed! |
| 269 | * removed! |
| 270 | * removed! |
| 271 | * |
| 272 | * removed! |
| 273 | * removed! |
| 274 | * removed! |
| 275 | * |
| 276 | * removed! |
| 277 | * removed! |
| 278 | * removed! |
| 279 | * |
| 280 | * removed! |
| 281 | * removed! |
| 282 | * removed! |
| 283 | * |
| 284 | * removed! |
| 285 | * removed! |
| 286 | * removed! |
| 287 | * |
| 288 | * removed! |
| 289 | * removed! |
| 290 | * removed! |
| 291 | * |
| 292 | * removed! |
| 293 | * removed! |
| 294 | * removed! |
| 295 | * |
| 296 | * removed! |
| 297 | * removed! |
| 298 | * removed! |
| 299 | * |
| 300 | * removed! |
| 301 | * removed! |
| 302 | * removed! |
| 303 | * |
| 304 | * removed! |
| 305 | * removed! |
| 306 | * removed! |
| 307 | * |
| 308 | * removed! |
| 309 | * removed! |
| 310 | * removed! |
| 311 | * |
| 312 | * removed! |
| 313 | * removed! |
| 314 | * removed! |
| 315 | * |
| 316 | * removed! |
| 317 | * removed! |
| 318 | * removed! |
| 319 | * |
| 320 | * removed! |
| 321 | * removed! |
| 322 | * removed! |
| 323 | * |
| 324 | * removed! |
| 325 | * removed! |
| 326 | * removed! |
| 327 | * |
| 328 | * removed! |
| 329 | * removed! |
| 330 | * removed! |
| 331 | * |
| 332 | * removed! |
| 333 | * removed! |
| 334 | * removed! |
| 335 | * removed! |
| 336 | * removed! |
| 337 | * removed! |
| 338 | * removed! |
| 339 | * removed! |
| 340 | * removed! |
| 341 | * removed! |
| 342 | * removed! |
| 343 | * removed! |
| 344 | * removed! |
| 345 | * removed! |
| 346 | * removed! |
| 347 | * removed! |
| 348 | * removed! |
| 349 | * removed! |
| 350 | * removed! |
| 351 | * removed! |
| 352 | * removed! |
| 353 | * removed! |
| 354 | * removed! |
| 355 | * removed! |
| 356 | * removed! |
| 357 | * removed! |
| 358 | * removed! |
| 359 | * removed! |
| 360 | * removed! |
| 361 | * removed! |
| 362 | * removed! |
| 363 | * removed! |
| 364 | * removed! |
| 365 | * removed! |
| 366 | * removed! |
| 367 | * removed! |
| 368 | * removed! |
| 369 | * removed! |
| 370 | * removed! |
| 371 | * removed! |
| 372 | * removed! |
| 373 | * removed! |
| 374 | * removed! |
| 375 | * removed! |
| 376 | * removed! |
| 377 | * removed! |
| 378 | * removed! |
| 379 | * removed! |
| 380 | * removed! |
| 381 | * removed! |
| 382 | * removed! |
| 383 | * removed! |
| 384 | * removed! |
| 385 | * removed! |
| 386 | * removed! |
| 387 | * removed! |
| 388 | * removed! |
| 389 | * removed! |
| 390 | * removed! |
| 391 | * removed! |
| 392 | * removed! |
| 393 | * removed! |
| 394 | * removed! |
| 395 | * removed! |
| 396 | * removed! |
| 397 | * removed! |
| 398 | * removed! |
| 399 | * removed! |
| 400 | * removed! |
| 401 | * removed! |
| 402 | * removed! |
| 403 | * removed! |
| 404 | * removed! |
| 405 | * removed! |
| 406 | * removed! |
| 407 | * removed! |
| 408 | * removed! |
| 409 | * removed! |
| 410 | * removed! |
| 411 | * removed! |
| 412 | * removed! |
| 413 | * removed! |
| 414 | * removed! |
| 415 | * removed! |
| 416 | * removed! |
| 417 | * removed! |
| 418 | * removed! |
| 419 | * removed! |
| 420 | * removed! |
| 421 | * removed! |
| 422 | * removed! |
| 423 | * removed! |
| 424 | * removed! |
| 425 | * removed! |
| 426 | * |
| 427 | *------------------------------------------------------------------------------ |
| 428 | * Upper this line, this part is controlled by PVCS VM. DO NOT MODIFY!! |
| 429 | *============================================================================ |
| 430 | ****************************************************************************/ |
| 431 | #if 0 |
| 432 | /* under construction !*/ |
| 433 | /* under construction !*/ |
| 434 | /* under construction !*/ |
| 435 | /* under construction !*/ |
| 436 | /* under construction !*/ |
| 437 | /* under construction !*/ |
| 438 | #endif |
| 439 | |
| 440 | #include "drv_features.h" |
| 441 | #include "drv_comm.h" |
| 442 | #include "reg_base.h" |
| 443 | #include "adc_hw.h" |
| 444 | #include "adc_sw.h" |
| 445 | #include "kal_public_api.h" |
| 446 | #include "syscomp_config.h" |
| 447 | #include "dcl.h" |
| 448 | |
| 449 | // For RHR ADD |
| 450 | #include "adc_channel.h" |
| 451 | #include "init.h" |
| 452 | |
| 453 | #include "nvram_struct.h" |
| 454 | #include "nvram_enums.h" |
| 455 | // #include "nvram_data_items.h" //violate MSBB rule, also seems to be useless |
| 456 | #include "nvram_interface.h" |
| 457 | #include "adc_nvram_def.h" |
| 458 | |
| 459 | #if !defined(DRV_ADC_OFF) |
| 460 | #include "cust_temp_det_ntc_cfg.h" |
| 461 | |
| 462 | extern kal_bool adc_sync_mode_on; |
| 463 | extern kal_uint16 adc_imm_mode_cnt; |
| 464 | extern kal_uint32 adc_sche_rw_status; |
| 465 | |
| 466 | #define BASE_ADDR_AUXADC (0xB6F2F000) |
| 467 | |
| 468 | #define AUXADC_CON0 (BASE_ADDR_AUXADC + 0x000) |
| 469 | #define AUXADC_CON1 (BASE_ADDR_AUXADC + 0x004) |
| 470 | #define AUXADC_CON1_SET (BASE_ADDR_AUXADC + 0x008) |
| 471 | #define AUXADC_CON1_CLR (BASE_ADDR_AUXADC + 0x00C) |
| 472 | #define AUXADC_CON2 (BASE_ADDR_AUXADC + 0x010) |
| 473 | |
| 474 | #define AUXADC_DAT(n) (BASE_ADDR_AUXADC + 0x014 + (n*4)) |
| 475 | |
| 476 | #define AUXADC_DAT0 (BASE_ADDR_AUXADC + 0x014) |
| 477 | #define AUXADC_DAT1 (BASE_ADDR_AUXADC + 0x018) |
| 478 | #define AUXADC_DAT2 (BASE_ADDR_AUXADC + 0x01C) |
| 479 | #define AUXADC_DAT3 (BASE_ADDR_AUXADC + 0x020) |
| 480 | #define AUXADC_DAT4 (BASE_ADDR_AUXADC + 0x024) |
| 481 | #define AUXADC_DAT5 (BASE_ADDR_AUXADC + 0x028) |
| 482 | #define AUXADC_DET_VOLT (BASE_ADDR_AUXADC + 0x084) |
| 483 | #define AUXADC_DET_SEL (BASE_ADDR_AUXADC + 0x088) |
| 484 | #define AUXADC_DET_PERIOD (BASE_ADDR_AUXADC + 0x08C) |
| 485 | #define AUXADC_DET_DEBT (BASE_ADDR_AUXADC + 0x090) |
| 486 | #define AUXADC_MISC (BASE_ADDR_AUXADC + 0x094) |
| 487 | #define AUXADC_SAMPLE_LIST (BASE_ADDR_AUXADC + 0x09C) |
| 488 | #define AUXADC_TST (BASE_ADDR_AUXADC + 0x0A4) |
| 489 | #define AUXADC_SPL_EN (BASE_ADDR_AUXADC + 0x0B0) |
| 490 | #define AUXADC_SPL_CFG0 (BASE_ADDR_AUXADC + 0x0B4) |
| 491 | #define AUXADC_SPL_CFG1 (BASE_ADDR_AUXADC + 0x0B8) |
| 492 | #define AUXADC_SPL_CFG2 (BASE_ADDR_AUXADC + 0x0BC) |
| 493 | #define AUXADC_SADC_CON (BASE_ADDR_AUXADC + 0x300) |
| 494 | |
| 495 | extern const kal_uint8 ADC_VBAT; |
| 496 | extern const kal_uint8 ADC_VISENSE; |
| 497 | extern const kal_uint8 ADC_VCHARGER; |
| 498 | |
| 499 | #define ADC_AUTO_SAMPLE_MODE_EN /* to enable auto sample mode */ |
| 500 | |
| 501 | #if 0 |
| 502 | /* under construction !*/ |
| 503 | /* under construction !*/ |
| 504 | /* under construction !*/ |
| 505 | /* under construction !*/ |
| 506 | /* under construction !*/ |
| 507 | #endif |
| 508 | |
| 509 | adc_customized_struct adc_customized_para = |
| 510 | { |
| 511 | ADC_ISENSE_FACTOR, |
| 512 | { |
| 513 | { |
| 514 | #if(ADC_MAX_CHANNEL >= 1) |
| 515 | ADC_CALIBRATION_SLOPE_CH0 |
| 516 | #endif |
| 517 | #if(ADC_MAX_CHANNEL >= 2) |
| 518 | ,ADC_CALIBRATION_SLOPE_CH1 |
| 519 | #endif |
| 520 | #if(ADC_MAX_CHANNEL >= 3) |
| 521 | ,ADC_CALIBRATION_SLOPE_CH2 |
| 522 | #endif |
| 523 | #if(ADC_MAX_CHANNEL >= 4) |
| 524 | ,ADC_CALIBRATION_SLOPE_CH3 |
| 525 | #endif |
| 526 | #if(ADC_MAX_CHANNEL >= 5) |
| 527 | ,ADC_CALIBRATION_SLOPE_CH4 |
| 528 | #endif |
| 529 | #if(ADC_MAX_CHANNEL >= 6) |
| 530 | ,ADC_CALIBRATION_SLOPE_CH5 |
| 531 | #endif |
| 532 | }, |
| 533 | { |
| 534 | #if(ADC_MAX_CHANNEL >= 1) |
| 535 | ADC_CALIBRATION_OFFSET_CH0 |
| 536 | #endif |
| 537 | #if(ADC_MAX_CHANNEL >= 2) |
| 538 | ,ADC_CALIBRATION_OFFSET_CH1 |
| 539 | #endif |
| 540 | #if(ADC_MAX_CHANNEL >= 3) |
| 541 | ,ADC_CALIBRATION_OFFSET_CH2 |
| 542 | #endif |
| 543 | #if(ADC_MAX_CHANNEL >= 4) |
| 544 | ,ADC_CALIBRATION_OFFSET_CH3 |
| 545 | #endif |
| 546 | #if(ADC_MAX_CHANNEL >= 5) |
| 547 | ,ADC_CALIBRATION_OFFSET_CH4 |
| 548 | #endif |
| 549 | } |
| 550 | }, |
| 551 | { |
| 552 | #if(ADC_MAX_CHANNEL >= 1) |
| 553 | ADC_VOLT_FACTOR_CH0 |
| 554 | #endif |
| 555 | #if(ADC_MAX_CHANNEL >= 2) |
| 556 | ,ADC_VOLT_FACTOR_CH1 |
| 557 | #endif |
| 558 | #if(ADC_MAX_CHANNEL >= 3) |
| 559 | ,ADC_VOLT_FACTOR_CH2 |
| 560 | #endif |
| 561 | #if(ADC_MAX_CHANNEL >= 4) |
| 562 | ,ADC_VOLT_FACTOR_CH3 |
| 563 | #endif |
| 564 | #if(ADC_MAX_CHANNEL >= 5) |
| 565 | ,ADC_VOLT_FACTOR_CH4 |
| 566 | #endif |
| 567 | } |
| 568 | }; |
| 569 | |
| 570 | typedef struct { |
| 571 | kal_uint32 Channel; |
| 572 | kal_uint32 Pull_Vdd; |
| 573 | kal_uint32 Pull_Res; |
| 574 | NTC_RT *Rt_Tbl; |
| 575 | kal_uint32 Rt_Tbl_Ent; |
| 576 | } TMP_DET_SETTING; |
| 577 | |
| 578 | TMP_DET_SETTING auxadc_tmp_det_setting[2]; /*= { |
| 579 | {Pmic_Ntc_ADC_Chan, Pmic_Ntc_Pull_Vdd, Pmic_Ntc_Pull_Res, Pmic_Ntc_Rt_Table, Pmic_Ntc_Rt_Table_Ent}, |
| 580 | {Asic_Ntc_ADC_Chan, Asic_Ntc_Pull_Vdd, Asic_Ntc_Pull_Res, Asic_Ntc_Rt_Table, Asic_Ntc_Rt_Table_Ent}, |
| 581 | };*/ |
| 582 | |
| 583 | kal_uint32 auxadc_tmp_det_setting_ent = sizeof(auxadc_tmp_det_setting)/sizeof(auxadc_tmp_det_setting[0]); |
| 584 | |
| 585 | |
| 586 | /* |
| 587 | * FUNCTION |
| 588 | * adc_read_calibration_data_inform |
| 589 | * |
| 590 | * DESCRIPTION |
| 591 | * This function is to set adc calibration data from NVRAM |
| 592 | * |
| 593 | * CALLS |
| 594 | * |
| 595 | * PARAMETERS |
| 596 | * lim_ptr: adc calibration data, send from NVRAM |
| 597 | * |
| 598 | * RETURNS |
| 599 | * void |
| 600 | * |
| 601 | * GLOBALS AFFECTED |
| 602 | * None |
| 603 | */ |
| 604 | void adc_read_calibration_data(void) |
| 605 | { |
| 606 | #if 0 |
| 607 | /* under construction !*/ |
| 608 | /* under construction !*/ |
| 609 | /* under construction !*/ |
| 610 | /* under construction !*/ |
| 611 | /* under construction !*/ |
| 612 | /* under construction !*/ |
| 613 | /* under construction !*/ |
| 614 | /* under construction !*/ |
| 615 | /* under construction !*/ |
| 616 | /* under construction !*/ |
| 617 | /* under construction !*/ |
| 618 | /* under construction !*/ |
| 619 | /* under construction !*/ |
| 620 | /* under construction !*/ |
| 621 | /* under construction !*/ |
| 622 | /* under construction !*/ |
| 623 | /* under construction !*/ |
| 624 | /* under construction !*/ |
| 625 | /* under construction !*/ |
| 626 | #else |
| 627 | int i; |
| 628 | |
| 629 | /* No calibration needed, so ... */ |
| 630 | for (i = 0; i < ADC_MAX_CHANNEL; i++) { |
| 631 | adc_customized_para.adc_cali_param.ADCSlope[i] = 1800000/4096; |
| 632 | adc_customized_para.adc_cali_param.ADCOffset[i] = 0; |
| 633 | } |
| 634 | #endif |
| 635 | } |
| 636 | |
| 637 | |
| 638 | /* |
| 639 | * FUNCTION |
| 640 | * ADC_GetData |
| 641 | * |
| 642 | * DESCRIPTION |
| 643 | * This function is to run ADC, and obtain the measured data |
| 644 | * |
| 645 | * CALLS |
| 646 | * |
| 647 | * PARAMETERS |
| 648 | * sel : selection of input signal source(0-4) |
| 649 | * |
| 650 | * RETURNS |
| 651 | * return the value of ADC output |
| 652 | * |
| 653 | * GLOBALS AFFECTED |
| 654 | * external_global |
| 655 | */ |
| 656 | /*must add power on function before this function*/ |
| 657 | #define ADC_MEA_TIMES 5 |
| 658 | #define ADC_MEA_TMO_MS 20 /* 16T(4~5us) per channel */ |
| 659 | kal_uint16 ADC_GetData(kal_uint8 chIdx) |
| 660 | { |
| 661 | volatile kal_uint32 auxData[ADC_MEA_TIMES], sum = 0; |
| 662 | kal_uint32 i; |
| 663 | volatile kal_uint32 start_time; |
| 664 | |
| 665 | #if defined(ADC_AUTO_SAMPLE_MODE_EN) |
| 666 | for (i = 0; i < ADC_MEA_TIMES; i++) { |
| 667 | start_time = drv_get_current_time(); |
| 668 | while(1) |
| 669 | { |
| 670 | /* Get ADC data */ |
| 671 | auxData[i] = DRV_Reg32(AUXADC_DAT(chIdx)); |
| 672 | |
| 673 | /* Check if RDY bit is set */ |
| 674 | if ((auxData[i] & (1 << 12))) { |
| 675 | break; |
| 676 | } |
| 677 | |
| 678 | /* According to Designer's comment, RDY will be set after 16T */ |
| 679 | if (drv_get_duration_ms(start_time) > ADC_MEA_TMO_MS) { |
| 680 | tst_sys_trace("Error: Wait ADC Data RDY Timeout!!!\r\n"); |
| 681 | break; |
| 682 | } |
| 683 | } |
| 684 | |
| 685 | auxData[i] &= ~(1<<12); |
| 686 | sum += auxData[i]; |
| 687 | } |
| 688 | #else |
| 689 | /* Select the ADC channel */ |
| 690 | DRV_SetBits32(AUXADC_CON1, (1 << chIdx)); |
| 691 | |
| 692 | for (i = 0; i < ADC_MEA_TIMES; i++) { |
| 693 | /* Set the immediate mode */ |
| 694 | DRV_SetBits32(AUXADC_CON1_SET, (1 << chIdx)); |
| 695 | |
| 696 | /* Get ADC data */ |
| 697 | start_time = drv_get_current_time(); |
| 698 | auxData[i] = DRV_Reg32(AUXADC_DAT(chIdx)); |
| 699 | while((!(auxData[i] & (1 << 12))) && (drv_get_duration_ms(start_time) <= ADC_MEA_TMO_MS)) |
| 700 | { |
| 701 | auxData[i] = DRV_Reg32(AUXADC_DAT(chIdx)); |
| 702 | } |
| 703 | auxData[i] &= ~(1<<12); |
| 704 | sum += auxData[i]; |
| 705 | |
| 706 | /* Clear the immediate mode */ |
| 707 | DRV_SetBits32(AUXADC_CON1_CLR, (1 << chIdx)); |
| 708 | } |
| 709 | |
| 710 | /* De-select the ADC channel */ |
| 711 | DRV_ClearBits32(AUXADC_CON1, (1 << chIdx)); |
| 712 | #endif |
| 713 | |
| 714 | sum = sum / ADC_MEA_TIMES; |
| 715 | |
| 716 | dbg_print("[ADC] %s: chn = %d, result = %X\r\n", __FUNCTION__, chIdx, sum); |
| 717 | return (kal_uint16)sum; |
| 718 | } |
| 719 | |
| 720 | /* |
| 721 | * FUNCTION |
| 722 | * ADC_GetMeaData |
| 723 | * |
| 724 | * DESCRIPTION |
| 725 | * This function is to run ADC, and obtain the average value of measured data |
| 726 | * Note that: This function only called before other tasks are running. |
| 727 | * |
| 728 | * CALLS |
| 729 | * |
| 730 | * PARAMETERS |
| 731 | * sel : selection of input signal source(0-4) |
| 732 | * meacount: measure count |
| 733 | * |
| 734 | * RETURNS |
| 735 | * return the average value of ADC output |
| 736 | * |
| 737 | * GLOBALS AFFECTED |
| 738 | * None |
| 739 | */ |
| 740 | kal_uint32 ADC_GetMeaData(kal_uint8 sel, kal_uint16 meacount) |
| 741 | { |
| 742 | kal_uint32 index; |
| 743 | kal_uint32 sum = 0; |
| 744 | |
| 745 | for(index = meacount; index != 0; index--) |
| 746 | { |
| 747 | sum += (kal_uint32)ADC_GetData(sel); |
| 748 | } |
| 749 | |
| 750 | return sum; |
| 751 | } |
| 752 | |
| 753 | /* |
| 754 | * FUNCTION |
| 755 | * ADC_GetData2Meta |
| 756 | * |
| 757 | * DESCRIPTION |
| 758 | * This function is called by META, and return the sum value. |
| 759 | * |
| 760 | * CALLS |
| 761 | * |
| 762 | * PARAMETERS |
| 763 | * sel : selection of input signal source(0-4) |
| 764 | * meacount: measure count |
| 765 | * |
| 766 | * RETURNS |
| 767 | * return the average value of ADC output |
| 768 | * |
| 769 | * GLOBALS AFFECTED |
| 770 | * None |
| 771 | */ |
| 772 | kal_uint32 ADC_GetData2Meta(kal_uint8 sel, kal_uint16 meacount) |
| 773 | { |
| 774 | kal_uint32 index; |
| 775 | kal_uint32 sum = 0; |
| 776 | |
| 777 | for(index = meacount; index != 0; index--) |
| 778 | { |
| 779 | sum += (kal_uint32)ADC_GetData(sel); |
| 780 | } |
| 781 | |
| 782 | dbg_print("[ADC] %s: %X\r\n", __FUNCTION__, sum); |
| 783 | |
| 784 | return sum; |
| 785 | } |
| 786 | |
| 787 | void ADC_Init(void) |
| 788 | { |
| 789 | /* Enable source clock */ |
| 790 | DRV_ClearBits32(0xB6F2D208, (1 << 0)); |
| 791 | |
| 792 | /* Change AUXADC from 1.5V to 1.8V */ |
| 793 | DRV_WriteReg32(0xB6F2F300, DRV_Reg32(0xB6F2F300)|(1<<12)); |
| 794 | |
| 795 | #if defined(ADC_AUTO_SAMPLE_MODE_EN) |
| 796 | /* Enable auto-sample mode */ |
| 797 | DRV_WriteReg32(AUXADC_CON0, 0x3F); |
| 798 | |
| 799 | /* Enable immediate mode */ |
| 800 | DRV_WriteReg32(AUXADC_CON1, 0x3F); |
| 801 | #endif |
| 802 | |
| 803 | /* Initilize global variable */ |
| 804 | auxadc_tmp_det_setting[0].Channel = Pmic_Ntc_ADC_Chan; |
| 805 | auxadc_tmp_det_setting[0].Pull_Vdd = Pmic_Ntc_Pull_Vdd; |
| 806 | auxadc_tmp_det_setting[0].Pull_Res = Pmic_Ntc_Pull_Res; |
| 807 | auxadc_tmp_det_setting[0].Rt_Tbl = Pmic_Ntc_Rt_Table; |
| 808 | auxadc_tmp_det_setting[0].Rt_Tbl_Ent= Pmic_Ntc_Rt_Table_Ent; |
| 809 | |
| 810 | auxadc_tmp_det_setting[1].Channel = Asic_Ntc_ADC_Chan; |
| 811 | auxadc_tmp_det_setting[1].Pull_Vdd = Asic_Ntc_Pull_Vdd; |
| 812 | auxadc_tmp_det_setting[1].Pull_Res = Asic_Ntc_Pull_Res; |
| 813 | auxadc_tmp_det_setting[1].Rt_Tbl = Asic_Ntc_Rt_Table; |
| 814 | auxadc_tmp_det_setting[1].Rt_Tbl_Ent= Asic_Ntc_Rt_Table_Ent; |
| 815 | } |
| 816 | |
| 817 | /* |
| 818 | * FUNCTION |
| 819 | * adc_adc2vol |
| 820 | * |
| 821 | * DESCRIPTION |
| 822 | * This function is to translate ADC value to voltage.(uV) |
| 823 | * |
| 824 | * CALLS |
| 825 | * |
| 826 | * PARAMETERS |
| 827 | * chann: ADC channel. |
| 828 | * adcVoltage: ADC value |
| 829 | * |
| 830 | * RETURNS |
| 831 | * voltage value(uV) |
| 832 | * |
| 833 | * GLOBALS AFFECTED |
| 834 | * None |
| 835 | */ |
| 836 | kal_int32 adc_adc2vol(kal_uint8 chann, double adcVoltage) |
| 837 | { |
| 838 | double result; |
| 839 | |
| 840 | if(chann >= ADC_MAX_CHANNEL) |
| 841 | ASSERT(0); |
| 842 | |
| 843 | /* Need get the Adc voltage firstly, or does the cali-data contain the conversion ??? */ |
| 844 | |
| 845 | /* Do Calibration */ |
| 846 | result = (double)((adcVoltage*(double)adc_customized_para.adc_cali_param.ADCSlope[chann]) + |
| 847 | (double)adc_customized_para.adc_cali_param.ADCOffset[chann]); |
| 848 | |
| 849 | return (kal_int32)result; |
| 850 | //return (kal_int32)(result/PMIC_ADC_FACTOR_VBAT*volt_factor); |
| 851 | } |
| 852 | |
| 853 | /* |
| 854 | * FUNCTION |
| 855 | * adc_measureVoltage |
| 856 | * |
| 857 | * DESCRIPTION |
| 858 | * This function is to measure voltage of specific channel |
| 859 | * |
| 860 | * CALLS |
| 861 | * Call this API after Drv_Init_Phase1() -->Drv_Customize_Init() --> Finish adc_customized_para initial |
| 862 | * |
| 863 | * PARAMETERS |
| 864 | * chann: ADC channel. |
| 865 | * |
| 866 | * RETURNS |
| 867 | * voltage value(uV) |
| 868 | * |
| 869 | * GLOBALS AFFECTED |
| 870 | * None |
| 871 | */ |
| 872 | kal_int32 adc_measureVoltage(kal_uint8 chann, double *adc_value) |
| 873 | { |
| 874 | *adc_value = (double)ADC_GetData(chann); |
| 875 | return adc_adc2vol(chann, *adc_value); |
| 876 | } |
| 877 | |
| 878 | /* |
| 879 | * FUNCTION |
| 880 | * ADC_Volt2Temp |
| 881 | * |
| 882 | * DESCRIPTION |
| 883 | * This function is to translate voltage to temperatrure |
| 884 | * |
| 885 | * CALLS |
| 886 | * |
| 887 | * PARAMETERS |
| 888 | * chIdx : AUXADC Channel |
| 889 | * uVolt : Calibrated Voltage Value, unit (uV) |
| 890 | * |
| 891 | * RETURNS |
| 892 | * temperature(m'C) |
| 893 | * |
| 894 | * GLOBALS AFFECTED |
| 895 | * None |
| 896 | */ |
| 897 | kal_int32 ADC_Volt2Temp(kal_uint8 chIdx, kal_uint32 uVolt) |
| 898 | { |
| 899 | TMP_DET_SETTING *setting = (TMP_DET_SETTING *)NULL; |
| 900 | NTC_RT *rt1 = (NTC_RT *)NULL; |
| 901 | NTC_RT *rt2 = (NTC_RT *)NULL; |
| 902 | double slope; |
| 903 | double ntc_res; |
| 904 | kal_int32 ntc_tmp = 0; |
| 905 | int i; |
| 906 | |
| 907 | for (i = 0; i < auxadc_tmp_det_setting_ent; i++) { |
| 908 | setting = &auxadc_tmp_det_setting[i]; |
| 909 | if (setting->Channel == chIdx) |
| 910 | break; |
| 911 | } |
| 912 | |
| 913 | if (i == auxadc_tmp_det_setting_ent) { |
| 914 | /* No setting found */ |
| 915 | return 0; |
| 916 | } |
| 917 | |
| 918 | ASSERT(uVolt <= setting->Pull_Vdd); |
| 919 | |
| 920 | if (uVolt < setting->Pull_Vdd) { |
| 921 | ntc_res = ((double)uVolt)*setting->Pull_Res/(setting->Pull_Vdd-uVolt); |
| 922 | } else { |
| 923 | /* pull up too small, or temperature too low */ |
| 924 | ntc_res = (double)0x0FFFFFFF; /* 268M ohm */ |
| 925 | } |
| 926 | |
| 927 | ASSERT(setting->Rt_Tbl_Ent >= 2); |
| 928 | |
| 929 | for (i = 0; i < setting->Rt_Tbl_Ent-1; i++) { |
| 930 | rt1 = &setting->Rt_Tbl[i]; /* lower temp, bigger res */ |
| 931 | rt2 = &setting->Rt_Tbl[i+1]; /* higher temp, smaller res */ |
| 932 | |
| 933 | if (ntc_res >= rt1->uRes) /* temp too low */ |
| 934 | break; |
| 935 | |
| 936 | if (rt2->uRes <= ntc_res) /* temp in range */ |
| 937 | break; |
| 938 | } |
| 939 | |
| 940 | ASSERT(rt2->iTemp > rt1->iTemp); |
| 941 | ASSERT(rt1->uRes > rt2->uRes); |
| 942 | |
| 943 | /* slope should be negative */ |
| 944 | slope = ((double)(rt1->iTemp - rt2->iTemp)) / ((double)(rt1->uRes - rt2->uRes)); |
| 945 | |
| 946 | ntc_tmp = rt2->iTemp*1000 + (kal_int32)(slope*(ntc_res - rt2->uRes)*1000); |
| 947 | |
| 948 | return ntc_tmp; |
| 949 | } |
| 950 | |
| 951 | |
| 952 | /* |
| 953 | * FUNCTION |
| 954 | * ADC_GetImmTemp |
| 955 | * |
| 956 | * DESCRIPTION |
| 957 | * This function is to get immediate temperature of specific channel |
| 958 | * |
| 959 | * CALLS |
| 960 | * adc_measureVoltage |
| 961 | * adc_Volt2Temp |
| 962 | * |
| 963 | * PARAMETERS |
| 964 | * chann: ADC channel. |
| 965 | * |
| 966 | * RETURNS |
| 967 | * immediate temperature(C) |
| 968 | * |
| 969 | * GLOBALS AFFECTED |
| 970 | * None |
| 971 | */ |
| 972 | kal_int16 ADC_GetImmTemp(kal_uint8 chann) |
| 973 | { |
| 974 | double adc_value; |
| 975 | kal_uint32 uVolt; |
| 976 | |
| 977 | |
| 978 | ASSERT(chann < ADC_MAX_CHANNEL); |
| 979 | |
| 980 | uVolt = adc_measureVoltage(chann, &adc_value); |
| 981 | |
| 982 | return ADC_Volt2Temp(chann, uVolt)/1000; |
| 983 | } |
| 984 | |
| 985 | |
| 986 | /* |
| 987 | * FUNCTION |
| 988 | * ADC_GetPhysicalChannel |
| 989 | * |
| 990 | * DESCRIPTION |
| 991 | * This function is to get physical AUXADC Channel ID |
| 992 | * |
| 993 | * CALLS |
| 994 | * |
| 995 | * |
| 996 | * PARAMETERS |
| 997 | * adcname: ADC channel name. |
| 998 | * |
| 999 | * RETURNS |
| 1000 | * <0 means error |
| 1001 | * >=0 means AUXADC Channel ID |
| 1002 | * |
| 1003 | * GLOBALS AFFECTED |
| 1004 | * None |
| 1005 | */ |
| 1006 | kal_int16 ADC_GetPhysicalChannel(kal_uint16 adcname) |
| 1007 | { |
| 1008 | kal_int16 iPhyChan = -1; |
| 1009 | |
| 1010 | if (adcname == DCL_MDTMP_ADC_CHANNEL) { |
| 1011 | iPhyChan = Asic_Ntc_ADC_Chan; |
| 1012 | } else if (adcname == DCL_PMICTMP_ADC_CHANNEL) { |
| 1013 | iPhyChan = Pmic_Ntc_ADC_Chan; |
| 1014 | } else { |
| 1015 | iPhyChan = -1; |
| 1016 | //ASSERT(0); |
| 1017 | } |
| 1018 | |
| 1019 | return iPhyChan; |
| 1020 | } |
| 1021 | |
| 1022 | #else /* !defined(DRV_ADC_OFF) */ |
| 1023 | |
| 1024 | #endif |
| 1025 | |