blob: 470a1226ba2b38bdd29721ee943ac373c093789a [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
Roger Lucasaf865762008-02-13 07:52:06 -05005 Copyright (c) 2005 Roger Lucas <vt8231@hiddenengine.co.uk>
Roger Lucas1de9e372005-11-26 20:20:05 +01006 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>
Roger Lucasec5e1a42007-06-12 21:04:08 +020032#include <linux/platform_device.h>
Roger Lucas1de9e372005-11-26 20:20:05 +010033#include <linux/hwmon.h>
34#include <linux/hwmon-sysfs.h>
35#include <linux/hwmon-vid.h>
36#include <linux/err.h>
Ingo Molnar9a61bf62006-01-18 23:19:26 +010037#include <linux/mutex.h>
Jean Delvareb9acb642009-01-07 16:37:35 +010038#include <linux/acpi.h>
H Hartley Sweeten6055fae2009-09-15 17:18:13 +020039#include <linux/io.h>
Roger Lucas1de9e372005-11-26 20:20:05 +010040
41static int force_addr;
42module_param(force_addr, int, 0);
43MODULE_PARM_DESC(force_addr, "Initialize the base address of the sensors");
44
Roger Lucasec5e1a42007-06-12 21:04:08 +020045static struct platform_device *pdev;
Roger Lucas1de9e372005-11-26 20:20:05 +010046
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 {
Roger Lucasec5e1a42007-06-12 21:04:08 +0200148 unsigned short addr;
149 const char *name;
150
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100151 struct mutex update_lock;
Tony Jones1beeffe2007-08-20 13:46:20 -0700152 struct device *hwmon_dev;
Roger Lucas1de9e372005-11-26 20:20:05 +0100153 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;
Roger Lucasec5e1a42007-06-12 21:04:08 +0200170static int vt8231_probe(struct platform_device *pdev);
Jean Delvared0546122007-07-22 12:09:48 +0200171static int __devexit vt8231_remove(struct platform_device *pdev);
Roger Lucas1de9e372005-11-26 20:20:05 +0100172static struct vt8231_data *vt8231_update_device(struct device *dev);
Roger Lucasec5e1a42007-06-12 21:04:08 +0200173static void vt8231_init_device(struct vt8231_data *data);
Roger Lucas1de9e372005-11-26 20:20:05 +0100174
Roger Lucasec5e1a42007-06-12 21:04:08 +0200175static inline int vt8231_read_value(struct vt8231_data *data, u8 reg)
Roger Lucas1de9e372005-11-26 20:20:05 +0100176{
Roger Lucasec5e1a42007-06-12 21:04:08 +0200177 return inb_p(data->addr + reg);
Roger Lucas1de9e372005-11-26 20:20:05 +0100178}
179
Roger Lucasec5e1a42007-06-12 21:04:08 +0200180static inline void vt8231_write_value(struct vt8231_data *data, u8 reg,
Roger Lucas1de9e372005-11-26 20:20:05 +0100181 u8 value)
182{
Roger Lucasec5e1a42007-06-12 21:04:08 +0200183 outb_p(value, data->addr + reg);
Roger Lucas1de9e372005-11-26 20:20:05 +0100184}
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;
Roger Lucasec5e1a42007-06-12 21:04:08 +0200222 struct vt8231_data *data = dev_get_drvdata(dev);
Roger Lucas1de9e372005-11-26 20:20:05 +0100223 unsigned long val = simple_strtoul(buf, NULL, 10);
224
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100225 mutex_lock(&data->update_lock);
Roger Lucas1de9e372005-11-26 20:20:05 +0100226 data->in_min[nr] = SENSORS_LIMIT(((val * 958) / 10000) + 3, 0, 255);
Roger Lucasec5e1a42007-06-12 21:04:08 +0200227 vt8231_write_value(data, regvoltmin[nr], data->in_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100228 mutex_unlock(&data->update_lock);
Roger Lucas1de9e372005-11-26 20:20:05 +0100229 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;
Roger Lucasec5e1a42007-06-12 21:04:08 +0200237 struct vt8231_data *data = dev_get_drvdata(dev);
Roger Lucas1de9e372005-11-26 20:20:05 +0100238 unsigned long val = simple_strtoul(buf, NULL, 10);
239
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100240 mutex_lock(&data->update_lock);
Roger Lucas1de9e372005-11-26 20:20:05 +0100241 data->in_max[nr] = SENSORS_LIMIT(((val * 958) / 10000) + 3, 0, 255);
Roger Lucasec5e1a42007-06-12 21:04:08 +0200242 vt8231_write_value(data, regvoltmax[nr], data->in_max[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100243 mutex_unlock(&data->update_lock);
Roger Lucas1de9e372005-11-26 20:20:05 +0100244 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{
Roger Lucasec5e1a42007-06-12 21:04:08 +0200278 struct vt8231_data *data = dev_get_drvdata(dev);
Roger Lucas1de9e372005-11-26 20:20:05 +0100279 unsigned long val = simple_strtoul(buf, NULL, 10);
280
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100281 mutex_lock(&data->update_lock);
Roger Lucas1de9e372005-11-26 20:20:05 +0100282 data->in_min[5] = SENSORS_LIMIT(((val * 958 * 34) / (10000 * 54)) + 3,
283 0, 255);
Roger Lucasec5e1a42007-06-12 21:04:08 +0200284 vt8231_write_value(data, regvoltmin[5], data->in_min[5]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100285 mutex_unlock(&data->update_lock);
Roger Lucas1de9e372005-11-26 20:20:05 +0100286 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{
Roger Lucasec5e1a42007-06-12 21:04:08 +0200292 struct vt8231_data *data = dev_get_drvdata(dev);
Roger Lucas1de9e372005-11-26 20:20:05 +0100293 unsigned long val = simple_strtoul(buf, NULL, 10);
294
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100295 mutex_lock(&data->update_lock);
Roger Lucas1de9e372005-11-26 20:20:05 +0100296 data->in_max[5] = SENSORS_LIMIT(((val * 958 * 34) / (10000 * 54)) + 3,
297 0, 255);
Roger Lucasec5e1a42007-06-12 21:04:08 +0200298 vt8231_write_value(data, regvoltmax[5], data->in_max[5]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100299 mutex_unlock(&data->update_lock);
Roger Lucas1de9e372005-11-26 20:20:05 +0100300 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{
Roger Lucasec5e1a42007-06-12 21:04:08 +0200346 struct vt8231_data *data = dev_get_drvdata(dev);
Roger Lucas1de9e372005-11-26 20:20:05 +0100347 int val = simple_strtol(buf, NULL, 10);
348
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100349 mutex_lock(&data->update_lock);
Roger Lucas1de9e372005-11-26 20:20:05 +0100350 data->temp_max[0] = SENSORS_LIMIT((val + 500) / 1000, 0, 255);
Roger Lucasec5e1a42007-06-12 21:04:08 +0200351 vt8231_write_value(data, regtempmax[0], data->temp_max[0]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100352 mutex_unlock(&data->update_lock);
Roger Lucas1de9e372005-11-26 20:20:05 +0100353 return count;
354}
355static ssize_t set_temp0_min(struct device *dev, struct device_attribute *attr,
356 const char *buf, size_t count)
357{
Roger Lucasec5e1a42007-06-12 21:04:08 +0200358 struct vt8231_data *data = dev_get_drvdata(dev);
Roger Lucas1de9e372005-11-26 20:20:05 +0100359 int val = simple_strtol(buf, NULL, 10);
360
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100361 mutex_lock(&data->update_lock);
Roger Lucas1de9e372005-11-26 20:20:05 +0100362 data->temp_min[0] = SENSORS_LIMIT((val + 500) / 1000, 0, 255);
Roger Lucasec5e1a42007-06-12 21:04:08 +0200363 vt8231_write_value(data, regtempmin[0], data->temp_min[0]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100364 mutex_unlock(&data->update_lock);
Roger Lucas1de9e372005-11-26 20:20:05 +0100365 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;
Roger Lucasec5e1a42007-06-12 21:04:08 +0200400 struct vt8231_data *data = dev_get_drvdata(dev);
Roger Lucas1de9e372005-11-26 20:20:05 +0100401 int val = simple_strtol(buf, NULL, 10);
402
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100403 mutex_lock(&data->update_lock);
Roger Lucas1de9e372005-11-26 20:20:05 +0100404 data->temp_max[nr] = SENSORS_LIMIT(TEMP_MAXMIN_TO_REG(val), 0, 255);
Roger Lucasec5e1a42007-06-12 21:04:08 +0200405 vt8231_write_value(data, regtempmax[nr], data->temp_max[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100406 mutex_unlock(&data->update_lock);
Roger Lucas1de9e372005-11-26 20:20:05 +0100407 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;
Roger Lucasec5e1a42007-06-12 21:04:08 +0200414 struct vt8231_data *data = dev_get_drvdata(dev);
Roger Lucas1de9e372005-11-26 20:20:05 +0100415 int val = simple_strtol(buf, NULL, 10);
416
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100417 mutex_lock(&data->update_lock);
Roger Lucas1de9e372005-11-26 20:20:05 +0100418 data->temp_min[nr] = SENSORS_LIMIT(TEMP_MAXMIN_TO_REG(val), 0, 255);
Roger Lucasec5e1a42007-06-12 21:04:08 +0200419 vt8231_write_value(data, regtempmin[nr], data->temp_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100420 mutex_unlock(&data->update_lock);
Roger Lucas1de9e372005-11-26 20:20:05 +0100421 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); \
Jean Delvaree3efa5a2006-02-05 23:11:16 +0100432static SENSOR_DEVICE_ATTR(temp##offset##_max_hyst, S_IRUGO | S_IWUSR, \
Roger Lucas1de9e372005-11-26 20:20:05 +0100433 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);
Jean Delvaree3efa5a2006-02-05 23:11:16 +0100437static DEVICE_ATTR(temp1_max_hyst, S_IRUGO | S_IWUSR, show_temp0_min, set_temp0_min);
Roger Lucas1de9e372005-11-26 20:20:05 +0100438
439define_temperature_sysfs(2);
440define_temperature_sysfs(3);
441define_temperature_sysfs(4);
442define_temperature_sysfs(5);
443define_temperature_sysfs(6);
444
Roger Lucas1de9e372005-11-26 20:20:05 +0100445/* 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;
Roger Lucasec5e1a42007-06-12 21:04:08 +0200480 struct vt8231_data *data = dev_get_drvdata(dev);
Roger Lucas1de9e372005-11-26 20:20:05 +0100481 int val = simple_strtoul(buf, NULL, 10);
482
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100483 mutex_lock(&data->update_lock);
Roger Lucas1de9e372005-11-26 20:20:05 +0100484 data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
Roger Lucasec5e1a42007-06-12 21:04:08 +0200485 vt8231_write_value(data, VT8231_REG_FAN_MIN(nr), data->fan_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100486 mutex_unlock(&data->update_lock);
Roger Lucas1de9e372005-11-26 20:20:05 +0100487 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{
Roger Lucasec5e1a42007-06-12 21:04:08 +0200493 struct vt8231_data *data = dev_get_drvdata(dev);
Roger Lucas1de9e372005-11-26 20:20:05 +0100494 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;
Roger Lucasec5e1a42007-06-12 21:04:08 +0200497 int old = vt8231_read_value(data, VT8231_REG_FANDIV);
Roger Lucas1de9e372005-11-26 20:20:05 +0100498 long min = FAN_FROM_REG(data->fan_min[nr],
499 DIV_FROM_REG(data->fan_div[nr]));
500
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100501 mutex_lock(&data->update_lock);
Roger Lucas1de9e372005-11-26 20:20:05 +0100502 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:
Joe Perchesb20ff132007-11-19 17:48:07 -0800508 dev_err(dev, "fan_div value %ld not supported. "
Roger Lucas1de9e372005-11-26 20:20:05 +0100509 "Choose one of 1, 2, 4 or 8!\n", val);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100510 mutex_unlock(&data->update_lock);
Roger Lucas1de9e372005-11-26 20:20:05 +0100511 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]));
Roger Lucasec5e1a42007-06-12 21:04:08 +0200516 vt8231_write_value(data, VT8231_REG_FAN_MIN(nr), data->fan_min[nr]);
Roger Lucas1de9e372005-11-26 20:20:05 +0100517
518 old = (old & 0x0f) | (data->fan_div[1] << 6) | (data->fan_div[0] << 4);
Roger Lucasec5e1a42007-06-12 21:04:08 +0200519 vt8231_write_value(data, VT8231_REG_FANDIV, old);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100520 mutex_unlock(&data->update_lock);
Roger Lucas1de9e372005-11-26 20:20:05 +0100521 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}
Roger Lucas1de9e372005-11-26 20:20:05 +0100543static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
544
Jean Delvare2d1374c2008-01-06 15:46:02 +0100545static 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
Roger Lucasec5e1a42007-06-12 21:04:08 +0200567static 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
Jean Delvare2d1374c2008-01-06 15:46:02 +0100575static struct attribute *vt8231_attributes_temps[6][5] = {
Roger Lucascbeeb5b2006-09-24 21:21:46 +0200576 {
577 &dev_attr_temp1_input.attr,
578 &dev_attr_temp1_max_hyst.attr,
579 &dev_attr_temp1_max.attr,
Jean Delvare2d1374c2008-01-06 15:46:02 +0100580 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
Roger Lucascbeeb5b2006-09-24 21:21:46 +0200581 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,
Jean Delvare2d1374c2008-01-06 15:46:02 +0100586 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
Roger Lucascbeeb5b2006-09-24 21:21:46 +0200587 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,
Jean Delvare2d1374c2008-01-06 15:46:02 +0100592 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
Roger Lucascbeeb5b2006-09-24 21:21:46 +0200593 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,
Jean Delvare2d1374c2008-01-06 15:46:02 +0100598 &sensor_dev_attr_temp4_alarm.dev_attr.attr,
Roger Lucascbeeb5b2006-09-24 21:21:46 +0200599 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,
Jean Delvare2d1374c2008-01-06 15:46:02 +0100604 &sensor_dev_attr_temp5_alarm.dev_attr.attr,
Roger Lucascbeeb5b2006-09-24 21:21:46 +0200605 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,
Jean Delvare2d1374c2008-01-06 15:46:02 +0100610 &sensor_dev_attr_temp6_alarm.dev_attr.attr,
Roger Lucascbeeb5b2006-09-24 21:21:46 +0200611 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
Jean Delvare2d1374c2008-01-06 15:46:02 +0100624static struct attribute *vt8231_attributes_volts[6][5] = {
Roger Lucascbeeb5b2006-09-24 21:21:46 +0200625 {
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,
Jean Delvare2d1374c2008-01-06 15:46:02 +0100629 &sensor_dev_attr_in0_alarm.dev_attr.attr,
Roger Lucascbeeb5b2006-09-24 21:21:46 +0200630 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,
Jean Delvare2d1374c2008-01-06 15:46:02 +0100635 &sensor_dev_attr_in1_alarm.dev_attr.attr,
Roger Lucascbeeb5b2006-09-24 21:21:46 +0200636 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,
Jean Delvare2d1374c2008-01-06 15:46:02 +0100641 &sensor_dev_attr_in2_alarm.dev_attr.attr,
Roger Lucascbeeb5b2006-09-24 21:21:46 +0200642 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,
Jean Delvare2d1374c2008-01-06 15:46:02 +0100647 &sensor_dev_attr_in3_alarm.dev_attr.attr,
Roger Lucascbeeb5b2006-09-24 21:21:46 +0200648 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,
Jean Delvare2d1374c2008-01-06 15:46:02 +0100653 &sensor_dev_attr_in4_alarm.dev_attr.attr,
Roger Lucascbeeb5b2006-09-24 21:21:46 +0200654 NULL
655 }, {
656 &dev_attr_in5_input.attr,
657 &dev_attr_in5_min.attr,
658 &dev_attr_in5_max.attr,
Jean Delvare2d1374c2008-01-06 15:46:02 +0100659 &sensor_dev_attr_in5_alarm.dev_attr.attr,
Roger Lucascbeeb5b2006-09-24 21:21:46 +0200660 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,
Jean Delvare2d1374c2008-01-06 15:46:02 +0100680 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
681 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
Roger Lucascbeeb5b2006-09-24 21:21:46 +0200682 &dev_attr_alarms.attr,
Roger Lucasec5e1a42007-06-12 21:04:08 +0200683 &dev_attr_name.attr,
Roger Lucascbeeb5b2006-09-24 21:21:46 +0200684 NULL
685};
686
687static const struct attribute_group vt8231_group = {
688 .attrs = vt8231_attributes,
689};
690
Roger Lucasec5e1a42007-06-12 21:04:08 +0200691static struct platform_driver vt8231_driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100692 .driver = {
Jean Delvare87218842006-09-03 22:36:14 +0200693 .owner = THIS_MODULE,
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100694 .name = "vt8231",
695 },
Roger Lucasec5e1a42007-06-12 21:04:08 +0200696 .probe = vt8231_probe,
697 .remove = __devexit_p(vt8231_remove),
Roger Lucas1de9e372005-11-26 20:20:05 +0100698};
699
700static struct pci_device_id vt8231_pci_ids[] = {
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
Mark M. Hoffmana022fef2007-10-14 15:00:24 -0400716static int vt8231_probe(struct platform_device *pdev)
Roger Lucas1de9e372005-11-26 20:20:05 +0100717{
Roger Lucasec5e1a42007-06-12 21:04:08 +0200718 struct resource *res;
Roger Lucas1de9e372005-11-26 20:20:05 +0100719 struct vt8231_data *data;
720 int err = 0, i;
Roger Lucas1de9e372005-11-26 20:20:05 +0100721
722 /* Reserve the ISA region */
Roger Lucasec5e1a42007-06-12 21:04:08 +0200723 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);
Roger Lucas1de9e372005-11-26 20:20:05 +0100728 return -ENODEV;
729 }
730
731 if (!(data = kzalloc(sizeof(struct vt8231_data), GFP_KERNEL))) {
732 err = -ENOMEM;
733 goto exit_release;
734 }
735
Roger Lucasec5e1a42007-06-12 21:04:08 +0200736 platform_set_drvdata(pdev, data);
737 data->addr = res->start;
738 data->name = "vt8231";
Roger Lucas1de9e372005-11-26 20:20:05 +0100739
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100740 mutex_init(&data->update_lock);
Roger Lucasec5e1a42007-06-12 21:04:08 +0200741 vt8231_init_device(data);
Roger Lucas1de9e372005-11-26 20:20:05 +0100742
743 /* Register sysfs hooks */
Roger Lucasec5e1a42007-06-12 21:04:08 +0200744 if ((err = sysfs_create_group(&pdev->dev.kobj, &vt8231_group)))
745 goto exit_free;
Roger Lucas1de9e372005-11-26 20:20:05 +0100746
747 /* Must update device information to find out the config field */
Roger Lucasec5e1a42007-06-12 21:04:08 +0200748 data->uch_config = vt8231_read_value(data, VT8231_REG_UCH_CONFIG);
Roger Lucas1de9e372005-11-26 20:20:05 +0100749
Roger Lucascbeeb5b2006-09-24 21:21:46 +0200750 for (i = 0; i < ARRAY_SIZE(vt8231_group_temps); i++) {
Roger Lucas1de9e372005-11-26 20:20:05 +0100751 if (ISTEMP(i, data->uch_config)) {
Roger Lucasec5e1a42007-06-12 21:04:08 +0200752 if ((err = sysfs_create_group(&pdev->dev.kobj,
Roger Lucascbeeb5b2006-09-24 21:21:46 +0200753 &vt8231_group_temps[i])))
754 goto exit_remove_files;
Roger Lucas1de9e372005-11-26 20:20:05 +0100755 }
756 }
757
Roger Lucascbeeb5b2006-09-24 21:21:46 +0200758 for (i = 0; i < ARRAY_SIZE(vt8231_group_volts); i++) {
Roger Lucas1de9e372005-11-26 20:20:05 +0100759 if (ISVOLT(i, data->uch_config)) {
Roger Lucasec5e1a42007-06-12 21:04:08 +0200760 if ((err = sysfs_create_group(&pdev->dev.kobj,
Roger Lucascbeeb5b2006-09-24 21:21:46 +0200761 &vt8231_group_volts[i])))
762 goto exit_remove_files;
Roger Lucas1de9e372005-11-26 20:20:05 +0100763 }
764 }
765
Tony Jones1beeffe2007-08-20 13:46:20 -0700766 data->hwmon_dev = hwmon_device_register(&pdev->dev);
767 if (IS_ERR(data->hwmon_dev)) {
768 err = PTR_ERR(data->hwmon_dev);
Roger Lucascbeeb5b2006-09-24 21:21:46 +0200769 goto exit_remove_files;
770 }
Roger Lucas1de9e372005-11-26 20:20:05 +0100771 return 0;
772
Roger Lucascbeeb5b2006-09-24 21:21:46 +0200773exit_remove_files:
774 for (i = 0; i < ARRAY_SIZE(vt8231_group_volts); i++)
Roger Lucasec5e1a42007-06-12 21:04:08 +0200775 sysfs_remove_group(&pdev->dev.kobj, &vt8231_group_volts[i]);
Roger Lucascbeeb5b2006-09-24 21:21:46 +0200776
777 for (i = 0; i < ARRAY_SIZE(vt8231_group_temps); i++)
Roger Lucasec5e1a42007-06-12 21:04:08 +0200778 sysfs_remove_group(&pdev->dev.kobj, &vt8231_group_temps[i]);
Roger Lucascbeeb5b2006-09-24 21:21:46 +0200779
Roger Lucasec5e1a42007-06-12 21:04:08 +0200780 sysfs_remove_group(&pdev->dev.kobj, &vt8231_group);
781
Roger Lucas1de9e372005-11-26 20:20:05 +0100782exit_free:
Jean Delvare04a62172007-06-12 13:57:19 +0200783 platform_set_drvdata(pdev, NULL);
Roger Lucas1de9e372005-11-26 20:20:05 +0100784 kfree(data);
Roger Lucasec5e1a42007-06-12 21:04:08 +0200785
Roger Lucas1de9e372005-11-26 20:20:05 +0100786exit_release:
Roger Lucasec5e1a42007-06-12 21:04:08 +0200787 release_region(res->start, VT8231_EXTENT);
Roger Lucas1de9e372005-11-26 20:20:05 +0100788 return err;
789}
790
Jean Delvared0546122007-07-22 12:09:48 +0200791static int __devexit vt8231_remove(struct platform_device *pdev)
Roger Lucas1de9e372005-11-26 20:20:05 +0100792{
Roger Lucasec5e1a42007-06-12 21:04:08 +0200793 struct vt8231_data *data = platform_get_drvdata(pdev);
794 int i;
Roger Lucas1de9e372005-11-26 20:20:05 +0100795
Tony Jones1beeffe2007-08-20 13:46:20 -0700796 hwmon_device_unregister(data->hwmon_dev);
Roger Lucas1de9e372005-11-26 20:20:05 +0100797
Roger Lucascbeeb5b2006-09-24 21:21:46 +0200798 for (i = 0; i < ARRAY_SIZE(vt8231_group_volts); i++)
Roger Lucasec5e1a42007-06-12 21:04:08 +0200799 sysfs_remove_group(&pdev->dev.kobj, &vt8231_group_volts[i]);
Roger Lucascbeeb5b2006-09-24 21:21:46 +0200800
801 for (i = 0; i < ARRAY_SIZE(vt8231_group_temps); i++)
Roger Lucasec5e1a42007-06-12 21:04:08 +0200802 sysfs_remove_group(&pdev->dev.kobj, &vt8231_group_temps[i]);
Roger Lucascbeeb5b2006-09-24 21:21:46 +0200803
Roger Lucasec5e1a42007-06-12 21:04:08 +0200804 sysfs_remove_group(&pdev->dev.kobj, &vt8231_group);
Roger Lucascbeeb5b2006-09-24 21:21:46 +0200805
Roger Lucasec5e1a42007-06-12 21:04:08 +0200806 release_region(data->addr, VT8231_EXTENT);
807 platform_set_drvdata(pdev, NULL);
Roger Lucas1de9e372005-11-26 20:20:05 +0100808 kfree(data);
Roger Lucas1de9e372005-11-26 20:20:05 +0100809 return 0;
810}
811
Roger Lucasec5e1a42007-06-12 21:04:08 +0200812static void vt8231_init_device(struct vt8231_data *data)
Roger Lucas1de9e372005-11-26 20:20:05 +0100813{
Roger Lucasec5e1a42007-06-12 21:04:08 +0200814 vt8231_write_value(data, VT8231_REG_TEMP1_CONFIG, 0);
815 vt8231_write_value(data, VT8231_REG_TEMP2_CONFIG, 0);
Roger Lucas1de9e372005-11-26 20:20:05 +0100816}
817
818static struct vt8231_data *vt8231_update_device(struct device *dev)
819{
Roger Lucasec5e1a42007-06-12 21:04:08 +0200820 struct vt8231_data *data = dev_get_drvdata(dev);
Roger Lucas1de9e372005-11-26 20:20:05 +0100821 int i;
822 u16 low;
823
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100824 mutex_lock(&data->update_lock);
Roger Lucas1de9e372005-11-26 20:20:05 +0100825
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)) {
Roger Lucasec5e1a42007-06-12 21:04:08 +0200830 data->in[i] = vt8231_read_value(data,
Roger Lucas1de9e372005-11-26 20:20:05 +0100831 regvolt[i]);
Roger Lucasec5e1a42007-06-12 21:04:08 +0200832 data->in_min[i] = vt8231_read_value(data,
Roger Lucas1de9e372005-11-26 20:20:05 +0100833 regvoltmin[i]);
Roger Lucasec5e1a42007-06-12 21:04:08 +0200834 data->in_max[i] = vt8231_read_value(data,
Roger Lucas1de9e372005-11-26 20:20:05 +0100835 regvoltmax[i]);
836 }
837 }
838 for (i = 0; i < 2; i++) {
Roger Lucasec5e1a42007-06-12 21:04:08 +0200839 data->fan[i] = vt8231_read_value(data,
Roger Lucas1de9e372005-11-26 20:20:05 +0100840 VT8231_REG_FAN(i));
Roger Lucasec5e1a42007-06-12 21:04:08 +0200841 data->fan_min[i] = vt8231_read_value(data,
Roger Lucas1de9e372005-11-26 20:20:05 +0100842 VT8231_REG_FAN_MIN(i));
843 }
844
Roger Lucasec5e1a42007-06-12 21:04:08 +0200845 low = vt8231_read_value(data, VT8231_REG_TEMP_LOW01);
Roger Lucas1de9e372005-11-26 20:20:05 +0100846 low = (low >> 6) | ((low & 0x30) >> 2)
Roger Lucasec5e1a42007-06-12 21:04:08 +0200847 | (vt8231_read_value(data, VT8231_REG_TEMP_LOW25) << 4);
Roger Lucas1de9e372005-11-26 20:20:05 +0100848 for (i = 0; i < 6; i++) {
849 if (ISTEMP(i, data->uch_config)) {
Roger Lucasec5e1a42007-06-12 21:04:08 +0200850 data->temp[i] = (vt8231_read_value(data,
Roger Lucas1de9e372005-11-26 20:20:05 +0100851 regtemp[i]) << 2)
852 | ((low >> (2 * i)) & 0x03);
Roger Lucasec5e1a42007-06-12 21:04:08 +0200853 data->temp_max[i] = vt8231_read_value(data,
Roger Lucas1de9e372005-11-26 20:20:05 +0100854 regtempmax[i]);
Roger Lucasec5e1a42007-06-12 21:04:08 +0200855 data->temp_min[i] = vt8231_read_value(data,
Roger Lucas1de9e372005-11-26 20:20:05 +0100856 regtempmin[i]);
857 }
858 }
859
Roger Lucasec5e1a42007-06-12 21:04:08 +0200860 i = vt8231_read_value(data, VT8231_REG_FANDIV);
Roger Lucas1de9e372005-11-26 20:20:05 +0100861 data->fan_div[0] = (i >> 4) & 0x03;
862 data->fan_div[1] = i >> 6;
Roger Lucasec5e1a42007-06-12 21:04:08 +0200863 data->alarms = vt8231_read_value(data, VT8231_REG_ALARM1) |
864 (vt8231_read_value(data, VT8231_REG_ALARM2) << 8);
Roger Lucas1de9e372005-11-26 20:20:05 +0100865
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
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100883 mutex_unlock(&data->update_lock);
Roger Lucas1de9e372005-11-26 20:20:05 +0100884
885 return data;
886}
887
Roger Lucasec5e1a42007-06-12 21:04:08 +0200888static 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
Jean Delvareb9acb642009-01-07 16:37:35 +0100898 err = acpi_check_resource_conflict(&res);
899 if (err)
900 goto exit;
901
Roger Lucasec5e1a42007-06-12 21:04:08 +0200902 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
Roger Lucas1de9e372005-11-26 20:20:05 +0100931static int __devinit vt8231_pci_probe(struct pci_dev *dev,
932 const struct pci_device_id *id)
933{
Roger Lucasec5e1a42007-06-12 21:04:08 +0200934 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 }
Roger Lucas1de9e372005-11-26 20:20:05 +0100944
945 if (PCIBIOS_SUCCESSFUL != pci_read_config_word(dev, VT8231_BASE_REG,
946 &val))
947 return -ENODEV;
948
Roger Lucasec5e1a42007-06-12 21:04:08 +0200949 address = val & ~(VT8231_EXTENT - 1);
950 if (address == 0) {
Roger Lucas1de9e372005-11-26 20:20:05 +0100951 dev_err(&dev->dev, "base address not set -\
952 upgrade BIOS or use force_addr=0xaddr\n");
953 return -ENODEV;
954 }
955
Roger Lucasec5e1a42007-06-12 21:04:08 +0200956 if (PCIBIOS_SUCCESSFUL != pci_read_config_word(dev, VT8231_ENABLE_REG,
957 &val))
958 return -ENODEV;
Roger Lucas1de9e372005-11-26 20:20:05 +0100959
Roger Lucasec5e1a42007-06-12 21:04:08 +0200960 if (!(val & 0x0001)) {
961 dev_warn(&dev->dev, "enabling sensors\n");
962 if (PCIBIOS_SUCCESSFUL !=
963 pci_write_config_word(dev, VT8231_ENABLE_REG,
964 val | 0x0001))
965 return -ENODEV;
Roger Lucas1de9e372005-11-26 20:20:05 +0100966 }
967
Roger Lucasec5e1a42007-06-12 21:04:08 +0200968 if (platform_driver_register(&vt8231_driver))
969 goto exit;
970
971 /* Sets global pdev as a side effect */
972 if (vt8231_device_add(address))
973 goto exit_unregister;
974
Roger Lucas1de9e372005-11-26 20:20:05 +0100975 /* Always return failure here. This is to allow other drivers to bind
976 * to this pci device. We don't really want to have control over the
977 * pci device, we only wanted to read as few register values from it.
978 */
Roger Lucasec5e1a42007-06-12 21:04:08 +0200979
980 /* We do, however, mark ourselves as using the PCI device to stop it
981 getting unloaded. */
982 s_bridge = pci_dev_get(dev);
983 return -ENODEV;
984
985exit_unregister:
986 platform_driver_unregister(&vt8231_driver);
987exit:
Roger Lucas1de9e372005-11-26 20:20:05 +0100988 return -ENODEV;
989}
990
991static int __init sm_vt8231_init(void)
992{
Richard Knutsson93b47682005-11-30 01:00:35 +0100993 return pci_register_driver(&vt8231_pci_driver);
Roger Lucas1de9e372005-11-26 20:20:05 +0100994}
995
996static void __exit sm_vt8231_exit(void)
997{
998 pci_unregister_driver(&vt8231_pci_driver);
999 if (s_bridge != NULL) {
Roger Lucasec5e1a42007-06-12 21:04:08 +02001000 platform_device_unregister(pdev);
1001 platform_driver_unregister(&vt8231_driver);
Roger Lucas1de9e372005-11-26 20:20:05 +01001002 pci_dev_put(s_bridge);
1003 s_bridge = NULL;
1004 }
1005}
1006
Roger Lucasaf865762008-02-13 07:52:06 -05001007MODULE_AUTHOR("Roger Lucas <vt8231@hiddenengine.co.uk>");
Roger Lucas1de9e372005-11-26 20:20:05 +01001008MODULE_DESCRIPTION("VT8231 sensors");
1009MODULE_LICENSE("GPL");
1010
1011module_init(sm_vt8231_init);
1012module_exit(sm_vt8231_exit);