tango_thermal.c 3.1 KB

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