cb4214e70eabadf04d300c5749ba0aef6bd438b7
[openwrt.git] / target / linux / brcm2708 / patches-3.14 / 0015-Added-hwmon-thermal-driver-for-reporting-core-temper.patch
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
6
7 ---
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
18
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
23 @@ -483,6 +483,14 @@
24                },
25  };
26  
27 +static struct platform_device bcm2835_hwmon_device = {
28 +       .name = "bcm2835_hwmon",
29 +};
30 +
31 +static struct platform_device bcm2835_thermal_device = {
32 +       .name = "bcm2835_thermal",
33 +};
34 +
35  int __init bcm_register_device(struct platform_device *pdev)
36  {
37         int ret;
38 @@ -594,6 +602,9 @@
39         for (i = 0; i < ARRAY_SIZE(bcm2708_alsa_devices); i++)
40                 bcm_register_device(&bcm2708_alsa_devices[i]);
41  
42 +       bcm_register_device(&bcm2835_hwmon_device);
43 +       bcm_register_device(&bcm2835_thermal_device);
44 +
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 @@
53          help
54            Support for the A/D converter on MC13783 and MC13892 PMIC.
55  
56 +config SENSORS_BCM2835
57 +       depends on THERMAL_BCM2835=n
58 +       tristate "Broadcom BCM2835 HWMON Driver"
59 +       help
60 +         If you say yes here you get support for the hardware
61 +         monitoring features of the BCM2835 Chip
62 +
63 +         This driver can also be built as a module.  If so, the module
64 +         will be called bcm2835-hwmon.
65 +
66  if ACPI
67  
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
73 @@ -0,0 +1,219 @@
74 +/*****************************************************************************
75 +* Copyright 2011 Broadcom Corporation.  All rights reserved.
76 +*
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").
81 +*
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
85 +* consent.
86 +*****************************************************************************/
87 +
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>
98 +
99 +#define MODULE_NAME "bcm2835_hwmon"
100 +
101 +/*#define HWMON_DEBUG_ENABLE*/
102 +
103 +#ifdef HWMON_DEBUG_ENABLE
104 +#define print_debug(fmt,...) printk(KERN_INFO "%s:%s:%d: "fmt"\n", MODULE_NAME, __func__, __LINE__, ##__VA_ARGS__)
105 +#else
106 +#define print_debug(fmt,...)
107 +#endif
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__)
110 +
111 +#define VC_TAG_GET_TEMP 0x00030006
112 +#define VC_TAG_GET_MAX_TEMP 0x0003000A
113 +
114 +/* --- STRUCTS --- */
115 +struct bcm2835_hwmon_data {
116 +       struct device *hwmon_dev;
117 +};
118 +
119 +/* tag part of the message */
120 +struct vc_msg_tag {
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 */
126 +};
127 +
128 +/* message structure to be sent to videocore */
129 +struct vc_msg {
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 */
134 +};
135 +
136 +typedef enum {
137 +       TEMP,
138 +       MAX_TEMP,
139 +} temp_type;
140 +
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);
144 +
145 +/* --- GLOBALS --- */
146 +
147 +static struct bcm2835_hwmon_data *bcm2835_data;
148 +static struct platform_driver bcm2835_hwmon_driver;
149 +
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);
153 +
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,
158 +       NULL,
159 +};
160 +
161 +static struct attribute_group bcm2835_attr_group = {
162 +       .attrs = bcm2835_attributes,
163 +};
164 +
165 +/* --- FUNCTIONS --- */
166 +
167 +static ssize_t bcm2835_get_name(struct device *dev, struct device_attribute *attr, char *buf)
168 +{
169 +       return sprintf(buf,"bcm2835_hwmon\n");
170 +}
171 +
172 +static ssize_t bcm2835_get_temp(struct device *dev, struct device_attribute *attr, char *buf)
173 +{
174 +       struct vc_msg msg;
175 +       int result;
176 +       uint temp = 0;
177 +       int index = ((struct sensor_device_attribute*)to_sensor_dev_attr(attr))->index;
178 +
179 +       print_debug("IN");
180 +
181 +       /* wipe all previous message data */
182 +       memset(&msg, 0, sizeof msg);
183 +
184 +       /* determine the message type */
185 +       if(index == TEMP)
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;
189 +       else
190 +       {
191 +               print_debug("Unknown temperature message!");
192 +               return -EINVAL;
193 +       }
194 +
195 +       msg.msg_size = sizeof msg;
196 +       msg.tag.buffer_size = 8;
197 +
198 +       /* send the message */
199 +       result = bcm_mailbox_property(&msg, sizeof msg);
200 +
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
205 +       else
206 +               print_debug("Failed to get temperature!");
207 +       #endif
208 +       print_debug("Got temperature as %u",temp);
209 +       print_debug("OUT");
210 +       return sprintf(buf, "%u\n", temp);
211 +}
212 +
213 +
214 +static int bcm2835_hwmon_probe(struct platform_device *pdev)
215 +{
216 +       int err;
217 +
218 +       print_debug("IN");
219 +       print_debug("HWMON Driver has been probed!");
220 +
221 +       /* check that the device isn't null!*/
222 +       if(pdev == NULL)
223 +       {
224 +               print_debug("Platform device is empty!");
225 +               return -ENODEV;
226 +       }
227 +
228 +       /* allocate memory for neccessary data */
229 +       bcm2835_data = kzalloc(sizeof(struct bcm2835_hwmon_data),GFP_KERNEL);
230 +       if(!bcm2835_data)
231 +       {
232 +               print_debug("Unable to allocate memory for hwmon data!");
233 +               err = -ENOMEM;
234 +               goto kzalloc_error;
235 +       }
236 +
237 +       /* create the sysfs files */
238 +       if(sysfs_create_group(&pdev->dev.kobj, &bcm2835_attr_group))
239 +       {
240 +               print_debug("Unable to create sysfs files!");
241 +               err = -EFAULT;
242 +               goto sysfs_error;
243 +       }
244 +
245 +       /* register the hwmon device */
246 +       bcm2835_data->hwmon_dev = hwmon_device_register(&pdev->dev);
247 +       if (IS_ERR(bcm2835_data->hwmon_dev))
248 +       {
249 +               err = PTR_ERR(bcm2835_data->hwmon_dev);
250 +               goto hwmon_error;
251 +       }
252 +       print_debug("OUT");
253 +       return 0;
254 +
255 +       /* error goto's */
256 +       hwmon_error:
257 +       sysfs_remove_group(&pdev->dev.kobj, &bcm2835_attr_group);
258 +
259 +       sysfs_error:
260 +       kfree(bcm2835_data);
261 +
262 +       kzalloc_error:
263 +
264 +       return err;
265 +
266 +}
267 +
268 +static int bcm2835_hwmon_remove(struct platform_device *pdev)
269 +{
270 +       print_debug("IN");
271 +       hwmon_device_unregister(bcm2835_data->hwmon_dev);
272 +
273 +       sysfs_remove_group(&pdev->dev.kobj, &bcm2835_attr_group);
274 +       print_debug("OUT");
275 +       return 0;
276 +}
277 +
278 +/* Hwmon Driver */
279 +static struct platform_driver bcm2835_hwmon_driver = {
280 +       .probe = bcm2835_hwmon_probe,
281 +       .remove = bcm2835_hwmon_remove,
282 +       .driver = {
283 +                               .name = "bcm2835_hwmon",
284 +                               .owner = THIS_MODULE,
285 +                       },
286 +};
287 +
288 +MODULE_LICENSE("GPL");
289 +MODULE_AUTHOR("Dorian Peake");
290 +MODULE_DESCRIPTION("HW Monitor driver for bcm2835 chip");
291 +
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
297 @@ -196,6 +196,12 @@
298           enforce idle time which results in more package C-state residency. The
299           user interface is exposed via generic thermal framework.
300  
301 +config THERMAL_BCM2835
302 +       tristate "BCM2835 Thermal Driver"
303 +       help
304 +         This will enable temperature monitoring for the Broadcom BCM2835
305 +         chip. If built as a module, it will be called 'bcm2835-thermal'.
306 +
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
314 @@ -28,6 +28,7 @@
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
326 @@ -0,0 +1,184 @@
327 +/*****************************************************************************
328 +* Copyright 2011 Broadcom Corporation.  All rights reserved.
329 +*
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").
334 +*
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
338 +* consent.
339 +*****************************************************************************/
340 +
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>
349 +
350 +
351 +/* --- DEFINITIONS --- */
352 +#define MODULE_NAME "bcm2835_thermal"
353 +
354 +/*#define THERMAL_DEBUG_ENABLE*/
355 +
356 +#ifdef THERMAL_DEBUG_ENABLE
357 +#define print_debug(fmt,...) printk(KERN_INFO "%s:%s:%d: "fmt"\n", MODULE_NAME, __func__, __LINE__, ##__VA_ARGS__)
358 +#else
359 +#define print_debug(fmt,...)
360 +#endif
361 +#define print_err(fmt,...) printk(KERN_ERR "%s:%s:%d: "fmt"\n", MODULE_NAME, __func__,__LINE__, ##__VA_ARGS__)
362 +
363 +#define VC_TAG_GET_TEMP 0x00030006
364 +#define VC_TAG_GET_MAX_TEMP 0x0003000A
365 +
366 +typedef enum {
367 +       TEMP,
368 +       MAX_TEMP,
369 +} temp_type;
370 +
371 +/* --- STRUCTS --- */
372 +/* tag part of the message */
373 +struct vc_msg_tag {
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 */
379 +};
380 +
381 +/* message structure to be sent to videocore */
382 +struct vc_msg {
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 */
387 +};
388 +
389 +struct bcm2835_thermal_data {
390 +       struct thermal_zone_device *thermal_dev;
391 +       struct vc_msg msg;
392 +};
393 +
394 +/* --- GLOBALS --- */
395 +static struct bcm2835_thermal_data bcm2835_data;
396 +
397 +/* Thermal Device Operations */
398 +static struct thermal_zone_device_ops ops;
399 +
400 +/* --- FUNCTIONS --- */
401 +
402 +static int bcm2835_get_temp_or_max(struct thermal_zone_device *thermal_dev, unsigned long *temp, unsigned tag_id)
403 +{
404 +       int result = -1, retry = 3;
405 +       print_debug("IN");
406 +
407 +       *temp = 0;
408 +       while (result != 0 && retry-- > 0) {
409 +               /* wipe all previous message data */
410 +               memset(&bcm2835_data.msg, 0, sizeof bcm2835_data.msg);
411 +
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;
416 +
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))
421 +                       result = -1;
422 +       }
423 +
424 +       /* check if it was all ok and return the rate in milli degrees C */
425 +       if (result == 0)
426 +               *temp = (uint)bcm2835_data.msg.tag.val;
427 +       else
428 +               print_err("Failed to get temperature! (%x:%d)\n", tag_id, result);
429 +       print_debug("OUT");
430 +       return result;
431 +}
432 +
433 +static int bcm2835_get_temp(struct thermal_zone_device *thermal_dev, unsigned long *temp)
434 +{
435 +       return bcm2835_get_temp_or_max(thermal_dev, temp, VC_TAG_GET_TEMP);
436 +}
437 +
438 +static int bcm2835_get_max_temp(struct thermal_zone_device *thermal_dev, int trip_num, unsigned long *temp)
439 +{
440 +       return bcm2835_get_temp_or_max(thermal_dev, temp, VC_TAG_GET_MAX_TEMP);
441 +}
442 +
443 +static int bcm2835_get_trip_type(struct thermal_zone_device * thermal_dev, int trip_num, enum thermal_trip_type *trip_type)
444 +{
445 +       *trip_type = THERMAL_TRIP_HOT;
446 +       return 0;
447 +}
448 +
449 +
450 +static int bcm2835_get_mode(struct thermal_zone_device *thermal_dev, enum thermal_device_mode *dev_mode)
451 +{
452 +       *dev_mode = THERMAL_DEVICE_ENABLED;
453 +       return 0;
454 +}
455 +
456 +
457 +static int bcm2835_thermal_probe(struct platform_device *pdev)
458 +{
459 +       print_debug("IN");
460 +       print_debug("THERMAL Driver has been probed!");
461 +
462 +       /* check that the device isn't null!*/
463 +       if(pdev == NULL)
464 +       {
465 +               print_debug("Platform device is empty!");
466 +               return -ENODEV;
467 +       }
468 +
469 +       if(!(bcm2835_data.thermal_dev = thermal_zone_device_register("bcm2835_thermal",  1, 0, NULL, &ops, NULL, 0, 0)))
470 +       {
471 +               print_debug("Unable to register the thermal device!");
472 +               return -EFAULT;
473 +       }
474 +       return 0;
475 +}
476 +
477 +
478 +static int bcm2835_thermal_remove(struct platform_device *pdev)
479 +{
480 +       print_debug("IN");
481 +
482 +       thermal_zone_device_unregister(bcm2835_data.thermal_dev);
483 +
484 +       print_debug("OUT");
485 +
486 +       return 0;
487 +}
488 +
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,
494 +};
495 +
496 +/* Thermal Driver */
497 +static struct platform_driver bcm2835_thermal_driver = {
498 +       .probe = bcm2835_thermal_probe,
499 +       .remove = bcm2835_thermal_remove,
500 +       .driver = {
501 +                               .name = "bcm2835_thermal",
502 +                               .owner = THIS_MODULE,
503 +                       },
504 +};
505 +
506 +MODULE_LICENSE("GPL");
507 +MODULE_AUTHOR("Dorian Peake");
508 +MODULE_DESCRIPTION("Thermal driver for bcm2835 chip");
509 +
510 +module_platform_driver(bcm2835_thermal_driver);