2 * omap34xx_temp.c - Linux kernel module for OMAP34xx hardware monitoring
4 * Copyright (C) 2008 Nokia Corporation
6 * Written by Peter De Schrijver <peter.de-schrijver@nokia.com>
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.
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.
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
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>
32 #include <mach/omap34xx.h>
33 #include <mach/control.h>
35 #define TEMP_SENSOR_SOC BIT(8)
36 #define TEMP_SENSOR_EOCZ BIT(7)
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)
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)
46 /* minimum delay for EOCZ falling is
47 * 36 cycles of the 32.768Khz clock */
48 #define EOCZ_MIN_FALLING_DELAY (36 * 30518)
50 /* maximum delay for EOCZ falling is
51 * 40 cycles of the 32.768Khz clock */
52 #define EOCZ_MAX_FALLING_DELAY (40 * 30518)
54 struct omap34xx_data {
55 struct device *hwmon_dev;
57 struct mutex update_lock;
60 unsigned long last_updated;
64 static struct platform_device omap34xx_temp_device = {
65 .name = "omap34xx_temp",
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};
79 static inline u32 wait_for_eocz(int min_delay, int max_delay, u32 level)
81 struct timespec timeout;
86 level *= TEMP_SENSOR_EOCZ;
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);
92 temp_sensor_reg = omap_ctrl_readl(OMAP343X_CONTROL_TEMP_SENSOR);
93 if ((temp_sensor_reg & TEMP_SENSOR_EOCZ) == level)
95 } while (ktime_us_delta(expire, ktime_get()) > 0);
97 return (temp_sensor_reg & TEMP_SENSOR_EOCZ) == level;
100 static void omap34xx_update(struct omap34xx_data *data)
104 mutex_lock(&data->update_lock);
107 || time_after(jiffies, data->last_updated + HZ)) {
109 clk_enable(data->clk_32k);
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);
115 if (!wait_for_eocz(EOCZ_MIN_RISING_DELAY,
116 EOCZ_MAX_RISING_DELAY, 1))
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);
123 if (!wait_for_eocz(EOCZ_MIN_FALLING_DELAY,
124 EOCZ_MAX_FALLING_DELAY, 0))
127 data->temp = omap_ctrl_readl(OMAP343X_CONTROL_TEMP_SENSOR) &
129 data->last_updated = jiffies;
133 clk_disable(data->clk_32k);
136 mutex_unlock(&data->update_lock);
139 static ssize_t show_name(struct device *dev,
140 struct device_attribute *devattr, char *buf)
142 struct omap34xx_data *data = dev_get_drvdata(dev);
144 return sprintf(buf, "%s\n", data->name);
147 static ssize_t show_temp_raw(struct device *dev,
148 struct device_attribute *devattr, char *buf)
150 struct omap34xx_data *data = dev_get_drvdata(dev);
152 omap34xx_update(data);
154 return sprintf(buf, "%d\n", data->temp);
157 static ssize_t show_temp(struct device *dev,
158 struct device_attribute *devattr, char *buf)
160 struct omap34xx_data *data = dev_get_drvdata(dev);
162 omap34xx_update(data);
164 return sprintf(buf, "%d\n", adc_to_temp[data->temp]);
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,
170 static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
172 static int __devinit omap34xx_temp_probe(void)
175 struct omap34xx_data *data;
177 err = platform_device_register(&omap34xx_temp_device);
180 "Unable to register omap34xx temperature device\n");
184 data = kzalloc(sizeof(struct omap34xx_data), GFP_KERNEL);
190 dev_set_drvdata(&omap34xx_temp_device.dev, data);
191 mutex_init(&data->update_lock);
192 data->name = "omap34xx_temp";
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);
200 err = device_create_file(&omap34xx_temp_device.dev,
201 &sensor_dev_attr_temp1_input.dev_attr);
205 err = device_create_file(&omap34xx_temp_device.dev,
206 &sensor_dev_attr_temp1_input_raw.dev_attr);
210 err = device_create_file(&omap34xx_temp_device.dev, &dev_attr_name);
212 goto exit_remove_raw;
214 data->hwmon_dev = hwmon_device_register(&omap34xx_temp_device.dev);
216 if (IS_ERR(data->hwmon_dev)) {
217 err = PTR_ERR(data->hwmon_dev);
218 goto exit_remove_all;
224 device_remove_file(&omap34xx_temp_device.dev,
227 device_remove_file(&omap34xx_temp_device.dev,
228 &sensor_dev_attr_temp1_input_raw.dev_attr);
230 device_remove_file(&omap34xx_temp_device.dev,
231 &sensor_dev_attr_temp1_input.dev_attr);
233 clk_put(data->clk_32k);
238 platform_device_unregister(&omap34xx_temp_device);
243 static int __init omap34xx_temp_init(void)
245 return omap34xx_temp_probe();
248 static void __exit omap34xx_temp_exit(void)
250 struct omap34xx_data *data =
251 dev_get_drvdata(&omap34xx_temp_device.dev);
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);
259 platform_device_unregister(&omap34xx_temp_device);
262 MODULE_AUTHOR("Peter De Schrijver");
263 MODULE_DESCRIPTION("Omap34xx temperature sensor");
264 MODULE_LICENSE("GPL");
266 module_init(omap34xx_temp_init)
267 module_exit(omap34xx_temp_exit)