Lines Matching +full:usb +full:- +full:charger

1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * DA9150 Charger Driver
17 #include <linux/usb/phy.h>
27 struct power_supply *usb; member
42 static inline int da9150_charger_supply_online(struct da9150_charger *charger, in da9150_charger_supply_online() argument
46 val->intval = (psy == charger->supply_online) ? 1 : 0; in da9150_charger_supply_online()
51 /* Charger Properties */
52 static int da9150_charger_vbus_voltage_now(struct da9150_charger *charger, in da9150_charger_vbus_voltage_now() argument
57 /* Read processed value - mV units */ in da9150_charger_vbus_voltage_now()
58 ret = iio_read_channel_processed(charger->vbus_chan, &v_val); in da9150_charger_vbus_voltage_now()
63 val->intval = v_val * 1000; in da9150_charger_vbus_voltage_now()
68 static int da9150_charger_ibus_current_avg(struct da9150_charger *charger, in da9150_charger_ibus_current_avg() argument
73 /* Read processed value - mA units */ in da9150_charger_ibus_current_avg()
74 ret = iio_read_channel_processed(charger->ibus_chan, &i_val); in da9150_charger_ibus_current_avg()
79 val->intval = i_val * 1000; in da9150_charger_ibus_current_avg()
84 static int da9150_charger_tjunc_temp(struct da9150_charger *charger, in da9150_charger_tjunc_temp() argument
89 /* Read processed value - 0.001 degrees C units */ in da9150_charger_tjunc_temp()
90 ret = iio_read_channel_processed(charger->tjunc_chan, &t_val); in da9150_charger_tjunc_temp()
95 val->intval = t_val / 100; in da9150_charger_tjunc_temp()
111 struct da9150_charger *charger = dev_get_drvdata(psy->dev.parent); in da9150_charger_get_prop() local
116 ret = da9150_charger_supply_online(charger, psy, val); in da9150_charger_get_prop()
119 ret = da9150_charger_vbus_voltage_now(charger, val); in da9150_charger_get_prop()
122 ret = da9150_charger_ibus_current_avg(charger, val); in da9150_charger_get_prop()
125 ret = da9150_charger_tjunc_temp(charger, val); in da9150_charger_get_prop()
128 ret = -EINVAL; in da9150_charger_get_prop()
136 static int da9150_charger_battery_status(struct da9150_charger *charger, in da9150_charger_battery_status() argument
142 reg = da9150_reg_read(charger->da9150, DA9150_STATUS_H); in da9150_charger_battery_status()
146 val->intval = POWER_SUPPLY_STATUS_DISCHARGING; in da9150_charger_battery_status()
151 reg = da9150_reg_read(charger->da9150, DA9150_STATUS_J); in da9150_charger_battery_status()
159 val->intval = POWER_SUPPLY_STATUS_CHARGING; in da9150_charger_battery_status()
166 val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING; in da9150_charger_battery_status()
169 val->intval = POWER_SUPPLY_STATUS_FULL; in da9150_charger_battery_status()
172 val->intval = POWER_SUPPLY_STATUS_UNKNOWN; in da9150_charger_battery_status()
179 static int da9150_charger_battery_health(struct da9150_charger *charger, in da9150_charger_battery_health() argument
184 reg = da9150_reg_read(charger->da9150, DA9150_STATUS_J); in da9150_charger_battery_health()
189 val->intval = POWER_SUPPLY_HEALTH_COLD; in da9150_charger_battery_health()
192 val->intval = POWER_SUPPLY_HEALTH_OVERHEAT; in da9150_charger_battery_health()
202 val->intval = POWER_SUPPLY_HEALTH_DEAD; in da9150_charger_battery_health()
205 val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE; in da9150_charger_battery_health()
208 val->intval = POWER_SUPPLY_HEALTH_GOOD; in da9150_charger_battery_health()
215 static int da9150_charger_battery_present(struct da9150_charger *charger, in da9150_charger_battery_present() argument
221 reg = da9150_reg_read(charger->da9150, DA9150_STATUS_J); in da9150_charger_battery_present()
223 val->intval = 0; in da9150_charger_battery_present()
225 val->intval = 1; in da9150_charger_battery_present()
230 static int da9150_charger_battery_charge_type(struct da9150_charger *charger, in da9150_charger_battery_charge_type() argument
235 reg = da9150_reg_read(charger->da9150, DA9150_STATUS_J); in da9150_charger_battery_charge_type()
239 val->intval = POWER_SUPPLY_CHARGE_TYPE_FAST; in da9150_charger_battery_charge_type()
244 val->intval = POWER_SUPPLY_CHARGE_TYPE_TRICKLE; in da9150_charger_battery_charge_type()
247 val->intval = POWER_SUPPLY_CHARGE_TYPE_NONE; in da9150_charger_battery_charge_type()
254 static int da9150_charger_battery_voltage_min(struct da9150_charger *charger, in da9150_charger_battery_voltage_min() argument
259 reg = da9150_reg_read(charger->da9150, DA9150_PPR_CHGCTRL_C); in da9150_charger_battery_voltage_min()
262 val->intval = ((reg & DA9150_CHG_VFAULT_MASK) * 50000) + 2500000; in da9150_charger_battery_voltage_min()
267 static int da9150_charger_battery_voltage_now(struct da9150_charger *charger, in da9150_charger_battery_voltage_now() argument
272 /* Read processed value - mV units */ in da9150_charger_battery_voltage_now()
273 ret = iio_read_channel_processed(charger->vbat_chan, &v_val); in da9150_charger_battery_voltage_now()
277 val->intval = v_val * 1000; in da9150_charger_battery_voltage_now()
282 static int da9150_charger_battery_current_max(struct da9150_charger *charger, in da9150_charger_battery_current_max() argument
287 reg = da9150_reg_read(charger->da9150, DA9150_PPR_CHGCTRL_D); in da9150_charger_battery_current_max()
290 val->intval = reg * 25000; in da9150_charger_battery_current_max()
295 static int da9150_charger_battery_voltage_max(struct da9150_charger *charger, in da9150_charger_battery_voltage_max() argument
300 reg = da9150_reg_read(charger->da9150, DA9150_PPR_CHGCTRL_B); in da9150_charger_battery_voltage_max()
303 val->intval = ((reg & DA9150_CHG_VBAT_MASK) * 25000) + 3650000; in da9150_charger_battery_voltage_max()
323 struct da9150_charger *charger = dev_get_drvdata(psy->dev.parent); in da9150_charger_battery_get_prop() local
328 ret = da9150_charger_battery_status(charger, val); in da9150_charger_battery_get_prop()
331 ret = da9150_charger_supply_online(charger, psy, val); in da9150_charger_battery_get_prop()
334 ret = da9150_charger_battery_health(charger, val); in da9150_charger_battery_get_prop()
337 ret = da9150_charger_battery_present(charger, val); in da9150_charger_battery_get_prop()
340 ret = da9150_charger_battery_charge_type(charger, val); in da9150_charger_battery_get_prop()
343 ret = da9150_charger_battery_voltage_min(charger, val); in da9150_charger_battery_get_prop()
346 ret = da9150_charger_battery_voltage_now(charger, val); in da9150_charger_battery_get_prop()
349 ret = da9150_charger_battery_current_max(charger, val); in da9150_charger_battery_get_prop()
352 ret = da9150_charger_battery_voltage_max(charger, val); in da9150_charger_battery_get_prop()
355 ret = -EINVAL; in da9150_charger_battery_get_prop()
364 struct da9150_charger *charger = data; in da9150_charger_chg_irq() local
366 power_supply_changed(charger->battery); in da9150_charger_chg_irq()
373 struct da9150_charger *charger = data; in da9150_charger_tjunc_irq() local
376 dev_crit(charger->dev, "TJunc over temperature!!!\n"); in da9150_charger_tjunc_irq()
377 power_supply_changed(charger->usb); in da9150_charger_tjunc_irq()
384 struct da9150_charger *charger = data; in da9150_charger_vfault_irq() local
387 dev_crit(charger->dev, "VSYS under voltage!!!\n"); in da9150_charger_vfault_irq()
388 power_supply_changed(charger->usb); in da9150_charger_vfault_irq()
389 power_supply_changed(charger->battery); in da9150_charger_vfault_irq()
396 struct da9150_charger *charger = data; in da9150_charger_vbus_irq() local
399 reg = da9150_reg_read(charger->da9150, DA9150_STATUS_H); in da9150_charger_vbus_irq()
401 /* Charger plugged in or battery only */ in da9150_charger_vbus_irq()
405 charger->supply_online = charger->battery; in da9150_charger_vbus_irq()
408 charger->supply_online = charger->usb; in da9150_charger_vbus_irq()
411 dev_warn(charger->dev, "Unknown VBUS state - reg = 0x%x\n", in da9150_charger_vbus_irq()
413 charger->supply_online = NULL; in da9150_charger_vbus_irq()
417 power_supply_changed(charger->usb); in da9150_charger_vbus_irq()
418 power_supply_changed(charger->battery); in da9150_charger_vbus_irq()
425 struct da9150_charger *charger = in da9150_charger_otg_work() local
428 switch (charger->usb_event) { in da9150_charger_otg_work()
431 da9150_set_bits(charger->da9150, DA9150_PPR_BKCTRL_A, in da9150_charger_otg_work()
436 power_supply_changed(charger->usb); in da9150_charger_otg_work()
437 power_supply_changed(charger->battery); in da9150_charger_otg_work()
438 da9150_set_bits(charger->da9150, DA9150_PPR_BKCTRL_A, in da9150_charger_otg_work()
447 struct da9150_charger *charger = in da9150_charger_otg_ncb() local
450 dev_dbg(charger->dev, "DA9150 OTG notify %lu\n", val); in da9150_charger_otg_ncb()
452 charger->usb_event = val; in da9150_charger_otg_ncb()
453 schedule_work(&charger->otg_work); in da9150_charger_otg_ncb()
462 struct device *dev = &pdev->dev; in da9150_charger_register_irq()
463 struct da9150_charger *charger = platform_get_drvdata(pdev); in da9150_charger_register_irq() local
471 charger); in da9150_charger_register_irq()
481 struct da9150_charger *charger = platform_get_drvdata(pdev); in da9150_charger_unregister_irq() local
488 free_irq(irq, charger); in da9150_charger_unregister_irq()
492 .name = "da9150-usb",
500 .name = "da9150-battery",
509 struct device *dev = &pdev->dev; in da9150_charger_probe()
510 struct da9150 *da9150 = dev_get_drvdata(dev->parent); in da9150_charger_probe()
511 struct da9150_charger *charger; in da9150_charger_probe() local
515 charger = devm_kzalloc(dev, sizeof(struct da9150_charger), GFP_KERNEL); in da9150_charger_probe()
516 if (!charger) in da9150_charger_probe()
517 return -ENOMEM; in da9150_charger_probe()
519 platform_set_drvdata(pdev, charger); in da9150_charger_probe()
520 charger->da9150 = da9150; in da9150_charger_probe()
521 charger->dev = dev; in da9150_charger_probe()
524 charger->ibus_chan = devm_iio_channel_get(dev, "CHAN_IBUS"); in da9150_charger_probe()
525 if (IS_ERR(charger->ibus_chan)) in da9150_charger_probe()
526 return PTR_ERR(charger->ibus_chan); in da9150_charger_probe()
528 charger->vbus_chan = devm_iio_channel_get(dev, "CHAN_VBUS"); in da9150_charger_probe()
529 if (IS_ERR(charger->vbus_chan)) in da9150_charger_probe()
530 return PTR_ERR(charger->vbus_chan); in da9150_charger_probe()
532 charger->tjunc_chan = devm_iio_channel_get(dev, "CHAN_TJUNC"); in da9150_charger_probe()
533 if (IS_ERR(charger->tjunc_chan)) in da9150_charger_probe()
534 return PTR_ERR(charger->tjunc_chan); in da9150_charger_probe()
536 charger->vbat_chan = devm_iio_channel_get(dev, "CHAN_VBAT"); in da9150_charger_probe()
537 if (IS_ERR(charger->vbat_chan)) in da9150_charger_probe()
538 return PTR_ERR(charger->vbat_chan); in da9150_charger_probe()
541 charger->usb = devm_power_supply_register(dev, &usb_desc, NULL); in da9150_charger_probe()
542 if (IS_ERR(charger->usb)) in da9150_charger_probe()
543 return PTR_ERR(charger->usb); in da9150_charger_probe()
545 charger->battery = devm_power_supply_register(dev, &battery_desc, NULL); in da9150_charger_probe()
546 if (IS_ERR(charger->battery)) in da9150_charger_probe()
547 return PTR_ERR(charger->battery); in da9150_charger_probe()
555 charger->supply_online = charger->battery; in da9150_charger_probe()
558 charger->supply_online = charger->usb; in da9150_charger_probe()
561 dev_warn(dev, "Unknown VBUS state - reg = 0x%x\n", reg); in da9150_charger_probe()
562 charger->supply_online = NULL; in da9150_charger_probe()
567 charger->usb_phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB2); in da9150_charger_probe()
568 if (!IS_ERR_OR_NULL(charger->usb_phy)) { in da9150_charger_probe()
569 INIT_WORK(&charger->otg_work, da9150_charger_otg_work); in da9150_charger_probe()
570 charger->otg_nb.notifier_call = da9150_charger_otg_ncb; in da9150_charger_probe()
571 usb_register_notifier(charger->usb_phy, &charger->otg_nb); in da9150_charger_probe()
605 if (!IS_ERR_OR_NULL(charger->usb_phy)) in da9150_charger_probe()
606 usb_unregister_notifier(charger->usb_phy, &charger->otg_nb); in da9150_charger_probe()
613 struct da9150_charger *charger = platform_get_drvdata(pdev); in da9150_charger_remove() local
618 free_irq(irq, charger); in da9150_charger_remove()
621 free_irq(irq, charger); in da9150_charger_remove()
624 free_irq(irq, charger); in da9150_charger_remove()
627 free_irq(irq, charger); in da9150_charger_remove()
629 if (!IS_ERR_OR_NULL(charger->usb_phy)) in da9150_charger_remove()
630 usb_unregister_notifier(charger->usb_phy, &charger->otg_nb); in da9150_charger_remove()
631 cancel_work_sync(&charger->otg_work); in da9150_charger_remove()
636 .name = "da9150-charger",
644 MODULE_DESCRIPTION("Charger Driver for DA9150");