]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - drivers/thermal/rcar_gen3_thermal.c
thermal: rcar_gen3_thermal: remove unneeded mutex
[mirror_ubuntu-jammy-kernel.git] / drivers / thermal / rcar_gen3_thermal.c
CommitLineData
564e73d2
WS
1/*
2 * R-Car Gen3 THS thermal sensor driver
3 * Based on rcar_thermal.c and work from Hien Dang and Khiem Nguyen.
4 *
5 * Copyright (C) 2016 Renesas Electronics Corporation.
6 * Copyright (C) 2016 Sang Engineering
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; version 2 of the License.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 */
18#include <linux/delay.h>
19#include <linux/err.h>
20#include <linux/interrupt.h>
21#include <linux/io.h>
22#include <linux/module.h>
564e73d2
WS
23#include <linux/of_device.h>
24#include <linux/platform_device.h>
25#include <linux/pm_runtime.h>
26#include <linux/thermal.h>
27
28/* Register offsets */
29#define REG_GEN3_IRQSTR 0x04
30#define REG_GEN3_IRQMSK 0x08
31#define REG_GEN3_IRQCTL 0x0C
32#define REG_GEN3_IRQEN 0x10
33#define REG_GEN3_IRQTEMP1 0x14
34#define REG_GEN3_IRQTEMP2 0x18
35#define REG_GEN3_IRQTEMP3 0x1C
36#define REG_GEN3_CTSR 0x20
37#define REG_GEN3_THCTR 0x20
38#define REG_GEN3_TEMP 0x28
39#define REG_GEN3_THCODE1 0x50
40#define REG_GEN3_THCODE2 0x54
41#define REG_GEN3_THCODE3 0x58
42
43/* CTSR bits */
44#define CTSR_PONM BIT(8)
45#define CTSR_AOUT BIT(7)
46#define CTSR_THBGR BIT(5)
47#define CTSR_VMEN BIT(4)
48#define CTSR_VMST BIT(1)
49#define CTSR_THSST BIT(0)
50
51/* THCTR bits */
52#define THCTR_PONM BIT(6)
53#define THCTR_THSST BIT(0)
54
55#define CTEMP_MASK 0xFFF
56
57#define MCELSIUS(temp) ((temp) * 1000)
58#define GEN3_FUSE_MASK 0xFFF
59
60#define TSC_MAX_NUM 3
61
62/* Structure for thermal temperature calculation */
63struct equation_coefs {
64 int a1;
65 int b1;
66 int a2;
67 int b2;
68};
69
70struct rcar_gen3_thermal_tsc {
71 void __iomem *base;
72 struct thermal_zone_device *zone;
73 struct equation_coefs coef;
564e73d2
WS
74};
75
76struct rcar_gen3_thermal_priv {
77 struct rcar_gen3_thermal_tsc *tscs[TSC_MAX_NUM];
78};
79
80struct rcar_gen3_thermal_data {
81 void (*thermal_init)(struct rcar_gen3_thermal_tsc *tsc);
82};
83
84static inline u32 rcar_gen3_thermal_read(struct rcar_gen3_thermal_tsc *tsc,
85 u32 reg)
86{
87 return ioread32(tsc->base + reg);
88}
89
90static inline void rcar_gen3_thermal_write(struct rcar_gen3_thermal_tsc *tsc,
91 u32 reg, u32 data)
92{
93 iowrite32(data, tsc->base + reg);
94}
95
96/*
97 * Linear approximation for temperature
98 *
99 * [reg] = [temp] * a + b => [temp] = ([reg] - b) / a
100 *
101 * The constants a and b are calculated using two triplets of int values PTAT
102 * and THCODE. PTAT and THCODE can either be read from hardware or use hard
103 * coded values from driver. The formula to calculate a and b are taken from
104 * BSP and sparsely documented and understood.
105 *
106 * Examining the linear formula and the formula used to calculate constants a
107 * and b while knowing that the span for PTAT and THCODE values are between
108 * 0x000 and 0xfff the largest integer possible is 0xfff * 0xfff == 0xffe001.
109 * Integer also needs to be signed so that leaves 7 bits for binary
110 * fixed point scaling.
111 */
112
113#define FIXPT_SHIFT 7
114#define FIXPT_INT(_x) ((_x) << FIXPT_SHIFT)
115#define FIXPT_DIV(_a, _b) DIV_ROUND_CLOSEST(((_a) << FIXPT_SHIFT), (_b))
116#define FIXPT_TO_MCELSIUS(_x) ((_x) * 1000 >> FIXPT_SHIFT)
117
118#define RCAR3_THERMAL_GRAN 500 /* mili Celsius */
119
120/* no idea where these constants come from */
121#define TJ_1 96
122#define TJ_3 -41
123
124static void rcar_gen3_thermal_calc_coefs(struct equation_coefs *coef,
125 int *ptat, int *thcode)
126{
127 int tj_2;
128
129 /* TODO: Find documentation and document constant calculation formula */
130
131 /*
132 * Division is not scaled in BSP and if scaled it might overflow
133 * the dividend (4095 * 4095 << 14 > INT_MAX) so keep it unscaled
134 */
135 tj_2 = (FIXPT_INT((ptat[1] - ptat[2]) * 137)
136 / (ptat[0] - ptat[2])) - FIXPT_INT(41);
137
138 coef->a1 = FIXPT_DIV(FIXPT_INT(thcode[1] - thcode[2]),
139 tj_2 - FIXPT_INT(TJ_3));
140 coef->b1 = FIXPT_INT(thcode[2]) - coef->a1 * TJ_3;
141
142 coef->a2 = FIXPT_DIV(FIXPT_INT(thcode[1] - thcode[0]),
143 tj_2 - FIXPT_INT(TJ_1));
144 coef->b2 = FIXPT_INT(thcode[0]) - coef->a2 * TJ_1;
145}
146
147static int rcar_gen3_thermal_round(int temp)
148{
149 int result, round_offs;
150
151 round_offs = temp >= 0 ? RCAR3_THERMAL_GRAN / 2 :
152 -RCAR3_THERMAL_GRAN / 2;
153 result = (temp + round_offs) / RCAR3_THERMAL_GRAN;
154 return result * RCAR3_THERMAL_GRAN;
155}
156
157static int rcar_gen3_thermal_get_temp(void *devdata, int *temp)
158{
159 struct rcar_gen3_thermal_tsc *tsc = devdata;
160 int mcelsius, val1, val2;
161 u32 reg;
162
163 /* Read register and convert to mili Celsius */
564e73d2
WS
164 reg = rcar_gen3_thermal_read(tsc, REG_GEN3_TEMP) & CTEMP_MASK;
165
166 val1 = FIXPT_DIV(FIXPT_INT(reg) - tsc->coef.b1, tsc->coef.a1);
167 val2 = FIXPT_DIV(FIXPT_INT(reg) - tsc->coef.b2, tsc->coef.a2);
168 mcelsius = FIXPT_TO_MCELSIUS((val1 + val2) / 2);
169
564e73d2
WS
170 /* Make sure we are inside specifications */
171 if ((mcelsius < MCELSIUS(-40)) || (mcelsius > MCELSIUS(125)))
172 return -EIO;
173
174 /* Round value to device granularity setting */
175 *temp = rcar_gen3_thermal_round(mcelsius);
176
177 return 0;
178}
179
180static struct thermal_zone_of_device_ops rcar_gen3_tz_of_ops = {
181 .get_temp = rcar_gen3_thermal_get_temp,
182};
183
184static void r8a7795_thermal_init(struct rcar_gen3_thermal_tsc *tsc)
185{
186 rcar_gen3_thermal_write(tsc, REG_GEN3_CTSR, CTSR_THBGR);
187 rcar_gen3_thermal_write(tsc, REG_GEN3_CTSR, 0x0);
188
189 usleep_range(1000, 2000);
190
191 rcar_gen3_thermal_write(tsc, REG_GEN3_CTSR, CTSR_PONM);
192 rcar_gen3_thermal_write(tsc, REG_GEN3_IRQCTL, 0x3F);
193 rcar_gen3_thermal_write(tsc, REG_GEN3_CTSR,
194 CTSR_PONM | CTSR_AOUT | CTSR_THBGR | CTSR_VMEN);
195
196 usleep_range(100, 200);
197
198 rcar_gen3_thermal_write(tsc, REG_GEN3_CTSR,
199 CTSR_PONM | CTSR_AOUT | CTSR_THBGR | CTSR_VMEN |
200 CTSR_VMST | CTSR_THSST);
201
202 usleep_range(1000, 2000);
203}
204
205static void r8a7796_thermal_init(struct rcar_gen3_thermal_tsc *tsc)
206{
207 u32 reg_val;
208
209 reg_val = rcar_gen3_thermal_read(tsc, REG_GEN3_THCTR);
210 reg_val &= ~THCTR_PONM;
211 rcar_gen3_thermal_write(tsc, REG_GEN3_THCTR, reg_val);
212
213 usleep_range(1000, 2000);
214
215 rcar_gen3_thermal_write(tsc, REG_GEN3_IRQCTL, 0x3F);
216 reg_val = rcar_gen3_thermal_read(tsc, REG_GEN3_THCTR);
217 reg_val |= THCTR_THSST;
218 rcar_gen3_thermal_write(tsc, REG_GEN3_THCTR, reg_val);
78aefd2d
NS
219
220 usleep_range(1000, 2000);
564e73d2
WS
221}
222
223static const struct rcar_gen3_thermal_data r8a7795_data = {
224 .thermal_init = r8a7795_thermal_init,
225};
226
227static const struct rcar_gen3_thermal_data r8a7796_data = {
228 .thermal_init = r8a7796_thermal_init,
229};
230
231static const struct of_device_id rcar_gen3_thermal_dt_ids[] = {
232 { .compatible = "renesas,r8a7795-thermal", .data = &r8a7795_data},
233 { .compatible = "renesas,r8a7796-thermal", .data = &r8a7796_data},
234 {},
235};
236MODULE_DEVICE_TABLE(of, rcar_gen3_thermal_dt_ids);
237
238static int rcar_gen3_thermal_remove(struct platform_device *pdev)
239{
240 struct device *dev = &pdev->dev;
241
242 pm_runtime_put(dev);
243 pm_runtime_disable(dev);
244
245 return 0;
246}
247
248static int rcar_gen3_thermal_probe(struct platform_device *pdev)
249{
250 struct rcar_gen3_thermal_priv *priv;
251 struct device *dev = &pdev->dev;
252 struct resource *res;
253 struct thermal_zone_device *zone;
254 int ret, i;
255 const struct rcar_gen3_thermal_data *match_data =
256 of_device_get_match_data(dev);
257
258 /* default values if FUSEs are missing */
259 /* TODO: Read values from hardware on supported platforms */
260 int ptat[3] = { 2351, 1509, 435 };
261 int thcode[TSC_MAX_NUM][3] = {
262 { 3248, 2800, 2221 },
263 { 3245, 2795, 2216 },
264 { 3250, 2805, 2237 },
265 };
266
267 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
268 if (!priv)
269 return -ENOMEM;
270
271 platform_set_drvdata(pdev, priv);
272
273 pm_runtime_enable(dev);
274 pm_runtime_get_sync(dev);
275
276 for (i = 0; i < TSC_MAX_NUM; i++) {
277 struct rcar_gen3_thermal_tsc *tsc;
278
279 tsc = devm_kzalloc(dev, sizeof(*tsc), GFP_KERNEL);
280 if (!tsc) {
281 ret = -ENOMEM;
282 goto error_unregister;
283 }
284
285 res = platform_get_resource(pdev, IORESOURCE_MEM, i);
286 if (!res)
287 break;
288
289 tsc->base = devm_ioremap_resource(dev, res);
290 if (IS_ERR(tsc->base)) {
291 ret = PTR_ERR(tsc->base);
292 goto error_unregister;
293 }
294
295 priv->tscs[i] = tsc;
564e73d2
WS
296
297 match_data->thermal_init(tsc);
298 rcar_gen3_thermal_calc_coefs(&tsc->coef, ptat, thcode[i]);
299
300 zone = devm_thermal_zone_of_sensor_register(dev, i, tsc,
301 &rcar_gen3_tz_of_ops);
302 if (IS_ERR(zone)) {
303 dev_err(dev, "Can't register thermal zone\n");
304 ret = PTR_ERR(zone);
305 goto error_unregister;
306 }
307 tsc->zone = zone;
308 }
309
310 return 0;
311
312error_unregister:
313 rcar_gen3_thermal_remove(pdev);
314
315 return ret;
316}
317
318static struct platform_driver rcar_gen3_thermal_driver = {
319 .driver = {
320 .name = "rcar_gen3_thermal",
321 .of_match_table = rcar_gen3_thermal_dt_ids,
322 },
323 .probe = rcar_gen3_thermal_probe,
324 .remove = rcar_gen3_thermal_remove,
325};
326module_platform_driver(rcar_gen3_thermal_driver);
327
328MODULE_LICENSE("GPL v2");
329MODULE_DESCRIPTION("R-Car Gen3 THS thermal sensor driver");
330MODULE_AUTHOR("Wolfram Sang <wsa+renesas@sang-engineering.com>");