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