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