xref: /openbmc/linux/drivers/hwmon/pc87360.c (revision 0c7beb2d)
1 /*
2  *  pc87360.c - Part of lm_sensors, Linux kernel modules
3  *              for hardware monitoring
4  *  Copyright (C) 2004, 2007 Jean Delvare <jdelvare@suse.de>
5  *
6  *  Copied from smsc47m1.c:
7  *  Copyright (C) 2002 Mark D. Studebaker <mdsxyz123@yahoo.com>
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  *  Supports the following chips:
24  *
25  *  Chip        #vin    #fan    #pwm    #temp   devid
26  *  PC87360     -       2       2       -       0xE1
27  *  PC87363     -       2       2       -       0xE8
28  *  PC87364     -       3       3       -       0xE4
29  *  PC87365     11      3       3       2       0xE5
30  *  PC87366     11      3       3       3-4     0xE9
31  *
32  *  This driver assumes that no more than one chip is present, and one of
33  *  the standard Super-I/O addresses is used (0x2E/0x2F or 0x4E/0x4F).
34  */
35 
36 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
37 
38 #include <linux/module.h>
39 #include <linux/init.h>
40 #include <linux/slab.h>
41 #include <linux/jiffies.h>
42 #include <linux/platform_device.h>
43 #include <linux/hwmon.h>
44 #include <linux/hwmon-sysfs.h>
45 #include <linux/hwmon-vid.h>
46 #include <linux/err.h>
47 #include <linux/mutex.h>
48 #include <linux/acpi.h>
49 #include <linux/io.h>
50 
51 static u8 devid;
52 static struct platform_device *pdev;
53 static unsigned short extra_isa[3];
54 static u8 confreg[4];
55 
56 static int init = 1;
57 module_param(init, int, 0);
58 MODULE_PARM_DESC(init,
59 "Chip initialization level:\n"
60 " 0: None\n"
61 "*1: Forcibly enable internal voltage and temperature channels, except in9\n"
62 " 2: Forcibly enable all voltage and temperature channels, except in9\n"
63 " 3: Forcibly enable all voltage and temperature channels, including in9");
64 
65 static unsigned short force_id;
66 module_param(force_id, ushort, 0);
67 MODULE_PARM_DESC(force_id, "Override the detected device ID");
68 
69 /*
70  * Super-I/O registers and operations
71  */
72 
73 #define DEV	0x07	/* Register: Logical device select */
74 #define DEVID	0x20	/* Register: Device ID */
75 #define ACT	0x30	/* Register: Device activation */
76 #define BASE	0x60	/* Register: Base address */
77 
78 #define FSCM	0x09	/* Logical device: fans */
79 #define VLM	0x0d	/* Logical device: voltages */
80 #define TMS	0x0e	/* Logical device: temperatures */
81 #define LDNI_MAX 3
82 static const u8 logdev[LDNI_MAX] = { FSCM, VLM, TMS };
83 
84 #define LD_FAN		0
85 #define LD_IN		1
86 #define LD_TEMP		2
87 
88 static inline void superio_outb(int sioaddr, int reg, int val)
89 {
90 	outb(reg, sioaddr);
91 	outb(val, sioaddr + 1);
92 }
93 
94 static inline int superio_inb(int sioaddr, int reg)
95 {
96 	outb(reg, sioaddr);
97 	return inb(sioaddr + 1);
98 }
99 
100 static inline void superio_exit(int sioaddr)
101 {
102 	outb(0x02, sioaddr);
103 	outb(0x02, sioaddr + 1);
104 }
105 
106 /*
107  * Logical devices
108  */
109 
110 #define PC87360_EXTENT		0x10
111 #define PC87365_REG_BANK	0x09
112 #define NO_BANK			0xff
113 
114 /*
115  * Fan registers and conversions
116  */
117 
118 /* nr has to be 0 or 1 (PC87360/87363) or 2 (PC87364/87365/87366) */
119 #define PC87360_REG_PRESCALE(nr)	(0x00 + 2 * (nr))
120 #define PC87360_REG_PWM(nr)		(0x01 + 2 * (nr))
121 #define PC87360_REG_FAN_MIN(nr)		(0x06 + 3 * (nr))
122 #define PC87360_REG_FAN(nr)		(0x07 + 3 * (nr))
123 #define PC87360_REG_FAN_STATUS(nr)	(0x08 + 3 * (nr))
124 
125 #define FAN_FROM_REG(val, div)		((val) == 0 ? 0 : \
126 					 480000 / ((val) * (div)))
127 #define FAN_TO_REG(val, div)		((val) <= 100 ? 0 : \
128 					 480000 / ((val) * (div)))
129 #define FAN_DIV_FROM_REG(val)		(1 << (((val) >> 5) & 0x03))
130 #define FAN_STATUS_FROM_REG(val)	((val) & 0x07)
131 
132 #define FAN_CONFIG_MONITOR(val, nr)	(((val) >> (2 + (nr) * 3)) & 1)
133 #define FAN_CONFIG_CONTROL(val, nr)	(((val) >> (3 + (nr) * 3)) & 1)
134 #define FAN_CONFIG_INVERT(val, nr)	(((val) >> (4 + (nr) * 3)) & 1)
135 
136 #define PWM_FROM_REG(val, inv)		((inv) ? 255 - (val) : (val))
137 static inline u8 PWM_TO_REG(int val, int inv)
138 {
139 	if (inv)
140 		val = 255 - val;
141 	if (val < 0)
142 		return 0;
143 	if (val > 255)
144 		return 255;
145 	return val;
146 }
147 
148 /*
149  * Voltage registers and conversions
150  */
151 
152 #define PC87365_REG_IN_CONVRATE		0x07
153 #define PC87365_REG_IN_CONFIG		0x08
154 #define PC87365_REG_IN			0x0B
155 #define PC87365_REG_IN_MIN		0x0D
156 #define PC87365_REG_IN_MAX		0x0C
157 #define PC87365_REG_IN_STATUS		0x0A
158 #define PC87365_REG_IN_ALARMS1		0x00
159 #define PC87365_REG_IN_ALARMS2		0x01
160 #define PC87365_REG_VID			0x06
161 
162 #define IN_FROM_REG(val, ref)		(((val) * (ref) + 128) / 256)
163 #define IN_TO_REG(val, ref)		((val) < 0 ? 0 : \
164 					 (val) * 256 >= (ref) * 255 ? 255 : \
165 					 ((val) * 256 + (ref) / 2) / (ref))
166 
167 /*
168  * Temperature registers and conversions
169  */
170 
171 #define PC87365_REG_TEMP_CONFIG		0x08
172 #define PC87365_REG_TEMP		0x0B
173 #define PC87365_REG_TEMP_MIN		0x0D
174 #define PC87365_REG_TEMP_MAX		0x0C
175 #define PC87365_REG_TEMP_CRIT		0x0E
176 #define PC87365_REG_TEMP_STATUS		0x0A
177 #define PC87365_REG_TEMP_ALARMS		0x00
178 
179 #define TEMP_FROM_REG(val)		((val) * 1000)
180 #define TEMP_TO_REG(val)		((val) < -55000 ? -55 : \
181 					 (val) > 127000 ? 127 : \
182 					 (val) < 0 ? ((val) - 500) / 1000 : \
183 					 ((val) + 500) / 1000)
184 
185 /*
186  * Device data
187  */
188 
189 struct pc87360_data {
190 	const char *name;
191 	struct device *hwmon_dev;
192 	struct mutex lock;
193 	struct mutex update_lock;
194 	char valid;		/* !=0 if following fields are valid */
195 	unsigned long last_updated;	/* In jiffies */
196 
197 	int address[3];
198 
199 	u8 fannr, innr, tempnr;
200 
201 	u8 fan[3];		/* Register value */
202 	u8 fan_min[3];		/* Register value */
203 	u8 fan_status[3];	/* Register value */
204 	u8 pwm[3];		/* Register value */
205 	u16 fan_conf;		/* Configuration register values, combined */
206 
207 	u16 in_vref;		/* 1 mV/bit */
208 	u8 in[14];		/* Register value */
209 	u8 in_min[14];		/* Register value */
210 	u8 in_max[14];		/* Register value */
211 	u8 in_crit[3];		/* Register value */
212 	u8 in_status[14];	/* Register value */
213 	u16 in_alarms;		/* Register values, combined, masked */
214 	u8 vid_conf;		/* Configuration register value */
215 	u8 vrm;
216 	u8 vid;			/* Register value */
217 
218 	s8 temp[3];		/* Register value */
219 	s8 temp_min[3];		/* Register value */
220 	s8 temp_max[3];		/* Register value */
221 	s8 temp_crit[3];	/* Register value */
222 	u8 temp_status[3];	/* Register value */
223 	u8 temp_alarms;		/* Register value, masked */
224 };
225 
226 /*
227  * Functions declaration
228  */
229 
230 static int pc87360_probe(struct platform_device *pdev);
231 static int pc87360_remove(struct platform_device *pdev);
232 
233 static int pc87360_read_value(struct pc87360_data *data, u8 ldi, u8 bank,
234 			      u8 reg);
235 static void pc87360_write_value(struct pc87360_data *data, u8 ldi, u8 bank,
236 				u8 reg, u8 value);
237 static void pc87360_init_device(struct platform_device *pdev,
238 				int use_thermistors);
239 static struct pc87360_data *pc87360_update_device(struct device *dev);
240 
241 /*
242  * Driver data
243  */
244 
245 static struct platform_driver pc87360_driver = {
246 	.driver = {
247 		.name	= "pc87360",
248 	},
249 	.probe		= pc87360_probe,
250 	.remove		= pc87360_remove,
251 };
252 
253 /*
254  * Sysfs stuff
255  */
256 
257 static ssize_t fan_input_show(struct device *dev,
258 			      struct device_attribute *devattr, char *buf)
259 {
260 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
261 	struct pc87360_data *data = pc87360_update_device(dev);
262 	return sprintf(buf, "%u\n", FAN_FROM_REG(data->fan[attr->index],
263 		       FAN_DIV_FROM_REG(data->fan_status[attr->index])));
264 }
265 static ssize_t fan_min_show(struct device *dev,
266 			    struct device_attribute *devattr, char *buf)
267 {
268 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
269 	struct pc87360_data *data = pc87360_update_device(dev);
270 	return sprintf(buf, "%u\n", FAN_FROM_REG(data->fan_min[attr->index],
271 		       FAN_DIV_FROM_REG(data->fan_status[attr->index])));
272 }
273 static ssize_t fan_div_show(struct device *dev,
274 			    struct device_attribute *devattr, char *buf)
275 {
276 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
277 	struct pc87360_data *data = pc87360_update_device(dev);
278 	return sprintf(buf, "%u\n",
279 		       FAN_DIV_FROM_REG(data->fan_status[attr->index]));
280 }
281 static ssize_t fan_status_show(struct device *dev,
282 			       struct device_attribute *devattr, char *buf)
283 {
284 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
285 	struct pc87360_data *data = pc87360_update_device(dev);
286 	return sprintf(buf, "%u\n",
287 		       FAN_STATUS_FROM_REG(data->fan_status[attr->index]));
288 }
289 static ssize_t fan_min_store(struct device *dev,
290 			     struct device_attribute *devattr,
291 			     const char *buf, size_t count)
292 {
293 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
294 	struct pc87360_data *data = dev_get_drvdata(dev);
295 	long fan_min;
296 	int err;
297 
298 	err = kstrtol(buf, 10, &fan_min);
299 	if (err)
300 		return err;
301 
302 	mutex_lock(&data->update_lock);
303 	fan_min = FAN_TO_REG(fan_min,
304 			     FAN_DIV_FROM_REG(data->fan_status[attr->index]));
305 
306 	/* If it wouldn't fit, change clock divisor */
307 	while (fan_min > 255
308 	    && (data->fan_status[attr->index] & 0x60) != 0x60) {
309 		fan_min >>= 1;
310 		data->fan[attr->index] >>= 1;
311 		data->fan_status[attr->index] += 0x20;
312 	}
313 	data->fan_min[attr->index] = fan_min > 255 ? 255 : fan_min;
314 	pc87360_write_value(data, LD_FAN, NO_BANK,
315 			    PC87360_REG_FAN_MIN(attr->index),
316 			    data->fan_min[attr->index]);
317 
318 	/* Write new divider, preserve alarm bits */
319 	pc87360_write_value(data, LD_FAN, NO_BANK,
320 			    PC87360_REG_FAN_STATUS(attr->index),
321 			    data->fan_status[attr->index] & 0xF9);
322 	mutex_unlock(&data->update_lock);
323 
324 	return count;
325 }
326 
327 static struct sensor_device_attribute fan_input[] = {
328 	SENSOR_ATTR_RO(fan1_input, fan_input, 0),
329 	SENSOR_ATTR_RO(fan2_input, fan_input, 1),
330 	SENSOR_ATTR_RO(fan3_input, fan_input, 2),
331 };
332 static struct sensor_device_attribute fan_status[] = {
333 	SENSOR_ATTR_RO(fan1_status, fan_status, 0),
334 	SENSOR_ATTR_RO(fan2_status, fan_status, 1),
335 	SENSOR_ATTR_RO(fan3_status, fan_status, 2),
336 };
337 static struct sensor_device_attribute fan_div[] = {
338 	SENSOR_ATTR_RO(fan1_div, fan_div, 0),
339 	SENSOR_ATTR_RO(fan2_div, fan_div, 1),
340 	SENSOR_ATTR_RO(fan3_div, fan_div, 2),
341 };
342 static struct sensor_device_attribute fan_min[] = {
343 	SENSOR_ATTR_RW(fan1_min, fan_min, 0),
344 	SENSOR_ATTR_RW(fan2_min, fan_min, 1),
345 	SENSOR_ATTR_RW(fan3_min, fan_min, 2),
346 };
347 
348 #define FAN_UNIT_ATTRS(X)		\
349 {	&fan_input[X].dev_attr.attr,	\
350 	&fan_status[X].dev_attr.attr,	\
351 	&fan_div[X].dev_attr.attr,	\
352 	&fan_min[X].dev_attr.attr,	\
353 	NULL				\
354 }
355 
356 static ssize_t pwm_show(struct device *dev, struct device_attribute *devattr,
357 			char *buf)
358 {
359 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
360 	struct pc87360_data *data = pc87360_update_device(dev);
361 	return sprintf(buf, "%u\n",
362 		       PWM_FROM_REG(data->pwm[attr->index],
363 				    FAN_CONFIG_INVERT(data->fan_conf,
364 						      attr->index)));
365 }
366 static ssize_t pwm_store(struct device *dev, struct device_attribute *devattr,
367 			 const char *buf, size_t count)
368 {
369 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
370 	struct pc87360_data *data = dev_get_drvdata(dev);
371 	long val;
372 	int err;
373 
374 	err = kstrtol(buf, 10, &val);
375 	if (err)
376 		return err;
377 
378 	mutex_lock(&data->update_lock);
379 	data->pwm[attr->index] = PWM_TO_REG(val,
380 			      FAN_CONFIG_INVERT(data->fan_conf, attr->index));
381 	pc87360_write_value(data, LD_FAN, NO_BANK, PC87360_REG_PWM(attr->index),
382 			    data->pwm[attr->index]);
383 	mutex_unlock(&data->update_lock);
384 	return count;
385 }
386 
387 static struct sensor_device_attribute pwm[] = {
388 	SENSOR_ATTR_RW(pwm1, pwm, 0),
389 	SENSOR_ATTR_RW(pwm2, pwm, 1),
390 	SENSOR_ATTR_RW(pwm3, pwm, 2),
391 };
392 
393 static struct attribute *pc8736x_fan_attr[][5] = {
394 	FAN_UNIT_ATTRS(0),
395 	FAN_UNIT_ATTRS(1),
396 	FAN_UNIT_ATTRS(2)
397 };
398 
399 static const struct attribute_group pc8736x_fan_attr_group[] = {
400 	{ .attrs = pc8736x_fan_attr[0], },
401 	{ .attrs = pc8736x_fan_attr[1], },
402 	{ .attrs = pc8736x_fan_attr[2], },
403 };
404 
405 static ssize_t in_input_show(struct device *dev,
406 			     struct device_attribute *devattr, char *buf)
407 {
408 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
409 	struct pc87360_data *data = pc87360_update_device(dev);
410 	return sprintf(buf, "%u\n", IN_FROM_REG(data->in[attr->index],
411 		       data->in_vref));
412 }
413 static ssize_t in_min_show(struct device *dev,
414 			   struct device_attribute *devattr, char *buf)
415 {
416 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
417 	struct pc87360_data *data = pc87360_update_device(dev);
418 	return sprintf(buf, "%u\n", IN_FROM_REG(data->in_min[attr->index],
419 		       data->in_vref));
420 }
421 static ssize_t in_max_show(struct device *dev,
422 			   struct device_attribute *devattr, char *buf)
423 {
424 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
425 	struct pc87360_data *data = pc87360_update_device(dev);
426 	return sprintf(buf, "%u\n", IN_FROM_REG(data->in_max[attr->index],
427 		       data->in_vref));
428 }
429 static ssize_t in_status_show(struct device *dev,
430 			      struct device_attribute *devattr, char *buf)
431 {
432 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
433 	struct pc87360_data *data = pc87360_update_device(dev);
434 	return sprintf(buf, "%u\n", data->in_status[attr->index]);
435 }
436 static ssize_t in_min_store(struct device *dev,
437 			    struct device_attribute *devattr, const char *buf,
438 			    size_t count)
439 {
440 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
441 	struct pc87360_data *data = dev_get_drvdata(dev);
442 	long val;
443 	int err;
444 
445 	err = kstrtol(buf, 10, &val);
446 	if (err)
447 		return err;
448 
449 	mutex_lock(&data->update_lock);
450 	data->in_min[attr->index] = IN_TO_REG(val, data->in_vref);
451 	pc87360_write_value(data, LD_IN, attr->index, PC87365_REG_IN_MIN,
452 			    data->in_min[attr->index]);
453 	mutex_unlock(&data->update_lock);
454 	return count;
455 }
456 static ssize_t in_max_store(struct device *dev,
457 			    struct device_attribute *devattr, const char *buf,
458 			    size_t count)
459 {
460 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
461 	struct pc87360_data *data = dev_get_drvdata(dev);
462 	long val;
463 	int err;
464 
465 	err = kstrtol(buf, 10, &val);
466 	if (err)
467 		return err;
468 
469 	mutex_lock(&data->update_lock);
470 	data->in_max[attr->index] = IN_TO_REG(val,
471 			       data->in_vref);
472 	pc87360_write_value(data, LD_IN, attr->index, PC87365_REG_IN_MAX,
473 			    data->in_max[attr->index]);
474 	mutex_unlock(&data->update_lock);
475 	return count;
476 }
477 
478 static struct sensor_device_attribute in_input[] = {
479 	SENSOR_ATTR_RO(in0_input, in_input, 0),
480 	SENSOR_ATTR_RO(in1_input, in_input, 1),
481 	SENSOR_ATTR_RO(in2_input, in_input, 2),
482 	SENSOR_ATTR_RO(in3_input, in_input, 3),
483 	SENSOR_ATTR_RO(in4_input, in_input, 4),
484 	SENSOR_ATTR_RO(in5_input, in_input, 5),
485 	SENSOR_ATTR_RO(in6_input, in_input, 6),
486 	SENSOR_ATTR_RO(in7_input, in_input, 7),
487 	SENSOR_ATTR_RO(in8_input, in_input, 8),
488 	SENSOR_ATTR_RO(in9_input, in_input, 9),
489 	SENSOR_ATTR_RO(in10_input, in_input, 10),
490 };
491 static struct sensor_device_attribute in_status[] = {
492 	SENSOR_ATTR_RO(in0_status, in_status, 0),
493 	SENSOR_ATTR_RO(in1_status, in_status, 1),
494 	SENSOR_ATTR_RO(in2_status, in_status, 2),
495 	SENSOR_ATTR_RO(in3_status, in_status, 3),
496 	SENSOR_ATTR_RO(in4_status, in_status, 4),
497 	SENSOR_ATTR_RO(in5_status, in_status, 5),
498 	SENSOR_ATTR_RO(in6_status, in_status, 6),
499 	SENSOR_ATTR_RO(in7_status, in_status, 7),
500 	SENSOR_ATTR_RO(in8_status, in_status, 8),
501 	SENSOR_ATTR_RO(in9_status, in_status, 9),
502 	SENSOR_ATTR_RO(in10_status, in_status, 10),
503 };
504 static struct sensor_device_attribute in_min[] = {
505 	SENSOR_ATTR_RW(in0_min, in_min, 0),
506 	SENSOR_ATTR_RW(in1_min, in_min, 1),
507 	SENSOR_ATTR_RW(in2_min, in_min, 2),
508 	SENSOR_ATTR_RW(in3_min, in_min, 3),
509 	SENSOR_ATTR_RW(in4_min, in_min, 4),
510 	SENSOR_ATTR_RW(in5_min, in_min, 5),
511 	SENSOR_ATTR_RW(in6_min, in_min, 6),
512 	SENSOR_ATTR_RW(in7_min, in_min, 7),
513 	SENSOR_ATTR_RW(in8_min, in_min, 8),
514 	SENSOR_ATTR_RW(in9_min, in_min, 9),
515 	SENSOR_ATTR_RW(in10_min, in_min, 10),
516 };
517 static struct sensor_device_attribute in_max[] = {
518 	SENSOR_ATTR_RW(in0_max, in_max, 0),
519 	SENSOR_ATTR_RW(in1_max, in_max, 1),
520 	SENSOR_ATTR_RW(in2_max, in_max, 2),
521 	SENSOR_ATTR_RW(in3_max, in_max, 3),
522 	SENSOR_ATTR_RW(in4_max, in_max, 4),
523 	SENSOR_ATTR_RW(in5_max, in_max, 5),
524 	SENSOR_ATTR_RW(in6_max, in_max, 6),
525 	SENSOR_ATTR_RW(in7_max, in_max, 7),
526 	SENSOR_ATTR_RW(in8_max, in_max, 8),
527 	SENSOR_ATTR_RW(in9_max, in_max, 9),
528 	SENSOR_ATTR_RW(in10_max, in_max, 10),
529 };
530 
531 /* (temp & vin) channel status register alarm bits (pdf sec.11.5.12) */
532 #define CHAN_ALM_MIN	0x02	/* min limit crossed */
533 #define CHAN_ALM_MAX	0x04	/* max limit exceeded */
534 #define TEMP_ALM_CRIT	0x08	/* temp crit exceeded (temp only) */
535 
536 /*
537  * show_in_min/max_alarm() reads data from the per-channel status
538  * register (sec 11.5.12), not the vin event status registers (sec
539  * 11.5.2) that (legacy) show_in_alarm() resds (via data->in_alarms)
540  */
541 
542 static ssize_t in_min_alarm_show(struct device *dev,
543 				 struct device_attribute *devattr, char *buf)
544 {
545 	struct pc87360_data *data = pc87360_update_device(dev);
546 	unsigned nr = to_sensor_dev_attr(devattr)->index;
547 
548 	return sprintf(buf, "%u\n", !!(data->in_status[nr] & CHAN_ALM_MIN));
549 }
550 static ssize_t in_max_alarm_show(struct device *dev,
551 				 struct device_attribute *devattr, char *buf)
552 {
553 	struct pc87360_data *data = pc87360_update_device(dev);
554 	unsigned nr = to_sensor_dev_attr(devattr)->index;
555 
556 	return sprintf(buf, "%u\n", !!(data->in_status[nr] & CHAN_ALM_MAX));
557 }
558 
559 static struct sensor_device_attribute in_min_alarm[] = {
560 	SENSOR_ATTR_RO(in0_min_alarm, in_min_alarm, 0),
561 	SENSOR_ATTR_RO(in1_min_alarm, in_min_alarm, 1),
562 	SENSOR_ATTR_RO(in2_min_alarm, in_min_alarm, 2),
563 	SENSOR_ATTR_RO(in3_min_alarm, in_min_alarm, 3),
564 	SENSOR_ATTR_RO(in4_min_alarm, in_min_alarm, 4),
565 	SENSOR_ATTR_RO(in5_min_alarm, in_min_alarm, 5),
566 	SENSOR_ATTR_RO(in6_min_alarm, in_min_alarm, 6),
567 	SENSOR_ATTR_RO(in7_min_alarm, in_min_alarm, 7),
568 	SENSOR_ATTR_RO(in8_min_alarm, in_min_alarm, 8),
569 	SENSOR_ATTR_RO(in9_min_alarm, in_min_alarm, 9),
570 	SENSOR_ATTR_RO(in10_min_alarm, in_min_alarm, 10),
571 };
572 static struct sensor_device_attribute in_max_alarm[] = {
573 	SENSOR_ATTR_RO(in0_max_alarm, in_max_alarm, 0),
574 	SENSOR_ATTR_RO(in1_max_alarm, in_max_alarm, 1),
575 	SENSOR_ATTR_RO(in2_max_alarm, in_max_alarm, 2),
576 	SENSOR_ATTR_RO(in3_max_alarm, in_max_alarm, 3),
577 	SENSOR_ATTR_RO(in4_max_alarm, in_max_alarm, 4),
578 	SENSOR_ATTR_RO(in5_max_alarm, in_max_alarm, 5),
579 	SENSOR_ATTR_RO(in6_max_alarm, in_max_alarm, 6),
580 	SENSOR_ATTR_RO(in7_max_alarm, in_max_alarm, 7),
581 	SENSOR_ATTR_RO(in8_max_alarm, in_max_alarm, 8),
582 	SENSOR_ATTR_RO(in9_max_alarm, in_max_alarm, 9),
583 	SENSOR_ATTR_RO(in10_max_alarm, in_max_alarm, 10),
584 };
585 
586 #define VIN_UNIT_ATTRS(X) \
587 	&in_input[X].dev_attr.attr,	\
588 	&in_status[X].dev_attr.attr,	\
589 	&in_min[X].dev_attr.attr,	\
590 	&in_max[X].dev_attr.attr,	\
591 	&in_min_alarm[X].dev_attr.attr,	\
592 	&in_max_alarm[X].dev_attr.attr
593 
594 static ssize_t cpu0_vid_show(struct device *dev,
595 			     struct device_attribute *attr, char *buf)
596 {
597 	struct pc87360_data *data = pc87360_update_device(dev);
598 	return sprintf(buf, "%u\n", vid_from_reg(data->vid, data->vrm));
599 }
600 static DEVICE_ATTR_RO(cpu0_vid);
601 
602 static ssize_t vrm_show(struct device *dev, struct device_attribute *attr,
603 			char *buf)
604 {
605 	struct pc87360_data *data = dev_get_drvdata(dev);
606 	return sprintf(buf, "%u\n", data->vrm);
607 }
608 static ssize_t vrm_store(struct device *dev, struct device_attribute *attr,
609 			 const char *buf, size_t count)
610 {
611 	struct pc87360_data *data = dev_get_drvdata(dev);
612 	unsigned long val;
613 	int err;
614 
615 	err = kstrtoul(buf, 10, &val);
616 	if (err)
617 		return err;
618 
619 	if (val > 255)
620 		return -EINVAL;
621 
622 	data->vrm = val;
623 	return count;
624 }
625 static DEVICE_ATTR_RW(vrm);
626 
627 static ssize_t alarms_in_show(struct device *dev,
628 			      struct device_attribute *attr, char *buf)
629 {
630 	struct pc87360_data *data = pc87360_update_device(dev);
631 	return sprintf(buf, "%u\n", data->in_alarms);
632 }
633 static DEVICE_ATTR_RO(alarms_in);
634 
635 static struct attribute *pc8736x_vin_attr_array[] = {
636 	VIN_UNIT_ATTRS(0),
637 	VIN_UNIT_ATTRS(1),
638 	VIN_UNIT_ATTRS(2),
639 	VIN_UNIT_ATTRS(3),
640 	VIN_UNIT_ATTRS(4),
641 	VIN_UNIT_ATTRS(5),
642 	VIN_UNIT_ATTRS(6),
643 	VIN_UNIT_ATTRS(7),
644 	VIN_UNIT_ATTRS(8),
645 	VIN_UNIT_ATTRS(9),
646 	VIN_UNIT_ATTRS(10),
647 	&dev_attr_cpu0_vid.attr,
648 	&dev_attr_vrm.attr,
649 	&dev_attr_alarms_in.attr,
650 	NULL
651 };
652 static const struct attribute_group pc8736x_vin_group = {
653 	.attrs = pc8736x_vin_attr_array,
654 };
655 
656 static ssize_t therm_input_show(struct device *dev,
657 				struct device_attribute *devattr, char *buf)
658 {
659 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
660 	struct pc87360_data *data = pc87360_update_device(dev);
661 	return sprintf(buf, "%u\n", IN_FROM_REG(data->in[attr->index],
662 		       data->in_vref));
663 }
664 static ssize_t therm_min_show(struct device *dev,
665 			      struct device_attribute *devattr, char *buf)
666 {
667 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
668 	struct pc87360_data *data = pc87360_update_device(dev);
669 	return sprintf(buf, "%u\n", IN_FROM_REG(data->in_min[attr->index],
670 		       data->in_vref));
671 }
672 static ssize_t therm_max_show(struct device *dev,
673 			      struct device_attribute *devattr, char *buf)
674 {
675 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
676 	struct pc87360_data *data = pc87360_update_device(dev);
677 	return sprintf(buf, "%u\n", IN_FROM_REG(data->in_max[attr->index],
678 		       data->in_vref));
679 }
680 static ssize_t therm_crit_show(struct device *dev,
681 			       struct device_attribute *devattr, char *buf)
682 {
683 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
684 	struct pc87360_data *data = pc87360_update_device(dev);
685 	return sprintf(buf, "%u\n", IN_FROM_REG(data->in_crit[attr->index-11],
686 		       data->in_vref));
687 }
688 static ssize_t therm_status_show(struct device *dev,
689 				 struct device_attribute *devattr, char *buf)
690 {
691 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
692 	struct pc87360_data *data = pc87360_update_device(dev);
693 	return sprintf(buf, "%u\n", data->in_status[attr->index]);
694 }
695 
696 static ssize_t therm_min_store(struct device *dev,
697 			       struct device_attribute *devattr,
698 			       const char *buf, size_t count)
699 {
700 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
701 	struct pc87360_data *data = dev_get_drvdata(dev);
702 	long val;
703 	int err;
704 
705 	err = kstrtol(buf, 10, &val);
706 	if (err)
707 		return err;
708 
709 	mutex_lock(&data->update_lock);
710 	data->in_min[attr->index] = IN_TO_REG(val, data->in_vref);
711 	pc87360_write_value(data, LD_IN, attr->index, PC87365_REG_TEMP_MIN,
712 			    data->in_min[attr->index]);
713 	mutex_unlock(&data->update_lock);
714 	return count;
715 }
716 
717 static ssize_t therm_max_store(struct device *dev,
718 			       struct device_attribute *devattr,
719 			       const char *buf, size_t count)
720 {
721 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
722 	struct pc87360_data *data = dev_get_drvdata(dev);
723 	long val;
724 	int err;
725 
726 	err = kstrtol(buf, 10, &val);
727 	if (err)
728 		return err;
729 
730 	mutex_lock(&data->update_lock);
731 	data->in_max[attr->index] = IN_TO_REG(val, data->in_vref);
732 	pc87360_write_value(data, LD_IN, attr->index, PC87365_REG_TEMP_MAX,
733 			    data->in_max[attr->index]);
734 	mutex_unlock(&data->update_lock);
735 	return count;
736 }
737 static ssize_t therm_crit_store(struct device *dev,
738 				struct device_attribute *devattr,
739 				const char *buf, size_t count)
740 {
741 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
742 	struct pc87360_data *data = dev_get_drvdata(dev);
743 	long val;
744 	int err;
745 
746 	err = kstrtol(buf, 10, &val);
747 	if (err)
748 		return err;
749 
750 	mutex_lock(&data->update_lock);
751 	data->in_crit[attr->index-11] = IN_TO_REG(val, data->in_vref);
752 	pc87360_write_value(data, LD_IN, attr->index, PC87365_REG_TEMP_CRIT,
753 			    data->in_crit[attr->index-11]);
754 	mutex_unlock(&data->update_lock);
755 	return count;
756 }
757 
758 /*
759  * the +11 term below reflects the fact that VLM units 11,12,13 are
760  * used in the chip to measure voltage across the thermistors
761  */
762 static struct sensor_device_attribute therm_input[] = {
763 	SENSOR_ATTR_RO(temp4_input, therm_input, 0 + 11),
764 	SENSOR_ATTR_RO(temp5_input, therm_input, 1 + 11),
765 	SENSOR_ATTR_RO(temp6_input, therm_input, 2 + 11),
766 };
767 static struct sensor_device_attribute therm_status[] = {
768 	SENSOR_ATTR_RO(temp4_status, therm_status, 0 + 11),
769 	SENSOR_ATTR_RO(temp5_status, therm_status, 1 + 11),
770 	SENSOR_ATTR_RO(temp6_status, therm_status, 2 + 11),
771 };
772 static struct sensor_device_attribute therm_min[] = {
773 	SENSOR_ATTR_RW(temp4_min, therm_min, 0 + 11),
774 	SENSOR_ATTR_RW(temp5_min, therm_min, 1 + 11),
775 	SENSOR_ATTR_RW(temp6_min, therm_min, 2 + 11),
776 };
777 static struct sensor_device_attribute therm_max[] = {
778 	SENSOR_ATTR_RW(temp4_max, therm_max, 0 + 11),
779 	SENSOR_ATTR_RW(temp5_max, therm_max, 1 + 11),
780 	SENSOR_ATTR_RW(temp6_max, therm_max, 2 + 11),
781 };
782 static struct sensor_device_attribute therm_crit[] = {
783 	SENSOR_ATTR_RW(temp4_crit, therm_crit, 0 + 11),
784 	SENSOR_ATTR_RW(temp5_crit, therm_crit, 1 + 11),
785 	SENSOR_ATTR_RW(temp6_crit, therm_crit, 2 + 11),
786 };
787 
788 /*
789  * show_therm_min/max_alarm() reads data from the per-channel voltage
790  * status register (sec 11.5.12)
791  */
792 
793 static ssize_t therm_min_alarm_show(struct device *dev,
794 				    struct device_attribute *devattr,
795 				    char *buf)
796 {
797 	struct pc87360_data *data = pc87360_update_device(dev);
798 	unsigned nr = to_sensor_dev_attr(devattr)->index;
799 
800 	return sprintf(buf, "%u\n", !!(data->in_status[nr] & CHAN_ALM_MIN));
801 }
802 static ssize_t therm_max_alarm_show(struct device *dev,
803 				    struct device_attribute *devattr,
804 				    char *buf)
805 {
806 	struct pc87360_data *data = pc87360_update_device(dev);
807 	unsigned nr = to_sensor_dev_attr(devattr)->index;
808 
809 	return sprintf(buf, "%u\n", !!(data->in_status[nr] & CHAN_ALM_MAX));
810 }
811 static ssize_t therm_crit_alarm_show(struct device *dev,
812 				     struct device_attribute *devattr,
813 				     char *buf)
814 {
815 	struct pc87360_data *data = pc87360_update_device(dev);
816 	unsigned nr = to_sensor_dev_attr(devattr)->index;
817 
818 	return sprintf(buf, "%u\n", !!(data->in_status[nr] & TEMP_ALM_CRIT));
819 }
820 
821 static struct sensor_device_attribute therm_min_alarm[] = {
822 	SENSOR_ATTR_RO(temp4_min_alarm, therm_min_alarm, 0 + 11),
823 	SENSOR_ATTR_RO(temp5_min_alarm, therm_min_alarm, 1 + 11),
824 	SENSOR_ATTR_RO(temp6_min_alarm, therm_min_alarm, 2 + 11),
825 };
826 static struct sensor_device_attribute therm_max_alarm[] = {
827 	SENSOR_ATTR_RO(temp4_max_alarm, therm_max_alarm, 0 + 11),
828 	SENSOR_ATTR_RO(temp5_max_alarm, therm_max_alarm, 1 + 11),
829 	SENSOR_ATTR_RO(temp6_max_alarm, therm_max_alarm, 2 + 11),
830 };
831 static struct sensor_device_attribute therm_crit_alarm[] = {
832 	SENSOR_ATTR_RO(temp4_crit_alarm, therm_crit_alarm, 0 + 11),
833 	SENSOR_ATTR_RO(temp5_crit_alarm, therm_crit_alarm, 1 + 11),
834 	SENSOR_ATTR_RO(temp6_crit_alarm, therm_crit_alarm, 2 + 11),
835 };
836 
837 #define THERM_UNIT_ATTRS(X) \
838 	&therm_input[X].dev_attr.attr,	\
839 	&therm_status[X].dev_attr.attr,	\
840 	&therm_min[X].dev_attr.attr,	\
841 	&therm_max[X].dev_attr.attr,	\
842 	&therm_crit[X].dev_attr.attr,	\
843 	&therm_min_alarm[X].dev_attr.attr, \
844 	&therm_max_alarm[X].dev_attr.attr, \
845 	&therm_crit_alarm[X].dev_attr.attr
846 
847 static struct attribute *pc8736x_therm_attr_array[] = {
848 	THERM_UNIT_ATTRS(0),
849 	THERM_UNIT_ATTRS(1),
850 	THERM_UNIT_ATTRS(2),
851 	NULL
852 };
853 static const struct attribute_group pc8736x_therm_group = {
854 	.attrs = pc8736x_therm_attr_array,
855 };
856 
857 static ssize_t temp_input_show(struct device *dev,
858 			       struct device_attribute *devattr, char *buf)
859 {
860 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
861 	struct pc87360_data *data = pc87360_update_device(dev);
862 	return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[attr->index]));
863 }
864 
865 static ssize_t temp_min_show(struct device *dev,
866 			     struct device_attribute *devattr, char *buf)
867 {
868 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
869 	struct pc87360_data *data = pc87360_update_device(dev);
870 	return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_min[attr->index]));
871 }
872 
873 static ssize_t temp_max_show(struct device *dev,
874 			     struct device_attribute *devattr, char *buf)
875 {
876 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
877 	struct pc87360_data *data = pc87360_update_device(dev);
878 	return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max[attr->index]));
879 }
880 
881 static ssize_t temp_crit_show(struct device *dev,
882 			      struct device_attribute *devattr, char *buf)
883 {
884 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
885 	struct pc87360_data *data = pc87360_update_device(dev);
886 	return sprintf(buf, "%d\n",
887 		       TEMP_FROM_REG(data->temp_crit[attr->index]));
888 }
889 
890 static ssize_t temp_status_show(struct device *dev,
891 				struct device_attribute *devattr, char *buf)
892 {
893 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
894 	struct pc87360_data *data = pc87360_update_device(dev);
895 	return sprintf(buf, "%d\n", data->temp_status[attr->index]);
896 }
897 
898 static ssize_t temp_min_store(struct device *dev,
899 			      struct device_attribute *devattr,
900 			      const char *buf, size_t count)
901 {
902 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
903 	struct pc87360_data *data = dev_get_drvdata(dev);
904 	long val;
905 	int err;
906 
907 	err = kstrtol(buf, 10, &val);
908 	if (err)
909 		return err;
910 
911 	mutex_lock(&data->update_lock);
912 	data->temp_min[attr->index] = TEMP_TO_REG(val);
913 	pc87360_write_value(data, LD_TEMP, attr->index, PC87365_REG_TEMP_MIN,
914 			    data->temp_min[attr->index]);
915 	mutex_unlock(&data->update_lock);
916 	return count;
917 }
918 
919 static ssize_t temp_max_store(struct device *dev,
920 			      struct device_attribute *devattr,
921 			      const char *buf, size_t count)
922 {
923 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
924 	struct pc87360_data *data = dev_get_drvdata(dev);
925 	long val;
926 	int err;
927 
928 	err = kstrtol(buf, 10, &val);
929 	if (err)
930 		return err;
931 
932 	mutex_lock(&data->update_lock);
933 	data->temp_max[attr->index] = TEMP_TO_REG(val);
934 	pc87360_write_value(data, LD_TEMP, attr->index, PC87365_REG_TEMP_MAX,
935 			    data->temp_max[attr->index]);
936 	mutex_unlock(&data->update_lock);
937 	return count;
938 }
939 
940 static ssize_t temp_crit_store(struct device *dev,
941 			       struct device_attribute *devattr,
942 			       const char *buf, size_t count)
943 {
944 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
945 	struct pc87360_data *data = dev_get_drvdata(dev);
946 	long val;
947 	int err;
948 
949 	err = kstrtol(buf, 10, &val);
950 	if (err)
951 		return err;
952 
953 	mutex_lock(&data->update_lock);
954 	data->temp_crit[attr->index] = TEMP_TO_REG(val);
955 	pc87360_write_value(data, LD_TEMP, attr->index, PC87365_REG_TEMP_CRIT,
956 			    data->temp_crit[attr->index]);
957 	mutex_unlock(&data->update_lock);
958 	return count;
959 }
960 
961 static struct sensor_device_attribute temp_input[] = {
962 	SENSOR_ATTR_RO(temp1_input, temp_input, 0),
963 	SENSOR_ATTR_RO(temp2_input, temp_input, 1),
964 	SENSOR_ATTR_RO(temp3_input, temp_input, 2),
965 };
966 static struct sensor_device_attribute temp_status[] = {
967 	SENSOR_ATTR_RO(temp1_status, temp_status, 0),
968 	SENSOR_ATTR_RO(temp2_status, temp_status, 1),
969 	SENSOR_ATTR_RO(temp3_status, temp_status, 2),
970 };
971 static struct sensor_device_attribute temp_min[] = {
972 	SENSOR_ATTR_RW(temp1_min, temp_min, 0),
973 	SENSOR_ATTR_RW(temp2_min, temp_min, 1),
974 	SENSOR_ATTR_RW(temp3_min, temp_min, 2),
975 };
976 static struct sensor_device_attribute temp_max[] = {
977 	SENSOR_ATTR_RW(temp1_max, temp_max, 0),
978 	SENSOR_ATTR_RW(temp2_max, temp_max, 1),
979 	SENSOR_ATTR_RW(temp3_max, temp_max, 2),
980 };
981 static struct sensor_device_attribute temp_crit[] = {
982 	SENSOR_ATTR_RW(temp1_crit, temp_crit, 0),
983 	SENSOR_ATTR_RW(temp2_crit, temp_crit, 1),
984 	SENSOR_ATTR_RW(temp3_crit, temp_crit, 2),
985 };
986 
987 static ssize_t alarms_temp_show(struct device *dev,
988 				struct device_attribute *attr, char *buf)
989 {
990 	struct pc87360_data *data = pc87360_update_device(dev);
991 	return sprintf(buf, "%u\n", data->temp_alarms);
992 }
993 
994 static DEVICE_ATTR_RO(alarms_temp);
995 
996 /*
997  * show_temp_min/max_alarm() reads data from the per-channel status
998  * register (sec 12.3.7), not the temp event status registers (sec
999  * 12.3.2) that show_temp_alarm() reads (via data->temp_alarms)
1000  */
1001 
1002 static ssize_t temp_min_alarm_show(struct device *dev,
1003 				   struct device_attribute *devattr,
1004 				   char *buf)
1005 {
1006 	struct pc87360_data *data = pc87360_update_device(dev);
1007 	unsigned nr = to_sensor_dev_attr(devattr)->index;
1008 
1009 	return sprintf(buf, "%u\n", !!(data->temp_status[nr] & CHAN_ALM_MIN));
1010 }
1011 
1012 static ssize_t temp_max_alarm_show(struct device *dev,
1013 				   struct device_attribute *devattr,
1014 				   char *buf)
1015 {
1016 	struct pc87360_data *data = pc87360_update_device(dev);
1017 	unsigned nr = to_sensor_dev_attr(devattr)->index;
1018 
1019 	return sprintf(buf, "%u\n", !!(data->temp_status[nr] & CHAN_ALM_MAX));
1020 }
1021 
1022 static ssize_t temp_crit_alarm_show(struct device *dev,
1023 				    struct device_attribute *devattr,
1024 				    char *buf)
1025 {
1026 	struct pc87360_data *data = pc87360_update_device(dev);
1027 	unsigned nr = to_sensor_dev_attr(devattr)->index;
1028 
1029 	return sprintf(buf, "%u\n", !!(data->temp_status[nr] & TEMP_ALM_CRIT));
1030 }
1031 
1032 static struct sensor_device_attribute temp_min_alarm[] = {
1033 	SENSOR_ATTR_RO(temp1_min_alarm, temp_min_alarm, 0),
1034 	SENSOR_ATTR_RO(temp2_min_alarm, temp_min_alarm, 1),
1035 	SENSOR_ATTR_RO(temp3_min_alarm, temp_min_alarm, 2),
1036 };
1037 
1038 static struct sensor_device_attribute temp_max_alarm[] = {
1039 	SENSOR_ATTR_RO(temp1_max_alarm, temp_max_alarm, 0),
1040 	SENSOR_ATTR_RO(temp2_max_alarm, temp_max_alarm, 1),
1041 	SENSOR_ATTR_RO(temp3_max_alarm, temp_max_alarm, 2),
1042 };
1043 
1044 static struct sensor_device_attribute temp_crit_alarm[] = {
1045 	SENSOR_ATTR_RO(temp1_crit_alarm, temp_crit_alarm, 0),
1046 	SENSOR_ATTR_RO(temp2_crit_alarm, temp_crit_alarm, 1),
1047 	SENSOR_ATTR_RO(temp3_crit_alarm, temp_crit_alarm, 2),
1048 };
1049 
1050 #define TEMP_FAULT	0x40	/* open diode */
1051 static ssize_t temp_fault_show(struct device *dev,
1052 			       struct device_attribute *devattr, char *buf)
1053 {
1054 	struct pc87360_data *data = pc87360_update_device(dev);
1055 	unsigned nr = to_sensor_dev_attr(devattr)->index;
1056 
1057 	return sprintf(buf, "%u\n", !!(data->temp_status[nr] & TEMP_FAULT));
1058 }
1059 static struct sensor_device_attribute temp_fault[] = {
1060 	SENSOR_ATTR_RO(temp1_fault, temp_fault, 0),
1061 	SENSOR_ATTR_RO(temp2_fault, temp_fault, 1),
1062 	SENSOR_ATTR_RO(temp3_fault, temp_fault, 2),
1063 };
1064 
1065 #define TEMP_UNIT_ATTRS(X)			\
1066 {	&temp_input[X].dev_attr.attr,		\
1067 	&temp_status[X].dev_attr.attr,		\
1068 	&temp_min[X].dev_attr.attr,		\
1069 	&temp_max[X].dev_attr.attr,		\
1070 	&temp_crit[X].dev_attr.attr,		\
1071 	&temp_min_alarm[X].dev_attr.attr,	\
1072 	&temp_max_alarm[X].dev_attr.attr,	\
1073 	&temp_crit_alarm[X].dev_attr.attr,	\
1074 	&temp_fault[X].dev_attr.attr,		\
1075 	NULL					\
1076 }
1077 
1078 static struct attribute *pc8736x_temp_attr[][10] = {
1079 	TEMP_UNIT_ATTRS(0),
1080 	TEMP_UNIT_ATTRS(1),
1081 	TEMP_UNIT_ATTRS(2)
1082 };
1083 
1084 static const struct attribute_group pc8736x_temp_attr_group[] = {
1085 	{ .attrs = pc8736x_temp_attr[0] },
1086 	{ .attrs = pc8736x_temp_attr[1] },
1087 	{ .attrs = pc8736x_temp_attr[2] }
1088 };
1089 
1090 static ssize_t name_show(struct device *dev,
1091 			struct device_attribute *devattr, char *buf)
1092 {
1093 	struct pc87360_data *data = dev_get_drvdata(dev);
1094 	return sprintf(buf, "%s\n", data->name);
1095 }
1096 
1097 static DEVICE_ATTR_RO(name);
1098 
1099 /*
1100  * Device detection, registration and update
1101  */
1102 
1103 static int __init pc87360_find(int sioaddr, u8 *devid,
1104 			       unsigned short *addresses)
1105 {
1106 	u16 val;
1107 	int i;
1108 	int nrdev; /* logical device count */
1109 
1110 	/* No superio_enter */
1111 
1112 	/* Identify device */
1113 	val = force_id ? force_id : superio_inb(sioaddr, DEVID);
1114 	switch (val) {
1115 	case 0xE1: /* PC87360 */
1116 	case 0xE8: /* PC87363 */
1117 	case 0xE4: /* PC87364 */
1118 		nrdev = 1;
1119 		break;
1120 	case 0xE5: /* PC87365 */
1121 	case 0xE9: /* PC87366 */
1122 		nrdev = 3;
1123 		break;
1124 	default:
1125 		superio_exit(sioaddr);
1126 		return -ENODEV;
1127 	}
1128 	/* Remember the device id */
1129 	*devid = val;
1130 
1131 	for (i = 0; i < nrdev; i++) {
1132 		/* select logical device */
1133 		superio_outb(sioaddr, DEV, logdev[i]);
1134 
1135 		val = superio_inb(sioaddr, ACT);
1136 		if (!(val & 0x01)) {
1137 			pr_info("Device 0x%02x not activated\n", logdev[i]);
1138 			continue;
1139 		}
1140 
1141 		val = (superio_inb(sioaddr, BASE) << 8)
1142 		    | superio_inb(sioaddr, BASE + 1);
1143 		if (!val) {
1144 			pr_info("Base address not set for device 0x%02x\n",
1145 				logdev[i]);
1146 			continue;
1147 		}
1148 
1149 		addresses[i] = val;
1150 
1151 		if (i == 0) { /* Fans */
1152 			confreg[0] = superio_inb(sioaddr, 0xF0);
1153 			confreg[1] = superio_inb(sioaddr, 0xF1);
1154 
1155 			pr_debug("Fan %d: mon=%d ctrl=%d inv=%d\n", 1,
1156 				 (confreg[0] >> 2) & 1, (confreg[0] >> 3) & 1,
1157 				 (confreg[0] >> 4) & 1);
1158 			pr_debug("Fan %d: mon=%d ctrl=%d inv=%d\n", 2,
1159 				 (confreg[0] >> 5) & 1, (confreg[0] >> 6) & 1,
1160 				 (confreg[0] >> 7) & 1);
1161 			pr_debug("Fan %d: mon=%d ctrl=%d inv=%d\n", 3,
1162 				 confreg[1] & 1, (confreg[1] >> 1) & 1,
1163 				 (confreg[1] >> 2) & 1);
1164 		} else if (i == 1) { /* Voltages */
1165 			/* Are we using thermistors? */
1166 			if (*devid == 0xE9) { /* PC87366 */
1167 				/*
1168 				 * These registers are not logical-device
1169 				 * specific, just that we won't need them if
1170 				 * we don't use the VLM device
1171 				 */
1172 				confreg[2] = superio_inb(sioaddr, 0x2B);
1173 				confreg[3] = superio_inb(sioaddr, 0x25);
1174 
1175 				if (confreg[2] & 0x40) {
1176 					pr_info("Using thermistors for temperature monitoring\n");
1177 				}
1178 				if (confreg[3] & 0xE0) {
1179 					pr_info("VID inputs routed (mode %u)\n",
1180 						confreg[3] >> 5);
1181 				}
1182 			}
1183 		}
1184 	}
1185 
1186 	superio_exit(sioaddr);
1187 	return 0;
1188 }
1189 
1190 static void pc87360_remove_files(struct device *dev)
1191 {
1192 	int i;
1193 
1194 	device_remove_file(dev, &dev_attr_name);
1195 	device_remove_file(dev, &dev_attr_alarms_temp);
1196 	for (i = 0; i < ARRAY_SIZE(pc8736x_temp_attr_group); i++)
1197 		sysfs_remove_group(&dev->kobj, &pc8736x_temp_attr_group[i]);
1198 	for (i = 0; i < ARRAY_SIZE(pc8736x_fan_attr_group); i++) {
1199 		sysfs_remove_group(&pdev->dev.kobj, &pc8736x_fan_attr_group[i]);
1200 		device_remove_file(dev, &pwm[i].dev_attr);
1201 	}
1202 	sysfs_remove_group(&dev->kobj, &pc8736x_therm_group);
1203 	sysfs_remove_group(&dev->kobj, &pc8736x_vin_group);
1204 }
1205 
1206 static int pc87360_probe(struct platform_device *pdev)
1207 {
1208 	int i;
1209 	struct pc87360_data *data;
1210 	int err = 0;
1211 	const char *name;
1212 	int use_thermistors = 0;
1213 	struct device *dev = &pdev->dev;
1214 
1215 	data = devm_kzalloc(dev, sizeof(struct pc87360_data), GFP_KERNEL);
1216 	if (!data)
1217 		return -ENOMEM;
1218 
1219 	switch (devid) {
1220 	default:
1221 		name = "pc87360";
1222 		data->fannr = 2;
1223 		break;
1224 	case 0xe8:
1225 		name = "pc87363";
1226 		data->fannr = 2;
1227 		break;
1228 	case 0xe4:
1229 		name = "pc87364";
1230 		data->fannr = 3;
1231 		break;
1232 	case 0xe5:
1233 		name = "pc87365";
1234 		data->fannr = extra_isa[0] ? 3 : 0;
1235 		data->innr = extra_isa[1] ? 11 : 0;
1236 		data->tempnr = extra_isa[2] ? 2 : 0;
1237 		break;
1238 	case 0xe9:
1239 		name = "pc87366";
1240 		data->fannr = extra_isa[0] ? 3 : 0;
1241 		data->innr = extra_isa[1] ? 14 : 0;
1242 		data->tempnr = extra_isa[2] ? 3 : 0;
1243 		break;
1244 	}
1245 
1246 	data->name = name;
1247 	mutex_init(&data->lock);
1248 	mutex_init(&data->update_lock);
1249 	platform_set_drvdata(pdev, data);
1250 
1251 	for (i = 0; i < LDNI_MAX; i++) {
1252 		data->address[i] = extra_isa[i];
1253 		if (data->address[i]
1254 		 && !devm_request_region(dev, extra_isa[i], PC87360_EXTENT,
1255 					 pc87360_driver.driver.name)) {
1256 			dev_err(dev,
1257 				"Region 0x%x-0x%x already in use!\n",
1258 				extra_isa[i], extra_isa[i]+PC87360_EXTENT-1);
1259 			return -EBUSY;
1260 		}
1261 	}
1262 
1263 	/* Retrieve the fans configuration from Super-I/O space */
1264 	if (data->fannr)
1265 		data->fan_conf = confreg[0] | (confreg[1] << 8);
1266 
1267 	/*
1268 	 * Use the correct reference voltage
1269 	 * Unless both the VLM and the TMS logical devices agree to
1270 	 * use an external Vref, the internal one is used.
1271 	 */
1272 	if (data->innr) {
1273 		i = pc87360_read_value(data, LD_IN, NO_BANK,
1274 				       PC87365_REG_IN_CONFIG);
1275 		if (data->tempnr) {
1276 			i &= pc87360_read_value(data, LD_TEMP, NO_BANK,
1277 						PC87365_REG_TEMP_CONFIG);
1278 		}
1279 		data->in_vref = (i&0x02) ? 3025 : 2966;
1280 		dev_dbg(dev, "Using %s reference voltage\n",
1281 			(i&0x02) ? "external" : "internal");
1282 
1283 		data->vid_conf = confreg[3];
1284 		data->vrm = vid_which_vrm();
1285 	}
1286 
1287 	/* Fan clock dividers may be needed before any data is read */
1288 	for (i = 0; i < data->fannr; i++) {
1289 		if (FAN_CONFIG_MONITOR(data->fan_conf, i))
1290 			data->fan_status[i] = pc87360_read_value(data,
1291 					      LD_FAN, NO_BANK,
1292 					      PC87360_REG_FAN_STATUS(i));
1293 	}
1294 
1295 	if (init > 0) {
1296 		if (devid == 0xe9 && data->address[1]) /* PC87366 */
1297 			use_thermistors = confreg[2] & 0x40;
1298 
1299 		pc87360_init_device(pdev, use_thermistors);
1300 	}
1301 
1302 	/* Register all-or-nothing sysfs groups */
1303 
1304 	if (data->innr) {
1305 		err = sysfs_create_group(&dev->kobj, &pc8736x_vin_group);
1306 		if (err)
1307 			goto error;
1308 	}
1309 
1310 	if (data->innr == 14) {
1311 		err = sysfs_create_group(&dev->kobj, &pc8736x_therm_group);
1312 		if (err)
1313 			goto error;
1314 	}
1315 
1316 	/* create device attr-files for varying sysfs groups */
1317 
1318 	if (data->tempnr) {
1319 		for (i = 0; i < data->tempnr; i++) {
1320 			err = sysfs_create_group(&dev->kobj,
1321 						 &pc8736x_temp_attr_group[i]);
1322 			if (err)
1323 				goto error;
1324 		}
1325 		err = device_create_file(dev, &dev_attr_alarms_temp);
1326 		if (err)
1327 			goto error;
1328 	}
1329 
1330 	for (i = 0; i < data->fannr; i++) {
1331 		if (FAN_CONFIG_MONITOR(data->fan_conf, i)) {
1332 			err = sysfs_create_group(&dev->kobj,
1333 						 &pc8736x_fan_attr_group[i]);
1334 			if (err)
1335 				goto error;
1336 		}
1337 		if (FAN_CONFIG_CONTROL(data->fan_conf, i)) {
1338 			err = device_create_file(dev, &pwm[i].dev_attr);
1339 			if (err)
1340 				goto error;
1341 		}
1342 	}
1343 
1344 	err = device_create_file(dev, &dev_attr_name);
1345 	if (err)
1346 		goto error;
1347 
1348 	data->hwmon_dev = hwmon_device_register(dev);
1349 	if (IS_ERR(data->hwmon_dev)) {
1350 		err = PTR_ERR(data->hwmon_dev);
1351 		goto error;
1352 	}
1353 	return 0;
1354 
1355 error:
1356 	pc87360_remove_files(dev);
1357 	return err;
1358 }
1359 
1360 static int pc87360_remove(struct platform_device *pdev)
1361 {
1362 	struct pc87360_data *data = platform_get_drvdata(pdev);
1363 
1364 	hwmon_device_unregister(data->hwmon_dev);
1365 	pc87360_remove_files(&pdev->dev);
1366 
1367 	return 0;
1368 }
1369 
1370 /*
1371  * ldi is the logical device index
1372  * bank is for voltages and temperatures only
1373  */
1374 static int pc87360_read_value(struct pc87360_data *data, u8 ldi, u8 bank,
1375 			      u8 reg)
1376 {
1377 	int res;
1378 
1379 	mutex_lock(&(data->lock));
1380 	if (bank != NO_BANK)
1381 		outb_p(bank, data->address[ldi] + PC87365_REG_BANK);
1382 	res = inb_p(data->address[ldi] + reg);
1383 	mutex_unlock(&(data->lock));
1384 
1385 	return res;
1386 }
1387 
1388 static void pc87360_write_value(struct pc87360_data *data, u8 ldi, u8 bank,
1389 				u8 reg, u8 value)
1390 {
1391 	mutex_lock(&(data->lock));
1392 	if (bank != NO_BANK)
1393 		outb_p(bank, data->address[ldi] + PC87365_REG_BANK);
1394 	outb_p(value, data->address[ldi] + reg);
1395 	mutex_unlock(&(data->lock));
1396 }
1397 
1398 /* (temp & vin) channel conversion status register flags (pdf sec.11.5.12) */
1399 #define CHAN_CNVRTD	0x80	/* new data ready */
1400 #define CHAN_ENA	0x01	/* enabled channel (temp or vin) */
1401 #define CHAN_ALM_ENA	0x10	/* propagate to alarms-reg ?? (chk val!) */
1402 #define CHAN_READY	(CHAN_ENA|CHAN_CNVRTD) /* sample ready mask */
1403 
1404 #define TEMP_OTS_OE	0x20	/* OTS Output Enable */
1405 #define VIN_RW1C_MASK	(CHAN_READY|CHAN_ALM_MAX|CHAN_ALM_MIN)   /* 0x87 */
1406 #define TEMP_RW1C_MASK	(VIN_RW1C_MASK|TEMP_ALM_CRIT|TEMP_FAULT) /* 0xCF */
1407 
1408 static void pc87360_init_device(struct platform_device *pdev,
1409 				int use_thermistors)
1410 {
1411 	struct pc87360_data *data = platform_get_drvdata(pdev);
1412 	int i, nr;
1413 	const u8 init_in[14] = { 2, 2, 2, 2, 2, 2, 2, 1, 1, 3, 1, 2, 2, 2 };
1414 	const u8 init_temp[3] = { 2, 2, 1 };
1415 	u8 reg;
1416 
1417 	if (init >= 2 && data->innr) {
1418 		reg = pc87360_read_value(data, LD_IN, NO_BANK,
1419 					 PC87365_REG_IN_CONVRATE);
1420 		dev_info(&pdev->dev,
1421 			 "VLM conversion set to 1s period, 160us delay\n");
1422 		pc87360_write_value(data, LD_IN, NO_BANK,
1423 				    PC87365_REG_IN_CONVRATE,
1424 				    (reg & 0xC0) | 0x11);
1425 	}
1426 
1427 	nr = data->innr < 11 ? data->innr : 11;
1428 	for (i = 0; i < nr; i++) {
1429 		reg = pc87360_read_value(data, LD_IN, i,
1430 					 PC87365_REG_IN_STATUS);
1431 		dev_dbg(&pdev->dev, "bios in%d status:0x%02x\n", i, reg);
1432 		if (init >= init_in[i]) {
1433 			/* Forcibly enable voltage channel */
1434 			if (!(reg & CHAN_ENA)) {
1435 				dev_dbg(&pdev->dev, "Forcibly enabling in%d\n",
1436 					i);
1437 				pc87360_write_value(data, LD_IN, i,
1438 						    PC87365_REG_IN_STATUS,
1439 						    (reg & 0x68) | 0x87);
1440 			}
1441 		}
1442 	}
1443 
1444 	/*
1445 	 * We can't blindly trust the Super-I/O space configuration bit,
1446 	 * most BIOS won't set it properly
1447 	 */
1448 	dev_dbg(&pdev->dev, "bios thermistors:%d\n", use_thermistors);
1449 	for (i = 11; i < data->innr; i++) {
1450 		reg = pc87360_read_value(data, LD_IN, i,
1451 					 PC87365_REG_TEMP_STATUS);
1452 		use_thermistors = use_thermistors || (reg & CHAN_ENA);
1453 		/* thermistors are temp[4-6], measured on vin[11-14] */
1454 		dev_dbg(&pdev->dev, "bios temp%d_status:0x%02x\n", i-7, reg);
1455 	}
1456 	dev_dbg(&pdev->dev, "using thermistors:%d\n", use_thermistors);
1457 
1458 	i = use_thermistors ? 2 : 0;
1459 	for (; i < data->tempnr; i++) {
1460 		reg = pc87360_read_value(data, LD_TEMP, i,
1461 					 PC87365_REG_TEMP_STATUS);
1462 		dev_dbg(&pdev->dev, "bios temp%d_status:0x%02x\n", i + 1, reg);
1463 		if (init >= init_temp[i]) {
1464 			/* Forcibly enable temperature channel */
1465 			if (!(reg & CHAN_ENA)) {
1466 				dev_dbg(&pdev->dev,
1467 					"Forcibly enabling temp%d\n", i + 1);
1468 				pc87360_write_value(data, LD_TEMP, i,
1469 						    PC87365_REG_TEMP_STATUS,
1470 						    0xCF);
1471 			}
1472 		}
1473 	}
1474 
1475 	if (use_thermistors) {
1476 		for (i = 11; i < data->innr; i++) {
1477 			if (init >= init_in[i]) {
1478 				/*
1479 				 * The pin may already be used by thermal
1480 				 * diodes
1481 				 */
1482 				reg = pc87360_read_value(data, LD_TEMP,
1483 				      (i - 11) / 2, PC87365_REG_TEMP_STATUS);
1484 				if (reg & CHAN_ENA) {
1485 					dev_dbg(&pdev->dev,
1486 			"Skipping temp%d, pin already in use by temp%d\n",
1487 						i - 7, (i - 11) / 2);
1488 					continue;
1489 				}
1490 
1491 				/* Forcibly enable thermistor channel */
1492 				reg = pc87360_read_value(data, LD_IN, i,
1493 							 PC87365_REG_IN_STATUS);
1494 				if (!(reg & CHAN_ENA)) {
1495 					dev_dbg(&pdev->dev,
1496 						"Forcibly enabling temp%d\n",
1497 						i - 7);
1498 					pc87360_write_value(data, LD_IN, i,
1499 						PC87365_REG_TEMP_STATUS,
1500 						(reg & 0x60) | 0x8F);
1501 				}
1502 			}
1503 		}
1504 	}
1505 
1506 	if (data->innr) {
1507 		reg = pc87360_read_value(data, LD_IN, NO_BANK,
1508 					 PC87365_REG_IN_CONFIG);
1509 		dev_dbg(&pdev->dev, "bios vin-cfg:0x%02x\n", reg);
1510 		if (reg & CHAN_ENA) {
1511 			dev_dbg(&pdev->dev,
1512 				"Forcibly enabling monitoring (VLM)\n");
1513 			pc87360_write_value(data, LD_IN, NO_BANK,
1514 					    PC87365_REG_IN_CONFIG,
1515 					    reg & 0xFE);
1516 		}
1517 	}
1518 
1519 	if (data->tempnr) {
1520 		reg = pc87360_read_value(data, LD_TEMP, NO_BANK,
1521 					 PC87365_REG_TEMP_CONFIG);
1522 		dev_dbg(&pdev->dev, "bios temp-cfg:0x%02x\n", reg);
1523 		if (reg & CHAN_ENA) {
1524 			dev_dbg(&pdev->dev,
1525 				"Forcibly enabling monitoring (TMS)\n");
1526 			pc87360_write_value(data, LD_TEMP, NO_BANK,
1527 					    PC87365_REG_TEMP_CONFIG,
1528 					    reg & 0xFE);
1529 		}
1530 
1531 		if (init >= 2) {
1532 			/* Chip config as documented by National Semi. */
1533 			pc87360_write_value(data, LD_TEMP, 0xF, 0xA, 0x08);
1534 			/*
1535 			 * We voluntarily omit the bank here, in case the
1536 			 * sequence itself matters. It shouldn't be a problem,
1537 			 * since nobody else is supposed to access the
1538 			 * device at that point.
1539 			 */
1540 			pc87360_write_value(data, LD_TEMP, NO_BANK, 0xB, 0x04);
1541 			pc87360_write_value(data, LD_TEMP, NO_BANK, 0xC, 0x35);
1542 			pc87360_write_value(data, LD_TEMP, NO_BANK, 0xD, 0x05);
1543 			pc87360_write_value(data, LD_TEMP, NO_BANK, 0xE, 0x05);
1544 		}
1545 	}
1546 }
1547 
1548 static void pc87360_autodiv(struct device *dev, int nr)
1549 {
1550 	struct pc87360_data *data = dev_get_drvdata(dev);
1551 	u8 old_min = data->fan_min[nr];
1552 
1553 	/* Increase clock divider if needed and possible */
1554 	if ((data->fan_status[nr] & 0x04) /* overflow flag */
1555 	 || (data->fan[nr] >= 224)) { /* next to overflow */
1556 		if ((data->fan_status[nr] & 0x60) != 0x60) {
1557 			data->fan_status[nr] += 0x20;
1558 			data->fan_min[nr] >>= 1;
1559 			data->fan[nr] >>= 1;
1560 			dev_dbg(dev,
1561 				"Increasing clock divider to %d for fan %d\n",
1562 				FAN_DIV_FROM_REG(data->fan_status[nr]), nr + 1);
1563 		}
1564 	} else {
1565 		/* Decrease clock divider if possible */
1566 		while (!(data->fan_min[nr] & 0x80) /* min "nails" divider */
1567 		 && data->fan[nr] < 85 /* bad accuracy */
1568 		 && (data->fan_status[nr] & 0x60) != 0x00) {
1569 			data->fan_status[nr] -= 0x20;
1570 			data->fan_min[nr] <<= 1;
1571 			data->fan[nr] <<= 1;
1572 			dev_dbg(dev,
1573 				"Decreasing clock divider to %d for fan %d\n",
1574 				FAN_DIV_FROM_REG(data->fan_status[nr]),
1575 				nr + 1);
1576 		}
1577 	}
1578 
1579 	/* Write new fan min if it changed */
1580 	if (old_min != data->fan_min[nr]) {
1581 		pc87360_write_value(data, LD_FAN, NO_BANK,
1582 				    PC87360_REG_FAN_MIN(nr),
1583 				    data->fan_min[nr]);
1584 	}
1585 }
1586 
1587 static struct pc87360_data *pc87360_update_device(struct device *dev)
1588 {
1589 	struct pc87360_data *data = dev_get_drvdata(dev);
1590 	u8 i;
1591 
1592 	mutex_lock(&data->update_lock);
1593 
1594 	if (time_after(jiffies, data->last_updated + HZ * 2) || !data->valid) {
1595 		dev_dbg(dev, "Data update\n");
1596 
1597 		/* Fans */
1598 		for (i = 0; i < data->fannr; i++) {
1599 			if (FAN_CONFIG_MONITOR(data->fan_conf, i)) {
1600 				data->fan_status[i] =
1601 					pc87360_read_value(data, LD_FAN,
1602 					NO_BANK, PC87360_REG_FAN_STATUS(i));
1603 				data->fan[i] = pc87360_read_value(data, LD_FAN,
1604 					       NO_BANK, PC87360_REG_FAN(i));
1605 				data->fan_min[i] = pc87360_read_value(data,
1606 						   LD_FAN, NO_BANK,
1607 						   PC87360_REG_FAN_MIN(i));
1608 				/* Change clock divider if needed */
1609 				pc87360_autodiv(dev, i);
1610 				/* Clear bits and write new divider */
1611 				pc87360_write_value(data, LD_FAN, NO_BANK,
1612 						    PC87360_REG_FAN_STATUS(i),
1613 						    data->fan_status[i]);
1614 			}
1615 			if (FAN_CONFIG_CONTROL(data->fan_conf, i))
1616 				data->pwm[i] = pc87360_read_value(data, LD_FAN,
1617 					       NO_BANK, PC87360_REG_PWM(i));
1618 		}
1619 
1620 		/* Voltages */
1621 		for (i = 0; i < data->innr; i++) {
1622 			data->in_status[i] = pc87360_read_value(data, LD_IN, i,
1623 					     PC87365_REG_IN_STATUS);
1624 			/* Clear bits */
1625 			pc87360_write_value(data, LD_IN, i,
1626 					    PC87365_REG_IN_STATUS,
1627 					    data->in_status[i]);
1628 			if ((data->in_status[i] & CHAN_READY) == CHAN_READY) {
1629 				data->in[i] = pc87360_read_value(data, LD_IN,
1630 					      i, PC87365_REG_IN);
1631 			}
1632 			if (data->in_status[i] & CHAN_ENA) {
1633 				data->in_min[i] = pc87360_read_value(data,
1634 						  LD_IN, i,
1635 						  PC87365_REG_IN_MIN);
1636 				data->in_max[i] = pc87360_read_value(data,
1637 						  LD_IN, i,
1638 						  PC87365_REG_IN_MAX);
1639 				if (i >= 11)
1640 					data->in_crit[i-11] =
1641 						pc87360_read_value(data, LD_IN,
1642 						i, PC87365_REG_TEMP_CRIT);
1643 			}
1644 		}
1645 		if (data->innr) {
1646 			data->in_alarms = pc87360_read_value(data, LD_IN,
1647 					  NO_BANK, PC87365_REG_IN_ALARMS1)
1648 					| ((pc87360_read_value(data, LD_IN,
1649 					    NO_BANK, PC87365_REG_IN_ALARMS2)
1650 					    & 0x07) << 8);
1651 			data->vid = (data->vid_conf & 0xE0) ?
1652 				    pc87360_read_value(data, LD_IN,
1653 				    NO_BANK, PC87365_REG_VID) : 0x1F;
1654 		}
1655 
1656 		/* Temperatures */
1657 		for (i = 0; i < data->tempnr; i++) {
1658 			data->temp_status[i] = pc87360_read_value(data,
1659 					       LD_TEMP, i,
1660 					       PC87365_REG_TEMP_STATUS);
1661 			/* Clear bits */
1662 			pc87360_write_value(data, LD_TEMP, i,
1663 					    PC87365_REG_TEMP_STATUS,
1664 					    data->temp_status[i]);
1665 			if ((data->temp_status[i] & CHAN_READY) == CHAN_READY) {
1666 				data->temp[i] = pc87360_read_value(data,
1667 						LD_TEMP, i,
1668 						PC87365_REG_TEMP);
1669 			}
1670 			if (data->temp_status[i] & CHAN_ENA) {
1671 				data->temp_min[i] = pc87360_read_value(data,
1672 						    LD_TEMP, i,
1673 						    PC87365_REG_TEMP_MIN);
1674 				data->temp_max[i] = pc87360_read_value(data,
1675 						    LD_TEMP, i,
1676 						    PC87365_REG_TEMP_MAX);
1677 				data->temp_crit[i] = pc87360_read_value(data,
1678 						     LD_TEMP, i,
1679 						     PC87365_REG_TEMP_CRIT);
1680 			}
1681 		}
1682 		if (data->tempnr) {
1683 			data->temp_alarms = pc87360_read_value(data, LD_TEMP,
1684 					    NO_BANK, PC87365_REG_TEMP_ALARMS)
1685 					    & 0x3F;
1686 		}
1687 
1688 		data->last_updated = jiffies;
1689 		data->valid = 1;
1690 	}
1691 
1692 	mutex_unlock(&data->update_lock);
1693 
1694 	return data;
1695 }
1696 
1697 static int __init pc87360_device_add(unsigned short address)
1698 {
1699 	struct resource res[3];
1700 	int err, i, res_count;
1701 
1702 	pdev = platform_device_alloc("pc87360", address);
1703 	if (!pdev) {
1704 		err = -ENOMEM;
1705 		pr_err("Device allocation failed\n");
1706 		goto exit;
1707 	}
1708 
1709 	memset(res, 0, 3 * sizeof(struct resource));
1710 	res_count = 0;
1711 	for (i = 0; i < 3; i++) {
1712 		if (!extra_isa[i])
1713 			continue;
1714 		res[res_count].start = extra_isa[i];
1715 		res[res_count].end = extra_isa[i] + PC87360_EXTENT - 1;
1716 		res[res_count].name = "pc87360",
1717 		res[res_count].flags = IORESOURCE_IO,
1718 
1719 		err = acpi_check_resource_conflict(&res[res_count]);
1720 		if (err)
1721 			goto exit_device_put;
1722 
1723 		res_count++;
1724 	}
1725 
1726 	err = platform_device_add_resources(pdev, res, res_count);
1727 	if (err) {
1728 		pr_err("Device resources addition failed (%d)\n", err);
1729 		goto exit_device_put;
1730 	}
1731 
1732 	err = platform_device_add(pdev);
1733 	if (err) {
1734 		pr_err("Device addition failed (%d)\n", err);
1735 		goto exit_device_put;
1736 	}
1737 
1738 	return 0;
1739 
1740 exit_device_put:
1741 	platform_device_put(pdev);
1742 exit:
1743 	return err;
1744 }
1745 
1746 static int __init pc87360_init(void)
1747 {
1748 	int err, i;
1749 	unsigned short address = 0;
1750 
1751 	if (pc87360_find(0x2e, &devid, extra_isa)
1752 	 && pc87360_find(0x4e, &devid, extra_isa)) {
1753 		pr_warn("PC8736x not detected, module not inserted\n");
1754 		return -ENODEV;
1755 	}
1756 
1757 	/* Arbitrarily pick one of the addresses */
1758 	for (i = 0; i < 3; i++) {
1759 		if (extra_isa[i] != 0x0000) {
1760 			address = extra_isa[i];
1761 			break;
1762 		}
1763 	}
1764 
1765 	if (address == 0x0000) {
1766 		pr_warn("No active logical device, module not inserted\n");
1767 		return -ENODEV;
1768 	}
1769 
1770 	err = platform_driver_register(&pc87360_driver);
1771 	if (err)
1772 		goto exit;
1773 
1774 	/* Sets global pdev as a side effect */
1775 	err = pc87360_device_add(address);
1776 	if (err)
1777 		goto exit_driver;
1778 
1779 	return 0;
1780 
1781  exit_driver:
1782 	platform_driver_unregister(&pc87360_driver);
1783  exit:
1784 	return err;
1785 }
1786 
1787 static void __exit pc87360_exit(void)
1788 {
1789 	platform_device_unregister(pdev);
1790 	platform_driver_unregister(&pc87360_driver);
1791 }
1792 
1793 
1794 MODULE_AUTHOR("Jean Delvare <jdelvare@suse.de>");
1795 MODULE_DESCRIPTION("PC8736x hardware monitor");
1796 MODULE_LICENSE("GPL");
1797 
1798 module_init(pc87360_init);
1799 module_exit(pc87360_exit);
1800