blob: 2335d440f72d7b2e0d41c9633c1ff2cfc72534b4 [file] [log] [blame]
Thomas Gleixner74ba9202019-05-20 09:19:02 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Roger Lucas1de9e372005-11-26 20:20:05 +01002/*
Guenter Roeck61ba0312012-01-19 11:02:27 -08003 * vt8231.c - Part of lm_sensors, Linux kernel modules
4 * for hardware monitoring
5 *
6 * Copyright (c) 2005 Roger Lucas <vt8231@hiddenengine.co.uk>
7 * Copyright (c) 2002 Mark D. Studebaker <mdsxyz123@yahoo.com>
8 * Aaron M. Marsh <amarsh@sdf.lonestar.org>
Guenter Roeck61ba0312012-01-19 11:02:27 -08009 */
Roger Lucas1de9e372005-11-26 20:20:05 +010010
Guenter Roeck61ba0312012-01-19 11:02:27 -080011/*
12 * Supports VIA VT8231 South Bridge embedded sensors
13 */
Roger Lucas1de9e372005-11-26 20:20:05 +010014
Joe Perches9d72be02010-10-20 06:51:53 +000015#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
16
Roger Lucas1de9e372005-11-26 20:20:05 +010017#include <linux/module.h>
18#include <linux/init.h>
19#include <linux/slab.h>
20#include <linux/pci.h>
21#include <linux/jiffies.h>
Roger Lucasec5e1a42007-06-12 21:04:08 +020022#include <linux/platform_device.h>
Roger Lucas1de9e372005-11-26 20:20:05 +010023#include <linux/hwmon.h>
24#include <linux/hwmon-sysfs.h>
25#include <linux/hwmon-vid.h>
26#include <linux/err.h>
Ingo Molnar9a61bf62006-01-18 23:19:26 +010027#include <linux/mutex.h>
Jean Delvareb9acb642009-01-07 16:37:35 +010028#include <linux/acpi.h>
H Hartley Sweeten6055fae2009-09-15 17:18:13 +020029#include <linux/io.h>
Roger Lucas1de9e372005-11-26 20:20:05 +010030
31static int force_addr;
32module_param(force_addr, int, 0);
33MODULE_PARM_DESC(force_addr, "Initialize the base address of the sensors");
34
Roger Lucasec5e1a42007-06-12 21:04:08 +020035static struct platform_device *pdev;
Roger Lucas1de9e372005-11-26 20:20:05 +010036
37#define VT8231_EXTENT 0x80
38#define VT8231_BASE_REG 0x70
39#define VT8231_ENABLE_REG 0x74
40
Guenter Roeck61ba0312012-01-19 11:02:27 -080041/*
42 * The VT8231 registers
43 *
44 * The reset value for the input channel configuration is used (Reg 0x4A=0x07)
45 * which sets the selected inputs marked with '*' below if multiple options are
46 * possible:
47 *
48 * Voltage Mode Temperature Mode
49 * Sensor Linux Id Linux Id VIA Id
50 * -------- -------- -------- ------
51 * CPU Diode N/A temp1 0
52 * UIC1 in0 temp2 * 1
53 * UIC2 in1 * temp3 2
54 * UIC3 in2 * temp4 3
55 * UIC4 in3 * temp5 4
56 * UIC5 in4 * temp6 5
57 * 3.3V in5 N/A
58 *
59 * Note that the BIOS may set the configuration register to a different value
60 * to match the motherboard configuration.
61 */
Roger Lucas1de9e372005-11-26 20:20:05 +010062
63/* fans numbered 0-1 */
64#define VT8231_REG_FAN_MIN(nr) (0x3b + (nr))
65#define VT8231_REG_FAN(nr) (0x29 + (nr))
66
67/* Voltage inputs numbered 0-5 */
68
69static const u8 regvolt[] = { 0x21, 0x22, 0x23, 0x24, 0x25, 0x26 };
70static const u8 regvoltmax[] = { 0x3d, 0x2b, 0x2d, 0x2f, 0x31, 0x33 };
71static const u8 regvoltmin[] = { 0x3e, 0x2c, 0x2e, 0x30, 0x32, 0x34 };
72
Guenter Roeck61ba0312012-01-19 11:02:27 -080073/*
74 * Temperatures are numbered 1-6 according to the Linux kernel specification.
75 *
76 * In the VIA datasheet, however, the temperatures are numbered from zero.
77 * Since it is important that this driver can easily be compared to the VIA
78 * datasheet, we will use the VIA numbering within this driver and map the
79 * kernel sysfs device name to the VIA number in the sysfs callback.
80 */
Roger Lucas1de9e372005-11-26 20:20:05 +010081
82#define VT8231_REG_TEMP_LOW01 0x49
83#define VT8231_REG_TEMP_LOW25 0x4d
84
85static const u8 regtemp[] = { 0x1f, 0x21, 0x22, 0x23, 0x24, 0x25 };
86static const u8 regtempmax[] = { 0x39, 0x3d, 0x2b, 0x2d, 0x2f, 0x31 };
87static const u8 regtempmin[] = { 0x3a, 0x3e, 0x2c, 0x2e, 0x30, 0x32 };
88
89#define TEMP_FROM_REG(reg) (((253 * 4 - (reg)) * 550 + 105) / 210)
90#define TEMP_MAXMIN_FROM_REG(reg) (((253 - (reg)) * 2200 + 105) / 210)
91#define TEMP_MAXMIN_TO_REG(val) (253 - ((val) * 210 + 1100) / 2200)
92
93#define VT8231_REG_CONFIG 0x40
94#define VT8231_REG_ALARM1 0x41
95#define VT8231_REG_ALARM2 0x42
96#define VT8231_REG_FANDIV 0x47
97#define VT8231_REG_UCH_CONFIG 0x4a
98#define VT8231_REG_TEMP1_CONFIG 0x4b
99#define VT8231_REG_TEMP2_CONFIG 0x4c
100
Guenter Roeck61ba0312012-01-19 11:02:27 -0800101/*
102 * temps 0-5 as numbered in VIA datasheet - see later for mapping to Linux
103 * numbering
104 */
Roger Lucas1de9e372005-11-26 20:20:05 +0100105#define ISTEMP(i, ch_config) ((i) == 0 ? 1 : \
106 ((ch_config) >> ((i)+1)) & 0x01)
107/* voltages 0-5 */
108#define ISVOLT(i, ch_config) ((i) == 5 ? 1 : \
109 !(((ch_config) >> ((i)+2)) & 0x01))
110
111#define DIV_FROM_REG(val) (1 << (val))
112
Guenter Roeck61ba0312012-01-19 11:02:27 -0800113/*
114 * NB The values returned here are NOT temperatures. The calibration curves
115 * for the thermistor curves are board-specific and must go in the
116 * sensors.conf file. Temperature sensors are actually ten bits, but the
117 * VIA datasheet only considers the 8 MSBs obtained from the regtemp[]
118 * register. The temperature value returned should have a magnitude of 3,
119 * so we use the VIA scaling as the "true" scaling and use the remaining 2
120 * LSBs as fractional precision.
121 *
122 * All the on-chip hardware temperature comparisons for the alarms are only
123 * 8-bits wide, and compare against the 8 MSBs of the temperature. The bits
124 * in the registers VT8231_REG_TEMP_LOW01 and VT8231_REG_TEMP_LOW25 are
125 * ignored.
126 */
Roger Lucas1de9e372005-11-26 20:20:05 +0100127
Guenter Roeck61ba0312012-01-19 11:02:27 -0800128/*
129 ****** FAN RPM CONVERSIONS ********
130 * This chip saturates back at 0, not at 255 like many the other chips.
131 * So, 0 means 0 RPM
132 */
Roger Lucas1de9e372005-11-26 20:20:05 +0100133static inline u8 FAN_TO_REG(long rpm, int div)
134{
Dan Carpenter3806b452013-12-12 08:05:33 +0100135 if (rpm <= 0 || rpm > 1310720)
Roger Lucas1de9e372005-11-26 20:20:05 +0100136 return 0;
Guenter Roeck2a844c12013-01-09 08:09:34 -0800137 return clamp_val(1310720 / (rpm * div), 1, 255);
Roger Lucas1de9e372005-11-26 20:20:05 +0100138}
139
140#define FAN_FROM_REG(val, div) ((val) == 0 ? 0 : 1310720 / ((val) * (div)))
141
142struct vt8231_data {
Roger Lucasec5e1a42007-06-12 21:04:08 +0200143 unsigned short addr;
144 const char *name;
145
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100146 struct mutex update_lock;
Tony Jones1beeffe2007-08-20 13:46:20 -0700147 struct device *hwmon_dev;
Roger Lucas1de9e372005-11-26 20:20:05 +0100148 char valid; /* !=0 if following fields are valid */
149 unsigned long last_updated; /* In jiffies */
150
151 u8 in[6]; /* Register value */
152 u8 in_max[6]; /* Register value */
153 u8 in_min[6]; /* Register value */
154 u16 temp[6]; /* Register value 10 bit, right aligned */
155 u8 temp_max[6]; /* Register value */
156 u8 temp_min[6]; /* Register value */
157 u8 fan[2]; /* Register value */
158 u8 fan_min[2]; /* Register value */
159 u8 fan_div[2]; /* Register encoding, shifted right */
160 u16 alarms; /* Register encoding */
161 u8 uch_config;
162};
163
164static struct pci_dev *s_bridge;
Roger Lucasec5e1a42007-06-12 21:04:08 +0200165static int vt8231_probe(struct platform_device *pdev);
Bill Pemberton281dfd02012-11-19 13:25:51 -0500166static int vt8231_remove(struct platform_device *pdev);
Roger Lucas1de9e372005-11-26 20:20:05 +0100167static struct vt8231_data *vt8231_update_device(struct device *dev);
Roger Lucasec5e1a42007-06-12 21:04:08 +0200168static void vt8231_init_device(struct vt8231_data *data);
Roger Lucas1de9e372005-11-26 20:20:05 +0100169
Roger Lucasec5e1a42007-06-12 21:04:08 +0200170static inline int vt8231_read_value(struct vt8231_data *data, u8 reg)
Roger Lucas1de9e372005-11-26 20:20:05 +0100171{
Roger Lucasec5e1a42007-06-12 21:04:08 +0200172 return inb_p(data->addr + reg);
Roger Lucas1de9e372005-11-26 20:20:05 +0100173}
174
Roger Lucasec5e1a42007-06-12 21:04:08 +0200175static inline void vt8231_write_value(struct vt8231_data *data, u8 reg,
Roger Lucas1de9e372005-11-26 20:20:05 +0100176 u8 value)
177{
Roger Lucasec5e1a42007-06-12 21:04:08 +0200178 outb_p(value, data->addr + reg);
Roger Lucas1de9e372005-11-26 20:20:05 +0100179}
180
181/* following are the sysfs callback functions */
Guenter Roeck08ea5a82019-01-22 14:30:27 -0800182static ssize_t in_show(struct device *dev, struct device_attribute *attr,
183 char *buf)
Roger Lucas1de9e372005-11-26 20:20:05 +0100184{
185 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
186 int nr = sensor_attr->index;
187 struct vt8231_data *data = vt8231_update_device(dev);
188
189 return sprintf(buf, "%d\n", ((data->in[nr] - 3) * 10000) / 958);
190}
191
Guenter Roeck08ea5a82019-01-22 14:30:27 -0800192static ssize_t in_min_show(struct device *dev, struct device_attribute *attr,
193 char *buf)
Roger Lucas1de9e372005-11-26 20:20:05 +0100194{
195 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
196 int nr = sensor_attr->index;
197 struct vt8231_data *data = vt8231_update_device(dev);
198
199 return sprintf(buf, "%d\n", ((data->in_min[nr] - 3) * 10000) / 958);
200}
201
Guenter Roeck08ea5a82019-01-22 14:30:27 -0800202static ssize_t in_max_show(struct device *dev, struct device_attribute *attr,
203 char *buf)
Roger Lucas1de9e372005-11-26 20:20:05 +0100204{
205 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
206 int nr = sensor_attr->index;
207 struct vt8231_data *data = vt8231_update_device(dev);
208
209 return sprintf(buf, "%d\n", (((data->in_max[nr] - 3) * 10000) / 958));
210}
211
Guenter Roeck08ea5a82019-01-22 14:30:27 -0800212static ssize_t in_min_store(struct device *dev, struct device_attribute *attr,
213 const char *buf, size_t count)
Roger Lucas1de9e372005-11-26 20:20:05 +0100214{
215 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
216 int nr = sensor_attr->index;
Roger Lucasec5e1a42007-06-12 21:04:08 +0200217 struct vt8231_data *data = dev_get_drvdata(dev);
Guenter Roeck65fe5c72012-01-15 07:03:38 -0800218 unsigned long val;
219 int err;
220
221 err = kstrtoul(buf, 10, &val);
222 if (err)
223 return err;
Roger Lucas1de9e372005-11-26 20:20:05 +0100224
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100225 mutex_lock(&data->update_lock);
Guenter Roeck2a844c12013-01-09 08:09:34 -0800226 data->in_min[nr] = clamp_val(((val * 958) / 10000) + 3, 0, 255);
Roger Lucasec5e1a42007-06-12 21:04:08 +0200227 vt8231_write_value(data, regvoltmin[nr], data->in_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100228 mutex_unlock(&data->update_lock);
Roger Lucas1de9e372005-11-26 20:20:05 +0100229 return count;
230}
231
Guenter Roeck08ea5a82019-01-22 14:30:27 -0800232static ssize_t in_max_store(struct device *dev, struct device_attribute *attr,
233 const char *buf, size_t count)
Roger Lucas1de9e372005-11-26 20:20:05 +0100234{
235 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
236 int nr = sensor_attr->index;
Roger Lucasec5e1a42007-06-12 21:04:08 +0200237 struct vt8231_data *data = dev_get_drvdata(dev);
Guenter Roeck65fe5c72012-01-15 07:03:38 -0800238 unsigned long val;
239 int err;
240
241 err = kstrtoul(buf, 10, &val);
242 if (err)
243 return err;
Roger Lucas1de9e372005-11-26 20:20:05 +0100244
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100245 mutex_lock(&data->update_lock);
Guenter Roeck2a844c12013-01-09 08:09:34 -0800246 data->in_max[nr] = clamp_val(((val * 958) / 10000) + 3, 0, 255);
Roger Lucasec5e1a42007-06-12 21:04:08 +0200247 vt8231_write_value(data, regvoltmax[nr], data->in_max[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100248 mutex_unlock(&data->update_lock);
Roger Lucas1de9e372005-11-26 20:20:05 +0100249 return count;
250}
251
252/* Special case for input 5 as this has 3.3V scaling built into the chip */
Julia Lawalld5034db2016-12-22 13:05:18 +0100253static ssize_t in5_input_show(struct device *dev,
254 struct device_attribute *attr, char *buf)
Roger Lucas1de9e372005-11-26 20:20:05 +0100255{
256 struct vt8231_data *data = vt8231_update_device(dev);
257
258 return sprintf(buf, "%d\n",
259 (((data->in[5] - 3) * 10000 * 54) / (958 * 34)));
260}
261
Julia Lawalld5034db2016-12-22 13:05:18 +0100262static ssize_t in5_min_show(struct device *dev, struct device_attribute *attr,
Roger Lucas1de9e372005-11-26 20:20:05 +0100263 char *buf)
264{
265 struct vt8231_data *data = vt8231_update_device(dev);
266
267 return sprintf(buf, "%d\n",
268 (((data->in_min[5] - 3) * 10000 * 54) / (958 * 34)));
269}
270
Julia Lawalld5034db2016-12-22 13:05:18 +0100271static ssize_t in5_max_show(struct device *dev, struct device_attribute *attr,
Roger Lucas1de9e372005-11-26 20:20:05 +0100272 char *buf)
273{
274 struct vt8231_data *data = vt8231_update_device(dev);
275
276 return sprintf(buf, "%d\n",
277 (((data->in_max[5] - 3) * 10000 * 54) / (958 * 34)));
278}
279
Julia Lawalld5034db2016-12-22 13:05:18 +0100280static ssize_t in5_min_store(struct device *dev,
281 struct device_attribute *attr, const char *buf,
282 size_t count)
Roger Lucas1de9e372005-11-26 20:20:05 +0100283{
Roger Lucasec5e1a42007-06-12 21:04:08 +0200284 struct vt8231_data *data = dev_get_drvdata(dev);
Guenter Roeck65fe5c72012-01-15 07:03:38 -0800285 unsigned long val;
286 int err;
287
288 err = kstrtoul(buf, 10, &val);
289 if (err)
290 return err;
Roger Lucas1de9e372005-11-26 20:20:05 +0100291
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100292 mutex_lock(&data->update_lock);
Guenter Roeck2a844c12013-01-09 08:09:34 -0800293 data->in_min[5] = clamp_val(((val * 958 * 34) / (10000 * 54)) + 3,
294 0, 255);
Roger Lucasec5e1a42007-06-12 21:04:08 +0200295 vt8231_write_value(data, regvoltmin[5], data->in_min[5]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100296 mutex_unlock(&data->update_lock);
Roger Lucas1de9e372005-11-26 20:20:05 +0100297 return count;
298}
299
Julia Lawalld5034db2016-12-22 13:05:18 +0100300static ssize_t in5_max_store(struct device *dev,
301 struct device_attribute *attr, const char *buf,
302 size_t count)
Roger Lucas1de9e372005-11-26 20:20:05 +0100303{
Roger Lucasec5e1a42007-06-12 21:04:08 +0200304 struct vt8231_data *data = dev_get_drvdata(dev);
Guenter Roeck65fe5c72012-01-15 07:03:38 -0800305 unsigned long val;
306 int err;
307
308 err = kstrtoul(buf, 10, &val);
309 if (err)
310 return err;
Roger Lucas1de9e372005-11-26 20:20:05 +0100311
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100312 mutex_lock(&data->update_lock);
Guenter Roeck2a844c12013-01-09 08:09:34 -0800313 data->in_max[5] = clamp_val(((val * 958 * 34) / (10000 * 54)) + 3,
314 0, 255);
Roger Lucasec5e1a42007-06-12 21:04:08 +0200315 vt8231_write_value(data, regvoltmax[5], data->in_max[5]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100316 mutex_unlock(&data->update_lock);
Roger Lucas1de9e372005-11-26 20:20:05 +0100317 return count;
318}
319
Guenter Roeck08ea5a82019-01-22 14:30:27 -0800320static SENSOR_DEVICE_ATTR_RO(in0_input, in, 0);
321static SENSOR_DEVICE_ATTR_RW(in0_min, in_min, 0);
322static SENSOR_DEVICE_ATTR_RW(in0_max, in_max, 0);
323static SENSOR_DEVICE_ATTR_RO(in1_input, in, 1);
324static SENSOR_DEVICE_ATTR_RW(in1_min, in_min, 1);
325static SENSOR_DEVICE_ATTR_RW(in1_max, in_max, 1);
326static SENSOR_DEVICE_ATTR_RO(in2_input, in, 2);
327static SENSOR_DEVICE_ATTR_RW(in2_min, in_min, 2);
328static SENSOR_DEVICE_ATTR_RW(in2_max, in_max, 2);
329static SENSOR_DEVICE_ATTR_RO(in3_input, in, 3);
330static SENSOR_DEVICE_ATTR_RW(in3_min, in_min, 3);
331static SENSOR_DEVICE_ATTR_RW(in3_max, in_max, 3);
332static SENSOR_DEVICE_ATTR_RO(in4_input, in, 4);
333static SENSOR_DEVICE_ATTR_RW(in4_min, in_min, 4);
334static SENSOR_DEVICE_ATTR_RW(in4_max, in_max, 4);
Roger Lucas1de9e372005-11-26 20:20:05 +0100335
Julia Lawalld5034db2016-12-22 13:05:18 +0100336static DEVICE_ATTR_RO(in5_input);
337static DEVICE_ATTR_RW(in5_min);
338static DEVICE_ATTR_RW(in5_max);
Roger Lucas1de9e372005-11-26 20:20:05 +0100339
340/* Temperatures */
Julia Lawalld5034db2016-12-22 13:05:18 +0100341static ssize_t temp1_input_show(struct device *dev,
342 struct device_attribute *attr, char *buf)
Roger Lucas1de9e372005-11-26 20:20:05 +0100343{
344 struct vt8231_data *data = vt8231_update_device(dev);
345 return sprintf(buf, "%d\n", data->temp[0] * 250);
346}
347
Julia Lawalld5034db2016-12-22 13:05:18 +0100348static ssize_t temp1_max_show(struct device *dev, struct device_attribute *attr,
Roger Lucas1de9e372005-11-26 20:20:05 +0100349 char *buf)
350{
351 struct vt8231_data *data = vt8231_update_device(dev);
352 return sprintf(buf, "%d\n", data->temp_max[0] * 1000);
353}
354
Julia Lawalld5034db2016-12-22 13:05:18 +0100355static ssize_t temp1_max_hyst_show(struct device *dev,
356 struct device_attribute *attr, char *buf)
Roger Lucas1de9e372005-11-26 20:20:05 +0100357{
358 struct vt8231_data *data = vt8231_update_device(dev);
359 return sprintf(buf, "%d\n", data->temp_min[0] * 1000);
360}
361
Julia Lawalld5034db2016-12-22 13:05:18 +0100362static ssize_t temp1_max_store(struct device *dev,
363 struct device_attribute *attr, const char *buf,
364 size_t count)
Roger Lucas1de9e372005-11-26 20:20:05 +0100365{
Roger Lucasec5e1a42007-06-12 21:04:08 +0200366 struct vt8231_data *data = dev_get_drvdata(dev);
Guenter Roeck65fe5c72012-01-15 07:03:38 -0800367 long val;
368 int err;
369
370 err = kstrtol(buf, 10, &val);
371 if (err)
372 return err;
Roger Lucas1de9e372005-11-26 20:20:05 +0100373
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100374 mutex_lock(&data->update_lock);
Guenter Roeck2a844c12013-01-09 08:09:34 -0800375 data->temp_max[0] = clamp_val((val + 500) / 1000, 0, 255);
Roger Lucasec5e1a42007-06-12 21:04:08 +0200376 vt8231_write_value(data, regtempmax[0], data->temp_max[0]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100377 mutex_unlock(&data->update_lock);
Roger Lucas1de9e372005-11-26 20:20:05 +0100378 return count;
379}
Julia Lawalld5034db2016-12-22 13:05:18 +0100380static ssize_t temp1_max_hyst_store(struct device *dev,
381 struct device_attribute *attr,
382 const char *buf, size_t count)
Roger Lucas1de9e372005-11-26 20:20:05 +0100383{
Roger Lucasec5e1a42007-06-12 21:04:08 +0200384 struct vt8231_data *data = dev_get_drvdata(dev);
Guenter Roeck65fe5c72012-01-15 07:03:38 -0800385 long val;
386 int err;
387
388 err = kstrtol(buf, 10, &val);
389 if (err)
390 return err;
Roger Lucas1de9e372005-11-26 20:20:05 +0100391
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100392 mutex_lock(&data->update_lock);
Guenter Roeck2a844c12013-01-09 08:09:34 -0800393 data->temp_min[0] = clamp_val((val + 500) / 1000, 0, 255);
Roger Lucasec5e1a42007-06-12 21:04:08 +0200394 vt8231_write_value(data, regtempmin[0], data->temp_min[0]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100395 mutex_unlock(&data->update_lock);
Roger Lucas1de9e372005-11-26 20:20:05 +0100396 return count;
397}
398
Guenter Roeck08ea5a82019-01-22 14:30:27 -0800399static ssize_t temp_show(struct device *dev, struct device_attribute *attr,
400 char *buf)
Roger Lucas1de9e372005-11-26 20:20:05 +0100401{
402 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
403 int nr = sensor_attr->index;
404 struct vt8231_data *data = vt8231_update_device(dev);
405 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr]));
406}
407
Guenter Roeck08ea5a82019-01-22 14:30:27 -0800408static ssize_t temp_max_show(struct device *dev,
409 struct device_attribute *attr, char *buf)
Roger Lucas1de9e372005-11-26 20:20:05 +0100410{
411 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
412 int nr = sensor_attr->index;
413 struct vt8231_data *data = vt8231_update_device(dev);
414 return sprintf(buf, "%d\n", TEMP_MAXMIN_FROM_REG(data->temp_max[nr]));
415}
416
Guenter Roeck08ea5a82019-01-22 14:30:27 -0800417static ssize_t temp_min_show(struct device *dev,
418 struct device_attribute *attr, char *buf)
Roger Lucas1de9e372005-11-26 20:20:05 +0100419{
420 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
421 int nr = sensor_attr->index;
422 struct vt8231_data *data = vt8231_update_device(dev);
423 return sprintf(buf, "%d\n", TEMP_MAXMIN_FROM_REG(data->temp_min[nr]));
424}
425
Guenter Roeck08ea5a82019-01-22 14:30:27 -0800426static ssize_t temp_max_store(struct device *dev,
427 struct device_attribute *attr, const char *buf,
428 size_t count)
Roger Lucas1de9e372005-11-26 20:20:05 +0100429{
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);
Guenter Roeck2a844c12013-01-09 08:09:34 -0800441 data->temp_max[nr] = clamp_val(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}
Guenter Roeck08ea5a82019-01-22 14:30:27 -0800446static ssize_t temp_min_store(struct device *dev,
447 struct device_attribute *attr, const char *buf,
448 size_t count)
Roger Lucas1de9e372005-11-26 20:20:05 +0100449{
450 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
451 int nr = sensor_attr->index;
Roger Lucasec5e1a42007-06-12 21:04:08 +0200452 struct vt8231_data *data = dev_get_drvdata(dev);
Guenter Roeck65fe5c72012-01-15 07:03:38 -0800453 long val;
454 int err;
455
456 err = kstrtol(buf, 10, &val);
457 if (err)
458 return err;
Roger Lucas1de9e372005-11-26 20:20:05 +0100459
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100460 mutex_lock(&data->update_lock);
Guenter Roeck2a844c12013-01-09 08:09:34 -0800461 data->temp_min[nr] = clamp_val(TEMP_MAXMIN_TO_REG(val), 0, 255);
Roger Lucasec5e1a42007-06-12 21:04:08 +0200462 vt8231_write_value(data, regtempmin[nr], data->temp_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100463 mutex_unlock(&data->update_lock);
Roger Lucas1de9e372005-11-26 20:20:05 +0100464 return count;
465}
466
Guenter Roeck61ba0312012-01-19 11:02:27 -0800467/*
468 * Note that these map the Linux temperature sensor numbering (1-6) to the VIA
469 * temperature sensor numbering (0-5)
470 */
Roger Lucas1de9e372005-11-26 20:20:05 +0100471
Julia Lawalld5034db2016-12-22 13:05:18 +0100472static DEVICE_ATTR_RO(temp1_input);
473static DEVICE_ATTR_RW(temp1_max);
474static DEVICE_ATTR_RW(temp1_max_hyst);
Roger Lucas1de9e372005-11-26 20:20:05 +0100475
Guenter Roeck08ea5a82019-01-22 14:30:27 -0800476static SENSOR_DEVICE_ATTR_RO(temp2_input, temp, 1);
477static SENSOR_DEVICE_ATTR_RW(temp2_max, temp_max, 1);
478static SENSOR_DEVICE_ATTR_RW(temp2_max_hyst, temp_min, 1);
479static SENSOR_DEVICE_ATTR_RO(temp3_input, temp, 2);
480static SENSOR_DEVICE_ATTR_RW(temp3_max, temp_max, 2);
481static SENSOR_DEVICE_ATTR_RW(temp3_max_hyst, temp_min, 2);
482static SENSOR_DEVICE_ATTR_RO(temp4_input, temp, 3);
483static SENSOR_DEVICE_ATTR_RW(temp4_max, temp_max, 3);
484static SENSOR_DEVICE_ATTR_RW(temp4_max_hyst, temp_min, 3);
485static SENSOR_DEVICE_ATTR_RO(temp5_input, temp, 4);
486static SENSOR_DEVICE_ATTR_RW(temp5_max, temp_max, 4);
487static SENSOR_DEVICE_ATTR_RW(temp5_max_hyst, temp_min, 4);
488static SENSOR_DEVICE_ATTR_RO(temp6_input, temp, 5);
489static SENSOR_DEVICE_ATTR_RW(temp6_max, temp_max, 5);
490static SENSOR_DEVICE_ATTR_RW(temp6_max_hyst, temp_min, 5);
Roger Lucas1de9e372005-11-26 20:20:05 +0100491
Roger Lucas1de9e372005-11-26 20:20:05 +0100492/* Fans */
Guenter Roeck08ea5a82019-01-22 14:30:27 -0800493static ssize_t fan_show(struct device *dev, struct device_attribute *attr,
494 char *buf)
Roger Lucas1de9e372005-11-26 20:20:05 +0100495{
496 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
497 int nr = sensor_attr->index;
498 struct vt8231_data *data = vt8231_update_device(dev);
499 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr],
500 DIV_FROM_REG(data->fan_div[nr])));
501}
502
Guenter Roeck08ea5a82019-01-22 14:30:27 -0800503static ssize_t fan_min_show(struct device *dev, struct device_attribute *attr,
504 char *buf)
Roger Lucas1de9e372005-11-26 20:20:05 +0100505{
506 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
507 int nr = sensor_attr->index;
508 struct vt8231_data *data = vt8231_update_device(dev);
509 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[nr],
510 DIV_FROM_REG(data->fan_div[nr])));
511}
512
Guenter Roeck08ea5a82019-01-22 14:30:27 -0800513static ssize_t fan_div_show(struct device *dev, struct device_attribute *attr,
514 char *buf)
Roger Lucas1de9e372005-11-26 20:20:05 +0100515{
516 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
517 int nr = sensor_attr->index;
518 struct vt8231_data *data = vt8231_update_device(dev);
519 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]));
520}
521
Guenter Roeck08ea5a82019-01-22 14:30:27 -0800522static ssize_t fan_min_store(struct device *dev,
523 struct device_attribute *attr, const char *buf,
524 size_t count)
Roger Lucas1de9e372005-11-26 20:20:05 +0100525{
526 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
527 int nr = sensor_attr->index;
Roger Lucasec5e1a42007-06-12 21:04:08 +0200528 struct vt8231_data *data = dev_get_drvdata(dev);
Guenter Roeck65fe5c72012-01-15 07:03:38 -0800529 unsigned long val;
530 int err;
531
532 err = kstrtoul(buf, 10, &val);
533 if (err)
534 return err;
Roger Lucas1de9e372005-11-26 20:20:05 +0100535
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100536 mutex_lock(&data->update_lock);
Roger Lucas1de9e372005-11-26 20:20:05 +0100537 data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
Roger Lucasec5e1a42007-06-12 21:04:08 +0200538 vt8231_write_value(data, VT8231_REG_FAN_MIN(nr), data->fan_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100539 mutex_unlock(&data->update_lock);
Roger Lucas1de9e372005-11-26 20:20:05 +0100540 return count;
541}
542
Guenter Roeck08ea5a82019-01-22 14:30:27 -0800543static ssize_t fan_div_store(struct device *dev,
544 struct device_attribute *attr, const char *buf,
545 size_t count)
Roger Lucas1de9e372005-11-26 20:20:05 +0100546{
Roger Lucasec5e1a42007-06-12 21:04:08 +0200547 struct vt8231_data *data = dev_get_drvdata(dev);
Roger Lucas1de9e372005-11-26 20:20:05 +0100548 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
Guenter Roeck65fe5c72012-01-15 07:03:38 -0800549 unsigned long val;
Roger Lucas1de9e372005-11-26 20:20:05 +0100550 int nr = sensor_attr->index;
Roger Lucasec5e1a42007-06-12 21:04:08 +0200551 int old = vt8231_read_value(data, VT8231_REG_FANDIV);
Roger Lucas1de9e372005-11-26 20:20:05 +0100552 long min = FAN_FROM_REG(data->fan_min[nr],
553 DIV_FROM_REG(data->fan_div[nr]));
Guenter Roeck65fe5c72012-01-15 07:03:38 -0800554 int err;
555
556 err = kstrtoul(buf, 10, &val);
557 if (err)
558 return err;
Roger Lucas1de9e372005-11-26 20:20:05 +0100559
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100560 mutex_lock(&data->update_lock);
Roger Lucas1de9e372005-11-26 20:20:05 +0100561 switch (val) {
Guenter Roeck65fe5c72012-01-15 07:03:38 -0800562 case 1:
563 data->fan_div[nr] = 0;
564 break;
565 case 2:
566 data->fan_div[nr] = 1;
567 break;
568 case 4:
569 data->fan_div[nr] = 2;
570 break;
571 case 8:
572 data->fan_div[nr] = 3;
573 break;
Roger Lucas1de9e372005-11-26 20:20:05 +0100574 default:
Guenter Roeckb55f3752013-01-10 10:01:24 -0800575 dev_err(dev,
576 "fan_div value %ld not supported. Choose one of 1, 2, 4 or 8!\n",
577 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
Guenter Roeck08ea5a82019-01-22 14:30:27 -0800592static SENSOR_DEVICE_ATTR_RO(fan1_input, fan, 0);
593static SENSOR_DEVICE_ATTR_RW(fan1_min, fan_min, 0);
594static SENSOR_DEVICE_ATTR_RW(fan1_div, fan_div, 0);
595static SENSOR_DEVICE_ATTR_RO(fan2_input, fan, 1);
596static SENSOR_DEVICE_ATTR_RW(fan2_min, fan_min, 1);
597static SENSOR_DEVICE_ATTR_RW(fan2_div, fan_div, 1);
Roger Lucas1de9e372005-11-26 20:20:05 +0100598
599/* Alarms */
Julia Lawalld5034db2016-12-22 13:05:18 +0100600static ssize_t alarms_show(struct device *dev, struct device_attribute *attr,
Roger Lucas1de9e372005-11-26 20:20:05 +0100601 char *buf)
602{
603 struct vt8231_data *data = vt8231_update_device(dev);
604 return sprintf(buf, "%d\n", data->alarms);
605}
Julia Lawalld5034db2016-12-22 13:05:18 +0100606static DEVICE_ATTR_RO(alarms);
Roger Lucas1de9e372005-11-26 20:20:05 +0100607
Guenter Roeck08ea5a82019-01-22 14:30:27 -0800608static ssize_t alarm_show(struct device *dev, struct device_attribute *attr,
Jean Delvare2d1374c2008-01-06 15:46:02 +0100609 char *buf)
610{
611 int bitnr = to_sensor_dev_attr(attr)->index;
612 struct vt8231_data *data = vt8231_update_device(dev);
613 return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
614}
Guenter Roeck08ea5a82019-01-22 14:30:27 -0800615static SENSOR_DEVICE_ATTR_RO(temp1_alarm, alarm, 4);
616static SENSOR_DEVICE_ATTR_RO(temp2_alarm, alarm, 11);
617static SENSOR_DEVICE_ATTR_RO(temp3_alarm, alarm, 0);
618static SENSOR_DEVICE_ATTR_RO(temp4_alarm, alarm, 1);
619static SENSOR_DEVICE_ATTR_RO(temp5_alarm, alarm, 3);
620static SENSOR_DEVICE_ATTR_RO(temp6_alarm, alarm, 8);
621static SENSOR_DEVICE_ATTR_RO(in0_alarm, alarm, 11);
622static SENSOR_DEVICE_ATTR_RO(in1_alarm, alarm, 0);
623static SENSOR_DEVICE_ATTR_RO(in2_alarm, alarm, 1);
624static SENSOR_DEVICE_ATTR_RO(in3_alarm, alarm, 3);
625static SENSOR_DEVICE_ATTR_RO(in4_alarm, alarm, 8);
626static SENSOR_DEVICE_ATTR_RO(in5_alarm, alarm, 2);
627static SENSOR_DEVICE_ATTR_RO(fan1_alarm, alarm, 6);
628static SENSOR_DEVICE_ATTR_RO(fan2_alarm, alarm, 7);
Jean Delvare2d1374c2008-01-06 15:46:02 +0100629
Julia Lawalld5034db2016-12-22 13:05:18 +0100630static ssize_t name_show(struct device *dev, struct device_attribute
Roger Lucasec5e1a42007-06-12 21:04:08 +0200631 *devattr, char *buf)
632{
633 struct vt8231_data *data = dev_get_drvdata(dev);
634 return sprintf(buf, "%s\n", data->name);
635}
Julia Lawalld5034db2016-12-22 13:05:18 +0100636static DEVICE_ATTR_RO(name);
Roger Lucasec5e1a42007-06-12 21:04:08 +0200637
Jean Delvare2d1374c2008-01-06 15:46:02 +0100638static struct attribute *vt8231_attributes_temps[6][5] = {
Roger Lucascbeeb5b2006-09-24 21:21:46 +0200639 {
640 &dev_attr_temp1_input.attr,
641 &dev_attr_temp1_max_hyst.attr,
642 &dev_attr_temp1_max.attr,
Jean Delvare2d1374c2008-01-06 15:46:02 +0100643 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
Roger Lucascbeeb5b2006-09-24 21:21:46 +0200644 NULL
645 }, {
646 &sensor_dev_attr_temp2_input.dev_attr.attr,
647 &sensor_dev_attr_temp2_max_hyst.dev_attr.attr,
648 &sensor_dev_attr_temp2_max.dev_attr.attr,
Jean Delvare2d1374c2008-01-06 15:46:02 +0100649 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
Roger Lucascbeeb5b2006-09-24 21:21:46 +0200650 NULL
651 }, {
652 &sensor_dev_attr_temp3_input.dev_attr.attr,
653 &sensor_dev_attr_temp3_max_hyst.dev_attr.attr,
654 &sensor_dev_attr_temp3_max.dev_attr.attr,
Jean Delvare2d1374c2008-01-06 15:46:02 +0100655 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
Roger Lucascbeeb5b2006-09-24 21:21:46 +0200656 NULL
657 }, {
658 &sensor_dev_attr_temp4_input.dev_attr.attr,
659 &sensor_dev_attr_temp4_max_hyst.dev_attr.attr,
660 &sensor_dev_attr_temp4_max.dev_attr.attr,
Jean Delvare2d1374c2008-01-06 15:46:02 +0100661 &sensor_dev_attr_temp4_alarm.dev_attr.attr,
Roger Lucascbeeb5b2006-09-24 21:21:46 +0200662 NULL
663 }, {
664 &sensor_dev_attr_temp5_input.dev_attr.attr,
665 &sensor_dev_attr_temp5_max_hyst.dev_attr.attr,
666 &sensor_dev_attr_temp5_max.dev_attr.attr,
Jean Delvare2d1374c2008-01-06 15:46:02 +0100667 &sensor_dev_attr_temp5_alarm.dev_attr.attr,
Roger Lucascbeeb5b2006-09-24 21:21:46 +0200668 NULL
669 }, {
670 &sensor_dev_attr_temp6_input.dev_attr.attr,
671 &sensor_dev_attr_temp6_max_hyst.dev_attr.attr,
672 &sensor_dev_attr_temp6_max.dev_attr.attr,
Jean Delvare2d1374c2008-01-06 15:46:02 +0100673 &sensor_dev_attr_temp6_alarm.dev_attr.attr,
Roger Lucascbeeb5b2006-09-24 21:21:46 +0200674 NULL
675 }
676};
677
678static const struct attribute_group vt8231_group_temps[6] = {
679 { .attrs = vt8231_attributes_temps[0] },
680 { .attrs = vt8231_attributes_temps[1] },
681 { .attrs = vt8231_attributes_temps[2] },
682 { .attrs = vt8231_attributes_temps[3] },
683 { .attrs = vt8231_attributes_temps[4] },
684 { .attrs = vt8231_attributes_temps[5] },
685};
686
Jean Delvare2d1374c2008-01-06 15:46:02 +0100687static struct attribute *vt8231_attributes_volts[6][5] = {
Roger Lucascbeeb5b2006-09-24 21:21:46 +0200688 {
689 &sensor_dev_attr_in0_input.dev_attr.attr,
690 &sensor_dev_attr_in0_min.dev_attr.attr,
691 &sensor_dev_attr_in0_max.dev_attr.attr,
Jean Delvare2d1374c2008-01-06 15:46:02 +0100692 &sensor_dev_attr_in0_alarm.dev_attr.attr,
Roger Lucascbeeb5b2006-09-24 21:21:46 +0200693 NULL
694 }, {
695 &sensor_dev_attr_in1_input.dev_attr.attr,
696 &sensor_dev_attr_in1_min.dev_attr.attr,
697 &sensor_dev_attr_in1_max.dev_attr.attr,
Jean Delvare2d1374c2008-01-06 15:46:02 +0100698 &sensor_dev_attr_in1_alarm.dev_attr.attr,
Roger Lucascbeeb5b2006-09-24 21:21:46 +0200699 NULL
700 }, {
701 &sensor_dev_attr_in2_input.dev_attr.attr,
702 &sensor_dev_attr_in2_min.dev_attr.attr,
703 &sensor_dev_attr_in2_max.dev_attr.attr,
Jean Delvare2d1374c2008-01-06 15:46:02 +0100704 &sensor_dev_attr_in2_alarm.dev_attr.attr,
Roger Lucascbeeb5b2006-09-24 21:21:46 +0200705 NULL
706 }, {
707 &sensor_dev_attr_in3_input.dev_attr.attr,
708 &sensor_dev_attr_in3_min.dev_attr.attr,
709 &sensor_dev_attr_in3_max.dev_attr.attr,
Jean Delvare2d1374c2008-01-06 15:46:02 +0100710 &sensor_dev_attr_in3_alarm.dev_attr.attr,
Roger Lucascbeeb5b2006-09-24 21:21:46 +0200711 NULL
712 }, {
713 &sensor_dev_attr_in4_input.dev_attr.attr,
714 &sensor_dev_attr_in4_min.dev_attr.attr,
715 &sensor_dev_attr_in4_max.dev_attr.attr,
Jean Delvare2d1374c2008-01-06 15:46:02 +0100716 &sensor_dev_attr_in4_alarm.dev_attr.attr,
Roger Lucascbeeb5b2006-09-24 21:21:46 +0200717 NULL
718 }, {
719 &dev_attr_in5_input.attr,
720 &dev_attr_in5_min.attr,
721 &dev_attr_in5_max.attr,
Jean Delvare2d1374c2008-01-06 15:46:02 +0100722 &sensor_dev_attr_in5_alarm.dev_attr.attr,
Roger Lucascbeeb5b2006-09-24 21:21:46 +0200723 NULL
724 }
725};
726
727static const struct attribute_group vt8231_group_volts[6] = {
728 { .attrs = vt8231_attributes_volts[0] },
729 { .attrs = vt8231_attributes_volts[1] },
730 { .attrs = vt8231_attributes_volts[2] },
731 { .attrs = vt8231_attributes_volts[3] },
732 { .attrs = vt8231_attributes_volts[4] },
733 { .attrs = vt8231_attributes_volts[5] },
734};
735
736static struct attribute *vt8231_attributes[] = {
737 &sensor_dev_attr_fan1_input.dev_attr.attr,
738 &sensor_dev_attr_fan2_input.dev_attr.attr,
739 &sensor_dev_attr_fan1_min.dev_attr.attr,
740 &sensor_dev_attr_fan2_min.dev_attr.attr,
741 &sensor_dev_attr_fan1_div.dev_attr.attr,
742 &sensor_dev_attr_fan2_div.dev_attr.attr,
Jean Delvare2d1374c2008-01-06 15:46:02 +0100743 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
744 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
Roger Lucascbeeb5b2006-09-24 21:21:46 +0200745 &dev_attr_alarms.attr,
Roger Lucasec5e1a42007-06-12 21:04:08 +0200746 &dev_attr_name.attr,
Roger Lucascbeeb5b2006-09-24 21:21:46 +0200747 NULL
748};
749
750static const struct attribute_group vt8231_group = {
751 .attrs = vt8231_attributes,
752};
753
Roger Lucasec5e1a42007-06-12 21:04:08 +0200754static struct platform_driver vt8231_driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100755 .driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100756 .name = "vt8231",
757 },
Roger Lucasec5e1a42007-06-12 21:04:08 +0200758 .probe = vt8231_probe,
Bill Pemberton9e5e9b72012-11-19 13:21:20 -0500759 .remove = vt8231_remove,
Roger Lucas1de9e372005-11-26 20:20:05 +0100760};
761
Jingoo Hancd9bb052013-12-03 07:10:29 +0000762static const struct pci_device_id vt8231_pci_ids[] = {
Roger Lucas1de9e372005-11-26 20:20:05 +0100763 { PCI_DEVICE(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8231_4) },
764 { 0, }
765};
766
767MODULE_DEVICE_TABLE(pci, vt8231_pci_ids);
768
Bill Pemberton6c931ae2012-11-19 13:22:35 -0500769static int vt8231_pci_probe(struct pci_dev *dev,
Guenter Roeck65fe5c72012-01-15 07:03:38 -0800770 const struct pci_device_id *id);
Roger Lucas1de9e372005-11-26 20:20:05 +0100771
772static struct pci_driver vt8231_pci_driver = {
773 .name = "vt8231",
774 .id_table = vt8231_pci_ids,
775 .probe = vt8231_pci_probe,
776};
777
Mark M. Hoffmana022fef2007-10-14 15:00:24 -0400778static int vt8231_probe(struct platform_device *pdev)
Roger Lucas1de9e372005-11-26 20:20:05 +0100779{
Roger Lucasec5e1a42007-06-12 21:04:08 +0200780 struct resource *res;
Roger Lucas1de9e372005-11-26 20:20:05 +0100781 struct vt8231_data *data;
782 int err = 0, i;
Roger Lucas1de9e372005-11-26 20:20:05 +0100783
784 /* Reserve the ISA region */
Roger Lucasec5e1a42007-06-12 21:04:08 +0200785 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
Guenter Roeck7711f1b2012-06-02 11:35:55 -0700786 if (!devm_request_region(&pdev->dev, res->start, VT8231_EXTENT,
787 vt8231_driver.driver.name)) {
Roger Lucasec5e1a42007-06-12 21:04:08 +0200788 dev_err(&pdev->dev, "Region 0x%lx-0x%lx already in use!\n",
789 (unsigned long)res->start, (unsigned long)res->end);
Roger Lucas1de9e372005-11-26 20:20:05 +0100790 return -ENODEV;
791 }
792
Guenter Roeck7711f1b2012-06-02 11:35:55 -0700793 data = devm_kzalloc(&pdev->dev, sizeof(struct vt8231_data), GFP_KERNEL);
794 if (!data)
795 return -ENOMEM;
Roger Lucas1de9e372005-11-26 20:20:05 +0100796
Roger Lucasec5e1a42007-06-12 21:04:08 +0200797 platform_set_drvdata(pdev, data);
798 data->addr = res->start;
799 data->name = "vt8231";
Roger Lucas1de9e372005-11-26 20:20:05 +0100800
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100801 mutex_init(&data->update_lock);
Roger Lucasec5e1a42007-06-12 21:04:08 +0200802 vt8231_init_device(data);
Roger Lucas1de9e372005-11-26 20:20:05 +0100803
804 /* Register sysfs hooks */
Guenter Roeck65fe5c72012-01-15 07:03:38 -0800805 err = sysfs_create_group(&pdev->dev.kobj, &vt8231_group);
806 if (err)
Guenter Roeck7711f1b2012-06-02 11:35:55 -0700807 return err;
Roger Lucas1de9e372005-11-26 20:20:05 +0100808
809 /* Must update device information to find out the config field */
Roger Lucasec5e1a42007-06-12 21:04:08 +0200810 data->uch_config = vt8231_read_value(data, VT8231_REG_UCH_CONFIG);
Roger Lucas1de9e372005-11-26 20:20:05 +0100811
Roger Lucascbeeb5b2006-09-24 21:21:46 +0200812 for (i = 0; i < ARRAY_SIZE(vt8231_group_temps); i++) {
Roger Lucas1de9e372005-11-26 20:20:05 +0100813 if (ISTEMP(i, data->uch_config)) {
Guenter Roeck65fe5c72012-01-15 07:03:38 -0800814 err = sysfs_create_group(&pdev->dev.kobj,
815 &vt8231_group_temps[i]);
816 if (err)
Roger Lucascbeeb5b2006-09-24 21:21:46 +0200817 goto exit_remove_files;
Roger Lucas1de9e372005-11-26 20:20:05 +0100818 }
819 }
820
Roger Lucascbeeb5b2006-09-24 21:21:46 +0200821 for (i = 0; i < ARRAY_SIZE(vt8231_group_volts); i++) {
Roger Lucas1de9e372005-11-26 20:20:05 +0100822 if (ISVOLT(i, data->uch_config)) {
Guenter Roeck65fe5c72012-01-15 07:03:38 -0800823 err = sysfs_create_group(&pdev->dev.kobj,
824 &vt8231_group_volts[i]);
825 if (err)
Roger Lucascbeeb5b2006-09-24 21:21:46 +0200826 goto exit_remove_files;
Roger Lucas1de9e372005-11-26 20:20:05 +0100827 }
828 }
829
Tony Jones1beeffe2007-08-20 13:46:20 -0700830 data->hwmon_dev = hwmon_device_register(&pdev->dev);
831 if (IS_ERR(data->hwmon_dev)) {
832 err = PTR_ERR(data->hwmon_dev);
Roger Lucascbeeb5b2006-09-24 21:21:46 +0200833 goto exit_remove_files;
834 }
Roger Lucas1de9e372005-11-26 20:20:05 +0100835 return 0;
836
Roger Lucascbeeb5b2006-09-24 21:21:46 +0200837exit_remove_files:
838 for (i = 0; i < ARRAY_SIZE(vt8231_group_volts); i++)
Roger Lucasec5e1a42007-06-12 21:04:08 +0200839 sysfs_remove_group(&pdev->dev.kobj, &vt8231_group_volts[i]);
Roger Lucascbeeb5b2006-09-24 21:21:46 +0200840
841 for (i = 0; i < ARRAY_SIZE(vt8231_group_temps); i++)
Roger Lucasec5e1a42007-06-12 21:04:08 +0200842 sysfs_remove_group(&pdev->dev.kobj, &vt8231_group_temps[i]);
Roger Lucascbeeb5b2006-09-24 21:21:46 +0200843
Roger Lucasec5e1a42007-06-12 21:04:08 +0200844 sysfs_remove_group(&pdev->dev.kobj, &vt8231_group);
Roger Lucas1de9e372005-11-26 20:20:05 +0100845 return err;
846}
847
Bill Pemberton281dfd02012-11-19 13:25:51 -0500848static int vt8231_remove(struct platform_device *pdev)
Roger Lucas1de9e372005-11-26 20:20:05 +0100849{
Roger Lucasec5e1a42007-06-12 21:04:08 +0200850 struct vt8231_data *data = platform_get_drvdata(pdev);
851 int i;
Roger Lucas1de9e372005-11-26 20:20:05 +0100852
Tony Jones1beeffe2007-08-20 13:46:20 -0700853 hwmon_device_unregister(data->hwmon_dev);
Roger Lucas1de9e372005-11-26 20:20:05 +0100854
Roger Lucascbeeb5b2006-09-24 21:21:46 +0200855 for (i = 0; i < ARRAY_SIZE(vt8231_group_volts); i++)
Roger Lucasec5e1a42007-06-12 21:04:08 +0200856 sysfs_remove_group(&pdev->dev.kobj, &vt8231_group_volts[i]);
Roger Lucascbeeb5b2006-09-24 21:21:46 +0200857
858 for (i = 0; i < ARRAY_SIZE(vt8231_group_temps); i++)
Roger Lucasec5e1a42007-06-12 21:04:08 +0200859 sysfs_remove_group(&pdev->dev.kobj, &vt8231_group_temps[i]);
Roger Lucascbeeb5b2006-09-24 21:21:46 +0200860
Roger Lucasec5e1a42007-06-12 21:04:08 +0200861 sysfs_remove_group(&pdev->dev.kobj, &vt8231_group);
Roger Lucascbeeb5b2006-09-24 21:21:46 +0200862
Roger Lucas1de9e372005-11-26 20:20:05 +0100863 return 0;
864}
865
Roger Lucasec5e1a42007-06-12 21:04:08 +0200866static void vt8231_init_device(struct vt8231_data *data)
Roger Lucas1de9e372005-11-26 20:20:05 +0100867{
Roger Lucasec5e1a42007-06-12 21:04:08 +0200868 vt8231_write_value(data, VT8231_REG_TEMP1_CONFIG, 0);
869 vt8231_write_value(data, VT8231_REG_TEMP2_CONFIG, 0);
Roger Lucas1de9e372005-11-26 20:20:05 +0100870}
871
872static struct vt8231_data *vt8231_update_device(struct device *dev)
873{
Roger Lucasec5e1a42007-06-12 21:04:08 +0200874 struct vt8231_data *data = dev_get_drvdata(dev);
Roger Lucas1de9e372005-11-26 20:20:05 +0100875 int i;
876 u16 low;
877
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100878 mutex_lock(&data->update_lock);
Roger Lucas1de9e372005-11-26 20:20:05 +0100879
880 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
881 || !data->valid) {
882 for (i = 0; i < 6; i++) {
883 if (ISVOLT(i, data->uch_config)) {
Roger Lucasec5e1a42007-06-12 21:04:08 +0200884 data->in[i] = vt8231_read_value(data,
Roger Lucas1de9e372005-11-26 20:20:05 +0100885 regvolt[i]);
Roger Lucasec5e1a42007-06-12 21:04:08 +0200886 data->in_min[i] = vt8231_read_value(data,
Roger Lucas1de9e372005-11-26 20:20:05 +0100887 regvoltmin[i]);
Roger Lucasec5e1a42007-06-12 21:04:08 +0200888 data->in_max[i] = vt8231_read_value(data,
Roger Lucas1de9e372005-11-26 20:20:05 +0100889 regvoltmax[i]);
890 }
891 }
892 for (i = 0; i < 2; i++) {
Roger Lucasec5e1a42007-06-12 21:04:08 +0200893 data->fan[i] = vt8231_read_value(data,
Roger Lucas1de9e372005-11-26 20:20:05 +0100894 VT8231_REG_FAN(i));
Roger Lucasec5e1a42007-06-12 21:04:08 +0200895 data->fan_min[i] = vt8231_read_value(data,
Roger Lucas1de9e372005-11-26 20:20:05 +0100896 VT8231_REG_FAN_MIN(i));
897 }
898
Roger Lucasec5e1a42007-06-12 21:04:08 +0200899 low = vt8231_read_value(data, VT8231_REG_TEMP_LOW01);
Roger Lucas1de9e372005-11-26 20:20:05 +0100900 low = (low >> 6) | ((low & 0x30) >> 2)
Roger Lucasec5e1a42007-06-12 21:04:08 +0200901 | (vt8231_read_value(data, VT8231_REG_TEMP_LOW25) << 4);
Roger Lucas1de9e372005-11-26 20:20:05 +0100902 for (i = 0; i < 6; i++) {
903 if (ISTEMP(i, data->uch_config)) {
Roger Lucasec5e1a42007-06-12 21:04:08 +0200904 data->temp[i] = (vt8231_read_value(data,
Roger Lucas1de9e372005-11-26 20:20:05 +0100905 regtemp[i]) << 2)
906 | ((low >> (2 * i)) & 0x03);
Roger Lucasec5e1a42007-06-12 21:04:08 +0200907 data->temp_max[i] = vt8231_read_value(data,
Roger Lucas1de9e372005-11-26 20:20:05 +0100908 regtempmax[i]);
Roger Lucasec5e1a42007-06-12 21:04:08 +0200909 data->temp_min[i] = vt8231_read_value(data,
Roger Lucas1de9e372005-11-26 20:20:05 +0100910 regtempmin[i]);
911 }
912 }
913
Roger Lucasec5e1a42007-06-12 21:04:08 +0200914 i = vt8231_read_value(data, VT8231_REG_FANDIV);
Roger Lucas1de9e372005-11-26 20:20:05 +0100915 data->fan_div[0] = (i >> 4) & 0x03;
916 data->fan_div[1] = i >> 6;
Roger Lucasec5e1a42007-06-12 21:04:08 +0200917 data->alarms = vt8231_read_value(data, VT8231_REG_ALARM1) |
918 (vt8231_read_value(data, VT8231_REG_ALARM2) << 8);
Roger Lucas1de9e372005-11-26 20:20:05 +0100919
920 /* Set alarm flags correctly */
Guenter Roeck65fe5c72012-01-15 07:03:38 -0800921 if (!data->fan[0] && data->fan_min[0])
Roger Lucas1de9e372005-11-26 20:20:05 +0100922 data->alarms |= 0x40;
Guenter Roeck65fe5c72012-01-15 07:03:38 -0800923 else if (data->fan[0] && !data->fan_min[0])
Roger Lucas1de9e372005-11-26 20:20:05 +0100924 data->alarms &= ~0x40;
Roger Lucas1de9e372005-11-26 20:20:05 +0100925
Guenter Roeck65fe5c72012-01-15 07:03:38 -0800926 if (!data->fan[1] && data->fan_min[1])
Roger Lucas1de9e372005-11-26 20:20:05 +0100927 data->alarms |= 0x80;
Guenter Roeck65fe5c72012-01-15 07:03:38 -0800928 else if (data->fan[1] && !data->fan_min[1])
Roger Lucas1de9e372005-11-26 20:20:05 +0100929 data->alarms &= ~0x80;
Roger Lucas1de9e372005-11-26 20:20:05 +0100930
931 data->last_updated = jiffies;
932 data->valid = 1;
933 }
934
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100935 mutex_unlock(&data->update_lock);
Roger Lucas1de9e372005-11-26 20:20:05 +0100936
937 return data;
938}
939
Bill Pemberton6c931ae2012-11-19 13:22:35 -0500940static int vt8231_device_add(unsigned short address)
Roger Lucasec5e1a42007-06-12 21:04:08 +0200941{
942 struct resource res = {
943 .start = address,
944 .end = address + VT8231_EXTENT - 1,
945 .name = "vt8231",
946 .flags = IORESOURCE_IO,
947 };
948 int err;
949
Jean Delvareb9acb642009-01-07 16:37:35 +0100950 err = acpi_check_resource_conflict(&res);
951 if (err)
952 goto exit;
953
Roger Lucasec5e1a42007-06-12 21:04:08 +0200954 pdev = platform_device_alloc("vt8231", address);
955 if (!pdev) {
956 err = -ENOMEM;
Joe Perches9d72be02010-10-20 06:51:53 +0000957 pr_err("Device allocation failed\n");
Roger Lucasec5e1a42007-06-12 21:04:08 +0200958 goto exit;
959 }
960
961 err = platform_device_add_resources(pdev, &res, 1);
962 if (err) {
Joe Perches9d72be02010-10-20 06:51:53 +0000963 pr_err("Device resource addition failed (%d)\n", err);
Roger Lucasec5e1a42007-06-12 21:04:08 +0200964 goto exit_device_put;
965 }
966
967 err = platform_device_add(pdev);
968 if (err) {
Joe Perches9d72be02010-10-20 06:51:53 +0000969 pr_err("Device addition failed (%d)\n", err);
Roger Lucasec5e1a42007-06-12 21:04:08 +0200970 goto exit_device_put;
971 }
972
973 return 0;
974
975exit_device_put:
976 platform_device_put(pdev);
977exit:
978 return err;
979}
980
Bill Pemberton6c931ae2012-11-19 13:22:35 -0500981static int vt8231_pci_probe(struct pci_dev *dev,
Roger Lucas1de9e372005-11-26 20:20:05 +0100982 const struct pci_device_id *id)
983{
Roger Lucasec5e1a42007-06-12 21:04:08 +0200984 u16 address, val;
985 if (force_addr) {
986 address = force_addr & 0xff00;
987 dev_warn(&dev->dev, "Forcing ISA address 0x%x\n",
988 address);
989
990 if (PCIBIOS_SUCCESSFUL !=
991 pci_write_config_word(dev, VT8231_BASE_REG, address | 1))
992 return -ENODEV;
993 }
Roger Lucas1de9e372005-11-26 20:20:05 +0100994
995 if (PCIBIOS_SUCCESSFUL != pci_read_config_word(dev, VT8231_BASE_REG,
996 &val))
997 return -ENODEV;
998
Roger Lucasec5e1a42007-06-12 21:04:08 +0200999 address = val & ~(VT8231_EXTENT - 1);
1000 if (address == 0) {
Joe Perches4cae7872010-03-05 13:43:56 -08001001 dev_err(&dev->dev, "base address not set - upgrade BIOS or use force_addr=0xaddr\n");
Roger Lucas1de9e372005-11-26 20:20:05 +01001002 return -ENODEV;
1003 }
1004
Roger Lucasec5e1a42007-06-12 21:04:08 +02001005 if (PCIBIOS_SUCCESSFUL != pci_read_config_word(dev, VT8231_ENABLE_REG,
1006 &val))
1007 return -ENODEV;
Roger Lucas1de9e372005-11-26 20:20:05 +01001008
Roger Lucasec5e1a42007-06-12 21:04:08 +02001009 if (!(val & 0x0001)) {
1010 dev_warn(&dev->dev, "enabling sensors\n");
1011 if (PCIBIOS_SUCCESSFUL !=
1012 pci_write_config_word(dev, VT8231_ENABLE_REG,
1013 val | 0x0001))
1014 return -ENODEV;
Roger Lucas1de9e372005-11-26 20:20:05 +01001015 }
1016
Roger Lucasec5e1a42007-06-12 21:04:08 +02001017 if (platform_driver_register(&vt8231_driver))
1018 goto exit;
1019
1020 /* Sets global pdev as a side effect */
1021 if (vt8231_device_add(address))
1022 goto exit_unregister;
1023
Guenter Roeck61ba0312012-01-19 11:02:27 -08001024 /*
1025 * Always return failure here. This is to allow other drivers to bind
Roger Lucas1de9e372005-11-26 20:20:05 +01001026 * to this pci device. We don't really want to have control over the
1027 * pci device, we only wanted to read as few register values from it.
1028 */
Roger Lucasec5e1a42007-06-12 21:04:08 +02001029
Guenter Roeck61ba0312012-01-19 11:02:27 -08001030 /*
1031 * We do, however, mark ourselves as using the PCI device to stop it
1032 * getting unloaded.
1033 */
Roger Lucasec5e1a42007-06-12 21:04:08 +02001034 s_bridge = pci_dev_get(dev);
1035 return -ENODEV;
1036
1037exit_unregister:
1038 platform_driver_unregister(&vt8231_driver);
1039exit:
Roger Lucas1de9e372005-11-26 20:20:05 +01001040 return -ENODEV;
1041}
1042
1043static int __init sm_vt8231_init(void)
1044{
Richard Knutsson93b47682005-11-30 01:00:35 +01001045 return pci_register_driver(&vt8231_pci_driver);
Roger Lucas1de9e372005-11-26 20:20:05 +01001046}
1047
1048static void __exit sm_vt8231_exit(void)
1049{
1050 pci_unregister_driver(&vt8231_pci_driver);
1051 if (s_bridge != NULL) {
Roger Lucasec5e1a42007-06-12 21:04:08 +02001052 platform_device_unregister(pdev);
1053 platform_driver_unregister(&vt8231_driver);
Roger Lucas1de9e372005-11-26 20:20:05 +01001054 pci_dev_put(s_bridge);
1055 s_bridge = NULL;
1056 }
1057}
1058
Roger Lucasaf865762008-02-13 07:52:06 -05001059MODULE_AUTHOR("Roger Lucas <vt8231@hiddenengine.co.uk>");
Roger Lucas1de9e372005-11-26 20:20:05 +01001060MODULE_DESCRIPTION("VT8231 sensors");
1061MODULE_LICENSE("GPL");
1062
1063module_init(sm_vt8231_init);
1064module_exit(sm_vt8231_exit);