xref: /openbmc/linux/drivers/thermal/k3_bandgap.c (revision 7a010c3c)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * TI Bandgap temperature sensor driver for K3 SoC Family
4  *
5  * Copyright (C) 2020 Texas Instruments Incorporated - http://www.ti.com/
6  */
7 
8 #include <linux/err.h>
9 #include <linux/init.h>
10 #include <linux/io.h>
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/of.h>
14 #include <linux/of_platform.h>
15 #include <linux/pm_runtime.h>
16 #include <linux/thermal.h>
17 #include <linux/types.h>
18 
19 #define K3_VTM_DEVINFO_PWR0_OFFSET		0x4
20 #define K3_VTM_DEVINFO_PWR0_TEMPSENS_CT_MASK	0xf0
21 #define K3_VTM_TMPSENS0_CTRL_OFFSET	0x80
22 #define K3_VTM_REGS_PER_TS			0x10
23 #define K3_VTM_TS_STAT_DTEMP_MASK	0x3ff
24 #define K3_VTM_TMPSENS_CTRL_CBIASSEL	BIT(0)
25 #define K3_VTM_TMPSENS_CTRL_SOC		BIT(5)
26 #define K3_VTM_TMPSENS_CTRL_CLRZ		BIT(6)
27 #define K3_VTM_TMPSENS_CTRL_CLKON_REQ	BIT(7)
28 
29 #define K3_VTM_ADC_BEGIN_VAL		540
30 #define K3_VTM_ADC_END_VAL		944
31 
32 static const int k3_adc_to_temp[] = {
33 	-40000, -40000, -40000, -40000, -39800, -39400, -39000, -38600, -38200,
34 	-37800, -37400, -37000, -36600, -36200, -35800, -35300, -34700, -34200,
35 	-33800, -33400, -33000, -32600, -32200, -31800, -31400, -31000, -30600,
36 	-30200, -29800, -29400, -29000, -28600, -28200, -27700, -27100, -26600,
37 	-26200, -25800, -25400, -25000, -24600, -24200, -23800, -23400, -23000,
38 	-22600, -22200, -21800, -21400, -21000, -20500, -19900, -19400, -19000,
39 	-18600, -18200, -17800, -17400, -17000, -16600, -16200, -15800, -15400,
40 	-15000, -14600, -14200, -13800, -13400, -13000, -12500, -11900, -11400,
41 	-11000, -10600, -10200, -9800, -9400, -9000, -8600, -8200, -7800, -7400,
42 	-7000, -6600, -6200, -5800, -5400, -5000, -4500, -3900, -3400, -3000,
43 	-2600, -2200, -1800, -1400, -1000, -600, -200, 200, 600, 1000, 1400,
44 	1800, 2200, 2600, 3000, 3400, 3900, 4500, 5000, 5400, 5800, 6200, 6600,
45 	7000, 7400, 7800, 8200, 8600, 9000, 9400, 9800, 10200, 10600, 11000,
46 	11400, 11800, 12200, 12700, 13300, 13800, 14200, 14600, 15000, 15400,
47 	15800, 16200, 16600, 17000, 17400, 17800, 18200, 18600, 19000, 19400,
48 	19800, 20200, 20600, 21000, 21400, 21900, 22500, 23000, 23400, 23800,
49 	24200, 24600, 25000, 25400, 25800, 26200, 26600, 27000, 27400, 27800,
50 	28200, 28600, 29000, 29400, 29800, 30200, 30600, 31000, 31400, 31900,
51 	32500, 33000, 33400, 33800, 34200, 34600, 35000, 35400, 35800, 36200,
52 	36600, 37000, 37400, 37800, 38200, 38600, 39000, 39400, 39800, 40200,
53 	40600, 41000, 41400, 41800, 42200, 42600, 43100, 43700, 44200, 44600,
54 	45000, 45400, 45800, 46200, 46600, 47000, 47400, 47800, 48200, 48600,
55 	49000, 49400, 49800, 50200, 50600, 51000, 51400, 51800, 52200, 52600,
56 	53000, 53400, 53800, 54200, 54600, 55000, 55400, 55900, 56500, 57000,
57 	57400, 57800, 58200, 58600, 59000, 59400, 59800, 60200, 60600, 61000,
58 	61400, 61800, 62200, 62600, 63000, 63400, 63800, 64200, 64600, 65000,
59 	65400, 65800, 66200, 66600, 67000, 67400, 67800, 68200, 68600, 69000,
60 	69400, 69800, 70200, 70600, 71000, 71500, 72100, 72600, 73000, 73400,
61 	73800, 74200, 74600, 75000, 75400, 75800, 76200, 76600, 77000, 77400,
62 	77800, 78200, 78600, 79000, 79400, 79800, 80200, 80600, 81000, 81400,
63 	81800, 82200, 82600, 83000, 83400, 83800, 84200, 84600, 85000, 85400,
64 	85800, 86200, 86600, 87000, 87400, 87800, 88200, 88600, 89000, 89400,
65 	89800, 90200, 90600, 91000, 91400, 91800, 92200, 92600, 93000, 93400,
66 	93800, 94200, 94600, 95000, 95400, 95800, 96200, 96600, 97000, 97500,
67 	98100, 98600, 99000, 99400, 99800, 100200, 100600, 101000, 101400,
68 	101800, 102200, 102600, 103000, 103400, 103800, 104200, 104600, 105000,
69 	105400, 105800, 106200, 106600, 107000, 107400, 107800, 108200, 108600,
70 	109000, 109400, 109800, 110200, 110600, 111000, 111400, 111800, 112200,
71 	112600, 113000, 113400, 113800, 114200, 114600, 115000, 115400, 115800,
72 	116200, 116600, 117000, 117400, 117800, 118200, 118600, 119000, 119400,
73 	119800, 120200, 120600, 121000, 121400, 121800, 122200, 122600, 123000,
74 	123400, 123800, 124200, 124600, 124900, 125000,
75 };
76 
77 struct k3_bandgap {
78 	void __iomem *base;
79 	const struct k3_bandgap_data *conf;
80 };
81 
82 /* common data structures */
83 struct k3_thermal_data {
84 	struct thermal_zone_device *tzd;
85 	struct k3_bandgap *bgp;
86 	int sensor_id;
87 	u32 ctrl_offset;
88 	u32 stat_offset;
89 };
90 
91 static unsigned int vtm_get_best_value(unsigned int s0, unsigned int s1,
92 				       unsigned int s2)
93 {
94 	int d01 = abs(s0 - s1);
95 	int d02 = abs(s0 - s2);
96 	int d12 = abs(s1 - s2);
97 
98 	if (d01 <= d02 && d01 <= d12)
99 		return (s0 + s1) / 2;
100 
101 	if (d02 <= d01 && d02 <= d12)
102 		return (s0 + s2) / 2;
103 
104 	return (s1 + s2) / 2;
105 }
106 
107 static int k3_bgp_read_temp(struct k3_thermal_data *devdata,
108 			    int *temp)
109 {
110 	struct k3_bandgap *bgp;
111 	unsigned int dtemp, s0, s1, s2;
112 
113 	bgp = devdata->bgp;
114 
115 	/*
116 	 * Errata is applicable for am654 pg 1.0 silicon. There
117 	 * is a variation of the order for 8-10 degree centigrade.
118 	 * Work around that by getting the average of two closest
119 	 * readings out of three readings everytime we want to
120 	 * report temperatures.
121 	 *
122 	 * Errata workaround.
123 	 */
124 	s0 = readl(bgp->base + devdata->stat_offset) &
125 		K3_VTM_TS_STAT_DTEMP_MASK;
126 	s1 = readl(bgp->base + devdata->stat_offset) &
127 		K3_VTM_TS_STAT_DTEMP_MASK;
128 	s2 = readl(bgp->base + devdata->stat_offset) &
129 		K3_VTM_TS_STAT_DTEMP_MASK;
130 	dtemp = vtm_get_best_value(s0, s1, s2);
131 
132 	if (dtemp < K3_VTM_ADC_BEGIN_VAL || dtemp > K3_VTM_ADC_END_VAL)
133 		return -EINVAL;
134 
135 	*temp = k3_adc_to_temp[dtemp - K3_VTM_ADC_BEGIN_VAL];
136 
137 	return 0;
138 }
139 
140 static int k3_thermal_get_temp(void *devdata, int *temp)
141 {
142 	struct k3_thermal_data *data = devdata;
143 	int ret = 0;
144 
145 	ret = k3_bgp_read_temp(data, temp);
146 	if (ret)
147 		return ret;
148 
149 	return ret;
150 }
151 
152 static const struct thermal_zone_of_device_ops k3_of_thermal_ops = {
153 	.get_temp = k3_thermal_get_temp,
154 };
155 
156 static const struct of_device_id of_k3_bandgap_match[];
157 
158 static int k3_bandgap_probe(struct platform_device *pdev)
159 {
160 	int ret = 0, cnt, val, id;
161 	struct resource *res;
162 	struct device *dev = &pdev->dev;
163 	struct k3_bandgap *bgp;
164 	struct k3_thermal_data *data;
165 
166 	if (ARRAY_SIZE(k3_adc_to_temp) != (K3_VTM_ADC_END_VAL + 1 -
167 						K3_VTM_ADC_BEGIN_VAL))
168 		return -EINVAL;
169 
170 	bgp = devm_kzalloc(&pdev->dev, sizeof(*bgp), GFP_KERNEL);
171 	if (!bgp)
172 		return -ENOMEM;
173 
174 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
175 	bgp->base = devm_ioremap_resource(dev, res);
176 	if (IS_ERR(bgp->base))
177 		return PTR_ERR(bgp->base);
178 
179 	pm_runtime_enable(dev);
180 	ret = pm_runtime_get_sync(dev);
181 	if (ret < 0) {
182 		pm_runtime_put_noidle(dev);
183 		pm_runtime_disable(dev);
184 		return ret;
185 	}
186 
187 	/* Get the sensor count in the VTM */
188 	val = readl(bgp->base + K3_VTM_DEVINFO_PWR0_OFFSET);
189 	cnt = val & K3_VTM_DEVINFO_PWR0_TEMPSENS_CT_MASK;
190 	cnt >>= __ffs(K3_VTM_DEVINFO_PWR0_TEMPSENS_CT_MASK);
191 
192 	data = devm_kcalloc(dev, cnt, sizeof(*data), GFP_KERNEL);
193 	if (!data) {
194 		ret = -ENOMEM;
195 		goto err_alloc;
196 	}
197 
198 	/* Register the thermal sensors */
199 	for (id = 0; id < cnt; id++) {
200 		data[id].sensor_id = id;
201 		data[id].bgp = bgp;
202 		data[id].ctrl_offset = K3_VTM_TMPSENS0_CTRL_OFFSET +
203 					id * K3_VTM_REGS_PER_TS;
204 		data[id].stat_offset = data[id].ctrl_offset + 0x8;
205 
206 		val = readl(data[id].bgp->base + data[id].ctrl_offset);
207 		val |= (K3_VTM_TMPSENS_CTRL_SOC |
208 			K3_VTM_TMPSENS_CTRL_CLRZ |
209 			K3_VTM_TMPSENS_CTRL_CLKON_REQ);
210 		val &= ~K3_VTM_TMPSENS_CTRL_CBIASSEL;
211 		writel(val, data[id].bgp->base + data[id].ctrl_offset);
212 
213 		data[id].tzd =
214 		devm_thermal_zone_of_sensor_register(dev, id,
215 						     &data[id],
216 						     &k3_of_thermal_ops);
217 		if (IS_ERR(data[id].tzd)) {
218 			dev_err(dev, "thermal zone device is NULL\n");
219 			ret = PTR_ERR(data[id].tzd);
220 			goto err_alloc;
221 		}
222 	}
223 
224 	platform_set_drvdata(pdev, bgp);
225 
226 	return 0;
227 
228 err_alloc:
229 	pm_runtime_put_sync(dev);
230 	pm_runtime_disable(dev);
231 
232 	return ret;
233 }
234 
235 static int k3_bandgap_remove(struct platform_device *pdev)
236 {
237 	pm_runtime_put_sync(&pdev->dev);
238 	pm_runtime_disable(&pdev->dev);
239 
240 	return 0;
241 }
242 
243 static const struct of_device_id of_k3_bandgap_match[] = {
244 	{
245 		.compatible = "ti,am654-vtm",
246 	},
247 	{ /* sentinel */ },
248 };
249 MODULE_DEVICE_TABLE(of, of_k3_bandgap_match);
250 
251 static struct platform_driver k3_bandgap_sensor_driver = {
252 	.probe = k3_bandgap_probe,
253 	.remove = k3_bandgap_remove,
254 	.driver = {
255 		.name = "k3-soc-thermal",
256 		.of_match_table	= of_k3_bandgap_match,
257 	},
258 };
259 
260 module_platform_driver(k3_bandgap_sensor_driver);
261 
262 MODULE_DESCRIPTION("K3 bandgap temperature sensor driver");
263 MODULE_LICENSE("GPL v2");
264 MODULE_AUTHOR("J Keerthy <j-keerthy@ti.com>");
265