[PATCH] Documentation/powerpc: index update
[linux-2.6.22.y-op.git] / drivers / hwmon / vt8231.c
blobd00a726d0239a6d620deb280022ea1b7b478fa72
1 /*
2 vt8231.c - Part of lm_sensors, Linux kernel modules
3 for hardware monitoring
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>
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.
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., 675 Mass Ave, Cambridge, MA 02139, USA.
24 /* Supports VIA VT8231 South Bridge embedded sensors
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>
32 #include <linux/i2c.h>
33 #include <linux/i2c-isa.h>
34 #include <linux/hwmon.h>
35 #include <linux/hwmon-sysfs.h>
36 #include <linux/hwmon-vid.h>
37 #include <linux/err.h>
38 #include <asm/io.h>
40 static int force_addr;
41 module_param(force_addr, int, 0);
42 MODULE_PARM_DESC(force_addr, "Initialize the base address of the sensors");
44 /* Device address
45 Note that we can't determine the ISA address until we have initialized
46 our module */
47 static unsigned short isa_address;
49 #define VT8231_EXTENT 0x80
50 #define VT8231_BASE_REG 0x70
51 #define VT8231_ENABLE_REG 0x74
53 /* The VT8231 registers
55 The reset value for the input channel configuration is used (Reg 0x4A=0x07)
56 which sets the selected inputs marked with '*' below if multiple options are
57 possible:
59 Voltage Mode Temperature Mode
60 Sensor Linux Id Linux Id VIA Id
61 -------- -------- -------- ------
62 CPU Diode N/A temp1 0
63 UIC1 in0 temp2 * 1
64 UIC2 in1 * temp3 2
65 UIC3 in2 * temp4 3
66 UIC4 in3 * temp5 4
67 UIC5 in4 * temp6 5
68 3.3V in5 N/A
70 Note that the BIOS may set the configuration register to a different value
71 to match the motherboard configuration.
74 /* fans numbered 0-1 */
75 #define VT8231_REG_FAN_MIN(nr) (0x3b + (nr))
76 #define VT8231_REG_FAN(nr) (0x29 + (nr))
78 /* Voltage inputs numbered 0-5 */
80 static const u8 regvolt[] = { 0x21, 0x22, 0x23, 0x24, 0x25, 0x26 };
81 static const u8 regvoltmax[] = { 0x3d, 0x2b, 0x2d, 0x2f, 0x31, 0x33 };
82 static const u8 regvoltmin[] = { 0x3e, 0x2c, 0x2e, 0x30, 0x32, 0x34 };
84 /* Temperatures are numbered 1-6 according to the Linux kernel specification.
86 ** In the VIA datasheet, however, the temperatures are numbered from zero.
87 ** Since it is important that this driver can easily be compared to the VIA
88 ** datasheet, we will use the VIA numbering within this driver and map the
89 ** kernel sysfs device name to the VIA number in the sysfs callback.
92 #define VT8231_REG_TEMP_LOW01 0x49
93 #define VT8231_REG_TEMP_LOW25 0x4d
95 static const u8 regtemp[] = { 0x1f, 0x21, 0x22, 0x23, 0x24, 0x25 };
96 static const u8 regtempmax[] = { 0x39, 0x3d, 0x2b, 0x2d, 0x2f, 0x31 };
97 static const u8 regtempmin[] = { 0x3a, 0x3e, 0x2c, 0x2e, 0x30, 0x32 };
99 #define TEMP_FROM_REG(reg) (((253 * 4 - (reg)) * 550 + 105) / 210)
100 #define TEMP_MAXMIN_FROM_REG(reg) (((253 - (reg)) * 2200 + 105) / 210)
101 #define TEMP_MAXMIN_TO_REG(val) (253 - ((val) * 210 + 1100) / 2200)
103 #define VT8231_REG_CONFIG 0x40
104 #define VT8231_REG_ALARM1 0x41
105 #define VT8231_REG_ALARM2 0x42
106 #define VT8231_REG_FANDIV 0x47
107 #define VT8231_REG_UCH_CONFIG 0x4a
108 #define VT8231_REG_TEMP1_CONFIG 0x4b
109 #define VT8231_REG_TEMP2_CONFIG 0x4c
111 /* temps 0-5 as numbered in VIA datasheet - see later for mapping to Linux
112 ** numbering
114 #define ISTEMP(i, ch_config) ((i) == 0 ? 1 : \
115 ((ch_config) >> ((i)+1)) & 0x01)
116 /* voltages 0-5 */
117 #define ISVOLT(i, ch_config) ((i) == 5 ? 1 : \
118 !(((ch_config) >> ((i)+2)) & 0x01))
120 #define DIV_FROM_REG(val) (1 << (val))
122 /* NB The values returned here are NOT temperatures. The calibration curves
123 ** for the thermistor curves are board-specific and must go in the
124 ** sensors.conf file. Temperature sensors are actually ten bits, but the
125 ** VIA datasheet only considers the 8 MSBs obtained from the regtemp[]
126 ** register. The temperature value returned should have a magnitude of 3,
127 ** so we use the VIA scaling as the "true" scaling and use the remaining 2
128 ** LSBs as fractional precision.
130 ** All the on-chip hardware temperature comparisons for the alarms are only
131 ** 8-bits wide, and compare against the 8 MSBs of the temperature. The bits
132 ** in the registers VT8231_REG_TEMP_LOW01 and VT8231_REG_TEMP_LOW25 are
133 ** ignored.
136 /******** FAN RPM CONVERSIONS ********
137 ** This chip saturates back at 0, not at 255 like many the other chips.
138 ** So, 0 means 0 RPM
140 static inline u8 FAN_TO_REG(long rpm, int div)
142 if (rpm == 0)
143 return 0;
144 return SENSORS_LIMIT(1310720 / (rpm * div), 1, 255);
147 #define FAN_FROM_REG(val, div) ((val) == 0 ? 0 : 1310720 / ((val) * (div)))
149 struct vt8231_data {
150 struct i2c_client client;
151 struct semaphore update_lock;
152 struct class_device *class_dev;
153 char valid; /* !=0 if following fields are valid */
154 unsigned long last_updated; /* In jiffies */
156 u8 in[6]; /* Register value */
157 u8 in_max[6]; /* Register value */
158 u8 in_min[6]; /* Register value */
159 u16 temp[6]; /* Register value 10 bit, right aligned */
160 u8 temp_max[6]; /* Register value */
161 u8 temp_min[6]; /* Register value */
162 u8 fan[2]; /* Register value */
163 u8 fan_min[2]; /* Register value */
164 u8 fan_div[2]; /* Register encoding, shifted right */
165 u16 alarms; /* Register encoding */
166 u8 uch_config;
169 static struct pci_dev *s_bridge;
170 static int vt8231_detect(struct i2c_adapter *adapter);
171 static int vt8231_detach_client(struct i2c_client *client);
172 static struct vt8231_data *vt8231_update_device(struct device *dev);
173 static void vt8231_init_client(struct i2c_client *client);
175 static inline int vt8231_read_value(struct i2c_client *client, u8 reg)
177 return inb_p(client->addr + reg);
180 static inline void vt8231_write_value(struct i2c_client *client, u8 reg,
181 u8 value)
183 outb_p(value, client->addr + reg);
186 /* following are the sysfs callback functions */
187 static ssize_t show_in(struct device *dev, struct device_attribute *attr,
188 char *buf)
190 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
191 int nr = sensor_attr->index;
192 struct vt8231_data *data = vt8231_update_device(dev);
194 return sprintf(buf, "%d\n", ((data->in[nr] - 3) * 10000) / 958);
197 static ssize_t show_in_min(struct device *dev, struct device_attribute *attr,
198 char *buf)
200 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
201 int nr = sensor_attr->index;
202 struct vt8231_data *data = vt8231_update_device(dev);
204 return sprintf(buf, "%d\n", ((data->in_min[nr] - 3) * 10000) / 958);
207 static ssize_t show_in_max(struct device *dev, struct device_attribute *attr,
208 char *buf)
210 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
211 int nr = sensor_attr->index;
212 struct vt8231_data *data = vt8231_update_device(dev);
214 return sprintf(buf, "%d\n", (((data->in_max[nr] - 3) * 10000) / 958));
217 static ssize_t set_in_min(struct device *dev, struct device_attribute *attr,
218 const char *buf, size_t count)
220 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
221 int nr = sensor_attr->index;
222 struct i2c_client *client = to_i2c_client(dev);
223 struct vt8231_data *data = i2c_get_clientdata(client);
224 unsigned long val = simple_strtoul(buf, NULL, 10);
226 down(&data->update_lock);
227 data->in_min[nr] = SENSORS_LIMIT(((val * 958) / 10000) + 3, 0, 255);
228 vt8231_write_value(client, regvoltmin[nr], data->in_min[nr]);
229 up(&data->update_lock);
230 return count;
233 static ssize_t set_in_max(struct device *dev, struct device_attribute *attr,
234 const char *buf, size_t count)
236 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
237 int nr = sensor_attr->index;
238 struct i2c_client *client = to_i2c_client(dev);
239 struct vt8231_data *data = i2c_get_clientdata(client);
240 unsigned long val = simple_strtoul(buf, NULL, 10);
242 down(&data->update_lock);
243 data->in_max[nr] = SENSORS_LIMIT(((val * 958) / 10000) + 3, 0, 255);
244 vt8231_write_value(client, regvoltmax[nr], data->in_max[nr]);
245 up(&data->update_lock);
246 return count;
249 /* Special case for input 5 as this has 3.3V scaling built into the chip */
250 static ssize_t show_in5(struct device *dev, struct device_attribute *attr,
251 char *buf)
253 struct vt8231_data *data = vt8231_update_device(dev);
255 return sprintf(buf, "%d\n",
256 (((data->in[5] - 3) * 10000 * 54) / (958 * 34)));
259 static ssize_t show_in5_min(struct device *dev, struct device_attribute *attr,
260 char *buf)
262 struct vt8231_data *data = vt8231_update_device(dev);
264 return sprintf(buf, "%d\n",
265 (((data->in_min[5] - 3) * 10000 * 54) / (958 * 34)));
268 static ssize_t show_in5_max(struct device *dev, struct device_attribute *attr,
269 char *buf)
271 struct vt8231_data *data = vt8231_update_device(dev);
273 return sprintf(buf, "%d\n",
274 (((data->in_max[5] - 3) * 10000 * 54) / (958 * 34)));
277 static ssize_t set_in5_min(struct device *dev, struct device_attribute *attr,
278 const char *buf, size_t count)
280 struct i2c_client *client = to_i2c_client(dev);
281 struct vt8231_data *data = i2c_get_clientdata(client);
282 unsigned long val = simple_strtoul(buf, NULL, 10);
284 down(&data->update_lock);
285 data->in_min[5] = SENSORS_LIMIT(((val * 958 * 34) / (10000 * 54)) + 3,
286 0, 255);
287 vt8231_write_value(client, regvoltmin[5], data->in_min[5]);
288 up(&data->update_lock);
289 return count;
292 static ssize_t set_in5_max(struct device *dev, struct device_attribute *attr,
293 const char *buf, size_t count)
295 struct i2c_client *client = to_i2c_client(dev);
296 struct vt8231_data *data = i2c_get_clientdata(client);
297 unsigned long val = simple_strtoul(buf, NULL, 10);
299 down(&data->update_lock);
300 data->in_max[5] = SENSORS_LIMIT(((val * 958 * 34) / (10000 * 54)) + 3,
301 0, 255);
302 vt8231_write_value(client, regvoltmax[5], data->in_max[5]);
303 up(&data->update_lock);
304 return count;
307 #define define_voltage_sysfs(offset) \
308 static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \
309 show_in, NULL, offset); \
310 static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
311 show_in_min, set_in_min, offset); \
312 static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
313 show_in_max, set_in_max, offset)
315 define_voltage_sysfs(0);
316 define_voltage_sysfs(1);
317 define_voltage_sysfs(2);
318 define_voltage_sysfs(3);
319 define_voltage_sysfs(4);
321 static DEVICE_ATTR(in5_input, S_IRUGO, show_in5, NULL);
322 static DEVICE_ATTR(in5_min, S_IRUGO | S_IWUSR, show_in5_min, set_in5_min);
323 static DEVICE_ATTR(in5_max, S_IRUGO | S_IWUSR, show_in5_max, set_in5_max);
325 /* Temperatures */
326 static ssize_t show_temp0(struct device *dev, struct device_attribute *attr,
327 char *buf)
329 struct vt8231_data *data = vt8231_update_device(dev);
330 return sprintf(buf, "%d\n", data->temp[0] * 250);
333 static ssize_t show_temp0_max(struct device *dev, struct device_attribute *attr,
334 char *buf)
336 struct vt8231_data *data = vt8231_update_device(dev);
337 return sprintf(buf, "%d\n", data->temp_max[0] * 1000);
340 static ssize_t show_temp0_min(struct device *dev, struct device_attribute *attr,
341 char *buf)
343 struct vt8231_data *data = vt8231_update_device(dev);
344 return sprintf(buf, "%d\n", data->temp_min[0] * 1000);
347 static ssize_t set_temp0_max(struct device *dev, struct device_attribute *attr,
348 const char *buf, size_t count)
350 struct i2c_client *client = to_i2c_client(dev);
351 struct vt8231_data *data = i2c_get_clientdata(client);
352 int val = simple_strtol(buf, NULL, 10);
354 down(&data->update_lock);
355 data->temp_max[0] = SENSORS_LIMIT((val + 500) / 1000, 0, 255);
356 vt8231_write_value(client, regtempmax[0], data->temp_max[0]);
357 up(&data->update_lock);
358 return count;
360 static ssize_t set_temp0_min(struct device *dev, struct device_attribute *attr,
361 const char *buf, size_t count)
363 struct i2c_client *client = to_i2c_client(dev);
364 struct vt8231_data *data = i2c_get_clientdata(client);
365 int val = simple_strtol(buf, NULL, 10);
367 down(&data->update_lock);
368 data->temp_min[0] = SENSORS_LIMIT((val + 500) / 1000, 0, 255);
369 vt8231_write_value(client, regtempmin[0], data->temp_min[0]);
370 up(&data->update_lock);
371 return count;
374 static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
375 char *buf)
377 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
378 int nr = sensor_attr->index;
379 struct vt8231_data *data = vt8231_update_device(dev);
380 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr]));
383 static ssize_t show_temp_max(struct device *dev, struct device_attribute *attr,
384 char *buf)
386 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
387 int nr = sensor_attr->index;
388 struct vt8231_data *data = vt8231_update_device(dev);
389 return sprintf(buf, "%d\n", TEMP_MAXMIN_FROM_REG(data->temp_max[nr]));
392 static ssize_t show_temp_min(struct device *dev, struct device_attribute *attr,
393 char *buf)
395 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
396 int nr = sensor_attr->index;
397 struct vt8231_data *data = vt8231_update_device(dev);
398 return sprintf(buf, "%d\n", TEMP_MAXMIN_FROM_REG(data->temp_min[nr]));
401 static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
402 const char *buf, size_t count)
404 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
405 int nr = sensor_attr->index;
406 struct i2c_client *client = to_i2c_client(dev);
407 struct vt8231_data *data = i2c_get_clientdata(client);
408 int val = simple_strtol(buf, NULL, 10);
410 down(&data->update_lock);
411 data->temp_max[nr] = SENSORS_LIMIT(TEMP_MAXMIN_TO_REG(val), 0, 255);
412 vt8231_write_value(client, regtempmax[nr], data->temp_max[nr]);
413 up(&data->update_lock);
414 return count;
416 static ssize_t set_temp_min(struct device *dev, struct device_attribute *attr,
417 const char *buf, size_t count)
419 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
420 int nr = sensor_attr->index;
421 struct i2c_client *client = to_i2c_client(dev);
422 struct vt8231_data *data = i2c_get_clientdata(client);
423 int val = simple_strtol(buf, NULL, 10);
425 down(&data->update_lock);
426 data->temp_min[nr] = SENSORS_LIMIT(TEMP_MAXMIN_TO_REG(val), 0, 255);
427 vt8231_write_value(client, regtempmin[nr], data->temp_min[nr]);
428 up(&data->update_lock);
429 return count;
432 /* Note that these map the Linux temperature sensor numbering (1-6) to the VIA
433 ** temperature sensor numbering (0-5)
435 #define define_temperature_sysfs(offset) \
436 static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
437 show_temp, NULL, offset - 1); \
438 static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
439 show_temp_max, set_temp_max, offset - 1); \
440 static SENSOR_DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
441 show_temp_min, set_temp_min, offset - 1)
443 static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp0, NULL);
444 static DEVICE_ATTR(temp1_max, S_IRUGO | S_IWUSR, show_temp0_max, set_temp0_max);
445 static DEVICE_ATTR(temp1_min, S_IRUGO | S_IWUSR, show_temp0_min, set_temp0_min);
447 define_temperature_sysfs(2);
448 define_temperature_sysfs(3);
449 define_temperature_sysfs(4);
450 define_temperature_sysfs(5);
451 define_temperature_sysfs(6);
453 #define CFG_INFO_TEMP(id) { &sensor_dev_attr_temp##id##_input.dev_attr, \
454 &sensor_dev_attr_temp##id##_min.dev_attr, \
455 &sensor_dev_attr_temp##id##_max.dev_attr }
456 #define CFG_INFO_VOLT(id) { &sensor_dev_attr_in##id##_input.dev_attr, \
457 &sensor_dev_attr_in##id##_min.dev_attr, \
458 &sensor_dev_attr_in##id##_max.dev_attr }
460 struct str_device_attr_table {
461 struct device_attribute *input;
462 struct device_attribute *min;
463 struct device_attribute *max;
466 static struct str_device_attr_table cfg_info_temp[] = {
467 { &dev_attr_temp1_input, &dev_attr_temp1_min, &dev_attr_temp1_max },
468 CFG_INFO_TEMP(2),
469 CFG_INFO_TEMP(3),
470 CFG_INFO_TEMP(4),
471 CFG_INFO_TEMP(5),
472 CFG_INFO_TEMP(6)
475 static struct str_device_attr_table cfg_info_volt[] = {
476 CFG_INFO_VOLT(0),
477 CFG_INFO_VOLT(1),
478 CFG_INFO_VOLT(2),
479 CFG_INFO_VOLT(3),
480 CFG_INFO_VOLT(4),
481 { &dev_attr_in5_input, &dev_attr_in5_min, &dev_attr_in5_max }
484 /* Fans */
485 static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
486 char *buf)
488 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
489 int nr = sensor_attr->index;
490 struct vt8231_data *data = vt8231_update_device(dev);
491 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr],
492 DIV_FROM_REG(data->fan_div[nr])));
495 static ssize_t show_fan_min(struct device *dev, struct device_attribute *attr,
496 char *buf)
498 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
499 int nr = sensor_attr->index;
500 struct vt8231_data *data = vt8231_update_device(dev);
501 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[nr],
502 DIV_FROM_REG(data->fan_div[nr])));
505 static ssize_t show_fan_div(struct device *dev, struct device_attribute *attr,
506 char *buf)
508 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
509 int nr = sensor_attr->index;
510 struct vt8231_data *data = vt8231_update_device(dev);
511 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]));
514 static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
515 const char *buf, size_t count)
517 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
518 int nr = sensor_attr->index;
519 struct i2c_client *client = to_i2c_client(dev);
520 struct vt8231_data *data = i2c_get_clientdata(client);
521 int val = simple_strtoul(buf, NULL, 10);
523 down(&data->update_lock);
524 data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
525 vt8231_write_value(client, VT8231_REG_FAN_MIN(nr), data->fan_min[nr]);
526 up(&data->update_lock);
527 return count;
530 static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
531 const char *buf, size_t count)
533 struct i2c_client *client = to_i2c_client(dev);
534 struct vt8231_data *data = i2c_get_clientdata(client);
535 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
536 unsigned long val = simple_strtoul(buf, NULL, 10);
537 int nr = sensor_attr->index;
538 int old = vt8231_read_value(client, VT8231_REG_FANDIV);
539 long min = FAN_FROM_REG(data->fan_min[nr],
540 DIV_FROM_REG(data->fan_div[nr]));
542 down(&data->update_lock);
543 switch (val) {
544 case 1: data->fan_div[nr] = 0; break;
545 case 2: data->fan_div[nr] = 1; break;
546 case 4: data->fan_div[nr] = 2; break;
547 case 8: data->fan_div[nr] = 3; break;
548 default:
549 dev_err(&client->dev, "fan_div value %ld not supported."
550 "Choose one of 1, 2, 4 or 8!\n", val);
551 up(&data->update_lock);
552 return -EINVAL;
555 /* Correct the fan minimum speed */
556 data->fan_min[nr] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
557 vt8231_write_value(client, VT8231_REG_FAN_MIN(nr), data->fan_min[nr]);
559 old = (old & 0x0f) | (data->fan_div[1] << 6) | (data->fan_div[0] << 4);
560 vt8231_write_value(client, VT8231_REG_FANDIV, old);
561 up(&data->update_lock);
562 return count;
566 #define define_fan_sysfs(offset) \
567 static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
568 show_fan, NULL, offset - 1); \
569 static SENSOR_DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
570 show_fan_div, set_fan_div, offset - 1); \
571 static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
572 show_fan_min, set_fan_min, offset - 1)
574 define_fan_sysfs(1);
575 define_fan_sysfs(2);
577 /* Alarms */
578 static ssize_t show_alarms(struct device *dev, struct device_attribute *attr,
579 char *buf)
581 struct vt8231_data *data = vt8231_update_device(dev);
582 return sprintf(buf, "%d\n", data->alarms);
585 static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
587 static struct i2c_driver vt8231_driver = {
588 .driver = {
589 .name = "vt8231",
591 .attach_adapter = vt8231_detect,
592 .detach_client = vt8231_detach_client,
595 static struct pci_device_id vt8231_pci_ids[] = {
596 { PCI_DEVICE(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8231_4) },
597 { 0, }
600 MODULE_DEVICE_TABLE(pci, vt8231_pci_ids);
602 static int __devinit vt8231_pci_probe(struct pci_dev *dev,
603 const struct pci_device_id *id);
605 static struct pci_driver vt8231_pci_driver = {
606 .name = "vt8231",
607 .id_table = vt8231_pci_ids,
608 .probe = vt8231_pci_probe,
611 int vt8231_detect(struct i2c_adapter *adapter)
613 struct i2c_client *client;
614 struct vt8231_data *data;
615 int err = 0, i;
616 u16 val;
618 /* 8231 requires multiple of 256 */
619 if (force_addr) {
620 isa_address = force_addr & 0xFF00;
621 dev_warn(&adapter->dev, "forcing ISA address 0x%04X\n",
622 isa_address);
623 if (PCIBIOS_SUCCESSFUL != pci_write_config_word(s_bridge,
624 VT8231_BASE_REG, isa_address))
625 return -ENODEV;
628 if (PCIBIOS_SUCCESSFUL !=
629 pci_read_config_word(s_bridge, VT8231_ENABLE_REG, &val))
630 return -ENODEV;
632 if (!(val & 0x0001)) {
633 dev_warn(&adapter->dev, "enabling sensors\n");
634 if (PCIBIOS_SUCCESSFUL !=
635 pci_write_config_word(s_bridge, VT8231_ENABLE_REG,
636 val | 0x0001))
637 return -ENODEV;
640 /* Reserve the ISA region */
641 if (!request_region(isa_address, VT8231_EXTENT,
642 vt8231_pci_driver.name)) {
643 dev_err(&adapter->dev, "region 0x%x already in use!\n",
644 isa_address);
645 return -ENODEV;
648 if (!(data = kzalloc(sizeof(struct vt8231_data), GFP_KERNEL))) {
649 err = -ENOMEM;
650 goto exit_release;
653 client = &data->client;
654 i2c_set_clientdata(client, data);
655 client->addr = isa_address;
656 client->adapter = adapter;
657 client->driver = &vt8231_driver;
658 client->dev.parent = &adapter->dev;
660 /* Fill in the remaining client fields and put into the global list */
661 strlcpy(client->name, "vt8231", I2C_NAME_SIZE);
663 init_MUTEX(&data->update_lock);
665 /* Tell the I2C layer a new client has arrived */
666 if ((err = i2c_attach_client(client)))
667 goto exit_free;
669 vt8231_init_client(client);
671 /* Register sysfs hooks */
672 data->class_dev = hwmon_device_register(&client->dev);
673 if (IS_ERR(data->class_dev)) {
674 err = PTR_ERR(data->class_dev);
675 goto exit_detach;
678 /* Must update device information to find out the config field */
679 data->uch_config = vt8231_read_value(client, VT8231_REG_UCH_CONFIG);
681 for (i = 0; i < ARRAY_SIZE(cfg_info_temp); i++) {
682 if (ISTEMP(i, data->uch_config)) {
683 device_create_file(&client->dev,
684 cfg_info_temp[i].input);
685 device_create_file(&client->dev, cfg_info_temp[i].max);
686 device_create_file(&client->dev, cfg_info_temp[i].min);
690 for (i = 0; i < ARRAY_SIZE(cfg_info_volt); i++) {
691 if (ISVOLT(i, data->uch_config)) {
692 device_create_file(&client->dev,
693 cfg_info_volt[i].input);
694 device_create_file(&client->dev, cfg_info_volt[i].max);
695 device_create_file(&client->dev, cfg_info_volt[i].min);
699 device_create_file(&client->dev, &sensor_dev_attr_fan1_input.dev_attr);
700 device_create_file(&client->dev, &sensor_dev_attr_fan2_input.dev_attr);
701 device_create_file(&client->dev, &sensor_dev_attr_fan1_min.dev_attr);
702 device_create_file(&client->dev, &sensor_dev_attr_fan2_min.dev_attr);
703 device_create_file(&client->dev, &sensor_dev_attr_fan1_div.dev_attr);
704 device_create_file(&client->dev, &sensor_dev_attr_fan2_div.dev_attr);
706 device_create_file(&client->dev, &dev_attr_alarms);
707 return 0;
709 exit_detach:
710 i2c_detach_client(client);
711 exit_free:
712 kfree(data);
713 exit_release:
714 release_region(isa_address, VT8231_EXTENT);
715 return err;
718 static int vt8231_detach_client(struct i2c_client *client)
720 struct vt8231_data *data = i2c_get_clientdata(client);
721 int err;
723 hwmon_device_unregister(data->class_dev);
725 if ((err = i2c_detach_client(client))) {
726 return err;
729 release_region(client->addr, VT8231_EXTENT);
730 kfree(data);
732 return 0;
735 static void vt8231_init_client(struct i2c_client *client)
737 vt8231_write_value(client, VT8231_REG_TEMP1_CONFIG, 0);
738 vt8231_write_value(client, VT8231_REG_TEMP2_CONFIG, 0);
741 static struct vt8231_data *vt8231_update_device(struct device *dev)
743 struct i2c_client *client = to_i2c_client(dev);
744 struct vt8231_data *data = i2c_get_clientdata(client);
745 int i;
746 u16 low;
748 down(&data->update_lock);
750 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
751 || !data->valid) {
752 for (i = 0; i < 6; i++) {
753 if (ISVOLT(i, data->uch_config)) {
754 data->in[i] = vt8231_read_value(client,
755 regvolt[i]);
756 data->in_min[i] = vt8231_read_value(client,
757 regvoltmin[i]);
758 data->in_max[i] = vt8231_read_value(client,
759 regvoltmax[i]);
762 for (i = 0; i < 2; i++) {
763 data->fan[i] = vt8231_read_value(client,
764 VT8231_REG_FAN(i));
765 data->fan_min[i] = vt8231_read_value(client,
766 VT8231_REG_FAN_MIN(i));
769 low = vt8231_read_value(client, VT8231_REG_TEMP_LOW01);
770 low = (low >> 6) | ((low & 0x30) >> 2)
771 | (vt8231_read_value(client, VT8231_REG_TEMP_LOW25) << 4);
772 for (i = 0; i < 6; i++) {
773 if (ISTEMP(i, data->uch_config)) {
774 data->temp[i] = (vt8231_read_value(client,
775 regtemp[i]) << 2)
776 | ((low >> (2 * i)) & 0x03);
777 data->temp_max[i] = vt8231_read_value(client,
778 regtempmax[i]);
779 data->temp_min[i] = vt8231_read_value(client,
780 regtempmin[i]);
784 i = vt8231_read_value(client, VT8231_REG_FANDIV);
785 data->fan_div[0] = (i >> 4) & 0x03;
786 data->fan_div[1] = i >> 6;
787 data->alarms = vt8231_read_value(client, VT8231_REG_ALARM1) |
788 (vt8231_read_value(client, VT8231_REG_ALARM2) << 8);
790 /* Set alarm flags correctly */
791 if (!data->fan[0] && data->fan_min[0]) {
792 data->alarms |= 0x40;
793 } else if (data->fan[0] && !data->fan_min[0]) {
794 data->alarms &= ~0x40;
797 if (!data->fan[1] && data->fan_min[1]) {
798 data->alarms |= 0x80;
799 } else if (data->fan[1] && !data->fan_min[1]) {
800 data->alarms &= ~0x80;
803 data->last_updated = jiffies;
804 data->valid = 1;
807 up(&data->update_lock);
809 return data;
812 static int __devinit vt8231_pci_probe(struct pci_dev *dev,
813 const struct pci_device_id *id)
815 u16 val;
817 if (PCIBIOS_SUCCESSFUL != pci_read_config_word(dev, VT8231_BASE_REG,
818 &val))
819 return -ENODEV;
821 isa_address = val & ~(VT8231_EXTENT - 1);
822 if (isa_address == 0 && force_addr == 0) {
823 dev_err(&dev->dev, "base address not set -\
824 upgrade BIOS or use force_addr=0xaddr\n");
825 return -ENODEV;
828 s_bridge = pci_dev_get(dev);
830 if (i2c_isa_add_driver(&vt8231_driver)) {
831 pci_dev_put(s_bridge);
832 s_bridge = NULL;
835 /* Always return failure here. This is to allow other drivers to bind
836 * to this pci device. We don't really want to have control over the
837 * pci device, we only wanted to read as few register values from it.
839 return -ENODEV;
842 static int __init sm_vt8231_init(void)
844 return pci_module_init(&vt8231_pci_driver);
847 static void __exit sm_vt8231_exit(void)
849 pci_unregister_driver(&vt8231_pci_driver);
850 if (s_bridge != NULL) {
851 i2c_isa_del_driver(&vt8231_driver);
852 pci_dev_put(s_bridge);
853 s_bridge = NULL;
857 MODULE_AUTHOR("Roger Lucas <roger@planbit.co.uk>");
858 MODULE_DESCRIPTION("VT8231 sensors");
859 MODULE_LICENSE("GPL");
861 module_init(sm_vt8231_init);
862 module_exit(sm_vt8231_exit);