ixp4xx: remove linux 3.10 support
[openwrt.git] / target / linux / brcm2708 / patches-3.10 / 0033-Added-hwmon-thermal-driver-for-reporting-core-temper.patch
1 From 4c7d4bffe1986d856b37460f87950dabe29c616e 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/196] 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 Index: linux-3.10.49/arch/arm/mach-bcm2708/bcm2708.c
20 ===================================================================
21 --- linux-3.10.49.orig/arch/arm/mach-bcm2708/bcm2708.c  2014-09-27 11:40:40.999449257 -0700
22 +++ linux-3.10.49/arch/arm/mach-bcm2708/bcm2708.c       2014-09-27 11:40:40.979449258 -0700
23 @@ -562,6 +562,14 @@
24         .resource = bcm2708_bsc1_resources,
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 @@ -682,6 +690,9 @@
39         bcm_register_device(&bcm2708_bsc0_device);
40         bcm_register_device(&bcm2708_bsc1_device);
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.10.49/drivers/hwmon/Kconfig
49 ===================================================================
50 --- linux-3.10.49.orig/drivers/hwmon/Kconfig    2014-09-27 11:40:40.999449257 -0700
51 +++ linux-3.10.49/drivers/hwmon/Kconfig 2014-09-27 11:40:40.979449258 -0700
52 @@ -1537,6 +1537,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.10.49/drivers/hwmon/bcm2835-hwmon.c
70 ===================================================================
71 --- /dev/null   1970-01-01 00:00:00.000000000 +0000
72 +++ linux-3.10.49/drivers/hwmon/bcm2835-hwmon.c 2014-09-27 11:40:40.979449258 -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.10.49/drivers/thermal/Kconfig
294 ===================================================================
295 --- linux-3.10.49.orig/drivers/thermal/Kconfig  2014-09-27 11:40:40.999449257 -0700
296 +++ linux-3.10.49/drivers/thermal/Kconfig       2014-09-27 11:40:40.983449258 -0700
297 @@ -169,4 +169,11 @@
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  endif
308 +
309 Index: linux-3.10.49/drivers/thermal/Makefile
310 ===================================================================
311 --- linux-3.10.49.orig/drivers/thermal/Makefile 2014-09-27 11:40:40.999449257 -0700
312 +++ linux-3.10.49/drivers/thermal/Makefile      2014-09-27 11:40:40.983449258 -0700
313 @@ -23,4 +23,5 @@
314  obj-$(CONFIG_ARMADA_THERMAL)   += armada_thermal.o
315  obj-$(CONFIG_DB8500_CPUFREQ_COOLING)   += db8500_cpufreq_cooling.o
316  obj-$(CONFIG_INTEL_POWERCLAMP) += intel_powerclamp.o
317 +obj-$(CONFIG_THERMAL_BCM2835)  += bcm2835-thermal.o
318  
319 Index: linux-3.10.49/drivers/thermal/bcm2835-thermal.c
320 ===================================================================
321 --- /dev/null   1970-01-01 00:00:00.000000000 +0000
322 +++ linux-3.10.49/drivers/thermal/bcm2835-thermal.c     2014-09-27 11:40:40.983449258 -0700
323 @@ -0,0 +1,208 @@
324 +/*****************************************************************************
325 +* Copyright 2011 Broadcom Corporation.  All rights reserved.
326 +*
327 +* Unless you and Broadcom execute a separate written software license
328 +* agreement governing use of this software, this software is licensed to you
329 +* under the terms of the GNU General Public License version 2, available at
330 +* http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
331 +*
332 +* Notwithstanding the above, under no circumstances may you combine this
333 +* software in any way with any other Broadcom software provided under a
334 +* license other than the GPL, without Broadcom's express prior written
335 +* consent.
336 +*****************************************************************************/
337 +
338 +#include <linux/kernel.h>
339 +#include <linux/module.h>
340 +#include <linux/init.h>
341 +#include <linux/platform_device.h>
342 +#include <linux/slab.h>
343 +#include <linux/sysfs.h>
344 +#include <mach/vcio.h>
345 +#include <linux/thermal.h>
346 +
347 +
348 +/* --- DEFINITIONS --- */
349 +#define MODULE_NAME "bcm2835_thermal"
350 +
351 +/*#define THERMAL_DEBUG_ENABLE*/
352 +
353 +#ifdef THERMAL_DEBUG_ENABLE
354 +#define print_debug(fmt,...) printk(KERN_INFO "%s:%s:%d: "fmt"\n", MODULE_NAME, __func__, __LINE__, ##__VA_ARGS__)
355 +#else
356 +#define print_debug(fmt,...)
357 +#endif
358 +#define print_err(fmt,...) printk(KERN_ERR "%s:%s:%d: "fmt"\n", MODULE_NAME, __func__,__LINE__, ##__VA_ARGS__)
359 +#define print_info(fmt,...) printk(KERN_INFO "%s: "fmt"\n", MODULE_NAME, ##__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 +/* --- PROTOTYPES --- */
393 +static int bcm2835_get_temp(struct thermal_zone_device *thermal_dev, unsigned long *);
394 +static int bcm2835_get_max_temp(struct thermal_zone_device *thermal_dev, int, unsigned long *);
395 +static int bcm2835_get_trip_type(struct thermal_zone_device *thermal_dev, int trip_num, enum thermal_trip_type *trip_type);
396 +static int bcm2835_get_mode(struct thermal_zone_device *thermal_dev, enum thermal_device_mode *dev_mode);
397 +
398 +/* --- GLOBALS --- */
399 +static struct bcm2835_thermal_data bcm2835_data;
400 +
401 +/* Thermal Device Operations */
402 +static struct thermal_zone_device_ops ops;
403 +
404 +/* --- FUNCTIONS --- */
405 +static int bcm2835_get_max_temp(struct thermal_zone_device *thermal_dev, int trip_num, unsigned long *temp)
406 +{
407 +       int result;
408 +
409 +       print_debug("IN");
410 +
411 +       /* wipe all previous message data */
412 +       memset(&bcm2835_data.msg, 0, sizeof bcm2835_data.msg);
413 +
414 +       /* prepare message */
415 +       bcm2835_data.msg.msg_size = sizeof bcm2835_data.msg;
416 +       bcm2835_data.msg.tag.buffer_size = 8;
417 +       bcm2835_data.msg.tag.tag_id = VC_TAG_GET_MAX_TEMP;
418 +
419 +       /* send the message */
420 +       result = bcm_mailbox_property(&bcm2835_data.msg, sizeof bcm2835_data.msg);
421 +
422 +       /* check if it was all ok and return the rate in milli degrees C */
423 +       if (result == 0 && (bcm2835_data.msg.request_code & 0x80000000))
424 +               *temp = (uint)bcm2835_data.msg.tag.val;
425 +       #ifdef THERMAL_DEBUG_ENABLE
426 +       else
427 +               print_debug("Failed to get temperature!");
428 +       #endif
429 +       print_debug("Got temperature as %u",(uint)*temp);
430 +       print_debug("OUT");
431 +       return 0;
432 +}
433 +
434 +static int bcm2835_get_temp(struct thermal_zone_device *thermal_dev, unsigned long *temp)
435 +{
436 +       int result;
437 +
438 +       print_debug("IN");
439 +
440 +       /* wipe all previous message data */
441 +       memset(&bcm2835_data.msg, 0, sizeof bcm2835_data.msg);
442 +
443 +       /* prepare message */
444 +       bcm2835_data.msg.msg_size = sizeof bcm2835_data.msg;
445 +       bcm2835_data.msg.tag.buffer_size = 8;
446 +       bcm2835_data.msg.tag.tag_id = VC_TAG_GET_TEMP;
447 +
448 +       /* send the message */
449 +       result = bcm_mailbox_property(&bcm2835_data.msg, sizeof bcm2835_data.msg);
450 +
451 +       /* check if it was all ok and return the rate in milli degrees C */
452 +       if (result == 0 && (bcm2835_data.msg.request_code & 0x80000000))
453 +               *temp = (uint)bcm2835_data.msg.tag.val;
454 +       #ifdef THERMAL_DEBUG_ENABLE
455 +       else
456 +               print_debug("Failed to get temperature!");
457 +       #endif
458 +       print_debug("Got temperature as %u",(uint)*temp);
459 +       print_debug("OUT");
460 +       return 0;
461 +}
462 +
463 +
464 +static int bcm2835_get_trip_type(struct thermal_zone_device * thermal_dev, int trip_num, enum thermal_trip_type *trip_type)
465 +{
466 +       *trip_type = THERMAL_TRIP_HOT;
467 +       return 0;
468 +}
469 +
470 +
471 +static int bcm2835_get_mode(struct thermal_zone_device *thermal_dev, enum thermal_device_mode *dev_mode)
472 +{
473 +       *dev_mode = THERMAL_DEVICE_ENABLED;
474 +       return 0;
475 +}
476 +
477 +
478 +static int bcm2835_thermal_probe(struct platform_device *pdev)
479 +{
480 +       print_debug("IN");
481 +       print_debug("THERMAL Driver has been probed!");
482 +
483 +       /* check that the device isn't null!*/
484 +       if(pdev == NULL)
485 +       {
486 +               print_debug("Platform device is empty!");
487 +               return -ENODEV;
488 +       }
489 +
490 +       if(!(bcm2835_data.thermal_dev = thermal_zone_device_register("bcm2835_thermal",  1, 0, NULL, &ops, NULL, 0, 0)))
491 +       {
492 +               print_debug("Unable to register the thermal device!");
493 +               return -EFAULT;
494 +       }
495 +       return 0;
496 +}
497 +
498 +
499 +static int bcm2835_thermal_remove(struct platform_device *pdev)
500 +{
501 +       print_debug("IN");
502 +
503 +       thermal_zone_device_unregister(bcm2835_data.thermal_dev);
504 +
505 +       print_debug("OUT");
506 +
507 +       return 0;
508 +}
509 +
510 +static struct thermal_zone_device_ops ops  = {
511 +       .get_temp = bcm2835_get_temp,
512 +       .get_trip_temp = bcm2835_get_max_temp,
513 +       .get_trip_type = bcm2835_get_trip_type,
514 +       .get_mode = bcm2835_get_mode,
515 +};
516 +
517 +/* Thermal Driver */
518 +static struct platform_driver bcm2835_thermal_driver = {
519 +       .probe = bcm2835_thermal_probe,
520 +       .remove = bcm2835_thermal_remove,
521 +       .driver = {
522 +                               .name = "bcm2835_thermal",
523 +                               .owner = THIS_MODULE,
524 +                       },
525 +};
526 +
527 +MODULE_LICENSE("GPL");
528 +MODULE_AUTHOR("Dorian Peake");
529 +MODULE_DESCRIPTION("Thermal driver for bcm2835 chip");
530 +
531 +module_platform_driver(bcm2835_thermal_driver);