15
15
#include <linux/regmap.h>
16
16
#include <linux/thermal.h>
17
17
#include <linux/nvmem-consumer.h>
18
+ #include <linux/pm_runtime.h>
18
19
19
20
#define REG_SET 0x4
20
21
#define REG_CLR 0x8
@@ -194,6 +195,7 @@ static struct thermal_soc_data thermal_imx7d_data = {
194
195
};
195
196
196
197
struct imx_thermal_data {
198
+ struct device * dev ;
197
199
struct cpufreq_policy * policy ;
198
200
struct thermal_zone_device * tz ;
199
201
struct thermal_cooling_device * cdev ;
@@ -252,44 +254,15 @@ static int imx_get_temp(struct thermal_zone_device *tz, int *temp)
252
254
const struct thermal_soc_data * soc_data = data -> socdata ;
253
255
struct regmap * map = data -> tempmon ;
254
256
unsigned int n_meas ;
255
- bool wait , run_measurement ;
256
257
u32 val ;
258
+ int ret ;
257
259
258
- run_measurement = !data -> irq_enabled ;
259
- if (!run_measurement ) {
260
- /* Check if a measurement is currently in progress */
261
- regmap_read (map , soc_data -> temp_data , & val );
262
- wait = !(val & soc_data -> temp_valid_mask );
263
- } else {
264
- /*
265
- * Every time we measure the temperature, we will power on the
266
- * temperature sensor, enable measurements, take a reading,
267
- * disable measurements, power off the temperature sensor.
268
- */
269
- regmap_write (map , soc_data -> sensor_ctrl + REG_CLR ,
270
- soc_data -> power_down_mask );
271
- regmap_write (map , soc_data -> sensor_ctrl + REG_SET ,
272
- soc_data -> measure_temp_mask );
273
-
274
- wait = true;
275
- }
276
-
277
- /*
278
- * According to the temp sensor designers, it may require up to ~17us
279
- * to complete a measurement.
280
- */
281
- if (wait )
282
- usleep_range (20 , 50 );
260
+ ret = pm_runtime_resume_and_get (data -> dev );
261
+ if (ret < 0 )
262
+ return ret ;
283
263
284
264
regmap_read (map , soc_data -> temp_data , & val );
285
265
286
- if (run_measurement ) {
287
- regmap_write (map , soc_data -> sensor_ctrl + REG_CLR ,
288
- soc_data -> measure_temp_mask );
289
- regmap_write (map , soc_data -> sensor_ctrl + REG_SET ,
290
- soc_data -> power_down_mask );
291
- }
292
-
293
266
if ((val & soc_data -> temp_valid_mask ) == 0 ) {
294
267
dev_dbg (& tz -> device , "temp measurement never finished\n" );
295
268
return - EAGAIN ;
@@ -328,31 +301,25 @@ static int imx_get_temp(struct thermal_zone_device *tz, int *temp)
328
301
enable_irq (data -> irq );
329
302
}
330
303
304
+ pm_runtime_put (data -> dev );
305
+
331
306
return 0 ;
332
307
}
333
308
334
309
static int imx_change_mode (struct thermal_zone_device * tz ,
335
310
enum thermal_device_mode mode )
336
311
{
337
312
struct imx_thermal_data * data = tz -> devdata ;
338
- struct regmap * map = data -> tempmon ;
339
- const struct thermal_soc_data * soc_data = data -> socdata ;
340
313
341
314
if (mode == THERMAL_DEVICE_ENABLED ) {
342
- regmap_write (map , soc_data -> sensor_ctrl + REG_CLR ,
343
- soc_data -> power_down_mask );
344
- regmap_write (map , soc_data -> sensor_ctrl + REG_SET ,
345
- soc_data -> measure_temp_mask );
315
+ pm_runtime_get (data -> dev );
346
316
347
317
if (!data -> irq_enabled ) {
348
318
data -> irq_enabled = true;
349
319
enable_irq (data -> irq );
350
320
}
351
321
} else {
352
- regmap_write (map , soc_data -> sensor_ctrl + REG_CLR ,
353
- soc_data -> measure_temp_mask );
354
- regmap_write (map , soc_data -> sensor_ctrl + REG_SET ,
355
- soc_data -> power_down_mask );
322
+ pm_runtime_put (data -> dev );
356
323
357
324
if (data -> irq_enabled ) {
358
325
disable_irq (data -> irq );
@@ -393,6 +360,11 @@ static int imx_set_trip_temp(struct thermal_zone_device *tz, int trip,
393
360
int temp )
394
361
{
395
362
struct imx_thermal_data * data = tz -> devdata ;
363
+ int ret ;
364
+
365
+ ret = pm_runtime_resume_and_get (data -> dev );
366
+ if (ret < 0 )
367
+ return ret ;
396
368
397
369
/* do not allow changing critical threshold */
398
370
if (trip == IMX_TRIP_CRITICAL )
@@ -406,6 +378,8 @@ static int imx_set_trip_temp(struct thermal_zone_device *tz, int trip,
406
378
407
379
imx_set_alarm_temp (data , temp );
408
380
381
+ pm_runtime_put (data -> dev );
382
+
409
383
return 0 ;
410
384
}
411
385
@@ -681,6 +655,8 @@ static int imx_thermal_probe(struct platform_device *pdev)
681
655
if (!data )
682
656
return - ENOMEM ;
683
657
658
+ data -> dev = & pdev -> dev ;
659
+
684
660
map = syscon_regmap_lookup_by_phandle (pdev -> dev .of_node , "fsl,tempmon" );
685
661
if (IS_ERR (map )) {
686
662
ret = PTR_ERR (map );
@@ -800,6 +776,16 @@ static int imx_thermal_probe(struct platform_device *pdev)
800
776
data -> socdata -> power_down_mask );
801
777
regmap_write (map , data -> socdata -> sensor_ctrl + REG_SET ,
802
778
data -> socdata -> measure_temp_mask );
779
+ /* After power up, we need a delay before first access can be done. */
780
+ usleep_range (20 , 50 );
781
+
782
+ /* the core was configured and enabled just before */
783
+ pm_runtime_set_active (& pdev -> dev );
784
+ pm_runtime_enable (data -> dev );
785
+
786
+ ret = pm_runtime_resume_and_get (data -> dev );
787
+ if (ret < 0 )
788
+ goto disable_runtime_pm ;
803
789
804
790
data -> irq_enabled = true;
805
791
ret = thermal_zone_device_enable (data -> tz );
@@ -814,10 +800,15 @@ static int imx_thermal_probe(struct platform_device *pdev)
814
800
goto thermal_zone_unregister ;
815
801
}
816
802
803
+ pm_runtime_put (data -> dev );
804
+
817
805
return 0 ;
818
806
819
807
thermal_zone_unregister :
820
808
thermal_zone_device_unregister (data -> tz );
809
+ disable_runtime_pm :
810
+ pm_runtime_put_noidle (data -> dev );
811
+ pm_runtime_disable (data -> dev );
821
812
clk_disable :
822
813
clk_disable_unprepare (data -> thermal_clk );
823
814
legacy_cleanup :
@@ -829,13 +820,9 @@ static int imx_thermal_probe(struct platform_device *pdev)
829
820
static int imx_thermal_remove (struct platform_device * pdev )
830
821
{
831
822
struct imx_thermal_data * data = platform_get_drvdata (pdev );
832
- struct regmap * map = data -> tempmon ;
833
823
834
- /* Disable measurements */
835
- regmap_write (map , data -> socdata -> sensor_ctrl + REG_SET ,
836
- data -> socdata -> power_down_mask );
837
- if (!IS_ERR (data -> thermal_clk ))
838
- clk_disable_unprepare (data -> thermal_clk );
824
+ pm_runtime_put_noidle (data -> dev );
825
+ pm_runtime_disable (data -> dev );
839
826
840
827
thermal_zone_device_unregister (data -> tz );
841
828
imx_thermal_unregister_legacy_cooling (data );
@@ -858,29 +845,79 @@ static int __maybe_unused imx_thermal_suspend(struct device *dev)
858
845
ret = thermal_zone_device_disable (data -> tz );
859
846
if (ret )
860
847
return ret ;
848
+
849
+ return pm_runtime_force_suspend (data -> dev );
850
+ }
851
+
852
+ static int __maybe_unused imx_thermal_resume (struct device * dev )
853
+ {
854
+ struct imx_thermal_data * data = dev_get_drvdata (dev );
855
+ int ret ;
856
+
857
+ ret = pm_runtime_force_resume (data -> dev );
858
+ if (ret )
859
+ return ret ;
860
+ /* Enabled thermal sensor after resume */
861
+ return thermal_zone_device_enable (data -> tz );
862
+ }
863
+
864
+ static int __maybe_unused imx_thermal_runtime_suspend (struct device * dev )
865
+ {
866
+ struct imx_thermal_data * data = dev_get_drvdata (dev );
867
+ const struct thermal_soc_data * socdata = data -> socdata ;
868
+ struct regmap * map = data -> tempmon ;
869
+ int ret ;
870
+
871
+ ret = regmap_write (map , socdata -> sensor_ctrl + REG_CLR ,
872
+ socdata -> measure_temp_mask );
873
+ if (ret )
874
+ return ret ;
875
+
876
+ ret = regmap_write (map , socdata -> sensor_ctrl + REG_SET ,
877
+ socdata -> power_down_mask );
878
+ if (ret )
879
+ return ret ;
880
+
861
881
clk_disable_unprepare (data -> thermal_clk );
862
882
863
883
return 0 ;
864
884
}
865
885
866
- static int __maybe_unused imx_thermal_resume (struct device * dev )
886
+ static int __maybe_unused imx_thermal_runtime_resume (struct device * dev )
867
887
{
868
888
struct imx_thermal_data * data = dev_get_drvdata (dev );
889
+ const struct thermal_soc_data * socdata = data -> socdata ;
890
+ struct regmap * map = data -> tempmon ;
869
891
int ret ;
870
892
871
893
ret = clk_prepare_enable (data -> thermal_clk );
872
894
if (ret )
873
895
return ret ;
874
- /* Enabled thermal sensor after resume */
875
- ret = thermal_zone_device_enable (data -> tz );
896
+
897
+ ret = regmap_write (map , socdata -> sensor_ctrl + REG_CLR ,
898
+ socdata -> power_down_mask );
899
+ if (ret )
900
+ return ret ;
901
+
902
+ ret = regmap_write (map , socdata -> sensor_ctrl + REG_SET ,
903
+ socdata -> measure_temp_mask );
876
904
if (ret )
877
905
return ret ;
878
906
907
+ /*
908
+ * According to the temp sensor designers, it may require up to ~17us
909
+ * to complete a measurement.
910
+ */
911
+ usleep_range (20 , 50 );
912
+
879
913
return 0 ;
880
914
}
881
915
882
- static SIMPLE_DEV_PM_OPS (imx_thermal_pm_ops ,
883
- imx_thermal_suspend , imx_thermal_resume ) ;
916
+ static const struct dev_pm_ops imx_thermal_pm_ops = {
917
+ SET_SYSTEM_SLEEP_PM_OPS (imx_thermal_suspend , imx_thermal_resume )
918
+ SET_RUNTIME_PM_OPS (imx_thermal_runtime_suspend ,
919
+ imx_thermal_runtime_resume , NULL )
920
+ };
884
921
885
922
static struct platform_driver imx_thermal = {
886
923
.driver = {
0 commit comments