@@ -525,6 +525,7 @@ static int hf_clock_enable(bool on, bool blocking)
525
525
{
526
526
int ret = - ENODEV ;
527
527
struct device * clock ;
528
+ static bool clock_requested ;
528
529
529
530
clock = device_get_binding (CONFIG_CLOCK_CONTROL_NRF_M16SRC_DRV_NAME );
530
531
if (!clock ) {
@@ -533,8 +534,18 @@ static int hf_clock_enable(bool on, bool blocking)
533
534
}
534
535
535
536
if (on ) {
537
+ if (clock_requested ) {
538
+ /* Do not request HFCLK multiple times. */
539
+ return 0 ;
540
+ }
536
541
ret = clock_control_on (clock , (void * )blocking );
537
542
} 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
+ }
538
549
ret = clock_control_off (clock , (void * )blocking );
539
550
}
540
551
@@ -544,9 +555,14 @@ static int hf_clock_enable(bool on, bool blocking)
544
555
return ret ;
545
556
}
546
557
558
+ clock_requested = on ;
547
559
LOG_DBG ("HF clock %s success (%d)" , on ? "start" : "stop" , ret );
548
560
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 ;
550
566
}
551
567
552
568
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)
731
747
case USBD_ATTACHED :
732
748
LOG_DBG ("USB detected" );
733
749
nrfx_usbd_enable ();
750
+ (void ) hf_clock_enable (true, false);
751
+
734
752
break ;
735
753
736
754
case USBD_POWERED :
@@ -748,6 +766,7 @@ static inline void usbd_work_process_pwr_events(struct usbd_pwr_event *pwr_evt)
748
766
LOG_DBG ("USB Removed" );
749
767
ctx -> ready = false;
750
768
nrfx_usbd_disable ();
769
+ (void ) hf_clock_enable (false, false);
751
770
752
771
if (ctx -> status_cb ) {
753
772
ctx -> status_cb (USB_DC_DISCONNECTED , NULL );
@@ -1248,14 +1267,6 @@ int usb_dc_attach(void)
1248
1267
DT_NORDIC_NRF_USBD_USBD_0_IRQ_PRIORITY ,
1249
1268
nrfx_isr , nrfx_usbd_irq_handler , 0 );
1250
1269
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
-
1259
1270
err = nrfx_usbd_init (usbd_event_handler );
1260
1271
1261
1272
if (err != NRFX_SUCCESS ) {
@@ -1280,7 +1291,6 @@ int usb_dc_attach(void)
1280
1291
int usb_dc_detach (void )
1281
1292
{
1282
1293
struct nrf_usbd_ctx * ctx = get_usbd_ctx ();
1283
- int ret ;
1284
1294
1285
1295
k_mutex_lock (& ctx -> drv_lock , K_FOREVER );
1286
1296
@@ -1289,18 +1299,13 @@ int usb_dc_detach(void)
1289
1299
1290
1300
nrfx_usbd_disable ();
1291
1301
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);
1299
1303
nrf5_power_usb_power_int_enable (false);
1300
1304
1301
1305
ctx -> attached = false;
1302
1306
k_mutex_unlock (& ctx -> drv_lock );
1303
- return ret ;
1307
+
1308
+ return 0 ;
1304
1309
}
1305
1310
1306
1311
int usb_dc_reset (void )
0 commit comments