amlogic_thermal.c 8.4 KB

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