]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/hwmon/vt8231.c
Merge branch 'packaging' of git://git.kernel.org/pub/scm/linux/kernel/git/mmarek...
[net-next-2.6.git] / drivers / hwmon / vt8231.c
CommitLineData
1de9e371
RL
1/*
2 vt8231.c - Part of lm_sensors, Linux kernel modules
3 for hardware monitoring
4
af865765 5 Copyright (c) 2005 Roger Lucas <vt8231@hiddenengine.co.uk>
1de9e371
RL
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>
ec5e1a4b 32#include <linux/platform_device.h>
1de9e371
RL
33#include <linux/hwmon.h>
34#include <linux/hwmon-sysfs.h>
35#include <linux/hwmon-vid.h>
36#include <linux/err.h>
9a61bf63 37#include <linux/mutex.h>
b9acb64a 38#include <linux/acpi.h>
6055fae8 39#include <linux/io.h>
1de9e371
RL
40
41static int force_addr;
42module_param(force_addr, int, 0);
43MODULE_PARM_DESC(force_addr, "Initialize the base address of the sensors");
44
ec5e1a4b 45static struct platform_device *pdev;
1de9e371
RL
46
47#define VT8231_EXTENT 0x80
48#define VT8231_BASE_REG 0x70
49#define VT8231_ENABLE_REG 0x74
50
51/* The VT8231 registers
52
53 The reset value for the input channel configuration is used (Reg 0x4A=0x07)
54 which sets the selected inputs marked with '*' below if multiple options are
55 possible:
56
57 Voltage Mode Temperature Mode
58 Sensor Linux Id Linux Id VIA Id
59 -------- -------- -------- ------
60 CPU Diode N/A temp1 0
61 UIC1 in0 temp2 * 1
62 UIC2 in1 * temp3 2
63 UIC3 in2 * temp4 3
64 UIC4 in3 * temp5 4
65 UIC5 in4 * temp6 5
66 3.3V in5 N/A
67
68 Note that the BIOS may set the configuration register to a different value
69 to match the motherboard configuration.
70*/
71
72/* fans numbered 0-1 */
73#define VT8231_REG_FAN_MIN(nr) (0x3b + (nr))
74#define VT8231_REG_FAN(nr) (0x29 + (nr))
75
76/* Voltage inputs numbered 0-5 */
77
78static const u8 regvolt[] = { 0x21, 0x22, 0x23, 0x24, 0x25, 0x26 };
79static const u8 regvoltmax[] = { 0x3d, 0x2b, 0x2d, 0x2f, 0x31, 0x33 };
80static const u8 regvoltmin[] = { 0x3e, 0x2c, 0x2e, 0x30, 0x32, 0x34 };
81
82/* Temperatures are numbered 1-6 according to the Linux kernel specification.
83**
84** In the VIA datasheet, however, the temperatures are numbered from zero.
85** Since it is important that this driver can easily be compared to the VIA
86** datasheet, we will use the VIA numbering within this driver and map the
87** kernel sysfs device name to the VIA number in the sysfs callback.
88*/
89
90#define VT8231_REG_TEMP_LOW01 0x49
91#define VT8231_REG_TEMP_LOW25 0x4d
92
93static const u8 regtemp[] = { 0x1f, 0x21, 0x22, 0x23, 0x24, 0x25 };
94static const u8 regtempmax[] = { 0x39, 0x3d, 0x2b, 0x2d, 0x2f, 0x31 };
95static const u8 regtempmin[] = { 0x3a, 0x3e, 0x2c, 0x2e, 0x30, 0x32 };
96
97#define TEMP_FROM_REG(reg) (((253 * 4 - (reg)) * 550 + 105) / 210)
98#define TEMP_MAXMIN_FROM_REG(reg) (((253 - (reg)) * 2200 + 105) / 210)
99#define TEMP_MAXMIN_TO_REG(val) (253 - ((val) * 210 + 1100) / 2200)
100
101#define VT8231_REG_CONFIG 0x40
102#define VT8231_REG_ALARM1 0x41
103#define VT8231_REG_ALARM2 0x42
104#define VT8231_REG_FANDIV 0x47
105#define VT8231_REG_UCH_CONFIG 0x4a
106#define VT8231_REG_TEMP1_CONFIG 0x4b
107#define VT8231_REG_TEMP2_CONFIG 0x4c
108
109/* temps 0-5 as numbered in VIA datasheet - see later for mapping to Linux
110** numbering
111*/
112#define ISTEMP(i, ch_config) ((i) == 0 ? 1 : \
113 ((ch_config) >> ((i)+1)) & 0x01)
114/* voltages 0-5 */
115#define ISVOLT(i, ch_config) ((i) == 5 ? 1 : \
116 !(((ch_config) >> ((i)+2)) & 0x01))
117
118#define DIV_FROM_REG(val) (1 << (val))
119
120/* NB The values returned here are NOT temperatures. The calibration curves
121** for the thermistor curves are board-specific and must go in the
122** sensors.conf file. Temperature sensors are actually ten bits, but the
123** VIA datasheet only considers the 8 MSBs obtained from the regtemp[]
124** register. The temperature value returned should have a magnitude of 3,
125** so we use the VIA scaling as the "true" scaling and use the remaining 2
126** LSBs as fractional precision.
127**
128** All the on-chip hardware temperature comparisons for the alarms are only
129** 8-bits wide, and compare against the 8 MSBs of the temperature. The bits
130** in the registers VT8231_REG_TEMP_LOW01 and VT8231_REG_TEMP_LOW25 are
131** ignored.
132*/
133
134/******** FAN RPM CONVERSIONS ********
135** This chip saturates back at 0, not at 255 like many the other chips.
136** So, 0 means 0 RPM
137*/
138static inline u8 FAN_TO_REG(long rpm, int div)
139{
140 if (rpm == 0)
141 return 0;
142 return SENSORS_LIMIT(1310720 / (rpm * div), 1, 255);
143}
144
145#define FAN_FROM_REG(val, div) ((val) == 0 ? 0 : 1310720 / ((val) * (div)))
146
147struct vt8231_data {
ec5e1a4b
RL
148 unsigned short addr;
149 const char *name;
150
9a61bf63 151 struct mutex update_lock;
1beeffe4 152 struct device *hwmon_dev;
1de9e371
RL
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;
ec5e1a4b 170static int vt8231_probe(struct platform_device *pdev);
d0546128 171static int __devexit vt8231_remove(struct platform_device *pdev);
1de9e371 172static struct vt8231_data *vt8231_update_device(struct device *dev);
ec5e1a4b 173static void vt8231_init_device(struct vt8231_data *data);
1de9e371 174
ec5e1a4b 175static inline int vt8231_read_value(struct vt8231_data *data, u8 reg)
1de9e371 176{
ec5e1a4b 177 return inb_p(data->addr + reg);
1de9e371
RL
178}
179
ec5e1a4b 180static inline void vt8231_write_value(struct vt8231_data *data, u8 reg,
1de9e371
RL
181 u8 value)
182{
ec5e1a4b 183 outb_p(value, data->addr + reg);
1de9e371
RL
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;
ec5e1a4b 222 struct vt8231_data *data = dev_get_drvdata(dev);
1de9e371
RL
223 unsigned long val = simple_strtoul(buf, NULL, 10);
224
9a61bf63 225 mutex_lock(&data->update_lock);
1de9e371 226 data->in_min[nr] = SENSORS_LIMIT(((val * 958) / 10000) + 3, 0, 255);
ec5e1a4b 227 vt8231_write_value(data, regvoltmin[nr], data->in_min[nr]);
9a61bf63 228 mutex_unlock(&data->update_lock);
1de9e371
RL
229 return count;
230}
231
232static ssize_t set_in_max(struct device *dev, struct device_attribute *attr,
233 const char *buf, size_t count)
234{
235 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
236 int nr = sensor_attr->index;
ec5e1a4b 237 struct vt8231_data *data = dev_get_drvdata(dev);
1de9e371
RL
238 unsigned long val = simple_strtoul(buf, NULL, 10);
239
9a61bf63 240 mutex_lock(&data->update_lock);
1de9e371 241 data->in_max[nr] = SENSORS_LIMIT(((val * 958) / 10000) + 3, 0, 255);
ec5e1a4b 242 vt8231_write_value(data, regvoltmax[nr], data->in_max[nr]);
9a61bf63 243 mutex_unlock(&data->update_lock);
1de9e371
RL
244 return count;
245}
246
247/* Special case for input 5 as this has 3.3V scaling built into the chip */
248static ssize_t show_in5(struct device *dev, struct device_attribute *attr,
249 char *buf)
250{
251 struct vt8231_data *data = vt8231_update_device(dev);
252
253 return sprintf(buf, "%d\n",
254 (((data->in[5] - 3) * 10000 * 54) / (958 * 34)));
255}
256
257static ssize_t show_in5_min(struct device *dev, struct device_attribute *attr,
258 char *buf)
259{
260 struct vt8231_data *data = vt8231_update_device(dev);
261
262 return sprintf(buf, "%d\n",
263 (((data->in_min[5] - 3) * 10000 * 54) / (958 * 34)));
264}
265
266static ssize_t show_in5_max(struct device *dev, struct device_attribute *attr,
267 char *buf)
268{
269 struct vt8231_data *data = vt8231_update_device(dev);
270
271 return sprintf(buf, "%d\n",
272 (((data->in_max[5] - 3) * 10000 * 54) / (958 * 34)));
273}
274
275static ssize_t set_in5_min(struct device *dev, struct device_attribute *attr,
276 const char *buf, size_t count)
277{
ec5e1a4b 278 struct vt8231_data *data = dev_get_drvdata(dev);
1de9e371
RL
279 unsigned long val = simple_strtoul(buf, NULL, 10);
280
9a61bf63 281 mutex_lock(&data->update_lock);
1de9e371
RL
282 data->in_min[5] = SENSORS_LIMIT(((val * 958 * 34) / (10000 * 54)) + 3,
283 0, 255);
ec5e1a4b 284 vt8231_write_value(data, regvoltmin[5], data->in_min[5]);
9a61bf63 285 mutex_unlock(&data->update_lock);
1de9e371
RL
286 return count;
287}
288
289static ssize_t set_in5_max(struct device *dev, struct device_attribute *attr,
290 const char *buf, size_t count)
291{
ec5e1a4b 292 struct vt8231_data *data = dev_get_drvdata(dev);
1de9e371
RL
293 unsigned long val = simple_strtoul(buf, NULL, 10);
294
9a61bf63 295 mutex_lock(&data->update_lock);
1de9e371
RL
296 data->in_max[5] = SENSORS_LIMIT(((val * 958 * 34) / (10000 * 54)) + 3,
297 0, 255);
ec5e1a4b 298 vt8231_write_value(data, regvoltmax[5], data->in_max[5]);
9a61bf63 299 mutex_unlock(&data->update_lock);
1de9e371
RL
300 return count;
301}
302
303#define define_voltage_sysfs(offset) \
304static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \
305 show_in, NULL, offset); \
306static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
307 show_in_min, set_in_min, offset); \
308static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
309 show_in_max, set_in_max, offset)
310
311define_voltage_sysfs(0);
312define_voltage_sysfs(1);
313define_voltage_sysfs(2);
314define_voltage_sysfs(3);
315define_voltage_sysfs(4);
316
317static DEVICE_ATTR(in5_input, S_IRUGO, show_in5, NULL);
318static DEVICE_ATTR(in5_min, S_IRUGO | S_IWUSR, show_in5_min, set_in5_min);
319static DEVICE_ATTR(in5_max, S_IRUGO | S_IWUSR, show_in5_max, set_in5_max);
320
321/* Temperatures */
322static ssize_t show_temp0(struct device *dev, struct device_attribute *attr,
323 char *buf)
324{
325 struct vt8231_data *data = vt8231_update_device(dev);
326 return sprintf(buf, "%d\n", data->temp[0] * 250);
327}
328
329static ssize_t show_temp0_max(struct device *dev, struct device_attribute *attr,
330 char *buf)
331{
332 struct vt8231_data *data = vt8231_update_device(dev);
333 return sprintf(buf, "%d\n", data->temp_max[0] * 1000);
334}
335
336static ssize_t show_temp0_min(struct device *dev, struct device_attribute *attr,
337 char *buf)
338{
339 struct vt8231_data *data = vt8231_update_device(dev);
340 return sprintf(buf, "%d\n", data->temp_min[0] * 1000);
341}
342
343static ssize_t set_temp0_max(struct device *dev, struct device_attribute *attr,
344 const char *buf, size_t count)
345{
ec5e1a4b 346 struct vt8231_data *data = dev_get_drvdata(dev);
1de9e371
RL
347 int val = simple_strtol(buf, NULL, 10);
348
9a61bf63 349 mutex_lock(&data->update_lock);
1de9e371 350 data->temp_max[0] = SENSORS_LIMIT((val + 500) / 1000, 0, 255);
ec5e1a4b 351 vt8231_write_value(data, regtempmax[0], data->temp_max[0]);
9a61bf63 352 mutex_unlock(&data->update_lock);
1de9e371
RL
353 return count;
354}
355static ssize_t set_temp0_min(struct device *dev, struct device_attribute *attr,
356 const char *buf, size_t count)
357{
ec5e1a4b 358 struct vt8231_data *data = dev_get_drvdata(dev);
1de9e371
RL
359 int val = simple_strtol(buf, NULL, 10);
360
9a61bf63 361 mutex_lock(&data->update_lock);
1de9e371 362 data->temp_min[0] = SENSORS_LIMIT((val + 500) / 1000, 0, 255);
ec5e1a4b 363 vt8231_write_value(data, regtempmin[0], data->temp_min[0]);
9a61bf63 364 mutex_unlock(&data->update_lock);
1de9e371
RL
365 return count;
366}
367
368static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
369 char *buf)
370{
371 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
372 int nr = sensor_attr->index;
373 struct vt8231_data *data = vt8231_update_device(dev);
374 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr]));
375}
376
377static ssize_t show_temp_max(struct device *dev, struct device_attribute *attr,
378 char *buf)
379{
380 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
381 int nr = sensor_attr->index;
382 struct vt8231_data *data = vt8231_update_device(dev);
383 return sprintf(buf, "%d\n", TEMP_MAXMIN_FROM_REG(data->temp_max[nr]));
384}
385
386static ssize_t show_temp_min(struct device *dev, struct device_attribute *attr,
387 char *buf)
388{
389 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
390 int nr = sensor_attr->index;
391 struct vt8231_data *data = vt8231_update_device(dev);
392 return sprintf(buf, "%d\n", TEMP_MAXMIN_FROM_REG(data->temp_min[nr]));
393}
394
395static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
396 const char *buf, size_t count)
397{
398 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
399 int nr = sensor_attr->index;
ec5e1a4b 400 struct vt8231_data *data = dev_get_drvdata(dev);
1de9e371
RL
401 int val = simple_strtol(buf, NULL, 10);
402
9a61bf63 403 mutex_lock(&data->update_lock);
1de9e371 404 data->temp_max[nr] = SENSORS_LIMIT(TEMP_MAXMIN_TO_REG(val), 0, 255);
ec5e1a4b 405 vt8231_write_value(data, regtempmax[nr], data->temp_max[nr]);
9a61bf63 406 mutex_unlock(&data->update_lock);
1de9e371
RL
407 return count;
408}
409static ssize_t set_temp_min(struct device *dev, struct device_attribute *attr,
410 const char *buf, size_t count)
411{
412 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
413 int nr = sensor_attr->index;
ec5e1a4b 414 struct vt8231_data *data = dev_get_drvdata(dev);
1de9e371
RL
415 int val = simple_strtol(buf, NULL, 10);
416
9a61bf63 417 mutex_lock(&data->update_lock);
1de9e371 418 data->temp_min[nr] = SENSORS_LIMIT(TEMP_MAXMIN_TO_REG(val), 0, 255);
ec5e1a4b 419 vt8231_write_value(data, regtempmin[nr], data->temp_min[nr]);
9a61bf63 420 mutex_unlock(&data->update_lock);
1de9e371
RL
421 return count;
422}
423
424/* Note that these map the Linux temperature sensor numbering (1-6) to the VIA
425** temperature sensor numbering (0-5)
426*/
427#define define_temperature_sysfs(offset) \
428static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
429 show_temp, NULL, offset - 1); \
430static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
431 show_temp_max, set_temp_max, offset - 1); \
e3efa5a7 432static SENSOR_DEVICE_ATTR(temp##offset##_max_hyst, S_IRUGO | S_IWUSR, \
1de9e371
RL
433 show_temp_min, set_temp_min, offset - 1)
434
435static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp0, NULL);
436static DEVICE_ATTR(temp1_max, S_IRUGO | S_IWUSR, show_temp0_max, set_temp0_max);
e3efa5a7 437static DEVICE_ATTR(temp1_max_hyst, S_IRUGO | S_IWUSR, show_temp0_min, set_temp0_min);
1de9e371
RL
438
439define_temperature_sysfs(2);
440define_temperature_sysfs(3);
441define_temperature_sysfs(4);
442define_temperature_sysfs(5);
443define_temperature_sysfs(6);
444
1de9e371
RL
445/* Fans */
446static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
447 char *buf)
448{
449 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
450 int nr = sensor_attr->index;
451 struct vt8231_data *data = vt8231_update_device(dev);
452 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr],
453 DIV_FROM_REG(data->fan_div[nr])));
454}
455
456static ssize_t show_fan_min(struct device *dev, struct device_attribute *attr,
457 char *buf)
458{
459 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
460 int nr = sensor_attr->index;
461 struct vt8231_data *data = vt8231_update_device(dev);
462 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[nr],
463 DIV_FROM_REG(data->fan_div[nr])));
464}
465
466static ssize_t show_fan_div(struct device *dev, struct device_attribute *attr,
467 char *buf)
468{
469 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
470 int nr = sensor_attr->index;
471 struct vt8231_data *data = vt8231_update_device(dev);
472 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]));
473}
474
475static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
476 const char *buf, size_t count)
477{
478 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
479 int nr = sensor_attr->index;
ec5e1a4b 480 struct vt8231_data *data = dev_get_drvdata(dev);
1de9e371
RL
481 int val = simple_strtoul(buf, NULL, 10);
482
9a61bf63 483 mutex_lock(&data->update_lock);
1de9e371 484 data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
ec5e1a4b 485 vt8231_write_value(data, VT8231_REG_FAN_MIN(nr), data->fan_min[nr]);
9a61bf63 486 mutex_unlock(&data->update_lock);
1de9e371
RL
487 return count;
488}
489
490static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
491 const char *buf, size_t count)
492{
ec5e1a4b 493 struct vt8231_data *data = dev_get_drvdata(dev);
1de9e371
RL
494 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
495 unsigned long val = simple_strtoul(buf, NULL, 10);
496 int nr = sensor_attr->index;
ec5e1a4b 497 int old = vt8231_read_value(data, VT8231_REG_FANDIV);
1de9e371
RL
498 long min = FAN_FROM_REG(data->fan_min[nr],
499 DIV_FROM_REG(data->fan_div[nr]));
500
9a61bf63 501 mutex_lock(&data->update_lock);
1de9e371
RL
502 switch (val) {
503 case 1: data->fan_div[nr] = 0; break;
504 case 2: data->fan_div[nr] = 1; break;
505 case 4: data->fan_div[nr] = 2; break;
506 case 8: data->fan_div[nr] = 3; break;
507 default:
b20ff13a 508 dev_err(dev, "fan_div value %ld not supported. "
1de9e371 509 "Choose one of 1, 2, 4 or 8!\n", val);
9a61bf63 510 mutex_unlock(&data->update_lock);
1de9e371
RL
511 return -EINVAL;
512 }
513
514 /* Correct the fan minimum speed */
515 data->fan_min[nr] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
ec5e1a4b 516 vt8231_write_value(data, VT8231_REG_FAN_MIN(nr), data->fan_min[nr]);
1de9e371
RL
517
518 old = (old & 0x0f) | (data->fan_div[1] << 6) | (data->fan_div[0] << 4);
ec5e1a4b 519 vt8231_write_value(data, VT8231_REG_FANDIV, old);
9a61bf63 520 mutex_unlock(&data->update_lock);
1de9e371
RL
521 return count;
522}
523
524
525#define define_fan_sysfs(offset) \
526static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
527 show_fan, NULL, offset - 1); \
528static SENSOR_DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
529 show_fan_div, set_fan_div, offset - 1); \
530static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
531 show_fan_min, set_fan_min, offset - 1)
532
533define_fan_sysfs(1);
534define_fan_sysfs(2);
535
536/* Alarms */
537static ssize_t show_alarms(struct device *dev, struct device_attribute *attr,
538 char *buf)
539{
540 struct vt8231_data *data = vt8231_update_device(dev);
541 return sprintf(buf, "%d\n", data->alarms);
542}
1de9e371
RL
543static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
544
2d1374ca
JD
545static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
546 char *buf)
547{
548 int bitnr = to_sensor_dev_attr(attr)->index;
549 struct vt8231_data *data = vt8231_update_device(dev);
550 return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
551}
552static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 4);
553static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 11);
554static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 0);
555static SENSOR_DEVICE_ATTR(temp4_alarm, S_IRUGO, show_alarm, NULL, 1);
556static SENSOR_DEVICE_ATTR(temp5_alarm, S_IRUGO, show_alarm, NULL, 3);
557static SENSOR_DEVICE_ATTR(temp6_alarm, S_IRUGO, show_alarm, NULL, 8);
558static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 11);
559static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 0);
560static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 1);
561static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3);
562static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 8);
563static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 2);
564static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 6);
565static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 7);
566
ec5e1a4b
RL
567static ssize_t show_name(struct device *dev, struct device_attribute
568 *devattr, char *buf)
569{
570 struct vt8231_data *data = dev_get_drvdata(dev);
571 return sprintf(buf, "%s\n", data->name);
572}
573static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
574
2d1374ca 575static struct attribute *vt8231_attributes_temps[6][5] = {
cbeeb5b7
RL
576 {
577 &dev_attr_temp1_input.attr,
578 &dev_attr_temp1_max_hyst.attr,
579 &dev_attr_temp1_max.attr,
2d1374ca 580 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
cbeeb5b7
RL
581 NULL
582 }, {
583 &sensor_dev_attr_temp2_input.dev_attr.attr,
584 &sensor_dev_attr_temp2_max_hyst.dev_attr.attr,
585 &sensor_dev_attr_temp2_max.dev_attr.attr,
2d1374ca 586 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
cbeeb5b7
RL
587 NULL
588 }, {
589 &sensor_dev_attr_temp3_input.dev_attr.attr,
590 &sensor_dev_attr_temp3_max_hyst.dev_attr.attr,
591 &sensor_dev_attr_temp3_max.dev_attr.attr,
2d1374ca 592 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
cbeeb5b7
RL
593 NULL
594 }, {
595 &sensor_dev_attr_temp4_input.dev_attr.attr,
596 &sensor_dev_attr_temp4_max_hyst.dev_attr.attr,
597 &sensor_dev_attr_temp4_max.dev_attr.attr,
2d1374ca 598 &sensor_dev_attr_temp4_alarm.dev_attr.attr,
cbeeb5b7
RL
599 NULL
600 }, {
601 &sensor_dev_attr_temp5_input.dev_attr.attr,
602 &sensor_dev_attr_temp5_max_hyst.dev_attr.attr,
603 &sensor_dev_attr_temp5_max.dev_attr.attr,
2d1374ca 604 &sensor_dev_attr_temp5_alarm.dev_attr.attr,
cbeeb5b7
RL
605 NULL
606 }, {
607 &sensor_dev_attr_temp6_input.dev_attr.attr,
608 &sensor_dev_attr_temp6_max_hyst.dev_attr.attr,
609 &sensor_dev_attr_temp6_max.dev_attr.attr,
2d1374ca 610 &sensor_dev_attr_temp6_alarm.dev_attr.attr,
cbeeb5b7
RL
611 NULL
612 }
613};
614
615static const struct attribute_group vt8231_group_temps[6] = {
616 { .attrs = vt8231_attributes_temps[0] },
617 { .attrs = vt8231_attributes_temps[1] },
618 { .attrs = vt8231_attributes_temps[2] },
619 { .attrs = vt8231_attributes_temps[3] },
620 { .attrs = vt8231_attributes_temps[4] },
621 { .attrs = vt8231_attributes_temps[5] },
622};
623
2d1374ca 624static struct attribute *vt8231_attributes_volts[6][5] = {
cbeeb5b7
RL
625 {
626 &sensor_dev_attr_in0_input.dev_attr.attr,
627 &sensor_dev_attr_in0_min.dev_attr.attr,
628 &sensor_dev_attr_in0_max.dev_attr.attr,
2d1374ca 629 &sensor_dev_attr_in0_alarm.dev_attr.attr,
cbeeb5b7
RL
630 NULL
631 }, {
632 &sensor_dev_attr_in1_input.dev_attr.attr,
633 &sensor_dev_attr_in1_min.dev_attr.attr,
634 &sensor_dev_attr_in1_max.dev_attr.attr,
2d1374ca 635 &sensor_dev_attr_in1_alarm.dev_attr.attr,
cbeeb5b7
RL
636 NULL
637 }, {
638 &sensor_dev_attr_in2_input.dev_attr.attr,
639 &sensor_dev_attr_in2_min.dev_attr.attr,
640 &sensor_dev_attr_in2_max.dev_attr.attr,
2d1374ca 641 &sensor_dev_attr_in2_alarm.dev_attr.attr,
cbeeb5b7
RL
642 NULL
643 }, {
644 &sensor_dev_attr_in3_input.dev_attr.attr,
645 &sensor_dev_attr_in3_min.dev_attr.attr,
646 &sensor_dev_attr_in3_max.dev_attr.attr,
2d1374ca 647 &sensor_dev_attr_in3_alarm.dev_attr.attr,
cbeeb5b7
RL
648 NULL
649 }, {
650 &sensor_dev_attr_in4_input.dev_attr.attr,
651 &sensor_dev_attr_in4_min.dev_attr.attr,
652 &sensor_dev_attr_in4_max.dev_attr.attr,
2d1374ca 653 &sensor_dev_attr_in4_alarm.dev_attr.attr,
cbeeb5b7
RL
654 NULL
655 }, {
656 &dev_attr_in5_input.attr,
657 &dev_attr_in5_min.attr,
658 &dev_attr_in5_max.attr,
2d1374ca 659 &sensor_dev_attr_in5_alarm.dev_attr.attr,
cbeeb5b7
RL
660 NULL
661 }
662};
663
664static const struct attribute_group vt8231_group_volts[6] = {
665 { .attrs = vt8231_attributes_volts[0] },
666 { .attrs = vt8231_attributes_volts[1] },
667 { .attrs = vt8231_attributes_volts[2] },
668 { .attrs = vt8231_attributes_volts[3] },
669 { .attrs = vt8231_attributes_volts[4] },
670 { .attrs = vt8231_attributes_volts[5] },
671};
672
673static struct attribute *vt8231_attributes[] = {
674 &sensor_dev_attr_fan1_input.dev_attr.attr,
675 &sensor_dev_attr_fan2_input.dev_attr.attr,
676 &sensor_dev_attr_fan1_min.dev_attr.attr,
677 &sensor_dev_attr_fan2_min.dev_attr.attr,
678 &sensor_dev_attr_fan1_div.dev_attr.attr,
679 &sensor_dev_attr_fan2_div.dev_attr.attr,
2d1374ca
JD
680 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
681 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
cbeeb5b7 682 &dev_attr_alarms.attr,
ec5e1a4b 683 &dev_attr_name.attr,
cbeeb5b7
RL
684 NULL
685};
686
687static const struct attribute_group vt8231_group = {
688 .attrs = vt8231_attributes,
689};
690
ec5e1a4b 691static struct platform_driver vt8231_driver = {
cdaf7934 692 .driver = {
87218842 693 .owner = THIS_MODULE,
cdaf7934
LR
694 .name = "vt8231",
695 },
ec5e1a4b
RL
696 .probe = vt8231_probe,
697 .remove = __devexit_p(vt8231_remove),
1de9e371
RL
698};
699
3dd3a156 700static const struct pci_device_id vt8231_pci_ids[] = {
1de9e371
RL
701 { PCI_DEVICE(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8231_4) },
702 { 0, }
703};
704
705MODULE_DEVICE_TABLE(pci, vt8231_pci_ids);
706
707static int __devinit vt8231_pci_probe(struct pci_dev *dev,
708 const struct pci_device_id *id);
709
710static struct pci_driver vt8231_pci_driver = {
711 .name = "vt8231",
712 .id_table = vt8231_pci_ids,
713 .probe = vt8231_pci_probe,
714};
715
a022fef5 716static int vt8231_probe(struct platform_device *pdev)
1de9e371 717{
ec5e1a4b 718 struct resource *res;
1de9e371
RL
719 struct vt8231_data *data;
720 int err = 0, i;
1de9e371
RL
721
722 /* Reserve the ISA region */
ec5e1a4b
RL
723 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
724 if (!request_region(res->start, VT8231_EXTENT,
725 vt8231_driver.driver.name)) {
726 dev_err(&pdev->dev, "Region 0x%lx-0x%lx already in use!\n",
727 (unsigned long)res->start, (unsigned long)res->end);
1de9e371
RL
728 return -ENODEV;
729 }
730
731 if (!(data = kzalloc(sizeof(struct vt8231_data), GFP_KERNEL))) {
732 err = -ENOMEM;
733 goto exit_release;
734 }
735
ec5e1a4b
RL
736 platform_set_drvdata(pdev, data);
737 data->addr = res->start;
738 data->name = "vt8231";
1de9e371 739
9a61bf63 740 mutex_init(&data->update_lock);
ec5e1a4b 741 vt8231_init_device(data);
1de9e371
RL
742
743 /* Register sysfs hooks */
ec5e1a4b
RL
744 if ((err = sysfs_create_group(&pdev->dev.kobj, &vt8231_group)))
745 goto exit_free;
1de9e371
RL
746
747 /* Must update device information to find out the config field */
ec5e1a4b 748 data->uch_config = vt8231_read_value(data, VT8231_REG_UCH_CONFIG);
1de9e371 749
cbeeb5b7 750 for (i = 0; i < ARRAY_SIZE(vt8231_group_temps); i++) {
1de9e371 751 if (ISTEMP(i, data->uch_config)) {
ec5e1a4b 752 if ((err = sysfs_create_group(&pdev->dev.kobj,
cbeeb5b7
RL
753 &vt8231_group_temps[i])))
754 goto exit_remove_files;
1de9e371
RL
755 }
756 }
757
cbeeb5b7 758 for (i = 0; i < ARRAY_SIZE(vt8231_group_volts); i++) {
1de9e371 759 if (ISVOLT(i, data->uch_config)) {
ec5e1a4b 760 if ((err = sysfs_create_group(&pdev->dev.kobj,
cbeeb5b7
RL
761 &vt8231_group_volts[i])))
762 goto exit_remove_files;
1de9e371
RL
763 }
764 }
765
1beeffe4
TJ
766 data->hwmon_dev = hwmon_device_register(&pdev->dev);
767 if (IS_ERR(data->hwmon_dev)) {
768 err = PTR_ERR(data->hwmon_dev);
cbeeb5b7
RL
769 goto exit_remove_files;
770 }
1de9e371
RL
771 return 0;
772
cbeeb5b7
RL
773exit_remove_files:
774 for (i = 0; i < ARRAY_SIZE(vt8231_group_volts); i++)
ec5e1a4b 775 sysfs_remove_group(&pdev->dev.kobj, &vt8231_group_volts[i]);
cbeeb5b7
RL
776
777 for (i = 0; i < ARRAY_SIZE(vt8231_group_temps); i++)
ec5e1a4b
RL
778 sysfs_remove_group(&pdev->dev.kobj, &vt8231_group_temps[i]);
779
780 sysfs_remove_group(&pdev->dev.kobj, &vt8231_group);
cbeeb5b7 781
1de9e371 782exit_free:
04a6217d 783 platform_set_drvdata(pdev, NULL);
1de9e371 784 kfree(data);
ec5e1a4b 785
1de9e371 786exit_release:
ec5e1a4b 787 release_region(res->start, VT8231_EXTENT);
1de9e371
RL
788 return err;
789}
790
d0546128 791static int __devexit vt8231_remove(struct platform_device *pdev)
1de9e371 792{
ec5e1a4b
RL
793 struct vt8231_data *data = platform_get_drvdata(pdev);
794 int i;
1de9e371 795
1beeffe4 796 hwmon_device_unregister(data->hwmon_dev);
1de9e371 797
cbeeb5b7 798 for (i = 0; i < ARRAY_SIZE(vt8231_group_volts); i++)
ec5e1a4b 799 sysfs_remove_group(&pdev->dev.kobj, &vt8231_group_volts[i]);
cbeeb5b7
RL
800
801 for (i = 0; i < ARRAY_SIZE(vt8231_group_temps); i++)
ec5e1a4b 802 sysfs_remove_group(&pdev->dev.kobj, &vt8231_group_temps[i]);
cbeeb5b7 803
ec5e1a4b 804 sysfs_remove_group(&pdev->dev.kobj, &vt8231_group);
cbeeb5b7 805
ec5e1a4b
RL
806 release_region(data->addr, VT8231_EXTENT);
807 platform_set_drvdata(pdev, NULL);
1de9e371 808 kfree(data);
1de9e371
RL
809 return 0;
810}
811
ec5e1a4b 812static void vt8231_init_device(struct vt8231_data *data)
1de9e371 813{
ec5e1a4b
RL
814 vt8231_write_value(data, VT8231_REG_TEMP1_CONFIG, 0);
815 vt8231_write_value(data, VT8231_REG_TEMP2_CONFIG, 0);
1de9e371
RL
816}
817
818static struct vt8231_data *vt8231_update_device(struct device *dev)
819{
ec5e1a4b 820 struct vt8231_data *data = dev_get_drvdata(dev);
1de9e371
RL
821 int i;
822 u16 low;
823
9a61bf63 824 mutex_lock(&data->update_lock);
1de9e371
RL
825
826 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
827 || !data->valid) {
828 for (i = 0; i < 6; i++) {
829 if (ISVOLT(i, data->uch_config)) {
ec5e1a4b 830 data->in[i] = vt8231_read_value(data,
1de9e371 831 regvolt[i]);
ec5e1a4b 832 data->in_min[i] = vt8231_read_value(data,
1de9e371 833 regvoltmin[i]);
ec5e1a4b 834 data->in_max[i] = vt8231_read_value(data,
1de9e371
RL
835 regvoltmax[i]);
836 }
837 }
838 for (i = 0; i < 2; i++) {
ec5e1a4b 839 data->fan[i] = vt8231_read_value(data,
1de9e371 840 VT8231_REG_FAN(i));
ec5e1a4b 841 data->fan_min[i] = vt8231_read_value(data,
1de9e371
RL
842 VT8231_REG_FAN_MIN(i));
843 }
844
ec5e1a4b 845 low = vt8231_read_value(data, VT8231_REG_TEMP_LOW01);
1de9e371 846 low = (low >> 6) | ((low & 0x30) >> 2)
ec5e1a4b 847 | (vt8231_read_value(data, VT8231_REG_TEMP_LOW25) << 4);
1de9e371
RL
848 for (i = 0; i < 6; i++) {
849 if (ISTEMP(i, data->uch_config)) {
ec5e1a4b 850 data->temp[i] = (vt8231_read_value(data,
1de9e371
RL
851 regtemp[i]) << 2)
852 | ((low >> (2 * i)) & 0x03);
ec5e1a4b 853 data->temp_max[i] = vt8231_read_value(data,
1de9e371 854 regtempmax[i]);
ec5e1a4b 855 data->temp_min[i] = vt8231_read_value(data,
1de9e371
RL
856 regtempmin[i]);
857 }
858 }
859
ec5e1a4b 860 i = vt8231_read_value(data, VT8231_REG_FANDIV);
1de9e371
RL
861 data->fan_div[0] = (i >> 4) & 0x03;
862 data->fan_div[1] = i >> 6;
ec5e1a4b
RL
863 data->alarms = vt8231_read_value(data, VT8231_REG_ALARM1) |
864 (vt8231_read_value(data, VT8231_REG_ALARM2) << 8);
1de9e371
RL
865
866 /* Set alarm flags correctly */
867 if (!data->fan[0] && data->fan_min[0]) {
868 data->alarms |= 0x40;
869 } else if (data->fan[0] && !data->fan_min[0]) {
870 data->alarms &= ~0x40;
871 }
872
873 if (!data->fan[1] && data->fan_min[1]) {
874 data->alarms |= 0x80;
875 } else if (data->fan[1] && !data->fan_min[1]) {
876 data->alarms &= ~0x80;
877 }
878
879 data->last_updated = jiffies;
880 data->valid = 1;
881 }
882
9a61bf63 883 mutex_unlock(&data->update_lock);
1de9e371
RL
884
885 return data;
886}
887
ec5e1a4b
RL
888static int __devinit vt8231_device_add(unsigned short address)
889{
890 struct resource res = {
891 .start = address,
892 .end = address + VT8231_EXTENT - 1,
893 .name = "vt8231",
894 .flags = IORESOURCE_IO,
895 };
896 int err;
897
b9acb64a
JD
898 err = acpi_check_resource_conflict(&res);
899 if (err)
900 goto exit;
901
ec5e1a4b
RL
902 pdev = platform_device_alloc("vt8231", address);
903 if (!pdev) {
904 err = -ENOMEM;
905 printk(KERN_ERR "vt8231: Device allocation failed\n");
906 goto exit;
907 }
908
909 err = platform_device_add_resources(pdev, &res, 1);
910 if (err) {
911 printk(KERN_ERR "vt8231: Device resource addition failed "
912 "(%d)\n", err);
913 goto exit_device_put;
914 }
915
916 err = platform_device_add(pdev);
917 if (err) {
918 printk(KERN_ERR "vt8231: Device addition failed (%d)\n",
919 err);
920 goto exit_device_put;
921 }
922
923 return 0;
924
925exit_device_put:
926 platform_device_put(pdev);
927exit:
928 return err;
929}
930
1de9e371
RL
931static int __devinit vt8231_pci_probe(struct pci_dev *dev,
932 const struct pci_device_id *id)
933{
ec5e1a4b
RL
934 u16 address, val;
935 if (force_addr) {
936 address = force_addr & 0xff00;
937 dev_warn(&dev->dev, "Forcing ISA address 0x%x\n",
938 address);
939
940 if (PCIBIOS_SUCCESSFUL !=
941 pci_write_config_word(dev, VT8231_BASE_REG, address | 1))
942 return -ENODEV;
943 }
1de9e371
RL
944
945 if (PCIBIOS_SUCCESSFUL != pci_read_config_word(dev, VT8231_BASE_REG,
946 &val))
947 return -ENODEV;
948
ec5e1a4b
RL
949 address = val & ~(VT8231_EXTENT - 1);
950 if (address == 0) {
4cae7878 951 dev_err(&dev->dev, "base address not set - upgrade BIOS or use force_addr=0xaddr\n");
1de9e371
RL
952 return -ENODEV;
953 }
954
ec5e1a4b
RL
955 if (PCIBIOS_SUCCESSFUL != pci_read_config_word(dev, VT8231_ENABLE_REG,
956 &val))
957 return -ENODEV;
1de9e371 958
ec5e1a4b
RL
959 if (!(val & 0x0001)) {
960 dev_warn(&dev->dev, "enabling sensors\n");
961 if (PCIBIOS_SUCCESSFUL !=
962 pci_write_config_word(dev, VT8231_ENABLE_REG,
963 val | 0x0001))
964 return -ENODEV;
1de9e371
RL
965 }
966
ec5e1a4b
RL
967 if (platform_driver_register(&vt8231_driver))
968 goto exit;
969
970 /* Sets global pdev as a side effect */
971 if (vt8231_device_add(address))
972 goto exit_unregister;
973
1de9e371
RL
974 /* Always return failure here. This is to allow other drivers to bind
975 * to this pci device. We don't really want to have control over the
976 * pci device, we only wanted to read as few register values from it.
977 */
ec5e1a4b
RL
978
979 /* We do, however, mark ourselves as using the PCI device to stop it
980 getting unloaded. */
981 s_bridge = pci_dev_get(dev);
982 return -ENODEV;
983
984exit_unregister:
985 platform_driver_unregister(&vt8231_driver);
986exit:
1de9e371
RL
987 return -ENODEV;
988}
989
990static int __init sm_vt8231_init(void)
991{
93b47684 992 return pci_register_driver(&vt8231_pci_driver);
1de9e371
RL
993}
994
995static void __exit sm_vt8231_exit(void)
996{
997 pci_unregister_driver(&vt8231_pci_driver);
998 if (s_bridge != NULL) {
ec5e1a4b
RL
999 platform_device_unregister(pdev);
1000 platform_driver_unregister(&vt8231_driver);
1de9e371
RL
1001 pci_dev_put(s_bridge);
1002 s_bridge = NULL;
1003 }
1004}
1005
af865765 1006MODULE_AUTHOR("Roger Lucas <vt8231@hiddenengine.co.uk>");
1de9e371
RL
1007MODULE_DESCRIPTION("VT8231 sensors");
1008MODULE_LICENSE("GPL");
1009
1010module_init(sm_vt8231_init);
1011module_exit(sm_vt8231_exit);