2 * pc87360.c - Part of lm_sensors, Linux kernel modules
3 * for hardware monitoring
4 * Copyright (C) 2004 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/config.h>
37 #include <linux/module.h>
38 #include <linux/init.h>
39 #include <linux/slab.h>
40 #include <linux/i2c.h>
41 #include <linux/i2c-sensor.h>
42 #include <linux/i2c-vid.h>
45 static unsigned short normal_i2c[] = { I2C_CLIENT_END };
46 static unsigned int normal_isa[] = { 0, I2C_CLIENT_ISA_END };
47 static struct i2c_force_data forces[] = {{ NULL }};
49 static unsigned int extra_isa[3];
52 enum chips { any_chip, pc87360, pc87363, pc87364, pc87365, pc87366 };
53 static struct i2c_address_data addr_data = {
54 .normal_i2c = normal_i2c,
55 .normal_isa = normal_isa,
60 module_param(init, int, 0);
61 MODULE_PARM_DESC(init,
62 "Chip initialization level:\n"
64 "*1: Forcibly enable internal voltage and temperature channels, except in9\n"
65 " 2: Forcibly enable all voltage and temperature channels, except in9\n"
66 " 3: Forcibly enable all voltage and temperature channels, including in9");
69 * Super-I/O registers and operations
72 #define DEV 0x07 /* Register: Logical device select */
73 #define DEVID 0x20 /* Register: Device ID */
74 #define ACT 0x30 /* Register: Device activation */
75 #define BASE 0x60 /* Register: Base address */
77 #define FSCM 0x09 /* Logical device: fans */
78 #define VLM 0x0d /* Logical device: voltages */
79 #define TMS 0x0e /* Logical device: temperatures */
80 static const u8 logdev[3] = { FSCM, VLM, TMS };
86 static inline void superio_outb(int sioaddr, int reg, int val)
92 static inline int superio_inb(int sioaddr, int reg)
95 return inb(sioaddr+1);
98 static inline void superio_exit(int sioaddr)
101 outb(0x02, sioaddr+1);
108 #define PC87360_EXTENT 0x10
109 #define PC87365_REG_BANK 0x09
113 * Fan registers and conversions
116 /* nr has to be 0 or 1 (PC87360/87363) or 2 (PC87364/87365/87366) */
117 #define PC87360_REG_PRESCALE(nr) (0x00 + 2 * (nr))
118 #define PC87360_REG_PWM(nr) (0x01 + 2 * (nr))
119 #define PC87360_REG_FAN_MIN(nr) (0x06 + 3 * (nr))
120 #define PC87360_REG_FAN(nr) (0x07 + 3 * (nr))
121 #define PC87360_REG_FAN_STATUS(nr) (0x08 + 3 * (nr))
123 #define FAN_FROM_REG(val,div) ((val) == 0 ? 0: \
124 480000 / ((val)*(div)))
125 #define FAN_TO_REG(val,div) ((val) <= 100 ? 0 : \
126 480000 / ((val)*(div)))
127 #define FAN_DIV_FROM_REG(val) (1 << ((val >> 5) & 0x03))
128 #define FAN_STATUS_FROM_REG(val) ((val) & 0x07)
130 #define FAN_CONFIG_MONITOR(val,nr) (((val) >> (2 + nr * 3)) & 1)
131 #define FAN_CONFIG_CONTROL(val,nr) (((val) >> (3 + nr * 3)) & 1)
132 #define FAN_CONFIG_INVERT(val,nr) (((val) >> (4 + nr * 3)) & 1)
134 #define PWM_FROM_REG(val,inv) ((inv) ? 255 - (val) : (val))
135 static inline u8 PWM_TO_REG(int val, int inv)
147 * Voltage registers and conversions
150 #define PC87365_REG_IN_CONVRATE 0x07
151 #define PC87365_REG_IN_CONFIG 0x08
152 #define PC87365_REG_IN 0x0B
153 #define PC87365_REG_IN_MIN 0x0D
154 #define PC87365_REG_IN_MAX 0x0C
155 #define PC87365_REG_IN_STATUS 0x0A
156 #define PC87365_REG_IN_ALARMS1 0x00
157 #define PC87365_REG_IN_ALARMS2 0x01
158 #define PC87365_REG_VID 0x06
160 #define IN_FROM_REG(val,ref) (((val) * (ref) + 128) / 256)
161 #define IN_TO_REG(val,ref) ((val) < 0 ? 0 : \
162 (val)*256 >= (ref)*255 ? 255: \
163 ((val) * 256 + (ref)/2) / (ref))
166 * Temperature registers and conversions
169 #define PC87365_REG_TEMP_CONFIG 0x08
170 #define PC87365_REG_TEMP 0x0B
171 #define PC87365_REG_TEMP_MIN 0x0D
172 #define PC87365_REG_TEMP_MAX 0x0C
173 #define PC87365_REG_TEMP_CRIT 0x0E
174 #define PC87365_REG_TEMP_STATUS 0x0A
175 #define PC87365_REG_TEMP_ALARMS 0x00
177 #define TEMP_FROM_REG(val) ((val) * 1000)
178 #define TEMP_TO_REG(val) ((val) < -55000 ? -55 : \
179 (val) > 127000 ? 127 : \
180 (val) < 0 ? ((val) - 500) / 1000 : \
181 ((val) + 500) / 1000)
184 * Client data (each client gets its own)
187 struct pc87360_data {
188 struct i2c_client client;
189 struct semaphore lock;
190 struct semaphore update_lock;
191 char valid; /* !=0 if following fields are valid */
192 unsigned long last_updated; /* In jiffies */
196 u8 fannr, innr, tempnr;
198 u8 fan[3]; /* Register value */
199 u8 fan_min[3]; /* Register value */
200 u8 fan_status[3]; /* Register value */
201 u8 pwm[3]; /* Register value */
202 u16 fan_conf; /* Configuration register values, combined */
204 u16 in_vref; /* 1 mV/bit */
205 u8 in[14]; /* Register value */
206 u8 in_min[14]; /* Register value */
207 u8 in_max[14]; /* Register value */
208 u8 in_crit[3]; /* Register value */
209 u8 in_status[14]; /* Register value */
210 u16 in_alarms; /* Register values, combined, masked */
211 u8 vid_conf; /* Configuration register value */
213 u8 vid; /* Register value */
215 s8 temp[3]; /* Register value */
216 s8 temp_min[3]; /* Register value */
217 s8 temp_max[3]; /* Register value */
218 s8 temp_crit[3]; /* Register value */
219 u8 temp_status[3]; /* Register value */
220 u8 temp_alarms; /* Register value, masked */
224 * Functions declaration
227 static int pc87360_attach_adapter(struct i2c_adapter *adapter);
228 static int pc87360_detect(struct i2c_adapter *adapter, int address, int kind);
229 static int pc87360_detach_client(struct i2c_client *client);
231 static int pc87360_read_value(struct pc87360_data *data, u8 ldi, u8 bank,
233 static void pc87360_write_value(struct pc87360_data *data, u8 ldi, u8 bank,
235 static void pc87360_init_client(struct i2c_client *client, int use_thermistors);
236 static struct pc87360_data *pc87360_update_device(struct device *dev);
239 * Driver data (common to all clients)
242 static struct i2c_driver pc87360_driver = {
243 .owner = THIS_MODULE,
245 .flags = I2C_DF_NOTIFY,
246 .attach_adapter = pc87360_attach_adapter,
247 .detach_client = pc87360_detach_client,
254 static ssize_t set_fan_min(struct device *dev, const char *buf,
255 size_t count, int nr)
257 struct i2c_client *client = to_i2c_client(dev);
258 struct pc87360_data *data = i2c_get_clientdata(client);
259 long fan_min = simple_strtol(buf, NULL, 10);
261 fan_min = FAN_TO_REG(fan_min, FAN_DIV_FROM_REG(data->fan_status[nr]));
263 /* If it wouldn't fit, change clock divisor */
265 && (data->fan_status[nr] & 0x60) != 0x60) {
268 data->fan_status[nr] += 0x20;
270 data->fan_min[nr] = fan_min > 255 ? 255 : fan_min;
271 pc87360_write_value(data, LD_FAN, NO_BANK, PC87360_REG_FAN_MIN(nr),
274 /* Write new divider, preserve alarm bits */
275 pc87360_write_value(data, LD_FAN, NO_BANK, PC87360_REG_FAN_STATUS(nr),
276 data->fan_status[nr] & 0xF9);
281 #define show_and_set_fan(offset) \
282 static ssize_t show_fan##offset##_input(struct device *dev, char *buf) \
284 struct pc87360_data *data = pc87360_update_device(dev); \
285 return sprintf(buf, "%u\n", FAN_FROM_REG(data->fan[offset-1], \
286 FAN_DIV_FROM_REG(data->fan_status[offset-1]))); \
288 static ssize_t show_fan##offset##_min(struct device *dev, char *buf) \
290 struct pc87360_data *data = pc87360_update_device(dev); \
291 return sprintf(buf, "%u\n", FAN_FROM_REG(data->fan_min[offset-1], \
292 FAN_DIV_FROM_REG(data->fan_status[offset-1]))); \
294 static ssize_t show_fan##offset##_div(struct device *dev, char *buf) \
296 struct pc87360_data *data = pc87360_update_device(dev); \
297 return sprintf(buf, "%u\n", \
298 FAN_DIV_FROM_REG(data->fan_status[offset-1])); \
300 static ssize_t show_fan##offset##_status(struct device *dev, char *buf) \
302 struct pc87360_data *data = pc87360_update_device(dev); \
303 return sprintf(buf, "%u\n", \
304 FAN_STATUS_FROM_REG(data->fan_status[offset-1])); \
306 static ssize_t set_fan##offset##_min(struct device *dev, const char *buf, \
309 return set_fan_min(dev, buf, count, offset-1); \
311 static DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
312 show_fan##offset##_input, NULL); \
313 static DEVICE_ATTR(fan##offset##_min, S_IWUSR | S_IRUGO, \
314 show_fan##offset##_min, set_fan##offset##_min); \
315 static DEVICE_ATTR(fan##offset##_div, S_IRUGO, \
316 show_fan##offset##_div, NULL); \
317 static DEVICE_ATTR(fan##offset##_status, S_IRUGO, \
318 show_fan##offset##_status, NULL);
323 #define show_and_set_pwm(offset) \
324 static ssize_t show_pwm##offset(struct device *dev, char *buf) \
326 struct pc87360_data *data = pc87360_update_device(dev); \
327 return sprintf(buf, "%u\n", \
328 PWM_FROM_REG(data->pwm[offset-1], \
329 FAN_CONFIG_INVERT(data->fan_conf, \
332 static ssize_t set_pwm##offset(struct device *dev, const char *buf, \
335 struct i2c_client *client = to_i2c_client(dev); \
336 struct pc87360_data *data = i2c_get_clientdata(client); \
337 long val = simple_strtol(buf, NULL, 10); \
338 data->pwm[offset-1] = PWM_TO_REG(val, \
339 FAN_CONFIG_INVERT(data->fan_conf, offset-1)); \
340 pc87360_write_value(data, LD_FAN, NO_BANK, PC87360_REG_PWM(offset-1), \
341 data->pwm[offset-1]); \
344 static DEVICE_ATTR(pwm##offset, S_IWUSR | S_IRUGO, \
345 show_pwm##offset, set_pwm##offset);
350 #define show_and_set_in(offset) \
351 static ssize_t show_in##offset##_input(struct device *dev, char *buf) \
353 struct pc87360_data *data = pc87360_update_device(dev); \
354 return sprintf(buf, "%u\n", IN_FROM_REG(data->in[offset], \
357 static ssize_t show_in##offset##_min(struct device *dev, char *buf) \
359 struct pc87360_data *data = pc87360_update_device(dev); \
360 return sprintf(buf, "%u\n", IN_FROM_REG(data->in_min[offset], \
363 static ssize_t show_in##offset##_max(struct device *dev, char *buf) \
365 struct pc87360_data *data = pc87360_update_device(dev); \
366 return sprintf(buf, "%u\n", IN_FROM_REG(data->in_max[offset], \
369 static ssize_t show_in##offset##_status(struct device *dev, char *buf) \
371 struct pc87360_data *data = pc87360_update_device(dev); \
372 return sprintf(buf, "%u\n", data->in_status[offset]); \
374 static ssize_t set_in##offset##_min(struct device *dev, const char *buf, \
377 struct i2c_client *client = to_i2c_client(dev); \
378 struct pc87360_data *data = i2c_get_clientdata(client); \
379 long val = simple_strtol(buf, NULL, 10); \
380 data->in_min[offset] = IN_TO_REG(val, data->in_vref); \
381 pc87360_write_value(data, LD_IN, offset, PC87365_REG_IN_MIN, \
382 data->in_min[offset]); \
385 static ssize_t set_in##offset##_max(struct device *dev, const char *buf, \
388 struct i2c_client *client = to_i2c_client(dev); \
389 struct pc87360_data *data = i2c_get_clientdata(client); \
390 long val = simple_strtol(buf, NULL, 10); \
391 data->in_max[offset] = IN_TO_REG(val, \
393 pc87360_write_value(data, LD_IN, offset, PC87365_REG_IN_MAX, \
394 data->in_max[offset]); \
397 static DEVICE_ATTR(in##offset##_input, S_IRUGO, \
398 show_in##offset##_input, NULL); \
399 static DEVICE_ATTR(in##offset##_min, S_IWUSR | S_IRUGO, \
400 show_in##offset##_min, set_in##offset##_min); \
401 static DEVICE_ATTR(in##offset##_max, S_IWUSR | S_IRUGO, \
402 show_in##offset##_max, set_in##offset##_max); \
403 static DEVICE_ATTR(in##offset##_status, S_IRUGO, \
404 show_in##offset##_status, NULL);
417 #define show_and_set_therm(offset) \
418 static ssize_t show_temp##offset##_input(struct device *dev, char *buf) \
420 struct pc87360_data *data = pc87360_update_device(dev); \
421 return sprintf(buf, "%u\n", IN_FROM_REG(data->in[offset+7], \
424 static ssize_t show_temp##offset##_min(struct device *dev, char *buf) \
426 struct pc87360_data *data = pc87360_update_device(dev); \
427 return sprintf(buf, "%u\n", IN_FROM_REG(data->in_min[offset+7], \
430 static ssize_t show_temp##offset##_max(struct device *dev, char *buf) \
432 struct pc87360_data *data = pc87360_update_device(dev); \
433 return sprintf(buf, "%u\n", IN_FROM_REG(data->in_max[offset+7], \
436 static ssize_t show_temp##offset##_crit(struct device *dev, char *buf) \
438 struct pc87360_data *data = pc87360_update_device(dev); \
439 return sprintf(buf, "%u\n", IN_FROM_REG(data->in_crit[offset-4], \
442 static ssize_t show_temp##offset##_status(struct device *dev, char *buf) \
444 struct pc87360_data *data = pc87360_update_device(dev); \
445 return sprintf(buf, "%u\n", data->in_status[offset+7]); \
447 static ssize_t set_temp##offset##_min(struct device *dev, const char *buf, \
450 struct i2c_client *client = to_i2c_client(dev); \
451 struct pc87360_data *data = i2c_get_clientdata(client); \
452 long val = simple_strtol(buf, NULL, 10); \
453 data->in_min[offset+7] = IN_TO_REG(val, data->in_vref); \
454 pc87360_write_value(data, LD_IN, offset+7, PC87365_REG_TEMP_MIN, \
455 data->in_min[offset+7]); \
458 static ssize_t set_temp##offset##_max(struct device *dev, const char *buf, \
461 struct i2c_client *client = to_i2c_client(dev); \
462 struct pc87360_data *data = i2c_get_clientdata(client); \
463 long val = simple_strtol(buf, NULL, 10); \
464 data->in_max[offset+7] = IN_TO_REG(val, data->in_vref); \
465 pc87360_write_value(data, LD_IN, offset+7, PC87365_REG_TEMP_MAX, \
466 data->in_max[offset+7]); \
469 static ssize_t set_temp##offset##_crit(struct device *dev, const char *buf, \
472 struct i2c_client *client = to_i2c_client(dev); \
473 struct pc87360_data *data = i2c_get_clientdata(client); \
474 long val = simple_strtol(buf, NULL, 10); \
475 data->in_crit[offset-4] = IN_TO_REG(val, data->in_vref); \
476 pc87360_write_value(data, LD_IN, offset+7, PC87365_REG_TEMP_CRIT, \
477 data->in_crit[offset-4]); \
480 static DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
481 show_temp##offset##_input, NULL); \
482 static DEVICE_ATTR(temp##offset##_min, S_IWUSR | S_IRUGO, \
483 show_temp##offset##_min, set_temp##offset##_min); \
484 static DEVICE_ATTR(temp##offset##_max, S_IWUSR | S_IRUGO, \
485 show_temp##offset##_max, set_temp##offset##_max); \
486 static DEVICE_ATTR(temp##offset##_crit, S_IWUSR | S_IRUGO, \
487 show_temp##offset##_crit, set_temp##offset##_crit); \
488 static DEVICE_ATTR(temp##offset##_status, S_IRUGO, \
489 show_temp##offset##_status, NULL);
490 show_and_set_therm(4)
491 show_and_set_therm(5)
492 show_and_set_therm(6)
494 static ssize_t show_vid(struct device *dev, 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, char *buf)
503 struct pc87360_data *data = pc87360_update_device(dev);
504 return sprintf(buf, "%u\n", data->vrm);
506 static ssize_t set_vrm(struct device *dev, const char *buf, size_t count)
508 struct i2c_client *client = to_i2c_client(dev);
509 struct pc87360_data *data = i2c_get_clientdata(client);
510 data->vrm = simple_strtoul(buf, NULL, 10);
513 static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm, set_vrm);
515 static ssize_t show_in_alarms(struct device *dev, char *buf)
517 struct pc87360_data *data = pc87360_update_device(dev);
518 return sprintf(buf, "%u\n", data->in_alarms);
520 static DEVICE_ATTR(alarms_in, S_IRUGO, show_in_alarms, NULL);
522 #define show_and_set_temp(offset) \
523 static ssize_t show_temp##offset##_input(struct device *dev, char *buf) \
525 struct pc87360_data *data = pc87360_update_device(dev); \
526 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[offset-1])); \
528 static ssize_t show_temp##offset##_min(struct device *dev, char *buf) \
530 struct pc87360_data *data = pc87360_update_device(dev); \
531 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_min[offset-1])); \
533 static ssize_t show_temp##offset##_max(struct device *dev, char *buf) \
535 struct pc87360_data *data = pc87360_update_device(dev); \
536 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max[offset-1])); \
538 static ssize_t show_temp##offset##_crit(struct device *dev, char *buf) \
540 struct pc87360_data *data = pc87360_update_device(dev); \
541 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_crit[offset-1])); \
543 static ssize_t show_temp##offset##_status(struct device *dev, char *buf) \
545 struct pc87360_data *data = pc87360_update_device(dev); \
546 return sprintf(buf, "%d\n", data->temp_status[offset-1]); \
548 static ssize_t set_temp##offset##_min(struct device *dev, const char *buf, \
551 struct i2c_client *client = to_i2c_client(dev); \
552 struct pc87360_data *data = i2c_get_clientdata(client); \
553 long val = simple_strtol(buf, NULL, 10); \
554 data->temp_min[offset-1] = TEMP_TO_REG(val); \
555 pc87360_write_value(data, LD_TEMP, offset-1, PC87365_REG_TEMP_MIN, \
556 data->temp_min[offset-1]); \
559 static ssize_t set_temp##offset##_max(struct device *dev, const char *buf, \
562 struct i2c_client *client = to_i2c_client(dev); \
563 struct pc87360_data *data = i2c_get_clientdata(client); \
564 long val = simple_strtol(buf, NULL, 10); \
565 data->temp_max[offset-1] = TEMP_TO_REG(val); \
566 pc87360_write_value(data, LD_TEMP, offset-1, PC87365_REG_TEMP_MAX, \
567 data->temp_max[offset-1]); \
570 static ssize_t set_temp##offset##_crit(struct device *dev, const char *buf, \
573 struct i2c_client *client = to_i2c_client(dev); \
574 struct pc87360_data *data = i2c_get_clientdata(client); \
575 long val = simple_strtol(buf, NULL, 10); \
576 data->temp_crit[offset-1] = TEMP_TO_REG(val); \
577 pc87360_write_value(data, LD_TEMP, offset-1, PC87365_REG_TEMP_CRIT, \
578 data->temp_crit[offset-1]); \
581 static DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
582 show_temp##offset##_input, NULL); \
583 static DEVICE_ATTR(temp##offset##_min, S_IWUSR | S_IRUGO, \
584 show_temp##offset##_min, set_temp##offset##_min); \
585 static DEVICE_ATTR(temp##offset##_max, S_IWUSR | S_IRUGO, \
586 show_temp##offset##_max, set_temp##offset##_max); \
587 static DEVICE_ATTR(temp##offset##_crit, S_IWUSR | S_IRUGO, \
588 show_temp##offset##_crit, set_temp##offset##_crit); \
589 static DEVICE_ATTR(temp##offset##_status, S_IRUGO, \
590 show_temp##offset##_status, NULL);
595 static ssize_t show_temp_alarms(struct device *dev, char *buf)
597 struct pc87360_data *data = pc87360_update_device(dev);
598 return sprintf(buf, "%u\n", data->temp_alarms);
600 static DEVICE_ATTR(alarms_temp, S_IRUGO, show_temp_alarms, NULL);
603 * Device detection, registration and update
606 static int pc87360_attach_adapter(struct i2c_adapter *adapter)
608 return i2c_detect(adapter, &addr_data, pc87360_detect);
611 static int pc87360_find(int sioaddr, u8 *devid, int *address)
615 int nrdev; /* logical device count */
617 /* No superio_enter */
619 /* Identify device */
620 val = superio_inb(sioaddr, DEVID);
622 case 0xE1: /* PC87360 */
623 case 0xE8: /* PC87363 */
624 case 0xE4: /* PC87364 */
627 case 0xE5: /* PC87365 */
628 case 0xE9: /* PC87366 */
632 superio_exit(sioaddr);
635 /* Remember the device id */
638 for (i = 0; i < nrdev; i++) {
639 /* select logical device */
640 superio_outb(sioaddr, DEV, logdev[i]);
642 val = superio_inb(sioaddr, ACT);
644 printk(KERN_INFO "pc87360: Device 0x%02x not "
645 "activated\n", logdev[i]);
649 val = (superio_inb(sioaddr, BASE) << 8)
650 | superio_inb(sioaddr, BASE + 1);
652 printk(KERN_INFO "pc87360: Base address not set for "
653 "device 0x%02x\n", logdev[i]);
659 if (i==0) { /* Fans */
660 confreg[0] = superio_inb(sioaddr, 0xF0);
661 confreg[1] = superio_inb(sioaddr, 0xF1);
664 printk(KERN_DEBUG "pc87360: Fan 1: mon=%d "
665 "ctrl=%d inv=%d\n", (confreg[0]>>2)&1,
666 (confreg[0]>>3)&1, (confreg[0]>>4)&1);
667 printk(KERN_DEBUG "pc87360: Fan 2: mon=%d "
668 "ctrl=%d inv=%d\n", (confreg[0]>>5)&1,
669 (confreg[0]>>6)&1, (confreg[0]>>7)&1);
670 printk(KERN_DEBUG "pc87360: Fan 3: mon=%d "
671 "ctrl=%d inv=%d\n", confreg[1]&1,
672 (confreg[1]>>1)&1, (confreg[1]>>2)&1);
674 } else if (i==1) { /* Voltages */
675 /* Are we using thermistors? */
676 if (*devid == 0xE9) { /* PC87366 */
677 /* These registers are not logical-device
678 specific, just that we won't need them if
679 we don't use the VLM device */
680 confreg[2] = superio_inb(sioaddr, 0x2B);
681 confreg[3] = superio_inb(sioaddr, 0x25);
683 if (confreg[2] & 0x40) {
684 printk(KERN_INFO "pc87360: Using "
685 "thermistors for temperature "
688 if (confreg[3] & 0xE0) {
689 printk(KERN_INFO "pc87360: VID "
690 "inputs routed (mode %u)\n",
697 superio_exit(sioaddr);
701 /* We don't really care about the address.
702 Read from extra_isa instead. */
703 int pc87360_detect(struct i2c_adapter *adapter, int address, int kind)
706 struct i2c_client *new_client;
707 struct pc87360_data *data;
709 const char *name = "pc87360";
710 int use_thermistors = 0;
712 if (!i2c_is_isa_adapter(adapter))
715 if (!(data = kmalloc(sizeof(struct pc87360_data), GFP_KERNEL)))
717 memset(data, 0x00, sizeof(struct pc87360_data));
719 new_client = &data->client;
720 i2c_set_clientdata(new_client, data);
721 new_client->addr = address;
722 init_MUTEX(&data->lock);
723 new_client->adapter = adapter;
724 new_client->driver = &pc87360_driver;
725 new_client->flags = 0;
741 data->fannr = extra_isa[0] ? 3 : 0;
742 data->innr = extra_isa[1] ? 11 : 0;
743 data->tempnr = extra_isa[2] ? 2 : 0;
747 data->fannr = extra_isa[0] ? 3 : 0;
748 data->innr = extra_isa[1] ? 14 : 0;
749 data->tempnr = extra_isa[2] ? 3 : 0;
753 strcpy(new_client->name, name);
755 init_MUTEX(&data->update_lock);
757 for (i = 0; i < 3; i++) {
758 if (((data->address[i] = extra_isa[i]))
759 && !request_region(extra_isa[i], PC87360_EXTENT,
760 pc87360_driver.name)) {
761 dev_err(&new_client->dev, "Region 0x%x-0x%x already "
762 "in use!\n", extra_isa[i],
763 extra_isa[i]+PC87360_EXTENT-1);
764 for (i--; i >= 0; i--)
765 release_region(extra_isa[i], PC87360_EXTENT);
771 /* Retrieve the fans configuration from Super-I/O space */
773 data->fan_conf = confreg[0] | (confreg[1] << 8);
775 if ((err = i2c_attach_client(new_client)))
778 /* Use the correct reference voltage
779 Unless both the VLM and the TMS logical devices agree to
780 use an external Vref, the internal one is used. */
782 i = pc87360_read_value(data, LD_IN, NO_BANK,
783 PC87365_REG_IN_CONFIG);
785 i &= pc87360_read_value(data, LD_TEMP, NO_BANK,
786 PC87365_REG_TEMP_CONFIG);
788 data->in_vref = (i&0x02) ? 3025 : 2966;
789 dev_dbg(&new_client->dev, "Using %s reference voltage\n",
790 (i&0x02) ? "external" : "internal");
792 data->vid_conf = confreg[3];
796 /* Fan clock dividers may be needed before any data is read */
797 for (i = 0; i < data->fannr; i++) {
798 if (FAN_CONFIG_MONITOR(data->fan_conf, i))
799 data->fan_status[i] = pc87360_read_value(data,
801 PC87360_REG_FAN_STATUS(i));
805 if (devid == 0xe9 && data->address[1]) /* PC87366 */
806 use_thermistors = confreg[2] & 0x40;
808 pc87360_init_client(new_client, use_thermistors);
811 /* Register sysfs hooks */
813 device_create_file(&new_client->dev, &dev_attr_in0_input);
814 device_create_file(&new_client->dev, &dev_attr_in1_input);
815 device_create_file(&new_client->dev, &dev_attr_in2_input);
816 device_create_file(&new_client->dev, &dev_attr_in3_input);
817 device_create_file(&new_client->dev, &dev_attr_in4_input);
818 device_create_file(&new_client->dev, &dev_attr_in5_input);
819 device_create_file(&new_client->dev, &dev_attr_in6_input);
820 device_create_file(&new_client->dev, &dev_attr_in7_input);
821 device_create_file(&new_client->dev, &dev_attr_in8_input);
822 device_create_file(&new_client->dev, &dev_attr_in9_input);
823 device_create_file(&new_client->dev, &dev_attr_in10_input);
824 device_create_file(&new_client->dev, &dev_attr_in0_min);
825 device_create_file(&new_client->dev, &dev_attr_in1_min);
826 device_create_file(&new_client->dev, &dev_attr_in2_min);
827 device_create_file(&new_client->dev, &dev_attr_in3_min);
828 device_create_file(&new_client->dev, &dev_attr_in4_min);
829 device_create_file(&new_client->dev, &dev_attr_in5_min);
830 device_create_file(&new_client->dev, &dev_attr_in6_min);
831 device_create_file(&new_client->dev, &dev_attr_in7_min);
832 device_create_file(&new_client->dev, &dev_attr_in8_min);
833 device_create_file(&new_client->dev, &dev_attr_in9_min);
834 device_create_file(&new_client->dev, &dev_attr_in10_min);
835 device_create_file(&new_client->dev, &dev_attr_in0_max);
836 device_create_file(&new_client->dev, &dev_attr_in1_max);
837 device_create_file(&new_client->dev, &dev_attr_in2_max);
838 device_create_file(&new_client->dev, &dev_attr_in3_max);
839 device_create_file(&new_client->dev, &dev_attr_in4_max);
840 device_create_file(&new_client->dev, &dev_attr_in5_max);
841 device_create_file(&new_client->dev, &dev_attr_in6_max);
842 device_create_file(&new_client->dev, &dev_attr_in7_max);
843 device_create_file(&new_client->dev, &dev_attr_in8_max);
844 device_create_file(&new_client->dev, &dev_attr_in9_max);
845 device_create_file(&new_client->dev, &dev_attr_in10_max);
846 device_create_file(&new_client->dev, &dev_attr_in0_status);
847 device_create_file(&new_client->dev, &dev_attr_in1_status);
848 device_create_file(&new_client->dev, &dev_attr_in2_status);
849 device_create_file(&new_client->dev, &dev_attr_in3_status);
850 device_create_file(&new_client->dev, &dev_attr_in4_status);
851 device_create_file(&new_client->dev, &dev_attr_in5_status);
852 device_create_file(&new_client->dev, &dev_attr_in6_status);
853 device_create_file(&new_client->dev, &dev_attr_in7_status);
854 device_create_file(&new_client->dev, &dev_attr_in8_status);
855 device_create_file(&new_client->dev, &dev_attr_in9_status);
856 device_create_file(&new_client->dev, &dev_attr_in10_status);
858 device_create_file(&new_client->dev, &dev_attr_cpu0_vid);
859 device_create_file(&new_client->dev, &dev_attr_vrm);
860 device_create_file(&new_client->dev, &dev_attr_alarms_in);
864 device_create_file(&new_client->dev, &dev_attr_temp1_input);
865 device_create_file(&new_client->dev, &dev_attr_temp2_input);
866 device_create_file(&new_client->dev, &dev_attr_temp1_min);
867 device_create_file(&new_client->dev, &dev_attr_temp2_min);
868 device_create_file(&new_client->dev, &dev_attr_temp1_max);
869 device_create_file(&new_client->dev, &dev_attr_temp2_max);
870 device_create_file(&new_client->dev, &dev_attr_temp1_crit);
871 device_create_file(&new_client->dev, &dev_attr_temp2_crit);
872 device_create_file(&new_client->dev, &dev_attr_temp1_status);
873 device_create_file(&new_client->dev, &dev_attr_temp2_status);
875 device_create_file(&new_client->dev, &dev_attr_alarms_temp);
877 if (data->tempnr == 3) {
878 device_create_file(&new_client->dev, &dev_attr_temp3_input);
879 device_create_file(&new_client->dev, &dev_attr_temp3_min);
880 device_create_file(&new_client->dev, &dev_attr_temp3_max);
881 device_create_file(&new_client->dev, &dev_attr_temp3_crit);
882 device_create_file(&new_client->dev, &dev_attr_temp3_status);
884 if (data->innr == 14) {
885 device_create_file(&new_client->dev, &dev_attr_temp4_input);
886 device_create_file(&new_client->dev, &dev_attr_temp5_input);
887 device_create_file(&new_client->dev, &dev_attr_temp6_input);
888 device_create_file(&new_client->dev, &dev_attr_temp4_min);
889 device_create_file(&new_client->dev, &dev_attr_temp5_min);
890 device_create_file(&new_client->dev, &dev_attr_temp6_min);
891 device_create_file(&new_client->dev, &dev_attr_temp4_max);
892 device_create_file(&new_client->dev, &dev_attr_temp5_max);
893 device_create_file(&new_client->dev, &dev_attr_temp6_max);
894 device_create_file(&new_client->dev, &dev_attr_temp4_crit);
895 device_create_file(&new_client->dev, &dev_attr_temp5_crit);
896 device_create_file(&new_client->dev, &dev_attr_temp6_crit);
897 device_create_file(&new_client->dev, &dev_attr_temp4_status);
898 device_create_file(&new_client->dev, &dev_attr_temp5_status);
899 device_create_file(&new_client->dev, &dev_attr_temp6_status);
903 if (FAN_CONFIG_MONITOR(data->fan_conf, 0)) {
904 device_create_file(&new_client->dev,
905 &dev_attr_fan1_input);
906 device_create_file(&new_client->dev,
908 device_create_file(&new_client->dev,
910 device_create_file(&new_client->dev,
911 &dev_attr_fan1_status);
914 if (FAN_CONFIG_MONITOR(data->fan_conf, 1)) {
915 device_create_file(&new_client->dev,
916 &dev_attr_fan2_input);
917 device_create_file(&new_client->dev,
919 device_create_file(&new_client->dev,
921 device_create_file(&new_client->dev,
922 &dev_attr_fan2_status);
925 if (FAN_CONFIG_CONTROL(data->fan_conf, 0))
926 device_create_file(&new_client->dev, &dev_attr_pwm1);
927 if (FAN_CONFIG_CONTROL(data->fan_conf, 1))
928 device_create_file(&new_client->dev, &dev_attr_pwm2);
930 if (data->fannr == 3) {
931 if (FAN_CONFIG_MONITOR(data->fan_conf, 2)) {
932 device_create_file(&new_client->dev,
933 &dev_attr_fan3_input);
934 device_create_file(&new_client->dev,
936 device_create_file(&new_client->dev,
938 device_create_file(&new_client->dev,
939 &dev_attr_fan3_status);
942 if (FAN_CONFIG_CONTROL(data->fan_conf, 2))
943 device_create_file(&new_client->dev, &dev_attr_pwm3);
949 for (i = 0; i < 3; i++) {
950 if (data->address[i]) {
951 release_region(data->address[i], PC87360_EXTENT);
959 static int pc87360_detach_client(struct i2c_client *client)
961 struct pc87360_data *data = i2c_get_clientdata(client);
964 if ((i = i2c_detach_client(client))) {
965 dev_err(&client->dev, "Client deregistration failed, "
966 "client not detached.\n");
970 for (i = 0; i < 3; i++) {
971 if (data->address[i]) {
972 release_region(data->address[i], PC87360_EXTENT);
980 /* ldi is the logical device index
981 bank is for voltages and temperatures only */
982 static int pc87360_read_value(struct pc87360_data *data, u8 ldi, u8 bank,
989 outb_p(bank, data->address[ldi] + PC87365_REG_BANK);
990 res = inb_p(data->address[ldi] + reg);
996 static void pc87360_write_value(struct pc87360_data *data, u8 ldi, u8 bank,
1000 if (bank != NO_BANK)
1001 outb_p(bank, data->address[ldi] + PC87365_REG_BANK);
1002 outb_p(value, data->address[ldi] + reg);
1006 static void pc87360_init_client(struct i2c_client *client, int use_thermistors)
1008 struct pc87360_data *data = i2c_get_clientdata(client);
1010 const u8 init_in[14] = { 2, 2, 2, 2, 2, 2, 2, 1, 1, 3, 1, 2, 2, 2 };
1011 const u8 init_temp[3] = { 2, 2, 1 };
1014 if (init >= 2 && data->innr) {
1015 reg = pc87360_read_value(data, LD_IN, NO_BANK,
1016 PC87365_REG_IN_CONVRATE);
1017 dev_info(&client->dev, "VLM conversion set to"
1018 "1s period, 160us delay\n");
1019 pc87360_write_value(data, LD_IN, NO_BANK,
1020 PC87365_REG_IN_CONVRATE,
1021 (reg & 0xC0) | 0x11);
1024 nr = data->innr < 11 ? data->innr : 11;
1025 for (i=0; i<nr; i++) {
1026 if (init >= init_in[i]) {
1027 /* Forcibly enable voltage channel */
1028 reg = pc87360_read_value(data, LD_IN, i,
1029 PC87365_REG_IN_STATUS);
1030 if (!(reg & 0x01)) {
1031 dev_dbg(&client->dev, "Forcibly "
1032 "enabling in%d\n", i);
1033 pc87360_write_value(data, LD_IN, i,
1034 PC87365_REG_IN_STATUS,
1035 (reg & 0x68) | 0x87);
1040 /* We can't blindly trust the Super-I/O space configuration bit,
1041 most BIOS won't set it properly */
1042 for (i=11; i<data->innr; i++) {
1043 reg = pc87360_read_value(data, LD_IN, i,
1044 PC87365_REG_TEMP_STATUS);
1045 use_thermistors = use_thermistors || (reg & 0x01);
1048 i = use_thermistors ? 2 : 0;
1049 for (; i<data->tempnr; i++) {
1050 if (init >= init_temp[i]) {
1051 /* Forcibly enable temperature channel */
1052 reg = pc87360_read_value(data, LD_TEMP, i,
1053 PC87365_REG_TEMP_STATUS);
1054 if (!(reg & 0x01)) {
1055 dev_dbg(&client->dev, "Forcibly "
1056 "enabling temp%d\n", i+1);
1057 pc87360_write_value(data, LD_TEMP, i,
1058 PC87365_REG_TEMP_STATUS,
1064 if (use_thermistors) {
1065 for (i=11; i<data->innr; i++) {
1066 if (init >= init_in[i]) {
1067 /* The pin may already be used by thermal
1069 reg = pc87360_read_value(data, LD_TEMP,
1070 (i-11)/2, PC87365_REG_TEMP_STATUS);
1072 dev_dbg(&client->dev, "Skipping "
1073 "temp%d, pin already in use "
1074 "by temp%d\n", i-7, (i-11)/2);
1078 /* Forcibly enable thermistor channel */
1079 reg = pc87360_read_value(data, LD_IN, i,
1080 PC87365_REG_IN_STATUS);
1081 if (!(reg & 0x01)) {
1082 dev_dbg(&client->dev, "Forcibly "
1083 "enabling temp%d\n", i-7);
1084 pc87360_write_value(data, LD_IN, i,
1085 PC87365_REG_TEMP_STATUS,
1086 (reg & 0x60) | 0x8F);
1093 reg = pc87360_read_value(data, LD_IN, NO_BANK,
1094 PC87365_REG_IN_CONFIG);
1096 dev_dbg(&client->dev, "Forcibly "
1097 "enabling monitoring (VLM)\n");
1098 pc87360_write_value(data, LD_IN, NO_BANK,
1099 PC87365_REG_IN_CONFIG,
1105 reg = pc87360_read_value(data, LD_TEMP, NO_BANK,
1106 PC87365_REG_TEMP_CONFIG);
1108 dev_dbg(&client->dev, "Forcibly enabling "
1109 "monitoring (TMS)\n");
1110 pc87360_write_value(data, LD_TEMP, NO_BANK,
1111 PC87365_REG_TEMP_CONFIG,
1116 /* Chip config as documented by National Semi. */
1117 pc87360_write_value(data, LD_TEMP, 0xF, 0xA, 0x08);
1118 /* We voluntarily omit the bank here, in case the
1119 sequence itself matters. It shouldn't be a problem,
1120 since nobody else is supposed to access the
1121 device at that point. */
1122 pc87360_write_value(data, LD_TEMP, NO_BANK, 0xB, 0x04);
1123 pc87360_write_value(data, LD_TEMP, NO_BANK, 0xC, 0x35);
1124 pc87360_write_value(data, LD_TEMP, NO_BANK, 0xD, 0x05);
1125 pc87360_write_value(data, LD_TEMP, NO_BANK, 0xE, 0x05);
1130 static void pc87360_autodiv(struct i2c_client *client, int nr)
1132 struct pc87360_data *data = i2c_get_clientdata(client);
1133 u8 old_min = data->fan_min[nr];
1135 /* Increase clock divider if needed and possible */
1136 if ((data->fan_status[nr] & 0x04) /* overflow flag */
1137 || (data->fan[nr] >= 224)) { /* next to overflow */
1138 if ((data->fan_status[nr] & 0x60) != 0x60) {
1139 data->fan_status[nr] += 0x20;
1140 data->fan_min[nr] >>= 1;
1141 data->fan[nr] >>= 1;
1142 dev_dbg(&client->dev, "Increasing "
1143 "clock divider to %d for fan %d\n",
1144 FAN_DIV_FROM_REG(data->fan_status[nr]), nr+1);
1147 /* Decrease clock divider if possible */
1148 while (!(data->fan_min[nr] & 0x80) /* min "nails" divider */
1149 && data->fan[nr] < 85 /* bad accuracy */
1150 && (data->fan_status[nr] & 0x60) != 0x00) {
1151 data->fan_status[nr] -= 0x20;
1152 data->fan_min[nr] <<= 1;
1153 data->fan[nr] <<= 1;
1154 dev_dbg(&client->dev, "Decreasing "
1155 "clock divider to %d for fan %d\n",
1156 FAN_DIV_FROM_REG(data->fan_status[nr]),
1161 /* Write new fan min if it changed */
1162 if (old_min != data->fan_min[nr]) {
1163 pc87360_write_value(data, LD_FAN, NO_BANK,
1164 PC87360_REG_FAN_MIN(nr),
1169 static struct pc87360_data *pc87360_update_device(struct device *dev)
1171 struct i2c_client *client = to_i2c_client(dev);
1172 struct pc87360_data *data = i2c_get_clientdata(client);
1175 down(&data->update_lock);
1177 if ((jiffies - data->last_updated > HZ * 2)
1178 || (jiffies < data->last_updated) || !data->valid) {
1179 dev_dbg(&client->dev, "Data update\n");
1182 for (i = 0; i < data->fannr; i++) {
1183 if (FAN_CONFIG_MONITOR(data->fan_conf, i)) {
1184 data->fan_status[i] =
1185 pc87360_read_value(data, LD_FAN,
1186 NO_BANK, PC87360_REG_FAN_STATUS(i));
1187 data->fan[i] = pc87360_read_value(data, LD_FAN,
1188 NO_BANK, PC87360_REG_FAN(i));
1189 data->fan_min[i] = pc87360_read_value(data,
1191 PC87360_REG_FAN_MIN(i));
1192 /* Change clock divider if needed */
1193 pc87360_autodiv(client, i);
1194 /* Clear bits and write new divider */
1195 pc87360_write_value(data, LD_FAN, NO_BANK,
1196 PC87360_REG_FAN_STATUS(i),
1197 data->fan_status[i]);
1199 if (FAN_CONFIG_CONTROL(data->fan_conf, i))
1200 data->pwm[i] = pc87360_read_value(data, LD_FAN,
1201 NO_BANK, PC87360_REG_PWM(i));
1205 for (i = 0; i < data->innr; i++) {
1206 data->in_status[i] = pc87360_read_value(data, LD_IN, i,
1207 PC87365_REG_IN_STATUS);
1209 pc87360_write_value(data, LD_IN, i,
1210 PC87365_REG_IN_STATUS,
1211 data->in_status[i]);
1212 if ((data->in_status[i] & 0x81) == 0x81) {
1213 data->in[i] = pc87360_read_value(data, LD_IN,
1216 if (data->in_status[i] & 0x01) {
1217 data->in_min[i] = pc87360_read_value(data,
1219 PC87365_REG_IN_MIN);
1220 data->in_max[i] = pc87360_read_value(data,
1222 PC87365_REG_IN_MAX);
1224 data->in_crit[i-11] =
1225 pc87360_read_value(data, LD_IN,
1226 i, PC87365_REG_TEMP_CRIT);
1230 data->in_alarms = pc87360_read_value(data, LD_IN,
1231 NO_BANK, PC87365_REG_IN_ALARMS1)
1232 | ((pc87360_read_value(data, LD_IN,
1233 NO_BANK, PC87365_REG_IN_ALARMS2)
1235 data->vid = (data->vid_conf & 0xE0) ?
1236 pc87360_read_value(data, LD_IN,
1237 NO_BANK, PC87365_REG_VID) : 0x1F;
1241 for (i = 0; i < data->tempnr; i++) {
1242 data->temp_status[i] = pc87360_read_value(data,
1244 PC87365_REG_TEMP_STATUS);
1246 pc87360_write_value(data, LD_TEMP, i,
1247 PC87365_REG_TEMP_STATUS,
1248 data->temp_status[i]);
1249 if ((data->temp_status[i] & 0x81) == 0x81) {
1250 data->temp[i] = pc87360_read_value(data,
1254 if (data->temp_status[i] & 0x01) {
1255 data->temp_min[i] = pc87360_read_value(data,
1257 PC87365_REG_TEMP_MIN);
1258 data->temp_max[i] = pc87360_read_value(data,
1260 PC87365_REG_TEMP_MAX);
1261 data->temp_crit[i] = pc87360_read_value(data,
1263 PC87365_REG_TEMP_CRIT);
1267 data->temp_alarms = pc87360_read_value(data, LD_TEMP,
1268 NO_BANK, PC87365_REG_TEMP_ALARMS)
1272 data->last_updated = jiffies;
1276 up(&data->update_lock);
1281 static int __init pc87360_init(void)
1285 if (pc87360_find(0x2e, &devid, extra_isa)
1286 && pc87360_find(0x4e, &devid, extra_isa)) {
1287 printk(KERN_WARNING "pc87360: PC8736x not detected, "
1288 "module not inserted.\n");
1292 /* Arbitrarily pick one of the addresses */
1293 for (i = 0; i < 3; i++) {
1294 if (extra_isa[i] != 0x0000) {
1295 normal_isa[0] = extra_isa[i];
1300 if (normal_isa[0] == 0x0000) {
1301 printk(KERN_WARNING "pc87360: No active logical device, "
1302 "module not inserted.\n");
1306 return i2c_add_driver(&pc87360_driver);
1309 static void __exit pc87360_exit(void)
1311 i2c_del_driver(&pc87360_driver);
1315 MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
1316 MODULE_DESCRIPTION("PC8736x hardware monitor");
1317 MODULE_LICENSE("GPL");
1319 module_init(pc87360_init);
1320 module_exit(pc87360_exit);