From 9c8d0f165496081750b7446bf22c3f5bcc9ff21f Mon Sep 17 00:00:00 2001 From: Raj Aryan Date: Thu, 26 Nov 2026 16:30:17 +0530 Subject: [PATCH 1/3] FROMLIST: dt-bindings: power: supply: Add qcom,pm4125-charger PM4125 integrates the SMBLite switch-mode battery charger IP, which differs enough from the SMB2 IP already supported on PMI8998/PM660 (separate SPMI peripheral bases per block, a different interrupt set, raw ADC-based JEITA thresholds) that it has no existing binding to reuse. Add one to describe it. Link: https://lore.kernel.org/all/20261126-smblite-charger-for-pm4125-v2-1-20656509518b@oss.qualcomm.com/ Signed-off-by: Raj Aryan Signed-off-by: Rakesh Kota --- .../bindings/mfd/qcom,spmi-pmic.yaml | 1 + .../power/supply/qcom,pm4125-charger.yaml | 119 ++++++++++++++++++ 2 files changed, 120 insertions(+) create mode 100644 Documentation/devicetree/bindings/power/supply/qcom,pm4125-charger.yaml diff --git a/Documentation/devicetree/bindings/mfd/qcom,spmi-pmic.yaml b/Documentation/devicetree/bindings/mfd/qcom,spmi-pmic.yaml index e57ced1559672..e493449a8f290 100644 --- a/Documentation/devicetree/bindings/mfd/qcom,spmi-pmic.yaml +++ b/Documentation/devicetree/bindings/mfd/qcom,spmi-pmic.yaml @@ -154,6 +154,7 @@ patternProperties: "^charger@[0-9a-f]+$": type: object oneOf: + - $ref: /schemas/power/supply/qcom,pm4125-charger.yaml# - $ref: /schemas/power/supply/qcom,pm8916-lbc.yaml# - $ref: /schemas/power/supply/qcom,pm8941-charger.yaml# - $ref: /schemas/power/supply/qcom,pm8941-coincell.yaml# diff --git a/Documentation/devicetree/bindings/power/supply/qcom,pm4125-charger.yaml b/Documentation/devicetree/bindings/power/supply/qcom,pm4125-charger.yaml new file mode 100644 index 0000000000000..4c88d836355b4 --- /dev/null +++ b/Documentation/devicetree/bindings/power/supply/qcom,pm4125-charger.yaml @@ -0,0 +1,119 @@ +# SPDX-License-Identifier: GPL-2.0 OR BSD-2-Clause +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/power/supply/qcom,pm4125-charger.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: Qualcomm PM4125 SMBLite Switch-Mode Battery Charger + +maintainers: + - Raj Aryan + - Rakesh Kota + +description: | + The Qualcomm SMBLite is a switch-mode battery charger IP found on + the entry-level Qualcomm PM4125 PMIC. It exposes five separate + SPMI peripheral bases for CHGR, DCDC, USB, MISC and BATIF blocks and + supports USB input current limiting, JEITA temperature protection, + battery overvoltage detection and a hardware watchdog. + +properties: + compatible: + const: qcom,pm4125-charger + + reg: + maxItems: 1 + + interrupts: + maxItems: 4 + + interrupt-names: + items: + - const: usb-plugin + - const: bat-ov + - const: wdog-bark + - const: usbin-icl-change + + io-channels: + maxItems: 1 + description: + USB input voltage channel (ADC5_USB_IN_V_16). + + io-channel-names: + items: + - const: usbin_v + + monitored-battery: true + + qcom,jeita-soft-thresholds: + $ref: /schemas/types.yaml#/definitions/uint32-array + items: + - description: COOL threshold as a raw ADC comparator value. + maximum: 0xffff + - description: WARM threshold as a raw ADC comparator value. + maximum: 0xffff + description: | + JEITA soft temperature thresholds (COOL, WARM) as raw 16-bit ADC + comparator values written to the BATIF ADC comparator registers. + Example: <0x5314 0x25e3> for COOL=15°C, WARM=45°C. + + qcom,jeita-hard-thresholds: + $ref: /schemas/types.yaml#/definitions/uint32-array + items: + - description: COLD threshold as a raw ADC comparator value. + maximum: 0xffff + - description: HOT threshold as a raw ADC comparator value. + maximum: 0xffff + description: | + JEITA hard temperature thresholds (COLD, HOT) as raw 16-bit ADC + comparator values written to the BATIF ADC comparator registers. + Example: <0x58cd 0x20b8> for COLD=0°C, HOT=60°C. + +required: + - compatible + - reg + - interrupts + - interrupt-names + - io-channels + - io-channel-names + +dependencies: + qcom,jeita-soft-thresholds: + - qcom,jeita-hard-thresholds + qcom,jeita-hard-thresholds: + - qcom,jeita-soft-thresholds + +allOf: + - $ref: power-supply.yaml# + +additionalProperties: false + +examples: + - | + #include + #include + + pmic { + #address-cells = <1>; + #size-cells = <0>; + + charger@1000 { + compatible = "qcom,pm4125-charger"; + reg = <0x1000>; + + interrupts = <0x0 0x13 0x0 IRQ_TYPE_EDGE_BOTH>, + <0x0 0x12 0x3 IRQ_TYPE_EDGE_BOTH>, + <0x0 0x16 0x1 IRQ_TYPE_EDGE_RISING>, + <0x0 0x13 0x6 IRQ_TYPE_EDGE_RISING>; + interrupt-names = "usb-plugin", + "bat-ov", + "wdog-bark", + "usbin-icl-change"; + + io-channels = <&pm4125_vadc ADC5_USB_IN_V_16>; + io-channel-names = "usbin_v"; + + qcom,jeita-soft-thresholds = <0x5314 0x25e3>; /* COOL, WARM */ + qcom,jeita-hard-thresholds = <0x58cd 0x20b8>; /* COLD, HOT */ + }; + }; From 06580f9224b3122bc1aeaac91e81d16dc029dcdc Mon Sep 17 00:00:00 2001 From: Raj Aryan Date: Thu, 26 Nov 2026 16:30:18 +0530 Subject: [PATCH 2/3] FROMLIST: power: supply: qcom_smbx: Add PM4125 SMBLite charger support PM4125 uses the SMBLite charger IP, a reduced-feature variant of SMB2. While both use the same SPMI bus and driver model, they differ in register layout, status reporting, recharge mode and JEITA configuration. Extend qcom_smbx to handle these differences so PM4125 can use the existing charger driver. Select register bases and handling based on chip->type, allowing SMB2 and SMBLite to share one driver and power-supply class device. Link: https://lore.kernel.org/all/20261126-smblite-charger-for-pm4125-v2-2-20656509518b@oss.qualcomm.com/ Signed-off-by: Raj Aryan Signed-off-by: Rakesh Kota --- drivers/power/supply/qcom_smbx.c | 1076 ++++++++++++++++++++++++++---- 1 file changed, 964 insertions(+), 112 deletions(-) diff --git a/drivers/power/supply/qcom_smbx.c b/drivers/power/supply/qcom_smbx.c index ad2cf516e2ded..253ea997b6801 100644 --- a/drivers/power/supply/qcom_smbx.c +++ b/drivers/power/supply/qcom_smbx.c @@ -4,8 +4,9 @@ * Copyright (c) 2023, Linaro Ltd. * Author: Casey Connolly * - * This driver is for the switch-mode battery charger and boost - * hardware found in pmi8998 and related PMICs. + * This driver supports the switch-mode battery charger found in: + * - PMI8998 / PM660 (SMB2 IP) + * - PM4125 (SMBLite IP) */ #include @@ -20,8 +21,16 @@ #include #include #include +#include #include #include +#include + +/* PMIC IP types */ +enum smb_type { + SMB2, /* PMI8998, PM660 - SMB2 IP */ + SMBLITE, /* PM4125 - SMBLite IP */ +}; /* clang-format off */ #define BATTERY_CHARGER_STATUS_1 0x06 @@ -352,6 +361,118 @@ #define SMB2_FLOAT_VOLTAGE_MIN_UV 3487500 #define SMB2_FLOAT_VOLTAGE_MAX_UV 4920000 #define SMB2_FLOAT_VOLTAGE_STEP_UV 7500 + +/* ================================================================ + * SMBLite specific registers and base addresses + * Source: downstream smblite-reg.h (qpnp-smblite driver) + * ================================================================ + */ +/* SMBLite peripheral base addresses (PM4125) */ +#define SMBLITE_CHGR_BASE 0x1000 +#define SMBLITE_DCDC_BASE 0x1100 +#define SMBLITE_BATIF_BASE 0x1200 +#define SMBLITE_USBIN_BASE 0x1300 +/* 0x1500 (typec) belongs to the qcom,pm4125-typec peripheral, not this driver */ +#define SMBLITE_MISC_BASE 0x1600 + +/* ---- SMBLITE CHGR registers (chg_base offsets) ---- */ + +#define SMBLITE_BATTERY_CHARGER_STATUS_1 0x06 +#define SMBLITE_CHARGER_VBAT_STATUS 0x08 +#define SMBLITE_BAT_OV_BIT BIT(7) + +#define SMBLITE_CHARGING_ENABLE_CMD 0x46 +#define SMBLITE_CHARGING_ENABLE_CMD_BIT BIT(0) + +#define SMBLITE_CHGR_FAST_CHARGE_CURRENT_CFG 0x54 +#define SMBLITE_CHGR_FLOAT_VOLTAGE_CFG 0x58 +/* + * chg_base + 0x60 = CHGR_TERM_CFG_REG (termination current config). + * SMBLite has NO pre-charge current register at this offset. + * Do NOT write pre-charge current to 0x60. + */ +#define SMBLITE_CHGR_CHG_TERM_CFG 0x60 + +#define SMBLITE_CHGR_RECHG_CFG 0x70 +#define SMBLITE_RECHG_MASK GENMASK(7, 6) +#define SMBLITE_CHGR_RECHG_SOC_THD_CFG 0x74 +#define SMBLITE_CHGR_CHG_INHIBIT_CFG 0x78 + +/* + * ---- SMBLite DCDC registers (dcdc_base offsets) ---- + * This peripheral also holds the OTG/VBUS enable registers (0x50/0x52/0x56), + * which belong to the qcom,pm4125-vbus-reg peripheral and are intentionally + * not touched here. The three below are status/config registers with no + * other home, so they are read directly by this driver. + */ +#define SMBLITE_ICL_STATUS 0x09 + +#define SMBLITE_POWER_PATH_STATUS 0x0B +#define SMBLITE_VALID_INPUT_POWER_SOURCE_BIT BIT(7) +#define SMBLITE_USE_USBIN_BIT BIT(5) + +#define SMBLITE_DCDC_LDO_CFG 0x70 +#define SMBLITE_LDO_MODE_BIT BIT(0) + +#define SMBLITE_BATIF_BPD_CFG 0x60 + +#define SMBLITE_BATIF_JEITA_COMP_STATUS 0x0C +#define SMBLITE_JEITA_ADC_THOT_AFP_BIT BIT(5) +#define SMBLITE_JEITA_ADC_THOT_BIT BIT(4) +#define SMBLITE_JEITA_ADC_WARM_BIT BIT(3) +#define SMBLITE_JEITA_ADC_COOL_BIT BIT(2) +#define SMBLITE_JEITA_ADC_TCOLD_BIT BIT(1) +#define SMBLITE_JEITA_ADC_TCOLD_AFP_BIT BIT(0) + +#define SMBLITE_JEITA_HOT_THRESHOLD_CFG 0x84 +#define SMBLITE_JEITA_WARM_THRESHOLD_CFG 0x86 +#define SMBLITE_JEITA_COOL_THRESHOLD_CFG 0x88 +#define SMBLITE_JEITA_COLD_THRESHOLD_CFG 0x8A + +#define SMBLITE_APSD_STATUS 0x0A +#define SMBLITE_APSD_RESULT_STATUS 0x0B +#define SMBLITE_CMD_APSD 0x45 +#define SMBLITE_USB_APSD_EN 0x44 +#define SMBLITE_USB_APSD_CFG 0x46 +#define SMBLITE_USBIN_QC23_EN 0x48 +#define SMBLITE_HVDCP_EN_BIT BIT(2) + +#define SMBLITE_USBIN_ICL_OPTIONS 0x50 +#define SMBLITE_USBIN_MODE_CHG_BIT BIT(2) +#define SMBLITE_USB51_MODE_BIT BIT(1) + +#define SMBLITE_USBIN_CURRENT_LIMIT_CFG 0x52 +#define SMBLITE_USBIN_INPUT_SUSPEND 0x54 +#define SMBLITE_SUSPEND_ON_COLLAPSE_USBIN_BIT BIT(7) + +#define SMBLITE_USBIN_AICL_OPTIONS_CFG 0x60 +#define SMBLITE_USBIN_AICL_EN_BIT BIT(7) +#define SMBLITE_USBIN_AICL_START_AT_MAX BIT(4) + +#define SMBLITE_MISC_AICL_RERUN_CFG 0x54 +#define SMBLITE_USBIN_AICL_PERIODIC_RERUN_EN_BIT BIT(5) +#define SMBLITE_USBIN_AICL_RERUN_TIME_MASK GENMASK(1, 0) +#define SMBLITE_AICL_RERUN_TIME_12S_VAL 0x01 + +#define SMBLITE_WD_CFG 0x58 +#define SMBLITE_BITE_WDOG_DISABLE_CHARGING_CFG_BIT BIT(7) +#define SMBLITE_BARK_WDOG_INT_EN_BIT BIT(4) +#define SMBLITE_WDOG_TIMER_EN_ON_PLUGIN_BIT BIT(1) +#define SMBLITE_WDOG_TIMER_EN_BIT BIT(0) +#define SMBLITE_SNARL_BARK_BITE_WD_CFG 0x59 +#define SMBLITE_BARK_BITE_WDOG_PET 0x5A +#define SMBLITE_BARK_WDOG_TIMEOUT_MASK GENMASK(5, 4) /* 0x30 */ +#define SMBLITE_BITE_WDOG_TIMEOUT_MASK GENMASK(7, 6) /* 0xC0 */ +#define SMBLITE_BITE_WDOG_TIMEOUT_8S 0x3 +#define SMBLITE_BITE_WDOG_TIMEOUT_SHIFT 6 + +#define SMBLITE_ICL_STEP_UA 100000 +#define SMBLITE_FCC_STEP_UA 100000 +#define SMBLITE_ICL_MAX_UA 2000000 +#define SMBLITE_FV_STEP_UV 20000 +#define SMBLITE_FV_MIN_UV 3600000 +#define SMBLITE_FV_MAX_UV 4600000 + /* clang-format on */ enum charger_status { @@ -365,6 +486,17 @@ enum charger_status { DISABLE_CHARGE, }; +enum smblite_charger_status { + SMBLITE_INHIBIT_CHARGE = 0, + SMBLITE_TRICKLE_CHARGE, + SMBLITE_PRE_CHARGE, + SMBLITE_FULLON_CHARGE, + SMBLITE_TAPER_CHARGE, + SMBLITE_TERMINATE_CHARGE, + SMBLITE_CHARGING_PAUSED, + SMBLITE_CHARGING_DISABLED, +}; + struct smb_init_register { u16 addr; u8 mask; @@ -374,6 +506,7 @@ struct smb_init_register { /** * struct smb_chip - smb chip structure * @dev: Device reference for power_supply + * @type: PMIC IP type (SMB2 or SMBLITE) * @name: The platform device name * @base: Base address for smb registers * @regmap: Register map @@ -381,13 +514,19 @@ struct smb_init_register { * @initial_charge_enable: Charging enable state before hardware setup * @status_change_work: Worker to handle plug/unplug events * @cable_irq: USB plugin IRQ + * @wdog_irq: Watchdog bark IRQ * @wakeup_enabled: If the cable IRQ will cause a wakeup * @usb_in_i_chan: USB_IN current measurement channel * @usb_in_v_chan: USB_IN voltage measurement channel * @chg_psy: Charger power supply instance + * @ldo_mode: True if operating in LDO mode (SMBLITE only) + * @icl_step_ua: ICL step size in uA + * @icl_max_ua: Maximum ICL in uA + * @charge_status: Cached POWER_SUPPLY_STATUS_* value */ struct smb_chip { struct device *dev; + enum smb_type type; const char *name; unsigned int base; struct regmap *regmap; @@ -396,12 +535,17 @@ struct smb_chip { struct delayed_work status_change_work; int cable_irq; + int wdog_irq; bool wakeup_enabled; struct iio_channel *usb_in_i_chan; struct iio_channel *usb_in_v_chan; struct power_supply *chg_psy; + bool ldo_mode; + unsigned int icl_step_ua; + unsigned int icl_max_ua; + int charge_status; }; static enum power_supply_property smb_properties[] = { @@ -416,19 +560,49 @@ static enum power_supply_property smb_properties[] = { POWER_SUPPLY_PROP_USB_TYPE, }; +static enum power_supply_property smblite_properties[] = { + POWER_SUPPLY_PROP_MANUFACTURER, + POWER_SUPPLY_PROP_MODEL_NAME, + POWER_SUPPLY_PROP_CURRENT_MAX, + POWER_SUPPLY_PROP_VOLTAGE_NOW, + POWER_SUPPLY_PROP_STATUS, + POWER_SUPPLY_PROP_HEALTH, + POWER_SUPPLY_PROP_ONLINE, + POWER_SUPPLY_PROP_CHARGE_TYPE, + POWER_SUPPLY_PROP_USB_TYPE, +}; + +/* + * smb_get_prop_usb_online - check whether a valid USB input is present. + * + * SMB2 (0x60B): P_PATH_USE_USBIN BIT(4) && VALID_INPUT BIT(0) + * SMBLite (dcdc_base + 0x0B = 0x110B): + * VALID_INPUT_POWER_SOURCE_STS BIT(7) && USE_USBIN BIT(5) + */ static int smb_get_prop_usb_online(struct smb_chip *chip, int *val) { unsigned int stat; + u16 reg; int rc; - rc = regmap_read(chip->regmap, chip->base + POWER_PATH_STATUS, &stat); + if (chip->type == SMBLITE) + reg = SMBLITE_DCDC_BASE + SMBLITE_POWER_PATH_STATUS; + else + reg = chip->base + POWER_PATH_STATUS; + + rc = regmap_read(chip->regmap, reg, &stat); if (rc < 0) { dev_err(chip->dev, "Couldn't read power path status: %d\n", rc); return rc; } - *val = (stat & P_PATH_USE_USBIN_BIT) && - (stat & P_PATH_VALID_INPUT_POWER_SOURCE_STS_BIT); + if (chip->type == SMBLITE) + *val = (stat & SMBLITE_VALID_INPUT_POWER_SOURCE_BIT) && + (stat & SMBLITE_USE_USBIN_BIT); + else + *val = (stat & P_PATH_USE_USBIN_BIT) && + (stat & P_PATH_VALID_INPUT_POWER_SOURCE_STS_BIT); + return 0; } @@ -439,6 +613,7 @@ static int smb_get_prop_usb_online(struct smb_chip *chip, int *val) static int smb_apsd_get_charger_type(struct smb_chip *chip, int *val) { unsigned int apsd_stat, stat; + u16 apsd_reg, result_reg; int usb_online = 0; int rc; @@ -448,7 +623,15 @@ static int smb_apsd_get_charger_type(struct smb_chip *chip, int *val) return rc; } - rc = regmap_read(chip->regmap, chip->base + APSD_STATUS, &apsd_stat); + if (chip->type == SMBLITE) { + apsd_reg = SMBLITE_USBIN_BASE + SMBLITE_APSD_STATUS; + result_reg = SMBLITE_USBIN_BASE + SMBLITE_APSD_RESULT_STATUS; + } else { + apsd_reg = chip->base + APSD_STATUS; + result_reg = chip->base + APSD_RESULT_STATUS; + } + + rc = regmap_read(chip->regmap, apsd_reg, &apsd_stat); if (rc < 0) { dev_err(chip->dev, "Failed to read apsd status, rc = %d", rc); return rc; @@ -458,7 +641,7 @@ static int smb_apsd_get_charger_type(struct smb_chip *chip, int *val) return -EAGAIN; } - rc = regmap_read(chip->regmap, chip->base + APSD_RESULT_STATUS, &stat); + rc = regmap_read(chip->regmap, result_reg, &stat); if (rc < 0) { dev_err(chip->dev, "Failed to read apsd result, rc = %d", rc); return rc; @@ -476,11 +659,33 @@ static int smb_apsd_get_charger_type(struct smb_chip *chip, int *val) return 0; } +static unsigned int smb_usb_type_current_ua(enum power_supply_usb_type usb_type) +{ + switch (usb_type) { + case POWER_SUPPLY_USB_TYPE_CDP: + return CDP_CURRENT_UA; + case POWER_SUPPLY_USB_TYPE_DCP: + return DCP_CURRENT_UA; + case POWER_SUPPLY_USB_TYPE_SDP: + default: + return SDP_CURRENT_UA; + } +} + +/* + * smb_get_prop_status - report POWER_SUPPLY_STATUS_* for the battery. + * + * BAT_OV detection: + * SMB2: BATTERY_CHARGER_STATUS_2 (chip->base + 0x07), BIT(5) + * SMBLite: CHARGER_VBAT_STATUS_REG (chg_base + 0x08 = 0x1008), BIT(7) + * + * Charger status enum differs between IPs -- handled by dedicated branches. + */ static int smb_get_prop_status(struct smb_chip *chip, int *val) { - unsigned char stat[2]; - int usb_online = 0; - int rc; + unsigned int chg_status, stat_batov; + u16 chg_status_reg, reg_batov; + int usb_online = 0, rc; rc = smb_get_prop_usb_online(chip, &usb_online); if (!usb_online) { @@ -488,65 +693,132 @@ static int smb_get_prop_status(struct smb_chip *chip, int *val) return rc; } - rc = regmap_bulk_read(chip->regmap, - chip->base + BATTERY_CHARGER_STATUS_1, &stat, 2); + if (chip->type == SMBLITE) { + chg_status_reg = SMBLITE_CHGR_BASE + SMBLITE_BATTERY_CHARGER_STATUS_1; + reg_batov = SMBLITE_CHGR_BASE + SMBLITE_CHARGER_VBAT_STATUS; + } else { + chg_status_reg = chip->base + BATTERY_CHARGER_STATUS_1; + reg_batov = chip->base + BATTERY_CHARGER_STATUS_2; + } + + rc = regmap_read(chip->regmap, reg_batov, &stat_batov); if (rc < 0) { - dev_err(chip->dev, "Failed to read charging status ret=%d\n", - rc); + dev_err(chip->dev, "Failed to read bat-ov status ret=%d\n", rc); return rc; } - if (stat[1] & CHARGER_ERROR_STATUS_BAT_OV_BIT) { - *val = POWER_SUPPLY_STATUS_NOT_CHARGING; - return 0; + /* Check BAT_OV -- different register/bit per IP */ + if (chip->type == SMBLITE) { + if (stat_batov & SMBLITE_BAT_OV_BIT) { + *val = POWER_SUPPLY_STATUS_NOT_CHARGING; + chip->charge_status = POWER_SUPPLY_STATUS_NOT_CHARGING; + return 0; + } + } else { + if (stat_batov & CHARGER_ERROR_STATUS_BAT_OV_BIT) { + *val = POWER_SUPPLY_STATUS_NOT_CHARGING; + chip->charge_status = POWER_SUPPLY_STATUS_NOT_CHARGING; + return 0; + } } - stat[0] = stat[0] & BATTERY_CHARGER_STATUS_MASK; - - switch (stat[0]) { - case TRICKLE_CHARGE: - case PRE_CHARGE: - case FAST_CHARGE: - case FULLON_CHARGE: - case TAPER_CHARGE: - *val = POWER_SUPPLY_STATUS_CHARGING; - return rc; - case DISABLE_CHARGE: - *val = POWER_SUPPLY_STATUS_NOT_CHARGING; - return rc; - case TERMINATE_CHARGE: - case INHIBIT_CHARGE: - *val = POWER_SUPPLY_STATUS_FULL; - return rc; - default: - *val = POWER_SUPPLY_STATUS_UNKNOWN; + rc = regmap_read(chip->regmap, chg_status_reg, &chg_status); + if (rc < 0) { + dev_err(chip->dev, "Failed to read charging status ret=%d\n", rc); return rc; } + + chg_status &= BATTERY_CHARGER_STATUS_MASK; + + if (chip->type == SMBLITE) { + switch (chg_status) { + case SMBLITE_TRICKLE_CHARGE: + case SMBLITE_PRE_CHARGE: + case SMBLITE_FULLON_CHARGE: + case SMBLITE_TAPER_CHARGE: + *val = POWER_SUPPLY_STATUS_CHARGING; + chip->charge_status = POWER_SUPPLY_STATUS_CHARGING; + break; + case SMBLITE_TERMINATE_CHARGE: + *val = POWER_SUPPLY_STATUS_FULL; + chip->charge_status = POWER_SUPPLY_STATUS_FULL; + break; + case SMBLITE_INHIBIT_CHARGE: + case SMBLITE_CHARGING_PAUSED: + case SMBLITE_CHARGING_DISABLED: + *val = POWER_SUPPLY_STATUS_NOT_CHARGING; + chip->charge_status = POWER_SUPPLY_STATUS_NOT_CHARGING; + break; + default: + *val = POWER_SUPPLY_STATUS_UNKNOWN; + chip->charge_status = POWER_SUPPLY_STATUS_UNKNOWN; + break; + } + } else { + switch (chg_status) { + case TRICKLE_CHARGE: + case PRE_CHARGE: + case FAST_CHARGE: + case FULLON_CHARGE: + case TAPER_CHARGE: + *val = POWER_SUPPLY_STATUS_CHARGING; + chip->charge_status = POWER_SUPPLY_STATUS_CHARGING; + break; + case DISABLE_CHARGE: + *val = POWER_SUPPLY_STATUS_NOT_CHARGING; + chip->charge_status = POWER_SUPPLY_STATUS_NOT_CHARGING; + break; + case TERMINATE_CHARGE: + case INHIBIT_CHARGE: + *val = POWER_SUPPLY_STATUS_FULL; + chip->charge_status = POWER_SUPPLY_STATUS_FULL; + break; + default: + *val = POWER_SUPPLY_STATUS_UNKNOWN; + chip->charge_status = POWER_SUPPLY_STATUS_UNKNOWN; + break; + } + } + + return 0; } static inline int smb_get_current_limit(struct smb_chip *chip, unsigned int *val) { - int rc = regmap_read(chip->regmap, chip->base + ICL_STATUS, val); + u16 reg; + int rc; + + if (chip->type == SMBLITE) + reg = SMBLITE_DCDC_BASE + SMBLITE_ICL_STATUS; + else + reg = chip->base + ICL_STATUS; + rc = regmap_read(chip->regmap, reg, val); if (rc >= 0) - *val *= CURRENT_SCALE_FACTOR; + *val *= chip->icl_step_ua; return rc; } static int smb_set_current_limit(struct smb_chip *chip, unsigned int val) { unsigned char val_raw; + u16 reg; - if (val > 4800000) { - dev_err(chip->dev, - "Can't set current limit higher than 4800000uA"); + if (val > chip->icl_max_ua) { + dev_err(chip->dev, "Can't set current limit higher than %duA", + chip->icl_max_ua); return -EINVAL; } - val_raw = val / CURRENT_SCALE_FACTOR; - return regmap_write(chip->regmap, chip->base + USBIN_CURRENT_LIMIT_CFG, - val_raw); + val_raw = val / chip->icl_step_ua; + + if (chip->type == SMBLITE) + reg = SMBLITE_USBIN_BASE + SMBLITE_USBIN_CURRENT_LIMIT_CFG; + else + reg = chip->base + USBIN_CURRENT_LIMIT_CFG; + + return regmap_write(chip->regmap, reg, val_raw); } static void smb_status_change_work(struct work_struct *work) @@ -576,27 +848,20 @@ static void smb_status_change_work(struct work_struct *work) } if (rc < 0) { - rc = regmap_update_bits(chip->regmap, chip->base + CMD_APSD, - APSD_RERUN_BIT, APSD_RERUN_BIT); + if (chip->type == SMBLITE) + rc = regmap_update_bits(chip->regmap, + SMBLITE_USBIN_BASE + SMBLITE_CMD_APSD, + APSD_RERUN_BIT, APSD_RERUN_BIT); + else + rc = regmap_update_bits(chip->regmap, chip->base + CMD_APSD, + APSD_RERUN_BIT, APSD_RERUN_BIT); schedule_delayed_work(&chip->status_change_work, msecs_to_jiffies(1000)); dev_dbg(chip->dev, "get charger type failed, rerun apsd\n"); return; } - switch (charger_type) { - case POWER_SUPPLY_USB_TYPE_CDP: - current_ua = CDP_CURRENT_UA; - break; - case POWER_SUPPLY_USB_TYPE_DCP: - current_ua = DCP_CURRENT_UA; - break; - case POWER_SUPPLY_USB_TYPE_SDP: - default: - current_ua = SDP_CURRENT_UA; - break; - } - + current_ua = smb_usb_type_current_ua(charger_type); smb_set_current_limit(chip, current_ua); power_supply_changed(chip->chg_psy); } @@ -622,11 +887,61 @@ static int smb_get_iio_chan(struct smb_chip *chip, struct iio_channel *chan, return iio_read_channel_processed(chan, val); } +/* + * smb_get_prop_health - battery health based on temperature status. + * + * SMBLite: BATTERY_TEMP_STATUS_REG = batif_base + 0x0C (smblite-reg.h) + * SMB2: BATTERY_CHARGER_STATUS_2 = chip->base + 0x07 + */ static int smb_get_prop_health(struct smb_chip *chip, int *val) { int rc; unsigned int stat; + if (chip->type == SMBLITE) { + rc = regmap_read(chip->regmap, + SMBLITE_CHGR_BASE + SMBLITE_CHARGER_VBAT_STATUS, + &stat); + if (rc < 0) { + dev_err(chip->dev, + "Couldn't read SMBLITE vbat status rc=%d\n", rc); + return rc; + } + if (stat & SMBLITE_BAT_OV_BIT) { + *val = POWER_SUPPLY_HEALTH_OVERVOLTAGE; + return 0; + } + + rc = regmap_read(chip->regmap, + SMBLITE_BATIF_BASE + SMBLITE_BATIF_JEITA_COMP_STATUS, + &stat); + if (rc < 0) { + dev_err(chip->dev, + "Couldn't read SMBLITE JEITA status rc=%d\n", rc); + return rc; + } + if (stat & (SMBLITE_JEITA_ADC_THOT_AFP_BIT | + SMBLITE_JEITA_ADC_THOT_BIT)) { + *val = POWER_SUPPLY_HEALTH_OVERHEAT; + return 0; + } + if (stat & (SMBLITE_JEITA_ADC_TCOLD_AFP_BIT | + SMBLITE_JEITA_ADC_TCOLD_BIT)) { + *val = POWER_SUPPLY_HEALTH_COLD; + return 0; + } + if (stat & SMBLITE_JEITA_ADC_WARM_BIT) { + *val = POWER_SUPPLY_HEALTH_WARM; + return 0; + } + if (stat & SMBLITE_JEITA_ADC_COOL_BIT) { + *val = POWER_SUPPLY_HEALTH_COOL; + return 0; + } + *val = POWER_SUPPLY_HEALTH_GOOD; + return 0; + } + rc = regmap_read(chip->regmap, chip->base + BATTERY_CHARGER_STATUS_2, &stat); if (rc < 0) { @@ -666,6 +981,8 @@ static int smb_get_property(struct power_supply *psy, case POWER_SUPPLY_PROP_CURRENT_MAX: return smb_get_current_limit(chip, &val->intval); case POWER_SUPPLY_PROP_CURRENT_NOW: + if (chip->type == SMBLITE) + return -EOPNOTSUPP; return smb_get_iio_chan(chip, chip->usb_in_i_chan, &val->intval); case POWER_SUPPLY_PROP_VOLTAGE_NOW: @@ -679,6 +996,30 @@ static int smb_get_property(struct power_supply *psy, return smb_get_prop_health(chip, &val->intval); case POWER_SUPPLY_PROP_USB_TYPE: return smb_apsd_get_charger_type(chip, &val->intval); + case POWER_SUPPLY_PROP_CHARGE_TYPE: { + int status, rc; + + if (chip->type != SMBLITE) + return -EOPNOTSUPP; + + rc = smb_get_prop_status(chip, &status); + if (rc < 0) + return rc; + + switch (status) { + case POWER_SUPPLY_STATUS_FULL: + val->intval = POWER_SUPPLY_CHARGE_TYPE_NONE; + break; + case POWER_SUPPLY_STATUS_CHARGING: + val->intval = POWER_SUPPLY_CHARGE_TYPE_STANDARD; + break; + default: + val->intval = POWER_SUPPLY_CHARGE_TYPE_UNKNOWN; + break; + } + + return 0; + } default: dev_err(chip->dev, "invalid property: %d\n", psp); return -EINVAL; @@ -711,35 +1052,94 @@ static int smb_property_is_writable(struct power_supply *psy, } } +/* + * smb_handle_batt_overvoltage - IRQ handler for battery overvoltage. + * + * SMB2: chip->base + BATTERY_CHARGER_STATUS_2 (offset 0x07), BIT(5) + * SMBLite: SMBLITE_CHGR_BASE + SMBLITE_CHARGER_VBAT_STATUS (0x1008), BIT(7) + */ static irqreturn_t smb_handle_batt_overvoltage(int irq, void *data) { struct smb_chip *chip = data; unsigned int status; + u16 reg; + u8 bit; int rc; - rc = regmap_read(chip->regmap, - chip->base + BATTERY_CHARGER_STATUS_2, &status); + if (chip->type == SMBLITE) { + reg = SMBLITE_CHGR_BASE + SMBLITE_CHARGER_VBAT_STATUS; + bit = SMBLITE_BAT_OV_BIT; + } else { + reg = chip->base + BATTERY_CHARGER_STATUS_2; + bit = CHARGER_ERROR_STATUS_BAT_OV_BIT; + } + + rc = regmap_read(chip->regmap, reg, &status); if (rc < 0) { dev_err(chip->dev, "Couldn't read charger status: %d\n", rc); return IRQ_HANDLED; } - if (status & CHARGER_ERROR_STATUS_BAT_OV_BIT) { - /* The hardware stops charging automatically */ + if (status & bit) dev_err(chip->dev, "battery overvoltage detected\n"); - } power_supply_changed(chip->chg_psy); return IRQ_HANDLED; } +/* + * smb_handle_usb_plugin - IRQ handler for USB plug/unplug events. + * + * SMBLite: if the charger shows INHIBIT_CHARGE (0x00) right after plug, + * toggle the charging enable bit (chg_base + 0x46) to kick-start charging. + * SMB2: simply schedules the status work. + */ static irqreturn_t smb_handle_usb_plugin(int irq, void *data) { struct smb_chip *chip = data; + int usb_online = 0; + int rc; power_supply_changed(chip->chg_psy); + smb_get_prop_usb_online(chip, &usb_online); + if (!usb_online) + goto schedule_work; + + if (chip->type == SMBLITE) { + unsigned int chgr_status; + u16 chgr_reg = SMBLITE_CHGR_BASE + SMBLITE_BATTERY_CHARGER_STATUS_1; + u16 chgen_reg = SMBLITE_CHGR_BASE + SMBLITE_CHARGING_ENABLE_CMD; + + rc = regmap_read(chip->regmap, chgr_reg, &chgr_status); + if (rc < 0) { + dev_err(chip->dev, + "Couldn't read charger status: %d\n", rc); + goto schedule_work; + } + + if ((chgr_status & BATTERY_CHARGER_STATUS_MASK) == + SMBLITE_INHIBIT_CHARGE) { + rc = regmap_update_bits(chip->regmap, chgen_reg, + SMBLITE_CHARGING_ENABLE_CMD_BIT, 0); + if (rc < 0) { + dev_err(chip->dev, + "Couldn't disable charging: %d\n", rc); + goto schedule_work; + } + + usleep_range(10000, 10100); + rc = regmap_update_bits(chip->regmap, chgen_reg, + SMBLITE_CHARGING_ENABLE_CMD_BIT, + SMBLITE_CHARGING_ENABLE_CMD_BIT); + if (rc < 0) + dev_err(chip->dev, + "Couldn't re-enable charging: %d\n", rc); + } + } + +schedule_work: schedule_delayed_work(&chip->status_change_work, msecs_to_jiffies(1500)); @@ -755,21 +1155,176 @@ static irqreturn_t smb_handle_usb_icl_change(int irq, void *data) return IRQ_HANDLED; } +/* + * smb_handle_wdog_bark - pet the watchdog after a bark interrupt. + * + * SMB2: MISC base 0x600 + 0x43 = 0x643 + * SMBLite: MISC base 0x1600 + 0x5A = 0x165A (per smblite-reg.h) + * + * NOTE: downstream qpnp-smblite enables the watchdog (WD_CFG bits + * WDOG_TIMER_EN_ON_PLUGIN + BARK_WDOG_INT_EN + BITE_WDOG_DISABLE_CHARGING). + * The wdog-bark IRQ is registered here to pet it; the watchdog config itself + * is written in smblite_init_seq[]. + */ static irqreturn_t smb_handle_wdog_bark(int irq, void *data) { struct smb_chip *chip = data; + u16 reg; int rc; power_supply_changed(chip->chg_psy); - rc = regmap_write(chip->regmap, chip->base + BARK_BITE_WDOG_PET, - BARK_BITE_WDOG_PET_BIT); + if (chip->type == SMBLITE) + reg = SMBLITE_MISC_BASE + SMBLITE_BARK_BITE_WDOG_PET; + else + reg = chip->base + BARK_BITE_WDOG_PET; + + rc = regmap_write(chip->regmap, reg, BARK_BITE_WDOG_PET_BIT); if (rc < 0) dev_err(chip->dev, "Couldn't pet the dog rc=%d\n", rc); return IRQ_HANDLED; } +/* ================================================================ + * SMBLite-specific initialisation helpers + * ================================================================ + */ +static int smblite_write_jeita_16(struct smb_chip *chip, u16 addr, u16 val) +{ + u8 buf[2]; + + buf[0] = (val >> 8) & 0xff; + buf[1] = val & 0xff; + return regmap_bulk_write(chip->regmap, addr, buf, 2); +} + +static int smblite_program_jeita_thresholds(struct smb_chip *chip) +{ + u32 soft[2], hard[2]; + struct device_node *np = chip->dev->of_node; + bool soft_present, hard_present; + int rc; + + if (chip->type != SMBLITE || !np) + return 0; + + soft_present = of_find_property(np, "qcom,jeita-soft-thresholds", NULL); + hard_present = of_find_property(np, "qcom,jeita-hard-thresholds", NULL); + if (!soft_present && !hard_present) + return 0; + if (!soft_present || !hard_present) + return -EINVAL; + + rc = of_property_read_u32_array(np, + "qcom,jeita-soft-thresholds", + soft, ARRAY_SIZE(soft)); + if (rc) + return rc; + + rc = of_property_read_u32_array(np, + "qcom,jeita-hard-thresholds", + hard, ARRAY_SIZE(hard)); + if (rc) + return rc; + + if (soft[0] > 0xffff || soft[1] > 0xffff || + hard[0] > 0xffff || hard[1] > 0xffff) + return -EINVAL; + + rc = smblite_write_jeita_16(chip, + SMBLITE_BATIF_BASE + SMBLITE_JEITA_COOL_THRESHOLD_CFG, soft[0]); + if (rc < 0) + return rc; + + rc = smblite_write_jeita_16(chip, + SMBLITE_BATIF_BASE + SMBLITE_JEITA_WARM_THRESHOLD_CFG, soft[1]); + if (rc < 0) + return rc; + + rc = smblite_write_jeita_16(chip, + SMBLITE_BATIF_BASE + SMBLITE_JEITA_COLD_THRESHOLD_CFG, hard[0]); + if (rc < 0) + return rc; + + rc = smblite_write_jeita_16(chip, + SMBLITE_BATIF_BASE + SMBLITE_JEITA_HOT_THRESHOLD_CFG, hard[1]); + if (rc < 0) + return rc; + + dev_dbg(chip->dev, + "JEITA: COOL=0x%04x WARM=0x%04x COLD=0x%04x HOT=0x%04x\n", + soft[0] & 0xffff, soft[1] & 0xffff, + hard[0] & 0xffff, hard[1] & 0xffff); + + return 0; +} + +/* + * smblite_config_fast_charge_safety_timer - configure the fast-charge + * safety timer. + * + * Matches downstream smblite_init_hw()'s masked write on + * CHGR_FAST_CHARGE_SAFETY_TIMER_CFG_REG (chg_base + 0x90): + * mask = FAST_CHARGE_SAFETY_TIMER_EN_BIT | FAST_CHARGE_SAFETY_TIMER_MASK (0x0B) + * val = FAST_CHARGE_SAFETY_TIMER_EN_BIT | FAST_CHARGE_SAFETY_TIMER_768_MIN (0x0A) + * + * This register has no relation to JEITA. The downstream qpnp-smblite + * driver never programs JEITA thresholds for SMBLite at all, so there + * is no downstream reference for an "enable JEITA" bit or register -- + * smblite_program_jeita_thresholds() only writes the threshold + * comparator registers themselves. + */ +static int smblite_config_fast_charge_safety_timer(struct smb_chip *chip) +{ + if (chip->type != SMBLITE) + return 0; + + return regmap_update_bits(chip->regmap, + SMBLITE_CHGR_BASE + 0x90, + 0x0B, 0x0A); +} + +static int smblite_write_init_reg(struct smb_chip *chip, u16 addr, u8 val, + const char *name) +{ + int rc; + + rc = regmap_write(chip->regmap, addr, val); + if (rc < 0) + return dev_err_probe(chip->dev, rc, + "Couldn't configure %s\n", name); + + return 0; +} + +static int smblite_detect_ldo_mode(struct smb_chip *chip) +{ + unsigned int val; + int rc; + + if (chip->type != SMBLITE) + return 0; + + rc = regmap_read(chip->regmap, + SMBLITE_DCDC_BASE + SMBLITE_DCDC_LDO_CFG, &val); + if (rc < 0) + return rc; + + chip->ldo_mode = !!(val & SMBLITE_LDO_MODE_BIT); + dev_info(chip->dev, "Operating in %s mode\n", + chip->ldo_mode ? "LDO" : "switching"); + return 0; +} + +static int smblite_init_hw_specific(struct smb_chip *chip) +{ + if (chip->type != SMBLITE) + return 0; + + return smblite_detect_ldo_mode(chip); +} + static const struct power_supply_desc smb_psy_desc = { .name = "pmi8998_charger", .type = POWER_SUPPLY_TYPE_USB, @@ -890,37 +1445,216 @@ static const struct smb_init_register smb_init_seq[] = { .val = 1000000 / CURRENT_SCALE_FACTOR }, }; +/* ================================================================ + * SMBLite init sequence (PM4125) + * All addresses are absolute (peripheral_base + offset). + * + * Modelled strictly after downstream smblite_init_hw() in qpnp-smblite.c. + * Key differences from SMB2: + * - CHARGING_ENABLE_CMD is at chg_base + 0x46 (not 0x42) + * - Watchdog IS enabled (downstream enables it with specific timeout bits) + * - No pre-charge current register at 0x60 (that is CHGR_TERM_CFG_REG) + * - AICL enable bit is BIT(7) in usbin_base + 0x60 + * - Suspend-on-collapse is in usbin_base + 0x54 BIT(7) + * ================================================================ + */ +static const struct smb_init_register smblite_init_seq[] = { + /* AICL: periodic rerun every 12 s (misc_base + 0x54) */ + { .addr = SMBLITE_MISC_BASE + SMBLITE_MISC_AICL_RERUN_CFG, + .mask = SMBLITE_USBIN_AICL_PERIODIC_RERUN_EN_BIT | + SMBLITE_USBIN_AICL_RERUN_TIME_MASK, + .val = SMBLITE_USBIN_AICL_PERIODIC_RERUN_EN_BIT | + SMBLITE_AICL_RERUN_TIME_12S_VAL }, + /* Enable AICL (usbin_base + 0x60) */ + { .addr = SMBLITE_USBIN_BASE + SMBLITE_USBIN_AICL_OPTIONS_CFG, + .mask = SMBLITE_USBIN_AICL_EN_BIT | SMBLITE_USBIN_AICL_START_AT_MAX, + .val = SMBLITE_USBIN_AICL_EN_BIT }, + /* Suspend on collapse (usbin_base + 0x54 BIT(7)) */ + { .addr = SMBLITE_USBIN_BASE + SMBLITE_USBIN_INPUT_SUSPEND, + .mask = SMBLITE_SUSPEND_ON_COLLAPSE_USBIN_BIT, + .val = SMBLITE_SUSPEND_ON_COLLAPSE_USBIN_BIT }, + /* + * Watchdog config (misc_base + 0x59): + * Bark timeout = 16 s (ilog2(16/16)=0 << shift), bite timeout = 8 s. + * Matches downstream DEFAULT_WD_BARK_TIME=16. + */ + { .addr = SMBLITE_MISC_BASE + SMBLITE_SNARL_BARK_BITE_WD_CFG, + .mask = SMBLITE_BARK_WDOG_TIMEOUT_MASK | SMBLITE_BITE_WDOG_TIMEOUT_MASK, /* 0xF0 */ + .val = (SMBLITE_BITE_WDOG_TIMEOUT_8S << SMBLITE_BITE_WDOG_TIMEOUT_SHIFT) }, /* 0xC0 */ + /* + * Enable watchdog: WDOG_TIMER_EN_ON_PLUGIN + BARK_WDOG_INT_EN + * + BITE_WDOG_DISABLE_CHARGING (misc_base + 0x58) + * Matches downstream WD_CFG_REG enable sequence. + */ + { .addr = SMBLITE_MISC_BASE + SMBLITE_WD_CFG, + .mask = SMBLITE_WDOG_TIMER_EN_BIT | SMBLITE_WDOG_TIMER_EN_ON_PLUGIN_BIT | + SMBLITE_BARK_WDOG_INT_EN_BIT | + SMBLITE_BITE_WDOG_DISABLE_CHARGING_CFG_BIT, + .val = SMBLITE_WDOG_TIMER_EN_ON_PLUGIN_BIT | + SMBLITE_BARK_WDOG_INT_EN_BIT | + SMBLITE_BITE_WDOG_DISABLE_CHARGING_CFG_BIT }, + /* SDP: 500 mA USB 2.0 (usbin_base + 0x50) */ + { .addr = SMBLITE_USBIN_BASE + SMBLITE_USBIN_ICL_OPTIONS, + .mask = SMBLITE_USB51_MODE_BIT | SMBLITE_USBIN_MODE_CHG_BIT, + .val = SMBLITE_USB51_MODE_BIT }, + /* Disable HVDCP (usbin_base + 0x48) */ + { .addr = SMBLITE_USBIN_BASE + SMBLITE_USBIN_QC23_EN, + .mask = SMBLITE_HVDCP_EN_BIT, + .val = 0 }, + /* Fast-charge current 1.5 A (chg_base + 0x54) */ + { .addr = SMBLITE_CHGR_BASE + SMBLITE_CHGR_FAST_CHARGE_CURRENT_CFG, + .mask = FAST_CHARGE_CURRENT_SETTING_MASK, + .val = 1500000 / SMBLITE_FCC_STEP_UA }, +}; + static int smb_init_hw(struct smb_chip *chip) { - int rc, i; + const struct smb_init_register *init_seq; + int seq_len, rc, i; - for (i = 0; i < ARRAY_SIZE(smb_init_seq); i++) { - dev_dbg(chip->dev, "%d: Writing 0x%02x to 0x%02x\n", i, - smb_init_seq[i].val, smb_init_seq[i].addr); - rc = regmap_update_bits(chip->regmap, - chip->base + smb_init_seq[i].addr, - smb_init_seq[i].mask, - smb_init_seq[i].val); + rc = smblite_init_hw_specific(chip); + if (rc < 0) + return rc; + + if (chip->type == SMBLITE) { + init_seq = smblite_init_seq; + seq_len = ARRAY_SIZE(smblite_init_seq); + } else { + init_seq = smb_init_seq; + seq_len = ARRAY_SIZE(smb_init_seq); + } + + for (i = 0; i < seq_len; i++) { + /* + * SMB2: addresses in smb_init_seq are relative offsets; + * prepend chip->base (read from DT "reg"). + * SMBLite: addresses in smblite_init_seq are already absolute. + */ + u32 addr = (chip->type == SMB2) ? + (chip->base + init_seq[i].addr) : + init_seq[i].addr; + + dev_dbg(chip->dev, "%d: Writing 0x%02x to 0x%04x\n", i, + init_seq[i].val, addr); + + rc = regmap_update_bits(chip->regmap, addr, + init_seq[i].mask, + init_seq[i].val); if (rc < 0) return dev_err_probe(chip->dev, rc, "%s: init command %d failed\n", __func__, i); } + /* + * SMBLite vendor bring-up register writes. + * These are undocumented initialisation values derived from the + * downstream qpnp-smblite driver. They are SMBLITE-only. + * + * Float voltage is NOT written here: smb_probe() programs it from + * the battery-info (or leaves the hardware default untouched if no + * battery info is available), so an unconditional write here would + * clobber that "preserve hardware setting" fallback. + * + * Charging is NOT enabled here either: smb_probe() enables it only + * after the float voltage step above has run, matching the + * downstream qpnp-smblite ordering (fv_votable before clearing + * chg_disable_votable). + * + * IBAT_STATUS/VFLT_STATUS (chg_base + 0x0A/0x0B) are read-only + * hardware status registers downstream -- there is nothing to + * configure there, so they are not written. + */ + if (chip->type == SMBLITE) { + rc = smblite_config_fast_charge_safety_timer(chip); + if (rc < 0) + return dev_err_probe(chip->dev, rc, + "Couldn't config fast charge safety timer\n"); + + rc = smblite_write_init_reg(chip, + SMBLITE_BATIF_BASE + + SMBLITE_BATIF_BPD_CFG, 0x00, + "battery-present detection"); + if (rc < 0) + return rc; + + rc = smblite_write_init_reg(chip, + SMBLITE_CHGR_BASE + + SMBLITE_CHGR_CHG_INHIBIT_CFG, + 0x00, "charge inhibit"); + if (rc < 0) + return rc; + + /* SOC-based recharge, matching SMBLITE_CHGR_RECHG_SOC_THD_CFG below */ + rc = regmap_update_bits(chip->regmap, + SMBLITE_CHGR_BASE + SMBLITE_CHGR_RECHG_CFG, + SMBLITE_RECHG_MASK, SMBLITE_RECHG_MASK); + if (rc < 0) + return dev_err_probe(chip->dev, rc, + "Couldn't configure recharge strategy\n"); + + rc = smblite_write_init_reg(chip, + SMBLITE_CHGR_BASE + + SMBLITE_CHGR_RECHG_SOC_THD_CFG, + 0xFA, "SOC recharge threshold"); + if (rc < 0) + return rc; + + rc = smblite_write_init_reg(chip, + SMBLITE_CHGR_BASE + + SMBLITE_CHGR_CHG_TERM_CFG, + 0x80, "charge termination"); + if (rc < 0) + return rc; + + rc = smblite_write_init_reg(chip, + SMBLITE_USBIN_BASE + + SMBLITE_USB_APSD_EN, 0x10, + "APSD enable"); + if (rc < 0) + return rc; + + rc = smblite_write_init_reg(chip, + SMBLITE_USBIN_BASE + + SMBLITE_USB_APSD_CFG, 0x00, + "APSD configuration"); + if (rc < 0) + return rc; + } + return 0; } -static void smb_restore_charge_enable(void *data) +static void smb_restore_charge_state(void *data) { struct smb_chip *chip = data; + u16 reg; + u8 charge_enable; int rc; - rc = regmap_update_bits(chip->regmap, - chip->base + CHARGING_ENABLE_CMD, + if (chip->type == SMBLITE) + reg = SMBLITE_CHGR_BASE + SMBLITE_CHARGING_ENABLE_CMD; + else + reg = chip->base + CHARGING_ENABLE_CMD; + + /* Keep SMBLite charging disabled if probe fails partway through setup. */ + charge_enable = chip->type == SMBLITE ? 0 : chip->initial_charge_enable; + rc = regmap_update_bits(chip->regmap, reg, CHARGING_ENABLE_CMD_BIT, - chip->initial_charge_enable); + charge_enable); + if (rc < 0) + dev_err(chip->dev, "Couldn't set probe-failure charging state: %d\n", + rc); +} + +static void smb_disable_wdog_irq_wake(void *data) +{ + struct smb_chip *chip = data; + int rc; + + rc = disable_irq_wake(chip->wdog_irq); if (rc < 0) - dev_err(chip->dev, "Couldn't restore charging state: %d\n", rc); + dev_err(chip->dev, "Couldn't disable watchdog IRQ wake: %d\n", rc); } static int smb_init_irq(struct smb_chip *chip, int *irq, const char *name, @@ -953,6 +1687,7 @@ static int smb_probe(struct platform_device *pdev) unsigned int charge_enable; unsigned int float_voltage_sel; int float_voltage_uv; + const char *pmic_name; int rc, irq; chip = devm_kzalloc(&pdev->dev, sizeof(*chip), GFP_KERNEL); @@ -960,45 +1695,89 @@ static int smb_probe(struct platform_device *pdev) return -ENOMEM; chip->dev = &pdev->dev; - chip->name = pdev->name; + + pmic_name = device_get_match_data(&pdev->dev); + if (!pmic_name) + return -EINVAL; + chip->name = pmic_name; + + if (strcmp(pmic_name, "pm4125") == 0) { + chip->type = SMBLITE; + chip->icl_step_ua = SMBLITE_ICL_STEP_UA; + chip->icl_max_ua = SMBLITE_ICL_MAX_UA; + /* Use the charger peripheral as the base for SMBLite accesses. */ + chip->base = SMBLITE_CHGR_BASE; + } else { + /* SMB2: PMI8998, PM660 */ + chip->type = SMB2; + chip->icl_step_ua = CURRENT_SCALE_FACTOR; + chip->icl_max_ua = 4800000; + rc = device_property_read_u32(chip->dev, "reg", &chip->base); + if (rc < 0) + return dev_err_probe(chip->dev, rc, + "Couldn't read base address\n"); + } chip->regmap = dev_get_regmap(pdev->dev.parent, NULL); if (!chip->regmap) return dev_err_probe(chip->dev, -ENODEV, "failed to locate the regmap\n"); - rc = device_property_read_u32(chip->dev, "reg", &chip->base); - if (rc < 0) - return dev_err_probe(chip->dev, rc, - "Couldn't read base address\n"); - + /* + * IIO channels: + * usbin_v -- voltage, supported by both SMB2 and SMBLite + * usbin_i -- current, SMB2 only (not in downstream smblite) + */ chip->usb_in_v_chan = devm_iio_channel_get(chip->dev, "usbin_v"); if (IS_ERR(chip->usb_in_v_chan)) return dev_err_probe(chip->dev, PTR_ERR(chip->usb_in_v_chan), "Couldn't get usbin_v IIO channel\n"); - chip->usb_in_i_chan = devm_iio_channel_get(chip->dev, "usbin_i"); - if (IS_ERR(chip->usb_in_i_chan)) { - return dev_err_probe(chip->dev, PTR_ERR(chip->usb_in_i_chan), - "Couldn't get usbin_i IIO channel\n"); + if (chip->type != SMBLITE) { + chip->usb_in_i_chan = devm_iio_channel_get(chip->dev, "usbin_i"); + if (IS_ERR(chip->usb_in_i_chan)) + return dev_err_probe(chip->dev, + PTR_ERR(chip->usb_in_i_chan), + "Couldn't get usbin_i IIO channel\n"); } - rc = regmap_read(chip->regmap, chip->base + CHARGING_ENABLE_CMD, - &charge_enable); + if (chip->type == SMBLITE) + rc = regmap_read(chip->regmap, + SMBLITE_CHGR_BASE + SMBLITE_CHARGING_ENABLE_CMD, + &charge_enable); + else + rc = regmap_read(chip->regmap, chip->base + CHARGING_ENABLE_CMD, + &charge_enable); if (rc < 0) return dev_err_probe(chip->dev, rc, "Couldn't read charging state\n"); chip->initial_charge_enable = charge_enable & CHARGING_ENABLE_CMD_BIT; - rc = devm_add_action_or_reset(chip->dev, smb_restore_charge_enable, chip); + rc = devm_add_action_or_reset(chip->dev, smb_restore_charge_state, chip); if (rc) return dev_err_probe(chip->dev, rc, "Couldn't register charging state rollback\n"); + if (chip->type == SMBLITE) { + rc = regmap_update_bits(chip->regmap, + SMBLITE_CHGR_BASE + SMBLITE_CHARGING_ENABLE_CMD, + SMBLITE_CHARGING_ENABLE_CMD_BIT, 0); + if (rc < 0) + return dev_err_probe(chip->dev, rc, + "Couldn't disable charging for setup\n"); + } + rc = smb_init_hw(chip); if (rc < 0) return rc; + if (chip->type == SMBLITE) { + rc = smblite_program_jeita_thresholds(chip); + if (rc < 0) + return dev_err_probe(chip->dev, rc, + "Couldn't program JEITA thresholds\n"); + } + supply_config.drv_data = chip; supply_config.fwnode = dev_fwnode(&pdev->dev); @@ -1006,6 +1785,12 @@ static int smb_probe(struct platform_device *pdev) if (!desc) return -ENOMEM; memcpy(desc, &smb_psy_desc, sizeof(smb_psy_desc)); + + if (chip->type == SMBLITE) { + desc->properties = smblite_properties; + desc->num_properties = ARRAY_SIZE(smblite_properties); + } + desc->name = devm_kasprintf(chip->dev, GFP_KERNEL, "%s-charger", (const char *)device_get_match_data(chip->dev)); @@ -1019,9 +1804,18 @@ static int smb_probe(struct platform_device *pdev) "failed to register power supply\n"); rc = power_supply_get_battery_info(chip->chg_psy, &chip->batt_info); - if (rc) + if (rc && (chip->type == SMB2 || rc != -ENOENT)) return dev_err_probe(chip->dev, rc, "Failed to get battery info\n"); + if (!chip->batt_info) { + if (chip->type == SMB2) + return dev_err_probe(chip->dev, -ENODATA, + "Failed to get battery info\n"); + + dev_warn(chip->dev, + "No battery info, skipping float voltage configuration\n"); + chip->batt_info = NULL; + } rc = devm_delayed_work_autocancel(chip->dev, &chip->status_change_work, smb_status_change_work); @@ -1029,6 +1823,9 @@ static int smb_probe(struct platform_device *pdev) return dev_err_probe(chip->dev, rc, "Failed to init status change work\n"); + if (!chip->batt_info) + goto init_irqs; + if (power_supply_battery_info_has_prop(chip->batt_info, POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX)) { float_voltage_uv = @@ -1037,27 +1834,67 @@ static int smb_probe(struct platform_device *pdev) POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN)) { float_voltage_uv = chip->batt_info->voltage_max_design_uv; } else { - dev_warn(chip->dev, "No battery float voltage; preserving hardware setting\n"); - goto skip_float_voltage; + dev_warn(chip->dev, + "No battery float voltage; preserving hardware setting\n"); + goto init_irqs; + } + + if (chip->type == SMBLITE) { + if (float_voltage_uv < SMBLITE_FV_MIN_UV || + float_voltage_uv > SMBLITE_FV_MAX_UV) + return dev_err_probe(chip->dev, -EINVAL, + "float voltage %d uV outside %d-%d uV\n", + float_voltage_uv, SMBLITE_FV_MIN_UV, + SMBLITE_FV_MAX_UV); + + float_voltage_sel = (float_voltage_uv - SMBLITE_FV_MIN_UV) / + SMBLITE_FV_STEP_UV; + + rc = regmap_write(chip->regmap, + SMBLITE_CHGR_BASE + + SMBLITE_CHGR_FLOAT_VOLTAGE_CFG, + float_voltage_sel); + } else { + if (float_voltage_uv < SMB2_FLOAT_VOLTAGE_MIN_UV || + float_voltage_uv > SMB2_FLOAT_VOLTAGE_MAX_UV) + return dev_err_probe(chip->dev, -EINVAL, + "float voltage %d uV outside %d-%d uV\n", + float_voltage_uv, + SMB2_FLOAT_VOLTAGE_MIN_UV, + SMB2_FLOAT_VOLTAGE_MAX_UV); + + float_voltage_sel = + (float_voltage_uv - SMB2_FLOAT_VOLTAGE_MIN_UV) / + SMB2_FLOAT_VOLTAGE_STEP_UV; + + rc = regmap_update_bits(chip->regmap, + chip->base + FLOAT_VOLTAGE_CFG, + FLOAT_VOLTAGE_SETTING_MASK, + float_voltage_sel); } - if (float_voltage_uv < SMB2_FLOAT_VOLTAGE_MIN_UV || - float_voltage_uv > SMB2_FLOAT_VOLTAGE_MAX_UV) - return dev_err_probe(chip->dev, -EINVAL, - "float voltage %d uV outside %d-%d uV\n", - float_voltage_uv, - SMB2_FLOAT_VOLTAGE_MIN_UV, - SMB2_FLOAT_VOLTAGE_MAX_UV); - - float_voltage_sel = - (float_voltage_uv - SMB2_FLOAT_VOLTAGE_MIN_UV) / - SMB2_FLOAT_VOLTAGE_STEP_UV; - rc = regmap_update_bits(chip->regmap, chip->base + FLOAT_VOLTAGE_CFG, - FLOAT_VOLTAGE_SETTING_MASK, float_voltage_sel); if (rc < 0) return dev_err_probe(chip->dev, rc, "Couldn't set vbat max\n"); -skip_float_voltage: +init_irqs: + + /* + * SMBLite: enable charging only after the float voltage has been + * configured (or deliberately left at its hardware default, above), + * matching the downstream qpnp-smblite ordering of applying fv + * before clearing chg_disable_votable. SMB2 keeps its pre-existing + * behaviour of enabling charging as part of smb_init_seq. + */ + if (chip->type == SMBLITE) { + rc = regmap_update_bits(chip->regmap, + SMBLITE_CHGR_BASE + SMBLITE_CHARGING_ENABLE_CMD, + SMBLITE_CHARGING_ENABLE_CMD_BIT, + SMBLITE_CHARGING_ENABLE_CMD_BIT); + if (rc < 0) + return dev_err_probe(chip->dev, rc, + "Couldn't enable charging\n"); + } + rc = smb_init_irq(chip, &irq, "bat-ov", smb_handle_batt_overvoltage); if (rc < 0) return rc; @@ -1071,7 +1908,8 @@ static int smb_probe(struct platform_device *pdev) smb_handle_usb_icl_change); if (rc < 0) return rc; - rc = smb_init_irq(chip, &irq, "wdog-bark", smb_handle_wdog_bark); + rc = smb_init_irq(chip, &chip->wdog_irq, "wdog-bark", + smb_handle_wdog_bark); if (rc < 0) return rc; @@ -1081,7 +1919,20 @@ static int smb_probe(struct platform_device *pdev) if (rc < 0) return dev_err_probe(chip->dev, rc, "Couldn't set wake irq\n"); - devm_remove_action(chip->dev, smb_restore_charge_enable, chip); + if (chip->type == SMBLITE) { + rc = enable_irq_wake(chip->wdog_irq); + if (rc < 0) + return dev_err_probe(chip->dev, rc, + "Couldn't enable watchdog IRQ wake\n"); + + rc = devm_add_action_or_reset(chip->dev, + smb_disable_wdog_irq_wake, chip); + if (rc) + return dev_err_probe(chip->dev, rc, + "Couldn't register watchdog IRQ wake cleanup\n"); + } + + devm_remove_action(chip->dev, smb_restore_charge_state, chip); platform_set_drvdata(pdev, chip); @@ -1094,6 +1945,7 @@ static int smb_probe(struct platform_device *pdev) static const struct of_device_id smb_match_id_table[] = { { .compatible = "qcom,pmi8998-charger", .data = "pmi8998" }, { .compatible = "qcom,pm660-charger", .data = "pm660" }, + { .compatible = "qcom,pm4125-charger", .data = "pm4125" }, { /* sentinal */ } }; MODULE_DEVICE_TABLE(of, smb_match_id_table); @@ -1109,5 +1961,5 @@ static struct platform_driver qcom_spmi_smb = { module_platform_driver(qcom_spmi_smb); MODULE_AUTHOR("Casey Connolly "); -MODULE_DESCRIPTION("Qualcomm SMB2 Charger Driver"); +MODULE_DESCRIPTION("Qualcomm SMB2/SMBLite charger driver"); MODULE_LICENSE("GPL"); From 6be189ea01cdb49af4092c1728a1c3fcd1d3deee Mon Sep 17 00:00:00 2001 From: Raj Aryan Date: Thu, 26 Nov 2026 16:35:47 +0530 Subject: [PATCH 3/3] FROMLIST: arm64: dts: qcom: Add PM4125 charger node and shikra HS phy bridge PM4125 integrates an SMBLite switch-mode battery charger. Add its charger node to the PM4125 device tree and enable it on the Shikra CQM SOM. Shikra's primary HS phy endpoint was wired directly to the dwc3 controller, leaving no place to switch the USB role at runtime. Route it through an intermediate connector/controller port pair and mark the phy usb-role-switch capable, needed by the upcoming QUSB2 charger-detection work. Link: https://lore.kernel.org/all/20261126-smblite-dt-v1-1-0f4f4401db3b@oss.qualcomm.com/ Signed-off-by: Raj Aryan Signed-off-by: Krishna Kurapati Signed-off-by: Rakesh Kota --- arch/arm64/boot/dts/qcom/pm4125.dtsi | 20 +++++++++++++++++++ arch/arm64/boot/dts/qcom/shikra-cqm-evk.dts | 12 ++++++----- arch/arm64/boot/dts/qcom/shikra-cqm-som.dtsi | 4 ++++ arch/arm64/boot/dts/qcom/shikra-cqs-evk.dts | 10 +++++----- arch/arm64/boot/dts/qcom/shikra.dtsi | 21 ++++++++++++++++++++ 5 files changed, 57 insertions(+), 10 deletions(-) diff --git a/arch/arm64/boot/dts/qcom/pm4125.dtsi b/arch/arm64/boot/dts/qcom/pm4125.dtsi index ca900948c0b3e..c7fad5e75f5f4 100644 --- a/arch/arm64/boot/dts/qcom/pm4125.dtsi +++ b/arch/arm64/boot/dts/qcom/pm4125.dtsi @@ -36,6 +36,26 @@ }; }; + pm4125_charger: charger@1000 { + compatible = "qcom,pm4125-charger"; + reg = <0x1000>; + + interrupts = <0x0 0x13 0x0 IRQ_TYPE_EDGE_BOTH>, + <0x0 0x12 0x3 IRQ_TYPE_EDGE_BOTH>, + <0x0 0x16 0x1 IRQ_TYPE_EDGE_RISING>, + <0x0 0x13 0x6 IRQ_TYPE_EDGE_RISING>; + interrupt-names = "usb-plugin", + "bat-ov", + "wdog-bark", + "usbin-icl-change"; + io-channels = <&pm4125_adc ADC5_USB_IN_V_16>; + io-channel-names = "usbin_v"; + qcom,jeita-soft-thresholds = <0x5314 0x25e3>; /* COOL, WARM */ + qcom,jeita-hard-thresholds = <0x58cd 0x20b8>; /* COLD, HOT */ + + status = "disabled"; + }; + pm4125_vbus: usb-vbus-regulator@1100 { compatible = "qcom,pm4125-vbus-reg"; reg = <0x1100>; diff --git a/arch/arm64/boot/dts/qcom/shikra-cqm-evk.dts b/arch/arm64/boot/dts/qcom/shikra-cqm-evk.dts index ba530ef8a962a..38ac5c8d39790 100644 --- a/arch/arm64/boot/dts/qcom/shikra-cqm-evk.dts +++ b/arch/arm64/boot/dts/qcom/shikra-cqm-evk.dts @@ -451,7 +451,7 @@ }; &pm4125_hs_in { - remote-endpoint = <&usb_1_dwc3_hs>; + remote-endpoint = <&usb_1_hsphy_connector>; }; &pm4125_ss_in { @@ -674,15 +674,13 @@ }; }; -&usb_1_dwc3_hs { - remote-endpoint = <&pm4125_hs_in>; -}; - &usb_1_hsphy { vdd-supply = <&pm4125_l12>; vdda-pll-supply = <&pm4125_l13>; vdda-phy-dpdm-supply = <&pm4125_l21>; + usb-role-switch; + status = "okay"; }; @@ -712,6 +710,10 @@ status = "okay"; }; +&usb_1_hsphy_connector { + remote-endpoint = <&pm4125_hs_in>; +}; + &usb_qmpphy_out { remote-endpoint = <&pm4125_ss_in>; }; diff --git a/arch/arm64/boot/dts/qcom/shikra-cqm-som.dtsi b/arch/arm64/boot/dts/qcom/shikra-cqm-som.dtsi index 287c5682553c3..c5b5e02741845 100644 --- a/arch/arm64/boot/dts/qcom/shikra-cqm-som.dtsi +++ b/arch/arm64/boot/dts/qcom/shikra-cqm-som.dtsi @@ -234,6 +234,10 @@ }; }; +&pm4125_charger { + status = "okay"; +}; + &pm4125_gpios { pm4125_adc_gpio5_default: pm4125-adc-gpio5-state { pins = "gpio5"; diff --git a/arch/arm64/boot/dts/qcom/shikra-cqs-evk.dts b/arch/arm64/boot/dts/qcom/shikra-cqs-evk.dts index 25aaebfbe777c..e167c1c2b30b2 100644 --- a/arch/arm64/boot/dts/qcom/shikra-cqs-evk.dts +++ b/arch/arm64/boot/dts/qcom/shikra-cqs-evk.dts @@ -459,7 +459,7 @@ }; &pm4125_hs_in { - remote-endpoint = <&usb_1_dwc3_hs>; + remote-endpoint = <&usb_1_hsphy_connector>; }; &pm4125_ss_in { @@ -713,10 +713,6 @@ }; }; -&usb_1_dwc3_hs { - remote-endpoint = <&pm4125_hs_in>; -}; - &usb_1_hsphy { vdd-supply = <&pm4125_l12>; vdda-pll-supply = <&pm4125_l13>; @@ -756,6 +752,10 @@ status = "okay"; }; +&usb_1_hsphy_connector { + remote-endpoint = <&pm4125_hs_in>; +}; + &usb_qmpphy_out { remote-endpoint = <&pm4125_ss_in>; }; diff --git a/arch/arm64/boot/dts/qcom/shikra.dtsi b/arch/arm64/boot/dts/qcom/shikra.dtsi index 04ad6670e0d1e..b1b5840a81657 100644 --- a/arch/arm64/boot/dts/qcom/shikra.dtsi +++ b/arch/arm64/boot/dts/qcom/shikra.dtsi @@ -1332,6 +1332,26 @@ #phy-cells = <0>; status = "disabled"; + + ports { + #address-cells = <1>; + #size-cells = <0>; + + port@0 { + reg = <0>; + + usb_1_hsphy_connector: endpoint { + }; + }; + + port@1 { + reg = <1>; + + usb_1_hsphy_controller: endpoint { + remote-endpoint = <&usb_1_dwc3_hs>; + }; + }; + }; }; usb_qmpphy: phy@1615000 { @@ -3134,6 +3154,7 @@ reg = <0>; usb_1_dwc3_hs: endpoint { + remote-endpoint = <&usb_1_hsphy_controller>; }; };