1 From de9bdcff7ec73589cb314a1569ce5aba5fe09146 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 017/114] Added hwmon/thermal driver for reporting core
5 temperature. Thanks Dorian
8 arch/arm/mach-bcm2708/bcm2708.c | 11 ++
9 drivers/hwmon/Kconfig | 10 ++
10 drivers/hwmon/Makefile | 1 +
11 drivers/hwmon/bcm2835-hwmon.c | 219 ++++++++++++++++++++++++++++++++++++++
12 drivers/thermal/Kconfig | 6 ++
13 drivers/thermal/Makefile | 1 +
14 drivers/thermal/bcm2835-thermal.c | 184 ++++++++++++++++++++++++++++++++
15 7 files changed, 432 insertions(+)
16 create mode 100644 drivers/hwmon/bcm2835-hwmon.c
17 create mode 100644 drivers/thermal/bcm2835-thermal.c
19 diff --git a/arch/arm/mach-bcm2708/bcm2708.c b/arch/arm/mach-bcm2708/bcm2708.c
20 index f3dccae..af57d11 100644
21 --- a/arch/arm/mach-bcm2708/bcm2708.c
22 +++ b/arch/arm/mach-bcm2708/bcm2708.c
23 @@ -455,6 +455,14 @@ static struct platform_device bcm2708_alsa_devices[] = {
27 +static struct platform_device bcm2835_hwmon_device = {
28 + .name = "bcm2835_hwmon",
31 +static struct platform_device bcm2835_thermal_device = {
32 + .name = "bcm2835_thermal",
35 int __init bcm_register_device(struct platform_device *pdev)
38 @@ -563,6 +571,9 @@ void __init bcm2708_init(void)
39 for (i = 0; i < ARRAY_SIZE(bcm2708_alsa_devices); i++)
40 bcm_register_device(&bcm2708_alsa_devices[i]);
42 + bcm_register_device(&bcm2835_hwmon_device);
43 + bcm_register_device(&bcm2835_thermal_device);
45 for (i = 0; i < ARRAY_SIZE(amba_devs); i++) {
46 struct amba_device *d = amba_devs[i];
47 amba_device_register(d, &iomem_resource);
48 diff --git a/drivers/hwmon/Kconfig b/drivers/hwmon/Kconfig
49 index 5286d7c..d52e192 100644
50 --- a/drivers/hwmon/Kconfig
51 +++ b/drivers/hwmon/Kconfig
52 @@ -1680,6 +1680,16 @@ config SENSORS_ULTRA45
53 This driver provides support for the Ultra45 workstation environmental
56 +config SENSORS_BCM2835
57 + depends on THERMAL_BCM2835=n
58 + tristate "Broadcom BCM2835 HWMON Driver"
60 + If you say yes here you get support for the hardware
61 + monitoring features of the BCM2835 Chip
63 + This driver can also be built as a module. If so, the module
64 + will be called bcm2835-hwmon.
68 comment "ACPI drivers"
69 diff --git a/drivers/hwmon/Makefile b/drivers/hwmon/Makefile
70 index c90a761..15aaecf 100644
71 --- a/drivers/hwmon/Makefile
72 +++ b/drivers/hwmon/Makefile
73 @@ -153,6 +153,7 @@ obj-$(CONFIG_SENSORS_W83L785TS) += w83l785ts.o
74 obj-$(CONFIG_SENSORS_WM831X) += wm831x-hwmon.o
75 obj-$(CONFIG_SENSORS_WM8350) += wm8350-hwmon.o
76 obj-$(CONFIG_SENSORS_GSC) += gsc.o
77 +obj-$(CONFIG_SENSORS_BCM2835) += bcm2835-hwmon.o
79 obj-$(CONFIG_PMBUS) += pmbus/
81 diff --git a/drivers/hwmon/bcm2835-hwmon.c b/drivers/hwmon/bcm2835-hwmon.c
83 index 0000000..5bbed45
85 +++ b/drivers/hwmon/bcm2835-hwmon.c
87 +/*****************************************************************************
88 +* Copyright 2011 Broadcom Corporation. All rights reserved.
90 +* Unless you and Broadcom execute a separate written software license
91 +* agreement governing use of this software, this software is licensed to you
92 +* under the terms of the GNU General Public License version 2, available at
93 +* http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
95 +* Notwithstanding the above, under no circumstances may you combine this
96 +* software in any way with any other Broadcom software provided under a
97 +* license other than the GPL, without Broadcom's express prior written
99 +*****************************************************************************/
101 +#include <linux/kernel.h>
102 +#include <linux/module.h>
103 +#include <linux/init.h>
104 +#include <linux/hwmon.h>
105 +#include <linux/hwmon-sysfs.h>
106 +#include <linux/platform_device.h>
107 +#include <linux/sysfs.h>
108 +#include <mach/vcio.h>
109 +#include <linux/slab.h>
110 +#include <linux/err.h>
112 +#define MODULE_NAME "bcm2835_hwmon"
114 +/*#define HWMON_DEBUG_ENABLE*/
116 +#ifdef HWMON_DEBUG_ENABLE
117 +#define print_debug(fmt,...) printk(KERN_INFO "%s:%s:%d: "fmt"\n", MODULE_NAME, __func__, __LINE__, ##__VA_ARGS__)
119 +#define print_debug(fmt,...)
121 +#define print_err(fmt,...) printk(KERN_ERR "%s:%s:%d: "fmt"\n", MODULE_NAME, __func__,__LINE__, ##__VA_ARGS__)
122 +#define print_info(fmt,...) printk(KERN_INFO "%s: "fmt"\n", MODULE_NAME, ##__VA_ARGS__)
124 +#define VC_TAG_GET_TEMP 0x00030006
125 +#define VC_TAG_GET_MAX_TEMP 0x0003000A
127 +/* --- STRUCTS --- */
128 +struct bcm2835_hwmon_data {
129 + struct device *hwmon_dev;
132 +/* tag part of the message */
134 + uint32_t tag_id; /* the tag ID for the temperature */
135 + uint32_t buffer_size; /* size of the buffer (should be 8) */
136 + uint32_t request_code; /* identifies message as a request (should be 0) */
137 + uint32_t id; /* extra ID field (should be 0) */
138 + uint32_t val; /* returned value of the temperature */
141 +/* message structure to be sent to videocore */
143 + uint32_t msg_size; /* simply, sizeof(struct vc_msg) */
144 + uint32_t request_code; /* holds various information like the success and number of bytes returned (refer to mailboxes wiki) */
145 + struct vc_msg_tag tag; /* the tag structure above to make */
146 + uint32_t end_tag; /* an end identifier, should be set to NULL */
154 +/* --- PROTOTYPES --- */
155 +static ssize_t bcm2835_get_temp(struct device *dev, struct device_attribute *attr, char *buf);
156 +static ssize_t bcm2835_get_name(struct device *dev, struct device_attribute *attr, char *buf);
158 +/* --- GLOBALS --- */
160 +static struct bcm2835_hwmon_data *bcm2835_data;
161 +static struct platform_driver bcm2835_hwmon_driver;
163 +static SENSOR_DEVICE_ATTR(name, S_IRUGO,bcm2835_get_name,NULL,0);
164 +static SENSOR_DEVICE_ATTR(temp1_input,S_IRUGO,bcm2835_get_temp,NULL,TEMP);
165 +static SENSOR_DEVICE_ATTR(temp1_max,S_IRUGO,bcm2835_get_temp,NULL,MAX_TEMP);
167 +static struct attribute* bcm2835_attributes[] = {
168 + &sensor_dev_attr_name.dev_attr.attr,
169 + &sensor_dev_attr_temp1_input.dev_attr.attr,
170 + &sensor_dev_attr_temp1_max.dev_attr.attr,
174 +static struct attribute_group bcm2835_attr_group = {
175 + .attrs = bcm2835_attributes,
178 +/* --- FUNCTIONS --- */
180 +static ssize_t bcm2835_get_name(struct device *dev, struct device_attribute *attr, char *buf)
182 + return sprintf(buf,"bcm2835_hwmon\n");
185 +static ssize_t bcm2835_get_temp(struct device *dev, struct device_attribute *attr, char *buf)
190 + int index = ((struct sensor_device_attribute*)to_sensor_dev_attr(attr))->index;
194 + /* wipe all previous message data */
195 + memset(&msg, 0, sizeof msg);
197 + /* determine the message type */
199 + msg.tag.tag_id = VC_TAG_GET_TEMP;
200 + else if (index == MAX_TEMP)
201 + msg.tag.tag_id = VC_TAG_GET_MAX_TEMP;
204 + print_debug("Unknown temperature message!");
208 + msg.msg_size = sizeof msg;
209 + msg.tag.buffer_size = 8;
211 + /* send the message */
212 + result = bcm_mailbox_property(&msg, sizeof msg);
214 + /* check if it was all ok and return the rate in milli degrees C */
215 + if (result == 0 && (msg.request_code & 0x80000000))
216 + temp = (uint)msg.tag.val;
217 + #ifdef HWMON_DEBUG_ENABLE
219 + print_debug("Failed to get temperature!");
221 + print_debug("Got temperature as %u",temp);
222 + print_debug("OUT");
223 + return sprintf(buf, "%u\n", temp);
227 +static int bcm2835_hwmon_probe(struct platform_device *pdev)
232 + print_debug("HWMON Driver has been probed!");
234 + /* check that the device isn't null!*/
237 + print_debug("Platform device is empty!");
241 + /* allocate memory for neccessary data */
242 + bcm2835_data = kzalloc(sizeof(struct bcm2835_hwmon_data),GFP_KERNEL);
245 + print_debug("Unable to allocate memory for hwmon data!");
247 + goto kzalloc_error;
250 + /* create the sysfs files */
251 + if(sysfs_create_group(&pdev->dev.kobj, &bcm2835_attr_group))
253 + print_debug("Unable to create sysfs files!");
258 + /* register the hwmon device */
259 + bcm2835_data->hwmon_dev = hwmon_device_register(&pdev->dev);
260 + if (IS_ERR(bcm2835_data->hwmon_dev))
262 + err = PTR_ERR(bcm2835_data->hwmon_dev);
265 + print_debug("OUT");
270 + sysfs_remove_group(&pdev->dev.kobj, &bcm2835_attr_group);
273 + kfree(bcm2835_data);
281 +static int bcm2835_hwmon_remove(struct platform_device *pdev)
284 + hwmon_device_unregister(bcm2835_data->hwmon_dev);
286 + sysfs_remove_group(&pdev->dev.kobj, &bcm2835_attr_group);
287 + print_debug("OUT");
292 +static struct platform_driver bcm2835_hwmon_driver = {
293 + .probe = bcm2835_hwmon_probe,
294 + .remove = bcm2835_hwmon_remove,
296 + .name = "bcm2835_hwmon",
297 + .owner = THIS_MODULE,
301 +MODULE_LICENSE("GPL");
302 +MODULE_AUTHOR("Dorian Peake");
303 +MODULE_DESCRIPTION("HW Monitor driver for bcm2835 chip");
305 +module_platform_driver(bcm2835_hwmon_driver);
306 diff --git a/drivers/thermal/Kconfig b/drivers/thermal/Kconfig
307 index f554d25..fecc621 100644
308 --- a/drivers/thermal/Kconfig
309 +++ b/drivers/thermal/Kconfig
310 @@ -206,6 +206,12 @@ config INTEL_POWERCLAMP
311 enforce idle time which results in more package C-state residency. The
312 user interface is exposed via generic thermal framework.
314 +config THERMAL_BCM2835
315 + tristate "BCM2835 Thermal Driver"
317 + This will enable temperature monitoring for the Broadcom BCM2835
318 + chip. If built as a module, it will be called 'bcm2835-thermal'.
320 config X86_PKG_TEMP_THERMAL
321 tristate "X86 package temperature thermal driver"
322 depends on X86_THERMAL_VECTOR
323 diff --git a/drivers/thermal/Makefile b/drivers/thermal/Makefile
324 index 39c4fe8..30a4741 100644
325 --- a/drivers/thermal/Makefile
326 +++ b/drivers/thermal/Makefile
327 @@ -29,6 +29,7 @@ obj-$(CONFIG_ARMADA_THERMAL) += armada_thermal.o
328 obj-$(CONFIG_IMX_THERMAL) += imx_thermal.o
329 obj-$(CONFIG_DB8500_CPUFREQ_COOLING) += db8500_cpufreq_cooling.o
330 obj-$(CONFIG_INTEL_POWERCLAMP) += intel_powerclamp.o
331 +obj-$(CONFIG_THERMAL_BCM2835) += bcm2835-thermal.o
332 obj-$(CONFIG_X86_PKG_TEMP_THERMAL) += x86_pkg_temp_thermal.o
333 obj-$(CONFIG_INTEL_SOC_DTS_THERMAL) += intel_soc_dts_thermal.o
334 obj-$(CONFIG_TI_SOC_THERMAL) += ti-soc-thermal/
335 diff --git a/drivers/thermal/bcm2835-thermal.c b/drivers/thermal/bcm2835-thermal.c
337 index 0000000..85fceb5
339 +++ b/drivers/thermal/bcm2835-thermal.c
341 +/*****************************************************************************
342 +* Copyright 2011 Broadcom Corporation. All rights reserved.
344 +* Unless you and Broadcom execute a separate written software license
345 +* agreement governing use of this software, this software is licensed to you
346 +* under the terms of the GNU General Public License version 2, available at
347 +* http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
349 +* Notwithstanding the above, under no circumstances may you combine this
350 +* software in any way with any other Broadcom software provided under a
351 +* license other than the GPL, without Broadcom's express prior written
353 +*****************************************************************************/
355 +#include <linux/kernel.h>
356 +#include <linux/module.h>
357 +#include <linux/init.h>
358 +#include <linux/platform_device.h>
359 +#include <linux/slab.h>
360 +#include <linux/sysfs.h>
361 +#include <mach/vcio.h>
362 +#include <linux/thermal.h>
365 +/* --- DEFINITIONS --- */
366 +#define MODULE_NAME "bcm2835_thermal"
368 +/*#define THERMAL_DEBUG_ENABLE*/
370 +#ifdef THERMAL_DEBUG_ENABLE
371 +#define print_debug(fmt,...) printk(KERN_INFO "%s:%s:%d: "fmt"\n", MODULE_NAME, __func__, __LINE__, ##__VA_ARGS__)
373 +#define print_debug(fmt,...)
375 +#define print_err(fmt,...) printk(KERN_ERR "%s:%s:%d: "fmt"\n", MODULE_NAME, __func__,__LINE__, ##__VA_ARGS__)
377 +#define VC_TAG_GET_TEMP 0x00030006
378 +#define VC_TAG_GET_MAX_TEMP 0x0003000A
385 +/* --- STRUCTS --- */
386 +/* tag part of the message */
388 + uint32_t tag_id; /* the tag ID for the temperature */
389 + uint32_t buffer_size; /* size of the buffer (should be 8) */
390 + uint32_t request_code; /* identifies message as a request (should be 0) */
391 + uint32_t id; /* extra ID field (should be 0) */
392 + uint32_t val; /* returned value of the temperature */
395 +/* message structure to be sent to videocore */
397 + uint32_t msg_size; /* simply, sizeof(struct vc_msg) */
398 + uint32_t request_code; /* holds various information like the success and number of bytes returned (refer to mailboxes wiki) */
399 + struct vc_msg_tag tag; /* the tag structure above to make */
400 + uint32_t end_tag; /* an end identifier, should be set to NULL */
403 +struct bcm2835_thermal_data {
404 + struct thermal_zone_device *thermal_dev;
408 +/* --- GLOBALS --- */
409 +static struct bcm2835_thermal_data bcm2835_data;
411 +/* Thermal Device Operations */
412 +static struct thermal_zone_device_ops ops;
414 +/* --- FUNCTIONS --- */
416 +static int bcm2835_get_temp_or_max(struct thermal_zone_device *thermal_dev, unsigned long *temp, unsigned tag_id)
418 + int result = -1, retry = 3;
422 + while (result != 0 && retry-- > 0) {
423 + /* wipe all previous message data */
424 + memset(&bcm2835_data.msg, 0, sizeof bcm2835_data.msg);
426 + /* prepare message */
427 + bcm2835_data.msg.msg_size = sizeof bcm2835_data.msg;
428 + bcm2835_data.msg.tag.buffer_size = 8;
429 + bcm2835_data.msg.tag.tag_id = tag_id;
431 + /* send the message */
432 + result = bcm_mailbox_property(&bcm2835_data.msg, sizeof bcm2835_data.msg);
433 + 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);
434 + if (!(bcm2835_data.msg.request_code & 0x80000000))
438 + /* check if it was all ok and return the rate in milli degrees C */
440 + *temp = (uint)bcm2835_data.msg.tag.val;
442 + print_err("Failed to get temperature! (%x:%d)\n", tag_id, result);
443 + print_debug("OUT");
447 +static int bcm2835_get_temp(struct thermal_zone_device *thermal_dev, unsigned long *temp)
449 + return bcm2835_get_temp_or_max(thermal_dev, temp, VC_TAG_GET_TEMP);
452 +static int bcm2835_get_max_temp(struct thermal_zone_device *thermal_dev, int trip_num, unsigned long *temp)
454 + return bcm2835_get_temp_or_max(thermal_dev, temp, VC_TAG_GET_MAX_TEMP);
457 +static int bcm2835_get_trip_type(struct thermal_zone_device * thermal_dev, int trip_num, enum thermal_trip_type *trip_type)
459 + *trip_type = THERMAL_TRIP_HOT;
464 +static int bcm2835_get_mode(struct thermal_zone_device *thermal_dev, enum thermal_device_mode *dev_mode)
466 + *dev_mode = THERMAL_DEVICE_ENABLED;
471 +static int bcm2835_thermal_probe(struct platform_device *pdev)
474 + print_debug("THERMAL Driver has been probed!");
476 + /* check that the device isn't null!*/
479 + print_debug("Platform device is empty!");
483 + if(!(bcm2835_data.thermal_dev = thermal_zone_device_register("bcm2835_thermal", 1, 0, NULL, &ops, NULL, 0, 0)))
485 + print_debug("Unable to register the thermal device!");
492 +static int bcm2835_thermal_remove(struct platform_device *pdev)
496 + thermal_zone_device_unregister(bcm2835_data.thermal_dev);
498 + print_debug("OUT");
503 +static struct thermal_zone_device_ops ops = {
504 + .get_temp = bcm2835_get_temp,
505 + .get_trip_temp = bcm2835_get_max_temp,
506 + .get_trip_type = bcm2835_get_trip_type,
507 + .get_mode = bcm2835_get_mode,
510 +/* Thermal Driver */
511 +static struct platform_driver bcm2835_thermal_driver = {
512 + .probe = bcm2835_thermal_probe,
513 + .remove = bcm2835_thermal_remove,
515 + .name = "bcm2835_thermal",
516 + .owner = THIS_MODULE,
520 +MODULE_LICENSE("GPL");
521 +MODULE_AUTHOR("Dorian Peake");
522 +MODULE_DESCRIPTION("Thermal driver for bcm2835 chip");
524 +module_platform_driver(bcm2835_thermal_driver);