dove_thermal.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Dove thermal sensor driver
  4. *
  5. * Copyright (C) 2013 Andrew Lunn <andrew@lunn.ch>
  6. */
  7. #include <linux/device.h>
  8. #include <linux/err.h>
  9. #include <linux/io.h>
  10. #include <linux/kernel.h>
  11. #include <linux/of.h>
  12. #include <linux/module.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/thermal.h>
  15. #define DOVE_THERMAL_TEMP_OFFSET 1
  16. #define DOVE_THERMAL_TEMP_MASK 0x1FF
  17. /* Dove Thermal Manager Control and Status Register */
  18. #define PMU_TM_DISABLE_OFFS 0
  19. #define PMU_TM_DISABLE_MASK (0x1 << PMU_TM_DISABLE_OFFS)
  20. /* Dove Theraml Diode Control 0 Register */
  21. #define PMU_TDC0_SW_RST_MASK (0x1 << 1)
  22. #define PMU_TDC0_SEL_VCAL_OFFS 5
  23. #define PMU_TDC0_SEL_VCAL_MASK (0x3 << PMU_TDC0_SEL_VCAL_OFFS)
  24. #define PMU_TDC0_REF_CAL_CNT_OFFS 11
  25. #define PMU_TDC0_REF_CAL_CNT_MASK (0x1FF << PMU_TDC0_REF_CAL_CNT_OFFS)
  26. #define PMU_TDC0_AVG_NUM_OFFS 25
  27. #define PMU_TDC0_AVG_NUM_MASK (0x7 << PMU_TDC0_AVG_NUM_OFFS)
  28. /* Dove Thermal Diode Control 1 Register */
  29. #define PMU_TEMP_DIOD_CTRL1_REG 0x04
  30. #define PMU_TDC1_TEMP_VALID_MASK (0x1 << 10)
  31. /* Dove Thermal Sensor Dev Structure */
  32. struct dove_thermal_priv {
  33. void __iomem *sensor;
  34. void __iomem *control;
  35. };
  36. static int dove_init_sensor(const struct dove_thermal_priv *priv)
  37. {
  38. u32 reg;
  39. u32 i;
  40. /* Configure the Diode Control Register #0 */
  41. reg = readl_relaxed(priv->control);
  42. /* Use average of 2 */
  43. reg &= ~PMU_TDC0_AVG_NUM_MASK;
  44. reg |= (0x1 << PMU_TDC0_AVG_NUM_OFFS);
  45. /* Reference calibration value */
  46. reg &= ~PMU_TDC0_REF_CAL_CNT_MASK;
  47. reg |= (0x0F1 << PMU_TDC0_REF_CAL_CNT_OFFS);
  48. /* Set the high level reference for calibration */
  49. reg &= ~PMU_TDC0_SEL_VCAL_MASK;
  50. reg |= (0x2 << PMU_TDC0_SEL_VCAL_OFFS);
  51. writel(reg, priv->control);
  52. /* Reset the sensor */
  53. reg = readl_relaxed(priv->control);
  54. writel((reg | PMU_TDC0_SW_RST_MASK), priv->control);
  55. writel(reg, priv->control);
  56. /* Enable the sensor */
  57. reg = readl_relaxed(priv->sensor);
  58. reg &= ~PMU_TM_DISABLE_MASK;
  59. writel(reg, priv->sensor);
  60. /* Poll the sensor for the first reading */
  61. for (i = 0; i < 1000000; i++) {
  62. reg = readl_relaxed(priv->sensor);
  63. if (reg & DOVE_THERMAL_TEMP_MASK)
  64. break;
  65. }
  66. if (i == 1000000)
  67. return -EIO;
  68. return 0;
  69. }
  70. static int dove_get_temp(struct thermal_zone_device *thermal,
  71. int *temp)
  72. {
  73. unsigned long reg;
  74. struct dove_thermal_priv *priv = thermal_zone_device_priv(thermal);
  75. /* Valid check */
  76. reg = readl_relaxed(priv->control + PMU_TEMP_DIOD_CTRL1_REG);
  77. if ((reg & PMU_TDC1_TEMP_VALID_MASK) == 0x0)
  78. return -EIO;
  79. /*
  80. * Calculate temperature. According to Marvell internal
  81. * documentation the formula for this is:
  82. * Celsius = (322-reg)/1.3625
  83. */
  84. reg = readl_relaxed(priv->sensor);
  85. reg = (reg >> DOVE_THERMAL_TEMP_OFFSET) & DOVE_THERMAL_TEMP_MASK;
  86. *temp = ((3220000000UL - (10000000UL * reg)) / 13625);
  87. return 0;
  88. }
  89. static const struct thermal_zone_device_ops ops = {
  90. .get_temp = dove_get_temp,
  91. };
  92. static const struct of_device_id dove_thermal_id_table[] = {
  93. { .compatible = "marvell,dove-thermal" },
  94. {}
  95. };
  96. static int dove_thermal_probe(struct platform_device *pdev)
  97. {
  98. struct thermal_zone_device *thermal = NULL;
  99. struct dove_thermal_priv *priv;
  100. int ret;
  101. priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
  102. if (!priv)
  103. return -ENOMEM;
  104. priv->sensor = devm_platform_get_and_ioremap_resource(pdev, 0, NULL);
  105. if (IS_ERR(priv->sensor))
  106. return PTR_ERR(priv->sensor);
  107. priv->control = devm_platform_get_and_ioremap_resource(pdev, 1, NULL);
  108. if (IS_ERR(priv->control))
  109. return PTR_ERR(priv->control);
  110. ret = dove_init_sensor(priv);
  111. if (ret) {
  112. dev_err(&pdev->dev, "Failed to initialize sensor\n");
  113. return ret;
  114. }
  115. thermal = thermal_tripless_zone_device_register("dove_thermal", priv,
  116. &ops, NULL);
  117. if (IS_ERR(thermal)) {
  118. dev_err(&pdev->dev,
  119. "Failed to register thermal zone device\n");
  120. return PTR_ERR(thermal);
  121. }
  122. ret = thermal_zone_device_enable(thermal);
  123. if (ret) {
  124. thermal_zone_device_unregister(thermal);
  125. return ret;
  126. }
  127. platform_set_drvdata(pdev, thermal);
  128. return 0;
  129. }
  130. static void dove_thermal_exit(struct platform_device *pdev)
  131. {
  132. struct thermal_zone_device *dove_thermal =
  133. platform_get_drvdata(pdev);
  134. thermal_zone_device_unregister(dove_thermal);
  135. }
  136. MODULE_DEVICE_TABLE(of, dove_thermal_id_table);
  137. static struct platform_driver dove_thermal_driver = {
  138. .probe = dove_thermal_probe,
  139. .remove = dove_thermal_exit,
  140. .driver = {
  141. .name = "dove_thermal",
  142. .of_match_table = dove_thermal_id_table,
  143. },
  144. };
  145. module_platform_driver(dove_thermal_driver);
  146. MODULE_AUTHOR("Andrew Lunn <andrew@lunn.ch>");
  147. MODULE_DESCRIPTION("Dove thermal driver");
  148. MODULE_LICENSE("GPL");