tango_thermal.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. #include <linux/io.h>
  3. #include <linux/delay.h>
  4. #include <linux/module.h>
  5. #include <linux/thermal.h>
  6. #include <linux/platform_device.h>
  7. /*
  8. * According to a data sheet draft, "this temperature sensor uses a bandgap
  9. * type of circuit to compare a voltage which has a negative temperature
  10. * coefficient with a voltage that is proportional to absolute temperature.
  11. * A resistor bank allows 41 different temperature thresholds to be selected
  12. * and the logic output will then indicate whether the actual die temperature
  13. * lies above or below the selected threshold."
  14. */
  15. #define TEMPSI_CMD 0
  16. #define TEMPSI_RES 4
  17. #define TEMPSI_CFG 8
  18. #define CMD_OFF 0
  19. #define CMD_ON 1
  20. #define CMD_READ 2
  21. #define IDX_MIN 15
  22. #define IDX_MAX 40
  23. struct tango_thermal_priv {
  24. void __iomem *base;
  25. int thresh_idx;
  26. };
  27. static bool temp_above_thresh(void __iomem *base, int thresh_idx)
  28. {
  29. writel(CMD_READ | thresh_idx << 8, base + TEMPSI_CMD);
  30. usleep_range(10, 20);
  31. writel(CMD_READ | thresh_idx << 8, base + TEMPSI_CMD);
  32. return readl(base + TEMPSI_RES);
  33. }
  34. static int tango_get_temp(void *arg, int *res)
  35. {
  36. struct tango_thermal_priv *priv = arg;
  37. int idx = priv->thresh_idx;
  38. if (temp_above_thresh(priv->base, idx)) {
  39. /* Search upward by incrementing thresh_idx */
  40. while (idx < IDX_MAX && temp_above_thresh(priv->base, ++idx))
  41. cpu_relax();
  42. idx = idx - 1; /* always return lower bound */
  43. } else {
  44. /* Search downward by decrementing thresh_idx */
  45. while (idx > IDX_MIN && !temp_above_thresh(priv->base, --idx))
  46. cpu_relax();
  47. }
  48. *res = (idx * 9 / 2 - 38) * 1000; /* millidegrees Celsius */
  49. priv->thresh_idx = idx;
  50. return 0;
  51. }
  52. static const struct thermal_zone_of_device_ops ops = {
  53. .get_temp = tango_get_temp,
  54. };
  55. static void tango_thermal_init(struct tango_thermal_priv *priv)
  56. {
  57. writel(0, priv->base + TEMPSI_CFG);
  58. writel(CMD_ON, priv->base + TEMPSI_CMD);
  59. }
  60. static int tango_thermal_probe(struct platform_device *pdev)
  61. {
  62. struct resource *res;
  63. struct tango_thermal_priv *priv;
  64. struct thermal_zone_device *tzdev;
  65. priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
  66. if (!priv)
  67. return -ENOMEM;
  68. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  69. priv->base = devm_ioremap_resource(&pdev->dev, res);
  70. if (IS_ERR(priv->base))
  71. return PTR_ERR(priv->base);
  72. platform_set_drvdata(pdev, priv);
  73. priv->thresh_idx = IDX_MIN;
  74. tango_thermal_init(priv);
  75. tzdev = devm_thermal_zone_of_sensor_register(&pdev->dev, 0, priv, &ops);
  76. return PTR_ERR_OR_ZERO(tzdev);
  77. }
  78. static int __maybe_unused tango_thermal_resume(struct device *dev)
  79. {
  80. tango_thermal_init(dev_get_drvdata(dev));
  81. return 0;
  82. }
  83. static SIMPLE_DEV_PM_OPS(tango_thermal_pm, NULL, tango_thermal_resume);
  84. static const struct of_device_id tango_sensor_ids[] = {
  85. {
  86. .compatible = "sigma,smp8758-thermal",
  87. },
  88. { /* sentinel */ }
  89. };
  90. MODULE_DEVICE_TABLE(of, tango_sensor_ids);
  91. static struct platform_driver tango_thermal_driver = {
  92. .probe = tango_thermal_probe,
  93. .driver = {
  94. .name = "tango-thermal",
  95. .of_match_table = tango_sensor_ids,
  96. .pm = &tango_thermal_pm,
  97. },
  98. };
  99. module_platform_driver(tango_thermal_driver);
  100. MODULE_LICENSE("GPL");
  101. MODULE_AUTHOR("Sigma Designs");
  102. MODULE_DESCRIPTION("Tango temperature sensor");