2 * drivers/hwmon/tsc210x_sensors.c
4 * hwmon interface for TSC210X sensors
6 * Copyright (c) 2005-2007 Andrzej Zaborowski <balrog@zabor.org>
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.
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.
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
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>
30 # include <linux/apm-emulation.h>
33 #include <linux/spi/tsc210x.h>
35 struct tsc210x_hwmon {
36 int bat[2], aux[2], temp[2];
38 struct class_device *dev;
39 struct tsc210x_config *pdata;
46 # define apm_lock() spin_lock(&hwmon->apm_lock)
47 # define apm_unlock() spin_unlock(&hwmon->apm_lock)
53 static void tsc210x_ports(struct tsc210x_hwmon *hwmon, int bat[], int aux[])
56 hwmon->bat[0] = bat[0];
57 hwmon->bat[1] = bat[1];
58 hwmon->aux[0] = aux[0];
59 hwmon->aux[1] = aux[1];
63 static void tsc210x_temp1(struct tsc210x_hwmon *hwmon, int temp)
66 hwmon->temp[0] = temp;
70 static void tsc210x_temp2(struct tsc210x_hwmon *hwmon, int temp)
73 hwmon->temp[1] = temp;
77 #define TSC210X_INPUT(devname, field) \
78 static ssize_t tsc_show_ ## devname(struct device *dev, \
79 struct device_attribute *devattr, char *buf) \
81 struct tsc210x_hwmon *hwmon = (struct tsc210x_hwmon *) \
82 platform_get_drvdata(to_platform_device(dev)); \
83 return sprintf(buf, "%i\n", hwmon->field); \
85 static DEVICE_ATTR(devname ## _input, S_IRUGO, tsc_show_ ## devname, NULL);
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])
94 static ssize_t tsc_show_temp1(struct device *dev,
95 struct device_attribute *devattr, char *buf)
97 struct tsc210x_hwmon *hwmon = (struct tsc210x_hwmon *)
98 platform_get_drvdata(to_platform_device(dev));
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.
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.
116 diff = hwmon->pdata->temp_at25c[1] - hwmon->pdata->temp_at25c[0];
118 value = (t2 - t1) * 298150 / diff; /* This is in Kelvins now */
120 value -= 273150; /* Celcius millidegree */
121 return sprintf(buf, "%i\n", value);
123 static DEVICE_ATTR(temp1_input, S_IRUGO, tsc_show_temp1, NULL);
126 static struct tsc210x_hwmon *apm_hwmon;
128 static void tsc210x_get_power_status(struct apm_power_info *info)
130 struct tsc210x_hwmon *hwmon = apm_hwmon;
132 hwmon->pdata->apm_report(info, hwmon->bat);
137 static int tsc210x_hwmon_probe(struct platform_device *pdev)
139 struct tsc210x_hwmon *hwmon;
140 struct tsc210x_config *pdata = pdev->dev.platform_data;
143 hwmon = (struct tsc210x_hwmon *)
144 kzalloc(sizeof(struct tsc210x_hwmon), GFP_KERNEL);
146 printk(KERN_ERR "%s: allocation failed\n", __FUNCTION__);
150 hwmon->dev = hwmon_device_register(&pdev->dev);
151 if (IS_ERR(hwmon->dev)) {
153 printk(KERN_ERR "%s: Class registration failed\n",
155 return PTR_ERR(hwmon->dev);
158 hwmon->pdata = pdata;
161 spin_lock_init(&hwmon->apm_lock);
163 if (pdata->apm_report) {
165 apm_get_power_status = tsc210x_get_power_status;
169 platform_set_drvdata(pdev, hwmon);
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);
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);
187 if (pdata->apm_report)
188 apm_get_power_status = 0;
190 hwmon_device_unregister(hwmon->dev);
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);
208 if (status) /* Not fatal */
209 printk(KERN_ERR "%s: Creating one or more "
210 "attribute files failed\n", __FUNCTION__);
215 static int tsc210x_hwmon_remove(struct platform_device *pdev)
217 struct tsc210x_hwmon *dev = platform_get_drvdata(pdev);
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);
224 if (dev->pdata->apm_report)
225 apm_get_power_status = 0;
227 hwmon_device_unregister(dev->dev);
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 */
237 .name = "tsc210x-hwmon",
241 static int __init tsc210x_hwmon_init(void)
243 return platform_driver_register(&tsc210x_hwmon_driver);
246 static void __exit tsc210x_hwmon_exit(void)
248 platform_driver_unregister(&tsc210x_hwmon_driver);
251 module_init(tsc210x_hwmon_init);
252 module_exit(tsc210x_hwmon_exit);
254 MODULE_AUTHOR("Andrzej Zaborowski");
255 MODULE_DESCRIPTION("hwmon driver for TI TSC210x-connected sensors.");
256 MODULE_LICENSE("GPL");