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