Add support for the TI BQ25792 battery charger (regulator part)
v9 cover Add support for the TI BQ25792 battery charger (regulator part)
This adds support for the TI BQ25792 battery charger, which is similar in overall logic to the BQ25703A, but has a different register layout and slightly different lower-level programming logic.
No cross-tree dependencies here, as the MFD part is already in master.
---
Changes in v9:
- Rebased onto recent -next
- Add a fix clamping the BQ25703 OTG current limit to its hardware maximum
- Clamp the BQ25792 OTG current limit and preserve PRECHG_TMR when programming it
- Only look up the OTG enable GPIO on BQ25703A
- Link to v8: https://patch.msgid.link/[email protected]
Changes in v8:
- Rebased onto recent -next and dropped patches already applied by Sebastian (thanks!)
- Link to v7: https://patch.msgid.link/[email protected]
Changes in v7:
- Rebase onto recent -next and dropped patches already applied by Mark and Lee
- Enable the Input Current Optimizer to improve reliability with unrecognized chargers
- Explicitly program the battery cell count at init time to alleviate transient glitches with the charger going into spurious battery overvoltage state due to misdetected battery cell count
- Handle return values of all regmap writes in the init function
- Link to v6: https://lore.kernel.org/r/[email protected]
Changes in v6:
- Changed -EINVAL to -ENODEV for non-match cases in the MFD driver, to stay in line with what other drivers do in similar situations (Lee Jones)
- Link to v5: https://lore.kernel.org/r/[email protected]
Changes in v5:
- Added non-OF match data and switched to i2c_get_match_data() to support non-OF platforms (Lee Jones)
- Shifted the types in the enum to start at 1 to avoid confusion with zero-initialized data and non-match cases (Lee Jones)
- Reinstated the const qualifier on the MFD cell array (Lee Jones)
- Link to v4: https://lore.kernel.org/r/[email protected]
Changes in v4:
- Avoid additional data structures and pass 'type' within the existing struct bq257xx_device instead (Lee Jones)
- Move comments for new struct fields to the patches where those fields are added (Sebastian Reichel)
- Collect tags from Sebastian Reichel (thanks!)
- Link to v3: https://lore.kernel.org/r/[email protected]
Changes in v3:
- Move MFD cell definitions back out of the probe function (Lee Jones)
- Collect tags from Mark Brown, Krzysztof Kozlowski and Chris Morgan (thanks!)
- Enable ship FET functionality at init for BQ25792
- Link to v2: https://lore.kernel.org/r/[email protected]
Changes in v2:
- Fix an error in DT schema (thanks Rob's bot)
- Ensure the broadest constraints for all variants remain in the common part of the schema, per writing-schema doc (thanks Krzysztof)
- Link to v1: https://lore.kernel.org/r/[email protected]
---
Alexey Charkov (3):
regulator: bq257xx: Clamp the BQ25703 OTG current limit to the hardware maximum
regulator: bq257xx: Drop the regulator_dev from the driver data
regulator: bq257xx: Add support for BQ25792
drivers/regulator/bq257xx-regulator.c | 112 +++++++++++++++++++++++++++++++--- 1 file changed, 105 insertions(+), 7 deletions(-)
---
base-commit: 7649ba73f9e7e1e04d0b46213e39c79ae09dd55c
change-id: 20260303-bq25792-0132ac86846d
Best regards,
Signature · 1 line
Alexey Charkov <[email protected]>
PATCH v9 1/3 regulator: bq257xx: Clamp the BQ25703 OTG current limit to the hardware maximum
The regulator core only clamps max_uA when the regulator node sets regulator-max-microamp, so a consumer can request an arbitrarily large limit. FIELD_PREP() then silently drops the bits above the 7-bit field, and a large request programs a tiny limit while the rounding check still passes on the unmasked value.
Clamp max_uA to the hardware maximum before converting it to a register value, so that oversized requests program the highest available limit.
--- drivers/regulator/bq257xx-regulator.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/regulator/bq257xx-regulator.c +1 -1
@@ -45,7 +45,7 @@ static int bq25703_vbus_set_cur_limit(struct regulator_dev *rdev,if ((min_uA >BQ25703_OTG_CUR_MAX_UA ) || (max_uA <0 ))return -EINVAL ; - reg = (max_uA /BQ25703_OTG_CUR_STEP_UA ); + reg =min (max_uA,BQ25703_OTG_CUR_MAX_UA ) /BQ25703_OTG_CUR_STEP_UA ;/* Catch rounding errors since our step is 50000uA. */ if ((reg *BQ25703_OTG_CUR_STEP_UA ) < min_uA)
Signature · 1 line
2.56.0
PATCH v9 2/3 regulator: bq257xx: Drop the regulator_dev from the driver data
In reply to Alexey CharkovThe field was not used anywhere in the driver, so just drop it. This helps further slim down the platform data structure.
--- drivers/regulator/bq257xx-regulator.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/regulator/bq257xx-regulator.c +4 -4
@@ -15,7 +15,6 @@#include <linux/regulator/of_regulator.h> struct bq257xx_reg_data { -struct regulator_dev *bq257xx_reg;struct gpio_desc *otg_en_gpio;struct regulator_desc desc; };
@@ -144,6 +143,7 @@ static int bq257xx_regulator_probe(struct platform_device *pdev)struct device *dev = &pdev->dev;struct bq257xx_reg_data *pdata;struct regulator_config cfg = {}; +struct regulator_dev *rdev;device_set_of_node_from_dev (&pdev->dev, pdev->dev.parent);
@@ -162,9 +162,9 @@ static int bq257xx_regulator_probe(struct platform_device *pdev)if (!cfg.regmap)return -ENODEV ; - pdata->bq257xx_reg =devm_regulator_register (dev, &pdata->desc, &cfg); -if (IS_ERR (pdata->bq257xx_reg)) { -return dev_err_probe (&pdev->dev,PTR_ERR (pdata->bq257xx_reg), + rdev =devm_regulator_register (dev, &pdata->desc, &cfg); +if (IS_ERR (rdev)) { +return dev_err_probe (&pdev->dev,PTR_ERR (rdev),"error registering bq257xx regulator" ); }
Signature · 1 line
2.56.0
PATCH v9 3/3 regulator: bq257xx: Add support for BQ25792
In reply to Alexey CharkovAdd support for TI BQ25792, an integrated battery charger and buck/boost regulator. This enables VBUS output from the charger's boost converter for use in USB OTG applications, supporting 2.8-22V output at up to 3.32A with 10mV and 40mA resolution.
--- drivers/regulator/bq257xx-regulator.c | 102 +++++++++++++++++++++++++++++++++- 1 file changed, 100 insertions(+), 2 deletions(-)
drivers/regulator/bq257xx-regulator.c +100 -2
@@ -31,6 +31,32 @@ static int bq25703_vbus_get_cur_limit(struct regulator_dev *rdev)return FIELD_GET (BQ25703_OTG_CUR_MASK , reg) *BQ25703_OTG_CUR_STEP_UA ; } +static int bq25792_vbus_get_cur_limit (struct regulator_dev *rdev) +{ +struct regmap *regmap =rdev_get_regmap (rdev); +int ret; +unsigned int reg; + + ret =regmap_read (regmap,BQ25792_REG0D_IOTG_REGULATION , ®); +if (ret) +return ret; +return FIELD_GET (BQ25792_REG0D_IOTG_MASK , reg) *BQ25792_OTG_CUR_STEP_UA ; +} + +static int bq25792_vbus_get_voltage_sel (struct regulator_dev *rdev) +{ +struct regmap *regmap =rdev_get_regmap (rdev); +__be16 reg; +int ret; + + ret =regmap_raw_read (regmap,BQ25792_REG0B_VOTG_REGULATION , + ®,sizeof (reg)); +if (ret) +return ret; + +return FIELD_GET (BQ25792_REG0B_VOTG_MASK ,be16_to_cpu (reg)); +} +/* * Check if the minimum current and maximum current requested are * sane values, then set the register accordingly.
@@ -54,6 +80,38 @@ static int bq25703_vbus_set_cur_limit(struct regulator_dev *rdev, FIELD_PREP(BQ25703_OTG_CUR_MASK, reg)); } +static int bq25792_vbus_set_cur_limit (struct regulator_dev *rdev, +int min_uA,int max_uA) +{ +struct regmap *regmap =rdev_get_regmap (rdev); +unsigned int reg; + +if ((min_uA >BQ25792_OTG_CUR_MAX_UA ) || + (max_uA <BQ25792_OTG_CUR_MIN_UA )) +return -EINVAL ; + + reg =min (max_uA,BQ25792_OTG_CUR_MAX_UA ) /BQ25792_OTG_CUR_STEP_UA ; + +/* Catch rounding errors since our step is 40000uA. */ +if ((reg *BQ25792_OTG_CUR_STEP_UA ) < min_uA) +return -EINVAL ; + +return regmap_update_bits (regmap,BQ25792_REG0D_IOTG_REGULATION , +BQ25792_REG0D_IOTG_MASK , +FIELD_PREP (BQ25792_REG0D_IOTG_MASK , reg)); +} + +static int bq25792_vbus_set_voltage_sel (struct regulator_dev *rdev, +unsigned int sel) +{ +struct regmap *regmap =rdev_get_regmap (rdev); +__be16 reg; + + reg =cpu_to_be16 (FIELD_PREP (BQ25792_REG0B_VOTG_MASK , sel)); +return regmap_raw_write (regmap,BQ25792_REG0B_VOTG_REGULATION , + ®,sizeof (reg)); +} +static int bq25703_vbus_enable (struct regulator_dev *rdev) {struct bq257xx_reg_data *pdata =rdev_get_drvdata (rdev);
@@ -101,6 +159,34 @@ static const struct regulator_desc bq25703_vbus_desc = { .vsel_mask = BQ25703_OTG_VOLT_MASK, }; +static const struct regulator_ops bq25792_vbus_ops = { +/* No GPIO for enabling the OTG regulator */ + .enable = regulator_enable_regmap, + .disable = regulator_disable_regmap, + .is_enabled = regulator_is_enabled_regmap, + .list_voltage = regulator_list_voltage_linear, + .get_voltage_sel = bq25792_vbus_get_voltage_sel, + .set_voltage_sel = bq25792_vbus_set_voltage_sel, + .get_current_limit = bq25792_vbus_get_cur_limit, + .set_current_limit = bq25792_vbus_set_cur_limit, +}; + +static const struct regulator_desc bq25792_vbus_desc = { + .name ="vbus" , + .of_match =of_match_ptr ("vbus" ), + .regulators_node =of_match_ptr ("regulators" ), + .type =REGULATOR_VOLTAGE , + .owner =THIS_MODULE , + .ops = &bq25792_vbus_ops, + .min_uV =BQ25792_OTG_VOLT_MIN_UV , + .uV_step =BQ25792_OTG_VOLT_STEP_UV , + .n_voltages =BQ25792_OTG_VOLT_NUM_VOLT , + .enable_mask =BQ25792_REG12_EN_OTG , + .enable_reg =BQ25792_REG12_CHARGER_CONTROL_3 , + .enable_val =BQ25792_REG12_EN_OTG , + .disable_val =0 , +}; +/* Get optional GPIO for OTG regulator enable. */ static void bq257xx_reg_dt_parse_gpio (struct platform_device *pdev) {
@@ -141,6 +227,7 @@ static void bq257xx_reg_dt_parse_gpio(struct platform_device *pdev)static int bq257xx_regulator_probe (struct platform_device *pdev) {struct device *dev = &pdev->dev; +struct bq257xx_device *bq =dev_get_drvdata (pdev->dev.parent);struct bq257xx_reg_data *pdata;struct regulator_config cfg = {};struct regulator_dev *rdev;
@@ -151,10 +238,21 @@ static int bq257xx_regulator_probe(struct platform_device *pdev)if (!pdata)return -ENOMEM ; - pdata->desc = bq25703_vbus_desc; +switch (bq->type) { +case BQ25703A : + pdata->desc = bq25703_vbus_desc; +break ; +case BQ25792 : + pdata->desc = bq25792_vbus_desc; +break ; +default : +return dev_err_probe (&pdev->dev, -EINVAL , +"Unsupported device type\n" ); + }platform_set_drvdata (pdev, pdata); -bq257xx_reg_dt_parse_gpio (pdev); +if (bq->type ==BQ25703A ) +bq257xx_reg_dt_parse_gpio (pdev); cfg.dev = &pdev->dev; cfg.driver_data = pdata;
Signature · 1 line
2.56.0
Mark Brown
maintainerQuoted 5 lines
This adds support for the TI BQ25792 battery charger, which is similar in overall logic to the BQ25703A, but has a different register layout and slightly different lower-level programming logic.
No cross-tree dependencies here, as the MFD part is already in master.
This doesn't apply against current code, please check and resend.
Signature · 1 line
Mark Brown <[email protected]>
Quoted 8 lines · Mark Brown
Quoted 6 lines
This adds support for the TI BQ25792 battery charger, which is similar in overall logic to the BQ25703A, but has a different register layout and slightly different lower-level programming logic.
No cross-tree dependencies here, as the MFD part is already in master.
This doesn't apply against current code, please check and resend.
Thanks for the heads-up, I will rebase and resend. In the meantime, I've also added some further functionality to make it safer to use while sourcing >5V over USB PD, so I'll restructure the series a little bit while at that.
Best regards,
Alexey