amlogic_thermal.c 8.6 KB

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