blob: d00a726d0239a6d620deb280022ea1b7b478fa72 [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>
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>
39
40static int force_addr;
41module_param(force_addr, int, 0);
42MODULE_PARM_DESC(force_addr, "Initialize the base address of the sensors");
43
44/* Device address
45 Note that we can't determine the ISA address until we have initialized
46 our module */
47static unsigned short isa_address;
48
49#define VT8231_EXTENT 0x80
50#define VT8231_BASE_REG 0x70
51#define VT8231_ENABLE_REG 0x74
52
53/* The VT8231 registers
54
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:
58
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
69
70 Note that the BIOS may set the configuration register to a different value
71 to match the motherboard configuration.
72*/
73
74/* fans numbered 0-1 */
75#define VT8231_REG_FAN_MIN(nr) (0x3b + (nr))
76#define VT8231_REG_FAN(nr) (0x29 + (nr))
77
78/* Voltage inputs numbered 0-5 */
79
80static const u8 regvolt[] = { 0x21, 0x22, 0x23, 0x24, 0x25, 0x26 };
81static const u8 regvoltmax[] = { 0x3d, 0x2b, 0x2d, 0x2f, 0x31, 0x33 };
82static const u8 regvoltmin[] = { 0x3e, 0x2c, 0x2e, 0x30, 0x32, 0x34 };
83
84/* Temperatures are numbered 1-6 according to the Linux kernel specification.
85**
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.
90*/
91
92#define VT8231_REG_TEMP_LOW01 0x49
93#define VT8231_REG_TEMP_LOW25 0x4d
94
95static const u8 regtemp[] = { 0x1f, 0x21, 0x22, 0x23, 0x24, 0x25 };
96static const u8 regtempmax[] = { 0x39, 0x3d, 0x2b, 0x2d, 0x2f, 0x31 };
97static const u8 regtempmin[] = { 0x3a, 0x3e, 0x2c, 0x2e, 0x30, 0x32 };
98
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)
102
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
110
111/* temps 0-5 as numbered in VIA datasheet - see later for mapping to Linux
112** numbering
113*/
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))
119
120#define DIV_FROM_REG(val) (1 << (val))
121
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.
129**
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.
134*/
135
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
139*/
140static inline u8 FAN_TO_REG(long rpm, int div)
141{
142 if (rpm == 0)
143 return 0;
144 return SENSORS_LIMIT(1310720 / (rpm * div), 1, 255);
145}
146
147#define FAN_FROM_REG(val, div) ((val) == 0 ? 0 : 1310720 / ((val) * (div)))
148
149struct 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 */
155
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;
167};
168
169static struct pci_dev *s_bridge;
170static int vt8231_detect(struct i2c_adapter *adapter);
171static int vt8231_detach_client(struct i2c_client *client);
172static struct vt8231_data *vt8231_update_device(struct device *dev);
173static void vt8231_init_client(struct i2c_client *client);
174
175static inline int vt8231_read_value(struct i2c_client *client, u8 reg)
176{
177 return inb_p(client->addr + reg);
178}
179
180static inline void vt8231_write_value(struct i2c_client *client, u8 reg,
181 u8 value)
182{
183 outb_p(value, client->addr + reg);
184}
185
186/* following are the sysfs callback functions */
187static ssize_t show_in(struct device *dev, struct device_attribute *attr,
188 char *buf)
189{
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);
193
194 return sprintf(buf, "%d\n", ((data->in[nr] - 3) * 10000) / 958);
195}
196
197static ssize_t show_in_min(struct device *dev, struct device_attribute *attr,
198 char *buf)
199{
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);
203
204 return sprintf(buf, "%d\n", ((data->in_min[nr] - 3) * 10000) / 958);
205}
206
207static ssize_t show_in_max(struct device *dev, struct device_attribute *attr,
208 char *buf)
209{
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);
213
214 return sprintf(buf, "%d\n", (((data->in_max[nr] - 3) * 10000) / 958));
215}
216
217static ssize_t set_in_min(struct device *dev, struct device_attribute *attr,
218 const char *buf, size_t count)
219{
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);
225
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;
231}
232
233static ssize_t set_in_max(struct device *dev, struct device_attribute *attr,
234 const char *buf, size_t count)
235{
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);
241
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;
247}
248
249/* Special case for input 5 as this has 3.3V scaling built into the chip */
250static ssize_t show_in5(struct device *dev, struct device_attribute *attr,
251 char *buf)
252{
253 struct vt8231_data *data = vt8231_update_device(dev);
254
255 return sprintf(buf, "%d\n",
256 (((data->in[5] - 3) * 10000 * 54) / (958 * 34)));
257}
258
259static ssize_t show_in5_min(struct device *dev, struct device_attribute *attr,
260 char *buf)
261{
262 struct vt8231_data *data = vt8231_update_device(dev);
263
264 return sprintf(buf, "%d\n",
265 (((data->in_min[5] - 3) * 10000 * 54) / (958 * 34)));
266}
267
268static ssize_t show_in5_max(struct device *dev, struct device_attribute *attr,
269 char *buf)
270{
271 struct vt8231_data *data = vt8231_update_device(dev);
272
273 return sprintf(buf, "%d\n",
274 (((data->in_max[5] - 3) * 10000 * 54) / (958 * 34)));
275}
276
277static ssize_t set_in5_min(struct device *dev, struct device_attribute *attr,
278 const char *buf, size_t count)
279{
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);
283
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;
290}
291
292static ssize_t set_in5_max(struct device *dev, struct device_attribute *attr,
293 const char *buf, size_t count)
294{
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);
298
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;
305}
306
307#define define_voltage_sysfs(offset) \
308static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \
309 show_in, NULL, offset); \
310static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
311 show_in_min, set_in_min, offset); \
312static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
313 show_in_max, set_in_max, offset)
314
315define_voltage_sysfs(0);
316define_voltage_sysfs(1);
317define_voltage_sysfs(2);
318define_voltage_sysfs(3);
319define_voltage_sysfs(4);
320
321static DEVICE_ATTR(in5_input, S_IRUGO, show_in5, NULL);
322static DEVICE_ATTR(in5_min, S_IRUGO | S_IWUSR, show_in5_min, set_in5_min);
323static DEVICE_ATTR(in5_max, S_IRUGO | S_IWUSR, show_in5_max, set_in5_max);
324
325/* Temperatures */
326static ssize_t show_temp0(struct device *dev, struct device_attribute *attr,
327 char *buf)
328{
329 struct vt8231_data *data = vt8231_update_device(dev);
330 return sprintf(buf, "%d\n", data->temp[0] * 250);
331}
332
333static ssize_t show_temp0_max(struct device *dev, struct device_attribute *attr,
334 char *buf)
335{
336 struct vt8231_data *data = vt8231_update_device(dev);
337 return sprintf(buf, "%d\n", data->temp_max[0] * 1000);
338}
339
340static ssize_t show_temp0_min(struct device *dev, struct device_attribute *attr,
341 char *buf)
342{
343 struct vt8231_data *data = vt8231_update_device(dev);
344 return sprintf(buf, "%d\n", data->temp_min[0] * 1000);
345}
346
347static ssize_t set_temp0_max(struct device *dev, struct device_attribute *attr,
348 const char *buf, size_t count)
349{
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);
353
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;
359}
360static ssize_t set_temp0_min(struct device *dev, struct device_attribute *attr,
361 const char *buf, size_t count)
362{
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);
366
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;
372}
373
374static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
375 char *buf)
376{
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]));
381}
382
383static ssize_t show_temp_max(struct device *dev, struct device_attribute *attr,
384 char *buf)
385{
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]));
390}
391
392static ssize_t show_temp_min(struct device *dev, struct device_attribute *attr,
393 char *buf)
394{
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]));
399}
400
401static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
402 const char *buf, size_t count)
403{
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);
409
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;
415}
416static ssize_t set_temp_min(struct device *dev, struct device_attribute *attr,
417 const char *buf, size_t count)
418{
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);
424
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;
430}
431
432/* Note that these map the Linux temperature sensor numbering (1-6) to the VIA
433** temperature sensor numbering (0-5)
434*/
435#define define_temperature_sysfs(offset) \
436static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
437 show_temp, NULL, offset - 1); \
438static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
439 show_temp_max, set_temp_max, offset - 1); \
440static SENSOR_DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
441 show_temp_min, set_temp_min, offset - 1)
442
443static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp0, NULL);
444static DEVICE_ATTR(temp1_max, S_IRUGO | S_IWUSR, show_temp0_max, set_temp0_max);
445static DEVICE_ATTR(temp1_min, S_IRUGO | S_IWUSR, show_temp0_min, set_temp0_min);
446
447define_temperature_sysfs(2);
448define_temperature_sysfs(3);
449define_temperature_sysfs(4);
450define_temperature_sysfs(5);
451define_temperature_sysfs(6);
452
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 }
459
460struct str_device_attr_table {
461 struct device_attribute *input;
462 struct device_attribute *min;
463 struct device_attribute *max;
464};
465
466static 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)
473};
474
475static 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 }
482};
483
484/* Fans */
485static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
486 char *buf)
487{
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])));
493}
494
495static ssize_t show_fan_min(struct device *dev, struct device_attribute *attr,
496 char *buf)
497{
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])));
503}
504
505static ssize_t show_fan_div(struct device *dev, struct device_attribute *attr,
506 char *buf)
507{
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]));
512}
513
514static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
515 const char *buf, size_t count)
516{
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);
522
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;
528}
529
530static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
531 const char *buf, size_t count)
532{
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]));
541
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;
553 }
554
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]);
558
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;
563}
564
565
566#define define_fan_sysfs(offset) \
567static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
568 show_fan, NULL, offset - 1); \
569static SENSOR_DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
570 show_fan_div, set_fan_div, offset - 1); \
571static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
572 show_fan_min, set_fan_min, offset - 1)
573
574define_fan_sysfs(1);
575define_fan_sysfs(2);
576
577/* Alarms */
578static ssize_t show_alarms(struct device *dev, struct device_attribute *attr,
579 char *buf)
580{
581 struct vt8231_data *data = vt8231_update_device(dev);
582 return sprintf(buf, "%d\n", data->alarms);
583}
584
585static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
586
587static struct i2c_driver vt8231_driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100588 .driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100589 .name = "vt8231",
590 },
Roger Lucas1de9e372005-11-26 20:20:05 +0100591 .attach_adapter = vt8231_detect,
592 .detach_client = vt8231_detach_client,
593};
594
595static struct pci_device_id vt8231_pci_ids[] = {
596 { PCI_DEVICE(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8231_4) },
597 { 0, }
598};
599
600MODULE_DEVICE_TABLE(pci, vt8231_pci_ids);
601
602static int __devinit vt8231_pci_probe(struct pci_dev *dev,
603 const struct pci_device_id *id);
604
605static struct pci_driver vt8231_pci_driver = {
606 .name = "vt8231",
607 .id_table = vt8231_pci_ids,
608 .probe = vt8231_pci_probe,
609};
610
611int vt8231_detect(struct i2c_adapter *adapter)
612{
613 struct i2c_client *client;
614 struct vt8231_data *data;
615 int err = 0, i;
616 u16 val;
617
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;
626 }
627
628 if (PCIBIOS_SUCCESSFUL !=
629 pci_read_config_word(s_bridge, VT8231_ENABLE_REG, &val))
630 return -ENODEV;
631
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;
638 }
639
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;
646 }
647
648 if (!(data = kzalloc(sizeof(struct vt8231_data), GFP_KERNEL))) {
649 err = -ENOMEM;
650 goto exit_release;
651 }
652
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;
659
660 /* Fill in the remaining client fields and put into the global list */
661 strlcpy(client->name, "vt8231", I2C_NAME_SIZE);
662
663 init_MUTEX(&data->update_lock);
664
665 /* Tell the I2C layer a new client has arrived */
666 if ((err = i2c_attach_client(client)))
667 goto exit_free;
668
669 vt8231_init_client(client);
670
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;
676 }
677
678 /* Must update device information to find out the config field */
679 data->uch_config = vt8231_read_value(client, VT8231_REG_UCH_CONFIG);
680
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);
687 }
688 }
689
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);
696 }
697 }
698
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);
705
706 device_create_file(&client->dev, &dev_attr_alarms);
707 return 0;
708
709exit_detach:
710 i2c_detach_client(client);
711exit_free:
712 kfree(data);
713exit_release:
714 release_region(isa_address, VT8231_EXTENT);
715 return err;
716}
717
718static int vt8231_detach_client(struct i2c_client *client)
719{
720 struct vt8231_data *data = i2c_get_clientdata(client);
721 int err;
722
723 hwmon_device_unregister(data->class_dev);
724
725 if ((err = i2c_detach_client(client))) {
726 return err;
727 }
728
729 release_region(client->addr, VT8231_EXTENT);
730 kfree(data);
731
732 return 0;
733}
734
735static void vt8231_init_client(struct i2c_client *client)
736{
737 vt8231_write_value(client, VT8231_REG_TEMP1_CONFIG, 0);
738 vt8231_write_value(client, VT8231_REG_TEMP2_CONFIG, 0);
739}
740
741static struct vt8231_data *vt8231_update_device(struct device *dev)
742{
743 struct i2c_client *client = to_i2c_client(dev);
744 struct vt8231_data *data = i2c_get_clientdata(client);
745 int i;
746 u16 low;
747
748 down(&data->update_lock);
749
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]);
760 }
761 }
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));
767 }
768
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]);
781 }
782 }
783
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);
789
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;
795 }
796
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;
801 }
802
803 data->last_updated = jiffies;
804 data->valid = 1;
805 }
806
807 up(&data->update_lock);
808
809 return data;
810}
811
812static int __devinit vt8231_pci_probe(struct pci_dev *dev,
813 const struct pci_device_id *id)
814{
815 u16 val;
816
817 if (PCIBIOS_SUCCESSFUL != pci_read_config_word(dev, VT8231_BASE_REG,
818 &val))
819 return -ENODEV;
820
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;
826 }
827
828 s_bridge = pci_dev_get(dev);
829
830 if (i2c_isa_add_driver(&vt8231_driver)) {
831 pci_dev_put(s_bridge);
832 s_bridge = NULL;
833 }
834
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.
838 */
839 return -ENODEV;
840}
841
842static int __init sm_vt8231_init(void)
843{
844 return pci_module_init(&vt8231_pci_driver);
845}
846
847static void __exit sm_vt8231_exit(void)
848{
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;
854 }
855}
856
857MODULE_AUTHOR("Roger Lucas <roger@planbit.co.uk>");
858MODULE_DESCRIPTION("VT8231 sensors");
859MODULE_LICENSE("GPL");
860
861module_init(sm_vt8231_init);
862module_exit(sm_vt8231_exit);