spear_thermal.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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->devdata;
  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 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 = spear_thermal->devdata;
  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 = spear_thermal->devdata;
  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. struct resource *res;
  78. int ret = 0, val;
  79. if (!np || !of_property_read_u32(np, "st,thermal-flags", &val)) {
  80. dev_err(&pdev->dev, "Failed: DT Pdata not passed\n");
  81. return -EINVAL;
  82. }
  83. stdev = devm_kzalloc(&pdev->dev, sizeof(*stdev), GFP_KERNEL);
  84. if (!stdev)
  85. return -ENOMEM;
  86. /* Enable thermal sensor */
  87. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  88. stdev->thermal_base = devm_ioremap_resource(&pdev->dev, res);
  89. if (IS_ERR(stdev->thermal_base))
  90. return PTR_ERR(stdev->thermal_base);
  91. stdev->clk = devm_clk_get(&pdev->dev, NULL);
  92. if (IS_ERR(stdev->clk)) {
  93. dev_err(&pdev->dev, "Can't get clock\n");
  94. return PTR_ERR(stdev->clk);
  95. }
  96. ret = clk_enable(stdev->clk);
  97. if (ret) {
  98. dev_err(&pdev->dev, "Can't enable clock\n");
  99. return ret;
  100. }
  101. stdev->flags = val;
  102. writel_relaxed(stdev->flags, stdev->thermal_base);
  103. spear_thermal = thermal_zone_device_register("spear_thermal", 0, 0,
  104. stdev, &ops, NULL, 0, 0);
  105. if (IS_ERR(spear_thermal)) {
  106. dev_err(&pdev->dev, "thermal zone device is NULL\n");
  107. ret = PTR_ERR(spear_thermal);
  108. goto disable_clk;
  109. }
  110. ret = thermal_zone_device_enable(spear_thermal);
  111. if (ret) {
  112. dev_err(&pdev->dev, "Cannot enable thermal zone\n");
  113. goto unregister_tzd;
  114. }
  115. platform_set_drvdata(pdev, spear_thermal);
  116. dev_info(&spear_thermal->device, "Thermal Sensor Loaded at: 0x%p.\n",
  117. stdev->thermal_base);
  118. return 0;
  119. unregister_tzd:
  120. thermal_zone_device_unregister(spear_thermal);
  121. disable_clk:
  122. clk_disable(stdev->clk);
  123. return ret;
  124. }
  125. static int spear_thermal_exit(struct platform_device *pdev)
  126. {
  127. unsigned int actual_mask = 0;
  128. struct thermal_zone_device *spear_thermal = platform_get_drvdata(pdev);
  129. struct spear_thermal_dev *stdev = spear_thermal->devdata;
  130. thermal_zone_device_unregister(spear_thermal);
  131. /* Disable SPEAr Thermal Sensor */
  132. actual_mask = readl_relaxed(stdev->thermal_base);
  133. writel_relaxed(actual_mask & ~stdev->flags, stdev->thermal_base);
  134. clk_disable(stdev->clk);
  135. return 0;
  136. }
  137. static const struct of_device_id spear_thermal_id_table[] = {
  138. { .compatible = "st,thermal-spear1340" },
  139. {}
  140. };
  141. MODULE_DEVICE_TABLE(of, spear_thermal_id_table);
  142. static struct platform_driver spear_thermal_driver = {
  143. .probe = spear_thermal_probe,
  144. .remove = spear_thermal_exit,
  145. .driver = {
  146. .name = "spear_thermal",
  147. .pm = &spear_thermal_pm_ops,
  148. .of_match_table = spear_thermal_id_table,
  149. },
  150. };
  151. module_platform_driver(spear_thermal_driver);
  152. MODULE_AUTHOR("Vincenzo Frascino <vincenzo.frascino@st.com>");
  153. MODULE_DESCRIPTION("SPEAr thermal driver");
  154. MODULE_LICENSE("GPL");