1 From 4c7d4bffe1986d856b37460f87950dabe29c616e 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 033/196] 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 | 7 ++
13 drivers/thermal/Makefile | 1 +
14 drivers/thermal/bcm2835-thermal.c | 208 ++++++++++++++++++++++++++++++++++++
15 7 files changed, 457 insertions(+)
16 create mode 100644 drivers/hwmon/bcm2835-hwmon.c
17 create mode 100644 drivers/thermal/bcm2835-thermal.c
19 Index: linux-3.10.49/arch/arm/mach-bcm2708/bcm2708.c
20 ===================================================================
21 --- linux-3.10.49.orig/arch/arm/mach-bcm2708/bcm2708.c 2014-09-27 11:40:40.999449257 -0700
22 +++ linux-3.10.49/arch/arm/mach-bcm2708/bcm2708.c 2014-09-27 11:40:40.979449258 -0700
24 .resource = bcm2708_bsc1_resources,
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 bcm_register_device(&bcm2708_bsc0_device);
40 bcm_register_device(&bcm2708_bsc1_device);
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.10.49/drivers/hwmon/Kconfig
49 ===================================================================
50 --- linux-3.10.49.orig/drivers/hwmon/Kconfig 2014-09-27 11:40:40.999449257 -0700
51 +++ linux-3.10.49/drivers/hwmon/Kconfig 2014-09-27 11:40:40.979449258 -0700
52 @@ -1537,6 +1537,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.10.49/drivers/hwmon/bcm2835-hwmon.c
70 ===================================================================
71 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
72 +++ linux-3.10.49/drivers/hwmon/bcm2835-hwmon.c 2014-09-27 11:40:40.979449258 -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.10.49/drivers/thermal/Kconfig
294 ===================================================================
295 --- linux-3.10.49.orig/drivers/thermal/Kconfig 2014-09-27 11:40:40.999449257 -0700
296 +++ linux-3.10.49/drivers/thermal/Kconfig 2014-09-27 11:40:40.983449258 -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'.
309 Index: linux-3.10.49/drivers/thermal/Makefile
310 ===================================================================
311 --- linux-3.10.49.orig/drivers/thermal/Makefile 2014-09-27 11:40:40.999449257 -0700
312 +++ linux-3.10.49/drivers/thermal/Makefile 2014-09-27 11:40:40.983449258 -0700
314 obj-$(CONFIG_ARMADA_THERMAL) += armada_thermal.o
315 obj-$(CONFIG_DB8500_CPUFREQ_COOLING) += db8500_cpufreq_cooling.o
316 obj-$(CONFIG_INTEL_POWERCLAMP) += intel_powerclamp.o
317 +obj-$(CONFIG_THERMAL_BCM2835) += bcm2835-thermal.o
319 Index: linux-3.10.49/drivers/thermal/bcm2835-thermal.c
320 ===================================================================
321 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
322 +++ linux-3.10.49/drivers/thermal/bcm2835-thermal.c 2014-09-27 11:40:40.983449258 -0700
324 +/*****************************************************************************
325 +* Copyright 2011 Broadcom Corporation. All rights reserved.
327 +* Unless you and Broadcom execute a separate written software license
328 +* agreement governing use of this software, this software is licensed to you
329 +* under the terms of the GNU General Public License version 2, available at
330 +* http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
332 +* Notwithstanding the above, under no circumstances may you combine this
333 +* software in any way with any other Broadcom software provided under a
334 +* license other than the GPL, without Broadcom's express prior written
336 +*****************************************************************************/
338 +#include <linux/kernel.h>
339 +#include <linux/module.h>
340 +#include <linux/init.h>
341 +#include <linux/platform_device.h>
342 +#include <linux/slab.h>
343 +#include <linux/sysfs.h>
344 +#include <mach/vcio.h>
345 +#include <linux/thermal.h>
348 +/* --- DEFINITIONS --- */
349 +#define MODULE_NAME "bcm2835_thermal"
351 +/*#define THERMAL_DEBUG_ENABLE*/
353 +#ifdef THERMAL_DEBUG_ENABLE
354 +#define print_debug(fmt,...) printk(KERN_INFO "%s:%s:%d: "fmt"\n", MODULE_NAME, __func__, __LINE__, ##__VA_ARGS__)
356 +#define print_debug(fmt,...)
358 +#define print_err(fmt,...) printk(KERN_ERR "%s:%s:%d: "fmt"\n", MODULE_NAME, __func__,__LINE__, ##__VA_ARGS__)
359 +#define print_info(fmt,...) printk(KERN_INFO "%s: "fmt"\n", MODULE_NAME, ##__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 +/* --- PROTOTYPES --- */
393 +static int bcm2835_get_temp(struct thermal_zone_device *thermal_dev, unsigned long *);
394 +static int bcm2835_get_max_temp(struct thermal_zone_device *thermal_dev, int, unsigned long *);
395 +static int bcm2835_get_trip_type(struct thermal_zone_device *thermal_dev, int trip_num, enum thermal_trip_type *trip_type);
396 +static int bcm2835_get_mode(struct thermal_zone_device *thermal_dev, enum thermal_device_mode *dev_mode);
398 +/* --- GLOBALS --- */
399 +static struct bcm2835_thermal_data bcm2835_data;
401 +/* Thermal Device Operations */
402 +static struct thermal_zone_device_ops ops;
404 +/* --- FUNCTIONS --- */
405 +static int bcm2835_get_max_temp(struct thermal_zone_device *thermal_dev, int trip_num, unsigned long *temp)
411 + /* wipe all previous message data */
412 + memset(&bcm2835_data.msg, 0, sizeof bcm2835_data.msg);
414 + /* prepare message */
415 + bcm2835_data.msg.msg_size = sizeof bcm2835_data.msg;
416 + bcm2835_data.msg.tag.buffer_size = 8;
417 + bcm2835_data.msg.tag.tag_id = VC_TAG_GET_MAX_TEMP;
419 + /* send the message */
420 + result = bcm_mailbox_property(&bcm2835_data.msg, sizeof bcm2835_data.msg);
422 + /* check if it was all ok and return the rate in milli degrees C */
423 + if (result == 0 && (bcm2835_data.msg.request_code & 0x80000000))
424 + *temp = (uint)bcm2835_data.msg.tag.val;
425 + #ifdef THERMAL_DEBUG_ENABLE
427 + print_debug("Failed to get temperature!");
429 + print_debug("Got temperature as %u",(uint)*temp);
430 + print_debug("OUT");
434 +static int bcm2835_get_temp(struct thermal_zone_device *thermal_dev, unsigned long *temp)
440 + /* wipe all previous message data */
441 + memset(&bcm2835_data.msg, 0, sizeof bcm2835_data.msg);
443 + /* prepare message */
444 + bcm2835_data.msg.msg_size = sizeof bcm2835_data.msg;
445 + bcm2835_data.msg.tag.buffer_size = 8;
446 + bcm2835_data.msg.tag.tag_id = VC_TAG_GET_TEMP;
448 + /* send the message */
449 + result = bcm_mailbox_property(&bcm2835_data.msg, sizeof bcm2835_data.msg);
451 + /* check if it was all ok and return the rate in milli degrees C */
452 + if (result == 0 && (bcm2835_data.msg.request_code & 0x80000000))
453 + *temp = (uint)bcm2835_data.msg.tag.val;
454 + #ifdef THERMAL_DEBUG_ENABLE
456 + print_debug("Failed to get temperature!");
458 + print_debug("Got temperature as %u",(uint)*temp);
459 + print_debug("OUT");
464 +static int bcm2835_get_trip_type(struct thermal_zone_device * thermal_dev, int trip_num, enum thermal_trip_type *trip_type)
466 + *trip_type = THERMAL_TRIP_HOT;
471 +static int bcm2835_get_mode(struct thermal_zone_device *thermal_dev, enum thermal_device_mode *dev_mode)
473 + *dev_mode = THERMAL_DEVICE_ENABLED;
478 +static int bcm2835_thermal_probe(struct platform_device *pdev)
481 + print_debug("THERMAL Driver has been probed!");
483 + /* check that the device isn't null!*/
486 + print_debug("Platform device is empty!");
490 + if(!(bcm2835_data.thermal_dev = thermal_zone_device_register("bcm2835_thermal", 1, 0, NULL, &ops, NULL, 0, 0)))
492 + print_debug("Unable to register the thermal device!");
499 +static int bcm2835_thermal_remove(struct platform_device *pdev)
503 + thermal_zone_device_unregister(bcm2835_data.thermal_dev);
505 + print_debug("OUT");
510 +static struct thermal_zone_device_ops ops = {
511 + .get_temp = bcm2835_get_temp,
512 + .get_trip_temp = bcm2835_get_max_temp,
513 + .get_trip_type = bcm2835_get_trip_type,
514 + .get_mode = bcm2835_get_mode,
517 +/* Thermal Driver */
518 +static struct platform_driver bcm2835_thermal_driver = {
519 + .probe = bcm2835_thermal_probe,
520 + .remove = bcm2835_thermal_remove,
522 + .name = "bcm2835_thermal",
523 + .owner = THIS_MODULE,
527 +MODULE_LICENSE("GPL");
528 +MODULE_AUTHOR("Dorian Peake");
529 +MODULE_DESCRIPTION("Thermal driver for bcm2835 chip");
531 +module_platform_driver(bcm2835_thermal_driver);