static void lpm_mode_check(struct battery_data *battery) { extern int charging_mode_from_boot; pr_info("%s : charging_mode_from_boot(%d), ta_connected(%d)\n", __func__, charging_mode_from_boot, check_ta_conn(battery)); if (!charging_mode_from_boot) return; if (check_ta_conn(battery)) { battery->charging_mode_booting = 1; lpm_mode_flag = 1; pr_info("%s : charging_mode_booting(%d)\n", __func__, battery->charging_mode_booting); } else { pr_info("%s: ta no longer connected, powering off\n", __func__); if (pm_power_off) { #ifdef CONFIG_KERNEL_DEBUG_SEC kernel_sec_upload_cause_type upload_cause = kernel_sec_get_upload_cause(); if (upload_cause == UPLOAD_CAUSE_INIT) /* Clear the magic number because it's normal reboot */ kernel_sec_clear_upload_magic_number(); #endif pm_power_off(); } } }
static ssize_t p3_bat_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) { int x = 0; int ret = 0; const ptrdiff_t off = attr - p3_battery_attrs; switch (off) { case FORCE_USB_CHARGING: if (sscanf(buf, "%d\n", &x) == 1) { if (x == 1) test_batterydata->info.force_usb_charging = true; else test_batterydata->info.force_usb_charging = false; ret = count; p3_bat_status_update(&test_batterydata->psy_battery); } break; case BATT_RESET_SOC: if (sscanf(buf, "%d\n", &x) == 1) { if (x == 1) { if (!check_ta_conn(test_batterydata)) fg_reset_soc(); } ret = count; } p3_bat_status_update(&test_batterydata->psy_battery); pr_debug("Reset SOC:%d ", x); break; case BATT_RESET_CAP: if (sscanf(buf, "%d\n", &x) == 1 || x == 2 || x == 3 || x == 4) { if (x == 1 || x == 2 || x == 3 || x == 4) fg_reset_capacity(); ret = count; } pr_debug("%s: Reset CAP:%d\n", __func__, x); break; case BATT_FG_REG: if (sscanf(buf, "%d\n", &x) == 1) { if (x == 1) fg_periodic_read(); ret = count; } pr_debug("%s: FG Register:%d\n", __func__, x); break; #ifdef CONFIG_SAMSUNG_LPM_MODE case CHARGING_MODE_BOOTING: if (sscanf(buf, "%d\n", &x) == 1) { test_batterydata->charging_mode_booting = x; ret = count; } break; #endif default: ret = -EINVAL; } return ret; }
static void lpm_mode_check(struct battery_data *battery) { extern int charging_mode_from_boot; pr_info("%s : charging_mode_from_boot(%d), ta_connected(%d)\n", __func__, charging_mode_from_boot, check_ta_conn(battery)); if (!charging_mode_from_boot) return; if (check_ta_conn(battery)) { battery->charging_mode_booting = 1; lpm_mode_flag = 1; pr_info("%s : charging_mode_booting(%d)\n", __func__, battery->charging_mode_booting); } else { pr_info("%s: ta no longer connected, powering off\n", __func__); if (pm_power_off) pm_power_off(); } }
static u32 get_charger_status(struct battery_data *battery) { if (check_ta_conn(battery)) { if (battery->current_cable_status == CHARGER_USB) pr_info("Charger Status : USB\n"); else pr_info("Charger Status : TA\n"); return 1; } else { pr_info("Charger Status : Battery.\n"); return 0; } }
static void p3_bat_status_update(struct power_supply *bat_ps) { struct battery_data *battery = container_of(bat_ps, struct battery_data, psy_battery); int old_level, old_temp, old_health, old_is_full; int current_charging_status; if (!battery->p3_battery_initial) return; mutex_lock(&battery->work_lock); old_temp = battery->info.batt_temp; old_health = battery->info.batt_health; old_level = battery->info.level; old_is_full = battery->info.batt_is_full; battery->info.batt_temp = p3_get_bat_temp(bat_ps); battery->info.level = p3_get_bat_level(bat_ps); if (!battery->info.charging_enabled && !battery->info.batt_is_full && !check_jig_on()) { if (battery->info.level > old_level) battery->info.level = old_level; } battery->info.batt_vol = p3_get_bat_vol(bat_ps); current_charging_status = gpio_get_value(battery->pdata->charger.fullcharge_line); if (current_charging_status >= 0) { if (check_ta_conn(battery) && battery->previous_charging_status == 0 && current_charging_status == 1) { pr_debug("BATTERY FULL"); cancel_delayed_work(&battery->fullcharging_work); schedule_delayed_work(&battery->fullcharging_work, msecs_to_jiffies(300)); wake_lock_timeout(&battery->fullcharge_wake_lock, HZ * 30); } battery->previous_charging_status = current_charging_status; } power_supply_changed(bat_ps); pr_debug("call power_supply_changed"); mutex_unlock(&battery->work_lock); }
static void p3_get_cable_status(struct battery_data *battery) { if (check_ta_conn(battery)) { if (battery->pdata->check_dedicated_charger()) battery->current_cable_status = CHARGER_AC; else battery->current_cable_status = CHARGER_USB; set_irq_type(gpio_to_irq(battery->pdata->charger.connect_line), IRQ_TYPE_LEVEL_HIGH); if (battery->pdata->inform_charger_connection) battery->pdata->inform_charger_connection(true); } else { battery->current_cable_status = CHARGER_BATTERY; set_irq_type(gpio_to_irq(battery->pdata->charger.connect_line), IRQ_TYPE_LEVEL_LOW); if (battery->pdata->inform_charger_connection) battery->pdata->inform_charger_connection(false); battery->info.batt_improper_ta = 0; // clear flag } pr_info("%s: current_status : %d\n", __func__, battery->current_cable_status); }
static int p3_get_bat_level(struct power_supply *bat_ps) { struct battery_data *battery = container_of(bat_ps, struct battery_data, psy_battery); int fg_soc; int fg_vfsoc; int fg_vcell; int fg_current; int avg_current; int recover_flag = 0; recover_flag = fg_check_cap_corruption(); /* check VFcapacity every five minutes */ if (!(battery->fg_chk_cnt++ % 10)) { fg_check_vf_fullcap_range(); battery->fg_chk_cnt = 1; } fg_soc = get_fuelgauge_value(FG_LEVEL); if (fg_soc < 0) { pr_info("Can't read soc!!!"); fg_soc = battery->info.level; } if (!check_jig_on() && !max17042_chip_data->info.low_batt_comp_flag) { if (((fg_soc+5) < max17042_chip_data->info.previous_repsoc) || (fg_soc > (max17042_chip_data->info.previous_repsoc+5))) battery->fg_skip = 1; } /* skip one time (maximum 30 seconds) because of corruption. */ if (battery->fg_skip) { pr_info("%s: skip update until corruption check " "is done (fg_skip_cnt:%d)\n", __func__, ++battery->fg_skip_cnt); fg_soc = battery->info.level; if (recover_flag || battery->fg_skip_cnt > 10) { battery->fg_skip = 0; battery->fg_skip_cnt = 0; } } if (battery->low_batt_boot_flag) { fg_soc = 0; if (check_ta_conn(battery) && !check_UV_charging_case()) { fg_adjust_capacity(); battery->low_batt_boot_flag = 0; } if (!check_ta_conn(battery)) battery->low_batt_boot_flag = 0; } fg_vcell = get_fuelgauge_value(FG_VOLTAGE); if (fg_vcell < 0) { pr_info("Can't read vcell!!!"); fg_vcell = battery->info.batt_vol; } else battery->info.batt_vol = fg_vcell; fg_current = get_fuelgauge_value(FG_CURRENT); avg_current = get_fuelgauge_value(FG_CURRENT_AVG); fg_vfsoc = get_fuelgauge_value(FG_VF_SOC); // Algorithm for reducing time to fully charged (from MAXIM) if(battery->info.charging_enabled && // Charging is enabled !battery->info.batt_is_recharging && // Not Recharging battery->info.charging_source == CHARGER_AC && // Only AC (Not USB cable) !battery->is_first_check && // Skip when first check after boot up (fg_vfsoc>70 && (fg_current>20 && fg_current<250) && (avg_current>20 && avg_current<260))) { if(battery->full_check_flag == 2) { pr_info("%s: force fully charged SOC !! (%d)", __func__, battery->full_check_flag); fg_set_full_charged(); fg_soc = get_fuelgauge_value(FG_LEVEL); } else if(battery->full_check_flag < 2) pr_info("%s: full_check_flag (%d)", __func__, battery->full_check_flag); if(battery->full_check_flag++ > 10000) // prevent overflow battery->full_check_flag = 3; } else battery->full_check_flag = 0; if (battery->info.charging_source == CHARGER_AC && battery->info.batt_improper_ta == 0) { if (is_over_abs_time(battery)) { fg_soc = 100; battery->info.batt_is_full = 1; pr_info("%s: charging time is over", __func__); pr_info("%s: fg_vcell = %d, fg_soc = %d," " is_full = %d\n", __func__, fg_vcell, fg_soc, battery->info.batt_is_full); p3_set_chg_en(battery, 0); goto __end__; } } if (fg_vcell <= battery->pdata->recharge_voltage) { if (battery->info.batt_is_full && !battery->info.charging_enabled) { if (++battery->recharging_cnt > 1) { pr_info("recharging(under full)"); battery->info.batt_is_recharging = 1; p3_set_chg_en(battery, 1); battery->recharging_cnt = 0; pr_info("%s: fg_vcell = %d, fg_soc = %d," " is_recharging = %d\n", __func__, fg_vcell, fg_soc, battery->info.batt_is_recharging); } } else battery->recharging_cnt = 0; } else battery->recharging_cnt = 0; if (fg_soc > 100) fg_soc = 100; /* Checks vcell level and tries to compensate SOC if needed.*/ /* If jig cable is connected, then skip low batt compensation check. */ if (!check_jig_on() && !battery->info.charging_enabled) fg_soc = p3_low_batt_compensation(fg_soc, fg_vcell, fg_current); __end__: pr_debug("fg_vcell = %d, fg_soc = %d, is_full = %d", fg_vcell, fg_soc, battery->info.batt_is_full); if(battery->is_first_check) battery->is_first_check = false; if (battery->info.batt_is_full && (battery->info.charging_source != CHARGER_USB)) fg_soc = 100; #if 0 // not used else { if (fg_soc >= 100) fg_soc = 99; } #endif return fg_soc; }
static int __devinit p3_bat_probe(struct platform_device *pdev) { struct p3_battery_platform_data *pdata = dev_get_platdata(&pdev->dev); struct battery_data *battery; int ret; unsigned long trigger; int irq_num; battery = kzalloc(sizeof(*battery), GFP_KERNEL); if (!battery) return -ENOMEM; battery->pdata = pdata; if (!battery->pdata) { pr_err("%s : No platform data\n", __func__); return -EINVAL; } battery->pdata->init_charger_gpio(); platform_set_drvdata(pdev, battery); test_batterydata = battery; battery->present = 1; battery->info.level = 100; battery->info.charging_source = CHARGER_BATTERY; battery->info.batt_health = POWER_SUPPLY_HEALTH_GOOD; battery->is_first_check = true; battery->psy_battery.name = "battery"; battery->psy_battery.type = POWER_SUPPLY_TYPE_BATTERY; battery->psy_battery.properties = p3_battery_properties; battery->psy_battery.num_properties = ARRAY_SIZE(p3_battery_properties); battery->psy_battery.get_property = p3_bat_get_property; battery->psy_usb.name = "usb"; battery->psy_usb.type = POWER_SUPPLY_TYPE_USB; battery->psy_usb.supplied_to = supply_list; battery->psy_usb.num_supplicants = ARRAY_SIZE(supply_list); battery->psy_usb.properties = p3_power_properties; battery->psy_usb.num_properties = ARRAY_SIZE(p3_power_properties); battery->psy_usb.get_property = p3_usb_get_property; battery->psy_ac.name = "ac"; battery->psy_ac.type = POWER_SUPPLY_TYPE_MAINS; battery->psy_ac.supplied_to = supply_list; battery->psy_ac.num_supplicants = ARRAY_SIZE(supply_list); battery->psy_ac.properties = p3_power_properties; battery->psy_ac.num_properties = ARRAY_SIZE(p3_power_properties); battery->psy_ac.get_property = p3_ac_get_property; mutex_init(&battery->work_lock); wake_lock_init(&battery->vbus_wake_lock, WAKE_LOCK_SUSPEND, "vbus wake lock"); wake_lock_init(&battery->work_wake_lock, WAKE_LOCK_SUSPEND, "batt_work wake lock"); wake_lock_init(&battery->cable_wake_lock, WAKE_LOCK_SUSPEND, "temp wake lock"); wake_lock_init(&battery->fullcharge_wake_lock, WAKE_LOCK_SUSPEND, "fullcharge wake lock"); #ifdef __TEST_DEVICE_DRIVER__ wake_lock_init(&battery->wake_lock_for_dev, WAKE_LOCK_SUSPEND, "test mode wake lock"); #endif /* __TEST_DEVICE_DRIVER__ */ INIT_WORK(&battery->battery_work, p3_bat_work); INIT_WORK(&battery->cable_work, p3_cable_work); INIT_DELAYED_WORK(&battery->fuelgauge_work, fuelgauge_work_handler); INIT_DELAYED_WORK(&battery->fullcharging_work, fullcharging_work_handler); INIT_DELAYED_WORK(&battery->full_comp_work, full_comp_work_handler); INIT_DELAYED_WORK(&battery->TA_work, p3_TA_work_handler); battery->p3_TA_workqueue = create_singlethread_workqueue( "p3_TA_workqueue"); if (!battery->p3_TA_workqueue) { pr_err("Failed to create single workqueue\n"); ret = -ENOMEM; goto err_workqueue_init; } battery->last_poll = alarm_get_elapsed_realtime(); alarm_init(&battery->alarm, ANDROID_ALARM_ELAPSED_REALTIME_WAKEUP, p3_battery_alarm); ret = power_supply_register(&pdev->dev, &battery->psy_battery); if (ret) { pr_err("Failed to register battery power supply.\n"); goto err_battery_psy_register; } ret = power_supply_register(&pdev->dev, &battery->psy_usb); if (ret) { pr_err("Failed to register USB power supply.\n"); goto err_usb_psy_register; } ret = power_supply_register(&pdev->dev, &battery->psy_ac); if (ret) { pr_err("Failed to register AC power supply.\n"); goto err_ac_psy_register; } /* create sec detail attributes */ p3_bat_create_attrs(battery->psy_battery.dev); #ifdef __TEST_DEVICE_DRIVER__ sec_batt_test_create_attrs(battery->psy_ac.dev); #endif /* __TEST_DEVICE_DRIVER__ */ battery->p3_battery_initial = 1; battery->low_batt_boot_flag = 0; battery->connect_irq = gpio_to_irq(pdata->charger.connect_line); if (check_ta_conn(battery)) trigger = IRQF_TRIGGER_HIGH; else trigger = IRQF_TRIGGER_LOW; if (request_irq(battery->connect_irq, p3_TA_interrupt_handler, trigger, "TA_CON intr", battery)) { pr_err("p3_TA_interrupt_handler register failed!\n"); goto err_charger_irq; } disable_irq(battery->connect_irq); // Get initial cable status and enable connection irq. p3_get_cable_status(battery); battery->previous_cable_status = battery->current_cable_status; enable_irq(battery->connect_irq); if (check_ta_conn(battery) && check_UV_charging_case()) battery->low_batt_boot_flag = 1; mutex_lock(&battery->work_lock); fg_alert_init(); mutex_unlock(&battery->work_lock); p3_bat_status_update(&battery->psy_battery); p3_cable_check_status(battery); /* before enable fullcharge interrupt, check fullcharge */ if (battery->info.charging_source == CHARGER_AC && battery->info.charging_enabled && gpio_get_value(pdata->charger.fullcharge_line) == 1) p3_cable_charging(battery); /* Request IRQ */ irq_num = gpio_to_irq(max17042_chip_data->pdata->fuel_alert_line); if (request_irq(irq_num, low_battery_isr, IRQF_TRIGGER_FALLING, "FUEL_ALRT irq", battery)) pr_err("Can NOT request irq 'IRQ_FUEL_ALERT' %d ", irq_num); #ifdef CONFIG_SAMSUNG_LPM_MODE lpm_mode_check(battery); #endif return 0; err_charger_irq: alarm_cancel(&battery->alarm); power_supply_unregister(&battery->psy_ac); err_ac_psy_register: power_supply_unregister(&battery->psy_usb); err_usb_psy_register: power_supply_unregister(&battery->psy_battery); err_battery_psy_register: destroy_workqueue(battery->p3_TA_workqueue); err_workqueue_init: wake_lock_destroy(&battery->vbus_wake_lock); wake_lock_destroy(&battery->work_wake_lock); wake_lock_destroy(&battery->cable_wake_lock); wake_lock_destroy(&battery->fullcharge_wake_lock); mutex_destroy(&battery->work_lock); kfree(battery); return ret; }