Lines Matching +full:bypass +full:- +full:ovp +full:- +full:limit +full:- +full:microvolt

1 // SPDX-License-Identifier: GPL-2.0
3 // Copyright (C) 2020 Texas Instruments Incorporated - http://www.ti.com/
24 bool ovp; member
31 bool bypass; member
280 ret = regmap_read(bq->regmap, BQ25980_BUSOCP, &busocp_reg_code); in bq25980_get_input_curr_lim()
289 return regmap_update_bits(bq->regmap, BQ25980_CHRGR_CTRL_2, in bq25980_set_hiz()
306 if (bq->state.bypass) in bq25980_set_input_curr_lim()
307 busocp = min(busocp, bq->chip_info->busocp_sc_max); in bq25980_set_input_curr_lim()
309 busocp = min(busocp, bq->chip_info->busocp_byp_max); in bq25980_set_input_curr_lim()
311 busocp_reg_code = (busocp - BQ25980_BUSOCP_OFFSET_uA) in bq25980_set_input_curr_lim()
314 ret = regmap_write(bq->regmap, BQ25980_BUSOCP, busocp_reg_code); in bq25980_set_input_curr_lim()
318 return regmap_write(bq->regmap, BQ25980_BUSOCP_ALM, busocp_reg_code); in bq25980_set_input_curr_lim()
328 if (bq->state.bypass) { in bq25980_get_input_volt_lim()
329 busovp_step = bq->chip_info->busovp_byp_step; in bq25980_get_input_volt_lim()
330 busovp_offset = bq->chip_info->busovp_byp_offset; in bq25980_get_input_volt_lim()
332 busovp_step = bq->chip_info->busovp_sc_step; in bq25980_get_input_volt_lim()
333 busovp_offset = bq->chip_info->busovp_sc_offset; in bq25980_get_input_volt_lim()
336 ret = regmap_read(bq->regmap, BQ25980_BUSOVP, &busovp_reg_code); in bq25980_get_input_volt_lim()
350 if (bq->state.bypass) { in bq25980_set_input_volt_lim()
351 busovp_step = bq->chip_info->busovp_byp_step; in bq25980_set_input_volt_lim()
352 busovp_offset = bq->chip_info->busovp_byp_offset; in bq25980_set_input_volt_lim()
353 if (busovp > bq->chip_info->busovp_byp_max) in bq25980_set_input_volt_lim()
354 busovp = bq->chip_info->busovp_byp_max; in bq25980_set_input_volt_lim()
355 else if (busovp < bq->chip_info->busovp_byp_min) in bq25980_set_input_volt_lim()
356 busovp = bq->chip_info->busovp_byp_min; in bq25980_set_input_volt_lim()
358 busovp_step = bq->chip_info->busovp_sc_step; in bq25980_set_input_volt_lim()
359 busovp_offset = bq->chip_info->busovp_sc_offset; in bq25980_set_input_volt_lim()
360 if (busovp > bq->chip_info->busovp_sc_max) in bq25980_set_input_volt_lim()
361 busovp = bq->chip_info->busovp_sc_max; in bq25980_set_input_volt_lim()
362 else if (busovp < bq->chip_info->busovp_sc_min) in bq25980_set_input_volt_lim()
363 busovp = bq->chip_info->busovp_sc_min; in bq25980_set_input_volt_lim()
366 busovp_reg_code = (busovp - busovp_offset) / busovp_step; in bq25980_set_input_volt_lim()
368 ret = regmap_write(bq->regmap, BQ25980_BUSOVP, busovp_reg_code); in bq25980_set_input_volt_lim()
372 return regmap_write(bq->regmap, BQ25980_BUSOVP_ALM, busovp_reg_code); in bq25980_set_input_volt_lim()
380 ret = regmap_read(bq->regmap, BQ25980_BATOCP, &batocp_reg_code); in bq25980_get_const_charge_curr()
394 batocp = min(batocp, bq->chip_info->batocp_max); in bq25980_set_const_charge_curr()
398 ret = regmap_update_bits(bq->regmap, BQ25980_BATOCP, in bq25980_set_const_charge_curr()
403 return regmap_update_bits(bq->regmap, BQ25980_BATOCP_ALM, in bq25980_set_const_charge_curr()
412 ret = regmap_read(bq->regmap, BQ25980_BATOVP, &batovp_reg_code); in bq25980_get_const_charge_volt()
416 return ((batovp_reg_code * bq->chip_info->batovp_step) + in bq25980_get_const_charge_volt()
417 bq->chip_info->batovp_offset); in bq25980_get_const_charge_volt()
425 if (batovp < bq->chip_info->batovp_min) in bq25980_set_const_charge_volt()
426 batovp = bq->chip_info->batovp_min; in bq25980_set_const_charge_volt()
428 if (batovp > bq->chip_info->batovp_max) in bq25980_set_const_charge_volt()
429 batovp = bq->chip_info->batovp_max; in bq25980_set_const_charge_volt()
431 batovp_reg_code = (batovp - bq->chip_info->batovp_offset) / in bq25980_set_const_charge_volt()
432 bq->chip_info->batovp_step; in bq25980_set_const_charge_volt()
434 ret = regmap_write(bq->regmap, BQ25980_BATOVP, batovp_reg_code); in bq25980_set_const_charge_volt()
438 return regmap_write(bq->regmap, BQ25980_BATOVP_ALM, batovp_reg_code); in bq25980_set_const_charge_volt()
446 ret = regmap_update_bits(bq->regmap, BQ25980_CHRGR_CTRL_2, in bq25980_set_bypass()
449 ret = regmap_update_bits(bq->regmap, BQ25980_CHRGR_CTRL_2, in bq25980_set_bypass()
454 bq->state.bypass = en_bypass; in bq25980_set_bypass()
456 return bq->state.bypass; in bq25980_set_bypass()
464 ret = regmap_update_bits(bq->regmap, BQ25980_CHRGR_CTRL_2, in bq25980_set_chg_en()
467 ret = regmap_update_bits(bq->regmap, BQ25980_CHRGR_CTRL_2, in bq25980_set_chg_en()
472 bq->state.ce = en_chg; in bq25980_set_chg_en()
483 ret = regmap_read(bq->regmap, BQ25980_IBUS_ADC_MSB, &ibus_adc_msb); in bq25980_get_adc_ibus()
487 ret = regmap_read(bq->regmap, BQ25980_IBUS_ADC_LSB, &ibus_adc_lsb); in bq25980_get_adc_ibus()
505 ret = regmap_read(bq->regmap, BQ25980_VBUS_ADC_MSB, &vbus_adc_msb); in bq25980_get_adc_vbus()
509 ret = regmap_read(bq->regmap, BQ25980_VBUS_ADC_LSB, &vbus_adc_lsb); in bq25980_get_adc_vbus()
524 ret = regmap_read(bq->regmap, BQ25980_IBAT_ADC_MSB, &ibat_adc_msb); in bq25980_get_ibat_adc()
528 ret = regmap_read(bq->regmap, BQ25980_IBAT_ADC_LSB, &ibat_adc_lsb); in bq25980_get_ibat_adc()
546 ret = regmap_read(bq->regmap, BQ25980_VBAT_ADC_MSB, &vsys_adc_msb); in bq25980_get_adc_vbat()
550 ret = regmap_read(bq->regmap, BQ25980_VBAT_ADC_LSB, &vsys_adc_lsb); in bq25980_get_adc_vbat()
570 ret = regmap_read(bq->regmap, BQ25980_STAT1, &stat1); in bq25980_get_state()
574 ret = regmap_read(bq->regmap, BQ25980_STAT2, &stat2); in bq25980_get_state()
578 ret = regmap_read(bq->regmap, BQ25980_STAT3, &stat3); in bq25980_get_state()
582 ret = regmap_read(bq->regmap, BQ25980_STAT4, &stat4); in bq25980_get_state()
586 ret = regmap_read(bq->regmap, BQ25980_CHRGR_CTRL_2, &chg_ctrl_2); in bq25980_get_state()
590 ret = regmap_read(bq->regmap, BQ25980_IBAT_ADC_MSB, &ibat_adc_msb); in bq25980_get_state()
594 state->dischg = ibat_adc_msb & BQ25980_ADC_POLARITY_BIT; in bq25980_get_state()
595 state->ovp = (stat1 & BQ25980_STAT1_OVP_MASK) | in bq25980_get_state()
597 state->ocp = (stat1 & BQ25980_STAT1_OCP_MASK) | in bq25980_get_state()
599 state->tflt = stat4 & BQ25980_STAT4_TFLT_MASK; in bq25980_get_state()
600 state->wdt = stat4 & BQ25980_WD_STAT; in bq25980_get_state()
601 state->online = stat3 & BQ25980_PRESENT_MASK; in bq25980_get_state()
602 state->ce = chg_ctrl_2 & BQ25980_CHG_EN; in bq25980_get_state()
603 state->hiz = chg_ctrl_2 & BQ25980_EN_HIZ; in bq25980_get_state()
604 state->bypass = chg_ctrl_2 & BQ25980_EN_BYPASS; in bq25980_get_state()
618 ret = bq25980_set_const_charge_curr(bq, val->intval); in bq25980_set_battery_property()
624 ret = bq25980_set_const_charge_volt(bq, val->intval); in bq25980_set_battery_property()
630 return -EINVAL; in bq25980_set_battery_property()
645 val->intval = bq->init_data.ichg_max; in bq25980_get_battery_property()
649 val->intval = bq->init_data.vreg_max; in bq25980_get_battery_property()
654 val->intval = ret; in bq25980_get_battery_property()
662 val->intval = ret; in bq25980_get_battery_property()
666 return -EINVAL; in bq25980_get_battery_property()
677 int ret = -EINVAL; in bq25980_set_charger_property()
681 ret = bq25980_set_input_curr_lim(bq, val->intval); in bq25980_set_charger_property()
687 ret = bq25980_set_input_volt_lim(bq, val->intval); in bq25980_set_charger_property()
693 ret = bq25980_set_bypass(bq, val->intval); in bq25980_set_charger_property()
699 ret = bq25980_set_chg_en(bq, val->intval); in bq25980_set_charger_property()
705 return -EINVAL; in bq25980_set_charger_property()
719 mutex_lock(&bq->lock); in bq25980_get_charger_property()
721 mutex_unlock(&bq->lock); in bq25980_get_charger_property()
727 val->strval = BQ25980_MANUFACTURER; in bq25980_get_charger_property()
730 val->strval = bq->model_name; in bq25980_get_charger_property()
733 val->intval = state.online; in bq25980_get_charger_property()
740 val->intval = ret; in bq25980_get_charger_property()
748 val->intval = ret; in bq25980_get_charger_property()
752 val->intval = POWER_SUPPLY_HEALTH_GOOD; in bq25980_get_charger_property()
755 val->intval = POWER_SUPPLY_HEALTH_OVERHEAT; in bq25980_get_charger_property()
756 else if (state.ovp) in bq25980_get_charger_property()
757 val->intval = POWER_SUPPLY_HEALTH_OVERVOLTAGE; in bq25980_get_charger_property()
759 val->intval = POWER_SUPPLY_HEALTH_OVERCURRENT; in bq25980_get_charger_property()
761 val->intval = in bq25980_get_charger_property()
766 val->intval = POWER_SUPPLY_STATUS_UNKNOWN; in bq25980_get_charger_property()
769 val->intval = POWER_SUPPLY_STATUS_CHARGING; in bq25980_get_charger_property()
771 val->intval = POWER_SUPPLY_STATUS_DISCHARGING; in bq25980_get_charger_property()
773 val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING; in bq25980_get_charger_property()
777 val->intval = POWER_SUPPLY_CHARGE_TYPE_UNKNOWN; in bq25980_get_charger_property()
780 val->intval = POWER_SUPPLY_CHARGE_TYPE_NONE; in bq25980_get_charger_property()
781 else if (state.bypass) in bq25980_get_charger_property()
782 val->intval = POWER_SUPPLY_CHARGE_TYPE_FAST; in bq25980_get_charger_property()
783 else if (!state.bypass) in bq25980_get_charger_property()
784 val->intval = POWER_SUPPLY_CHARGE_TYPE_STANDARD; in bq25980_get_charger_property()
792 val->intval = ret; in bq25980_get_charger_property()
800 val->intval = ret; in bq25980_get_charger_property()
808 val->intval = ret; in bq25980_get_charger_property()
816 val->intval = ret; in bq25980_get_charger_property()
820 return -EINVAL; in bq25980_get_charger_property()
831 mutex_lock(&bq->lock); in bq25980_state_changed()
832 old_state = bq->state; in bq25980_state_changed()
833 mutex_unlock(&bq->lock); in bq25980_state_changed()
835 return (old_state.dischg != new_state->dischg || in bq25980_state_changed()
836 old_state.ovp != new_state->ovp || in bq25980_state_changed()
837 old_state.ocp != new_state->ocp || in bq25980_state_changed()
838 old_state.online != new_state->online || in bq25980_state_changed()
839 old_state.wdt != new_state->wdt || in bq25980_state_changed()
840 old_state.tflt != new_state->tflt || in bq25980_state_changed()
841 old_state.ce != new_state->ce || in bq25980_state_changed()
842 old_state.hiz != new_state->hiz || in bq25980_state_changed()
843 old_state.bypass != new_state->bypass); in bq25980_state_changed()
859 mutex_lock(&bq->lock); in bq25980_irq_handler_thread()
860 bq->state = state; in bq25980_irq_handler_thread()
861 mutex_unlock(&bq->lock); in bq25980_irq_handler_thread()
863 power_supply_changed(bq->charger); in bq25980_irq_handler_thread()
892 "main-battery",
912 .name = "bq25980-charger",
922 .name = "bq25980-battery",
1076 .of_node = dev->of_node, }; in bq25980_power_supply_init()
1081 bq->charger = devm_power_supply_register(bq->dev, in bq25980_power_supply_init()
1084 if (IS_ERR(bq->charger)) in bq25980_power_supply_init()
1085 return -EINVAL; in bq25980_power_supply_init()
1087 bq->battery = devm_power_supply_register(bq->dev, in bq25980_power_supply_init()
1090 if (IS_ERR(bq->battery)) in bq25980_power_supply_init()
1091 return -EINVAL; in bq25980_power_supply_init()
1100 int wd_max_val = BQ25980_NUM_WD_VAL - 1; in bq25980_hw_init()
1106 if (bq->watchdog_timer) { in bq25980_hw_init()
1107 if (bq->watchdog_timer >= bq25980_watchdog_time[wd_max_val]) in bq25980_hw_init()
1111 if (bq->watchdog_timer > bq25980_watchdog_time[i] && in bq25980_hw_init()
1112 bq->watchdog_timer < bq25980_watchdog_time[i + 1]) { in bq25980_hw_init()
1120 ret = regmap_update_bits(bq->regmap, BQ25980_CHRGR_CTRL_3, in bq25980_hw_init()
1125 ret = power_supply_get_battery_info(bq->charger, &bat_info); in bq25980_hw_init()
1127 dev_warn(bq->dev, "battery info missing\n"); in bq25980_hw_init()
1128 return -EINVAL; in bq25980_hw_init()
1131 bq->init_data.ichg_max = bat_info.constant_charge_current_max_ua; in bq25980_hw_init()
1132 bq->init_data.vreg_max = bat_info.constant_charge_voltage_max_uv; in bq25980_hw_init()
1134 if (bq->state.bypass) { in bq25980_hw_init()
1135 ret = regmap_update_bits(bq->regmap, BQ25980_CHRGR_CTRL_2, in bq25980_hw_init()
1140 curr_val = bq->init_data.bypass_ilim; in bq25980_hw_init()
1141 volt_val = bq->init_data.bypass_vlim; in bq25980_hw_init()
1143 curr_val = bq->init_data.sc_ilim; in bq25980_hw_init()
1144 volt_val = bq->init_data.sc_vlim; in bq25980_hw_init()
1155 return regmap_update_bits(bq->regmap, BQ25980_ADC_CONTROL1, in bq25980_hw_init()
1163 ret = device_property_read_u32(bq->dev, "ti,watchdog-timeout-ms", in bq25980_parse_dt()
1164 &bq->watchdog_timer); in bq25980_parse_dt()
1166 bq->watchdog_timer = BQ25980_WATCHDOG_MIN; in bq25980_parse_dt()
1168 if (bq->watchdog_timer > BQ25980_WATCHDOG_MAX || in bq25980_parse_dt()
1169 bq->watchdog_timer < BQ25980_WATCHDOG_MIN) in bq25980_parse_dt()
1170 return -EINVAL; in bq25980_parse_dt()
1172 ret = device_property_read_u32(bq->dev, in bq25980_parse_dt()
1173 "ti,sc-ovp-limit-microvolt", in bq25980_parse_dt()
1174 &bq->init_data.sc_vlim); in bq25980_parse_dt()
1176 bq->init_data.sc_vlim = bq->chip_info->busovp_sc_def; in bq25980_parse_dt()
1178 if (bq->init_data.sc_vlim > bq->chip_info->busovp_sc_max || in bq25980_parse_dt()
1179 bq->init_data.sc_vlim < bq->chip_info->busovp_sc_min) { in bq25980_parse_dt()
1180 dev_err(bq->dev, "SC ovp limit is out of range\n"); in bq25980_parse_dt()
1181 return -EINVAL; in bq25980_parse_dt()
1184 ret = device_property_read_u32(bq->dev, in bq25980_parse_dt()
1185 "ti,sc-ocp-limit-microamp", in bq25980_parse_dt()
1186 &bq->init_data.sc_ilim); in bq25980_parse_dt()
1188 bq->init_data.sc_ilim = bq->chip_info->busocp_def; in bq25980_parse_dt()
1190 if (bq->init_data.sc_ilim > bq->chip_info->busocp_sc_max || in bq25980_parse_dt()
1191 bq->init_data.sc_ilim < bq->chip_info->busocp_sc_min) { in bq25980_parse_dt()
1192 dev_err(bq->dev, "SC ocp limit is out of range\n"); in bq25980_parse_dt()
1193 return -EINVAL; in bq25980_parse_dt()
1196 ret = device_property_read_u32(bq->dev, in bq25980_parse_dt()
1197 "ti,bypass-ovp-limit-microvolt", in bq25980_parse_dt()
1198 &bq->init_data.bypass_vlim); in bq25980_parse_dt()
1200 bq->init_data.bypass_vlim = bq->chip_info->busovp_byp_def; in bq25980_parse_dt()
1202 if (bq->init_data.bypass_vlim > bq->chip_info->busovp_byp_max || in bq25980_parse_dt()
1203 bq->init_data.bypass_vlim < bq->chip_info->busovp_byp_min) { in bq25980_parse_dt()
1204 dev_err(bq->dev, "Bypass ovp limit is out of range\n"); in bq25980_parse_dt()
1205 return -EINVAL; in bq25980_parse_dt()
1208 ret = device_property_read_u32(bq->dev, in bq25980_parse_dt()
1209 "ti,bypass-ocp-limit-microamp", in bq25980_parse_dt()
1210 &bq->init_data.bypass_ilim); in bq25980_parse_dt()
1212 bq->init_data.bypass_ilim = bq->chip_info->busocp_def; in bq25980_parse_dt()
1214 if (bq->init_data.bypass_ilim > bq->chip_info->busocp_byp_max || in bq25980_parse_dt()
1215 bq->init_data.bypass_ilim < bq->chip_info->busocp_byp_min) { in bq25980_parse_dt()
1216 dev_err(bq->dev, "Bypass ocp limit is out of range\n"); in bq25980_parse_dt()
1217 return -EINVAL; in bq25980_parse_dt()
1221 bq->state.bypass = device_property_read_bool(bq->dev, in bq25980_parse_dt()
1222 "ti,bypass-enable"); in bq25980_parse_dt()
1229 struct device *dev = &client->dev; in bq25980_probe()
1235 return -ENOMEM; in bq25980_probe()
1237 bq->client = client; in bq25980_probe()
1238 bq->dev = dev; in bq25980_probe()
1240 mutex_init(&bq->lock); in bq25980_probe()
1242 strncpy(bq->model_name, id->name, I2C_NAME_SIZE); in bq25980_probe()
1243 bq->chip_info = &bq25980_chip_info_tbl[id->driver_data]; in bq25980_probe()
1245 bq->regmap = devm_regmap_init_i2c(client, in bq25980_probe()
1246 bq->chip_info->regmap_config); in bq25980_probe()
1247 if (IS_ERR(bq->regmap)) { in bq25980_probe()
1249 return PTR_ERR(bq->regmap); in bq25980_probe()
1260 if (client->irq) { in bq25980_probe()
1261 ret = devm_request_threaded_irq(dev, client->irq, NULL, in bq25980_probe()
1265 dev_name(&client->dev), bq); in bq25980_probe()
1303 .name = "bq25980-charger",
1312 MODULE_AUTHOR("Ricardo Rivera-Matos <r-rivera-matos@ti.com>");