GNU Linux-libre 4.19.264-gnu1
[releases.git] / drivers / thermal / broadcom / bcm2835_thermal.c
1 /*
2  * Driver for Broadcom BCM2835 SoC temperature sensor
3  *
4  * Copyright (C) 2016 Martin Sperl
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  */
16
17 #include <linux/clk.h>
18 #include <linux/debugfs.h>
19 #include <linux/device.h>
20 #include <linux/err.h>
21 #include <linux/io.h>
22 #include <linux/kernel.h>
23 #include <linux/module.h>
24 #include <linux/of.h>
25 #include <linux/of_address.h>
26 #include <linux/of_device.h>
27 #include <linux/platform_device.h>
28 #include <linux/thermal.h>
29
30 #include "../thermal_hwmon.h"
31
32 #define BCM2835_TS_TSENSCTL                     0x00
33 #define BCM2835_TS_TSENSSTAT                    0x04
34
35 #define BCM2835_TS_TSENSCTL_PRWDW               BIT(0)
36 #define BCM2835_TS_TSENSCTL_RSTB                BIT(1)
37
38 /*
39  * bandgap reference voltage in 6 mV increments
40  * 000b = 1178 mV, 001b = 1184 mV, ... 111b = 1220 mV
41  */
42 #define BCM2835_TS_TSENSCTL_CTRL_BITS           3
43 #define BCM2835_TS_TSENSCTL_CTRL_SHIFT          2
44 #define BCM2835_TS_TSENSCTL_CTRL_MASK               \
45         GENMASK(BCM2835_TS_TSENSCTL_CTRL_BITS +     \
46                 BCM2835_TS_TSENSCTL_CTRL_SHIFT - 1, \
47                 BCM2835_TS_TSENSCTL_CTRL_SHIFT)
48 #define BCM2835_TS_TSENSCTL_CTRL_DEFAULT        1
49 #define BCM2835_TS_TSENSCTL_EN_INT              BIT(5)
50 #define BCM2835_TS_TSENSCTL_DIRECT              BIT(6)
51 #define BCM2835_TS_TSENSCTL_CLR_INT             BIT(7)
52 #define BCM2835_TS_TSENSCTL_THOLD_SHIFT         8
53 #define BCM2835_TS_TSENSCTL_THOLD_BITS          10
54 #define BCM2835_TS_TSENSCTL_THOLD_MASK               \
55         GENMASK(BCM2835_TS_TSENSCTL_THOLD_BITS +     \
56                 BCM2835_TS_TSENSCTL_THOLD_SHIFT - 1, \
57                 BCM2835_TS_TSENSCTL_THOLD_SHIFT)
58 /*
59  * time how long the block to be asserted in reset
60  * which based on a clock counter (TSENS clock assumed)
61  */
62 #define BCM2835_TS_TSENSCTL_RSTDELAY_SHIFT      18
63 #define BCM2835_TS_TSENSCTL_RSTDELAY_BITS       8
64 #define BCM2835_TS_TSENSCTL_REGULEN             BIT(26)
65
66 #define BCM2835_TS_TSENSSTAT_DATA_BITS          10
67 #define BCM2835_TS_TSENSSTAT_DATA_SHIFT         0
68 #define BCM2835_TS_TSENSSTAT_DATA_MASK               \
69         GENMASK(BCM2835_TS_TSENSSTAT_DATA_BITS +     \
70                 BCM2835_TS_TSENSSTAT_DATA_SHIFT - 1, \
71                 BCM2835_TS_TSENSSTAT_DATA_SHIFT)
72 #define BCM2835_TS_TSENSSTAT_VALID              BIT(10)
73 #define BCM2835_TS_TSENSSTAT_INTERRUPT          BIT(11)
74
75 struct bcm2835_thermal_data {
76         struct thermal_zone_device *tz;
77         void __iomem *regs;
78         struct clk *clk;
79         struct dentry *debugfsdir;
80 };
81
82 static int bcm2835_thermal_adc2temp(u32 adc, int offset, int slope)
83 {
84         return offset + slope * adc;
85 }
86
87 static int bcm2835_thermal_temp2adc(int temp, int offset, int slope)
88 {
89         temp -= offset;
90         temp /= slope;
91
92         if (temp < 0)
93                 temp = 0;
94         if (temp >= BIT(BCM2835_TS_TSENSSTAT_DATA_BITS))
95                 temp = BIT(BCM2835_TS_TSENSSTAT_DATA_BITS) - 1;
96
97         return temp;
98 }
99
100 static int bcm2835_thermal_get_temp(void *d, int *temp)
101 {
102         struct bcm2835_thermal_data *data = d;
103         u32 val = readl(data->regs + BCM2835_TS_TSENSSTAT);
104
105         if (!(val & BCM2835_TS_TSENSSTAT_VALID))
106                 return -EIO;
107
108         val &= BCM2835_TS_TSENSSTAT_DATA_MASK;
109
110         *temp = bcm2835_thermal_adc2temp(
111                 val,
112                 thermal_zone_get_offset(data->tz),
113                 thermal_zone_get_slope(data->tz));
114
115         return 0;
116 }
117
118 static const struct debugfs_reg32 bcm2835_thermal_regs[] = {
119         {
120                 .name = "ctl",
121                 .offset = 0
122         },
123         {
124                 .name = "stat",
125                 .offset = 4
126         }
127 };
128
129 static void bcm2835_thermal_debugfs(struct platform_device *pdev)
130 {
131         struct bcm2835_thermal_data *data = platform_get_drvdata(pdev);
132         struct debugfs_regset32 *regset;
133
134         data->debugfsdir = debugfs_create_dir("bcm2835_thermal", NULL);
135         if (!data->debugfsdir)
136                 return;
137
138         regset = devm_kzalloc(&pdev->dev, sizeof(*regset), GFP_KERNEL);
139         if (!regset)
140                 return;
141
142         regset->regs = bcm2835_thermal_regs;
143         regset->nregs = ARRAY_SIZE(bcm2835_thermal_regs);
144         regset->base = data->regs;
145
146         debugfs_create_regset32("regset", 0444, data->debugfsdir, regset);
147 }
148
149 static const struct thermal_zone_of_device_ops bcm2835_thermal_ops = {
150         .get_temp = bcm2835_thermal_get_temp,
151 };
152
153 /*
154  * Note: as per Raspberry Foundation FAQ
155  * (https://www.raspberrypi.org/help/faqs/#performanceOperatingTemperature)
156  * the recommended temperature range for the SoC -40C to +85C
157  * so the trip limit is set to 80C.
158  * this applies to all the BCM283X SoC
159  */
160
161 static const struct of_device_id bcm2835_thermal_of_match_table[] = {
162         {
163                 .compatible = "brcm,bcm2835-thermal",
164         },
165         {
166                 .compatible = "brcm,bcm2836-thermal",
167         },
168         {
169                 .compatible = "brcm,bcm2837-thermal",
170         },
171         {},
172 };
173 MODULE_DEVICE_TABLE(of, bcm2835_thermal_of_match_table);
174
175 static int bcm2835_thermal_probe(struct platform_device *pdev)
176 {
177         const struct of_device_id *match;
178         struct thermal_zone_device *tz;
179         struct bcm2835_thermal_data *data;
180         struct resource *res;
181         int err = 0;
182         u32 val;
183         unsigned long rate;
184
185         data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
186         if (!data)
187                 return -ENOMEM;
188
189         match = of_match_device(bcm2835_thermal_of_match_table,
190                                 &pdev->dev);
191         if (!match)
192                 return -EINVAL;
193
194         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
195         data->regs = devm_ioremap_resource(&pdev->dev, res);
196         if (IS_ERR(data->regs)) {
197                 err = PTR_ERR(data->regs);
198                 dev_err(&pdev->dev, "Could not get registers: %d\n", err);
199                 return err;
200         }
201
202         data->clk = devm_clk_get(&pdev->dev, NULL);
203         if (IS_ERR(data->clk)) {
204                 err = PTR_ERR(data->clk);
205                 if (err != -EPROBE_DEFER)
206                         dev_err(&pdev->dev, "Could not get clk: %d\n", err);
207                 return err;
208         }
209
210         err = clk_prepare_enable(data->clk);
211         if (err)
212                 return err;
213
214         rate = clk_get_rate(data->clk);
215         if ((rate < 1920000) || (rate > 5000000))
216                 dev_warn(&pdev->dev,
217                          "Clock %pCn running at %lu Hz is outside of the recommended range: 1.92 to 5MHz\n",
218                          data->clk, rate);
219
220         /* register of thermal sensor and get info from DT */
221         tz = thermal_zone_of_sensor_register(&pdev->dev, 0, data,
222                                              &bcm2835_thermal_ops);
223         if (IS_ERR(tz)) {
224                 err = PTR_ERR(tz);
225                 dev_err(&pdev->dev,
226                         "Failed to register the thermal device: %d\n",
227                         err);
228                 goto err_clk;
229         }
230
231         /*
232          * right now the FW does set up the HW-block, so we are not
233          * touching the configuration registers.
234          * But if the HW is not enabled, then set it up
235          * using "sane" values used by the firmware right now.
236          */
237         val = readl(data->regs + BCM2835_TS_TSENSCTL);
238         if (!(val & BCM2835_TS_TSENSCTL_RSTB)) {
239                 int trip_temp, offset, slope;
240
241                 slope = thermal_zone_get_slope(tz);
242                 offset = thermal_zone_get_offset(tz);
243                 /*
244                  * For now we deal only with critical, otherwise
245                  * would need to iterate
246                  */
247                 err = tz->ops->get_trip_temp(tz, 0, &trip_temp);
248                 if (err < 0) {
249                         dev_err(&pdev->dev,
250                                 "Not able to read trip_temp: %d\n",
251                                 err);
252                         goto err_tz;
253                 }
254
255                 /* set bandgap reference voltage and enable voltage regulator */
256                 val = (BCM2835_TS_TSENSCTL_CTRL_DEFAULT <<
257                        BCM2835_TS_TSENSCTL_CTRL_SHIFT) |
258                       BCM2835_TS_TSENSCTL_REGULEN;
259
260                 /* use the recommended reset duration */
261                 val |= (0xFE << BCM2835_TS_TSENSCTL_RSTDELAY_SHIFT);
262
263                 /*  trip_adc value from info */
264                 val |= bcm2835_thermal_temp2adc(trip_temp,
265                                                 offset,
266                                                 slope)
267                         << BCM2835_TS_TSENSCTL_THOLD_SHIFT;
268
269                 /* write the value back to the register as 2 steps */
270                 writel(val, data->regs + BCM2835_TS_TSENSCTL);
271                 val |= BCM2835_TS_TSENSCTL_RSTB;
272                 writel(val, data->regs + BCM2835_TS_TSENSCTL);
273         }
274
275         data->tz = tz;
276
277         platform_set_drvdata(pdev, data);
278
279         /*
280          * Thermal_zone doesn't enable hwmon as default,
281          * enable it here
282          */
283         tz->tzp->no_hwmon = false;
284         err = thermal_add_hwmon_sysfs(tz);
285         if (err)
286                 goto err_tz;
287
288         bcm2835_thermal_debugfs(pdev);
289
290         return 0;
291 err_tz:
292         thermal_zone_of_sensor_unregister(&pdev->dev, tz);
293 err_clk:
294         clk_disable_unprepare(data->clk);
295
296         return err;
297 }
298
299 static int bcm2835_thermal_remove(struct platform_device *pdev)
300 {
301         struct bcm2835_thermal_data *data = platform_get_drvdata(pdev);
302         struct thermal_zone_device *tz = data->tz;
303
304         debugfs_remove_recursive(data->debugfsdir);
305         thermal_zone_of_sensor_unregister(&pdev->dev, tz);
306         clk_disable_unprepare(data->clk);
307
308         return 0;
309 }
310
311 static struct platform_driver bcm2835_thermal_driver = {
312         .probe = bcm2835_thermal_probe,
313         .remove = bcm2835_thermal_remove,
314         .driver = {
315                 .name = "bcm2835_thermal",
316                 .of_match_table = bcm2835_thermal_of_match_table,
317         },
318 };
319 module_platform_driver(bcm2835_thermal_driver);
320
321 MODULE_AUTHOR("Martin Sperl");
322 MODULE_DESCRIPTION("Thermal driver for bcm2835 chip");
323 MODULE_LICENSE("GPL");