1 From 22964c3b89f28e0957965aee59e713670d3a7729 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 15/54] 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 Index: linux-3.14.18/arch/arm/mach-bcm2708/bcm2708.c
20 ===================================================================
21 --- linux-3.14.18.orig/arch/arm/mach-bcm2708/bcm2708.c 2014-09-27 12:03:08.607411837 -0700
22 +++ linux-3.14.18/arch/arm/mach-bcm2708/bcm2708.c 2014-09-27 12:03:08.603411837 -0700
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)
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 Index: linux-3.14.18/drivers/hwmon/Kconfig
49 ===================================================================
50 --- linux-3.14.18.orig/drivers/hwmon/Kconfig 2014-09-27 12:03:08.607411837 -0700
51 +++ linux-3.14.18/drivers/hwmon/Kconfig 2014-09-27 12:03:08.603411837 -0700
52 @@ -1565,6 +1565,16 @@
54 Support for the A/D converter on MC13783 and MC13892 PMIC.
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 Index: linux-3.14.18/drivers/hwmon/bcm2835-hwmon.c
70 ===================================================================
71 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
72 +++ linux-3.14.18/drivers/hwmon/bcm2835-hwmon.c 2014-09-27 12:03:08.603411837 -0700
74 +/*****************************************************************************
75 +* Copyright 2011 Broadcom Corporation. All rights reserved.
77 +* Unless you and Broadcom execute a separate written software license
78 +* agreement governing use of this software, this software is licensed to you
79 +* under the terms of the GNU General Public License version 2, available at
80 +* http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
82 +* Notwithstanding the above, under no circumstances may you combine this
83 +* software in any way with any other Broadcom software provided under a
84 +* license other than the GPL, without Broadcom's express prior written
86 +*****************************************************************************/
88 +#include <linux/kernel.h>
89 +#include <linux/module.h>
90 +#include <linux/init.h>
91 +#include <linux/hwmon.h>
92 +#include <linux/hwmon-sysfs.h>
93 +#include <linux/platform_device.h>
94 +#include <linux/sysfs.h>
95 +#include <mach/vcio.h>
96 +#include <linux/slab.h>
97 +#include <linux/err.h>
99 +#define MODULE_NAME "bcm2835_hwmon"
101 +/*#define HWMON_DEBUG_ENABLE*/
103 +#ifdef HWMON_DEBUG_ENABLE
104 +#define print_debug(fmt,...) printk(KERN_INFO "%s:%s:%d: "fmt"\n", MODULE_NAME, __func__, __LINE__, ##__VA_ARGS__)
106 +#define print_debug(fmt,...)
108 +#define print_err(fmt,...) printk(KERN_ERR "%s:%s:%d: "fmt"\n", MODULE_NAME, __func__,__LINE__, ##__VA_ARGS__)
109 +#define print_info(fmt,...) printk(KERN_INFO "%s: "fmt"\n", MODULE_NAME, ##__VA_ARGS__)
111 +#define VC_TAG_GET_TEMP 0x00030006
112 +#define VC_TAG_GET_MAX_TEMP 0x0003000A
114 +/* --- STRUCTS --- */
115 +struct bcm2835_hwmon_data {
116 + struct device *hwmon_dev;
119 +/* tag part of the message */
121 + uint32_t tag_id; /* the tag ID for the temperature */
122 + uint32_t buffer_size; /* size of the buffer (should be 8) */
123 + uint32_t request_code; /* identifies message as a request (should be 0) */
124 + uint32_t id; /* extra ID field (should be 0) */
125 + uint32_t val; /* returned value of the temperature */
128 +/* message structure to be sent to videocore */
130 + uint32_t msg_size; /* simply, sizeof(struct vc_msg) */
131 + uint32_t request_code; /* holds various information like the success and number of bytes returned (refer to mailboxes wiki) */
132 + struct vc_msg_tag tag; /* the tag structure above to make */
133 + uint32_t end_tag; /* an end identifier, should be set to NULL */
141 +/* --- PROTOTYPES --- */
142 +static ssize_t bcm2835_get_temp(struct device *dev, struct device_attribute *attr, char *buf);
143 +static ssize_t bcm2835_get_name(struct device *dev, struct device_attribute *attr, char *buf);
145 +/* --- GLOBALS --- */
147 +static struct bcm2835_hwmon_data *bcm2835_data;
148 +static struct platform_driver bcm2835_hwmon_driver;
150 +static SENSOR_DEVICE_ATTR(name, S_IRUGO,bcm2835_get_name,NULL,0);
151 +static SENSOR_DEVICE_ATTR(temp1_input,S_IRUGO,bcm2835_get_temp,NULL,TEMP);
152 +static SENSOR_DEVICE_ATTR(temp1_max,S_IRUGO,bcm2835_get_temp,NULL,MAX_TEMP);
154 +static struct attribute* bcm2835_attributes[] = {
155 + &sensor_dev_attr_name.dev_attr.attr,
156 + &sensor_dev_attr_temp1_input.dev_attr.attr,
157 + &sensor_dev_attr_temp1_max.dev_attr.attr,
161 +static struct attribute_group bcm2835_attr_group = {
162 + .attrs = bcm2835_attributes,
165 +/* --- FUNCTIONS --- */
167 +static ssize_t bcm2835_get_name(struct device *dev, struct device_attribute *attr, char *buf)
169 + return sprintf(buf,"bcm2835_hwmon\n");
172 +static ssize_t bcm2835_get_temp(struct device *dev, struct device_attribute *attr, char *buf)
177 + int index = ((struct sensor_device_attribute*)to_sensor_dev_attr(attr))->index;
181 + /* wipe all previous message data */
182 + memset(&msg, 0, sizeof msg);
184 + /* determine the message type */
186 + msg.tag.tag_id = VC_TAG_GET_TEMP;
187 + else if (index == MAX_TEMP)
188 + msg.tag.tag_id = VC_TAG_GET_MAX_TEMP;
191 + print_debug("Unknown temperature message!");
195 + msg.msg_size = sizeof msg;
196 + msg.tag.buffer_size = 8;
198 + /* send the message */
199 + result = bcm_mailbox_property(&msg, sizeof msg);
201 + /* check if it was all ok and return the rate in milli degrees C */
202 + if (result == 0 && (msg.request_code & 0x80000000))
203 + temp = (uint)msg.tag.val;
204 + #ifdef HWMON_DEBUG_ENABLE
206 + print_debug("Failed to get temperature!");
208 + print_debug("Got temperature as %u",temp);
209 + print_debug("OUT");
210 + return sprintf(buf, "%u\n", temp);
214 +static int bcm2835_hwmon_probe(struct platform_device *pdev)
219 + print_debug("HWMON Driver has been probed!");
221 + /* check that the device isn't null!*/
224 + print_debug("Platform device is empty!");
228 + /* allocate memory for neccessary data */
229 + bcm2835_data = kzalloc(sizeof(struct bcm2835_hwmon_data),GFP_KERNEL);
232 + print_debug("Unable to allocate memory for hwmon data!");
234 + goto kzalloc_error;
237 + /* create the sysfs files */
238 + if(sysfs_create_group(&pdev->dev.kobj, &bcm2835_attr_group))
240 + print_debug("Unable to create sysfs files!");
245 + /* register the hwmon device */
246 + bcm2835_data->hwmon_dev = hwmon_device_register(&pdev->dev);
247 + if (IS_ERR(bcm2835_data->hwmon_dev))
249 + err = PTR_ERR(bcm2835_data->hwmon_dev);
252 + print_debug("OUT");
257 + sysfs_remove_group(&pdev->dev.kobj, &bcm2835_attr_group);
260 + kfree(bcm2835_data);
268 +static int bcm2835_hwmon_remove(struct platform_device *pdev)
271 + hwmon_device_unregister(bcm2835_data->hwmon_dev);
273 + sysfs_remove_group(&pdev->dev.kobj, &bcm2835_attr_group);
274 + print_debug("OUT");
279 +static struct platform_driver bcm2835_hwmon_driver = {
280 + .probe = bcm2835_hwmon_probe,
281 + .remove = bcm2835_hwmon_remove,
283 + .name = "bcm2835_hwmon",
284 + .owner = THIS_MODULE,
288 +MODULE_LICENSE("GPL");
289 +MODULE_AUTHOR("Dorian Peake");
290 +MODULE_DESCRIPTION("HW Monitor driver for bcm2835 chip");
292 +module_platform_driver(bcm2835_hwmon_driver);
293 Index: linux-3.14.18/drivers/thermal/Kconfig
294 ===================================================================
295 --- linux-3.14.18.orig/drivers/thermal/Kconfig 2014-09-27 12:03:08.607411837 -0700
296 +++ linux-3.14.18/drivers/thermal/Kconfig 2014-09-27 12:03:08.603411837 -0700
298 enforce idle time which results in more package C-state residency. The
299 user interface is exposed via generic thermal framework.
301 +config THERMAL_BCM2835
302 + tristate "BCM2835 Thermal Driver"
304 + This will enable temperature monitoring for the Broadcom BCM2835
305 + chip. If built as a module, it will be called 'bcm2835-thermal'.
307 config X86_PKG_TEMP_THERMAL
308 tristate "X86 package temperature thermal driver"
309 depends on X86_THERMAL_VECTOR
310 Index: linux-3.14.18/drivers/thermal/Makefile
311 ===================================================================
312 --- linux-3.14.18.orig/drivers/thermal/Makefile 2014-09-27 12:03:08.607411837 -0700
313 +++ linux-3.14.18/drivers/thermal/Makefile 2014-09-27 12:03:08.603411837 -0700
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_TI_SOC_THERMAL) += ti-soc-thermal/
321 obj-$(CONFIG_ACPI_INT3403_THERMAL) += int3403_thermal.o
322 Index: linux-3.14.18/drivers/thermal/bcm2835-thermal.c
323 ===================================================================
324 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
325 +++ linux-3.14.18/drivers/thermal/bcm2835-thermal.c 2014-09-27 12:03:08.603411837 -0700
327 +/*****************************************************************************
328 +* Copyright 2011 Broadcom Corporation. All rights reserved.
330 +* Unless you and Broadcom execute a separate written software license
331 +* agreement governing use of this software, this software is licensed to you
332 +* under the terms of the GNU General Public License version 2, available at
333 +* http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
335 +* Notwithstanding the above, under no circumstances may you combine this
336 +* software in any way with any other Broadcom software provided under a
337 +* license other than the GPL, without Broadcom's express prior written
339 +*****************************************************************************/
341 +#include <linux/kernel.h>
342 +#include <linux/module.h>
343 +#include <linux/init.h>
344 +#include <linux/platform_device.h>
345 +#include <linux/slab.h>
346 +#include <linux/sysfs.h>
347 +#include <mach/vcio.h>
348 +#include <linux/thermal.h>
351 +/* --- DEFINITIONS --- */
352 +#define MODULE_NAME "bcm2835_thermal"
354 +/*#define THERMAL_DEBUG_ENABLE*/
356 +#ifdef THERMAL_DEBUG_ENABLE
357 +#define print_debug(fmt,...) printk(KERN_INFO "%s:%s:%d: "fmt"\n", MODULE_NAME, __func__, __LINE__, ##__VA_ARGS__)
359 +#define print_debug(fmt,...)
361 +#define print_err(fmt,...) printk(KERN_ERR "%s:%s:%d: "fmt"\n", MODULE_NAME, __func__,__LINE__, ##__VA_ARGS__)
363 +#define VC_TAG_GET_TEMP 0x00030006
364 +#define VC_TAG_GET_MAX_TEMP 0x0003000A
371 +/* --- STRUCTS --- */
372 +/* tag part of the message */
374 + uint32_t tag_id; /* the tag ID for the temperature */
375 + uint32_t buffer_size; /* size of the buffer (should be 8) */
376 + uint32_t request_code; /* identifies message as a request (should be 0) */
377 + uint32_t id; /* extra ID field (should be 0) */
378 + uint32_t val; /* returned value of the temperature */
381 +/* message structure to be sent to videocore */
383 + uint32_t msg_size; /* simply, sizeof(struct vc_msg) */
384 + uint32_t request_code; /* holds various information like the success and number of bytes returned (refer to mailboxes wiki) */
385 + struct vc_msg_tag tag; /* the tag structure above to make */
386 + uint32_t end_tag; /* an end identifier, should be set to NULL */
389 +struct bcm2835_thermal_data {
390 + struct thermal_zone_device *thermal_dev;
394 +/* --- GLOBALS --- */
395 +static struct bcm2835_thermal_data bcm2835_data;
397 +/* Thermal Device Operations */
398 +static struct thermal_zone_device_ops ops;
400 +/* --- FUNCTIONS --- */
402 +static int bcm2835_get_temp_or_max(struct thermal_zone_device *thermal_dev, unsigned long *temp, unsigned tag_id)
404 + int result = -1, retry = 3;
408 + while (result != 0 && retry-- > 0) {
409 + /* wipe all previous message data */
410 + memset(&bcm2835_data.msg, 0, sizeof bcm2835_data.msg);
412 + /* prepare message */
413 + bcm2835_data.msg.msg_size = sizeof bcm2835_data.msg;
414 + bcm2835_data.msg.tag.buffer_size = 8;
415 + bcm2835_data.msg.tag.tag_id = tag_id;
417 + /* send the message */
418 + result = bcm_mailbox_property(&bcm2835_data.msg, sizeof bcm2835_data.msg);
419 + 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);
420 + if (!(bcm2835_data.msg.request_code & 0x80000000))
424 + /* check if it was all ok and return the rate in milli degrees C */
426 + *temp = (uint)bcm2835_data.msg.tag.val;
428 + print_err("Failed to get temperature! (%x:%d)\n", tag_id, result);
429 + print_debug("OUT");
433 +static int bcm2835_get_temp(struct thermal_zone_device *thermal_dev, unsigned long *temp)
435 + return bcm2835_get_temp_or_max(thermal_dev, temp, VC_TAG_GET_TEMP);
438 +static int bcm2835_get_max_temp(struct thermal_zone_device *thermal_dev, int trip_num, unsigned long *temp)
440 + return bcm2835_get_temp_or_max(thermal_dev, temp, VC_TAG_GET_MAX_TEMP);
443 +static int bcm2835_get_trip_type(struct thermal_zone_device * thermal_dev, int trip_num, enum thermal_trip_type *trip_type)
445 + *trip_type = THERMAL_TRIP_HOT;
450 +static int bcm2835_get_mode(struct thermal_zone_device *thermal_dev, enum thermal_device_mode *dev_mode)
452 + *dev_mode = THERMAL_DEVICE_ENABLED;
457 +static int bcm2835_thermal_probe(struct platform_device *pdev)
460 + print_debug("THERMAL Driver has been probed!");
462 + /* check that the device isn't null!*/
465 + print_debug("Platform device is empty!");
469 + if(!(bcm2835_data.thermal_dev = thermal_zone_device_register("bcm2835_thermal", 1, 0, NULL, &ops, NULL, 0, 0)))
471 + print_debug("Unable to register the thermal device!");
478 +static int bcm2835_thermal_remove(struct platform_device *pdev)
482 + thermal_zone_device_unregister(bcm2835_data.thermal_dev);
484 + print_debug("OUT");
489 +static struct thermal_zone_device_ops ops = {
490 + .get_temp = bcm2835_get_temp,
491 + .get_trip_temp = bcm2835_get_max_temp,
492 + .get_trip_type = bcm2835_get_trip_type,
493 + .get_mode = bcm2835_get_mode,
496 +/* Thermal Driver */
497 +static struct platform_driver bcm2835_thermal_driver = {
498 + .probe = bcm2835_thermal_probe,
499 + .remove = bcm2835_thermal_remove,
501 + .name = "bcm2835_thermal",
502 + .owner = THIS_MODULE,
506 +MODULE_LICENSE("GPL");
507 +MODULE_AUTHOR("Dorian Peake");
508 +MODULE_DESCRIPTION("Thermal driver for bcm2835 chip");
510 +module_platform_driver(bcm2835_thermal_driver);