1 /* 2 * (C) Copyright 2017 Rockchip Electronics Co., Ltd 3 * 4 * SPDX-License-Identifier: GPL-2.0+ 5 */ 6 7 #include <asm/io.h> 8 #include <common.h> 9 #include <boot_rkimg.h> 10 #include <console.h> 11 #include <dm.h> 12 #include <errno.h> 13 #include <key.h> 14 #include <led.h> 15 #include <rtc.h> 16 #include <pwm.h> 17 #include <asm/arch/rockchip_smccc.h> 18 #include <asm/suspend.h> 19 #include <linux/input.h> 20 #include <power/charge_display.h> 21 #include <power/charge_animation.h> 22 #include <power/rockchip_pm.h> 23 #include <power/fuel_gauge.h> 24 #include <power/pmic.h> 25 #include <power/rk8xx_pmic.h> 26 #include <power/regulator.h> 27 #include <video_rockchip.h> 28 #ifdef CONFIG_IRQ 29 #include <irq-generic.h> 30 #include <rk_timer_irq.h> 31 #endif 32 33 DECLARE_GLOBAL_DATA_PTR; 34 35 #define IMAGE_RESET_IDX -1 36 #define IMAGE_SOC_100_IDX(n) ((n) - 2) 37 #define IMAGE_LOWPOWER_IDX(n) ((n) - 1) 38 #define SYSTEM_SUSPEND_DELAY_MS 5000 39 #define FUEL_GAUGE_POLL_MS 1000 40 41 #define LED_CHARGING_NAME "battery_charging" 42 #define LED_CHARGING_FULL_NAME "battery_full" 43 44 struct charge_image { 45 const char *name; 46 int soc; 47 int period; /* ms */ 48 }; 49 50 struct charge_animation_priv { 51 struct udevice *pmic; 52 struct udevice *fg; 53 struct udevice *charger; 54 struct udevice *rtc; 55 #ifdef CONFIG_LED 56 struct udevice *led_charging; 57 struct udevice *led_full; 58 #endif 59 const struct charge_image *image; 60 int image_num; 61 62 int auto_wakeup_key_state; 63 ulong auto_screen_off_timeout; /* ms */ 64 ulong suspend_delay_timeout; /* ms */ 65 }; 66 67 /* 68 * IF you want to use your own charge images, please: 69 * 70 * 1. Update the following 'image[]' to point to your own images; 71 * 2. You must set the failed image as last one and soc = -1 !!! 72 */ 73 static const struct charge_image image[] = { 74 { .name = "battery_0.bmp", .soc = 5, .period = 600 }, 75 { .name = "battery_1.bmp", .soc = 20, .period = 600 }, 76 { .name = "battery_2.bmp", .soc = 40, .period = 600 }, 77 { .name = "battery_3.bmp", .soc = 60, .period = 600 }, 78 { .name = "battery_4.bmp", .soc = 80, .period = 600 }, 79 { .name = "battery_5.bmp", .soc = 100, .period = 600 }, 80 { .name = "battery_fail.bmp", .soc = -1, .period = 1000 }, 81 }; 82 83 static int charge_animation_ofdata_to_platdata(struct udevice *dev) 84 { 85 struct charge_animation_pdata *pdata = dev_get_platdata(dev); 86 87 /* charge mode */ 88 pdata->uboot_charge = 89 dev_read_u32_default(dev, "rockchip,uboot-charge-on", 0); 90 pdata->android_charge = 91 dev_read_u32_default(dev, "rockchip,android-charge-on", 0); 92 93 pdata->exit_charge_level = 94 dev_read_u32_default(dev, "rockchip,uboot-exit-charge-level", 0); 95 pdata->exit_charge_voltage = 96 dev_read_u32_default(dev, "rockchip,uboot-exit-charge-voltage", 0); 97 98 pdata->low_power_voltage = 99 dev_read_u32_default(dev, "rockchip,uboot-low-power-voltage", 0); 100 101 pdata->screen_on_voltage = 102 dev_read_u32_default(dev, "rockchip,screen-on-voltage", 0); 103 pdata->system_suspend = 104 dev_read_u32_default(dev, "rockchip,system-suspend", 0); 105 106 pdata->auto_wakeup_interval = 107 dev_read_u32_default(dev, "rockchip,auto-wakeup-interval", 0); 108 pdata->auto_wakeup_screen_invert = 109 dev_read_u32_default(dev, "rockchip,auto-wakeup-screen-invert", 0); 110 111 pdata->auto_off_screen_interval = 112 dev_read_u32_default(dev, "rockchip,auto-off-screen-interval", 15); 113 114 if (pdata->screen_on_voltage > pdata->exit_charge_voltage) 115 pdata->screen_on_voltage = pdata->exit_charge_voltage; 116 117 debug("mode: uboot=%d, android=%d; exit: soc=%d%%, voltage=%dmv;\n" 118 "lp_voltage=%d%%, screen_on=%dmv\n", 119 pdata->uboot_charge, pdata->android_charge, 120 pdata->exit_charge_level, pdata->exit_charge_voltage, 121 pdata->low_power_voltage, pdata->screen_on_voltage); 122 123 return 0; 124 } 125 126 static int check_key_press(struct udevice *dev) 127 { 128 struct charge_animation_pdata *pdata = dev_get_platdata(dev); 129 struct charge_animation_priv *priv = dev_get_priv(dev); 130 u32 state, rtc_state = 0; 131 132 #ifdef CONFIG_DM_RTC 133 if (priv->rtc) 134 rtc_state = rtc_alarm_trigger(priv->rtc); 135 #endif 136 if (rtc_state) { 137 printf("rtc alarm trigger...\n"); 138 return KEY_PRESS_LONG_DOWN; 139 } 140 141 state = key_read(KEY_POWER); 142 if (state < 0) 143 printf("read power key failed: %d\n", state); 144 else if (state == KEY_PRESS_DOWN) 145 printf("power key pressed...\n"); 146 else if (state == KEY_PRESS_LONG_DOWN) 147 printf("power key long pressed...\n"); 148 149 /* Fixup key state for following cases */ 150 if (pdata->auto_wakeup_interval) { 151 if (pdata->auto_wakeup_screen_invert) { 152 if (priv->auto_wakeup_key_state == KEY_PRESS_DOWN) { 153 /* Value is updated in timer interrupt */ 154 priv->auto_wakeup_key_state = KEY_PRESS_NONE; 155 state = KEY_PRESS_DOWN; 156 } 157 } 158 } else if (pdata->auto_off_screen_interval) { 159 if (priv->auto_screen_off_timeout && 160 get_timer(priv->auto_screen_off_timeout) > 161 pdata->auto_off_screen_interval * 1000) { /* 1000ms */ 162 state = KEY_PRESS_DOWN; 163 printf("Auto screen off\n"); 164 } 165 } 166 167 return state; 168 } 169 170 /* 171 * If not enable CONFIG_IRQ, cpu can't suspend to ATF or wfi, so that wakeup 172 * period timer is useless. 173 */ 174 #if !defined(CONFIG_IRQ) || !defined(CONFIG_ARM_CPU_SUSPEND) 175 static int system_suspend_enter(struct udevice *dev) 176 { 177 return 0; 178 } 179 180 static void autowakeup_timer_init(struct udevice *dev, uint32_t seconds) {} 181 static void autowakeup_timer_uninit(void) {} 182 183 #else 184 static int system_suspend_enter(struct udevice *dev) 185 { 186 struct charge_animation_pdata *pdata = dev_get_platdata(dev); 187 struct charge_animation_priv *priv = dev_get_priv(dev); 188 189 /* 190 * When cpu is in wfi and we try to give a long key press event without 191 * key release, cpu would wakeup and enter wfi again immediately. So 192 * here is the problem: cpu can only wakeup when long key released. 193 * 194 * Actually, we want cpu can detect long key event without key release, 195 * so we give a suspend delay timeout for cpu to detect this. 196 */ 197 if (priv->suspend_delay_timeout && 198 get_timer(priv->suspend_delay_timeout) <= SYSTEM_SUSPEND_DELAY_MS) 199 return 0; 200 201 if (pdata->system_suspend && IS_ENABLED(CONFIG_ARM_SMCCC)) { 202 printf("\nSystem suspend: "); 203 putc('0'); 204 regulators_enable_state_mem(false); 205 putc('1'); 206 local_irq_disable(); 207 putc('2'); 208 irqs_suspend(); 209 putc('3'); 210 device_suspend(); 211 putc('4'); 212 putc('\n'); 213 214 /* Trap into ATF for low power mode */ 215 cpu_suspend(0, psci_system_suspend); 216 217 putc('\n'); 218 putc('4'); 219 device_resume(); 220 putc('3'); 221 irqs_resume(); 222 putc('2'); 223 local_irq_enable(); 224 putc('1'); 225 putc('\n'); 226 } else { 227 printf("\nWfi\n"); 228 wfi(); 229 putc('1'); 230 } 231 232 priv->suspend_delay_timeout = get_timer(0); 233 234 /* 235 * We must wait for key release event finish, otherwise 236 * we may read key state too early. 237 */ 238 mdelay(300); 239 240 return 0; 241 } 242 243 static void timer_irq_handler(int irq, void *data) 244 { 245 struct udevice *dev = data; 246 struct charge_animation_priv *priv = dev_get_priv(dev); 247 static long long count; 248 249 writel(TIMER_CLR_INT, TIMER_BASE + TIMER_INTSTATUS); 250 251 priv->auto_wakeup_key_state = KEY_PRESS_DOWN; 252 printf("auto wakeup count: %lld\n", ++count); 253 } 254 255 static void autowakeup_timer_init(struct udevice *dev, uint32_t seconds) 256 { 257 uint64_t period = 24000000ULL * seconds; 258 259 /* Disable before conifg */ 260 writel(0, TIMER_BASE + TIMER_CTRL); 261 262 /* Config */ 263 writel((uint32_t)period, TIMER_BASE + TIMER_LOAD_COUNT0); 264 writel((uint32_t)(period >> 32), TIMER_BASE + TIMER_LOAD_COUNT1); 265 writel(TIMER_CLR_INT, TIMER_BASE + TIMER_INTSTATUS); 266 writel(TIMER_EN | TIMER_INT_EN, TIMER_BASE + TIMER_CTRL); 267 268 /* IRQ */ 269 irq_install_handler(TIMER_IRQ, timer_irq_handler, dev); 270 irq_handler_enable(TIMER_IRQ); 271 } 272 273 static void autowakeup_timer_uninit(void) 274 { 275 irq_free_handler(TIMER_IRQ); 276 } 277 #endif 278 279 #ifdef CONFIG_DRM_ROCKCHIP 280 static void charge_show_bmp(const char *name) 281 { 282 rockchip_show_bmp(name); 283 } 284 285 static void charge_show_logo(void) 286 { 287 rockchip_show_logo(); 288 } 289 #else 290 static void charge_show_bmp(const char *name) {} 291 static void charge_show_logo(void) {} 292 #endif 293 294 #ifdef CONFIG_LED 295 static int leds_update(struct udevice *dev, int soc) 296 { 297 struct charge_animation_priv *priv = dev_get_priv(dev); 298 static int old_soc = -1; 299 int ret, ledst; 300 301 if (old_soc == soc) 302 return 0; 303 304 old_soc = soc; 305 if (priv->led_charging) { 306 ledst = (soc < 100) ? LEDST_ON : LEDST_OFF; 307 ret = led_set_state(priv->led_charging, ledst); 308 if (ret) { 309 printf("set charging led %s failed, ret=%d\n", 310 (ledst == LEDST_ON) ? "ON" : "OFF", ret); 311 return ret; 312 } 313 } 314 315 if (priv->led_full) { 316 ledst = (soc == 100) ? LEDST_ON : LEDST_OFF; 317 ret = led_set_state(priv->led_full, ledst); 318 if (ret) { 319 printf("set charging full led %s failed, ret=%d\n", 320 ledst == LEDST_ON ? "ON" : "OFF", ret); 321 return ret; 322 } 323 } 324 325 return 0; 326 } 327 #else 328 static int leds_update(struct udevice *dev, int soc) { return 0; } 329 #endif 330 331 static int fg_charger_get_chrg_online(struct udevice *dev) 332 { 333 struct charge_animation_priv *priv = dev_get_priv(dev); 334 struct udevice *charger; 335 336 charger = priv->charger ? : priv->fg; 337 338 return fuel_gauge_get_chrg_online(charger); 339 } 340 341 static int charge_extrem_low_power(struct udevice *dev) 342 { 343 struct charge_animation_pdata *pdata = dev_get_platdata(dev); 344 struct charge_animation_priv *priv = dev_get_priv(dev); 345 struct udevice *pmic = priv->pmic; 346 struct udevice *fg = priv->fg; 347 int voltage, soc, charging = 1; 348 static int timer_initialized; 349 int ret; 350 351 voltage = fuel_gauge_get_voltage(fg); 352 if (voltage < 0) 353 return -EINVAL; 354 355 while (voltage < pdata->low_power_voltage + 50) { 356 /* Check charger online */ 357 charging = fg_charger_get_chrg_online(dev); 358 if (charging <= 0) { 359 printf("%s: Not charging, online=%d. Shutdown...\n", 360 __func__, charging); 361 /* wait uart flush before shutdown */ 362 mdelay(5); 363 /* PMIC shutdown */ 364 pmic_shutdown(pmic); 365 366 printf("Cpu should never reach here, shutdown failed !\n"); 367 continue; 368 } 369 370 /* Enable auto wakeup */ 371 if (!timer_initialized) { 372 timer_initialized = 1; 373 autowakeup_timer_init(dev, 5); 374 } 375 376 /* 377 * Just for fuel gauge to update something important, 378 * including charge current, coulometer or other. 379 */ 380 soc = fuel_gauge_get_soc(fg); 381 if (soc < 0 || soc > 100) { 382 printf("get soc failed: %d\n", soc); 383 continue; 384 } 385 386 /* Update led */ 387 ret = leds_update(dev, soc); 388 if (ret) 389 printf("update led failed: %d\n", ret); 390 391 printf("Extrem low power, force charging... threshold=%dmv, now=%dmv\n", 392 pdata->low_power_voltage, voltage); 393 394 /* System suspend */ 395 system_suspend_enter(dev); 396 397 /* Update voltage */ 398 voltage = fuel_gauge_get_voltage(fg); 399 if (voltage < 0) { 400 printf("get voltage failed: %d\n", voltage); 401 continue; 402 } 403 404 if (ctrlc()) { 405 printf("Extrem low charge: exit by ctrl+c\n"); 406 break; 407 } 408 } 409 410 autowakeup_timer_uninit(); 411 412 return 0; 413 } 414 415 static int charge_animation_show(struct udevice *dev) 416 { 417 struct charge_animation_pdata *pdata = dev_get_platdata(dev); 418 struct charge_animation_priv *priv = dev_get_priv(dev); 419 const struct charge_image *image = priv->image; 420 struct udevice *pmic = priv->pmic; 421 struct udevice *fg = priv->fg; 422 const char *preboot = env_get("preboot"); 423 int image_num = priv->image_num; 424 bool ever_lowpower_screen_off = false; 425 bool screen_on = true; 426 ulong show_start = 0, charge_start = 0, debug_start = 0; 427 ulong delta; 428 ulong ms = 0, sec = 0; 429 int start_idx = 0, show_idx = -1, old_show_idx = IMAGE_RESET_IDX; 430 int soc, voltage, current, key_state; 431 int i, charging = 1, ret; 432 int boot_mode; 433 int first_poll_fg = 1; 434 435 /* 436 * Check sequence: 437 * 438 * 1. Extrem low power charge? 439 * 2. Preboot cmd? 440 * 3. Valid boot mode? 441 * 4. U-Boot charge enabled by dts config? 442 * 5. Screen off before charge? 443 * 6. Enter charge ! 444 * 445 */ 446 if (!fuel_gauge_bat_is_exist(fg)) { 447 printf("Exit charge: battery is not exist\n"); 448 return 0; 449 } 450 451 /* Extrem low power charge */ 452 ret = charge_extrem_low_power(dev); 453 if (ret < 0) { 454 printf("extrem low power charge failed, ret=%d\n", ret); 455 return ret; 456 } 457 458 /* If there is preboot command, exit */ 459 if (preboot && !strstr(preboot, "dvfs")) { 460 printf("Exit charge: due to preboot cmd '%s'\n", preboot); 461 return 0; 462 } 463 464 /* Not valid charge mode, exit */ 465 #ifdef CONFIG_RKIMG_BOOTLOADER 466 boot_mode = rockchip_get_boot_mode(); 467 if ((boot_mode != BOOT_MODE_CHARGING) && 468 (boot_mode != BOOT_MODE_UNDEFINE)) { 469 printf("Exit charge: due to boot mode\n"); 470 return 0; 471 } 472 #endif 473 474 /* Not charger online, exit */ 475 charging = fg_charger_get_chrg_online(dev); 476 if (charging <= 0) { 477 printf("Exit charge: due to charger offline\n"); 478 return 0; 479 } 480 481 /* Enter android charge, set property for kernel */ 482 if (pdata->android_charge) { 483 env_update("bootargs", "androidboot.mode=charger"); 484 printf("Android charge mode\n"); 485 } 486 487 /* Not enable U-Boot charge, exit */ 488 if (!pdata->uboot_charge) { 489 printf("Exit charge: due to not enable uboot charge\n"); 490 return 0; 491 } 492 493 voltage = fuel_gauge_get_voltage(fg); 494 if (voltage < 0) { 495 printf("get voltage failed: %d\n", voltage); 496 return -EINVAL; 497 } 498 499 /* If low power, turn off screen */ 500 if (voltage <= pdata->screen_on_voltage + 50) { 501 screen_on = false; 502 ever_lowpower_screen_off = true; 503 charge_show_bmp(NULL); 504 } 505 506 /* Auto wakeup */ 507 if (pdata->auto_wakeup_interval) { 508 printf("Auto wakeup: %dS\n", pdata->auto_wakeup_interval); 509 autowakeup_timer_init(dev, pdata->auto_wakeup_interval); 510 } 511 512 /* Give a message warning when CONFIG_IRQ is not enabled */ 513 #ifdef CONFIG_IRQ 514 printf("Enter U-Boot charging mode\n"); 515 #else 516 printf("Enter U-Boot charging mode(without IRQ)\n"); 517 #endif 518 519 charge_start = get_timer(0); 520 delta = get_timer(0); 521 522 /* Charging ! */ 523 while (1) { 524 /* 525 * At the most time, fuel gauge is usually a i2c device, we 526 * should avoid read/write all the time. We had better set 527 * poll seconds to update fuel gauge info. 528 */ 529 if (!first_poll_fg && get_timer(delta) < FUEL_GAUGE_POLL_MS) 530 goto show_images; 531 532 delta = get_timer(0); 533 534 debug("step1 (%d)... \n", screen_on); 535 536 /* 537 * Most fuel gauge is I2C interface, it shouldn't be interrupted 538 * during transfer. The power key event depends on interrupt, so 539 * we should disable local irq when update fuel gauge. 540 */ 541 local_irq_disable(); 542 543 /* Step1: Is charging now ? */ 544 charging = fg_charger_get_chrg_online(dev); 545 if (charging <= 0) { 546 printf("Not charging, online=%d. Shutdown...\n", 547 charging); 548 549 /* wait uart flush before shutdown */ 550 mdelay(5); 551 552 /* PMIC shutdown */ 553 pmic_shutdown(pmic); 554 555 printf("Cpu should never reach here, shutdown failed !\n"); 556 continue; 557 } 558 559 debug("step2 (%d)... show_idx=%d\n", screen_on, show_idx); 560 561 /* Step2: get soc and voltage */ 562 soc = fuel_gauge_get_soc(fg); 563 if (soc < 0 || soc > 100) { 564 printf("get soc failed: %d\n", soc); 565 continue; 566 } 567 568 voltage = fuel_gauge_get_voltage(fg); 569 if (voltage < 0) { 570 printf("get voltage failed: %d\n", voltage); 571 continue; 572 } 573 574 current = fuel_gauge_get_current(fg); 575 if (current == -ENOSYS) { 576 printf("get current failed: %d\n", current); 577 continue; 578 } 579 580 first_poll_fg = 0; 581 local_irq_enable(); 582 583 show_images: 584 /* 585 * Just for debug, otherwise there will be nothing output which 586 * is not good to know what happen. 587 */ 588 if (!debug_start) 589 debug_start = get_timer(0); 590 if (get_timer(debug_start) > 20000) { 591 debug_start = get_timer(0); 592 printf("[%8ld]: soc=%d%%, vol=%dmv, c=%dma, " 593 "online=%d, screen_on=%d\n", 594 get_timer(0)/1000, soc, voltage, 595 current, charging, screen_on); 596 } 597 598 /* Update leds */ 599 ret = leds_update(dev, soc); 600 if (ret) 601 printf("update led failed: %d\n", ret); 602 603 /* 604 * If ever lowpower screen off, force screen_on=false, which 605 * means key event can't modify screen_on, only voltage higher 606 * then threshold can update screen_on=true; 607 */ 608 if (ever_lowpower_screen_off) 609 screen_on = false; 610 611 /* 612 * Auto turn on screen when voltage higher than Vol screen on. 613 * 'ever_lowpower_screen_off' means enter the while(1) loop with 614 * screen off. 615 */ 616 if ((ever_lowpower_screen_off) && 617 (voltage > pdata->screen_on_voltage)) { 618 ever_lowpower_screen_off = false; 619 screen_on = true; 620 show_idx = IMAGE_RESET_IDX; 621 } 622 623 /* 624 * IMAGE_RESET_IDX means show_idx show be update by start_idx. 625 * When short key pressed event trigged, we will set show_idx 626 * as IMAGE_RESET_IDX which updates images index from start_idx 627 * that calculate by current soc. 628 */ 629 if (show_idx == IMAGE_RESET_IDX) { 630 for (i = 0; i < IMAGE_SOC_100_IDX(image_num); i++) { 631 /* Find out which image we start to show */ 632 if ((soc >= image[i].soc) && 633 (soc < image[i + 1].soc)) { 634 start_idx = i; 635 break; 636 } 637 638 if (soc >= 100) { 639 start_idx = IMAGE_SOC_100_IDX(image_num); 640 break; 641 } 642 } 643 644 debug("%s: show_idx=%d, screen_on=%d\n", 645 __func__, show_idx, screen_on); 646 647 /* Mark start index and start time */ 648 show_idx = start_idx; 649 show_start = get_timer(0); 650 } 651 652 debug("step3 (%d)... show_idx=%d\n", screen_on, show_idx); 653 654 /* Step3: show images */ 655 if (screen_on) { 656 /* Don't call 'charge_show_bmp' unless image changed */ 657 if (old_show_idx != show_idx) { 658 old_show_idx = show_idx; 659 debug("SHOW: %s\n", image[show_idx].name); 660 charge_show_bmp(image[show_idx].name); 661 } 662 /* Re-calculate timeout to off screen */ 663 if (priv->auto_screen_off_timeout == 0) 664 priv->auto_screen_off_timeout = get_timer(0); 665 } else { 666 priv->auto_screen_off_timeout = 0; 667 system_suspend_enter(dev); 668 } 669 670 mdelay(5); 671 672 /* Every image shows period */ 673 if (get_timer(show_start) > image[show_idx].period) { 674 show_start = get_timer(0); 675 /* Update to next image */ 676 show_idx++; 677 if (show_idx > IMAGE_SOC_100_IDX(image_num)) 678 show_idx = IMAGE_RESET_IDX; 679 } 680 681 debug("step4 (%d)... \n", screen_on); 682 683 /* 684 * Step4: check key event. 685 * 686 * Short key event: turn on/off screen; 687 * Long key event: show logo and boot system or still charging. 688 */ 689 key_state = check_key_press(dev); 690 if (key_state == KEY_PRESS_DOWN) { 691 /* 692 * Clear current image index, and show image 693 * from start_idx 694 */ 695 old_show_idx = IMAGE_RESET_IDX; 696 show_idx = IMAGE_RESET_IDX; 697 698 /* 699 * Reverse the screen state 700 * 701 * If screen_on=false, means this short key pressed 702 * event turn on the screen and we need show images. 703 * 704 * If screen_on=true, means this short key pressed 705 * event turn off the screen and we never show images. 706 */ 707 if (screen_on) { 708 charge_show_bmp(NULL); /* Turn off screen */ 709 screen_on = false; 710 priv->suspend_delay_timeout = get_timer(0); 711 } else { 712 screen_on = true; 713 } 714 715 printf("screen %s\n", screen_on ? "on" : "off"); 716 } else if (key_state == KEY_PRESS_LONG_DOWN) { 717 /* Set screen_on=true anyway when key long pressed */ 718 if (!screen_on) 719 screen_on = true; 720 721 printf("screen %s\n", screen_on ? "on" : "off"); 722 723 /* Is able to boot now ? */ 724 if (soc < pdata->exit_charge_level) { 725 printf("soc=%d%%, threshold soc=%d%%\n", 726 soc, pdata->exit_charge_level); 727 printf("Low power, unable to boot, charging...\n"); 728 show_idx = IMAGE_LOWPOWER_IDX(image_num); 729 continue; 730 } 731 732 if (voltage < pdata->exit_charge_voltage) { 733 printf("voltage=%dmv, threshold voltage=%dmv\n", 734 voltage, pdata->exit_charge_voltage); 735 printf("Low power, unable to boot, charging...\n"); 736 show_idx = IMAGE_LOWPOWER_IDX(image_num); 737 continue; 738 } 739 740 /* Success exit charging */ 741 printf("Exit charge animation...\n"); 742 charge_show_logo(); 743 break; 744 } else { 745 /* Do nothing */ 746 } 747 748 debug("step5 (%d)... \n", screen_on); 749 750 /* Step5: Exit by ctrl+c */ 751 if (ctrlc()) { 752 if (voltage >= pdata->screen_on_voltage) 753 charge_show_logo(); 754 printf("Exit charge, due to ctrl+c\n"); 755 break; 756 } 757 } 758 759 if (pdata->auto_wakeup_interval) 760 autowakeup_timer_uninit(); 761 762 ms = get_timer(charge_start); 763 if (ms >= 1000) { 764 sec = ms / 1000; 765 ms = ms % 1000; 766 } 767 768 printf("charging time total: %lu.%lus, soc=%d%%, vol=%dmv\n", 769 sec, ms, soc, voltage); 770 771 return 0; 772 } 773 774 static int fg_charger_get_device(struct udevice **fuel_gauge, 775 struct udevice **charger) 776 { 777 struct udevice *dev; 778 struct uclass *uc; 779 int ret, cap; 780 781 *fuel_gauge = NULL, 782 *charger = NULL; 783 784 ret = uclass_get(UCLASS_FG, &uc); 785 if (ret) 786 return ret; 787 788 for (uclass_first_device(UCLASS_FG, &dev); 789 dev; 790 uclass_next_device(&dev)) { 791 cap = fuel_gauge_capability(dev); 792 if (cap == (FG_CAP_CHARGER | FG_CAP_FUEL_GAUGE)) { 793 *fuel_gauge = dev; 794 *charger = NULL; 795 } else if (cap == FG_CAP_FUEL_GAUGE) { 796 *fuel_gauge = dev; 797 } else if (cap == FG_CAP_CHARGER) { 798 *charger = dev; 799 } 800 } 801 802 return (*fuel_gauge) ? 0 : -ENODEV; 803 } 804 805 static const struct dm_charge_display_ops charge_animation_ops = { 806 .show = charge_animation_show, 807 }; 808 809 static int charge_animation_probe(struct udevice *dev) 810 { 811 struct charge_animation_priv *priv = dev_get_priv(dev); 812 int ret, soc; 813 814 /* Get PMIC: used for power off system */ 815 ret = uclass_get_device(UCLASS_PMIC, 0, &priv->pmic); 816 if (ret) { 817 if (ret == -ENODEV) 818 printf("Can't find PMIC\n"); 819 else 820 printf("Get UCLASS PMIC failed: %d\n", ret); 821 return ret; 822 } 823 824 /* Get fuel gauge and charger(If need) */ 825 ret = fg_charger_get_device(&priv->fg, &priv->charger); 826 if (ret) { 827 if (ret == -ENODEV) 828 debug("Can't find FG\n"); 829 else 830 debug("Get UCLASS FG failed: %d\n", ret); 831 return ret; 832 } 833 834 /* Get rtc: used for power on */ 835 ret = uclass_get_device(UCLASS_RTC, 0, &priv->rtc); 836 if (ret) { 837 if (ret == -ENODEV) 838 debug("Can't find RTC\n"); 839 else 840 debug("Get UCLASS RTC failed: %d\n", ret); 841 } 842 843 /* Get PWRKEY: used for wakeup and turn off/on LCD */ 844 if (key_read(KEY_POWER) == KEY_NOT_EXIST) { 845 debug("Can't find power key\n"); 846 return -EINVAL; 847 } 848 849 /* Initialize charge current */ 850 soc = fuel_gauge_get_soc(priv->fg); 851 if (soc < 0 || soc > 100) { 852 debug("get soc failed: %d\n", soc); 853 return -EINVAL; 854 } 855 856 /* Get leds */ 857 #ifdef CONFIG_LED 858 ret = led_get_by_label(LED_CHARGING_NAME, &priv->led_charging); 859 if (!ret) 860 printf("Found Charging LED\n"); 861 ret = led_get_by_label(LED_CHARGING_FULL_NAME, &priv->led_full); 862 if (!ret) 863 printf("Found Charging-Full LED\n"); 864 #endif 865 866 /* Get charge images */ 867 priv->image = image; 868 priv->image_num = ARRAY_SIZE(image); 869 870 printf("Enable charge animation display\n"); 871 872 return 0; 873 } 874 875 static const struct udevice_id charge_animation_ids[] = { 876 { .compatible = "rockchip,uboot-charge" }, 877 { }, 878 }; 879 880 U_BOOT_DRIVER(charge_animation) = { 881 .name = "charge-animation", 882 .id = UCLASS_CHARGE_DISPLAY, 883 .probe = charge_animation_probe, 884 .of_match = charge_animation_ids, 885 .ops = &charge_animation_ops, 886 .ofdata_to_platdata = charge_animation_ofdata_to_platdata, 887 .platdata_auto_alloc_size = sizeof(struct charge_animation_pdata), 888 .priv_auto_alloc_size = sizeof(struct charge_animation_priv), 889 }; 890