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