xref: /linux/drivers/thermal/k3_bandgap.c (revision 621cde16e49b3ecf7d59a8106a20aaebfb4a59a9)
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/platform_device.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 };
82 
83 /* common data structures */
84 struct k3_thermal_data {
85 	struct thermal_zone_device *tzd;
86 	struct k3_bandgap *bgp;
87 	int sensor_id;
88 	u32 ctrl_offset;
89 	u32 stat_offset;
90 };
91 
vtm_get_best_value(unsigned int s0,unsigned int s1,unsigned int s2)92 static unsigned int vtm_get_best_value(unsigned int s0, unsigned int s1,
93 				       unsigned int s2)
94 {
95 	int d01 = abs(s0 - s1);
96 	int d02 = abs(s0 - s2);
97 	int d12 = abs(s1 - s2);
98 
99 	if (d01 <= d02 && d01 <= d12)
100 		return (s0 + s1) / 2;
101 
102 	if (d02 <= d01 && d02 <= d12)
103 		return (s0 + s2) / 2;
104 
105 	return (s1 + s2) / 2;
106 }
107 
k3_bgp_read_temp(struct k3_thermal_data * devdata,int * temp)108 static int k3_bgp_read_temp(struct k3_thermal_data *devdata,
109 			    int *temp)
110 {
111 	struct k3_bandgap *bgp;
112 	unsigned int dtemp, s0, s1, s2;
113 
114 	bgp = devdata->bgp;
115 
116 	/*
117 	 * Errata is applicable for am654 pg 1.0 silicon. There
118 	 * is a variation of the order for 8-10 degree centigrade.
119 	 * Work around that by getting the average of two closest
120 	 * readings out of three readings everytime we want to
121 	 * report temperatures.
122 	 *
123 	 * Errata workaround.
124 	 */
125 	s0 = readl(bgp->base + devdata->stat_offset) &
126 		K3_VTM_TS_STAT_DTEMP_MASK;
127 	s1 = readl(bgp->base + devdata->stat_offset) &
128 		K3_VTM_TS_STAT_DTEMP_MASK;
129 	s2 = readl(bgp->base + devdata->stat_offset) &
130 		K3_VTM_TS_STAT_DTEMP_MASK;
131 	dtemp = vtm_get_best_value(s0, s1, s2);
132 
133 	if (dtemp < K3_VTM_ADC_BEGIN_VAL || dtemp > K3_VTM_ADC_END_VAL)
134 		return -EINVAL;
135 
136 	*temp = k3_adc_to_temp[dtemp - K3_VTM_ADC_BEGIN_VAL];
137 
138 	return 0;
139 }
140 
k3_thermal_get_temp(struct thermal_zone_device * tz,int * temp)141 static int k3_thermal_get_temp(struct thermal_zone_device *tz, int *temp)
142 {
143 	struct k3_thermal_data *data = thermal_zone_device_priv(tz);
144 	int ret = 0;
145 
146 	ret = k3_bgp_read_temp(data, temp);
147 	if (ret)
148 		return ret;
149 
150 	return ret;
151 }
152 
153 static const struct thermal_zone_device_ops k3_of_thermal_ops = {
154 	.get_temp = k3_thermal_get_temp,
155 };
156 
157 static const struct of_device_id of_k3_bandgap_match[];
158 
k3_bandgap_probe(struct platform_device * pdev)159 static int k3_bandgap_probe(struct platform_device *pdev)
160 {
161 	int ret = 0, cnt, val, id;
162 	struct resource *res;
163 	struct device *dev = &pdev->dev;
164 	struct k3_bandgap *bgp;
165 	struct k3_thermal_data *data;
166 
167 	if (ARRAY_SIZE(k3_adc_to_temp) != (K3_VTM_ADC_END_VAL + 1 -
168 						K3_VTM_ADC_BEGIN_VAL))
169 		return -EINVAL;
170 
171 	bgp = devm_kzalloc(&pdev->dev, sizeof(*bgp), GFP_KERNEL);
172 	if (!bgp)
173 		return -ENOMEM;
174 
175 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
176 	bgp->base = devm_ioremap_resource(dev, res);
177 	if (IS_ERR(bgp->base))
178 		return PTR_ERR(bgp->base);
179 
180 	pm_runtime_enable(dev);
181 	ret = pm_runtime_get_sync(dev);
182 	if (ret < 0) {
183 		pm_runtime_put_noidle(dev);
184 		pm_runtime_disable(dev);
185 		return ret;
186 	}
187 
188 	/* Get the sensor count in the VTM */
189 	val = readl(bgp->base + K3_VTM_DEVINFO_PWR0_OFFSET);
190 	cnt = val & K3_VTM_DEVINFO_PWR0_TEMPSENS_CT_MASK;
191 	cnt >>= __ffs(K3_VTM_DEVINFO_PWR0_TEMPSENS_CT_MASK);
192 
193 	data = devm_kcalloc(dev, cnt, sizeof(*data), GFP_KERNEL);
194 	if (!data) {
195 		ret = -ENOMEM;
196 		goto err_alloc;
197 	}
198 
199 	/* Register the thermal sensors */
200 	for (id = 0; id < cnt; id++) {
201 		data[id].sensor_id = id;
202 		data[id].bgp = bgp;
203 		data[id].ctrl_offset = K3_VTM_TMPSENS0_CTRL_OFFSET +
204 					id * K3_VTM_REGS_PER_TS;
205 		data[id].stat_offset = data[id].ctrl_offset + 0x8;
206 
207 		val = readl(data[id].bgp->base + data[id].ctrl_offset);
208 		val |= (K3_VTM_TMPSENS_CTRL_SOC |
209 			K3_VTM_TMPSENS_CTRL_CLRZ |
210 			K3_VTM_TMPSENS_CTRL_CLKON_REQ);
211 		val &= ~K3_VTM_TMPSENS_CTRL_CBIASSEL;
212 		writel(val, data[id].bgp->base + data[id].ctrl_offset);
213 
214 		data[id].tzd =
215 		devm_thermal_of_zone_register(dev, id,
216 					      &data[id],
217 					      &k3_of_thermal_ops);
218 		if (IS_ERR(data[id].tzd)) {
219 			dev_err(dev, "thermal zone device is NULL\n");
220 			ret = PTR_ERR(data[id].tzd);
221 			goto err_alloc;
222 		}
223 
224 		devm_thermal_add_hwmon_sysfs(dev, data[id].tzd);
225 	}
226 
227 
228 	return 0;
229 
230 err_alloc:
231 	pm_runtime_put_sync(dev);
232 	pm_runtime_disable(dev);
233 
234 	return ret;
235 }
236 
k3_bandgap_remove(struct platform_device * pdev)237 static void k3_bandgap_remove(struct platform_device *pdev)
238 {
239 	pm_runtime_put_sync(&pdev->dev);
240 	pm_runtime_disable(&pdev->dev);
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_new = 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