brcm2708: update against latest rpi-3.10.y branch
[openwrt.git] / target / linux / brcm2708 / patches-3.10 / 0033-Added-hwmon-thermal-driver-for-reporting-core-temper.patch
1 From 40934fa5ae748fab27146883be929d4190b35c16 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/174] 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           |   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
18
19 --- a/arch/arm/mach-bcm2708/bcm2708.c
20 +++ b/arch/arm/mach-bcm2708/bcm2708.c
21 @@ -562,6 +562,14 @@ static struct platform_device bcm2708_bs
22         .resource = bcm2708_bsc1_resources,
23  };
24  
25 +static struct platform_device bcm2835_hwmon_device = {
26 +       .name = "bcm2835_hwmon",
27 +};
28 +
29 +static struct platform_device bcm2835_thermal_device = {
30 +       .name = "bcm2835_thermal",
31 +};
32 +
33  int __init bcm_register_device(struct platform_device *pdev)
34  {
35         int ret;
36 @@ -682,6 +690,9 @@ void __init bcm2708_init(void)
37         bcm_register_device(&bcm2708_bsc0_device);
38         bcm_register_device(&bcm2708_bsc1_device);
39  
40 +       bcm_register_device(&bcm2835_hwmon_device);
41 +       bcm_register_device(&bcm2835_thermal_device);
42 +
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 @@ -1537,6 +1537,16 @@ config SENSORS_MC13783_ADC
49          help
50            Support for the A/D converter on MC13783 and MC13892 PMIC.
51  
52 +config SENSORS_BCM2835
53 +       depends on THERMAL_BCM2835=n
54 +       tristate "Broadcom BCM2835 HWMON Driver"
55 +       help
56 +         If you say yes here you get support for the hardware
57 +         monitoring features of the BCM2835 Chip
58 +
59 +         This driver can also be built as a module.  If so, the module
60 +         will be called bcm2835-hwmon.
61 +
62  if ACPI
63  
64  comment "ACPI drivers"
65 --- /dev/null
66 +++ b/drivers/hwmon/bcm2835-hwmon.c
67 @@ -0,0 +1,219 @@
68 +/*****************************************************************************
69 +* Copyright 2011 Broadcom Corporation.  All rights reserved.
70 +*
71 +* Unless you and Broadcom execute a separate written software license
72 +* agreement governing use of this software, this software is licensed to you
73 +* under the terms of the GNU General Public License version 2, available at
74 +* http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
75 +*
76 +* Notwithstanding the above, under no circumstances may you combine this
77 +* software in any way with any other Broadcom software provided under a
78 +* license other than the GPL, without Broadcom's express prior written
79 +* consent.
80 +*****************************************************************************/
81 +
82 +#include <linux/kernel.h>
83 +#include <linux/module.h>
84 +#include <linux/init.h>
85 +#include <linux/hwmon.h>
86 +#include <linux/hwmon-sysfs.h>
87 +#include <linux/platform_device.h>
88 +#include <linux/sysfs.h>
89 +#include <mach/vcio.h>
90 +#include <linux/slab.h>
91 +#include <linux/err.h>
92 +
93 +#define MODULE_NAME "bcm2835_hwmon"
94 +
95 +/*#define HWMON_DEBUG_ENABLE*/
96 +
97 +#ifdef HWMON_DEBUG_ENABLE
98 +#define print_debug(fmt,...) printk(KERN_INFO "%s:%s:%d: "fmt"\n", MODULE_NAME, __func__, __LINE__, ##__VA_ARGS__)
99 +#else
100 +#define print_debug(fmt,...)
101 +#endif
102 +#define print_err(fmt,...) printk(KERN_ERR "%s:%s:%d: "fmt"\n", MODULE_NAME, __func__,__LINE__, ##__VA_ARGS__)
103 +#define print_info(fmt,...) printk(KERN_INFO "%s: "fmt"\n", MODULE_NAME, ##__VA_ARGS__)
104 +
105 +#define VC_TAG_GET_TEMP 0x00030006
106 +#define VC_TAG_GET_MAX_TEMP 0x0003000A
107 +
108 +/* --- STRUCTS --- */
109 +struct bcm2835_hwmon_data {
110 +       struct device *hwmon_dev;
111 +};
112 +
113 +/* tag part of the message */
114 +struct vc_msg_tag {
115 +       uint32_t tag_id;                /* the tag ID for the temperature */
116 +       uint32_t buffer_size;   /* size of the buffer (should be 8) */
117 +       uint32_t request_code;  /* identifies message as a request (should be 0) */
118 +       uint32_t id;                    /* extra ID field (should be 0) */
119 +       uint32_t val;                   /* returned value of the temperature */
120 +};
121 +
122 +/* message structure to be sent to videocore */
123 +struct vc_msg {
124 +       uint32_t msg_size;              /* simply, sizeof(struct vc_msg) */
125 +       uint32_t request_code;          /* holds various information like the success and number of bytes returned (refer to mailboxes wiki) */
126 +       struct vc_msg_tag tag;          /* the tag structure above to make */
127 +       uint32_t end_tag;               /* an end identifier, should be set to NULL */
128 +};
129 +
130 +typedef enum {
131 +       TEMP,
132 +       MAX_TEMP,
133 +} temp_type;
134 +
135 +/* --- PROTOTYPES --- */
136 +static ssize_t bcm2835_get_temp(struct device *dev, struct device_attribute *attr, char *buf);
137 +static ssize_t bcm2835_get_name(struct device *dev, struct device_attribute *attr, char *buf);
138 +
139 +/* --- GLOBALS --- */
140 +
141 +static struct bcm2835_hwmon_data *bcm2835_data;
142 +static struct platform_driver bcm2835_hwmon_driver;
143 +
144 +static SENSOR_DEVICE_ATTR(name, S_IRUGO,bcm2835_get_name,NULL,0);
145 +static SENSOR_DEVICE_ATTR(temp1_input,S_IRUGO,bcm2835_get_temp,NULL,TEMP);
146 +static SENSOR_DEVICE_ATTR(temp1_max,S_IRUGO,bcm2835_get_temp,NULL,MAX_TEMP);
147 +
148 +static struct attribute* bcm2835_attributes[] = {
149 +       &sensor_dev_attr_name.dev_attr.attr,
150 +       &sensor_dev_attr_temp1_input.dev_attr.attr,
151 +       &sensor_dev_attr_temp1_max.dev_attr.attr,
152 +       NULL,
153 +};
154 +
155 +static struct attribute_group bcm2835_attr_group = {
156 +       .attrs = bcm2835_attributes,
157 +};
158 +
159 +/* --- FUNCTIONS --- */
160 +
161 +static ssize_t bcm2835_get_name(struct device *dev, struct device_attribute *attr, char *buf)
162 +{
163 +       return sprintf(buf,"bcm2835_hwmon\n");
164 +}
165 +
166 +static ssize_t bcm2835_get_temp(struct device *dev, struct device_attribute *attr, char *buf)
167 +{
168 +       struct vc_msg msg;
169 +       int result;
170 +       uint temp = 0;
171 +       int index = ((struct sensor_device_attribute*)to_sensor_dev_attr(attr))->index;
172 +
173 +       print_debug("IN");
174 +
175 +       /* wipe all previous message data */
176 +       memset(&msg, 0, sizeof msg);
177 +
178 +       /* determine the message type */
179 +       if(index == TEMP)
180 +               msg.tag.tag_id = VC_TAG_GET_TEMP;
181 +       else if (index == MAX_TEMP)
182 +               msg.tag.tag_id = VC_TAG_GET_MAX_TEMP;
183 +       else
184 +       {
185 +               print_debug("Unknown temperature message!");
186 +               return -EINVAL;
187 +       }
188 +
189 +       msg.msg_size = sizeof msg;
190 +       msg.tag.buffer_size = 8;
191 +
192 +       /* send the message */
193 +       result = bcm_mailbox_property(&msg, sizeof msg);
194 +
195 +       /* check if it was all ok and return the rate in milli degrees C */
196 +       if (result == 0 && (msg.request_code & 0x80000000))
197 +               temp = (uint)msg.tag.val;
198 +       #ifdef HWMON_DEBUG_ENABLE
199 +       else
200 +               print_debug("Failed to get temperature!");
201 +       #endif
202 +       print_debug("Got temperature as %u",temp);
203 +       print_debug("OUT");
204 +       return sprintf(buf, "%u\n", temp);
205 +}
206 +
207 +
208 +static int bcm2835_hwmon_probe(struct platform_device *pdev)
209 +{
210 +       int err;
211 +
212 +       print_debug("IN");
213 +       print_debug("HWMON Driver has been probed!");
214 +
215 +       /* check that the device isn't null!*/
216 +       if(pdev == NULL)
217 +       {
218 +               print_debug("Platform device is empty!");
219 +               return -ENODEV;
220 +       }
221 +
222 +       /* allocate memory for neccessary data */
223 +       bcm2835_data = kzalloc(sizeof(struct bcm2835_hwmon_data),GFP_KERNEL);
224 +       if(!bcm2835_data)
225 +       {
226 +               print_debug("Unable to allocate memory for hwmon data!");
227 +               err = -ENOMEM;
228 +               goto kzalloc_error;
229 +       }
230 +
231 +       /* create the sysfs files */
232 +       if(sysfs_create_group(&pdev->dev.kobj, &bcm2835_attr_group))
233 +       {
234 +               print_debug("Unable to create sysfs files!");
235 +               err = -EFAULT;
236 +               goto sysfs_error;
237 +       }
238 +
239 +       /* register the hwmon device */
240 +       bcm2835_data->hwmon_dev = hwmon_device_register(&pdev->dev);
241 +       if (IS_ERR(bcm2835_data->hwmon_dev))
242 +       {
243 +               err = PTR_ERR(bcm2835_data->hwmon_dev);
244 +               goto hwmon_error;
245 +       }
246 +       print_debug("OUT");
247 +       return 0;
248 +
249 +       /* error goto's */
250 +       hwmon_error:
251 +       sysfs_remove_group(&pdev->dev.kobj, &bcm2835_attr_group);
252 +
253 +       sysfs_error:
254 +       kfree(bcm2835_data);
255 +
256 +       kzalloc_error:
257 +
258 +       return err;
259 +
260 +}
261 +
262 +static int bcm2835_hwmon_remove(struct platform_device *pdev)
263 +{
264 +       print_debug("IN");
265 +       hwmon_device_unregister(bcm2835_data->hwmon_dev);
266 +
267 +       sysfs_remove_group(&pdev->dev.kobj, &bcm2835_attr_group);
268 +       print_debug("OUT");
269 +       return 0;
270 +}
271 +
272 +/* Hwmon Driver */
273 +static struct platform_driver bcm2835_hwmon_driver = {
274 +       .probe = bcm2835_hwmon_probe,
275 +       .remove = bcm2835_hwmon_remove,
276 +       .driver = {
277 +                               .name = "bcm2835_hwmon",
278 +                               .owner = THIS_MODULE,
279 +                       },
280 +};
281 +
282 +MODULE_LICENSE("GPL");
283 +MODULE_AUTHOR("Dorian Peake");
284 +MODULE_DESCRIPTION("HW Monitor driver for bcm2835 chip");
285 +
286 +module_platform_driver(bcm2835_hwmon_driver);
287 --- a/drivers/thermal/Kconfig
288 +++ b/drivers/thermal/Kconfig
289 @@ -169,4 +169,11 @@ config INTEL_POWERCLAMP
290           enforce idle time which results in more package C-state residency. The
291           user interface is exposed via generic thermal framework.
292  
293 +config THERMAL_BCM2835
294 +       tristate "BCM2835 Thermal Driver"
295 +       help
296 +         This will enable temperature monitoring for the Broadcom BCM2835
297 +         chip. If built as a module, it will be called 'bcm2835-thermal'.
298 +
299  endif
300 +
301 --- a/drivers/thermal/Makefile
302 +++ b/drivers/thermal/Makefile
303 @@ -23,4 +23,5 @@ obj-$(CONFIG_DB8500_THERMAL)  += db8500_t
304  obj-$(CONFIG_ARMADA_THERMAL)   += armada_thermal.o
305  obj-$(CONFIG_DB8500_CPUFREQ_COOLING)   += db8500_cpufreq_cooling.o
306  obj-$(CONFIG_INTEL_POWERCLAMP) += intel_powerclamp.o
307 +obj-$(CONFIG_THERMAL_BCM2835)  += bcm2835-thermal.o
308  
309 --- /dev/null
310 +++ b/drivers/thermal/bcm2835-thermal.c
311 @@ -0,0 +1,208 @@
312 +/*****************************************************************************
313 +* Copyright 2011 Broadcom Corporation.  All rights reserved.
314 +*
315 +* Unless you and Broadcom execute a separate written software license
316 +* agreement governing use of this software, this software is licensed to you
317 +* under the terms of the GNU General Public License version 2, available at
318 +* http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
319 +*
320 +* Notwithstanding the above, under no circumstances may you combine this
321 +* software in any way with any other Broadcom software provided under a
322 +* license other than the GPL, without Broadcom's express prior written
323 +* consent.
324 +*****************************************************************************/
325 +
326 +#include <linux/kernel.h>
327 +#include <linux/module.h>
328 +#include <linux/init.h>
329 +#include <linux/platform_device.h>
330 +#include <linux/slab.h>
331 +#include <linux/sysfs.h>
332 +#include <mach/vcio.h>
333 +#include <linux/thermal.h>
334 +
335 +
336 +/* --- DEFINITIONS --- */
337 +#define MODULE_NAME "bcm2835_thermal"
338 +
339 +/*#define THERMAL_DEBUG_ENABLE*/
340 +
341 +#ifdef THERMAL_DEBUG_ENABLE
342 +#define print_debug(fmt,...) printk(KERN_INFO "%s:%s:%d: "fmt"\n", MODULE_NAME, __func__, __LINE__, ##__VA_ARGS__)
343 +#else
344 +#define print_debug(fmt,...)
345 +#endif
346 +#define print_err(fmt,...) printk(KERN_ERR "%s:%s:%d: "fmt"\n", MODULE_NAME, __func__,__LINE__, ##__VA_ARGS__)
347 +#define print_info(fmt,...) printk(KERN_INFO "%s: "fmt"\n", MODULE_NAME, ##__VA_ARGS__)
348 +
349 +#define VC_TAG_GET_TEMP 0x00030006
350 +#define VC_TAG_GET_MAX_TEMP 0x0003000A
351 +
352 +typedef enum {
353 +       TEMP,
354 +       MAX_TEMP,
355 +} temp_type;
356 +
357 +/* --- STRUCTS --- */
358 +/* tag part of the message */
359 +struct vc_msg_tag {
360 +       uint32_t tag_id;                /* the tag ID for the temperature */
361 +       uint32_t buffer_size;   /* size of the buffer (should be 8) */
362 +       uint32_t request_code;  /* identifies message as a request (should be 0) */
363 +       uint32_t id;                    /* extra ID field (should be 0) */
364 +       uint32_t val;                   /* returned value of the temperature */
365 +};
366 +
367 +/* message structure to be sent to videocore */
368 +struct vc_msg {
369 +       uint32_t msg_size;              /* simply, sizeof(struct vc_msg) */
370 +       uint32_t request_code;          /* holds various information like the success and number of bytes returned (refer to mailboxes wiki) */
371 +       struct vc_msg_tag tag;          /* the tag structure above to make */
372 +       uint32_t end_tag;               /* an end identifier, should be set to NULL */
373 +};
374 +
375 +struct bcm2835_thermal_data {
376 +       struct thermal_zone_device *thermal_dev;
377 +       struct vc_msg msg;
378 +};
379 +
380 +/* --- PROTOTYPES --- */
381 +static int bcm2835_get_temp(struct thermal_zone_device *thermal_dev, unsigned long *);
382 +static int bcm2835_get_max_temp(struct thermal_zone_device *thermal_dev, int, unsigned long *);
383 +static int bcm2835_get_trip_type(struct thermal_zone_device *thermal_dev, int trip_num, enum thermal_trip_type *trip_type);
384 +static int bcm2835_get_mode(struct thermal_zone_device *thermal_dev, enum thermal_device_mode *dev_mode);
385 +
386 +/* --- GLOBALS --- */
387 +static struct bcm2835_thermal_data bcm2835_data;
388 +
389 +/* Thermal Device Operations */
390 +static struct thermal_zone_device_ops ops;
391 +
392 +/* --- FUNCTIONS --- */
393 +static int bcm2835_get_max_temp(struct thermal_zone_device *thermal_dev, int trip_num, unsigned long *temp)
394 +{
395 +       int result;
396 +
397 +       print_debug("IN");
398 +
399 +       /* wipe all previous message data */
400 +       memset(&bcm2835_data.msg, 0, sizeof bcm2835_data.msg);
401 +
402 +       /* prepare message */
403 +       bcm2835_data.msg.msg_size = sizeof bcm2835_data.msg;
404 +       bcm2835_data.msg.tag.buffer_size = 8;
405 +       bcm2835_data.msg.tag.tag_id = VC_TAG_GET_MAX_TEMP;
406 +
407 +       /* send the message */
408 +       result = bcm_mailbox_property(&bcm2835_data.msg, sizeof bcm2835_data.msg);
409 +
410 +       /* check if it was all ok and return the rate in milli degrees C */
411 +       if (result == 0 && (bcm2835_data.msg.request_code & 0x80000000))
412 +               *temp = (uint)bcm2835_data.msg.tag.val;
413 +       #ifdef THERMAL_DEBUG_ENABLE
414 +       else
415 +               print_debug("Failed to get temperature!");
416 +       #endif
417 +       print_debug("Got temperature as %u",(uint)*temp);
418 +       print_debug("OUT");
419 +       return 0;
420 +}
421 +
422 +static int bcm2835_get_temp(struct thermal_zone_device *thermal_dev, unsigned long *temp)
423 +{
424 +       int result;
425 +
426 +       print_debug("IN");
427 +
428 +       /* wipe all previous message data */
429 +       memset(&bcm2835_data.msg, 0, sizeof bcm2835_data.msg);
430 +
431 +       /* prepare message */
432 +       bcm2835_data.msg.msg_size = sizeof bcm2835_data.msg;
433 +       bcm2835_data.msg.tag.buffer_size = 8;
434 +       bcm2835_data.msg.tag.tag_id = VC_TAG_GET_TEMP;
435 +
436 +       /* send the message */
437 +       result = bcm_mailbox_property(&bcm2835_data.msg, sizeof bcm2835_data.msg);
438 +
439 +       /* check if it was all ok and return the rate in milli degrees C */
440 +       if (result == 0 && (bcm2835_data.msg.request_code & 0x80000000))
441 +               *temp = (uint)bcm2835_data.msg.tag.val;
442 +       #ifdef THERMAL_DEBUG_ENABLE
443 +       else
444 +               print_debug("Failed to get temperature!");
445 +       #endif
446 +       print_debug("Got temperature as %u",(uint)*temp);
447 +       print_debug("OUT");
448 +       return 0;
449 +}
450 +
451 +
452 +static int bcm2835_get_trip_type(struct thermal_zone_device * thermal_dev, int trip_num, enum thermal_trip_type *trip_type)
453 +{
454 +       *trip_type = THERMAL_TRIP_HOT;
455 +       return 0;
456 +}
457 +
458 +
459 +static int bcm2835_get_mode(struct thermal_zone_device *thermal_dev, enum thermal_device_mode *dev_mode)
460 +{
461 +       *dev_mode = THERMAL_DEVICE_ENABLED;
462 +       return 0;
463 +}
464 +
465 +
466 +static int bcm2835_thermal_probe(struct platform_device *pdev)
467 +{
468 +       print_debug("IN");
469 +       print_debug("THERMAL Driver has been probed!");
470 +
471 +       /* check that the device isn't null!*/
472 +       if(pdev == NULL)
473 +       {
474 +               print_debug("Platform device is empty!");
475 +               return -ENODEV;
476 +       }
477 +
478 +       if(!(bcm2835_data.thermal_dev = thermal_zone_device_register("bcm2835_thermal",  1, 0, NULL, &ops, NULL, 0, 0)))
479 +       {
480 +               print_debug("Unable to register the thermal device!");
481 +               return -EFAULT;
482 +       }
483 +       return 0;
484 +}
485 +
486 +
487 +static int bcm2835_thermal_remove(struct platform_device *pdev)
488 +{
489 +       print_debug("IN");
490 +
491 +       thermal_zone_device_unregister(bcm2835_data.thermal_dev);
492 +
493 +       print_debug("OUT");
494 +
495 +       return 0;
496 +}
497 +
498 +static struct thermal_zone_device_ops ops  = {
499 +       .get_temp = bcm2835_get_temp,
500 +       .get_trip_temp = bcm2835_get_max_temp,
501 +       .get_trip_type = bcm2835_get_trip_type,
502 +       .get_mode = bcm2835_get_mode,
503 +};
504 +
505 +/* Thermal Driver */
506 +static struct platform_driver bcm2835_thermal_driver = {
507 +       .probe = bcm2835_thermal_probe,
508 +       .remove = bcm2835_thermal_remove,
509 +       .driver = {
510 +                               .name = "bcm2835_thermal",
511 +                               .owner = THIS_MODULE,
512 +                       },
513 +};
514 +
515 +MODULE_LICENSE("GPL");
516 +MODULE_AUTHOR("Dorian Peake");
517 +MODULE_DESCRIPTION("Thermal driver for bcm2835 chip");
518 +
519 +module_platform_driver(bcm2835_thermal_driver);