spear_thermal.c 5.1 KB

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