md: lock address when changing attributes of component devices
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / hwmon / pc87360.c
blob9d660133d517302535c1d42ef6e0ee79097b9acc
1 /*
2 * pc87360.c - Part of lm_sensors, Linux kernel modules
3 * for hardware monitoring
4 * Copyright (C) 2004, 2007 Jean Delvare <khali@linux-fr.org>
6 * Copied from smsc47m1.c:
7 * Copyright (C) 2002 Mark D. Studebaker <mdsxyz123@yahoo.com>
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.
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.
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.
23 * Supports the following chips:
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
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).
36 #include <linux/module.h>
37 #include <linux/init.h>
38 #include <linux/slab.h>
39 #include <linux/jiffies.h>
40 #include <linux/platform_device.h>
41 #include <linux/hwmon.h>
42 #include <linux/hwmon-sysfs.h>
43 #include <linux/hwmon-vid.h>
44 #include <linux/err.h>
45 #include <linux/mutex.h>
46 #include <asm/io.h>
48 static u8 devid;
49 static struct platform_device *pdev;
50 static unsigned short extra_isa[3];
51 static u8 confreg[4];
53 static int init = 1;
54 module_param(init, int, 0);
55 MODULE_PARM_DESC(init,
56 "Chip initialization level:\n"
57 " 0: None\n"
58 "*1: Forcibly enable internal voltage and temperature channels, except in9\n"
59 " 2: Forcibly enable all voltage and temperature channels, except in9\n"
60 " 3: Forcibly enable all voltage and temperature channels, including in9");
63 * Super-I/O registers and operations
66 #define DEV 0x07 /* Register: Logical device select */
67 #define DEVID 0x20 /* Register: Device ID */
68 #define ACT 0x30 /* Register: Device activation */
69 #define BASE 0x60 /* Register: Base address */
71 #define FSCM 0x09 /* Logical device: fans */
72 #define VLM 0x0d /* Logical device: voltages */
73 #define TMS 0x0e /* Logical device: temperatures */
74 static const u8 logdev[3] = { FSCM, VLM, TMS };
76 #define LD_FAN 0
77 #define LD_IN 1
78 #define LD_TEMP 2
80 static inline void superio_outb(int sioaddr, int reg, int val)
82 outb(reg, sioaddr);
83 outb(val, sioaddr+1);
86 static inline int superio_inb(int sioaddr, int reg)
88 outb(reg, sioaddr);
89 return inb(sioaddr+1);
92 static inline void superio_exit(int sioaddr)
94 outb(0x02, sioaddr);
95 outb(0x02, sioaddr+1);
99 * Logical devices
102 #define PC87360_EXTENT 0x10
103 #define PC87365_REG_BANK 0x09
104 #define NO_BANK 0xff
107 * Fan registers and conversions
110 /* nr has to be 0 or 1 (PC87360/87363) or 2 (PC87364/87365/87366) */
111 #define PC87360_REG_PRESCALE(nr) (0x00 + 2 * (nr))
112 #define PC87360_REG_PWM(nr) (0x01 + 2 * (nr))
113 #define PC87360_REG_FAN_MIN(nr) (0x06 + 3 * (nr))
114 #define PC87360_REG_FAN(nr) (0x07 + 3 * (nr))
115 #define PC87360_REG_FAN_STATUS(nr) (0x08 + 3 * (nr))
117 #define FAN_FROM_REG(val,div) ((val) == 0 ? 0: \
118 480000 / ((val)*(div)))
119 #define FAN_TO_REG(val,div) ((val) <= 100 ? 0 : \
120 480000 / ((val)*(div)))
121 #define FAN_DIV_FROM_REG(val) (1 << ((val >> 5) & 0x03))
122 #define FAN_STATUS_FROM_REG(val) ((val) & 0x07)
124 #define FAN_CONFIG_MONITOR(val,nr) (((val) >> (2 + nr * 3)) & 1)
125 #define FAN_CONFIG_CONTROL(val,nr) (((val) >> (3 + nr * 3)) & 1)
126 #define FAN_CONFIG_INVERT(val,nr) (((val) >> (4 + nr * 3)) & 1)
128 #define PWM_FROM_REG(val,inv) ((inv) ? 255 - (val) : (val))
129 static inline u8 PWM_TO_REG(int val, int inv)
131 if (inv)
132 val = 255 - val;
133 if (val < 0)
134 return 0;
135 if (val > 255)
136 return 255;
137 return val;
141 * Voltage registers and conversions
144 #define PC87365_REG_IN_CONVRATE 0x07
145 #define PC87365_REG_IN_CONFIG 0x08
146 #define PC87365_REG_IN 0x0B
147 #define PC87365_REG_IN_MIN 0x0D
148 #define PC87365_REG_IN_MAX 0x0C
149 #define PC87365_REG_IN_STATUS 0x0A
150 #define PC87365_REG_IN_ALARMS1 0x00
151 #define PC87365_REG_IN_ALARMS2 0x01
152 #define PC87365_REG_VID 0x06
154 #define IN_FROM_REG(val,ref) (((val) * (ref) + 128) / 256)
155 #define IN_TO_REG(val,ref) ((val) < 0 ? 0 : \
156 (val)*256 >= (ref)*255 ? 255: \
157 ((val) * 256 + (ref)/2) / (ref))
160 * Temperature registers and conversions
163 #define PC87365_REG_TEMP_CONFIG 0x08
164 #define PC87365_REG_TEMP 0x0B
165 #define PC87365_REG_TEMP_MIN 0x0D
166 #define PC87365_REG_TEMP_MAX 0x0C
167 #define PC87365_REG_TEMP_CRIT 0x0E
168 #define PC87365_REG_TEMP_STATUS 0x0A
169 #define PC87365_REG_TEMP_ALARMS 0x00
171 #define TEMP_FROM_REG(val) ((val) * 1000)
172 #define TEMP_TO_REG(val) ((val) < -55000 ? -55 : \
173 (val) > 127000 ? 127 : \
174 (val) < 0 ? ((val) - 500) / 1000 : \
175 ((val) + 500) / 1000)
178 * Device data
181 struct pc87360_data {
182 const char *name;
183 struct device *hwmon_dev;
184 struct mutex lock;
185 struct mutex update_lock;
186 char valid; /* !=0 if following fields are valid */
187 unsigned long last_updated; /* In jiffies */
189 int address[3];
191 u8 fannr, innr, tempnr;
193 u8 fan[3]; /* Register value */
194 u8 fan_min[3]; /* Register value */
195 u8 fan_status[3]; /* Register value */
196 u8 pwm[3]; /* Register value */
197 u16 fan_conf; /* Configuration register values, combined */
199 u16 in_vref; /* 1 mV/bit */
200 u8 in[14]; /* Register value */
201 u8 in_min[14]; /* Register value */
202 u8 in_max[14]; /* Register value */
203 u8 in_crit[3]; /* Register value */
204 u8 in_status[14]; /* Register value */
205 u16 in_alarms; /* Register values, combined, masked */
206 u8 vid_conf; /* Configuration register value */
207 u8 vrm;
208 u8 vid; /* Register value */
210 s8 temp[3]; /* Register value */
211 s8 temp_min[3]; /* Register value */
212 s8 temp_max[3]; /* Register value */
213 s8 temp_crit[3]; /* Register value */
214 u8 temp_status[3]; /* Register value */
215 u8 temp_alarms; /* Register value, masked */
219 * Functions declaration
222 static int pc87360_probe(struct platform_device *pdev);
223 static int __devexit pc87360_remove(struct platform_device *pdev);
225 static int pc87360_read_value(struct pc87360_data *data, u8 ldi, u8 bank,
226 u8 reg);
227 static void pc87360_write_value(struct pc87360_data *data, u8 ldi, u8 bank,
228 u8 reg, u8 value);
229 static void pc87360_init_device(struct platform_device *pdev,
230 int use_thermistors);
231 static struct pc87360_data *pc87360_update_device(struct device *dev);
234 * Driver data
237 static struct platform_driver pc87360_driver = {
238 .driver = {
239 .owner = THIS_MODULE,
240 .name = "pc87360",
242 .probe = pc87360_probe,
243 .remove = __devexit_p(pc87360_remove),
247 * Sysfs stuff
250 static ssize_t show_fan_input(struct device *dev, struct device_attribute *devattr, char *buf)
252 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
253 struct pc87360_data *data = pc87360_update_device(dev);
254 return sprintf(buf, "%u\n", FAN_FROM_REG(data->fan[attr->index],
255 FAN_DIV_FROM_REG(data->fan_status[attr->index])));
257 static ssize_t show_fan_min(struct device *dev, struct device_attribute *devattr, char *buf)
259 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
260 struct pc87360_data *data = pc87360_update_device(dev);
261 return sprintf(buf, "%u\n", FAN_FROM_REG(data->fan_min[attr->index],
262 FAN_DIV_FROM_REG(data->fan_status[attr->index])));
264 static ssize_t show_fan_div(struct device *dev, struct device_attribute *devattr, char *buf)
266 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
267 struct pc87360_data *data = pc87360_update_device(dev);
268 return sprintf(buf, "%u\n",
269 FAN_DIV_FROM_REG(data->fan_status[attr->index]));
271 static ssize_t show_fan_status(struct device *dev, struct device_attribute *devattr, char *buf)
273 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
274 struct pc87360_data *data = pc87360_update_device(dev);
275 return sprintf(buf, "%u\n",
276 FAN_STATUS_FROM_REG(data->fan_status[attr->index]));
278 static ssize_t set_fan_min(struct device *dev, struct device_attribute *devattr, const char *buf,
279 size_t count)
281 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
282 struct pc87360_data *data = dev_get_drvdata(dev);
283 long fan_min = simple_strtol(buf, NULL, 10);
285 mutex_lock(&data->update_lock);
286 fan_min = FAN_TO_REG(fan_min, FAN_DIV_FROM_REG(data->fan_status[attr->index]));
288 /* If it wouldn't fit, change clock divisor */
289 while (fan_min > 255
290 && (data->fan_status[attr->index] & 0x60) != 0x60) {
291 fan_min >>= 1;
292 data->fan[attr->index] >>= 1;
293 data->fan_status[attr->index] += 0x20;
295 data->fan_min[attr->index] = fan_min > 255 ? 255 : fan_min;
296 pc87360_write_value(data, LD_FAN, NO_BANK, PC87360_REG_FAN_MIN(attr->index),
297 data->fan_min[attr->index]);
299 /* Write new divider, preserve alarm bits */
300 pc87360_write_value(data, LD_FAN, NO_BANK, PC87360_REG_FAN_STATUS(attr->index),
301 data->fan_status[attr->index] & 0xF9);
302 mutex_unlock(&data->update_lock);
304 return count;
307 static struct sensor_device_attribute fan_input[] = {
308 SENSOR_ATTR(fan1_input, S_IRUGO, show_fan_input, NULL, 0),
309 SENSOR_ATTR(fan2_input, S_IRUGO, show_fan_input, NULL, 1),
310 SENSOR_ATTR(fan3_input, S_IRUGO, show_fan_input, NULL, 2),
312 static struct sensor_device_attribute fan_status[] = {
313 SENSOR_ATTR(fan1_status, S_IRUGO, show_fan_status, NULL, 0),
314 SENSOR_ATTR(fan2_status, S_IRUGO, show_fan_status, NULL, 1),
315 SENSOR_ATTR(fan3_status, S_IRUGO, show_fan_status, NULL, 2),
317 static struct sensor_device_attribute fan_div[] = {
318 SENSOR_ATTR(fan1_div, S_IRUGO, show_fan_div, NULL, 0),
319 SENSOR_ATTR(fan2_div, S_IRUGO, show_fan_div, NULL, 1),
320 SENSOR_ATTR(fan3_div, S_IRUGO, show_fan_div, NULL, 2),
322 static struct sensor_device_attribute fan_min[] = {
323 SENSOR_ATTR(fan1_min, S_IWUSR | S_IRUGO, show_fan_min, set_fan_min, 0),
324 SENSOR_ATTR(fan2_min, S_IWUSR | S_IRUGO, show_fan_min, set_fan_min, 1),
325 SENSOR_ATTR(fan3_min, S_IWUSR | S_IRUGO, show_fan_min, set_fan_min, 2),
328 #define FAN_UNIT_ATTRS(X) \
329 &fan_input[X].dev_attr.attr, \
330 &fan_status[X].dev_attr.attr, \
331 &fan_div[X].dev_attr.attr, \
332 &fan_min[X].dev_attr.attr
334 static ssize_t show_pwm(struct device *dev, struct device_attribute *devattr, char *buf)
336 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
337 struct pc87360_data *data = pc87360_update_device(dev);
338 return sprintf(buf, "%u\n",
339 PWM_FROM_REG(data->pwm[attr->index],
340 FAN_CONFIG_INVERT(data->fan_conf,
341 attr->index)));
343 static ssize_t set_pwm(struct device *dev, struct device_attribute *devattr, const char *buf,
344 size_t count)
346 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
347 struct pc87360_data *data = dev_get_drvdata(dev);
348 long val = simple_strtol(buf, NULL, 10);
350 mutex_lock(&data->update_lock);
351 data->pwm[attr->index] = PWM_TO_REG(val,
352 FAN_CONFIG_INVERT(data->fan_conf, attr->index));
353 pc87360_write_value(data, LD_FAN, NO_BANK, PC87360_REG_PWM(attr->index),
354 data->pwm[attr->index]);
355 mutex_unlock(&data->update_lock);
356 return count;
359 static struct sensor_device_attribute pwm[] = {
360 SENSOR_ATTR(pwm1, S_IWUSR | S_IRUGO, show_pwm, set_pwm, 0),
361 SENSOR_ATTR(pwm2, S_IWUSR | S_IRUGO, show_pwm, set_pwm, 1),
362 SENSOR_ATTR(pwm3, S_IWUSR | S_IRUGO, show_pwm, set_pwm, 2),
365 static struct attribute * pc8736x_fan_attr_array[] = {
366 FAN_UNIT_ATTRS(0),
367 FAN_UNIT_ATTRS(1),
368 FAN_UNIT_ATTRS(2),
369 &pwm[0].dev_attr.attr,
370 &pwm[1].dev_attr.attr,
371 &pwm[2].dev_attr.attr,
372 NULL
374 static const struct attribute_group pc8736x_fan_group = {
375 .attrs = pc8736x_fan_attr_array,
378 static ssize_t show_in_input(struct device *dev, struct device_attribute *devattr, char *buf)
380 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
381 struct pc87360_data *data = pc87360_update_device(dev);
382 return sprintf(buf, "%u\n", IN_FROM_REG(data->in[attr->index],
383 data->in_vref));
385 static ssize_t show_in_min(struct device *dev, struct device_attribute *devattr, char *buf)
387 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
388 struct pc87360_data *data = pc87360_update_device(dev);
389 return sprintf(buf, "%u\n", IN_FROM_REG(data->in_min[attr->index],
390 data->in_vref));
392 static ssize_t show_in_max(struct device *dev, struct device_attribute *devattr, char *buf)
394 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
395 struct pc87360_data *data = pc87360_update_device(dev);
396 return sprintf(buf, "%u\n", IN_FROM_REG(data->in_max[attr->index],
397 data->in_vref));
399 static ssize_t show_in_status(struct device *dev, struct device_attribute *devattr, char *buf)
401 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
402 struct pc87360_data *data = pc87360_update_device(dev);
403 return sprintf(buf, "%u\n", data->in_status[attr->index]);
405 static ssize_t set_in_min(struct device *dev, struct device_attribute *devattr, const char *buf,
406 size_t count)
408 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
409 struct pc87360_data *data = dev_get_drvdata(dev);
410 long val = simple_strtol(buf, NULL, 10);
412 mutex_lock(&data->update_lock);
413 data->in_min[attr->index] = IN_TO_REG(val, data->in_vref);
414 pc87360_write_value(data, LD_IN, attr->index, PC87365_REG_IN_MIN,
415 data->in_min[attr->index]);
416 mutex_unlock(&data->update_lock);
417 return count;
419 static ssize_t set_in_max(struct device *dev, struct device_attribute *devattr, const char *buf,
420 size_t count)
422 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
423 struct pc87360_data *data = dev_get_drvdata(dev);
424 long val = simple_strtol(buf, NULL, 10);
426 mutex_lock(&data->update_lock);
427 data->in_max[attr->index] = IN_TO_REG(val,
428 data->in_vref);
429 pc87360_write_value(data, LD_IN, attr->index, PC87365_REG_IN_MAX,
430 data->in_max[attr->index]);
431 mutex_unlock(&data->update_lock);
432 return count;
435 static struct sensor_device_attribute in_input[] = {
436 SENSOR_ATTR(in0_input, S_IRUGO, show_in_input, NULL, 0),
437 SENSOR_ATTR(in1_input, S_IRUGO, show_in_input, NULL, 1),
438 SENSOR_ATTR(in2_input, S_IRUGO, show_in_input, NULL, 2),
439 SENSOR_ATTR(in3_input, S_IRUGO, show_in_input, NULL, 3),
440 SENSOR_ATTR(in4_input, S_IRUGO, show_in_input, NULL, 4),
441 SENSOR_ATTR(in5_input, S_IRUGO, show_in_input, NULL, 5),
442 SENSOR_ATTR(in6_input, S_IRUGO, show_in_input, NULL, 6),
443 SENSOR_ATTR(in7_input, S_IRUGO, show_in_input, NULL, 7),
444 SENSOR_ATTR(in8_input, S_IRUGO, show_in_input, NULL, 8),
445 SENSOR_ATTR(in9_input, S_IRUGO, show_in_input, NULL, 9),
446 SENSOR_ATTR(in10_input, S_IRUGO, show_in_input, NULL, 10),
448 static struct sensor_device_attribute in_status[] = {
449 SENSOR_ATTR(in0_status, S_IRUGO, show_in_status, NULL, 0),
450 SENSOR_ATTR(in1_status, S_IRUGO, show_in_status, NULL, 1),
451 SENSOR_ATTR(in2_status, S_IRUGO, show_in_status, NULL, 2),
452 SENSOR_ATTR(in3_status, S_IRUGO, show_in_status, NULL, 3),
453 SENSOR_ATTR(in4_status, S_IRUGO, show_in_status, NULL, 4),
454 SENSOR_ATTR(in5_status, S_IRUGO, show_in_status, NULL, 5),
455 SENSOR_ATTR(in6_status, S_IRUGO, show_in_status, NULL, 6),
456 SENSOR_ATTR(in7_status, S_IRUGO, show_in_status, NULL, 7),
457 SENSOR_ATTR(in8_status, S_IRUGO, show_in_status, NULL, 8),
458 SENSOR_ATTR(in9_status, S_IRUGO, show_in_status, NULL, 9),
459 SENSOR_ATTR(in10_status, S_IRUGO, show_in_status, NULL, 10),
461 static struct sensor_device_attribute in_min[] = {
462 SENSOR_ATTR(in0_min, S_IWUSR | S_IRUGO, show_in_min, set_in_min, 0),
463 SENSOR_ATTR(in1_min, S_IWUSR | S_IRUGO, show_in_min, set_in_min, 1),
464 SENSOR_ATTR(in2_min, S_IWUSR | S_IRUGO, show_in_min, set_in_min, 2),
465 SENSOR_ATTR(in3_min, S_IWUSR | S_IRUGO, show_in_min, set_in_min, 3),
466 SENSOR_ATTR(in4_min, S_IWUSR | S_IRUGO, show_in_min, set_in_min, 4),
467 SENSOR_ATTR(in5_min, S_IWUSR | S_IRUGO, show_in_min, set_in_min, 5),
468 SENSOR_ATTR(in6_min, S_IWUSR | S_IRUGO, show_in_min, set_in_min, 6),
469 SENSOR_ATTR(in7_min, S_IWUSR | S_IRUGO, show_in_min, set_in_min, 7),
470 SENSOR_ATTR(in8_min, S_IWUSR | S_IRUGO, show_in_min, set_in_min, 8),
471 SENSOR_ATTR(in9_min, S_IWUSR | S_IRUGO, show_in_min, set_in_min, 9),
472 SENSOR_ATTR(in10_min, S_IWUSR | S_IRUGO, show_in_min, set_in_min, 10),
474 static struct sensor_device_attribute in_max[] = {
475 SENSOR_ATTR(in0_max, S_IWUSR | S_IRUGO, show_in_max, set_in_max, 0),
476 SENSOR_ATTR(in1_max, S_IWUSR | S_IRUGO, show_in_max, set_in_max, 1),
477 SENSOR_ATTR(in2_max, S_IWUSR | S_IRUGO, show_in_max, set_in_max, 2),
478 SENSOR_ATTR(in3_max, S_IWUSR | S_IRUGO, show_in_max, set_in_max, 3),
479 SENSOR_ATTR(in4_max, S_IWUSR | S_IRUGO, show_in_max, set_in_max, 4),
480 SENSOR_ATTR(in5_max, S_IWUSR | S_IRUGO, show_in_max, set_in_max, 5),
481 SENSOR_ATTR(in6_max, S_IWUSR | S_IRUGO, show_in_max, set_in_max, 6),
482 SENSOR_ATTR(in7_max, S_IWUSR | S_IRUGO, show_in_max, set_in_max, 7),
483 SENSOR_ATTR(in8_max, S_IWUSR | S_IRUGO, show_in_max, set_in_max, 8),
484 SENSOR_ATTR(in9_max, S_IWUSR | S_IRUGO, show_in_max, set_in_max, 9),
485 SENSOR_ATTR(in10_max, S_IWUSR | S_IRUGO, show_in_max, set_in_max, 10),
488 #define VIN_UNIT_ATTRS(X) \
489 &in_input[X].dev_attr.attr, \
490 &in_status[X].dev_attr.attr, \
491 &in_min[X].dev_attr.attr, \
492 &in_max[X].dev_attr.attr
494 static ssize_t show_vid(struct device *dev, struct device_attribute *attr, char *buf)
496 struct pc87360_data *data = pc87360_update_device(dev);
497 return sprintf(buf, "%u\n", vid_from_reg(data->vid, data->vrm));
499 static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL);
501 static ssize_t show_vrm(struct device *dev, struct device_attribute *attr, char *buf)
503 struct pc87360_data *data = dev_get_drvdata(dev);
504 return sprintf(buf, "%u\n", data->vrm);
506 static ssize_t set_vrm(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
508 struct pc87360_data *data = dev_get_drvdata(dev);
509 data->vrm = simple_strtoul(buf, NULL, 10);
510 return count;
512 static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm, set_vrm);
514 static ssize_t show_in_alarms(struct device *dev, struct device_attribute *attr, char *buf)
516 struct pc87360_data *data = pc87360_update_device(dev);
517 return sprintf(buf, "%u\n", data->in_alarms);
519 static DEVICE_ATTR(alarms_in, S_IRUGO, show_in_alarms, NULL);
521 static struct attribute *pc8736x_vin_attr_array[] = {
522 VIN_UNIT_ATTRS(0),
523 VIN_UNIT_ATTRS(1),
524 VIN_UNIT_ATTRS(2),
525 VIN_UNIT_ATTRS(3),
526 VIN_UNIT_ATTRS(4),
527 VIN_UNIT_ATTRS(5),
528 VIN_UNIT_ATTRS(6),
529 VIN_UNIT_ATTRS(7),
530 VIN_UNIT_ATTRS(8),
531 VIN_UNIT_ATTRS(9),
532 VIN_UNIT_ATTRS(10),
533 &dev_attr_cpu0_vid.attr,
534 &dev_attr_vrm.attr,
535 &dev_attr_alarms_in.attr,
536 NULL
538 static const struct attribute_group pc8736x_vin_group = {
539 .attrs = pc8736x_vin_attr_array,
542 static ssize_t show_therm_input(struct device *dev, struct device_attribute *devattr, char *buf)
544 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
545 struct pc87360_data *data = pc87360_update_device(dev);
546 return sprintf(buf, "%u\n", IN_FROM_REG(data->in[attr->index],
547 data->in_vref));
549 static ssize_t show_therm_min(struct device *dev, struct device_attribute *devattr, char *buf)
551 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
552 struct pc87360_data *data = pc87360_update_device(dev);
553 return sprintf(buf, "%u\n", IN_FROM_REG(data->in_min[attr->index],
554 data->in_vref));
556 static ssize_t show_therm_max(struct device *dev, struct device_attribute *devattr, char *buf)
558 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
559 struct pc87360_data *data = pc87360_update_device(dev);
560 return sprintf(buf, "%u\n", IN_FROM_REG(data->in_max[attr->index],
561 data->in_vref));
563 static ssize_t show_therm_crit(struct device *dev, struct device_attribute *devattr, char *buf)
565 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
566 struct pc87360_data *data = pc87360_update_device(dev);
567 return sprintf(buf, "%u\n", IN_FROM_REG(data->in_crit[attr->index-11],
568 data->in_vref));
570 static ssize_t show_therm_status(struct device *dev, struct device_attribute *devattr, char *buf)
572 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
573 struct pc87360_data *data = pc87360_update_device(dev);
574 return sprintf(buf, "%u\n", data->in_status[attr->index]);
576 static ssize_t set_therm_min(struct device *dev, struct device_attribute *devattr, const char *buf,
577 size_t count)
579 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
580 struct pc87360_data *data = dev_get_drvdata(dev);
581 long val = simple_strtol(buf, NULL, 10);
583 mutex_lock(&data->update_lock);
584 data->in_min[attr->index] = IN_TO_REG(val, data->in_vref);
585 pc87360_write_value(data, LD_IN, attr->index, PC87365_REG_TEMP_MIN,
586 data->in_min[attr->index]);
587 mutex_unlock(&data->update_lock);
588 return count;
590 static ssize_t set_therm_max(struct device *dev, struct device_attribute *devattr, const char *buf,
591 size_t count)
593 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
594 struct pc87360_data *data = dev_get_drvdata(dev);
595 long val = simple_strtol(buf, NULL, 10);
597 mutex_lock(&data->update_lock);
598 data->in_max[attr->index] = IN_TO_REG(val, data->in_vref);
599 pc87360_write_value(data, LD_IN, attr->index, PC87365_REG_TEMP_MAX,
600 data->in_max[attr->index]);
601 mutex_unlock(&data->update_lock);
602 return count;
604 static ssize_t set_therm_crit(struct device *dev, struct device_attribute *devattr, const char *buf,
605 size_t count)
607 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
608 struct pc87360_data *data = dev_get_drvdata(dev);
609 long val = simple_strtol(buf, NULL, 10);
611 mutex_lock(&data->update_lock);
612 data->in_crit[attr->index-11] = IN_TO_REG(val, data->in_vref);
613 pc87360_write_value(data, LD_IN, attr->index, PC87365_REG_TEMP_CRIT,
614 data->in_crit[attr->index-11]);
615 mutex_unlock(&data->update_lock);
616 return count;
619 /* the +11 term below reflects the fact that VLM units 11,12,13 are
620 used in the chip to measure voltage across the thermistors
622 static struct sensor_device_attribute therm_input[] = {
623 SENSOR_ATTR(temp4_input, S_IRUGO, show_therm_input, NULL, 0+11),
624 SENSOR_ATTR(temp5_input, S_IRUGO, show_therm_input, NULL, 1+11),
625 SENSOR_ATTR(temp6_input, S_IRUGO, show_therm_input, NULL, 2+11),
627 static struct sensor_device_attribute therm_status[] = {
628 SENSOR_ATTR(temp4_status, S_IRUGO, show_therm_status, NULL, 0+11),
629 SENSOR_ATTR(temp5_status, S_IRUGO, show_therm_status, NULL, 1+11),
630 SENSOR_ATTR(temp6_status, S_IRUGO, show_therm_status, NULL, 2+11),
632 static struct sensor_device_attribute therm_min[] = {
633 SENSOR_ATTR(temp4_min, S_IRUGO | S_IWUSR,
634 show_therm_min, set_therm_min, 0+11),
635 SENSOR_ATTR(temp5_min, S_IRUGO | S_IWUSR,
636 show_therm_min, set_therm_min, 1+11),
637 SENSOR_ATTR(temp6_min, S_IRUGO | S_IWUSR,
638 show_therm_min, set_therm_min, 2+11),
640 static struct sensor_device_attribute therm_max[] = {
641 SENSOR_ATTR(temp4_max, S_IRUGO | S_IWUSR,
642 show_therm_max, set_therm_max, 0+11),
643 SENSOR_ATTR(temp5_max, S_IRUGO | S_IWUSR,
644 show_therm_max, set_therm_max, 1+11),
645 SENSOR_ATTR(temp6_max, S_IRUGO | S_IWUSR,
646 show_therm_max, set_therm_max, 2+11),
648 static struct sensor_device_attribute therm_crit[] = {
649 SENSOR_ATTR(temp4_crit, S_IRUGO | S_IWUSR,
650 show_therm_crit, set_therm_crit, 0+11),
651 SENSOR_ATTR(temp5_crit, S_IRUGO | S_IWUSR,
652 show_therm_crit, set_therm_crit, 1+11),
653 SENSOR_ATTR(temp6_crit, S_IRUGO | S_IWUSR,
654 show_therm_crit, set_therm_crit, 2+11),
657 #define THERM_UNIT_ATTRS(X) \
658 &therm_input[X].dev_attr.attr, \
659 &therm_status[X].dev_attr.attr, \
660 &therm_min[X].dev_attr.attr, \
661 &therm_max[X].dev_attr.attr, \
662 &therm_crit[X].dev_attr.attr
664 static struct attribute * pc8736x_therm_attr_array[] = {
665 THERM_UNIT_ATTRS(0),
666 THERM_UNIT_ATTRS(1),
667 THERM_UNIT_ATTRS(2),
668 NULL
670 static const struct attribute_group pc8736x_therm_group = {
671 .attrs = pc8736x_therm_attr_array,
674 static ssize_t show_temp_input(struct device *dev, struct device_attribute *devattr, char *buf)
676 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
677 struct pc87360_data *data = pc87360_update_device(dev);
678 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[attr->index]));
680 static ssize_t show_temp_min(struct device *dev, struct device_attribute *devattr, char *buf)
682 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
683 struct pc87360_data *data = pc87360_update_device(dev);
684 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_min[attr->index]));
686 static ssize_t show_temp_max(struct device *dev, struct device_attribute *devattr, char *buf)
688 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
689 struct pc87360_data *data = pc87360_update_device(dev);
690 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max[attr->index]));
692 static ssize_t show_temp_crit(struct device *dev, struct device_attribute *devattr, char *buf)
694 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
695 struct pc87360_data *data = pc87360_update_device(dev);
696 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_crit[attr->index]));
698 static ssize_t show_temp_status(struct device *dev, struct device_attribute *devattr, char *buf)
700 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
701 struct pc87360_data *data = pc87360_update_device(dev);
702 return sprintf(buf, "%d\n", data->temp_status[attr->index]);
704 static ssize_t set_temp_min(struct device *dev, struct device_attribute *devattr, const char *buf,
705 size_t count)
707 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
708 struct pc87360_data *data = dev_get_drvdata(dev);
709 long val = simple_strtol(buf, NULL, 10);
711 mutex_lock(&data->update_lock);
712 data->temp_min[attr->index] = TEMP_TO_REG(val);
713 pc87360_write_value(data, LD_TEMP, attr->index, PC87365_REG_TEMP_MIN,
714 data->temp_min[attr->index]);
715 mutex_unlock(&data->update_lock);
716 return count;
718 static ssize_t set_temp_max(struct device *dev, struct device_attribute *devattr, const char *buf,
719 size_t count)
721 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
722 struct pc87360_data *data = dev_get_drvdata(dev);
723 long val = simple_strtol(buf, NULL, 10);
725 mutex_lock(&data->update_lock);
726 data->temp_max[attr->index] = TEMP_TO_REG(val);
727 pc87360_write_value(data, LD_TEMP, attr->index, PC87365_REG_TEMP_MAX,
728 data->temp_max[attr->index]);
729 mutex_unlock(&data->update_lock);
730 return count;
732 static ssize_t set_temp_crit(struct device *dev, struct device_attribute *devattr, const char *buf,
733 size_t count)
735 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
736 struct pc87360_data *data = dev_get_drvdata(dev);
737 long val = simple_strtol(buf, NULL, 10);
739 mutex_lock(&data->update_lock);
740 data->temp_crit[attr->index] = TEMP_TO_REG(val);
741 pc87360_write_value(data, LD_TEMP, attr->index, PC87365_REG_TEMP_CRIT,
742 data->temp_crit[attr->index]);
743 mutex_unlock(&data->update_lock);
744 return count;
747 static struct sensor_device_attribute temp_input[] = {
748 SENSOR_ATTR(temp1_input, S_IRUGO, show_temp_input, NULL, 0),
749 SENSOR_ATTR(temp2_input, S_IRUGO, show_temp_input, NULL, 1),
750 SENSOR_ATTR(temp3_input, S_IRUGO, show_temp_input, NULL, 2),
752 static struct sensor_device_attribute temp_status[] = {
753 SENSOR_ATTR(temp1_status, S_IRUGO, show_temp_status, NULL, 0),
754 SENSOR_ATTR(temp2_status, S_IRUGO, show_temp_status, NULL, 1),
755 SENSOR_ATTR(temp3_status, S_IRUGO, show_temp_status, NULL, 2),
757 static struct sensor_device_attribute temp_min[] = {
758 SENSOR_ATTR(temp1_min, S_IRUGO | S_IWUSR,
759 show_temp_min, set_temp_min, 0),
760 SENSOR_ATTR(temp2_min, S_IRUGO | S_IWUSR,
761 show_temp_min, set_temp_min, 1),
762 SENSOR_ATTR(temp3_min, S_IRUGO | S_IWUSR,
763 show_temp_min, set_temp_min, 2),
765 static struct sensor_device_attribute temp_max[] = {
766 SENSOR_ATTR(temp1_max, S_IRUGO | S_IWUSR,
767 show_temp_max, set_temp_max, 0),
768 SENSOR_ATTR(temp2_max, S_IRUGO | S_IWUSR,
769 show_temp_max, set_temp_max, 1),
770 SENSOR_ATTR(temp3_max, S_IRUGO | S_IWUSR,
771 show_temp_max, set_temp_max, 2),
773 static struct sensor_device_attribute temp_crit[] = {
774 SENSOR_ATTR(temp1_crit, S_IRUGO | S_IWUSR,
775 show_temp_crit, set_temp_crit, 0),
776 SENSOR_ATTR(temp2_crit, S_IRUGO | S_IWUSR,
777 show_temp_crit, set_temp_crit, 1),
778 SENSOR_ATTR(temp3_crit, S_IRUGO | S_IWUSR,
779 show_temp_crit, set_temp_crit, 2),
782 static ssize_t show_temp_alarms(struct device *dev, struct device_attribute *attr, char *buf)
784 struct pc87360_data *data = pc87360_update_device(dev);
785 return sprintf(buf, "%u\n", data->temp_alarms);
787 static DEVICE_ATTR(alarms_temp, S_IRUGO, show_temp_alarms, NULL);
789 #define TEMP_UNIT_ATTRS(X) \
790 &temp_input[X].dev_attr.attr, \
791 &temp_status[X].dev_attr.attr, \
792 &temp_min[X].dev_attr.attr, \
793 &temp_max[X].dev_attr.attr, \
794 &temp_crit[X].dev_attr.attr
796 static struct attribute * pc8736x_temp_attr_array[] = {
797 TEMP_UNIT_ATTRS(0),
798 TEMP_UNIT_ATTRS(1),
799 TEMP_UNIT_ATTRS(2),
800 /* include the few miscellaneous atts here */
801 &dev_attr_alarms_temp.attr,
802 NULL
804 static const struct attribute_group pc8736x_temp_group = {
805 .attrs = pc8736x_temp_attr_array,
808 static ssize_t show_name(struct device *dev, struct device_attribute
809 *devattr, char *buf)
811 struct pc87360_data *data = dev_get_drvdata(dev);
812 return sprintf(buf, "%s\n", data->name);
814 static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
817 * Device detection, registration and update
820 static int __init pc87360_find(int sioaddr, u8 *devid, unsigned short *addresses)
822 u16 val;
823 int i;
824 int nrdev; /* logical device count */
826 /* No superio_enter */
828 /* Identify device */
829 val = superio_inb(sioaddr, DEVID);
830 switch (val) {
831 case 0xE1: /* PC87360 */
832 case 0xE8: /* PC87363 */
833 case 0xE4: /* PC87364 */
834 nrdev = 1;
835 break;
836 case 0xE5: /* PC87365 */
837 case 0xE9: /* PC87366 */
838 nrdev = 3;
839 break;
840 default:
841 superio_exit(sioaddr);
842 return -ENODEV;
844 /* Remember the device id */
845 *devid = val;
847 for (i = 0; i < nrdev; i++) {
848 /* select logical device */
849 superio_outb(sioaddr, DEV, logdev[i]);
851 val = superio_inb(sioaddr, ACT);
852 if (!(val & 0x01)) {
853 printk(KERN_INFO "pc87360: Device 0x%02x not "
854 "activated\n", logdev[i]);
855 continue;
858 val = (superio_inb(sioaddr, BASE) << 8)
859 | superio_inb(sioaddr, BASE + 1);
860 if (!val) {
861 printk(KERN_INFO "pc87360: Base address not set for "
862 "device 0x%02x\n", logdev[i]);
863 continue;
866 addresses[i] = val;
868 if (i==0) { /* Fans */
869 confreg[0] = superio_inb(sioaddr, 0xF0);
870 confreg[1] = superio_inb(sioaddr, 0xF1);
872 #ifdef DEBUG
873 printk(KERN_DEBUG "pc87360: Fan 1: mon=%d "
874 "ctrl=%d inv=%d\n", (confreg[0]>>2)&1,
875 (confreg[0]>>3)&1, (confreg[0]>>4)&1);
876 printk(KERN_DEBUG "pc87360: Fan 2: mon=%d "
877 "ctrl=%d inv=%d\n", (confreg[0]>>5)&1,
878 (confreg[0]>>6)&1, (confreg[0]>>7)&1);
879 printk(KERN_DEBUG "pc87360: Fan 3: mon=%d "
880 "ctrl=%d inv=%d\n", confreg[1]&1,
881 (confreg[1]>>1)&1, (confreg[1]>>2)&1);
882 #endif
883 } else if (i==1) { /* Voltages */
884 /* Are we using thermistors? */
885 if (*devid == 0xE9) { /* PC87366 */
886 /* These registers are not logical-device
887 specific, just that we won't need them if
888 we don't use the VLM device */
889 confreg[2] = superio_inb(sioaddr, 0x2B);
890 confreg[3] = superio_inb(sioaddr, 0x25);
892 if (confreg[2] & 0x40) {
893 printk(KERN_INFO "pc87360: Using "
894 "thermistors for temperature "
895 "monitoring\n");
897 if (confreg[3] & 0xE0) {
898 printk(KERN_INFO "pc87360: VID "
899 "inputs routed (mode %u)\n",
900 confreg[3] >> 5);
906 superio_exit(sioaddr);
907 return 0;
910 static int __devinit pc87360_probe(struct platform_device *pdev)
912 int i;
913 struct pc87360_data *data;
914 int err = 0;
915 const char *name = "pc87360";
916 int use_thermistors = 0;
917 struct device *dev = &pdev->dev;
919 if (!(data = kzalloc(sizeof(struct pc87360_data), GFP_KERNEL)))
920 return -ENOMEM;
922 data->fannr = 2;
923 data->innr = 0;
924 data->tempnr = 0;
926 switch (devid) {
927 case 0xe8:
928 name = "pc87363";
929 break;
930 case 0xe4:
931 name = "pc87364";
932 data->fannr = 3;
933 break;
934 case 0xe5:
935 name = "pc87365";
936 data->fannr = extra_isa[0] ? 3 : 0;
937 data->innr = extra_isa[1] ? 11 : 0;
938 data->tempnr = extra_isa[2] ? 2 : 0;
939 break;
940 case 0xe9:
941 name = "pc87366";
942 data->fannr = extra_isa[0] ? 3 : 0;
943 data->innr = extra_isa[1] ? 14 : 0;
944 data->tempnr = extra_isa[2] ? 3 : 0;
945 break;
948 data->name = name;
949 data->valid = 0;
950 mutex_init(&data->lock);
951 mutex_init(&data->update_lock);
952 platform_set_drvdata(pdev, data);
954 for (i = 0; i < 3; i++) {
955 if (((data->address[i] = extra_isa[i]))
956 && !request_region(extra_isa[i], PC87360_EXTENT,
957 pc87360_driver.driver.name)) {
958 dev_err(dev, "Region 0x%x-0x%x already "
959 "in use!\n", extra_isa[i],
960 extra_isa[i]+PC87360_EXTENT-1);
961 for (i--; i >= 0; i--)
962 release_region(extra_isa[i], PC87360_EXTENT);
963 err = -EBUSY;
964 goto ERROR1;
968 /* Retrieve the fans configuration from Super-I/O space */
969 if (data->fannr)
970 data->fan_conf = confreg[0] | (confreg[1] << 8);
972 /* Use the correct reference voltage
973 Unless both the VLM and the TMS logical devices agree to
974 use an external Vref, the internal one is used. */
975 if (data->innr) {
976 i = pc87360_read_value(data, LD_IN, NO_BANK,
977 PC87365_REG_IN_CONFIG);
978 if (data->tempnr) {
979 i &= pc87360_read_value(data, LD_TEMP, NO_BANK,
980 PC87365_REG_TEMP_CONFIG);
982 data->in_vref = (i&0x02) ? 3025 : 2966;
983 dev_dbg(dev, "Using %s reference voltage\n",
984 (i&0x02) ? "external" : "internal");
986 data->vid_conf = confreg[3];
987 data->vrm = vid_which_vrm();
990 /* Fan clock dividers may be needed before any data is read */
991 for (i = 0; i < data->fannr; i++) {
992 if (FAN_CONFIG_MONITOR(data->fan_conf, i))
993 data->fan_status[i] = pc87360_read_value(data,
994 LD_FAN, NO_BANK,
995 PC87360_REG_FAN_STATUS(i));
998 if (init > 0) {
999 if (devid == 0xe9 && data->address[1]) /* PC87366 */
1000 use_thermistors = confreg[2] & 0x40;
1002 pc87360_init_device(pdev, use_thermistors);
1005 /* Register all-or-nothing sysfs groups */
1007 if (data->innr &&
1008 (err = sysfs_create_group(&dev->kobj,
1009 &pc8736x_vin_group)))
1010 goto ERROR3;
1012 if (data->innr == 14 &&
1013 (err = sysfs_create_group(&dev->kobj,
1014 &pc8736x_therm_group)))
1015 goto ERROR3;
1017 /* create device attr-files for varying sysfs groups */
1019 if (data->tempnr) {
1020 for (i = 0; i < data->tempnr; i++) {
1021 if ((err = device_create_file(dev,
1022 &temp_input[i].dev_attr))
1023 || (err = device_create_file(dev,
1024 &temp_min[i].dev_attr))
1025 || (err = device_create_file(dev,
1026 &temp_max[i].dev_attr))
1027 || (err = device_create_file(dev,
1028 &temp_crit[i].dev_attr))
1029 || (err = device_create_file(dev,
1030 &temp_status[i].dev_attr)))
1031 goto ERROR3;
1033 if ((err = device_create_file(dev, &dev_attr_alarms_temp)))
1034 goto ERROR3;
1037 for (i = 0; i < data->fannr; i++) {
1038 if (FAN_CONFIG_MONITOR(data->fan_conf, i)
1039 && ((err = device_create_file(dev,
1040 &fan_input[i].dev_attr))
1041 || (err = device_create_file(dev,
1042 &fan_min[i].dev_attr))
1043 || (err = device_create_file(dev,
1044 &fan_div[i].dev_attr))
1045 || (err = device_create_file(dev,
1046 &fan_status[i].dev_attr))))
1047 goto ERROR3;
1049 if (FAN_CONFIG_CONTROL(data->fan_conf, i)
1050 && (err = device_create_file(dev, &pwm[i].dev_attr)))
1051 goto ERROR3;
1054 if ((err = device_create_file(dev, &dev_attr_name)))
1055 goto ERROR3;
1057 data->hwmon_dev = hwmon_device_register(dev);
1058 if (IS_ERR(data->hwmon_dev)) {
1059 err = PTR_ERR(data->hwmon_dev);
1060 goto ERROR3;
1062 return 0;
1064 ERROR3:
1065 device_remove_file(dev, &dev_attr_name);
1066 /* can still remove groups whose members were added individually */
1067 sysfs_remove_group(&dev->kobj, &pc8736x_temp_group);
1068 sysfs_remove_group(&dev->kobj, &pc8736x_fan_group);
1069 sysfs_remove_group(&dev->kobj, &pc8736x_therm_group);
1070 sysfs_remove_group(&dev->kobj, &pc8736x_vin_group);
1071 for (i = 0; i < 3; i++) {
1072 if (data->address[i]) {
1073 release_region(data->address[i], PC87360_EXTENT);
1076 ERROR1:
1077 kfree(data);
1078 return err;
1081 static int __devexit pc87360_remove(struct platform_device *pdev)
1083 struct pc87360_data *data = platform_get_drvdata(pdev);
1084 int i;
1086 hwmon_device_unregister(data->hwmon_dev);
1088 device_remove_file(&pdev->dev, &dev_attr_name);
1089 sysfs_remove_group(&pdev->dev.kobj, &pc8736x_temp_group);
1090 sysfs_remove_group(&pdev->dev.kobj, &pc8736x_fan_group);
1091 sysfs_remove_group(&pdev->dev.kobj, &pc8736x_therm_group);
1092 sysfs_remove_group(&pdev->dev.kobj, &pc8736x_vin_group);
1094 for (i = 0; i < 3; i++) {
1095 if (data->address[i]) {
1096 release_region(data->address[i], PC87360_EXTENT);
1099 kfree(data);
1101 return 0;
1104 /* ldi is the logical device index
1105 bank is for voltages and temperatures only */
1106 static int pc87360_read_value(struct pc87360_data *data, u8 ldi, u8 bank,
1107 u8 reg)
1109 int res;
1111 mutex_lock(&(data->lock));
1112 if (bank != NO_BANK)
1113 outb_p(bank, data->address[ldi] + PC87365_REG_BANK);
1114 res = inb_p(data->address[ldi] + reg);
1115 mutex_unlock(&(data->lock));
1117 return res;
1120 static void pc87360_write_value(struct pc87360_data *data, u8 ldi, u8 bank,
1121 u8 reg, u8 value)
1123 mutex_lock(&(data->lock));
1124 if (bank != NO_BANK)
1125 outb_p(bank, data->address[ldi] + PC87365_REG_BANK);
1126 outb_p(value, data->address[ldi] + reg);
1127 mutex_unlock(&(data->lock));
1130 static void pc87360_init_device(struct platform_device *pdev,
1131 int use_thermistors)
1133 struct pc87360_data *data = platform_get_drvdata(pdev);
1134 int i, nr;
1135 const u8 init_in[14] = { 2, 2, 2, 2, 2, 2, 2, 1, 1, 3, 1, 2, 2, 2 };
1136 const u8 init_temp[3] = { 2, 2, 1 };
1137 u8 reg;
1139 if (init >= 2 && data->innr) {
1140 reg = pc87360_read_value(data, LD_IN, NO_BANK,
1141 PC87365_REG_IN_CONVRATE);
1142 dev_info(&pdev->dev, "VLM conversion set to "
1143 "1s period, 160us delay\n");
1144 pc87360_write_value(data, LD_IN, NO_BANK,
1145 PC87365_REG_IN_CONVRATE,
1146 (reg & 0xC0) | 0x11);
1149 nr = data->innr < 11 ? data->innr : 11;
1150 for (i = 0; i < nr; i++) {
1151 if (init >= init_in[i]) {
1152 /* Forcibly enable voltage channel */
1153 reg = pc87360_read_value(data, LD_IN, i,
1154 PC87365_REG_IN_STATUS);
1155 if (!(reg & 0x01)) {
1156 dev_dbg(&pdev->dev, "Forcibly "
1157 "enabling in%d\n", i);
1158 pc87360_write_value(data, LD_IN, i,
1159 PC87365_REG_IN_STATUS,
1160 (reg & 0x68) | 0x87);
1165 /* We can't blindly trust the Super-I/O space configuration bit,
1166 most BIOS won't set it properly */
1167 for (i = 11; i < data->innr; i++) {
1168 reg = pc87360_read_value(data, LD_IN, i,
1169 PC87365_REG_TEMP_STATUS);
1170 use_thermistors = use_thermistors || (reg & 0x01);
1173 i = use_thermistors ? 2 : 0;
1174 for (; i < data->tempnr; i++) {
1175 if (init >= init_temp[i]) {
1176 /* Forcibly enable temperature channel */
1177 reg = pc87360_read_value(data, LD_TEMP, i,
1178 PC87365_REG_TEMP_STATUS);
1179 if (!(reg & 0x01)) {
1180 dev_dbg(&pdev->dev, "Forcibly "
1181 "enabling temp%d\n", i+1);
1182 pc87360_write_value(data, LD_TEMP, i,
1183 PC87365_REG_TEMP_STATUS,
1184 0xCF);
1189 if (use_thermistors) {
1190 for (i = 11; i < data->innr; i++) {
1191 if (init >= init_in[i]) {
1192 /* The pin may already be used by thermal
1193 diodes */
1194 reg = pc87360_read_value(data, LD_TEMP,
1195 (i-11)/2, PC87365_REG_TEMP_STATUS);
1196 if (reg & 0x01) {
1197 dev_dbg(&pdev->dev, "Skipping "
1198 "temp%d, pin already in use "
1199 "by temp%d\n", i-7, (i-11)/2);
1200 continue;
1203 /* Forcibly enable thermistor channel */
1204 reg = pc87360_read_value(data, LD_IN, i,
1205 PC87365_REG_IN_STATUS);
1206 if (!(reg & 0x01)) {
1207 dev_dbg(&pdev->dev, "Forcibly "
1208 "enabling temp%d\n", i-7);
1209 pc87360_write_value(data, LD_IN, i,
1210 PC87365_REG_TEMP_STATUS,
1211 (reg & 0x60) | 0x8F);
1217 if (data->innr) {
1218 reg = pc87360_read_value(data, LD_IN, NO_BANK,
1219 PC87365_REG_IN_CONFIG);
1220 if (reg & 0x01) {
1221 dev_dbg(&pdev->dev, "Forcibly "
1222 "enabling monitoring (VLM)\n");
1223 pc87360_write_value(data, LD_IN, NO_BANK,
1224 PC87365_REG_IN_CONFIG,
1225 reg & 0xFE);
1229 if (data->tempnr) {
1230 reg = pc87360_read_value(data, LD_TEMP, NO_BANK,
1231 PC87365_REG_TEMP_CONFIG);
1232 if (reg & 0x01) {
1233 dev_dbg(&pdev->dev, "Forcibly enabling "
1234 "monitoring (TMS)\n");
1235 pc87360_write_value(data, LD_TEMP, NO_BANK,
1236 PC87365_REG_TEMP_CONFIG,
1237 reg & 0xFE);
1240 if (init >= 2) {
1241 /* Chip config as documented by National Semi. */
1242 pc87360_write_value(data, LD_TEMP, 0xF, 0xA, 0x08);
1243 /* We voluntarily omit the bank here, in case the
1244 sequence itself matters. It shouldn't be a problem,
1245 since nobody else is supposed to access the
1246 device at that point. */
1247 pc87360_write_value(data, LD_TEMP, NO_BANK, 0xB, 0x04);
1248 pc87360_write_value(data, LD_TEMP, NO_BANK, 0xC, 0x35);
1249 pc87360_write_value(data, LD_TEMP, NO_BANK, 0xD, 0x05);
1250 pc87360_write_value(data, LD_TEMP, NO_BANK, 0xE, 0x05);
1255 static void pc87360_autodiv(struct device *dev, int nr)
1257 struct pc87360_data *data = dev_get_drvdata(dev);
1258 u8 old_min = data->fan_min[nr];
1260 /* Increase clock divider if needed and possible */
1261 if ((data->fan_status[nr] & 0x04) /* overflow flag */
1262 || (data->fan[nr] >= 224)) { /* next to overflow */
1263 if ((data->fan_status[nr] & 0x60) != 0x60) {
1264 data->fan_status[nr] += 0x20;
1265 data->fan_min[nr] >>= 1;
1266 data->fan[nr] >>= 1;
1267 dev_dbg(dev, "Increasing "
1268 "clock divider to %d for fan %d\n",
1269 FAN_DIV_FROM_REG(data->fan_status[nr]), nr+1);
1271 } else {
1272 /* Decrease clock divider if possible */
1273 while (!(data->fan_min[nr] & 0x80) /* min "nails" divider */
1274 && data->fan[nr] < 85 /* bad accuracy */
1275 && (data->fan_status[nr] & 0x60) != 0x00) {
1276 data->fan_status[nr] -= 0x20;
1277 data->fan_min[nr] <<= 1;
1278 data->fan[nr] <<= 1;
1279 dev_dbg(dev, "Decreasing "
1280 "clock divider to %d for fan %d\n",
1281 FAN_DIV_FROM_REG(data->fan_status[nr]),
1282 nr+1);
1286 /* Write new fan min if it changed */
1287 if (old_min != data->fan_min[nr]) {
1288 pc87360_write_value(data, LD_FAN, NO_BANK,
1289 PC87360_REG_FAN_MIN(nr),
1290 data->fan_min[nr]);
1294 static struct pc87360_data *pc87360_update_device(struct device *dev)
1296 struct pc87360_data *data = dev_get_drvdata(dev);
1297 u8 i;
1299 mutex_lock(&data->update_lock);
1301 if (time_after(jiffies, data->last_updated + HZ * 2) || !data->valid) {
1302 dev_dbg(dev, "Data update\n");
1304 /* Fans */
1305 for (i = 0; i < data->fannr; i++) {
1306 if (FAN_CONFIG_MONITOR(data->fan_conf, i)) {
1307 data->fan_status[i] =
1308 pc87360_read_value(data, LD_FAN,
1309 NO_BANK, PC87360_REG_FAN_STATUS(i));
1310 data->fan[i] = pc87360_read_value(data, LD_FAN,
1311 NO_BANK, PC87360_REG_FAN(i));
1312 data->fan_min[i] = pc87360_read_value(data,
1313 LD_FAN, NO_BANK,
1314 PC87360_REG_FAN_MIN(i));
1315 /* Change clock divider if needed */
1316 pc87360_autodiv(dev, i);
1317 /* Clear bits and write new divider */
1318 pc87360_write_value(data, LD_FAN, NO_BANK,
1319 PC87360_REG_FAN_STATUS(i),
1320 data->fan_status[i]);
1322 if (FAN_CONFIG_CONTROL(data->fan_conf, i))
1323 data->pwm[i] = pc87360_read_value(data, LD_FAN,
1324 NO_BANK, PC87360_REG_PWM(i));
1327 /* Voltages */
1328 for (i = 0; i < data->innr; i++) {
1329 data->in_status[i] = pc87360_read_value(data, LD_IN, i,
1330 PC87365_REG_IN_STATUS);
1331 /* Clear bits */
1332 pc87360_write_value(data, LD_IN, i,
1333 PC87365_REG_IN_STATUS,
1334 data->in_status[i]);
1335 if ((data->in_status[i] & 0x81) == 0x81) {
1336 data->in[i] = pc87360_read_value(data, LD_IN,
1337 i, PC87365_REG_IN);
1339 if (data->in_status[i] & 0x01) {
1340 data->in_min[i] = pc87360_read_value(data,
1341 LD_IN, i,
1342 PC87365_REG_IN_MIN);
1343 data->in_max[i] = pc87360_read_value(data,
1344 LD_IN, i,
1345 PC87365_REG_IN_MAX);
1346 if (i >= 11)
1347 data->in_crit[i-11] =
1348 pc87360_read_value(data, LD_IN,
1349 i, PC87365_REG_TEMP_CRIT);
1352 if (data->innr) {
1353 data->in_alarms = pc87360_read_value(data, LD_IN,
1354 NO_BANK, PC87365_REG_IN_ALARMS1)
1355 | ((pc87360_read_value(data, LD_IN,
1356 NO_BANK, PC87365_REG_IN_ALARMS2)
1357 & 0x07) << 8);
1358 data->vid = (data->vid_conf & 0xE0) ?
1359 pc87360_read_value(data, LD_IN,
1360 NO_BANK, PC87365_REG_VID) : 0x1F;
1363 /* Temperatures */
1364 for (i = 0; i < data->tempnr; i++) {
1365 data->temp_status[i] = pc87360_read_value(data,
1366 LD_TEMP, i,
1367 PC87365_REG_TEMP_STATUS);
1368 /* Clear bits */
1369 pc87360_write_value(data, LD_TEMP, i,
1370 PC87365_REG_TEMP_STATUS,
1371 data->temp_status[i]);
1372 if ((data->temp_status[i] & 0x81) == 0x81) {
1373 data->temp[i] = pc87360_read_value(data,
1374 LD_TEMP, i,
1375 PC87365_REG_TEMP);
1377 if (data->temp_status[i] & 0x01) {
1378 data->temp_min[i] = pc87360_read_value(data,
1379 LD_TEMP, i,
1380 PC87365_REG_TEMP_MIN);
1381 data->temp_max[i] = pc87360_read_value(data,
1382 LD_TEMP, i,
1383 PC87365_REG_TEMP_MAX);
1384 data->temp_crit[i] = pc87360_read_value(data,
1385 LD_TEMP, i,
1386 PC87365_REG_TEMP_CRIT);
1389 if (data->tempnr) {
1390 data->temp_alarms = pc87360_read_value(data, LD_TEMP,
1391 NO_BANK, PC87365_REG_TEMP_ALARMS)
1392 & 0x3F;
1395 data->last_updated = jiffies;
1396 data->valid = 1;
1399 mutex_unlock(&data->update_lock);
1401 return data;
1404 static int __init pc87360_device_add(unsigned short address)
1406 struct resource res = {
1407 .name = "pc87360",
1408 .flags = IORESOURCE_IO,
1410 int err, i;
1412 pdev = platform_device_alloc("pc87360", address);
1413 if (!pdev) {
1414 err = -ENOMEM;
1415 printk(KERN_ERR "pc87360: Device allocation failed\n");
1416 goto exit;
1419 for (i = 0; i < 3; i++) {
1420 if (!extra_isa[i])
1421 continue;
1422 res.start = extra_isa[i];
1423 res.end = extra_isa[i] + PC87360_EXTENT - 1;
1424 err = platform_device_add_resources(pdev, &res, 1);
1425 if (err) {
1426 printk(KERN_ERR "pc87360: Device resource[%d] "
1427 "addition failed (%d)\n", i, err);
1428 goto exit_device_put;
1432 err = platform_device_add(pdev);
1433 if (err) {
1434 printk(KERN_ERR "pc87360: Device addition failed (%d)\n",
1435 err);
1436 goto exit_device_put;
1439 return 0;
1441 exit_device_put:
1442 platform_device_put(pdev);
1443 exit:
1444 return err;
1447 static int __init pc87360_init(void)
1449 int err, i;
1450 unsigned short address = 0;
1452 if (pc87360_find(0x2e, &devid, extra_isa)
1453 && pc87360_find(0x4e, &devid, extra_isa)) {
1454 printk(KERN_WARNING "pc87360: PC8736x not detected, "
1455 "module not inserted.\n");
1456 return -ENODEV;
1459 /* Arbitrarily pick one of the addresses */
1460 for (i = 0; i < 3; i++) {
1461 if (extra_isa[i] != 0x0000) {
1462 address = extra_isa[i];
1463 break;
1467 if (address == 0x0000) {
1468 printk(KERN_WARNING "pc87360: No active logical device, "
1469 "module not inserted.\n");
1470 return -ENODEV;
1473 err = platform_driver_register(&pc87360_driver);
1474 if (err)
1475 goto exit;
1477 /* Sets global pdev as a side effect */
1478 err = pc87360_device_add(address);
1479 if (err)
1480 goto exit_driver;
1482 return 0;
1484 exit_driver:
1485 platform_driver_unregister(&pc87360_driver);
1486 exit:
1487 return err;
1490 static void __exit pc87360_exit(void)
1492 platform_device_unregister(pdev);
1493 platform_driver_unregister(&pc87360_driver);
1497 MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
1498 MODULE_DESCRIPTION("PC8736x hardware monitor");
1499 MODULE_LICENSE("GPL");
1501 module_init(pc87360_init);
1502 module_exit(pc87360_exit);