9
9
* Copyright (C) 2014 Cogent Embedded, Inc.
10
10
*/
11
11
12
+ #include <linux/cleanup.h>
12
13
#include <linux/extcon-provider.h>
13
14
#include <linux/interrupt.h>
14
15
#include <linux/io.h>
@@ -107,7 +108,6 @@ struct rcar_gen3_phy {
107
108
struct rcar_gen3_chan * ch ;
108
109
u32 int_enable_bits ;
109
110
bool initialized ;
110
- bool otg_initialized ;
111
111
bool powered ;
112
112
};
113
113
@@ -119,9 +119,8 @@ struct rcar_gen3_chan {
119
119
struct regulator * vbus ;
120
120
struct reset_control * rstc ;
121
121
struct work_struct work ;
122
- struct mutex lock ; /* protects rphys[...].powered */
122
+ spinlock_t lock ; /* protects access to hardware and driver data structure. */
123
123
enum usb_dr_mode dr_mode ;
124
- int irq ;
125
124
u32 obint_enable_bits ;
126
125
bool extcon_host ;
127
126
bool is_otg_channel ;
@@ -320,16 +319,15 @@ static bool rcar_gen3_is_any_rphy_initialized(struct rcar_gen3_chan *ch)
320
319
return false;
321
320
}
322
321
323
- static bool rcar_gen3_needs_init_otg (struct rcar_gen3_chan * ch )
322
+ static bool rcar_gen3_is_any_otg_rphy_initialized (struct rcar_gen3_chan * ch )
324
323
{
325
- int i ;
326
-
327
- for (i = 0 ; i < NUM_OF_PHYS ; i ++ ) {
328
- if (ch -> rphys [i ].otg_initialized )
329
- return false;
324
+ for (enum rcar_gen3_phy_index i = PHY_INDEX_BOTH_HC ; i <= PHY_INDEX_EHCI ;
325
+ i ++ ) {
326
+ if (ch -> rphys [i ].initialized )
327
+ return true;
330
328
}
331
329
332
- return true ;
330
+ return false ;
333
331
}
334
332
335
333
static bool rcar_gen3_are_all_rphys_power_off (struct rcar_gen3_chan * ch )
@@ -351,7 +349,9 @@ static ssize_t role_store(struct device *dev, struct device_attribute *attr,
351
349
bool is_b_device ;
352
350
enum phy_mode cur_mode , new_mode ;
353
351
354
- if (!ch -> is_otg_channel || !rcar_gen3_is_any_rphy_initialized (ch ))
352
+ guard (spinlock_irqsave )(& ch -> lock );
353
+
354
+ if (!ch -> is_otg_channel || !rcar_gen3_is_any_otg_rphy_initialized (ch ))
355
355
return - EIO ;
356
356
357
357
if (sysfs_streq (buf , "host" ))
@@ -389,7 +389,7 @@ static ssize_t role_show(struct device *dev, struct device_attribute *attr,
389
389
{
390
390
struct rcar_gen3_chan * ch = dev_get_drvdata (dev );
391
391
392
- if (!ch -> is_otg_channel || !rcar_gen3_is_any_rphy_initialized (ch ))
392
+ if (!ch -> is_otg_channel || !rcar_gen3_is_any_otg_rphy_initialized (ch ))
393
393
return - EIO ;
394
394
395
395
return sprintf (buf , "%s\n" , rcar_gen3_is_host (ch ) ? "host" :
@@ -402,6 +402,9 @@ static void rcar_gen3_init_otg(struct rcar_gen3_chan *ch)
402
402
void __iomem * usb2_base = ch -> base ;
403
403
u32 val ;
404
404
405
+ if (!ch -> is_otg_channel || rcar_gen3_is_any_otg_rphy_initialized (ch ))
406
+ return ;
407
+
405
408
/* Should not use functions of read-modify-write a register */
406
409
val = readl (usb2_base + USB2_LINECTRL1 );
407
410
val = (val & ~USB2_LINECTRL1_DP_RPD ) | USB2_LINECTRL1_DPRPD_EN |
@@ -415,7 +418,7 @@ static void rcar_gen3_init_otg(struct rcar_gen3_chan *ch)
415
418
val = readl (usb2_base + USB2_ADPCTRL );
416
419
writel (val | USB2_ADPCTRL_IDPULLUP , usb2_base + USB2_ADPCTRL );
417
420
}
418
- msleep (20 );
421
+ mdelay (20 );
419
422
420
423
writel (0xffffffff , usb2_base + USB2_OBINTSTA );
421
424
writel (ch -> obint_enable_bits , usb2_base + USB2_OBINTEN );
@@ -427,16 +430,27 @@ static irqreturn_t rcar_gen3_phy_usb2_irq(int irq, void *_ch)
427
430
{
428
431
struct rcar_gen3_chan * ch = _ch ;
429
432
void __iomem * usb2_base = ch -> base ;
430
- u32 status = readl ( usb2_base + USB2_OBINTSTA ) ;
433
+ struct device * dev = ch -> dev ;
431
434
irqreturn_t ret = IRQ_NONE ;
435
+ u32 status ;
436
+
437
+ pm_runtime_get_noresume (dev );
438
+
439
+ if (pm_runtime_suspended (dev ))
440
+ goto rpm_put ;
432
441
433
- if (status & ch -> obint_enable_bits ) {
434
- dev_vdbg (ch -> dev , "%s: %08x\n" , __func__ , status );
435
- writel (ch -> obint_enable_bits , usb2_base + USB2_OBINTSTA );
436
- rcar_gen3_device_recognition (ch );
437
- ret = IRQ_HANDLED ;
442
+ scoped_guard (spinlock , & ch -> lock ) {
443
+ status = readl (usb2_base + USB2_OBINTSTA );
444
+ if (status & ch -> obint_enable_bits ) {
445
+ dev_vdbg (dev , "%s: %08x\n" , __func__ , status );
446
+ writel (ch -> obint_enable_bits , usb2_base + USB2_OBINTSTA );
447
+ rcar_gen3_device_recognition (ch );
448
+ ret = IRQ_HANDLED ;
449
+ }
438
450
}
439
451
452
+ rpm_put :
453
+ pm_runtime_put_noidle (dev );
440
454
return ret ;
441
455
}
442
456
@@ -446,32 +460,23 @@ static int rcar_gen3_phy_usb2_init(struct phy *p)
446
460
struct rcar_gen3_chan * channel = rphy -> ch ;
447
461
void __iomem * usb2_base = channel -> base ;
448
462
u32 val ;
449
- int ret ;
450
463
451
- if (!rcar_gen3_is_any_rphy_initialized (channel ) && channel -> irq >= 0 ) {
452
- INIT_WORK (& channel -> work , rcar_gen3_phy_usb2_work );
453
- ret = request_irq (channel -> irq , rcar_gen3_phy_usb2_irq ,
454
- IRQF_SHARED , dev_name (channel -> dev ), channel );
455
- if (ret < 0 ) {
456
- dev_err (channel -> dev , "No irq handler (%d)\n" , channel -> irq );
457
- return ret ;
458
- }
459
- }
464
+ guard (spinlock_irqsave )(& channel -> lock );
460
465
461
466
/* Initialize USB2 part */
462
467
val = readl (usb2_base + USB2_INT_ENABLE );
463
468
val |= USB2_INT_ENABLE_UCOM_INTEN | rphy -> int_enable_bits ;
464
469
writel (val , usb2_base + USB2_INT_ENABLE );
465
- writel (USB2_SPD_RSM_TIMSET_INIT , usb2_base + USB2_SPD_RSM_TIMSET );
466
- writel (USB2_OC_TIMSET_INIT , usb2_base + USB2_OC_TIMSET );
467
-
468
- /* Initialize otg part */
469
- if (channel -> is_otg_channel ) {
470
- if (rcar_gen3_needs_init_otg (channel ))
471
- rcar_gen3_init_otg (channel );
472
- rphy -> otg_initialized = true;
470
+
471
+ if (!rcar_gen3_is_any_rphy_initialized (channel )) {
472
+ writel (USB2_SPD_RSM_TIMSET_INIT , usb2_base + USB2_SPD_RSM_TIMSET );
473
+ writel (USB2_OC_TIMSET_INIT , usb2_base + USB2_OC_TIMSET );
473
474
}
474
475
476
+ /* Initialize otg part (only if we initialize a PHY with IRQs). */
477
+ if (rphy -> int_enable_bits )
478
+ rcar_gen3_init_otg (channel );
479
+
475
480
rphy -> initialized = true;
476
481
477
482
return 0 ;
@@ -484,20 +489,16 @@ static int rcar_gen3_phy_usb2_exit(struct phy *p)
484
489
void __iomem * usb2_base = channel -> base ;
485
490
u32 val ;
486
491
487
- rphy -> initialized = false ;
492
+ guard ( spinlock_irqsave )( & channel -> lock ) ;
488
493
489
- if (channel -> is_otg_channel )
490
- rphy -> otg_initialized = false;
494
+ rphy -> initialized = false;
491
495
492
496
val = readl (usb2_base + USB2_INT_ENABLE );
493
497
val &= ~rphy -> int_enable_bits ;
494
498
if (!rcar_gen3_is_any_rphy_initialized (channel ))
495
499
val &= ~USB2_INT_ENABLE_UCOM_INTEN ;
496
500
writel (val , usb2_base + USB2_INT_ENABLE );
497
501
498
- if (channel -> irq >= 0 && !rcar_gen3_is_any_rphy_initialized (channel ))
499
- free_irq (channel -> irq , channel );
500
-
501
502
return 0 ;
502
503
}
503
504
@@ -509,16 +510,17 @@ static int rcar_gen3_phy_usb2_power_on(struct phy *p)
509
510
u32 val ;
510
511
int ret = 0 ;
511
512
512
- mutex_lock (& channel -> lock );
513
- if (!rcar_gen3_are_all_rphys_power_off (channel ))
514
- goto out ;
515
-
516
513
if (channel -> vbus ) {
517
514
ret = regulator_enable (channel -> vbus );
518
515
if (ret )
519
- goto out ;
516
+ return ret ;
520
517
}
521
518
519
+ guard (spinlock_irqsave )(& channel -> lock );
520
+
521
+ if (!rcar_gen3_are_all_rphys_power_off (channel ))
522
+ goto out ;
523
+
522
524
val = readl (usb2_base + USB2_USBCTR );
523
525
val |= USB2_USBCTR_PLL_RST ;
524
526
writel (val , usb2_base + USB2_USBCTR );
@@ -528,7 +530,6 @@ static int rcar_gen3_phy_usb2_power_on(struct phy *p)
528
530
out :
529
531
/* The powered flag should be set for any other phys anyway */
530
532
rphy -> powered = true;
531
- mutex_unlock (& channel -> lock );
532
533
533
534
return 0 ;
534
535
}
@@ -539,18 +540,20 @@ static int rcar_gen3_phy_usb2_power_off(struct phy *p)
539
540
struct rcar_gen3_chan * channel = rphy -> ch ;
540
541
int ret = 0 ;
541
542
542
- mutex_lock ( & channel -> lock );
543
- rphy -> powered = false;
543
+ scoped_guard ( spinlock_irqsave , & channel -> lock ) {
544
+ rphy -> powered = false;
544
545
545
- if (!rcar_gen3_are_all_rphys_power_off (channel ))
546
- goto out ;
546
+ if (rcar_gen3_are_all_rphys_power_off (channel )) {
547
+ u32 val = readl (channel -> base + USB2_USBCTR );
548
+
549
+ val |= USB2_USBCTR_PLL_RST ;
550
+ writel (val , channel -> base + USB2_USBCTR );
551
+ }
552
+ }
547
553
548
554
if (channel -> vbus )
549
555
ret = regulator_disable (channel -> vbus );
550
556
551
- out :
552
- mutex_unlock (& channel -> lock );
553
-
554
557
return ret ;
555
558
}
556
559
@@ -703,7 +706,7 @@ static int rcar_gen3_phy_usb2_probe(struct platform_device *pdev)
703
706
struct device * dev = & pdev -> dev ;
704
707
struct rcar_gen3_chan * channel ;
705
708
struct phy_provider * provider ;
706
- int ret = 0 , i ;
709
+ int ret = 0 , i , irq ;
707
710
708
711
if (!dev -> of_node ) {
709
712
dev_err (dev , "This driver needs device tree\n" );
@@ -719,8 +722,6 @@ static int rcar_gen3_phy_usb2_probe(struct platform_device *pdev)
719
722
return PTR_ERR (channel -> base );
720
723
721
724
channel -> obint_enable_bits = USB2_OBINT_BITS ;
722
- /* get irq number here and request_irq for OTG in phy_init */
723
- channel -> irq = platform_get_irq_optional (pdev , 0 );
724
725
channel -> dr_mode = rcar_gen3_get_dr_mode (dev -> of_node );
725
726
if (channel -> dr_mode != USB_DR_MODE_UNKNOWN ) {
726
727
channel -> is_otg_channel = true;
@@ -763,7 +764,7 @@ static int rcar_gen3_phy_usb2_probe(struct platform_device *pdev)
763
764
if (phy_data -> no_adp_ctrl )
764
765
channel -> obint_enable_bits = USB2_OBINT_IDCHG_EN ;
765
766
766
- mutex_init (& channel -> lock );
767
+ spin_lock_init (& channel -> lock );
767
768
for (i = 0 ; i < NUM_OF_PHYS ; i ++ ) {
768
769
channel -> rphys [i ].phy = devm_phy_create (dev , NULL ,
769
770
phy_data -> phy_usb2_ops );
@@ -789,6 +790,20 @@ static int rcar_gen3_phy_usb2_probe(struct platform_device *pdev)
789
790
channel -> vbus = NULL ;
790
791
}
791
792
793
+ irq = platform_get_irq_optional (pdev , 0 );
794
+ if (irq < 0 && irq != - ENXIO ) {
795
+ ret = irq ;
796
+ goto error ;
797
+ } else if (irq > 0 ) {
798
+ INIT_WORK (& channel -> work , rcar_gen3_phy_usb2_work );
799
+ ret = devm_request_irq (dev , irq , rcar_gen3_phy_usb2_irq ,
800
+ IRQF_SHARED , dev_name (dev ), channel );
801
+ if (ret < 0 ) {
802
+ dev_err (dev , "Failed to request irq (%d)\n" , irq );
803
+ goto error ;
804
+ }
805
+ }
806
+
792
807
provider = devm_of_phy_provider_register (dev , rcar_gen3_phy_usb2_xlate );
793
808
if (IS_ERR (provider )) {
794
809
dev_err (dev , "Failed to register PHY provider\n" );
0 commit comments