brcm2708: add support for 3.10 kernel
[15.05/openwrt.git] / target / linux / brcm2708 / patches-3.10 / 010-bcm2835-thermal-driver.patch
1 diff -urN linux-3.10/drivers/thermal/bcm2835-thermal.c linux-rpi-3.10.y/drivers/thermal/bcm2835-thermal.c
2 --- linux-3.10/drivers/thermal/bcm2835-thermal.c        1970-01-01 01:00:00.000000000 +0100
3 +++ linux-rpi-3.10.y/drivers/thermal/bcm2835-thermal.c  2013-07-06 15:25:50.000000000 +0100
4 @@ -0,0 +1,184 @@
5 +/*****************************************************************************
6 +* Copyright 2011 Broadcom Corporation.  All rights reserved.
7 +*
8 +* Unless you and Broadcom execute a separate written software license
9 +* agreement governing use of this software, this software is licensed to you
10 +* under the terms of the GNU General Public License version 2, available at
11 +* http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
12 +*
13 +* Notwithstanding the above, under no circumstances may you combine this
14 +* software in any way with any other Broadcom software provided under a
15 +* license other than the GPL, without Broadcom's express prior written
16 +* consent.
17 +*****************************************************************************/
18 +
19 +#include <linux/kernel.h>
20 +#include <linux/module.h>
21 +#include <linux/init.h>
22 +#include <linux/platform_device.h>
23 +#include <linux/slab.h>
24 +#include <linux/sysfs.h>
25 +#include <mach/vcio.h>
26 +#include <linux/thermal.h>
27 +
28 +
29 +/* --- DEFINITIONS --- */
30 +#define MODULE_NAME "bcm2835_thermal"
31 +
32 +/*#define THERMAL_DEBUG_ENABLE*/
33 +
34 +#ifdef THERMAL_DEBUG_ENABLE
35 +#define print_debug(fmt,...) printk(KERN_INFO "%s:%s:%d: "fmt"\n", MODULE_NAME, __func__, __LINE__, ##__VA_ARGS__)
36 +#else
37 +#define print_debug(fmt,...)
38 +#endif
39 +#define print_err(fmt,...) printk(KERN_ERR "%s:%s:%d: "fmt"\n", MODULE_NAME, __func__,__LINE__, ##__VA_ARGS__)
40 +
41 +#define VC_TAG_GET_TEMP 0x00030006
42 +#define VC_TAG_GET_MAX_TEMP 0x0003000A
43 +
44 +typedef enum {
45 +       TEMP,
46 +       MAX_TEMP,
47 +} temp_type;
48 +
49 +/* --- STRUCTS --- */
50 +/* tag part of the message */
51 +struct vc_msg_tag {
52 +       uint32_t tag_id;                /* the tag ID for the temperature */
53 +       uint32_t buffer_size;   /* size of the buffer (should be 8) */
54 +       uint32_t request_code;  /* identifies message as a request (should be 0) */
55 +       uint32_t id;                    /* extra ID field (should be 0) */
56 +       uint32_t val;                   /* returned value of the temperature */
57 +};
58 +
59 +/* message structure to be sent to videocore */
60 +struct vc_msg {
61 +       uint32_t msg_size;              /* simply, sizeof(struct vc_msg) */
62 +       uint32_t request_code;          /* holds various information like the success and number of bytes returned (refer to mailboxes wiki) */
63 +       struct vc_msg_tag tag;          /* the tag structure above to make */
64 +       uint32_t end_tag;               /* an end identifier, should be set to NULL */
65 +};
66 +
67 +struct bcm2835_thermal_data {
68 +       struct thermal_zone_device *thermal_dev;
69 +       struct vc_msg msg;
70 +};
71 +
72 +/* --- GLOBALS --- */
73 +static struct bcm2835_thermal_data bcm2835_data;
74 +
75 +/* Thermal Device Operations */
76 +static struct thermal_zone_device_ops ops;
77 +
78 +/* --- FUNCTIONS --- */
79 +
80 +static int bcm2835_get_temp_or_max(struct thermal_zone_device *thermal_dev, unsigned long *temp, unsigned tag_id)
81 +{
82 +       int result = -1, retry = 3;
83 +       print_debug("IN");
84 +
85 +       *temp = 0;
86 +       while (result != 0 && retry-- > 0) {
87 +               /* wipe all previous message data */
88 +               memset(&bcm2835_data.msg, 0, sizeof bcm2835_data.msg);
89 +
90 +               /* prepare message */
91 +               bcm2835_data.msg.msg_size = sizeof bcm2835_data.msg;
92 +               bcm2835_data.msg.tag.buffer_size = 8;
93 +               bcm2835_data.msg.tag.tag_id = tag_id;
94 +
95 +               /* send the message */
96 +               result = bcm_mailbox_property(&bcm2835_data.msg, sizeof bcm2835_data.msg);
97 +               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);
98 +               if (!(bcm2835_data.msg.request_code & 0x80000000))
99 +                       result = -1;
100 +       }
101 +
102 +       /* check if it was all ok and return the rate in milli degrees C */
103 +       if (result == 0)
104 +               *temp = (uint)bcm2835_data.msg.tag.val;
105 +       else
106 +               print_err("Failed to get temperature! (%x:%d)\n", tag_id, result);
107 +       print_debug("OUT");
108 +       return result;
109 +}
110 +
111 +static int bcm2835_get_temp(struct thermal_zone_device *thermal_dev, unsigned long *temp)
112 +{
113 +       return bcm2835_get_temp_or_max(thermal_dev, temp, VC_TAG_GET_TEMP);
114 +}
115 +
116 +static int bcm2835_get_max_temp(struct thermal_zone_device *thermal_dev, int trip_num, unsigned long *temp)
117 +{
118 +       return bcm2835_get_temp_or_max(thermal_dev, temp, VC_TAG_GET_MAX_TEMP);
119 +}
120 +
121 +static int bcm2835_get_trip_type(struct thermal_zone_device * thermal_dev, int trip_num, enum thermal_trip_type *trip_type)
122 +{
123 +       *trip_type = THERMAL_TRIP_HOT;
124 +       return 0;
125 +}
126 +
127 +
128 +static int bcm2835_get_mode(struct thermal_zone_device *thermal_dev, enum thermal_device_mode *dev_mode)
129 +{
130 +       *dev_mode = THERMAL_DEVICE_ENABLED;
131 +       return 0;
132 +}
133 +
134 +
135 +static int bcm2835_thermal_probe(struct platform_device *pdev)
136 +{
137 +       print_debug("IN");
138 +       print_debug("THERMAL Driver has been probed!");
139 +
140 +       /* check that the device isn't null!*/
141 +       if(pdev == NULL)
142 +       {
143 +               print_debug("Platform device is empty!");
144 +               return -ENODEV;
145 +       }
146 +
147 +       if(!(bcm2835_data.thermal_dev = thermal_zone_device_register("bcm2835_thermal",  1, 0, NULL, &ops, NULL, 0, 0)))
148 +       {
149 +               print_debug("Unable to register the thermal device!");
150 +               return -EFAULT;
151 +       }
152 +       return 0;
153 +}
154 +
155 +
156 +static int bcm2835_thermal_remove(struct platform_device *pdev)
157 +{
158 +       print_debug("IN");
159 +
160 +       thermal_zone_device_unregister(bcm2835_data.thermal_dev);
161 +
162 +       print_debug("OUT");
163 +
164 +       return 0;
165 +}
166 +
167 +static struct thermal_zone_device_ops ops  = {
168 +       .get_temp = bcm2835_get_temp,
169 +       .get_trip_temp = bcm2835_get_max_temp,
170 +       .get_trip_type = bcm2835_get_trip_type,
171 +       .get_mode = bcm2835_get_mode,
172 +};
173 +
174 +/* Thermal Driver */
175 +static struct platform_driver bcm2835_thermal_driver = {
176 +       .probe = bcm2835_thermal_probe,
177 +       .remove = bcm2835_thermal_remove,
178 +       .driver = {
179 +                               .name = "bcm2835_thermal",
180 +                               .owner = THIS_MODULE,
181 +                       },
182 +};
183 +
184 +MODULE_LICENSE("GPL");
185 +MODULE_AUTHOR("Dorian Peake");
186 +MODULE_DESCRIPTION("Thermal driver for bcm2835 chip");
187 +
188 +module_platform_driver(bcm2835_thermal_driver);
189 diff -urN linux-3.10/drivers/thermal/Kconfig linux-rpi-3.10.y/drivers/thermal/Kconfig
190 --- linux-3.10/drivers/thermal/Kconfig  2013-06-30 23:13:29.000000000 +0100
191 +++ linux-rpi-3.10.y/drivers/thermal/Kconfig    2013-07-06 15:25:50.000000000 +0100
192 @@ -169,4 +169,11 @@
193           enforce idle time which results in more package C-state residency. The
194           user interface is exposed via generic thermal framework.
195  
196 +config THERMAL_BCM2835
197 +       tristate "BCM2835 Thermal Driver"
198 +       help
199 +         This will enable temperature monitoring for the Broadcom BCM2835
200 +         chip. If built as a module, it will be called 'bcm2835-thermal'.
201 +
202  endif
203 +
204 diff -urN linux-3.10/drivers/thermal/Makefile linux-rpi-3.10.y/drivers/thermal/Makefile
205 --- linux-3.10/drivers/thermal/Makefile 2013-06-30 23:13:29.000000000 +0100
206 +++ linux-rpi-3.10.y/drivers/thermal/Makefile   2013-07-06 15:25:50.000000000 +0100
207 @@ -23,4 +23,5 @@
208  obj-$(CONFIG_ARMADA_THERMAL)   += armada_thermal.o
209  obj-$(CONFIG_DB8500_CPUFREQ_COOLING)   += db8500_cpufreq_cooling.o
210  obj-$(CONFIG_INTEL_POWERCLAMP) += intel_powerclamp.o
211 +obj-$(CONFIG_THERMAL_BCM2835)  += bcm2835-thermal.o
212