tango_thermal.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 int tango_thermal_probe(struct platform_device *pdev)
  55. {
  56. struct resource *res;
  57. struct tango_thermal_priv *priv;
  58. struct thermal_zone_device *tzdev;
  59. priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
  60. if (!priv)
  61. return -ENOMEM;
  62. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  63. priv->base = devm_ioremap_resource(&pdev->dev, res);
  64. if (IS_ERR(priv->base))
  65. return PTR_ERR(priv->base);
  66. priv->thresh_idx = IDX_MIN;
  67. writel(0, priv->base + TEMPSI_CFG);
  68. writel(CMD_ON, priv->base + TEMPSI_CMD);
  69. tzdev = devm_thermal_zone_of_sensor_register(&pdev->dev, 0, priv, &ops);
  70. return PTR_ERR_OR_ZERO(tzdev);
  71. }
  72. static const struct of_device_id tango_sensor_ids[] = {
  73. {
  74. .compatible = "sigma,smp8758-thermal",
  75. },
  76. { /* sentinel */ }
  77. };
  78. static struct platform_driver tango_thermal_driver = {
  79. .probe = tango_thermal_probe,
  80. .driver = {
  81. .name = "tango-thermal",
  82. .of_match_table = tango_sensor_ids,
  83. },
  84. };
  85. module_platform_driver(tango_thermal_driver);
  86. MODULE_LICENSE("GPL");
  87. MODULE_AUTHOR("Sigma Designs");
  88. MODULE_DESCRIPTION("Tango temperature sensor");