blob: 3e63eaf19041edec1d92ee2207d9f204992bf9b9 [file] [log] [blame]
Roger Lucas1de9e372005-11-26 20:20:05 +01001/*
2 vt8231.c - Part of lm_sensors, Linux kernel modules
3 for hardware monitoring
4
5 Copyright (c) 2005 Roger Lucas <roger@planbit.co.uk>
6 Copyright (c) 2002 Mark D. Studebaker <mdsxyz123@yahoo.com>
7 Aaron M. Marsh <amarsh@sdf.lonestar.org>
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
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., 675 Mass Ave, Cambridge, MA 02139, USA.
22*/
23
24/* Supports VIA VT8231 South Bridge embedded sensors
25*/
26
27#include <linux/module.h>
28#include <linux/init.h>
29#include <linux/slab.h>
30#include <linux/pci.h>
31#include <linux/jiffies.h>
Roger Lucasec5e1a42007-06-12 21:04:08 +020032#include <linux/platform_device.h>
Roger Lucas1de9e372005-11-26 20:20:05 +010033#include <linux/hwmon.h>
34#include <linux/hwmon-sysfs.h>
35#include <linux/hwmon-vid.h>
36#include <linux/err.h>
Ingo Molnar9a61bf62006-01-18 23:19:26 +010037#include <linux/mutex.h>
Roger Lucas1de9e372005-11-26 20:20:05 +010038#include <asm/io.h>
39
40static int force_addr;
41module_param(force_addr, int, 0);
42MODULE_PARM_DESC(force_addr, "Initialize the base address of the sensors");
43
Roger Lucasec5e1a42007-06-12 21:04:08 +020044static struct platform_device *pdev;
Roger Lucas1de9e372005-11-26 20:20:05 +010045
46#define VT8231_EXTENT 0x80
47#define VT8231_BASE_REG 0x70
48#define VT8231_ENABLE_REG 0x74
49
50/* The VT8231 registers
51
52 The reset value for the input channel configuration is used (Reg 0x4A=0x07)
53 which sets the selected inputs marked with '*' below if multiple options are
54 possible:
55
56 Voltage Mode Temperature Mode
57 Sensor Linux Id Linux Id VIA Id
58 -------- -------- -------- ------
59 CPU Diode N/A temp1 0
60 UIC1 in0 temp2 * 1
61 UIC2 in1 * temp3 2
62 UIC3 in2 * temp4 3
63 UIC4 in3 * temp5 4
64 UIC5 in4 * temp6 5
65 3.3V in5 N/A
66
67 Note that the BIOS may set the configuration register to a different value
68 to match the motherboard configuration.
69*/
70
71/* fans numbered 0-1 */
72#define VT8231_REG_FAN_MIN(nr) (0x3b + (nr))
73#define VT8231_REG_FAN(nr) (0x29 + (nr))
74
75/* Voltage inputs numbered 0-5 */
76
77static const u8 regvolt[] = { 0x21, 0x22, 0x23, 0x24, 0x25, 0x26 };
78static const u8 regvoltmax[] = { 0x3d, 0x2b, 0x2d, 0x2f, 0x31, 0x33 };
79static const u8 regvoltmin[] = { 0x3e, 0x2c, 0x2e, 0x30, 0x32, 0x34 };
80
81/* Temperatures are numbered 1-6 according to the Linux kernel specification.
82**
83** In the VIA datasheet, however, the temperatures are numbered from zero.
84** Since it is important that this driver can easily be compared to the VIA
85** datasheet, we will use the VIA numbering within this driver and map the
86** kernel sysfs device name to the VIA number in the sysfs callback.
87*/
88
89#define VT8231_REG_TEMP_LOW01 0x49
90#define VT8231_REG_TEMP_LOW25 0x4d
91
92static const u8 regtemp[] = { 0x1f, 0x21, 0x22, 0x23, 0x24, 0x25 };
93static const u8 regtempmax[] = { 0x39, 0x3d, 0x2b, 0x2d, 0x2f, 0x31 };
94static const u8 regtempmin[] = { 0x3a, 0x3e, 0x2c, 0x2e, 0x30, 0x32 };
95
96#define TEMP_FROM_REG(reg) (((253 * 4 - (reg)) * 550 + 105) / 210)
97#define TEMP_MAXMIN_FROM_REG(reg) (((253 - (reg)) * 2200 + 105) / 210)
98#define TEMP_MAXMIN_TO_REG(val) (253 - ((val) * 210 + 1100) / 2200)
99
100#define VT8231_REG_CONFIG 0x40
101#define VT8231_REG_ALARM1 0x41
102#define VT8231_REG_ALARM2 0x42
103#define VT8231_REG_FANDIV 0x47
104#define VT8231_REG_UCH_CONFIG 0x4a
105#define VT8231_REG_TEMP1_CONFIG 0x4b
106#define VT8231_REG_TEMP2_CONFIG 0x4c
107
108/* temps 0-5 as numbered in VIA datasheet - see later for mapping to Linux
109** numbering
110*/
111#define ISTEMP(i, ch_config) ((i) == 0 ? 1 : \
112 ((ch_config) >> ((i)+1)) & 0x01)
113/* voltages 0-5 */
114#define ISVOLT(i, ch_config) ((i) == 5 ? 1 : \
115 !(((ch_config) >> ((i)+2)) & 0x01))
116
117#define DIV_FROM_REG(val) (1 << (val))
118
119/* NB The values returned here are NOT temperatures. The calibration curves
120** for the thermistor curves are board-specific and must go in the
121** sensors.conf file. Temperature sensors are actually ten bits, but the
122** VIA datasheet only considers the 8 MSBs obtained from the regtemp[]
123** register. The temperature value returned should have a magnitude of 3,
124** so we use the VIA scaling as the "true" scaling and use the remaining 2
125** LSBs as fractional precision.
126**
127** All the on-chip hardware temperature comparisons for the alarms are only
128** 8-bits wide, and compare against the 8 MSBs of the temperature. The bits
129** in the registers VT8231_REG_TEMP_LOW01 and VT8231_REG_TEMP_LOW25 are
130** ignored.
131*/
132
133/******** FAN RPM CONVERSIONS ********
134** This chip saturates back at 0, not at 255 like many the other chips.
135** So, 0 means 0 RPM
136*/
137static inline u8 FAN_TO_REG(long rpm, int div)
138{
139 if (rpm == 0)
140 return 0;
141 return SENSORS_LIMIT(1310720 / (rpm * div), 1, 255);
142}
143
144#define FAN_FROM_REG(val, div) ((val) == 0 ? 0 : 1310720 / ((val) * (div)))
145
146struct vt8231_data {
Roger Lucasec5e1a42007-06-12 21:04:08 +0200147 unsigned short addr;
148 const char *name;
149
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100150 struct mutex update_lock;
Roger Lucas1de9e372005-11-26 20:20:05 +0100151 struct class_device *class_dev;
152 char valid; /* !=0 if following fields are valid */
153 unsigned long last_updated; /* In jiffies */
154
155 u8 in[6]; /* Register value */
156 u8 in_max[6]; /* Register value */
157 u8 in_min[6]; /* Register value */
158 u16 temp[6]; /* Register value 10 bit, right aligned */
159 u8 temp_max[6]; /* Register value */
160 u8 temp_min[6]; /* Register value */
161 u8 fan[2]; /* Register value */
162 u8 fan_min[2]; /* Register value */
163 u8 fan_div[2]; /* Register encoding, shifted right */
164 u16 alarms; /* Register encoding */
165 u8 uch_config;
166};
167
168static struct pci_dev *s_bridge;
Roger Lucasec5e1a42007-06-12 21:04:08 +0200169static int vt8231_probe(struct platform_device *pdev);
Jean Delvared0546122007-07-22 12:09:48 +0200170static int __devexit vt8231_remove(struct platform_device *pdev);
Roger Lucas1de9e372005-11-26 20:20:05 +0100171static struct vt8231_data *vt8231_update_device(struct device *dev);
Roger Lucasec5e1a42007-06-12 21:04:08 +0200172static void vt8231_init_device(struct vt8231_data *data);
Roger Lucas1de9e372005-11-26 20:20:05 +0100173
Roger Lucasec5e1a42007-06-12 21:04:08 +0200174static inline int vt8231_read_value(struct vt8231_data *data, u8 reg)
Roger Lucas1de9e372005-11-26 20:20:05 +0100175{
Roger Lucasec5e1a42007-06-12 21:04:08 +0200176 return inb_p(data->addr + reg);
Roger Lucas1de9e372005-11-26 20:20:05 +0100177}
178
Roger Lucasec5e1a42007-06-12 21:04:08 +0200179static inline void vt8231_write_value(struct vt8231_data *data, u8 reg,
Roger Lucas1de9e372005-11-26 20:20:05 +0100180 u8 value)
181{
Roger Lucasec5e1a42007-06-12 21:04:08 +0200182 outb_p(value, data->addr + reg);
Roger Lucas1de9e372005-11-26 20:20:05 +0100183}
184
185/* following are the sysfs callback functions */
186static ssize_t show_in(struct device *dev, struct device_attribute *attr,
187 char *buf)
188{
189 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
190 int nr = sensor_attr->index;
191 struct vt8231_data *data = vt8231_update_device(dev);
192
193 return sprintf(buf, "%d\n", ((data->in[nr] - 3) * 10000) / 958);
194}
195
196static ssize_t show_in_min(struct device *dev, struct device_attribute *attr,
197 char *buf)
198{
199 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
200 int nr = sensor_attr->index;
201 struct vt8231_data *data = vt8231_update_device(dev);
202
203 return sprintf(buf, "%d\n", ((data->in_min[nr] - 3) * 10000) / 958);
204}
205
206static ssize_t show_in_max(struct device *dev, struct device_attribute *attr,
207 char *buf)
208{
209 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
210 int nr = sensor_attr->index;
211 struct vt8231_data *data = vt8231_update_device(dev);
212
213 return sprintf(buf, "%d\n", (((data->in_max[nr] - 3) * 10000) / 958));
214}
215
216static ssize_t set_in_min(struct device *dev, struct device_attribute *attr,
217 const char *buf, size_t count)
218{
219 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
220 int nr = sensor_attr->index;
Roger Lucasec5e1a42007-06-12 21:04:08 +0200221 struct vt8231_data *data = dev_get_drvdata(dev);
Roger Lucas1de9e372005-11-26 20:20:05 +0100222 unsigned long val = simple_strtoul(buf, NULL, 10);
223
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100224 mutex_lock(&data->update_lock);
Roger Lucas1de9e372005-11-26 20:20:05 +0100225 data->in_min[nr] = SENSORS_LIMIT(((val * 958) / 10000) + 3, 0, 255);
Roger Lucasec5e1a42007-06-12 21:04:08 +0200226 vt8231_write_value(data, regvoltmin[nr], data->in_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100227 mutex_unlock(&data->update_lock);
Roger Lucas1de9e372005-11-26 20:20:05 +0100228 return count;
229}
230
231static ssize_t set_in_max(struct device *dev, struct device_attribute *attr,
232 const char *buf, size_t count)
233{
234 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
235 int nr = sensor_attr->index;
Roger Lucasec5e1a42007-06-12 21:04:08 +0200236 struct vt8231_data *data = dev_get_drvdata(dev);
Roger Lucas1de9e372005-11-26 20:20:05 +0100237 unsigned long val = simple_strtoul(buf, NULL, 10);
238
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100239 mutex_lock(&data->update_lock);
Roger Lucas1de9e372005-11-26 20:20:05 +0100240 data->in_max[nr] = SENSORS_LIMIT(((val * 958) / 10000) + 3, 0, 255);
Roger Lucasec5e1a42007-06-12 21:04:08 +0200241 vt8231_write_value(data, regvoltmax[nr], data->in_max[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100242 mutex_unlock(&data->update_lock);
Roger Lucas1de9e372005-11-26 20:20:05 +0100243 return count;
244}
245
246/* Special case for input 5 as this has 3.3V scaling built into the chip */
247static ssize_t show_in5(struct device *dev, struct device_attribute *attr,
248 char *buf)
249{
250 struct vt8231_data *data = vt8231_update_device(dev);
251
252 return sprintf(buf, "%d\n",
253 (((data->in[5] - 3) * 10000 * 54) / (958 * 34)));
254}
255
256static ssize_t show_in5_min(struct device *dev, struct device_attribute *attr,
257 char *buf)
258{
259 struct vt8231_data *data = vt8231_update_device(dev);
260
261 return sprintf(buf, "%d\n",
262 (((data->in_min[5] - 3) * 10000 * 54) / (958 * 34)));
263}
264
265static ssize_t show_in5_max(struct device *dev, struct device_attribute *attr,
266 char *buf)
267{
268 struct vt8231_data *data = vt8231_update_device(dev);
269
270 return sprintf(buf, "%d\n",
271 (((data->in_max[5] - 3) * 10000 * 54) / (958 * 34)));
272}
273
274static ssize_t set_in5_min(struct device *dev, struct device_attribute *attr,
275 const char *buf, size_t count)
276{
Roger Lucasec5e1a42007-06-12 21:04:08 +0200277 struct vt8231_data *data = dev_get_drvdata(dev);
Roger Lucas1de9e372005-11-26 20:20:05 +0100278 unsigned long val = simple_strtoul(buf, NULL, 10);
279
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100280 mutex_lock(&data->update_lock);
Roger Lucas1de9e372005-11-26 20:20:05 +0100281 data->in_min[5] = SENSORS_LIMIT(((val * 958 * 34) / (10000 * 54)) + 3,
282 0, 255);
Roger Lucasec5e1a42007-06-12 21:04:08 +0200283 vt8231_write_value(data, regvoltmin[5], data->in_min[5]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100284 mutex_unlock(&data->update_lock);
Roger Lucas1de9e372005-11-26 20:20:05 +0100285 return count;
286}
287
288static ssize_t set_in5_max(struct device *dev, struct device_attribute *attr,
289 const char *buf, size_t count)
290{
Roger Lucasec5e1a42007-06-12 21:04:08 +0200291 struct vt8231_data *data = dev_get_drvdata(dev);
Roger Lucas1de9e372005-11-26 20:20:05 +0100292 unsigned long val = simple_strtoul(buf, NULL, 10);
293
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100294 mutex_lock(&data->update_lock);
Roger Lucas1de9e372005-11-26 20:20:05 +0100295 data->in_max[5] = SENSORS_LIMIT(((val * 958 * 34) / (10000 * 54)) + 3,
296 0, 255);
Roger Lucasec5e1a42007-06-12 21:04:08 +0200297 vt8231_write_value(data, regvoltmax[5], data->in_max[5]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100298 mutex_unlock(&data->update_lock);
Roger Lucas1de9e372005-11-26 20:20:05 +0100299 return count;
300}
301
302#define define_voltage_sysfs(offset) \
303static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \
304 show_in, NULL, offset); \
305static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
306 show_in_min, set_in_min, offset); \
307static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
308 show_in_max, set_in_max, offset)
309
310define_voltage_sysfs(0);
311define_voltage_sysfs(1);
312define_voltage_sysfs(2);
313define_voltage_sysfs(3);
314define_voltage_sysfs(4);
315
316static DEVICE_ATTR(in5_input, S_IRUGO, show_in5, NULL);
317static DEVICE_ATTR(in5_min, S_IRUGO | S_IWUSR, show_in5_min, set_in5_min);
318static DEVICE_ATTR(in5_max, S_IRUGO | S_IWUSR, show_in5_max, set_in5_max);
319
320/* Temperatures */
321static ssize_t show_temp0(struct device *dev, struct device_attribute *attr,
322 char *buf)
323{
324 struct vt8231_data *data = vt8231_update_device(dev);
325 return sprintf(buf, "%d\n", data->temp[0] * 250);
326}
327
328static ssize_t show_temp0_max(struct device *dev, struct device_attribute *attr,
329 char *buf)
330{
331 struct vt8231_data *data = vt8231_update_device(dev);
332 return sprintf(buf, "%d\n", data->temp_max[0] * 1000);
333}
334
335static ssize_t show_temp0_min(struct device *dev, struct device_attribute *attr,
336 char *buf)
337{
338 struct vt8231_data *data = vt8231_update_device(dev);
339 return sprintf(buf, "%d\n", data->temp_min[0] * 1000);
340}
341
342static ssize_t set_temp0_max(struct device *dev, struct device_attribute *attr,
343 const char *buf, size_t count)
344{
Roger Lucasec5e1a42007-06-12 21:04:08 +0200345 struct vt8231_data *data = dev_get_drvdata(dev);
Roger Lucas1de9e372005-11-26 20:20:05 +0100346 int val = simple_strtol(buf, NULL, 10);
347
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100348 mutex_lock(&data->update_lock);
Roger Lucas1de9e372005-11-26 20:20:05 +0100349 data->temp_max[0] = SENSORS_LIMIT((val + 500) / 1000, 0, 255);
Roger Lucasec5e1a42007-06-12 21:04:08 +0200350 vt8231_write_value(data, regtempmax[0], data->temp_max[0]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100351 mutex_unlock(&data->update_lock);
Roger Lucas1de9e372005-11-26 20:20:05 +0100352 return count;
353}
354static ssize_t set_temp0_min(struct device *dev, struct device_attribute *attr,
355 const char *buf, size_t count)
356{
Roger Lucasec5e1a42007-06-12 21:04:08 +0200357 struct vt8231_data *data = dev_get_drvdata(dev);
Roger Lucas1de9e372005-11-26 20:20:05 +0100358 int val = simple_strtol(buf, NULL, 10);
359
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100360 mutex_lock(&data->update_lock);
Roger Lucas1de9e372005-11-26 20:20:05 +0100361 data->temp_min[0] = SENSORS_LIMIT((val + 500) / 1000, 0, 255);
Roger Lucasec5e1a42007-06-12 21:04:08 +0200362 vt8231_write_value(data, regtempmin[0], data->temp_min[0]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100363 mutex_unlock(&data->update_lock);
Roger Lucas1de9e372005-11-26 20:20:05 +0100364 return count;
365}
366
367static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
368 char *buf)
369{
370 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
371 int nr = sensor_attr->index;
372 struct vt8231_data *data = vt8231_update_device(dev);
373 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr]));
374}
375
376static ssize_t show_temp_max(struct device *dev, struct device_attribute *attr,
377 char *buf)
378{
379 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
380 int nr = sensor_attr->index;
381 struct vt8231_data *data = vt8231_update_device(dev);
382 return sprintf(buf, "%d\n", TEMP_MAXMIN_FROM_REG(data->temp_max[nr]));
383}
384
385static ssize_t show_temp_min(struct device *dev, struct device_attribute *attr,
386 char *buf)
387{
388 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
389 int nr = sensor_attr->index;
390 struct vt8231_data *data = vt8231_update_device(dev);
391 return sprintf(buf, "%d\n", TEMP_MAXMIN_FROM_REG(data->temp_min[nr]));
392}
393
394static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
395 const char *buf, size_t count)
396{
397 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
398 int nr = sensor_attr->index;
Roger Lucasec5e1a42007-06-12 21:04:08 +0200399 struct vt8231_data *data = dev_get_drvdata(dev);
Roger Lucas1de9e372005-11-26 20:20:05 +0100400 int val = simple_strtol(buf, NULL, 10);
401
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100402 mutex_lock(&data->update_lock);
Roger Lucas1de9e372005-11-26 20:20:05 +0100403 data->temp_max[nr] = SENSORS_LIMIT(TEMP_MAXMIN_TO_REG(val), 0, 255);
Roger Lucasec5e1a42007-06-12 21:04:08 +0200404 vt8231_write_value(data, regtempmax[nr], data->temp_max[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100405 mutex_unlock(&data->update_lock);
Roger Lucas1de9e372005-11-26 20:20:05 +0100406 return count;
407}
408static ssize_t set_temp_min(struct device *dev, struct device_attribute *attr,
409 const char *buf, size_t count)
410{
411 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
412 int nr = sensor_attr->index;
Roger Lucasec5e1a42007-06-12 21:04:08 +0200413 struct vt8231_data *data = dev_get_drvdata(dev);
Roger Lucas1de9e372005-11-26 20:20:05 +0100414 int val = simple_strtol(buf, NULL, 10);
415
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100416 mutex_lock(&data->update_lock);
Roger Lucas1de9e372005-11-26 20:20:05 +0100417 data->temp_min[nr] = SENSORS_LIMIT(TEMP_MAXMIN_TO_REG(val), 0, 255);
Roger Lucasec5e1a42007-06-12 21:04:08 +0200418 vt8231_write_value(data, regtempmin[nr], data->temp_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100419 mutex_unlock(&data->update_lock);
Roger Lucas1de9e372005-11-26 20:20:05 +0100420 return count;
421}
422
423/* Note that these map the Linux temperature sensor numbering (1-6) to the VIA
424** temperature sensor numbering (0-5)
425*/
426#define define_temperature_sysfs(offset) \
427static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
428 show_temp, NULL, offset - 1); \
429static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
430 show_temp_max, set_temp_max, offset - 1); \
Jean Delvaree3efa5a2006-02-05 23:11:16 +0100431static SENSOR_DEVICE_ATTR(temp##offset##_max_hyst, S_IRUGO | S_IWUSR, \
Roger Lucas1de9e372005-11-26 20:20:05 +0100432 show_temp_min, set_temp_min, offset - 1)
433
434static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp0, NULL);
435static DEVICE_ATTR(temp1_max, S_IRUGO | S_IWUSR, show_temp0_max, set_temp0_max);
Jean Delvaree3efa5a2006-02-05 23:11:16 +0100436static DEVICE_ATTR(temp1_max_hyst, S_IRUGO | S_IWUSR, show_temp0_min, set_temp0_min);
Roger Lucas1de9e372005-11-26 20:20:05 +0100437
438define_temperature_sysfs(2);
439define_temperature_sysfs(3);
440define_temperature_sysfs(4);
441define_temperature_sysfs(5);
442define_temperature_sysfs(6);
443
Roger Lucas1de9e372005-11-26 20:20:05 +0100444/* Fans */
445static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
446 char *buf)
447{
448 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
449 int nr = sensor_attr->index;
450 struct vt8231_data *data = vt8231_update_device(dev);
451 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr],
452 DIV_FROM_REG(data->fan_div[nr])));
453}
454
455static ssize_t show_fan_min(struct device *dev, struct device_attribute *attr,
456 char *buf)
457{
458 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
459 int nr = sensor_attr->index;
460 struct vt8231_data *data = vt8231_update_device(dev);
461 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[nr],
462 DIV_FROM_REG(data->fan_div[nr])));
463}
464
465static ssize_t show_fan_div(struct device *dev, struct device_attribute *attr,
466 char *buf)
467{
468 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
469 int nr = sensor_attr->index;
470 struct vt8231_data *data = vt8231_update_device(dev);
471 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]));
472}
473
474static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
475 const char *buf, size_t count)
476{
477 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
478 int nr = sensor_attr->index;
Roger Lucasec5e1a42007-06-12 21:04:08 +0200479 struct vt8231_data *data = dev_get_drvdata(dev);
Roger Lucas1de9e372005-11-26 20:20:05 +0100480 int val = simple_strtoul(buf, NULL, 10);
481
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100482 mutex_lock(&data->update_lock);
Roger Lucas1de9e372005-11-26 20:20:05 +0100483 data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
Roger Lucasec5e1a42007-06-12 21:04:08 +0200484 vt8231_write_value(data, VT8231_REG_FAN_MIN(nr), data->fan_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100485 mutex_unlock(&data->update_lock);
Roger Lucas1de9e372005-11-26 20:20:05 +0100486 return count;
487}
488
489static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
490 const char *buf, size_t count)
491{
Roger Lucasec5e1a42007-06-12 21:04:08 +0200492 struct vt8231_data *data = dev_get_drvdata(dev);
Roger Lucas1de9e372005-11-26 20:20:05 +0100493 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
494 unsigned long val = simple_strtoul(buf, NULL, 10);
495 int nr = sensor_attr->index;
Roger Lucasec5e1a42007-06-12 21:04:08 +0200496 int old = vt8231_read_value(data, VT8231_REG_FANDIV);
Roger Lucas1de9e372005-11-26 20:20:05 +0100497 long min = FAN_FROM_REG(data->fan_min[nr],
498 DIV_FROM_REG(data->fan_div[nr]));
499
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100500 mutex_lock(&data->update_lock);
Roger Lucas1de9e372005-11-26 20:20:05 +0100501 switch (val) {
502 case 1: data->fan_div[nr] = 0; break;
503 case 2: data->fan_div[nr] = 1; break;
504 case 4: data->fan_div[nr] = 2; break;
505 case 8: data->fan_div[nr] = 3; break;
506 default:
Roger Lucasec5e1a42007-06-12 21:04:08 +0200507 dev_err(dev, "fan_div value %ld not supported."
Roger Lucas1de9e372005-11-26 20:20:05 +0100508 "Choose one of 1, 2, 4 or 8!\n", val);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100509 mutex_unlock(&data->update_lock);
Roger Lucas1de9e372005-11-26 20:20:05 +0100510 return -EINVAL;
511 }
512
513 /* Correct the fan minimum speed */
514 data->fan_min[nr] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
Roger Lucasec5e1a42007-06-12 21:04:08 +0200515 vt8231_write_value(data, VT8231_REG_FAN_MIN(nr), data->fan_min[nr]);
Roger Lucas1de9e372005-11-26 20:20:05 +0100516
517 old = (old & 0x0f) | (data->fan_div[1] << 6) | (data->fan_div[0] << 4);
Roger Lucasec5e1a42007-06-12 21:04:08 +0200518 vt8231_write_value(data, VT8231_REG_FANDIV, old);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100519 mutex_unlock(&data->update_lock);
Roger Lucas1de9e372005-11-26 20:20:05 +0100520 return count;
521}
522
523
524#define define_fan_sysfs(offset) \
525static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
526 show_fan, NULL, offset - 1); \
527static SENSOR_DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
528 show_fan_div, set_fan_div, offset - 1); \
529static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
530 show_fan_min, set_fan_min, offset - 1)
531
532define_fan_sysfs(1);
533define_fan_sysfs(2);
534
535/* Alarms */
536static ssize_t show_alarms(struct device *dev, struct device_attribute *attr,
537 char *buf)
538{
539 struct vt8231_data *data = vt8231_update_device(dev);
540 return sprintf(buf, "%d\n", data->alarms);
541}
Roger Lucas1de9e372005-11-26 20:20:05 +0100542static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
543
Roger Lucasec5e1a42007-06-12 21:04:08 +0200544static ssize_t show_name(struct device *dev, struct device_attribute
545 *devattr, char *buf)
546{
547 struct vt8231_data *data = dev_get_drvdata(dev);
548 return sprintf(buf, "%s\n", data->name);
549}
550static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
551
Roger Lucascbeeb5b2006-09-24 21:21:46 +0200552static struct attribute *vt8231_attributes_temps[6][4] = {
553 {
554 &dev_attr_temp1_input.attr,
555 &dev_attr_temp1_max_hyst.attr,
556 &dev_attr_temp1_max.attr,
557 NULL
558 }, {
559 &sensor_dev_attr_temp2_input.dev_attr.attr,
560 &sensor_dev_attr_temp2_max_hyst.dev_attr.attr,
561 &sensor_dev_attr_temp2_max.dev_attr.attr,
562 NULL
563 }, {
564 &sensor_dev_attr_temp3_input.dev_attr.attr,
565 &sensor_dev_attr_temp3_max_hyst.dev_attr.attr,
566 &sensor_dev_attr_temp3_max.dev_attr.attr,
567 NULL
568 }, {
569 &sensor_dev_attr_temp4_input.dev_attr.attr,
570 &sensor_dev_attr_temp4_max_hyst.dev_attr.attr,
571 &sensor_dev_attr_temp4_max.dev_attr.attr,
572 NULL
573 }, {
574 &sensor_dev_attr_temp5_input.dev_attr.attr,
575 &sensor_dev_attr_temp5_max_hyst.dev_attr.attr,
576 &sensor_dev_attr_temp5_max.dev_attr.attr,
577 NULL
578 }, {
579 &sensor_dev_attr_temp6_input.dev_attr.attr,
580 &sensor_dev_attr_temp6_max_hyst.dev_attr.attr,
581 &sensor_dev_attr_temp6_max.dev_attr.attr,
582 NULL
583 }
584};
585
586static const struct attribute_group vt8231_group_temps[6] = {
587 { .attrs = vt8231_attributes_temps[0] },
588 { .attrs = vt8231_attributes_temps[1] },
589 { .attrs = vt8231_attributes_temps[2] },
590 { .attrs = vt8231_attributes_temps[3] },
591 { .attrs = vt8231_attributes_temps[4] },
592 { .attrs = vt8231_attributes_temps[5] },
593};
594
595static struct attribute *vt8231_attributes_volts[6][4] = {
596 {
597 &sensor_dev_attr_in0_input.dev_attr.attr,
598 &sensor_dev_attr_in0_min.dev_attr.attr,
599 &sensor_dev_attr_in0_max.dev_attr.attr,
600 NULL
601 }, {
602 &sensor_dev_attr_in1_input.dev_attr.attr,
603 &sensor_dev_attr_in1_min.dev_attr.attr,
604 &sensor_dev_attr_in1_max.dev_attr.attr,
605 NULL
606 }, {
607 &sensor_dev_attr_in2_input.dev_attr.attr,
608 &sensor_dev_attr_in2_min.dev_attr.attr,
609 &sensor_dev_attr_in2_max.dev_attr.attr,
610 NULL
611 }, {
612 &sensor_dev_attr_in3_input.dev_attr.attr,
613 &sensor_dev_attr_in3_min.dev_attr.attr,
614 &sensor_dev_attr_in3_max.dev_attr.attr,
615 NULL
616 }, {
617 &sensor_dev_attr_in4_input.dev_attr.attr,
618 &sensor_dev_attr_in4_min.dev_attr.attr,
619 &sensor_dev_attr_in4_max.dev_attr.attr,
620 NULL
621 }, {
622 &dev_attr_in5_input.attr,
623 &dev_attr_in5_min.attr,
624 &dev_attr_in5_max.attr,
625 NULL
626 }
627};
628
629static const struct attribute_group vt8231_group_volts[6] = {
630 { .attrs = vt8231_attributes_volts[0] },
631 { .attrs = vt8231_attributes_volts[1] },
632 { .attrs = vt8231_attributes_volts[2] },
633 { .attrs = vt8231_attributes_volts[3] },
634 { .attrs = vt8231_attributes_volts[4] },
635 { .attrs = vt8231_attributes_volts[5] },
636};
637
638static struct attribute *vt8231_attributes[] = {
639 &sensor_dev_attr_fan1_input.dev_attr.attr,
640 &sensor_dev_attr_fan2_input.dev_attr.attr,
641 &sensor_dev_attr_fan1_min.dev_attr.attr,
642 &sensor_dev_attr_fan2_min.dev_attr.attr,
643 &sensor_dev_attr_fan1_div.dev_attr.attr,
644 &sensor_dev_attr_fan2_div.dev_attr.attr,
645 &dev_attr_alarms.attr,
Roger Lucasec5e1a42007-06-12 21:04:08 +0200646 &dev_attr_name.attr,
Roger Lucascbeeb5b2006-09-24 21:21:46 +0200647 NULL
648};
649
650static const struct attribute_group vt8231_group = {
651 .attrs = vt8231_attributes,
652};
653
Roger Lucasec5e1a42007-06-12 21:04:08 +0200654static struct platform_driver vt8231_driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100655 .driver = {
Jean Delvare87218842006-09-03 22:36:14 +0200656 .owner = THIS_MODULE,
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100657 .name = "vt8231",
658 },
Roger Lucasec5e1a42007-06-12 21:04:08 +0200659 .probe = vt8231_probe,
660 .remove = __devexit_p(vt8231_remove),
Roger Lucas1de9e372005-11-26 20:20:05 +0100661};
662
663static struct pci_device_id vt8231_pci_ids[] = {
664 { PCI_DEVICE(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8231_4) },
665 { 0, }
666};
667
668MODULE_DEVICE_TABLE(pci, vt8231_pci_ids);
669
670static int __devinit vt8231_pci_probe(struct pci_dev *dev,
671 const struct pci_device_id *id);
672
673static struct pci_driver vt8231_pci_driver = {
674 .name = "vt8231",
675 .id_table = vt8231_pci_ids,
676 .probe = vt8231_pci_probe,
677};
678
Roger Lucasec5e1a42007-06-12 21:04:08 +0200679int vt8231_probe(struct platform_device *pdev)
Roger Lucas1de9e372005-11-26 20:20:05 +0100680{
Roger Lucasec5e1a42007-06-12 21:04:08 +0200681 struct resource *res;
Roger Lucas1de9e372005-11-26 20:20:05 +0100682 struct vt8231_data *data;
683 int err = 0, i;
Roger Lucas1de9e372005-11-26 20:20:05 +0100684
685 /* Reserve the ISA region */
Roger Lucasec5e1a42007-06-12 21:04:08 +0200686 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
687 if (!request_region(res->start, VT8231_EXTENT,
688 vt8231_driver.driver.name)) {
689 dev_err(&pdev->dev, "Region 0x%lx-0x%lx already in use!\n",
690 (unsigned long)res->start, (unsigned long)res->end);
Roger Lucas1de9e372005-11-26 20:20:05 +0100691 return -ENODEV;
692 }
693
694 if (!(data = kzalloc(sizeof(struct vt8231_data), GFP_KERNEL))) {
695 err = -ENOMEM;
696 goto exit_release;
697 }
698
Roger Lucasec5e1a42007-06-12 21:04:08 +0200699 platform_set_drvdata(pdev, data);
700 data->addr = res->start;
701 data->name = "vt8231";
Roger Lucas1de9e372005-11-26 20:20:05 +0100702
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100703 mutex_init(&data->update_lock);
Roger Lucasec5e1a42007-06-12 21:04:08 +0200704 vt8231_init_device(data);
Roger Lucas1de9e372005-11-26 20:20:05 +0100705
706 /* Register sysfs hooks */
Roger Lucasec5e1a42007-06-12 21:04:08 +0200707 if ((err = sysfs_create_group(&pdev->dev.kobj, &vt8231_group)))
708 goto exit_free;
Roger Lucas1de9e372005-11-26 20:20:05 +0100709
710 /* Must update device information to find out the config field */
Roger Lucasec5e1a42007-06-12 21:04:08 +0200711 data->uch_config = vt8231_read_value(data, VT8231_REG_UCH_CONFIG);
Roger Lucas1de9e372005-11-26 20:20:05 +0100712
Roger Lucascbeeb5b2006-09-24 21:21:46 +0200713 for (i = 0; i < ARRAY_SIZE(vt8231_group_temps); i++) {
Roger Lucas1de9e372005-11-26 20:20:05 +0100714 if (ISTEMP(i, data->uch_config)) {
Roger Lucasec5e1a42007-06-12 21:04:08 +0200715 if ((err = sysfs_create_group(&pdev->dev.kobj,
Roger Lucascbeeb5b2006-09-24 21:21:46 +0200716 &vt8231_group_temps[i])))
717 goto exit_remove_files;
Roger Lucas1de9e372005-11-26 20:20:05 +0100718 }
719 }
720
Roger Lucascbeeb5b2006-09-24 21:21:46 +0200721 for (i = 0; i < ARRAY_SIZE(vt8231_group_volts); i++) {
Roger Lucas1de9e372005-11-26 20:20:05 +0100722 if (ISVOLT(i, data->uch_config)) {
Roger Lucasec5e1a42007-06-12 21:04:08 +0200723 if ((err = sysfs_create_group(&pdev->dev.kobj,
Roger Lucascbeeb5b2006-09-24 21:21:46 +0200724 &vt8231_group_volts[i])))
725 goto exit_remove_files;
Roger Lucas1de9e372005-11-26 20:20:05 +0100726 }
727 }
728
Roger Lucasec5e1a42007-06-12 21:04:08 +0200729 data->class_dev = hwmon_device_register(&pdev->dev);
Roger Lucascbeeb5b2006-09-24 21:21:46 +0200730 if (IS_ERR(data->class_dev)) {
731 err = PTR_ERR(data->class_dev);
732 goto exit_remove_files;
733 }
Roger Lucas1de9e372005-11-26 20:20:05 +0100734 return 0;
735
Roger Lucascbeeb5b2006-09-24 21:21:46 +0200736exit_remove_files:
737 for (i = 0; i < ARRAY_SIZE(vt8231_group_volts); i++)
Roger Lucasec5e1a42007-06-12 21:04:08 +0200738 sysfs_remove_group(&pdev->dev.kobj, &vt8231_group_volts[i]);
Roger Lucascbeeb5b2006-09-24 21:21:46 +0200739
740 for (i = 0; i < ARRAY_SIZE(vt8231_group_temps); i++)
Roger Lucasec5e1a42007-06-12 21:04:08 +0200741 sysfs_remove_group(&pdev->dev.kobj, &vt8231_group_temps[i]);
Roger Lucascbeeb5b2006-09-24 21:21:46 +0200742
Roger Lucasec5e1a42007-06-12 21:04:08 +0200743 sysfs_remove_group(&pdev->dev.kobj, &vt8231_group);
744
Roger Lucas1de9e372005-11-26 20:20:05 +0100745exit_free:
Jean Delvare04a62172007-06-12 13:57:19 +0200746 platform_set_drvdata(pdev, NULL);
Roger Lucas1de9e372005-11-26 20:20:05 +0100747 kfree(data);
Roger Lucasec5e1a42007-06-12 21:04:08 +0200748
Roger Lucas1de9e372005-11-26 20:20:05 +0100749exit_release:
Roger Lucasec5e1a42007-06-12 21:04:08 +0200750 release_region(res->start, VT8231_EXTENT);
Roger Lucas1de9e372005-11-26 20:20:05 +0100751 return err;
752}
753
Jean Delvared0546122007-07-22 12:09:48 +0200754static int __devexit vt8231_remove(struct platform_device *pdev)
Roger Lucas1de9e372005-11-26 20:20:05 +0100755{
Roger Lucasec5e1a42007-06-12 21:04:08 +0200756 struct vt8231_data *data = platform_get_drvdata(pdev);
757 int i;
Roger Lucas1de9e372005-11-26 20:20:05 +0100758
759 hwmon_device_unregister(data->class_dev);
760
Roger Lucascbeeb5b2006-09-24 21:21:46 +0200761 for (i = 0; i < ARRAY_SIZE(vt8231_group_volts); i++)
Roger Lucasec5e1a42007-06-12 21:04:08 +0200762 sysfs_remove_group(&pdev->dev.kobj, &vt8231_group_volts[i]);
Roger Lucascbeeb5b2006-09-24 21:21:46 +0200763
764 for (i = 0; i < ARRAY_SIZE(vt8231_group_temps); i++)
Roger Lucasec5e1a42007-06-12 21:04:08 +0200765 sysfs_remove_group(&pdev->dev.kobj, &vt8231_group_temps[i]);
Roger Lucascbeeb5b2006-09-24 21:21:46 +0200766
Roger Lucasec5e1a42007-06-12 21:04:08 +0200767 sysfs_remove_group(&pdev->dev.kobj, &vt8231_group);
Roger Lucascbeeb5b2006-09-24 21:21:46 +0200768
Roger Lucasec5e1a42007-06-12 21:04:08 +0200769 release_region(data->addr, VT8231_EXTENT);
770 platform_set_drvdata(pdev, NULL);
Roger Lucas1de9e372005-11-26 20:20:05 +0100771 kfree(data);
Roger Lucas1de9e372005-11-26 20:20:05 +0100772 return 0;
773}
774
Roger Lucasec5e1a42007-06-12 21:04:08 +0200775static void vt8231_init_device(struct vt8231_data *data)
Roger Lucas1de9e372005-11-26 20:20:05 +0100776{
Roger Lucasec5e1a42007-06-12 21:04:08 +0200777 vt8231_write_value(data, VT8231_REG_TEMP1_CONFIG, 0);
778 vt8231_write_value(data, VT8231_REG_TEMP2_CONFIG, 0);
Roger Lucas1de9e372005-11-26 20:20:05 +0100779}
780
781static struct vt8231_data *vt8231_update_device(struct device *dev)
782{
Roger Lucasec5e1a42007-06-12 21:04:08 +0200783 struct vt8231_data *data = dev_get_drvdata(dev);
Roger Lucas1de9e372005-11-26 20:20:05 +0100784 int i;
785 u16 low;
786
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100787 mutex_lock(&data->update_lock);
Roger Lucas1de9e372005-11-26 20:20:05 +0100788
789 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
790 || !data->valid) {
791 for (i = 0; i < 6; i++) {
792 if (ISVOLT(i, data->uch_config)) {
Roger Lucasec5e1a42007-06-12 21:04:08 +0200793 data->in[i] = vt8231_read_value(data,
Roger Lucas1de9e372005-11-26 20:20:05 +0100794 regvolt[i]);
Roger Lucasec5e1a42007-06-12 21:04:08 +0200795 data->in_min[i] = vt8231_read_value(data,
Roger Lucas1de9e372005-11-26 20:20:05 +0100796 regvoltmin[i]);
Roger Lucasec5e1a42007-06-12 21:04:08 +0200797 data->in_max[i] = vt8231_read_value(data,
Roger Lucas1de9e372005-11-26 20:20:05 +0100798 regvoltmax[i]);
799 }
800 }
801 for (i = 0; i < 2; i++) {
Roger Lucasec5e1a42007-06-12 21:04:08 +0200802 data->fan[i] = vt8231_read_value(data,
Roger Lucas1de9e372005-11-26 20:20:05 +0100803 VT8231_REG_FAN(i));
Roger Lucasec5e1a42007-06-12 21:04:08 +0200804 data->fan_min[i] = vt8231_read_value(data,
Roger Lucas1de9e372005-11-26 20:20:05 +0100805 VT8231_REG_FAN_MIN(i));
806 }
807
Roger Lucasec5e1a42007-06-12 21:04:08 +0200808 low = vt8231_read_value(data, VT8231_REG_TEMP_LOW01);
Roger Lucas1de9e372005-11-26 20:20:05 +0100809 low = (low >> 6) | ((low & 0x30) >> 2)
Roger Lucasec5e1a42007-06-12 21:04:08 +0200810 | (vt8231_read_value(data, VT8231_REG_TEMP_LOW25) << 4);
Roger Lucas1de9e372005-11-26 20:20:05 +0100811 for (i = 0; i < 6; i++) {
812 if (ISTEMP(i, data->uch_config)) {
Roger Lucasec5e1a42007-06-12 21:04:08 +0200813 data->temp[i] = (vt8231_read_value(data,
Roger Lucas1de9e372005-11-26 20:20:05 +0100814 regtemp[i]) << 2)
815 | ((low >> (2 * i)) & 0x03);
Roger Lucasec5e1a42007-06-12 21:04:08 +0200816 data->temp_max[i] = vt8231_read_value(data,
Roger Lucas1de9e372005-11-26 20:20:05 +0100817 regtempmax[i]);
Roger Lucasec5e1a42007-06-12 21:04:08 +0200818 data->temp_min[i] = vt8231_read_value(data,
Roger Lucas1de9e372005-11-26 20:20:05 +0100819 regtempmin[i]);
820 }
821 }
822
Roger Lucasec5e1a42007-06-12 21:04:08 +0200823 i = vt8231_read_value(data, VT8231_REG_FANDIV);
Roger Lucas1de9e372005-11-26 20:20:05 +0100824 data->fan_div[0] = (i >> 4) & 0x03;
825 data->fan_div[1] = i >> 6;
Roger Lucasec5e1a42007-06-12 21:04:08 +0200826 data->alarms = vt8231_read_value(data, VT8231_REG_ALARM1) |
827 (vt8231_read_value(data, VT8231_REG_ALARM2) << 8);
Roger Lucas1de9e372005-11-26 20:20:05 +0100828
829 /* Set alarm flags correctly */
830 if (!data->fan[0] && data->fan_min[0]) {
831 data->alarms |= 0x40;
832 } else if (data->fan[0] && !data->fan_min[0]) {
833 data->alarms &= ~0x40;
834 }
835
836 if (!data->fan[1] && data->fan_min[1]) {
837 data->alarms |= 0x80;
838 } else if (data->fan[1] && !data->fan_min[1]) {
839 data->alarms &= ~0x80;
840 }
841
842 data->last_updated = jiffies;
843 data->valid = 1;
844 }
845
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100846 mutex_unlock(&data->update_lock);
Roger Lucas1de9e372005-11-26 20:20:05 +0100847
848 return data;
849}
850
Roger Lucasec5e1a42007-06-12 21:04:08 +0200851static int __devinit vt8231_device_add(unsigned short address)
852{
853 struct resource res = {
854 .start = address,
855 .end = address + VT8231_EXTENT - 1,
856 .name = "vt8231",
857 .flags = IORESOURCE_IO,
858 };
859 int err;
860
861 pdev = platform_device_alloc("vt8231", address);
862 if (!pdev) {
863 err = -ENOMEM;
864 printk(KERN_ERR "vt8231: Device allocation failed\n");
865 goto exit;
866 }
867
868 err = platform_device_add_resources(pdev, &res, 1);
869 if (err) {
870 printk(KERN_ERR "vt8231: Device resource addition failed "
871 "(%d)\n", err);
872 goto exit_device_put;
873 }
874
875 err = platform_device_add(pdev);
876 if (err) {
877 printk(KERN_ERR "vt8231: Device addition failed (%d)\n",
878 err);
879 goto exit_device_put;
880 }
881
882 return 0;
883
884exit_device_put:
885 platform_device_put(pdev);
886exit:
887 return err;
888}
889
Roger Lucas1de9e372005-11-26 20:20:05 +0100890static int __devinit vt8231_pci_probe(struct pci_dev *dev,
891 const struct pci_device_id *id)
892{
Roger Lucasec5e1a42007-06-12 21:04:08 +0200893 u16 address, val;
894 if (force_addr) {
895 address = force_addr & 0xff00;
896 dev_warn(&dev->dev, "Forcing ISA address 0x%x\n",
897 address);
898
899 if (PCIBIOS_SUCCESSFUL !=
900 pci_write_config_word(dev, VT8231_BASE_REG, address | 1))
901 return -ENODEV;
902 }
Roger Lucas1de9e372005-11-26 20:20:05 +0100903
904 if (PCIBIOS_SUCCESSFUL != pci_read_config_word(dev, VT8231_BASE_REG,
905 &val))
906 return -ENODEV;
907
Roger Lucasec5e1a42007-06-12 21:04:08 +0200908 address = val & ~(VT8231_EXTENT - 1);
909 if (address == 0) {
Roger Lucas1de9e372005-11-26 20:20:05 +0100910 dev_err(&dev->dev, "base address not set -\
911 upgrade BIOS or use force_addr=0xaddr\n");
912 return -ENODEV;
913 }
914
Roger Lucasec5e1a42007-06-12 21:04:08 +0200915 if (PCIBIOS_SUCCESSFUL != pci_read_config_word(dev, VT8231_ENABLE_REG,
916 &val))
917 return -ENODEV;
Roger Lucas1de9e372005-11-26 20:20:05 +0100918
Roger Lucasec5e1a42007-06-12 21:04:08 +0200919 if (!(val & 0x0001)) {
920 dev_warn(&dev->dev, "enabling sensors\n");
921 if (PCIBIOS_SUCCESSFUL !=
922 pci_write_config_word(dev, VT8231_ENABLE_REG,
923 val | 0x0001))
924 return -ENODEV;
Roger Lucas1de9e372005-11-26 20:20:05 +0100925 }
926
Roger Lucasec5e1a42007-06-12 21:04:08 +0200927 if (platform_driver_register(&vt8231_driver))
928 goto exit;
929
930 /* Sets global pdev as a side effect */
931 if (vt8231_device_add(address))
932 goto exit_unregister;
933
Roger Lucas1de9e372005-11-26 20:20:05 +0100934 /* Always return failure here. This is to allow other drivers to bind
935 * to this pci device. We don't really want to have control over the
936 * pci device, we only wanted to read as few register values from it.
937 */
Roger Lucasec5e1a42007-06-12 21:04:08 +0200938
939 /* We do, however, mark ourselves as using the PCI device to stop it
940 getting unloaded. */
941 s_bridge = pci_dev_get(dev);
942 return -ENODEV;
943
944exit_unregister:
945 platform_driver_unregister(&vt8231_driver);
946exit:
Roger Lucas1de9e372005-11-26 20:20:05 +0100947 return -ENODEV;
948}
949
950static int __init sm_vt8231_init(void)
951{
Richard Knutsson93b47682005-11-30 01:00:35 +0100952 return pci_register_driver(&vt8231_pci_driver);
Roger Lucas1de9e372005-11-26 20:20:05 +0100953}
954
955static void __exit sm_vt8231_exit(void)
956{
957 pci_unregister_driver(&vt8231_pci_driver);
958 if (s_bridge != NULL) {
Roger Lucasec5e1a42007-06-12 21:04:08 +0200959 platform_device_unregister(pdev);
960 platform_driver_unregister(&vt8231_driver);
Roger Lucas1de9e372005-11-26 20:20:05 +0100961 pci_dev_put(s_bridge);
962 s_bridge = NULL;
963 }
964}
965
966MODULE_AUTHOR("Roger Lucas <roger@planbit.co.uk>");
967MODULE_DESCRIPTION("VT8231 sensors");
968MODULE_LICENSE("GPL");
969
970module_init(sm_vt8231_init);
971module_exit(sm_vt8231_exit);