1 // SPDX-License-Identifier: GPL-2.0+
3 * Amlogic Thermal Sensor Driver
5 * Copyright (C) 2017 Huan Biao <huan.biao@amlogic.com>
6 * Copyright (C) 2019 Guillaume La Roque <glaroque@baylibre.com>
8 * Register value to celsius temperature formulas:
10 * U = ---------, Uptat = ---------
13 * Temperature = A * ( Uptat + u_efuse / 2^16 )- B
15 * A B m n : calibration parameters
16 * u_efuse : fused calibration value, it's a signed 16 bits value
19 #include <linux/bitfield.h>
20 #include <linux/clk.h>
22 #include <linux/mfd/syscon.h>
23 #include <linux/module.h>
25 #include <linux/platform_device.h>
26 #include <linux/regmap.h>
27 #include <linux/thermal.h>
29 #include "thermal_hwmon.h"
31 #define TSENSOR_CFG_REG1 0x4
32 #define TSENSOR_CFG_REG1_RSET_VBG BIT(12)
33 #define TSENSOR_CFG_REG1_RSET_ADC BIT(11)
34 #define TSENSOR_CFG_REG1_VCM_EN BIT(10)
35 #define TSENSOR_CFG_REG1_VBG_EN BIT(9)
36 #define TSENSOR_CFG_REG1_OUT_CTL BIT(6)
37 #define TSENSOR_CFG_REG1_FILTER_EN BIT(5)
38 #define TSENSOR_CFG_REG1_DEM_EN BIT(3)
39 #define TSENSOR_CFG_REG1_CH_SEL GENMASK(1, 0)
40 #define TSENSOR_CFG_REG1_ENABLE \
41 (TSENSOR_CFG_REG1_FILTER_EN | \
42 TSENSOR_CFG_REG1_VCM_EN | \
43 TSENSOR_CFG_REG1_VBG_EN | \
44 TSENSOR_CFG_REG1_DEM_EN | \
45 TSENSOR_CFG_REG1_CH_SEL)
47 #define TSENSOR_STAT0 0x40
49 #define TSENSOR_STAT9 0x64
51 #define TSENSOR_READ_TEMP_MASK GENMASK(15, 0)
52 #define TSENSOR_TEMP_MASK GENMASK(11, 0)
54 #define TSENSOR_TRIM_SIGN_MASK BIT(15)
55 #define TSENSOR_TRIM_TEMP_MASK GENMASK(14, 0)
56 #define TSENSOR_TRIM_VERSION_MASK GENMASK(31, 24)
58 #define TSENSOR_TRIM_VERSION(_version) \
59 FIELD_GET(TSENSOR_TRIM_VERSION_MASK, _version)
61 #define TSENSOR_TRIM_CALIB_VALID_MASK (GENMASK(3, 2) | BIT(7))
63 #define TSENSOR_CALIB_OFFSET 1
64 #define TSENSOR_CALIB_SHIFT 4
67 * struct amlogic_thermal_soc_calib_data
68 * @A: calibration parameters
69 * @B: calibration parameters
70 * @m: calibration parameters
71 * @n: calibration parameters
73 * This structure is required for configuration of amlogic thermal driver.
75 struct amlogic_thermal_soc_calib_data {
83 * struct amlogic_thermal_data
84 * @u_efuse_off: register offset to read fused calibration value
85 * @calibration_parameters: calibration parameters structure pointer
86 * @regmap_config: regmap config for the device
87 * This structure is required for configuration of amlogic thermal driver.
89 struct amlogic_thermal_data {
91 const struct amlogic_thermal_soc_calib_data *calibration_parameters;
92 const struct regmap_config *regmap_config;
95 struct amlogic_thermal {
96 struct platform_device *pdev;
97 const struct amlogic_thermal_data *data;
98 struct regmap *regmap;
99 struct regmap *sec_ao_map;
101 struct thermal_zone_device *tzd;
106 * Calculate a temperature value from a temperature code.
107 * The unit of the temperature is degree milliCelsius.
109 static int amlogic_thermal_code_to_millicelsius(struct amlogic_thermal *pdata,
112 const struct amlogic_thermal_soc_calib_data *param =
113 pdata->data->calibration_parameters;
115 s64 factor, Uptat, uefuse;
117 uefuse = pdata->trim_info & TSENSOR_TRIM_SIGN_MASK ?
118 ~(pdata->trim_info & TSENSOR_TRIM_TEMP_MASK) + 1 :
119 (pdata->trim_info & TSENSOR_TRIM_TEMP_MASK);
121 factor = param->n * temp_code;
122 factor = div_s64(factor, 100);
124 Uptat = temp_code * param->m;
125 Uptat = div_s64(Uptat, 100);
126 Uptat = Uptat * BIT(16);
127 Uptat = div_s64(Uptat, BIT(16) + factor);
129 temp = (Uptat + uefuse) * param->A;
130 temp = div_s64(temp, BIT(16));
131 temp = (temp - param->B) * 100;
136 static int amlogic_thermal_initialize(struct amlogic_thermal *pdata)
141 regmap_read(pdata->sec_ao_map, pdata->data->u_efuse_off,
144 ver = TSENSOR_TRIM_VERSION(pdata->trim_info);
146 if ((ver & TSENSOR_TRIM_CALIB_VALID_MASK) == 0) {
148 dev_err(&pdata->pdev->dev,
149 "tsensor thermal calibration not supported: 0x%x!\n",
156 static int amlogic_thermal_enable(struct amlogic_thermal *data)
160 ret = clk_prepare_enable(data->clk);
164 regmap_update_bits(data->regmap, TSENSOR_CFG_REG1,
165 TSENSOR_CFG_REG1_ENABLE, TSENSOR_CFG_REG1_ENABLE);
170 static int amlogic_thermal_disable(struct amlogic_thermal *data)
172 regmap_update_bits(data->regmap, TSENSOR_CFG_REG1,
173 TSENSOR_CFG_REG1_ENABLE, 0);
174 clk_disable_unprepare(data->clk);
179 static int amlogic_thermal_get_temp(struct thermal_zone_device *tz, int *temp)
182 struct amlogic_thermal *pdata = thermal_zone_device_priv(tz);
187 regmap_read(pdata->regmap, TSENSOR_STAT0, &tval);
189 amlogic_thermal_code_to_millicelsius(pdata,
190 tval & TSENSOR_READ_TEMP_MASK);
195 static const struct thermal_zone_device_ops amlogic_thermal_ops = {
196 .get_temp = amlogic_thermal_get_temp,
199 static const struct regmap_config amlogic_thermal_regmap_config_g12a = {
203 .max_register = TSENSOR_STAT9,
206 static const struct amlogic_thermal_soc_calib_data amlogic_thermal_g12a = {
213 static const struct amlogic_thermal_data amlogic_thermal_g12a_cpu_param = {
214 .u_efuse_off = 0x128,
215 .calibration_parameters = &amlogic_thermal_g12a,
216 .regmap_config = &amlogic_thermal_regmap_config_g12a,
219 static const struct amlogic_thermal_data amlogic_thermal_g12a_ddr_param = {
221 .calibration_parameters = &amlogic_thermal_g12a,
222 .regmap_config = &amlogic_thermal_regmap_config_g12a,
225 static const struct of_device_id of_amlogic_thermal_match[] = {
227 .compatible = "amlogic,g12a-ddr-thermal",
228 .data = &amlogic_thermal_g12a_ddr_param,
231 .compatible = "amlogic,g12a-cpu-thermal",
232 .data = &amlogic_thermal_g12a_cpu_param,
236 MODULE_DEVICE_TABLE(of, of_amlogic_thermal_match);
238 static int amlogic_thermal_probe(struct platform_device *pdev)
240 struct amlogic_thermal *pdata;
241 struct device *dev = &pdev->dev;
245 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
249 pdata->data = of_device_get_match_data(dev);
251 platform_set_drvdata(pdev, pdata);
253 base = devm_platform_ioremap_resource(pdev, 0);
255 return PTR_ERR(base);
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);
262 pdata->clk = devm_clk_get(dev, NULL);
263 if (IS_ERR(pdata->clk))
264 return dev_err_probe(dev, PTR_ERR(pdata->clk), "failed to get clock\n");
266 pdata->sec_ao_map = syscon_regmap_lookup_by_phandle
267 (pdev->dev.of_node, "amlogic,ao-secure");
268 if (IS_ERR(pdata->sec_ao_map)) {
269 dev_err(dev, "syscon regmap lookup failed.\n");
270 return PTR_ERR(pdata->sec_ao_map);
273 pdata->tzd = devm_thermal_of_zone_register(&pdev->dev,
276 &amlogic_thermal_ops);
277 if (IS_ERR(pdata->tzd)) {
278 ret = PTR_ERR(pdata->tzd);
279 dev_err(dev, "Failed to register tsensor: %d\n", ret);
283 devm_thermal_add_hwmon_sysfs(&pdev->dev, pdata->tzd);
285 ret = amlogic_thermal_initialize(pdata);
289 ret = amlogic_thermal_enable(pdata);
294 static int amlogic_thermal_remove(struct platform_device *pdev)
296 struct amlogic_thermal *data = platform_get_drvdata(pdev);
298 return amlogic_thermal_disable(data);
301 static int __maybe_unused amlogic_thermal_suspend(struct device *dev)
303 struct amlogic_thermal *data = dev_get_drvdata(dev);
305 return amlogic_thermal_disable(data);
308 static int __maybe_unused amlogic_thermal_resume(struct device *dev)
310 struct amlogic_thermal *data = dev_get_drvdata(dev);
312 return amlogic_thermal_enable(data);
315 static SIMPLE_DEV_PM_OPS(amlogic_thermal_pm_ops,
316 amlogic_thermal_suspend, amlogic_thermal_resume);
318 static struct platform_driver amlogic_thermal_driver = {
320 .name = "amlogic_thermal",
321 .pm = &amlogic_thermal_pm_ops,
322 .of_match_table = of_amlogic_thermal_match,
324 .probe = amlogic_thermal_probe,
325 .remove = amlogic_thermal_remove,
328 module_platform_driver(amlogic_thermal_driver);
330 MODULE_AUTHOR("Guillaume La Roque <glaroque@baylibre.com>");
331 MODULE_DESCRIPTION("Amlogic thermal driver");
332 MODULE_LICENSE("GPL v2");