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 405 autowakeup_timer_uninit(); 406 407 return 0; 408 } 409 410 static int charge_animation_show(struct udevice *dev) 411 { 412 struct charge_animation_pdata *pdata = dev_get_platdata(dev); 413 struct charge_animation_priv *priv = dev_get_priv(dev); 414 const struct charge_image *image = priv->image; 415 struct udevice *pmic = priv->pmic; 416 struct udevice *fg = priv->fg; 417 const char *preboot = env_get("preboot"); 418 int image_num = priv->image_num; 419 bool ever_lowpower_screen_off = false; 420 bool screen_on = true; 421 ulong show_start = 0, charge_start = 0, debug_start = 0; 422 ulong delta; 423 ulong ms = 0, sec = 0; 424 int start_idx = 0, show_idx = -1, old_show_idx = IMAGE_RESET_IDX; 425 int soc, voltage, current, key_state; 426 int i, charging = 1, ret; 427 int boot_mode; 428 int first_poll_fg = 1; 429 430 /* 431 * Check sequence: 432 * 433 * 1. Extrem low power charge? 434 * 2. Preboot cmd? 435 * 3. Valid boot mode? 436 * 4. U-Boot charge enabled by dts config? 437 * 5. Screen off before charge? 438 * 6. Enter charge ! 439 * 440 */ 441 if (!fuel_gauge_bat_is_exist(fg)) { 442 printf("Exit charge: battery is not exist\n"); 443 return 0; 444 } 445 446 /* Extrem low power charge */ 447 ret = charge_extrem_low_power(dev); 448 if (ret < 0) { 449 printf("extrem low power charge failed, ret=%d\n", ret); 450 return ret; 451 } 452 453 /* If there is preboot command, exit */ 454 if (preboot && !strstr(preboot, "dvfs")) { 455 printf("Exit charge: due to preboot cmd '%s'\n", preboot); 456 return 0; 457 } 458 459 /* Not valid charge mode, exit */ 460 #ifdef CONFIG_RKIMG_BOOTLOADER 461 boot_mode = rockchip_get_boot_mode(); 462 if ((boot_mode != BOOT_MODE_CHARGING) && 463 (boot_mode != BOOT_MODE_UNDEFINE)) { 464 printf("Exit charge: due to boot mode\n"); 465 return 0; 466 } 467 #endif 468 469 /* Not charger online, exit */ 470 charging = fg_charger_get_chrg_online(dev); 471 if (charging <= 0) { 472 printf("Exit charge: due to charger offline\n"); 473 return 0; 474 } 475 476 /* Enter android charge, set property for kernel */ 477 if (pdata->android_charge) { 478 env_update("bootargs", "androidboot.mode=charger"); 479 printf("Android charge mode\n"); 480 } 481 482 /* Not enable U-Boot charge, exit */ 483 if (!pdata->uboot_charge) { 484 printf("Exit charge: due to not enable uboot charge\n"); 485 return 0; 486 } 487 488 voltage = fuel_gauge_get_voltage(fg); 489 if (voltage < 0) { 490 printf("get voltage failed: %d\n", voltage); 491 return -EINVAL; 492 } 493 494 /* If low power, turn off screen */ 495 if (voltage <= pdata->screen_on_voltage + 50) { 496 screen_on = false; 497 ever_lowpower_screen_off = true; 498 charge_show_bmp(NULL); 499 } 500 501 /* Auto wakeup */ 502 if (pdata->auto_wakeup_interval) { 503 printf("Auto wakeup: %dS\n", pdata->auto_wakeup_interval); 504 autowakeup_timer_init(dev, pdata->auto_wakeup_interval); 505 } 506 507 /* Give a message warning when CONFIG_IRQ is not enabled */ 508 #ifdef CONFIG_IRQ 509 printf("Enter U-Boot charging mode\n"); 510 #else 511 printf("Enter U-Boot charging mode(without IRQ)\n"); 512 #endif 513 514 charge_start = get_timer(0); 515 delta = get_timer(0); 516 517 /* Charging ! */ 518 while (1) { 519 /* 520 * At the most time, fuel gauge is usually a i2c device, we 521 * should avoid read/write all the time. We had better set 522 * poll seconds to update fuel gauge info. 523 */ 524 if (!first_poll_fg && get_timer(delta) < FUEL_GAUGE_POLL_MS) 525 goto show_images; 526 527 delta = get_timer(0); 528 529 debug("step1 (%d)... \n", screen_on); 530 531 /* 532 * Most fuel gauge is I2C interface, it shouldn't be interrupted 533 * during transfer. The power key event depends on interrupt, so 534 * we should disable local irq when update fuel gauge. 535 */ 536 local_irq_disable(); 537 538 /* Step1: Is charging now ? */ 539 charging = fg_charger_get_chrg_online(dev); 540 if (charging <= 0) { 541 printf("Not charging, online=%d. Shutdown...\n", 542 charging); 543 544 /* wait uart flush before shutdown */ 545 mdelay(5); 546 547 /* PMIC shutdown */ 548 pmic_shutdown(pmic); 549 550 printf("Cpu should never reach here, shutdown failed !\n"); 551 continue; 552 } 553 554 debug("step2 (%d)... show_idx=%d\n", screen_on, show_idx); 555 556 /* Step2: get soc and voltage */ 557 soc = fuel_gauge_get_soc(fg); 558 if (soc < 0 || soc > 100) { 559 printf("get soc failed: %d\n", soc); 560 continue; 561 } 562 563 voltage = fuel_gauge_get_voltage(fg); 564 if (voltage < 0) { 565 printf("get voltage failed: %d\n", voltage); 566 continue; 567 } 568 569 current = fuel_gauge_get_current(fg); 570 if (current == -ENOSYS) { 571 printf("get current failed: %d\n", current); 572 continue; 573 } 574 575 first_poll_fg = 0; 576 local_irq_enable(); 577 578 show_images: 579 /* 580 * Just for debug, otherwise there will be nothing output which 581 * is not good to know what happen. 582 */ 583 if (!debug_start) 584 debug_start = get_timer(0); 585 if (get_timer(debug_start) > 20000) { 586 debug_start = get_timer(0); 587 printf("[%8ld]: soc=%d%%, vol=%dmv, c=%dma, " 588 "online=%d, screen_on=%d\n", 589 get_timer(0)/1000, soc, voltage, 590 current, charging, screen_on); 591 } 592 593 /* Update leds */ 594 ret = leds_update(dev, soc); 595 if (ret) 596 printf("update led failed: %d\n", ret); 597 598 /* 599 * If ever lowpower screen off, force screen_on=false, which 600 * means key event can't modify screen_on, only voltage higher 601 * then threshold can update screen_on=true; 602 */ 603 if (ever_lowpower_screen_off) 604 screen_on = false; 605 606 /* 607 * Auto turn on screen when voltage higher than Vol screen on. 608 * 'ever_lowpower_screen_off' means enter the while(1) loop with 609 * screen off. 610 */ 611 if ((ever_lowpower_screen_off) && 612 (voltage > pdata->screen_on_voltage)) { 613 ever_lowpower_screen_off = false; 614 screen_on = true; 615 show_idx = IMAGE_RESET_IDX; 616 } 617 618 /* 619 * IMAGE_RESET_IDX means show_idx show be update by start_idx. 620 * When short key pressed event trigged, we will set show_idx 621 * as IMAGE_RESET_IDX which updates images index from start_idx 622 * that calculate by current soc. 623 */ 624 if (show_idx == IMAGE_RESET_IDX) { 625 for (i = 0; i < IMAGE_SOC_100_IDX(image_num); i++) { 626 /* Find out which image we start to show */ 627 if ((soc >= image[i].soc) && 628 (soc < image[i + 1].soc)) { 629 start_idx = i; 630 break; 631 } 632 633 if (soc >= 100) { 634 start_idx = IMAGE_SOC_100_IDX(image_num); 635 break; 636 } 637 } 638 639 debug("%s: show_idx=%d, screen_on=%d\n", 640 __func__, show_idx, screen_on); 641 642 /* Mark start index and start time */ 643 show_idx = start_idx; 644 show_start = get_timer(0); 645 } 646 647 debug("step3 (%d)... show_idx=%d\n", screen_on, show_idx); 648 649 /* Step3: show images */ 650 if (screen_on) { 651 /* Don't call 'charge_show_bmp' unless image changed */ 652 if (old_show_idx != show_idx) { 653 old_show_idx = show_idx; 654 debug("SHOW: %s\n", image[show_idx].name); 655 charge_show_bmp(image[show_idx].name); 656 } 657 /* Re-calculate timeout to off screen */ 658 if (priv->auto_screen_off_timeout == 0) 659 priv->auto_screen_off_timeout = get_timer(0); 660 } else { 661 priv->auto_screen_off_timeout = 0; 662 system_suspend_enter(dev); 663 } 664 665 mdelay(5); 666 667 /* Every image shows period */ 668 if (get_timer(show_start) > image[show_idx].period) { 669 show_start = get_timer(0); 670 /* Update to next image */ 671 show_idx++; 672 if (show_idx > IMAGE_SOC_100_IDX(image_num)) 673 show_idx = IMAGE_RESET_IDX; 674 } 675 676 debug("step4 (%d)... \n", screen_on); 677 678 /* 679 * Step4: check key event. 680 * 681 * Short key event: turn on/off screen; 682 * Long key event: show logo and boot system or still charging. 683 */ 684 key_state = check_key_press(dev); 685 if (key_state == KEY_PRESS_DOWN) { 686 /* 687 * Clear current image index, and show image 688 * from start_idx 689 */ 690 old_show_idx = IMAGE_RESET_IDX; 691 show_idx = IMAGE_RESET_IDX; 692 693 /* 694 * Reverse the screen state 695 * 696 * If screen_on=false, means this short key pressed 697 * event turn on the screen and we need show images. 698 * 699 * If screen_on=true, means this short key pressed 700 * event turn off the screen and we never show images. 701 */ 702 if (screen_on) { 703 charge_show_bmp(NULL); /* Turn off screen */ 704 screen_on = false; 705 priv->suspend_delay_timeout = get_timer(0); 706 } else { 707 screen_on = true; 708 } 709 710 printf("screen %s\n", screen_on ? "on" : "off"); 711 } else if (key_state == KEY_PRESS_LONG_DOWN) { 712 /* Set screen_on=true anyway when key long pressed */ 713 if (!screen_on) 714 screen_on = true; 715 716 printf("screen %s\n", screen_on ? "on" : "off"); 717 718 /* Is able to boot now ? */ 719 if (soc < pdata->exit_charge_level) { 720 printf("soc=%d%%, threshold soc=%d%%\n", 721 soc, pdata->exit_charge_level); 722 printf("Low power, unable to boot, charging...\n"); 723 show_idx = IMAGE_LOWPOWER_IDX(image_num); 724 continue; 725 } 726 727 if (voltage < pdata->exit_charge_voltage) { 728 printf("voltage=%dmv, threshold voltage=%dmv\n", 729 voltage, pdata->exit_charge_voltage); 730 printf("Low power, unable to boot, charging...\n"); 731 show_idx = IMAGE_LOWPOWER_IDX(image_num); 732 continue; 733 } 734 735 /* Success exit charging */ 736 printf("Exit charge animation...\n"); 737 charge_show_logo(); 738 break; 739 } else { 740 /* Do nothing */ 741 } 742 743 debug("step5 (%d)... \n", screen_on); 744 745 /* Step5: Exit by ctrl+c */ 746 if (ctrlc()) { 747 if (voltage >= pdata->screen_on_voltage) 748 charge_show_logo(); 749 printf("Exit charge, due to ctrl+c\n"); 750 break; 751 } 752 } 753 754 if (pdata->auto_wakeup_interval) 755 autowakeup_timer_uninit(); 756 757 ms = get_timer(charge_start); 758 if (ms >= 1000) { 759 sec = ms / 1000; 760 ms = ms % 1000; 761 } 762 763 printf("charging time total: %lu.%lus, soc=%d%%, vol=%dmv\n", 764 sec, ms, soc, voltage); 765 766 return 0; 767 } 768 769 static int fg_charger_get_device(struct udevice **fuel_gauge, 770 struct udevice **charger) 771 { 772 struct udevice *dev; 773 struct uclass *uc; 774 int ret, cap; 775 776 *fuel_gauge = NULL, 777 *charger = NULL; 778 779 ret = uclass_get(UCLASS_FG, &uc); 780 if (ret) 781 return ret; 782 783 for (uclass_first_device(UCLASS_FG, &dev); 784 dev; 785 uclass_next_device(&dev)) { 786 cap = fuel_gauge_capability(dev); 787 if (cap == (FG_CAP_CHARGER | FG_CAP_FUEL_GAUGE)) { 788 *fuel_gauge = dev; 789 *charger = NULL; 790 } else if (cap == FG_CAP_FUEL_GAUGE) { 791 *fuel_gauge = dev; 792 } else if (cap == FG_CAP_CHARGER) { 793 *charger = dev; 794 } 795 } 796 797 return (*fuel_gauge) ? 0 : -ENODEV; 798 } 799 800 static const struct dm_charge_display_ops charge_animation_ops = { 801 .show = charge_animation_show, 802 }; 803 804 static int charge_animation_probe(struct udevice *dev) 805 { 806 struct charge_animation_priv *priv = dev_get_priv(dev); 807 int ret, soc; 808 809 /* Get PMIC: used for power off system */ 810 ret = uclass_get_device(UCLASS_PMIC, 0, &priv->pmic); 811 if (ret) { 812 if (ret == -ENODEV) 813 printf("Can't find PMIC\n"); 814 else 815 printf("Get UCLASS PMIC failed: %d\n", ret); 816 return ret; 817 } 818 819 /* Get fuel gauge and charger(If need) */ 820 ret = fg_charger_get_device(&priv->fg, &priv->charger); 821 if (ret) { 822 if (ret == -ENODEV) 823 debug("Can't find FG\n"); 824 else 825 debug("Get UCLASS FG failed: %d\n", ret); 826 return ret; 827 } 828 829 /* Get rtc: used for power on */ 830 ret = uclass_get_device(UCLASS_RTC, 0, &priv->rtc); 831 if (ret) { 832 if (ret == -ENODEV) 833 debug("Can't find RTC\n"); 834 else 835 debug("Get UCLASS RTC failed: %d\n", ret); 836 } 837 838 /* Get PWRKEY: used for wakeup and turn off/on LCD */ 839 if (key_read(KEY_POWER) == KEY_NOT_EXIST) { 840 debug("Can't find power key\n"); 841 return -EINVAL; 842 } 843 844 /* Initialize charge current */ 845 soc = fuel_gauge_get_soc(priv->fg); 846 if (soc < 0 || soc > 100) { 847 debug("get soc failed: %d\n", soc); 848 return -EINVAL; 849 } 850 851 /* Get leds */ 852 #ifdef CONFIG_LED 853 ret = led_get_by_label(LED_CHARGING_NAME, &priv->led_charging); 854 if (!ret) 855 printf("Found Charging LED\n"); 856 ret = led_get_by_label(LED_CHARGING_FULL_NAME, &priv->led_full); 857 if (!ret) 858 printf("Found Charging-Full LED\n"); 859 #endif 860 861 /* Get charge images */ 862 priv->image = image; 863 priv->image_num = ARRAY_SIZE(image); 864 865 printf("Enable charge animation display\n"); 866 867 return 0; 868 } 869 870 static const struct udevice_id charge_animation_ids[] = { 871 { .compatible = "rockchip,uboot-charge" }, 872 { }, 873 }; 874 875 U_BOOT_DRIVER(charge_animation) = { 876 .name = "charge-animation", 877 .id = UCLASS_CHARGE_DISPLAY, 878 .probe = charge_animation_probe, 879 .of_match = charge_animation_ids, 880 .ops = &charge_animation_ops, 881 .ofdata_to_platdata = charge_animation_ofdata_to_platdata, 882 .platdata_auto_alloc_size = sizeof(struct charge_animation_pdata), 883 .priv_auto_alloc_size = sizeof(struct charge_animation_priv), 884 }; 885