Enable USB support (peripheral mode) for Xiaomi Redmi 5A (riva)
v2 cover Enable USB support (peripheral mode) for Xiaomi Redmi 5A (riva)
The purpose of this series is to enable the device to act as a USB peripheral. It enables USB-related DT nodes (controller and PHY) and modifies the charger driver (BQ256XX) to expose its VBUS state through extcon.
Driver change is required since the device has its VBUS connected to the charger IC, which means cable connection/disconnection events must be fed from the charger IC, to the controller driver to dynamically enable/disable the USB gadget functionality.
---
Changes in v2:
- Make extcon feature optional
- Link to v1: https://patch.msgid.link/[email protected]
To: Sebastian Reichel <[email protected]>
To: Bjorn Andersson <[email protected]>
To: Konrad Dybcio <[email protected]>
To: Abel Vesa <[email protected]>
To: Rob Herring <[email protected]>
To: Krzysztof Kozlowski <[email protected]>
To: Conor Dooley <[email protected]>
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
---
Reza Kurniawan (2):
power: supply: bq256xx: Expose VBUS state through extcon
arm64: dts: qcom: msm8917-xiaomi-riva: Enable USB support (peripheral mode)
arch/arm64/boot/dts/qcom/msm8917-xiaomi-riva.dts | 17 ++++- drivers/power/supply/Kconfig | 1 + drivers/power/supply/bq256xx_charger.c | 96 ++++++++++++++++++++++-- 3 files changed, 105 insertions(+), 9 deletions(-)
---
base-commit: 68142f986ff04b2b70b31db00f719bf690f64a9a
change-id: 20260912-riva-usb-ec9ef25c885c
Best regards,
Signature · 1 line
Reza Kurniawan <[email protected]>
PATCH v2 1/2 power: supply: bq256xx: Expose VBUS state through extcon
Useful for USB controllers needing to sense VBUS state changes. This is needed for devices like Xiaomi Redmi 5A (riva) that doesn't have its VBUS session comparator wired directly to the USB connector pin to automatically enable certain USB functions upon the presence of a USB power input.
--- drivers/power/supply/Kconfig | 1 + drivers/power/supply/bq256xx_charger.c | 96 +++++++++++++++++++++++++++++++--- 2 files changed, 89 insertions(+), 8 deletions(-)
drivers/power/supply/Kconfig +1 -0
@@ -850,6 +850,7 @@ config CHARGER_BQ256XX tristate "TI BQ256XX battery charger driver" depends on I2C depends on GPIOLIB || COMPILE_TEST + depends on EXTCON || !EXTCON select REGMAP_I2C help Say Y to enable support for the TI BQ256XX battery chargers. The
drivers/power/supply/bq256xx_charger.c +88 -8
147 lines not on this page · raw message
Signature · 1 line
2.55.0
[email protected]
AI reviewRe: [PATCH v2 1/2] power: supply: bq256xx: Expose VBUS state through extcon
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Pre-existing issues:
- [Critical] This problem wasn't introduced by this patch, but an uninitialized stack pointer bat_info is dereferenced in the error path if battery info is missing. --
commit 63c7018777c13e9b5077c305354b34365e65a932
Author: Reza Kurniawan <[email protected]>
power: supply: bq256xx: Expose VBUS state through extcon
Useful for USB controllers needing to sense VBUS state changes. This is needed for devices like Xiaomi Redmi 5A (riva) that doesn't have its VBUS session comparator wired directly to the USB connector pin to automatically enable certain USB functions upon the presence of a USB power input.
Quoted 4 lines
drivers/power/supply/bq256xx_charger.c +0
-0
[ ... ]
drivers/power/supply/bq256xx_charger.c static int bq256xx_probe(struct i2c_client *client) · line 1828
Quoted 13 lines
drivers/power/supply/bq256xx_charger.c +6
-0@@ -1743,6 +1817,12 @@ static int bq256xx_probe(struct i2c_client *client)if (!IS_ERR_OR_NULL (bq->usb3_phy))usb_register_notifier (bq->usb3_phy, &bq->usb_nb); + ret =bq256xx_extcon_init (bq); +if (ret) { +dev_err (dev,"Failed to register extcon device\n" ); +return ret; + } +if (client->irq) { ret = devm_request_threaded_irq(dev, client->irq, NULL, bq256xx_irq_handler_thread,
[Severity: Critical]
This isn't a bug introduced by this patch, but looking at bq256xx_hw_init() called shortly after this block in bq256xx_probe(), is it possible for the bat_info pointer to remain uninitialized and cause memory corruption?
If power_supply_get_battery_info() returns an error other than -ENOMEM (such as -ENODEV when no "monitored-battery" node is defined), bat_info remains uninitialized, but it is then dereferenced in the error handling path:
drivers/power/supply/bq256xx_charger.c:bq256xx_hw_init() {
struct power_supply_battery_info *bat_info;
...
ret = power_supply_get_battery_info(bq->charger, &bat_info);
if (ret == -ENOMEM)
return ret;
if (ret) {
dev_warn(bq->dev, "battery info missing, default values will be applied\n");
bat_info->constant_charge_current_max_ua =
bq->chip_info->bq256xx_def_ichg;
...
}Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
PATCH v2 2/2 arm64: dts: qcom: msm8917-xiaomi-riva: Enable USB support (peripheral mode)
In reply to Reza KurniawanEnable USB node and PHY. Since VBUS is wired to the charger IC, also set extcon of the USB node to inform the USB driver of cable events emitted by the charger driver.
--- arch/arm64/boot/dts/qcom/msm8917-xiaomi-riva.dts | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-)
arch/arm64/boot/dts/qcom/msm8917-xiaomi-riva.dts +16 -1
@@ -69,7 +69,7 @@ power-monitor@55 { monitored-battery = <&battery>; }; - charger@6b { + bq25601_charger: charger@6b { compatible = "ti,bq25601"; reg = <0x6b>; interrupts-extended = <&tlmm 61 IRQ_TYPE_EDGE_FALLING>;
@@ -89,3 +89,18 @@ bq25601_int_default: bq25601-int-default-state { bias-pull-up; }; }; + +&usb { + dr_mode = "peripheral"; + extcon = <&bq25601_charger>; + + status = "okay"; +}; + +&usb_hs_phy { + vdd-supply = <&pm8937_l2>; + vdda1p8-supply = <&pm8937_l7>; + vdda3p3-supply = <&pm8937_l13>; + + status = "okay"; +};
Signature · 1 line
2.55.0
Re: [PATCH v2 2/2] arm64: dts: qcom: msm8917-xiaomi-riva: Enable USB support (peripheral mode)
Quoted 6 lines · Reza Kurniawan
Enable USB node and PHY. Since VBUS is wired to the charger IC, also set extcon of the USB node to inform the USB driver of cable events emitted by the charger driver.
Signed-off-by Reza Kurniawan <[email protected]>---
Konrad
Re: [PATCH v2 2/2] arm64: dts: qcom: msm8917-xiaomi-riva: Enable USB support (peripheral mode)
In reply to Reza KurniawanQuoted 9 lines · Reza Kurniawan
Enable USB node and PHY. Since VBUS is wired to the charger IC, also set extcon of the USB node to inform the USB driver of cable events emitted by the charger driver.
Signed-off-by Reza Kurniawan <[email protected]>--- arch/arm64/boot/dts/qcom/msm8917-xiaomi-riva.dts | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-)
Signature · 2 lines
With best wishes Dmitry
Sebastian Reichel
AppliedRe: (subset) [PATCH v2 0/2] Enable USB support (peripheral mode) for Xiaomi Redmi 5A (riva)
In reply to Reza KurniawanQuoted 10 lines · Reza Kurniawan
The purpose of this series is to enable the device to act as a USB peripheral. It enables USB-related DT nodes (controller and PHY) and modifies the charger driver (BQ256XX) to expose its VBUS state through extcon.
Driver change is required since the device has its VBUS connected to the charger IC, which means cable connection/disconnection events must be fed from the charger IC, to the controller driver to dynamically enable/disable the USB gadget functionality.
[...]
Applied, thanks!
[1/2] power: supply: bq256xx: Expose VBUS state through extcon
commit: b33596a615d163372f5e7519338d860cbfc37217
Best regards,
Signature · 1 line
Sebastian Reichel <[email protected]>
Re: (subset) [PATCH v2 0/2] Enable USB support (peripheral mode) for Xiaomi Redmi 5A (riva)
In reply to Reza KurniawanQuoted 10 lines · Reza Kurniawan
The purpose of this series is to enable the device to act as a USB peripheral. It enables USB-related DT nodes (controller and PHY) and modifies the charger driver (BQ256XX) to expose its VBUS state through extcon.
Driver change is required since the device has its VBUS connected to the charger IC, which means cable connection/disconnection events must be fed from the charger IC, to the controller driver to dynamically enable/disable the USB gadget functionality.
[...]
Applied, thanks!
[2/2] arm64: dts: qcom: msm8917-xiaomi-riva: Enable USB support (peripheral mode)
commit: f6a4df4921fb88f1e729a19a48e7aede852ed6a6
Best regards,
Signature · 1 line
Bjorn Andersson <[email protected]>