kirkwood_thermal.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * Kirkwood thermal sensor driver
  3. *
  4. * Copyright (C) 2012 Nobuhiro Iwamatsu <iwamatsu@nigauri.org>
  5. *
  6. * This software is licensed under the terms of the GNU General Public
  7. * License version 2, as published by the Free Software Foundation, and
  8. * may be copied, distributed, and modified under those terms.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. */
  16. #include <linux/device.h>
  17. #include <linux/err.h>
  18. #include <linux/io.h>
  19. #include <linux/kernel.h>
  20. #include <linux/of.h>
  21. #include <linux/module.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/thermal.h>
  24. #define KIRKWOOD_THERMAL_VALID_OFFSET 9
  25. #define KIRKWOOD_THERMAL_VALID_MASK 0x1
  26. #define KIRKWOOD_THERMAL_TEMP_OFFSET 10
  27. #define KIRKWOOD_THERMAL_TEMP_MASK 0x1FF
  28. /* Kirkwood Thermal Sensor Dev Structure */
  29. struct kirkwood_thermal_priv {
  30. void __iomem *sensor;
  31. };
  32. static int kirkwood_get_temp(struct thermal_zone_device *thermal,
  33. int *temp)
  34. {
  35. unsigned long reg;
  36. struct kirkwood_thermal_priv *priv = thermal->devdata;
  37. reg = readl_relaxed(priv->sensor);
  38. /* Valid check */
  39. if (!((reg >> KIRKWOOD_THERMAL_VALID_OFFSET) &
  40. KIRKWOOD_THERMAL_VALID_MASK)) {
  41. dev_err(&thermal->device,
  42. "Temperature sensor reading not valid\n");
  43. return -EIO;
  44. }
  45. /*
  46. * Calculate temperature. According to Marvell internal
  47. * documentation the formula for this is:
  48. * Celsius = (322-reg)/1.3625
  49. */
  50. reg = (reg >> KIRKWOOD_THERMAL_TEMP_OFFSET) &
  51. KIRKWOOD_THERMAL_TEMP_MASK;
  52. *temp = ((3220000000UL - (10000000UL * reg)) / 13625);
  53. return 0;
  54. }
  55. static struct thermal_zone_device_ops ops = {
  56. .get_temp = kirkwood_get_temp,
  57. };
  58. static const struct of_device_id kirkwood_thermal_id_table[] = {
  59. { .compatible = "marvell,kirkwood-thermal" },
  60. {}
  61. };
  62. static int kirkwood_thermal_probe(struct platform_device *pdev)
  63. {
  64. struct thermal_zone_device *thermal = NULL;
  65. struct kirkwood_thermal_priv *priv;
  66. struct resource *res;
  67. priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
  68. if (!priv)
  69. return -ENOMEM;
  70. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  71. priv->sensor = devm_ioremap_resource(&pdev->dev, res);
  72. if (IS_ERR(priv->sensor))
  73. return PTR_ERR(priv->sensor);
  74. thermal = thermal_zone_device_register("kirkwood_thermal", 0, 0,
  75. priv, &ops, NULL, 0, 0);
  76. if (IS_ERR(thermal)) {
  77. dev_err(&pdev->dev,
  78. "Failed to register thermal zone device\n");
  79. return PTR_ERR(thermal);
  80. }
  81. platform_set_drvdata(pdev, thermal);
  82. return 0;
  83. }
  84. static int kirkwood_thermal_exit(struct platform_device *pdev)
  85. {
  86. struct thermal_zone_device *kirkwood_thermal =
  87. platform_get_drvdata(pdev);
  88. thermal_zone_device_unregister(kirkwood_thermal);
  89. return 0;
  90. }
  91. MODULE_DEVICE_TABLE(of, kirkwood_thermal_id_table);
  92. static struct platform_driver kirkwood_thermal_driver = {
  93. .probe = kirkwood_thermal_probe,
  94. .remove = kirkwood_thermal_exit,
  95. .driver = {
  96. .name = "kirkwood_thermal",
  97. .of_match_table = kirkwood_thermal_id_table,
  98. },
  99. };
  100. module_platform_driver(kirkwood_thermal_driver);
  101. MODULE_AUTHOR("Nobuhiro Iwamatsu <iwamatsu@nigauri.org>");
  102. MODULE_DESCRIPTION("kirkwood thermal driver");
  103. MODULE_LICENSE("GPL");