dove_thermal.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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->devdata;
  75. /* Valid check */
  76. reg = readl_relaxed(priv->control + PMU_TEMP_DIOD_CTRL1_REG);
  77. if ((reg & PMU_TDC1_TEMP_VALID_MASK) == 0x0) {
  78. dev_err(&thermal->device,
  79. "Temperature sensor reading not valid\n");
  80. return -EIO;
  81. }
  82. /*
  83. * Calculate temperature. According to Marvell internal
  84. * documentation the formula for this is:
  85. * Celsius = (322-reg)/1.3625
  86. */
  87. reg = readl_relaxed(priv->sensor);
  88. reg = (reg >> DOVE_THERMAL_TEMP_OFFSET) & DOVE_THERMAL_TEMP_MASK;
  89. *temp = ((3220000000UL - (10000000UL * reg)) / 13625);
  90. return 0;
  91. }
  92. static struct thermal_zone_device_ops ops = {
  93. .get_temp = dove_get_temp,
  94. };
  95. static const struct of_device_id dove_thermal_id_table[] = {
  96. { .compatible = "marvell,dove-thermal" },
  97. {}
  98. };
  99. static int dove_thermal_probe(struct platform_device *pdev)
  100. {
  101. struct thermal_zone_device *thermal = NULL;
  102. struct dove_thermal_priv *priv;
  103. struct resource *res;
  104. int ret;
  105. priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
  106. if (!priv)
  107. return -ENOMEM;
  108. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  109. priv->sensor = devm_ioremap_resource(&pdev->dev, res);
  110. if (IS_ERR(priv->sensor))
  111. return PTR_ERR(priv->sensor);
  112. res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  113. priv->control = devm_ioremap_resource(&pdev->dev, res);
  114. if (IS_ERR(priv->control))
  115. return PTR_ERR(priv->control);
  116. ret = dove_init_sensor(priv);
  117. if (ret) {
  118. dev_err(&pdev->dev, "Failed to initialize sensor\n");
  119. return ret;
  120. }
  121. thermal = thermal_zone_device_register("dove_thermal", 0, 0,
  122. priv, &ops, NULL, 0, 0);
  123. if (IS_ERR(thermal)) {
  124. dev_err(&pdev->dev,
  125. "Failed to register thermal zone device\n");
  126. return PTR_ERR(thermal);
  127. }
  128. ret = thermal_zone_device_enable(thermal);
  129. if (ret) {
  130. thermal_zone_device_unregister(thermal);
  131. return ret;
  132. }
  133. platform_set_drvdata(pdev, thermal);
  134. return 0;
  135. }
  136. static int dove_thermal_exit(struct platform_device *pdev)
  137. {
  138. struct thermal_zone_device *dove_thermal =
  139. platform_get_drvdata(pdev);
  140. thermal_zone_device_unregister(dove_thermal);
  141. return 0;
  142. }
  143. MODULE_DEVICE_TABLE(of, dove_thermal_id_table);
  144. static struct platform_driver dove_thermal_driver = {
  145. .probe = dove_thermal_probe,
  146. .remove = dove_thermal_exit,
  147. .driver = {
  148. .name = "dove_thermal",
  149. .of_match_table = dove_thermal_id_table,
  150. },
  151. };
  152. module_platform_driver(dove_thermal_driver);
  153. MODULE_AUTHOR("Andrew Lunn <andrew@lunn.ch>");
  154. MODULE_DESCRIPTION("Dove thermal driver");
  155. MODULE_LICENSE("GPL");