st_thermal.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * ST Thermal Sensor Driver core routines
  4. * Author: Ajit Pal Singh <[email protected]>
  5. *
  6. * Copyright (C) 2003-2014 STMicroelectronics (R&D) Limited
  7. */
  8. #include <linux/clk.h>
  9. #include <linux/module.h>
  10. #include <linux/of.h>
  11. #include <linux/of_device.h>
  12. #include "st_thermal.h"
  13. /* The Thermal Framework expects millidegrees */
  14. #define mcelsius(temp) ((temp) * 1000)
  15. /*
  16. * Function to allocate regfields which are common
  17. * between syscfg and memory mapped based sensors
  18. */
  19. static int st_thermal_alloc_regfields(struct st_thermal_sensor *sensor)
  20. {
  21. struct device *dev = sensor->dev;
  22. struct regmap *regmap = sensor->regmap;
  23. const struct reg_field *reg_fields = sensor->cdata->reg_fields;
  24. sensor->dcorrect = devm_regmap_field_alloc(dev, regmap,
  25. reg_fields[DCORRECT]);
  26. sensor->overflow = devm_regmap_field_alloc(dev, regmap,
  27. reg_fields[OVERFLOW]);
  28. sensor->temp_data = devm_regmap_field_alloc(dev, regmap,
  29. reg_fields[DATA]);
  30. if (IS_ERR(sensor->dcorrect) ||
  31. IS_ERR(sensor->overflow) ||
  32. IS_ERR(sensor->temp_data)) {
  33. dev_err(dev, "failed to allocate common regfields\n");
  34. return -EINVAL;
  35. }
  36. return sensor->ops->alloc_regfields(sensor);
  37. }
  38. static int st_thermal_sensor_on(struct st_thermal_sensor *sensor)
  39. {
  40. int ret;
  41. struct device *dev = sensor->dev;
  42. ret = clk_prepare_enable(sensor->clk);
  43. if (ret) {
  44. dev_err(dev, "failed to enable clk\n");
  45. return ret;
  46. }
  47. ret = sensor->ops->power_ctrl(sensor, POWER_ON);
  48. if (ret) {
  49. dev_err(dev, "failed to power on sensor\n");
  50. clk_disable_unprepare(sensor->clk);
  51. }
  52. return ret;
  53. }
  54. static int st_thermal_sensor_off(struct st_thermal_sensor *sensor)
  55. {
  56. int ret;
  57. ret = sensor->ops->power_ctrl(sensor, POWER_OFF);
  58. if (ret)
  59. return ret;
  60. clk_disable_unprepare(sensor->clk);
  61. return 0;
  62. }
  63. static int st_thermal_calibration(struct st_thermal_sensor *sensor)
  64. {
  65. int ret;
  66. unsigned int val;
  67. struct device *dev = sensor->dev;
  68. /* Check if sensor calibration data is already written */
  69. ret = regmap_field_read(sensor->dcorrect, &val);
  70. if (ret) {
  71. dev_err(dev, "failed to read calibration data\n");
  72. return ret;
  73. }
  74. if (!val) {
  75. /*
  76. * Sensor calibration value not set by bootloader,
  77. * default calibration data to be used
  78. */
  79. ret = regmap_field_write(sensor->dcorrect,
  80. sensor->cdata->calibration_val);
  81. if (ret)
  82. dev_err(dev, "failed to set calibration data\n");
  83. }
  84. return ret;
  85. }
  86. /* Callback to get temperature from HW*/
  87. static int st_thermal_get_temp(struct thermal_zone_device *th, int *temperature)
  88. {
  89. struct st_thermal_sensor *sensor = th->devdata;
  90. struct device *dev = sensor->dev;
  91. unsigned int temp;
  92. unsigned int overflow;
  93. int ret;
  94. ret = regmap_field_read(sensor->overflow, &overflow);
  95. if (ret)
  96. return ret;
  97. if (overflow)
  98. return -EIO;
  99. ret = regmap_field_read(sensor->temp_data, &temp);
  100. if (ret)
  101. return ret;
  102. temp += sensor->cdata->temp_adjust_val;
  103. temp = mcelsius(temp);
  104. dev_dbg(dev, "temperature: %d\n", temp);
  105. *temperature = temp;
  106. return 0;
  107. }
  108. static int st_thermal_get_trip_type(struct thermal_zone_device *th,
  109. int trip, enum thermal_trip_type *type)
  110. {
  111. struct st_thermal_sensor *sensor = th->devdata;
  112. struct device *dev = sensor->dev;
  113. switch (trip) {
  114. case 0:
  115. *type = THERMAL_TRIP_CRITICAL;
  116. break;
  117. default:
  118. dev_err(dev, "invalid trip point\n");
  119. return -EINVAL;
  120. }
  121. return 0;
  122. }
  123. static int st_thermal_get_trip_temp(struct thermal_zone_device *th,
  124. int trip, int *temp)
  125. {
  126. struct st_thermal_sensor *sensor = th->devdata;
  127. struct device *dev = sensor->dev;
  128. switch (trip) {
  129. case 0:
  130. *temp = mcelsius(sensor->cdata->crit_temp);
  131. break;
  132. default:
  133. dev_err(dev, "Invalid trip point\n");
  134. return -EINVAL;
  135. }
  136. return 0;
  137. }
  138. static struct thermal_zone_device_ops st_tz_ops = {
  139. .get_temp = st_thermal_get_temp,
  140. .get_trip_type = st_thermal_get_trip_type,
  141. .get_trip_temp = st_thermal_get_trip_temp,
  142. };
  143. int st_thermal_register(struct platform_device *pdev,
  144. const struct of_device_id *st_thermal_of_match)
  145. {
  146. struct st_thermal_sensor *sensor;
  147. struct device *dev = &pdev->dev;
  148. struct device_node *np = dev->of_node;
  149. const struct of_device_id *match;
  150. int polling_delay;
  151. int ret;
  152. if (!np) {
  153. dev_err(dev, "device tree node not found\n");
  154. return -EINVAL;
  155. }
  156. sensor = devm_kzalloc(dev, sizeof(*sensor), GFP_KERNEL);
  157. if (!sensor)
  158. return -ENOMEM;
  159. sensor->dev = dev;
  160. match = of_match_device(st_thermal_of_match, dev);
  161. if (!(match && match->data))
  162. return -EINVAL;
  163. sensor->cdata = match->data;
  164. if (!sensor->cdata->ops)
  165. return -EINVAL;
  166. sensor->ops = sensor->cdata->ops;
  167. ret = (sensor->ops->regmap_init)(sensor);
  168. if (ret)
  169. return ret;
  170. ret = st_thermal_alloc_regfields(sensor);
  171. if (ret)
  172. return ret;
  173. sensor->clk = devm_clk_get(dev, "thermal");
  174. if (IS_ERR(sensor->clk)) {
  175. dev_err(dev, "failed to fetch clock\n");
  176. return PTR_ERR(sensor->clk);
  177. }
  178. if (sensor->ops->register_enable_irq) {
  179. ret = sensor->ops->register_enable_irq(sensor);
  180. if (ret)
  181. return ret;
  182. }
  183. ret = st_thermal_sensor_on(sensor);
  184. if (ret)
  185. return ret;
  186. ret = st_thermal_calibration(sensor);
  187. if (ret)
  188. goto sensor_off;
  189. polling_delay = sensor->ops->register_enable_irq ? 0 : 1000;
  190. sensor->thermal_dev =
  191. thermal_zone_device_register(dev_name(dev), 1, 0, sensor,
  192. &st_tz_ops, NULL, 0, polling_delay);
  193. if (IS_ERR(sensor->thermal_dev)) {
  194. dev_err(dev, "failed to register thermal zone device\n");
  195. ret = PTR_ERR(sensor->thermal_dev);
  196. goto sensor_off;
  197. }
  198. ret = thermal_zone_device_enable(sensor->thermal_dev);
  199. if (ret)
  200. goto tzd_unregister;
  201. platform_set_drvdata(pdev, sensor);
  202. return 0;
  203. tzd_unregister:
  204. thermal_zone_device_unregister(sensor->thermal_dev);
  205. sensor_off:
  206. st_thermal_sensor_off(sensor);
  207. return ret;
  208. }
  209. EXPORT_SYMBOL_GPL(st_thermal_register);
  210. int st_thermal_unregister(struct platform_device *pdev)
  211. {
  212. struct st_thermal_sensor *sensor = platform_get_drvdata(pdev);
  213. st_thermal_sensor_off(sensor);
  214. thermal_zone_device_unregister(sensor->thermal_dev);
  215. return 0;
  216. }
  217. EXPORT_SYMBOL_GPL(st_thermal_unregister);
  218. #ifdef CONFIG_PM_SLEEP
  219. static int st_thermal_suspend(struct device *dev)
  220. {
  221. struct st_thermal_sensor *sensor = dev_get_drvdata(dev);
  222. return st_thermal_sensor_off(sensor);
  223. }
  224. static int st_thermal_resume(struct device *dev)
  225. {
  226. int ret;
  227. struct st_thermal_sensor *sensor = dev_get_drvdata(dev);
  228. ret = st_thermal_sensor_on(sensor);
  229. if (ret)
  230. return ret;
  231. ret = st_thermal_calibration(sensor);
  232. if (ret)
  233. return ret;
  234. if (sensor->ops->enable_irq) {
  235. ret = sensor->ops->enable_irq(sensor);
  236. if (ret)
  237. return ret;
  238. }
  239. return 0;
  240. }
  241. #endif
  242. SIMPLE_DEV_PM_OPS(st_thermal_pm_ops, st_thermal_suspend, st_thermal_resume);
  243. EXPORT_SYMBOL_GPL(st_thermal_pm_ops);
  244. MODULE_AUTHOR("STMicroelectronics (R&D) Limited <[email protected]>");
  245. MODULE_DESCRIPTION("STMicroelectronics STi SoC Thermal Sensor Driver");
  246. MODULE_LICENSE("GPL v2");