kirkwood_thermal.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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_zone_device_priv(thermal);
  28. reg = readl_relaxed(priv->sensor);
  29. /* Valid check */
  30. if (!((reg >> KIRKWOOD_THERMAL_VALID_OFFSET) &
  31. KIRKWOOD_THERMAL_VALID_MASK))
  32. return -EIO;
  33. /*
  34. * Calculate temperature. According to Marvell internal
  35. * documentation the formula for this is:
  36. * Celsius = (322-reg)/1.3625
  37. */
  38. reg = (reg >> KIRKWOOD_THERMAL_TEMP_OFFSET) &
  39. KIRKWOOD_THERMAL_TEMP_MASK;
  40. *temp = ((3220000000UL - (10000000UL * reg)) / 13625);
  41. return 0;
  42. }
  43. static struct thermal_zone_device_ops ops = {
  44. .get_temp = kirkwood_get_temp,
  45. };
  46. static const struct of_device_id kirkwood_thermal_id_table[] = {
  47. { .compatible = "marvell,kirkwood-thermal" },
  48. {}
  49. };
  50. static int kirkwood_thermal_probe(struct platform_device *pdev)
  51. {
  52. struct thermal_zone_device *thermal = NULL;
  53. struct kirkwood_thermal_priv *priv;
  54. int ret;
  55. priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
  56. if (!priv)
  57. return -ENOMEM;
  58. priv->sensor = devm_platform_get_and_ioremap_resource(pdev, 0, NULL);
  59. if (IS_ERR(priv->sensor))
  60. return PTR_ERR(priv->sensor);
  61. thermal = thermal_tripless_zone_device_register("kirkwood_thermal",
  62. priv, &ops, NULL);
  63. if (IS_ERR(thermal)) {
  64. dev_err(&pdev->dev,
  65. "Failed to register thermal zone device\n");
  66. return PTR_ERR(thermal);
  67. }
  68. ret = thermal_zone_device_enable(thermal);
  69. if (ret) {
  70. thermal_zone_device_unregister(thermal);
  71. dev_err(&pdev->dev, "Failed to enable thermal zone device\n");
  72. return ret;
  73. }
  74. platform_set_drvdata(pdev, thermal);
  75. return 0;
  76. }
  77. static void kirkwood_thermal_exit(struct platform_device *pdev)
  78. {
  79. struct thermal_zone_device *kirkwood_thermal =
  80. platform_get_drvdata(pdev);
  81. thermal_zone_device_unregister(kirkwood_thermal);
  82. }
  83. MODULE_DEVICE_TABLE(of, kirkwood_thermal_id_table);
  84. static struct platform_driver kirkwood_thermal_driver = {
  85. .probe = kirkwood_thermal_probe,
  86. .remove_new = kirkwood_thermal_exit,
  87. .driver = {
  88. .name = "kirkwood_thermal",
  89. .of_match_table = kirkwood_thermal_id_table,
  90. },
  91. };
  92. module_platform_driver(kirkwood_thermal_driver);
  93. MODULE_AUTHOR("Nobuhiro Iwamatsu <iwamatsu@nigauri.org>");
  94. MODULE_DESCRIPTION("kirkwood thermal driver");
  95. MODULE_LICENSE("GPL");