Lines Matching +full:chg +full:- +full:int
1 // SPDX-License-Identifier: GPL-2.0+
3 // max14577_charger.c - Battery charger driver for the Maxim 14577/77836
11 #include <linux/mfd/max14577-private.h>
47 static int max14577_get_charger_state(struct max14577_charger *chg, int *val) in max14577_get_charger_state() argument
49 struct regmap *rmap = chg->max14577->regmap; in max14577_get_charger_state()
50 int ret; in max14577_get_charger_state()
55 * - CHGCTRL2/MBCHOSTEN == 1 in max14577_get_charger_state()
56 * - STATUS2/CGMBC == 1 in max14577_get_charger_state()
59 * - handle FULL after Top-off timer (EOC register may be off in max14577_get_charger_state()
61 * - handle properly dead-battery charging (respect timer) in max14577_get_charger_state()
62 * - handle timers (fast-charge and prequal) /MBCCHGERR/ in max14577_get_charger_state()
78 /* Charger or USB-cable is connected */ in max14577_get_charger_state()
94 * - POWER_SUPPLY_CHARGE_TYPE_NONE
95 * - POWER_SUPPLY_CHARGE_TYPE_FAST
97 static int max14577_get_charge_type(struct max14577_charger *chg, int *val) in max14577_get_charge_type() argument
99 int ret, charging; in max14577_get_charge_type()
105 * "When the battery is fully charged, the 30-minute (typ) in max14577_get_charge_type()
106 * top-off timer starts. The device continues to trickle in max14577_get_charge_type()
107 * charge the battery until the top-off timer runs out." in max14577_get_charge_type()
109 ret = max14577_get_charger_state(chg, &charging); in max14577_get_charge_type()
121 static int max14577_get_online(struct max14577_charger *chg, int *val) in max14577_get_online() argument
123 struct regmap *rmap = chg->max14577->regmap; in max14577_get_online()
125 int ret; in max14577_get_online()
133 chg_type = maxim_get_charger_type(chg->max14577->dev_type, reg_data); in max14577_get_online()
156 * - POWER_SUPPLY_HEALTH_DEAD
157 * - POWER_SUPPLY_HEALTH_OVERVOLTAGE
158 * - POWER_SUPPLY_HEALTH_GOOD
160 static int max14577_get_battery_health(struct max14577_charger *chg, int *val) in max14577_get_battery_health() argument
162 struct regmap *rmap = chg->max14577->regmap; in max14577_get_battery_health()
163 int ret; in max14577_get_battery_health()
172 chg_type = maxim_get_charger_type(chg->max14577->dev_type, reg_data); in max14577_get_battery_health()
199 static int max14577_get_present(struct max14577_charger *chg, int *val) in max14577_get_present() argument
206 static int max14577_set_fast_charge_timer(struct max14577_charger *chg, in max14577_set_fast_charge_timer() argument
213 reg_data = hours - 3; in max14577_set_fast_charge_timer()
220 dev_err(chg->dev, "Wrong value for Fast-Charge Timer: %lu\n", in max14577_set_fast_charge_timer()
222 return -EINVAL; in max14577_set_fast_charge_timer()
226 return max14577_update_reg(chg->max14577->regmap, in max14577_set_fast_charge_timer()
230 static int max14577_init_constant_voltage(struct max14577_charger *chg, in max14577_init_constant_voltage() argument
231 unsigned int uvolt) in max14577_init_constant_voltage()
237 return -EINVAL; in max14577_init_constant_voltage()
244 unsigned int val = uvolt; in max14577_init_constant_voltage()
246 val -= MAXIM_CHARGER_CONSTANT_VOLTAGE_MIN; in max14577_init_constant_voltage()
253 return -EINVAL; in max14577_init_constant_voltage()
257 return max14577_write_reg(chg->max14577->regmap, in max14577_init_constant_voltage()
261 static int max14577_init_eoc(struct max14577_charger *chg, in max14577_init_eoc() argument
262 unsigned int uamp) in max14577_init_eoc()
264 unsigned int current_bits; in max14577_init_eoc()
267 switch (chg->max14577->dev_type) { in max14577_init_eoc()
270 return -EINVAL; /* Requested current is too low */ in max14577_init_eoc()
278 uamp = min(uamp, 100000U) - 50000U; in max14577_init_eoc()
286 return -EINVAL; /* Requested current is too low */ in max14577_init_eoc()
289 uamp -= MAX14577_CHARGER_EOC_CURRENT_LIMIT_MIN; in max14577_init_eoc()
296 return max14577_update_reg(chg->max14577->regmap, in max14577_init_eoc()
301 static int max14577_init_fast_charge(struct max14577_charger *chg, in max14577_init_fast_charge() argument
302 unsigned int uamp) in max14577_init_fast_charge()
305 int ret; in max14577_init_fast_charge()
307 &maxim_charger_currents[chg->max14577->dev_type]; in max14577_init_fast_charge()
311 dev_err(chg->dev, "Wrong value for fast charge: %u\n", uamp); in max14577_init_fast_charge()
315 return max14577_update_reg(chg->max14577->regmap, in max14577_init_fast_charge()
326 static int max14577_charger_reg_init(struct max14577_charger *chg) in max14577_charger_reg_init() argument
328 struct regmap *rmap = chg->max14577->regmap; in max14577_charger_reg_init()
330 int ret; in max14577_charger_reg_init()
333 * Charger-Type Manual Detection, default off (set CHGTYPMAN to 0) in max14577_charger_reg_init()
334 * Charger-Detection Enable, default on (set CHGDETEN to 1) in max14577_charger_reg_init()
343 * Wall-Adapter Rapid Charge, default on in max14577_charger_reg_init()
344 * Battery-Charger, default on in max14577_charger_reg_init()
354 ret = max14577_init_constant_voltage(chg, chg->pdata->constant_uvolt); in max14577_charger_reg_init()
358 ret = max14577_init_eoc(chg, chg->pdata->eoc_uamp); in max14577_charger_reg_init()
362 ret = max14577_init_fast_charge(chg, chg->pdata->fast_charge_uamp); in max14577_charger_reg_init()
366 ret = max14577_set_fast_charge_timer(chg, in max14577_charger_reg_init()
371 /* Initialize Overvoltage-Protection Threshold */ in max14577_charger_reg_init()
372 switch (chg->pdata->ovp_uvolt) { in max14577_charger_reg_init()
379 reg_data = 0x1 + (chg->pdata->ovp_uvolt - 6000000) / 500000; in max14577_charger_reg_init()
382 dev_err(chg->dev, "Wrong value for OVP: %u\n", in max14577_charger_reg_init()
383 chg->pdata->ovp_uvolt); in max14577_charger_reg_init()
384 return -EINVAL; in max14577_charger_reg_init()
404 [MAXIM_DEVICE_TYPE_UNKNOWN] = "MAX14577-like",
410 static int max14577_charger_get_property(struct power_supply *psy, in max14577_charger_get_property()
414 struct max14577_charger *chg = power_supply_get_drvdata(psy); in max14577_charger_get_property() local
415 int ret = 0; in max14577_charger_get_property()
419 ret = max14577_get_charger_state(chg, &val->intval); in max14577_charger_get_property()
422 ret = max14577_get_charge_type(chg, &val->intval); in max14577_charger_get_property()
425 ret = max14577_get_battery_health(chg, &val->intval); in max14577_charger_get_property()
428 ret = max14577_get_present(chg, &val->intval); in max14577_charger_get_property()
431 ret = max14577_get_online(chg, &val->intval); in max14577_charger_get_property()
435 val->strval = model_names[chg->max14577->dev_type]; in max14577_charger_get_property()
438 val->strval = manufacturer; in max14577_charger_get_property()
441 return -EINVAL; in max14577_charger_get_property()
448 .name = "max14577-charger",
460 struct device_node *np = pdev->dev.of_node; in max14577_charger_dt_init()
461 int ret; in max14577_charger_dt_init()
464 dev_err(&pdev->dev, "No charger OF node\n"); in max14577_charger_dt_init()
465 return ERR_PTR(-EINVAL); in max14577_charger_dt_init()
468 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL); in max14577_charger_dt_init()
470 return ERR_PTR(-ENOMEM); in max14577_charger_dt_init()
472 ret = of_property_read_u32(np, "maxim,constant-uvolt", in max14577_charger_dt_init()
473 &pdata->constant_uvolt); in max14577_charger_dt_init()
475 dev_err(&pdev->dev, "Cannot parse maxim,constant-uvolt field from DT\n"); in max14577_charger_dt_init()
479 ret = of_property_read_u32(np, "maxim,fast-charge-uamp", in max14577_charger_dt_init()
480 &pdata->fast_charge_uamp); in max14577_charger_dt_init()
482 dev_err(&pdev->dev, "Cannot parse maxim,fast-charge-uamp field from DT\n"); in max14577_charger_dt_init()
486 ret = of_property_read_u32(np, "maxim,eoc-uamp", &pdata->eoc_uamp); in max14577_charger_dt_init()
488 dev_err(&pdev->dev, "Cannot parse maxim,eoc-uamp field from DT\n"); in max14577_charger_dt_init()
492 ret = of_property_read_u32(np, "maxim,ovp-uvolt", &pdata->ovp_uvolt); in max14577_charger_dt_init()
494 dev_err(&pdev->dev, "Cannot parse maxim,ovp-uvolt field from DT\n"); in max14577_charger_dt_init()
511 struct max14577_charger *chg = dev_get_drvdata(dev); in show_fast_charge_timer() local
513 int ret; in show_fast_charge_timer()
514 unsigned int val; in show_fast_charge_timer()
516 ret = max14577_read_reg(chg->max14577->regmap, MAX14577_REG_CHGCTRL1, in show_fast_charge_timer()
541 struct max14577_charger *chg = dev_get_drvdata(dev); in store_fast_charge_timer() local
543 int ret; in store_fast_charge_timer()
549 ret = max14577_set_fast_charge_timer(chg, val); in store_fast_charge_timer()
559 static int max14577_charger_probe(struct platform_device *pdev) in max14577_charger_probe()
561 struct max14577_charger *chg; in max14577_charger_probe() local
563 struct max14577 *max14577 = dev_get_drvdata(pdev->dev.parent); in max14577_charger_probe()
564 int ret; in max14577_charger_probe()
566 chg = devm_kzalloc(&pdev->dev, sizeof(*chg), GFP_KERNEL); in max14577_charger_probe()
567 if (!chg) in max14577_charger_probe()
568 return -ENOMEM; in max14577_charger_probe()
570 platform_set_drvdata(pdev, chg); in max14577_charger_probe()
571 chg->dev = &pdev->dev; in max14577_charger_probe()
572 chg->max14577 = max14577; in max14577_charger_probe()
574 chg->pdata = max14577_charger_dt_init(pdev); in max14577_charger_probe()
575 if (IS_ERR_OR_NULL(chg->pdata)) in max14577_charger_probe()
576 return PTR_ERR(chg->pdata); in max14577_charger_probe()
578 ret = max14577_charger_reg_init(chg); in max14577_charger_probe()
582 ret = device_create_file(&pdev->dev, &dev_attr_fast_charge_timer); in max14577_charger_probe()
584 dev_err(&pdev->dev, "failed: create sysfs entry\n"); in max14577_charger_probe()
588 psy_cfg.drv_data = chg; in max14577_charger_probe()
589 chg->charger = devm_power_supply_register(&pdev->dev, in max14577_charger_probe()
592 if (IS_ERR(chg->charger)) { in max14577_charger_probe()
593 dev_err(&pdev->dev, "failed: power supply register\n"); in max14577_charger_probe()
594 ret = PTR_ERR(chg->charger); in max14577_charger_probe()
605 device_remove_file(&pdev->dev, &dev_attr_fast_charge_timer); in max14577_charger_probe()
612 device_remove_file(&pdev->dev, &dev_attr_fast_charge_timer); in max14577_charger_remove()
616 { "max14577-charger", MAXIM_DEVICE_TYPE_MAX14577, },
617 { "max77836-charger", MAXIM_DEVICE_TYPE_MAX77836, },
623 { .compatible = "maxim,max14577-charger",
625 { .compatible = "maxim,max77836-charger",
633 .name = "max14577-charger",