spear_thermal.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. unsigned long *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 *stres = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  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. if (!stres) {
  97. dev_err(&pdev->dev, "memory resource missing\n");
  98. return -ENODEV;
  99. }
  100. stdev = devm_kzalloc(&pdev->dev, sizeof(*stdev), GFP_KERNEL);
  101. if (!stdev) {
  102. dev_err(&pdev->dev, "kzalloc fail\n");
  103. return -ENOMEM;
  104. }
  105. /* Enable thermal sensor */
  106. stdev->thermal_base = devm_ioremap(&pdev->dev, stres->start,
  107. resource_size(stres));
  108. if (!stdev->thermal_base) {
  109. dev_err(&pdev->dev, "ioremap failed\n");
  110. return -ENOMEM;
  111. }
  112. stdev->clk = devm_clk_get(&pdev->dev, NULL);
  113. if (IS_ERR(stdev->clk)) {
  114. dev_err(&pdev->dev, "Can't get clock\n");
  115. return PTR_ERR(stdev->clk);
  116. }
  117. ret = clk_enable(stdev->clk);
  118. if (ret) {
  119. dev_err(&pdev->dev, "Can't enable clock\n");
  120. return ret;
  121. }
  122. stdev->flags = val;
  123. writel_relaxed(stdev->flags, stdev->thermal_base);
  124. spear_thermal = thermal_zone_device_register("spear_thermal", 0, 0,
  125. stdev, &ops, NULL, 0, 0);
  126. if (IS_ERR(spear_thermal)) {
  127. dev_err(&pdev->dev, "thermal zone device is NULL\n");
  128. ret = PTR_ERR(spear_thermal);
  129. goto disable_clk;
  130. }
  131. platform_set_drvdata(pdev, spear_thermal);
  132. dev_info(&spear_thermal->device, "Thermal Sensor Loaded at: 0x%p.\n",
  133. stdev->thermal_base);
  134. return 0;
  135. disable_clk:
  136. clk_disable(stdev->clk);
  137. return ret;
  138. }
  139. static int spear_thermal_exit(struct platform_device *pdev)
  140. {
  141. unsigned int actual_mask = 0;
  142. struct thermal_zone_device *spear_thermal = platform_get_drvdata(pdev);
  143. struct spear_thermal_dev *stdev = spear_thermal->devdata;
  144. thermal_zone_device_unregister(spear_thermal);
  145. platform_set_drvdata(pdev, NULL);
  146. /* Disable SPEAr Thermal Sensor */
  147. actual_mask = readl_relaxed(stdev->thermal_base);
  148. writel_relaxed(actual_mask & ~stdev->flags, stdev->thermal_base);
  149. clk_disable(stdev->clk);
  150. return 0;
  151. }
  152. static const struct of_device_id spear_thermal_id_table[] = {
  153. { .compatible = "st,thermal-spear1340" },
  154. {}
  155. };
  156. MODULE_DEVICE_TABLE(of, spear_thermal_id_table);
  157. static struct platform_driver spear_thermal_driver = {
  158. .probe = spear_thermal_probe,
  159. .remove = spear_thermal_exit,
  160. .driver = {
  161. .name = "spear_thermal",
  162. .owner = THIS_MODULE,
  163. .pm = &spear_thermal_pm_ops,
  164. .of_match_table = of_match_ptr(spear_thermal_id_table),
  165. },
  166. };
  167. module_platform_driver(spear_thermal_driver);
  168. MODULE_AUTHOR("Vincenzo Frascino <vincenzo.frascino@st.com>");
  169. MODULE_DESCRIPTION("SPEAr thermal driver");
  170. MODULE_LICENSE("GPL");