Linux 2.6.17.7
[linux/fpc-iii.git] / drivers / hwmon / vt8231.c
blob686f3deb3093844f4129a338b8e441052225b344
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 <linux/mutex.h>
39 #include <asm/io.h>
41 static int force_addr;
42 module_param(force_addr, int, 0);
43 MODULE_PARM_DESC(force_addr, "Initialize the base address of the sensors");
45 /* Device address
46 Note that we can't determine the ISA address until we have initialized
47 our module */
48 static unsigned short isa_address;
50 #define VT8231_EXTENT 0x80
51 #define VT8231_BASE_REG 0x70
52 #define VT8231_ENABLE_REG 0x74
54 /* The VT8231 registers
56 The reset value for the input channel configuration is used (Reg 0x4A=0x07)
57 which sets the selected inputs marked with '*' below if multiple options are
58 possible:
60 Voltage Mode Temperature Mode
61 Sensor Linux Id Linux Id VIA Id
62 -------- -------- -------- ------
63 CPU Diode N/A temp1 0
64 UIC1 in0 temp2 * 1
65 UIC2 in1 * temp3 2
66 UIC3 in2 * temp4 3
67 UIC4 in3 * temp5 4
68 UIC5 in4 * temp6 5
69 3.3V in5 N/A
71 Note that the BIOS may set the configuration register to a different value
72 to match the motherboard configuration.
75 /* fans numbered 0-1 */
76 #define VT8231_REG_FAN_MIN(nr) (0x3b + (nr))
77 #define VT8231_REG_FAN(nr) (0x29 + (nr))
79 /* Voltage inputs numbered 0-5 */
81 static const u8 regvolt[] = { 0x21, 0x22, 0x23, 0x24, 0x25, 0x26 };
82 static const u8 regvoltmax[] = { 0x3d, 0x2b, 0x2d, 0x2f, 0x31, 0x33 };
83 static const u8 regvoltmin[] = { 0x3e, 0x2c, 0x2e, 0x30, 0x32, 0x34 };
85 /* Temperatures are numbered 1-6 according to the Linux kernel specification.
87 ** In the VIA datasheet, however, the temperatures are numbered from zero.
88 ** Since it is important that this driver can easily be compared to the VIA
89 ** datasheet, we will use the VIA numbering within this driver and map the
90 ** kernel sysfs device name to the VIA number in the sysfs callback.
93 #define VT8231_REG_TEMP_LOW01 0x49
94 #define VT8231_REG_TEMP_LOW25 0x4d
96 static const u8 regtemp[] = { 0x1f, 0x21, 0x22, 0x23, 0x24, 0x25 };
97 static const u8 regtempmax[] = { 0x39, 0x3d, 0x2b, 0x2d, 0x2f, 0x31 };
98 static const u8 regtempmin[] = { 0x3a, 0x3e, 0x2c, 0x2e, 0x30, 0x32 };
100 #define TEMP_FROM_REG(reg) (((253 * 4 - (reg)) * 550 + 105) / 210)
101 #define TEMP_MAXMIN_FROM_REG(reg) (((253 - (reg)) * 2200 + 105) / 210)
102 #define TEMP_MAXMIN_TO_REG(val) (253 - ((val) * 210 + 1100) / 2200)
104 #define VT8231_REG_CONFIG 0x40
105 #define VT8231_REG_ALARM1 0x41
106 #define VT8231_REG_ALARM2 0x42
107 #define VT8231_REG_FANDIV 0x47
108 #define VT8231_REG_UCH_CONFIG 0x4a
109 #define VT8231_REG_TEMP1_CONFIG 0x4b
110 #define VT8231_REG_TEMP2_CONFIG 0x4c
112 /* temps 0-5 as numbered in VIA datasheet - see later for mapping to Linux
113 ** numbering
115 #define ISTEMP(i, ch_config) ((i) == 0 ? 1 : \
116 ((ch_config) >> ((i)+1)) & 0x01)
117 /* voltages 0-5 */
118 #define ISVOLT(i, ch_config) ((i) == 5 ? 1 : \
119 !(((ch_config) >> ((i)+2)) & 0x01))
121 #define DIV_FROM_REG(val) (1 << (val))
123 /* NB The values returned here are NOT temperatures. The calibration curves
124 ** for the thermistor curves are board-specific and must go in the
125 ** sensors.conf file. Temperature sensors are actually ten bits, but the
126 ** VIA datasheet only considers the 8 MSBs obtained from the regtemp[]
127 ** register. The temperature value returned should have a magnitude of 3,
128 ** so we use the VIA scaling as the "true" scaling and use the remaining 2
129 ** LSBs as fractional precision.
131 ** All the on-chip hardware temperature comparisons for the alarms are only
132 ** 8-bits wide, and compare against the 8 MSBs of the temperature. The bits
133 ** in the registers VT8231_REG_TEMP_LOW01 and VT8231_REG_TEMP_LOW25 are
134 ** ignored.
137 /******** FAN RPM CONVERSIONS ********
138 ** This chip saturates back at 0, not at 255 like many the other chips.
139 ** So, 0 means 0 RPM
141 static inline u8 FAN_TO_REG(long rpm, int div)
143 if (rpm == 0)
144 return 0;
145 return SENSORS_LIMIT(1310720 / (rpm * div), 1, 255);
148 #define FAN_FROM_REG(val, div) ((val) == 0 ? 0 : 1310720 / ((val) * (div)))
150 struct vt8231_data {
151 struct i2c_client client;
152 struct mutex update_lock;
153 struct class_device *class_dev;
154 char valid; /* !=0 if following fields are valid */
155 unsigned long last_updated; /* In jiffies */
157 u8 in[6]; /* Register value */
158 u8 in_max[6]; /* Register value */
159 u8 in_min[6]; /* Register value */
160 u16 temp[6]; /* Register value 10 bit, right aligned */
161 u8 temp_max[6]; /* Register value */
162 u8 temp_min[6]; /* Register value */
163 u8 fan[2]; /* Register value */
164 u8 fan_min[2]; /* Register value */
165 u8 fan_div[2]; /* Register encoding, shifted right */
166 u16 alarms; /* Register encoding */
167 u8 uch_config;
170 static struct pci_dev *s_bridge;
171 static int vt8231_detect(struct i2c_adapter *adapter);
172 static int vt8231_detach_client(struct i2c_client *client);
173 static struct vt8231_data *vt8231_update_device(struct device *dev);
174 static void vt8231_init_client(struct i2c_client *client);
176 static inline int vt8231_read_value(struct i2c_client *client, u8 reg)
178 return inb_p(client->addr + reg);
181 static inline void vt8231_write_value(struct i2c_client *client, u8 reg,
182 u8 value)
184 outb_p(value, client->addr + reg);
187 /* following are the sysfs callback functions */
188 static ssize_t show_in(struct device *dev, struct device_attribute *attr,
189 char *buf)
191 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
192 int nr = sensor_attr->index;
193 struct vt8231_data *data = vt8231_update_device(dev);
195 return sprintf(buf, "%d\n", ((data->in[nr] - 3) * 10000) / 958);
198 static ssize_t show_in_min(struct device *dev, struct device_attribute *attr,
199 char *buf)
201 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
202 int nr = sensor_attr->index;
203 struct vt8231_data *data = vt8231_update_device(dev);
205 return sprintf(buf, "%d\n", ((data->in_min[nr] - 3) * 10000) / 958);
208 static ssize_t show_in_max(struct device *dev, struct device_attribute *attr,
209 char *buf)
211 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
212 int nr = sensor_attr->index;
213 struct vt8231_data *data = vt8231_update_device(dev);
215 return sprintf(buf, "%d\n", (((data->in_max[nr] - 3) * 10000) / 958));
218 static ssize_t set_in_min(struct device *dev, struct device_attribute *attr,
219 const char *buf, size_t count)
221 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
222 int nr = sensor_attr->index;
223 struct i2c_client *client = to_i2c_client(dev);
224 struct vt8231_data *data = i2c_get_clientdata(client);
225 unsigned long val = simple_strtoul(buf, NULL, 10);
227 mutex_lock(&data->update_lock);
228 data->in_min[nr] = SENSORS_LIMIT(((val * 958) / 10000) + 3, 0, 255);
229 vt8231_write_value(client, regvoltmin[nr], data->in_min[nr]);
230 mutex_unlock(&data->update_lock);
231 return count;
234 static ssize_t set_in_max(struct device *dev, struct device_attribute *attr,
235 const char *buf, size_t count)
237 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
238 int nr = sensor_attr->index;
239 struct i2c_client *client = to_i2c_client(dev);
240 struct vt8231_data *data = i2c_get_clientdata(client);
241 unsigned long val = simple_strtoul(buf, NULL, 10);
243 mutex_lock(&data->update_lock);
244 data->in_max[nr] = SENSORS_LIMIT(((val * 958) / 10000) + 3, 0, 255);
245 vt8231_write_value(client, regvoltmax[nr], data->in_max[nr]);
246 mutex_unlock(&data->update_lock);
247 return count;
250 /* Special case for input 5 as this has 3.3V scaling built into the chip */
251 static ssize_t show_in5(struct device *dev, struct device_attribute *attr,
252 char *buf)
254 struct vt8231_data *data = vt8231_update_device(dev);
256 return sprintf(buf, "%d\n",
257 (((data->in[5] - 3) * 10000 * 54) / (958 * 34)));
260 static ssize_t show_in5_min(struct device *dev, struct device_attribute *attr,
261 char *buf)
263 struct vt8231_data *data = vt8231_update_device(dev);
265 return sprintf(buf, "%d\n",
266 (((data->in_min[5] - 3) * 10000 * 54) / (958 * 34)));
269 static ssize_t show_in5_max(struct device *dev, struct device_attribute *attr,
270 char *buf)
272 struct vt8231_data *data = vt8231_update_device(dev);
274 return sprintf(buf, "%d\n",
275 (((data->in_max[5] - 3) * 10000 * 54) / (958 * 34)));
278 static ssize_t set_in5_min(struct device *dev, struct device_attribute *attr,
279 const char *buf, size_t count)
281 struct i2c_client *client = to_i2c_client(dev);
282 struct vt8231_data *data = i2c_get_clientdata(client);
283 unsigned long val = simple_strtoul(buf, NULL, 10);
285 mutex_lock(&data->update_lock);
286 data->in_min[5] = SENSORS_LIMIT(((val * 958 * 34) / (10000 * 54)) + 3,
287 0, 255);
288 vt8231_write_value(client, regvoltmin[5], data->in_min[5]);
289 mutex_unlock(&data->update_lock);
290 return count;
293 static ssize_t set_in5_max(struct device *dev, struct device_attribute *attr,
294 const char *buf, size_t count)
296 struct i2c_client *client = to_i2c_client(dev);
297 struct vt8231_data *data = i2c_get_clientdata(client);
298 unsigned long val = simple_strtoul(buf, NULL, 10);
300 mutex_lock(&data->update_lock);
301 data->in_max[5] = SENSORS_LIMIT(((val * 958 * 34) / (10000 * 54)) + 3,
302 0, 255);
303 vt8231_write_value(client, regvoltmax[5], data->in_max[5]);
304 mutex_unlock(&data->update_lock);
305 return count;
308 #define define_voltage_sysfs(offset) \
309 static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \
310 show_in, NULL, offset); \
311 static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
312 show_in_min, set_in_min, offset); \
313 static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
314 show_in_max, set_in_max, offset)
316 define_voltage_sysfs(0);
317 define_voltage_sysfs(1);
318 define_voltage_sysfs(2);
319 define_voltage_sysfs(3);
320 define_voltage_sysfs(4);
322 static DEVICE_ATTR(in5_input, S_IRUGO, show_in5, NULL);
323 static DEVICE_ATTR(in5_min, S_IRUGO | S_IWUSR, show_in5_min, set_in5_min);
324 static DEVICE_ATTR(in5_max, S_IRUGO | S_IWUSR, show_in5_max, set_in5_max);
326 /* Temperatures */
327 static ssize_t show_temp0(struct device *dev, struct device_attribute *attr,
328 char *buf)
330 struct vt8231_data *data = vt8231_update_device(dev);
331 return sprintf(buf, "%d\n", data->temp[0] * 250);
334 static ssize_t show_temp0_max(struct device *dev, struct device_attribute *attr,
335 char *buf)
337 struct vt8231_data *data = vt8231_update_device(dev);
338 return sprintf(buf, "%d\n", data->temp_max[0] * 1000);
341 static ssize_t show_temp0_min(struct device *dev, struct device_attribute *attr,
342 char *buf)
344 struct vt8231_data *data = vt8231_update_device(dev);
345 return sprintf(buf, "%d\n", data->temp_min[0] * 1000);
348 static ssize_t set_temp0_max(struct device *dev, struct device_attribute *attr,
349 const char *buf, size_t count)
351 struct i2c_client *client = to_i2c_client(dev);
352 struct vt8231_data *data = i2c_get_clientdata(client);
353 int val = simple_strtol(buf, NULL, 10);
355 mutex_lock(&data->update_lock);
356 data->temp_max[0] = SENSORS_LIMIT((val + 500) / 1000, 0, 255);
357 vt8231_write_value(client, regtempmax[0], data->temp_max[0]);
358 mutex_unlock(&data->update_lock);
359 return count;
361 static ssize_t set_temp0_min(struct device *dev, struct device_attribute *attr,
362 const char *buf, size_t count)
364 struct i2c_client *client = to_i2c_client(dev);
365 struct vt8231_data *data = i2c_get_clientdata(client);
366 int val = simple_strtol(buf, NULL, 10);
368 mutex_lock(&data->update_lock);
369 data->temp_min[0] = SENSORS_LIMIT((val + 500) / 1000, 0, 255);
370 vt8231_write_value(client, regtempmin[0], data->temp_min[0]);
371 mutex_unlock(&data->update_lock);
372 return count;
375 static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
376 char *buf)
378 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
379 int nr = sensor_attr->index;
380 struct vt8231_data *data = vt8231_update_device(dev);
381 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr]));
384 static ssize_t show_temp_max(struct device *dev, struct device_attribute *attr,
385 char *buf)
387 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
388 int nr = sensor_attr->index;
389 struct vt8231_data *data = vt8231_update_device(dev);
390 return sprintf(buf, "%d\n", TEMP_MAXMIN_FROM_REG(data->temp_max[nr]));
393 static ssize_t show_temp_min(struct device *dev, struct device_attribute *attr,
394 char *buf)
396 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
397 int nr = sensor_attr->index;
398 struct vt8231_data *data = vt8231_update_device(dev);
399 return sprintf(buf, "%d\n", TEMP_MAXMIN_FROM_REG(data->temp_min[nr]));
402 static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
403 const char *buf, size_t count)
405 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
406 int nr = sensor_attr->index;
407 struct i2c_client *client = to_i2c_client(dev);
408 struct vt8231_data *data = i2c_get_clientdata(client);
409 int val = simple_strtol(buf, NULL, 10);
411 mutex_lock(&data->update_lock);
412 data->temp_max[nr] = SENSORS_LIMIT(TEMP_MAXMIN_TO_REG(val), 0, 255);
413 vt8231_write_value(client, regtempmax[nr], data->temp_max[nr]);
414 mutex_unlock(&data->update_lock);
415 return count;
417 static ssize_t set_temp_min(struct device *dev, struct device_attribute *attr,
418 const char *buf, size_t count)
420 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
421 int nr = sensor_attr->index;
422 struct i2c_client *client = to_i2c_client(dev);
423 struct vt8231_data *data = i2c_get_clientdata(client);
424 int val = simple_strtol(buf, NULL, 10);
426 mutex_lock(&data->update_lock);
427 data->temp_min[nr] = SENSORS_LIMIT(TEMP_MAXMIN_TO_REG(val), 0, 255);
428 vt8231_write_value(client, regtempmin[nr], data->temp_min[nr]);
429 mutex_unlock(&data->update_lock);
430 return count;
433 /* Note that these map the Linux temperature sensor numbering (1-6) to the VIA
434 ** temperature sensor numbering (0-5)
436 #define define_temperature_sysfs(offset) \
437 static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
438 show_temp, NULL, offset - 1); \
439 static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
440 show_temp_max, set_temp_max, offset - 1); \
441 static SENSOR_DEVICE_ATTR(temp##offset##_max_hyst, S_IRUGO | S_IWUSR, \
442 show_temp_min, set_temp_min, offset - 1)
444 static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp0, NULL);
445 static DEVICE_ATTR(temp1_max, S_IRUGO | S_IWUSR, show_temp0_max, set_temp0_max);
446 static DEVICE_ATTR(temp1_max_hyst, S_IRUGO | S_IWUSR, show_temp0_min, set_temp0_min);
448 define_temperature_sysfs(2);
449 define_temperature_sysfs(3);
450 define_temperature_sysfs(4);
451 define_temperature_sysfs(5);
452 define_temperature_sysfs(6);
454 #define CFG_INFO_TEMP(id) { &sensor_dev_attr_temp##id##_input.dev_attr, \
455 &sensor_dev_attr_temp##id##_max_hyst.dev_attr, \
456 &sensor_dev_attr_temp##id##_max.dev_attr }
457 #define CFG_INFO_VOLT(id) { &sensor_dev_attr_in##id##_input.dev_attr, \
458 &sensor_dev_attr_in##id##_min.dev_attr, \
459 &sensor_dev_attr_in##id##_max.dev_attr }
461 struct str_device_attr_table {
462 struct device_attribute *input;
463 struct device_attribute *min;
464 struct device_attribute *max;
467 static struct str_device_attr_table cfg_info_temp[] = {
468 { &dev_attr_temp1_input, &dev_attr_temp1_max_hyst, &dev_attr_temp1_max },
469 CFG_INFO_TEMP(2),
470 CFG_INFO_TEMP(3),
471 CFG_INFO_TEMP(4),
472 CFG_INFO_TEMP(5),
473 CFG_INFO_TEMP(6)
476 static struct str_device_attr_table cfg_info_volt[] = {
477 CFG_INFO_VOLT(0),
478 CFG_INFO_VOLT(1),
479 CFG_INFO_VOLT(2),
480 CFG_INFO_VOLT(3),
481 CFG_INFO_VOLT(4),
482 { &dev_attr_in5_input, &dev_attr_in5_min, &dev_attr_in5_max }
485 /* Fans */
486 static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
487 char *buf)
489 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
490 int nr = sensor_attr->index;
491 struct vt8231_data *data = vt8231_update_device(dev);
492 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr],
493 DIV_FROM_REG(data->fan_div[nr])));
496 static ssize_t show_fan_min(struct device *dev, struct device_attribute *attr,
497 char *buf)
499 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
500 int nr = sensor_attr->index;
501 struct vt8231_data *data = vt8231_update_device(dev);
502 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[nr],
503 DIV_FROM_REG(data->fan_div[nr])));
506 static ssize_t show_fan_div(struct device *dev, struct device_attribute *attr,
507 char *buf)
509 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
510 int nr = sensor_attr->index;
511 struct vt8231_data *data = vt8231_update_device(dev);
512 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]));
515 static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
516 const char *buf, size_t count)
518 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
519 int nr = sensor_attr->index;
520 struct i2c_client *client = to_i2c_client(dev);
521 struct vt8231_data *data = i2c_get_clientdata(client);
522 int val = simple_strtoul(buf, NULL, 10);
524 mutex_lock(&data->update_lock);
525 data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
526 vt8231_write_value(client, VT8231_REG_FAN_MIN(nr), data->fan_min[nr]);
527 mutex_unlock(&data->update_lock);
528 return count;
531 static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
532 const char *buf, size_t count)
534 struct i2c_client *client = to_i2c_client(dev);
535 struct vt8231_data *data = i2c_get_clientdata(client);
536 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
537 unsigned long val = simple_strtoul(buf, NULL, 10);
538 int nr = sensor_attr->index;
539 int old = vt8231_read_value(client, VT8231_REG_FANDIV);
540 long min = FAN_FROM_REG(data->fan_min[nr],
541 DIV_FROM_REG(data->fan_div[nr]));
543 mutex_lock(&data->update_lock);
544 switch (val) {
545 case 1: data->fan_div[nr] = 0; break;
546 case 2: data->fan_div[nr] = 1; break;
547 case 4: data->fan_div[nr] = 2; break;
548 case 8: data->fan_div[nr] = 3; break;
549 default:
550 dev_err(&client->dev, "fan_div value %ld not supported."
551 "Choose one of 1, 2, 4 or 8!\n", val);
552 mutex_unlock(&data->update_lock);
553 return -EINVAL;
556 /* Correct the fan minimum speed */
557 data->fan_min[nr] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
558 vt8231_write_value(client, VT8231_REG_FAN_MIN(nr), data->fan_min[nr]);
560 old = (old & 0x0f) | (data->fan_div[1] << 6) | (data->fan_div[0] << 4);
561 vt8231_write_value(client, VT8231_REG_FANDIV, old);
562 mutex_unlock(&data->update_lock);
563 return count;
567 #define define_fan_sysfs(offset) \
568 static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
569 show_fan, NULL, offset - 1); \
570 static SENSOR_DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
571 show_fan_div, set_fan_div, offset - 1); \
572 static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
573 show_fan_min, set_fan_min, offset - 1)
575 define_fan_sysfs(1);
576 define_fan_sysfs(2);
578 /* Alarms */
579 static ssize_t show_alarms(struct device *dev, struct device_attribute *attr,
580 char *buf)
582 struct vt8231_data *data = vt8231_update_device(dev);
583 return sprintf(buf, "%d\n", data->alarms);
586 static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
588 static struct i2c_driver vt8231_driver = {
589 .driver = {
590 .name = "vt8231",
592 .attach_adapter = vt8231_detect,
593 .detach_client = vt8231_detach_client,
596 static struct pci_device_id vt8231_pci_ids[] = {
597 { PCI_DEVICE(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8231_4) },
598 { 0, }
601 MODULE_DEVICE_TABLE(pci, vt8231_pci_ids);
603 static int __devinit vt8231_pci_probe(struct pci_dev *dev,
604 const struct pci_device_id *id);
606 static struct pci_driver vt8231_pci_driver = {
607 .name = "vt8231",
608 .id_table = vt8231_pci_ids,
609 .probe = vt8231_pci_probe,
612 int vt8231_detect(struct i2c_adapter *adapter)
614 struct i2c_client *client;
615 struct vt8231_data *data;
616 int err = 0, i;
617 u16 val;
619 /* 8231 requires multiple of 256 */
620 if (force_addr) {
621 isa_address = force_addr & 0xFF00;
622 dev_warn(&adapter->dev, "forcing ISA address 0x%04X\n",
623 isa_address);
624 if (PCIBIOS_SUCCESSFUL != pci_write_config_word(s_bridge,
625 VT8231_BASE_REG, isa_address))
626 return -ENODEV;
629 if (PCIBIOS_SUCCESSFUL !=
630 pci_read_config_word(s_bridge, VT8231_ENABLE_REG, &val))
631 return -ENODEV;
633 if (!(val & 0x0001)) {
634 dev_warn(&adapter->dev, "enabling sensors\n");
635 if (PCIBIOS_SUCCESSFUL !=
636 pci_write_config_word(s_bridge, VT8231_ENABLE_REG,
637 val | 0x0001))
638 return -ENODEV;
641 /* Reserve the ISA region */
642 if (!request_region(isa_address, VT8231_EXTENT,
643 vt8231_pci_driver.name)) {
644 dev_err(&adapter->dev, "region 0x%x already in use!\n",
645 isa_address);
646 return -ENODEV;
649 if (!(data = kzalloc(sizeof(struct vt8231_data), GFP_KERNEL))) {
650 err = -ENOMEM;
651 goto exit_release;
654 client = &data->client;
655 i2c_set_clientdata(client, data);
656 client->addr = isa_address;
657 client->adapter = adapter;
658 client->driver = &vt8231_driver;
659 client->dev.parent = &adapter->dev;
661 /* Fill in the remaining client fields and put into the global list */
662 strlcpy(client->name, "vt8231", I2C_NAME_SIZE);
664 mutex_init(&data->update_lock);
666 /* Tell the I2C layer a new client has arrived */
667 if ((err = i2c_attach_client(client)))
668 goto exit_free;
670 vt8231_init_client(client);
672 /* Register sysfs hooks */
673 data->class_dev = hwmon_device_register(&client->dev);
674 if (IS_ERR(data->class_dev)) {
675 err = PTR_ERR(data->class_dev);
676 goto exit_detach;
679 /* Must update device information to find out the config field */
680 data->uch_config = vt8231_read_value(client, VT8231_REG_UCH_CONFIG);
682 for (i = 0; i < ARRAY_SIZE(cfg_info_temp); i++) {
683 if (ISTEMP(i, data->uch_config)) {
684 device_create_file(&client->dev,
685 cfg_info_temp[i].input);
686 device_create_file(&client->dev, cfg_info_temp[i].max);
687 device_create_file(&client->dev, cfg_info_temp[i].min);
691 for (i = 0; i < ARRAY_SIZE(cfg_info_volt); i++) {
692 if (ISVOLT(i, data->uch_config)) {
693 device_create_file(&client->dev,
694 cfg_info_volt[i].input);
695 device_create_file(&client->dev, cfg_info_volt[i].max);
696 device_create_file(&client->dev, cfg_info_volt[i].min);
700 device_create_file(&client->dev, &sensor_dev_attr_fan1_input.dev_attr);
701 device_create_file(&client->dev, &sensor_dev_attr_fan2_input.dev_attr);
702 device_create_file(&client->dev, &sensor_dev_attr_fan1_min.dev_attr);
703 device_create_file(&client->dev, &sensor_dev_attr_fan2_min.dev_attr);
704 device_create_file(&client->dev, &sensor_dev_attr_fan1_div.dev_attr);
705 device_create_file(&client->dev, &sensor_dev_attr_fan2_div.dev_attr);
707 device_create_file(&client->dev, &dev_attr_alarms);
708 return 0;
710 exit_detach:
711 i2c_detach_client(client);
712 exit_free:
713 kfree(data);
714 exit_release:
715 release_region(isa_address, VT8231_EXTENT);
716 return err;
719 static int vt8231_detach_client(struct i2c_client *client)
721 struct vt8231_data *data = i2c_get_clientdata(client);
722 int err;
724 hwmon_device_unregister(data->class_dev);
726 if ((err = i2c_detach_client(client))) {
727 return err;
730 release_region(client->addr, VT8231_EXTENT);
731 kfree(data);
733 return 0;
736 static void vt8231_init_client(struct i2c_client *client)
738 vt8231_write_value(client, VT8231_REG_TEMP1_CONFIG, 0);
739 vt8231_write_value(client, VT8231_REG_TEMP2_CONFIG, 0);
742 static struct vt8231_data *vt8231_update_device(struct device *dev)
744 struct i2c_client *client = to_i2c_client(dev);
745 struct vt8231_data *data = i2c_get_clientdata(client);
746 int i;
747 u16 low;
749 mutex_lock(&data->update_lock);
751 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
752 || !data->valid) {
753 for (i = 0; i < 6; i++) {
754 if (ISVOLT(i, data->uch_config)) {
755 data->in[i] = vt8231_read_value(client,
756 regvolt[i]);
757 data->in_min[i] = vt8231_read_value(client,
758 regvoltmin[i]);
759 data->in_max[i] = vt8231_read_value(client,
760 regvoltmax[i]);
763 for (i = 0; i < 2; i++) {
764 data->fan[i] = vt8231_read_value(client,
765 VT8231_REG_FAN(i));
766 data->fan_min[i] = vt8231_read_value(client,
767 VT8231_REG_FAN_MIN(i));
770 low = vt8231_read_value(client, VT8231_REG_TEMP_LOW01);
771 low = (low >> 6) | ((low & 0x30) >> 2)
772 | (vt8231_read_value(client, VT8231_REG_TEMP_LOW25) << 4);
773 for (i = 0; i < 6; i++) {
774 if (ISTEMP(i, data->uch_config)) {
775 data->temp[i] = (vt8231_read_value(client,
776 regtemp[i]) << 2)
777 | ((low >> (2 * i)) & 0x03);
778 data->temp_max[i] = vt8231_read_value(client,
779 regtempmax[i]);
780 data->temp_min[i] = vt8231_read_value(client,
781 regtempmin[i]);
785 i = vt8231_read_value(client, VT8231_REG_FANDIV);
786 data->fan_div[0] = (i >> 4) & 0x03;
787 data->fan_div[1] = i >> 6;
788 data->alarms = vt8231_read_value(client, VT8231_REG_ALARM1) |
789 (vt8231_read_value(client, VT8231_REG_ALARM2) << 8);
791 /* Set alarm flags correctly */
792 if (!data->fan[0] && data->fan_min[0]) {
793 data->alarms |= 0x40;
794 } else if (data->fan[0] && !data->fan_min[0]) {
795 data->alarms &= ~0x40;
798 if (!data->fan[1] && data->fan_min[1]) {
799 data->alarms |= 0x80;
800 } else if (data->fan[1] && !data->fan_min[1]) {
801 data->alarms &= ~0x80;
804 data->last_updated = jiffies;
805 data->valid = 1;
808 mutex_unlock(&data->update_lock);
810 return data;
813 static int __devinit vt8231_pci_probe(struct pci_dev *dev,
814 const struct pci_device_id *id)
816 u16 val;
818 if (PCIBIOS_SUCCESSFUL != pci_read_config_word(dev, VT8231_BASE_REG,
819 &val))
820 return -ENODEV;
822 isa_address = val & ~(VT8231_EXTENT - 1);
823 if (isa_address == 0 && force_addr == 0) {
824 dev_err(&dev->dev, "base address not set -\
825 upgrade BIOS or use force_addr=0xaddr\n");
826 return -ENODEV;
829 s_bridge = pci_dev_get(dev);
831 if (i2c_isa_add_driver(&vt8231_driver)) {
832 pci_dev_put(s_bridge);
833 s_bridge = NULL;
836 /* Always return failure here. This is to allow other drivers to bind
837 * to this pci device. We don't really want to have control over the
838 * pci device, we only wanted to read as few register values from it.
840 return -ENODEV;
843 static int __init sm_vt8231_init(void)
845 return pci_register_driver(&vt8231_pci_driver);
848 static void __exit sm_vt8231_exit(void)
850 pci_unregister_driver(&vt8231_pci_driver);
851 if (s_bridge != NULL) {
852 i2c_isa_del_driver(&vt8231_driver);
853 pci_dev_put(s_bridge);
854 s_bridge = NULL;
858 MODULE_AUTHOR("Roger Lucas <roger@planbit.co.uk>");
859 MODULE_DESCRIPTION("VT8231 sensors");
860 MODULE_LICENSE("GPL");
862 module_init(sm_vt8231_init);
863 module_exit(sm_vt8231_exit);