kernel: refresh patches
[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 --- a/arch/arm/mach-bcm2708/bcm2708.c
20 +++ b/arch/arm/mach-bcm2708/bcm2708.c
21 @@ -483,6 +483,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 @@ -594,6 +602,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 @@ -1565,6 +1565,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 @@ -196,6 +196,12 @@ 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  config X86_PKG_TEMP_THERMAL
300         tristate "X86 package temperature thermal driver"
301         depends on X86_THERMAL_VECTOR
302 --- a/drivers/thermal/Makefile
303 +++ b/drivers/thermal/Makefile
304 @@ -28,6 +28,7 @@ obj-$(CONFIG_ARMADA_THERMAL)  += armada_t
305  obj-$(CONFIG_IMX_THERMAL)      += imx_thermal.o
306  obj-$(CONFIG_DB8500_CPUFREQ_COOLING)   += db8500_cpufreq_cooling.o
307  obj-$(CONFIG_INTEL_POWERCLAMP) += intel_powerclamp.o
308 +obj-$(CONFIG_THERMAL_BCM2835)  += bcm2835-thermal.o
309  obj-$(CONFIG_X86_PKG_TEMP_THERMAL)     += x86_pkg_temp_thermal.o
310  obj-$(CONFIG_TI_SOC_THERMAL)   += ti-soc-thermal/
311  obj-$(CONFIG_ACPI_INT3403_THERMAL)     += int3403_thermal.o
312 --- /dev/null
313 +++ b/drivers/thermal/bcm2835-thermal.c
314 @@ -0,0 +1,184 @@
315 +/*****************************************************************************
316 +* Copyright 2011 Broadcom Corporation.  All rights reserved.
317 +*
318 +* Unless you and Broadcom execute a separate written software license
319 +* agreement governing use of this software, this software is licensed to you
320 +* under the terms of the GNU General Public License version 2, available at
321 +* http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
322 +*
323 +* Notwithstanding the above, under no circumstances may you combine this
324 +* software in any way with any other Broadcom software provided under a
325 +* license other than the GPL, without Broadcom's express prior written
326 +* consent.
327 +*****************************************************************************/
328 +
329 +#include <linux/kernel.h>
330 +#include <linux/module.h>
331 +#include <linux/init.h>
332 +#include <linux/platform_device.h>
333 +#include <linux/slab.h>
334 +#include <linux/sysfs.h>
335 +#include <mach/vcio.h>
336 +#include <linux/thermal.h>
337 +
338 +
339 +/* --- DEFINITIONS --- */
340 +#define MODULE_NAME "bcm2835_thermal"
341 +
342 +/*#define THERMAL_DEBUG_ENABLE*/
343 +
344 +#ifdef THERMAL_DEBUG_ENABLE
345 +#define print_debug(fmt,...) printk(KERN_INFO "%s:%s:%d: "fmt"\n", MODULE_NAME, __func__, __LINE__, ##__VA_ARGS__)
346 +#else
347 +#define print_debug(fmt,...)
348 +#endif
349 +#define print_err(fmt,...) printk(KERN_ERR "%s:%s:%d: "fmt"\n", MODULE_NAME, __func__,__LINE__, ##__VA_ARGS__)
350 +
351 +#define VC_TAG_GET_TEMP 0x00030006
352 +#define VC_TAG_GET_MAX_TEMP 0x0003000A
353 +
354 +typedef enum {
355 +       TEMP,
356 +       MAX_TEMP,
357 +} temp_type;
358 +
359 +/* --- STRUCTS --- */
360 +/* tag part of the message */
361 +struct vc_msg_tag {
362 +       uint32_t tag_id;                /* the tag ID for the temperature */
363 +       uint32_t buffer_size;   /* size of the buffer (should be 8) */
364 +       uint32_t request_code;  /* identifies message as a request (should be 0) */
365 +       uint32_t id;                    /* extra ID field (should be 0) */
366 +       uint32_t val;                   /* returned value of the temperature */
367 +};
368 +
369 +/* message structure to be sent to videocore */
370 +struct vc_msg {
371 +       uint32_t msg_size;              /* simply, sizeof(struct vc_msg) */
372 +       uint32_t request_code;          /* holds various information like the success and number of bytes returned (refer to mailboxes wiki) */
373 +       struct vc_msg_tag tag;          /* the tag structure above to make */
374 +       uint32_t end_tag;               /* an end identifier, should be set to NULL */
375 +};
376 +
377 +struct bcm2835_thermal_data {
378 +       struct thermal_zone_device *thermal_dev;
379 +       struct vc_msg msg;
380 +};
381 +
382 +/* --- GLOBALS --- */
383 +static struct bcm2835_thermal_data bcm2835_data;
384 +
385 +/* Thermal Device Operations */
386 +static struct thermal_zone_device_ops ops;
387 +
388 +/* --- FUNCTIONS --- */
389 +
390 +static int bcm2835_get_temp_or_max(struct thermal_zone_device *thermal_dev, unsigned long *temp, unsigned tag_id)
391 +{
392 +       int result = -1, retry = 3;
393 +       print_debug("IN");
394 +
395 +       *temp = 0;
396 +       while (result != 0 && retry-- > 0) {
397 +               /* wipe all previous message data */
398 +               memset(&bcm2835_data.msg, 0, sizeof bcm2835_data.msg);
399 +
400 +               /* prepare message */
401 +               bcm2835_data.msg.msg_size = sizeof bcm2835_data.msg;
402 +               bcm2835_data.msg.tag.buffer_size = 8;
403 +               bcm2835_data.msg.tag.tag_id = tag_id;
404 +
405 +               /* send the message */
406 +               result = bcm_mailbox_property(&bcm2835_data.msg, sizeof bcm2835_data.msg);
407 +               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);
408 +               if (!(bcm2835_data.msg.request_code & 0x80000000))
409 +                       result = -1;
410 +       }
411 +
412 +       /* check if it was all ok and return the rate in milli degrees C */
413 +       if (result == 0)
414 +               *temp = (uint)bcm2835_data.msg.tag.val;
415 +       else
416 +               print_err("Failed to get temperature! (%x:%d)\n", tag_id, result);
417 +       print_debug("OUT");
418 +       return result;
419 +}
420 +
421 +static int bcm2835_get_temp(struct thermal_zone_device *thermal_dev, unsigned long *temp)
422 +{
423 +       return bcm2835_get_temp_or_max(thermal_dev, temp, VC_TAG_GET_TEMP);
424 +}
425 +
426 +static int bcm2835_get_max_temp(struct thermal_zone_device *thermal_dev, int trip_num, unsigned long *temp)
427 +{
428 +       return bcm2835_get_temp_or_max(thermal_dev, temp, VC_TAG_GET_MAX_TEMP);
429 +}
430 +
431 +static int bcm2835_get_trip_type(struct thermal_zone_device * thermal_dev, int trip_num, enum thermal_trip_type *trip_type)
432 +{
433 +       *trip_type = THERMAL_TRIP_HOT;
434 +       return 0;
435 +}
436 +
437 +
438 +static int bcm2835_get_mode(struct thermal_zone_device *thermal_dev, enum thermal_device_mode *dev_mode)
439 +{
440 +       *dev_mode = THERMAL_DEVICE_ENABLED;
441 +       return 0;
442 +}
443 +
444 +
445 +static int bcm2835_thermal_probe(struct platform_device *pdev)
446 +{
447 +       print_debug("IN");
448 +       print_debug("THERMAL Driver has been probed!");
449 +
450 +       /* check that the device isn't null!*/
451 +       if(pdev == NULL)
452 +       {
453 +               print_debug("Platform device is empty!");
454 +               return -ENODEV;
455 +       }
456 +
457 +       if(!(bcm2835_data.thermal_dev = thermal_zone_device_register("bcm2835_thermal",  1, 0, NULL, &ops, NULL, 0, 0)))
458 +       {
459 +               print_debug("Unable to register the thermal device!");
460 +               return -EFAULT;
461 +       }
462 +       return 0;
463 +}
464 +
465 +
466 +static int bcm2835_thermal_remove(struct platform_device *pdev)
467 +{
468 +       print_debug("IN");
469 +
470 +       thermal_zone_device_unregister(bcm2835_data.thermal_dev);
471 +
472 +       print_debug("OUT");
473 +
474 +       return 0;
475 +}
476 +
477 +static struct thermal_zone_device_ops ops  = {
478 +       .get_temp = bcm2835_get_temp,
479 +       .get_trip_temp = bcm2835_get_max_temp,
480 +       .get_trip_type = bcm2835_get_trip_type,
481 +       .get_mode = bcm2835_get_mode,
482 +};
483 +
484 +/* Thermal Driver */
485 +static struct platform_driver bcm2835_thermal_driver = {
486 +       .probe = bcm2835_thermal_probe,
487 +       .remove = bcm2835_thermal_remove,
488 +       .driver = {
489 +                               .name = "bcm2835_thermal",
490 +                               .owner = THIS_MODULE,
491 +                       },
492 +};
493 +
494 +MODULE_LICENSE("GPL");
495 +MODULE_AUTHOR("Dorian Peake");
496 +MODULE_DESCRIPTION("Thermal driver for bcm2835 chip");
497 +
498 +module_platform_driver(bcm2835_thermal_driver);