xref: /openbmc/linux/drivers/hwmon/vt8231.c (revision c2dea0bc)
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/err.h>
26 #include <linux/mutex.h>
27 #include <linux/acpi.h>
28 #include <linux/io.h>
29 
30 static int force_addr;
31 module_param(force_addr, int, 0);
32 MODULE_PARM_DESC(force_addr, "Initialize the base address of the sensors");
33 
34 static struct platform_device *pdev;
35 
36 #define VT8231_EXTENT 0x80
37 #define VT8231_BASE_REG 0x70
38 #define VT8231_ENABLE_REG 0x74
39 
40 #define DRIVER_NAME "vt8231"
41 
42 /*
43  * The VT8231 registers
44  *
45  * The reset value for the input channel configuration is used (Reg 0x4A=0x07)
46  * which sets the selected inputs marked with '*' below if multiple options are
47  * possible:
48  *
49  *		    Voltage Mode	  Temperature Mode
50  *	Sensor	      Linux Id	      Linux Id	      VIA Id
51  *	--------      --------	      --------	      ------
52  *	CPU Diode	N/A		temp1		0
53  *	UIC1		in0		temp2 *		1
54  *	UIC2		in1 *		temp3		2
55  *	UIC3		in2 *		temp4		3
56  *	UIC4		in3 *		temp5		4
57  *	UIC5		in4 *		temp6		5
58  *	3.3V		in5		N/A
59  *
60  * Note that the BIOS may set the configuration register to a different value
61  * to match the motherboard configuration.
62  */
63 
64 /* fans numbered 0-1 */
65 #define VT8231_REG_FAN_MIN(nr)	(0x3b + (nr))
66 #define VT8231_REG_FAN(nr)	(0x29 + (nr))
67 
68 /* Voltage inputs numbered 0-5 */
69 
70 static const u8 regvolt[]    = { 0x21, 0x22, 0x23, 0x24, 0x25, 0x26 };
71 static const u8 regvoltmax[] = { 0x3d, 0x2b, 0x2d, 0x2f, 0x31, 0x33 };
72 static const u8 regvoltmin[] = { 0x3e, 0x2c, 0x2e, 0x30, 0x32, 0x34 };
73 
74 /*
75  * Temperatures are numbered 1-6 according to the Linux kernel specification.
76  *
77  * In the VIA datasheet, however, the temperatures are numbered from zero.
78  * Since it is important that this driver can easily be compared to the VIA
79  * datasheet, we will use the VIA numbering within this driver and map the
80  * kernel sysfs device name to the VIA number in the sysfs callback.
81  */
82 
83 #define VT8231_REG_TEMP_LOW01	0x49
84 #define VT8231_REG_TEMP_LOW25	0x4d
85 
86 static const u8 regtemp[]    = { 0x1f, 0x21, 0x22, 0x23, 0x24, 0x25 };
87 static const u8 regtempmax[] = { 0x39, 0x3d, 0x2b, 0x2d, 0x2f, 0x31 };
88 static const u8 regtempmin[] = { 0x3a, 0x3e, 0x2c, 0x2e, 0x30, 0x32 };
89 
90 #define TEMP_FROM_REG(reg)		(((253 * 4 - (reg)) * 550 + 105) / 210)
91 #define TEMP_MAXMIN_FROM_REG(reg)	(((253 - (reg)) * 2200 + 105) / 210)
92 #define TEMP_MAXMIN_TO_REG(val)		(253 - ((val) * 210 + 1100) / 2200)
93 
94 #define VT8231_REG_CONFIG 0x40
95 #define VT8231_REG_ALARM1 0x41
96 #define VT8231_REG_ALARM2 0x42
97 #define VT8231_REG_FANDIV 0x47
98 #define VT8231_REG_UCH_CONFIG 0x4a
99 #define VT8231_REG_TEMP1_CONFIG 0x4b
100 #define VT8231_REG_TEMP2_CONFIG 0x4c
101 
102 /*
103  * temps 0-5 as numbered in VIA datasheet - see later for mapping to Linux
104  * numbering
105  */
106 #define ISTEMP(i, ch_config) ((i) == 0 ? 1 : \
107 			      ((ch_config) >> ((i)+1)) & 0x01)
108 /* voltages 0-5 */
109 #define ISVOLT(i, ch_config) ((i) == 5 ? 1 : \
110 			      !(((ch_config) >> ((i)+2)) & 0x01))
111 
112 #define DIV_FROM_REG(val) (1 << (val))
113 
114 /*
115  * NB  The values returned here are NOT temperatures.  The calibration curves
116  *     for the thermistor curves are board-specific and must go in the
117  *     sensors.conf file.  Temperature sensors are actually ten bits, but the
118  *     VIA datasheet only considers the 8 MSBs obtained from the regtemp[]
119  *     register.  The temperature value returned should have a magnitude of 3,
120  *     so we use the VIA scaling as the "true" scaling and use the remaining 2
121  *     LSBs as fractional precision.
122  *
123  *     All the on-chip hardware temperature comparisons for the alarms are only
124  *     8-bits wide, and compare against the 8 MSBs of the temperature.  The bits
125  *     in the registers VT8231_REG_TEMP_LOW01 and VT8231_REG_TEMP_LOW25 are
126  *     ignored.
127  */
128 
129 /*
130  ****** FAN RPM CONVERSIONS ********
131  * This chip saturates back at 0, not at 255 like many the other chips.
132  * So, 0 means 0 RPM
133  */
134 static inline u8 FAN_TO_REG(long rpm, int div)
135 {
136 	if (rpm <= 0 || rpm > 1310720)
137 		return 0;
138 	return clamp_val(1310720 / (rpm * div), 1, 255);
139 }
140 
141 #define FAN_FROM_REG(val, div) ((val) == 0 ? 0 : 1310720 / ((val) * (div)))
142 
143 struct vt8231_data {
144 	unsigned short addr;
145 	const char *name;
146 
147 	struct mutex update_lock;
148 	struct device *hwmon_dev;
149 	bool valid;		/* true if following fields are valid */
150 	unsigned long last_updated;	/* In jiffies */
151 
152 	u8 in[6];		/* Register value */
153 	u8 in_max[6];		/* Register value */
154 	u8 in_min[6];		/* Register value */
155 	u16 temp[6];		/* Register value 10 bit, right aligned */
156 	u8 temp_max[6];		/* Register value */
157 	u8 temp_min[6];		/* Register value */
158 	u8 fan[2];		/* Register value */
159 	u8 fan_min[2];		/* Register value */
160 	u8 fan_div[2];		/* Register encoding, shifted right */
161 	u16 alarms;		/* Register encoding */
162 	u8 uch_config;
163 };
164 
165 static struct pci_dev *s_bridge;
166 
167 static inline int vt8231_read_value(struct vt8231_data *data, u8 reg)
168 {
169 	return inb_p(data->addr + reg);
170 }
171 
172 static inline void vt8231_write_value(struct vt8231_data *data, u8 reg,
173 					u8 value)
174 {
175 	outb_p(value, data->addr + reg);
176 }
177 
178 static struct vt8231_data *vt8231_update_device(struct device *dev)
179 {
180 	struct vt8231_data *data = dev_get_drvdata(dev);
181 	int i;
182 	u16 low;
183 
184 	mutex_lock(&data->update_lock);
185 
186 	if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
187 	    || !data->valid) {
188 		for (i = 0; i < 6; i++) {
189 			if (ISVOLT(i, data->uch_config)) {
190 				data->in[i] = vt8231_read_value(data,
191 						regvolt[i]);
192 				data->in_min[i] = vt8231_read_value(data,
193 						regvoltmin[i]);
194 				data->in_max[i] = vt8231_read_value(data,
195 						regvoltmax[i]);
196 			}
197 		}
198 		for (i = 0; i < 2; i++) {
199 			data->fan[i] = vt8231_read_value(data,
200 						VT8231_REG_FAN(i));
201 			data->fan_min[i] = vt8231_read_value(data,
202 						VT8231_REG_FAN_MIN(i));
203 		}
204 
205 		low = vt8231_read_value(data, VT8231_REG_TEMP_LOW01);
206 		low = (low >> 6) | ((low & 0x30) >> 2)
207 		    | (vt8231_read_value(data, VT8231_REG_TEMP_LOW25) << 4);
208 		for (i = 0; i < 6; i++) {
209 			if (ISTEMP(i, data->uch_config)) {
210 				data->temp[i] = (vt8231_read_value(data,
211 						       regtemp[i]) << 2)
212 						| ((low >> (2 * i)) & 0x03);
213 				data->temp_max[i] = vt8231_read_value(data,
214 						      regtempmax[i]);
215 				data->temp_min[i] = vt8231_read_value(data,
216 						      regtempmin[i]);
217 			}
218 		}
219 
220 		i = vt8231_read_value(data, VT8231_REG_FANDIV);
221 		data->fan_div[0] = (i >> 4) & 0x03;
222 		data->fan_div[1] = i >> 6;
223 		data->alarms = vt8231_read_value(data, VT8231_REG_ALARM1) |
224 			(vt8231_read_value(data, VT8231_REG_ALARM2) << 8);
225 
226 		/* Set alarm flags correctly */
227 		if (!data->fan[0] && data->fan_min[0])
228 			data->alarms |= 0x40;
229 		else if (data->fan[0] && !data->fan_min[0])
230 			data->alarms &= ~0x40;
231 
232 		if (!data->fan[1] && data->fan_min[1])
233 			data->alarms |= 0x80;
234 		else if (data->fan[1] && !data->fan_min[1])
235 			data->alarms &= ~0x80;
236 
237 		data->last_updated = jiffies;
238 		data->valid = true;
239 	}
240 
241 	mutex_unlock(&data->update_lock);
242 
243 	return data;
244 }
245 
246 /* following are the sysfs callback functions */
247 static ssize_t in_show(struct device *dev, struct device_attribute *attr,
248 		       char *buf)
249 {
250 	struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
251 	int nr = sensor_attr->index;
252 	struct vt8231_data *data = vt8231_update_device(dev);
253 
254 	return sprintf(buf, "%d\n", ((data->in[nr] - 3) * 10000) / 958);
255 }
256 
257 static ssize_t in_min_show(struct device *dev, struct device_attribute *attr,
258 			   char *buf)
259 {
260 	struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
261 	int nr = sensor_attr->index;
262 	struct vt8231_data *data = vt8231_update_device(dev);
263 
264 	return sprintf(buf, "%d\n", ((data->in_min[nr] - 3) * 10000) / 958);
265 }
266 
267 static ssize_t in_max_show(struct device *dev, struct device_attribute *attr,
268 			   char *buf)
269 {
270 	struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
271 	int nr = sensor_attr->index;
272 	struct vt8231_data *data = vt8231_update_device(dev);
273 
274 	return sprintf(buf, "%d\n", (((data->in_max[nr] - 3) * 10000) / 958));
275 }
276 
277 static ssize_t in_min_store(struct device *dev, struct device_attribute *attr,
278 			    const char *buf, size_t count)
279 {
280 	struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
281 	int nr = sensor_attr->index;
282 	struct vt8231_data *data = dev_get_drvdata(dev);
283 	unsigned long val;
284 	int err;
285 
286 	err = kstrtoul(buf, 10, &val);
287 	if (err)
288 		return err;
289 
290 	mutex_lock(&data->update_lock);
291 	data->in_min[nr] = clamp_val(((val * 958) / 10000) + 3, 0, 255);
292 	vt8231_write_value(data, regvoltmin[nr], data->in_min[nr]);
293 	mutex_unlock(&data->update_lock);
294 	return count;
295 }
296 
297 static ssize_t in_max_store(struct device *dev, struct device_attribute *attr,
298 			    const char *buf, size_t count)
299 {
300 	struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
301 	int nr = sensor_attr->index;
302 	struct vt8231_data *data = dev_get_drvdata(dev);
303 	unsigned long val;
304 	int err;
305 
306 	err = kstrtoul(buf, 10, &val);
307 	if (err)
308 		return err;
309 
310 	mutex_lock(&data->update_lock);
311 	data->in_max[nr] = clamp_val(((val * 958) / 10000) + 3, 0, 255);
312 	vt8231_write_value(data, regvoltmax[nr], data->in_max[nr]);
313 	mutex_unlock(&data->update_lock);
314 	return count;
315 }
316 
317 /* Special case for input 5 as this has 3.3V scaling built into the chip */
318 static ssize_t in5_input_show(struct device *dev,
319 			      struct device_attribute *attr, char *buf)
320 {
321 	struct vt8231_data *data = vt8231_update_device(dev);
322 
323 	return sprintf(buf, "%d\n",
324 		(((data->in[5] - 3) * 10000 * 54) / (958 * 34)));
325 }
326 
327 static ssize_t in5_min_show(struct device *dev, struct device_attribute *attr,
328 		char *buf)
329 {
330 	struct vt8231_data *data = vt8231_update_device(dev);
331 
332 	return sprintf(buf, "%d\n",
333 		(((data->in_min[5] - 3) * 10000 * 54) / (958 * 34)));
334 }
335 
336 static ssize_t in5_max_show(struct device *dev, struct device_attribute *attr,
337 		char *buf)
338 {
339 	struct vt8231_data *data = vt8231_update_device(dev);
340 
341 	return sprintf(buf, "%d\n",
342 		(((data->in_max[5] - 3) * 10000 * 54) / (958 * 34)));
343 }
344 
345 static ssize_t in5_min_store(struct device *dev,
346 			     struct device_attribute *attr, const char *buf,
347 			     size_t count)
348 {
349 	struct vt8231_data *data = dev_get_drvdata(dev);
350 	unsigned long val;
351 	int err;
352 
353 	err = kstrtoul(buf, 10, &val);
354 	if (err)
355 		return err;
356 
357 	mutex_lock(&data->update_lock);
358 	data->in_min[5] = clamp_val(((val * 958 * 34) / (10000 * 54)) + 3,
359 				    0, 255);
360 	vt8231_write_value(data, regvoltmin[5], data->in_min[5]);
361 	mutex_unlock(&data->update_lock);
362 	return count;
363 }
364 
365 static ssize_t in5_max_store(struct device *dev,
366 			     struct device_attribute *attr, const char *buf,
367 			     size_t count)
368 {
369 	struct vt8231_data *data = dev_get_drvdata(dev);
370 	unsigned long val;
371 	int err;
372 
373 	err = kstrtoul(buf, 10, &val);
374 	if (err)
375 		return err;
376 
377 	mutex_lock(&data->update_lock);
378 	data->in_max[5] = clamp_val(((val * 958 * 34) / (10000 * 54)) + 3,
379 				    0, 255);
380 	vt8231_write_value(data, regvoltmax[5], data->in_max[5]);
381 	mutex_unlock(&data->update_lock);
382 	return count;
383 }
384 
385 static SENSOR_DEVICE_ATTR_RO(in0_input, in, 0);
386 static SENSOR_DEVICE_ATTR_RW(in0_min, in_min, 0);
387 static SENSOR_DEVICE_ATTR_RW(in0_max, in_max, 0);
388 static SENSOR_DEVICE_ATTR_RO(in1_input, in, 1);
389 static SENSOR_DEVICE_ATTR_RW(in1_min, in_min, 1);
390 static SENSOR_DEVICE_ATTR_RW(in1_max, in_max, 1);
391 static SENSOR_DEVICE_ATTR_RO(in2_input, in, 2);
392 static SENSOR_DEVICE_ATTR_RW(in2_min, in_min, 2);
393 static SENSOR_DEVICE_ATTR_RW(in2_max, in_max, 2);
394 static SENSOR_DEVICE_ATTR_RO(in3_input, in, 3);
395 static SENSOR_DEVICE_ATTR_RW(in3_min, in_min, 3);
396 static SENSOR_DEVICE_ATTR_RW(in3_max, in_max, 3);
397 static SENSOR_DEVICE_ATTR_RO(in4_input, in, 4);
398 static SENSOR_DEVICE_ATTR_RW(in4_min, in_min, 4);
399 static SENSOR_DEVICE_ATTR_RW(in4_max, in_max, 4);
400 
401 static DEVICE_ATTR_RO(in5_input);
402 static DEVICE_ATTR_RW(in5_min);
403 static DEVICE_ATTR_RW(in5_max);
404 
405 /* Temperatures */
406 static ssize_t temp1_input_show(struct device *dev,
407 				struct device_attribute *attr, char *buf)
408 {
409 	struct vt8231_data *data = vt8231_update_device(dev);
410 	return sprintf(buf, "%d\n", data->temp[0] * 250);
411 }
412 
413 static ssize_t temp1_max_show(struct device *dev, struct device_attribute *attr,
414 		char *buf)
415 {
416 	struct vt8231_data *data = vt8231_update_device(dev);
417 	return sprintf(buf, "%d\n", data->temp_max[0] * 1000);
418 }
419 
420 static ssize_t temp1_max_hyst_show(struct device *dev,
421 				   struct device_attribute *attr, char *buf)
422 {
423 	struct vt8231_data *data = vt8231_update_device(dev);
424 	return sprintf(buf, "%d\n", data->temp_min[0] * 1000);
425 }
426 
427 static ssize_t temp1_max_store(struct device *dev,
428 			       struct device_attribute *attr, const char *buf,
429 			       size_t count)
430 {
431 	struct vt8231_data *data = dev_get_drvdata(dev);
432 	long val;
433 	int err;
434 
435 	err = kstrtol(buf, 10, &val);
436 	if (err)
437 		return err;
438 
439 	mutex_lock(&data->update_lock);
440 	data->temp_max[0] = clamp_val((val + 500) / 1000, 0, 255);
441 	vt8231_write_value(data, regtempmax[0], data->temp_max[0]);
442 	mutex_unlock(&data->update_lock);
443 	return count;
444 }
445 static ssize_t temp1_max_hyst_store(struct device *dev,
446 				    struct device_attribute *attr,
447 				    const char *buf, size_t count)
448 {
449 	struct vt8231_data *data = dev_get_drvdata(dev);
450 	long val;
451 	int err;
452 
453 	err = kstrtol(buf, 10, &val);
454 	if (err)
455 		return err;
456 
457 	mutex_lock(&data->update_lock);
458 	data->temp_min[0] = clamp_val((val + 500) / 1000, 0, 255);
459 	vt8231_write_value(data, regtempmin[0], data->temp_min[0]);
460 	mutex_unlock(&data->update_lock);
461 	return count;
462 }
463 
464 static ssize_t temp_show(struct device *dev, struct device_attribute *attr,
465 			 char *buf)
466 {
467 	struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
468 	int nr = sensor_attr->index;
469 	struct vt8231_data *data = vt8231_update_device(dev);
470 	return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr]));
471 }
472 
473 static ssize_t temp_max_show(struct device *dev,
474 			     struct device_attribute *attr, char *buf)
475 {
476 	struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
477 	int nr = sensor_attr->index;
478 	struct vt8231_data *data = vt8231_update_device(dev);
479 	return sprintf(buf, "%d\n", TEMP_MAXMIN_FROM_REG(data->temp_max[nr]));
480 }
481 
482 static ssize_t temp_min_show(struct device *dev,
483 			     struct device_attribute *attr, char *buf)
484 {
485 	struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
486 	int nr = sensor_attr->index;
487 	struct vt8231_data *data = vt8231_update_device(dev);
488 	return sprintf(buf, "%d\n", TEMP_MAXMIN_FROM_REG(data->temp_min[nr]));
489 }
490 
491 static ssize_t temp_max_store(struct device *dev,
492 			      struct device_attribute *attr, const char *buf,
493 			      size_t count)
494 {
495 	struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
496 	int nr = sensor_attr->index;
497 	struct vt8231_data *data = dev_get_drvdata(dev);
498 	long val;
499 	int err;
500 
501 	err = kstrtol(buf, 10, &val);
502 	if (err)
503 		return err;
504 
505 	mutex_lock(&data->update_lock);
506 	data->temp_max[nr] = clamp_val(TEMP_MAXMIN_TO_REG(val), 0, 255);
507 	vt8231_write_value(data, regtempmax[nr], data->temp_max[nr]);
508 	mutex_unlock(&data->update_lock);
509 	return count;
510 }
511 static ssize_t temp_min_store(struct device *dev,
512 			      struct device_attribute *attr, const char *buf,
513 			      size_t count)
514 {
515 	struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
516 	int nr = sensor_attr->index;
517 	struct vt8231_data *data = dev_get_drvdata(dev);
518 	long val;
519 	int err;
520 
521 	err = kstrtol(buf, 10, &val);
522 	if (err)
523 		return err;
524 
525 	mutex_lock(&data->update_lock);
526 	data->temp_min[nr] = clamp_val(TEMP_MAXMIN_TO_REG(val), 0, 255);
527 	vt8231_write_value(data, regtempmin[nr], data->temp_min[nr]);
528 	mutex_unlock(&data->update_lock);
529 	return count;
530 }
531 
532 /*
533  * Note that these map the Linux temperature sensor numbering (1-6) to the VIA
534  * temperature sensor numbering (0-5)
535  */
536 
537 static DEVICE_ATTR_RO(temp1_input);
538 static DEVICE_ATTR_RW(temp1_max);
539 static DEVICE_ATTR_RW(temp1_max_hyst);
540 
541 static SENSOR_DEVICE_ATTR_RO(temp2_input, temp, 1);
542 static SENSOR_DEVICE_ATTR_RW(temp2_max, temp_max, 1);
543 static SENSOR_DEVICE_ATTR_RW(temp2_max_hyst, temp_min, 1);
544 static SENSOR_DEVICE_ATTR_RO(temp3_input, temp, 2);
545 static SENSOR_DEVICE_ATTR_RW(temp3_max, temp_max, 2);
546 static SENSOR_DEVICE_ATTR_RW(temp3_max_hyst, temp_min, 2);
547 static SENSOR_DEVICE_ATTR_RO(temp4_input, temp, 3);
548 static SENSOR_DEVICE_ATTR_RW(temp4_max, temp_max, 3);
549 static SENSOR_DEVICE_ATTR_RW(temp4_max_hyst, temp_min, 3);
550 static SENSOR_DEVICE_ATTR_RO(temp5_input, temp, 4);
551 static SENSOR_DEVICE_ATTR_RW(temp5_max, temp_max, 4);
552 static SENSOR_DEVICE_ATTR_RW(temp5_max_hyst, temp_min, 4);
553 static SENSOR_DEVICE_ATTR_RO(temp6_input, temp, 5);
554 static SENSOR_DEVICE_ATTR_RW(temp6_max, temp_max, 5);
555 static SENSOR_DEVICE_ATTR_RW(temp6_max_hyst, temp_min, 5);
556 
557 /* Fans */
558 static ssize_t fan_show(struct device *dev, struct device_attribute *attr,
559 			char *buf)
560 {
561 	struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
562 	int nr = sensor_attr->index;
563 	struct vt8231_data *data = vt8231_update_device(dev);
564 	return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr],
565 				DIV_FROM_REG(data->fan_div[nr])));
566 }
567 
568 static ssize_t fan_min_show(struct device *dev, struct device_attribute *attr,
569 			    char *buf)
570 {
571 	struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
572 	int nr = sensor_attr->index;
573 	struct vt8231_data *data = vt8231_update_device(dev);
574 	return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[nr],
575 			DIV_FROM_REG(data->fan_div[nr])));
576 }
577 
578 static ssize_t fan_div_show(struct device *dev, struct device_attribute *attr,
579 			    char *buf)
580 {
581 	struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
582 	int nr = sensor_attr->index;
583 	struct vt8231_data *data = vt8231_update_device(dev);
584 	return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]));
585 }
586 
587 static ssize_t fan_min_store(struct device *dev,
588 			     struct device_attribute *attr, const char *buf,
589 			     size_t count)
590 {
591 	struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
592 	int nr = sensor_attr->index;
593 	struct vt8231_data *data = dev_get_drvdata(dev);
594 	unsigned long val;
595 	int err;
596 
597 	err = kstrtoul(buf, 10, &val);
598 	if (err)
599 		return err;
600 
601 	mutex_lock(&data->update_lock);
602 	data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
603 	vt8231_write_value(data, VT8231_REG_FAN_MIN(nr), data->fan_min[nr]);
604 	mutex_unlock(&data->update_lock);
605 	return count;
606 }
607 
608 static ssize_t fan_div_store(struct device *dev,
609 			     struct device_attribute *attr, const char *buf,
610 			     size_t count)
611 {
612 	struct vt8231_data *data = dev_get_drvdata(dev);
613 	struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
614 	unsigned long val;
615 	int nr = sensor_attr->index;
616 	int old = vt8231_read_value(data, VT8231_REG_FANDIV);
617 	long min = FAN_FROM_REG(data->fan_min[nr],
618 				 DIV_FROM_REG(data->fan_div[nr]));
619 	int err;
620 
621 	err = kstrtoul(buf, 10, &val);
622 	if (err)
623 		return err;
624 
625 	mutex_lock(&data->update_lock);
626 	switch (val) {
627 	case 1:
628 		data->fan_div[nr] = 0;
629 		break;
630 	case 2:
631 		data->fan_div[nr] = 1;
632 		break;
633 	case 4:
634 		data->fan_div[nr] = 2;
635 		break;
636 	case 8:
637 		data->fan_div[nr] = 3;
638 		break;
639 	default:
640 		dev_err(dev,
641 			"fan_div value %ld not supported. Choose one of 1, 2, 4 or 8!\n",
642 			val);
643 		mutex_unlock(&data->update_lock);
644 		return -EINVAL;
645 	}
646 
647 	/* Correct the fan minimum speed */
648 	data->fan_min[nr] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
649 	vt8231_write_value(data, VT8231_REG_FAN_MIN(nr), data->fan_min[nr]);
650 
651 	old = (old & 0x0f) | (data->fan_div[1] << 6) | (data->fan_div[0] << 4);
652 	vt8231_write_value(data, VT8231_REG_FANDIV, old);
653 	mutex_unlock(&data->update_lock);
654 	return count;
655 }
656 
657 static SENSOR_DEVICE_ATTR_RO(fan1_input, fan, 0);
658 static SENSOR_DEVICE_ATTR_RW(fan1_min, fan_min, 0);
659 static SENSOR_DEVICE_ATTR_RW(fan1_div, fan_div, 0);
660 static SENSOR_DEVICE_ATTR_RO(fan2_input, fan, 1);
661 static SENSOR_DEVICE_ATTR_RW(fan2_min, fan_min, 1);
662 static SENSOR_DEVICE_ATTR_RW(fan2_div, fan_div, 1);
663 
664 /* Alarms */
665 static ssize_t alarms_show(struct device *dev, struct device_attribute *attr,
666 			   char *buf)
667 {
668 	struct vt8231_data *data = vt8231_update_device(dev);
669 	return sprintf(buf, "%d\n", data->alarms);
670 }
671 static DEVICE_ATTR_RO(alarms);
672 
673 static ssize_t alarm_show(struct device *dev, struct device_attribute *attr,
674 			  char *buf)
675 {
676 	int bitnr = to_sensor_dev_attr(attr)->index;
677 	struct vt8231_data *data = vt8231_update_device(dev);
678 	return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
679 }
680 static SENSOR_DEVICE_ATTR_RO(temp1_alarm, alarm, 4);
681 static SENSOR_DEVICE_ATTR_RO(temp2_alarm, alarm, 11);
682 static SENSOR_DEVICE_ATTR_RO(temp3_alarm, alarm, 0);
683 static SENSOR_DEVICE_ATTR_RO(temp4_alarm, alarm, 1);
684 static SENSOR_DEVICE_ATTR_RO(temp5_alarm, alarm, 3);
685 static SENSOR_DEVICE_ATTR_RO(temp6_alarm, alarm, 8);
686 static SENSOR_DEVICE_ATTR_RO(in0_alarm, alarm, 11);
687 static SENSOR_DEVICE_ATTR_RO(in1_alarm, alarm, 0);
688 static SENSOR_DEVICE_ATTR_RO(in2_alarm, alarm, 1);
689 static SENSOR_DEVICE_ATTR_RO(in3_alarm, alarm, 3);
690 static SENSOR_DEVICE_ATTR_RO(in4_alarm, alarm, 8);
691 static SENSOR_DEVICE_ATTR_RO(in5_alarm, alarm, 2);
692 static SENSOR_DEVICE_ATTR_RO(fan1_alarm, alarm, 6);
693 static SENSOR_DEVICE_ATTR_RO(fan2_alarm, alarm, 7);
694 
695 static ssize_t name_show(struct device *dev, struct device_attribute
696 			 *devattr, char *buf)
697 {
698 	struct vt8231_data *data = dev_get_drvdata(dev);
699 	return sprintf(buf, "%s\n", data->name);
700 }
701 static DEVICE_ATTR_RO(name);
702 
703 static struct attribute *vt8231_attributes_temps[6][5] = {
704 	{
705 		&dev_attr_temp1_input.attr,
706 		&dev_attr_temp1_max_hyst.attr,
707 		&dev_attr_temp1_max.attr,
708 		&sensor_dev_attr_temp1_alarm.dev_attr.attr,
709 		NULL
710 	}, {
711 		&sensor_dev_attr_temp2_input.dev_attr.attr,
712 		&sensor_dev_attr_temp2_max_hyst.dev_attr.attr,
713 		&sensor_dev_attr_temp2_max.dev_attr.attr,
714 		&sensor_dev_attr_temp2_alarm.dev_attr.attr,
715 		NULL
716 	}, {
717 		&sensor_dev_attr_temp3_input.dev_attr.attr,
718 		&sensor_dev_attr_temp3_max_hyst.dev_attr.attr,
719 		&sensor_dev_attr_temp3_max.dev_attr.attr,
720 		&sensor_dev_attr_temp3_alarm.dev_attr.attr,
721 		NULL
722 	}, {
723 		&sensor_dev_attr_temp4_input.dev_attr.attr,
724 		&sensor_dev_attr_temp4_max_hyst.dev_attr.attr,
725 		&sensor_dev_attr_temp4_max.dev_attr.attr,
726 		&sensor_dev_attr_temp4_alarm.dev_attr.attr,
727 		NULL
728 	}, {
729 		&sensor_dev_attr_temp5_input.dev_attr.attr,
730 		&sensor_dev_attr_temp5_max_hyst.dev_attr.attr,
731 		&sensor_dev_attr_temp5_max.dev_attr.attr,
732 		&sensor_dev_attr_temp5_alarm.dev_attr.attr,
733 		NULL
734 	}, {
735 		&sensor_dev_attr_temp6_input.dev_attr.attr,
736 		&sensor_dev_attr_temp6_max_hyst.dev_attr.attr,
737 		&sensor_dev_attr_temp6_max.dev_attr.attr,
738 		&sensor_dev_attr_temp6_alarm.dev_attr.attr,
739 		NULL
740 	}
741 };
742 
743 static const struct attribute_group vt8231_group_temps[6] = {
744 	{ .attrs = vt8231_attributes_temps[0] },
745 	{ .attrs = vt8231_attributes_temps[1] },
746 	{ .attrs = vt8231_attributes_temps[2] },
747 	{ .attrs = vt8231_attributes_temps[3] },
748 	{ .attrs = vt8231_attributes_temps[4] },
749 	{ .attrs = vt8231_attributes_temps[5] },
750 };
751 
752 static struct attribute *vt8231_attributes_volts[6][5] = {
753 	{
754 		&sensor_dev_attr_in0_input.dev_attr.attr,
755 		&sensor_dev_attr_in0_min.dev_attr.attr,
756 		&sensor_dev_attr_in0_max.dev_attr.attr,
757 		&sensor_dev_attr_in0_alarm.dev_attr.attr,
758 		NULL
759 	}, {
760 		&sensor_dev_attr_in1_input.dev_attr.attr,
761 		&sensor_dev_attr_in1_min.dev_attr.attr,
762 		&sensor_dev_attr_in1_max.dev_attr.attr,
763 		&sensor_dev_attr_in1_alarm.dev_attr.attr,
764 		NULL
765 	}, {
766 		&sensor_dev_attr_in2_input.dev_attr.attr,
767 		&sensor_dev_attr_in2_min.dev_attr.attr,
768 		&sensor_dev_attr_in2_max.dev_attr.attr,
769 		&sensor_dev_attr_in2_alarm.dev_attr.attr,
770 		NULL
771 	}, {
772 		&sensor_dev_attr_in3_input.dev_attr.attr,
773 		&sensor_dev_attr_in3_min.dev_attr.attr,
774 		&sensor_dev_attr_in3_max.dev_attr.attr,
775 		&sensor_dev_attr_in3_alarm.dev_attr.attr,
776 		NULL
777 	}, {
778 		&sensor_dev_attr_in4_input.dev_attr.attr,
779 		&sensor_dev_attr_in4_min.dev_attr.attr,
780 		&sensor_dev_attr_in4_max.dev_attr.attr,
781 		&sensor_dev_attr_in4_alarm.dev_attr.attr,
782 		NULL
783 	}, {
784 		&dev_attr_in5_input.attr,
785 		&dev_attr_in5_min.attr,
786 		&dev_attr_in5_max.attr,
787 		&sensor_dev_attr_in5_alarm.dev_attr.attr,
788 		NULL
789 	}
790 };
791 
792 static const struct attribute_group vt8231_group_volts[6] = {
793 	{ .attrs = vt8231_attributes_volts[0] },
794 	{ .attrs = vt8231_attributes_volts[1] },
795 	{ .attrs = vt8231_attributes_volts[2] },
796 	{ .attrs = vt8231_attributes_volts[3] },
797 	{ .attrs = vt8231_attributes_volts[4] },
798 	{ .attrs = vt8231_attributes_volts[5] },
799 };
800 
801 static struct attribute *vt8231_attributes[] = {
802 	&sensor_dev_attr_fan1_input.dev_attr.attr,
803 	&sensor_dev_attr_fan2_input.dev_attr.attr,
804 	&sensor_dev_attr_fan1_min.dev_attr.attr,
805 	&sensor_dev_attr_fan2_min.dev_attr.attr,
806 	&sensor_dev_attr_fan1_div.dev_attr.attr,
807 	&sensor_dev_attr_fan2_div.dev_attr.attr,
808 	&sensor_dev_attr_fan1_alarm.dev_attr.attr,
809 	&sensor_dev_attr_fan2_alarm.dev_attr.attr,
810 	&dev_attr_alarms.attr,
811 	&dev_attr_name.attr,
812 	NULL
813 };
814 
815 static const struct attribute_group vt8231_group = {
816 	.attrs = vt8231_attributes,
817 };
818 
819 static void vt8231_init_device(struct vt8231_data *data)
820 {
821 	vt8231_write_value(data, VT8231_REG_TEMP1_CONFIG, 0);
822 	vt8231_write_value(data, VT8231_REG_TEMP2_CONFIG, 0);
823 }
824 
825 static int vt8231_probe(struct platform_device *pdev)
826 {
827 	struct resource *res;
828 	struct vt8231_data *data;
829 	int err = 0, i;
830 
831 	/* Reserve the ISA region */
832 	res = platform_get_resource(pdev, IORESOURCE_IO, 0);
833 	if (!devm_request_region(&pdev->dev, res->start, VT8231_EXTENT,
834 				 DRIVER_NAME)) {
835 		dev_err(&pdev->dev, "Region 0x%lx-0x%lx already in use!\n",
836 			(unsigned long)res->start, (unsigned long)res->end);
837 		return -ENODEV;
838 	}
839 
840 	data = devm_kzalloc(&pdev->dev, sizeof(struct vt8231_data), GFP_KERNEL);
841 	if (!data)
842 		return -ENOMEM;
843 
844 	platform_set_drvdata(pdev, data);
845 	data->addr = res->start;
846 	data->name = DRIVER_NAME;
847 
848 	mutex_init(&data->update_lock);
849 	vt8231_init_device(data);
850 
851 	/* Register sysfs hooks */
852 	err = sysfs_create_group(&pdev->dev.kobj, &vt8231_group);
853 	if (err)
854 		return err;
855 
856 	/* Must update device information to find out the config field */
857 	data->uch_config = vt8231_read_value(data, VT8231_REG_UCH_CONFIG);
858 
859 	for (i = 0; i < ARRAY_SIZE(vt8231_group_temps); i++) {
860 		if (ISTEMP(i, data->uch_config)) {
861 			err = sysfs_create_group(&pdev->dev.kobj,
862 						 &vt8231_group_temps[i]);
863 			if (err)
864 				goto exit_remove_files;
865 		}
866 	}
867 
868 	for (i = 0; i < ARRAY_SIZE(vt8231_group_volts); i++) {
869 		if (ISVOLT(i, data->uch_config)) {
870 			err = sysfs_create_group(&pdev->dev.kobj,
871 						 &vt8231_group_volts[i]);
872 			if (err)
873 				goto exit_remove_files;
874 		}
875 	}
876 
877 	data->hwmon_dev = hwmon_device_register(&pdev->dev);
878 	if (IS_ERR(data->hwmon_dev)) {
879 		err = PTR_ERR(data->hwmon_dev);
880 		goto exit_remove_files;
881 	}
882 	return 0;
883 
884 exit_remove_files:
885 	for (i = 0; i < ARRAY_SIZE(vt8231_group_volts); i++)
886 		sysfs_remove_group(&pdev->dev.kobj, &vt8231_group_volts[i]);
887 
888 	for (i = 0; i < ARRAY_SIZE(vt8231_group_temps); i++)
889 		sysfs_remove_group(&pdev->dev.kobj, &vt8231_group_temps[i]);
890 
891 	sysfs_remove_group(&pdev->dev.kobj, &vt8231_group);
892 	return err;
893 }
894 
895 static int vt8231_remove(struct platform_device *pdev)
896 {
897 	struct vt8231_data *data = platform_get_drvdata(pdev);
898 	int i;
899 
900 	hwmon_device_unregister(data->hwmon_dev);
901 
902 	for (i = 0; i < ARRAY_SIZE(vt8231_group_volts); i++)
903 		sysfs_remove_group(&pdev->dev.kobj, &vt8231_group_volts[i]);
904 
905 	for (i = 0; i < ARRAY_SIZE(vt8231_group_temps); i++)
906 		sysfs_remove_group(&pdev->dev.kobj, &vt8231_group_temps[i]);
907 
908 	sysfs_remove_group(&pdev->dev.kobj, &vt8231_group);
909 
910 	return 0;
911 }
912 
913 
914 static struct platform_driver vt8231_driver = {
915 	.driver = {
916 		.name	= DRIVER_NAME,
917 	},
918 	.probe	= vt8231_probe,
919 	.remove	= vt8231_remove,
920 };
921 
922 static const struct pci_device_id vt8231_pci_ids[] = {
923 	{ PCI_DEVICE(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8231_4) },
924 	{ 0, }
925 };
926 
927 MODULE_DEVICE_TABLE(pci, vt8231_pci_ids);
928 
929 static int vt8231_device_add(unsigned short address)
930 {
931 	struct resource res = {
932 		.start	= address,
933 		.end	= address + VT8231_EXTENT - 1,
934 		.name	= DRIVER_NAME,
935 		.flags	= IORESOURCE_IO,
936 	};
937 	int err;
938 
939 	err = acpi_check_resource_conflict(&res);
940 	if (err)
941 		goto exit;
942 
943 	pdev = platform_device_alloc(DRIVER_NAME, address);
944 	if (!pdev) {
945 		err = -ENOMEM;
946 		pr_err("Device allocation failed\n");
947 		goto exit;
948 	}
949 
950 	err = platform_device_add_resources(pdev, &res, 1);
951 	if (err) {
952 		pr_err("Device resource addition failed (%d)\n", err);
953 		goto exit_device_put;
954 	}
955 
956 	err = platform_device_add(pdev);
957 	if (err) {
958 		pr_err("Device addition failed (%d)\n", err);
959 		goto exit_device_put;
960 	}
961 
962 	return 0;
963 
964 exit_device_put:
965 	platform_device_put(pdev);
966 exit:
967 	return err;
968 }
969 
970 static int vt8231_pci_probe(struct pci_dev *dev,
971 				const struct pci_device_id *id)
972 {
973 	u16 address, val;
974 	if (force_addr) {
975 		address = force_addr & 0xff00;
976 		dev_warn(&dev->dev, "Forcing ISA address 0x%x\n",
977 			 address);
978 
979 		if (PCIBIOS_SUCCESSFUL !=
980 		    pci_write_config_word(dev, VT8231_BASE_REG, address | 1))
981 			return -ENODEV;
982 	}
983 
984 	pci_read_config_word(dev, VT8231_BASE_REG, &val);
985 	if (val == (u16)~0)
986 		return -ENODEV;
987 
988 	address = val & ~(VT8231_EXTENT - 1);
989 	if (address == 0) {
990 		dev_err(&dev->dev, "base address not set - upgrade BIOS or use force_addr=0xaddr\n");
991 		return -ENODEV;
992 	}
993 
994 	pci_read_config_word(dev, VT8231_ENABLE_REG, &val);
995 	if (val == (u16)~0)
996 		return -ENODEV;
997 
998 	if (!(val & 0x0001)) {
999 		dev_warn(&dev->dev, "enabling sensors\n");
1000 		if (PCIBIOS_SUCCESSFUL !=
1001 			pci_write_config_word(dev, VT8231_ENABLE_REG,
1002 							val | 0x0001))
1003 			return -ENODEV;
1004 	}
1005 
1006 	if (platform_driver_register(&vt8231_driver))
1007 		goto exit;
1008 
1009 	/* Sets global pdev as a side effect */
1010 	if (vt8231_device_add(address))
1011 		goto exit_unregister;
1012 
1013 	/*
1014 	 * Always return failure here.  This is to allow other drivers to bind
1015 	 * to this pci device.  We don't really want to have control over the
1016 	 * pci device, we only wanted to read as few register values from it.
1017 	 */
1018 
1019 	/*
1020 	 * We do, however, mark ourselves as using the PCI device to stop it
1021 	 * getting unloaded.
1022 	 */
1023 	s_bridge = pci_dev_get(dev);
1024 	return -ENODEV;
1025 
1026 exit_unregister:
1027 	platform_driver_unregister(&vt8231_driver);
1028 exit:
1029 	return -ENODEV;
1030 }
1031 
1032 static struct pci_driver vt8231_pci_driver = {
1033 	.name		= DRIVER_NAME,
1034 	.id_table	= vt8231_pci_ids,
1035 	.probe		= vt8231_pci_probe,
1036 };
1037 
1038 static int __init sm_vt8231_init(void)
1039 {
1040 	return pci_register_driver(&vt8231_pci_driver);
1041 }
1042 
1043 static void __exit sm_vt8231_exit(void)
1044 {
1045 	pci_unregister_driver(&vt8231_pci_driver);
1046 	if (s_bridge != NULL) {
1047 		platform_device_unregister(pdev);
1048 		platform_driver_unregister(&vt8231_driver);
1049 		pci_dev_put(s_bridge);
1050 		s_bridge = NULL;
1051 	}
1052 }
1053 
1054 MODULE_AUTHOR("Roger Lucas <vt8231@hiddenengine.co.uk>");
1055 MODULE_DESCRIPTION("VT8231 sensors");
1056 MODULE_LICENSE("GPL");
1057 
1058 module_init(sm_vt8231_init);
1059 module_exit(sm_vt8231_exit);
1060