brcm2708: update to v3.18
[15.05/openwrt.git] / target / linux / brcm2708 / patches-3.18 / 0017-Added-hwmon-thermal-driver-for-reporting-core-temper.patch
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
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 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[] = {
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 @@ -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]);
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 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
54           sensors.
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 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
78  
79  obj-$(CONFIG_PMBUS)            += pmbus/
80  
81 diff --git a/drivers/hwmon/bcm2835-hwmon.c b/drivers/hwmon/bcm2835-hwmon.c
82 new file mode 100644
83 index 0000000..5bbed45
84 --- /dev/null
85 +++ b/drivers/hwmon/bcm2835-hwmon.c
86 @@ -0,0 +1,219 @@
87 +/*****************************************************************************
88 +* Copyright 2011 Broadcom Corporation.  All rights reserved.
89 +*
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").
94 +*
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
98 +* consent.
99 +*****************************************************************************/
100 +
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>
111 +
112 +#define MODULE_NAME "bcm2835_hwmon"
113 +
114 +/*#define HWMON_DEBUG_ENABLE*/
115 +
116 +#ifdef HWMON_DEBUG_ENABLE
117 +#define print_debug(fmt,...) printk(KERN_INFO "%s:%s:%d: "fmt"\n", MODULE_NAME, __func__, __LINE__, ##__VA_ARGS__)
118 +#else
119 +#define print_debug(fmt,...)
120 +#endif
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__)
123 +
124 +#define VC_TAG_GET_TEMP 0x00030006
125 +#define VC_TAG_GET_MAX_TEMP 0x0003000A
126 +
127 +/* --- STRUCTS --- */
128 +struct bcm2835_hwmon_data {
129 +       struct device *hwmon_dev;
130 +};
131 +
132 +/* tag part of the message */
133 +struct vc_msg_tag {
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 */
139 +};
140 +
141 +/* message structure to be sent to videocore */
142 +struct vc_msg {
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 */
147 +};
148 +
149 +typedef enum {
150 +       TEMP,
151 +       MAX_TEMP,
152 +} temp_type;
153 +
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);
157 +
158 +/* --- GLOBALS --- */
159 +
160 +static struct bcm2835_hwmon_data *bcm2835_data;
161 +static struct platform_driver bcm2835_hwmon_driver;
162 +
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);
166 +
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,
171 +       NULL,
172 +};
173 +
174 +static struct attribute_group bcm2835_attr_group = {
175 +       .attrs = bcm2835_attributes,
176 +};
177 +
178 +/* --- FUNCTIONS --- */
179 +
180 +static ssize_t bcm2835_get_name(struct device *dev, struct device_attribute *attr, char *buf)
181 +{
182 +       return sprintf(buf,"bcm2835_hwmon\n");
183 +}
184 +
185 +static ssize_t bcm2835_get_temp(struct device *dev, struct device_attribute *attr, char *buf)
186 +{
187 +       struct vc_msg msg;
188 +       int result;
189 +       uint temp = 0;
190 +       int index = ((struct sensor_device_attribute*)to_sensor_dev_attr(attr))->index;
191 +
192 +       print_debug("IN");
193 +
194 +       /* wipe all previous message data */
195 +       memset(&msg, 0, sizeof msg);
196 +
197 +       /* determine the message type */
198 +       if(index == TEMP)
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;
202 +       else
203 +       {
204 +               print_debug("Unknown temperature message!");
205 +               return -EINVAL;
206 +       }
207 +
208 +       msg.msg_size = sizeof msg;
209 +       msg.tag.buffer_size = 8;
210 +
211 +       /* send the message */
212 +       result = bcm_mailbox_property(&msg, sizeof msg);
213 +
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
218 +       else
219 +               print_debug("Failed to get temperature!");
220 +       #endif
221 +       print_debug("Got temperature as %u",temp);
222 +       print_debug("OUT");
223 +       return sprintf(buf, "%u\n", temp);
224 +}
225 +
226 +
227 +static int bcm2835_hwmon_probe(struct platform_device *pdev)
228 +{
229 +       int err;
230 +
231 +       print_debug("IN");
232 +       print_debug("HWMON Driver has been probed!");
233 +
234 +       /* check that the device isn't null!*/
235 +       if(pdev == NULL)
236 +       {
237 +               print_debug("Platform device is empty!");
238 +               return -ENODEV;
239 +       }
240 +
241 +       /* allocate memory for neccessary data */
242 +       bcm2835_data = kzalloc(sizeof(struct bcm2835_hwmon_data),GFP_KERNEL);
243 +       if(!bcm2835_data)
244 +       {
245 +               print_debug("Unable to allocate memory for hwmon data!");
246 +               err = -ENOMEM;
247 +               goto kzalloc_error;
248 +       }
249 +
250 +       /* create the sysfs files */
251 +       if(sysfs_create_group(&pdev->dev.kobj, &bcm2835_attr_group))
252 +       {
253 +               print_debug("Unable to create sysfs files!");
254 +               err = -EFAULT;
255 +               goto sysfs_error;
256 +       }
257 +
258 +       /* register the hwmon device */
259 +       bcm2835_data->hwmon_dev = hwmon_device_register(&pdev->dev);
260 +       if (IS_ERR(bcm2835_data->hwmon_dev))
261 +       {
262 +               err = PTR_ERR(bcm2835_data->hwmon_dev);
263 +               goto hwmon_error;
264 +       }
265 +       print_debug("OUT");
266 +       return 0;
267 +
268 +       /* error goto's */
269 +       hwmon_error:
270 +       sysfs_remove_group(&pdev->dev.kobj, &bcm2835_attr_group);
271 +
272 +       sysfs_error:
273 +       kfree(bcm2835_data);
274 +
275 +       kzalloc_error:
276 +
277 +       return err;
278 +
279 +}
280 +
281 +static int bcm2835_hwmon_remove(struct platform_device *pdev)
282 +{
283 +       print_debug("IN");
284 +       hwmon_device_unregister(bcm2835_data->hwmon_dev);
285 +
286 +       sysfs_remove_group(&pdev->dev.kobj, &bcm2835_attr_group);
287 +       print_debug("OUT");
288 +       return 0;
289 +}
290 +
291 +/* Hwmon Driver */
292 +static struct platform_driver bcm2835_hwmon_driver = {
293 +       .probe = bcm2835_hwmon_probe,
294 +       .remove = bcm2835_hwmon_remove,
295 +       .driver = {
296 +                               .name = "bcm2835_hwmon",
297 +                               .owner = THIS_MODULE,
298 +                       },
299 +};
300 +
301 +MODULE_LICENSE("GPL");
302 +MODULE_AUTHOR("Dorian Peake");
303 +MODULE_DESCRIPTION("HW Monitor driver for bcm2835 chip");
304 +
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.
313  
314 +config THERMAL_BCM2835
315 +       tristate "BCM2835 Thermal Driver"
316 +       help
317 +         This will enable temperature monitoring for the Broadcom BCM2835
318 +         chip. If built as a module, it will be called 'bcm2835-thermal'.
319 +
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
336 new file mode 100644
337 index 0000000..85fceb5
338 --- /dev/null
339 +++ b/drivers/thermal/bcm2835-thermal.c
340 @@ -0,0 +1,184 @@
341 +/*****************************************************************************
342 +* Copyright 2011 Broadcom Corporation.  All rights reserved.
343 +*
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").
348 +*
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
352 +* consent.
353 +*****************************************************************************/
354 +
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>
363 +
364 +
365 +/* --- DEFINITIONS --- */
366 +#define MODULE_NAME "bcm2835_thermal"
367 +
368 +/*#define THERMAL_DEBUG_ENABLE*/
369 +
370 +#ifdef THERMAL_DEBUG_ENABLE
371 +#define print_debug(fmt,...) printk(KERN_INFO "%s:%s:%d: "fmt"\n", MODULE_NAME, __func__, __LINE__, ##__VA_ARGS__)
372 +#else
373 +#define print_debug(fmt,...)
374 +#endif
375 +#define print_err(fmt,...) printk(KERN_ERR "%s:%s:%d: "fmt"\n", MODULE_NAME, __func__,__LINE__, ##__VA_ARGS__)
376 +
377 +#define VC_TAG_GET_TEMP 0x00030006
378 +#define VC_TAG_GET_MAX_TEMP 0x0003000A
379 +
380 +typedef enum {
381 +       TEMP,
382 +       MAX_TEMP,
383 +} temp_type;
384 +
385 +/* --- STRUCTS --- */
386 +/* tag part of the message */
387 +struct vc_msg_tag {
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 */
393 +};
394 +
395 +/* message structure to be sent to videocore */
396 +struct vc_msg {
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 */
401 +};
402 +
403 +struct bcm2835_thermal_data {
404 +       struct thermal_zone_device *thermal_dev;
405 +       struct vc_msg msg;
406 +};
407 +
408 +/* --- GLOBALS --- */
409 +static struct bcm2835_thermal_data bcm2835_data;
410 +
411 +/* Thermal Device Operations */
412 +static struct thermal_zone_device_ops ops;
413 +
414 +/* --- FUNCTIONS --- */
415 +
416 +static int bcm2835_get_temp_or_max(struct thermal_zone_device *thermal_dev, unsigned long *temp, unsigned tag_id)
417 +{
418 +       int result = -1, retry = 3;
419 +       print_debug("IN");
420 +
421 +       *temp = 0;
422 +       while (result != 0 && retry-- > 0) {
423 +               /* wipe all previous message data */
424 +               memset(&bcm2835_data.msg, 0, sizeof bcm2835_data.msg);
425 +
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;
430 +
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))
435 +                       result = -1;
436 +       }
437 +
438 +       /* check if it was all ok and return the rate in milli degrees C */
439 +       if (result == 0)
440 +               *temp = (uint)bcm2835_data.msg.tag.val;
441 +       else
442 +               print_err("Failed to get temperature! (%x:%d)\n", tag_id, result);
443 +       print_debug("OUT");
444 +       return result;
445 +}
446 +
447 +static int bcm2835_get_temp(struct thermal_zone_device *thermal_dev, unsigned long *temp)
448 +{
449 +       return bcm2835_get_temp_or_max(thermal_dev, temp, VC_TAG_GET_TEMP);
450 +}
451 +
452 +static int bcm2835_get_max_temp(struct thermal_zone_device *thermal_dev, int trip_num, unsigned long *temp)
453 +{
454 +       return bcm2835_get_temp_or_max(thermal_dev, temp, VC_TAG_GET_MAX_TEMP);
455 +}
456 +
457 +static int bcm2835_get_trip_type(struct thermal_zone_device * thermal_dev, int trip_num, enum thermal_trip_type *trip_type)
458 +{
459 +       *trip_type = THERMAL_TRIP_HOT;
460 +       return 0;
461 +}
462 +
463 +
464 +static int bcm2835_get_mode(struct thermal_zone_device *thermal_dev, enum thermal_device_mode *dev_mode)
465 +{
466 +       *dev_mode = THERMAL_DEVICE_ENABLED;
467 +       return 0;
468 +}
469 +
470 +
471 +static int bcm2835_thermal_probe(struct platform_device *pdev)
472 +{
473 +       print_debug("IN");
474 +       print_debug("THERMAL Driver has been probed!");
475 +
476 +       /* check that the device isn't null!*/
477 +       if(pdev == NULL)
478 +       {
479 +               print_debug("Platform device is empty!");
480 +               return -ENODEV;
481 +       }
482 +
483 +       if(!(bcm2835_data.thermal_dev = thermal_zone_device_register("bcm2835_thermal",  1, 0, NULL, &ops, NULL, 0, 0)))
484 +       {
485 +               print_debug("Unable to register the thermal device!");
486 +               return -EFAULT;
487 +       }
488 +       return 0;
489 +}
490 +
491 +
492 +static int bcm2835_thermal_remove(struct platform_device *pdev)
493 +{
494 +       print_debug("IN");
495 +
496 +       thermal_zone_device_unregister(bcm2835_data.thermal_dev);
497 +
498 +       print_debug("OUT");
499 +
500 +       return 0;
501 +}
502 +
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,
508 +};
509 +
510 +/* Thermal Driver */
511 +static struct platform_driver bcm2835_thermal_driver = {
512 +       .probe = bcm2835_thermal_probe,
513 +       .remove = bcm2835_thermal_remove,
514 +       .driver = {
515 +                               .name = "bcm2835_thermal",
516 +                               .owner = THIS_MODULE,
517 +                       },
518 +};
519 +
520 +MODULE_LICENSE("GPL");
521 +MODULE_AUTHOR("Dorian Peake");
522 +MODULE_DESCRIPTION("Thermal driver for bcm2835 chip");
523 +
524 +module_platform_driver(bcm2835_thermal_driver);
525 -- 
526 1.8.3.2
527