2 +++ b/drivers/thermal/bcm2835-thermal.c
4 +/*****************************************************************************
5 +* Copyright 2011 Broadcom Corporation. All rights reserved.
7 +* Unless you and Broadcom execute a separate written software license
8 +* agreement governing use of this software, this software is licensed to you
9 +* under the terms of the GNU General Public License version 2, available at
10 +* http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
12 +* Notwithstanding the above, under no circumstances may you combine this
13 +* software in any way with any other Broadcom software provided under a
14 +* license other than the GPL, without Broadcom's express prior written
16 +*****************************************************************************/
18 +#include <linux/kernel.h>
19 +#include <linux/module.h>
20 +#include <linux/init.h>
21 +#include <linux/platform_device.h>
22 +#include <linux/slab.h>
23 +#include <linux/sysfs.h>
24 +#include <mach/vcio.h>
25 +#include <linux/thermal.h>
28 +/* --- DEFINITIONS --- */
29 +#define MODULE_NAME "bcm2835_thermal"
31 +/*#define THERMAL_DEBUG_ENABLE*/
33 +#ifdef THERMAL_DEBUG_ENABLE
34 +#define print_debug(fmt,...) printk(KERN_INFO "%s:%s:%d: "fmt"\n", MODULE_NAME, __func__, __LINE__, ##__VA_ARGS__)
36 +#define print_debug(fmt,...)
38 +#define print_err(fmt,...) printk(KERN_ERR "%s:%s:%d: "fmt"\n", MODULE_NAME, __func__,__LINE__, ##__VA_ARGS__)
40 +#define VC_TAG_GET_TEMP 0x00030006
41 +#define VC_TAG_GET_MAX_TEMP 0x0003000A
48 +/* --- STRUCTS --- */
49 +/* tag part of the message */
51 + uint32_t tag_id; /* the tag ID for the temperature */
52 + uint32_t buffer_size; /* size of the buffer (should be 8) */
53 + uint32_t request_code; /* identifies message as a request (should be 0) */
54 + uint32_t id; /* extra ID field (should be 0) */
55 + uint32_t val; /* returned value of the temperature */
58 +/* message structure to be sent to videocore */
60 + uint32_t msg_size; /* simply, sizeof(struct vc_msg) */
61 + uint32_t request_code; /* holds various information like the success and number of bytes returned (refer to mailboxes wiki) */
62 + struct vc_msg_tag tag; /* the tag structure above to make */
63 + uint32_t end_tag; /* an end identifier, should be set to NULL */
66 +struct bcm2835_thermal_data {
67 + struct thermal_zone_device *thermal_dev;
71 +/* --- GLOBALS --- */
72 +static struct bcm2835_thermal_data bcm2835_data;
74 +/* Thermal Device Operations */
75 +static struct thermal_zone_device_ops ops;
77 +/* --- FUNCTIONS --- */
79 +static int bcm2835_get_temp_or_max(struct thermal_zone_device *thermal_dev, unsigned long *temp, unsigned tag_id)
81 + int result = -1, retry = 3;
85 + while (result != 0 && retry-- > 0) {
86 + /* wipe all previous message data */
87 + memset(&bcm2835_data.msg, 0, sizeof bcm2835_data.msg);
89 + /* prepare message */
90 + bcm2835_data.msg.msg_size = sizeof bcm2835_data.msg;
91 + bcm2835_data.msg.tag.buffer_size = 8;
92 + bcm2835_data.msg.tag.tag_id = tag_id;
94 + /* send the message */
95 + result = bcm_mailbox_property(&bcm2835_data.msg, sizeof bcm2835_data.msg);
96 + 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);
97 + if (!(bcm2835_data.msg.request_code & 0x80000000))
101 + /* check if it was all ok and return the rate in milli degrees C */
103 + *temp = (uint)bcm2835_data.msg.tag.val;
105 + print_err("Failed to get temperature! (%x:%d)\n", tag_id, result);
106 + print_debug("OUT");
110 +static int bcm2835_get_temp(struct thermal_zone_device *thermal_dev, unsigned long *temp)
112 + return bcm2835_get_temp_or_max(thermal_dev, temp, VC_TAG_GET_TEMP);
115 +static int bcm2835_get_max_temp(struct thermal_zone_device *thermal_dev, int trip_num, unsigned long *temp)
117 + return bcm2835_get_temp_or_max(thermal_dev, temp, VC_TAG_GET_MAX_TEMP);
120 +static int bcm2835_get_trip_type(struct thermal_zone_device * thermal_dev, int trip_num, enum thermal_trip_type *trip_type)
122 + *trip_type = THERMAL_TRIP_HOT;
127 +static int bcm2835_get_mode(struct thermal_zone_device *thermal_dev, enum thermal_device_mode *dev_mode)
129 + *dev_mode = THERMAL_DEVICE_ENABLED;
134 +static int bcm2835_thermal_probe(struct platform_device *pdev)
137 + print_debug("THERMAL Driver has been probed!");
139 + /* check that the device isn't null!*/
142 + print_debug("Platform device is empty!");
146 + if(!(bcm2835_data.thermal_dev = thermal_zone_device_register("bcm2835_thermal", 1, 0, NULL, &ops, NULL, 0, 0)))
148 + print_debug("Unable to register the thermal device!");
155 +static int bcm2835_thermal_remove(struct platform_device *pdev)
159 + thermal_zone_device_unregister(bcm2835_data.thermal_dev);
161 + print_debug("OUT");
166 +static struct thermal_zone_device_ops ops = {
167 + .get_temp = bcm2835_get_temp,
168 + .get_trip_temp = bcm2835_get_max_temp,
169 + .get_trip_type = bcm2835_get_trip_type,
170 + .get_mode = bcm2835_get_mode,
173 +/* Thermal Driver */
174 +static struct platform_driver bcm2835_thermal_driver = {
175 + .probe = bcm2835_thermal_probe,
176 + .remove = bcm2835_thermal_remove,
178 + .name = "bcm2835_thermal",
179 + .owner = THIS_MODULE,
183 +MODULE_LICENSE("GPL");
184 +MODULE_AUTHOR("Dorian Peake");
185 +MODULE_DESCRIPTION("Thermal driver for bcm2835 chip");
187 +module_platform_driver(bcm2835_thermal_driver);
188 --- a/drivers/thermal/Kconfig
189 +++ b/drivers/thermal/Kconfig
190 @@ -169,4 +169,11 @@ config INTEL_POWERCLAMP
191 enforce idle time which results in more package C-state residency. The
192 user interface is exposed via generic thermal framework.
194 +config THERMAL_BCM2835
195 + tristate "BCM2835 Thermal Driver"
197 + This will enable temperature monitoring for the Broadcom BCM2835
198 + chip. If built as a module, it will be called 'bcm2835-thermal'.
202 --- a/drivers/thermal/Makefile
203 +++ b/drivers/thermal/Makefile
204 @@ -23,4 +23,5 @@ obj-$(CONFIG_DB8500_THERMAL) += db8500_t
205 obj-$(CONFIG_ARMADA_THERMAL) += armada_thermal.o
206 obj-$(CONFIG_DB8500_CPUFREQ_COOLING) += db8500_cpufreq_cooling.o
207 obj-$(CONFIG_INTEL_POWERCLAMP) += intel_powerclamp.o
208 +obj-$(CONFIG_THERMAL_BCM2835) += bcm2835-thermal.o