zx2967_thermal.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * ZTE's zx2967 family thermal sensor driver
  4. *
  5. * Copyright (C) 2017 ZTE Ltd.
  6. *
  7. * Author: Baoyou Xie <baoyou.xie@linaro.org>
  8. */
  9. #include <linux/clk.h>
  10. #include <linux/device.h>
  11. #include <linux/err.h>
  12. #include <linux/iopoll.h>
  13. #include <linux/module.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/thermal.h>
  16. /* Power Mode: 0->low 1->high */
  17. #define ZX2967_THERMAL_POWER_MODE 0
  18. #define ZX2967_POWER_MODE_LOW 0
  19. #define ZX2967_POWER_MODE_HIGH 1
  20. /* DCF Control Register */
  21. #define ZX2967_THERMAL_DCF 0x4
  22. #define ZX2967_DCF_EN BIT(1)
  23. #define ZX2967_DCF_FREEZE BIT(0)
  24. /* Selection Register */
  25. #define ZX2967_THERMAL_SEL 0x8
  26. /* Control Register */
  27. #define ZX2967_THERMAL_CTRL 0x10
  28. #define ZX2967_THERMAL_READY BIT(12)
  29. #define ZX2967_THERMAL_TEMP_MASK GENMASK(11, 0)
  30. #define ZX2967_THERMAL_ID_MASK 0x18
  31. #define ZX2967_THERMAL_ID 0x10
  32. #define ZX2967_GET_TEMP_TIMEOUT_US (100 * 1024)
  33. /**
  34. * struct zx2967_thermal_priv - zx2967 thermal sensor private structure
  35. * @tzd: struct thermal_zone_device where the sensor is registered
  36. * @lock: prevents read sensor in parallel
  37. * @clk_topcrm: topcrm clk structure
  38. * @clk_apb: apb clk structure
  39. * @regs: pointer to base address of the thermal sensor
  40. * @dev: struct device pointer
  41. */
  42. struct zx2967_thermal_priv {
  43. struct thermal_zone_device *tzd;
  44. struct mutex lock;
  45. struct clk *clk_topcrm;
  46. struct clk *clk_apb;
  47. void __iomem *regs;
  48. struct device *dev;
  49. };
  50. static int zx2967_thermal_get_temp(void *data, int *temp)
  51. {
  52. void __iomem *regs;
  53. struct zx2967_thermal_priv *priv = data;
  54. u32 val;
  55. int ret;
  56. if (!priv->tzd)
  57. return -EAGAIN;
  58. regs = priv->regs;
  59. mutex_lock(&priv->lock);
  60. writel_relaxed(ZX2967_POWER_MODE_LOW,
  61. regs + ZX2967_THERMAL_POWER_MODE);
  62. writel_relaxed(ZX2967_DCF_EN, regs + ZX2967_THERMAL_DCF);
  63. val = readl_relaxed(regs + ZX2967_THERMAL_SEL);
  64. val &= ~ZX2967_THERMAL_ID_MASK;
  65. val |= ZX2967_THERMAL_ID;
  66. writel_relaxed(val, regs + ZX2967_THERMAL_SEL);
  67. /*
  68. * Must wait for a while, surely it's a bit odd.
  69. * otherwise temperature value we got has a few deviation, even if
  70. * the THERMAL_READY bit is set.
  71. */
  72. usleep_range(100, 300);
  73. ret = readx_poll_timeout(readl, regs + ZX2967_THERMAL_CTRL,
  74. val, val & ZX2967_THERMAL_READY, 300,
  75. ZX2967_GET_TEMP_TIMEOUT_US);
  76. if (ret) {
  77. dev_err(priv->dev, "Thermal sensor data timeout\n");
  78. goto unlock;
  79. }
  80. writel_relaxed(ZX2967_DCF_FREEZE | ZX2967_DCF_EN,
  81. regs + ZX2967_THERMAL_DCF);
  82. val = readl_relaxed(regs + ZX2967_THERMAL_CTRL)
  83. & ZX2967_THERMAL_TEMP_MASK;
  84. writel_relaxed(ZX2967_POWER_MODE_HIGH,
  85. regs + ZX2967_THERMAL_POWER_MODE);
  86. /*
  87. * Calculate temperature
  88. * In dts, slope is multiplied by 1000.
  89. */
  90. *temp = DIV_ROUND_CLOSEST(((s32)val + priv->tzd->tzp->offset) * 1000,
  91. priv->tzd->tzp->slope);
  92. unlock:
  93. mutex_unlock(&priv->lock);
  94. return ret;
  95. }
  96. static const struct thermal_zone_of_device_ops zx2967_of_thermal_ops = {
  97. .get_temp = zx2967_thermal_get_temp,
  98. };
  99. static int zx2967_thermal_probe(struct platform_device *pdev)
  100. {
  101. struct zx2967_thermal_priv *priv;
  102. struct resource *res;
  103. int ret;
  104. priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
  105. if (!priv)
  106. return -ENOMEM;
  107. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  108. priv->regs = devm_ioremap_resource(&pdev->dev, res);
  109. if (IS_ERR(priv->regs))
  110. return PTR_ERR(priv->regs);
  111. priv->clk_topcrm = devm_clk_get(&pdev->dev, "topcrm");
  112. if (IS_ERR(priv->clk_topcrm)) {
  113. ret = PTR_ERR(priv->clk_topcrm);
  114. dev_err(&pdev->dev, "failed to get topcrm clock: %d\n", ret);
  115. return ret;
  116. }
  117. ret = clk_prepare_enable(priv->clk_topcrm);
  118. if (ret) {
  119. dev_err(&pdev->dev, "failed to enable topcrm clock: %d\n",
  120. ret);
  121. return ret;
  122. }
  123. priv->clk_apb = devm_clk_get(&pdev->dev, "apb");
  124. if (IS_ERR(priv->clk_apb)) {
  125. ret = PTR_ERR(priv->clk_apb);
  126. dev_err(&pdev->dev, "failed to get apb clock: %d\n", ret);
  127. goto disable_clk_topcrm;
  128. }
  129. ret = clk_prepare_enable(priv->clk_apb);
  130. if (ret) {
  131. dev_err(&pdev->dev, "failed to enable apb clock: %d\n",
  132. ret);
  133. goto disable_clk_topcrm;
  134. }
  135. mutex_init(&priv->lock);
  136. priv->tzd = thermal_zone_of_sensor_register(&pdev->dev,
  137. 0, priv, &zx2967_of_thermal_ops);
  138. if (IS_ERR(priv->tzd)) {
  139. ret = PTR_ERR(priv->tzd);
  140. dev_err(&pdev->dev, "failed to register sensor: %d\n", ret);
  141. goto disable_clk_all;
  142. }
  143. if (priv->tzd->tzp->slope == 0) {
  144. thermal_zone_of_sensor_unregister(&pdev->dev, priv->tzd);
  145. dev_err(&pdev->dev, "coefficients of sensor is invalid\n");
  146. ret = -EINVAL;
  147. goto disable_clk_all;
  148. }
  149. priv->dev = &pdev->dev;
  150. platform_set_drvdata(pdev, priv);
  151. return 0;
  152. disable_clk_all:
  153. clk_disable_unprepare(priv->clk_apb);
  154. disable_clk_topcrm:
  155. clk_disable_unprepare(priv->clk_topcrm);
  156. return ret;
  157. }
  158. static int zx2967_thermal_exit(struct platform_device *pdev)
  159. {
  160. struct zx2967_thermal_priv *priv = platform_get_drvdata(pdev);
  161. thermal_zone_of_sensor_unregister(&pdev->dev, priv->tzd);
  162. clk_disable_unprepare(priv->clk_topcrm);
  163. clk_disable_unprepare(priv->clk_apb);
  164. return 0;
  165. }
  166. static const struct of_device_id zx2967_thermal_id_table[] = {
  167. { .compatible = "zte,zx296718-thermal" },
  168. {}
  169. };
  170. MODULE_DEVICE_TABLE(of, zx2967_thermal_id_table);
  171. #ifdef CONFIG_PM_SLEEP
  172. static int zx2967_thermal_suspend(struct device *dev)
  173. {
  174. struct zx2967_thermal_priv *priv = dev_get_drvdata(dev);
  175. if (priv && priv->clk_topcrm)
  176. clk_disable_unprepare(priv->clk_topcrm);
  177. if (priv && priv->clk_apb)
  178. clk_disable_unprepare(priv->clk_apb);
  179. return 0;
  180. }
  181. static int zx2967_thermal_resume(struct device *dev)
  182. {
  183. struct zx2967_thermal_priv *priv = dev_get_drvdata(dev);
  184. int error;
  185. error = clk_prepare_enable(priv->clk_topcrm);
  186. if (error)
  187. return error;
  188. error = clk_prepare_enable(priv->clk_apb);
  189. if (error) {
  190. clk_disable_unprepare(priv->clk_topcrm);
  191. return error;
  192. }
  193. return 0;
  194. }
  195. #endif
  196. static SIMPLE_DEV_PM_OPS(zx2967_thermal_pm_ops,
  197. zx2967_thermal_suspend, zx2967_thermal_resume);
  198. static struct platform_driver zx2967_thermal_driver = {
  199. .probe = zx2967_thermal_probe,
  200. .remove = zx2967_thermal_exit,
  201. .driver = {
  202. .name = "zx2967_thermal",
  203. .of_match_table = zx2967_thermal_id_table,
  204. .pm = &zx2967_thermal_pm_ops,
  205. },
  206. };
  207. module_platform_driver(zx2967_thermal_driver);
  208. MODULE_AUTHOR("Baoyou Xie <baoyou.xie@linaro.org>");
  209. MODULE_DESCRIPTION("ZTE zx2967 thermal driver");
  210. MODULE_LICENSE("GPL v2");