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