]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/hwmon/tsc210x_sensors.c
TSC210x driver using the SPI framework.
[linux-2.6-omap-h63xx.git] / drivers / hwmon / tsc210x_sensors.c
1 /*
2  * drivers/hwmon/tsc210x_sensors.c
3  *
4  * hwmon interface for TSC210X sensors
5  *
6  * Copyright (c) 2005-2007 Andrzej Zaborowski  <balrog@zabor.org>
7  *
8  * This package is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This package is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this package; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
21  */
22 #include <linux/init.h>
23 #include <linux/err.h>
24 #include <linux/hwmon.h>
25 #include <linux/hwmon-sysfs.h>
26 #include <linux/platform_device.h>
27 #include <linux/spinlock.h>
28 #include <linux/autoconf.h>
29 #ifdef CONFIG_APM
30 # include <linux/apm-emulation.h>
31 #endif
32
33 #include <linux/spi/tsc210x.h>
34
35 struct tsc210x_hwmon {
36         int bat[2], aux[2], temp[2];
37
38         struct class_device *dev;
39         struct tsc210x_config *pdata;
40 #ifdef CONFIG_APM
41         spinlock_t apm_lock;
42 #endif
43 };
44
45 #ifdef CONFIG_APM
46 # define apm_lock()     spin_lock(&hwmon->apm_lock)
47 # define apm_unlock()   spin_unlock(&hwmon->apm_lock)
48 #else
49 # define apm_lock()
50 # define apm_unlock()
51 #endif
52
53 static void tsc210x_ports(struct tsc210x_hwmon *hwmon, int bat[], int aux[])
54 {
55         apm_lock();
56         hwmon->bat[0] = bat[0];
57         hwmon->bat[1] = bat[1];
58         hwmon->aux[0] = aux[0];
59         hwmon->aux[1] = aux[1];
60         apm_unlock();
61 }
62
63 static void tsc210x_temp1(struct tsc210x_hwmon *hwmon, int temp)
64 {
65         apm_lock();
66         hwmon->temp[0] = temp;
67         apm_unlock();
68 }
69
70 static void tsc210x_temp2(struct tsc210x_hwmon *hwmon, int temp)
71 {
72         apm_lock();
73         hwmon->temp[1] = temp;
74         apm_unlock();
75 }
76
77 #define TSC210X_INPUT(devname, field)   \
78 static ssize_t tsc_show_ ## devname(struct device *dev, \
79                 struct device_attribute *devattr, char *buf)    \
80 {       \
81         struct tsc210x_hwmon *hwmon = (struct tsc210x_hwmon *)  \
82                 platform_get_drvdata(to_platform_device(dev));  \
83         return sprintf(buf, "%i\n", hwmon->field);      \
84 }       \
85 static DEVICE_ATTR(devname ## _input, S_IRUGO, tsc_show_ ## devname, NULL);
86
87 TSC210X_INPUT(in0, bat[0])
88 TSC210X_INPUT(in1, bat[1])
89 TSC210X_INPUT(in2, aux[0])
90 TSC210X_INPUT(in3, aux[1])
91 TSC210X_INPUT(in4, temp[0])
92 TSC210X_INPUT(in5, temp[1])
93
94 static ssize_t tsc_show_temp1(struct device *dev,
95                 struct device_attribute *devattr, char *buf)
96 {
97         struct tsc210x_hwmon *hwmon = (struct tsc210x_hwmon *)
98                 platform_get_drvdata(to_platform_device(dev));
99         int t1, t2;
100         int diff, value;
101
102         t1 = hwmon->temp[0];
103         t2 = hwmon->temp[1];
104
105         /*
106          * Use method #2 (differential) to calculate current temperature.
107          * The difference between TEMP2 and TEMP1 input values is
108          * multiplied by a constant to obtain current temperature.
109          * To find this constant we use the values measured at 25 C as
110          * thermometer calibration data.
111          *
112          * 298150 is 25 degrees Celcius represented in Kelvins and
113          * multiplied by 1000 for fixed point precision (273.15 + 25).
114          * 273150 is zero degrees Celcius.
115          */
116         diff = hwmon->pdata->temp_at25c[1] - hwmon->pdata->temp_at25c[0];
117         BUG_ON(diff == 0);
118         value = (t2 - t1) * 298150 / diff;      /* This is in Kelvins now */
119
120         value -= 273150;                        /* Celcius millidegree */
121         return sprintf(buf, "%i\n", value);
122 }
123 static DEVICE_ATTR(temp1_input, S_IRUGO, tsc_show_temp1, NULL);
124
125 #ifdef CONFIG_APM
126 static struct tsc210x_hwmon *apm_hwmon;
127
128 static void tsc210x_get_power_status(struct apm_power_info *info)
129 {
130         struct tsc210x_hwmon *hwmon = apm_hwmon;
131         apm_lock();
132         hwmon->pdata->apm_report(info, hwmon->bat);
133         apm_unlock();
134 }
135 #endif
136
137 static int tsc210x_hwmon_probe(struct platform_device *pdev)
138 {
139         struct tsc210x_hwmon *hwmon;
140         struct tsc210x_config *pdata = pdev->dev.platform_data;
141         int status = 0;
142
143         hwmon = (struct tsc210x_hwmon *)
144                 kzalloc(sizeof(struct tsc210x_hwmon), GFP_KERNEL);
145         if (!hwmon) {
146                 printk(KERN_ERR "%s: allocation failed\n", __FUNCTION__);
147                 return -ENOMEM;
148         }
149
150         hwmon->dev = hwmon_device_register(&pdev->dev);
151         if (IS_ERR(hwmon->dev)) {
152                 kfree(hwmon);
153                 printk(KERN_ERR "%s: Class registration failed\n",
154                                 __FUNCTION__);
155                 return PTR_ERR(hwmon->dev);
156         }
157
158         hwmon->pdata = pdata;
159
160 #ifdef CONFIG_APM
161         spin_lock_init(&hwmon->apm_lock);
162
163         if (pdata->apm_report) {
164                 apm_hwmon = hwmon;
165                 apm_get_power_status = tsc210x_get_power_status;
166         }
167 #endif
168
169         platform_set_drvdata(pdev, hwmon);
170
171         if (pdata->monitor & (TSC_BAT1 | TSC_BAT2 | TSC_AUX1 | TSC_AUX2))
172                 status |= tsc210x_ports_cb(pdev->dev.parent,
173                                 (tsc210x_ports_t) tsc210x_ports, hwmon);
174         if (pdata->monitor & TSC_TEMP) {
175                 status |= tsc210x_temp1_cb(pdev->dev.parent,
176                                 (tsc210x_temp_t) tsc210x_temp1, hwmon);
177                 status |= tsc210x_temp2_cb(pdev->dev.parent,
178                                 (tsc210x_temp_t) tsc210x_temp2, hwmon);
179         }
180
181         if (status) {
182                 tsc210x_ports_cb(pdev->dev.parent, 0, 0);
183                 tsc210x_temp1_cb(pdev->dev.parent, 0, 0);
184                 tsc210x_temp2_cb(pdev->dev.parent, 0, 0);
185                 platform_set_drvdata(pdev, 0);
186 #ifdef CONFIG_APM
187                 if (pdata->apm_report)
188                         apm_get_power_status = 0;
189 #endif
190                 hwmon_device_unregister(hwmon->dev);
191                 kfree(hwmon);
192                 return status;
193         }
194
195         if (pdata->monitor & TSC_BAT1)
196                 status |= device_create_file(&pdev->dev, &dev_attr_in0_input);
197         if (pdata->monitor & TSC_BAT2)
198                 status |= device_create_file(&pdev->dev, &dev_attr_in1_input);
199         if (pdata->monitor & TSC_AUX1)
200                 status |= device_create_file(&pdev->dev, &dev_attr_in2_input);
201         if (pdata->monitor & TSC_AUX2)
202                 status |= device_create_file(&pdev->dev, &dev_attr_in3_input);
203         if (pdata->monitor & TSC_TEMP) {
204                 status |= device_create_file(&pdev->dev, &dev_attr_in4_input);
205                 status |= device_create_file(&pdev->dev, &dev_attr_in5_input);
206                 status |= device_create_file(&pdev->dev, &dev_attr_temp1_input);
207         }
208         if (status)     /* Not fatal */
209                 printk(KERN_ERR "%s: Creating one or more "
210                                 "attribute files failed\n", __FUNCTION__);
211
212         return 0;
213 }
214
215 static int tsc210x_hwmon_remove(struct platform_device *pdev)
216 {
217         struct tsc210x_hwmon *dev = platform_get_drvdata(pdev);
218
219         tsc210x_ports_cb(pdev->dev.parent, 0, 0);
220         tsc210x_temp1_cb(pdev->dev.parent, 0, 0);
221         tsc210x_temp2_cb(pdev->dev.parent, 0, 0);
222         platform_set_drvdata(pdev, 0);
223 #ifdef CONFIG_APM
224         if (dev->pdata->apm_report)
225                 apm_get_power_status = 0;
226 #endif
227         hwmon_device_unregister(dev->dev);
228         kfree(dev);
229         return 0;
230 }
231
232 static struct platform_driver tsc210x_hwmon_driver = {
233         .probe          = tsc210x_hwmon_probe,
234         .remove         = tsc210x_hwmon_remove,
235         /* Nothing to do on suspend/resume */
236         .driver         = {
237                 .name   = "tsc210x-hwmon",
238         },
239 };
240
241 static int __init tsc210x_hwmon_init(void)
242 {
243         return platform_driver_register(&tsc210x_hwmon_driver);
244 }
245
246 static void __exit tsc210x_hwmon_exit(void)
247 {
248         platform_driver_unregister(&tsc210x_hwmon_driver);
249 }
250
251 module_init(tsc210x_hwmon_init);
252 module_exit(tsc210x_hwmon_exit);
253
254 MODULE_AUTHOR("Andrzej Zaborowski");
255 MODULE_DESCRIPTION("hwmon driver for TI TSC210x-connected sensors.");
256 MODULE_LICENSE("GPL");