Skip to content

Commit 85d75ec

Browse files
pawelzadrozniaknashif
authored andcommitted
drivers: usb: nordic: Power consumption fix with cable detached
This commit fixes the issue with excess current being drawn during sleep due to active HFCLK with external crystal. Clock-related operations were moved to cable attachmend and detachment handlers to ensure that HFCLK is not requested when it's not needed. Signed-off-by: Paweł Zadrożniak <[email protected]>
1 parent 192fe2a commit 85d75ec

File tree

1 file changed

+23
-18
lines changed

1 file changed

+23
-18
lines changed

drivers/usb/device/usb_dc_nrfx.c

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -525,6 +525,7 @@ static int hf_clock_enable(bool on, bool blocking)
525525
{
526526
int ret = -ENODEV;
527527
struct device *clock;
528+
static bool clock_requested;
528529

529530
clock = device_get_binding(CONFIG_CLOCK_CONTROL_NRF_M16SRC_DRV_NAME);
530531
if (!clock) {
@@ -533,8 +534,18 @@ static int hf_clock_enable(bool on, bool blocking)
533534
}
534535

535536
if (on) {
537+
if (clock_requested) {
538+
/* Do not request HFCLK multiple times. */
539+
return 0;
540+
}
536541
ret = clock_control_on(clock, (void *)blocking);
537542
} else {
543+
if (!clock_requested) {
544+
/* Cancel the operation if clock has not
545+
* been requested by this driver before.
546+
*/
547+
return 0;
548+
}
538549
ret = clock_control_off(clock, (void *)blocking);
539550
}
540551

@@ -544,9 +555,14 @@ static int hf_clock_enable(bool on, bool blocking)
544555
return ret;
545556
}
546557

558+
clock_requested = on;
547559
LOG_DBG("HF clock %s success (%d)", on ? "start" : "stop", ret);
548560

549-
return ret;
561+
/* NOTE: Non-blocking HF clock enable can return -EINPROGRESS
562+
* if HF clock start was already requested. Such error code
563+
* does not need to be propagated, hence returned value is 0.
564+
*/
565+
return 0;
550566
}
551567

552568
static void usbd_enable_endpoints(struct nrf_usbd_ctx *ctx)
@@ -731,6 +747,8 @@ static inline void usbd_work_process_pwr_events(struct usbd_pwr_event *pwr_evt)
731747
case USBD_ATTACHED:
732748
LOG_DBG("USB detected");
733749
nrfx_usbd_enable();
750+
(void) hf_clock_enable(true, false);
751+
734752
break;
735753

736754
case USBD_POWERED:
@@ -748,6 +766,7 @@ static inline void usbd_work_process_pwr_events(struct usbd_pwr_event *pwr_evt)
748766
LOG_DBG("USB Removed");
749767
ctx->ready = false;
750768
nrfx_usbd_disable();
769+
(void) hf_clock_enable(false, false);
751770

752771
if (ctx->status_cb) {
753772
ctx->status_cb(USB_DC_DISCONNECTED, NULL);
@@ -1248,14 +1267,6 @@ int usb_dc_attach(void)
12481267
DT_NORDIC_NRF_USBD_USBD_0_IRQ_PRIORITY,
12491268
nrfx_isr, nrfx_usbd_irq_handler, 0);
12501269

1251-
/* NOTE: Non-blocking HF clock enable can return -EINPROGRESS
1252-
* if HF clock start was already requested.
1253-
*/
1254-
ret = hf_clock_enable(true, false);
1255-
if (ret && ret != -EINPROGRESS) {
1256-
return ret;
1257-
}
1258-
12591270
err = nrfx_usbd_init(usbd_event_handler);
12601271

12611272
if (err != NRFX_SUCCESS) {
@@ -1280,7 +1291,6 @@ int usb_dc_attach(void)
12801291
int usb_dc_detach(void)
12811292
{
12821293
struct nrf_usbd_ctx *ctx = get_usbd_ctx();
1283-
int ret;
12841294

12851295
k_mutex_lock(&ctx->drv_lock, K_FOREVER);
12861296

@@ -1289,18 +1299,13 @@ int usb_dc_detach(void)
12891299

12901300
nrfx_usbd_disable();
12911301
nrfx_usbd_uninit();
1292-
1293-
ret = hf_clock_enable(false, false);
1294-
if (ret) {
1295-
return ret;
1296-
k_mutex_unlock(&ctx->drv_lock);
1297-
}
1298-
1302+
(void) hf_clock_enable(false, false);
12991303
nrf5_power_usb_power_int_enable(false);
13001304

13011305
ctx->attached = false;
13021306
k_mutex_unlock(&ctx->drv_lock);
1303-
return ret;
1307+
1308+
return 0;
13041309
}
13051310

13061311
int usb_dc_reset(void)

0 commit comments

Comments
 (0)