aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/hwmon/vt8231.c
diff options
context:
space:
mode:
authorRoger Lucas <roger@planbit.co.uk>2005-11-26 14:20:05 -0500
committerGreg Kroah-Hartman <gregkh@suse.de>2006-01-06 01:16:21 -0500
commit1de9e371b89e1cf4da123f0d92efa8eb134ca5e8 (patch)
tree2640b33775ee6f6ebfa19f35976c3fe6dbe55c02 /drivers/hwmon/vt8231.c
parent1d26f455eb0db0bf4d4b7177547f4310b645a32a (diff)
[PATCH] hwmon: New vt8231 driver
Port the vt8231 hardware monitoring driver from lm_sensors CVS to Linux 2.6. Signed-off-by: Roger Lucas <roger@planbit.co.uk> Signed-off-by: Jean Delvare <khali@linux-fr.org> Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
Diffstat (limited to 'drivers/hwmon/vt8231.c')
-rw-r--r--drivers/hwmon/vt8231.c861
1 files changed, 861 insertions, 0 deletions
diff --git a/drivers/hwmon/vt8231.c b/drivers/hwmon/vt8231.c
new file mode 100644
index 000000000000..c8556a72a1cd
--- /dev/null
+++ b/drivers/hwmon/vt8231.c
@@ -0,0 +1,861 @@
1/*
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 = {
588 .owner = THIS_MODULE,
589 .name = "vt8231",
590 .attach_adapter = vt8231_detect,
591 .detach_client = vt8231_detach_client,
592};
593
594static struct pci_device_id vt8231_pci_ids[] = {
595 { PCI_DEVICE(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8231_4) },
596 { 0, }
597};
598
599MODULE_DEVICE_TABLE(pci, vt8231_pci_ids);
600
601static int __devinit vt8231_pci_probe(struct pci_dev *dev,
602 const struct pci_device_id *id);
603
604static struct pci_driver vt8231_pci_driver = {
605 .name = "vt8231",
606 .id_table = vt8231_pci_ids,
607 .probe = vt8231_pci_probe,
608};
609
610int vt8231_detect(struct i2c_adapter *adapter)
611{
612 struct i2c_client *client;
613 struct vt8231_data *data;
614 int err = 0, i;
615 u16 val;
616
617 /* 8231 requires multiple of 256 */
618 if (force_addr) {
619 isa_address = force_addr & 0xFF00;
620 dev_warn(&adapter->dev, "forcing ISA address 0x%04X\n",
621 isa_address);
622 if (PCIBIOS_SUCCESSFUL != pci_write_config_word(s_bridge,
623 VT8231_BASE_REG, isa_address))
624 return -ENODEV;
625 }
626
627 if (PCIBIOS_SUCCESSFUL !=
628 pci_read_config_word(s_bridge, VT8231_ENABLE_REG, &val))
629 return -ENODEV;
630
631 if (!(val & 0x0001)) {
632 dev_warn(&adapter->dev, "enabling sensors\n");
633 if (PCIBIOS_SUCCESSFUL !=
634 pci_write_config_word(s_bridge, VT8231_ENABLE_REG,
635 val | 0x0001))
636 return -ENODEV;
637 }
638
639 /* Reserve the ISA region */
640 if (!request_region(isa_address, VT8231_EXTENT,
641 vt8231_pci_driver.name)) {
642 dev_err(&adapter->dev, "region 0x%x already in use!\n",
643 isa_address);
644 return -ENODEV;
645 }
646
647 if (!(data = kzalloc(sizeof(struct vt8231_data), GFP_KERNEL))) {
648 err = -ENOMEM;
649 goto exit_release;
650 }
651
652 client = &data->client;
653 i2c_set_clientdata(client, data);
654 client->addr = isa_address;
655 client->adapter = adapter;
656 client->driver = &vt8231_driver;
657 client->dev.parent = &adapter->dev;
658
659 /* Fill in the remaining client fields and put into the global list */
660 strlcpy(client->name, "vt8231", I2C_NAME_SIZE);
661
662 init_MUTEX(&data->update_lock);
663
664 /* Tell the I2C layer a new client has arrived */
665 if ((err = i2c_attach_client(client)))
666 goto exit_free;
667
668 vt8231_init_client(client);
669
670 /* Register sysfs hooks */
671 data->class_dev = hwmon_device_register(&client->dev);
672 if (IS_ERR(data->class_dev)) {
673 err = PTR_ERR(data->class_dev);
674 goto exit_detach;
675 }
676
677 /* Must update device information to find out the config field */
678 data->uch_config = vt8231_read_value(client, VT8231_REG_UCH_CONFIG);
679
680 for (i = 0; i < ARRAY_SIZE(cfg_info_temp); i++) {
681 if (ISTEMP(i, data->uch_config)) {
682 device_create_file(&client->dev,
683 cfg_info_temp[i].input);
684 device_create_file(&client->dev, cfg_info_temp[i].max);
685 device_create_file(&client->dev, cfg_info_temp[i].min);
686 }
687 }
688
689 for (i = 0; i < ARRAY_SIZE(cfg_info_volt); i++) {
690 if (ISVOLT(i, data->uch_config)) {
691 device_create_file(&client->dev,
692 cfg_info_volt[i].input);
693 device_create_file(&client->dev, cfg_info_volt[i].max);
694 device_create_file(&client->dev, cfg_info_volt[i].min);
695 }
696 }
697
698 device_create_file(&client->dev, &sensor_dev_attr_fan1_input.dev_attr);
699 device_create_file(&client->dev, &sensor_dev_attr_fan2_input.dev_attr);
700 device_create_file(&client->dev, &sensor_dev_attr_fan1_min.dev_attr);
701 device_create_file(&client->dev, &sensor_dev_attr_fan2_min.dev_attr);
702 device_create_file(&client->dev, &sensor_dev_attr_fan1_div.dev_attr);
703 device_create_file(&client->dev, &sensor_dev_attr_fan2_div.dev_attr);
704
705 device_create_file(&client->dev, &dev_attr_alarms);
706 return 0;
707
708exit_detach:
709 i2c_detach_client(client);
710exit_free:
711 kfree(data);
712exit_release:
713 release_region(isa_address, VT8231_EXTENT);
714 return err;
715}
716
717static int vt8231_detach_client(struct i2c_client *client)
718{
719 struct vt8231_data *data = i2c_get_clientdata(client);
720 int err;
721
722 hwmon_device_unregister(data->class_dev);
723
724 if ((err = i2c_detach_client(client))) {
725 return err;
726 }
727
728 release_region(client->addr, VT8231_EXTENT);
729 kfree(data);
730
731 return 0;
732}
733
734static void vt8231_init_client(struct i2c_client *client)
735{
736 vt8231_write_value(client, VT8231_REG_TEMP1_CONFIG, 0);
737 vt8231_write_value(client, VT8231_REG_TEMP2_CONFIG, 0);
738}
739
740static struct vt8231_data *vt8231_update_device(struct device *dev)
741{
742 struct i2c_client *client = to_i2c_client(dev);
743 struct vt8231_data *data = i2c_get_clientdata(client);
744 int i;
745 u16 low;
746
747 down(&data->update_lock);
748
749 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
750 || !data->valid) {
751 for (i = 0; i < 6; i++) {
752 if (ISVOLT(i, data->uch_config)) {
753 data->in[i] = vt8231_read_value(client,
754 regvolt[i]);
755 data->in_min[i] = vt8231_read_value(client,
756 regvoltmin[i]);
757 data->in_max[i] = vt8231_read_value(client,
758 regvoltmax[i]);
759 }
760 }
761 for (i = 0; i < 2; i++) {
762 data->fan[i] = vt8231_read_value(client,
763 VT8231_REG_FAN(i));
764 data->fan_min[i] = vt8231_read_value(client,
765 VT8231_REG_FAN_MIN(i));
766 }
767
768 low = vt8231_read_value(client, VT8231_REG_TEMP_LOW01);
769 low = (low >> 6) | ((low & 0x30) >> 2)
770 | (vt8231_read_value(client, VT8231_REG_TEMP_LOW25) << 4);
771 for (i = 0; i < 6; i++) {
772 if (ISTEMP(i, data->uch_config)) {
773 data->temp[i] = (vt8231_read_value(client,
774 regtemp[i]) << 2)
775 | ((low >> (2 * i)) & 0x03);
776 data->temp_max[i] = vt8231_read_value(client,
777 regtempmax[i]);
778 data->temp_min[i] = vt8231_read_value(client,
779 regtempmin[i]);
780 }
781 }
782
783 i = vt8231_read_value(client, VT8231_REG_FANDIV);
784 data->fan_div[0] = (i >> 4) & 0x03;
785 data->fan_div[1] = i >> 6;
786 data->alarms = vt8231_read_value(client, VT8231_REG_ALARM1) |
787 (vt8231_read_value(client, VT8231_REG_ALARM2) << 8);
788
789 /* Set alarm flags correctly */
790 if (!data->fan[0] && data->fan_min[0]) {
791 data->alarms |= 0x40;
792 } else if (data->fan[0] && !data->fan_min[0]) {
793 data->alarms &= ~0x40;
794 }
795
796 if (!data->fan[1] && data->fan_min[1]) {
797 data->alarms |= 0x80;
798 } else if (data->fan[1] && !data->fan_min[1]) {
799 data->alarms &= ~0x80;
800 }
801
802 data->last_updated = jiffies;
803 data->valid = 1;
804 }
805
806 up(&data->update_lock);
807
808 return data;
809}
810
811static int __devinit vt8231_pci_probe(struct pci_dev *dev,
812 const struct pci_device_id *id)
813{
814 u16 val;
815
816 if (PCIBIOS_SUCCESSFUL != pci_read_config_word(dev, VT8231_BASE_REG,
817 &val))
818 return -ENODEV;
819
820 isa_address = val & ~(VT8231_EXTENT - 1);
821 if (isa_address == 0 && force_addr == 0) {
822 dev_err(&dev->dev, "base address not set -\
823 upgrade BIOS or use force_addr=0xaddr\n");
824 return -ENODEV;
825 }
826
827 s_bridge = pci_dev_get(dev);
828
829 if (i2c_isa_add_driver(&vt8231_driver)) {
830 pci_dev_put(s_bridge);
831 s_bridge = NULL;
832 }
833
834 /* Always return failure here. This is to allow other drivers to bind
835 * to this pci device. We don't really want to have control over the
836 * pci device, we only wanted to read as few register values from it.
837 */
838 return -ENODEV;
839}
840
841static int __init sm_vt8231_init(void)
842{
843 return pci_module_init(&vt8231_pci_driver);
844}
845
846static void __exit sm_vt8231_exit(void)
847{
848 pci_unregister_driver(&vt8231_pci_driver);
849 if (s_bridge != NULL) {
850 i2c_isa_del_driver(&vt8231_driver);
851 pci_dev_put(s_bridge);
852 s_bridge = NULL;
853 }
854}
855
856MODULE_AUTHOR("Roger Lucas <roger@planbit.co.uk>");
857MODULE_DESCRIPTION("VT8231 sensors");
858MODULE_LICENSE("GPL");
859
860module_init(sm_vt8231_init);
861module_exit(sm_vt8231_exit);