st_thermal.c 7.0 KB

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