Lines Matching refs:ecbat
71 static int yoga_c630_psy_update_bat_info(struct yoga_c630_psy *ecbat) in yoga_c630_psy_update_bat_info() argument
73 struct yoga_c630_ec *ec = ecbat->ec; in yoga_c630_psy_update_bat_info()
76 lockdep_assert_held(&ecbat->lock); in yoga_c630_psy_update_bat_info()
81 ecbat->bat_present = !!(val & LENOVO_EC_BAT_PRESENT_IS_PRESENT); in yoga_c630_psy_update_bat_info()
82 if (!ecbat->bat_present) in yoga_c630_psy_update_bat_info()
88 ecbat->unit_mA = val & LENOVO_EC_BAT_ATTRIBUTES_UNIT_IS_MA; in yoga_c630_psy_update_bat_info()
93 ecbat->design_capacity = val * 1000; in yoga_c630_psy_update_bat_info()
104 ecbat->design_voltage = val; in yoga_c630_psy_update_bat_info()
118 ecbat->full_charge_capacity = val * 1000; in yoga_c630_psy_update_bat_info()
120 if (!ecbat->unit_mA) { in yoga_c630_psy_update_bat_info()
121 ecbat->design_capacity *= 10; in yoga_c630_psy_update_bat_info()
122 ecbat->full_charge_capacity *= 10; in yoga_c630_psy_update_bat_info()
128 static int yoga_c630_psy_maybe_update_bat_status(struct yoga_c630_psy *ecbat) in yoga_c630_psy_maybe_update_bat_status() argument
130 struct yoga_c630_ec *ec = ecbat->ec; in yoga_c630_psy_maybe_update_bat_status()
134 guard(mutex)(&ecbat->lock); in yoga_c630_psy_maybe_update_bat_status()
135 if (time_before(jiffies, ecbat->last_status_update + LENOVO_EC_CACHE_TIME)) in yoga_c630_psy_maybe_update_bat_status()
141 ecbat->bat_status = val; in yoga_c630_psy_maybe_update_bat_status()
148 ecbat->capacity_now = val * 1000; in yoga_c630_psy_maybe_update_bat_status()
155 ecbat->voltage_now = val * 1000; in yoga_c630_psy_maybe_update_bat_status()
163 ecbat->current_now = current_mA * 1000; in yoga_c630_psy_maybe_update_bat_status()
164 ecbat->rate_now = current_mA * (ecbat->voltage_now / 1000); in yoga_c630_psy_maybe_update_bat_status()
168 if (!ecbat->unit_mA) in yoga_c630_psy_maybe_update_bat_status()
169 ecbat->capacity_now *= 10; in yoga_c630_psy_maybe_update_bat_status()
171 ecbat->last_status_update = jiffies; in yoga_c630_psy_maybe_update_bat_status()
176 static int yoga_c630_psy_update_adapter_status(struct yoga_c630_psy *ecbat) in yoga_c630_psy_update_adapter_status() argument
178 struct yoga_c630_ec *ec = ecbat->ec; in yoga_c630_psy_update_adapter_status()
181 guard(mutex)(&ecbat->lock); in yoga_c630_psy_update_adapter_status()
187 ecbat->adapter_online = !!(val & LENOVO_EC_ADPT_STATUS_PRESENT); in yoga_c630_psy_update_adapter_status()
192 static bool yoga_c630_psy_is_charged(struct yoga_c630_psy *ecbat) in yoga_c630_psy_is_charged() argument
194 if (ecbat->bat_status != 0) in yoga_c630_psy_is_charged()
197 if (ecbat->full_charge_capacity <= ecbat->capacity_now) in yoga_c630_psy_is_charged()
200 if (ecbat->design_capacity <= ecbat->capacity_now) in yoga_c630_psy_is_charged()
210 struct yoga_c630_psy *ecbat = power_supply_get_drvdata(psy); in yoga_c630_psy_bat_get_property() local
213 if (!ecbat->bat_present && psp != POWER_SUPPLY_PROP_PRESENT) in yoga_c630_psy_bat_get_property()
216 rc = yoga_c630_psy_maybe_update_bat_status(ecbat); in yoga_c630_psy_bat_get_property()
222 if (ecbat->bat_status & LENOVO_EC_BAT_STATUS_DISCHARGING) in yoga_c630_psy_bat_get_property()
224 else if (ecbat->bat_status & LENOVO_EC_BAT_STATUS_CHARGING) in yoga_c630_psy_bat_get_property()
226 else if (yoga_c630_psy_is_charged(ecbat)) in yoga_c630_psy_bat_get_property()
232 val->intval = ecbat->bat_present; in yoga_c630_psy_bat_get_property()
235 val->intval = ecbat->design_voltage; in yoga_c630_psy_bat_get_property()
239 val->intval = ecbat->design_capacity; in yoga_c630_psy_bat_get_property()
243 val->intval = ecbat->full_charge_capacity; in yoga_c630_psy_bat_get_property()
247 val->intval = ecbat->capacity_now; in yoga_c630_psy_bat_get_property()
250 val->intval = ecbat->current_now; in yoga_c630_psy_bat_get_property()
253 val->intval = ecbat->rate_now; in yoga_c630_psy_bat_get_property()
256 val->intval = ecbat->voltage_now; in yoga_c630_psy_bat_get_property()
330 struct yoga_c630_psy *ecbat = power_supply_get_drvdata(psy); in yoga_c630_psy_adpt_get_property() local
333 ret = yoga_c630_psy_update_adapter_status(ecbat); in yoga_c630_psy_adpt_get_property()
339 val->intval = ecbat->adapter_online; in yoga_c630_psy_adpt_get_property()
365 static int yoga_c630_psy_register_bat_psy(struct yoga_c630_psy *ecbat) in yoga_c630_psy_register_bat_psy() argument
369 bat_cfg.drv_data = ecbat; in yoga_c630_psy_register_bat_psy()
370 bat_cfg.fwnode = ecbat->fwnode; in yoga_c630_psy_register_bat_psy()
372 ecbat->bat_psy = power_supply_register(ecbat->dev, in yoga_c630_psy_register_bat_psy()
373 ecbat->unit_mA ? in yoga_c630_psy_register_bat_psy()
377 if (IS_ERR(ecbat->bat_psy)) { in yoga_c630_psy_register_bat_psy()
378 dev_err(ecbat->dev, "failed to register battery supply\n"); in yoga_c630_psy_register_bat_psy()
379 return PTR_ERR(ecbat->bat_psy); in yoga_c630_psy_register_bat_psy()
385 static void yoga_c630_ec_refresh_bat_info(struct yoga_c630_psy *ecbat) in yoga_c630_ec_refresh_bat_info() argument
389 guard(mutex)(&ecbat->lock); in yoga_c630_ec_refresh_bat_info()
391 current_unit = ecbat->unit_mA; in yoga_c630_ec_refresh_bat_info()
393 yoga_c630_psy_update_bat_info(ecbat); in yoga_c630_ec_refresh_bat_info()
395 if (current_unit != ecbat->unit_mA) { in yoga_c630_ec_refresh_bat_info()
396 power_supply_unregister(ecbat->bat_psy); in yoga_c630_ec_refresh_bat_info()
397 yoga_c630_psy_register_bat_psy(ecbat); in yoga_c630_ec_refresh_bat_info()
404 struct yoga_c630_psy *ecbat = container_of(nb, struct yoga_c630_psy, nb); in yoga_c630_psy_notify() local
408 yoga_c630_ec_refresh_bat_info(ecbat); in yoga_c630_psy_notify()
411 power_supply_changed(ecbat->adp_psy); in yoga_c630_psy_notify()
414 power_supply_changed(ecbat->bat_psy); in yoga_c630_psy_notify()
427 struct yoga_c630_psy *ecbat; in yoga_c630_psy_probe() local
430 ecbat = devm_kzalloc(&adev->dev, sizeof(*ecbat), GFP_KERNEL); in yoga_c630_psy_probe()
431 if (!ecbat) in yoga_c630_psy_probe()
434 ecbat->ec = ec; in yoga_c630_psy_probe()
435 ecbat->dev = dev; in yoga_c630_psy_probe()
436 mutex_init(&ecbat->lock); in yoga_c630_psy_probe()
437 ecbat->fwnode = adev->dev.parent->fwnode; in yoga_c630_psy_probe()
438 ecbat->nb.notifier_call = yoga_c630_psy_notify; in yoga_c630_psy_probe()
440 auxiliary_set_drvdata(adev, ecbat); in yoga_c630_psy_probe()
442 adp_cfg.drv_data = ecbat; in yoga_c630_psy_probe()
443 adp_cfg.fwnode = ecbat->fwnode; in yoga_c630_psy_probe()
447 ecbat->adp_psy = devm_power_supply_register(dev, &yoga_c630_psy_adpt_psy_desc, &adp_cfg); in yoga_c630_psy_probe()
448 if (IS_ERR(ecbat->adp_psy)) { in yoga_c630_psy_probe()
450 return PTR_ERR(ecbat->adp_psy); in yoga_c630_psy_probe()
453 scoped_guard(mutex, &ecbat->lock) { in yoga_c630_psy_probe()
454 ret = yoga_c630_psy_update_bat_info(ecbat); in yoga_c630_psy_probe()
458 ret = yoga_c630_psy_register_bat_psy(ecbat); in yoga_c630_psy_probe()
463 ret = yoga_c630_ec_register_notify(ecbat->ec, &ecbat->nb); in yoga_c630_psy_probe()
470 power_supply_unregister(ecbat->bat_psy); in yoga_c630_psy_probe()
476 struct yoga_c630_psy *ecbat = auxiliary_get_drvdata(adev); in yoga_c630_psy_remove() local
478 yoga_c630_ec_unregister_notify(ecbat->ec, &ecbat->nb); in yoga_c630_psy_remove()
479 power_supply_unregister(ecbat->bat_psy); in yoga_c630_psy_remove()