@@ -283,6 +283,38 @@ void dwc3_set_mode(struct dwc3 *dwc, u32 mode)
queue_work(system_freezable_wq, &dwc->drd_work);
}
+static void __dwc3_set_gadget_vbus_draw(struct work_struct *work)
+{
+ struct dwc3 *dwc = vbus_work_to_dwc(work);
+ struct power_supply *usb_psy;
+ union power_supply_propval val = {};
+ int ret;
+
+ usb_psy = power_supply_get_by_name(dwc->usb_psy_name);
+ if (!usb_psy) {
+ dev_err(dwc->dev, "couldn't get usb power supply\n");
+ return;
+ }
+
+ val.intval = 1000 * READ_ONCE(dwc->vbus_draw_ma);
+ ret = power_supply_set_property(usb_psy, POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT, &val);
+ if (ret < 0)
+ dev_err(dwc->dev, "failed to set power supply property\n");
+
+ power_supply_put(usb_psy);
+}
+
+int dwc3_set_gadget_vbus_draw(struct dwc3 *dwc, unsigned int mA)
+{
+ if (!dwc->usb_psy_name)
+ return -EOPNOTSUPP;
+
+ WRITE_ONCE(dwc->vbus_draw_ma, mA);
+ queue_work(system_highpri_wq, &dwc->vbus_draw_work);
+
+ return 0;
+}
+
u32 dwc3_core_fifo_space(struct dwc3_ep *dep, u8 type)
{
struct dwc3 *dwc = dep->dwc;
@@ -1631,8 +1663,6 @@ static void dwc3_get_properties(struct dwc3 *dwc)
u8 tx_thr_num_pkt_prd = 0;
u8 tx_max_burst_prd = 0;
u8 tx_fifo_resize_max_num;
- const char *usb_psy_name;
- int ret;
/* default to highest possible threshold */
lpm_nyet_threshold = 0xf;
@@ -1667,12 +1697,7 @@ static void dwc3_get_properties(struct dwc3 *dwc)
dwc->sys_wakeup = device_may_wakeup(dwc->sysdev);
- ret = device_property_read_string(dev, "usb-psy-name", &usb_psy_name);
- if (ret >= 0) {
- dwc->usb_psy = power_supply_get_by_name(usb_psy_name);
- if (!dwc->usb_psy)
- dev_err(dev, "couldn't get usb power supply\n");
- }
+ device_property_read_string(dev, "usb-psy-name", &dwc->usb_psy_name);
dwc->has_lpm_erratum = device_property_read_bool(dev,
"snps,has-lpm-erratum");
@@ -2130,21 +2155,22 @@ static int dwc3_probe(struct platform_device *pdev)
dwc3_get_properties(dwc);
+ if (dwc->usb_psy_name)
+ INIT_WORK(&dwc->vbus_draw_work, __dwc3_set_gadget_vbus_draw);
+
dwc3_get_software_properties(dwc);
dwc->reset = devm_reset_control_array_get_optional_shared(dev);
- if (IS_ERR(dwc->reset)) {
- ret = PTR_ERR(dwc->reset);
- goto err_put_psy;
- }
+ if (IS_ERR(dwc->reset))
+ return PTR_ERR(dwc->reset);
ret = dwc3_get_clocks(dwc);
if (ret)
- goto err_put_psy;
+ return ret;
ret = reset_control_deassert(dwc->reset);
if (ret)
- goto err_put_psy;
+ return ret;
ret = dwc3_clk_enable(dwc);
if (ret)
@@ -2245,9 +2271,6 @@ static int dwc3_probe(struct platform_device *pdev)
dwc3_clk_disable(dwc);
err_assert_reset:
reset_control_assert(dwc->reset);
-err_put_psy:
- if (dwc->usb_psy)
- power_supply_put(dwc->usb_psy);
return ret;
}
@@ -2258,6 +2281,8 @@ static void dwc3_remove(struct platform_device *pdev)
pm_runtime_get_sync(&pdev->dev);
+ if (dwc->usb_psy_name)
+ cancel_work_sync(&dwc->vbus_draw_work);
dwc3_core_exit_mode(dwc);
dwc3_debugfs_exit(dwc);
@@ -2276,9 +2301,6 @@ static void dwc3_remove(struct platform_device *pdev)
pm_runtime_set_suspended(&pdev->dev);
dwc3_free_event_buffers(dwc);
-
- if (dwc->usb_psy)
- power_supply_put(dwc->usb_psy);
}
#ifdef CONFIG_PM
@@ -993,6 +993,8 @@ struct dwc3_scratchpad_array {
/**
* struct dwc3 - representation of our controller
* @drd_work: workqueue used for role swapping
+ * @vbus_draw_work: work used for setting Vbus current through power_supply
+ * @vbus_draw_ma: Vbus current draw to be set
* @ep0_trb: trb which is used for the ctrl_req
* @bounce: address of bounce buffer
* @setup_buf: used while precessing STD USB requests
@@ -1045,7 +1047,7 @@ struct dwc3_scratchpad_array {
* @role_sw: usb_role_switch handle
* @role_switch_default_mode: default operation mode of controller while
* usb role is USB_ROLE_NONE.
- * @usb_psy: pointer to power supply interface.
+ * @usb_psy_name: name of the usb power supply interface.
* @usb2_phy: pointer to USB2 PHY
* @usb3_phy: pointer to USB3 PHY
* @usb2_generic_phy: pointer to array of USB2 PHYs
@@ -1163,6 +1165,8 @@ struct dwc3_scratchpad_array {
*/
struct dwc3 {
struct work_struct drd_work;
+ struct work_struct vbus_draw_work;
+ unsigned int vbus_draw_ma;
struct dwc3_trb *ep0_trb;
void *bounce;
u8 *setup_buf;
@@ -1223,7 +1227,7 @@ struct dwc3 {
struct usb_role_switch *role_sw;
enum usb_dr_mode role_switch_default_mode;
- struct power_supply *usb_psy;
+ const char *usb_psy_name;
u32 fladj;
u32 ref_clk_per;
@@ -1394,6 +1398,7 @@ struct dwc3 {
#define INCRX_UNDEF_LENGTH_BURST_MODE 1
#define work_to_dwc(w) (container_of((w), struct dwc3, drd_work))
+#define vbus_work_to_dwc(w) container_of((w), struct dwc3, vbus_draw_work)
/* -------------------------------------------------------------------------- */
@@ -1554,6 +1559,7 @@ struct dwc3_gadget_ep_cmd_params {
/* prototypes */
void dwc3_set_prtcap(struct dwc3 *dwc, u32 mode);
void dwc3_set_mode(struct dwc3 *dwc, u32 mode);
+int dwc3_set_gadget_vbus_draw(struct dwc3 *dwc, unsigned int mA);
u32 dwc3_core_fifo_space(struct dwc3_ep *dep, u8 type);
#define DWC3_IP_IS(_ip) \
@@ -3050,19 +3050,11 @@ static void dwc3_gadget_set_ssp_rate(struct usb_gadget *g,
static int dwc3_gadget_vbus_draw(struct usb_gadget *g, unsigned int mA)
{
struct dwc3 *dwc = gadget_to_dwc(g);
- union power_supply_propval val = {0};
- int ret;
if (dwc->usb2_phy)
return usb_phy_set_power(dwc->usb2_phy, mA);
- if (!dwc->usb_psy)
- return -EOPNOTSUPP;
-
- val.intval = 1000 * mA;
- ret = power_supply_set_property(dwc->usb_psy, POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT, &val);
-
- return ret;
+ return dwc3_set_gadget_vbus_draw(dwc, mA);
}
/**
It is possible that the usb power_supply is registered after the probe of dwc3. In this case, trying to get the usb power_supply during the probe will fail and there is no chance to try again. Also the usb power_supply might be unregistered at anytime so that the handle of it in dwc3 would become invalid. To fix this, get the handle right before calling to power_supply functions and put it afterward. Fixes: 6f0764b5adea ("usb: dwc3: add a power supply for current control") Cc: stable@vger.kernel.org Signed-off-by: Kyle Tso <kyletso@google.com> --- v1 -> v2: - move power_supply_put out of interrupt context drivers/usb/dwc3/core.c | 62 ++++++++++++++++++++++++++------------- drivers/usb/dwc3/core.h | 10 +++++-- drivers/usb/dwc3/gadget.c | 10 +------ 3 files changed, 51 insertions(+), 31 deletions(-)