power: supply: bq25890: Fixes for 6.2 + further work for 6.3
Subsystems (2)
v2 cover power: supply: bq25890: Fixes for 6.2 + further work for 6.3
Hi Sebastian, Marek,
Here is v2 of my bq25890 series from yesterday, addressing the few small remarks from Marek + adding Marek's Reviewed-by to most of my patches. Note I dropped patch 1/10 from the v1 series since that has already been merged.
Here is a (slightly updated) copy of the v1 cover letter:
I have been working on getting a Lenovo Yoga Tab 3 Pro (YT3-X90F) to work with the mainline kernel. This tablet has 2 batteries with 2 bq25892 chargers both connected to a single Micro-USB connector.
Supporting the 2 charger board also requires the recent HiZ mode patches from Marek, to avoid merging order problems / conflicts I have included a copy of Marek's series here so this series obsoletes the:
[PATCH 1/2] power: supply: bq25890: Factor out chip state update [PATCH 2/2] power: supply: bq25890: Add HiZ mode support [PATCH v2 1/2] power: supply: bq25890: Factor out chip state update [PATCH v2 2/2] power: supply: bq25890: Add HiZ mode support
patches from Marek.
While working on adding support for this I also noticed some generic issues with the bq25890 driver currently in linux-power-supply/for-next and I also have some fixes to the HiZ support on top of Marek's support.
So this entire series consist of 4 parts:
1. Patches 1-2:
Generic bugfixes for the bq25890 charger in its current state in linux-power-supply/for-next.
2. Patches 3-4:
Marek's HiZ support work, thank you Marek.
3. Patches 5-6:
Some fixes / improvements from me to Marek's HiZ support.
4. Patch 7-9:
The actual support for boards with 2 chargers.
Regards,
Hans
Hans de Goede (7):
power: supply: bq25890: Ensure pump_express_work is cancelled on
remove
power: supply: bq25890: Fix usb-notifier probe and remove races
power: supply: bq25890: Fix setting of F_CONV_RATE rate when disabling
HiZ mode
power: supply: bq25890: Always take HiZ mode into account for ADC rate
power: supply: bq25890: Support boards with more then one charger IC
power: supply: bq25890: Add support for having a secondary charger IC
power: supply: bq25890: Add new linux,iinlim-percentage property
Marek Vasut (2):
power: supply: bq25890: Factor out chip state update
power: supply: bq25890: Add HiZ mode support
drivers/platform/x86/x86-android-tablets.c | 2 +- drivers/power/supply/bq25890_charger.c | 226 ++++++++++++++++----- 2 files changed, 179 insertions(+), 49 deletions(-)
Signature · 1 line
2.37.3
PATCH v2 1/9 power: supply: bq25890: Ensure pump_express_work is cancelled on remove
The pump_express_work which gets queued from an external_power_changed callback might be pending / running on remove() (or on probe failure).
Add a devm action cancelling the work, to ensure that it is cancelled.
Note the devm action is added before devm_power_supply_register(), making it run after devm unregisters the power_supply, so that the work cannot be queued anymore (this is also why a devm action is used for this).
---
Changes in v2:
- Add comment that the devm_add_action() call must be done before devm registering the power_supply, to guarantee it running after the unregister
--- drivers/power/supply/bq25890_charger.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+)
drivers/power/supply/bq25890_charger.c +15 -0
@@ -1317,6 +1317,13 @@ static int bq25890_fw_probe(struct bq25890_device *bq)return 0 ; } +static void bq25890_non_devm_cleanup (void *data) +{ +struct bq25890_device *bq = data; + +cancel_delayed_work_sync (&bq->pump_express_work); +} +static int bq25890_probe (struct i2c_client *client) {struct device *dev = &client->dev;
@@ -1372,6 +1379,14 @@ static int bq25890_probe(struct i2c_client *client)/* OTG reporting */ bq->usb_phy =devm_usb_get_phy (dev,USB_PHY_TYPE_USB2 ); +/* +* This must be before bq25890_power_supply_init(), so that it runs +* after devm unregisters the power_supply. +*/ + ret =devm_add_action_or_reset (dev, bq25890_non_devm_cleanup, bq); +if (ret) +return ret; + ret =bq25890_register_regulator (bq);if (ret)return ret;
Signature · 1 line
2.37.3
Re: [PATCH v2 1/9] power: supply: bq25890: Ensure pump_express_work is cancelled on remove
Hi,
Quoted 5 lines · Hans de Goede
The
pump_express_workwhich gets queued from anexternal_power_changedcallback might be pending / running onremove()(or on probe failure).Add a devm action cancelling the work, to ensure that it is cancelled.
Quoted 8 lines
Note the devm action is added before
devm_power_supply_register(), making it run after devm unregisters thepower_supply, so that the work cannot be queued anymore (this is also why a devm action is used for this).Fixes 48f45b094dbb ("power: supply: bq25890: Support higher charging voltages through Pump Express+ protocol")Reviewed-by Marek Vasut <[email protected]>Signed-off-by Hans de Goede <[email protected]>---
Thanks, queued.
-- Sebastian
Quoted 44 lines
Changes in v2:
- Add comment that thedevm_add_action()call must be done before devm registering thepower_supply, to guarantee it running after the unregister--- drivers/power/supply/bq25890_charger.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+)drivers/power/supply/bq25890_charger.c +15
-0@@ -1317,6 +1317,13 @@ static int bq25890_fw_probe(struct bq25890_device *bq)return 0 ; } +static void bq25890_non_devm_cleanup (void *data) +{ +struct bq25890_device *bq = data; + +cancel_delayed_work_sync (&bq->pump_express_work); +} +static int bq25890_probe (struct i2c_client *client) {struct device *dev = &client->dev;@@ -1372,6 +1379,14 @@ static int bq25890_probe(struct i2c_client *client)/* OTG reporting */ bq->usb_phy =devm_usb_get_phy (dev,USB_PHY_TYPE_USB2 ); +/* +* This must be before bq25890_power_supply_init(), so that it runs +* after devm unregisters the power_supply. +*/ + ret =devm_add_action_or_reset (dev, bq25890_non_devm_cleanup, bq); +if (ret) +return ret; + ret =bq25890_register_regulator (bq);if (ret)return ret;Signature · 1 line
2.37.3
PATCH v2 2/9 power: supply: bq25890: Fix usb-notifier probe and remove races
In reply to Hans de GoedeThere are 2 races surrounding the usb-notifier:
1. The notifier, and thus usb_work, may run before the bq->charger power_supply class device is registered. But usb_work may call power_supply_changed() which relies on the psy device being registered.
2. usb_work may be pending/running at remove() time, so it needs to be cancelled on remove after unregistering the usb-notifier.
Fix 1. by moving usb-notifier registration to after the power_supply registration.
Fix 2. by adding a cancel_work_sync() call directly after the usb-notifier unregistration.
---
Changes in v2:
- Use return dev_err_probe() for the exit path previously using goto
--- drivers/power/supply/bq25890_charger.c | 30 +++++++++++--------------- 1 file changed, 12 insertions(+), 18 deletions(-)
drivers/power/supply/bq25890_charger.c +12 -18
@@ -1391,40 +1391,34 @@ static int bq25890_probe(struct i2c_client *client)if (ret)return ret; -if (!IS_ERR_OR_NULL (bq->usb_phy)) { -INIT_WORK (&bq->usb_work, bq25890_usb_work); - bq->usb_nb.notifier_call = bq25890_usb_notifier; -usb_register_notifier (bq->usb_phy, &bq->usb_nb); - } - ret =bq25890_power_supply_init (bq); -if (ret <0 ) { -dev_err (dev,"Failed to register power supply\n" ); - goto err_unregister_usb_notifier; - } +if (ret <0 ) +return dev_err_probe (dev, ret,"registering power supply\n" ); ret =devm_request_threaded_irq (dev, client->irq,NULL , bq25890_irq_handler_thread,IRQF_TRIGGER_FALLING |IRQF_ONESHOT ,BQ25890_IRQ_PIN , bq);if (ret) - goto err_unregister_usb_notifier; - -return 0 ; +return ret; -err_unregister_usb_notifier: -if (!IS_ERR_OR_NULL (bq->usb_phy)) -usb_unregister_notifier (bq->usb_phy, &bq->usb_nb); +if (!IS_ERR_OR_NULL (bq->usb_phy)) { +INIT_WORK (&bq->usb_work, bq25890_usb_work); + bq->usb_nb.notifier_call = bq25890_usb_notifier; +usb_register_notifier (bq->usb_phy, &bq->usb_nb); + } -return ret; +return 0 ; }static void bq25890_remove (struct i2c_client *client) {struct bq25890_device *bq =i2c_get_clientdata (client); -if (!IS_ERR_OR_NULL (bq->usb_phy)) +if (!IS_ERR_OR_NULL (bq->usb_phy)) {usb_unregister_notifier (bq->usb_phy, &bq->usb_nb); +cancel_work_sync (&bq->usb_work); + }if (!bq->skip_reset) {/* reset all registers to default values */
Signature · 1 line
2.37.3
Re: [PATCH v2 2/9] power: supply: bq25890: Fix usb-notifier probe and remove races
Hi,
Quoted 15 lines · Hans de Goede
There are 2 races surrounding the usb-notifier:
1. The notifier, and thus
usb_work, may run before thebq->chargerpower_supplyclass device is registered. Butusb_workmay callpower_supply_changed()which relies on the psy device being registered.2.
usb_workmay be pending/running atremove()time, so it needs to be cancelled on remove after unregistering the usb-notifier.Fix 1. by moving usb-notifier registration to after the
power_supplyregistration.Fix 2. by adding a
cancel_work_sync()call directly after the usb-notifier unregistration.
Quoted 6 lines
Reviewed-by Marek Vasut <[email protected]>Signed-off-by Hans de Goede <[email protected]>---
Changes in v2:
- Use returndev_err_probe()for the exit path previously using goto
---
Thanks, queued.
-- Sebastian
Quoted 63 lines
drivers/power/supply/bq25890_charger.c | 30 +++++++++++--------------- 1 file changed, 12 insertions(+), 18 deletions(-)drivers/power/supply/bq25890_charger.c +12
-18@@ -1391,40 +1391,34 @@ static int bq25890_probe(struct i2c_client *client)if (ret)return ret; -if (!IS_ERR_OR_NULL (bq->usb_phy)) { -INIT_WORK (&bq->usb_work, bq25890_usb_work); - bq->usb_nb.notifier_call = bq25890_usb_notifier; -usb_register_notifier (bq->usb_phy, &bq->usb_nb); - } - ret =bq25890_power_supply_init (bq); -if (ret <0 ) { -dev_err (dev,"Failed to register power supply\n" ); - goto err_unregister_usb_notifier; - } +if (ret <0 ) +return dev_err_probe (dev, ret,"registering power supply\n" ); ret =devm_request_threaded_irq (dev, client->irq,NULL , bq25890_irq_handler_thread,IRQF_TRIGGER_FALLING |IRQF_ONESHOT ,BQ25890_IRQ_PIN , bq);if (ret) - goto err_unregister_usb_notifier; - -return 0 ; +return ret; -err_unregister_usb_notifier: -if (!IS_ERR_OR_NULL (bq->usb_phy)) -usb_unregister_notifier (bq->usb_phy, &bq->usb_nb); +if (!IS_ERR_OR_NULL (bq->usb_phy)) { +INIT_WORK (&bq->usb_work, bq25890_usb_work); + bq->usb_nb.notifier_call = bq25890_usb_notifier; +usb_register_notifier (bq->usb_phy, &bq->usb_nb); + } -return ret; +return 0 ; }static void bq25890_remove (struct i2c_client *client) {struct bq25890_device *bq =i2c_get_clientdata (client); -if (!IS_ERR_OR_NULL (bq->usb_phy)) +if (!IS_ERR_OR_NULL (bq->usb_phy)) {usb_unregister_notifier (bq->usb_phy, &bq->usb_nb); +cancel_work_sync (&bq->usb_work); + }if (!bq->skip_reset) {/* reset all registers to default values */ Signature · 1 line
2.37.3
PATCH v2 3/9 power: supply: bq25890: Factor out chip state update
In reply to Hans de GoedeFrom: Marek Vasut <[email protected]>
Pull the chip state and ADC conversion update functionality out into separate function, so it can be reused elsewhere in the driver. This is a preparatory patch, no functional change.
--- drivers/power/supply/bq25890_charger.c | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-)
drivers/power/supply/bq25890_charger.c +16 -7
@@ -454,20 +454,18 @@ static int bq25890_get_vbus_voltage(struct bq25890_device *bq)return bq25890_find_val (ret,TBL_VBUSV ); } -static int bq25890_power_supply_get_property (struct power_supply *psy, -enum power_supply_property psp, -union power_supply_propval *val) +static void bq25890_update_state (struct bq25890_device *bq, +enum power_supply_property psp, +struct bq25890_state *state) { -struct bq25890_device *bq =power_supply_get_drvdata (psy); -struct bq25890_state state;bool do_adc_conv;int ret;mutex_lock (&bq->lock);/* update state in case we lost an interrupt */ __bq25890_handle_irq (bq); - state = bq->state; - do_adc_conv = !state.online &&bq25890_is_adc_property (psp); + *state = bq->state; + do_adc_conv = !state->online &&bq25890_is_adc_property (psp);if (do_adc_conv)bq25890_field_write (bq,F_CONV_START ,1 );mutex_unlock (&bq->lock);
@@ -475,6 +473,17 @@ static int bq25890_power_supply_get_property(struct power_supply *psy,if (do_adc_conv)regmap_field_read_poll_timeout (bq->rmap_fields[F_CONV_START ], ret, !ret,25000 ,1000000 ); +} + +static int bq25890_power_supply_get_property (struct power_supply *psy, +enum power_supply_property psp, +union power_supply_propval *val) +{ +struct bq25890_device *bq =power_supply_get_drvdata (psy); +struct bq25890_state state; +int ret; + +bq25890_update_state (bq, psp, &state);switch (psp) {case POWER_SUPPLY_PROP_STATUS :
Signature · 1 line
2.37.3
PATCH v2 4/9 power: supply: bq25890: Add HiZ mode support
In reply to Hans de GoedeFrom: Marek Vasut <[email protected]>
The bq25890 is capable of disconnecting itself from the external supply, in which case the system is supplied only from the battery. This can be useful e.g. to test the pure battery operation, or draw no power from USB port.
Implement support for this mode, which can be toggled by writing 0 or non-zero to sysfs 'online' attribute, to select either offline or online mode.
The IRQ handler has to be triggered to update chip state, as switching to and from HiZ mode does not generate an interrupt automatically.
The IRQ handler reinstates the HiZ mode in case a cable is replugged by the user, the chip itself clears the HiZ mode bit when cable is plugged in by the user and the chip detects PG bad-to-good transition.
--- drivers/power/supply/bq25890_charger.c | 58 +++++++++++++++++++------- 1 file changed, 44 insertions(+), 14 deletions(-)
drivers/power/supply/bq25890_charger.c +44 -14
@@ -95,6 +95,7 @@ struct bq25890_init_data {struct bq25890_state {u8 online; +u8 hiz;u8 chrg_status;u8 chrg_fault;u8 vsys_status;
@@ -119,6 +120,7 @@ struct bq25890_device {bool skip_reset;bool read_back_init_data; +bool force_hiz;u32 pump_express_vbus_max;enum bq25890_chip_version chip_version;struct bq25890_init_data init_data;
@@ -487,7 +489,7 @@ static int bq25890_power_supply_get_property(struct power_supply *psy,switch (psp) {case POWER_SUPPLY_PROP_STATUS : -if (!state.online) +if (!state.online || state.hiz) val->intval =POWER_SUPPLY_STATUS_DISCHARGING ;else if (state.chrg_status ==STATUS_NOT_CHARGING ) val->intval =POWER_SUPPLY_STATUS_NOT_CHARGING ;
@@ -502,7 +504,8 @@ static int bq25890_power_supply_get_property(struct power_supply *psy,break ;case POWER_SUPPLY_PROP_CHARGE_TYPE : -if (!state.online || state.chrg_status ==STATUS_NOT_CHARGING || +if (!state.online || state.hiz || + state.chrg_status ==STATUS_NOT_CHARGING || state.chrg_status ==STATUS_TERMINATION_DONE ) val->intval =POWER_SUPPLY_CHARGE_TYPE_NONE ;else if (state.chrg_status ==STATUS_PRE_CHARGING )
@@ -522,7 +525,7 @@ static int bq25890_power_supply_get_property(struct power_supply *psy,break ;case POWER_SUPPLY_PROP_ONLINE : - val->intval = state.online; + val->intval = state.online && !state.hiz;break ;case POWER_SUPPLY_PROP_HEALTH :
@@ -676,7 +679,8 @@ static int bq25890_power_supply_set_property(struct power_supply *psy, const union power_supply_propval *val) {struct bq25890_device *bq =power_supply_get_drvdata (psy); -int maxval; +struct bq25890_state state; +int maxval, ret;u8 lval;switch (psp) {
@@ -691,6 +695,12 @@ static int bq25890_power_supply_set_property(struct power_supply *psy,case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT : lval =bq25890_find_idx (val->intval,TBL_IINLIM );return bq25890_field_write (bq,F_IINLIM , lval); +case POWER_SUPPLY_PROP_ONLINE : + ret =bq25890_field_write (bq,F_EN_HIZ , !val->intval); +if (!ret) + bq->force_hiz = !val->intval; +bq25890_update_state (bq, psp, &state); +return ret;default :return -EINVAL ; }
@@ -703,6 +713,7 @@ static int bq25890_power_supply_property_is_writeable(struct power_supply *psy,case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT :case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE :case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT : +case POWER_SUPPLY_PROP_ONLINE :return true;default :return false;
@@ -757,6 +768,7 @@ static int bq25890_get_chip_state(struct bq25890_device *bq, } state_fields[] = { {F_CHG_STAT, &state->chrg_status}, {F_PG_STAT, &state->online}, + {F_EN_HIZ, &state->hiz}, {F_VSYS_STAT, &state->vsys_status}, {F_BOOST_FAULT, &state->boost_fault}, {F_BAT_FAULT, &state->bat_fault},
@@ -772,10 +784,11 @@ static int bq25890_get_chip_state(struct bq25890_device *bq, *state_fields[i].data = ret; } -dev_dbg (bq->dev,"S:CHG/PG/VSYS=%d/%d/%d, F:CHG/BOOST/BAT/NTC=%d/%d/%d/%d\n" , - state->chrg_status, state->online, state->vsys_status, - state->chrg_fault, state->boost_fault, state->bat_fault, - state->ntc_fault); +dev_dbg (bq->dev,"S:CHG/PG/HIZ/VSYS=%d/%d/%d/%d, F:CHG/BOOST/BAT/NTC=%d/%d/%d/%d\n" , + state->chrg_status, state->online, + state->hiz, state->vsys_status, + state->chrg_fault, state->boost_fault, + state->bat_fault, state->ntc_fault);return 0 ; }
@@ -792,16 +805,33 @@ static irqreturn_t __bq25890_handle_irq(struct bq25890_device *bq)if (!memcmp (&bq->state, &new_state,sizeof (new_state)))return IRQ_NONE ; -if (!new_state.online && bq->state.online) {/* power removed */ +/* power removed or HiZ */ +if ((!new_state.online || new_state.hiz) && bq->state.online) {/* disable ADC */ ret =bq25890_field_write (bq,F_CONV_RATE ,0 );if (ret <0 ) goto error; - }else if (new_state.online && !bq->state.online) {/* power inserted */ -/* enable ADC, to have control of charge current/voltage */ - ret =bq25890_field_write (bq,F_CONV_RATE ,1 ); -if (ret <0 ) - goto error; + }else if (new_state.online && !bq->state.online) { +/* +* Restore HiZ bit in case it was set by user. +* The chip does not retain this bit once the +* cable is re-plugged, hence the bit must be +* reset manually here. +*/ +if (bq->force_hiz) { + ret =bq25890_field_write (bq,F_EN_HIZ , bq->force_hiz); +if (ret <0 ) + goto error; + new_state.hiz =1 ; + } + +if (!new_state.hiz) { +/* power inserted and not HiZ */ +/* enable ADC, to have control of charge current/voltage */ + ret =bq25890_field_write (bq,F_CONV_RATE ,1 ); +if (ret <0 ) + goto error; + } } bq->state = new_state;
Signature · 1 line
2.37.3
PATCH v2 5/9 power: supply: bq25890: Fix setting of F_CONV_RATE rate when disabling HiZ mode
In reply to Hans de GoedeThe recent "power: supply: bq25890: Add HiZ mode support" change leaves F_CONV_RATE rate unset when disabling HiZ mode (setting POWER_SUPPLY_PROP_ONLINE to 1) while a charger is connected.
Separate the resetting HiZ mode (when necessary because of a charger (re)plug event) into its own "if {}" block which runs first.
And fix the setting of F_CONV_RATE rate by adding helper variables for the old and new F_CONV_RATE state which check both the online and hiz bits and then compare the helper variables to see if a F_CONV_RATE update is necessary.
--- drivers/power/supply/bq25890_charger.c | 41 +++++++++++--------------- 1 file changed, 17 insertions(+), 24 deletions(-)
drivers/power/supply/bq25890_charger.c +17 -24
@@ -795,6 +795,7 @@ static int bq25890_get_chip_state(struct bq25890_device *bq,static irqreturn_t __bq25890_handle_irq (struct bq25890_device *bq) { +bool adc_conv_rate, new_adc_conv_rate;struct bq25890_state new_state;int ret;
@@ -805,33 +806,25 @@ static irqreturn_t __bq25890_handle_irq(struct bq25890_device *bq)if (!memcmp (&bq->state, &new_state,sizeof (new_state)))return IRQ_NONE ; -/* power removed or HiZ */ -if ((!new_state.online || new_state.hiz) && bq->state.online) { -/* disable ADC */ - ret =bq25890_field_write (bq,F_CONV_RATE ,0 ); +/* +* Restore HiZ bit in case it was set by user. The chip does not retain +* this bit on cable replug, hence the bit must be reset manually here. +*/ +if (new_state.online && !bq->state.online && bq->force_hiz) { + ret =bq25890_field_write (bq,F_EN_HIZ , bq->force_hiz);if (ret <0 ) goto error; - }else if (new_state.online && !bq->state.online) { -/* -* Restore HiZ bit in case it was set by user. -* The chip does not retain this bit once the -* cable is re-plugged, hence the bit must be -* reset manually here. -*/ -if (bq->force_hiz) { - ret =bq25890_field_write (bq,F_EN_HIZ , bq->force_hiz); -if (ret <0 ) - goto error; - new_state.hiz =1 ; - } + new_state.hiz =1 ; + } -if (!new_state.hiz) { -/* power inserted and not HiZ */ -/* enable ADC, to have control of charge current/voltage */ - ret =bq25890_field_write (bq,F_CONV_RATE ,1 ); -if (ret <0 ) - goto error; - } +/* Should period ADC sampling be enabled? */ + adc_conv_rate = bq->state.online && !bq->state.hiz; + new_adc_conv_rate = new_state.online && !new_state.hiz; + +if (new_adc_conv_rate != adc_conv_rate) { + ret =bq25890_field_write (bq,F_CONV_RATE , new_adc_conv_rate); +if (ret <0 ) + goto error; } bq->state = new_state;
Signature · 1 line
2.37.3
PATCH v2 6/9 power: supply: bq25890: Always take HiZ mode into account for ADC rate
In reply to Hans de GoedeThe code to check if F_CONV_RATE has been set, or if a manual ADC conversion needs to be triggered, as well as the code to set the initial F_CONV_RATE value at probe both where not taking HiZ mode into account. Add checks for this.
--- drivers/power/supply/bq25890_charger.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/power/supply/bq25890_charger.c +2 -2
@@ -467,7 +467,7 @@ static void bq25890_update_state(struct bq25890_device *bq,/* update state in case we lost an interrupt */ __bq25890_handle_irq (bq); *state = bq->state; - do_adc_conv = !state->online &&bq25890_is_adc_property (psp); + do_adc_conv = (!state->online || state->hiz) &&bq25890_is_adc_property (psp);if (do_adc_conv)bq25890_field_write (bq,F_CONV_START ,1 );mutex_unlock (&bq->lock);
@@ -956,7 +956,7 @@ static int bq25890_hw_init(struct bq25890_device *bq) }/* Configure ADC for continuous conversions when charging */ - ret =bq25890_field_write (bq,F_CONV_RATE , !!bq->state.online); + ret =bq25890_field_write (bq,F_CONV_RATE , bq->state.online && !bq->state.hiz);if (ret <0 ) {dev_dbg (bq->dev,"Config ADC failed %d\n" , ret);return ret;
Signature · 1 line
2.37.3
PATCH v2 7/9 power: supply: bq25890: Support boards with more then one charger IC
In reply to Hans de GoedeSome devices, such as the Lenovo Yoga Tab 3 Pro (YT3-X90F) have multiple batteries with a separate bq25890 charger for each battery.
This requires the bq25890_charger code to use a unique name per registered power_supply class device, rather then hardcoding "bq25890-charger" as power_supply class device name.
Add a "-%d" prefix to the name, allocated through idr in the same way as several other power_supply drivers are already doing this.
Note this also updates: drivers/platform/x86/x86-android-tablets.c which refers to the charger by power_supply-class-device-name for the purpose of setting the "supplied-from" property on the fuel-gauge to this name.
---
As the subsystem maintainer for drivers/platform/x86 I'm fine with the small x86-android-tablets.c being merged through the linux-power-supply tree.
Also note I did a full grep for "bq25890-charger" and x86-android-tablets.c is the only file in the kernel tree referring to this name.
--- drivers/platform/x86/x86-android-tablets.c | 2 +- drivers/power/supply/bq25890_charger.c | 29 +++++++++++++++++++--- 2 files changed, 26 insertions(+), 5 deletions(-)
drivers/platform/x86/x86-android-tablets.c +1 -1
@@ -187,7 +187,7 @@ struct x86_dev_info {/* Generic / shared charger / battery settings */ static const char *const tusb1211_chg_det_psy[] = {"tusb1211-charger-detect" };static const char *const bq24190_psy[] = {"bq24190-charger" }; -static const char *const bq25890_psy[] = {"bq25890-charger" }; +static const char *const bq25890_psy[] = {"bq25890-charger-0" };static const struct property_entry fg_bq24190_supply_props[] = {PROPERTY_ENTRY_STRING_ARRAY ("supplied-from" , bq24190_psy),
drivers/power/supply/bq25890_charger.c +25 -4
@@ -108,6 +108,9 @@ struct bq25890_device {struct i2c_client *client;struct device *dev;struct power_supply *charger; +struct power_supply_desc desc; +char name[28 ];/* "bq25890-charger-%d" */ +int id;struct usb_phy *usb_phy;struct notifier_block usb_nb;
@@ -129,6 +132,9 @@ struct bq25890_device {struct mutex lock;/* protect state data */ }; +static DEFINE_IDR (bq25890_id); +static DEFINE_MUTEX (bq25890_id_mutex); +static const struct regmap_range bq25890_readonly_reg_ranges[] = {regmap_reg_range (0x0b ,0x0c ),regmap_reg_range (0x0e ,0x13 ),
@@ -989,7 +995,6 @@ static char *bq25890_charger_supplied_to[] = { };static const struct power_supply_desc bq25890_power_supply_desc = { - .name ="bq25890-charger" , .type =POWER_SUPPLY_TYPE_USB , .properties = bq25890_power_supply_props, .num_properties =ARRAY_SIZE (bq25890_power_supply_props),
@@ -1003,12 +1008,21 @@ static int bq25890_power_supply_init(struct bq25890_device *bq) {struct power_supply_config psy_cfg = { .drv_data = bq, }; +/* Get ID for the device */ +mutex_lock (&bq25890_id_mutex); + bq->id =idr_alloc (&bq25890_id, bq,0 ,0 ,GFP_KERNEL ); +mutex_unlock (&bq25890_id_mutex); +if (bq->id <0 ) +return bq->id; + +snprintf (bq->name,sizeof (bq->name),"bq25890-charger-%d" , bq->id); + bq->desc = bq25890_power_supply_desc; + bq->desc.name = bq->name; + psy_cfg.supplied_to = bq25890_charger_supplied_to; psy_cfg.num_supplicants =ARRAY_SIZE (bq25890_charger_supplied_to); - bq->charger =devm_power_supply_register (bq->dev, - &bq25890_power_supply_desc, - &psy_cfg); + bq->charger =devm_power_supply_register (bq->dev, &bq->desc, &psy_cfg);return PTR_ERR_OR_ZERO (bq->charger); }
@@ -1354,6 +1368,12 @@ static void bq25890_non_devm_cleanup(void *data)struct bq25890_device *bq = data;cancel_delayed_work_sync (&bq->pump_express_work); + +if (bq->id >=0 ) { +mutex_lock (&bq25890_id_mutex); +idr_remove (&bq25890_id, bq->id); +mutex_unlock (&bq25890_id_mutex); + } }static int bq25890_probe (struct i2c_client *client)
@@ -1368,6 +1388,7 @@ static int bq25890_probe(struct i2c_client *client) bq->client = client; bq->dev = dev; + bq->id =-1 ;mutex_init (&bq->lock);INIT_DELAYED_WORK (&bq->pump_express_work, bq25890_pump_express_work);
Signature · 1 line
2.37.3
PATCH v2 8/9 power: supply: bq25890: Add support for having a secondary charger IC
In reply to Hans de GoedeSome devices, such as the Lenovo Yoga Tab 3 Pro (YT3-X90F) have multiple batteries with a separate bq25890 charger for each battery.
This requires some coordination between the chargers specifically the main charger needs to put the secondary charger in Hi-Z mode when:
1. Enabling its 5V boost (OTG) output to power an external USB device, to avoid the secondary charger IC seeing this as external Vbus and then trying to charge the secondary battery from this.
2. Talking the Pump Express protocol to increase the external Vbus voltage. Having the secondary charger drawing current when the main charger is trying to talk the Pump Express protocol results in the external Vbus voltage not being raised.
Add a new "linux,secondary-charger-name" string device-property, which can be set to the power_supply class device's name of the secondary charger when there is a secondary charger; and make the Vbus regulator and Pump Express code put the secondary charger in Hi-Z mode when necessary.
So far this new property is only used on x86/ACPI (non devicetree) devs, IOW it is not used in actual devicetree files. The devicetree-bindings maintainers have requested properties like these to not be added to the devicetree-bindings, so the new property is deliberately not added to the existing devicetree-bindings.
--- drivers/power/supply/bq25890_charger.c | 45 +++++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-)
drivers/power/supply/bq25890_charger.c +44 -1
@@ -108,6 +108,7 @@ struct bq25890_device {struct i2c_client *client;struct device *dev;struct power_supply *charger; +struct power_supply *secondary_chrg;struct power_supply_desc desc;char name[28 ];/* "bq25890-charger-%d" */ int id;
@@ -1042,10 +1043,17 @@ static void bq25890_pump_express_work(struct work_struct *data) {struct bq25890_device *bq = container_of(data, struct bq25890_device, pump_express_work.work); +union power_supply_propval value;int voltage, i, ret;dev_dbg (bq->dev,"Start to request input voltage increasing\n" ); +/* If there is a second charger put in Hi-Z mode */ +if (bq->secondary_chrg) { + value.intval =0 ; +power_supply_set_property (bq->secondary_chrg,POWER_SUPPLY_PROP_ONLINE , &value); + } +/* Enable current pulse voltage control protocol */ ret =bq25890_field_write (bq,F_PUMPX_EN ,1 );if (ret <0 )
@@ -1077,6 +1085,11 @@ static void bq25890_pump_express_work(struct work_struct *data)bq25890_field_write (bq,F_PUMPX_EN ,0 ); +if (bq->secondary_chrg) { + value.intval =1 ; +power_supply_set_property (bq->secondary_chrg,POWER_SUPPLY_PROP_ONLINE , &value); + } +dev_info (bq->dev,"Hi-voltage charging requested, input voltage is %d mV\n" , voltage);
@@ -1123,6 +1136,17 @@ static int bq25890_usb_notifier(struct notifier_block *nb, unsigned long val,static int bq25890_vbus_enable (struct regulator_dev *rdev) {struct bq25890_device *bq =rdev_get_drvdata (rdev); +union power_supply_propval val = { + .intval =0 , + }; + +/* +* When enabling 5V boost / Vbus output, we need to put the secondary +* charger in Hi-Z mode to avoid it trying to charge the secondary +* battery from the 5V boost output. +*/ +if (bq->secondary_chrg) +power_supply_set_property (bq->secondary_chrg,POWER_SUPPLY_PROP_ONLINE , &val);return bq25890_set_otg_cfg (bq,1 ); }
@@ -1130,8 +1154,19 @@ static int bq25890_vbus_enable(struct regulator_dev *rdev)static int bq25890_vbus_disable (struct regulator_dev *rdev) {struct bq25890_device *bq =rdev_get_drvdata (rdev); +union power_supply_propval val = { + .intval =1 , + }; +int ret; + + ret =bq25890_set_otg_cfg (bq,0 ); +if (ret) +return ret; -return bq25890_set_otg_cfg (bq,0 ); +if (bq->secondary_chrg) +power_supply_set_property (bq->secondary_chrg,POWER_SUPPLY_PROP_ONLINE , &val); + +return 0 ; }static int bq25890_vbus_is_enabled (struct regulator_dev *rdev)
@@ -1342,6 +1377,14 @@ static int bq25890_fw_probe(struct bq25890_device *bq) {int ret;struct bq25890_init_data *init = &bq->init_data; +const char *str; + + ret =device_property_read_string (bq->dev,"linux,secondary-charger-name" , &str); +if (ret ==0 ) { + bq->secondary_chrg =power_supply_get_by_name (str); +if (!bq->secondary_chrg) +return -EPROBE_DEFER ; + }/* Optional, left at 0 if property is not present */ device_property_read_u32(bq->dev, "linux,pump-express-vbus-max",
Signature · 1 line
2.37.3
PATCH v2 9/9 power: supply: bq25890: Add new linux,iinlim-percentage property
In reply to Hans de GoedeSome devices, such as the Lenovo Yoga Tab 3 Pro (YT3-X90F) have multiple batteries with a separate bq25890 charger for each battery.
This requires the maximum current the external power-supply can deliver to be divided over the chargers. The Android vendor kernel shipped on the YT3-X90F divides this current with a 40/60 percent split so that batteries are done charging at approx. the same time if both were fully empty at the start.
Add support for a new "linux,iinlim-percentage" percentage property which can be set to indicate that a bq25890 charger should only use that percentage of the external power-supply's maximum current.
So far this new property is only used on x86/ACPI (non devicetree) devs, IOW it is not used in actual devicetree files. The devicetree-bindings maintainers have requested properties like these to not be added to the devicetree-bindings, so the new property is deliberately not added to the existing devicetree-bindings.
---
Changes in v2:
- Check that "linux,iinlim-percentage" is not > 100 when reading it
--- drivers/power/supply/bq25890_charger.c | 31 +++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-)
drivers/power/supply/bq25890_charger.c +28 -3
@@ -126,6 +126,7 @@ struct bq25890_device {bool read_back_init_data;bool force_hiz;u32 pump_express_vbus_max; +u32 iinlim_percentage;enum bq25890_chip_version chip_version;struct bq25890_init_data init_data;struct bq25890_state state;
@@ -727,6 +728,18 @@ static int bq25890_power_supply_property_is_writeable(struct power_supply *psy, } } +/* +* If there are multiple chargers the maximum current the external power-supply +* can deliver needs to be divided over the chargers. This is done according +* to the bq->iinlim_percentage setting. +*/ +static int bq25890_charger_get_scaled_iinlim_regval (struct bq25890_device *bq, +int iinlim_ua) +{ + iinlim_ua = iinlim_ua * bq->iinlim_percentage /100 ; +return bq25890_find_idx (iinlim_ua,TBL_IINLIM ); +} +/* On the BQ25892 try to get charger-type info from our supplier */ static void bq25890_charger_external_power_changed (struct power_supply *psy) {
@@ -745,7 +758,7 @@ static void bq25890_charger_external_power_changed(struct power_supply *psy)switch (val.intval) {case POWER_SUPPLY_USB_TYPE_DCP : - input_current_limit =bq25890_find_idx (2000000 ,TBL_IINLIM ); + input_current_limit =bq25890_charger_get_scaled_iinlim_regval (bq,2000000 );if (bq->pump_express_vbus_max) { queue_delayed_work(system_power_efficient_wq, &bq->pump_express_work,
@@ -754,11 +767,11 @@ static void bq25890_charger_external_power_changed(struct power_supply *psy)break ;case POWER_SUPPLY_USB_TYPE_CDP :case POWER_SUPPLY_USB_TYPE_ACA : - input_current_limit =bq25890_find_idx (1500000 ,TBL_IINLIM ); + input_current_limit =bq25890_charger_get_scaled_iinlim_regval (bq,1500000 );break ;case POWER_SUPPLY_USB_TYPE_SDP :default : - input_current_limit =bq25890_find_idx (500000 ,TBL_IINLIM ); + input_current_limit =bq25890_charger_get_scaled_iinlim_regval (bq,500000 ); }bq25890_field_write (bq,F_IINLIM , input_current_limit);
@@ -1378,6 +1391,7 @@ static int bq25890_fw_probe(struct bq25890_device *bq)int ret;struct bq25890_init_data *init = &bq->init_data;const char *str; +u32 val; ret =device_property_read_string (bq->dev,"linux,secondary-charger-name" , &str);if (ret ==0 ) {
@@ -1390,6 +1404,17 @@ static int bq25890_fw_probe(struct bq25890_device *bq)device_property_read_u32 (bq->dev,"linux,pump-express-vbus-max" , &bq->pump_express_vbus_max); + ret =device_property_read_u32 (bq->dev,"linux,iinlim-percentage" , &val); +if (ret ==0 ) { +if (val >100 ) { +dev_err (bq->dev,"Error linux,iinlim-percentage %u > 100\n" , val); +return -EINVAL ; + } + bq->iinlim_percentage = val; + }else { + bq->iinlim_percentage =100 ; + } + bq->skip_reset =device_property_read_bool (bq->dev,"linux,skip-reset" ); bq->read_back_init_data =device_property_read_bool (bq->dev,"linux,read-back-settings" );
Signature · 1 line
2.37.3
Hi,
Quoted 39 lines · Hans de Goede
Hi Sebastian, Marek,
Here is v2 of my bq25890 series from yesterday, addressing the few small remarks from Marek + adding Marek's Reviewed-by to most of my patches. Note I dropped patch 1/10 from the v1 series since that has already been merged.
Here is a (slightly updated) copy of the v1 cover letter:
I have been working on getting a Lenovo Yoga Tab 3 Pro (YT3-X90F) to work with the mainline kernel. This tablet has 2 batteries with 2 bq25892 chargers both connected to a single Micro-USB connector.
Supporting the 2 charger board also requires the recent HiZ mode patches from Marek, to avoid merging order problems / conflicts I have included a copy of Marek's series here so this series obsoletes the:
[PATCH 1/2] power: supply: bq25890: Factor out chip state update [PATCH 2/2] power: supply: bq25890: Add HiZ mode support [PATCH v2 1/2] power: supply: bq25890: Factor out chip state update [PATCH v2 2/2] power: supply: bq25890: Add HiZ mode support
patches from Marek.
While working on adding support for this I also noticed some generic issues with the bq25890 driver currently in linux-power-supply/for-next and I also have some fixes to the HiZ support on top of Marek's support.
So this entire series consist of 4 parts:
1. Patches 1-2:
Generic bugfixes for the bq25890 charger in its current state in linux-power-supply/for-next.
2. Patches 3-4:
Marek's HiZ support work, thank you Marek.
Quoted 7 lines
3. Patches 5-6:
Some fixes / improvements from me to Marek's HiZ support.
4. Patch 7-9:
The actual support for boards with 2 chargers.
I just queued patches 3-7 to power-supply's for-next branch.
Thanks,
-- Sebastian
Quoted 28 lines
Regards,
Hans
Hans de Goede (7):
power: supply: bq25890: Ensurepump_express_workis cancelled on
remove
power: supply: bq25890: Fix usb-notifier probe and remove races
power: supply: bq25890: Fix setting ofF_CONV_RATErate when disabling
HiZ mode
power: supply: bq25890: Always take HiZ mode into account for ADC rate
power: supply: bq25890: Support boards with more then one charger IC
power: supply: bq25890: Add support for having a secondary charger IC
power: supply: bq25890: Add new linux,iinlim-percentage propertyMarek Vasut (2):
power: supply: bq25890: Factor out chip state update
power: supply: bq25890: Add HiZ mode supportdrivers/platform/x86/x86-android-tablets.c | 2 +- drivers/power/supply/bq25890_charger.c | 226 ++++++++++++++++----- 2 files changed, 179 insertions(+), 49 deletions(-)Signature · 1 line
2.37.3
Hi Sebastian,
Quoted 44 lines · Sebastian Reichel
Hi,
Quoted 41 lines · Hans de Goede
Hi Sebastian, Marek,
Here is v2 of my bq25890 series from yesterday, addressing the few small remarks from Marek + adding Marek's Reviewed-by to most of my patches. Note I dropped patch 1/10 from the v1 series since that has already been merged.
Here is a (slightly updated) copy of the v1 cover letter:
I have been working on getting a Lenovo Yoga Tab 3 Pro (YT3-X90F) to work with the mainline kernel. This tablet has 2 batteries with 2 bq25892 chargers both connected to a single Micro-USB connector.
Supporting the 2 charger board also requires the recent HiZ mode patches from Marek, to avoid merging order problems / conflicts I have included a copy of Marek's series here so this series obsoletes the:
[PATCH 1/2] power: supply: bq25890: Factor out chip state update [PATCH 2/2] power: supply: bq25890: Add HiZ mode support [PATCH v2 1/2] power: supply: bq25890: Factor out chip state update [PATCH v2 2/2] power: supply: bq25890: Add HiZ mode support
patches from Marek.
While working on adding support for this I also noticed some generic issues with the bq25890 driver currently in linux-power-supply/for-next and I also have some fixes to the HiZ support on top of Marek's support.
So this entire series consist of 4 parts:
1. Patches 1-2:
Generic bugfixes for the bq25890 charger in its current state in linux-power-supply/for-next.
2. Patches 3-4:
Marek's HiZ support work, thank you Marek.
3. Patches 5-6:
Quoted 7 lines
Quoted 5 lines
Some fixes / improvements from me to Marek's HiZ support.
4. Patch 7-9:
The actual support for boards with 2 chargers.
I just queued patches 3-7 to power-supply's for-next branch.
Thank you!
Any particular reason why 8/9 and 9/9 have not been merged?
8/9 has Marek's Reviewed-by and no other remarks.
9/9 had a small remark by Marek about clamping the percentage, but that was for v1 and has been fixed (clamping added when reading the property) in v2.
Regards,
Hans
Quoted 33 lines
Thanks,
-- Sebastian
Quoted 28 lines
Regards,
Hans
Hans de Goede (7):
power: supply: bq25890: Ensurepump_express_workis cancelled on
remove
power: supply: bq25890: Fix usb-notifier probe and remove races
power: supply: bq25890: Fix setting ofF_CONV_RATErate when disabling
HiZ mode
power: supply: bq25890: Always take HiZ mode into account for ADC rate
power: supply: bq25890: Support boards with more then one charger IC
power: supply: bq25890: Add support for having a secondary charger IC
power: supply: bq25890: Add new linux,iinlim-percentage propertyMarek Vasut (2):
power: supply: bq25890: Factor out chip state update
power: supply: bq25890: Add HiZ mode supportdrivers/platform/x86/x86-android-tablets.c | 2 +- drivers/power/supply/bq25890_charger.c | 226 ++++++++++++++++----- 2 files changed, 179 insertions(+), 49 deletions(-)Signature · 1 line
2.37.3