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
6 Content-Type: text/plain; charset=UTF-8
7 Content-Transfer-Encoding: 8bit
9 Use the new firmware API instead of the legacy mailbox API.
10 Remove retry loop on failure to read temperature.
13 Signed-off-by: Noralf Trønnes <noralf@tronnes.org>
15 arch/arm/boot/dts/bcm2708_common.dtsi | 1 +
16 drivers/thermal/bcm2835-thermal.c | 197 +++++++++++++---------------------
17 2 files changed, 75 insertions(+), 123 deletions(-)
19 --- a/arch/arm/boot/dts/bcm2708_common.dtsi
20 +++ b/arch/arm/boot/dts/bcm2708_common.dtsi
24 compatible = "brcm,bcm2835-thermal";
25 + firmware = <&firmware>;
29 --- a/drivers/thermal/bcm2835-thermal.c
30 +++ b/drivers/thermal/bcm2835-thermal.c
33 *****************************************************************************/
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>
46 -/* --- DEFINITIONS --- */
47 -#define MODULE_NAME "bcm2835_thermal"
49 -/*#define THERMAL_DEBUG_ENABLE*/
51 -#ifdef THERMAL_DEBUG_ENABLE
52 -#define print_debug(fmt,...) printk(KERN_INFO "%s:%s:%d: "fmt"\n", MODULE_NAME, __func__, __LINE__, ##__VA_ARGS__)
54 -#define print_debug(fmt,...)
56 -#define print_err(fmt,...) printk(KERN_ERR "%s:%s:%d: "fmt"\n", MODULE_NAME, __func__,__LINE__, ##__VA_ARGS__)
58 -#define VC_TAG_GET_TEMP 0x00030006
59 -#define VC_TAG_GET_MAX_TEMP 0x0003000A
66 -/* --- STRUCTS --- */
67 -/* tag part of the message */
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 */
76 -/* message structure to be sent to videocore */
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 */
84 -struct bcm2835_thermal_data {
85 - struct thermal_zone_device *thermal_dev;
89 -/* --- GLOBALS --- */
90 -static struct bcm2835_thermal_data bcm2835_data;
92 -/* Thermal Device Operations */
93 -static struct thermal_zone_device_ops ops;
95 -/* --- FUNCTIONS --- */
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)
101 - int result = -1, retry = 3;
103 + struct rpi_firmware *fw = tz->devdata;
111 - while (result != 0 && retry-- > 0) {
112 - /* wipe all previous message data */
113 - memset(&bcm2835_data.msg, 0, sizeof bcm2835_data.msg);
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;
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))
126 + ret = rpi_firmware_property(fw, tag, &packet, sizeof(packet));
128 + dev_err(&tz->device, "Failed to get temperature\n");
132 - /* check if it was all ok and return the rate in milli degrees C */
134 - *temp = (uint)bcm2835_data.msg.tag.val;
136 - print_err("Failed to get temperature! (%x:%d)\n", tag_id, result);
137 - print_debug("OUT");
139 + *temp = packet.val;
140 + dev_dbg(&tz->device, "%stemp=%lu\n",
141 + tag == RPI_FIRMWARE_GET_MAX_TEMPERATURE ? "max" : "", *temp);
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)
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);
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)
159 - return bcm2835_get_temp_or_max(thermal_dev, temp, VC_TAG_GET_MAX_TEMP);
161 + * The maximum safe temperature of the SoC.
162 + * Overclock may be disabled above this temperature.
164 + return bcm2835_thermal_get_property(tz, temp,
165 + RPI_FIRMWARE_GET_MAX_TEMPERATURE);
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)
172 - *trip_type = THERMAL_TRIP_HOT;
173 + *type = THERMAL_TRIP_HOT;
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)
183 - *dev_mode = THERMAL_DEVICE_ENABLED;
184 + *mode = THERMAL_DEVICE_ENABLED;
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,
196 static int bcm2835_thermal_probe(struct platform_device *pdev)
199 - print_debug("THERMAL Driver has been probed!");
201 - /* check that the device isn't null!*/
204 - print_debug("Platform device is empty!");
206 + struct device_node *fw_np;
207 + struct rpi_firmware *fw;
208 + struct thermal_zone_device *tz;
210 + fw_np = of_parse_phandle(pdev->dev.of_node, "firmware", 0);
211 +/* Remove comment when booting without Device Tree is no longer supported
213 + dev_err(&pdev->dev, "Missing firmware node\n");
217 - if(!(bcm2835_data.thermal_dev = thermal_zone_device_register("bcm2835_thermal", 1, 0, NULL, &ops, NULL, 0, 0)))
219 - print_debug("Unable to register the thermal device!");
222 + fw = rpi_firmware_get(fw_np);
224 + return -EPROBE_DEFER;
226 + tz = thermal_zone_device_register("bcm2835_thermal", 1, 0, fw, &ops,
229 + dev_err(&pdev->dev, "Failed to register the thermal device\n");
230 + return PTR_ERR(tz);
233 + platform_set_drvdata(pdev, tz);
239 static int bcm2835_thermal_remove(struct platform_device *pdev)
243 - thermal_zone_device_unregister(bcm2835_data.thermal_dev);
245 - print_debug("OUT");
246 + thermal_zone_device_unregister(platform_get_drvdata(pdev));
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,
258 static const struct of_device_id bcm2835_thermal_of_match_table[] = {
259 { .compatible = "brcm,bcm2835-thermal", },
261 @@ -177,14 +129,13 @@ static struct platform_driver bcm2835_th
262 .probe = bcm2835_thermal_probe,
263 .remove = bcm2835_thermal_remove,
265 - .name = "bcm2835_thermal",
266 - .owner = THIS_MODULE,
267 - .of_match_table = bcm2835_thermal_of_match_table,
269 + .name = "bcm2835_thermal",
270 + .of_match_table = bcm2835_thermal_of_match_table,
273 +module_platform_driver(bcm2835_thermal_driver);
275 -MODULE_LICENSE("GPL");
276 MODULE_AUTHOR("Dorian Peake");
277 +MODULE_AUTHOR("Noralf Trønnes");
278 MODULE_DESCRIPTION("Thermal driver for bcm2835 chip");
280 -module_platform_driver(bcm2835_thermal_driver);
281 +MODULE_LICENSE("GPL");