spear_thermal.c 5.2 KB

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