hc
2024-12-19 9370bb92b2d16684ee45cf24e879c93c509162da
kernel/drivers/thermal/imx_thermal.c
....@@ -3,25 +3,19 @@
33 // Copyright 2013 Freescale Semiconductor, Inc.
44
55 #include <linux/clk.h>
6
-#include <linux/cpu.h>
76 #include <linux/cpufreq.h>
87 #include <linux/cpu_cooling.h>
98 #include <linux/delay.h>
10
-#include <linux/device.h>
11
-#include <linux/init.h>
129 #include <linux/interrupt.h>
1310 #include <linux/io.h>
14
-#include <linux/kernel.h>
1511 #include <linux/mfd/syscon.h>
1612 #include <linux/module.h>
1713 #include <linux/of.h>
1814 #include <linux/of_device.h>
19
-#include <linux/platform_device.h>
2015 #include <linux/regmap.h>
21
-#include <linux/slab.h>
2216 #include <linux/thermal.h>
23
-#include <linux/types.h>
2417 #include <linux/nvmem-consumer.h>
18
+#include <linux/pm_runtime.h>
2519
2620 #define REG_SET 0x4
2721 #define REG_CLR 0x8
....@@ -201,10 +195,10 @@
201195 };
202196
203197 struct imx_thermal_data {
198
+ struct device *dev;
204199 struct cpufreq_policy *policy;
205200 struct thermal_zone_device *tz;
206201 struct thermal_cooling_device *cdev;
207
- enum thermal_device_mode mode;
208202 struct regmap *tempmon;
209203 u32 c1, c2; /* See formula in imx_init_calib() */
210204 int temp_passive;
....@@ -260,42 +254,14 @@
260254 const struct thermal_soc_data *soc_data = data->socdata;
261255 struct regmap *map = data->tempmon;
262256 unsigned int n_meas;
263
- bool wait;
264257 u32 val;
258
+ int ret;
265259
266
- if (data->mode == THERMAL_DEVICE_ENABLED) {
267
- /* Check if a measurement is currently in progress */
268
- regmap_read(map, soc_data->temp_data, &val);
269
- wait = !(val & soc_data->temp_valid_mask);
270
- } else {
271
- /*
272
- * Every time we measure the temperature, we will power on the
273
- * temperature sensor, enable measurements, take a reading,
274
- * disable measurements, power off the temperature sensor.
275
- */
276
- regmap_write(map, soc_data->sensor_ctrl + REG_CLR,
277
- soc_data->power_down_mask);
278
- regmap_write(map, soc_data->sensor_ctrl + REG_SET,
279
- soc_data->measure_temp_mask);
280
-
281
- wait = true;
282
- }
283
-
284
- /*
285
- * According to the temp sensor designers, it may require up to ~17us
286
- * to complete a measurement.
287
- */
288
- if (wait)
289
- usleep_range(20, 50);
260
+ ret = pm_runtime_resume_and_get(data->dev);
261
+ if (ret < 0)
262
+ return ret;
290263
291264 regmap_read(map, soc_data->temp_data, &val);
292
-
293
- if (data->mode != THERMAL_DEVICE_ENABLED) {
294
- regmap_write(map, soc_data->sensor_ctrl + REG_CLR,
295
- soc_data->measure_temp_mask);
296
- regmap_write(map, soc_data->sensor_ctrl + REG_SET,
297
- soc_data->power_down_mask);
298
- }
299265
300266 if ((val & soc_data->temp_valid_mask) == 0) {
301267 dev_dbg(&tz->device, "temp measurement never finished\n");
....@@ -335,56 +301,31 @@
335301 enable_irq(data->irq);
336302 }
337303
338
- return 0;
339
-}
340
-
341
-static int imx_get_mode(struct thermal_zone_device *tz,
342
- enum thermal_device_mode *mode)
343
-{
344
- struct imx_thermal_data *data = tz->devdata;
345
-
346
- *mode = data->mode;
304
+ pm_runtime_put(data->dev);
347305
348306 return 0;
349307 }
350308
351
-static int imx_set_mode(struct thermal_zone_device *tz,
352
- enum thermal_device_mode mode)
309
+static int imx_change_mode(struct thermal_zone_device *tz,
310
+ enum thermal_device_mode mode)
353311 {
354312 struct imx_thermal_data *data = tz->devdata;
355
- struct regmap *map = data->tempmon;
356
- const struct thermal_soc_data *soc_data = data->socdata;
357313
358314 if (mode == THERMAL_DEVICE_ENABLED) {
359
- tz->polling_delay = IMX_POLLING_DELAY;
360
- tz->passive_delay = IMX_PASSIVE_DELAY;
361
-
362
- regmap_write(map, soc_data->sensor_ctrl + REG_CLR,
363
- soc_data->power_down_mask);
364
- regmap_write(map, soc_data->sensor_ctrl + REG_SET,
365
- soc_data->measure_temp_mask);
315
+ pm_runtime_get(data->dev);
366316
367317 if (!data->irq_enabled) {
368318 data->irq_enabled = true;
369319 enable_irq(data->irq);
370320 }
371321 } else {
372
- regmap_write(map, soc_data->sensor_ctrl + REG_CLR,
373
- soc_data->measure_temp_mask);
374
- regmap_write(map, soc_data->sensor_ctrl + REG_SET,
375
- soc_data->power_down_mask);
376
-
377
- tz->polling_delay = 0;
378
- tz->passive_delay = 0;
322
+ pm_runtime_put(data->dev);
379323
380324 if (data->irq_enabled) {
381325 disable_irq(data->irq);
382326 data->irq_enabled = false;
383327 }
384328 }
385
-
386
- data->mode = mode;
387
- thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
388329
389330 return 0;
390331 }
....@@ -419,6 +360,11 @@
419360 int temp)
420361 {
421362 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;
422368
423369 /* do not allow changing critical threshold */
424370 if (trip == IMX_TRIP_CRITICAL)
....@@ -431,6 +377,8 @@
431377 data->temp_passive = temp;
432378
433379 imx_set_alarm_temp(data, temp);
380
+
381
+ pm_runtime_put(data->dev);
434382
435383 return 0;
436384 }
....@@ -474,8 +422,7 @@
474422 .bind = imx_bind,
475423 .unbind = imx_unbind,
476424 .get_temp = imx_get_temp,
477
- .get_mode = imx_get_mode,
478
- .set_mode = imx_set_mode,
425
+ .change_mode = imx_change_mode,
479426 .get_trip_type = imx_get_trip_type,
480427 .get_trip_temp = imx_get_trip_temp,
481428 .get_crit_temp = imx_get_crit_temp,
....@@ -648,26 +595,54 @@
648595 };
649596 MODULE_DEVICE_TABLE(of, of_imx_thermal_match);
650597
598
+#ifdef CONFIG_CPU_FREQ
651599 /*
652600 * Create cooling device in case no #cooling-cells property is available in
653601 * CPU node
654602 */
655603 static int imx_thermal_register_legacy_cooling(struct imx_thermal_data *data)
656604 {
657
- struct device_node *np = of_get_cpu_node(data->policy->cpu, NULL);
658
- int ret;
605
+ struct device_node *np;
606
+ int ret = 0;
607
+
608
+ data->policy = cpufreq_cpu_get(0);
609
+ if (!data->policy) {
610
+ pr_debug("%s: CPUFreq policy not found\n", __func__);
611
+ return -EPROBE_DEFER;
612
+ }
613
+
614
+ np = of_get_cpu_node(data->policy->cpu, NULL);
659615
660616 if (!np || !of_find_property(np, "#cooling-cells", NULL)) {
661617 data->cdev = cpufreq_cooling_register(data->policy);
662618 if (IS_ERR(data->cdev)) {
663619 ret = PTR_ERR(data->cdev);
664620 cpufreq_cpu_put(data->policy);
665
- return ret;
666621 }
667622 }
668623
624
+ of_node_put(np);
625
+
626
+ return ret;
627
+}
628
+
629
+static void imx_thermal_unregister_legacy_cooling(struct imx_thermal_data *data)
630
+{
631
+ cpufreq_cooling_unregister(data->cdev);
632
+ cpufreq_cpu_put(data->policy);
633
+}
634
+
635
+#else
636
+
637
+static inline int imx_thermal_register_legacy_cooling(struct imx_thermal_data *data)
638
+{
669639 return 0;
670640 }
641
+
642
+static inline void imx_thermal_unregister_legacy_cooling(struct imx_thermal_data *data)
643
+{
644
+}
645
+#endif
671646
672647 static int imx_thermal_probe(struct platform_device *pdev)
673648 {
....@@ -679,6 +654,8 @@
679654 data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
680655 if (!data)
681656 return -ENOMEM;
657
+
658
+ data->dev = &pdev->dev;
682659
683660 map = syscon_regmap_lookup_by_phandle(pdev->dev.of_node, "fsl,tempmon");
684661 if (IS_ERR(map)) {
....@@ -715,17 +692,13 @@
715692
716693 if (of_find_property(pdev->dev.of_node, "nvmem-cells", NULL)) {
717694 ret = imx_init_from_nvmem_cells(pdev);
718
- if (ret == -EPROBE_DEFER)
719
- return ret;
720
- if (ret) {
721
- dev_err(&pdev->dev, "failed to init from nvmem: %d\n",
722
- ret);
723
- return ret;
724
- }
695
+ if (ret)
696
+ return dev_err_probe(&pdev->dev, ret,
697
+ "failed to init from nvmem\n");
725698 } else {
726699 ret = imx_init_from_tempmon_data(pdev);
727700 if (ret) {
728
- dev_err(&pdev->dev, "failed to init from from fsl,tempmon-data\n");
701
+ dev_err(&pdev->dev, "failed to init from fsl,tempmon-data\n");
729702 return ret;
730703 }
731704 }
....@@ -743,18 +716,10 @@
743716 regmap_write(map, data->socdata->sensor_ctrl + REG_SET,
744717 data->socdata->power_down_mask);
745718
746
- data->policy = cpufreq_cpu_get(0);
747
- if (!data->policy) {
748
- pr_debug("%s: CPUFreq policy not found\n", __func__);
749
- return -EPROBE_DEFER;
750
- }
751
-
752719 ret = imx_thermal_register_legacy_cooling(data);
753
- if (ret) {
754
- dev_err(&pdev->dev,
755
- "failed to register cpufreq cooling device: %d\n", ret);
756
- return ret;
757
- }
720
+ if (ret)
721
+ return dev_err_probe(&pdev->dev, ret,
722
+ "failed to register cpufreq cooling device\n");
758723
759724 data->thermal_clk = devm_clk_get(&pdev->dev, NULL);
760725 if (IS_ERR(data->thermal_clk)) {
....@@ -762,9 +727,7 @@
762727 if (ret != -EPROBE_DEFER)
763728 dev_err(&pdev->dev,
764729 "failed to get thermal clk: %d\n", ret);
765
- cpufreq_cooling_unregister(data->cdev);
766
- cpufreq_cpu_put(data->policy);
767
- return ret;
730
+ goto legacy_cleanup;
768731 }
769732
770733 /*
....@@ -777,9 +740,7 @@
777740 ret = clk_prepare_enable(data->thermal_clk);
778741 if (ret) {
779742 dev_err(&pdev->dev, "failed to enable thermal clk: %d\n", ret);
780
- cpufreq_cooling_unregister(data->cdev);
781
- cpufreq_cpu_put(data->policy);
782
- return ret;
743
+ goto legacy_cleanup;
783744 }
784745
785746 data->tz = thermal_zone_device_register("imx_thermal_zone",
....@@ -792,10 +753,7 @@
792753 ret = PTR_ERR(data->tz);
793754 dev_err(&pdev->dev,
794755 "failed to register thermal zone device %d\n", ret);
795
- clk_disable_unprepare(data->thermal_clk);
796
- cpufreq_cooling_unregister(data->cdev);
797
- cpufreq_cpu_put(data->policy);
798
- return ret;
756
+ goto clk_disable;
799757 }
800758
801759 dev_info(&pdev->dev, "%s CPU temperature grade - max:%dC"
....@@ -818,87 +776,148 @@
818776 data->socdata->power_down_mask);
819777 regmap_write(map, data->socdata->sensor_ctrl + REG_SET,
820778 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;
821789
822790 data->irq_enabled = true;
823
- data->mode = THERMAL_DEVICE_ENABLED;
791
+ ret = thermal_zone_device_enable(data->tz);
792
+ if (ret)
793
+ goto thermal_zone_unregister;
824794
825795 ret = devm_request_threaded_irq(&pdev->dev, data->irq,
826796 imx_thermal_alarm_irq, imx_thermal_alarm_irq_thread,
827797 0, "imx_thermal", data);
828798 if (ret < 0) {
829799 dev_err(&pdev->dev, "failed to request alarm irq: %d\n", ret);
830
- clk_disable_unprepare(data->thermal_clk);
831
- thermal_zone_device_unregister(data->tz);
832
- cpufreq_cooling_unregister(data->cdev);
833
- cpufreq_cpu_put(data->policy);
834
- return ret;
800
+ goto thermal_zone_unregister;
835801 }
836802
803
+ pm_runtime_put(data->dev);
804
+
837805 return 0;
806
+
807
+thermal_zone_unregister:
808
+ thermal_zone_device_unregister(data->tz);
809
+disable_runtime_pm:
810
+ pm_runtime_put_noidle(data->dev);
811
+ pm_runtime_disable(data->dev);
812
+clk_disable:
813
+ clk_disable_unprepare(data->thermal_clk);
814
+legacy_cleanup:
815
+ imx_thermal_unregister_legacy_cooling(data);
816
+
817
+ return ret;
838818 }
839819
840820 static int imx_thermal_remove(struct platform_device *pdev)
841821 {
842822 struct imx_thermal_data *data = platform_get_drvdata(pdev);
843
- struct regmap *map = data->tempmon;
844823
845
- /* Disable measurements */
846
- regmap_write(map, data->socdata->sensor_ctrl + REG_SET,
847
- data->socdata->power_down_mask);
848
- if (!IS_ERR(data->thermal_clk))
849
- clk_disable_unprepare(data->thermal_clk);
824
+ pm_runtime_put_noidle(data->dev);
825
+ pm_runtime_disable(data->dev);
850826
851827 thermal_zone_device_unregister(data->tz);
852
- cpufreq_cooling_unregister(data->cdev);
853
- cpufreq_cpu_put(data->policy);
828
+ imx_thermal_unregister_legacy_cooling(data);
854829
855830 return 0;
856831 }
857832
858
-#ifdef CONFIG_PM_SLEEP
859
-static int imx_thermal_suspend(struct device *dev)
833
+static int __maybe_unused imx_thermal_suspend(struct device *dev)
860834 {
861835 struct imx_thermal_data *data = dev_get_drvdata(dev);
862
- struct regmap *map = data->tempmon;
836
+ int ret;
863837
864838 /*
865839 * Need to disable thermal sensor, otherwise, when thermal core
866840 * try to get temperature before thermal sensor resume, a wrong
867841 * temperature will be read as the thermal sensor is powered
868
- * down.
842
+ * down. This is done in change_mode() operation called from
843
+ * thermal_zone_device_disable()
869844 */
870
- regmap_write(map, data->socdata->sensor_ctrl + REG_CLR,
871
- data->socdata->measure_temp_mask);
872
- regmap_write(map, data->socdata->sensor_ctrl + REG_SET,
873
- data->socdata->power_down_mask);
874
- data->mode = THERMAL_DEVICE_DISABLED;
845
+ ret = thermal_zone_device_disable(data->tz);
846
+ if (ret)
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
+
875881 clk_disable_unprepare(data->thermal_clk);
876882
877883 return 0;
878884 }
879885
880
-static int imx_thermal_resume(struct device *dev)
886
+static int __maybe_unused imx_thermal_runtime_resume(struct device *dev)
881887 {
882888 struct imx_thermal_data *data = dev_get_drvdata(dev);
889
+ const struct thermal_soc_data *socdata = data->socdata;
883890 struct regmap *map = data->tempmon;
884891 int ret;
885892
886893 ret = clk_prepare_enable(data->thermal_clk);
887894 if (ret)
888895 return ret;
889
- /* Enabled thermal sensor after resume */
890
- regmap_write(map, data->socdata->sensor_ctrl + REG_CLR,
891
- data->socdata->power_down_mask);
892
- regmap_write(map, data->socdata->sensor_ctrl + REG_SET,
893
- data->socdata->measure_temp_mask);
894
- data->mode = THERMAL_DEVICE_ENABLED;
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);
904
+ if (ret)
905
+ return ret;
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);
895912
896913 return 0;
897914 }
898
-#endif
899915
900
-static SIMPLE_DEV_PM_OPS(imx_thermal_pm_ops,
901
- 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
+};
902921
903922 static struct platform_driver imx_thermal = {
904923 .driver = {