brcm2708: switch to linux 4.4 and update patches
[openwrt.git] / target / linux / brcm2708 / patches-4.1 / 0135-thermal-bcm2835-Use-firmware-API.patch
1 From 9ad0d5fce4faf445bf5f6020df570f6cf6c17167 Mon Sep 17 00:00:00 2001
2 From: =?UTF-8?q?Noralf=20Tr=C3=B8nnes?= <noralf@tronnes.org>
3 Date: Mon, 20 Jul 2015 12:17:10 +0200
4 Subject: [PATCH 135/222] thermal: bcm2835: Use firmware API
5 MIME-Version: 1.0
6 Content-Type: text/plain; charset=UTF-8
7 Content-Transfer-Encoding: 8bit
8
9 Use the new firmware API instead of the legacy mailbox API.
10 Remove retry loop on failure to read temperature.
11 Clean up code.
12
13 Signed-off-by: Noralf Trønnes <noralf@tronnes.org>
14 ---
15  arch/arm/boot/dts/bcm2708_common.dtsi |   1 +
16  drivers/thermal/bcm2835-thermal.c     | 197 +++++++++++++---------------------
17  2 files changed, 75 insertions(+), 123 deletions(-)
18
19 --- a/arch/arm/boot/dts/bcm2708_common.dtsi
20 +++ b/arch/arm/boot/dts/bcm2708_common.dtsi
21 @@ -230,6 +230,7 @@
22  
23                 thermal: thermal {
24                         compatible = "brcm,bcm2835-thermal";
25 +                       firmware = <&firmware>;
26                 };
27         };
28  
29 --- a/drivers/thermal/bcm2835-thermal.c
30 +++ b/drivers/thermal/bcm2835-thermal.c
31 @@ -12,161 +12,113 @@
32  * consent.
33  *****************************************************************************/
34  
35 -#include <linux/kernel.h>
36  #include <linux/module.h>
37 -#include <linux/init.h>
38 -#include <linux/platform_data/mailbox-bcm2708.h>
39  #include <linux/platform_device.h>
40 -#include <linux/slab.h>
41 -#include <linux/sysfs.h>
42  #include <linux/thermal.h>
43 +#include <soc/bcm2835/raspberrypi-firmware.h>
44  
45 -
46 -/* --- DEFINITIONS --- */
47 -#define MODULE_NAME "bcm2835_thermal"
48 -
49 -/*#define THERMAL_DEBUG_ENABLE*/
50 -
51 -#ifdef THERMAL_DEBUG_ENABLE
52 -#define print_debug(fmt,...) printk(KERN_INFO "%s:%s:%d: "fmt"\n", MODULE_NAME, __func__, __LINE__, ##__VA_ARGS__)
53 -#else
54 -#define print_debug(fmt,...)
55 -#endif
56 -#define print_err(fmt,...) printk(KERN_ERR "%s:%s:%d: "fmt"\n", MODULE_NAME, __func__,__LINE__, ##__VA_ARGS__)
57 -
58 -#define VC_TAG_GET_TEMP 0x00030006
59 -#define VC_TAG_GET_MAX_TEMP 0x0003000A
60 -
61 -typedef enum {
62 -       TEMP,
63 -       MAX_TEMP,
64 -} temp_type;
65 -
66 -/* --- STRUCTS --- */
67 -/* tag part of the message */
68 -struct vc_msg_tag {
69 -       uint32_t tag_id;                /* the tag ID for the temperature */
70 -       uint32_t buffer_size;   /* size of the buffer (should be 8) */
71 -       uint32_t request_code;  /* identifies message as a request (should be 0) */
72 -       uint32_t id;                    /* extra ID field (should be 0) */
73 -       uint32_t val;                   /* returned value of the temperature */
74 -};
75 -
76 -/* message structure to be sent to videocore */
77 -struct vc_msg {
78 -       uint32_t msg_size;              /* simply, sizeof(struct vc_msg) */
79 -       uint32_t request_code;          /* holds various information like the success and number of bytes returned (refer to mailboxes wiki) */
80 -       struct vc_msg_tag tag;          /* the tag structure above to make */
81 -       uint32_t end_tag;               /* an end identifier, should be set to NULL */
82 -};
83 -
84 -struct bcm2835_thermal_data {
85 -       struct thermal_zone_device *thermal_dev;
86 -       struct vc_msg msg;
87 -};
88 -
89 -/* --- GLOBALS --- */
90 -static struct bcm2835_thermal_data bcm2835_data;
91 -
92 -/* Thermal Device Operations */
93 -static struct thermal_zone_device_ops ops;
94 -
95 -/* --- FUNCTIONS --- */
96 -
97 -static int bcm2835_get_temp_or_max(struct thermal_zone_device *thermal_dev, unsigned long *temp, unsigned tag_id)
98 +static int bcm2835_thermal_get_property(struct thermal_zone_device *tz,
99 +                                       unsigned long *temp, u32 tag)
100  {
101 -       int result = -1, retry = 3;
102 -       print_debug("IN");
103 +       struct rpi_firmware *fw = tz->devdata;
104 +       struct {
105 +               u32 id;
106 +               u32 val;
107 +       } packet;
108 +       int ret;
109  
110         *temp = 0;
111 -       while (result != 0 && retry-- > 0) {
112 -               /* wipe all previous message data */
113 -               memset(&bcm2835_data.msg, 0, sizeof bcm2835_data.msg);
114 -
115 -               /* prepare message */
116 -               bcm2835_data.msg.msg_size = sizeof bcm2835_data.msg;
117 -               bcm2835_data.msg.tag.buffer_size = 8;
118 -               bcm2835_data.msg.tag.tag_id = tag_id;
119 -
120 -               /* send the message */
121 -               result = bcm_mailbox_property(&bcm2835_data.msg, sizeof bcm2835_data.msg);
122 -               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);
123 -               if (!(bcm2835_data.msg.request_code & 0x80000000))
124 -                       result = -1;
125 +       packet.id = 0;
126 +       ret = rpi_firmware_property(fw, tag, &packet, sizeof(packet));
127 +       if (ret) {
128 +               dev_err(&tz->device, "Failed to get temperature\n");
129 +               return ret;
130         }
131  
132 -       /* check if it was all ok and return the rate in milli degrees C */
133 -       if (result == 0)
134 -               *temp = (uint)bcm2835_data.msg.tag.val;
135 -       else
136 -               print_err("Failed to get temperature! (%x:%d)\n", tag_id, result);
137 -       print_debug("OUT");
138 -       return result;
139 +       *temp = packet.val;
140 +       dev_dbg(&tz->device, "%stemp=%lu\n",
141 +               tag == RPI_FIRMWARE_GET_MAX_TEMPERATURE ? "max" : "", *temp);
142 +
143 +       return 0;
144  }
145  
146 -static int bcm2835_get_temp(struct thermal_zone_device *thermal_dev, unsigned long *temp)
147 +static int bcm2835_thermal_get_temp(struct thermal_zone_device *tz,
148 +                                   unsigned long *temp)
149  {
150 -       return bcm2835_get_temp_or_max(thermal_dev, temp, VC_TAG_GET_TEMP);
151 +       return bcm2835_thermal_get_property(tz, temp,
152 +                                           RPI_FIRMWARE_GET_TEMPERATURE);
153  }
154  
155 -static int bcm2835_get_max_temp(struct thermal_zone_device *thermal_dev, int trip_num, unsigned long *temp)
156 +static int bcm2835_thermal_get_max_temp(struct thermal_zone_device *tz,
157 +                                       int trip, unsigned long *temp)
158  {
159 -       return bcm2835_get_temp_or_max(thermal_dev, temp, VC_TAG_GET_MAX_TEMP);
160 +       /*
161 +        * The maximum safe temperature of the SoC.
162 +        * Overclock may be disabled above this temperature.
163 +        */
164 +       return bcm2835_thermal_get_property(tz, temp,
165 +                                           RPI_FIRMWARE_GET_MAX_TEMPERATURE);
166  }
167  
168 -static int bcm2835_get_trip_type(struct thermal_zone_device * thermal_dev, int trip_num, enum thermal_trip_type *trip_type)
169 +static int bcm2835_thermal_get_trip_type(struct thermal_zone_device *tz,
170 +                                        int trip, enum thermal_trip_type *type)
171  {
172 -       *trip_type = THERMAL_TRIP_HOT;
173 +       *type = THERMAL_TRIP_HOT;
174 +
175         return 0;
176  }
177  
178 -
179 -static int bcm2835_get_mode(struct thermal_zone_device *thermal_dev, enum thermal_device_mode *dev_mode)
180 +static int bcm2835_thermal_get_mode(struct thermal_zone_device *tz,
181 +                                   enum thermal_device_mode *mode)
182  {
183 -       *dev_mode = THERMAL_DEVICE_ENABLED;
184 +       *mode = THERMAL_DEVICE_ENABLED;
185 +
186         return 0;
187  }
188  
189 +static struct thermal_zone_device_ops ops  = {
190 +       .get_temp = bcm2835_thermal_get_temp,
191 +       .get_trip_temp = bcm2835_thermal_get_max_temp,
192 +       .get_trip_type = bcm2835_thermal_get_trip_type,
193 +       .get_mode = bcm2835_thermal_get_mode,
194 +};
195  
196  static int bcm2835_thermal_probe(struct platform_device *pdev)
197  {
198 -       print_debug("IN");
199 -       print_debug("THERMAL Driver has been probed!");
200 -
201 -       /* check that the device isn't null!*/
202 -       if(pdev == NULL)
203 -       {
204 -               print_debug("Platform device is empty!");
205 -               return -ENODEV;
206 +       struct device_node *fw_np;
207 +       struct rpi_firmware *fw;
208 +       struct thermal_zone_device *tz;
209 +
210 +       fw_np = of_parse_phandle(pdev->dev.of_node, "firmware", 0);
211 +/* Remove comment when booting without Device Tree is no longer supported
212 +       if (!fw_np) {
213 +               dev_err(&pdev->dev, "Missing firmware node\n");
214 +               return -ENOENT;
215         }
216 -
217 -       if(!(bcm2835_data.thermal_dev = thermal_zone_device_register("bcm2835_thermal",  1, 0, NULL, &ops, NULL, 0, 0)))
218 -       {
219 -               print_debug("Unable to register the thermal device!");
220 -               return -EFAULT;
221 +*/
222 +       fw = rpi_firmware_get(fw_np);
223 +       if (!fw)
224 +               return -EPROBE_DEFER;
225 +
226 +       tz = thermal_zone_device_register("bcm2835_thermal", 1, 0, fw, &ops,
227 +                                         NULL, 0, 0);
228 +       if (IS_ERR(tz)) {
229 +               dev_err(&pdev->dev, "Failed to register the thermal device\n");
230 +               return PTR_ERR(tz);
231         }
232 +
233 +       platform_set_drvdata(pdev, tz);
234 +
235         return 0;
236  }
237  
238 -
239  static int bcm2835_thermal_remove(struct platform_device *pdev)
240  {
241 -       print_debug("IN");
242 -
243 -       thermal_zone_device_unregister(bcm2835_data.thermal_dev);
244 -
245 -       print_debug("OUT");
246 +       thermal_zone_device_unregister(platform_get_drvdata(pdev));
247  
248         return 0;
249  }
250  
251 -static struct thermal_zone_device_ops ops  = {
252 -       .get_temp = bcm2835_get_temp,
253 -       .get_trip_temp = bcm2835_get_max_temp,
254 -       .get_trip_type = bcm2835_get_trip_type,
255 -       .get_mode = bcm2835_get_mode,
256 -};
257 -
258  static const struct of_device_id bcm2835_thermal_of_match_table[] = {
259         { .compatible = "brcm,bcm2835-thermal", },
260         {},
261 @@ -177,14 +129,13 @@ static struct platform_driver bcm2835_th
262         .probe = bcm2835_thermal_probe,
263         .remove = bcm2835_thermal_remove,
264         .driver = {
265 -                               .name = "bcm2835_thermal",
266 -                               .owner = THIS_MODULE,
267 -                               .of_match_table = bcm2835_thermal_of_match_table,
268 -                       },
269 +               .name = "bcm2835_thermal",
270 +               .of_match_table = bcm2835_thermal_of_match_table,
271 +       },
272  };
273 +module_platform_driver(bcm2835_thermal_driver);
274  
275 -MODULE_LICENSE("GPL");
276  MODULE_AUTHOR("Dorian Peake");
277 +MODULE_AUTHOR("Noralf Trønnes");
278  MODULE_DESCRIPTION("Thermal driver for bcm2835 chip");
279 -
280 -module_platform_driver(bcm2835_thermal_driver);
281 +MODULE_LICENSE("GPL");