20
20
#include <linux/delay.h>
21
21
#include <linux/interrupt.h>
22
22
#include <linux/module.h>
23
+ #include <linux/pm_opp.h>
23
24
#include <linux/regulator/consumer.h>
24
25
#include <linux/sched/clock.h>
25
26
#include <linux/iopoll.h>
@@ -274,7 +275,8 @@ static inline void ufshcd_add_delay_before_dme_cmd(struct ufs_hba *hba);
274
275
static int ufshcd_host_reset_and_restore (struct ufs_hba * hba );
275
276
static void ufshcd_resume_clkscaling (struct ufs_hba * hba );
276
277
static void ufshcd_suspend_clkscaling (struct ufs_hba * hba );
277
- static int ufshcd_scale_clks (struct ufs_hba * hba , bool scale_up );
278
+ static int ufshcd_scale_clks (struct ufs_hba * hba , unsigned long freq ,
279
+ bool scale_up );
278
280
static irqreturn_t ufshcd_intr (int irq , void * __hba );
279
281
static int ufshcd_change_power_mode (struct ufs_hba * hba ,
280
282
struct ufs_pa_layer_attr * pwr_mode );
@@ -1061,14 +1063,32 @@ static int ufshcd_set_clk_freq(struct ufs_hba *hba, bool scale_up)
1061
1063
return ret ;
1062
1064
}
1063
1065
1066
+ static int ufshcd_opp_set_rate (struct ufs_hba * hba , unsigned long freq )
1067
+ {
1068
+ struct dev_pm_opp * opp ;
1069
+ int ret ;
1070
+
1071
+ opp = dev_pm_opp_find_freq_floor_indexed (hba -> dev ,
1072
+ & freq , 0 );
1073
+ if (IS_ERR (opp ))
1074
+ return PTR_ERR (opp );
1075
+
1076
+ ret = dev_pm_opp_set_opp (hba -> dev , opp );
1077
+ dev_pm_opp_put (opp );
1078
+
1079
+ return ret ;
1080
+ }
1081
+
1064
1082
/**
1065
1083
* ufshcd_scale_clks - scale up or scale down UFS controller clocks
1066
1084
* @hba: per adapter instance
1085
+ * @freq: frequency to scale
1067
1086
* @scale_up: True if scaling up and false if scaling down
1068
1087
*
1069
1088
* Return: 0 if successful; < 0 upon failure.
1070
1089
*/
1071
- static int ufshcd_scale_clks (struct ufs_hba * hba , bool scale_up )
1090
+ static int ufshcd_scale_clks (struct ufs_hba * hba , unsigned long freq ,
1091
+ bool scale_up )
1072
1092
{
1073
1093
int ret = 0 ;
1074
1094
ktime_t start = ktime_get ();
@@ -1077,13 +1097,21 @@ static int ufshcd_scale_clks(struct ufs_hba *hba, bool scale_up)
1077
1097
if (ret )
1078
1098
goto out ;
1079
1099
1080
- ret = ufshcd_set_clk_freq (hba , scale_up );
1100
+ if (hba -> use_pm_opp )
1101
+ ret = ufshcd_opp_set_rate (hba , freq );
1102
+ else
1103
+ ret = ufshcd_set_clk_freq (hba , scale_up );
1081
1104
if (ret )
1082
1105
goto out ;
1083
1106
1084
1107
ret = ufshcd_vops_clk_scale_notify (hba , scale_up , POST_CHANGE );
1085
- if (ret )
1086
- ufshcd_set_clk_freq (hba , !scale_up );
1108
+ if (ret ) {
1109
+ if (hba -> use_pm_opp )
1110
+ ufshcd_opp_set_rate (hba ,
1111
+ hba -> devfreq -> previous_freq );
1112
+ else
1113
+ ufshcd_set_clk_freq (hba , !scale_up );
1114
+ }
1087
1115
1088
1116
out :
1089
1117
trace_ufshcd_profile_clk_scaling (dev_name (hba -> dev ),
@@ -1095,19 +1123,23 @@ static int ufshcd_scale_clks(struct ufs_hba *hba, bool scale_up)
1095
1123
/**
1096
1124
* ufshcd_is_devfreq_scaling_required - check if scaling is required or not
1097
1125
* @hba: per adapter instance
1126
+ * @freq: frequency to scale
1098
1127
* @scale_up: True if scaling up and false if scaling down
1099
1128
*
1100
1129
* Return: true if scaling is required, false otherwise.
1101
1130
*/
1102
1131
static bool ufshcd_is_devfreq_scaling_required (struct ufs_hba * hba ,
1103
- bool scale_up )
1132
+ unsigned long freq , bool scale_up )
1104
1133
{
1105
1134
struct ufs_clk_info * clki ;
1106
1135
struct list_head * head = & hba -> clk_list_head ;
1107
1136
1108
1137
if (list_empty (head ))
1109
1138
return false;
1110
1139
1140
+ if (hba -> use_pm_opp )
1141
+ return freq != hba -> clk_scaling .target_freq ;
1142
+
1111
1143
list_for_each_entry (clki , head , list ) {
1112
1144
if (!IS_ERR_OR_NULL (clki -> clk )) {
1113
1145
if (scale_up && clki -> max_freq ) {
@@ -1303,12 +1335,14 @@ static void ufshcd_clock_scaling_unprepare(struct ufs_hba *hba, int err, bool sc
1303
1335
/**
1304
1336
* ufshcd_devfreq_scale - scale up/down UFS clocks and gear
1305
1337
* @hba: per adapter instance
1338
+ * @freq: frequency to scale
1306
1339
* @scale_up: True for scaling up and false for scalin down
1307
1340
*
1308
1341
* Return: 0 for success; -EBUSY if scaling can't happen at this time; non-zero
1309
1342
* for any other errors.
1310
1343
*/
1311
- static int ufshcd_devfreq_scale (struct ufs_hba * hba , bool scale_up )
1344
+ static int ufshcd_devfreq_scale (struct ufs_hba * hba , unsigned long freq ,
1345
+ bool scale_up )
1312
1346
{
1313
1347
int ret = 0 ;
1314
1348
@@ -1323,7 +1357,7 @@ static int ufshcd_devfreq_scale(struct ufs_hba *hba, bool scale_up)
1323
1357
goto out_unprepare ;
1324
1358
}
1325
1359
1326
- ret = ufshcd_scale_clks (hba , scale_up );
1360
+ ret = ufshcd_scale_clks (hba , freq , scale_up );
1327
1361
if (ret ) {
1328
1362
if (!scale_up )
1329
1363
ufshcd_scale_gear (hba , true);
@@ -1334,7 +1368,8 @@ static int ufshcd_devfreq_scale(struct ufs_hba *hba, bool scale_up)
1334
1368
if (scale_up ) {
1335
1369
ret = ufshcd_scale_gear (hba , true);
1336
1370
if (ret ) {
1337
- ufshcd_scale_clks (hba , false);
1371
+ ufshcd_scale_clks (hba , hba -> devfreq -> previous_freq ,
1372
+ false);
1338
1373
goto out_unprepare ;
1339
1374
}
1340
1375
}
@@ -1393,9 +1428,22 @@ static int ufshcd_devfreq_target(struct device *dev,
1393
1428
if (!ufshcd_is_clkscaling_supported (hba ))
1394
1429
return - EINVAL ;
1395
1430
1396
- clki = list_first_entry (& hba -> clk_list_head , struct ufs_clk_info , list );
1397
- /* Override with the closest supported frequency */
1398
- * freq = (unsigned long ) clk_round_rate (clki -> clk , * freq );
1431
+ if (hba -> use_pm_opp ) {
1432
+ struct dev_pm_opp * opp ;
1433
+
1434
+ /* Get the recommended frequency from OPP framework */
1435
+ opp = devfreq_recommended_opp (dev , freq , flags );
1436
+ if (IS_ERR (opp ))
1437
+ return PTR_ERR (opp );
1438
+
1439
+ dev_pm_opp_put (opp );
1440
+ } else {
1441
+ /* Override with the closest supported frequency */
1442
+ clki = list_first_entry (& hba -> clk_list_head , struct ufs_clk_info ,
1443
+ list );
1444
+ * freq = (unsigned long ) clk_round_rate (clki -> clk , * freq );
1445
+ }
1446
+
1399
1447
spin_lock_irqsave (hba -> host -> host_lock , irq_flags );
1400
1448
if (ufshcd_eh_in_progress (hba )) {
1401
1449
spin_unlock_irqrestore (hba -> host -> host_lock , irq_flags );
@@ -1417,20 +1465,27 @@ static int ufshcd_devfreq_target(struct device *dev,
1417
1465
goto out ;
1418
1466
}
1419
1467
1420
- /* Decide based on the rounded-off frequency and update */
1421
- scale_up = * freq == clki -> max_freq ;
1422
- if (!scale_up )
1468
+ /* Decide based on the target or rounded-off frequency and update */
1469
+ if (hba -> use_pm_opp )
1470
+ scale_up = * freq > hba -> clk_scaling .target_freq ;
1471
+ else
1472
+ scale_up = * freq == clki -> max_freq ;
1473
+
1474
+ if (!hba -> use_pm_opp && !scale_up )
1423
1475
* freq = clki -> min_freq ;
1476
+
1424
1477
/* Update the frequency */
1425
- if (!ufshcd_is_devfreq_scaling_required (hba , scale_up )) {
1478
+ if (!ufshcd_is_devfreq_scaling_required (hba , * freq , scale_up )) {
1426
1479
spin_unlock_irqrestore (hba -> host -> host_lock , irq_flags );
1427
1480
ret = 0 ;
1428
1481
goto out ; /* no state change required */
1429
1482
}
1430
1483
spin_unlock_irqrestore (hba -> host -> host_lock , irq_flags );
1431
1484
1432
1485
start = ktime_get ();
1433
- ret = ufshcd_devfreq_scale (hba , scale_up );
1486
+ ret = ufshcd_devfreq_scale (hba , * freq , scale_up );
1487
+ if (!ret )
1488
+ hba -> clk_scaling .target_freq = * freq ;
1434
1489
1435
1490
trace_ufshcd_profile_clk_scaling (dev_name (hba -> dev ),
1436
1491
(scale_up ? "up" : "down" ),
@@ -1450,8 +1505,6 @@ static int ufshcd_devfreq_get_dev_status(struct device *dev,
1450
1505
struct ufs_hba * hba = dev_get_drvdata (dev );
1451
1506
struct ufs_clk_scaling * scaling = & hba -> clk_scaling ;
1452
1507
unsigned long flags ;
1453
- struct list_head * clk_list = & hba -> clk_list_head ;
1454
- struct ufs_clk_info * clki ;
1455
1508
ktime_t curr_t ;
1456
1509
1457
1510
if (!ufshcd_is_clkscaling_supported (hba ))
@@ -1464,17 +1517,24 @@ static int ufshcd_devfreq_get_dev_status(struct device *dev,
1464
1517
if (!scaling -> window_start_t )
1465
1518
goto start_window ;
1466
1519
1467
- clki = list_first_entry (clk_list , struct ufs_clk_info , list );
1468
1520
/*
1469
1521
* If current frequency is 0, then the ondemand governor considers
1470
1522
* there's no initial frequency set. And it always requests to set
1471
1523
* to max. frequency.
1472
1524
*/
1473
- stat -> current_frequency = clki -> curr_freq ;
1525
+ if (hba -> use_pm_opp ) {
1526
+ stat -> current_frequency = hba -> clk_scaling .target_freq ;
1527
+ } else {
1528
+ struct list_head * clk_list = & hba -> clk_list_head ;
1529
+ struct ufs_clk_info * clki ;
1530
+
1531
+ clki = list_first_entry (clk_list , struct ufs_clk_info , list );
1532
+ stat -> current_frequency = clki -> curr_freq ;
1533
+ }
1534
+
1474
1535
if (scaling -> is_busy_started )
1475
1536
scaling -> tot_busy_t += ktime_us_delta (curr_t ,
1476
1537
scaling -> busy_start_t );
1477
-
1478
1538
stat -> total_time = ktime_us_delta (curr_t , scaling -> window_start_t );
1479
1539
stat -> busy_time = scaling -> tot_busy_t ;
1480
1540
start_window :
@@ -1503,9 +1563,11 @@ static int ufshcd_devfreq_init(struct ufs_hba *hba)
1503
1563
if (list_empty (clk_list ))
1504
1564
return 0 ;
1505
1565
1506
- clki = list_first_entry (clk_list , struct ufs_clk_info , list );
1507
- dev_pm_opp_add (hba -> dev , clki -> min_freq , 0 );
1508
- dev_pm_opp_add (hba -> dev , clki -> max_freq , 0 );
1566
+ if (!hba -> use_pm_opp ) {
1567
+ clki = list_first_entry (clk_list , struct ufs_clk_info , list );
1568
+ dev_pm_opp_add (hba -> dev , clki -> min_freq , 0 );
1569
+ dev_pm_opp_add (hba -> dev , clki -> max_freq , 0 );
1570
+ }
1509
1571
1510
1572
ufshcd_vops_config_scaling_param (hba , & hba -> vps -> devfreq_profile ,
1511
1573
& hba -> vps -> ondemand_data );
@@ -1517,8 +1579,10 @@ static int ufshcd_devfreq_init(struct ufs_hba *hba)
1517
1579
ret = PTR_ERR (devfreq );
1518
1580
dev_err (hba -> dev , "Unable to register with devfreq %d\n" , ret );
1519
1581
1520
- dev_pm_opp_remove (hba -> dev , clki -> min_freq );
1521
- dev_pm_opp_remove (hba -> dev , clki -> max_freq );
1582
+ if (!hba -> use_pm_opp ) {
1583
+ dev_pm_opp_remove (hba -> dev , clki -> min_freq );
1584
+ dev_pm_opp_remove (hba -> dev , clki -> max_freq );
1585
+ }
1522
1586
return ret ;
1523
1587
}
1524
1588
@@ -1530,17 +1594,20 @@ static int ufshcd_devfreq_init(struct ufs_hba *hba)
1530
1594
static void ufshcd_devfreq_remove (struct ufs_hba * hba )
1531
1595
{
1532
1596
struct list_head * clk_list = & hba -> clk_list_head ;
1533
- struct ufs_clk_info * clki ;
1534
1597
1535
1598
if (!hba -> devfreq )
1536
1599
return ;
1537
1600
1538
1601
devfreq_remove_device (hba -> devfreq );
1539
1602
hba -> devfreq = NULL ;
1540
1603
1541
- clki = list_first_entry (clk_list , struct ufs_clk_info , list );
1542
- dev_pm_opp_remove (hba -> dev , clki -> min_freq );
1543
- dev_pm_opp_remove (hba -> dev , clki -> max_freq );
1604
+ if (!hba -> use_pm_opp ) {
1605
+ struct ufs_clk_info * clki ;
1606
+
1607
+ clki = list_first_entry (clk_list , struct ufs_clk_info , list );
1608
+ dev_pm_opp_remove (hba -> dev , clki -> min_freq );
1609
+ dev_pm_opp_remove (hba -> dev , clki -> max_freq );
1610
+ }
1544
1611
}
1545
1612
1546
1613
static void ufshcd_suspend_clkscaling (struct ufs_hba * hba )
@@ -1616,7 +1683,7 @@ static ssize_t ufshcd_clkscale_enable_store(struct device *dev,
1616
1683
ufshcd_resume_clkscaling (hba );
1617
1684
} else {
1618
1685
ufshcd_suspend_clkscaling (hba );
1619
- err = ufshcd_devfreq_scale (hba , true);
1686
+ err = ufshcd_devfreq_scale (hba , ULONG_MAX , true);
1620
1687
if (err )
1621
1688
dev_err (hba -> dev , "%s: failed to scale clocks up %d\n" ,
1622
1689
__func__ , err );
@@ -7617,7 +7684,7 @@ static int ufshcd_host_reset_and_restore(struct ufs_hba *hba)
7617
7684
hba -> silence_err_logs = false;
7618
7685
7619
7686
/* scale up clocks to max frequency before full reinitialization */
7620
- ufshcd_scale_clks (hba , true);
7687
+ ufshcd_scale_clks (hba , ULONG_MAX , true);
7621
7688
7622
7689
err = ufshcd_hba_enable (hba );
7623
7690
@@ -9163,6 +9230,17 @@ static int ufshcd_init_clocks(struct ufs_hba *hba)
9163
9230
dev_dbg (dev , "%s: clk: %s, rate: %lu\n" , __func__ ,
9164
9231
clki -> name , clk_get_rate (clki -> clk ));
9165
9232
}
9233
+
9234
+ /* Set Max. frequency for all clocks */
9235
+ if (hba -> use_pm_opp ) {
9236
+ ret = ufshcd_opp_set_rate (hba , ULONG_MAX );
9237
+ if (ret ) {
9238
+ dev_err (hba -> dev , "%s: failed to set OPP: %d" , __func__ ,
9239
+ ret );
9240
+ goto out ;
9241
+ }
9242
+ }
9243
+
9166
9244
out :
9167
9245
return ret ;
9168
9246
}
0 commit comments