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