• Home
  • Raw
  • Download

Lines Matching +full:current +full:- +full:limit +full:- +full:microamp

1 // SPDX-License-Identifier: GPL-2.0-only
23 #include <dt-bindings/power/summit,smb347-charger.h>
26 #define SMB3XX_SOFT_TEMP_COMPENSATE_DEFAULT -1
28 /* Use default factory programmed value for hard/soft temperature limit */
29 #define SMB3XX_TEMP_USE_DEFAULT -273
34 * reloaded from non-volatile registers after POR.
132 * struct smb347_charger - smb347 charger instance
142 * @max_charge_current: maximum current (in uA) the battery can be charged
144 * @pre_charge_current: current (in uA) to use in pre-charging phase
145 * @termination_current: current (in uA) used to determine when the
148 * pre-charge to fast charge mode
149 * @mains_current_limit: maximum input current drawn from AC/DC input (in uA)
150 * @usb_hc_current_limit: maximum input high current (in uA) drawn from USB
153 * current [%100 - %130] (in degree C)
154 * @soft_cold_temp_limit: soft cold temperature limit [%0 - %15] (in degree C),
156 * @soft_hot_temp_limit: soft hot temperature limit [%40 - %55] (in degree C),
158 * @hard_cold_temp_limit: hard cold temperature limit [%-5 - %10] (in degree C),
160 * @hard_hot_temp_limit: hard hot temperature limit [%50 - %65] (in degree C),
162 * @suspend_on_hard_temp_limit: suspend charging when hard limit is hit
164 * limit is hit
165 * @charge_current_compensation: current (in uA) for charging compensation
166 * current when temperature hits soft limits
178 * Hard and soft temperature limit values are given as described in the
183 * hot limit to be %53 deg C, @hard_hot_temp_limit should be set to %50.
185 * If zero value is given in any of the current and voltage values, the
228 /* Fast charge current in uA */
237 /* Pre-charge current in uA */
244 /* Termination current in uA */
254 /* Input current limit in uA */
264 /* Charge current compensation in uA */
271 /* Convert register value to current using lookup table */
275 return -EINVAL; in hw_to_current()
279 /* Convert current to register value using lookup table */
287 return i > 0 ? i - 1 : -EINVAL; in current_to_hw()
291 * smb347_update_ps_status - refreshes the power source status
305 ret = regmap_read(smb->regmap, IRQSTAT_E, &val); in smb347_update_ps_status()
313 if (smb->use_mains) in smb347_update_ps_status()
315 if (smb->use_usb) in smb347_update_ps_status()
318 ret = smb->mains_online != dc || smb->usb_online != usb; in smb347_update_ps_status()
319 smb->mains_online = dc; in smb347_update_ps_status()
320 smb->usb_online = usb; in smb347_update_ps_status()
326 * smb347_is_ps_online - returns whether input power source is connected
336 return smb->usb_online || smb->mains_online; in smb347_is_ps_online()
340 * smb347_charging_status - returns status of charging
344 * %1 means pre-charging, %2 fast-charging and %3 taper-charging.
354 ret = regmap_read(smb->regmap, STAT_C, &val); in smb347_charging_status()
365 if (smb->enable_control != SMB3XX_CHG_ENABLE_SW) { in smb347_charging_set()
366 dev_dbg(smb->dev, "charging enable/disable in SW disabled\n"); in smb347_charging_set()
370 if (smb->charging_enabled != enable) { in smb347_charging_set()
371 ret = regmap_update_bits(smb->regmap, CMD_A, CMD_A_CHG_ENABLED, in smb347_charging_set()
374 smb->charging_enabled = enable; in smb347_charging_set()
402 dev_err(smb->dev, "failed to enable charging\n"); in smb347_start_stop_charging()
406 dev_err(smb->dev, "failed to disable charging\n"); in smb347_start_stop_charging()
414 unsigned int id = smb->id; in smb347_set_charge_current()
417 if (smb->max_charge_current) { in smb347_set_charge_current()
419 smb->max_charge_current); in smb347_set_charge_current()
423 ret = regmap_update_bits(smb->regmap, CFG_CHARGE_CURRENT, in smb347_set_charge_current()
430 if (smb->pre_charge_current) { in smb347_set_charge_current()
432 smb->pre_charge_current); in smb347_set_charge_current()
436 ret = regmap_update_bits(smb->regmap, CFG_CHARGE_CURRENT, in smb347_set_charge_current()
443 if (smb->termination_current) { in smb347_set_charge_current()
445 smb->termination_current); in smb347_set_charge_current()
449 ret = regmap_update_bits(smb->regmap, CFG_CHARGE_CURRENT, in smb347_set_charge_current()
460 unsigned int id = smb->id; in smb347_set_current_limits()
463 if (smb->mains_current_limit) { in smb347_set_current_limits()
465 smb->mains_current_limit); in smb347_set_current_limits()
469 ret = regmap_update_bits(smb->regmap, CFG_CURRENT_LIMIT, in smb347_set_current_limits()
476 if (smb->usb_hc_current_limit) { in smb347_set_current_limits()
478 smb->usb_hc_current_limit); in smb347_set_current_limits()
482 ret = regmap_update_bits(smb->regmap, CFG_CURRENT_LIMIT, in smb347_set_current_limits()
495 if (smb->pre_to_fast_voltage) { in smb347_set_voltage_limits()
496 ret = smb->pre_to_fast_voltage; in smb347_set_voltage_limits()
499 ret = clamp_val(ret, 2400000, 3000000) - 2400000; in smb347_set_voltage_limits()
502 ret = regmap_update_bits(smb->regmap, CFG_FLOAT_VOLTAGE, in smb347_set_voltage_limits()
509 if (smb->max_charge_voltage) { in smb347_set_voltage_limits()
510 ret = smb->max_charge_voltage; in smb347_set_voltage_limits()
513 ret = clamp_val(ret, 3500000, 4500000) - 3500000; in smb347_set_voltage_limits()
516 ret = regmap_update_bits(smb->regmap, CFG_FLOAT_VOLTAGE, in smb347_set_voltage_limits()
527 unsigned int id = smb->id; in smb347_set_temp_limits()
532 if (smb->chip_temp_threshold) { in smb347_set_temp_limits()
533 val = smb->chip_temp_threshold; in smb347_set_temp_limits()
536 val = clamp_val(val, 100, 130) - 100; in smb347_set_temp_limits()
539 ret = regmap_update_bits(smb->regmap, CFG_OTG, in smb347_set_temp_limits()
546 if (smb->soft_cold_temp_limit != SMB3XX_TEMP_USE_DEFAULT) { in smb347_set_temp_limits()
547 val = smb->soft_cold_temp_limit; in smb347_set_temp_limits()
554 ret = regmap_update_bits(smb->regmap, CFG_TEMP_LIMIT, in smb347_set_temp_limits()
563 if (smb->soft_hot_temp_limit != SMB3XX_TEMP_USE_DEFAULT) { in smb347_set_temp_limits()
564 val = smb->soft_hot_temp_limit; in smb347_set_temp_limits()
566 val = clamp_val(val, 40, 55) - 40; in smb347_set_temp_limits()
569 ret = regmap_update_bits(smb->regmap, CFG_TEMP_LIMIT, in smb347_set_temp_limits()
578 if (smb->hard_cold_temp_limit != SMB3XX_TEMP_USE_DEFAULT) { in smb347_set_temp_limits()
579 val = smb->hard_cold_temp_limit; in smb347_set_temp_limits()
581 val = clamp_val(val, -5, 10) + 5; in smb347_set_temp_limits()
586 ret = regmap_update_bits(smb->regmap, CFG_TEMP_LIMIT, in smb347_set_temp_limits()
595 if (smb->hard_hot_temp_limit != SMB3XX_TEMP_USE_DEFAULT) { in smb347_set_temp_limits()
596 val = smb->hard_hot_temp_limit; in smb347_set_temp_limits()
598 val = clamp_val(val, 50, 65) - 50; in smb347_set_temp_limits()
601 ret = regmap_update_bits(smb->regmap, CFG_TEMP_LIMIT, in smb347_set_temp_limits()
615 * current and/or voltage depending on the configuration. in smb347_set_temp_limits()
617 * When hard limit is hit, the device will suspend charging in smb347_set_temp_limits()
621 ret = regmap_update_bits(smb->regmap, CFG_THERM, in smb347_set_temp_limits()
627 if (smb->suspend_on_hard_temp_limit) { in smb347_set_temp_limits()
628 ret = regmap_update_bits(smb->regmap, CFG_SYSOK, in smb347_set_temp_limits()
634 if (smb->soft_temp_limit_compensation != in smb347_set_temp_limits()
636 val = smb->soft_temp_limit_compensation & 0x3; in smb347_set_temp_limits()
638 ret = regmap_update_bits(smb->regmap, CFG_THERM, in smb347_set_temp_limits()
644 ret = regmap_update_bits(smb->regmap, CFG_THERM, in smb347_set_temp_limits()
651 if (smb->charge_current_compensation) { in smb347_set_temp_limits()
653 smb->charge_current_compensation); in smb347_set_temp_limits()
657 ret = regmap_update_bits(smb->regmap, CFG_OTG, in smb347_set_temp_limits()
668 * smb347_set_writable - enables/disables writing to non-volatile registers
671 * You can enable/disable writing to the non-volatile configuration
678 return regmap_update_bits(smb->regmap, CMD_A, CMD_A_ALLOW_WRITE, in smb347_set_writable()
712 if (!smb->use_usb) { in smb347_hw_init()
713 ret = regmap_update_bits(smb->regmap, CMD_A, in smb347_hw_init()
721 * If configured by platform data, we enable hardware Auto-OTG in smb347_hw_init()
724 ret = regmap_update_bits(smb->regmap, CFG_OTHER, CFG_OTHER_RID_MASK, in smb347_hw_init()
725 smb->use_usb_otg ? CFG_OTHER_RID_ENABLED_AUTO_OTG : 0); in smb347_hw_init()
730 switch (smb->enable_control) { in smb347_hw_init()
733 ret = regmap_set_bits(smb->regmap, CFG_PIN, CFG_PIN_EN_CTRL); in smb347_hw_init()
743 switch (smb->enable_control) { in smb347_hw_init()
755 ret = regmap_update_bits(smb->regmap, CFG_PIN, CFG_PIN_EN_CTRL_MASK, in smb347_hw_init()
761 ret = regmap_update_bits(smb->regmap, CFG_PIN, CFG_PIN_EN_APSD_IRQ, 0); in smb347_hw_init()
786 ret = regmap_read(smb->regmap, STAT_C, &stat_c); in smb347_interrupt()
788 dev_warn(smb->dev, "reading STAT_C failed\n"); in smb347_interrupt()
792 ret = regmap_read(smb->regmap, IRQSTAT_C, &irqstat_c); in smb347_interrupt()
794 dev_warn(smb->dev, "reading IRQSTAT_C failed\n"); in smb347_interrupt()
798 ret = regmap_read(smb->regmap, IRQSTAT_D, &irqstat_d); in smb347_interrupt()
800 dev_warn(smb->dev, "reading IRQSTAT_D failed\n"); in smb347_interrupt()
804 ret = regmap_read(smb->regmap, IRQSTAT_E, &irqstat_e); in smb347_interrupt()
806 dev_warn(smb->dev, "reading IRQSTAT_E failed\n"); in smb347_interrupt()
815 dev_err(smb->dev, "charging stopped due to charger error\n"); in smb347_interrupt()
816 if (smb->use_mains) in smb347_interrupt()
817 power_supply_changed(smb->mains); in smb347_interrupt()
818 if (smb->use_usb) in smb347_interrupt()
819 power_supply_changed(smb->usb); in smb347_interrupt()
824 * If we reached the termination current the battery is charged and in smb347_interrupt()
830 if (smb->use_mains) in smb347_interrupt()
831 power_supply_changed(smb->mains); in smb347_interrupt()
832 if (smb->use_usb) in smb347_interrupt()
833 power_supply_changed(smb->usb); in smb347_interrupt()
835 dev_dbg(smb->dev, "going to HW maintenance mode\n"); in smb347_interrupt()
844 dev_dbg(smb->dev, "total Charge Timeout INT received\n"); in smb347_interrupt()
847 dev_warn(smb->dev, "charging stopped due to timeout\n"); in smb347_interrupt()
848 if (smb->use_mains) in smb347_interrupt()
849 power_supply_changed(smb->mains); in smb347_interrupt()
850 if (smb->use_usb) in smb347_interrupt()
851 power_supply_changed(smb->usb); in smb347_interrupt()
862 if (smb->use_mains) in smb347_interrupt()
863 power_supply_changed(smb->mains); in smb347_interrupt()
864 if (smb->use_usb) in smb347_interrupt()
865 power_supply_changed(smb->usb); in smb347_interrupt()
877 if (smb->irq_unsupported) in smb347_irq_set()
886 * - under voltage in smb347_irq_set()
887 * - termination current reached in smb347_irq_set()
888 * - charger timeout in smb347_irq_set()
889 * - charger error in smb347_irq_set()
891 ret = regmap_update_bits(smb->regmap, CFG_FAULT_IRQ, 0xff, in smb347_irq_set()
896 ret = regmap_update_bits(smb->regmap, CFG_STATUS_IRQ, 0xff, in smb347_irq_set()
902 ret = regmap_update_bits(smb->regmap, CFG_PIN, CFG_PIN_EN_CHARGER_ERROR, in smb347_irq_set()
924 ret = devm_request_threaded_irq(smb->dev, client->irq, NULL, in smb347_irq_init()
926 client->name, smb); in smb347_irq_init()
938 ret = regmap_update_bits(smb->regmap, CFG_STAT, in smb347_irq_init()
948 * Returns the constant charge current programmed
953 unsigned int id = smb->id; in get_const_charge_current()
958 return -ENODATA; in get_const_charge_current()
960 ret = regmap_read(smb->regmap, STAT_B, &v); in get_const_charge_current()
965 * The current value is composition of FCC and PCC values in get_const_charge_current()
990 return -ENODATA; in get_const_charge_voltage()
992 ret = regmap_read(smb->regmap, STAT_A, &v); in get_const_charge_voltage()
1011 if (psy->desc->type == POWER_SUPPLY_TYPE_USB) { in smb347_get_charging_status()
1012 if (!smb->usb_online) in smb347_get_charging_status()
1015 if (!smb->mains_online) in smb347_get_charging_status()
1019 ret = regmap_read(smb->regmap, STAT_C, &val); in smb347_get_charging_status()
1033 * set to charging if battery is in pre-charge, in smb347_get_charging_status()
1068 val->intval = ret; in smb347_get_property_locked()
1072 if (psy->desc->type == POWER_SUPPLY_TYPE_USB) { in smb347_get_property_locked()
1073 if (!smb->usb_online) in smb347_get_property_locked()
1074 return -ENODATA; in smb347_get_property_locked()
1076 if (!smb->mains_online) in smb347_get_property_locked()
1077 return -ENODATA; in smb347_get_property_locked()
1081 * We handle trickle and pre-charging the same, and taper in smb347_get_property_locked()
1086 val->intval = POWER_SUPPLY_CHARGE_TYPE_TRICKLE; in smb347_get_property_locked()
1089 val->intval = POWER_SUPPLY_CHARGE_TYPE_FAST; in smb347_get_property_locked()
1092 val->intval = POWER_SUPPLY_CHARGE_TYPE_NONE; in smb347_get_property_locked()
1098 if (psy->desc->type == POWER_SUPPLY_TYPE_USB) in smb347_get_property_locked()
1099 val->intval = smb->usb_online; in smb347_get_property_locked()
1101 val->intval = smb->mains_online; in smb347_get_property_locked()
1108 val->intval = ret; in smb347_get_property_locked()
1115 val->intval = ret; in smb347_get_property_locked()
1119 return -EINVAL; in smb347_get_property_locked()
1130 struct i2c_client *client = to_i2c_client(smb->dev); in smb347_get_property()
1133 disable_irq(client->irq); in smb347_get_property()
1135 enable_irq(client->irq); in smb347_get_property()
1193 struct device *dev = smb->dev; in smb347_dt_parse_dev_info()
1195 smb->soft_temp_limit_compensation = in smb347_dt_parse_dev_info()
1199 * pre-initialize the values. See smb347_get_battery_info() below. in smb347_dt_parse_dev_info()
1201 smb->soft_cold_temp_limit = SMB3XX_TEMP_USE_DEFAULT; in smb347_dt_parse_dev_info()
1202 smb->hard_cold_temp_limit = SMB3XX_TEMP_USE_DEFAULT; in smb347_dt_parse_dev_info()
1203 smb->soft_hot_temp_limit = SMB3XX_TEMP_USE_DEFAULT; in smb347_dt_parse_dev_info()
1204 smb->hard_hot_temp_limit = SMB3XX_TEMP_USE_DEFAULT; in smb347_dt_parse_dev_info()
1207 device_property_read_u32(dev, "summit,fast-voltage-threshold-microvolt", in smb347_dt_parse_dev_info()
1208 &smb->pre_to_fast_voltage); in smb347_dt_parse_dev_info()
1209 device_property_read_u32(dev, "summit,mains-current-limit-microamp", in smb347_dt_parse_dev_info()
1210 &smb->mains_current_limit); in smb347_dt_parse_dev_info()
1211 device_property_read_u32(dev, "summit,usb-current-limit-microamp", in smb347_dt_parse_dev_info()
1212 &smb->usb_hc_current_limit); in smb347_dt_parse_dev_info()
1215 device_property_read_u32(dev, "summit,chip-temperature-threshold-celsius", in smb347_dt_parse_dev_info()
1216 &smb->chip_temp_threshold); in smb347_dt_parse_dev_info()
1217 device_property_read_u32(dev, "summit,soft-compensation-method", in smb347_dt_parse_dev_info()
1218 &smb->soft_temp_limit_compensation); in smb347_dt_parse_dev_info()
1219 device_property_read_u32(dev, "summit,charge-current-compensation-microamp", in smb347_dt_parse_dev_info()
1220 &smb->charge_current_compensation); in smb347_dt_parse_dev_info()
1223 smb->use_mains = device_property_read_bool(dev, "summit,enable-mains-charging"); in smb347_dt_parse_dev_info()
1224 smb->use_usb = device_property_read_bool(dev, "summit,enable-usb-charging"); in smb347_dt_parse_dev_info()
1225 smb->use_usb_otg = device_property_read_bool(dev, "summit,enable-otg-charging"); in smb347_dt_parse_dev_info()
1228 device_property_read_u32(dev, "summit,enable-charge-control", in smb347_dt_parse_dev_info()
1229 &smb->enable_control); in smb347_dt_parse_dev_info()
1238 if (smb->mains) in smb347_get_battery_info()
1239 supply = smb->mains; in smb347_get_battery_info()
1241 supply = smb->usb; in smb347_get_battery_info()
1244 if (err == -ENXIO || err == -ENODEV) in smb347_get_battery_info()
1249 if (info.constant_charge_current_max_ua != -EINVAL) in smb347_get_battery_info()
1250 smb->max_charge_current = info.constant_charge_current_max_ua; in smb347_get_battery_info()
1252 if (info.constant_charge_voltage_max_uv != -EINVAL) in smb347_get_battery_info()
1253 smb->max_charge_voltage = info.constant_charge_voltage_max_uv; in smb347_get_battery_info()
1255 if (info.precharge_current_ua != -EINVAL) in smb347_get_battery_info()
1256 smb->pre_charge_current = info.precharge_current_ua; in smb347_get_battery_info()
1258 if (info.charge_term_current_ua != -EINVAL) in smb347_get_battery_info()
1259 smb->termination_current = info.charge_term_current_ua; in smb347_get_battery_info()
1262 smb->soft_cold_temp_limit = info.temp_alert_min; in smb347_get_battery_info()
1265 smb->soft_hot_temp_limit = info.temp_alert_max; in smb347_get_battery_info()
1268 smb->hard_cold_temp_limit = info.temp_min; in smb347_get_battery_info()
1271 smb->hard_hot_temp_limit = info.temp_max; in smb347_get_battery_info()
1274 if (smb->hard_cold_temp_limit != SMB3XX_TEMP_USE_DEFAULT || in smb347_get_battery_info()
1275 smb->hard_hot_temp_limit != SMB3XX_TEMP_USE_DEFAULT) in smb347_get_battery_info()
1276 smb->suspend_on_hard_temp_limit = true; in smb347_get_battery_info()
1290 .name = "smb347-mains",
1298 .name = "smb347-usb",
1309 struct device *dev = &client->dev; in smb347_probe()
1315 return -ENOMEM; in smb347_probe()
1316 smb->dev = &client->dev; in smb347_probe()
1317 smb->id = id->driver_data; in smb347_probe()
1321 if (!smb->use_mains && !smb->use_usb) in smb347_probe()
1322 return -EINVAL; in smb347_probe()
1324 smb->regmap = devm_regmap_init_i2c(client, &smb347_regmap); in smb347_probe()
1325 if (IS_ERR(smb->regmap)) in smb347_probe()
1326 return PTR_ERR(smb->regmap); in smb347_probe()
1329 mains_usb_cfg.of_node = dev->of_node; in smb347_probe()
1330 if (smb->use_mains) { in smb347_probe()
1331 smb->mains = devm_power_supply_register(dev, &smb347_mains_desc, in smb347_probe()
1333 if (IS_ERR(smb->mains)) in smb347_probe()
1334 return PTR_ERR(smb->mains); in smb347_probe()
1337 if (smb->use_usb) { in smb347_probe()
1338 smb->usb = devm_power_supply_register(dev, &smb347_usb_desc, in smb347_probe()
1340 if (IS_ERR(smb->usb)) in smb347_probe()
1341 return PTR_ERR(smb->usb); in smb347_probe()
1356 if (client->irq) { in smb347_probe()
1361 smb->irq_unsupported = true; in smb347_probe()