kirkwood_thermal.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Kirkwood thermal sensor driver
  4. *
  5. * Copyright (C) 2012 Nobuhiro Iwamatsu <iwamatsu@nigauri.org>
  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 KIRKWOOD_THERMAL_VALID_OFFSET 9
  16. #define KIRKWOOD_THERMAL_VALID_MASK 0x1
  17. #define KIRKWOOD_THERMAL_TEMP_OFFSET 10
  18. #define KIRKWOOD_THERMAL_TEMP_MASK 0x1FF
  19. /* Kirkwood Thermal Sensor Dev Structure */
  20. struct kirkwood_thermal_priv {
  21. void __iomem *sensor;
  22. };
  23. static int kirkwood_get_temp(struct thermal_zone_device *thermal,
  24. int *temp)
  25. {
  26. unsigned long reg;
  27. struct kirkwood_thermal_priv *priv = thermal->devdata;
  28. reg = readl_relaxed(priv->sensor);
  29. /* Valid check */
  30. if (!((reg >> KIRKWOOD_THERMAL_VALID_OFFSET) &
  31. KIRKWOOD_THERMAL_VALID_MASK)) {
  32. dev_err(&thermal->device,
  33. "Temperature sensor reading not valid\n");
  34. return -EIO;
  35. }
  36. /*
  37. * Calculate temperature. According to Marvell internal
  38. * documentation the formula for this is:
  39. * Celsius = (322-reg)/1.3625
  40. */
  41. reg = (reg >> KIRKWOOD_THERMAL_TEMP_OFFSET) &
  42. KIRKWOOD_THERMAL_TEMP_MASK;
  43. *temp = ((3220000000UL - (10000000UL * reg)) / 13625);
  44. return 0;
  45. }
  46. static struct thermal_zone_device_ops ops = {
  47. .get_temp = kirkwood_get_temp,
  48. };
  49. static const struct of_device_id kirkwood_thermal_id_table[] = {
  50. { .compatible = "marvell,kirkwood-thermal" },
  51. {}
  52. };
  53. static int kirkwood_thermal_probe(struct platform_device *pdev)
  54. {
  55. struct thermal_zone_device *thermal = NULL;
  56. struct kirkwood_thermal_priv *priv;
  57. struct resource *res;
  58. int ret;
  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->sensor = devm_ioremap_resource(&pdev->dev, res);
  64. if (IS_ERR(priv->sensor))
  65. return PTR_ERR(priv->sensor);
  66. thermal = thermal_zone_device_register("kirkwood_thermal", 0, 0,
  67. priv, &ops, NULL, 0, 0);
  68. if (IS_ERR(thermal)) {
  69. dev_err(&pdev->dev,
  70. "Failed to register thermal zone device\n");
  71. return PTR_ERR(thermal);
  72. }
  73. ret = thermal_zone_device_enable(thermal);
  74. if (ret) {
  75. thermal_zone_device_unregister(thermal);
  76. dev_err(&pdev->dev, "Failed to enable thermal zone device\n");
  77. return ret;
  78. }
  79. platform_set_drvdata(pdev, thermal);
  80. return 0;
  81. }
  82. static int kirkwood_thermal_exit(struct platform_device *pdev)
  83. {
  84. struct thermal_zone_device *kirkwood_thermal =
  85. platform_get_drvdata(pdev);
  86. thermal_zone_device_unregister(kirkwood_thermal);
  87. return 0;
  88. }
  89. MODULE_DEVICE_TABLE(of, kirkwood_thermal_id_table);
  90. static struct platform_driver kirkwood_thermal_driver = {
  91. .probe = kirkwood_thermal_probe,
  92. .remove = kirkwood_thermal_exit,
  93. .driver = {
  94. .name = "kirkwood_thermal",
  95. .of_match_table = kirkwood_thermal_id_table,
  96. },
  97. };
  98. module_platform_driver(kirkwood_thermal_driver);
  99. MODULE_AUTHOR("Nobuhiro Iwamatsu <iwamatsu@nigauri.org>");
  100. MODULE_DESCRIPTION("kirkwood thermal driver");
  101. MODULE_LICENSE("GPL");