]>
Commit | Line | Data |
---|---|---|
6a92c366 VF |
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 | ||
18 | #include <linux/clk.h> | |
19 | #include <linux/device.h> | |
20 | #include <linux/err.h> | |
21 | #include <linux/io.h> | |
22 | #include <linux/kernel.h> | |
b9c7aff4 | 23 | #include <linux/of.h> |
6a92c366 VF |
24 | #include <linux/module.h> |
25 | #include <linux/platform_device.h> | |
6a92c366 VF |
26 | #include <linux/thermal.h> |
27 | ||
28 | #define MD_FACTOR 1000 | |
29 | ||
30 | /* SPEAr Thermal Sensor Dev Structure */ | |
31 | struct spear_thermal_dev { | |
32 | /* pointer to base address of the thermal sensor */ | |
33 | void __iomem *thermal_base; | |
34 | /* clk structure */ | |
35 | struct clk *clk; | |
36 | /* pointer to thermal flags */ | |
37 | unsigned int flags; | |
38 | }; | |
39 | ||
40 | static inline int thermal_get_temp(struct thermal_zone_device *thermal, | |
17e8351a | 41 | int *temp) |
6a92c366 VF |
42 | { |
43 | struct spear_thermal_dev *stdev = thermal->devdata; | |
44 | ||
45 | /* | |
46 | * Data are ready to be read after 628 usec from POWERDOWN signal | |
47 | * (PDN) = 1 | |
48 | */ | |
de716e32 | 49 | *temp = (readl_relaxed(stdev->thermal_base) & 0x7F) * MD_FACTOR; |
6a92c366 VF |
50 | return 0; |
51 | } | |
52 | ||
53 | static struct thermal_zone_device_ops ops = { | |
54 | .get_temp = thermal_get_temp, | |
55 | }; | |
56 | ||
d612c64d | 57 | static int __maybe_unused spear_thermal_suspend(struct device *dev) |
6a92c366 | 58 | { |
3fc62efe | 59 | struct thermal_zone_device *spear_thermal = dev_get_drvdata(dev); |
6a92c366 VF |
60 | struct spear_thermal_dev *stdev = spear_thermal->devdata; |
61 | unsigned int actual_mask = 0; | |
62 | ||
63 | /* Disable SPEAr Thermal Sensor */ | |
de716e32 VK |
64 | actual_mask = readl_relaxed(stdev->thermal_base); |
65 | writel_relaxed(actual_mask & ~stdev->flags, stdev->thermal_base); | |
6a92c366 VF |
66 | |
67 | clk_disable(stdev->clk); | |
68 | dev_info(dev, "Suspended.\n"); | |
69 | ||
70 | return 0; | |
71 | } | |
72 | ||
d612c64d | 73 | static int __maybe_unused spear_thermal_resume(struct device *dev) |
6a92c366 | 74 | { |
3fc62efe | 75 | struct thermal_zone_device *spear_thermal = dev_get_drvdata(dev); |
6a92c366 VF |
76 | struct spear_thermal_dev *stdev = spear_thermal->devdata; |
77 | unsigned int actual_mask = 0; | |
78 | int ret = 0; | |
79 | ||
80 | ret = clk_enable(stdev->clk); | |
81 | if (ret) { | |
3fc62efe | 82 | dev_err(dev, "Can't enable clock\n"); |
6a92c366 VF |
83 | return ret; |
84 | } | |
85 | ||
86 | /* Enable SPEAr Thermal Sensor */ | |
de716e32 VK |
87 | actual_mask = readl_relaxed(stdev->thermal_base); |
88 | writel_relaxed(actual_mask | stdev->flags, stdev->thermal_base); | |
6a92c366 VF |
89 | |
90 | dev_info(dev, "Resumed.\n"); | |
91 | ||
92 | return 0; | |
93 | } | |
6a92c366 VF |
94 | |
95 | static SIMPLE_DEV_PM_OPS(spear_thermal_pm_ops, spear_thermal_suspend, | |
96 | spear_thermal_resume); | |
97 | ||
98 | static int spear_thermal_probe(struct platform_device *pdev) | |
99 | { | |
100 | struct thermal_zone_device *spear_thermal = NULL; | |
101 | struct spear_thermal_dev *stdev; | |
b9c7aff4 | 102 | struct device_node *np = pdev->dev.of_node; |
253e3ae1 | 103 | struct resource *res; |
b9c7aff4 VK |
104 | int ret = 0, val; |
105 | ||
106 | if (!np || !of_property_read_u32(np, "st,thermal-flags", &val)) { | |
107 | dev_err(&pdev->dev, "Failed: DT Pdata not passed\n"); | |
108 | return -EINVAL; | |
109 | } | |
6a92c366 | 110 | |
6a92c366 | 111 | stdev = devm_kzalloc(&pdev->dev, sizeof(*stdev), GFP_KERNEL); |
fa018d3e | 112 | if (!stdev) |
6a92c366 | 113 | return -ENOMEM; |
6a92c366 VF |
114 | |
115 | /* Enable thermal sensor */ | |
c21bec86 ZR |
116 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
117 | stdev->thermal_base = devm_ioremap_resource(&pdev->dev, res); | |
118 | if (IS_ERR(stdev->thermal_base)) | |
253e3ae1 | 119 | return PTR_ERR(stdev->thermal_base); |
6a92c366 | 120 | |
03b79bda | 121 | stdev->clk = devm_clk_get(&pdev->dev, NULL); |
6a92c366 VF |
122 | if (IS_ERR(stdev->clk)) { |
123 | dev_err(&pdev->dev, "Can't get clock\n"); | |
124 | return PTR_ERR(stdev->clk); | |
125 | } | |
126 | ||
127 | ret = clk_enable(stdev->clk); | |
128 | if (ret) { | |
129 | dev_err(&pdev->dev, "Can't enable clock\n"); | |
03b79bda | 130 | return ret; |
6a92c366 VF |
131 | } |
132 | ||
b9c7aff4 | 133 | stdev->flags = val; |
de716e32 | 134 | writel_relaxed(stdev->flags, stdev->thermal_base); |
6a92c366 | 135 | |
c56f5c03 | 136 | spear_thermal = thermal_zone_device_register("spear_thermal", 0, 0, |
50125a9b | 137 | stdev, &ops, NULL, 0, 0); |
03ee62f0 | 138 | if (IS_ERR(spear_thermal)) { |
6a92c366 | 139 | dev_err(&pdev->dev, "thermal zone device is NULL\n"); |
03ee62f0 | 140 | ret = PTR_ERR(spear_thermal); |
6a92c366 VF |
141 | goto disable_clk; |
142 | } | |
143 | ||
144 | platform_set_drvdata(pdev, spear_thermal); | |
145 | ||
146 | dev_info(&spear_thermal->device, "Thermal Sensor Loaded at: 0x%p.\n", | |
147 | stdev->thermal_base); | |
148 | ||
149 | return 0; | |
150 | ||
151 | disable_clk: | |
152 | clk_disable(stdev->clk); | |
6a92c366 VF |
153 | |
154 | return ret; | |
155 | } | |
156 | ||
157 | static int spear_thermal_exit(struct platform_device *pdev) | |
158 | { | |
159 | unsigned int actual_mask = 0; | |
160 | struct thermal_zone_device *spear_thermal = platform_get_drvdata(pdev); | |
161 | struct spear_thermal_dev *stdev = spear_thermal->devdata; | |
162 | ||
163 | thermal_zone_device_unregister(spear_thermal); | |
6a92c366 VF |
164 | |
165 | /* Disable SPEAr Thermal Sensor */ | |
de716e32 VK |
166 | actual_mask = readl_relaxed(stdev->thermal_base); |
167 | writel_relaxed(actual_mask & ~stdev->flags, stdev->thermal_base); | |
6a92c366 VF |
168 | |
169 | clk_disable(stdev->clk); | |
6a92c366 VF |
170 | |
171 | return 0; | |
172 | } | |
173 | ||
b9c7aff4 VK |
174 | static const struct of_device_id spear_thermal_id_table[] = { |
175 | { .compatible = "st,thermal-spear1340" }, | |
176 | {} | |
177 | }; | |
178 | MODULE_DEVICE_TABLE(of, spear_thermal_id_table); | |
179 | ||
6a92c366 VF |
180 | static struct platform_driver spear_thermal_driver = { |
181 | .probe = spear_thermal_probe, | |
182 | .remove = spear_thermal_exit, | |
183 | .driver = { | |
184 | .name = "spear_thermal", | |
6a92c366 | 185 | .pm = &spear_thermal_pm_ops, |
5454f211 | 186 | .of_match_table = spear_thermal_id_table, |
6a92c366 VF |
187 | }, |
188 | }; | |
189 | ||
190 | module_platform_driver(spear_thermal_driver); | |
191 | ||
192 | MODULE_AUTHOR("Vincenzo Frascino <vincenzo.frascino@st.com>"); | |
193 | MODULE_DESCRIPTION("SPEAr thermal driver"); | |
194 | MODULE_LICENSE("GPL"); |