spear_thermal.c 5.1 KB

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