spear_thermal.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * SPEAr thermal driver.
  4. *
  5. * Copyright (C) 2011-2012 ST Microelectronics
  6. * Author: Vincenzo Frascino <vincenzo.frascino@st.com>
  7. */
  8. #include <linux/clk.h>
  9. #include <linux/device.h>
  10. #include <linux/err.h>
  11. #include <linux/io.h>
  12. #include <linux/kernel.h>
  13. #include <linux/of.h>
  14. #include <linux/module.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/thermal.h>
  17. #define MD_FACTOR 1000
  18. /* SPEAr Thermal Sensor Dev Structure */
  19. struct spear_thermal_dev {
  20. /* pointer to base address of the thermal sensor */
  21. void __iomem *thermal_base;
  22. /* clk structure */
  23. struct clk *clk;
  24. /* pointer to thermal flags */
  25. unsigned int flags;
  26. };
  27. static inline int thermal_get_temp(struct thermal_zone_device *thermal,
  28. int *temp)
  29. {
  30. struct spear_thermal_dev *stdev = thermal_zone_device_priv(thermal);
  31. /*
  32. * Data are ready to be read after 628 usec from POWERDOWN signal
  33. * (PDN) = 1
  34. */
  35. *temp = (readl_relaxed(stdev->thermal_base) & 0x7F) * MD_FACTOR;
  36. return 0;
  37. }
  38. static const struct thermal_zone_device_ops ops = {
  39. .get_temp = thermal_get_temp,
  40. };
  41. static int __maybe_unused spear_thermal_suspend(struct device *dev)
  42. {
  43. struct thermal_zone_device *spear_thermal = dev_get_drvdata(dev);
  44. struct spear_thermal_dev *stdev = thermal_zone_device_priv(spear_thermal);
  45. unsigned int actual_mask = 0;
  46. /* Disable SPEAr Thermal Sensor */
  47. actual_mask = readl_relaxed(stdev->thermal_base);
  48. writel_relaxed(actual_mask & ~stdev->flags, stdev->thermal_base);
  49. clk_disable(stdev->clk);
  50. dev_info(dev, "Suspended.\n");
  51. return 0;
  52. }
  53. static int __maybe_unused spear_thermal_resume(struct device *dev)
  54. {
  55. struct thermal_zone_device *spear_thermal = dev_get_drvdata(dev);
  56. struct spear_thermal_dev *stdev = thermal_zone_device_priv(spear_thermal);
  57. unsigned int actual_mask = 0;
  58. int ret = 0;
  59. ret = clk_enable(stdev->clk);
  60. if (ret) {
  61. dev_err(dev, "Can't enable clock\n");
  62. return ret;
  63. }
  64. /* Enable SPEAr Thermal Sensor */
  65. actual_mask = readl_relaxed(stdev->thermal_base);
  66. writel_relaxed(actual_mask | stdev->flags, stdev->thermal_base);
  67. dev_info(dev, "Resumed.\n");
  68. return 0;
  69. }
  70. static SIMPLE_DEV_PM_OPS(spear_thermal_pm_ops, spear_thermal_suspend,
  71. spear_thermal_resume);
  72. static int spear_thermal_probe(struct platform_device *pdev)
  73. {
  74. struct thermal_zone_device *spear_thermal = NULL;
  75. struct spear_thermal_dev *stdev;
  76. struct device_node *np = pdev->dev.of_node;
  77. int ret = 0, val;
  78. if (!np || !of_property_read_u32(np, "st,thermal-flags", &val)) {
  79. dev_err(&pdev->dev, "Failed: DT Pdata not passed\n");
  80. return -EINVAL;
  81. }
  82. stdev = devm_kzalloc(&pdev->dev, sizeof(*stdev), GFP_KERNEL);
  83. if (!stdev)
  84. return -ENOMEM;
  85. /* Enable thermal sensor */
  86. stdev->thermal_base = devm_platform_get_and_ioremap_resource(pdev, 0, NULL);
  87. if (IS_ERR(stdev->thermal_base))
  88. return PTR_ERR(stdev->thermal_base);
  89. stdev->clk = devm_clk_get(&pdev->dev, NULL);
  90. if (IS_ERR(stdev->clk)) {
  91. dev_err(&pdev->dev, "Can't get clock\n");
  92. return PTR_ERR(stdev->clk);
  93. }
  94. ret = clk_enable(stdev->clk);
  95. if (ret) {
  96. dev_err(&pdev->dev, "Can't enable clock\n");
  97. return ret;
  98. }
  99. stdev->flags = val;
  100. writel_relaxed(stdev->flags, stdev->thermal_base);
  101. spear_thermal = thermal_tripless_zone_device_register("spear_thermal",
  102. stdev, &ops, NULL);
  103. if (IS_ERR(spear_thermal)) {
  104. dev_err(&pdev->dev, "thermal zone device is NULL\n");
  105. ret = PTR_ERR(spear_thermal);
  106. goto disable_clk;
  107. }
  108. ret = thermal_zone_device_enable(spear_thermal);
  109. if (ret) {
  110. dev_err(&pdev->dev, "Cannot enable thermal zone\n");
  111. goto unregister_tzd;
  112. }
  113. platform_set_drvdata(pdev, spear_thermal);
  114. dev_info(&pdev->dev, "Thermal Sensor Loaded at: 0x%p.\n",
  115. stdev->thermal_base);
  116. return 0;
  117. unregister_tzd:
  118. thermal_zone_device_unregister(spear_thermal);
  119. disable_clk:
  120. clk_disable(stdev->clk);
  121. return ret;
  122. }
  123. static void spear_thermal_exit(struct platform_device *pdev)
  124. {
  125. unsigned int actual_mask = 0;
  126. struct thermal_zone_device *spear_thermal = platform_get_drvdata(pdev);
  127. struct spear_thermal_dev *stdev = thermal_zone_device_priv(spear_thermal);
  128. thermal_zone_device_unregister(spear_thermal);
  129. /* Disable SPEAr Thermal Sensor */
  130. actual_mask = readl_relaxed(stdev->thermal_base);
  131. writel_relaxed(actual_mask & ~stdev->flags, stdev->thermal_base);
  132. clk_disable(stdev->clk);
  133. }
  134. static const struct of_device_id spear_thermal_id_table[] = {
  135. { .compatible = "st,thermal-spear1340" },
  136. {}
  137. };
  138. MODULE_DEVICE_TABLE(of, spear_thermal_id_table);
  139. static struct platform_driver spear_thermal_driver = {
  140. .probe = spear_thermal_probe,
  141. .remove = spear_thermal_exit,
  142. .driver = {
  143. .name = "spear_thermal",
  144. .pm = &spear_thermal_pm_ops,
  145. .of_match_table = spear_thermal_id_table,
  146. },
  147. };
  148. module_platform_driver(spear_thermal_driver);
  149. MODULE_AUTHOR("Vincenzo Frascino <vincenzo.frascino@st.com>");
  150. MODULE_DESCRIPTION("SPEAr thermal driver");
  151. MODULE_LICENSE("GPL");