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 --- a/arch/arm/mach-bcm2708/bcm2708.c
20 +++ b/arch/arm/mach-bcm2708/bcm2708.c
21 @@ -455,6 +455,14 @@ static struct platform_device bcm2708_al
25 +static struct platform_device bcm2835_hwmon_device = {
26 + .name = "bcm2835_hwmon",
29 +static struct platform_device bcm2835_thermal_device = {
30 + .name = "bcm2835_thermal",
33 int __init bcm_register_device(struct platform_device *pdev)
36 @@ -563,6 +571,9 @@ void __init bcm2708_init(void)
37 for (i = 0; i < ARRAY_SIZE(bcm2708_alsa_devices); i++)
38 bcm_register_device(&bcm2708_alsa_devices[i]);
40 + bcm_register_device(&bcm2835_hwmon_device);
41 + bcm_register_device(&bcm2835_thermal_device);
43 for (i = 0; i < ARRAY_SIZE(amba_devs); i++) {
44 struct amba_device *d = amba_devs[i];
45 amba_device_register(d, &iomem_resource);
46 --- a/drivers/hwmon/Kconfig
47 +++ b/drivers/hwmon/Kconfig
48 @@ -1689,6 +1689,16 @@ config SENSORS_ULTRA45
49 This driver provides support for the Ultra45 workstation environmental
52 +config SENSORS_BCM2835
53 + depends on THERMAL_BCM2835=n
54 + tristate "Broadcom BCM2835 HWMON Driver"
56 + If you say yes here you get support for the hardware
57 + monitoring features of the BCM2835 Chip
59 + This driver can also be built as a module. If so, the module
60 + will be called bcm2835-hwmon.
64 comment "ACPI drivers"
65 --- a/drivers/hwmon/Makefile
66 +++ b/drivers/hwmon/Makefile
67 @@ -154,6 +154,7 @@ obj-$(CONFIG_SENSORS_W83L786NG) += w83l7
68 obj-$(CONFIG_SENSORS_WM831X) += wm831x-hwmon.o
69 obj-$(CONFIG_SENSORS_WM8350) += wm8350-hwmon.o
70 obj-$(CONFIG_SENSORS_GSC) += gsc.o
71 +obj-$(CONFIG_SENSORS_BCM2835) += bcm2835-hwmon.o
73 obj-$(CONFIG_PMBUS) += pmbus/
76 +++ b/drivers/hwmon/bcm2835-hwmon.c
78 +/*****************************************************************************
79 +* Copyright 2011 Broadcom Corporation. All rights reserved.
81 +* Unless you and Broadcom execute a separate written software license
82 +* agreement governing use of this software, this software is licensed to you
83 +* under the terms of the GNU General Public License version 2, available at
84 +* http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
86 +* Notwithstanding the above, under no circumstances may you combine this
87 +* software in any way with any other Broadcom software provided under a
88 +* license other than the GPL, without Broadcom's express prior written
90 +*****************************************************************************/
92 +#include <linux/kernel.h>
93 +#include <linux/module.h>
94 +#include <linux/init.h>
95 +#include <linux/hwmon.h>
96 +#include <linux/hwmon-sysfs.h>
97 +#include <linux/platform_device.h>
98 +#include <linux/sysfs.h>
99 +#include <mach/vcio.h>
100 +#include <linux/slab.h>
101 +#include <linux/err.h>
103 +#define MODULE_NAME "bcm2835_hwmon"
105 +/*#define HWMON_DEBUG_ENABLE*/
107 +#ifdef HWMON_DEBUG_ENABLE
108 +#define print_debug(fmt,...) printk(KERN_INFO "%s:%s:%d: "fmt"\n", MODULE_NAME, __func__, __LINE__, ##__VA_ARGS__)
110 +#define print_debug(fmt,...)
112 +#define print_err(fmt,...) printk(KERN_ERR "%s:%s:%d: "fmt"\n", MODULE_NAME, __func__,__LINE__, ##__VA_ARGS__)
113 +#define print_info(fmt,...) printk(KERN_INFO "%s: "fmt"\n", MODULE_NAME, ##__VA_ARGS__)
115 +#define VC_TAG_GET_TEMP 0x00030006
116 +#define VC_TAG_GET_MAX_TEMP 0x0003000A
118 +/* --- STRUCTS --- */
119 +struct bcm2835_hwmon_data {
120 + struct device *hwmon_dev;
123 +/* tag part of the message */
125 + uint32_t tag_id; /* the tag ID for the temperature */
126 + uint32_t buffer_size; /* size of the buffer (should be 8) */
127 + uint32_t request_code; /* identifies message as a request (should be 0) */
128 + uint32_t id; /* extra ID field (should be 0) */
129 + uint32_t val; /* returned value of the temperature */
132 +/* message structure to be sent to videocore */
134 + uint32_t msg_size; /* simply, sizeof(struct vc_msg) */
135 + uint32_t request_code; /* holds various information like the success and number of bytes returned (refer to mailboxes wiki) */
136 + struct vc_msg_tag tag; /* the tag structure above to make */
137 + uint32_t end_tag; /* an end identifier, should be set to NULL */
145 +/* --- PROTOTYPES --- */
146 +static ssize_t bcm2835_get_temp(struct device *dev, struct device_attribute *attr, char *buf);
147 +static ssize_t bcm2835_get_name(struct device *dev, struct device_attribute *attr, char *buf);
149 +/* --- GLOBALS --- */
151 +static struct bcm2835_hwmon_data *bcm2835_data;
152 +static struct platform_driver bcm2835_hwmon_driver;
154 +static SENSOR_DEVICE_ATTR(name, S_IRUGO,bcm2835_get_name,NULL,0);
155 +static SENSOR_DEVICE_ATTR(temp1_input,S_IRUGO,bcm2835_get_temp,NULL,TEMP);
156 +static SENSOR_DEVICE_ATTR(temp1_max,S_IRUGO,bcm2835_get_temp,NULL,MAX_TEMP);
158 +static struct attribute* bcm2835_attributes[] = {
159 + &sensor_dev_attr_name.dev_attr.attr,
160 + &sensor_dev_attr_temp1_input.dev_attr.attr,
161 + &sensor_dev_attr_temp1_max.dev_attr.attr,
165 +static struct attribute_group bcm2835_attr_group = {
166 + .attrs = bcm2835_attributes,
169 +/* --- FUNCTIONS --- */
171 +static ssize_t bcm2835_get_name(struct device *dev, struct device_attribute *attr, char *buf)
173 + return sprintf(buf,"bcm2835_hwmon\n");
176 +static ssize_t bcm2835_get_temp(struct device *dev, struct device_attribute *attr, char *buf)
181 + int index = ((struct sensor_device_attribute*)to_sensor_dev_attr(attr))->index;
185 + /* wipe all previous message data */
186 + memset(&msg, 0, sizeof msg);
188 + /* determine the message type */
190 + msg.tag.tag_id = VC_TAG_GET_TEMP;
191 + else if (index == MAX_TEMP)
192 + msg.tag.tag_id = VC_TAG_GET_MAX_TEMP;
195 + print_debug("Unknown temperature message!");
199 + msg.msg_size = sizeof msg;
200 + msg.tag.buffer_size = 8;
202 + /* send the message */
203 + result = bcm_mailbox_property(&msg, sizeof msg);
205 + /* check if it was all ok and return the rate in milli degrees C */
206 + if (result == 0 && (msg.request_code & 0x80000000))
207 + temp = (uint)msg.tag.val;
208 + #ifdef HWMON_DEBUG_ENABLE
210 + print_debug("Failed to get temperature!");
212 + print_debug("Got temperature as %u",temp);
213 + print_debug("OUT");
214 + return sprintf(buf, "%u\n", temp);
218 +static int bcm2835_hwmon_probe(struct platform_device *pdev)
223 + print_debug("HWMON Driver has been probed!");
225 + /* check that the device isn't null!*/
228 + print_debug("Platform device is empty!");
232 + /* allocate memory for neccessary data */
233 + bcm2835_data = kzalloc(sizeof(struct bcm2835_hwmon_data),GFP_KERNEL);
236 + print_debug("Unable to allocate memory for hwmon data!");
238 + goto kzalloc_error;
241 + /* create the sysfs files */
242 + if(sysfs_create_group(&pdev->dev.kobj, &bcm2835_attr_group))
244 + print_debug("Unable to create sysfs files!");
249 + /* register the hwmon device */
250 + bcm2835_data->hwmon_dev = hwmon_device_register(&pdev->dev);
251 + if (IS_ERR(bcm2835_data->hwmon_dev))
253 + err = PTR_ERR(bcm2835_data->hwmon_dev);
256 + print_debug("OUT");
261 + sysfs_remove_group(&pdev->dev.kobj, &bcm2835_attr_group);
264 + kfree(bcm2835_data);
272 +static int bcm2835_hwmon_remove(struct platform_device *pdev)
275 + hwmon_device_unregister(bcm2835_data->hwmon_dev);
277 + sysfs_remove_group(&pdev->dev.kobj, &bcm2835_attr_group);
278 + print_debug("OUT");
283 +static struct platform_driver bcm2835_hwmon_driver = {
284 + .probe = bcm2835_hwmon_probe,
285 + .remove = bcm2835_hwmon_remove,
287 + .name = "bcm2835_hwmon",
288 + .owner = THIS_MODULE,
292 +MODULE_LICENSE("GPL");
293 +MODULE_AUTHOR("Dorian Peake");
294 +MODULE_DESCRIPTION("HW Monitor driver for bcm2835 chip");
296 +module_platform_driver(bcm2835_hwmon_driver);
297 --- a/drivers/thermal/Kconfig
298 +++ b/drivers/thermal/Kconfig
299 @@ -206,6 +206,12 @@ config INTEL_POWERCLAMP
300 enforce idle time which results in more package C-state residency. The
301 user interface is exposed via generic thermal framework.
303 +config THERMAL_BCM2835
304 + tristate "BCM2835 Thermal Driver"
306 + This will enable temperature monitoring for the Broadcom BCM2835
307 + chip. If built as a module, it will be called 'bcm2835-thermal'.
309 config X86_PKG_TEMP_THERMAL
310 tristate "X86 package temperature thermal driver"
311 depends on X86_THERMAL_VECTOR
312 --- a/drivers/thermal/Makefile
313 +++ b/drivers/thermal/Makefile
314 @@ -29,6 +29,7 @@ obj-$(CONFIG_ARMADA_THERMAL) += armada_t
315 obj-$(CONFIG_IMX_THERMAL) += imx_thermal.o
316 obj-$(CONFIG_DB8500_CPUFREQ_COOLING) += db8500_cpufreq_cooling.o
317 obj-$(CONFIG_INTEL_POWERCLAMP) += intel_powerclamp.o
318 +obj-$(CONFIG_THERMAL_BCM2835) += bcm2835-thermal.o
319 obj-$(CONFIG_X86_PKG_TEMP_THERMAL) += x86_pkg_temp_thermal.o
320 obj-$(CONFIG_INTEL_SOC_DTS_THERMAL) += intel_soc_dts_thermal.o
321 obj-$(CONFIG_TI_SOC_THERMAL) += ti-soc-thermal/
323 +++ b/drivers/thermal/bcm2835-thermal.c
325 +/*****************************************************************************
326 +* Copyright 2011 Broadcom Corporation. All rights reserved.
328 +* Unless you and Broadcom execute a separate written software license
329 +* agreement governing use of this software, this software is licensed to you
330 +* under the terms of the GNU General Public License version 2, available at
331 +* http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
333 +* Notwithstanding the above, under no circumstances may you combine this
334 +* software in any way with any other Broadcom software provided under a
335 +* license other than the GPL, without Broadcom's express prior written
337 +*****************************************************************************/
339 +#include <linux/kernel.h>
340 +#include <linux/module.h>
341 +#include <linux/init.h>
342 +#include <linux/platform_device.h>
343 +#include <linux/slab.h>
344 +#include <linux/sysfs.h>
345 +#include <mach/vcio.h>
346 +#include <linux/thermal.h>
349 +/* --- DEFINITIONS --- */
350 +#define MODULE_NAME "bcm2835_thermal"
352 +/*#define THERMAL_DEBUG_ENABLE*/
354 +#ifdef THERMAL_DEBUG_ENABLE
355 +#define print_debug(fmt,...) printk(KERN_INFO "%s:%s:%d: "fmt"\n", MODULE_NAME, __func__, __LINE__, ##__VA_ARGS__)
357 +#define print_debug(fmt,...)
359 +#define print_err(fmt,...) printk(KERN_ERR "%s:%s:%d: "fmt"\n", MODULE_NAME, __func__,__LINE__, ##__VA_ARGS__)
361 +#define VC_TAG_GET_TEMP 0x00030006
362 +#define VC_TAG_GET_MAX_TEMP 0x0003000A
369 +/* --- STRUCTS --- */
370 +/* tag part of the message */
372 + uint32_t tag_id; /* the tag ID for the temperature */
373 + uint32_t buffer_size; /* size of the buffer (should be 8) */
374 + uint32_t request_code; /* identifies message as a request (should be 0) */
375 + uint32_t id; /* extra ID field (should be 0) */
376 + uint32_t val; /* returned value of the temperature */
379 +/* message structure to be sent to videocore */
381 + uint32_t msg_size; /* simply, sizeof(struct vc_msg) */
382 + uint32_t request_code; /* holds various information like the success and number of bytes returned (refer to mailboxes wiki) */
383 + struct vc_msg_tag tag; /* the tag structure above to make */
384 + uint32_t end_tag; /* an end identifier, should be set to NULL */
387 +struct bcm2835_thermal_data {
388 + struct thermal_zone_device *thermal_dev;
392 +/* --- GLOBALS --- */
393 +static struct bcm2835_thermal_data bcm2835_data;
395 +/* Thermal Device Operations */
396 +static struct thermal_zone_device_ops ops;
398 +/* --- FUNCTIONS --- */
400 +static int bcm2835_get_temp_or_max(struct thermal_zone_device *thermal_dev, unsigned long *temp, unsigned tag_id)
402 + int result = -1, retry = 3;
406 + while (result != 0 && retry-- > 0) {
407 + /* wipe all previous message data */
408 + memset(&bcm2835_data.msg, 0, sizeof bcm2835_data.msg);
410 + /* prepare message */
411 + bcm2835_data.msg.msg_size = sizeof bcm2835_data.msg;
412 + bcm2835_data.msg.tag.buffer_size = 8;
413 + bcm2835_data.msg.tag.tag_id = tag_id;
415 + /* send the message */
416 + result = bcm_mailbox_property(&bcm2835_data.msg, sizeof bcm2835_data.msg);
417 + 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);
418 + if (!(bcm2835_data.msg.request_code & 0x80000000))
422 + /* check if it was all ok and return the rate in milli degrees C */
424 + *temp = (uint)bcm2835_data.msg.tag.val;
426 + print_err("Failed to get temperature! (%x:%d)\n", tag_id, result);
427 + print_debug("OUT");
431 +static int bcm2835_get_temp(struct thermal_zone_device *thermal_dev, unsigned long *temp)
433 + return bcm2835_get_temp_or_max(thermal_dev, temp, VC_TAG_GET_TEMP);
436 +static int bcm2835_get_max_temp(struct thermal_zone_device *thermal_dev, int trip_num, unsigned long *temp)
438 + return bcm2835_get_temp_or_max(thermal_dev, temp, VC_TAG_GET_MAX_TEMP);
441 +static int bcm2835_get_trip_type(struct thermal_zone_device * thermal_dev, int trip_num, enum thermal_trip_type *trip_type)
443 + *trip_type = THERMAL_TRIP_HOT;
448 +static int bcm2835_get_mode(struct thermal_zone_device *thermal_dev, enum thermal_device_mode *dev_mode)
450 + *dev_mode = THERMAL_DEVICE_ENABLED;
455 +static int bcm2835_thermal_probe(struct platform_device *pdev)
458 + print_debug("THERMAL Driver has been probed!");
460 + /* check that the device isn't null!*/
463 + print_debug("Platform device is empty!");
467 + if(!(bcm2835_data.thermal_dev = thermal_zone_device_register("bcm2835_thermal", 1, 0, NULL, &ops, NULL, 0, 0)))
469 + print_debug("Unable to register the thermal device!");
476 +static int bcm2835_thermal_remove(struct platform_device *pdev)
480 + thermal_zone_device_unregister(bcm2835_data.thermal_dev);
482 + print_debug("OUT");
487 +static struct thermal_zone_device_ops ops = {
488 + .get_temp = bcm2835_get_temp,
489 + .get_trip_temp = bcm2835_get_max_temp,
490 + .get_trip_type = bcm2835_get_trip_type,
491 + .get_mode = bcm2835_get_mode,
494 +/* Thermal Driver */
495 +static struct platform_driver bcm2835_thermal_driver = {
496 + .probe = bcm2835_thermal_probe,
497 + .remove = bcm2835_thermal_remove,
499 + .name = "bcm2835_thermal",
500 + .owner = THIS_MODULE,
504 +MODULE_LICENSE("GPL");
505 +MODULE_AUTHOR("Dorian Peake");
506 +MODULE_DESCRIPTION("Thermal driver for bcm2835 chip");
508 +module_platform_driver(bcm2835_thermal_driver);