]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/hwmon/omap34xx_temp.c
18e07643165fef430b7631ad053a9011b44efb5f
[linux-2.6-omap-h63xx.git] / drivers / hwmon / omap34xx_temp.c
1 /*
2  * omap34xx_temp.c - Linux kernel module for OMAP34xx hardware monitoring
3  *
4  * Copyright (C) 2008 Nokia Corporation
5  *
6  * Written by Peter De Schrijver <peter.de-schrijver@nokia.com>
7  *
8  * Inspired by k8temp.c
9  *
10  * This file is subject to the terms and conditions of the GNU General
11  * Public License. See the file "COPYING" in the main directory of this
12  * archive for more details.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22  */
23
24 #include <linux/clk.h>
25 #include <linux/hrtimer.h>
26 #include <linux/module.h>
27 #include <linux/hwmon.h>
28 #include <linux/hwmon-sysfs.h>
29 #include <linux/err.h>
30 #include <linux/platform_device.h>
31 #include <linux/io.h>
32 #include <mach/omap34xx.h>
33 #include <mach/control.h>
34
35 #define TEMP_SENSOR_SOC BIT(8)
36 #define TEMP_SENSOR_EOCZ BIT(7)
37
38 /* minimum delay for EOCZ rise after SOC rise is
39  * 11 cycles of the 32.768Khz clock */
40 #define EOCZ_MIN_RISING_DELAY (11 * 30518)
41
42 /* maximum delay for EOCZ rise after SOC rise is
43  * 14 cycles of the 32.768Khz clock */
44 #define EOCZ_MAX_RISING_DELAY (14 * 30518)
45
46 /* minimum delay for EOCZ falling is
47  * 36 cycles of the 32.768Khz clock */
48 #define EOCZ_MIN_FALLING_DELAY (36 * 30518)
49
50 /* maximum delay for EOCZ falling is
51  * 40 cycles of the 32.768Khz clock */
52 #define EOCZ_MAX_FALLING_DELAY (40 * 30518)
53
54 struct omap34xx_data {
55         struct device *hwmon_dev;
56         struct clk *clk_32k;
57         struct mutex update_lock;
58         const char *name;
59         char valid;
60         unsigned long last_updated;
61         u32 temp;
62 };
63
64 static struct platform_device omap34xx_temp_device = {
65         .name   = "omap34xx_temp",
66         .id     = -1,
67 };
68
69 static int adc_to_temp[] = {
70         -40, -40, -40, -40, -40, -39, -38, -36, -34, -32, -31, -29, -28, -26,
71         -25, -24, -22, -21, -19, -18, -17, -15, -14, -12, -11, -9, -8, -7, -5,
72         -4, -2, -1, 0, 1, 3, 4, 5, 7, 8, 10, 11, 13, 14, 15, 17, 18, 20, 21,
73         22, 24, 25, 27, 28, 30, 31, 32, 34, 35, 37, 38, 39, 41, 42, 44, 45,
74         47, 48, 49, 51, 52, 53, 55, 56, 58, 59, 60, 62, 63, 65, 66, 67, 69,
75         70, 72, 73, 74, 76, 77, 79, 80, 81, 83, 84, 85, 87, 88, 89, 91, 92,
76         94, 95, 96, 98, 99, 100, 102, 103, 105, 106, 107, 109, 110, 111, 113,
77         114, 116, 117, 118, 120, 121, 122, 124, 124, 125, 125, 125, 125, 125};
78
79 static inline u32 wait_for_eocz(int min_delay, int max_delay, u32 level)
80 {
81         struct timespec timeout;
82         ktime_t expire;
83         u32 temp_sensor_reg;
84
85         level &= 1;
86         level *= TEMP_SENSOR_EOCZ;
87
88         expire = ktime_add_ns(ktime_get(), max_delay);
89         timeout = ns_to_timespec(min_delay);
90         hrtimer_nanosleep(&timeout, NULL, HRTIMER_MODE_REL, CLOCK_MONOTONIC);
91         do {
92                 temp_sensor_reg = omap_ctrl_readl(OMAP343X_CONTROL_TEMP_SENSOR);
93                 if ((temp_sensor_reg & TEMP_SENSOR_EOCZ) == level)
94                         break;
95         } while (ktime_us_delta(expire, ktime_get()) > 0);
96
97         return (temp_sensor_reg & TEMP_SENSOR_EOCZ) == level;
98 }
99
100 static void omap34xx_update(struct omap34xx_data *data)
101 {
102         u32 temp_sensor_reg;
103
104         mutex_lock(&data->update_lock);
105
106         if (!data->valid
107             || time_after(jiffies, data->last_updated + HZ)) {
108
109                 clk_enable(data->clk_32k);
110
111                 temp_sensor_reg = omap_ctrl_readl(OMAP343X_CONTROL_TEMP_SENSOR);
112                 temp_sensor_reg |= TEMP_SENSOR_SOC;
113                 omap_ctrl_writel(temp_sensor_reg, OMAP343X_CONTROL_TEMP_SENSOR);
114
115                 if (!wait_for_eocz(EOCZ_MIN_RISING_DELAY,
116                                         EOCZ_MAX_RISING_DELAY, 1))
117                         goto err;
118
119                 temp_sensor_reg = omap_ctrl_readl(OMAP343X_CONTROL_TEMP_SENSOR);
120                 temp_sensor_reg &= ~TEMP_SENSOR_SOC;
121                 omap_ctrl_writel(temp_sensor_reg, OMAP343X_CONTROL_TEMP_SENSOR);
122
123                 if (!wait_for_eocz(EOCZ_MIN_FALLING_DELAY,
124                                         EOCZ_MAX_FALLING_DELAY, 0))
125                         goto err;
126
127                 data->temp = omap_ctrl_readl(OMAP343X_CONTROL_TEMP_SENSOR) &
128                                                 ((1<<7) - 1);
129                 data->last_updated = jiffies;
130                 data->valid = 1;
131
132 err:
133                 clk_disable(data->clk_32k);
134         }
135
136         mutex_unlock(&data->update_lock);
137 }
138
139 static ssize_t show_name(struct device *dev,
140                         struct device_attribute *devattr, char *buf)
141 {
142         struct omap34xx_data *data = dev_get_drvdata(dev);
143
144         return sprintf(buf, "%s\n", data->name);
145 }
146
147 static ssize_t show_temp_raw(struct device *dev,
148                          struct device_attribute *devattr, char *buf)
149 {
150         struct omap34xx_data *data = dev_get_drvdata(dev);
151
152         omap34xx_update(data);
153
154         return sprintf(buf, "%d\n", data->temp);
155 }
156
157 static ssize_t show_temp(struct device *dev,
158                          struct device_attribute *devattr, char *buf)
159 {
160         struct omap34xx_data *data = dev_get_drvdata(dev);
161
162         omap34xx_update(data);
163
164         return sprintf(buf, "%d\n", adc_to_temp[data->temp]);
165 }
166
167 static SENSOR_DEVICE_ATTR_2(temp1_input, S_IRUGO, show_temp, NULL, 0, 0);
168 static SENSOR_DEVICE_ATTR_2(temp1_input_raw, S_IRUGO, show_temp_raw,
169                                 NULL, 0, 0);
170 static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
171
172 static int __devinit omap34xx_temp_probe(void)
173 {
174         int err;
175         struct omap34xx_data *data;
176
177         err = platform_device_register(&omap34xx_temp_device);
178         if (err) {
179                 printk(KERN_ERR
180                         "Unable to register omap34xx temperature device\n");
181                 goto exit;
182         }
183
184         data = kzalloc(sizeof(struct omap34xx_data), GFP_KERNEL);
185         if (!data) {
186                 err = -ENOMEM;
187                 goto exit_platform;
188         }
189
190         dev_set_drvdata(&omap34xx_temp_device.dev, data);
191         mutex_init(&data->update_lock);
192         data->name = "omap34xx_temp";
193
194         data->clk_32k = clk_get(&omap34xx_temp_device.dev, "ts_fck");
195         if (IS_ERR(data->clk_32k)) {
196                 err = PTR_ERR(data->clk_32k);
197                 goto exit_free;
198         }
199
200         err = device_create_file(&omap34xx_temp_device.dev,
201                                  &sensor_dev_attr_temp1_input.dev_attr);
202         if (err)
203                 goto clock_free;
204
205         err = device_create_file(&omap34xx_temp_device.dev,
206                                  &sensor_dev_attr_temp1_input_raw.dev_attr);
207         if (err)
208                 goto exit_remove;
209
210         err = device_create_file(&omap34xx_temp_device.dev, &dev_attr_name);
211         if (err)
212                 goto exit_remove_raw;
213
214         data->hwmon_dev = hwmon_device_register(&omap34xx_temp_device.dev);
215
216         if (IS_ERR(data->hwmon_dev)) {
217                 err = PTR_ERR(data->hwmon_dev);
218                 goto exit_remove_all;
219         }
220
221         return 0;
222
223 exit_remove_all:
224         device_remove_file(&omap34xx_temp_device.dev,
225                            &dev_attr_name);
226 exit_remove_raw:
227         device_remove_file(&omap34xx_temp_device.dev,
228                            &sensor_dev_attr_temp1_input_raw.dev_attr);
229 exit_remove:
230         device_remove_file(&omap34xx_temp_device.dev,
231                            &sensor_dev_attr_temp1_input.dev_attr);
232 clock_free:
233         clk_put(data->clk_32k);
234
235 exit_free:
236         kfree(data);
237 exit_platform:
238         platform_device_unregister(&omap34xx_temp_device);
239 exit:
240         return err;
241 }
242
243 static int __init omap34xx_temp_init(void)
244 {
245         return omap34xx_temp_probe();
246 }
247
248 static void __exit omap34xx_temp_exit(void)
249 {
250         struct omap34xx_data *data =
251                         dev_get_drvdata(&omap34xx_temp_device.dev);
252
253         clk_put(data->clk_32k);
254         hwmon_device_unregister(data->hwmon_dev);
255         device_remove_file(&omap34xx_temp_device.dev,
256                            &sensor_dev_attr_temp1_input.dev_attr);
257         device_remove_file(&omap34xx_temp_device.dev, &dev_attr_name);
258         kfree(data);
259         platform_device_unregister(&omap34xx_temp_device);
260 }
261
262 MODULE_AUTHOR("Peter De Schrijver");
263 MODULE_DESCRIPTION("Omap34xx temperature sensor");
264 MODULE_LICENSE("GPL");
265
266 module_init(omap34xx_temp_init)
267 module_exit(omap34xx_temp_exit)
268