6
6
#include <linux/clk.h>
7
7
#include <linux/delay.h>
8
8
#include <linux/err.h>
9
+ #include <linux/interconnect.h>
9
10
#include <linux/interrupt.h>
10
11
#include <linux/io.h>
11
12
#include <linux/list.h>
12
13
#include <linux/module.h>
13
14
#include <linux/of.h>
14
15
#include <linux/platform_device.h>
16
+ #include <linux/pm_opp.h>
15
17
#include <linux/pm_runtime.h>
16
18
#include <linux/spi/spi.h>
17
19
#include <linux/dmaengine.h>
121
123
#define SPI_DELAY_THRESHOLD 1
122
124
#define SPI_DELAY_RETRY 10
123
125
126
+ #define SPI_BUS_WIDTH 8
127
+
124
128
struct spi_qup {
125
129
void __iomem * base ;
126
130
struct device * dev ;
127
131
struct clk * cclk ; /* core clock */
128
132
struct clk * iclk ; /* interface clock */
133
+ struct icc_path * icc_path ; /* interconnect to RAM */
129
134
int irq ;
130
135
spinlock_t lock ;
131
136
@@ -148,6 +153,8 @@ struct spi_qup {
148
153
int mode ;
149
154
struct dma_slave_config rx_conf ;
150
155
struct dma_slave_config tx_conf ;
156
+
157
+ u32 bw_speed_hz ;
151
158
};
152
159
153
160
static int spi_qup_io_config (struct spi_device * spi , struct spi_transfer * xfer );
@@ -180,6 +187,23 @@ static inline bool spi_qup_is_valid_state(struct spi_qup *controller)
180
187
return opstate & QUP_STATE_VALID ;
181
188
}
182
189
190
+ static int spi_qup_vote_bw (struct spi_qup * controller , u32 speed_hz )
191
+ {
192
+ u32 needed_peak_bw ;
193
+ int ret ;
194
+
195
+ if (controller -> bw_speed_hz == speed_hz )
196
+ return 0 ;
197
+
198
+ needed_peak_bw = Bps_to_icc (speed_hz * SPI_BUS_WIDTH );
199
+ ret = icc_set_bw (controller -> icc_path , 0 , needed_peak_bw );
200
+ if (ret )
201
+ return ret ;
202
+
203
+ controller -> bw_speed_hz = speed_hz ;
204
+ return 0 ;
205
+ }
206
+
183
207
static int spi_qup_set_state (struct spi_qup * controller , u32 state )
184
208
{
185
209
unsigned long loop ;
@@ -450,6 +474,12 @@ static int spi_qup_do_dma(struct spi_device *spi, struct spi_transfer *xfer,
450
474
struct scatterlist * tx_sgl , * rx_sgl ;
451
475
int ret ;
452
476
477
+ ret = spi_qup_vote_bw (qup , xfer -> speed_hz );
478
+ if (ret ) {
479
+ dev_err (qup -> dev , "fail to vote for ICC bandwidth: %d\n" , ret );
480
+ return - EIO ;
481
+ }
482
+
453
483
if (xfer -> rx_buf )
454
484
rx_done = spi_qup_dma_done ;
455
485
else if (xfer -> tx_buf )
@@ -667,7 +697,7 @@ static int spi_qup_io_prep(struct spi_device *spi, struct spi_transfer *xfer)
667
697
return - EIO ;
668
698
}
669
699
670
- ret = clk_set_rate (controller -> cclk , xfer -> speed_hz );
700
+ ret = dev_pm_opp_set_rate (controller -> dev , xfer -> speed_hz );
671
701
if (ret ) {
672
702
dev_err (controller -> dev , "fail to set frequency %d" ,
673
703
xfer -> speed_hz );
@@ -993,6 +1023,7 @@ static void spi_qup_set_cs(struct spi_device *spi, bool val)
993
1023
static int spi_qup_probe (struct platform_device * pdev )
994
1024
{
995
1025
struct spi_controller * host ;
1026
+ struct icc_path * icc_path ;
996
1027
struct clk * iclk , * cclk ;
997
1028
struct spi_qup * controller ;
998
1029
struct resource * res ;
@@ -1018,6 +1049,11 @@ static int spi_qup_probe(struct platform_device *pdev)
1018
1049
if (IS_ERR (iclk ))
1019
1050
return PTR_ERR (iclk );
1020
1051
1052
+ icc_path = devm_of_icc_get (dev , NULL );
1053
+ if (IS_ERR (icc_path ))
1054
+ return dev_err_probe (dev , PTR_ERR (icc_path ),
1055
+ "failed to get interconnect path\n" );
1056
+
1021
1057
/* This is optional parameter */
1022
1058
if (of_property_read_u32 (dev -> of_node , "spi-max-frequency" , & max_freq ))
1023
1059
max_freq = SPI_MAX_RATE ;
@@ -1027,6 +1063,15 @@ static int spi_qup_probe(struct platform_device *pdev)
1027
1063
return - ENXIO ;
1028
1064
}
1029
1065
1066
+ ret = devm_pm_opp_set_clkname (dev , "core" );
1067
+ if (ret )
1068
+ return ret ;
1069
+
1070
+ /* OPP table is optional */
1071
+ ret = devm_pm_opp_of_add_table (dev );
1072
+ if (ret && ret != - ENODEV )
1073
+ return dev_err_probe (dev , ret , "invalid OPP table\n" );
1074
+
1030
1075
host = spi_alloc_host (dev , sizeof (struct spi_qup ));
1031
1076
if (!host ) {
1032
1077
dev_err (dev , "cannot allocate host\n" );
@@ -1060,6 +1105,7 @@ static int spi_qup_probe(struct platform_device *pdev)
1060
1105
controller -> base = base ;
1061
1106
controller -> iclk = iclk ;
1062
1107
controller -> cclk = cclk ;
1108
+ controller -> icc_path = icc_path ;
1063
1109
controller -> irq = irq ;
1064
1110
1065
1111
ret = spi_qup_init_dma (host , res -> start );
@@ -1180,6 +1226,7 @@ static int spi_qup_pm_suspend_runtime(struct device *device)
1180
1226
writel_relaxed (config , controller -> base + QUP_CONFIG );
1181
1227
1182
1228
clk_disable_unprepare (controller -> cclk );
1229
+ spi_qup_vote_bw (controller , 0 );
1183
1230
clk_disable_unprepare (controller -> iclk );
1184
1231
1185
1232
return 0 ;
@@ -1231,6 +1278,7 @@ static int spi_qup_suspend(struct device *device)
1231
1278
return ret ;
1232
1279
1233
1280
clk_disable_unprepare (controller -> cclk );
1281
+ spi_qup_vote_bw (controller , 0 );
1234
1282
clk_disable_unprepare (controller -> iclk );
1235
1283
return 0 ;
1236
1284
}
0 commit comments