blob: eb74f3778c9068538f19ef500d9126a403f4e1e6 [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
Joe Perches9d72be02010-10-20 06:51:53 +000027#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
28
Roger Lucas1de9e372005-11-26 20:20:05 +010029#include <linux/module.h>
30#include <linux/init.h>
31#include <linux/slab.h>
32#include <linux/pci.h>
33#include <linux/jiffies.h>
Roger Lucasec5e1a42007-06-12 21:04:08 +020034#include <linux/platform_device.h>
Roger Lucas1de9e372005-11-26 20:20:05 +010035#include <linux/hwmon.h>
36#include <linux/hwmon-sysfs.h>
37#include <linux/hwmon-vid.h>
38#include <linux/err.h>
Ingo Molnar9a61bf62006-01-18 23:19:26 +010039#include <linux/mutex.h>
Jean Delvareb9acb642009-01-07 16:37:35 +010040#include <linux/acpi.h>
H Hartley Sweeten6055fae2009-09-15 17:18:13 +020041#include <linux/io.h>
Roger Lucas1de9e372005-11-26 20:20:05 +010042
43static int force_addr;
44module_param(force_addr, int, 0);
45MODULE_PARM_DESC(force_addr, "Initialize the base address of the sensors");
46
Roger Lucasec5e1a42007-06-12 21:04:08 +020047static struct platform_device *pdev;
Roger Lucas1de9e372005-11-26 20:20:05 +010048
49#define VT8231_EXTENT 0x80
50#define VT8231_BASE_REG 0x70
51#define VT8231_ENABLE_REG 0x74
52
53/* The VT8231 registers
54
55 The reset value for the input channel configuration is used (Reg 0x4A=0x07)
56 which sets the selected inputs marked with '*' below if multiple options are
57 possible:
58
Guenter Roeck65fe5c72012-01-15 07:03:38 -080059 Voltage Mode Temperature Mode
60 Sensor Linux Id Linux Id VIA Id
61 -------- -------- -------- ------
Roger Lucas1de9e372005-11-26 20:20:05 +010062 CPU Diode N/A temp1 0
63 UIC1 in0 temp2 * 1
Guenter Roeck65fe5c72012-01-15 07:03:38 -080064 UIC2 in1 * temp3 2
Roger Lucas1de9e372005-11-26 20:20:05 +010065 UIC3 in2 * temp4 3
66 UIC4 in3 * temp5 4
67 UIC5 in4 * temp6 5
68 3.3V in5 N/A
69
70 Note that the BIOS may set the configuration register to a different value
71 to match the motherboard configuration.
72*/
73
74/* fans numbered 0-1 */
75#define VT8231_REG_FAN_MIN(nr) (0x3b + (nr))
76#define VT8231_REG_FAN(nr) (0x29 + (nr))
77
78/* Voltage inputs numbered 0-5 */
79
80static const u8 regvolt[] = { 0x21, 0x22, 0x23, 0x24, 0x25, 0x26 };
81static const u8 regvoltmax[] = { 0x3d, 0x2b, 0x2d, 0x2f, 0x31, 0x33 };
82static const u8 regvoltmin[] = { 0x3e, 0x2c, 0x2e, 0x30, 0x32, 0x34 };
83
84/* Temperatures are numbered 1-6 according to the Linux kernel specification.
85**
86** In the VIA datasheet, however, the temperatures are numbered from zero.
87** Since it is important that this driver can easily be compared to the VIA
88** datasheet, we will use the VIA numbering within this driver and map the
89** kernel sysfs device name to the VIA number in the sysfs callback.
90*/
91
92#define VT8231_REG_TEMP_LOW01 0x49
93#define VT8231_REG_TEMP_LOW25 0x4d
94
95static const u8 regtemp[] = { 0x1f, 0x21, 0x22, 0x23, 0x24, 0x25 };
96static const u8 regtempmax[] = { 0x39, 0x3d, 0x2b, 0x2d, 0x2f, 0x31 };
97static const u8 regtempmin[] = { 0x3a, 0x3e, 0x2c, 0x2e, 0x30, 0x32 };
98
99#define TEMP_FROM_REG(reg) (((253 * 4 - (reg)) * 550 + 105) / 210)
100#define TEMP_MAXMIN_FROM_REG(reg) (((253 - (reg)) * 2200 + 105) / 210)
101#define TEMP_MAXMIN_TO_REG(val) (253 - ((val) * 210 + 1100) / 2200)
102
103#define VT8231_REG_CONFIG 0x40
104#define VT8231_REG_ALARM1 0x41
105#define VT8231_REG_ALARM2 0x42
106#define VT8231_REG_FANDIV 0x47
107#define VT8231_REG_UCH_CONFIG 0x4a
108#define VT8231_REG_TEMP1_CONFIG 0x4b
109#define VT8231_REG_TEMP2_CONFIG 0x4c
110
111/* temps 0-5 as numbered in VIA datasheet - see later for mapping to Linux
112** numbering
113*/
114#define ISTEMP(i, ch_config) ((i) == 0 ? 1 : \
115 ((ch_config) >> ((i)+1)) & 0x01)
116/* voltages 0-5 */
117#define ISVOLT(i, ch_config) ((i) == 5 ? 1 : \
118 !(((ch_config) >> ((i)+2)) & 0x01))
119
120#define DIV_FROM_REG(val) (1 << (val))
121
122/* NB The values returned here are NOT temperatures. The calibration curves
123** for the thermistor curves are board-specific and must go in the
124** sensors.conf file. Temperature sensors are actually ten bits, but the
125** VIA datasheet only considers the 8 MSBs obtained from the regtemp[]
126** register. The temperature value returned should have a magnitude of 3,
127** so we use the VIA scaling as the "true" scaling and use the remaining 2
128** LSBs as fractional precision.
129**
130** All the on-chip hardware temperature comparisons for the alarms are only
131** 8-bits wide, and compare against the 8 MSBs of the temperature. The bits
132** in the registers VT8231_REG_TEMP_LOW01 and VT8231_REG_TEMP_LOW25 are
133** ignored.
134*/
135
136/******** FAN RPM CONVERSIONS ********
137** This chip saturates back at 0, not at 255 like many the other chips.
138** So, 0 means 0 RPM
139*/
140static inline u8 FAN_TO_REG(long rpm, int div)
141{
142 if (rpm == 0)
143 return 0;
144 return SENSORS_LIMIT(1310720 / (rpm * div), 1, 255);
145}
146
147#define FAN_FROM_REG(val, div) ((val) == 0 ? 0 : 1310720 / ((val) * (div)))
148
149struct vt8231_data {
Roger Lucasec5e1a42007-06-12 21:04:08 +0200150 unsigned short addr;
151 const char *name;
152
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100153 struct mutex update_lock;
Tony Jones1beeffe2007-08-20 13:46:20 -0700154 struct device *hwmon_dev;
Roger Lucas1de9e372005-11-26 20:20:05 +0100155 char valid; /* !=0 if following fields are valid */
156 unsigned long last_updated; /* In jiffies */
157
158 u8 in[6]; /* Register value */
159 u8 in_max[6]; /* Register value */
160 u8 in_min[6]; /* Register value */
161 u16 temp[6]; /* Register value 10 bit, right aligned */
162 u8 temp_max[6]; /* Register value */
163 u8 temp_min[6]; /* Register value */
164 u8 fan[2]; /* Register value */
165 u8 fan_min[2]; /* Register value */
166 u8 fan_div[2]; /* Register encoding, shifted right */
167 u16 alarms; /* Register encoding */
168 u8 uch_config;
169};
170
171static struct pci_dev *s_bridge;
Roger Lucasec5e1a42007-06-12 21:04:08 +0200172static int vt8231_probe(struct platform_device *pdev);
Jean Delvared0546122007-07-22 12:09:48 +0200173static int __devexit vt8231_remove(struct platform_device *pdev);
Roger Lucas1de9e372005-11-26 20:20:05 +0100174static struct vt8231_data *vt8231_update_device(struct device *dev);
Roger Lucasec5e1a42007-06-12 21:04:08 +0200175static void vt8231_init_device(struct vt8231_data *data);
Roger Lucas1de9e372005-11-26 20:20:05 +0100176
Roger Lucasec5e1a42007-06-12 21:04:08 +0200177static inline int vt8231_read_value(struct vt8231_data *data, u8 reg)
Roger Lucas1de9e372005-11-26 20:20:05 +0100178{
Roger Lucasec5e1a42007-06-12 21:04:08 +0200179 return inb_p(data->addr + reg);
Roger Lucas1de9e372005-11-26 20:20:05 +0100180}
181
Roger Lucasec5e1a42007-06-12 21:04:08 +0200182static inline void vt8231_write_value(struct vt8231_data *data, u8 reg,
Roger Lucas1de9e372005-11-26 20:20:05 +0100183 u8 value)
184{
Roger Lucasec5e1a42007-06-12 21:04:08 +0200185 outb_p(value, data->addr + reg);
Roger Lucas1de9e372005-11-26 20:20:05 +0100186}
187
188/* following are the sysfs callback functions */
189static ssize_t show_in(struct device *dev, struct device_attribute *attr,
190 char *buf)
191{
192 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
193 int nr = sensor_attr->index;
194 struct vt8231_data *data = vt8231_update_device(dev);
195
196 return sprintf(buf, "%d\n", ((data->in[nr] - 3) * 10000) / 958);
197}
198
199static ssize_t show_in_min(struct device *dev, struct device_attribute *attr,
200 char *buf)
201{
202 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
203 int nr = sensor_attr->index;
204 struct vt8231_data *data = vt8231_update_device(dev);
205
206 return sprintf(buf, "%d\n", ((data->in_min[nr] - 3) * 10000) / 958);
207}
208
209static ssize_t show_in_max(struct device *dev, struct device_attribute *attr,
210 char *buf)
211{
212 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
213 int nr = sensor_attr->index;
214 struct vt8231_data *data = vt8231_update_device(dev);
215
216 return sprintf(buf, "%d\n", (((data->in_max[nr] - 3) * 10000) / 958));
217}
218
219static ssize_t set_in_min(struct device *dev, struct device_attribute *attr,
220 const char *buf, size_t count)
221{
222 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
223 int nr = sensor_attr->index;
Roger Lucasec5e1a42007-06-12 21:04:08 +0200224 struct vt8231_data *data = dev_get_drvdata(dev);
Guenter Roeck65fe5c72012-01-15 07:03:38 -0800225 unsigned long val;
226 int err;
227
228 err = kstrtoul(buf, 10, &val);
229 if (err)
230 return err;
Roger Lucas1de9e372005-11-26 20:20:05 +0100231
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100232 mutex_lock(&data->update_lock);
Roger Lucas1de9e372005-11-26 20:20:05 +0100233 data->in_min[nr] = SENSORS_LIMIT(((val * 958) / 10000) + 3, 0, 255);
Roger Lucasec5e1a42007-06-12 21:04:08 +0200234 vt8231_write_value(data, regvoltmin[nr], data->in_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100235 mutex_unlock(&data->update_lock);
Roger Lucas1de9e372005-11-26 20:20:05 +0100236 return count;
237}
238
239static ssize_t set_in_max(struct device *dev, struct device_attribute *attr,
240 const char *buf, size_t count)
241{
242 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
243 int nr = sensor_attr->index;
Roger Lucasec5e1a42007-06-12 21:04:08 +0200244 struct vt8231_data *data = dev_get_drvdata(dev);
Guenter Roeck65fe5c72012-01-15 07:03:38 -0800245 unsigned long val;
246 int err;
247
248 err = kstrtoul(buf, 10, &val);
249 if (err)
250 return err;
Roger Lucas1de9e372005-11-26 20:20:05 +0100251
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100252 mutex_lock(&data->update_lock);
Roger Lucas1de9e372005-11-26 20:20:05 +0100253 data->in_max[nr] = SENSORS_LIMIT(((val * 958) / 10000) + 3, 0, 255);
Roger Lucasec5e1a42007-06-12 21:04:08 +0200254 vt8231_write_value(data, regvoltmax[nr], data->in_max[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100255 mutex_unlock(&data->update_lock);
Roger Lucas1de9e372005-11-26 20:20:05 +0100256 return count;
257}
258
259/* Special case for input 5 as this has 3.3V scaling built into the chip */
260static ssize_t show_in5(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[5] - 3) * 10000 * 54) / (958 * 34)));
267}
268
269static ssize_t show_in5_min(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_min[5] - 3) * 10000 * 54) / (958 * 34)));
276}
277
278static ssize_t show_in5_max(struct device *dev, struct device_attribute *attr,
279 char *buf)
280{
281 struct vt8231_data *data = vt8231_update_device(dev);
282
283 return sprintf(buf, "%d\n",
284 (((data->in_max[5] - 3) * 10000 * 54) / (958 * 34)));
285}
286
287static ssize_t set_in5_min(struct device *dev, struct device_attribute *attr,
288 const char *buf, size_t count)
289{
Roger Lucasec5e1a42007-06-12 21:04:08 +0200290 struct vt8231_data *data = dev_get_drvdata(dev);
Guenter Roeck65fe5c72012-01-15 07:03:38 -0800291 unsigned long val;
292 int err;
293
294 err = kstrtoul(buf, 10, &val);
295 if (err)
296 return err;
Roger Lucas1de9e372005-11-26 20:20:05 +0100297
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100298 mutex_lock(&data->update_lock);
Roger Lucas1de9e372005-11-26 20:20:05 +0100299 data->in_min[5] = SENSORS_LIMIT(((val * 958 * 34) / (10000 * 54)) + 3,
300 0, 255);
Roger Lucasec5e1a42007-06-12 21:04:08 +0200301 vt8231_write_value(data, regvoltmin[5], data->in_min[5]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100302 mutex_unlock(&data->update_lock);
Roger Lucas1de9e372005-11-26 20:20:05 +0100303 return count;
304}
305
306static ssize_t set_in5_max(struct device *dev, struct device_attribute *attr,
307 const char *buf, size_t count)
308{
Roger Lucasec5e1a42007-06-12 21:04:08 +0200309 struct vt8231_data *data = dev_get_drvdata(dev);
Guenter Roeck65fe5c72012-01-15 07:03:38 -0800310 unsigned long val;
311 int err;
312
313 err = kstrtoul(buf, 10, &val);
314 if (err)
315 return err;
Roger Lucas1de9e372005-11-26 20:20:05 +0100316
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100317 mutex_lock(&data->update_lock);
Roger Lucas1de9e372005-11-26 20:20:05 +0100318 data->in_max[5] = SENSORS_LIMIT(((val * 958 * 34) / (10000 * 54)) + 3,
319 0, 255);
Roger Lucasec5e1a42007-06-12 21:04:08 +0200320 vt8231_write_value(data, regvoltmax[5], data->in_max[5]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100321 mutex_unlock(&data->update_lock);
Roger Lucas1de9e372005-11-26 20:20:05 +0100322 return count;
323}
324
325#define define_voltage_sysfs(offset) \
326static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \
327 show_in, NULL, offset); \
328static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
329 show_in_min, set_in_min, offset); \
330static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
331 show_in_max, set_in_max, offset)
332
333define_voltage_sysfs(0);
334define_voltage_sysfs(1);
335define_voltage_sysfs(2);
336define_voltage_sysfs(3);
337define_voltage_sysfs(4);
338
339static DEVICE_ATTR(in5_input, S_IRUGO, show_in5, NULL);
340static DEVICE_ATTR(in5_min, S_IRUGO | S_IWUSR, show_in5_min, set_in5_min);
341static DEVICE_ATTR(in5_max, S_IRUGO | S_IWUSR, show_in5_max, set_in5_max);
342
343/* Temperatures */
344static ssize_t show_temp0(struct device *dev, struct device_attribute *attr,
345 char *buf)
346{
347 struct vt8231_data *data = vt8231_update_device(dev);
348 return sprintf(buf, "%d\n", data->temp[0] * 250);
349}
350
351static ssize_t show_temp0_max(struct device *dev, struct device_attribute *attr,
352 char *buf)
353{
354 struct vt8231_data *data = vt8231_update_device(dev);
355 return sprintf(buf, "%d\n", data->temp_max[0] * 1000);
356}
357
358static ssize_t show_temp0_min(struct device *dev, struct device_attribute *attr,
359 char *buf)
360{
361 struct vt8231_data *data = vt8231_update_device(dev);
362 return sprintf(buf, "%d\n", data->temp_min[0] * 1000);
363}
364
365static ssize_t set_temp0_max(struct device *dev, struct device_attribute *attr,
366 const char *buf, size_t count)
367{
Roger Lucasec5e1a42007-06-12 21:04:08 +0200368 struct vt8231_data *data = dev_get_drvdata(dev);
Guenter Roeck65fe5c72012-01-15 07:03:38 -0800369 long val;
370 int err;
371
372 err = kstrtol(buf, 10, &val);
373 if (err)
374 return err;
Roger Lucas1de9e372005-11-26 20:20:05 +0100375
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100376 mutex_lock(&data->update_lock);
Roger Lucas1de9e372005-11-26 20:20:05 +0100377 data->temp_max[0] = SENSORS_LIMIT((val + 500) / 1000, 0, 255);
Roger Lucasec5e1a42007-06-12 21:04:08 +0200378 vt8231_write_value(data, regtempmax[0], data->temp_max[0]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100379 mutex_unlock(&data->update_lock);
Roger Lucas1de9e372005-11-26 20:20:05 +0100380 return count;
381}
382static ssize_t set_temp0_min(struct device *dev, struct device_attribute *attr,
383 const char *buf, size_t count)
384{
Roger Lucasec5e1a42007-06-12 21:04:08 +0200385 struct vt8231_data *data = dev_get_drvdata(dev);
Guenter Roeck65fe5c72012-01-15 07:03:38 -0800386 long val;
387 int err;
388
389 err = kstrtol(buf, 10, &val);
390 if (err)
391 return err;
Roger Lucas1de9e372005-11-26 20:20:05 +0100392
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100393 mutex_lock(&data->update_lock);
Roger Lucas1de9e372005-11-26 20:20:05 +0100394 data->temp_min[0] = SENSORS_LIMIT((val + 500) / 1000, 0, 255);
Roger Lucasec5e1a42007-06-12 21:04:08 +0200395 vt8231_write_value(data, regtempmin[0], data->temp_min[0]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100396 mutex_unlock(&data->update_lock);
Roger Lucas1de9e372005-11-26 20:20:05 +0100397 return count;
398}
399
400static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
401 char *buf)
402{
403 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
404 int nr = sensor_attr->index;
405 struct vt8231_data *data = vt8231_update_device(dev);
406 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr]));
407}
408
409static ssize_t show_temp_max(struct device *dev, struct device_attribute *attr,
410 char *buf)
411{
412 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
413 int nr = sensor_attr->index;
414 struct vt8231_data *data = vt8231_update_device(dev);
415 return sprintf(buf, "%d\n", TEMP_MAXMIN_FROM_REG(data->temp_max[nr]));
416}
417
418static ssize_t show_temp_min(struct device *dev, struct device_attribute *attr,
419 char *buf)
420{
421 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
422 int nr = sensor_attr->index;
423 struct vt8231_data *data = vt8231_update_device(dev);
424 return sprintf(buf, "%d\n", TEMP_MAXMIN_FROM_REG(data->temp_min[nr]));
425}
426
427static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
428 const char *buf, size_t count)
429{
430 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
431 int nr = sensor_attr->index;
Roger Lucasec5e1a42007-06-12 21:04:08 +0200432 struct vt8231_data *data = dev_get_drvdata(dev);
Guenter Roeck65fe5c72012-01-15 07:03:38 -0800433 long val;
434 int err;
435
436 err = kstrtol(buf, 10, &val);
437 if (err)
438 return err;
Roger Lucas1de9e372005-11-26 20:20:05 +0100439
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100440 mutex_lock(&data->update_lock);
Roger Lucas1de9e372005-11-26 20:20:05 +0100441 data->temp_max[nr] = SENSORS_LIMIT(TEMP_MAXMIN_TO_REG(val), 0, 255);
Roger Lucasec5e1a42007-06-12 21:04:08 +0200442 vt8231_write_value(data, regtempmax[nr], data->temp_max[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100443 mutex_unlock(&data->update_lock);
Roger Lucas1de9e372005-11-26 20:20:05 +0100444 return count;
445}
446static ssize_t set_temp_min(struct device *dev, struct device_attribute *attr,
447 const char *buf, size_t count)
448{
449 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
450 int nr = sensor_attr->index;
Roger Lucasec5e1a42007-06-12 21:04:08 +0200451 struct vt8231_data *data = dev_get_drvdata(dev);
Guenter Roeck65fe5c72012-01-15 07:03:38 -0800452 long val;
453 int err;
454
455 err = kstrtol(buf, 10, &val);
456 if (err)
457 return err;
Roger Lucas1de9e372005-11-26 20:20:05 +0100458
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100459 mutex_lock(&data->update_lock);
Roger Lucas1de9e372005-11-26 20:20:05 +0100460 data->temp_min[nr] = SENSORS_LIMIT(TEMP_MAXMIN_TO_REG(val), 0, 255);
Roger Lucasec5e1a42007-06-12 21:04:08 +0200461 vt8231_write_value(data, regtempmin[nr], data->temp_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100462 mutex_unlock(&data->update_lock);
Roger Lucas1de9e372005-11-26 20:20:05 +0100463 return count;
464}
465
466/* Note that these map the Linux temperature sensor numbering (1-6) to the VIA
467** temperature sensor numbering (0-5)
468*/
469#define define_temperature_sysfs(offset) \
470static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
471 show_temp, NULL, offset - 1); \
472static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
473 show_temp_max, set_temp_max, offset - 1); \
Jean Delvaree3efa5a2006-02-05 23:11:16 +0100474static SENSOR_DEVICE_ATTR(temp##offset##_max_hyst, S_IRUGO | S_IWUSR, \
Roger Lucas1de9e372005-11-26 20:20:05 +0100475 show_temp_min, set_temp_min, offset - 1)
476
477static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp0, NULL);
478static DEVICE_ATTR(temp1_max, S_IRUGO | S_IWUSR, show_temp0_max, set_temp0_max);
Guenter Roeck65fe5c72012-01-15 07:03:38 -0800479static DEVICE_ATTR(temp1_max_hyst, S_IRUGO | S_IWUSR, show_temp0_min,
480 set_temp0_min);
Roger Lucas1de9e372005-11-26 20:20:05 +0100481
482define_temperature_sysfs(2);
483define_temperature_sysfs(3);
484define_temperature_sysfs(4);
485define_temperature_sysfs(5);
486define_temperature_sysfs(6);
487
Roger Lucas1de9e372005-11-26 20:20:05 +0100488/* Fans */
489static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
490 char *buf)
491{
492 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
493 int nr = sensor_attr->index;
494 struct vt8231_data *data = vt8231_update_device(dev);
495 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr],
496 DIV_FROM_REG(data->fan_div[nr])));
497}
498
499static ssize_t show_fan_min(struct device *dev, struct device_attribute *attr,
500 char *buf)
501{
502 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
503 int nr = sensor_attr->index;
504 struct vt8231_data *data = vt8231_update_device(dev);
505 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[nr],
506 DIV_FROM_REG(data->fan_div[nr])));
507}
508
509static ssize_t show_fan_div(struct device *dev, struct device_attribute *attr,
510 char *buf)
511{
512 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
513 int nr = sensor_attr->index;
514 struct vt8231_data *data = vt8231_update_device(dev);
515 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]));
516}
517
518static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
519 const char *buf, size_t count)
520{
521 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
522 int nr = sensor_attr->index;
Roger Lucasec5e1a42007-06-12 21:04:08 +0200523 struct vt8231_data *data = dev_get_drvdata(dev);
Guenter Roeck65fe5c72012-01-15 07:03:38 -0800524 unsigned long val;
525 int err;
526
527 err = kstrtoul(buf, 10, &val);
528 if (err)
529 return err;
Roger Lucas1de9e372005-11-26 20:20:05 +0100530
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100531 mutex_lock(&data->update_lock);
Roger Lucas1de9e372005-11-26 20:20:05 +0100532 data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
Roger Lucasec5e1a42007-06-12 21:04:08 +0200533 vt8231_write_value(data, VT8231_REG_FAN_MIN(nr), data->fan_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100534 mutex_unlock(&data->update_lock);
Roger Lucas1de9e372005-11-26 20:20:05 +0100535 return count;
536}
537
538static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
539 const char *buf, size_t count)
540{
Roger Lucasec5e1a42007-06-12 21:04:08 +0200541 struct vt8231_data *data = dev_get_drvdata(dev);
Roger Lucas1de9e372005-11-26 20:20:05 +0100542 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
Guenter Roeck65fe5c72012-01-15 07:03:38 -0800543 unsigned long val;
Roger Lucas1de9e372005-11-26 20:20:05 +0100544 int nr = sensor_attr->index;
Roger Lucasec5e1a42007-06-12 21:04:08 +0200545 int old = vt8231_read_value(data, VT8231_REG_FANDIV);
Roger Lucas1de9e372005-11-26 20:20:05 +0100546 long min = FAN_FROM_REG(data->fan_min[nr],
547 DIV_FROM_REG(data->fan_div[nr]));
Guenter Roeck65fe5c72012-01-15 07:03:38 -0800548 int err;
549
550 err = kstrtoul(buf, 10, &val);
551 if (err)
552 return err;
Roger Lucas1de9e372005-11-26 20:20:05 +0100553
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100554 mutex_lock(&data->update_lock);
Roger Lucas1de9e372005-11-26 20:20:05 +0100555 switch (val) {
Guenter Roeck65fe5c72012-01-15 07:03:38 -0800556 case 1:
557 data->fan_div[nr] = 0;
558 break;
559 case 2:
560 data->fan_div[nr] = 1;
561 break;
562 case 4:
563 data->fan_div[nr] = 2;
564 break;
565 case 8:
566 data->fan_div[nr] = 3;
567 break;
Roger Lucas1de9e372005-11-26 20:20:05 +0100568 default:
Joe Perchesb20ff132007-11-19 17:48:07 -0800569 dev_err(dev, "fan_div value %ld not supported. "
Guenter Roeck65fe5c72012-01-15 07:03:38 -0800570 "Choose one of 1, 2, 4 or 8!\n", val);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100571 mutex_unlock(&data->update_lock);
Roger Lucas1de9e372005-11-26 20:20:05 +0100572 return -EINVAL;
573 }
574
575 /* Correct the fan minimum speed */
576 data->fan_min[nr] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
Roger Lucasec5e1a42007-06-12 21:04:08 +0200577 vt8231_write_value(data, VT8231_REG_FAN_MIN(nr), data->fan_min[nr]);
Roger Lucas1de9e372005-11-26 20:20:05 +0100578
579 old = (old & 0x0f) | (data->fan_div[1] << 6) | (data->fan_div[0] << 4);
Roger Lucasec5e1a42007-06-12 21:04:08 +0200580 vt8231_write_value(data, VT8231_REG_FANDIV, old);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100581 mutex_unlock(&data->update_lock);
Roger Lucas1de9e372005-11-26 20:20:05 +0100582 return count;
583}
584
585
586#define define_fan_sysfs(offset) \
587static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
588 show_fan, NULL, offset - 1); \
589static SENSOR_DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
590 show_fan_div, set_fan_div, offset - 1); \
591static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
592 show_fan_min, set_fan_min, offset - 1)
593
594define_fan_sysfs(1);
595define_fan_sysfs(2);
596
597/* Alarms */
598static ssize_t show_alarms(struct device *dev, struct device_attribute *attr,
599 char *buf)
600{
601 struct vt8231_data *data = vt8231_update_device(dev);
602 return sprintf(buf, "%d\n", data->alarms);
603}
Roger Lucas1de9e372005-11-26 20:20:05 +0100604static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
605
Jean Delvare2d1374c2008-01-06 15:46:02 +0100606static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
607 char *buf)
608{
609 int bitnr = to_sensor_dev_attr(attr)->index;
610 struct vt8231_data *data = vt8231_update_device(dev);
611 return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
612}
613static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 4);
614static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 11);
615static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 0);
616static SENSOR_DEVICE_ATTR(temp4_alarm, S_IRUGO, show_alarm, NULL, 1);
617static SENSOR_DEVICE_ATTR(temp5_alarm, S_IRUGO, show_alarm, NULL, 3);
618static SENSOR_DEVICE_ATTR(temp6_alarm, S_IRUGO, show_alarm, NULL, 8);
619static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 11);
620static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 0);
621static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 1);
622static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3);
623static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 8);
624static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 2);
625static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 6);
626static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 7);
627
Roger Lucasec5e1a42007-06-12 21:04:08 +0200628static ssize_t show_name(struct device *dev, struct device_attribute
629 *devattr, char *buf)
630{
631 struct vt8231_data *data = dev_get_drvdata(dev);
632 return sprintf(buf, "%s\n", data->name);
633}
634static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
635
Jean Delvare2d1374c2008-01-06 15:46:02 +0100636static struct attribute *vt8231_attributes_temps[6][5] = {
Roger Lucascbeeb5b2006-09-24 21:21:46 +0200637 {
638 &dev_attr_temp1_input.attr,
639 &dev_attr_temp1_max_hyst.attr,
640 &dev_attr_temp1_max.attr,
Jean Delvare2d1374c2008-01-06 15:46:02 +0100641 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
Roger Lucascbeeb5b2006-09-24 21:21:46 +0200642 NULL
643 }, {
644 &sensor_dev_attr_temp2_input.dev_attr.attr,
645 &sensor_dev_attr_temp2_max_hyst.dev_attr.attr,
646 &sensor_dev_attr_temp2_max.dev_attr.attr,
Jean Delvare2d1374c2008-01-06 15:46:02 +0100647 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
Roger Lucascbeeb5b2006-09-24 21:21:46 +0200648 NULL
649 }, {
650 &sensor_dev_attr_temp3_input.dev_attr.attr,
651 &sensor_dev_attr_temp3_max_hyst.dev_attr.attr,
652 &sensor_dev_attr_temp3_max.dev_attr.attr,
Jean Delvare2d1374c2008-01-06 15:46:02 +0100653 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
Roger Lucascbeeb5b2006-09-24 21:21:46 +0200654 NULL
655 }, {
656 &sensor_dev_attr_temp4_input.dev_attr.attr,
657 &sensor_dev_attr_temp4_max_hyst.dev_attr.attr,
658 &sensor_dev_attr_temp4_max.dev_attr.attr,
Jean Delvare2d1374c2008-01-06 15:46:02 +0100659 &sensor_dev_attr_temp4_alarm.dev_attr.attr,
Roger Lucascbeeb5b2006-09-24 21:21:46 +0200660 NULL
661 }, {
662 &sensor_dev_attr_temp5_input.dev_attr.attr,
663 &sensor_dev_attr_temp5_max_hyst.dev_attr.attr,
664 &sensor_dev_attr_temp5_max.dev_attr.attr,
Jean Delvare2d1374c2008-01-06 15:46:02 +0100665 &sensor_dev_attr_temp5_alarm.dev_attr.attr,
Roger Lucascbeeb5b2006-09-24 21:21:46 +0200666 NULL
667 }, {
668 &sensor_dev_attr_temp6_input.dev_attr.attr,
669 &sensor_dev_attr_temp6_max_hyst.dev_attr.attr,
670 &sensor_dev_attr_temp6_max.dev_attr.attr,
Jean Delvare2d1374c2008-01-06 15:46:02 +0100671 &sensor_dev_attr_temp6_alarm.dev_attr.attr,
Roger Lucascbeeb5b2006-09-24 21:21:46 +0200672 NULL
673 }
674};
675
676static const struct attribute_group vt8231_group_temps[6] = {
677 { .attrs = vt8231_attributes_temps[0] },
678 { .attrs = vt8231_attributes_temps[1] },
679 { .attrs = vt8231_attributes_temps[2] },
680 { .attrs = vt8231_attributes_temps[3] },
681 { .attrs = vt8231_attributes_temps[4] },
682 { .attrs = vt8231_attributes_temps[5] },
683};
684
Jean Delvare2d1374c2008-01-06 15:46:02 +0100685static struct attribute *vt8231_attributes_volts[6][5] = {
Roger Lucascbeeb5b2006-09-24 21:21:46 +0200686 {
687 &sensor_dev_attr_in0_input.dev_attr.attr,
688 &sensor_dev_attr_in0_min.dev_attr.attr,
689 &sensor_dev_attr_in0_max.dev_attr.attr,
Jean Delvare2d1374c2008-01-06 15:46:02 +0100690 &sensor_dev_attr_in0_alarm.dev_attr.attr,
Roger Lucascbeeb5b2006-09-24 21:21:46 +0200691 NULL
692 }, {
693 &sensor_dev_attr_in1_input.dev_attr.attr,
694 &sensor_dev_attr_in1_min.dev_attr.attr,
695 &sensor_dev_attr_in1_max.dev_attr.attr,
Jean Delvare2d1374c2008-01-06 15:46:02 +0100696 &sensor_dev_attr_in1_alarm.dev_attr.attr,
Roger Lucascbeeb5b2006-09-24 21:21:46 +0200697 NULL
698 }, {
699 &sensor_dev_attr_in2_input.dev_attr.attr,
700 &sensor_dev_attr_in2_min.dev_attr.attr,
701 &sensor_dev_attr_in2_max.dev_attr.attr,
Jean Delvare2d1374c2008-01-06 15:46:02 +0100702 &sensor_dev_attr_in2_alarm.dev_attr.attr,
Roger Lucascbeeb5b2006-09-24 21:21:46 +0200703 NULL
704 }, {
705 &sensor_dev_attr_in3_input.dev_attr.attr,
706 &sensor_dev_attr_in3_min.dev_attr.attr,
707 &sensor_dev_attr_in3_max.dev_attr.attr,
Jean Delvare2d1374c2008-01-06 15:46:02 +0100708 &sensor_dev_attr_in3_alarm.dev_attr.attr,
Roger Lucascbeeb5b2006-09-24 21:21:46 +0200709 NULL
710 }, {
711 &sensor_dev_attr_in4_input.dev_attr.attr,
712 &sensor_dev_attr_in4_min.dev_attr.attr,
713 &sensor_dev_attr_in4_max.dev_attr.attr,
Jean Delvare2d1374c2008-01-06 15:46:02 +0100714 &sensor_dev_attr_in4_alarm.dev_attr.attr,
Roger Lucascbeeb5b2006-09-24 21:21:46 +0200715 NULL
716 }, {
717 &dev_attr_in5_input.attr,
718 &dev_attr_in5_min.attr,
719 &dev_attr_in5_max.attr,
Jean Delvare2d1374c2008-01-06 15:46:02 +0100720 &sensor_dev_attr_in5_alarm.dev_attr.attr,
Roger Lucascbeeb5b2006-09-24 21:21:46 +0200721 NULL
722 }
723};
724
725static const struct attribute_group vt8231_group_volts[6] = {
726 { .attrs = vt8231_attributes_volts[0] },
727 { .attrs = vt8231_attributes_volts[1] },
728 { .attrs = vt8231_attributes_volts[2] },
729 { .attrs = vt8231_attributes_volts[3] },
730 { .attrs = vt8231_attributes_volts[4] },
731 { .attrs = vt8231_attributes_volts[5] },
732};
733
734static struct attribute *vt8231_attributes[] = {
735 &sensor_dev_attr_fan1_input.dev_attr.attr,
736 &sensor_dev_attr_fan2_input.dev_attr.attr,
737 &sensor_dev_attr_fan1_min.dev_attr.attr,
738 &sensor_dev_attr_fan2_min.dev_attr.attr,
739 &sensor_dev_attr_fan1_div.dev_attr.attr,
740 &sensor_dev_attr_fan2_div.dev_attr.attr,
Jean Delvare2d1374c2008-01-06 15:46:02 +0100741 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
742 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
Roger Lucascbeeb5b2006-09-24 21:21:46 +0200743 &dev_attr_alarms.attr,
Roger Lucasec5e1a42007-06-12 21:04:08 +0200744 &dev_attr_name.attr,
Roger Lucascbeeb5b2006-09-24 21:21:46 +0200745 NULL
746};
747
748static const struct attribute_group vt8231_group = {
749 .attrs = vt8231_attributes,
750};
751
Roger Lucasec5e1a42007-06-12 21:04:08 +0200752static struct platform_driver vt8231_driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100753 .driver = {
Jean Delvare87218842006-09-03 22:36:14 +0200754 .owner = THIS_MODULE,
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100755 .name = "vt8231",
756 },
Roger Lucasec5e1a42007-06-12 21:04:08 +0200757 .probe = vt8231_probe,
758 .remove = __devexit_p(vt8231_remove),
Roger Lucas1de9e372005-11-26 20:20:05 +0100759};
760
Frans Meulenbroeks600151b2012-01-05 19:50:17 +0100761static DEFINE_PCI_DEVICE_TABLE(vt8231_pci_ids) = {
Roger Lucas1de9e372005-11-26 20:20:05 +0100762 { PCI_DEVICE(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8231_4) },
763 { 0, }
764};
765
766MODULE_DEVICE_TABLE(pci, vt8231_pci_ids);
767
768static int __devinit vt8231_pci_probe(struct pci_dev *dev,
Guenter Roeck65fe5c72012-01-15 07:03:38 -0800769 const struct pci_device_id *id);
Roger Lucas1de9e372005-11-26 20:20:05 +0100770
771static struct pci_driver vt8231_pci_driver = {
772 .name = "vt8231",
773 .id_table = vt8231_pci_ids,
774 .probe = vt8231_pci_probe,
775};
776
Mark M. Hoffmana022fef2007-10-14 15:00:24 -0400777static int vt8231_probe(struct platform_device *pdev)
Roger Lucas1de9e372005-11-26 20:20:05 +0100778{
Roger Lucasec5e1a42007-06-12 21:04:08 +0200779 struct resource *res;
Roger Lucas1de9e372005-11-26 20:20:05 +0100780 struct vt8231_data *data;
781 int err = 0, i;
Roger Lucas1de9e372005-11-26 20:20:05 +0100782
783 /* Reserve the ISA region */
Roger Lucasec5e1a42007-06-12 21:04:08 +0200784 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
785 if (!request_region(res->start, VT8231_EXTENT,
786 vt8231_driver.driver.name)) {
787 dev_err(&pdev->dev, "Region 0x%lx-0x%lx already in use!\n",
788 (unsigned long)res->start, (unsigned long)res->end);
Roger Lucas1de9e372005-11-26 20:20:05 +0100789 return -ENODEV;
790 }
791
Guenter Roeck65fe5c72012-01-15 07:03:38 -0800792 data = kzalloc(sizeof(struct vt8231_data), GFP_KERNEL);
793 if (!data) {
Roger Lucas1de9e372005-11-26 20:20:05 +0100794 err = -ENOMEM;
795 goto exit_release;
796 }
797
Roger Lucasec5e1a42007-06-12 21:04:08 +0200798 platform_set_drvdata(pdev, data);
799 data->addr = res->start;
800 data->name = "vt8231";
Roger Lucas1de9e372005-11-26 20:20:05 +0100801
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100802 mutex_init(&data->update_lock);
Roger Lucasec5e1a42007-06-12 21:04:08 +0200803 vt8231_init_device(data);
Roger Lucas1de9e372005-11-26 20:20:05 +0100804
805 /* Register sysfs hooks */
Guenter Roeck65fe5c72012-01-15 07:03:38 -0800806 err = sysfs_create_group(&pdev->dev.kobj, &vt8231_group);
807 if (err)
Roger Lucasec5e1a42007-06-12 21:04:08 +0200808 goto exit_free;
Roger Lucas1de9e372005-11-26 20:20:05 +0100809
810 /* Must update device information to find out the config field */
Roger Lucasec5e1a42007-06-12 21:04:08 +0200811 data->uch_config = vt8231_read_value(data, VT8231_REG_UCH_CONFIG);
Roger Lucas1de9e372005-11-26 20:20:05 +0100812
Roger Lucascbeeb5b2006-09-24 21:21:46 +0200813 for (i = 0; i < ARRAY_SIZE(vt8231_group_temps); i++) {
Roger Lucas1de9e372005-11-26 20:20:05 +0100814 if (ISTEMP(i, data->uch_config)) {
Guenter Roeck65fe5c72012-01-15 07:03:38 -0800815 err = sysfs_create_group(&pdev->dev.kobj,
816 &vt8231_group_temps[i]);
817 if (err)
Roger Lucascbeeb5b2006-09-24 21:21:46 +0200818 goto exit_remove_files;
Roger Lucas1de9e372005-11-26 20:20:05 +0100819 }
820 }
821
Roger Lucascbeeb5b2006-09-24 21:21:46 +0200822 for (i = 0; i < ARRAY_SIZE(vt8231_group_volts); i++) {
Roger Lucas1de9e372005-11-26 20:20:05 +0100823 if (ISVOLT(i, data->uch_config)) {
Guenter Roeck65fe5c72012-01-15 07:03:38 -0800824 err = sysfs_create_group(&pdev->dev.kobj,
825 &vt8231_group_volts[i]);
826 if (err)
Roger Lucascbeeb5b2006-09-24 21:21:46 +0200827 goto exit_remove_files;
Roger Lucas1de9e372005-11-26 20:20:05 +0100828 }
829 }
830
Tony Jones1beeffe2007-08-20 13:46:20 -0700831 data->hwmon_dev = hwmon_device_register(&pdev->dev);
832 if (IS_ERR(data->hwmon_dev)) {
833 err = PTR_ERR(data->hwmon_dev);
Roger Lucascbeeb5b2006-09-24 21:21:46 +0200834 goto exit_remove_files;
835 }
Roger Lucas1de9e372005-11-26 20:20:05 +0100836 return 0;
837
Roger Lucascbeeb5b2006-09-24 21:21:46 +0200838exit_remove_files:
839 for (i = 0; i < ARRAY_SIZE(vt8231_group_volts); i++)
Roger Lucasec5e1a42007-06-12 21:04:08 +0200840 sysfs_remove_group(&pdev->dev.kobj, &vt8231_group_volts[i]);
Roger Lucascbeeb5b2006-09-24 21:21:46 +0200841
842 for (i = 0; i < ARRAY_SIZE(vt8231_group_temps); i++)
Roger Lucasec5e1a42007-06-12 21:04:08 +0200843 sysfs_remove_group(&pdev->dev.kobj, &vt8231_group_temps[i]);
Roger Lucascbeeb5b2006-09-24 21:21:46 +0200844
Roger Lucasec5e1a42007-06-12 21:04:08 +0200845 sysfs_remove_group(&pdev->dev.kobj, &vt8231_group);
846
Roger Lucas1de9e372005-11-26 20:20:05 +0100847exit_free:
Jean Delvare04a62172007-06-12 13:57:19 +0200848 platform_set_drvdata(pdev, NULL);
Roger Lucas1de9e372005-11-26 20:20:05 +0100849 kfree(data);
Roger Lucasec5e1a42007-06-12 21:04:08 +0200850
Roger Lucas1de9e372005-11-26 20:20:05 +0100851exit_release:
Roger Lucasec5e1a42007-06-12 21:04:08 +0200852 release_region(res->start, VT8231_EXTENT);
Roger Lucas1de9e372005-11-26 20:20:05 +0100853 return err;
854}
855
Jean Delvared0546122007-07-22 12:09:48 +0200856static int __devexit vt8231_remove(struct platform_device *pdev)
Roger Lucas1de9e372005-11-26 20:20:05 +0100857{
Roger Lucasec5e1a42007-06-12 21:04:08 +0200858 struct vt8231_data *data = platform_get_drvdata(pdev);
859 int i;
Roger Lucas1de9e372005-11-26 20:20:05 +0100860
Tony Jones1beeffe2007-08-20 13:46:20 -0700861 hwmon_device_unregister(data->hwmon_dev);
Roger Lucas1de9e372005-11-26 20:20:05 +0100862
Roger Lucascbeeb5b2006-09-24 21:21:46 +0200863 for (i = 0; i < ARRAY_SIZE(vt8231_group_volts); i++)
Roger Lucasec5e1a42007-06-12 21:04:08 +0200864 sysfs_remove_group(&pdev->dev.kobj, &vt8231_group_volts[i]);
Roger Lucascbeeb5b2006-09-24 21:21:46 +0200865
866 for (i = 0; i < ARRAY_SIZE(vt8231_group_temps); i++)
Roger Lucasec5e1a42007-06-12 21:04:08 +0200867 sysfs_remove_group(&pdev->dev.kobj, &vt8231_group_temps[i]);
Roger Lucascbeeb5b2006-09-24 21:21:46 +0200868
Roger Lucasec5e1a42007-06-12 21:04:08 +0200869 sysfs_remove_group(&pdev->dev.kobj, &vt8231_group);
Roger Lucascbeeb5b2006-09-24 21:21:46 +0200870
Roger Lucasec5e1a42007-06-12 21:04:08 +0200871 release_region(data->addr, VT8231_EXTENT);
872 platform_set_drvdata(pdev, NULL);
Roger Lucas1de9e372005-11-26 20:20:05 +0100873 kfree(data);
Roger Lucas1de9e372005-11-26 20:20:05 +0100874 return 0;
875}
876
Roger Lucasec5e1a42007-06-12 21:04:08 +0200877static void vt8231_init_device(struct vt8231_data *data)
Roger Lucas1de9e372005-11-26 20:20:05 +0100878{
Roger Lucasec5e1a42007-06-12 21:04:08 +0200879 vt8231_write_value(data, VT8231_REG_TEMP1_CONFIG, 0);
880 vt8231_write_value(data, VT8231_REG_TEMP2_CONFIG, 0);
Roger Lucas1de9e372005-11-26 20:20:05 +0100881}
882
883static struct vt8231_data *vt8231_update_device(struct device *dev)
884{
Roger Lucasec5e1a42007-06-12 21:04:08 +0200885 struct vt8231_data *data = dev_get_drvdata(dev);
Roger Lucas1de9e372005-11-26 20:20:05 +0100886 int i;
887 u16 low;
888
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100889 mutex_lock(&data->update_lock);
Roger Lucas1de9e372005-11-26 20:20:05 +0100890
891 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
892 || !data->valid) {
893 for (i = 0; i < 6; i++) {
894 if (ISVOLT(i, data->uch_config)) {
Roger Lucasec5e1a42007-06-12 21:04:08 +0200895 data->in[i] = vt8231_read_value(data,
Roger Lucas1de9e372005-11-26 20:20:05 +0100896 regvolt[i]);
Roger Lucasec5e1a42007-06-12 21:04:08 +0200897 data->in_min[i] = vt8231_read_value(data,
Roger Lucas1de9e372005-11-26 20:20:05 +0100898 regvoltmin[i]);
Roger Lucasec5e1a42007-06-12 21:04:08 +0200899 data->in_max[i] = vt8231_read_value(data,
Roger Lucas1de9e372005-11-26 20:20:05 +0100900 regvoltmax[i]);
901 }
902 }
903 for (i = 0; i < 2; i++) {
Roger Lucasec5e1a42007-06-12 21:04:08 +0200904 data->fan[i] = vt8231_read_value(data,
Roger Lucas1de9e372005-11-26 20:20:05 +0100905 VT8231_REG_FAN(i));
Roger Lucasec5e1a42007-06-12 21:04:08 +0200906 data->fan_min[i] = vt8231_read_value(data,
Roger Lucas1de9e372005-11-26 20:20:05 +0100907 VT8231_REG_FAN_MIN(i));
908 }
909
Roger Lucasec5e1a42007-06-12 21:04:08 +0200910 low = vt8231_read_value(data, VT8231_REG_TEMP_LOW01);
Roger Lucas1de9e372005-11-26 20:20:05 +0100911 low = (low >> 6) | ((low & 0x30) >> 2)
Roger Lucasec5e1a42007-06-12 21:04:08 +0200912 | (vt8231_read_value(data, VT8231_REG_TEMP_LOW25) << 4);
Roger Lucas1de9e372005-11-26 20:20:05 +0100913 for (i = 0; i < 6; i++) {
914 if (ISTEMP(i, data->uch_config)) {
Roger Lucasec5e1a42007-06-12 21:04:08 +0200915 data->temp[i] = (vt8231_read_value(data,
Roger Lucas1de9e372005-11-26 20:20:05 +0100916 regtemp[i]) << 2)
917 | ((low >> (2 * i)) & 0x03);
Roger Lucasec5e1a42007-06-12 21:04:08 +0200918 data->temp_max[i] = vt8231_read_value(data,
Roger Lucas1de9e372005-11-26 20:20:05 +0100919 regtempmax[i]);
Roger Lucasec5e1a42007-06-12 21:04:08 +0200920 data->temp_min[i] = vt8231_read_value(data,
Roger Lucas1de9e372005-11-26 20:20:05 +0100921 regtempmin[i]);
922 }
923 }
924
Roger Lucasec5e1a42007-06-12 21:04:08 +0200925 i = vt8231_read_value(data, VT8231_REG_FANDIV);
Roger Lucas1de9e372005-11-26 20:20:05 +0100926 data->fan_div[0] = (i >> 4) & 0x03;
927 data->fan_div[1] = i >> 6;
Roger Lucasec5e1a42007-06-12 21:04:08 +0200928 data->alarms = vt8231_read_value(data, VT8231_REG_ALARM1) |
929 (vt8231_read_value(data, VT8231_REG_ALARM2) << 8);
Roger Lucas1de9e372005-11-26 20:20:05 +0100930
931 /* Set alarm flags correctly */
Guenter Roeck65fe5c72012-01-15 07:03:38 -0800932 if (!data->fan[0] && data->fan_min[0])
Roger Lucas1de9e372005-11-26 20:20:05 +0100933 data->alarms |= 0x40;
Guenter Roeck65fe5c72012-01-15 07:03:38 -0800934 else if (data->fan[0] && !data->fan_min[0])
Roger Lucas1de9e372005-11-26 20:20:05 +0100935 data->alarms &= ~0x40;
Roger Lucas1de9e372005-11-26 20:20:05 +0100936
Guenter Roeck65fe5c72012-01-15 07:03:38 -0800937 if (!data->fan[1] && data->fan_min[1])
Roger Lucas1de9e372005-11-26 20:20:05 +0100938 data->alarms |= 0x80;
Guenter Roeck65fe5c72012-01-15 07:03:38 -0800939 else if (data->fan[1] && !data->fan_min[1])
Roger Lucas1de9e372005-11-26 20:20:05 +0100940 data->alarms &= ~0x80;
Roger Lucas1de9e372005-11-26 20:20:05 +0100941
942 data->last_updated = jiffies;
943 data->valid = 1;
944 }
945
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100946 mutex_unlock(&data->update_lock);
Roger Lucas1de9e372005-11-26 20:20:05 +0100947
948 return data;
949}
950
Roger Lucasec5e1a42007-06-12 21:04:08 +0200951static int __devinit vt8231_device_add(unsigned short address)
952{
953 struct resource res = {
954 .start = address,
955 .end = address + VT8231_EXTENT - 1,
956 .name = "vt8231",
957 .flags = IORESOURCE_IO,
958 };
959 int err;
960
Jean Delvareb9acb642009-01-07 16:37:35 +0100961 err = acpi_check_resource_conflict(&res);
962 if (err)
963 goto exit;
964
Roger Lucasec5e1a42007-06-12 21:04:08 +0200965 pdev = platform_device_alloc("vt8231", address);
966 if (!pdev) {
967 err = -ENOMEM;
Joe Perches9d72be02010-10-20 06:51:53 +0000968 pr_err("Device allocation failed\n");
Roger Lucasec5e1a42007-06-12 21:04:08 +0200969 goto exit;
970 }
971
972 err = platform_device_add_resources(pdev, &res, 1);
973 if (err) {
Joe Perches9d72be02010-10-20 06:51:53 +0000974 pr_err("Device resource addition failed (%d)\n", err);
Roger Lucasec5e1a42007-06-12 21:04:08 +0200975 goto exit_device_put;
976 }
977
978 err = platform_device_add(pdev);
979 if (err) {
Joe Perches9d72be02010-10-20 06:51:53 +0000980 pr_err("Device addition failed (%d)\n", err);
Roger Lucasec5e1a42007-06-12 21:04:08 +0200981 goto exit_device_put;
982 }
983
984 return 0;
985
986exit_device_put:
987 platform_device_put(pdev);
988exit:
989 return err;
990}
991
Roger Lucas1de9e372005-11-26 20:20:05 +0100992static int __devinit vt8231_pci_probe(struct pci_dev *dev,
993 const struct pci_device_id *id)
994{
Roger Lucasec5e1a42007-06-12 21:04:08 +0200995 u16 address, val;
996 if (force_addr) {
997 address = force_addr & 0xff00;
998 dev_warn(&dev->dev, "Forcing ISA address 0x%x\n",
999 address);
1000
1001 if (PCIBIOS_SUCCESSFUL !=
1002 pci_write_config_word(dev, VT8231_BASE_REG, address | 1))
1003 return -ENODEV;
1004 }
Roger Lucas1de9e372005-11-26 20:20:05 +01001005
1006 if (PCIBIOS_SUCCESSFUL != pci_read_config_word(dev, VT8231_BASE_REG,
1007 &val))
1008 return -ENODEV;
1009
Roger Lucasec5e1a42007-06-12 21:04:08 +02001010 address = val & ~(VT8231_EXTENT - 1);
1011 if (address == 0) {
Joe Perches4cae7872010-03-05 13:43:56 -08001012 dev_err(&dev->dev, "base address not set - upgrade BIOS or use force_addr=0xaddr\n");
Roger Lucas1de9e372005-11-26 20:20:05 +01001013 return -ENODEV;
1014 }
1015
Roger Lucasec5e1a42007-06-12 21:04:08 +02001016 if (PCIBIOS_SUCCESSFUL != pci_read_config_word(dev, VT8231_ENABLE_REG,
1017 &val))
1018 return -ENODEV;
Roger Lucas1de9e372005-11-26 20:20:05 +01001019
Roger Lucasec5e1a42007-06-12 21:04:08 +02001020 if (!(val & 0x0001)) {
1021 dev_warn(&dev->dev, "enabling sensors\n");
1022 if (PCIBIOS_SUCCESSFUL !=
1023 pci_write_config_word(dev, VT8231_ENABLE_REG,
1024 val | 0x0001))
1025 return -ENODEV;
Roger Lucas1de9e372005-11-26 20:20:05 +01001026 }
1027
Roger Lucasec5e1a42007-06-12 21:04:08 +02001028 if (platform_driver_register(&vt8231_driver))
1029 goto exit;
1030
1031 /* Sets global pdev as a side effect */
1032 if (vt8231_device_add(address))
1033 goto exit_unregister;
1034
Roger Lucas1de9e372005-11-26 20:20:05 +01001035 /* Always return failure here. This is to allow other drivers to bind
1036 * to this pci device. We don't really want to have control over the
1037 * pci device, we only wanted to read as few register values from it.
1038 */
Roger Lucasec5e1a42007-06-12 21:04:08 +02001039
1040 /* We do, however, mark ourselves as using the PCI device to stop it
1041 getting unloaded. */
1042 s_bridge = pci_dev_get(dev);
1043 return -ENODEV;
1044
1045exit_unregister:
1046 platform_driver_unregister(&vt8231_driver);
1047exit:
Roger Lucas1de9e372005-11-26 20:20:05 +01001048 return -ENODEV;
1049}
1050
1051static int __init sm_vt8231_init(void)
1052{
Richard Knutsson93b47682005-11-30 01:00:35 +01001053 return pci_register_driver(&vt8231_pci_driver);
Roger Lucas1de9e372005-11-26 20:20:05 +01001054}
1055
1056static void __exit sm_vt8231_exit(void)
1057{
1058 pci_unregister_driver(&vt8231_pci_driver);
1059 if (s_bridge != NULL) {
Roger Lucasec5e1a42007-06-12 21:04:08 +02001060 platform_device_unregister(pdev);
1061 platform_driver_unregister(&vt8231_driver);
Roger Lucas1de9e372005-11-26 20:20:05 +01001062 pci_dev_put(s_bridge);
1063 s_bridge = NULL;
1064 }
1065}
1066
Roger Lucasaf865762008-02-13 07:52:06 -05001067MODULE_AUTHOR("Roger Lucas <vt8231@hiddenengine.co.uk>");
Roger Lucas1de9e372005-11-26 20:20:05 +01001068MODULE_DESCRIPTION("VT8231 sensors");
1069MODULE_LICENSE("GPL");
1070
1071module_init(sm_vt8231_init);
1072module_exit(sm_vt8231_exit);