dove_thermal.c 5.0 KB

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