brcm2708: switch to linux 4.4 and update patches
[openwrt.git] / target / linux / brcm2708 / patches-4.4 / 0045-Added-hwmon-thermal-driver-for-reporting-core-temper.patch
1 From 76ad29cef7e6dc73d7f9c3cd3578783d1daefa6d Mon Sep 17 00:00:00 2001
2 From: popcornmix <popcornmix@gmail.com>
3 Date: Tue, 26 Mar 2013 19:24:24 +0000
4 Subject: [PATCH 045/156] Added hwmon/thermal driver for reporting core
5  temperature. Thanks Dorian
6 MIME-Version: 1.0
7 Content-Type: text/plain; charset=UTF-8
8 Content-Transfer-Encoding: 8bit
9
10 BCM270x: Move thermal sensor to Device Tree
11
12 Add Device Tree support to bcm2835-thermal driver.
13 Add thermal sensor device to Device Tree.
14 Don't add platform device when booting in DT mode.
15
16 Signed-off-by: Noralf Trønnes <noralf@tronnes.org>
17 ---
18  drivers/thermal/Kconfig           |   7 ++
19  drivers/thermal/Makefile          |   1 +
20  drivers/thermal/bcm2835-thermal.c | 141 ++++++++++++++++++++++++++++++++++++++
21  3 files changed, 149 insertions(+)
22  create mode 100644 drivers/thermal/bcm2835-thermal.c
23
24 --- a/drivers/thermal/Kconfig
25 +++ b/drivers/thermal/Kconfig
26 @@ -285,6 +285,13 @@ config INTEL_POWERCLAMP
27           enforce idle time which results in more package C-state residency. The
28           user interface is exposed via generic thermal framework.
29  
30 +config THERMAL_BCM2835
31 +       depends on RASPBERRYPI_FIRMWARE
32 +       tristate "BCM2835 Thermal Driver"
33 +       help
34 +         This will enable temperature monitoring for the Broadcom BCM2835
35 +         chip. If built as a module, it will be called 'bcm2835-thermal'.
36 +
37  config X86_PKG_TEMP_THERMAL
38         tristate "X86 package temperature thermal driver"
39         depends on X86_THERMAL_VECTOR
40 --- a/drivers/thermal/Makefile
41 +++ b/drivers/thermal/Makefile
42 @@ -38,6 +38,7 @@ obj-$(CONFIG_ARMADA_THERMAL)  += armada_t
43  obj-$(CONFIG_IMX_THERMAL)      += imx_thermal.o
44  obj-$(CONFIG_DB8500_CPUFREQ_COOLING)   += db8500_cpufreq_cooling.o
45  obj-$(CONFIG_INTEL_POWERCLAMP) += intel_powerclamp.o
46 +obj-$(CONFIG_THERMAL_BCM2835)  += bcm2835-thermal.o
47  obj-$(CONFIG_X86_PKG_TEMP_THERMAL)     += x86_pkg_temp_thermal.o
48  obj-$(CONFIG_INTEL_SOC_DTS_IOSF_CORE)  += intel_soc_dts_iosf.o
49  obj-$(CONFIG_INTEL_SOC_DTS_THERMAL)    += intel_soc_dts_thermal.o
50 --- /dev/null
51 +++ b/drivers/thermal/bcm2835-thermal.c
52 @@ -0,0 +1,141 @@
53 +/*****************************************************************************
54 +* Copyright 2011 Broadcom Corporation.  All rights reserved.
55 +*
56 +* Unless you and Broadcom execute a separate written software license
57 +* agreement governing use of this software, this software is licensed to you
58 +* under the terms of the GNU General Public License version 2, available at
59 +* http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
60 +*
61 +* Notwithstanding the above, under no circumstances may you combine this
62 +* software in any way with any other Broadcom software provided under a
63 +* license other than the GPL, without Broadcom's express prior written
64 +* consent.
65 +*****************************************************************************/
66 +
67 +#include <linux/module.h>
68 +#include <linux/platform_device.h>
69 +#include <linux/thermal.h>
70 +#include <soc/bcm2835/raspberrypi-firmware.h>
71 +
72 +static int bcm2835_thermal_get_property(struct thermal_zone_device *tz,
73 +                                       int *temp, u32 tag)
74 +{
75 +       struct rpi_firmware *fw = tz->devdata;
76 +       struct {
77 +               u32 id;
78 +               u32 val;
79 +       } packet;
80 +       int ret;
81 +
82 +       *temp = 0;
83 +       packet.id = 0;
84 +       ret = rpi_firmware_property(fw, tag, &packet, sizeof(packet));
85 +       if (ret) {
86 +               dev_err(&tz->device, "Failed to get temperature\n");
87 +               return ret;
88 +       }
89 +
90 +       *temp = packet.val;
91 +       dev_dbg(&tz->device, "%stemp=%d\n",
92 +               tag == RPI_FIRMWARE_GET_MAX_TEMPERATURE ? "max" : "", *temp);
93 +
94 +       return 0;
95 +}
96 +
97 +static int bcm2835_thermal_get_temp(struct thermal_zone_device *tz,
98 +                                   int *temp)
99 +{
100 +       return bcm2835_thermal_get_property(tz, temp,
101 +                                           RPI_FIRMWARE_GET_TEMPERATURE);
102 +}
103 +
104 +static int bcm2835_thermal_get_max_temp(struct thermal_zone_device *tz,
105 +                                       int trip, int *temp)
106 +{
107 +       /*
108 +        * The maximum safe temperature of the SoC.
109 +        * Overclock may be disabled above this temperature.
110 +        */
111 +       return bcm2835_thermal_get_property(tz, temp,
112 +                                           RPI_FIRMWARE_GET_MAX_TEMPERATURE);
113 +}
114 +
115 +static int bcm2835_thermal_get_trip_type(struct thermal_zone_device *tz,
116 +                                        int trip, enum thermal_trip_type *type)
117 +{
118 +       *type = THERMAL_TRIP_HOT;
119 +
120 +       return 0;
121 +}
122 +
123 +static int bcm2835_thermal_get_mode(struct thermal_zone_device *tz,
124 +                                   enum thermal_device_mode *mode)
125 +{
126 +       *mode = THERMAL_DEVICE_ENABLED;
127 +
128 +       return 0;
129 +}
130 +
131 +static struct thermal_zone_device_ops ops  = {
132 +       .get_temp = bcm2835_thermal_get_temp,
133 +       .get_trip_temp = bcm2835_thermal_get_max_temp,
134 +       .get_trip_type = bcm2835_thermal_get_trip_type,
135 +       .get_mode = bcm2835_thermal_get_mode,
136 +};
137 +
138 +static int bcm2835_thermal_probe(struct platform_device *pdev)
139 +{
140 +       struct device_node *fw_np;
141 +       struct rpi_firmware *fw;
142 +       struct thermal_zone_device *tz;
143 +
144 +       fw_np = of_parse_phandle(pdev->dev.of_node, "firmware", 0);
145 +/* Remove comment when booting without Device Tree is no longer supported
146 +       if (!fw_np) {
147 +               dev_err(&pdev->dev, "Missing firmware node\n");
148 +               return -ENOENT;
149 +       }
150 +*/
151 +       fw = rpi_firmware_get(fw_np);
152 +       if (!fw)
153 +               return -EPROBE_DEFER;
154 +
155 +       tz = thermal_zone_device_register("bcm2835_thermal", 1, 0, fw, &ops,
156 +                                         NULL, 0, 0);
157 +       if (IS_ERR(tz)) {
158 +               dev_err(&pdev->dev, "Failed to register the thermal device\n");
159 +               return PTR_ERR(tz);
160 +       }
161 +
162 +       platform_set_drvdata(pdev, tz);
163 +
164 +       return 0;
165 +}
166 +
167 +static int bcm2835_thermal_remove(struct platform_device *pdev)
168 +{
169 +       thermal_zone_device_unregister(platform_get_drvdata(pdev));
170 +
171 +       return 0;
172 +}
173 +
174 +static const struct of_device_id bcm2835_thermal_of_match_table[] = {
175 +       { .compatible = "brcm,bcm2835-thermal", },
176 +       {},
177 +};
178 +MODULE_DEVICE_TABLE(of, bcm2835_thermal_of_match_table);
179 +
180 +static struct platform_driver bcm2835_thermal_driver = {
181 +       .probe = bcm2835_thermal_probe,
182 +       .remove = bcm2835_thermal_remove,
183 +       .driver = {
184 +               .name = "bcm2835_thermal",
185 +               .of_match_table = bcm2835_thermal_of_match_table,
186 +       },
187 +};
188 +module_platform_driver(bcm2835_thermal_driver);
189 +
190 +MODULE_AUTHOR("Dorian Peake");
191 +MODULE_AUTHOR("Noralf Trønnes");
192 +MODULE_DESCRIPTION("Thermal driver for bcm2835 chip");
193 +MODULE_LICENSE("GPL");