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