xref: /openbmc/linux/drivers/thermal/k3_j72xx_bandgap.c (revision 99a049aace3257cf7644c3bbf67cb1a655248cc2)
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 		/* case 0 */
155ffcb2fc8SKeerthy 		derived_table[i] = derived_table[i + 1] - 300;
156ffcb2fc8SKeerthy 	}
157ffcb2fc8SKeerthy 
158ffcb2fc8SKeerthy 	/*
159ffcb2fc8SKeerthy 	 * Fill the last trailing 0s which are unfilled with increments of
160ffcb2fc8SKeerthy 	 * 100 milli celsius till 1023 code
161ffcb2fc8SKeerthy 	 */
162ffcb2fc8SKeerthy 	i = TABLE_SIZE - 1;
163ffcb2fc8SKeerthy 	while (!derived_table[i])
164ffcb2fc8SKeerthy 		i--;
165ffcb2fc8SKeerthy 
166ffcb2fc8SKeerthy 	i++;
167ffcb2fc8SKeerthy 	inc = 1;
168ffcb2fc8SKeerthy 	while (i < TABLE_SIZE) {
169ffcb2fc8SKeerthy 		derived_table[i] = derived_table[i - 1] + inc * 100;
170ffcb2fc8SKeerthy 		i++;
171ffcb2fc8SKeerthy 	}
172ffcb2fc8SKeerthy 
173ffcb2fc8SKeerthy 	return 0;
174ffcb2fc8SKeerthy }
175ffcb2fc8SKeerthy 
176ffcb2fc8SKeerthy struct k3_thermal_data;
177ffcb2fc8SKeerthy 
178ffcb2fc8SKeerthy struct k3_j72xx_bandgap {
179ffcb2fc8SKeerthy 	struct device *dev;
180ffcb2fc8SKeerthy 	void __iomem *base;
181ffcb2fc8SKeerthy 	void __iomem *cfg2_base;
182ffcb2fc8SKeerthy 	void __iomem *fuse_base;
183ffcb2fc8SKeerthy 	struct k3_thermal_data *ts_data[K3_VTM_MAX_NUM_TS];
184ffcb2fc8SKeerthy };
185ffcb2fc8SKeerthy 
186ffcb2fc8SKeerthy /* common data structures */
187ffcb2fc8SKeerthy struct k3_thermal_data {
188ffcb2fc8SKeerthy 	struct k3_j72xx_bandgap *bgp;
189ffcb2fc8SKeerthy 	u32 ctrl_offset;
190ffcb2fc8SKeerthy 	u32 stat_offset;
191ffcb2fc8SKeerthy };
192ffcb2fc8SKeerthy 
193ffcb2fc8SKeerthy static int two_cmp(int tmp, int mask)
194ffcb2fc8SKeerthy {
195ffcb2fc8SKeerthy 	tmp = ~(tmp);
196ffcb2fc8SKeerthy 	tmp &= mask;
197ffcb2fc8SKeerthy 	tmp += 1;
198ffcb2fc8SKeerthy 
199ffcb2fc8SKeerthy 	/* Return negative value */
200ffcb2fc8SKeerthy 	return (0 - tmp);
201ffcb2fc8SKeerthy }
202ffcb2fc8SKeerthy 
203ffcb2fc8SKeerthy static unsigned int vtm_get_best_value(unsigned int s0, unsigned int s1,
204ffcb2fc8SKeerthy 				       unsigned int s2)
205ffcb2fc8SKeerthy {
206ffcb2fc8SKeerthy 	int d01 = abs(s0 - s1);
207ffcb2fc8SKeerthy 	int d02 = abs(s0 - s2);
208ffcb2fc8SKeerthy 	int d12 = abs(s1 - s2);
209ffcb2fc8SKeerthy 
210ffcb2fc8SKeerthy 	if (d01 <= d02 && d01 <= d12)
211ffcb2fc8SKeerthy 		return (s0 + s1) / 2;
212ffcb2fc8SKeerthy 
213ffcb2fc8SKeerthy 	if (d02 <= d01 && d02 <= d12)
214ffcb2fc8SKeerthy 		return (s0 + s2) / 2;
215ffcb2fc8SKeerthy 
216ffcb2fc8SKeerthy 	return (s1 + s2) / 2;
217ffcb2fc8SKeerthy }
218ffcb2fc8SKeerthy 
219ffcb2fc8SKeerthy static inline int k3_bgp_read_temp(struct k3_thermal_data *devdata,
220ffcb2fc8SKeerthy 				   int *temp)
221ffcb2fc8SKeerthy {
222ffcb2fc8SKeerthy 	struct k3_j72xx_bandgap *bgp;
223ffcb2fc8SKeerthy 	unsigned int dtemp, s0, s1, s2;
224ffcb2fc8SKeerthy 
225ffcb2fc8SKeerthy 	bgp = devdata->bgp;
226ffcb2fc8SKeerthy 	/*
227ffcb2fc8SKeerthy 	 * Errata is applicable for am654 pg 1.0 silicon/J7ES. There
228ffcb2fc8SKeerthy 	 * is a variation of the order for certain degree centigrade on AM654.
229ffcb2fc8SKeerthy 	 * Work around that by getting the average of two closest
230ffcb2fc8SKeerthy 	 * readings out of three readings everytime we want to
231ffcb2fc8SKeerthy 	 * report temperatures.
232ffcb2fc8SKeerthy 	 *
233ffcb2fc8SKeerthy 	 * Errata workaround.
234ffcb2fc8SKeerthy 	 */
235ffcb2fc8SKeerthy 	s0 = readl(bgp->base + devdata->stat_offset) &
236ffcb2fc8SKeerthy 		K3_VTM_TS_STAT_DTEMP_MASK;
237ffcb2fc8SKeerthy 	s1 = readl(bgp->base + devdata->stat_offset) &
238ffcb2fc8SKeerthy 		K3_VTM_TS_STAT_DTEMP_MASK;
239ffcb2fc8SKeerthy 	s2 = readl(bgp->base + devdata->stat_offset) &
240ffcb2fc8SKeerthy 		K3_VTM_TS_STAT_DTEMP_MASK;
241ffcb2fc8SKeerthy 	dtemp = vtm_get_best_value(s0, s1, s2);
242ffcb2fc8SKeerthy 
243ffcb2fc8SKeerthy 	if (dtemp < 0 || dtemp >= TABLE_SIZE)
244ffcb2fc8SKeerthy 		return -EINVAL;
245ffcb2fc8SKeerthy 
246ffcb2fc8SKeerthy 	*temp = derived_table[dtemp];
247ffcb2fc8SKeerthy 
248ffcb2fc8SKeerthy 	return 0;
249ffcb2fc8SKeerthy }
250ffcb2fc8SKeerthy 
251ffcb2fc8SKeerthy /* Get temperature callback function for thermal zone */
252ffcb2fc8SKeerthy static int k3_thermal_get_temp(void *devdata, int *temp)
253ffcb2fc8SKeerthy {
254ffcb2fc8SKeerthy 	struct k3_thermal_data *data = devdata;
255ffcb2fc8SKeerthy 	int ret = 0;
256ffcb2fc8SKeerthy 
257ffcb2fc8SKeerthy 	ret = k3_bgp_read_temp(data, temp);
258ffcb2fc8SKeerthy 	if (ret)
259ffcb2fc8SKeerthy 		return ret;
260ffcb2fc8SKeerthy 
261ffcb2fc8SKeerthy 	return ret;
262ffcb2fc8SKeerthy }
263ffcb2fc8SKeerthy 
264ffcb2fc8SKeerthy static const struct thermal_zone_of_device_ops k3_of_thermal_ops = {
265ffcb2fc8SKeerthy 	.get_temp = k3_thermal_get_temp,
266ffcb2fc8SKeerthy };
267ffcb2fc8SKeerthy 
268ffcb2fc8SKeerthy static int k3_j72xx_bandgap_temp_to_adc_code(int temp)
269ffcb2fc8SKeerthy {
270ffcb2fc8SKeerthy 	int low = 0, high = TABLE_SIZE - 1, mid;
271ffcb2fc8SKeerthy 
272ffcb2fc8SKeerthy 	if (temp > 160000 || temp < -50000)
273ffcb2fc8SKeerthy 		return -EINVAL;
274ffcb2fc8SKeerthy 
275ffcb2fc8SKeerthy 	/* Binary search to find the adc code */
276ffcb2fc8SKeerthy 	while (low < (high - 1)) {
277ffcb2fc8SKeerthy 		mid = (low + high) / 2;
278ffcb2fc8SKeerthy 		if (temp <= derived_table[mid])
279ffcb2fc8SKeerthy 			high = mid;
280ffcb2fc8SKeerthy 		else
281ffcb2fc8SKeerthy 			low = mid;
282ffcb2fc8SKeerthy 	}
283ffcb2fc8SKeerthy 
284ffcb2fc8SKeerthy 	return mid;
285ffcb2fc8SKeerthy }
286ffcb2fc8SKeerthy 
287ffcb2fc8SKeerthy static void get_efuse_values(int id, struct k3_thermal_data *data, int *err,
288ffcb2fc8SKeerthy 			     struct k3_j72xx_bandgap *bgp)
289ffcb2fc8SKeerthy {
290ffcb2fc8SKeerthy 	int i, tmp, pow;
291ffcb2fc8SKeerthy 	int ct_offsets[5][K3_VTM_CORRECTION_TEMP_CNT] = {
292ffcb2fc8SKeerthy 		{ 0x0, 0x8, 0x4 },
293ffcb2fc8SKeerthy 		{ 0x0, 0x8, 0x4 },
294ffcb2fc8SKeerthy 		{ 0x0, -1,  0x4 },
295ffcb2fc8SKeerthy 		{ 0x0, 0xC, -1 },
296ffcb2fc8SKeerthy 		{ 0x0, 0xc, 0x8 }
297ffcb2fc8SKeerthy 	};
298ffcb2fc8SKeerthy 	int ct_bm[5][K3_VTM_CORRECTION_TEMP_CNT] = {
299ffcb2fc8SKeerthy 		{ 0x3f, 0x1fe000, 0x1ff },
300ffcb2fc8SKeerthy 		{ 0xfc0, 0x1fe000, 0x3fe00 },
301ffcb2fc8SKeerthy 		{ 0x3f000, 0x7f800000, 0x7fc0000 },
302ffcb2fc8SKeerthy 		{ 0xfc0000, 0x1fe0, 0x1f800000 },
303ffcb2fc8SKeerthy 		{ 0x3f000000, 0x1fe000, 0x1ff0 }
304ffcb2fc8SKeerthy 	};
305ffcb2fc8SKeerthy 
306ffcb2fc8SKeerthy 	for (i = 0; i < 3; i++) {
307ffcb2fc8SKeerthy 		/* Extract the offset value using bit-mask */
308ffcb2fc8SKeerthy 		if (ct_offsets[id][i] == -1 && i == 1) {
309ffcb2fc8SKeerthy 			/* 25C offset Case of Sensor 2 split between 2 regs */
310ffcb2fc8SKeerthy 			tmp = (readl(bgp->fuse_base + 0x8) & 0xE0000000) >> (29);
311ffcb2fc8SKeerthy 			tmp |= ((readl(bgp->fuse_base + 0xC) & 0x1F) << 3);
312ffcb2fc8SKeerthy 			pow = tmp & 0x80;
313ffcb2fc8SKeerthy 		} else if (ct_offsets[id][i] == -1 && i == 2) {
314ffcb2fc8SKeerthy 			/* 125C Case of Sensor 3 split between 2 regs */
315ffcb2fc8SKeerthy 			tmp = (readl(bgp->fuse_base + 0x4) & 0xF8000000) >> (27);
316ffcb2fc8SKeerthy 			tmp |= ((readl(bgp->fuse_base + 0x8) & 0xF) << 5);
317ffcb2fc8SKeerthy 			pow = tmp & 0x100;
318ffcb2fc8SKeerthy 		} else {
319ffcb2fc8SKeerthy 			tmp = readl(bgp->fuse_base + ct_offsets[id][i]);
320ffcb2fc8SKeerthy 			tmp &= ct_bm[id][i];
321ffcb2fc8SKeerthy 			tmp = tmp >> __ffs(ct_bm[id][i]);
322ffcb2fc8SKeerthy 
323ffcb2fc8SKeerthy 			/* Obtain the sign bit pow*/
324ffcb2fc8SKeerthy 			pow = ct_bm[id][i] >> __ffs(ct_bm[id][i]);
325ffcb2fc8SKeerthy 			pow += 1;
326ffcb2fc8SKeerthy 			pow /= 2;
327ffcb2fc8SKeerthy 		}
328ffcb2fc8SKeerthy 
329ffcb2fc8SKeerthy 		/* Check for negative value */
330ffcb2fc8SKeerthy 		if (tmp & pow) {
331ffcb2fc8SKeerthy 			/* 2's complement value */
332ffcb2fc8SKeerthy 			tmp = two_cmp(tmp, ct_bm[id][i] >> __ffs(ct_bm[id][i]));
333ffcb2fc8SKeerthy 		}
334ffcb2fc8SKeerthy 		err[i] = tmp;
335ffcb2fc8SKeerthy 	}
336ffcb2fc8SKeerthy 
337ffcb2fc8SKeerthy 	/* Err value for 150C is set to 0 */
338ffcb2fc8SKeerthy 	err[i] = 0;
339ffcb2fc8SKeerthy }
340ffcb2fc8SKeerthy 
341ffcb2fc8SKeerthy static void print_look_up_table(struct device *dev, int *ref_table)
342ffcb2fc8SKeerthy {
343ffcb2fc8SKeerthy 	int i;
344ffcb2fc8SKeerthy 
345ffcb2fc8SKeerthy 	dev_dbg(dev, "The contents of derived array\n");
346ffcb2fc8SKeerthy 	dev_dbg(dev, "Code   Temperature\n");
347ffcb2fc8SKeerthy 	for (i = 0; i < TABLE_SIZE; i++)
348ffcb2fc8SKeerthy 		dev_dbg(dev, "%d       %d %d\n", i, derived_table[i], ref_table[i]);
349ffcb2fc8SKeerthy }
350ffcb2fc8SKeerthy 
351ffcb2fc8SKeerthy struct k3_j72xx_bandgap_data {
352ffcb2fc8SKeerthy 	unsigned int has_errata_i2128;
353ffcb2fc8SKeerthy };
354ffcb2fc8SKeerthy 
355ffcb2fc8SKeerthy static int k3_j72xx_bandgap_probe(struct platform_device *pdev)
356ffcb2fc8SKeerthy {
357ffcb2fc8SKeerthy 	int ret = 0, cnt, val, id;
358ffcb2fc8SKeerthy 	int high_max, low_temp;
359ffcb2fc8SKeerthy 	struct resource *res;
360ffcb2fc8SKeerthy 	struct device *dev = &pdev->dev;
361ffcb2fc8SKeerthy 	struct k3_j72xx_bandgap *bgp;
362ffcb2fc8SKeerthy 	struct k3_thermal_data *data;
363ffcb2fc8SKeerthy 	int workaround_needed = 0;
364ffcb2fc8SKeerthy 	const struct k3_j72xx_bandgap_data *driver_data;
365ffcb2fc8SKeerthy 	struct thermal_zone_device *ti_thermal;
366ffcb2fc8SKeerthy 	int *ref_table;
367ffcb2fc8SKeerthy 	struct err_values err_vals;
368ffcb2fc8SKeerthy 
369ffcb2fc8SKeerthy 	const s64 golden_factors[] = {
370ffcb2fc8SKeerthy 		-490019999999999936,
371ffcb2fc8SKeerthy 		3251200000000000,
372ffcb2fc8SKeerthy 		-1705800000000,
373ffcb2fc8SKeerthy 		603730000,
374ffcb2fc8SKeerthy 		-92627,
375ffcb2fc8SKeerthy 	};
376ffcb2fc8SKeerthy 
377ffcb2fc8SKeerthy 	const s64 pvt_wa_factors[] = {
378ffcb2fc8SKeerthy 		-415230000000000000,
379ffcb2fc8SKeerthy 		3126600000000000,
380ffcb2fc8SKeerthy 		-1157800000000,
381ffcb2fc8SKeerthy 	};
382ffcb2fc8SKeerthy 
383ffcb2fc8SKeerthy 	bgp = devm_kzalloc(&pdev->dev, sizeof(*bgp), GFP_KERNEL);
384ffcb2fc8SKeerthy 	if (!bgp)
385ffcb2fc8SKeerthy 		return -ENOMEM;
386ffcb2fc8SKeerthy 
387ffcb2fc8SKeerthy 	bgp->dev = dev;
388ffcb2fc8SKeerthy 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
389ffcb2fc8SKeerthy 	bgp->base = devm_ioremap_resource(dev, res);
390ffcb2fc8SKeerthy 	if (IS_ERR(bgp->base))
391ffcb2fc8SKeerthy 		return PTR_ERR(bgp->base);
392ffcb2fc8SKeerthy 
393ffcb2fc8SKeerthy 	res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
394ffcb2fc8SKeerthy 	bgp->cfg2_base = devm_ioremap_resource(dev, res);
395ffcb2fc8SKeerthy 	if (IS_ERR(bgp->cfg2_base))
396ffcb2fc8SKeerthy 		return PTR_ERR(bgp->cfg2_base);
397ffcb2fc8SKeerthy 
398ffcb2fc8SKeerthy 	res = platform_get_resource(pdev, IORESOURCE_MEM, 2);
399ffcb2fc8SKeerthy 	bgp->fuse_base = devm_ioremap_resource(dev, res);
400ffcb2fc8SKeerthy 	if (IS_ERR(bgp->fuse_base))
401ffcb2fc8SKeerthy 		return PTR_ERR(bgp->fuse_base);
402ffcb2fc8SKeerthy 
403ffcb2fc8SKeerthy 	driver_data = of_device_get_match_data(dev);
404ffcb2fc8SKeerthy 	if (driver_data)
405ffcb2fc8SKeerthy 		workaround_needed = driver_data->has_errata_i2128;
406ffcb2fc8SKeerthy 
407ffcb2fc8SKeerthy 	pm_runtime_enable(dev);
408ffcb2fc8SKeerthy 	ret = pm_runtime_get_sync(dev);
409ffcb2fc8SKeerthy 	if (ret < 0) {
410ffcb2fc8SKeerthy 		pm_runtime_put_noidle(dev);
411ffcb2fc8SKeerthy 		pm_runtime_disable(dev);
412ffcb2fc8SKeerthy 		return ret;
413ffcb2fc8SKeerthy 	}
414ffcb2fc8SKeerthy 
415ffcb2fc8SKeerthy 	/* Get the sensor count in the VTM */
416ffcb2fc8SKeerthy 	val = readl(bgp->base + K3_VTM_DEVINFO_PWR0_OFFSET);
417ffcb2fc8SKeerthy 	cnt = val & K3_VTM_DEVINFO_PWR0_TEMPSENS_CT_MASK;
418ffcb2fc8SKeerthy 	cnt >>= __ffs(K3_VTM_DEVINFO_PWR0_TEMPSENS_CT_MASK);
419ffcb2fc8SKeerthy 
420ffcb2fc8SKeerthy 	data = devm_kcalloc(bgp->dev, cnt, sizeof(*data), GFP_KERNEL);
421ffcb2fc8SKeerthy 	if (!data) {
422ffcb2fc8SKeerthy 		ret = -ENOMEM;
423ffcb2fc8SKeerthy 		goto err_alloc;
424ffcb2fc8SKeerthy 	}
425ffcb2fc8SKeerthy 
426ffcb2fc8SKeerthy 	ref_table = kzalloc(sizeof(*ref_table) * TABLE_SIZE, GFP_KERNEL);
427ffcb2fc8SKeerthy 	if (!ref_table) {
428ffcb2fc8SKeerthy 		ret = -ENOMEM;
429ffcb2fc8SKeerthy 		goto err_alloc;
430ffcb2fc8SKeerthy 	}
431ffcb2fc8SKeerthy 
432ffcb2fc8SKeerthy 	derived_table = devm_kzalloc(bgp->dev, sizeof(*derived_table) * TABLE_SIZE,
433ffcb2fc8SKeerthy 				     GFP_KERNEL);
434ffcb2fc8SKeerthy 	if (!derived_table) {
435ffcb2fc8SKeerthy 		ret = -ENOMEM;
436*99a049aaSBryan Brattlof 		goto err_free_ref_table;
437ffcb2fc8SKeerthy 	}
438ffcb2fc8SKeerthy 
439ffcb2fc8SKeerthy 	/* Workaround not needed if bit30/bit31 is set even for J721e */
440ffcb2fc8SKeerthy 	if (workaround_needed && (readl(bgp->fuse_base + 0x0) & 0xc0000000) == 0xc0000000)
441ffcb2fc8SKeerthy 		workaround_needed = false;
442ffcb2fc8SKeerthy 
443ffcb2fc8SKeerthy 	dev_dbg(bgp->dev, "Work around %sneeded\n",
444ffcb2fc8SKeerthy 		workaround_needed ? "not " : "");
445ffcb2fc8SKeerthy 
446ffcb2fc8SKeerthy 	if (!workaround_needed)
447ffcb2fc8SKeerthy 		init_table(5, ref_table, golden_factors);
448ffcb2fc8SKeerthy 	else
449ffcb2fc8SKeerthy 		init_table(3, ref_table, pvt_wa_factors);
450ffcb2fc8SKeerthy 
451ffcb2fc8SKeerthy 	/* Register the thermal sensors */
452ffcb2fc8SKeerthy 	for (id = 0; id < cnt; id++) {
453ffcb2fc8SKeerthy 		data[id].bgp = bgp;
454ffcb2fc8SKeerthy 		data[id].ctrl_offset = K3_VTM_TMPSENS0_CTRL_OFFSET + id * 0x20;
455ffcb2fc8SKeerthy 		data[id].stat_offset = data[id].ctrl_offset +
456ffcb2fc8SKeerthy 					K3_VTM_TMPSENS_STAT_OFFSET;
457ffcb2fc8SKeerthy 
458ffcb2fc8SKeerthy 		if (workaround_needed) {
459ffcb2fc8SKeerthy 			/* ref adc values for -40C, 30C & 125C respectively */
460ffcb2fc8SKeerthy 			err_vals.refs[0] = MINUS40CREF;
461ffcb2fc8SKeerthy 			err_vals.refs[1] = PLUS30CREF;
462ffcb2fc8SKeerthy 			err_vals.refs[2] = PLUS125CREF;
463ffcb2fc8SKeerthy 			err_vals.refs[3] = PLUS150CREF;
464ffcb2fc8SKeerthy 			get_efuse_values(id, &data[id], err_vals.errs, bgp);
465ffcb2fc8SKeerthy 		}
466ffcb2fc8SKeerthy 
467ffcb2fc8SKeerthy 		if (id == 0 && workaround_needed)
468ffcb2fc8SKeerthy 			prep_lookup_table(&err_vals, ref_table);
469ffcb2fc8SKeerthy 		else if (id == 0 && !workaround_needed)
470ffcb2fc8SKeerthy 			memcpy(derived_table, ref_table, TABLE_SIZE * 4);
471ffcb2fc8SKeerthy 
472ffcb2fc8SKeerthy 		val = readl(data[id].bgp->cfg2_base + data[id].ctrl_offset);
473ffcb2fc8SKeerthy 		val |= (K3_VTM_TMPSENS_CTRL_MAXT_OUTRG_EN |
474ffcb2fc8SKeerthy 			K3_VTM_TMPSENS_CTRL_SOC |
475ffcb2fc8SKeerthy 			K3_VTM_TMPSENS_CTRL_CLRZ | BIT(4));
476ffcb2fc8SKeerthy 		writel(val, data[id].bgp->cfg2_base + data[id].ctrl_offset);
477ffcb2fc8SKeerthy 
478ffcb2fc8SKeerthy 		bgp->ts_data[id] = &data[id];
479ffcb2fc8SKeerthy 		ti_thermal =
480ffcb2fc8SKeerthy 		devm_thermal_zone_of_sensor_register(bgp->dev, id,
481ffcb2fc8SKeerthy 						     &data[id],
482ffcb2fc8SKeerthy 						     &k3_of_thermal_ops);
483ffcb2fc8SKeerthy 		if (IS_ERR(ti_thermal)) {
484ffcb2fc8SKeerthy 			dev_err(bgp->dev, "thermal zone device is NULL\n");
485ffcb2fc8SKeerthy 			ret = PTR_ERR(ti_thermal);
486*99a049aaSBryan Brattlof 			goto err_free_ref_table;
487ffcb2fc8SKeerthy 		}
488ffcb2fc8SKeerthy 	}
489ffcb2fc8SKeerthy 
490ffcb2fc8SKeerthy 	/*
491ffcb2fc8SKeerthy 	 * Program TSHUT thresholds
492ffcb2fc8SKeerthy 	 * Step 1: set the thresholds to ~123C and 105C WKUP_VTM_MISC_CTRL2
493ffcb2fc8SKeerthy 	 * Step 2: WKUP_VTM_TMPSENS_CTRL_j set the MAXT_OUTRG_EN  bit
494ffcb2fc8SKeerthy 	 *         This is already taken care as per of init
495ffcb2fc8SKeerthy 	 * Step 3: WKUP_VTM_MISC_CTRL set the ANYMAXT_OUTRG_ALERT_EN  bit
496ffcb2fc8SKeerthy 	 */
497ffcb2fc8SKeerthy 	high_max = k3_j72xx_bandgap_temp_to_adc_code(MAX_TEMP);
498ffcb2fc8SKeerthy 	low_temp = k3_j72xx_bandgap_temp_to_adc_code(COOL_DOWN_TEMP);
499ffcb2fc8SKeerthy 
500ffcb2fc8SKeerthy 	writel((low_temp << 16) | high_max, data[0].bgp->cfg2_base +
501ffcb2fc8SKeerthy 	       K3_VTM_MISC_CTRL2_OFFSET);
502ffcb2fc8SKeerthy 	mdelay(100);
503ffcb2fc8SKeerthy 	writel(K3_VTM_ANYMAXT_OUTRG_ALERT_EN, data[0].bgp->cfg2_base +
504ffcb2fc8SKeerthy 	       K3_VTM_MISC_CTRL_OFFSET);
505ffcb2fc8SKeerthy 
506ffcb2fc8SKeerthy 	platform_set_drvdata(pdev, bgp);
507ffcb2fc8SKeerthy 
508ffcb2fc8SKeerthy 	print_look_up_table(dev, ref_table);
509ffcb2fc8SKeerthy 	/*
510ffcb2fc8SKeerthy 	 * Now that the derived_table has the appropriate look up values
511ffcb2fc8SKeerthy 	 * Free up the ref_table
512ffcb2fc8SKeerthy 	 */
513ffcb2fc8SKeerthy 	kfree(ref_table);
514ffcb2fc8SKeerthy 
515ffcb2fc8SKeerthy 	return 0;
516ffcb2fc8SKeerthy 
517*99a049aaSBryan Brattlof err_free_ref_table:
518*99a049aaSBryan Brattlof 	kfree(ref_table);
519*99a049aaSBryan Brattlof 
520ffcb2fc8SKeerthy err_alloc:
521ffcb2fc8SKeerthy 	pm_runtime_put_sync(&pdev->dev);
522ffcb2fc8SKeerthy 	pm_runtime_disable(&pdev->dev);
523ffcb2fc8SKeerthy 
524ffcb2fc8SKeerthy 	return ret;
525ffcb2fc8SKeerthy }
526ffcb2fc8SKeerthy 
527ffcb2fc8SKeerthy static int k3_j72xx_bandgap_remove(struct platform_device *pdev)
528ffcb2fc8SKeerthy {
529ffcb2fc8SKeerthy 	pm_runtime_put_sync(&pdev->dev);
530ffcb2fc8SKeerthy 	pm_runtime_disable(&pdev->dev);
531ffcb2fc8SKeerthy 
532ffcb2fc8SKeerthy 	return 0;
533ffcb2fc8SKeerthy }
534ffcb2fc8SKeerthy 
535ffcb2fc8SKeerthy const struct k3_j72xx_bandgap_data k3_j72xx_bandgap_j721e_data = {
536ffcb2fc8SKeerthy 	.has_errata_i2128 = 1,
537ffcb2fc8SKeerthy };
538ffcb2fc8SKeerthy 
539ffcb2fc8SKeerthy const struct k3_j72xx_bandgap_data k3_j72xx_bandgap_j7200_data = {
540ffcb2fc8SKeerthy 	.has_errata_i2128 = 0,
541ffcb2fc8SKeerthy };
542ffcb2fc8SKeerthy 
543ffcb2fc8SKeerthy static const struct of_device_id of_k3_j72xx_bandgap_match[] = {
544ffcb2fc8SKeerthy 	{
545ffcb2fc8SKeerthy 		.compatible = "ti,j721e-vtm",
546ffcb2fc8SKeerthy 		.data = &k3_j72xx_bandgap_j721e_data,
547ffcb2fc8SKeerthy 	},
548ffcb2fc8SKeerthy 	{
549ffcb2fc8SKeerthy 		.compatible = "ti,j7200-vtm",
550ffcb2fc8SKeerthy 		.data = &k3_j72xx_bandgap_j7200_data,
551ffcb2fc8SKeerthy 	},
552ffcb2fc8SKeerthy 	{ /* sentinel */ },
553ffcb2fc8SKeerthy };
554ffcb2fc8SKeerthy MODULE_DEVICE_TABLE(of, of_k3_j72xx_bandgap_match);
555ffcb2fc8SKeerthy 
556ffcb2fc8SKeerthy static struct platform_driver k3_j72xx_bandgap_sensor_driver = {
557ffcb2fc8SKeerthy 	.probe = k3_j72xx_bandgap_probe,
558ffcb2fc8SKeerthy 	.remove = k3_j72xx_bandgap_remove,
559ffcb2fc8SKeerthy 	.driver = {
560ffcb2fc8SKeerthy 		.name = "k3-j72xx-soc-thermal",
561ffcb2fc8SKeerthy 		.of_match_table	= of_k3_j72xx_bandgap_match,
562ffcb2fc8SKeerthy 	},
563ffcb2fc8SKeerthy };
564ffcb2fc8SKeerthy 
565ffcb2fc8SKeerthy module_platform_driver(k3_j72xx_bandgap_sensor_driver);
566ffcb2fc8SKeerthy 
567ffcb2fc8SKeerthy MODULE_DESCRIPTION("K3 bandgap temperature sensor driver");
568ffcb2fc8SKeerthy MODULE_LICENSE("GPL");
569ffcb2fc8SKeerthy MODULE_AUTHOR("J Keerthy <j-keerthy@ti.com>");
570