amlogic_thermal.c 8.5 KB

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