Lines Matching +full:battery +full:- +full:profile

1 // SPDX-License-Identifier: GPL-2.0
8 * Authors: xuhuicong <xhc@rock-chips.com>
24 #include <linux/devm-helpers.h>
65 struct power_supply_battery_info *battery; member
89 ret = regmap_bulk_read(cw_bat->regmap, reg, &value, sizeof(value)); in cw_read_word()
104 ret = regmap_read(cw_bat->regmap, CW2015_REG_MODE, &reg_val); in cw_update_profile()
110 dev_err(cw_bat->dev, in cw_update_profile()
111 "Gauge is in sleep mode, can't update battery info\n"); in cw_update_profile()
112 return -EINVAL; in cw_update_profile()
115 /* write new battery info */ in cw_update_profile()
116 ret = regmap_raw_write(cw_bat->regmap, CW2015_REG_BATINFO, in cw_update_profile()
117 cw_bat->bat_profile, in cw_update_profile()
125 reg_val |= CW2015_ATHD(cw_bat->alert_level); in cw_update_profile()
126 ret = regmap_write(cw_bat->regmap, CW2015_REG_CONFIG, reg_val); in cw_update_profile()
130 /* reset gauge to apply new battery profile */ in cw_update_profile()
133 ret = regmap_write(cw_bat->regmap, CW2015_REG_MODE, reg_val); in cw_update_profile()
141 ret = regmap_write(cw_bat->regmap, CW2015_REG_MODE, reset_val); in cw_update_profile()
146 ret = regmap_read_poll_timeout(cw_bat->regmap, CW2015_REG_SOC, in cw_update_profile()
150 dev_err(cw_bat->dev, in cw_update_profile()
151 "Gauge did not become ready after profile upload\n"); in cw_update_profile()
153 dev_dbg(cw_bat->dev, "Battery profile updated\n"); in cw_update_profile()
165 ret = regmap_write(cw_bat->regmap, CW2015_REG_MODE, reg_val); in cw_init()
170 ret = regmap_read(cw_bat->regmap, CW2015_REG_CONFIG, &reg_val); in cw_init()
174 if ((reg_val & CW2015_MASK_ATHD) != CW2015_ATHD(cw_bat->alert_level)) { in cw_init()
175 dev_dbg(cw_bat->dev, "Setting new alert level\n"); in cw_init()
177 reg_val |= ~CW2015_ATHD(cw_bat->alert_level); in cw_init()
178 ret = regmap_write(cw_bat->regmap, CW2015_REG_CONFIG, reg_val); in cw_init()
183 ret = regmap_read(cw_bat->regmap, CW2015_REG_CONFIG, &reg_val); in cw_init()
188 dev_dbg(cw_bat->dev, in cw_init()
189 "Battery profile not present, uploading battery profile\n"); in cw_init()
190 if (cw_bat->bat_profile) { in cw_init()
193 dev_err(cw_bat->dev, in cw_init()
194 "Failed to upload battery profile\n"); in cw_init()
198 dev_warn(cw_bat->dev, in cw_init()
199 "No profile specified, continuing without profile\n"); in cw_init()
201 } else if (cw_bat->bat_profile) { in cw_init()
204 ret = regmap_raw_read(cw_bat->regmap, CW2015_REG_BATINFO, in cw_init()
207 dev_err(cw_bat->dev, in cw_init()
208 "Failed to read stored battery profile\n"); in cw_init()
212 if (memcmp(bat_info, cw_bat->bat_profile, CW2015_SIZE_BATINFO)) { in cw_init()
213 dev_warn(cw_bat->dev, "Replacing stored battery profile\n"); in cw_init()
219 dev_warn(cw_bat->dev, in cw_init()
220 "Can't check current battery profile, no profile provided\n"); in cw_init()
223 dev_dbg(cw_bat->dev, "Battery profile configured\n"); in cw_init()
233 ret = regmap_write(cw_bat->regmap, CW2015_REG_MODE, reset_val); in cw_power_on_reset()
241 ret = regmap_write(cw_bat->regmap, CW2015_REG_MODE, reset_val); in cw_power_on_reset()
252 (((current) < (previous) + (up)) && ((current) > (previous) - (down)))
259 ret = regmap_read(cw_bat->regmap, CW2015_REG_SOC, &soc); in cw_get_soc()
265 CW2015_BAT_SOC_ERROR_MS / cw_bat->poll_interval_ms; in cw_get_soc()
267 dev_err(cw_bat->dev, "Invalid SoC %d%%\n", soc); in cw_get_soc()
268 cw_bat->read_errors++; in cw_get_soc()
269 if (cw_bat->read_errors > max_error_cycles) { in cw_get_soc()
270 dev_warn(cw_bat->dev, in cw_get_soc()
273 cw_bat->read_errors = 0; in cw_get_soc()
275 return cw_bat->soc; in cw_get_soc()
277 cw_bat->read_errors = 0; in cw_get_soc()
280 if (cw_bat->status == POWER_SUPPLY_STATUS_CHARGING && soc == cw_bat->soc) { in cw_get_soc()
282 CW2015_BAT_CHARGING_STUCK_MS / cw_bat->poll_interval_ms; in cw_get_soc()
284 cw_bat->charge_stuck_cnt++; in cw_get_soc()
285 if (cw_bat->charge_stuck_cnt > max_stuck_cycles) { in cw_get_soc()
286 dev_warn(cw_bat->dev, in cw_get_soc()
289 cw_bat->charge_stuck_cnt = 0; in cw_get_soc()
292 cw_bat->charge_stuck_cnt = 0; in cw_get_soc()
296 if (cw_bat->charger_attached && HYSTERESIS(soc, cw_bat->soc, 0, 3)) in cw_get_soc()
297 soc = cw_bat->soc; in cw_get_soc()
300 if (!cw_bat->charger_attached && HYSTERESIS(soc, cw_bat->soc, 3, 0)) in cw_get_soc()
301 soc = cw_bat->soc; in cw_get_soc()
328 dev_dbg(cw_bat->dev, "Read voltage: %d mV, raw=0x%04x\n", in cw_get_voltage()
349 ret = power_supply_am_i_supplied(cw_bat->rk_bat); in cw_update_charge_status()
351 dev_warn(cw_bat->dev, "Failed to get supply state: %d\n", ret); in cw_update_charge_status()
356 if (cw_bat->charger_attached != charger_attached) { in cw_update_charge_status()
357 cw_bat->battery_changed = true; in cw_update_charge_status()
359 cw_bat->charge_count++; in cw_update_charge_status()
361 cw_bat->charger_attached = charger_attached; in cw_update_charge_status()
371 dev_err(cw_bat->dev, "Failed to get SoC from gauge: %d\n", soc); in cw_update_soc()
372 else if (cw_bat->soc != soc) { in cw_update_soc()
373 cw_bat->soc = soc; in cw_update_soc()
374 cw_bat->battery_changed = true; in cw_update_soc()
384 dev_err(cw_bat->dev, "Failed to get voltage from gauge: %d\n", in cw_update_voltage()
387 cw_bat->voltage_mv = voltage_mv; in cw_update_voltage()
394 if (cw_bat->charger_attached) { in cw_update_status()
395 if (cw_bat->soc >= 100) in cw_update_status()
401 if (cw_bat->status != status) in cw_update_status()
402 cw_bat->battery_changed = true; in cw_update_status()
403 cw_bat->status = status; in cw_update_status()
412 dev_err(cw_bat->dev, "Failed to get time to empty from gauge: %d\n", in cw_update_time_to_empty()
414 else if (cw_bat->time_to_empty != time_to_empty) { in cw_update_time_to_empty()
415 cw_bat->time_to_empty = time_to_empty; in cw_update_time_to_empty()
416 cw_bat->battery_changed = true; in cw_update_time_to_empty()
429 ret = regmap_read(cw_bat->regmap, CW2015_REG_MODE, &reg_val); in cw_bat_work()
431 dev_err(cw_bat->dev, "Failed to read mode from gauge: %d\n", ret); in cw_bat_work()
447 dev_dbg(cw_bat->dev, "charger_attached = %d\n", cw_bat->charger_attached); in cw_bat_work()
448 dev_dbg(cw_bat->dev, "status = %d\n", cw_bat->status); in cw_bat_work()
449 dev_dbg(cw_bat->dev, "soc = %d%%\n", cw_bat->soc); in cw_bat_work()
450 dev_dbg(cw_bat->dev, "voltage = %dmV\n", cw_bat->voltage_mv); in cw_bat_work()
452 if (cw_bat->battery_changed) in cw_bat_work()
453 power_supply_changed(cw_bat->rk_bat); in cw_bat_work()
454 cw_bat->battery_changed = false; in cw_bat_work()
456 queue_delayed_work(cw_bat->battery_workqueue, in cw_bat_work()
457 &cw_bat->battery_delay_work, in cw_bat_work()
458 msecs_to_jiffies(cw_bat->poll_interval_ms)); in cw_bat_work()
463 return cw_bat->time_to_empty > 0 && in cw_battery_valid_time_to_empty()
464 cw_bat->time_to_empty < CW2015_MASK_SOC && in cw_battery_valid_time_to_empty()
465 cw_bat->status == POWER_SUPPLY_STATUS_DISCHARGING; in cw_battery_valid_time_to_empty()
477 val->intval = cw_bat->soc; in cw_battery_get_property()
481 val->intval = cw_bat->status; in cw_battery_get_property()
485 val->intval = !!cw_bat->voltage_mv; in cw_battery_get_property()
489 val->intval = cw_bat->voltage_mv * 1000; in cw_battery_get_property()
494 val->intval = cw_bat->time_to_empty; in cw_battery_get_property()
496 val->intval = 0; in cw_battery_get_property()
500 val->intval = POWER_SUPPLY_TECHNOLOGY_LION; in cw_battery_get_property()
504 val->intval = cw_bat->charge_count; in cw_battery_get_property()
509 if (cw_bat->battery->charge_full_design_uah > 0) in cw_battery_get_property()
510 val->intval = cw_bat->battery->charge_full_design_uah; in cw_battery_get_property()
512 val->intval = 0; in cw_battery_get_property()
516 val->intval = cw_bat->battery->charge_full_design_uah; in cw_battery_get_property()
517 val->intval = val->intval * cw_bat->soc / 100; in cw_battery_get_property()
522 cw_bat->battery->charge_full_design_uah > 0) { in cw_battery_get_property()
524 val->intval = cw_bat->battery->charge_full_design_uah; in cw_battery_get_property()
525 val->intval = val->intval * cw_bat->soc / 100; in cw_battery_get_property()
528 val->intval = 60 * val->intval / cw_bat->time_to_empty; in cw_battery_get_property()
530 val->intval = 0; in cw_battery_get_property()
556 .name = "cw2015-battery",
565 struct device *dev = cw_bat->dev; in cw2015_parse_properties()
569 length = device_property_count_u8(dev, "cellwise,battery-profile"); in cw2015_parse_properties()
571 dev_warn(cw_bat->dev, in cw2015_parse_properties()
572 "No battery-profile found, using current flash contents\n"); in cw2015_parse_properties()
574 dev_err(cw_bat->dev, "battery-profile must be %d bytes\n", in cw2015_parse_properties()
576 return -EINVAL; in cw2015_parse_properties()
578 cw_bat->bat_profile = devm_kzalloc(dev, length, GFP_KERNEL); in cw2015_parse_properties()
579 if (!cw_bat->bat_profile) in cw2015_parse_properties()
580 return -ENOMEM; in cw2015_parse_properties()
583 "cellwise,battery-profile", in cw2015_parse_properties()
584 cw_bat->bat_profile, in cw2015_parse_properties()
590 ret = device_property_read_u32(dev, "cellwise,monitor-interval-ms", in cw2015_parse_properties()
591 &cw_bat->poll_interval_ms); in cw2015_parse_properties()
593 dev_dbg(cw_bat->dev, "Using default poll interval\n"); in cw2015_parse_properties()
594 cw_bat->poll_interval_ms = CW2015_DEFAULT_POLL_INTERVAL_MS; in cw2015_parse_properties()
605 CW2015_REG_BATINFO + CW2015_SIZE_BATINFO - 1),
617 CW2015_REG_BATINFO + CW2015_SIZE_BATINFO - 1),
640 .max_register = CW2015_REG_BATINFO + CW2015_SIZE_BATINFO - 1,
649 cw_bat = devm_kzalloc(&client->dev, sizeof(*cw_bat), GFP_KERNEL); in cw_bat_probe()
651 return -ENOMEM; in cw_bat_probe()
654 cw_bat->dev = &client->dev; in cw_bat_probe()
655 cw_bat->soc = 1; in cw_bat_probe()
659 dev_err(cw_bat->dev, "Failed to parse cw2015 properties\n"); in cw_bat_probe()
663 cw_bat->regmap = devm_regmap_init_i2c(client, &cw2015_regmap_config); in cw_bat_probe()
664 if (IS_ERR(cw_bat->regmap)) { in cw_bat_probe()
665 dev_err(cw_bat->dev, "Failed to allocate regmap: %ld\n", in cw_bat_probe()
666 PTR_ERR(cw_bat->regmap)); in cw_bat_probe()
667 return PTR_ERR(cw_bat->regmap); in cw_bat_probe()
672 dev_err(cw_bat->dev, "Init failed: %d\n", ret); in cw_bat_probe()
677 psy_cfg.fwnode = dev_fwnode(cw_bat->dev); in cw_bat_probe()
679 cw_bat->rk_bat = devm_power_supply_register(&client->dev, in cw_bat_probe()
682 if (IS_ERR(cw_bat->rk_bat)) { in cw_bat_probe()
684 dev_err_probe(&client->dev, PTR_ERR(cw_bat->rk_bat), in cw_bat_probe()
686 return PTR_ERR(cw_bat->rk_bat); in cw_bat_probe()
689 ret = power_supply_get_battery_info(cw_bat->rk_bat, &cw_bat->battery); in cw_bat_probe()
691 /* Allocate an empty battery */ in cw_bat_probe()
692 cw_bat->battery = devm_kzalloc(&client->dev, in cw_bat_probe()
693 sizeof(*cw_bat->battery), in cw_bat_probe()
695 if (!cw_bat->battery) in cw_bat_probe()
696 return -ENOMEM; in cw_bat_probe()
697 dev_warn(cw_bat->dev, in cw_bat_probe()
698 "No monitored battery, some properties will be missing\n"); in cw_bat_probe()
701 cw_bat->battery_workqueue = create_singlethread_workqueue("rk_battery"); in cw_bat_probe()
702 if (!cw_bat->battery_workqueue) in cw_bat_probe()
703 return -ENOMEM; in cw_bat_probe()
705 devm_delayed_work_autocancel(&client->dev, in cw_bat_probe()
706 &cw_bat->battery_delay_work, cw_bat_work); in cw_bat_probe()
707 queue_delayed_work(cw_bat->battery_workqueue, in cw_bat_probe()
708 &cw_bat->battery_delay_work, msecs_to_jiffies(10)); in cw_bat_probe()
717 cancel_delayed_work_sync(&cw_bat->battery_delay_work); in cw_bat_suspend()
726 queue_delayed_work(cw_bat->battery_workqueue, in cw_bat_resume()
727 &cw_bat->battery_delay_work, 0); in cw_bat_resume()
756 MODULE_AUTHOR("xhc<xhc@rock-chips.com>");
758 MODULE_DESCRIPTION("cw2015/cw2013 battery driver");