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