1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Amlogic Thermal Sensor Driver
4  *
5  * Copyright (C) 2017 Huan Biao <huan.biao@amlogic.com>
6  * Copyright (C) 2019 Guillaume La Roque <glaroque@baylibre.com>
7  *
8  * Register value to celsius temperature formulas:
9  *	Read_Val	    m * U
10  * U = ---------, Uptat = ---------
11  *	2^16		  1 + n * U
12  *
13  * Temperature = A * ( Uptat + u_efuse / 2^16 )- B
14  *
15  *  A B m n : calibration parameters
16  *  u_efuse : fused calibration value, it's a signed 16 bits value
17  */
18 
19 #include <linux/bitfield.h>
20 #include <linux/clk.h>
21 #include <linux/io.h>
22 #include <linux/mfd/syscon.h>
23 #include <linux/module.h>
24 #include <linux/of.h>
25 #include <linux/of_address.h>
26 #include <linux/of_device.h>
27 #include <linux/platform_device.h>
28 #include <linux/regmap.h>
29 #include <linux/thermal.h>
30 
31 #include "thermal_core.h"
32 
33 #define TSENSOR_CFG_REG1			0x4
34 	#define TSENSOR_CFG_REG1_RSET_VBG	BIT(12)
35 	#define TSENSOR_CFG_REG1_RSET_ADC	BIT(11)
36 	#define TSENSOR_CFG_REG1_VCM_EN		BIT(10)
37 	#define TSENSOR_CFG_REG1_VBG_EN		BIT(9)
38 	#define TSENSOR_CFG_REG1_OUT_CTL	BIT(6)
39 	#define TSENSOR_CFG_REG1_FILTER_EN	BIT(5)
40 	#define TSENSOR_CFG_REG1_DEM_EN		BIT(3)
41 	#define TSENSOR_CFG_REG1_CH_SEL		GENMASK(1, 0)
42 	#define TSENSOR_CFG_REG1_ENABLE		\
43 		(TSENSOR_CFG_REG1_FILTER_EN |	\
44 		 TSENSOR_CFG_REG1_VCM_EN |	\
45 		 TSENSOR_CFG_REG1_VBG_EN |	\
46 		 TSENSOR_CFG_REG1_DEM_EN |	\
47 		 TSENSOR_CFG_REG1_CH_SEL)
48 
49 #define TSENSOR_STAT0			0x40
50 
51 #define TSENSOR_STAT9			0x64
52 
53 #define TSENSOR_READ_TEMP_MASK		GENMASK(15, 0)
54 #define TSENSOR_TEMP_MASK		GENMASK(11, 0)
55 
56 #define TSENSOR_TRIM_SIGN_MASK		BIT(15)
57 #define TSENSOR_TRIM_TEMP_MASK		GENMASK(14, 0)
58 #define TSENSOR_TRIM_VERSION_MASK	GENMASK(31, 24)
59 
60 #define TSENSOR_TRIM_VERSION(_version)	\
61 	FIELD_GET(TSENSOR_TRIM_VERSION_MASK, _version)
62 
63 #define TSENSOR_TRIM_CALIB_VALID_MASK	(GENMASK(3, 2) | BIT(7))
64 
65 #define TSENSOR_CALIB_OFFSET	1
66 #define TSENSOR_CALIB_SHIFT	4
67 
68 /**
69  * struct amlogic_thermal_soc_calib_data
70  * @A, B, m, n: calibration parameters
71  * This structure is required for configuration of amlogic thermal driver.
72  */
73 struct amlogic_thermal_soc_calib_data {
74 	int A;
75 	int B;
76 	int m;
77 	int n;
78 };
79 
80 /**
81  * struct amlogic_thermal_data
82  * @u_efuse_off: register offset to read fused calibration value
83  * @calibration_parameters: calibration parameters structure pointer
84  * @regmap_config: regmap config for the device
85  * This structure is required for configuration of amlogic thermal driver.
86  */
87 struct amlogic_thermal_data {
88 	int u_efuse_off;
89 	const struct amlogic_thermal_soc_calib_data *calibration_parameters;
90 	const struct regmap_config *regmap_config;
91 };
92 
93 struct amlogic_thermal {
94 	struct platform_device *pdev;
95 	const struct amlogic_thermal_data *data;
96 	struct regmap *regmap;
97 	struct regmap *sec_ao_map;
98 	struct clk *clk;
99 	struct thermal_zone_device *tzd;
100 	u32 trim_info;
101 };
102 
103 /*
104  * Calculate a temperature value from a temperature code.
105  * The unit of the temperature is degree milliCelsius.
106  */
107 static int amlogic_thermal_code_to_millicelsius(struct amlogic_thermal *pdata,
108 						int temp_code)
109 {
110 	const struct amlogic_thermal_soc_calib_data *param =
111 					pdata->data->calibration_parameters;
112 	int temp;
113 	s64 factor, Uptat, uefuse;
114 
115 	uefuse = pdata->trim_info & TSENSOR_TRIM_SIGN_MASK ?
116 			     ~(pdata->trim_info & TSENSOR_TRIM_TEMP_MASK) + 1 :
117 			     (pdata->trim_info & TSENSOR_TRIM_TEMP_MASK);
118 
119 	factor = param->n * temp_code;
120 	factor = div_s64(factor, 100);
121 
122 	Uptat = temp_code * param->m;
123 	Uptat = div_s64(Uptat, 100);
124 	Uptat = Uptat * BIT(16);
125 	Uptat = div_s64(Uptat, BIT(16) + factor);
126 
127 	temp = (Uptat + uefuse) * param->A;
128 	temp = div_s64(temp, BIT(16));
129 	temp = (temp - param->B) * 100;
130 
131 	return temp;
132 }
133 
134 static int amlogic_thermal_initialize(struct amlogic_thermal *pdata)
135 {
136 	int ret = 0;
137 	int ver;
138 
139 	regmap_read(pdata->sec_ao_map, pdata->data->u_efuse_off,
140 		    &pdata->trim_info);
141 
142 	ver = TSENSOR_TRIM_VERSION(pdata->trim_info);
143 
144 	if ((ver & TSENSOR_TRIM_CALIB_VALID_MASK) == 0) {
145 		ret = -EINVAL;
146 		dev_err(&pdata->pdev->dev,
147 			"tsensor thermal calibration not supported: 0x%x!\n",
148 			ver);
149 	}
150 
151 	return ret;
152 }
153 
154 static int amlogic_thermal_enable(struct amlogic_thermal *data)
155 {
156 	int ret;
157 
158 	ret = clk_prepare_enable(data->clk);
159 	if (ret)
160 		return ret;
161 
162 	regmap_update_bits(data->regmap, TSENSOR_CFG_REG1,
163 			   TSENSOR_CFG_REG1_ENABLE, TSENSOR_CFG_REG1_ENABLE);
164 
165 	return 0;
166 }
167 
168 static int amlogic_thermal_disable(struct amlogic_thermal *data)
169 {
170 	regmap_update_bits(data->regmap, TSENSOR_CFG_REG1,
171 			   TSENSOR_CFG_REG1_ENABLE, 0);
172 	clk_disable_unprepare(data->clk);
173 
174 	return 0;
175 }
176 
177 static int amlogic_thermal_get_temp(void *data, int *temp)
178 {
179 	unsigned int tval;
180 	struct amlogic_thermal *pdata = data;
181 
182 	if (!data)
183 		return -EINVAL;
184 
185 	regmap_read(pdata->regmap, TSENSOR_STAT0, &tval);
186 	*temp =
187 	   amlogic_thermal_code_to_millicelsius(pdata,
188 						tval & TSENSOR_READ_TEMP_MASK);
189 
190 	return 0;
191 }
192 
193 static const struct thermal_zone_of_device_ops amlogic_thermal_ops = {
194 	.get_temp	= amlogic_thermal_get_temp,
195 };
196 
197 static const struct regmap_config amlogic_thermal_regmap_config_g12a = {
198 	.reg_bits = 8,
199 	.val_bits = 32,
200 	.reg_stride = 4,
201 	.max_register = TSENSOR_STAT9,
202 };
203 
204 static const struct amlogic_thermal_soc_calib_data amlogic_thermal_g12a = {
205 	.A = 9411,
206 	.B = 3159,
207 	.m = 424,
208 	.n = 324,
209 };
210 
211 static const struct amlogic_thermal_data amlogic_thermal_g12a_cpu_param = {
212 	.u_efuse_off = 0x128,
213 	.calibration_parameters = &amlogic_thermal_g12a,
214 	.regmap_config = &amlogic_thermal_regmap_config_g12a,
215 };
216 
217 static const struct amlogic_thermal_data amlogic_thermal_g12a_ddr_param = {
218 	.u_efuse_off = 0xf0,
219 	.calibration_parameters = &amlogic_thermal_g12a,
220 	.regmap_config = &amlogic_thermal_regmap_config_g12a,
221 };
222 
223 static const struct of_device_id of_amlogic_thermal_match[] = {
224 	{
225 		.compatible = "amlogic,g12a-ddr-thermal",
226 		.data = &amlogic_thermal_g12a_ddr_param,
227 	},
228 	{
229 		.compatible = "amlogic,g12a-cpu-thermal",
230 		.data = &amlogic_thermal_g12a_cpu_param,
231 	},
232 	{ /* sentinel */ }
233 };
234 MODULE_DEVICE_TABLE(of, of_amlogic_thermal_match);
235 
236 static int amlogic_thermal_probe(struct platform_device *pdev)
237 {
238 	struct amlogic_thermal *pdata;
239 	struct device *dev = &pdev->dev;
240 	void __iomem *base;
241 	int ret;
242 
243 	pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
244 	if (!pdata)
245 		return -ENOMEM;
246 
247 	pdata->data = of_device_get_match_data(dev);
248 	pdata->pdev = pdev;
249 	platform_set_drvdata(pdev, pdata);
250 
251 	base = devm_platform_ioremap_resource(pdev, 0);
252 	if (IS_ERR(base)) {
253 		dev_err(dev, "failed to get io address\n");
254 		return PTR_ERR(base);
255 	}
256 
257 	pdata->regmap = devm_regmap_init_mmio(dev, base,
258 					      pdata->data->regmap_config);
259 	if (IS_ERR(pdata->regmap))
260 		return PTR_ERR(pdata->regmap);
261 
262 	pdata->clk = devm_clk_get(dev, NULL);
263 	if (IS_ERR(pdata->clk)) {
264 		if (PTR_ERR(pdata->clk) != -EPROBE_DEFER)
265 			dev_err(dev, "failed to get clock\n");
266 		return PTR_ERR(pdata->clk);
267 	}
268 
269 	pdata->sec_ao_map = syscon_regmap_lookup_by_phandle
270 		(pdev->dev.of_node, "amlogic,ao-secure");
271 	if (IS_ERR(pdata->sec_ao_map)) {
272 		dev_err(dev, "syscon regmap lookup failed.\n");
273 		return PTR_ERR(pdata->sec_ao_map);
274 	}
275 
276 	pdata->tzd = devm_thermal_zone_of_sensor_register(&pdev->dev,
277 							  0,
278 							  pdata,
279 							  &amlogic_thermal_ops);
280 	if (IS_ERR(pdata->tzd)) {
281 		ret = PTR_ERR(pdata->tzd);
282 		dev_err(dev, "Failed to register tsensor: %d\n", ret);
283 		return ret;
284 	}
285 
286 	ret = amlogic_thermal_initialize(pdata);
287 	if (ret)
288 		return ret;
289 
290 	ret = amlogic_thermal_enable(pdata);
291 
292 	return ret;
293 }
294 
295 static int amlogic_thermal_remove(struct platform_device *pdev)
296 {
297 	struct amlogic_thermal *data = platform_get_drvdata(pdev);
298 
299 	return amlogic_thermal_disable(data);
300 }
301 
302 static int __maybe_unused amlogic_thermal_suspend(struct device *dev)
303 {
304 	struct amlogic_thermal *data = dev_get_drvdata(dev);
305 
306 	return amlogic_thermal_disable(data);
307 }
308 
309 static int __maybe_unused amlogic_thermal_resume(struct device *dev)
310 {
311 	struct amlogic_thermal *data = dev_get_drvdata(dev);
312 
313 	return amlogic_thermal_enable(data);
314 }
315 
316 static SIMPLE_DEV_PM_OPS(amlogic_thermal_pm_ops,
317 			 amlogic_thermal_suspend, amlogic_thermal_resume);
318 
319 static struct platform_driver amlogic_thermal_driver = {
320 	.driver = {
321 		.name		= "amlogic_thermal",
322 		.pm		= &amlogic_thermal_pm_ops,
323 		.of_match_table = of_amlogic_thermal_match,
324 	},
325 	.probe	= amlogic_thermal_probe,
326 	.remove	= amlogic_thermal_remove,
327 };
328 
329 module_platform_driver(amlogic_thermal_driver);
330 
331 MODULE_AUTHOR("Guillaume La Roque <glaroque@baylibre.com>");
332 MODULE_DESCRIPTION("Amlogic thermal driver");
333 MODULE_LICENSE("GPL v2");
334