1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (c) 2014, Sony Mobile Communications Inc.
4 * This driver is for the multi-block Switch-Mode Battery Charger and Boost
5 * (SMBB) hardware, found in Qualcomm PM8941 PMICs. The charger is an
6 * integrated, single-cell lithium-ion battery charger.
14 * - Boost (not implemented)
19 #include <linux/errno.h>
20 #include <linux/interrupt.h>
21 #include <linux/kernel.h>
22 #include <linux/module.h>
23 #include <linux/mutex.h>
25 #include <linux/platform_device.h>
26 #include <linux/power_supply.h>
27 #include <linux/regmap.h>
28 #include <linux/slab.h>
29 #include <linux/extcon-provider.h>
30 #include <linux/regulator/driver.h>
32 #define SMBB_CHG_VMAX 0x040
33 #define SMBB_CHG_VSAFE 0x041
34 #define SMBB_CHG_CFG 0x043
35 #define SMBB_CHG_IMAX 0x044
36 #define SMBB_CHG_ISAFE 0x045
37 #define SMBB_CHG_VIN_MIN 0x047
38 #define SMBB_CHG_CTRL 0x049
39 #define CTRL_EN BIT(7)
40 #define SMBB_CHG_VBAT_WEAK 0x052
41 #define SMBB_CHG_IBAT_TERM_CHG 0x05b
42 #define IBAT_TERM_CHG_IEOC BIT(7)
43 #define IBAT_TERM_CHG_IEOC_BMS BIT(7)
44 #define IBAT_TERM_CHG_IEOC_CHG 0
45 #define SMBB_CHG_VBAT_DET 0x05d
46 #define SMBB_CHG_TCHG_MAX_EN 0x060
47 #define TCHG_MAX_EN BIT(7)
48 #define SMBB_CHG_WDOG_TIME 0x062
49 #define SMBB_CHG_WDOG_EN 0x065
50 #define WDOG_EN BIT(7)
52 #define SMBB_BUCK_REG_MODE 0x174
53 #define BUCK_REG_MODE BIT(0)
54 #define BUCK_REG_MODE_VBAT BIT(0)
55 #define BUCK_REG_MODE_VSYS 0
57 #define SMBB_BAT_PRES_STATUS 0x208
58 #define PRES_STATUS_BAT_PRES BIT(7)
59 #define SMBB_BAT_TEMP_STATUS 0x209
60 #define TEMP_STATUS_OK BIT(7)
61 #define TEMP_STATUS_HOT BIT(6)
62 #define SMBB_BAT_BTC_CTRL 0x249
63 #define BTC_CTRL_COMP_EN BIT(7)
64 #define BTC_CTRL_COLD_EXT BIT(1)
65 #define BTC_CTRL_HOT_EXT_N BIT(0)
67 #define SMBB_USB_IMAX 0x344
68 #define SMBB_USB_OTG_CTL 0x348
69 #define OTG_CTL_EN BIT(0)
70 #define SMBB_USB_ENUM_TIMER_STOP 0x34e
71 #define ENUM_TIMER_STOP BIT(0)
72 #define SMBB_USB_SEC_ACCESS 0x3d0
73 #define SEC_ACCESS_MAGIC 0xa5
74 #define SMBB_USB_REV_BST 0x3ed
75 #define REV_BST_CHG_GONE BIT(7)
77 #define SMBB_DC_IMAX 0x444
79 #define SMBB_MISC_REV2 0x601
80 #define SMBB_MISC_BOOT_DONE 0x642
81 #define BOOT_DONE BIT(7)
83 #define STATUS_USBIN_VALID BIT(0) /* USB connection is valid */
84 #define STATUS_DCIN_VALID BIT(1) /* DC connection is valid */
85 #define STATUS_BAT_HOT BIT(2) /* Battery temp 1=Hot, 0=Cold */
86 #define STATUS_BAT_OK BIT(3) /* Battery temp OK */
87 #define STATUS_BAT_PRESENT BIT(4) /* Battery is present */
88 #define STATUS_CHG_DONE BIT(5) /* Charge cycle is complete */
89 #define STATUS_CHG_TRKL BIT(6) /* Trickle charging */
90 #define STATUS_CHG_FAST BIT(7) /* Fast charging */
91 #define STATUS_CHG_GONE BIT(8) /* No charger is connected */
106 struct smbb_charger {
107 unsigned int revision;
110 struct extcon_dev *edev;
114 unsigned long status;
115 struct mutex statlock;
117 unsigned int attr[_ATTR_CNT];
119 struct power_supply *usb_psy;
120 struct power_supply *dc_psy;
121 struct power_supply *bat_psy;
122 struct regmap *regmap;
124 struct regulator_desc otg_rdesc;
125 struct regulator_dev *otg_reg;
128 static const unsigned int smbb_usb_extcon_cable[] = {
133 static int smbb_vbat_weak_fn(unsigned int index)
135 return 2100000 + index * 100000;
138 static int smbb_vin_fn(unsigned int index)
141 return 5600000 + (index - 43) * 200000;
142 return 3400000 + index * 50000;
145 static int smbb_vmax_fn(unsigned int index)
147 return 3240000 + index * 10000;
150 static int smbb_vbat_det_fn(unsigned int index)
152 return 3240000 + index * 20000;
155 static int smbb_imax_fn(unsigned int index)
158 return 100000 + index * 50000;
159 return index * 100000;
162 static int smbb_bat_imax_fn(unsigned int index)
164 return index * 50000;
167 static unsigned int smbb_hw_lookup(unsigned int val, int (*fn)(unsigned int))
172 for (widx = sel = 0; (*fn)(widx) <= val; ++widx)
178 static const struct smbb_charger_attr {
181 unsigned int safe_reg;
184 unsigned int fail_ok;
185 int (*hw_fn)(unsigned int);
186 } smbb_charger_attrs[] = {
188 .name = "qcom,fast-charge-safe-current",
189 .reg = SMBB_CHG_ISAFE,
192 .hw_fn = smbb_bat_imax_fn,
196 .name = "qcom,fast-charge-current-limit",
197 .reg = SMBB_CHG_IMAX,
198 .safe_reg = SMBB_CHG_ISAFE,
201 .hw_fn = smbb_bat_imax_fn,
204 .name = "qcom,dc-current-limit",
208 .hw_fn = smbb_imax_fn,
211 .name = "qcom,fast-charge-safe-voltage",
212 .reg = SMBB_CHG_VSAFE,
215 .hw_fn = smbb_vmax_fn,
219 .name = "qcom,fast-charge-high-threshold-voltage",
220 .reg = SMBB_CHG_VMAX,
221 .safe_reg = SMBB_CHG_VSAFE,
224 .hw_fn = smbb_vmax_fn,
227 .name = "qcom,fast-charge-low-threshold-voltage",
228 .reg = SMBB_CHG_VBAT_WEAK,
231 .hw_fn = smbb_vbat_weak_fn,
234 .name = "qcom,auto-recharge-threshold-voltage",
235 .reg = SMBB_CHG_VBAT_DET,
238 .hw_fn = smbb_vbat_det_fn,
241 .name = "qcom,minimum-input-voltage",
242 .reg = SMBB_CHG_VIN_MIN,
245 .hw_fn = smbb_vin_fn,
247 [ATTR_USBIN_IMAX] = {
248 .name = "usb-charge-current-limit",
249 .reg = SMBB_USB_IMAX,
252 .hw_fn = smbb_imax_fn,
256 static int smbb_charger_attr_write(struct smbb_charger *chg,
257 enum smbb_attr which, unsigned int val)
259 const struct smbb_charger_attr *prop;
264 prop = &smbb_charger_attrs[which];
266 if (val > prop->max || val < prop->min) {
267 dev_err(chg->dev, "value out of range for %s [%u:%u]\n",
268 prop->name, prop->min, prop->max);
272 if (prop->safe_reg) {
273 rc = regmap_read(chg->regmap,
274 chg->addr + prop->safe_reg, &wval);
277 "unable to read safe value for '%s'\n",
282 wval = prop->hw_fn(wval);
286 "%s above safe value, clamping at %u\n",
292 wval = smbb_hw_lookup(val, prop->hw_fn);
294 rc = regmap_write(chg->regmap, chg->addr + prop->reg, wval);
296 dev_err(chg->dev, "unable to update %s", prop->name);
299 out = prop->hw_fn(wval);
302 "%s inaccurate, rounded to %u\n",
306 dev_dbg(chg->dev, "%s <= %d\n", prop->name, out);
308 chg->attr[which] = out;
313 static int smbb_charger_attr_read(struct smbb_charger *chg,
314 enum smbb_attr which)
316 const struct smbb_charger_attr *prop;
320 prop = &smbb_charger_attrs[which];
322 rc = regmap_read(chg->regmap, chg->addr + prop->reg, &val);
324 dev_err(chg->dev, "failed to read %s\n", prop->name);
327 val = prop->hw_fn(val);
328 dev_dbg(chg->dev, "%s => %d\n", prop->name, val);
330 chg->attr[which] = val;
335 static int smbb_charger_attr_parse(struct smbb_charger *chg,
336 enum smbb_attr which)
338 const struct smbb_charger_attr *prop;
342 prop = &smbb_charger_attrs[which];
344 rc = of_property_read_u32(chg->dev->of_node, prop->name, &val);
346 rc = smbb_charger_attr_write(chg, which, val);
347 if (!rc || !prop->fail_ok)
350 return smbb_charger_attr_read(chg, which);
353 static void smbb_set_line_flag(struct smbb_charger *chg, int irq, int flag)
358 ret = irq_get_irqchip_state(irq, IRQCHIP_STATE_LINE_LEVEL, &state);
360 dev_err(chg->dev, "failed to read irq line\n");
364 mutex_lock(&chg->statlock);
368 chg->status &= ~flag;
369 mutex_unlock(&chg->statlock);
371 dev_dbg(chg->dev, "status = %03lx\n", chg->status);
374 static irqreturn_t smbb_usb_valid_handler(int irq, void *_data)
376 struct smbb_charger *chg = _data;
378 smbb_set_line_flag(chg, irq, STATUS_USBIN_VALID);
379 extcon_set_state_sync(chg->edev, EXTCON_USB,
380 chg->status & STATUS_USBIN_VALID);
381 power_supply_changed(chg->usb_psy);
386 static irqreturn_t smbb_dc_valid_handler(int irq, void *_data)
388 struct smbb_charger *chg = _data;
390 smbb_set_line_flag(chg, irq, STATUS_DCIN_VALID);
391 if (!chg->dc_disabled)
392 power_supply_changed(chg->dc_psy);
397 static irqreturn_t smbb_bat_temp_handler(int irq, void *_data)
399 struct smbb_charger *chg = _data;
403 rc = regmap_read(chg->regmap, chg->addr + SMBB_BAT_TEMP_STATUS, &val);
407 mutex_lock(&chg->statlock);
408 if (val & TEMP_STATUS_OK) {
409 chg->status |= STATUS_BAT_OK;
411 chg->status &= ~STATUS_BAT_OK;
412 if (val & TEMP_STATUS_HOT)
413 chg->status |= STATUS_BAT_HOT;
415 mutex_unlock(&chg->statlock);
417 power_supply_changed(chg->bat_psy);
421 static irqreturn_t smbb_bat_present_handler(int irq, void *_data)
423 struct smbb_charger *chg = _data;
425 smbb_set_line_flag(chg, irq, STATUS_BAT_PRESENT);
426 power_supply_changed(chg->bat_psy);
431 static irqreturn_t smbb_chg_done_handler(int irq, void *_data)
433 struct smbb_charger *chg = _data;
435 smbb_set_line_flag(chg, irq, STATUS_CHG_DONE);
436 power_supply_changed(chg->bat_psy);
441 static irqreturn_t smbb_chg_gone_handler(int irq, void *_data)
443 struct smbb_charger *chg = _data;
445 smbb_set_line_flag(chg, irq, STATUS_CHG_GONE);
446 power_supply_changed(chg->bat_psy);
447 power_supply_changed(chg->usb_psy);
448 if (!chg->dc_disabled)
449 power_supply_changed(chg->dc_psy);
454 static irqreturn_t smbb_chg_fast_handler(int irq, void *_data)
456 struct smbb_charger *chg = _data;
458 smbb_set_line_flag(chg, irq, STATUS_CHG_FAST);
459 power_supply_changed(chg->bat_psy);
464 static irqreturn_t smbb_chg_trkl_handler(int irq, void *_data)
466 struct smbb_charger *chg = _data;
468 smbb_set_line_flag(chg, irq, STATUS_CHG_TRKL);
469 power_supply_changed(chg->bat_psy);
474 static const struct smbb_irq {
476 irqreturn_t (*handler)(int, void *);
477 } smbb_charger_irqs[] = {
478 { "chg-done", smbb_chg_done_handler },
479 { "chg-fast", smbb_chg_fast_handler },
480 { "chg-trkl", smbb_chg_trkl_handler },
481 { "bat-temp-ok", smbb_bat_temp_handler },
482 { "bat-present", smbb_bat_present_handler },
483 { "chg-gone", smbb_chg_gone_handler },
484 { "usb-valid", smbb_usb_valid_handler },
485 { "dc-valid", smbb_dc_valid_handler },
488 static int smbb_usbin_get_property(struct power_supply *psy,
489 enum power_supply_property psp,
490 union power_supply_propval *val)
492 struct smbb_charger *chg = power_supply_get_drvdata(psy);
496 case POWER_SUPPLY_PROP_ONLINE:
497 mutex_lock(&chg->statlock);
498 val->intval = !(chg->status & STATUS_CHG_GONE) &&
499 (chg->status & STATUS_USBIN_VALID);
500 mutex_unlock(&chg->statlock);
502 case POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT:
503 val->intval = chg->attr[ATTR_USBIN_IMAX];
505 case POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT_MAX:
506 val->intval = 2500000;
516 static int smbb_usbin_set_property(struct power_supply *psy,
517 enum power_supply_property psp,
518 const union power_supply_propval *val)
520 struct smbb_charger *chg = power_supply_get_drvdata(psy);
524 case POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT:
525 rc = smbb_charger_attr_write(chg, ATTR_USBIN_IMAX,
536 static int smbb_dcin_get_property(struct power_supply *psy,
537 enum power_supply_property psp,
538 union power_supply_propval *val)
540 struct smbb_charger *chg = power_supply_get_drvdata(psy);
544 case POWER_SUPPLY_PROP_ONLINE:
545 mutex_lock(&chg->statlock);
546 val->intval = !(chg->status & STATUS_CHG_GONE) &&
547 (chg->status & STATUS_DCIN_VALID);
548 mutex_unlock(&chg->statlock);
550 case POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT:
551 val->intval = chg->attr[ATTR_DCIN_IMAX];
553 case POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT_MAX:
554 val->intval = 2500000;
564 static int smbb_dcin_set_property(struct power_supply *psy,
565 enum power_supply_property psp,
566 const union power_supply_propval *val)
568 struct smbb_charger *chg = power_supply_get_drvdata(psy);
572 case POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT:
573 rc = smbb_charger_attr_write(chg, ATTR_DCIN_IMAX,
584 static int smbb_charger_writable_property(struct power_supply *psy,
585 enum power_supply_property psp)
587 return psp == POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT;
590 static int smbb_battery_get_property(struct power_supply *psy,
591 enum power_supply_property psp,
592 union power_supply_propval *val)
594 struct smbb_charger *chg = power_supply_get_drvdata(psy);
595 unsigned long status;
598 mutex_lock(&chg->statlock);
599 status = chg->status;
600 mutex_unlock(&chg->statlock);
603 case POWER_SUPPLY_PROP_STATUS:
604 if (status & STATUS_CHG_GONE)
605 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
606 else if (!(status & (STATUS_DCIN_VALID | STATUS_USBIN_VALID)))
607 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
608 else if (status & STATUS_CHG_DONE)
609 val->intval = POWER_SUPPLY_STATUS_FULL;
610 else if (!(status & STATUS_BAT_OK))
611 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
612 else if (status & (STATUS_CHG_FAST | STATUS_CHG_TRKL))
613 val->intval = POWER_SUPPLY_STATUS_CHARGING;
614 else /* everything is ok for charging, but we are not... */
615 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
617 case POWER_SUPPLY_PROP_HEALTH:
618 if (status & STATUS_BAT_OK)
619 val->intval = POWER_SUPPLY_HEALTH_GOOD;
620 else if (status & STATUS_BAT_HOT)
621 val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
623 val->intval = POWER_SUPPLY_HEALTH_COLD;
625 case POWER_SUPPLY_PROP_CHARGE_TYPE:
626 if (status & STATUS_CHG_FAST)
627 val->intval = POWER_SUPPLY_CHARGE_TYPE_FAST;
628 else if (status & STATUS_CHG_TRKL)
629 val->intval = POWER_SUPPLY_CHARGE_TYPE_TRICKLE;
631 val->intval = POWER_SUPPLY_CHARGE_TYPE_NONE;
633 case POWER_SUPPLY_PROP_PRESENT:
634 val->intval = !!(status & STATUS_BAT_PRESENT);
636 case POWER_SUPPLY_PROP_CURRENT_MAX:
637 val->intval = chg->attr[ATTR_BAT_IMAX];
639 case POWER_SUPPLY_PROP_VOLTAGE_MAX:
640 val->intval = chg->attr[ATTR_BAT_VMAX];
642 case POWER_SUPPLY_PROP_TECHNOLOGY:
643 /* this charger is a single-cell lithium-ion battery charger
644 * only. If you hook up some other technology, there will be
647 val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
649 case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
650 val->intval = 3000000; /* single-cell li-ion low end */
660 static int smbb_battery_set_property(struct power_supply *psy,
661 enum power_supply_property psp,
662 const union power_supply_propval *val)
664 struct smbb_charger *chg = power_supply_get_drvdata(psy);
668 case POWER_SUPPLY_PROP_CURRENT_MAX:
669 rc = smbb_charger_attr_write(chg, ATTR_BAT_IMAX, val->intval);
671 case POWER_SUPPLY_PROP_VOLTAGE_MAX:
672 rc = smbb_charger_attr_write(chg, ATTR_BAT_VMAX, val->intval);
682 static int smbb_battery_writable_property(struct power_supply *psy,
683 enum power_supply_property psp)
686 case POWER_SUPPLY_PROP_CURRENT_MAX:
687 case POWER_SUPPLY_PROP_VOLTAGE_MAX:
694 static enum power_supply_property smbb_charger_properties[] = {
695 POWER_SUPPLY_PROP_ONLINE,
696 POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT,
697 POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT_MAX,
700 static enum power_supply_property smbb_battery_properties[] = {
701 POWER_SUPPLY_PROP_STATUS,
702 POWER_SUPPLY_PROP_HEALTH,
703 POWER_SUPPLY_PROP_PRESENT,
704 POWER_SUPPLY_PROP_CHARGE_TYPE,
705 POWER_SUPPLY_PROP_CURRENT_MAX,
706 POWER_SUPPLY_PROP_VOLTAGE_MAX,
707 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
708 POWER_SUPPLY_PROP_TECHNOLOGY,
711 static const struct reg_off_mask_default {
715 unsigned int rev_mask;
716 } smbb_charger_setup[] = {
717 /* The bootloader is supposed to set this... make sure anyway. */
718 { SMBB_MISC_BOOT_DONE, BOOT_DONE, BOOT_DONE },
720 /* Disable software timer */
721 { SMBB_CHG_TCHG_MAX_EN, TCHG_MAX_EN, 0 },
723 /* Clear and disable watchdog */
724 { SMBB_CHG_WDOG_TIME, 0xff, 160 },
725 { SMBB_CHG_WDOG_EN, WDOG_EN, 0 },
727 /* Use charger based EoC detection */
728 { SMBB_CHG_IBAT_TERM_CHG, IBAT_TERM_CHG_IEOC, IBAT_TERM_CHG_IEOC_CHG },
730 /* Disable GSM PA load adjustment.
731 * The PA signal is incorrectly connected on v2.
733 { SMBB_CHG_CFG, 0xff, 0x00, BIT(3) },
735 /* Use VBAT (not VSYS) to compensate for IR drop during fast charging */
736 { SMBB_BUCK_REG_MODE, BUCK_REG_MODE, BUCK_REG_MODE_VBAT },
738 /* Enable battery temperature comparators */
739 { SMBB_BAT_BTC_CTRL, BTC_CTRL_COMP_EN, BTC_CTRL_COMP_EN },
741 /* Stop USB enumeration timer */
742 { SMBB_USB_ENUM_TIMER_STOP, ENUM_TIMER_STOP, ENUM_TIMER_STOP },
744 #if 0 /* FIXME supposedly only to disable hardware ARB termination */
745 { SMBB_USB_SEC_ACCESS, SEC_ACCESS_MAGIC },
746 { SMBB_USB_REV_BST, 0xff, REV_BST_CHG_GONE },
749 /* Stop USB enumeration timer, again */
750 { SMBB_USB_ENUM_TIMER_STOP, ENUM_TIMER_STOP, ENUM_TIMER_STOP },
752 /* Enable charging */
753 { SMBB_CHG_CTRL, CTRL_EN, CTRL_EN },
756 static char *smbb_bif[] = { "smbb-bif" };
758 static const struct power_supply_desc bat_psy_desc = {
760 .type = POWER_SUPPLY_TYPE_BATTERY,
761 .properties = smbb_battery_properties,
762 .num_properties = ARRAY_SIZE(smbb_battery_properties),
763 .get_property = smbb_battery_get_property,
764 .set_property = smbb_battery_set_property,
765 .property_is_writeable = smbb_battery_writable_property,
768 static const struct power_supply_desc usb_psy_desc = {
769 .name = "smbb-usbin",
770 .type = POWER_SUPPLY_TYPE_USB,
771 .properties = smbb_charger_properties,
772 .num_properties = ARRAY_SIZE(smbb_charger_properties),
773 .get_property = smbb_usbin_get_property,
774 .set_property = smbb_usbin_set_property,
775 .property_is_writeable = smbb_charger_writable_property,
778 static const struct power_supply_desc dc_psy_desc = {
780 .type = POWER_SUPPLY_TYPE_MAINS,
781 .properties = smbb_charger_properties,
782 .num_properties = ARRAY_SIZE(smbb_charger_properties),
783 .get_property = smbb_dcin_get_property,
784 .set_property = smbb_dcin_set_property,
785 .property_is_writeable = smbb_charger_writable_property,
788 static int smbb_chg_otg_enable(struct regulator_dev *rdev)
790 struct smbb_charger *chg = rdev_get_drvdata(rdev);
793 rc = regmap_update_bits(chg->regmap, chg->addr + SMBB_USB_OTG_CTL,
794 OTG_CTL_EN, OTG_CTL_EN);
796 dev_err(chg->dev, "failed to update OTG_CTL\n");
800 static int smbb_chg_otg_disable(struct regulator_dev *rdev)
802 struct smbb_charger *chg = rdev_get_drvdata(rdev);
805 rc = regmap_update_bits(chg->regmap, chg->addr + SMBB_USB_OTG_CTL,
808 dev_err(chg->dev, "failed to update OTG_CTL\n");
812 static int smbb_chg_otg_is_enabled(struct regulator_dev *rdev)
814 struct smbb_charger *chg = rdev_get_drvdata(rdev);
815 unsigned int value = 0;
818 rc = regmap_read(chg->regmap, chg->addr + SMBB_USB_OTG_CTL, &value);
820 dev_err(chg->dev, "failed to read OTG_CTL\n");
822 return !!(value & OTG_CTL_EN);
825 static const struct regulator_ops smbb_chg_otg_ops = {
826 .enable = smbb_chg_otg_enable,
827 .disable = smbb_chg_otg_disable,
828 .is_enabled = smbb_chg_otg_is_enabled,
831 static int smbb_charger_probe(struct platform_device *pdev)
833 struct power_supply_config bat_cfg = {};
834 struct power_supply_config usb_cfg = {};
835 struct power_supply_config dc_cfg = {};
836 struct smbb_charger *chg;
837 struct regulator_config config = { };
840 chg = devm_kzalloc(&pdev->dev, sizeof(*chg), GFP_KERNEL);
844 chg->dev = &pdev->dev;
845 mutex_init(&chg->statlock);
847 chg->regmap = dev_get_regmap(pdev->dev.parent, NULL);
849 dev_err(&pdev->dev, "failed to locate regmap\n");
853 rc = of_property_read_u32(pdev->dev.of_node, "reg", &chg->addr);
855 dev_err(&pdev->dev, "missing or invalid 'reg' property\n");
859 rc = regmap_read(chg->regmap, chg->addr + SMBB_MISC_REV2, &chg->revision);
861 dev_err(&pdev->dev, "unable to read revision\n");
866 if (chg->revision != 1 && chg->revision != 2 && chg->revision != 3) {
867 dev_err(&pdev->dev, "v%d hardware not supported\n", chg->revision);
870 dev_info(&pdev->dev, "Initializing SMBB rev %u", chg->revision);
872 chg->dc_disabled = of_property_read_bool(pdev->dev.of_node, "qcom,disable-dc");
874 for (i = 0; i < _ATTR_CNT; ++i) {
875 rc = smbb_charger_attr_parse(chg, i);
877 dev_err(&pdev->dev, "failed to parse/apply settings\n");
882 bat_cfg.drv_data = chg;
883 bat_cfg.of_node = pdev->dev.of_node;
884 chg->bat_psy = devm_power_supply_register(&pdev->dev,
887 if (IS_ERR(chg->bat_psy)) {
888 dev_err(&pdev->dev, "failed to register battery\n");
889 return PTR_ERR(chg->bat_psy);
892 usb_cfg.drv_data = chg;
893 usb_cfg.supplied_to = smbb_bif;
894 usb_cfg.num_supplicants = ARRAY_SIZE(smbb_bif);
895 chg->usb_psy = devm_power_supply_register(&pdev->dev,
898 if (IS_ERR(chg->usb_psy)) {
899 dev_err(&pdev->dev, "failed to register USB power supply\n");
900 return PTR_ERR(chg->usb_psy);
903 chg->edev = devm_extcon_dev_allocate(&pdev->dev, smbb_usb_extcon_cable);
904 if (IS_ERR(chg->edev)) {
905 dev_err(&pdev->dev, "failed to allocate extcon device\n");
909 rc = devm_extcon_dev_register(&pdev->dev, chg->edev);
911 dev_err(&pdev->dev, "failed to register extcon device\n");
915 if (!chg->dc_disabled) {
916 dc_cfg.drv_data = chg;
917 dc_cfg.supplied_to = smbb_bif;
918 dc_cfg.num_supplicants = ARRAY_SIZE(smbb_bif);
919 chg->dc_psy = devm_power_supply_register(&pdev->dev,
922 if (IS_ERR(chg->dc_psy)) {
923 dev_err(&pdev->dev, "failed to register DC power supply\n");
924 return PTR_ERR(chg->dc_psy);
928 for (i = 0; i < ARRAY_SIZE(smbb_charger_irqs); ++i) {
931 irq = platform_get_irq_byname(pdev, smbb_charger_irqs[i].name);
935 smbb_charger_irqs[i].handler(irq, chg);
937 rc = devm_request_threaded_irq(&pdev->dev, irq, NULL,
938 smbb_charger_irqs[i].handler, IRQF_ONESHOT,
939 smbb_charger_irqs[i].name, chg);
941 dev_err(&pdev->dev, "failed to request irq '%s'\n",
942 smbb_charger_irqs[i].name);
948 * otg regulator is used to control VBUS voltage direction
949 * when USB switches between host and gadget mode
951 chg->otg_rdesc.id = -1;
952 chg->otg_rdesc.name = "otg-vbus";
953 chg->otg_rdesc.ops = &smbb_chg_otg_ops;
954 chg->otg_rdesc.owner = THIS_MODULE;
955 chg->otg_rdesc.type = REGULATOR_VOLTAGE;
956 chg->otg_rdesc.supply_name = "usb-otg-in";
957 chg->otg_rdesc.of_match = "otg-vbus";
959 config.dev = &pdev->dev;
960 config.driver_data = chg;
962 chg->otg_reg = devm_regulator_register(&pdev->dev, &chg->otg_rdesc,
964 if (IS_ERR(chg->otg_reg))
965 return PTR_ERR(chg->otg_reg);
967 chg->jeita_ext_temp = of_property_read_bool(pdev->dev.of_node,
968 "qcom,jeita-extended-temp-range");
970 /* Set temperature range to [35%:70%] or [25%:80%] accordingly */
971 rc = regmap_update_bits(chg->regmap, chg->addr + SMBB_BAT_BTC_CTRL,
972 BTC_CTRL_COLD_EXT | BTC_CTRL_HOT_EXT_N,
973 chg->jeita_ext_temp ?
978 "unable to set %s temperature range\n",
979 chg->jeita_ext_temp ? "JEITA extended" : "normal");
983 for (i = 0; i < ARRAY_SIZE(smbb_charger_setup); ++i) {
984 const struct reg_off_mask_default *r = &smbb_charger_setup[i];
986 if (r->rev_mask & BIT(chg->revision))
989 rc = regmap_update_bits(chg->regmap, chg->addr + r->offset,
993 "unable to initializing charging, bailing\n");
998 platform_set_drvdata(pdev, chg);
1003 static void smbb_charger_remove(struct platform_device *pdev)
1005 struct smbb_charger *chg;
1007 chg = platform_get_drvdata(pdev);
1009 regmap_update_bits(chg->regmap, chg->addr + SMBB_CHG_CTRL, CTRL_EN, 0);
1012 static const struct of_device_id smbb_charger_id_table[] = {
1013 { .compatible = "qcom,pm8226-charger" },
1014 { .compatible = "qcom,pm8941-charger" },
1017 MODULE_DEVICE_TABLE(of, smbb_charger_id_table);
1019 static struct platform_driver smbb_charger_driver = {
1020 .probe = smbb_charger_probe,
1021 .remove_new = smbb_charger_remove,
1023 .name = "qcom-smbb",
1024 .of_match_table = smbb_charger_id_table,
1027 module_platform_driver(smbb_charger_driver);
1029 MODULE_DESCRIPTION("Qualcomm Switch-Mode Battery Charger and Boost driver");
1030 MODULE_LICENSE("GPL v2");