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 #include <linux/firmware/meson/meson_sm.h> 29 30 #include "thermal_hwmon.h" 31 32 #define TSENSOR_CFG_REG1 0x4 33 #define TSENSOR_CFG_REG1_RSET_VBG BIT(12) 34 #define TSENSOR_CFG_REG1_RSET_ADC BIT(11) 35 #define TSENSOR_CFG_REG1_VCM_EN BIT(10) 36 #define TSENSOR_CFG_REG1_VBG_EN BIT(9) 37 #define TSENSOR_CFG_REG1_OUT_CTL BIT(6) 38 #define TSENSOR_CFG_REG1_FILTER_EN BIT(5) 39 #define TSENSOR_CFG_REG1_DEM_EN BIT(3) 40 #define TSENSOR_CFG_REG1_CH_SEL GENMASK(1, 0) 41 #define TSENSOR_CFG_REG1_ENABLE \ 42 (TSENSOR_CFG_REG1_FILTER_EN | \ 43 TSENSOR_CFG_REG1_VCM_EN | \ 44 TSENSOR_CFG_REG1_VBG_EN | \ 45 TSENSOR_CFG_REG1_DEM_EN | \ 46 TSENSOR_CFG_REG1_CH_SEL) 47 48 #define TSENSOR_STAT0 0x40 49 50 #define TSENSOR_STAT9 0x64 51 52 #define TSENSOR_READ_TEMP_MASK GENMASK(15, 0) 53 #define TSENSOR_TEMP_MASK GENMASK(11, 0) 54 55 #define TSENSOR_TRIM_SIGN_MASK BIT(15) 56 #define TSENSOR_TRIM_TEMP_MASK GENMASK(14, 0) 57 #define TSENSOR_TRIM_VERSION_MASK GENMASK(31, 24) 58 59 #define TSENSOR_TRIM_VERSION(_version) \ 60 FIELD_GET(TSENSOR_TRIM_VERSION_MASK, _version) 61 62 #define TSENSOR_TRIM_CALIB_VALID_MASK (GENMASK(3, 2) | BIT(7)) 63 64 #define TSENSOR_CALIB_OFFSET 1 65 #define TSENSOR_CALIB_SHIFT 4 66 67 /** 68 * struct amlogic_thermal_soc_calib_data 69 * @A: calibration parameters 70 * @B: calibration parameters 71 * @m: calibration parameters 72 * @n: calibration parameters 73 * 74 * This structure is required for configuration of amlogic thermal driver. 75 */ 76 struct amlogic_thermal_soc_calib_data { 77 int A; 78 int B; 79 int m; 80 int n; 81 }; 82 83 /** 84 * struct amlogic_thermal_data 85 * @u_efuse_off: register offset to read fused calibration value 86 * @calibration_parameters: calibration parameters structure pointer 87 * @regmap_config: regmap config for the device 88 * @use_sm: read data from secure monitor instead of efuse 89 * This structure is required for configuration of amlogic thermal driver. 90 */ 91 struct 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 bool use_sm; 96 }; 97 98 struct amlogic_thermal { 99 struct platform_device *pdev; 100 const struct amlogic_thermal_data *data; 101 struct regmap *regmap; 102 struct regmap *sec_ao_map; 103 struct clk *clk; 104 struct thermal_zone_device *tzd; 105 u32 trim_info; 106 struct meson_sm_firmware *sm_fw; 107 u32 tsensor_id; 108 }; 109 110 /* 111 * Calculate a temperature value from a temperature code. 112 * The unit of the temperature is degree milliCelsius. 113 */ 114 static int amlogic_thermal_code_to_millicelsius(struct amlogic_thermal *pdata, 115 int temp_code) 116 { 117 const struct amlogic_thermal_soc_calib_data *param = 118 pdata->data->calibration_parameters; 119 int temp; 120 s64 factor, uptat, uefuse; 121 122 uefuse = pdata->trim_info & TSENSOR_TRIM_SIGN_MASK ? 123 ~(pdata->trim_info & TSENSOR_TRIM_TEMP_MASK) + 1 : 124 (pdata->trim_info & TSENSOR_TRIM_TEMP_MASK); 125 126 factor = param->n * temp_code; 127 factor = div_s64(factor, 100); 128 129 uptat = temp_code * param->m; 130 uptat = div_s64(uptat, 100); 131 uptat = uptat * BIT(16); 132 uptat = div_s64(uptat, BIT(16) + factor); 133 134 temp = (uptat + uefuse) * param->A; 135 temp = div_s64(temp, BIT(16)); 136 temp = (temp - param->B) * 100; 137 138 return temp; 139 } 140 141 static int amlogic_thermal_enable(struct amlogic_thermal *data) 142 { 143 int ret; 144 145 ret = clk_prepare_enable(data->clk); 146 if (ret) 147 return ret; 148 149 regmap_update_bits(data->regmap, TSENSOR_CFG_REG1, 150 TSENSOR_CFG_REG1_ENABLE, TSENSOR_CFG_REG1_ENABLE); 151 152 return 0; 153 } 154 155 static void amlogic_thermal_disable(struct amlogic_thermal *data) 156 { 157 regmap_update_bits(data->regmap, TSENSOR_CFG_REG1, 158 TSENSOR_CFG_REG1_ENABLE, 0); 159 clk_disable_unprepare(data->clk); 160 } 161 162 static int amlogic_thermal_get_temp(struct thermal_zone_device *tz, int *temp) 163 { 164 unsigned int tval; 165 struct amlogic_thermal *pdata = thermal_zone_device_priv(tz); 166 167 if (!pdata) 168 return -EINVAL; 169 170 regmap_read(pdata->regmap, TSENSOR_STAT0, &tval); 171 *temp = 172 amlogic_thermal_code_to_millicelsius(pdata, 173 tval & TSENSOR_READ_TEMP_MASK); 174 175 return 0; 176 } 177 178 static int amlogic_thermal_probe_sm(struct platform_device *pdev, 179 struct amlogic_thermal *pdata) 180 { 181 struct device *dev = &pdev->dev; 182 struct of_phandle_args ph_args; 183 int ret; 184 185 ret = of_parse_phandle_with_fixed_args(pdev->dev.of_node, 186 "amlogic,secure-monitor", 187 1, 0, &ph_args); 188 if (ret) 189 return ret; 190 191 if (!ph_args.np) { 192 dev_err(dev, "Failed to parse secure monitor phandle\n"); 193 return -ENODEV; 194 } 195 196 pdata->sm_fw = meson_sm_get(ph_args.np); 197 of_node_put(ph_args.np); 198 if (!pdata->sm_fw) { 199 dev_err(dev, "Failed to get secure monitor firmware\n"); 200 return -EPROBE_DEFER; 201 } 202 203 pdata->tsensor_id = ph_args.args[0]; 204 205 return meson_sm_get_thermal_calib(pdata->sm_fw, 206 &pdata->trim_info, 207 pdata->tsensor_id); 208 } 209 210 static int amlogic_thermal_probe_syscon(struct platform_device *pdev, 211 struct amlogic_thermal *pdata) 212 { 213 struct device *dev = &pdev->dev; 214 int ver; 215 216 pdata->sec_ao_map = 217 syscon_regmap_lookup_by_phandle(pdev->dev.of_node, 218 "amlogic,ao-secure"); 219 if (IS_ERR(pdata->sec_ao_map)) { 220 dev_err(dev, "syscon regmap lookup failed.\n"); 221 return PTR_ERR(pdata->sec_ao_map); 222 } 223 224 regmap_read(pdata->sec_ao_map, pdata->data->u_efuse_off, 225 &pdata->trim_info); 226 227 ver = TSENSOR_TRIM_VERSION(pdata->trim_info); 228 229 if ((ver & TSENSOR_TRIM_CALIB_VALID_MASK) == 0) { 230 dev_err(&pdata->pdev->dev, 231 "tsensor thermal calibration not supported: 0x%x!\n", 232 ver); 233 return -EINVAL; 234 } 235 236 return 0; 237 } 238 239 static const struct thermal_zone_device_ops amlogic_thermal_ops = { 240 .get_temp = amlogic_thermal_get_temp, 241 }; 242 243 static const struct regmap_config amlogic_thermal_regmap_config_g12a = { 244 .reg_bits = 8, 245 .val_bits = 32, 246 .reg_stride = 4, 247 .max_register = TSENSOR_STAT9, 248 }; 249 250 static const struct amlogic_thermal_soc_calib_data amlogic_thermal_g12a = { 251 .A = 9411, 252 .B = 3159, 253 .m = 424, 254 .n = 324, 255 }; 256 257 static const struct amlogic_thermal_data amlogic_thermal_g12a_cpu_param = { 258 .u_efuse_off = 0x128, 259 .calibration_parameters = &amlogic_thermal_g12a, 260 .regmap_config = &amlogic_thermal_regmap_config_g12a, 261 }; 262 263 static const struct amlogic_thermal_data amlogic_thermal_g12a_ddr_param = { 264 .u_efuse_off = 0xf0, 265 .calibration_parameters = &amlogic_thermal_g12a, 266 .regmap_config = &amlogic_thermal_regmap_config_g12a, 267 }; 268 269 static const struct amlogic_thermal_data amlogic_thermal_a1_cpu_param = { 270 .u_efuse_off = 0x114, 271 .calibration_parameters = &amlogic_thermal_g12a, 272 .regmap_config = &amlogic_thermal_regmap_config_g12a, 273 }; 274 275 static const struct amlogic_thermal_data amlogic_thermal_t7_param = { 276 .use_sm = true, 277 .calibration_parameters = &amlogic_thermal_g12a, 278 .regmap_config = &amlogic_thermal_regmap_config_g12a, 279 }; 280 281 static const struct of_device_id of_amlogic_thermal_match[] = { 282 { 283 .compatible = "amlogic,g12a-ddr-thermal", 284 .data = &amlogic_thermal_g12a_ddr_param, 285 }, 286 { 287 .compatible = "amlogic,g12a-cpu-thermal", 288 .data = &amlogic_thermal_g12a_cpu_param, 289 }, 290 { 291 .compatible = "amlogic,a1-cpu-thermal", 292 .data = &amlogic_thermal_a1_cpu_param, 293 }, 294 { 295 .compatible = "amlogic,t7-thermal", 296 .data = &amlogic_thermal_t7_param, 297 }, 298 { /* sentinel */ } 299 }; 300 MODULE_DEVICE_TABLE(of, of_amlogic_thermal_match); 301 302 static int amlogic_thermal_probe(struct platform_device *pdev) 303 { 304 struct amlogic_thermal *pdata; 305 struct device *dev = &pdev->dev; 306 void __iomem *base; 307 int ret; 308 309 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL); 310 if (!pdata) 311 return -ENOMEM; 312 313 pdata->data = of_device_get_match_data(dev); 314 pdata->pdev = pdev; 315 platform_set_drvdata(pdev, pdata); 316 317 base = devm_platform_ioremap_resource(pdev, 0); 318 if (IS_ERR(base)) 319 return PTR_ERR(base); 320 321 pdata->regmap = devm_regmap_init_mmio(dev, base, 322 pdata->data->regmap_config); 323 if (IS_ERR(pdata->regmap)) 324 return PTR_ERR(pdata->regmap); 325 326 pdata->clk = devm_clk_get(dev, NULL); 327 if (IS_ERR(pdata->clk)) 328 return dev_err_probe(dev, PTR_ERR(pdata->clk), "failed to get clock\n"); 329 330 if (pdata->data->use_sm) 331 ret = amlogic_thermal_probe_sm(pdev, pdata); 332 else 333 ret = amlogic_thermal_probe_syscon(pdev, pdata); 334 if (ret) 335 return ret; 336 337 pdata->tzd = devm_thermal_of_zone_register(&pdev->dev, 338 0, 339 pdata, 340 &amlogic_thermal_ops); 341 if (IS_ERR(pdata->tzd)) { 342 ret = PTR_ERR(pdata->tzd); 343 dev_err(dev, "Failed to register tsensor: %d\n", ret); 344 return ret; 345 } 346 347 devm_thermal_add_hwmon_sysfs(&pdev->dev, pdata->tzd); 348 349 ret = amlogic_thermal_enable(pdata); 350 351 return ret; 352 } 353 354 static void amlogic_thermal_remove(struct platform_device *pdev) 355 { 356 struct amlogic_thermal *data = platform_get_drvdata(pdev); 357 358 amlogic_thermal_disable(data); 359 } 360 361 static int amlogic_thermal_suspend(struct device *dev) 362 { 363 struct amlogic_thermal *data = dev_get_drvdata(dev); 364 365 amlogic_thermal_disable(data); 366 367 return 0; 368 } 369 370 static int amlogic_thermal_resume(struct device *dev) 371 { 372 struct amlogic_thermal *data = dev_get_drvdata(dev); 373 374 return amlogic_thermal_enable(data); 375 } 376 377 static DEFINE_SIMPLE_DEV_PM_OPS(amlogic_thermal_pm_ops, 378 amlogic_thermal_suspend, 379 amlogic_thermal_resume); 380 381 static struct platform_driver amlogic_thermal_driver = { 382 .driver = { 383 .name = "amlogic_thermal", 384 .pm = pm_ptr(&amlogic_thermal_pm_ops), 385 .of_match_table = of_amlogic_thermal_match, 386 }, 387 .probe = amlogic_thermal_probe, 388 .remove = amlogic_thermal_remove, 389 }; 390 391 module_platform_driver(amlogic_thermal_driver); 392 393 MODULE_AUTHOR("Guillaume La Roque <glaroque@baylibre.com>"); 394 MODULE_DESCRIPTION("Amlogic thermal driver"); 395 MODULE_LICENSE("GPL v2"); 396