xref: /linux/drivers/thermal/k3_j72xx_bandgap.c (revision 08b7174fb8d126e607e385e34b9e1da4f3be274f)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * TI Bandgap temperature sensor driver for J72XX SoC Family
4  *
5  * Copyright (C) 2021 Texas Instruments Incorporated - http://www.ti.com/
6  */
7 
8 #include <linux/math.h>
9 #include <linux/math64.h>
10 #include <linux/module.h>
11 #include <linux/init.h>
12 #include <linux/kernel.h>
13 #include <linux/platform_device.h>
14 #include <linux/pm_runtime.h>
15 #include <linux/err.h>
16 #include <linux/types.h>
17 #include <linux/io.h>
18 #include <linux/thermal.h>
19 #include <linux/of.h>
20 #include <linux/delay.h>
21 #include <linux/slab.h>
22 
23 #define K3_VTM_DEVINFO_PWR0_OFFSET		0x4
24 #define K3_VTM_DEVINFO_PWR0_TEMPSENS_CT_MASK	0xf0
25 #define K3_VTM_TMPSENS0_CTRL_OFFSET		0x300
26 #define K3_VTM_MISC_CTRL_OFFSET			0xc
27 #define K3_VTM_TMPSENS_STAT_OFFSET		0x8
28 #define K3_VTM_ANYMAXT_OUTRG_ALERT_EN		0x1
29 #define K3_VTM_MISC_CTRL2_OFFSET		0x10
30 #define K3_VTM_TS_STAT_DTEMP_MASK		0x3ff
31 #define K3_VTM_MAX_NUM_TS			8
32 #define K3_VTM_TMPSENS_CTRL_SOC			BIT(5)
33 #define K3_VTM_TMPSENS_CTRL_CLRZ		BIT(6)
34 #define K3_VTM_TMPSENS_CTRL_CLKON_REQ		BIT(7)
35 #define K3_VTM_TMPSENS_CTRL_MAXT_OUTRG_EN	BIT(11)
36 
37 #define K3_VTM_CORRECTION_TEMP_CNT		3
38 
39 #define MINUS40CREF				5
40 #define PLUS30CREF				253
41 #define PLUS125CREF				730
42 #define PLUS150CREF				940
43 
44 #define TABLE_SIZE				1024
45 #define MAX_TEMP				123000
46 #define COOL_DOWN_TEMP				105000
47 
48 #define FACTORS_REDUCTION			13
49 static int *derived_table;
50 
51 static int compute_value(int index, const s64 *factors, int nr_factors,
52 			 int reduction)
53 {
54 	s64 value = 0;
55 	int i;
56 
57 	for (i = 0; i < nr_factors; i++)
58 		value += factors[i] * int_pow(index, i);
59 
60 	return (int)div64_s64(value, int_pow(10, reduction));
61 }
62 
63 static void init_table(int factors_size, int *table, const s64 *factors)
64 {
65 	int i;
66 
67 	for (i = 0; i < TABLE_SIZE; i++)
68 		table[i] = compute_value(i, factors, factors_size,
69 					 FACTORS_REDUCTION);
70 }
71 
72 /**
73  * struct err_values - structure containing error/reference values
74  * @refs: reference error values for -40C, 30C, 125C & 150C
75  * @errs: Actual error values for -40C, 30C, 125C & 150C read from the efuse
76  */
77 struct err_values {
78 	int refs[4];
79 	int errs[4];
80 };
81 
82 static void create_table_segments(struct err_values *err_vals, int seg,
83 				  int *ref_table)
84 {
85 	int m = 0, c, num, den, i, err, idx1, idx2, err1, err2, ref1, ref2;
86 
87 	if (seg == 0)
88 		idx1 = 0;
89 	else
90 		idx1 = err_vals->refs[seg];
91 
92 	idx2 = err_vals->refs[seg + 1];
93 	err1 = err_vals->errs[seg];
94 	err2 = err_vals->errs[seg + 1];
95 	ref1 = err_vals->refs[seg];
96 	ref2 = err_vals->refs[seg + 1];
97 
98 	/*
99 	 * Calculate the slope with adc values read from the register
100 	 * as the y-axis param and err in adc value as x-axis param
101 	 */
102 	num = ref2 - ref1;
103 	den = err2 - err1;
104 	if (den)
105 		m = num / den;
106 	c = ref2 - m * err2;
107 
108 	/*
109 	 * Take care of divide by zero error if error values are same
110 	 * Or when the slope is 0
111 	 */
112 	if (den != 0 && m != 0) {
113 		for (i = idx1; i <= idx2; i++) {
114 			err = (i - c) / m;
115 			if (((i + err) < 0) || ((i + err) >= TABLE_SIZE))
116 				continue;
117 			derived_table[i] = ref_table[i + err];
118 		}
119 	} else { /* Constant error take care of divide by zero */
120 		for (i = idx1; i <= idx2; i++) {
121 			if (((i + err1) < 0) || ((i + err1) >= TABLE_SIZE))
122 				continue;
123 			derived_table[i] = ref_table[i + err1];
124 		}
125 	}
126 }
127 
128 static int prep_lookup_table(struct err_values *err_vals, int *ref_table)
129 {
130 	int inc, i, seg;
131 
132 	/*
133 	 * Fill up the lookup table under 3 segments
134 	 * region -40C to +30C
135 	 * region +30C to +125C
136 	 * region +125C to +150C
137 	 */
138 	for (seg = 0; seg < 3; seg++)
139 		create_table_segments(err_vals, seg, ref_table);
140 
141 	/* Get to the first valid temperature */
142 	i = 0;
143 	while (!derived_table[i])
144 		i++;
145 
146 	/*
147 	 * Get to the last zero index and back fill the temperature for
148 	 * sake of continuity
149 	 */
150 	if (i) {
151 		/* 300 milli celsius steps */
152 		while (i--)
153 			derived_table[i] = derived_table[i + 1] - 300;
154 	}
155 
156 	/*
157 	 * Fill the last trailing 0s which are unfilled with increments of
158 	 * 100 milli celsius till 1023 code
159 	 */
160 	i = TABLE_SIZE - 1;
161 	while (!derived_table[i])
162 		i--;
163 
164 	i++;
165 	inc = 1;
166 	while (i < TABLE_SIZE) {
167 		derived_table[i] = derived_table[i - 1] + inc * 100;
168 		i++;
169 	}
170 
171 	return 0;
172 }
173 
174 struct k3_thermal_data;
175 
176 struct k3_j72xx_bandgap {
177 	struct device *dev;
178 	void __iomem *base;
179 	void __iomem *cfg2_base;
180 	struct k3_thermal_data *ts_data[K3_VTM_MAX_NUM_TS];
181 };
182 
183 /* common data structures */
184 struct k3_thermal_data {
185 	struct k3_j72xx_bandgap *bgp;
186 	u32 ctrl_offset;
187 	u32 stat_offset;
188 };
189 
190 static int two_cmp(int tmp, int mask)
191 {
192 	tmp = ~(tmp);
193 	tmp &= mask;
194 	tmp += 1;
195 
196 	/* Return negative value */
197 	return (0 - tmp);
198 }
199 
200 static unsigned int vtm_get_best_value(unsigned int s0, unsigned int s1,
201 				       unsigned int s2)
202 {
203 	int d01 = abs(s0 - s1);
204 	int d02 = abs(s0 - s2);
205 	int d12 = abs(s1 - s2);
206 
207 	if (d01 <= d02 && d01 <= d12)
208 		return (s0 + s1) / 2;
209 
210 	if (d02 <= d01 && d02 <= d12)
211 		return (s0 + s2) / 2;
212 
213 	return (s1 + s2) / 2;
214 }
215 
216 static inline int k3_bgp_read_temp(struct k3_thermal_data *devdata,
217 				   int *temp)
218 {
219 	struct k3_j72xx_bandgap *bgp;
220 	unsigned int dtemp, s0, s1, s2;
221 
222 	bgp = devdata->bgp;
223 	/*
224 	 * Errata is applicable for am654 pg 1.0 silicon/J7ES. There
225 	 * is a variation of the order for certain degree centigrade on AM654.
226 	 * Work around that by getting the average of two closest
227 	 * readings out of three readings everytime we want to
228 	 * report temperatures.
229 	 *
230 	 * Errata workaround.
231 	 */
232 	s0 = readl(bgp->base + devdata->stat_offset) &
233 		K3_VTM_TS_STAT_DTEMP_MASK;
234 	s1 = readl(bgp->base + devdata->stat_offset) &
235 		K3_VTM_TS_STAT_DTEMP_MASK;
236 	s2 = readl(bgp->base + devdata->stat_offset) &
237 		K3_VTM_TS_STAT_DTEMP_MASK;
238 	dtemp = vtm_get_best_value(s0, s1, s2);
239 
240 	if (dtemp < 0 || dtemp >= TABLE_SIZE)
241 		return -EINVAL;
242 
243 	*temp = derived_table[dtemp];
244 
245 	return 0;
246 }
247 
248 /* Get temperature callback function for thermal zone */
249 static int k3_thermal_get_temp(struct thermal_zone_device *tz, int *temp)
250 {
251 	return k3_bgp_read_temp(thermal_zone_device_priv(tz), temp);
252 }
253 
254 static const struct thermal_zone_device_ops k3_of_thermal_ops = {
255 	.get_temp = k3_thermal_get_temp,
256 };
257 
258 static int k3_j72xx_bandgap_temp_to_adc_code(int temp)
259 {
260 	int low = 0, high = TABLE_SIZE - 1, mid;
261 
262 	if (temp > 160000 || temp < -50000)
263 		return -EINVAL;
264 
265 	/* Binary search to find the adc code */
266 	while (low < (high - 1)) {
267 		mid = (low + high) / 2;
268 		if (temp <= derived_table[mid])
269 			high = mid;
270 		else
271 			low = mid;
272 	}
273 
274 	return mid;
275 }
276 
277 static void get_efuse_values(int id, struct k3_thermal_data *data, int *err,
278 			     void __iomem *fuse_base)
279 {
280 	int i, tmp, pow;
281 	int ct_offsets[5][K3_VTM_CORRECTION_TEMP_CNT] = {
282 		{ 0x0, 0x8, 0x4 },
283 		{ 0x0, 0x8, 0x4 },
284 		{ 0x0, -1,  0x4 },
285 		{ 0x0, 0xC, -1 },
286 		{ 0x0, 0xc, 0x8 }
287 	};
288 	int ct_bm[5][K3_VTM_CORRECTION_TEMP_CNT] = {
289 		{ 0x3f, 0x1fe000, 0x1ff },
290 		{ 0xfc0, 0x1fe000, 0x3fe00 },
291 		{ 0x3f000, 0x7f800000, 0x7fc0000 },
292 		{ 0xfc0000, 0x1fe0, 0x1f800000 },
293 		{ 0x3f000000, 0x1fe000, 0x1ff0 }
294 	};
295 
296 	for (i = 0; i < 3; i++) {
297 		/* Extract the offset value using bit-mask */
298 		if (ct_offsets[id][i] == -1 && i == 1) {
299 			/* 25C offset Case of Sensor 2 split between 2 regs */
300 			tmp = (readl(fuse_base + 0x8) & 0xE0000000) >> (29);
301 			tmp |= ((readl(fuse_base + 0xC) & 0x1F) << 3);
302 			pow = tmp & 0x80;
303 		} else if (ct_offsets[id][i] == -1 && i == 2) {
304 			/* 125C Case of Sensor 3 split between 2 regs */
305 			tmp = (readl(fuse_base + 0x4) & 0xF8000000) >> (27);
306 			tmp |= ((readl(fuse_base + 0x8) & 0xF) << 5);
307 			pow = tmp & 0x100;
308 		} else {
309 			tmp = readl(fuse_base + ct_offsets[id][i]);
310 			tmp &= ct_bm[id][i];
311 			tmp = tmp >> __ffs(ct_bm[id][i]);
312 
313 			/* Obtain the sign bit pow*/
314 			pow = ct_bm[id][i] >> __ffs(ct_bm[id][i]);
315 			pow += 1;
316 			pow /= 2;
317 		}
318 
319 		/* Check for negative value */
320 		if (tmp & pow) {
321 			/* 2's complement value */
322 			tmp = two_cmp(tmp, ct_bm[id][i] >> __ffs(ct_bm[id][i]));
323 		}
324 		err[i] = tmp;
325 	}
326 
327 	/* Err value for 150C is set to 0 */
328 	err[i] = 0;
329 }
330 
331 static void print_look_up_table(struct device *dev, int *ref_table)
332 {
333 	int i;
334 
335 	dev_dbg(dev, "The contents of derived array\n");
336 	dev_dbg(dev, "Code   Temperature\n");
337 	for (i = 0; i < TABLE_SIZE; i++)
338 		dev_dbg(dev, "%d       %d %d\n", i, derived_table[i], ref_table[i]);
339 }
340 
341 struct k3_j72xx_bandgap_data {
342 	const bool has_errata_i2128;
343 };
344 
345 static int k3_j72xx_bandgap_probe(struct platform_device *pdev)
346 {
347 	int ret = 0, cnt, val, id;
348 	int high_max, low_temp;
349 	struct resource *res;
350 	struct device *dev = &pdev->dev;
351 	struct k3_j72xx_bandgap *bgp;
352 	struct k3_thermal_data *data;
353 	bool workaround_needed = false;
354 	const struct k3_j72xx_bandgap_data *driver_data;
355 	struct thermal_zone_device *ti_thermal;
356 	int *ref_table;
357 	struct err_values err_vals;
358 	void __iomem *fuse_base;
359 
360 	const s64 golden_factors[] = {
361 		-490019999999999936,
362 		3251200000000000,
363 		-1705800000000,
364 		603730000,
365 		-92627,
366 	};
367 
368 	const s64 pvt_wa_factors[] = {
369 		-415230000000000000,
370 		3126600000000000,
371 		-1157800000000,
372 	};
373 
374 	bgp = devm_kzalloc(&pdev->dev, sizeof(*bgp), GFP_KERNEL);
375 	if (!bgp)
376 		return -ENOMEM;
377 
378 	bgp->dev = dev;
379 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
380 	bgp->base = devm_ioremap_resource(dev, res);
381 	if (IS_ERR(bgp->base))
382 		return PTR_ERR(bgp->base);
383 
384 	res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
385 	bgp->cfg2_base = devm_ioremap_resource(dev, res);
386 	if (IS_ERR(bgp->cfg2_base))
387 		return PTR_ERR(bgp->cfg2_base);
388 
389 	driver_data = of_device_get_match_data(dev);
390 	if (driver_data)
391 		workaround_needed = driver_data->has_errata_i2128;
392 
393 	/*
394 	 * Some of TI's J721E SoCs require a software trimming procedure
395 	 * for the temperature monitors to function properly. To determine
396 	 * if this particular SoC is NOT affected, both bits in the
397 	 * WKUP_SPARE_FUSE0[31:30] will be set (0xC0000000) indicating
398 	 * when software trimming should NOT be applied.
399 	 *
400 	 * https://www.ti.com/lit/er/sprz455c/sprz455c.pdf
401 	 */
402 	if (workaround_needed) {
403 		res = platform_get_resource(pdev, IORESOURCE_MEM, 2);
404 		fuse_base = devm_ioremap_resource(dev, res);
405 		if (IS_ERR(fuse_base))
406 			return PTR_ERR(fuse_base);
407 
408 		if ((readl(fuse_base) & 0xc0000000) == 0xc0000000)
409 			workaround_needed = false;
410 	}
411 
412 	dev_dbg(bgp->dev, "Work around %sneeded\n",
413 		workaround_needed ? "" : "not ");
414 
415 	pm_runtime_enable(dev);
416 	ret = pm_runtime_get_sync(dev);
417 	if (ret < 0) {
418 		pm_runtime_put_noidle(dev);
419 		pm_runtime_disable(dev);
420 		return ret;
421 	}
422 
423 	/* Get the sensor count in the VTM */
424 	val = readl(bgp->base + K3_VTM_DEVINFO_PWR0_OFFSET);
425 	cnt = val & K3_VTM_DEVINFO_PWR0_TEMPSENS_CT_MASK;
426 	cnt >>= __ffs(K3_VTM_DEVINFO_PWR0_TEMPSENS_CT_MASK);
427 
428 	data = devm_kcalloc(bgp->dev, cnt, sizeof(*data), GFP_KERNEL);
429 	if (!data) {
430 		ret = -ENOMEM;
431 		goto err_alloc;
432 	}
433 
434 	ref_table = kzalloc(sizeof(*ref_table) * TABLE_SIZE, GFP_KERNEL);
435 	if (!ref_table) {
436 		ret = -ENOMEM;
437 		goto err_alloc;
438 	}
439 
440 	derived_table = devm_kzalloc(bgp->dev, sizeof(*derived_table) * TABLE_SIZE,
441 				     GFP_KERNEL);
442 	if (!derived_table) {
443 		ret = -ENOMEM;
444 		goto err_free_ref_table;
445 	}
446 
447 	if (!workaround_needed)
448 		init_table(5, ref_table, golden_factors);
449 	else
450 		init_table(3, ref_table, pvt_wa_factors);
451 
452 	/* Register the thermal sensors */
453 	for (id = 0; id < cnt; id++) {
454 		data[id].bgp = bgp;
455 		data[id].ctrl_offset = K3_VTM_TMPSENS0_CTRL_OFFSET + id * 0x20;
456 		data[id].stat_offset = data[id].ctrl_offset +
457 					K3_VTM_TMPSENS_STAT_OFFSET;
458 
459 		if (workaround_needed) {
460 			/* ref adc values for -40C, 30C & 125C respectively */
461 			err_vals.refs[0] = MINUS40CREF;
462 			err_vals.refs[1] = PLUS30CREF;
463 			err_vals.refs[2] = PLUS125CREF;
464 			err_vals.refs[3] = PLUS150CREF;
465 			get_efuse_values(id, &data[id], err_vals.errs, fuse_base);
466 		}
467 
468 		if (id == 0 && workaround_needed)
469 			prep_lookup_table(&err_vals, ref_table);
470 		else if (id == 0 && !workaround_needed)
471 			memcpy(derived_table, ref_table, TABLE_SIZE * 4);
472 
473 		val = readl(data[id].bgp->cfg2_base + data[id].ctrl_offset);
474 		val |= (K3_VTM_TMPSENS_CTRL_MAXT_OUTRG_EN |
475 			K3_VTM_TMPSENS_CTRL_SOC |
476 			K3_VTM_TMPSENS_CTRL_CLRZ | BIT(4));
477 		writel(val, data[id].bgp->cfg2_base + data[id].ctrl_offset);
478 
479 		bgp->ts_data[id] = &data[id];
480 		ti_thermal = devm_thermal_of_zone_register(bgp->dev, id, &data[id],
481 							   &k3_of_thermal_ops);
482 		if (IS_ERR(ti_thermal)) {
483 			dev_err(bgp->dev, "thermal zone device is NULL\n");
484 			ret = PTR_ERR(ti_thermal);
485 			goto err_free_ref_table;
486 		}
487 	}
488 
489 	/*
490 	 * Program TSHUT thresholds
491 	 * Step 1: set the thresholds to ~123C and 105C WKUP_VTM_MISC_CTRL2
492 	 * Step 2: WKUP_VTM_TMPSENS_CTRL_j set the MAXT_OUTRG_EN  bit
493 	 *         This is already taken care as per of init
494 	 * Step 3: WKUP_VTM_MISC_CTRL set the ANYMAXT_OUTRG_ALERT_EN  bit
495 	 */
496 	high_max = k3_j72xx_bandgap_temp_to_adc_code(MAX_TEMP);
497 	low_temp = k3_j72xx_bandgap_temp_to_adc_code(COOL_DOWN_TEMP);
498 
499 	writel((low_temp << 16) | high_max, data[0].bgp->cfg2_base +
500 	       K3_VTM_MISC_CTRL2_OFFSET);
501 	mdelay(100);
502 	writel(K3_VTM_ANYMAXT_OUTRG_ALERT_EN, data[0].bgp->cfg2_base +
503 	       K3_VTM_MISC_CTRL_OFFSET);
504 
505 	print_look_up_table(dev, ref_table);
506 	/*
507 	 * Now that the derived_table has the appropriate look up values
508 	 * Free up the ref_table
509 	 */
510 	kfree(ref_table);
511 
512 	return 0;
513 
514 err_free_ref_table:
515 	kfree(ref_table);
516 
517 err_alloc:
518 	pm_runtime_put_sync(&pdev->dev);
519 	pm_runtime_disable(&pdev->dev);
520 
521 	return ret;
522 }
523 
524 static int k3_j72xx_bandgap_remove(struct platform_device *pdev)
525 {
526 	pm_runtime_put_sync(&pdev->dev);
527 	pm_runtime_disable(&pdev->dev);
528 
529 	return 0;
530 }
531 
532 static const struct k3_j72xx_bandgap_data k3_j72xx_bandgap_j721e_data = {
533 	.has_errata_i2128 = true,
534 };
535 
536 static const struct k3_j72xx_bandgap_data k3_j72xx_bandgap_j7200_data = {
537 	.has_errata_i2128 = false,
538 };
539 
540 static const struct of_device_id of_k3_j72xx_bandgap_match[] = {
541 	{
542 		.compatible = "ti,j721e-vtm",
543 		.data = &k3_j72xx_bandgap_j721e_data,
544 	},
545 	{
546 		.compatible = "ti,j7200-vtm",
547 		.data = &k3_j72xx_bandgap_j7200_data,
548 	},
549 	{ /* sentinel */ },
550 };
551 MODULE_DEVICE_TABLE(of, of_k3_j72xx_bandgap_match);
552 
553 static struct platform_driver k3_j72xx_bandgap_sensor_driver = {
554 	.probe = k3_j72xx_bandgap_probe,
555 	.remove = k3_j72xx_bandgap_remove,
556 	.driver = {
557 		.name = "k3-j72xx-soc-thermal",
558 		.of_match_table	= of_k3_j72xx_bandgap_match,
559 	},
560 };
561 
562 module_platform_driver(k3_j72xx_bandgap_sensor_driver);
563 
564 MODULE_DESCRIPTION("K3 bandgap temperature sensor driver");
565 MODULE_LICENSE("GPL");
566 MODULE_AUTHOR("J Keerthy <j-keerthy@ti.com>");
567