blob: a6a4aa0eee166d3ac029d2e7c49c8a4c714fe2bf [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>
Ingo Molnar9a61bf62006-01-18 23:19:26 +010038#include <linux/mutex.h>
Roger Lucas1de9e372005-11-26 20:20:05 +010039#include <asm/io.h>
40
41static int force_addr;
42module_param(force_addr, int, 0);
43MODULE_PARM_DESC(force_addr, "Initialize the base address of the sensors");
44
45/* Device address
46 Note that we can't determine the ISA address until we have initialized
47 our module */
48static unsigned short isa_address;
49
50#define VT8231_EXTENT 0x80
51#define VT8231_BASE_REG 0x70
52#define VT8231_ENABLE_REG 0x74
53
54/* The VT8231 registers
55
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:
59
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
70
71 Note that the BIOS may set the configuration register to a different value
72 to match the motherboard configuration.
73*/
74
75/* fans numbered 0-1 */
76#define VT8231_REG_FAN_MIN(nr) (0x3b + (nr))
77#define VT8231_REG_FAN(nr) (0x29 + (nr))
78
79/* Voltage inputs numbered 0-5 */
80
81static const u8 regvolt[] = { 0x21, 0x22, 0x23, 0x24, 0x25, 0x26 };
82static const u8 regvoltmax[] = { 0x3d, 0x2b, 0x2d, 0x2f, 0x31, 0x33 };
83static const u8 regvoltmin[] = { 0x3e, 0x2c, 0x2e, 0x30, 0x32, 0x34 };
84
85/* Temperatures are numbered 1-6 according to the Linux kernel specification.
86**
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.
91*/
92
93#define VT8231_REG_TEMP_LOW01 0x49
94#define VT8231_REG_TEMP_LOW25 0x4d
95
96static const u8 regtemp[] = { 0x1f, 0x21, 0x22, 0x23, 0x24, 0x25 };
97static const u8 regtempmax[] = { 0x39, 0x3d, 0x2b, 0x2d, 0x2f, 0x31 };
98static const u8 regtempmin[] = { 0x3a, 0x3e, 0x2c, 0x2e, 0x30, 0x32 };
99
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)
103
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
111
112/* temps 0-5 as numbered in VIA datasheet - see later for mapping to Linux
113** numbering
114*/
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))
120
121#define DIV_FROM_REG(val) (1 << (val))
122
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.
130**
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.
135*/
136
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
140*/
141static inline u8 FAN_TO_REG(long rpm, int div)
142{
143 if (rpm == 0)
144 return 0;
145 return SENSORS_LIMIT(1310720 / (rpm * div), 1, 255);
146}
147
148#define FAN_FROM_REG(val, div) ((val) == 0 ? 0 : 1310720 / ((val) * (div)))
149
150struct vt8231_data {
151 struct i2c_client client;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100152 struct mutex update_lock;
Roger Lucas1de9e372005-11-26 20:20:05 +0100153 struct class_device *class_dev;
154 char valid; /* !=0 if following fields are valid */
155 unsigned long last_updated; /* In jiffies */
156
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;
168};
169
170static struct pci_dev *s_bridge;
171static int vt8231_detect(struct i2c_adapter *adapter);
172static int vt8231_detach_client(struct i2c_client *client);
173static struct vt8231_data *vt8231_update_device(struct device *dev);
174static void vt8231_init_client(struct i2c_client *client);
175
176static inline int vt8231_read_value(struct i2c_client *client, u8 reg)
177{
178 return inb_p(client->addr + reg);
179}
180
181static inline void vt8231_write_value(struct i2c_client *client, u8 reg,
182 u8 value)
183{
184 outb_p(value, client->addr + reg);
185}
186
187/* following are the sysfs callback functions */
188static ssize_t show_in(struct device *dev, struct device_attribute *attr,
189 char *buf)
190{
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);
194
195 return sprintf(buf, "%d\n", ((data->in[nr] - 3) * 10000) / 958);
196}
197
198static ssize_t show_in_min(struct device *dev, struct device_attribute *attr,
199 char *buf)
200{
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);
204
205 return sprintf(buf, "%d\n", ((data->in_min[nr] - 3) * 10000) / 958);
206}
207
208static ssize_t show_in_max(struct device *dev, struct device_attribute *attr,
209 char *buf)
210{
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);
214
215 return sprintf(buf, "%d\n", (((data->in_max[nr] - 3) * 10000) / 958));
216}
217
218static ssize_t set_in_min(struct device *dev, struct device_attribute *attr,
219 const char *buf, size_t count)
220{
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);
226
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100227 mutex_lock(&data->update_lock);
Roger Lucas1de9e372005-11-26 20:20:05 +0100228 data->in_min[nr] = SENSORS_LIMIT(((val * 958) / 10000) + 3, 0, 255);
229 vt8231_write_value(client, regvoltmin[nr], data->in_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100230 mutex_unlock(&data->update_lock);
Roger Lucas1de9e372005-11-26 20:20:05 +0100231 return count;
232}
233
234static ssize_t set_in_max(struct device *dev, struct device_attribute *attr,
235 const char *buf, size_t count)
236{
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);
242
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100243 mutex_lock(&data->update_lock);
Roger Lucas1de9e372005-11-26 20:20:05 +0100244 data->in_max[nr] = SENSORS_LIMIT(((val * 958) / 10000) + 3, 0, 255);
245 vt8231_write_value(client, regvoltmax[nr], data->in_max[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100246 mutex_unlock(&data->update_lock);
Roger Lucas1de9e372005-11-26 20:20:05 +0100247 return count;
248}
249
250/* Special case for input 5 as this has 3.3V scaling built into the chip */
251static ssize_t show_in5(struct device *dev, struct device_attribute *attr,
252 char *buf)
253{
254 struct vt8231_data *data = vt8231_update_device(dev);
255
256 return sprintf(buf, "%d\n",
257 (((data->in[5] - 3) * 10000 * 54) / (958 * 34)));
258}
259
260static ssize_t show_in5_min(struct device *dev, struct device_attribute *attr,
261 char *buf)
262{
263 struct vt8231_data *data = vt8231_update_device(dev);
264
265 return sprintf(buf, "%d\n",
266 (((data->in_min[5] - 3) * 10000 * 54) / (958 * 34)));
267}
268
269static ssize_t show_in5_max(struct device *dev, struct device_attribute *attr,
270 char *buf)
271{
272 struct vt8231_data *data = vt8231_update_device(dev);
273
274 return sprintf(buf, "%d\n",
275 (((data->in_max[5] - 3) * 10000 * 54) / (958 * 34)));
276}
277
278static ssize_t set_in5_min(struct device *dev, struct device_attribute *attr,
279 const char *buf, size_t count)
280{
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);
284
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100285 mutex_lock(&data->update_lock);
Roger Lucas1de9e372005-11-26 20:20:05 +0100286 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]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100289 mutex_unlock(&data->update_lock);
Roger Lucas1de9e372005-11-26 20:20:05 +0100290 return count;
291}
292
293static ssize_t set_in5_max(struct device *dev, struct device_attribute *attr,
294 const char *buf, size_t count)
295{
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);
299
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100300 mutex_lock(&data->update_lock);
Roger Lucas1de9e372005-11-26 20:20:05 +0100301 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]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100304 mutex_unlock(&data->update_lock);
Roger Lucas1de9e372005-11-26 20:20:05 +0100305 return count;
306}
307
308#define define_voltage_sysfs(offset) \
309static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \
310 show_in, NULL, offset); \
311static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
312 show_in_min, set_in_min, offset); \
313static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
314 show_in_max, set_in_max, offset)
315
316define_voltage_sysfs(0);
317define_voltage_sysfs(1);
318define_voltage_sysfs(2);
319define_voltage_sysfs(3);
320define_voltage_sysfs(4);
321
322static DEVICE_ATTR(in5_input, S_IRUGO, show_in5, NULL);
323static DEVICE_ATTR(in5_min, S_IRUGO | S_IWUSR, show_in5_min, set_in5_min);
324static DEVICE_ATTR(in5_max, S_IRUGO | S_IWUSR, show_in5_max, set_in5_max);
325
326/* Temperatures */
327static ssize_t show_temp0(struct device *dev, struct device_attribute *attr,
328 char *buf)
329{
330 struct vt8231_data *data = vt8231_update_device(dev);
331 return sprintf(buf, "%d\n", data->temp[0] * 250);
332}
333
334static ssize_t show_temp0_max(struct device *dev, struct device_attribute *attr,
335 char *buf)
336{
337 struct vt8231_data *data = vt8231_update_device(dev);
338 return sprintf(buf, "%d\n", data->temp_max[0] * 1000);
339}
340
341static ssize_t show_temp0_min(struct device *dev, struct device_attribute *attr,
342 char *buf)
343{
344 struct vt8231_data *data = vt8231_update_device(dev);
345 return sprintf(buf, "%d\n", data->temp_min[0] * 1000);
346}
347
348static ssize_t set_temp0_max(struct device *dev, struct device_attribute *attr,
349 const char *buf, size_t count)
350{
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);
354
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100355 mutex_lock(&data->update_lock);
Roger Lucas1de9e372005-11-26 20:20:05 +0100356 data->temp_max[0] = SENSORS_LIMIT((val + 500) / 1000, 0, 255);
357 vt8231_write_value(client, regtempmax[0], data->temp_max[0]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100358 mutex_unlock(&data->update_lock);
Roger Lucas1de9e372005-11-26 20:20:05 +0100359 return count;
360}
361static ssize_t set_temp0_min(struct device *dev, struct device_attribute *attr,
362 const char *buf, size_t count)
363{
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);
367
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100368 mutex_lock(&data->update_lock);
Roger Lucas1de9e372005-11-26 20:20:05 +0100369 data->temp_min[0] = SENSORS_LIMIT((val + 500) / 1000, 0, 255);
370 vt8231_write_value(client, regtempmin[0], data->temp_min[0]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100371 mutex_unlock(&data->update_lock);
Roger Lucas1de9e372005-11-26 20:20:05 +0100372 return count;
373}
374
375static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
376 char *buf)
377{
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]));
382}
383
384static ssize_t show_temp_max(struct device *dev, struct device_attribute *attr,
385 char *buf)
386{
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]));
391}
392
393static ssize_t show_temp_min(struct device *dev, struct device_attribute *attr,
394 char *buf)
395{
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]));
400}
401
402static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
403 const char *buf, size_t count)
404{
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);
410
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100411 mutex_lock(&data->update_lock);
Roger Lucas1de9e372005-11-26 20:20:05 +0100412 data->temp_max[nr] = SENSORS_LIMIT(TEMP_MAXMIN_TO_REG(val), 0, 255);
413 vt8231_write_value(client, regtempmax[nr], data->temp_max[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100414 mutex_unlock(&data->update_lock);
Roger Lucas1de9e372005-11-26 20:20:05 +0100415 return count;
416}
417static ssize_t set_temp_min(struct device *dev, struct device_attribute *attr,
418 const char *buf, size_t count)
419{
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);
425
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100426 mutex_lock(&data->update_lock);
Roger Lucas1de9e372005-11-26 20:20:05 +0100427 data->temp_min[nr] = SENSORS_LIMIT(TEMP_MAXMIN_TO_REG(val), 0, 255);
428 vt8231_write_value(client, regtempmin[nr], data->temp_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100429 mutex_unlock(&data->update_lock);
Roger Lucas1de9e372005-11-26 20:20:05 +0100430 return count;
431}
432
433/* Note that these map the Linux temperature sensor numbering (1-6) to the VIA
434** temperature sensor numbering (0-5)
435*/
436#define define_temperature_sysfs(offset) \
437static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
438 show_temp, NULL, offset - 1); \
439static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
440 show_temp_max, set_temp_max, offset - 1); \
Jean Delvaree3efa5a2006-02-05 23:11:16 +0100441static SENSOR_DEVICE_ATTR(temp##offset##_max_hyst, S_IRUGO | S_IWUSR, \
Roger Lucas1de9e372005-11-26 20:20:05 +0100442 show_temp_min, set_temp_min, offset - 1)
443
444static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp0, NULL);
445static DEVICE_ATTR(temp1_max, S_IRUGO | S_IWUSR, show_temp0_max, set_temp0_max);
Jean Delvaree3efa5a2006-02-05 23:11:16 +0100446static DEVICE_ATTR(temp1_max_hyst, S_IRUGO | S_IWUSR, show_temp0_min, set_temp0_min);
Roger Lucas1de9e372005-11-26 20:20:05 +0100447
448define_temperature_sysfs(2);
449define_temperature_sysfs(3);
450define_temperature_sysfs(4);
451define_temperature_sysfs(5);
452define_temperature_sysfs(6);
453
Roger Lucas1de9e372005-11-26 20:20:05 +0100454/* Fans */
455static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
456 char *buf)
457{
458 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
459 int nr = sensor_attr->index;
460 struct vt8231_data *data = vt8231_update_device(dev);
461 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr],
462 DIV_FROM_REG(data->fan_div[nr])));
463}
464
465static ssize_t show_fan_min(struct device *dev, struct device_attribute *attr,
466 char *buf)
467{
468 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
469 int nr = sensor_attr->index;
470 struct vt8231_data *data = vt8231_update_device(dev);
471 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[nr],
472 DIV_FROM_REG(data->fan_div[nr])));
473}
474
475static ssize_t show_fan_div(struct device *dev, struct device_attribute *attr,
476 char *buf)
477{
478 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
479 int nr = sensor_attr->index;
480 struct vt8231_data *data = vt8231_update_device(dev);
481 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]));
482}
483
484static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
485 const char *buf, size_t count)
486{
487 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
488 int nr = sensor_attr->index;
489 struct i2c_client *client = to_i2c_client(dev);
490 struct vt8231_data *data = i2c_get_clientdata(client);
491 int val = simple_strtoul(buf, NULL, 10);
492
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100493 mutex_lock(&data->update_lock);
Roger Lucas1de9e372005-11-26 20:20:05 +0100494 data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
495 vt8231_write_value(client, VT8231_REG_FAN_MIN(nr), data->fan_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100496 mutex_unlock(&data->update_lock);
Roger Lucas1de9e372005-11-26 20:20:05 +0100497 return count;
498}
499
500static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
501 const char *buf, size_t count)
502{
503 struct i2c_client *client = to_i2c_client(dev);
504 struct vt8231_data *data = i2c_get_clientdata(client);
505 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
506 unsigned long val = simple_strtoul(buf, NULL, 10);
507 int nr = sensor_attr->index;
508 int old = vt8231_read_value(client, VT8231_REG_FANDIV);
509 long min = FAN_FROM_REG(data->fan_min[nr],
510 DIV_FROM_REG(data->fan_div[nr]));
511
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100512 mutex_lock(&data->update_lock);
Roger Lucas1de9e372005-11-26 20:20:05 +0100513 switch (val) {
514 case 1: data->fan_div[nr] = 0; break;
515 case 2: data->fan_div[nr] = 1; break;
516 case 4: data->fan_div[nr] = 2; break;
517 case 8: data->fan_div[nr] = 3; break;
518 default:
519 dev_err(&client->dev, "fan_div value %ld not supported."
520 "Choose one of 1, 2, 4 or 8!\n", val);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100521 mutex_unlock(&data->update_lock);
Roger Lucas1de9e372005-11-26 20:20:05 +0100522 return -EINVAL;
523 }
524
525 /* Correct the fan minimum speed */
526 data->fan_min[nr] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
527 vt8231_write_value(client, VT8231_REG_FAN_MIN(nr), data->fan_min[nr]);
528
529 old = (old & 0x0f) | (data->fan_div[1] << 6) | (data->fan_div[0] << 4);
530 vt8231_write_value(client, VT8231_REG_FANDIV, old);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100531 mutex_unlock(&data->update_lock);
Roger Lucas1de9e372005-11-26 20:20:05 +0100532 return count;
533}
534
535
536#define define_fan_sysfs(offset) \
537static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
538 show_fan, NULL, offset - 1); \
539static SENSOR_DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
540 show_fan_div, set_fan_div, offset - 1); \
541static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
542 show_fan_min, set_fan_min, offset - 1)
543
544define_fan_sysfs(1);
545define_fan_sysfs(2);
546
547/* Alarms */
548static ssize_t show_alarms(struct device *dev, struct device_attribute *attr,
549 char *buf)
550{
551 struct vt8231_data *data = vt8231_update_device(dev);
552 return sprintf(buf, "%d\n", data->alarms);
553}
554
555static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
556
Roger Lucascbeeb5b2006-09-24 21:21:46 +0200557static struct attribute *vt8231_attributes_temps[6][4] = {
558 {
559 &dev_attr_temp1_input.attr,
560 &dev_attr_temp1_max_hyst.attr,
561 &dev_attr_temp1_max.attr,
562 NULL
563 }, {
564 &sensor_dev_attr_temp2_input.dev_attr.attr,
565 &sensor_dev_attr_temp2_max_hyst.dev_attr.attr,
566 &sensor_dev_attr_temp2_max.dev_attr.attr,
567 NULL
568 }, {
569 &sensor_dev_attr_temp3_input.dev_attr.attr,
570 &sensor_dev_attr_temp3_max_hyst.dev_attr.attr,
571 &sensor_dev_attr_temp3_max.dev_attr.attr,
572 NULL
573 }, {
574 &sensor_dev_attr_temp4_input.dev_attr.attr,
575 &sensor_dev_attr_temp4_max_hyst.dev_attr.attr,
576 &sensor_dev_attr_temp4_max.dev_attr.attr,
577 NULL
578 }, {
579 &sensor_dev_attr_temp5_input.dev_attr.attr,
580 &sensor_dev_attr_temp5_max_hyst.dev_attr.attr,
581 &sensor_dev_attr_temp5_max.dev_attr.attr,
582 NULL
583 }, {
584 &sensor_dev_attr_temp6_input.dev_attr.attr,
585 &sensor_dev_attr_temp6_max_hyst.dev_attr.attr,
586 &sensor_dev_attr_temp6_max.dev_attr.attr,
587 NULL
588 }
589};
590
591static const struct attribute_group vt8231_group_temps[6] = {
592 { .attrs = vt8231_attributes_temps[0] },
593 { .attrs = vt8231_attributes_temps[1] },
594 { .attrs = vt8231_attributes_temps[2] },
595 { .attrs = vt8231_attributes_temps[3] },
596 { .attrs = vt8231_attributes_temps[4] },
597 { .attrs = vt8231_attributes_temps[5] },
598};
599
600static struct attribute *vt8231_attributes_volts[6][4] = {
601 {
602 &sensor_dev_attr_in0_input.dev_attr.attr,
603 &sensor_dev_attr_in0_min.dev_attr.attr,
604 &sensor_dev_attr_in0_max.dev_attr.attr,
605 NULL
606 }, {
607 &sensor_dev_attr_in1_input.dev_attr.attr,
608 &sensor_dev_attr_in1_min.dev_attr.attr,
609 &sensor_dev_attr_in1_max.dev_attr.attr,
610 NULL
611 }, {
612 &sensor_dev_attr_in2_input.dev_attr.attr,
613 &sensor_dev_attr_in2_min.dev_attr.attr,
614 &sensor_dev_attr_in2_max.dev_attr.attr,
615 NULL
616 }, {
617 &sensor_dev_attr_in3_input.dev_attr.attr,
618 &sensor_dev_attr_in3_min.dev_attr.attr,
619 &sensor_dev_attr_in3_max.dev_attr.attr,
620 NULL
621 }, {
622 &sensor_dev_attr_in4_input.dev_attr.attr,
623 &sensor_dev_attr_in4_min.dev_attr.attr,
624 &sensor_dev_attr_in4_max.dev_attr.attr,
625 NULL
626 }, {
627 &dev_attr_in5_input.attr,
628 &dev_attr_in5_min.attr,
629 &dev_attr_in5_max.attr,
630 NULL
631 }
632};
633
634static const struct attribute_group vt8231_group_volts[6] = {
635 { .attrs = vt8231_attributes_volts[0] },
636 { .attrs = vt8231_attributes_volts[1] },
637 { .attrs = vt8231_attributes_volts[2] },
638 { .attrs = vt8231_attributes_volts[3] },
639 { .attrs = vt8231_attributes_volts[4] },
640 { .attrs = vt8231_attributes_volts[5] },
641};
642
643static struct attribute *vt8231_attributes[] = {
644 &sensor_dev_attr_fan1_input.dev_attr.attr,
645 &sensor_dev_attr_fan2_input.dev_attr.attr,
646 &sensor_dev_attr_fan1_min.dev_attr.attr,
647 &sensor_dev_attr_fan2_min.dev_attr.attr,
648 &sensor_dev_attr_fan1_div.dev_attr.attr,
649 &sensor_dev_attr_fan2_div.dev_attr.attr,
650 &dev_attr_alarms.attr,
651 NULL
652};
653
654static const struct attribute_group vt8231_group = {
655 .attrs = vt8231_attributes,
656};
657
Roger Lucas1de9e372005-11-26 20:20:05 +0100658static struct i2c_driver vt8231_driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100659 .driver = {
Jean Delvare87218842006-09-03 22:36:14 +0200660 .owner = THIS_MODULE,
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100661 .name = "vt8231",
662 },
Roger Lucas1de9e372005-11-26 20:20:05 +0100663 .attach_adapter = vt8231_detect,
664 .detach_client = vt8231_detach_client,
665};
666
667static struct pci_device_id vt8231_pci_ids[] = {
668 { PCI_DEVICE(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8231_4) },
669 { 0, }
670};
671
672MODULE_DEVICE_TABLE(pci, vt8231_pci_ids);
673
674static int __devinit vt8231_pci_probe(struct pci_dev *dev,
675 const struct pci_device_id *id);
676
677static struct pci_driver vt8231_pci_driver = {
678 .name = "vt8231",
679 .id_table = vt8231_pci_ids,
680 .probe = vt8231_pci_probe,
681};
682
683int vt8231_detect(struct i2c_adapter *adapter)
684{
685 struct i2c_client *client;
686 struct vt8231_data *data;
687 int err = 0, i;
688 u16 val;
689
690 /* 8231 requires multiple of 256 */
691 if (force_addr) {
692 isa_address = force_addr & 0xFF00;
693 dev_warn(&adapter->dev, "forcing ISA address 0x%04X\n",
694 isa_address);
695 if (PCIBIOS_SUCCESSFUL != pci_write_config_word(s_bridge,
696 VT8231_BASE_REG, isa_address))
697 return -ENODEV;
698 }
699
700 if (PCIBIOS_SUCCESSFUL !=
701 pci_read_config_word(s_bridge, VT8231_ENABLE_REG, &val))
702 return -ENODEV;
703
704 if (!(val & 0x0001)) {
705 dev_warn(&adapter->dev, "enabling sensors\n");
706 if (PCIBIOS_SUCCESSFUL !=
707 pci_write_config_word(s_bridge, VT8231_ENABLE_REG,
708 val | 0x0001))
709 return -ENODEV;
710 }
711
712 /* Reserve the ISA region */
713 if (!request_region(isa_address, VT8231_EXTENT,
714 vt8231_pci_driver.name)) {
715 dev_err(&adapter->dev, "region 0x%x already in use!\n",
716 isa_address);
717 return -ENODEV;
718 }
719
720 if (!(data = kzalloc(sizeof(struct vt8231_data), GFP_KERNEL))) {
721 err = -ENOMEM;
722 goto exit_release;
723 }
724
725 client = &data->client;
726 i2c_set_clientdata(client, data);
727 client->addr = isa_address;
728 client->adapter = adapter;
729 client->driver = &vt8231_driver;
Roger Lucas1de9e372005-11-26 20:20:05 +0100730
731 /* Fill in the remaining client fields and put into the global list */
732 strlcpy(client->name, "vt8231", I2C_NAME_SIZE);
733
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100734 mutex_init(&data->update_lock);
Roger Lucas1de9e372005-11-26 20:20:05 +0100735
736 /* Tell the I2C layer a new client has arrived */
737 if ((err = i2c_attach_client(client)))
738 goto exit_free;
739
740 vt8231_init_client(client);
741
742 /* Register sysfs hooks */
Roger Lucascbeeb5b2006-09-24 21:21:46 +0200743 if ((err = sysfs_create_group(&client->dev.kobj, &vt8231_group)))
Roger Lucas1de9e372005-11-26 20:20:05 +0100744 goto exit_detach;
Roger Lucas1de9e372005-11-26 20:20:05 +0100745
746 /* Must update device information to find out the config field */
747 data->uch_config = vt8231_read_value(client, VT8231_REG_UCH_CONFIG);
748
Roger Lucascbeeb5b2006-09-24 21:21:46 +0200749 for (i = 0; i < ARRAY_SIZE(vt8231_group_temps); i++) {
Roger Lucas1de9e372005-11-26 20:20:05 +0100750 if (ISTEMP(i, data->uch_config)) {
Roger Lucascbeeb5b2006-09-24 21:21:46 +0200751 if ((err = sysfs_create_group(&client->dev.kobj,
752 &vt8231_group_temps[i])))
753 goto exit_remove_files;
Roger Lucas1de9e372005-11-26 20:20:05 +0100754 }
755 }
756
Roger Lucascbeeb5b2006-09-24 21:21:46 +0200757 for (i = 0; i < ARRAY_SIZE(vt8231_group_volts); i++) {
Roger Lucas1de9e372005-11-26 20:20:05 +0100758 if (ISVOLT(i, data->uch_config)) {
Roger Lucascbeeb5b2006-09-24 21:21:46 +0200759 if ((err = sysfs_create_group(&client->dev.kobj,
760 &vt8231_group_volts[i])))
761 goto exit_remove_files;
Roger Lucas1de9e372005-11-26 20:20:05 +0100762 }
763 }
764
Roger Lucascbeeb5b2006-09-24 21:21:46 +0200765 data->class_dev = hwmon_device_register(&client->dev);
766 if (IS_ERR(data->class_dev)) {
767 err = PTR_ERR(data->class_dev);
768 goto exit_remove_files;
769 }
Roger Lucas1de9e372005-11-26 20:20:05 +0100770 return 0;
771
Roger Lucascbeeb5b2006-09-24 21:21:46 +0200772exit_remove_files:
773 for (i = 0; i < ARRAY_SIZE(vt8231_group_volts); i++)
774 sysfs_remove_group(&client->dev.kobj, &vt8231_group_volts[i]);
775
776 for (i = 0; i < ARRAY_SIZE(vt8231_group_temps); i++)
777 sysfs_remove_group(&client->dev.kobj, &vt8231_group_temps[i]);
778
779 sysfs_remove_group(&client->dev.kobj, &vt8231_group);
Roger Lucas1de9e372005-11-26 20:20:05 +0100780exit_detach:
781 i2c_detach_client(client);
782exit_free:
783 kfree(data);
784exit_release:
785 release_region(isa_address, VT8231_EXTENT);
786 return err;
787}
788
789static int vt8231_detach_client(struct i2c_client *client)
790{
791 struct vt8231_data *data = i2c_get_clientdata(client);
Roger Lucascbeeb5b2006-09-24 21:21:46 +0200792 int err, i;
Roger Lucas1de9e372005-11-26 20:20:05 +0100793
794 hwmon_device_unregister(data->class_dev);
795
Roger Lucascbeeb5b2006-09-24 21:21:46 +0200796 for (i = 0; i < ARRAY_SIZE(vt8231_group_volts); i++)
797 sysfs_remove_group(&client->dev.kobj, &vt8231_group_volts[i]);
798
799 for (i = 0; i < ARRAY_SIZE(vt8231_group_temps); i++)
800 sysfs_remove_group(&client->dev.kobj, &vt8231_group_temps[i]);
801
802 sysfs_remove_group(&client->dev.kobj, &vt8231_group);
803
Roger Lucas1de9e372005-11-26 20:20:05 +0100804 if ((err = i2c_detach_client(client))) {
805 return err;
806 }
807
808 release_region(client->addr, VT8231_EXTENT);
809 kfree(data);
810
811 return 0;
812}
813
814static void vt8231_init_client(struct i2c_client *client)
815{
816 vt8231_write_value(client, VT8231_REG_TEMP1_CONFIG, 0);
817 vt8231_write_value(client, VT8231_REG_TEMP2_CONFIG, 0);
818}
819
820static struct vt8231_data *vt8231_update_device(struct device *dev)
821{
822 struct i2c_client *client = to_i2c_client(dev);
823 struct vt8231_data *data = i2c_get_clientdata(client);
824 int i;
825 u16 low;
826
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100827 mutex_lock(&data->update_lock);
Roger Lucas1de9e372005-11-26 20:20:05 +0100828
829 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
830 || !data->valid) {
831 for (i = 0; i < 6; i++) {
832 if (ISVOLT(i, data->uch_config)) {
833 data->in[i] = vt8231_read_value(client,
834 regvolt[i]);
835 data->in_min[i] = vt8231_read_value(client,
836 regvoltmin[i]);
837 data->in_max[i] = vt8231_read_value(client,
838 regvoltmax[i]);
839 }
840 }
841 for (i = 0; i < 2; i++) {
842 data->fan[i] = vt8231_read_value(client,
843 VT8231_REG_FAN(i));
844 data->fan_min[i] = vt8231_read_value(client,
845 VT8231_REG_FAN_MIN(i));
846 }
847
848 low = vt8231_read_value(client, VT8231_REG_TEMP_LOW01);
849 low = (low >> 6) | ((low & 0x30) >> 2)
850 | (vt8231_read_value(client, VT8231_REG_TEMP_LOW25) << 4);
851 for (i = 0; i < 6; i++) {
852 if (ISTEMP(i, data->uch_config)) {
853 data->temp[i] = (vt8231_read_value(client,
854 regtemp[i]) << 2)
855 | ((low >> (2 * i)) & 0x03);
856 data->temp_max[i] = vt8231_read_value(client,
857 regtempmax[i]);
858 data->temp_min[i] = vt8231_read_value(client,
859 regtempmin[i]);
860 }
861 }
862
863 i = vt8231_read_value(client, VT8231_REG_FANDIV);
864 data->fan_div[0] = (i >> 4) & 0x03;
865 data->fan_div[1] = i >> 6;
866 data->alarms = vt8231_read_value(client, VT8231_REG_ALARM1) |
867 (vt8231_read_value(client, VT8231_REG_ALARM2) << 8);
868
869 /* Set alarm flags correctly */
870 if (!data->fan[0] && data->fan_min[0]) {
871 data->alarms |= 0x40;
872 } else if (data->fan[0] && !data->fan_min[0]) {
873 data->alarms &= ~0x40;
874 }
875
876 if (!data->fan[1] && data->fan_min[1]) {
877 data->alarms |= 0x80;
878 } else if (data->fan[1] && !data->fan_min[1]) {
879 data->alarms &= ~0x80;
880 }
881
882 data->last_updated = jiffies;
883 data->valid = 1;
884 }
885
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100886 mutex_unlock(&data->update_lock);
Roger Lucas1de9e372005-11-26 20:20:05 +0100887
888 return data;
889}
890
891static int __devinit vt8231_pci_probe(struct pci_dev *dev,
892 const struct pci_device_id *id)
893{
894 u16 val;
895
896 if (PCIBIOS_SUCCESSFUL != pci_read_config_word(dev, VT8231_BASE_REG,
897 &val))
898 return -ENODEV;
899
900 isa_address = val & ~(VT8231_EXTENT - 1);
901 if (isa_address == 0 && force_addr == 0) {
902 dev_err(&dev->dev, "base address not set -\
903 upgrade BIOS or use force_addr=0xaddr\n");
904 return -ENODEV;
905 }
906
907 s_bridge = pci_dev_get(dev);
908
909 if (i2c_isa_add_driver(&vt8231_driver)) {
910 pci_dev_put(s_bridge);
911 s_bridge = NULL;
912 }
913
914 /* Always return failure here. This is to allow other drivers to bind
915 * to this pci device. We don't really want to have control over the
916 * pci device, we only wanted to read as few register values from it.
917 */
918 return -ENODEV;
919}
920
921static int __init sm_vt8231_init(void)
922{
Richard Knutsson93b47682005-11-30 01:00:35 +0100923 return pci_register_driver(&vt8231_pci_driver);
Roger Lucas1de9e372005-11-26 20:20:05 +0100924}
925
926static void __exit sm_vt8231_exit(void)
927{
928 pci_unregister_driver(&vt8231_pci_driver);
929 if (s_bridge != NULL) {
930 i2c_isa_del_driver(&vt8231_driver);
931 pci_dev_put(s_bridge);
932 s_bridge = NULL;
933 }
934}
935
936MODULE_AUTHOR("Roger Lucas <roger@planbit.co.uk>");
937MODULE_DESCRIPTION("VT8231 sensors");
938MODULE_LICENSE("GPL");
939
940module_init(sm_vt8231_init);
941module_exit(sm_vt8231_exit);