st_thermal.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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, int *temperature)
  93. {
  94. struct st_thermal_sensor *sensor = th->devdata;
  95. struct device *dev = sensor->dev;
  96. unsigned int temp;
  97. unsigned int overflow;
  98. int ret;
  99. ret = regmap_field_read(sensor->overflow, &overflow);
  100. if (ret)
  101. return ret;
  102. if (overflow)
  103. return -EIO;
  104. ret = regmap_field_read(sensor->temp_data, &temp);
  105. if (ret)
  106. return ret;
  107. temp += sensor->cdata->temp_adjust_val;
  108. temp = mcelsius(temp);
  109. dev_dbg(dev, "temperature: %d\n", temp);
  110. *temperature = temp;
  111. return 0;
  112. }
  113. static int st_thermal_get_trip_type(struct thermal_zone_device *th,
  114. int trip, enum thermal_trip_type *type)
  115. {
  116. struct st_thermal_sensor *sensor = th->devdata;
  117. struct device *dev = sensor->dev;
  118. switch (trip) {
  119. case 0:
  120. *type = THERMAL_TRIP_CRITICAL;
  121. break;
  122. default:
  123. dev_err(dev, "invalid trip point\n");
  124. return -EINVAL;
  125. }
  126. return 0;
  127. }
  128. static int st_thermal_get_trip_temp(struct thermal_zone_device *th,
  129. int trip, int *temp)
  130. {
  131. struct st_thermal_sensor *sensor = th->devdata;
  132. struct device *dev = sensor->dev;
  133. switch (trip) {
  134. case 0:
  135. *temp = mcelsius(sensor->cdata->crit_temp);
  136. break;
  137. default:
  138. dev_err(dev, "Invalid trip point\n");
  139. return -EINVAL;
  140. }
  141. return 0;
  142. }
  143. static struct thermal_zone_device_ops st_tz_ops = {
  144. .get_temp = st_thermal_get_temp,
  145. .get_trip_type = st_thermal_get_trip_type,
  146. .get_trip_temp = st_thermal_get_trip_temp,
  147. };
  148. int st_thermal_register(struct platform_device *pdev,
  149. const struct of_device_id *st_thermal_of_match)
  150. {
  151. struct st_thermal_sensor *sensor;
  152. struct device *dev = &pdev->dev;
  153. struct device_node *np = dev->of_node;
  154. const struct of_device_id *match;
  155. int polling_delay;
  156. int ret;
  157. if (!np) {
  158. dev_err(dev, "device tree node not found\n");
  159. return -EINVAL;
  160. }
  161. sensor = devm_kzalloc(dev, sizeof(*sensor), GFP_KERNEL);
  162. if (!sensor)
  163. return -ENOMEM;
  164. sensor->dev = dev;
  165. match = of_match_device(st_thermal_of_match, dev);
  166. if (!(match && match->data))
  167. return -EINVAL;
  168. sensor->cdata = match->data;
  169. if (!sensor->cdata->ops)
  170. return -EINVAL;
  171. sensor->ops = sensor->cdata->ops;
  172. ret = (sensor->ops->regmap_init)(sensor);
  173. if (ret)
  174. return ret;
  175. ret = st_thermal_alloc_regfields(sensor);
  176. if (ret)
  177. return ret;
  178. sensor->clk = devm_clk_get(dev, "thermal");
  179. if (IS_ERR(sensor->clk)) {
  180. dev_err(dev, "failed to fetch clock\n");
  181. return PTR_ERR(sensor->clk);
  182. }
  183. if (sensor->ops->register_enable_irq) {
  184. ret = sensor->ops->register_enable_irq(sensor);
  185. if (ret)
  186. return ret;
  187. }
  188. ret = st_thermal_sensor_on(sensor);
  189. if (ret)
  190. return ret;
  191. ret = st_thermal_calibration(sensor);
  192. if (ret)
  193. goto sensor_off;
  194. polling_delay = sensor->ops->register_enable_irq ? 0 : 1000;
  195. sensor->thermal_dev =
  196. thermal_zone_device_register(dev_name(dev), 1, 0, sensor,
  197. &st_tz_ops, NULL, 0, polling_delay);
  198. if (IS_ERR(sensor->thermal_dev)) {
  199. dev_err(dev, "failed to register thermal zone device\n");
  200. ret = PTR_ERR(sensor->thermal_dev);
  201. goto sensor_off;
  202. }
  203. platform_set_drvdata(pdev, sensor);
  204. return 0;
  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 platform_device *pdev = to_platform_device(dev);
  222. struct st_thermal_sensor *sensor = platform_get_drvdata(pdev);
  223. return st_thermal_sensor_off(sensor);
  224. }
  225. static int st_thermal_resume(struct device *dev)
  226. {
  227. int ret;
  228. struct platform_device *pdev = to_platform_device(dev);
  229. struct st_thermal_sensor *sensor = platform_get_drvdata(pdev);
  230. ret = st_thermal_sensor_on(sensor);
  231. if (ret)
  232. return ret;
  233. ret = st_thermal_calibration(sensor);
  234. if (ret)
  235. return ret;
  236. if (sensor->ops->enable_irq) {
  237. ret = sensor->ops->enable_irq(sensor);
  238. if (ret)
  239. return ret;
  240. }
  241. return 0;
  242. }
  243. #endif
  244. SIMPLE_DEV_PM_OPS(st_thermal_pm_ops, st_thermal_suspend, st_thermal_resume);
  245. EXPORT_SYMBOL_GPL(st_thermal_pm_ops);
  246. MODULE_AUTHOR("STMicroelectronics (R&D) Limited <ajitpal.singh@st.com>");
  247. MODULE_DESCRIPTION("STMicroelectronics STi SoC Thermal Sensor Driver");
  248. MODULE_LICENSE("GPL v2");