86a3270712c27d87d5c5678c9006229fc64b6a71
[openwrt/openwrt.git] / target / linux / brcm2708 / patches-4.1 / 0018-Added-hwmon-thermal-driver-for-reporting-core-temper.patch
1 From 40d8a3f8af68f312479b38b0e704b4fe57c97f4c 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 018/148] 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 arch/arm/mach-bcm2708/bcm2708.c | 6 ++
19 arch/arm/mach-bcm2709/bcm2709.c | 6 ++
20 drivers/thermal/Kconfig | 7 ++
21 drivers/thermal/Makefile | 1 +
22 drivers/thermal/bcm2835-thermal.c | 190 ++++++++++++++++++++++++++++++++++++++
23 5 files changed, 210 insertions(+)
24 create mode 100644 drivers/thermal/bcm2835-thermal.c
25
26 --- a/arch/arm/mach-bcm2708/bcm2708.c
27 +++ b/arch/arm/mach-bcm2708/bcm2708.c
28 @@ -505,6 +505,10 @@ static struct platform_device bcm2708_al
29 },
30 };
31
32 +static struct platform_device bcm2835_thermal_device = {
33 + .name = "bcm2835_thermal",
34 +};
35 +
36 int __init bcm_register_device(struct platform_device *pdev)
37 {
38 int ret;
39 @@ -651,6 +655,8 @@ void __init bcm2708_init(void)
40 for (i = 0; i < ARRAY_SIZE(bcm2708_alsa_devices); i++)
41 bcm_register_device_dt(&bcm2708_alsa_devices[i]);
42
43 + bcm_register_device_dt(&bcm2835_thermal_device);
44 +
45 if (!use_dt) {
46 for (i = 0; i < ARRAY_SIZE(amba_devs); i++) {
47 struct amba_device *d = amba_devs[i];
48 --- a/arch/arm/mach-bcm2709/bcm2709.c
49 +++ b/arch/arm/mach-bcm2709/bcm2709.c
50 @@ -525,6 +525,10 @@ static struct platform_device bcm2708_al
51 },
52 };
53
54 +static struct platform_device bcm2835_thermal_device = {
55 + .name = "bcm2835_thermal",
56 +};
57 +
58 int __init bcm_register_device(struct platform_device *pdev)
59 {
60 int ret;
61 @@ -671,6 +675,8 @@ void __init bcm2709_init(void)
62 for (i = 0; i < ARRAY_SIZE(bcm2708_alsa_devices); i++)
63 bcm_register_device_dt(&bcm2708_alsa_devices[i]);
64
65 + bcm_register_device_dt(&bcm2835_thermal_device);
66 +
67 if (!use_dt) {
68 for (i = 0; i < ARRAY_SIZE(amba_devs); i++) {
69 struct amba_device *d = amba_devs[i];
70 --- a/drivers/thermal/Kconfig
71 +++ b/drivers/thermal/Kconfig
72 @@ -238,6 +238,13 @@ config INTEL_POWERCLAMP
73 enforce idle time which results in more package C-state residency. The
74 user interface is exposed via generic thermal framework.
75
76 +config THERMAL_BCM2835
77 + depends on BCM2708_MBOX
78 + tristate "BCM2835 Thermal Driver"
79 + help
80 + This will enable temperature monitoring for the Broadcom BCM2835
81 + chip. If built as a module, it will be called 'bcm2835-thermal'.
82 +
83 config X86_PKG_TEMP_THERMAL
84 tristate "X86 package temperature thermal driver"
85 depends on X86_THERMAL_VECTOR
86 --- a/drivers/thermal/Makefile
87 +++ b/drivers/thermal/Makefile
88 @@ -33,6 +33,7 @@ obj-$(CONFIG_ARMADA_THERMAL) += armada_t
89 obj-$(CONFIG_IMX_THERMAL) += imx_thermal.o
90 obj-$(CONFIG_DB8500_CPUFREQ_COOLING) += db8500_cpufreq_cooling.o
91 obj-$(CONFIG_INTEL_POWERCLAMP) += intel_powerclamp.o
92 +obj-$(CONFIG_THERMAL_BCM2835) += bcm2835-thermal.o
93 obj-$(CONFIG_X86_PKG_TEMP_THERMAL) += x86_pkg_temp_thermal.o
94 obj-$(CONFIG_INTEL_SOC_DTS_THERMAL) += intel_soc_dts_thermal.o
95 obj-$(CONFIG_TI_SOC_THERMAL) += ti-soc-thermal/
96 --- /dev/null
97 +++ b/drivers/thermal/bcm2835-thermal.c
98 @@ -0,0 +1,190 @@
99 +/*****************************************************************************
100 +* Copyright 2011 Broadcom Corporation. All rights reserved.
101 +*
102 +* Unless you and Broadcom execute a separate written software license
103 +* agreement governing use of this software, this software is licensed to you
104 +* under the terms of the GNU General Public License version 2, available at
105 +* http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
106 +*
107 +* Notwithstanding the above, under no circumstances may you combine this
108 +* software in any way with any other Broadcom software provided under a
109 +* license other than the GPL, without Broadcom's express prior written
110 +* consent.
111 +*****************************************************************************/
112 +
113 +#include <linux/kernel.h>
114 +#include <linux/module.h>
115 +#include <linux/init.h>
116 +#include <linux/platform_data/mailbox-bcm2708.h>
117 +#include <linux/platform_device.h>
118 +#include <linux/slab.h>
119 +#include <linux/sysfs.h>
120 +#include <linux/thermal.h>
121 +
122 +
123 +/* --- DEFINITIONS --- */
124 +#define MODULE_NAME "bcm2835_thermal"
125 +
126 +/*#define THERMAL_DEBUG_ENABLE*/
127 +
128 +#ifdef THERMAL_DEBUG_ENABLE
129 +#define print_debug(fmt,...) printk(KERN_INFO "%s:%s:%d: "fmt"\n", MODULE_NAME, __func__, __LINE__, ##__VA_ARGS__)
130 +#else
131 +#define print_debug(fmt,...)
132 +#endif
133 +#define print_err(fmt,...) printk(KERN_ERR "%s:%s:%d: "fmt"\n", MODULE_NAME, __func__,__LINE__, ##__VA_ARGS__)
134 +
135 +#define VC_TAG_GET_TEMP 0x00030006
136 +#define VC_TAG_GET_MAX_TEMP 0x0003000A
137 +
138 +typedef enum {
139 + TEMP,
140 + MAX_TEMP,
141 +} temp_type;
142 +
143 +/* --- STRUCTS --- */
144 +/* tag part of the message */
145 +struct vc_msg_tag {
146 + uint32_t tag_id; /* the tag ID for the temperature */
147 + uint32_t buffer_size; /* size of the buffer (should be 8) */
148 + uint32_t request_code; /* identifies message as a request (should be 0) */
149 + uint32_t id; /* extra ID field (should be 0) */
150 + uint32_t val; /* returned value of the temperature */
151 +};
152 +
153 +/* message structure to be sent to videocore */
154 +struct vc_msg {
155 + uint32_t msg_size; /* simply, sizeof(struct vc_msg) */
156 + uint32_t request_code; /* holds various information like the success and number of bytes returned (refer to mailboxes wiki) */
157 + struct vc_msg_tag tag; /* the tag structure above to make */
158 + uint32_t end_tag; /* an end identifier, should be set to NULL */
159 +};
160 +
161 +struct bcm2835_thermal_data {
162 + struct thermal_zone_device *thermal_dev;
163 + struct vc_msg msg;
164 +};
165 +
166 +/* --- GLOBALS --- */
167 +static struct bcm2835_thermal_data bcm2835_data;
168 +
169 +/* Thermal Device Operations */
170 +static struct thermal_zone_device_ops ops;
171 +
172 +/* --- FUNCTIONS --- */
173 +
174 +static int bcm2835_get_temp_or_max(struct thermal_zone_device *thermal_dev, unsigned long *temp, unsigned tag_id)
175 +{
176 + int result = -1, retry = 3;
177 + print_debug("IN");
178 +
179 + *temp = 0;
180 + while (result != 0 && retry-- > 0) {
181 + /* wipe all previous message data */
182 + memset(&bcm2835_data.msg, 0, sizeof bcm2835_data.msg);
183 +
184 + /* prepare message */
185 + bcm2835_data.msg.msg_size = sizeof bcm2835_data.msg;
186 + bcm2835_data.msg.tag.buffer_size = 8;
187 + bcm2835_data.msg.tag.tag_id = tag_id;
188 +
189 + /* send the message */
190 + result = bcm_mailbox_property(&bcm2835_data.msg, sizeof bcm2835_data.msg);
191 + print_debug("Got %stemperature as %u (%d,%x)\n", tag_id==VC_TAG_GET_MAX_TEMP ? "max ":"", (uint)bcm2835_data.msg.tag.val, result, bcm2835_data.msg.request_code);
192 + if (!(bcm2835_data.msg.request_code & 0x80000000))
193 + result = -1;
194 + }
195 +
196 + /* check if it was all ok and return the rate in milli degrees C */
197 + if (result == 0)
198 + *temp = (uint)bcm2835_data.msg.tag.val;
199 + else
200 + print_err("Failed to get temperature! (%x:%d)\n", tag_id, result);
201 + print_debug("OUT");
202 + return result;
203 +}
204 +
205 +static int bcm2835_get_temp(struct thermal_zone_device *thermal_dev, unsigned long *temp)
206 +{
207 + return bcm2835_get_temp_or_max(thermal_dev, temp, VC_TAG_GET_TEMP);
208 +}
209 +
210 +static int bcm2835_get_max_temp(struct thermal_zone_device *thermal_dev, int trip_num, unsigned long *temp)
211 +{
212 + return bcm2835_get_temp_or_max(thermal_dev, temp, VC_TAG_GET_MAX_TEMP);
213 +}
214 +
215 +static int bcm2835_get_trip_type(struct thermal_zone_device * thermal_dev, int trip_num, enum thermal_trip_type *trip_type)
216 +{
217 + *trip_type = THERMAL_TRIP_HOT;
218 + return 0;
219 +}
220 +
221 +
222 +static int bcm2835_get_mode(struct thermal_zone_device *thermal_dev, enum thermal_device_mode *dev_mode)
223 +{
224 + *dev_mode = THERMAL_DEVICE_ENABLED;
225 + return 0;
226 +}
227 +
228 +
229 +static int bcm2835_thermal_probe(struct platform_device *pdev)
230 +{
231 + print_debug("IN");
232 + print_debug("THERMAL Driver has been probed!");
233 +
234 + /* check that the device isn't null!*/
235 + if(pdev == NULL)
236 + {
237 + print_debug("Platform device is empty!");
238 + return -ENODEV;
239 + }
240 +
241 + if(!(bcm2835_data.thermal_dev = thermal_zone_device_register("bcm2835_thermal", 1, 0, NULL, &ops, NULL, 0, 0)))
242 + {
243 + print_debug("Unable to register the thermal device!");
244 + return -EFAULT;
245 + }
246 + return 0;
247 +}
248 +
249 +
250 +static int bcm2835_thermal_remove(struct platform_device *pdev)
251 +{
252 + print_debug("IN");
253 +
254 + thermal_zone_device_unregister(bcm2835_data.thermal_dev);
255 +
256 + print_debug("OUT");
257 +
258 + return 0;
259 +}
260 +
261 +static struct thermal_zone_device_ops ops = {
262 + .get_temp = bcm2835_get_temp,
263 + .get_trip_temp = bcm2835_get_max_temp,
264 + .get_trip_type = bcm2835_get_trip_type,
265 + .get_mode = bcm2835_get_mode,
266 +};
267 +
268 +static const struct of_device_id bcm2835_thermal_of_match_table[] = {
269 + { .compatible = "brcm,bcm2835-thermal", },
270 + {},
271 +};
272 +MODULE_DEVICE_TABLE(of, bcm2835_thermal_of_match_table);
273 +
274 +static struct platform_driver bcm2835_thermal_driver = {
275 + .probe = bcm2835_thermal_probe,
276 + .remove = bcm2835_thermal_remove,
277 + .driver = {
278 + .name = "bcm2835_thermal",
279 + .owner = THIS_MODULE,
280 + .of_match_table = bcm2835_thermal_of_match_table,
281 + },
282 +};
283 +
284 +MODULE_LICENSE("GPL");
285 +MODULE_AUTHOR("Dorian Peake");
286 +MODULE_DESCRIPTION("Thermal driver for bcm2835 chip");
287 +
288 +module_platform_driver(bcm2835_thermal_driver);