blob: a86ef1b192093c7050b00eefa47912343445f787 [file] [log] [blame]
Roger Lucas1de9e372005-11-26 20:20:05 +01001/*
Guenter Roeck61ba0312012-01-19 11:02:27 -08002 * vt8231.c - Part of lm_sensors, Linux kernel modules
3 * for hardware monitoring
4 *
5 * Copyright (c) 2005 Roger Lucas <vt8231@hiddenengine.co.uk>
6 * Copyright (c) 2002 Mark D. Studebaker <mdsxyz123@yahoo.com>
7 * Aaron M. Marsh <amarsh@sdf.lonestar.org>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
Roger Lucas1de9e372005-11-26 20:20:05 +010023
Guenter Roeck61ba0312012-01-19 11:02:27 -080024/*
25 * Supports VIA VT8231 South Bridge embedded sensors
26 */
Roger Lucas1de9e372005-11-26 20:20:05 +010027
Joe Perches9d72be02010-10-20 06:51:53 +000028#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
29
Roger Lucas1de9e372005-11-26 20:20:05 +010030#include <linux/module.h>
31#include <linux/init.h>
32#include <linux/slab.h>
33#include <linux/pci.h>
34#include <linux/jiffies.h>
Roger Lucasec5e1a42007-06-12 21:04:08 +020035#include <linux/platform_device.h>
Roger Lucas1de9e372005-11-26 20:20:05 +010036#include <linux/hwmon.h>
37#include <linux/hwmon-sysfs.h>
38#include <linux/hwmon-vid.h>
39#include <linux/err.h>
Ingo Molnar9a61bf62006-01-18 23:19:26 +010040#include <linux/mutex.h>
Jean Delvareb9acb642009-01-07 16:37:35 +010041#include <linux/acpi.h>
H Hartley Sweeten6055fae2009-09-15 17:18:13 +020042#include <linux/io.h>
Roger Lucas1de9e372005-11-26 20:20:05 +010043
44static int force_addr;
45module_param(force_addr, int, 0);
46MODULE_PARM_DESC(force_addr, "Initialize the base address of the sensors");
47
Roger Lucasec5e1a42007-06-12 21:04:08 +020048static struct platform_device *pdev;
Roger Lucas1de9e372005-11-26 20:20:05 +010049
50#define VT8231_EXTENT 0x80
51#define VT8231_BASE_REG 0x70
52#define VT8231_ENABLE_REG 0x74
53
Guenter Roeck61ba0312012-01-19 11:02:27 -080054/*
55 * The VT8231 registers
56 *
57 * The reset value for the input channel configuration is used (Reg 0x4A=0x07)
58 * which sets the selected inputs marked with '*' below if multiple options are
59 * possible:
60 *
61 * Voltage Mode Temperature Mode
62 * Sensor Linux Id Linux Id VIA Id
63 * -------- -------- -------- ------
64 * CPU Diode N/A temp1 0
65 * UIC1 in0 temp2 * 1
66 * UIC2 in1 * temp3 2
67 * UIC3 in2 * temp4 3
68 * UIC4 in3 * temp5 4
69 * UIC5 in4 * temp6 5
70 * 3.3V in5 N/A
71 *
72 * Note that the BIOS may set the configuration register to a different value
73 * to match the motherboard configuration.
74 */
Roger Lucas1de9e372005-11-26 20:20:05 +010075
76/* fans numbered 0-1 */
77#define VT8231_REG_FAN_MIN(nr) (0x3b + (nr))
78#define VT8231_REG_FAN(nr) (0x29 + (nr))
79
80/* Voltage inputs numbered 0-5 */
81
82static const u8 regvolt[] = { 0x21, 0x22, 0x23, 0x24, 0x25, 0x26 };
83static const u8 regvoltmax[] = { 0x3d, 0x2b, 0x2d, 0x2f, 0x31, 0x33 };
84static const u8 regvoltmin[] = { 0x3e, 0x2c, 0x2e, 0x30, 0x32, 0x34 };
85
Guenter Roeck61ba0312012-01-19 11:02:27 -080086/*
87 * Temperatures are numbered 1-6 according to the Linux kernel specification.
88 *
89 * In the VIA datasheet, however, the temperatures are numbered from zero.
90 * Since it is important that this driver can easily be compared to the VIA
91 * datasheet, we will use the VIA numbering within this driver and map the
92 * kernel sysfs device name to the VIA number in the sysfs callback.
93 */
Roger Lucas1de9e372005-11-26 20:20:05 +010094
95#define VT8231_REG_TEMP_LOW01 0x49
96#define VT8231_REG_TEMP_LOW25 0x4d
97
98static const u8 regtemp[] = { 0x1f, 0x21, 0x22, 0x23, 0x24, 0x25 };
99static const u8 regtempmax[] = { 0x39, 0x3d, 0x2b, 0x2d, 0x2f, 0x31 };
100static const u8 regtempmin[] = { 0x3a, 0x3e, 0x2c, 0x2e, 0x30, 0x32 };
101
102#define TEMP_FROM_REG(reg) (((253 * 4 - (reg)) * 550 + 105) / 210)
103#define TEMP_MAXMIN_FROM_REG(reg) (((253 - (reg)) * 2200 + 105) / 210)
104#define TEMP_MAXMIN_TO_REG(val) (253 - ((val) * 210 + 1100) / 2200)
105
106#define VT8231_REG_CONFIG 0x40
107#define VT8231_REG_ALARM1 0x41
108#define VT8231_REG_ALARM2 0x42
109#define VT8231_REG_FANDIV 0x47
110#define VT8231_REG_UCH_CONFIG 0x4a
111#define VT8231_REG_TEMP1_CONFIG 0x4b
112#define VT8231_REG_TEMP2_CONFIG 0x4c
113
Guenter Roeck61ba0312012-01-19 11:02:27 -0800114/*
115 * temps 0-5 as numbered in VIA datasheet - see later for mapping to Linux
116 * numbering
117 */
Roger Lucas1de9e372005-11-26 20:20:05 +0100118#define ISTEMP(i, ch_config) ((i) == 0 ? 1 : \
119 ((ch_config) >> ((i)+1)) & 0x01)
120/* voltages 0-5 */
121#define ISVOLT(i, ch_config) ((i) == 5 ? 1 : \
122 !(((ch_config) >> ((i)+2)) & 0x01))
123
124#define DIV_FROM_REG(val) (1 << (val))
125
Guenter Roeck61ba0312012-01-19 11:02:27 -0800126/*
127 * NB The values returned here are NOT temperatures. The calibration curves
128 * for the thermistor curves are board-specific and must go in the
129 * sensors.conf file. Temperature sensors are actually ten bits, but the
130 * VIA datasheet only considers the 8 MSBs obtained from the regtemp[]
131 * register. The temperature value returned should have a magnitude of 3,
132 * so we use the VIA scaling as the "true" scaling and use the remaining 2
133 * LSBs as fractional precision.
134 *
135 * All the on-chip hardware temperature comparisons for the alarms are only
136 * 8-bits wide, and compare against the 8 MSBs of the temperature. The bits
137 * in the registers VT8231_REG_TEMP_LOW01 and VT8231_REG_TEMP_LOW25 are
138 * ignored.
139 */
Roger Lucas1de9e372005-11-26 20:20:05 +0100140
Guenter Roeck61ba0312012-01-19 11:02:27 -0800141/*
142 ****** FAN RPM CONVERSIONS ********
143 * This chip saturates back at 0, not at 255 like many the other chips.
144 * So, 0 means 0 RPM
145 */
Roger Lucas1de9e372005-11-26 20:20:05 +0100146static inline u8 FAN_TO_REG(long rpm, int div)
147{
148 if (rpm == 0)
149 return 0;
150 return SENSORS_LIMIT(1310720 / (rpm * div), 1, 255);
151}
152
153#define FAN_FROM_REG(val, div) ((val) == 0 ? 0 : 1310720 / ((val) * (div)))
154
155struct vt8231_data {
Roger Lucasec5e1a42007-06-12 21:04:08 +0200156 unsigned short addr;
157 const char *name;
158
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100159 struct mutex update_lock;
Tony Jones1beeffe2007-08-20 13:46:20 -0700160 struct device *hwmon_dev;
Roger Lucas1de9e372005-11-26 20:20:05 +0100161 char valid; /* !=0 if following fields are valid */
162 unsigned long last_updated; /* In jiffies */
163
164 u8 in[6]; /* Register value */
165 u8 in_max[6]; /* Register value */
166 u8 in_min[6]; /* Register value */
167 u16 temp[6]; /* Register value 10 bit, right aligned */
168 u8 temp_max[6]; /* Register value */
169 u8 temp_min[6]; /* Register value */
170 u8 fan[2]; /* Register value */
171 u8 fan_min[2]; /* Register value */
172 u8 fan_div[2]; /* Register encoding, shifted right */
173 u16 alarms; /* Register encoding */
174 u8 uch_config;
175};
176
177static struct pci_dev *s_bridge;
Roger Lucasec5e1a42007-06-12 21:04:08 +0200178static int vt8231_probe(struct platform_device *pdev);
Jean Delvared0546122007-07-22 12:09:48 +0200179static int __devexit vt8231_remove(struct platform_device *pdev);
Roger Lucas1de9e372005-11-26 20:20:05 +0100180static struct vt8231_data *vt8231_update_device(struct device *dev);
Roger Lucasec5e1a42007-06-12 21:04:08 +0200181static void vt8231_init_device(struct vt8231_data *data);
Roger Lucas1de9e372005-11-26 20:20:05 +0100182
Roger Lucasec5e1a42007-06-12 21:04:08 +0200183static inline int vt8231_read_value(struct vt8231_data *data, u8 reg)
Roger Lucas1de9e372005-11-26 20:20:05 +0100184{
Roger Lucasec5e1a42007-06-12 21:04:08 +0200185 return inb_p(data->addr + reg);
Roger Lucas1de9e372005-11-26 20:20:05 +0100186}
187
Roger Lucasec5e1a42007-06-12 21:04:08 +0200188static inline void vt8231_write_value(struct vt8231_data *data, u8 reg,
Roger Lucas1de9e372005-11-26 20:20:05 +0100189 u8 value)
190{
Roger Lucasec5e1a42007-06-12 21:04:08 +0200191 outb_p(value, data->addr + reg);
Roger Lucas1de9e372005-11-26 20:20:05 +0100192}
193
194/* following are the sysfs callback functions */
195static ssize_t show_in(struct device *dev, struct device_attribute *attr,
196 char *buf)
197{
198 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
199 int nr = sensor_attr->index;
200 struct vt8231_data *data = vt8231_update_device(dev);
201
202 return sprintf(buf, "%d\n", ((data->in[nr] - 3) * 10000) / 958);
203}
204
205static ssize_t show_in_min(struct device *dev, struct device_attribute *attr,
206 char *buf)
207{
208 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
209 int nr = sensor_attr->index;
210 struct vt8231_data *data = vt8231_update_device(dev);
211
212 return sprintf(buf, "%d\n", ((data->in_min[nr] - 3) * 10000) / 958);
213}
214
215static ssize_t show_in_max(struct device *dev, struct device_attribute *attr,
216 char *buf)
217{
218 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
219 int nr = sensor_attr->index;
220 struct vt8231_data *data = vt8231_update_device(dev);
221
222 return sprintf(buf, "%d\n", (((data->in_max[nr] - 3) * 10000) / 958));
223}
224
225static ssize_t set_in_min(struct device *dev, struct device_attribute *attr,
226 const char *buf, size_t count)
227{
228 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
229 int nr = sensor_attr->index;
Roger Lucasec5e1a42007-06-12 21:04:08 +0200230 struct vt8231_data *data = dev_get_drvdata(dev);
Guenter Roeck65fe5c72012-01-15 07:03:38 -0800231 unsigned long val;
232 int err;
233
234 err = kstrtoul(buf, 10, &val);
235 if (err)
236 return err;
Roger Lucas1de9e372005-11-26 20:20:05 +0100237
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100238 mutex_lock(&data->update_lock);
Roger Lucas1de9e372005-11-26 20:20:05 +0100239 data->in_min[nr] = SENSORS_LIMIT(((val * 958) / 10000) + 3, 0, 255);
Roger Lucasec5e1a42007-06-12 21:04:08 +0200240 vt8231_write_value(data, regvoltmin[nr], data->in_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100241 mutex_unlock(&data->update_lock);
Roger Lucas1de9e372005-11-26 20:20:05 +0100242 return count;
243}
244
245static ssize_t set_in_max(struct device *dev, struct device_attribute *attr,
246 const char *buf, size_t count)
247{
248 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
249 int nr = sensor_attr->index;
Roger Lucasec5e1a42007-06-12 21:04:08 +0200250 struct vt8231_data *data = dev_get_drvdata(dev);
Guenter Roeck65fe5c72012-01-15 07:03:38 -0800251 unsigned long val;
252 int err;
253
254 err = kstrtoul(buf, 10, &val);
255 if (err)
256 return err;
Roger Lucas1de9e372005-11-26 20:20:05 +0100257
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100258 mutex_lock(&data->update_lock);
Roger Lucas1de9e372005-11-26 20:20:05 +0100259 data->in_max[nr] = SENSORS_LIMIT(((val * 958) / 10000) + 3, 0, 255);
Roger Lucasec5e1a42007-06-12 21:04:08 +0200260 vt8231_write_value(data, regvoltmax[nr], data->in_max[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100261 mutex_unlock(&data->update_lock);
Roger Lucas1de9e372005-11-26 20:20:05 +0100262 return count;
263}
264
265/* Special case for input 5 as this has 3.3V scaling built into the chip */
266static ssize_t show_in5(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[5] - 3) * 10000 * 54) / (958 * 34)));
273}
274
275static ssize_t show_in5_min(struct device *dev, struct device_attribute *attr,
276 char *buf)
277{
278 struct vt8231_data *data = vt8231_update_device(dev);
279
280 return sprintf(buf, "%d\n",
281 (((data->in_min[5] - 3) * 10000 * 54) / (958 * 34)));
282}
283
284static ssize_t show_in5_max(struct device *dev, struct device_attribute *attr,
285 char *buf)
286{
287 struct vt8231_data *data = vt8231_update_device(dev);
288
289 return sprintf(buf, "%d\n",
290 (((data->in_max[5] - 3) * 10000 * 54) / (958 * 34)));
291}
292
293static ssize_t set_in5_min(struct device *dev, struct device_attribute *attr,
294 const char *buf, size_t count)
295{
Roger Lucasec5e1a42007-06-12 21:04:08 +0200296 struct vt8231_data *data = dev_get_drvdata(dev);
Guenter Roeck65fe5c72012-01-15 07:03:38 -0800297 unsigned long val;
298 int err;
299
300 err = kstrtoul(buf, 10, &val);
301 if (err)
302 return err;
Roger Lucas1de9e372005-11-26 20:20:05 +0100303
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100304 mutex_lock(&data->update_lock);
Roger Lucas1de9e372005-11-26 20:20:05 +0100305 data->in_min[5] = SENSORS_LIMIT(((val * 958 * 34) / (10000 * 54)) + 3,
306 0, 255);
Roger Lucasec5e1a42007-06-12 21:04:08 +0200307 vt8231_write_value(data, regvoltmin[5], data->in_min[5]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100308 mutex_unlock(&data->update_lock);
Roger Lucas1de9e372005-11-26 20:20:05 +0100309 return count;
310}
311
312static ssize_t set_in5_max(struct device *dev, struct device_attribute *attr,
313 const char *buf, size_t count)
314{
Roger Lucasec5e1a42007-06-12 21:04:08 +0200315 struct vt8231_data *data = dev_get_drvdata(dev);
Guenter Roeck65fe5c72012-01-15 07:03:38 -0800316 unsigned long val;
317 int err;
318
319 err = kstrtoul(buf, 10, &val);
320 if (err)
321 return err;
Roger Lucas1de9e372005-11-26 20:20:05 +0100322
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100323 mutex_lock(&data->update_lock);
Roger Lucas1de9e372005-11-26 20:20:05 +0100324 data->in_max[5] = SENSORS_LIMIT(((val * 958 * 34) / (10000 * 54)) + 3,
325 0, 255);
Roger Lucasec5e1a42007-06-12 21:04:08 +0200326 vt8231_write_value(data, regvoltmax[5], data->in_max[5]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100327 mutex_unlock(&data->update_lock);
Roger Lucas1de9e372005-11-26 20:20:05 +0100328 return count;
329}
330
331#define define_voltage_sysfs(offset) \
332static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \
333 show_in, NULL, offset); \
334static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
335 show_in_min, set_in_min, offset); \
336static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
337 show_in_max, set_in_max, offset)
338
339define_voltage_sysfs(0);
340define_voltage_sysfs(1);
341define_voltage_sysfs(2);
342define_voltage_sysfs(3);
343define_voltage_sysfs(4);
344
345static DEVICE_ATTR(in5_input, S_IRUGO, show_in5, NULL);
346static DEVICE_ATTR(in5_min, S_IRUGO | S_IWUSR, show_in5_min, set_in5_min);
347static DEVICE_ATTR(in5_max, S_IRUGO | S_IWUSR, show_in5_max, set_in5_max);
348
349/* Temperatures */
350static ssize_t show_temp0(struct device *dev, struct device_attribute *attr,
351 char *buf)
352{
353 struct vt8231_data *data = vt8231_update_device(dev);
354 return sprintf(buf, "%d\n", data->temp[0] * 250);
355}
356
357static ssize_t show_temp0_max(struct device *dev, struct device_attribute *attr,
358 char *buf)
359{
360 struct vt8231_data *data = vt8231_update_device(dev);
361 return sprintf(buf, "%d\n", data->temp_max[0] * 1000);
362}
363
364static ssize_t show_temp0_min(struct device *dev, struct device_attribute *attr,
365 char *buf)
366{
367 struct vt8231_data *data = vt8231_update_device(dev);
368 return sprintf(buf, "%d\n", data->temp_min[0] * 1000);
369}
370
371static ssize_t set_temp0_max(struct device *dev, struct device_attribute *attr,
372 const char *buf, size_t count)
373{
Roger Lucasec5e1a42007-06-12 21:04:08 +0200374 struct vt8231_data *data = dev_get_drvdata(dev);
Guenter Roeck65fe5c72012-01-15 07:03:38 -0800375 long val;
376 int err;
377
378 err = kstrtol(buf, 10, &val);
379 if (err)
380 return err;
Roger Lucas1de9e372005-11-26 20:20:05 +0100381
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100382 mutex_lock(&data->update_lock);
Roger Lucas1de9e372005-11-26 20:20:05 +0100383 data->temp_max[0] = SENSORS_LIMIT((val + 500) / 1000, 0, 255);
Roger Lucasec5e1a42007-06-12 21:04:08 +0200384 vt8231_write_value(data, regtempmax[0], data->temp_max[0]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100385 mutex_unlock(&data->update_lock);
Roger Lucas1de9e372005-11-26 20:20:05 +0100386 return count;
387}
388static ssize_t set_temp0_min(struct device *dev, struct device_attribute *attr,
389 const char *buf, size_t count)
390{
Roger Lucasec5e1a42007-06-12 21:04:08 +0200391 struct vt8231_data *data = dev_get_drvdata(dev);
Guenter Roeck65fe5c72012-01-15 07:03:38 -0800392 long val;
393 int err;
394
395 err = kstrtol(buf, 10, &val);
396 if (err)
397 return err;
Roger Lucas1de9e372005-11-26 20:20:05 +0100398
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100399 mutex_lock(&data->update_lock);
Roger Lucas1de9e372005-11-26 20:20:05 +0100400 data->temp_min[0] = SENSORS_LIMIT((val + 500) / 1000, 0, 255);
Roger Lucasec5e1a42007-06-12 21:04:08 +0200401 vt8231_write_value(data, regtempmin[0], data->temp_min[0]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100402 mutex_unlock(&data->update_lock);
Roger Lucas1de9e372005-11-26 20:20:05 +0100403 return count;
404}
405
406static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
407 char *buf)
408{
409 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
410 int nr = sensor_attr->index;
411 struct vt8231_data *data = vt8231_update_device(dev);
412 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr]));
413}
414
415static ssize_t show_temp_max(struct device *dev, struct device_attribute *attr,
416 char *buf)
417{
418 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
419 int nr = sensor_attr->index;
420 struct vt8231_data *data = vt8231_update_device(dev);
421 return sprintf(buf, "%d\n", TEMP_MAXMIN_FROM_REG(data->temp_max[nr]));
422}
423
424static ssize_t show_temp_min(struct device *dev, struct device_attribute *attr,
425 char *buf)
426{
427 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
428 int nr = sensor_attr->index;
429 struct vt8231_data *data = vt8231_update_device(dev);
430 return sprintf(buf, "%d\n", TEMP_MAXMIN_FROM_REG(data->temp_min[nr]));
431}
432
433static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
434 const char *buf, size_t count)
435{
436 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
437 int nr = sensor_attr->index;
Roger Lucasec5e1a42007-06-12 21:04:08 +0200438 struct vt8231_data *data = dev_get_drvdata(dev);
Guenter Roeck65fe5c72012-01-15 07:03:38 -0800439 long val;
440 int err;
441
442 err = kstrtol(buf, 10, &val);
443 if (err)
444 return err;
Roger Lucas1de9e372005-11-26 20:20:05 +0100445
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100446 mutex_lock(&data->update_lock);
Roger Lucas1de9e372005-11-26 20:20:05 +0100447 data->temp_max[nr] = SENSORS_LIMIT(TEMP_MAXMIN_TO_REG(val), 0, 255);
Roger Lucasec5e1a42007-06-12 21:04:08 +0200448 vt8231_write_value(data, regtempmax[nr], data->temp_max[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100449 mutex_unlock(&data->update_lock);
Roger Lucas1de9e372005-11-26 20:20:05 +0100450 return count;
451}
452static ssize_t set_temp_min(struct device *dev, struct device_attribute *attr,
453 const char *buf, size_t count)
454{
455 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
456 int nr = sensor_attr->index;
Roger Lucasec5e1a42007-06-12 21:04:08 +0200457 struct vt8231_data *data = dev_get_drvdata(dev);
Guenter Roeck65fe5c72012-01-15 07:03:38 -0800458 long val;
459 int err;
460
461 err = kstrtol(buf, 10, &val);
462 if (err)
463 return err;
Roger Lucas1de9e372005-11-26 20:20:05 +0100464
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100465 mutex_lock(&data->update_lock);
Roger Lucas1de9e372005-11-26 20:20:05 +0100466 data->temp_min[nr] = SENSORS_LIMIT(TEMP_MAXMIN_TO_REG(val), 0, 255);
Roger Lucasec5e1a42007-06-12 21:04:08 +0200467 vt8231_write_value(data, regtempmin[nr], data->temp_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100468 mutex_unlock(&data->update_lock);
Roger Lucas1de9e372005-11-26 20:20:05 +0100469 return count;
470}
471
Guenter Roeck61ba0312012-01-19 11:02:27 -0800472/*
473 * Note that these map the Linux temperature sensor numbering (1-6) to the VIA
474 * temperature sensor numbering (0-5)
475 */
Roger Lucas1de9e372005-11-26 20:20:05 +0100476#define define_temperature_sysfs(offset) \
477static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
478 show_temp, NULL, offset - 1); \
479static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
480 show_temp_max, set_temp_max, offset - 1); \
Jean Delvaree3efa5a2006-02-05 23:11:16 +0100481static SENSOR_DEVICE_ATTR(temp##offset##_max_hyst, S_IRUGO | S_IWUSR, \
Roger Lucas1de9e372005-11-26 20:20:05 +0100482 show_temp_min, set_temp_min, offset - 1)
483
484static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp0, NULL);
485static DEVICE_ATTR(temp1_max, S_IRUGO | S_IWUSR, show_temp0_max, set_temp0_max);
Guenter Roeck65fe5c72012-01-15 07:03:38 -0800486static DEVICE_ATTR(temp1_max_hyst, S_IRUGO | S_IWUSR, show_temp0_min,
487 set_temp0_min);
Roger Lucas1de9e372005-11-26 20:20:05 +0100488
489define_temperature_sysfs(2);
490define_temperature_sysfs(3);
491define_temperature_sysfs(4);
492define_temperature_sysfs(5);
493define_temperature_sysfs(6);
494
Roger Lucas1de9e372005-11-26 20:20:05 +0100495/* Fans */
496static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
497 char *buf)
498{
499 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
500 int nr = sensor_attr->index;
501 struct vt8231_data *data = vt8231_update_device(dev);
502 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr],
503 DIV_FROM_REG(data->fan_div[nr])));
504}
505
506static ssize_t show_fan_min(struct device *dev, struct device_attribute *attr,
507 char *buf)
508{
509 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
510 int nr = sensor_attr->index;
511 struct vt8231_data *data = vt8231_update_device(dev);
512 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[nr],
513 DIV_FROM_REG(data->fan_div[nr])));
514}
515
516static ssize_t show_fan_div(struct device *dev, struct device_attribute *attr,
517 char *buf)
518{
519 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
520 int nr = sensor_attr->index;
521 struct vt8231_data *data = vt8231_update_device(dev);
522 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]));
523}
524
525static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
526 const char *buf, size_t count)
527{
528 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
529 int nr = sensor_attr->index;
Roger Lucasec5e1a42007-06-12 21:04:08 +0200530 struct vt8231_data *data = dev_get_drvdata(dev);
Guenter Roeck65fe5c72012-01-15 07:03:38 -0800531 unsigned long val;
532 int err;
533
534 err = kstrtoul(buf, 10, &val);
535 if (err)
536 return err;
Roger Lucas1de9e372005-11-26 20:20:05 +0100537
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100538 mutex_lock(&data->update_lock);
Roger Lucas1de9e372005-11-26 20:20:05 +0100539 data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
Roger Lucasec5e1a42007-06-12 21:04:08 +0200540 vt8231_write_value(data, VT8231_REG_FAN_MIN(nr), data->fan_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100541 mutex_unlock(&data->update_lock);
Roger Lucas1de9e372005-11-26 20:20:05 +0100542 return count;
543}
544
545static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
546 const char *buf, size_t count)
547{
Roger Lucasec5e1a42007-06-12 21:04:08 +0200548 struct vt8231_data *data = dev_get_drvdata(dev);
Roger Lucas1de9e372005-11-26 20:20:05 +0100549 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
Guenter Roeck65fe5c72012-01-15 07:03:38 -0800550 unsigned long val;
Roger Lucas1de9e372005-11-26 20:20:05 +0100551 int nr = sensor_attr->index;
Roger Lucasec5e1a42007-06-12 21:04:08 +0200552 int old = vt8231_read_value(data, VT8231_REG_FANDIV);
Roger Lucas1de9e372005-11-26 20:20:05 +0100553 long min = FAN_FROM_REG(data->fan_min[nr],
554 DIV_FROM_REG(data->fan_div[nr]));
Guenter Roeck65fe5c72012-01-15 07:03:38 -0800555 int err;
556
557 err = kstrtoul(buf, 10, &val);
558 if (err)
559 return err;
Roger Lucas1de9e372005-11-26 20:20:05 +0100560
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100561 mutex_lock(&data->update_lock);
Roger Lucas1de9e372005-11-26 20:20:05 +0100562 switch (val) {
Guenter Roeck65fe5c72012-01-15 07:03:38 -0800563 case 1:
564 data->fan_div[nr] = 0;
565 break;
566 case 2:
567 data->fan_div[nr] = 1;
568 break;
569 case 4:
570 data->fan_div[nr] = 2;
571 break;
572 case 8:
573 data->fan_div[nr] = 3;
574 break;
Roger Lucas1de9e372005-11-26 20:20:05 +0100575 default:
Joe Perchesb20ff132007-11-19 17:48:07 -0800576 dev_err(dev, "fan_div value %ld not supported. "
Guenter Roeck65fe5c72012-01-15 07:03:38 -0800577 "Choose one of 1, 2, 4 or 8!\n", val);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100578 mutex_unlock(&data->update_lock);
Roger Lucas1de9e372005-11-26 20:20:05 +0100579 return -EINVAL;
580 }
581
582 /* Correct the fan minimum speed */
583 data->fan_min[nr] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
Roger Lucasec5e1a42007-06-12 21:04:08 +0200584 vt8231_write_value(data, VT8231_REG_FAN_MIN(nr), data->fan_min[nr]);
Roger Lucas1de9e372005-11-26 20:20:05 +0100585
586 old = (old & 0x0f) | (data->fan_div[1] << 6) | (data->fan_div[0] << 4);
Roger Lucasec5e1a42007-06-12 21:04:08 +0200587 vt8231_write_value(data, VT8231_REG_FANDIV, old);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100588 mutex_unlock(&data->update_lock);
Roger Lucas1de9e372005-11-26 20:20:05 +0100589 return count;
590}
591
592
593#define define_fan_sysfs(offset) \
594static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
595 show_fan, NULL, offset - 1); \
596static SENSOR_DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
597 show_fan_div, set_fan_div, offset - 1); \
598static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
599 show_fan_min, set_fan_min, offset - 1)
600
601define_fan_sysfs(1);
602define_fan_sysfs(2);
603
604/* Alarms */
605static ssize_t show_alarms(struct device *dev, struct device_attribute *attr,
606 char *buf)
607{
608 struct vt8231_data *data = vt8231_update_device(dev);
609 return sprintf(buf, "%d\n", data->alarms);
610}
Roger Lucas1de9e372005-11-26 20:20:05 +0100611static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
612
Jean Delvare2d1374c2008-01-06 15:46:02 +0100613static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
614 char *buf)
615{
616 int bitnr = to_sensor_dev_attr(attr)->index;
617 struct vt8231_data *data = vt8231_update_device(dev);
618 return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
619}
620static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 4);
621static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 11);
622static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 0);
623static SENSOR_DEVICE_ATTR(temp4_alarm, S_IRUGO, show_alarm, NULL, 1);
624static SENSOR_DEVICE_ATTR(temp5_alarm, S_IRUGO, show_alarm, NULL, 3);
625static SENSOR_DEVICE_ATTR(temp6_alarm, S_IRUGO, show_alarm, NULL, 8);
626static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 11);
627static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 0);
628static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 1);
629static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3);
630static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 8);
631static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 2);
632static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 6);
633static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 7);
634
Roger Lucasec5e1a42007-06-12 21:04:08 +0200635static ssize_t show_name(struct device *dev, struct device_attribute
636 *devattr, char *buf)
637{
638 struct vt8231_data *data = dev_get_drvdata(dev);
639 return sprintf(buf, "%s\n", data->name);
640}
641static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
642
Jean Delvare2d1374c2008-01-06 15:46:02 +0100643static struct attribute *vt8231_attributes_temps[6][5] = {
Roger Lucascbeeb5b2006-09-24 21:21:46 +0200644 {
645 &dev_attr_temp1_input.attr,
646 &dev_attr_temp1_max_hyst.attr,
647 &dev_attr_temp1_max.attr,
Jean Delvare2d1374c2008-01-06 15:46:02 +0100648 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
Roger Lucascbeeb5b2006-09-24 21:21:46 +0200649 NULL
650 }, {
651 &sensor_dev_attr_temp2_input.dev_attr.attr,
652 &sensor_dev_attr_temp2_max_hyst.dev_attr.attr,
653 &sensor_dev_attr_temp2_max.dev_attr.attr,
Jean Delvare2d1374c2008-01-06 15:46:02 +0100654 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
Roger Lucascbeeb5b2006-09-24 21:21:46 +0200655 NULL
656 }, {
657 &sensor_dev_attr_temp3_input.dev_attr.attr,
658 &sensor_dev_attr_temp3_max_hyst.dev_attr.attr,
659 &sensor_dev_attr_temp3_max.dev_attr.attr,
Jean Delvare2d1374c2008-01-06 15:46:02 +0100660 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
Roger Lucascbeeb5b2006-09-24 21:21:46 +0200661 NULL
662 }, {
663 &sensor_dev_attr_temp4_input.dev_attr.attr,
664 &sensor_dev_attr_temp4_max_hyst.dev_attr.attr,
665 &sensor_dev_attr_temp4_max.dev_attr.attr,
Jean Delvare2d1374c2008-01-06 15:46:02 +0100666 &sensor_dev_attr_temp4_alarm.dev_attr.attr,
Roger Lucascbeeb5b2006-09-24 21:21:46 +0200667 NULL
668 }, {
669 &sensor_dev_attr_temp5_input.dev_attr.attr,
670 &sensor_dev_attr_temp5_max_hyst.dev_attr.attr,
671 &sensor_dev_attr_temp5_max.dev_attr.attr,
Jean Delvare2d1374c2008-01-06 15:46:02 +0100672 &sensor_dev_attr_temp5_alarm.dev_attr.attr,
Roger Lucascbeeb5b2006-09-24 21:21:46 +0200673 NULL
674 }, {
675 &sensor_dev_attr_temp6_input.dev_attr.attr,
676 &sensor_dev_attr_temp6_max_hyst.dev_attr.attr,
677 &sensor_dev_attr_temp6_max.dev_attr.attr,
Jean Delvare2d1374c2008-01-06 15:46:02 +0100678 &sensor_dev_attr_temp6_alarm.dev_attr.attr,
Roger Lucascbeeb5b2006-09-24 21:21:46 +0200679 NULL
680 }
681};
682
683static const struct attribute_group vt8231_group_temps[6] = {
684 { .attrs = vt8231_attributes_temps[0] },
685 { .attrs = vt8231_attributes_temps[1] },
686 { .attrs = vt8231_attributes_temps[2] },
687 { .attrs = vt8231_attributes_temps[3] },
688 { .attrs = vt8231_attributes_temps[4] },
689 { .attrs = vt8231_attributes_temps[5] },
690};
691
Jean Delvare2d1374c2008-01-06 15:46:02 +0100692static struct attribute *vt8231_attributes_volts[6][5] = {
Roger Lucascbeeb5b2006-09-24 21:21:46 +0200693 {
694 &sensor_dev_attr_in0_input.dev_attr.attr,
695 &sensor_dev_attr_in0_min.dev_attr.attr,
696 &sensor_dev_attr_in0_max.dev_attr.attr,
Jean Delvare2d1374c2008-01-06 15:46:02 +0100697 &sensor_dev_attr_in0_alarm.dev_attr.attr,
Roger Lucascbeeb5b2006-09-24 21:21:46 +0200698 NULL
699 }, {
700 &sensor_dev_attr_in1_input.dev_attr.attr,
701 &sensor_dev_attr_in1_min.dev_attr.attr,
702 &sensor_dev_attr_in1_max.dev_attr.attr,
Jean Delvare2d1374c2008-01-06 15:46:02 +0100703 &sensor_dev_attr_in1_alarm.dev_attr.attr,
Roger Lucascbeeb5b2006-09-24 21:21:46 +0200704 NULL
705 }, {
706 &sensor_dev_attr_in2_input.dev_attr.attr,
707 &sensor_dev_attr_in2_min.dev_attr.attr,
708 &sensor_dev_attr_in2_max.dev_attr.attr,
Jean Delvare2d1374c2008-01-06 15:46:02 +0100709 &sensor_dev_attr_in2_alarm.dev_attr.attr,
Roger Lucascbeeb5b2006-09-24 21:21:46 +0200710 NULL
711 }, {
712 &sensor_dev_attr_in3_input.dev_attr.attr,
713 &sensor_dev_attr_in3_min.dev_attr.attr,
714 &sensor_dev_attr_in3_max.dev_attr.attr,
Jean Delvare2d1374c2008-01-06 15:46:02 +0100715 &sensor_dev_attr_in3_alarm.dev_attr.attr,
Roger Lucascbeeb5b2006-09-24 21:21:46 +0200716 NULL
717 }, {
718 &sensor_dev_attr_in4_input.dev_attr.attr,
719 &sensor_dev_attr_in4_min.dev_attr.attr,
720 &sensor_dev_attr_in4_max.dev_attr.attr,
Jean Delvare2d1374c2008-01-06 15:46:02 +0100721 &sensor_dev_attr_in4_alarm.dev_attr.attr,
Roger Lucascbeeb5b2006-09-24 21:21:46 +0200722 NULL
723 }, {
724 &dev_attr_in5_input.attr,
725 &dev_attr_in5_min.attr,
726 &dev_attr_in5_max.attr,
Jean Delvare2d1374c2008-01-06 15:46:02 +0100727 &sensor_dev_attr_in5_alarm.dev_attr.attr,
Roger Lucascbeeb5b2006-09-24 21:21:46 +0200728 NULL
729 }
730};
731
732static const struct attribute_group vt8231_group_volts[6] = {
733 { .attrs = vt8231_attributes_volts[0] },
734 { .attrs = vt8231_attributes_volts[1] },
735 { .attrs = vt8231_attributes_volts[2] },
736 { .attrs = vt8231_attributes_volts[3] },
737 { .attrs = vt8231_attributes_volts[4] },
738 { .attrs = vt8231_attributes_volts[5] },
739};
740
741static struct attribute *vt8231_attributes[] = {
742 &sensor_dev_attr_fan1_input.dev_attr.attr,
743 &sensor_dev_attr_fan2_input.dev_attr.attr,
744 &sensor_dev_attr_fan1_min.dev_attr.attr,
745 &sensor_dev_attr_fan2_min.dev_attr.attr,
746 &sensor_dev_attr_fan1_div.dev_attr.attr,
747 &sensor_dev_attr_fan2_div.dev_attr.attr,
Jean Delvare2d1374c2008-01-06 15:46:02 +0100748 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
749 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
Roger Lucascbeeb5b2006-09-24 21:21:46 +0200750 &dev_attr_alarms.attr,
Roger Lucasec5e1a42007-06-12 21:04:08 +0200751 &dev_attr_name.attr,
Roger Lucascbeeb5b2006-09-24 21:21:46 +0200752 NULL
753};
754
755static const struct attribute_group vt8231_group = {
756 .attrs = vt8231_attributes,
757};
758
Roger Lucasec5e1a42007-06-12 21:04:08 +0200759static struct platform_driver vt8231_driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100760 .driver = {
Jean Delvare87218842006-09-03 22:36:14 +0200761 .owner = THIS_MODULE,
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100762 .name = "vt8231",
763 },
Roger Lucasec5e1a42007-06-12 21:04:08 +0200764 .probe = vt8231_probe,
Bill Pemberton9e5e9b72012-11-19 13:21:20 -0500765 .remove = vt8231_remove,
Roger Lucas1de9e372005-11-26 20:20:05 +0100766};
767
Frans Meulenbroeks600151b2012-01-05 19:50:17 +0100768static DEFINE_PCI_DEVICE_TABLE(vt8231_pci_ids) = {
Roger Lucas1de9e372005-11-26 20:20:05 +0100769 { PCI_DEVICE(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8231_4) },
770 { 0, }
771};
772
773MODULE_DEVICE_TABLE(pci, vt8231_pci_ids);
774
Bill Pemberton6c931ae2012-11-19 13:22:35 -0500775static int vt8231_pci_probe(struct pci_dev *dev,
Guenter Roeck65fe5c72012-01-15 07:03:38 -0800776 const struct pci_device_id *id);
Roger Lucas1de9e372005-11-26 20:20:05 +0100777
778static struct pci_driver vt8231_pci_driver = {
779 .name = "vt8231",
780 .id_table = vt8231_pci_ids,
781 .probe = vt8231_pci_probe,
782};
783
Mark M. Hoffmana022fef2007-10-14 15:00:24 -0400784static int vt8231_probe(struct platform_device *pdev)
Roger Lucas1de9e372005-11-26 20:20:05 +0100785{
Roger Lucasec5e1a42007-06-12 21:04:08 +0200786 struct resource *res;
Roger Lucas1de9e372005-11-26 20:20:05 +0100787 struct vt8231_data *data;
788 int err = 0, i;
Roger Lucas1de9e372005-11-26 20:20:05 +0100789
790 /* Reserve the ISA region */
Roger Lucasec5e1a42007-06-12 21:04:08 +0200791 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
Guenter Roeck7711f1b2012-06-02 11:35:55 -0700792 if (!devm_request_region(&pdev->dev, res->start, VT8231_EXTENT,
793 vt8231_driver.driver.name)) {
Roger Lucasec5e1a42007-06-12 21:04:08 +0200794 dev_err(&pdev->dev, "Region 0x%lx-0x%lx already in use!\n",
795 (unsigned long)res->start, (unsigned long)res->end);
Roger Lucas1de9e372005-11-26 20:20:05 +0100796 return -ENODEV;
797 }
798
Guenter Roeck7711f1b2012-06-02 11:35:55 -0700799 data = devm_kzalloc(&pdev->dev, sizeof(struct vt8231_data), GFP_KERNEL);
800 if (!data)
801 return -ENOMEM;
Roger Lucas1de9e372005-11-26 20:20:05 +0100802
Roger Lucasec5e1a42007-06-12 21:04:08 +0200803 platform_set_drvdata(pdev, data);
804 data->addr = res->start;
805 data->name = "vt8231";
Roger Lucas1de9e372005-11-26 20:20:05 +0100806
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100807 mutex_init(&data->update_lock);
Roger Lucasec5e1a42007-06-12 21:04:08 +0200808 vt8231_init_device(data);
Roger Lucas1de9e372005-11-26 20:20:05 +0100809
810 /* Register sysfs hooks */
Guenter Roeck65fe5c72012-01-15 07:03:38 -0800811 err = sysfs_create_group(&pdev->dev.kobj, &vt8231_group);
812 if (err)
Guenter Roeck7711f1b2012-06-02 11:35:55 -0700813 return err;
Roger Lucas1de9e372005-11-26 20:20:05 +0100814
815 /* Must update device information to find out the config field */
Roger Lucasec5e1a42007-06-12 21:04:08 +0200816 data->uch_config = vt8231_read_value(data, VT8231_REG_UCH_CONFIG);
Roger Lucas1de9e372005-11-26 20:20:05 +0100817
Roger Lucascbeeb5b2006-09-24 21:21:46 +0200818 for (i = 0; i < ARRAY_SIZE(vt8231_group_temps); i++) {
Roger Lucas1de9e372005-11-26 20:20:05 +0100819 if (ISTEMP(i, data->uch_config)) {
Guenter Roeck65fe5c72012-01-15 07:03:38 -0800820 err = sysfs_create_group(&pdev->dev.kobj,
821 &vt8231_group_temps[i]);
822 if (err)
Roger Lucascbeeb5b2006-09-24 21:21:46 +0200823 goto exit_remove_files;
Roger Lucas1de9e372005-11-26 20:20:05 +0100824 }
825 }
826
Roger Lucascbeeb5b2006-09-24 21:21:46 +0200827 for (i = 0; i < ARRAY_SIZE(vt8231_group_volts); i++) {
Roger Lucas1de9e372005-11-26 20:20:05 +0100828 if (ISVOLT(i, data->uch_config)) {
Guenter Roeck65fe5c72012-01-15 07:03:38 -0800829 err = sysfs_create_group(&pdev->dev.kobj,
830 &vt8231_group_volts[i]);
831 if (err)
Roger Lucascbeeb5b2006-09-24 21:21:46 +0200832 goto exit_remove_files;
Roger Lucas1de9e372005-11-26 20:20:05 +0100833 }
834 }
835
Tony Jones1beeffe2007-08-20 13:46:20 -0700836 data->hwmon_dev = hwmon_device_register(&pdev->dev);
837 if (IS_ERR(data->hwmon_dev)) {
838 err = PTR_ERR(data->hwmon_dev);
Roger Lucascbeeb5b2006-09-24 21:21:46 +0200839 goto exit_remove_files;
840 }
Roger Lucas1de9e372005-11-26 20:20:05 +0100841 return 0;
842
Roger Lucascbeeb5b2006-09-24 21:21:46 +0200843exit_remove_files:
844 for (i = 0; i < ARRAY_SIZE(vt8231_group_volts); i++)
Roger Lucasec5e1a42007-06-12 21:04:08 +0200845 sysfs_remove_group(&pdev->dev.kobj, &vt8231_group_volts[i]);
Roger Lucascbeeb5b2006-09-24 21:21:46 +0200846
847 for (i = 0; i < ARRAY_SIZE(vt8231_group_temps); i++)
Roger Lucasec5e1a42007-06-12 21:04:08 +0200848 sysfs_remove_group(&pdev->dev.kobj, &vt8231_group_temps[i]);
Roger Lucascbeeb5b2006-09-24 21:21:46 +0200849
Roger Lucasec5e1a42007-06-12 21:04:08 +0200850 sysfs_remove_group(&pdev->dev.kobj, &vt8231_group);
Roger Lucas1de9e372005-11-26 20:20:05 +0100851 return err;
852}
853
Jean Delvared0546122007-07-22 12:09:48 +0200854static int __devexit vt8231_remove(struct platform_device *pdev)
Roger Lucas1de9e372005-11-26 20:20:05 +0100855{
Roger Lucasec5e1a42007-06-12 21:04:08 +0200856 struct vt8231_data *data = platform_get_drvdata(pdev);
857 int i;
Roger Lucas1de9e372005-11-26 20:20:05 +0100858
Tony Jones1beeffe2007-08-20 13:46:20 -0700859 hwmon_device_unregister(data->hwmon_dev);
Roger Lucas1de9e372005-11-26 20:20:05 +0100860
Roger Lucascbeeb5b2006-09-24 21:21:46 +0200861 for (i = 0; i < ARRAY_SIZE(vt8231_group_volts); i++)
Roger Lucasec5e1a42007-06-12 21:04:08 +0200862 sysfs_remove_group(&pdev->dev.kobj, &vt8231_group_volts[i]);
Roger Lucascbeeb5b2006-09-24 21:21:46 +0200863
864 for (i = 0; i < ARRAY_SIZE(vt8231_group_temps); i++)
Roger Lucasec5e1a42007-06-12 21:04:08 +0200865 sysfs_remove_group(&pdev->dev.kobj, &vt8231_group_temps[i]);
Roger Lucascbeeb5b2006-09-24 21:21:46 +0200866
Roger Lucasec5e1a42007-06-12 21:04:08 +0200867 sysfs_remove_group(&pdev->dev.kobj, &vt8231_group);
Roger Lucascbeeb5b2006-09-24 21:21:46 +0200868
Roger Lucas1de9e372005-11-26 20:20:05 +0100869 return 0;
870}
871
Roger Lucasec5e1a42007-06-12 21:04:08 +0200872static void vt8231_init_device(struct vt8231_data *data)
Roger Lucas1de9e372005-11-26 20:20:05 +0100873{
Roger Lucasec5e1a42007-06-12 21:04:08 +0200874 vt8231_write_value(data, VT8231_REG_TEMP1_CONFIG, 0);
875 vt8231_write_value(data, VT8231_REG_TEMP2_CONFIG, 0);
Roger Lucas1de9e372005-11-26 20:20:05 +0100876}
877
878static struct vt8231_data *vt8231_update_device(struct device *dev)
879{
Roger Lucasec5e1a42007-06-12 21:04:08 +0200880 struct vt8231_data *data = dev_get_drvdata(dev);
Roger Lucas1de9e372005-11-26 20:20:05 +0100881 int i;
882 u16 low;
883
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100884 mutex_lock(&data->update_lock);
Roger Lucas1de9e372005-11-26 20:20:05 +0100885
886 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
887 || !data->valid) {
888 for (i = 0; i < 6; i++) {
889 if (ISVOLT(i, data->uch_config)) {
Roger Lucasec5e1a42007-06-12 21:04:08 +0200890 data->in[i] = vt8231_read_value(data,
Roger Lucas1de9e372005-11-26 20:20:05 +0100891 regvolt[i]);
Roger Lucasec5e1a42007-06-12 21:04:08 +0200892 data->in_min[i] = vt8231_read_value(data,
Roger Lucas1de9e372005-11-26 20:20:05 +0100893 regvoltmin[i]);
Roger Lucasec5e1a42007-06-12 21:04:08 +0200894 data->in_max[i] = vt8231_read_value(data,
Roger Lucas1de9e372005-11-26 20:20:05 +0100895 regvoltmax[i]);
896 }
897 }
898 for (i = 0; i < 2; i++) {
Roger Lucasec5e1a42007-06-12 21:04:08 +0200899 data->fan[i] = vt8231_read_value(data,
Roger Lucas1de9e372005-11-26 20:20:05 +0100900 VT8231_REG_FAN(i));
Roger Lucasec5e1a42007-06-12 21:04:08 +0200901 data->fan_min[i] = vt8231_read_value(data,
Roger Lucas1de9e372005-11-26 20:20:05 +0100902 VT8231_REG_FAN_MIN(i));
903 }
904
Roger Lucasec5e1a42007-06-12 21:04:08 +0200905 low = vt8231_read_value(data, VT8231_REG_TEMP_LOW01);
Roger Lucas1de9e372005-11-26 20:20:05 +0100906 low = (low >> 6) | ((low & 0x30) >> 2)
Roger Lucasec5e1a42007-06-12 21:04:08 +0200907 | (vt8231_read_value(data, VT8231_REG_TEMP_LOW25) << 4);
Roger Lucas1de9e372005-11-26 20:20:05 +0100908 for (i = 0; i < 6; i++) {
909 if (ISTEMP(i, data->uch_config)) {
Roger Lucasec5e1a42007-06-12 21:04:08 +0200910 data->temp[i] = (vt8231_read_value(data,
Roger Lucas1de9e372005-11-26 20:20:05 +0100911 regtemp[i]) << 2)
912 | ((low >> (2 * i)) & 0x03);
Roger Lucasec5e1a42007-06-12 21:04:08 +0200913 data->temp_max[i] = vt8231_read_value(data,
Roger Lucas1de9e372005-11-26 20:20:05 +0100914 regtempmax[i]);
Roger Lucasec5e1a42007-06-12 21:04:08 +0200915 data->temp_min[i] = vt8231_read_value(data,
Roger Lucas1de9e372005-11-26 20:20:05 +0100916 regtempmin[i]);
917 }
918 }
919
Roger Lucasec5e1a42007-06-12 21:04:08 +0200920 i = vt8231_read_value(data, VT8231_REG_FANDIV);
Roger Lucas1de9e372005-11-26 20:20:05 +0100921 data->fan_div[0] = (i >> 4) & 0x03;
922 data->fan_div[1] = i >> 6;
Roger Lucasec5e1a42007-06-12 21:04:08 +0200923 data->alarms = vt8231_read_value(data, VT8231_REG_ALARM1) |
924 (vt8231_read_value(data, VT8231_REG_ALARM2) << 8);
Roger Lucas1de9e372005-11-26 20:20:05 +0100925
926 /* Set alarm flags correctly */
Guenter Roeck65fe5c72012-01-15 07:03:38 -0800927 if (!data->fan[0] && data->fan_min[0])
Roger Lucas1de9e372005-11-26 20:20:05 +0100928 data->alarms |= 0x40;
Guenter Roeck65fe5c72012-01-15 07:03:38 -0800929 else if (data->fan[0] && !data->fan_min[0])
Roger Lucas1de9e372005-11-26 20:20:05 +0100930 data->alarms &= ~0x40;
Roger Lucas1de9e372005-11-26 20:20:05 +0100931
Guenter Roeck65fe5c72012-01-15 07:03:38 -0800932 if (!data->fan[1] && data->fan_min[1])
Roger Lucas1de9e372005-11-26 20:20:05 +0100933 data->alarms |= 0x80;
Guenter Roeck65fe5c72012-01-15 07:03:38 -0800934 else if (data->fan[1] && !data->fan_min[1])
Roger Lucas1de9e372005-11-26 20:20:05 +0100935 data->alarms &= ~0x80;
Roger Lucas1de9e372005-11-26 20:20:05 +0100936
937 data->last_updated = jiffies;
938 data->valid = 1;
939 }
940
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100941 mutex_unlock(&data->update_lock);
Roger Lucas1de9e372005-11-26 20:20:05 +0100942
943 return data;
944}
945
Bill Pemberton6c931ae2012-11-19 13:22:35 -0500946static int vt8231_device_add(unsigned short address)
Roger Lucasec5e1a42007-06-12 21:04:08 +0200947{
948 struct resource res = {
949 .start = address,
950 .end = address + VT8231_EXTENT - 1,
951 .name = "vt8231",
952 .flags = IORESOURCE_IO,
953 };
954 int err;
955
Jean Delvareb9acb642009-01-07 16:37:35 +0100956 err = acpi_check_resource_conflict(&res);
957 if (err)
958 goto exit;
959
Roger Lucasec5e1a42007-06-12 21:04:08 +0200960 pdev = platform_device_alloc("vt8231", address);
961 if (!pdev) {
962 err = -ENOMEM;
Joe Perches9d72be02010-10-20 06:51:53 +0000963 pr_err("Device allocation failed\n");
Roger Lucasec5e1a42007-06-12 21:04:08 +0200964 goto exit;
965 }
966
967 err = platform_device_add_resources(pdev, &res, 1);
968 if (err) {
Joe Perches9d72be02010-10-20 06:51:53 +0000969 pr_err("Device resource addition failed (%d)\n", err);
Roger Lucasec5e1a42007-06-12 21:04:08 +0200970 goto exit_device_put;
971 }
972
973 err = platform_device_add(pdev);
974 if (err) {
Joe Perches9d72be02010-10-20 06:51:53 +0000975 pr_err("Device addition failed (%d)\n", err);
Roger Lucasec5e1a42007-06-12 21:04:08 +0200976 goto exit_device_put;
977 }
978
979 return 0;
980
981exit_device_put:
982 platform_device_put(pdev);
983exit:
984 return err;
985}
986
Bill Pemberton6c931ae2012-11-19 13:22:35 -0500987static int vt8231_pci_probe(struct pci_dev *dev,
Roger Lucas1de9e372005-11-26 20:20:05 +0100988 const struct pci_device_id *id)
989{
Roger Lucasec5e1a42007-06-12 21:04:08 +0200990 u16 address, val;
991 if (force_addr) {
992 address = force_addr & 0xff00;
993 dev_warn(&dev->dev, "Forcing ISA address 0x%x\n",
994 address);
995
996 if (PCIBIOS_SUCCESSFUL !=
997 pci_write_config_word(dev, VT8231_BASE_REG, address | 1))
998 return -ENODEV;
999 }
Roger Lucas1de9e372005-11-26 20:20:05 +01001000
1001 if (PCIBIOS_SUCCESSFUL != pci_read_config_word(dev, VT8231_BASE_REG,
1002 &val))
1003 return -ENODEV;
1004
Roger Lucasec5e1a42007-06-12 21:04:08 +02001005 address = val & ~(VT8231_EXTENT - 1);
1006 if (address == 0) {
Joe Perches4cae7872010-03-05 13:43:56 -08001007 dev_err(&dev->dev, "base address not set - upgrade BIOS or use force_addr=0xaddr\n");
Roger Lucas1de9e372005-11-26 20:20:05 +01001008 return -ENODEV;
1009 }
1010
Roger Lucasec5e1a42007-06-12 21:04:08 +02001011 if (PCIBIOS_SUCCESSFUL != pci_read_config_word(dev, VT8231_ENABLE_REG,
1012 &val))
1013 return -ENODEV;
Roger Lucas1de9e372005-11-26 20:20:05 +01001014
Roger Lucasec5e1a42007-06-12 21:04:08 +02001015 if (!(val & 0x0001)) {
1016 dev_warn(&dev->dev, "enabling sensors\n");
1017 if (PCIBIOS_SUCCESSFUL !=
1018 pci_write_config_word(dev, VT8231_ENABLE_REG,
1019 val | 0x0001))
1020 return -ENODEV;
Roger Lucas1de9e372005-11-26 20:20:05 +01001021 }
1022
Roger Lucasec5e1a42007-06-12 21:04:08 +02001023 if (platform_driver_register(&vt8231_driver))
1024 goto exit;
1025
1026 /* Sets global pdev as a side effect */
1027 if (vt8231_device_add(address))
1028 goto exit_unregister;
1029
Guenter Roeck61ba0312012-01-19 11:02:27 -08001030 /*
1031 * Always return failure here. This is to allow other drivers to bind
Roger Lucas1de9e372005-11-26 20:20:05 +01001032 * to this pci device. We don't really want to have control over the
1033 * pci device, we only wanted to read as few register values from it.
1034 */
Roger Lucasec5e1a42007-06-12 21:04:08 +02001035
Guenter Roeck61ba0312012-01-19 11:02:27 -08001036 /*
1037 * We do, however, mark ourselves as using the PCI device to stop it
1038 * getting unloaded.
1039 */
Roger Lucasec5e1a42007-06-12 21:04:08 +02001040 s_bridge = pci_dev_get(dev);
1041 return -ENODEV;
1042
1043exit_unregister:
1044 platform_driver_unregister(&vt8231_driver);
1045exit:
Roger Lucas1de9e372005-11-26 20:20:05 +01001046 return -ENODEV;
1047}
1048
1049static int __init sm_vt8231_init(void)
1050{
Richard Knutsson93b47682005-11-30 01:00:35 +01001051 return pci_register_driver(&vt8231_pci_driver);
Roger Lucas1de9e372005-11-26 20:20:05 +01001052}
1053
1054static void __exit sm_vt8231_exit(void)
1055{
1056 pci_unregister_driver(&vt8231_pci_driver);
1057 if (s_bridge != NULL) {
Roger Lucasec5e1a42007-06-12 21:04:08 +02001058 platform_device_unregister(pdev);
1059 platform_driver_unregister(&vt8231_driver);
Roger Lucas1de9e372005-11-26 20:20:05 +01001060 pci_dev_put(s_bridge);
1061 s_bridge = NULL;
1062 }
1063}
1064
Roger Lucasaf865762008-02-13 07:52:06 -05001065MODULE_AUTHOR("Roger Lucas <vt8231@hiddenengine.co.uk>");
Roger Lucas1de9e372005-11-26 20:20:05 +01001066MODULE_DESCRIPTION("VT8231 sensors");
1067MODULE_LICENSE("GPL");
1068
1069module_init(sm_vt8231_init);
1070module_exit(sm_vt8231_exit);