xref: /openbmc/linux/drivers/hwmon/vt8231.c (revision 03638e62)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * 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>
9  */
10 
11 /*
12  * Supports VIA VT8231 South Bridge embedded sensors
13  */
14 
15 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
16 
17 #include <linux/module.h>
18 #include <linux/init.h>
19 #include <linux/slab.h>
20 #include <linux/pci.h>
21 #include <linux/jiffies.h>
22 #include <linux/platform_device.h>
23 #include <linux/hwmon.h>
24 #include <linux/hwmon-sysfs.h>
25 #include <linux/hwmon-vid.h>
26 #include <linux/err.h>
27 #include <linux/mutex.h>
28 #include <linux/acpi.h>
29 #include <linux/io.h>
30 
31 static int force_addr;
32 module_param(force_addr, int, 0);
33 MODULE_PARM_DESC(force_addr, "Initialize the base address of the sensors");
34 
35 static struct platform_device *pdev;
36 
37 #define VT8231_EXTENT 0x80
38 #define VT8231_BASE_REG 0x70
39 #define VT8231_ENABLE_REG 0x74
40 
41 /*
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  */
62 
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 
69 static const u8 regvolt[]    = { 0x21, 0x22, 0x23, 0x24, 0x25, 0x26 };
70 static const u8 regvoltmax[] = { 0x3d, 0x2b, 0x2d, 0x2f, 0x31, 0x33 };
71 static const u8 regvoltmin[] = { 0x3e, 0x2c, 0x2e, 0x30, 0x32, 0x34 };
72 
73 /*
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  */
81 
82 #define VT8231_REG_TEMP_LOW01	0x49
83 #define VT8231_REG_TEMP_LOW25	0x4d
84 
85 static const u8 regtemp[]    = { 0x1f, 0x21, 0x22, 0x23, 0x24, 0x25 };
86 static const u8 regtempmax[] = { 0x39, 0x3d, 0x2b, 0x2d, 0x2f, 0x31 };
87 static 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 
101 /*
102  * temps 0-5 as numbered in VIA datasheet - see later for mapping to Linux
103  * numbering
104  */
105 #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 
113 /*
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  */
127 
128 /*
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  */
133 static inline u8 FAN_TO_REG(long rpm, int div)
134 {
135 	if (rpm <= 0 || rpm > 1310720)
136 		return 0;
137 	return clamp_val(1310720 / (rpm * div), 1, 255);
138 }
139 
140 #define FAN_FROM_REG(val, div) ((val) == 0 ? 0 : 1310720 / ((val) * (div)))
141 
142 struct vt8231_data {
143 	unsigned short addr;
144 	const char *name;
145 
146 	struct mutex update_lock;
147 	struct device *hwmon_dev;
148 	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 
164 static struct pci_dev *s_bridge;
165 static int vt8231_probe(struct platform_device *pdev);
166 static int vt8231_remove(struct platform_device *pdev);
167 static struct vt8231_data *vt8231_update_device(struct device *dev);
168 static void vt8231_init_device(struct vt8231_data *data);
169 
170 static inline int vt8231_read_value(struct vt8231_data *data, u8 reg)
171 {
172 	return inb_p(data->addr + reg);
173 }
174 
175 static inline void vt8231_write_value(struct vt8231_data *data, u8 reg,
176 					u8 value)
177 {
178 	outb_p(value, data->addr + reg);
179 }
180 
181 /* following are the sysfs callback functions */
182 static ssize_t in_show(struct device *dev, struct device_attribute *attr,
183 		       char *buf)
184 {
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 
192 static ssize_t in_min_show(struct device *dev, struct device_attribute *attr,
193 			   char *buf)
194 {
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 
202 static ssize_t in_max_show(struct device *dev, struct device_attribute *attr,
203 			   char *buf)
204 {
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 
212 static ssize_t in_min_store(struct device *dev, struct device_attribute *attr,
213 			    const char *buf, size_t count)
214 {
215 	struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
216 	int nr = sensor_attr->index;
217 	struct vt8231_data *data = dev_get_drvdata(dev);
218 	unsigned long val;
219 	int err;
220 
221 	err = kstrtoul(buf, 10, &val);
222 	if (err)
223 		return err;
224 
225 	mutex_lock(&data->update_lock);
226 	data->in_min[nr] = clamp_val(((val * 958) / 10000) + 3, 0, 255);
227 	vt8231_write_value(data, regvoltmin[nr], data->in_min[nr]);
228 	mutex_unlock(&data->update_lock);
229 	return count;
230 }
231 
232 static ssize_t in_max_store(struct device *dev, struct device_attribute *attr,
233 			    const char *buf, size_t count)
234 {
235 	struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
236 	int nr = sensor_attr->index;
237 	struct vt8231_data *data = dev_get_drvdata(dev);
238 	unsigned long val;
239 	int err;
240 
241 	err = kstrtoul(buf, 10, &val);
242 	if (err)
243 		return err;
244 
245 	mutex_lock(&data->update_lock);
246 	data->in_max[nr] = clamp_val(((val * 958) / 10000) + 3, 0, 255);
247 	vt8231_write_value(data, regvoltmax[nr], data->in_max[nr]);
248 	mutex_unlock(&data->update_lock);
249 	return count;
250 }
251 
252 /* Special case for input 5 as this has 3.3V scaling built into the chip */
253 static ssize_t in5_input_show(struct device *dev,
254 			      struct device_attribute *attr, char *buf)
255 {
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 
262 static ssize_t in5_min_show(struct device *dev, struct device_attribute *attr,
263 		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 
271 static ssize_t in5_max_show(struct device *dev, struct device_attribute *attr,
272 		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 
280 static ssize_t in5_min_store(struct device *dev,
281 			     struct device_attribute *attr, const char *buf,
282 			     size_t count)
283 {
284 	struct vt8231_data *data = dev_get_drvdata(dev);
285 	unsigned long val;
286 	int err;
287 
288 	err = kstrtoul(buf, 10, &val);
289 	if (err)
290 		return err;
291 
292 	mutex_lock(&data->update_lock);
293 	data->in_min[5] = clamp_val(((val * 958 * 34) / (10000 * 54)) + 3,
294 				    0, 255);
295 	vt8231_write_value(data, regvoltmin[5], data->in_min[5]);
296 	mutex_unlock(&data->update_lock);
297 	return count;
298 }
299 
300 static ssize_t in5_max_store(struct device *dev,
301 			     struct device_attribute *attr, const char *buf,
302 			     size_t count)
303 {
304 	struct vt8231_data *data = dev_get_drvdata(dev);
305 	unsigned long val;
306 	int err;
307 
308 	err = kstrtoul(buf, 10, &val);
309 	if (err)
310 		return err;
311 
312 	mutex_lock(&data->update_lock);
313 	data->in_max[5] = clamp_val(((val * 958 * 34) / (10000 * 54)) + 3,
314 				    0, 255);
315 	vt8231_write_value(data, regvoltmax[5], data->in_max[5]);
316 	mutex_unlock(&data->update_lock);
317 	return count;
318 }
319 
320 static SENSOR_DEVICE_ATTR_RO(in0_input, in, 0);
321 static SENSOR_DEVICE_ATTR_RW(in0_min, in_min, 0);
322 static SENSOR_DEVICE_ATTR_RW(in0_max, in_max, 0);
323 static SENSOR_DEVICE_ATTR_RO(in1_input, in, 1);
324 static SENSOR_DEVICE_ATTR_RW(in1_min, in_min, 1);
325 static SENSOR_DEVICE_ATTR_RW(in1_max, in_max, 1);
326 static SENSOR_DEVICE_ATTR_RO(in2_input, in, 2);
327 static SENSOR_DEVICE_ATTR_RW(in2_min, in_min, 2);
328 static SENSOR_DEVICE_ATTR_RW(in2_max, in_max, 2);
329 static SENSOR_DEVICE_ATTR_RO(in3_input, in, 3);
330 static SENSOR_DEVICE_ATTR_RW(in3_min, in_min, 3);
331 static SENSOR_DEVICE_ATTR_RW(in3_max, in_max, 3);
332 static SENSOR_DEVICE_ATTR_RO(in4_input, in, 4);
333 static SENSOR_DEVICE_ATTR_RW(in4_min, in_min, 4);
334 static SENSOR_DEVICE_ATTR_RW(in4_max, in_max, 4);
335 
336 static DEVICE_ATTR_RO(in5_input);
337 static DEVICE_ATTR_RW(in5_min);
338 static DEVICE_ATTR_RW(in5_max);
339 
340 /* Temperatures */
341 static ssize_t temp1_input_show(struct device *dev,
342 				struct device_attribute *attr, char *buf)
343 {
344 	struct vt8231_data *data = vt8231_update_device(dev);
345 	return sprintf(buf, "%d\n", data->temp[0] * 250);
346 }
347 
348 static ssize_t temp1_max_show(struct device *dev, struct device_attribute *attr,
349 		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 
355 static ssize_t temp1_max_hyst_show(struct device *dev,
356 				   struct device_attribute *attr, char *buf)
357 {
358 	struct vt8231_data *data = vt8231_update_device(dev);
359 	return sprintf(buf, "%d\n", data->temp_min[0] * 1000);
360 }
361 
362 static ssize_t temp1_max_store(struct device *dev,
363 			       struct device_attribute *attr, const char *buf,
364 			       size_t count)
365 {
366 	struct vt8231_data *data = dev_get_drvdata(dev);
367 	long val;
368 	int err;
369 
370 	err = kstrtol(buf, 10, &val);
371 	if (err)
372 		return err;
373 
374 	mutex_lock(&data->update_lock);
375 	data->temp_max[0] = clamp_val((val + 500) / 1000, 0, 255);
376 	vt8231_write_value(data, regtempmax[0], data->temp_max[0]);
377 	mutex_unlock(&data->update_lock);
378 	return count;
379 }
380 static ssize_t temp1_max_hyst_store(struct device *dev,
381 				    struct device_attribute *attr,
382 				    const char *buf, size_t count)
383 {
384 	struct vt8231_data *data = dev_get_drvdata(dev);
385 	long val;
386 	int err;
387 
388 	err = kstrtol(buf, 10, &val);
389 	if (err)
390 		return err;
391 
392 	mutex_lock(&data->update_lock);
393 	data->temp_min[0] = clamp_val((val + 500) / 1000, 0, 255);
394 	vt8231_write_value(data, regtempmin[0], data->temp_min[0]);
395 	mutex_unlock(&data->update_lock);
396 	return count;
397 }
398 
399 static ssize_t temp_show(struct device *dev, struct device_attribute *attr,
400 			 char *buf)
401 {
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 
408 static ssize_t temp_max_show(struct device *dev,
409 			     struct device_attribute *attr, char *buf)
410 {
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 
417 static ssize_t temp_min_show(struct device *dev,
418 			     struct device_attribute *attr, char *buf)
419 {
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 
426 static ssize_t temp_max_store(struct device *dev,
427 			      struct device_attribute *attr, const char *buf,
428 			      size_t count)
429 {
430 	struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
431 	int nr = sensor_attr->index;
432 	struct vt8231_data *data = dev_get_drvdata(dev);
433 	long val;
434 	int err;
435 
436 	err = kstrtol(buf, 10, &val);
437 	if (err)
438 		return err;
439 
440 	mutex_lock(&data->update_lock);
441 	data->temp_max[nr] = clamp_val(TEMP_MAXMIN_TO_REG(val), 0, 255);
442 	vt8231_write_value(data, regtempmax[nr], data->temp_max[nr]);
443 	mutex_unlock(&data->update_lock);
444 	return count;
445 }
446 static ssize_t temp_min_store(struct device *dev,
447 			      struct device_attribute *attr, const char *buf,
448 			      size_t count)
449 {
450 	struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
451 	int nr = sensor_attr->index;
452 	struct vt8231_data *data = dev_get_drvdata(dev);
453 	long val;
454 	int err;
455 
456 	err = kstrtol(buf, 10, &val);
457 	if (err)
458 		return err;
459 
460 	mutex_lock(&data->update_lock);
461 	data->temp_min[nr] = clamp_val(TEMP_MAXMIN_TO_REG(val), 0, 255);
462 	vt8231_write_value(data, regtempmin[nr], data->temp_min[nr]);
463 	mutex_unlock(&data->update_lock);
464 	return count;
465 }
466 
467 /*
468  * Note that these map the Linux temperature sensor numbering (1-6) to the VIA
469  * temperature sensor numbering (0-5)
470  */
471 
472 static DEVICE_ATTR_RO(temp1_input);
473 static DEVICE_ATTR_RW(temp1_max);
474 static DEVICE_ATTR_RW(temp1_max_hyst);
475 
476 static SENSOR_DEVICE_ATTR_RO(temp2_input, temp, 1);
477 static SENSOR_DEVICE_ATTR_RW(temp2_max, temp_max, 1);
478 static SENSOR_DEVICE_ATTR_RW(temp2_max_hyst, temp_min, 1);
479 static SENSOR_DEVICE_ATTR_RO(temp3_input, temp, 2);
480 static SENSOR_DEVICE_ATTR_RW(temp3_max, temp_max, 2);
481 static SENSOR_DEVICE_ATTR_RW(temp3_max_hyst, temp_min, 2);
482 static SENSOR_DEVICE_ATTR_RO(temp4_input, temp, 3);
483 static SENSOR_DEVICE_ATTR_RW(temp4_max, temp_max, 3);
484 static SENSOR_DEVICE_ATTR_RW(temp4_max_hyst, temp_min, 3);
485 static SENSOR_DEVICE_ATTR_RO(temp5_input, temp, 4);
486 static SENSOR_DEVICE_ATTR_RW(temp5_max, temp_max, 4);
487 static SENSOR_DEVICE_ATTR_RW(temp5_max_hyst, temp_min, 4);
488 static SENSOR_DEVICE_ATTR_RO(temp6_input, temp, 5);
489 static SENSOR_DEVICE_ATTR_RW(temp6_max, temp_max, 5);
490 static SENSOR_DEVICE_ATTR_RW(temp6_max_hyst, temp_min, 5);
491 
492 /* Fans */
493 static ssize_t fan_show(struct device *dev, struct device_attribute *attr,
494 			char *buf)
495 {
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 
503 static ssize_t fan_min_show(struct device *dev, struct device_attribute *attr,
504 			    char *buf)
505 {
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 
513 static ssize_t fan_div_show(struct device *dev, struct device_attribute *attr,
514 			    char *buf)
515 {
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 
522 static ssize_t fan_min_store(struct device *dev,
523 			     struct device_attribute *attr, const char *buf,
524 			     size_t count)
525 {
526 	struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
527 	int nr = sensor_attr->index;
528 	struct vt8231_data *data = dev_get_drvdata(dev);
529 	unsigned long val;
530 	int err;
531 
532 	err = kstrtoul(buf, 10, &val);
533 	if (err)
534 		return err;
535 
536 	mutex_lock(&data->update_lock);
537 	data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
538 	vt8231_write_value(data, VT8231_REG_FAN_MIN(nr), data->fan_min[nr]);
539 	mutex_unlock(&data->update_lock);
540 	return count;
541 }
542 
543 static ssize_t fan_div_store(struct device *dev,
544 			     struct device_attribute *attr, const char *buf,
545 			     size_t count)
546 {
547 	struct vt8231_data *data = dev_get_drvdata(dev);
548 	struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
549 	unsigned long val;
550 	int nr = sensor_attr->index;
551 	int old = vt8231_read_value(data, VT8231_REG_FANDIV);
552 	long min = FAN_FROM_REG(data->fan_min[nr],
553 				 DIV_FROM_REG(data->fan_div[nr]));
554 	int err;
555 
556 	err = kstrtoul(buf, 10, &val);
557 	if (err)
558 		return err;
559 
560 	mutex_lock(&data->update_lock);
561 	switch (val) {
562 	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;
574 	default:
575 		dev_err(dev,
576 			"fan_div value %ld not supported. Choose one of 1, 2, 4 or 8!\n",
577 			val);
578 		mutex_unlock(&data->update_lock);
579 		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]));
584 	vt8231_write_value(data, VT8231_REG_FAN_MIN(nr), data->fan_min[nr]);
585 
586 	old = (old & 0x0f) | (data->fan_div[1] << 6) | (data->fan_div[0] << 4);
587 	vt8231_write_value(data, VT8231_REG_FANDIV, old);
588 	mutex_unlock(&data->update_lock);
589 	return count;
590 }
591 
592 static SENSOR_DEVICE_ATTR_RO(fan1_input, fan, 0);
593 static SENSOR_DEVICE_ATTR_RW(fan1_min, fan_min, 0);
594 static SENSOR_DEVICE_ATTR_RW(fan1_div, fan_div, 0);
595 static SENSOR_DEVICE_ATTR_RO(fan2_input, fan, 1);
596 static SENSOR_DEVICE_ATTR_RW(fan2_min, fan_min, 1);
597 static SENSOR_DEVICE_ATTR_RW(fan2_div, fan_div, 1);
598 
599 /* Alarms */
600 static ssize_t alarms_show(struct device *dev, struct device_attribute *attr,
601 			   char *buf)
602 {
603 	struct vt8231_data *data = vt8231_update_device(dev);
604 	return sprintf(buf, "%d\n", data->alarms);
605 }
606 static DEVICE_ATTR_RO(alarms);
607 
608 static ssize_t alarm_show(struct device *dev, struct device_attribute *attr,
609 			  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 }
615 static SENSOR_DEVICE_ATTR_RO(temp1_alarm, alarm, 4);
616 static SENSOR_DEVICE_ATTR_RO(temp2_alarm, alarm, 11);
617 static SENSOR_DEVICE_ATTR_RO(temp3_alarm, alarm, 0);
618 static SENSOR_DEVICE_ATTR_RO(temp4_alarm, alarm, 1);
619 static SENSOR_DEVICE_ATTR_RO(temp5_alarm, alarm, 3);
620 static SENSOR_DEVICE_ATTR_RO(temp6_alarm, alarm, 8);
621 static SENSOR_DEVICE_ATTR_RO(in0_alarm, alarm, 11);
622 static SENSOR_DEVICE_ATTR_RO(in1_alarm, alarm, 0);
623 static SENSOR_DEVICE_ATTR_RO(in2_alarm, alarm, 1);
624 static SENSOR_DEVICE_ATTR_RO(in3_alarm, alarm, 3);
625 static SENSOR_DEVICE_ATTR_RO(in4_alarm, alarm, 8);
626 static SENSOR_DEVICE_ATTR_RO(in5_alarm, alarm, 2);
627 static SENSOR_DEVICE_ATTR_RO(fan1_alarm, alarm, 6);
628 static SENSOR_DEVICE_ATTR_RO(fan2_alarm, alarm, 7);
629 
630 static ssize_t name_show(struct device *dev, struct device_attribute
631 			 *devattr, char *buf)
632 {
633 	struct vt8231_data *data = dev_get_drvdata(dev);
634 	return sprintf(buf, "%s\n", data->name);
635 }
636 static DEVICE_ATTR_RO(name);
637 
638 static struct attribute *vt8231_attributes_temps[6][5] = {
639 	{
640 		&dev_attr_temp1_input.attr,
641 		&dev_attr_temp1_max_hyst.attr,
642 		&dev_attr_temp1_max.attr,
643 		&sensor_dev_attr_temp1_alarm.dev_attr.attr,
644 		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,
649 		&sensor_dev_attr_temp2_alarm.dev_attr.attr,
650 		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,
655 		&sensor_dev_attr_temp3_alarm.dev_attr.attr,
656 		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,
661 		&sensor_dev_attr_temp4_alarm.dev_attr.attr,
662 		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,
667 		&sensor_dev_attr_temp5_alarm.dev_attr.attr,
668 		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,
673 		&sensor_dev_attr_temp6_alarm.dev_attr.attr,
674 		NULL
675 	}
676 };
677 
678 static 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 
687 static struct attribute *vt8231_attributes_volts[6][5] = {
688 	{
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,
692 		&sensor_dev_attr_in0_alarm.dev_attr.attr,
693 		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,
698 		&sensor_dev_attr_in1_alarm.dev_attr.attr,
699 		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,
704 		&sensor_dev_attr_in2_alarm.dev_attr.attr,
705 		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,
710 		&sensor_dev_attr_in3_alarm.dev_attr.attr,
711 		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,
716 		&sensor_dev_attr_in4_alarm.dev_attr.attr,
717 		NULL
718 	}, {
719 		&dev_attr_in5_input.attr,
720 		&dev_attr_in5_min.attr,
721 		&dev_attr_in5_max.attr,
722 		&sensor_dev_attr_in5_alarm.dev_attr.attr,
723 		NULL
724 	}
725 };
726 
727 static 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 
736 static 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,
743 	&sensor_dev_attr_fan1_alarm.dev_attr.attr,
744 	&sensor_dev_attr_fan2_alarm.dev_attr.attr,
745 	&dev_attr_alarms.attr,
746 	&dev_attr_name.attr,
747 	NULL
748 };
749 
750 static const struct attribute_group vt8231_group = {
751 	.attrs = vt8231_attributes,
752 };
753 
754 static struct platform_driver vt8231_driver = {
755 	.driver = {
756 		.name	= "vt8231",
757 	},
758 	.probe	= vt8231_probe,
759 	.remove	= vt8231_remove,
760 };
761 
762 static const struct pci_device_id vt8231_pci_ids[] = {
763 	{ PCI_DEVICE(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8231_4) },
764 	{ 0, }
765 };
766 
767 MODULE_DEVICE_TABLE(pci, vt8231_pci_ids);
768 
769 static int vt8231_pci_probe(struct pci_dev *dev,
770 				      const struct pci_device_id *id);
771 
772 static struct pci_driver vt8231_pci_driver = {
773 	.name		= "vt8231",
774 	.id_table	= vt8231_pci_ids,
775 	.probe		= vt8231_pci_probe,
776 };
777 
778 static int vt8231_probe(struct platform_device *pdev)
779 {
780 	struct resource *res;
781 	struct vt8231_data *data;
782 	int err = 0, i;
783 
784 	/* Reserve the ISA region */
785 	res = platform_get_resource(pdev, IORESOURCE_IO, 0);
786 	if (!devm_request_region(&pdev->dev, res->start, VT8231_EXTENT,
787 				 vt8231_driver.driver.name)) {
788 		dev_err(&pdev->dev, "Region 0x%lx-0x%lx already in use!\n",
789 			(unsigned long)res->start, (unsigned long)res->end);
790 		return -ENODEV;
791 	}
792 
793 	data = devm_kzalloc(&pdev->dev, sizeof(struct vt8231_data), GFP_KERNEL);
794 	if (!data)
795 		return -ENOMEM;
796 
797 	platform_set_drvdata(pdev, data);
798 	data->addr = res->start;
799 	data->name = "vt8231";
800 
801 	mutex_init(&data->update_lock);
802 	vt8231_init_device(data);
803 
804 	/* Register sysfs hooks */
805 	err = sysfs_create_group(&pdev->dev.kobj, &vt8231_group);
806 	if (err)
807 		return err;
808 
809 	/* Must update device information to find out the config field */
810 	data->uch_config = vt8231_read_value(data, VT8231_REG_UCH_CONFIG);
811 
812 	for (i = 0; i < ARRAY_SIZE(vt8231_group_temps); i++) {
813 		if (ISTEMP(i, data->uch_config)) {
814 			err = sysfs_create_group(&pdev->dev.kobj,
815 						 &vt8231_group_temps[i]);
816 			if (err)
817 				goto exit_remove_files;
818 		}
819 	}
820 
821 	for (i = 0; i < ARRAY_SIZE(vt8231_group_volts); i++) {
822 		if (ISVOLT(i, data->uch_config)) {
823 			err = sysfs_create_group(&pdev->dev.kobj,
824 						 &vt8231_group_volts[i]);
825 			if (err)
826 				goto exit_remove_files;
827 		}
828 	}
829 
830 	data->hwmon_dev = hwmon_device_register(&pdev->dev);
831 	if (IS_ERR(data->hwmon_dev)) {
832 		err = PTR_ERR(data->hwmon_dev);
833 		goto exit_remove_files;
834 	}
835 	return 0;
836 
837 exit_remove_files:
838 	for (i = 0; i < ARRAY_SIZE(vt8231_group_volts); i++)
839 		sysfs_remove_group(&pdev->dev.kobj, &vt8231_group_volts[i]);
840 
841 	for (i = 0; i < ARRAY_SIZE(vt8231_group_temps); i++)
842 		sysfs_remove_group(&pdev->dev.kobj, &vt8231_group_temps[i]);
843 
844 	sysfs_remove_group(&pdev->dev.kobj, &vt8231_group);
845 	return err;
846 }
847 
848 static int vt8231_remove(struct platform_device *pdev)
849 {
850 	struct vt8231_data *data = platform_get_drvdata(pdev);
851 	int i;
852 
853 	hwmon_device_unregister(data->hwmon_dev);
854 
855 	for (i = 0; i < ARRAY_SIZE(vt8231_group_volts); i++)
856 		sysfs_remove_group(&pdev->dev.kobj, &vt8231_group_volts[i]);
857 
858 	for (i = 0; i < ARRAY_SIZE(vt8231_group_temps); i++)
859 		sysfs_remove_group(&pdev->dev.kobj, &vt8231_group_temps[i]);
860 
861 	sysfs_remove_group(&pdev->dev.kobj, &vt8231_group);
862 
863 	return 0;
864 }
865 
866 static void vt8231_init_device(struct vt8231_data *data)
867 {
868 	vt8231_write_value(data, VT8231_REG_TEMP1_CONFIG, 0);
869 	vt8231_write_value(data, VT8231_REG_TEMP2_CONFIG, 0);
870 }
871 
872 static struct vt8231_data *vt8231_update_device(struct device *dev)
873 {
874 	struct vt8231_data *data = dev_get_drvdata(dev);
875 	int i;
876 	u16 low;
877 
878 	mutex_lock(&data->update_lock);
879 
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)) {
884 				data->in[i] = vt8231_read_value(data,
885 						regvolt[i]);
886 				data->in_min[i] = vt8231_read_value(data,
887 						regvoltmin[i]);
888 				data->in_max[i] = vt8231_read_value(data,
889 						regvoltmax[i]);
890 			}
891 		}
892 		for (i = 0; i < 2; i++) {
893 			data->fan[i] = vt8231_read_value(data,
894 						VT8231_REG_FAN(i));
895 			data->fan_min[i] = vt8231_read_value(data,
896 						VT8231_REG_FAN_MIN(i));
897 		}
898 
899 		low = vt8231_read_value(data, VT8231_REG_TEMP_LOW01);
900 		low = (low >> 6) | ((low & 0x30) >> 2)
901 		    | (vt8231_read_value(data, VT8231_REG_TEMP_LOW25) << 4);
902 		for (i = 0; i < 6; i++) {
903 			if (ISTEMP(i, data->uch_config)) {
904 				data->temp[i] = (vt8231_read_value(data,
905 						       regtemp[i]) << 2)
906 						| ((low >> (2 * i)) & 0x03);
907 				data->temp_max[i] = vt8231_read_value(data,
908 						      regtempmax[i]);
909 				data->temp_min[i] = vt8231_read_value(data,
910 						      regtempmin[i]);
911 			}
912 		}
913 
914 		i = vt8231_read_value(data, VT8231_REG_FANDIV);
915 		data->fan_div[0] = (i >> 4) & 0x03;
916 		data->fan_div[1] = i >> 6;
917 		data->alarms = vt8231_read_value(data, VT8231_REG_ALARM1) |
918 			(vt8231_read_value(data, VT8231_REG_ALARM2) << 8);
919 
920 		/* Set alarm flags correctly */
921 		if (!data->fan[0] && data->fan_min[0])
922 			data->alarms |= 0x40;
923 		else if (data->fan[0] && !data->fan_min[0])
924 			data->alarms &= ~0x40;
925 
926 		if (!data->fan[1] && data->fan_min[1])
927 			data->alarms |= 0x80;
928 		else if (data->fan[1] && !data->fan_min[1])
929 			data->alarms &= ~0x80;
930 
931 		data->last_updated = jiffies;
932 		data->valid = 1;
933 	}
934 
935 	mutex_unlock(&data->update_lock);
936 
937 	return data;
938 }
939 
940 static int vt8231_device_add(unsigned short address)
941 {
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 
950 	err = acpi_check_resource_conflict(&res);
951 	if (err)
952 		goto exit;
953 
954 	pdev = platform_device_alloc("vt8231", address);
955 	if (!pdev) {
956 		err = -ENOMEM;
957 		pr_err("Device allocation failed\n");
958 		goto exit;
959 	}
960 
961 	err = platform_device_add_resources(pdev, &res, 1);
962 	if (err) {
963 		pr_err("Device resource addition failed (%d)\n", err);
964 		goto exit_device_put;
965 	}
966 
967 	err = platform_device_add(pdev);
968 	if (err) {
969 		pr_err("Device addition failed (%d)\n", err);
970 		goto exit_device_put;
971 	}
972 
973 	return 0;
974 
975 exit_device_put:
976 	platform_device_put(pdev);
977 exit:
978 	return err;
979 }
980 
981 static int vt8231_pci_probe(struct pci_dev *dev,
982 				const struct pci_device_id *id)
983 {
984 	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 	}
994 
995 	if (PCIBIOS_SUCCESSFUL != pci_read_config_word(dev, VT8231_BASE_REG,
996 							&val))
997 		return -ENODEV;
998 
999 	address = val & ~(VT8231_EXTENT - 1);
1000 	if (address == 0) {
1001 		dev_err(&dev->dev, "base address not set - upgrade BIOS or use force_addr=0xaddr\n");
1002 		return -ENODEV;
1003 	}
1004 
1005 	if (PCIBIOS_SUCCESSFUL != pci_read_config_word(dev, VT8231_ENABLE_REG,
1006 							&val))
1007 		return -ENODEV;
1008 
1009 	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;
1015 	}
1016 
1017 	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 
1024 	/*
1025 	 * Always return failure here.  This is to allow other drivers to bind
1026 	 * 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 	 */
1029 
1030 	/*
1031 	 * We do, however, mark ourselves as using the PCI device to stop it
1032 	 * getting unloaded.
1033 	 */
1034 	s_bridge = pci_dev_get(dev);
1035 	return -ENODEV;
1036 
1037 exit_unregister:
1038 	platform_driver_unregister(&vt8231_driver);
1039 exit:
1040 	return -ENODEV;
1041 }
1042 
1043 static int __init sm_vt8231_init(void)
1044 {
1045 	return pci_register_driver(&vt8231_pci_driver);
1046 }
1047 
1048 static void __exit sm_vt8231_exit(void)
1049 {
1050 	pci_unregister_driver(&vt8231_pci_driver);
1051 	if (s_bridge != NULL) {
1052 		platform_device_unregister(pdev);
1053 		platform_driver_unregister(&vt8231_driver);
1054 		pci_dev_put(s_bridge);
1055 		s_bridge = NULL;
1056 	}
1057 }
1058 
1059 MODULE_AUTHOR("Roger Lucas <vt8231@hiddenengine.co.uk>");
1060 MODULE_DESCRIPTION("VT8231 sensors");
1061 MODULE_LICENSE("GPL");
1062 
1063 module_init(sm_vt8231_init);
1064 module_exit(sm_vt8231_exit);
1065