[PATCH] x86: consolidate smp_send_stop()
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / hwmon / pc87360.c
blobc8a21be09d8756cc612de0ebab23e7691ce973a8
1 /*
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/module.h>
37 #include <linux/init.h>
38 #include <linux/slab.h>
39 #include <linux/jiffies.h>
40 #include <linux/i2c.h>
41 #include <linux/i2c-isa.h>
42 #include <linux/hwmon.h>
43 #include <linux/hwmon-sysfs.h>
44 #include <linux/hwmon-vid.h>
45 #include <linux/err.h>
46 #include <linux/mutex.h>
47 #include <asm/io.h>
49 static u8 devid;
50 static unsigned short address;
51 static unsigned short extra_isa[3];
52 static u8 confreg[4];
54 enum chips { any_chip, pc87360, pc87363, pc87364, pc87365, pc87366 };
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");
66 * Super-I/O registers and operations
69 #define DEV 0x07 /* Register: Logical device select */
70 #define DEVID 0x20 /* Register: Device ID */
71 #define ACT 0x30 /* Register: Device activation */
72 #define BASE 0x60 /* Register: Base address */
74 #define FSCM 0x09 /* Logical device: fans */
75 #define VLM 0x0d /* Logical device: voltages */
76 #define TMS 0x0e /* Logical device: temperatures */
77 static const u8 logdev[3] = { FSCM, VLM, TMS };
79 #define LD_FAN 0
80 #define LD_IN 1
81 #define LD_TEMP 2
83 static inline void superio_outb(int sioaddr, int reg, int val)
85 outb(reg, sioaddr);
86 outb(val, sioaddr+1);
89 static inline int superio_inb(int sioaddr, int reg)
91 outb(reg, sioaddr);
92 return inb(sioaddr+1);
95 static inline void superio_exit(int sioaddr)
97 outb(0x02, sioaddr);
98 outb(0x02, sioaddr+1);
102 * Logical devices
105 #define PC87360_EXTENT 0x10
106 #define PC87365_REG_BANK 0x09
107 #define NO_BANK 0xff
110 * Fan registers and conversions
113 /* nr has to be 0 or 1 (PC87360/87363) or 2 (PC87364/87365/87366) */
114 #define PC87360_REG_PRESCALE(nr) (0x00 + 2 * (nr))
115 #define PC87360_REG_PWM(nr) (0x01 + 2 * (nr))
116 #define PC87360_REG_FAN_MIN(nr) (0x06 + 3 * (nr))
117 #define PC87360_REG_FAN(nr) (0x07 + 3 * (nr))
118 #define PC87360_REG_FAN_STATUS(nr) (0x08 + 3 * (nr))
120 #define FAN_FROM_REG(val,div) ((val) == 0 ? 0: \
121 480000 / ((val)*(div)))
122 #define FAN_TO_REG(val,div) ((val) <= 100 ? 0 : \
123 480000 / ((val)*(div)))
124 #define FAN_DIV_FROM_REG(val) (1 << ((val >> 5) & 0x03))
125 #define FAN_STATUS_FROM_REG(val) ((val) & 0x07)
127 #define FAN_CONFIG_MONITOR(val,nr) (((val) >> (2 + nr * 3)) & 1)
128 #define FAN_CONFIG_CONTROL(val,nr) (((val) >> (3 + nr * 3)) & 1)
129 #define FAN_CONFIG_INVERT(val,nr) (((val) >> (4 + nr * 3)) & 1)
131 #define PWM_FROM_REG(val,inv) ((inv) ? 255 - (val) : (val))
132 static inline u8 PWM_TO_REG(int val, int inv)
134 if (inv)
135 val = 255 - val;
136 if (val < 0)
137 return 0;
138 if (val > 255)
139 return 255;
140 return val;
144 * Voltage registers and conversions
147 #define PC87365_REG_IN_CONVRATE 0x07
148 #define PC87365_REG_IN_CONFIG 0x08
149 #define PC87365_REG_IN 0x0B
150 #define PC87365_REG_IN_MIN 0x0D
151 #define PC87365_REG_IN_MAX 0x0C
152 #define PC87365_REG_IN_STATUS 0x0A
153 #define PC87365_REG_IN_ALARMS1 0x00
154 #define PC87365_REG_IN_ALARMS2 0x01
155 #define PC87365_REG_VID 0x06
157 #define IN_FROM_REG(val,ref) (((val) * (ref) + 128) / 256)
158 #define IN_TO_REG(val,ref) ((val) < 0 ? 0 : \
159 (val)*256 >= (ref)*255 ? 255: \
160 ((val) * 256 + (ref)/2) / (ref))
163 * Temperature registers and conversions
166 #define PC87365_REG_TEMP_CONFIG 0x08
167 #define PC87365_REG_TEMP 0x0B
168 #define PC87365_REG_TEMP_MIN 0x0D
169 #define PC87365_REG_TEMP_MAX 0x0C
170 #define PC87365_REG_TEMP_CRIT 0x0E
171 #define PC87365_REG_TEMP_STATUS 0x0A
172 #define PC87365_REG_TEMP_ALARMS 0x00
174 #define TEMP_FROM_REG(val) ((val) * 1000)
175 #define TEMP_TO_REG(val) ((val) < -55000 ? -55 : \
176 (val) > 127000 ? 127 : \
177 (val) < 0 ? ((val) - 500) / 1000 : \
178 ((val) + 500) / 1000)
181 * Client data (each client gets its own)
184 struct pc87360_data {
185 struct i2c_client client;
186 struct class_device *class_dev;
187 struct mutex lock;
188 struct mutex update_lock;
189 char valid; /* !=0 if following fields are valid */
190 unsigned long last_updated; /* In jiffies */
192 int address[3];
194 u8 fannr, innr, tempnr;
196 u8 fan[3]; /* Register value */
197 u8 fan_min[3]; /* Register value */
198 u8 fan_status[3]; /* Register value */
199 u8 pwm[3]; /* Register value */
200 u16 fan_conf; /* Configuration register values, combined */
202 u16 in_vref; /* 1 mV/bit */
203 u8 in[14]; /* Register value */
204 u8 in_min[14]; /* Register value */
205 u8 in_max[14]; /* Register value */
206 u8 in_crit[3]; /* Register value */
207 u8 in_status[14]; /* Register value */
208 u16 in_alarms; /* Register values, combined, masked */
209 u8 vid_conf; /* Configuration register value */
210 u8 vrm;
211 u8 vid; /* Register value */
213 s8 temp[3]; /* Register value */
214 s8 temp_min[3]; /* Register value */
215 s8 temp_max[3]; /* Register value */
216 s8 temp_crit[3]; /* Register value */
217 u8 temp_status[3]; /* Register value */
218 u8 temp_alarms; /* Register value, masked */
222 * Functions declaration
225 static int pc87360_detect(struct i2c_adapter *adapter);
226 static int pc87360_detach_client(struct i2c_client *client);
228 static int pc87360_read_value(struct pc87360_data *data, u8 ldi, u8 bank,
229 u8 reg);
230 static void pc87360_write_value(struct pc87360_data *data, u8 ldi, u8 bank,
231 u8 reg, u8 value);
232 static void pc87360_init_client(struct i2c_client *client, int use_thermistors);
233 static struct pc87360_data *pc87360_update_device(struct device *dev);
236 * Driver data (common to all clients)
239 static struct i2c_driver pc87360_driver = {
240 .driver = {
241 .owner = THIS_MODULE,
242 .name = "pc87360",
244 .attach_adapter = pc87360_detect,
245 .detach_client = pc87360_detach_client,
249 * Sysfs stuff
252 static ssize_t show_fan_input(struct device *dev, struct device_attribute *devattr, char *buf)
254 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
255 struct pc87360_data *data = pc87360_update_device(dev);
256 return sprintf(buf, "%u\n", FAN_FROM_REG(data->fan[attr->index],
257 FAN_DIV_FROM_REG(data->fan_status[attr->index])));
259 static ssize_t show_fan_min(struct device *dev, struct device_attribute *devattr, char *buf)
261 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
262 struct pc87360_data *data = pc87360_update_device(dev);
263 return sprintf(buf, "%u\n", FAN_FROM_REG(data->fan_min[attr->index],
264 FAN_DIV_FROM_REG(data->fan_status[attr->index])));
266 static ssize_t show_fan_div(struct device *dev, struct device_attribute *devattr, char *buf)
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",
271 FAN_DIV_FROM_REG(data->fan_status[attr->index]));
273 static ssize_t show_fan_status(struct device *dev, struct device_attribute *devattr, char *buf)
275 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
276 struct pc87360_data *data = pc87360_update_device(dev);
277 return sprintf(buf, "%u\n",
278 FAN_STATUS_FROM_REG(data->fan_status[attr->index]));
280 static ssize_t set_fan_min(struct device *dev, struct device_attribute *devattr, const char *buf,
281 size_t count)
283 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
284 struct i2c_client *client = to_i2c_client(dev);
285 struct pc87360_data *data = i2c_get_clientdata(client);
286 long fan_min = simple_strtol(buf, NULL, 10);
288 mutex_lock(&data->update_lock);
289 fan_min = FAN_TO_REG(fan_min, FAN_DIV_FROM_REG(data->fan_status[attr->index]));
291 /* If it wouldn't fit, change clock divisor */
292 while (fan_min > 255
293 && (data->fan_status[attr->index] & 0x60) != 0x60) {
294 fan_min >>= 1;
295 data->fan[attr->index] >>= 1;
296 data->fan_status[attr->index] += 0x20;
298 data->fan_min[attr->index] = fan_min > 255 ? 255 : fan_min;
299 pc87360_write_value(data, LD_FAN, NO_BANK, PC87360_REG_FAN_MIN(attr->index),
300 data->fan_min[attr->index]);
302 /* Write new divider, preserve alarm bits */
303 pc87360_write_value(data, LD_FAN, NO_BANK, PC87360_REG_FAN_STATUS(attr->index),
304 data->fan_status[attr->index] & 0xF9);
305 mutex_unlock(&data->update_lock);
307 return count;
310 static struct sensor_device_attribute fan_input[] = {
311 SENSOR_ATTR(fan1_input, S_IRUGO, show_fan_input, NULL, 0),
312 SENSOR_ATTR(fan2_input, S_IRUGO, show_fan_input, NULL, 1),
313 SENSOR_ATTR(fan3_input, S_IRUGO, show_fan_input, NULL, 2),
315 static struct sensor_device_attribute fan_status[] = {
316 SENSOR_ATTR(fan1_status, S_IRUGO, show_fan_status, NULL, 0),
317 SENSOR_ATTR(fan2_status, S_IRUGO, show_fan_status, NULL, 1),
318 SENSOR_ATTR(fan3_status, S_IRUGO, show_fan_status, NULL, 2),
320 static struct sensor_device_attribute fan_div[] = {
321 SENSOR_ATTR(fan1_div, S_IRUGO, show_fan_div, NULL, 0),
322 SENSOR_ATTR(fan2_div, S_IRUGO, show_fan_div, NULL, 1),
323 SENSOR_ATTR(fan3_div, S_IRUGO, show_fan_div, NULL, 2),
325 static struct sensor_device_attribute fan_min[] = {
326 SENSOR_ATTR(fan1_min, S_IWUSR | S_IRUGO, show_fan_min, set_fan_min, 0),
327 SENSOR_ATTR(fan2_min, S_IWUSR | S_IRUGO, show_fan_min, set_fan_min, 1),
328 SENSOR_ATTR(fan3_min, S_IWUSR | S_IRUGO, show_fan_min, set_fan_min, 2),
331 #define FAN_UNIT_ATTRS(X) \
332 &fan_input[X].dev_attr.attr, \
333 &fan_status[X].dev_attr.attr, \
334 &fan_div[X].dev_attr.attr, \
335 &fan_min[X].dev_attr.attr
337 static ssize_t show_pwm(struct device *dev, struct device_attribute *devattr, char *buf)
339 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
340 struct pc87360_data *data = pc87360_update_device(dev);
341 return sprintf(buf, "%u\n",
342 PWM_FROM_REG(data->pwm[attr->index],
343 FAN_CONFIG_INVERT(data->fan_conf,
344 attr->index)));
346 static ssize_t set_pwm(struct device *dev, struct device_attribute *devattr, const char *buf,
347 size_t count)
349 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
350 struct i2c_client *client = to_i2c_client(dev);
351 struct pc87360_data *data = i2c_get_clientdata(client);
352 long val = simple_strtol(buf, NULL, 10);
354 mutex_lock(&data->update_lock);
355 data->pwm[attr->index] = PWM_TO_REG(val,
356 FAN_CONFIG_INVERT(data->fan_conf, attr->index));
357 pc87360_write_value(data, LD_FAN, NO_BANK, PC87360_REG_PWM(attr->index),
358 data->pwm[attr->index]);
359 mutex_unlock(&data->update_lock);
360 return count;
363 static struct sensor_device_attribute pwm[] = {
364 SENSOR_ATTR(pwm1, S_IWUSR | S_IRUGO, show_pwm, set_pwm, 0),
365 SENSOR_ATTR(pwm2, S_IWUSR | S_IRUGO, show_pwm, set_pwm, 1),
366 SENSOR_ATTR(pwm3, S_IWUSR | S_IRUGO, show_pwm, set_pwm, 2),
369 static struct attribute * pc8736x_fan_attr_array[] = {
370 FAN_UNIT_ATTRS(0),
371 FAN_UNIT_ATTRS(1),
372 FAN_UNIT_ATTRS(2),
373 &pwm[0].dev_attr.attr,
374 &pwm[1].dev_attr.attr,
375 &pwm[2].dev_attr.attr,
376 NULL
378 static const struct attribute_group pc8736x_fan_group = {
379 .attrs = pc8736x_fan_attr_array,
382 static ssize_t show_in_input(struct device *dev, struct device_attribute *devattr, char *buf)
384 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
385 struct pc87360_data *data = pc87360_update_device(dev);
386 return sprintf(buf, "%u\n", IN_FROM_REG(data->in[attr->index],
387 data->in_vref));
389 static ssize_t show_in_min(struct device *dev, struct device_attribute *devattr, char *buf)
391 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
392 struct pc87360_data *data = pc87360_update_device(dev);
393 return sprintf(buf, "%u\n", IN_FROM_REG(data->in_min[attr->index],
394 data->in_vref));
396 static ssize_t show_in_max(struct device *dev, struct device_attribute *devattr, char *buf)
398 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
399 struct pc87360_data *data = pc87360_update_device(dev);
400 return sprintf(buf, "%u\n", IN_FROM_REG(data->in_max[attr->index],
401 data->in_vref));
403 static ssize_t show_in_status(struct device *dev, struct device_attribute *devattr, char *buf)
405 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
406 struct pc87360_data *data = pc87360_update_device(dev);
407 return sprintf(buf, "%u\n", data->in_status[attr->index]);
409 static ssize_t set_in_min(struct device *dev, struct device_attribute *devattr, const char *buf,
410 size_t count)
412 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
413 struct i2c_client *client = to_i2c_client(dev);
414 struct pc87360_data *data = i2c_get_clientdata(client);
415 long val = simple_strtol(buf, NULL, 10);
417 mutex_lock(&data->update_lock);
418 data->in_min[attr->index] = IN_TO_REG(val, data->in_vref);
419 pc87360_write_value(data, LD_IN, attr->index, PC87365_REG_IN_MIN,
420 data->in_min[attr->index]);
421 mutex_unlock(&data->update_lock);
422 return count;
424 static ssize_t set_in_max(struct device *dev, struct device_attribute *devattr, const char *buf,
425 size_t count)
427 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
428 struct i2c_client *client = to_i2c_client(dev);
429 struct pc87360_data *data = i2c_get_clientdata(client);
430 long val = simple_strtol(buf, NULL, 10);
432 mutex_lock(&data->update_lock);
433 data->in_max[attr->index] = IN_TO_REG(val,
434 data->in_vref);
435 pc87360_write_value(data, LD_IN, attr->index, PC87365_REG_IN_MAX,
436 data->in_max[attr->index]);
437 mutex_unlock(&data->update_lock);
438 return count;
441 static struct sensor_device_attribute in_input[] = {
442 SENSOR_ATTR(in0_input, S_IRUGO, show_in_input, NULL, 0),
443 SENSOR_ATTR(in1_input, S_IRUGO, show_in_input, NULL, 1),
444 SENSOR_ATTR(in2_input, S_IRUGO, show_in_input, NULL, 2),
445 SENSOR_ATTR(in3_input, S_IRUGO, show_in_input, NULL, 3),
446 SENSOR_ATTR(in4_input, S_IRUGO, show_in_input, NULL, 4),
447 SENSOR_ATTR(in5_input, S_IRUGO, show_in_input, NULL, 5),
448 SENSOR_ATTR(in6_input, S_IRUGO, show_in_input, NULL, 6),
449 SENSOR_ATTR(in7_input, S_IRUGO, show_in_input, NULL, 7),
450 SENSOR_ATTR(in8_input, S_IRUGO, show_in_input, NULL, 8),
451 SENSOR_ATTR(in9_input, S_IRUGO, show_in_input, NULL, 9),
452 SENSOR_ATTR(in10_input, S_IRUGO, show_in_input, NULL, 10),
454 static struct sensor_device_attribute in_status[] = {
455 SENSOR_ATTR(in0_status, S_IRUGO, show_in_status, NULL, 0),
456 SENSOR_ATTR(in1_status, S_IRUGO, show_in_status, NULL, 1),
457 SENSOR_ATTR(in2_status, S_IRUGO, show_in_status, NULL, 2),
458 SENSOR_ATTR(in3_status, S_IRUGO, show_in_status, NULL, 3),
459 SENSOR_ATTR(in4_status, S_IRUGO, show_in_status, NULL, 4),
460 SENSOR_ATTR(in5_status, S_IRUGO, show_in_status, NULL, 5),
461 SENSOR_ATTR(in6_status, S_IRUGO, show_in_status, NULL, 6),
462 SENSOR_ATTR(in7_status, S_IRUGO, show_in_status, NULL, 7),
463 SENSOR_ATTR(in8_status, S_IRUGO, show_in_status, NULL, 8),
464 SENSOR_ATTR(in9_status, S_IRUGO, show_in_status, NULL, 9),
465 SENSOR_ATTR(in10_status, S_IRUGO, show_in_status, NULL, 10),
467 static struct sensor_device_attribute in_min[] = {
468 SENSOR_ATTR(in0_min, S_IWUSR | S_IRUGO, show_in_min, set_in_min, 0),
469 SENSOR_ATTR(in1_min, S_IWUSR | S_IRUGO, show_in_min, set_in_min, 1),
470 SENSOR_ATTR(in2_min, S_IWUSR | S_IRUGO, show_in_min, set_in_min, 2),
471 SENSOR_ATTR(in3_min, S_IWUSR | S_IRUGO, show_in_min, set_in_min, 3),
472 SENSOR_ATTR(in4_min, S_IWUSR | S_IRUGO, show_in_min, set_in_min, 4),
473 SENSOR_ATTR(in5_min, S_IWUSR | S_IRUGO, show_in_min, set_in_min, 5),
474 SENSOR_ATTR(in6_min, S_IWUSR | S_IRUGO, show_in_min, set_in_min, 6),
475 SENSOR_ATTR(in7_min, S_IWUSR | S_IRUGO, show_in_min, set_in_min, 7),
476 SENSOR_ATTR(in8_min, S_IWUSR | S_IRUGO, show_in_min, set_in_min, 8),
477 SENSOR_ATTR(in9_min, S_IWUSR | S_IRUGO, show_in_min, set_in_min, 9),
478 SENSOR_ATTR(in10_min, S_IWUSR | S_IRUGO, show_in_min, set_in_min, 10),
480 static struct sensor_device_attribute in_max[] = {
481 SENSOR_ATTR(in0_max, S_IWUSR | S_IRUGO, show_in_max, set_in_max, 0),
482 SENSOR_ATTR(in1_max, S_IWUSR | S_IRUGO, show_in_max, set_in_max, 1),
483 SENSOR_ATTR(in2_max, S_IWUSR | S_IRUGO, show_in_max, set_in_max, 2),
484 SENSOR_ATTR(in3_max, S_IWUSR | S_IRUGO, show_in_max, set_in_max, 3),
485 SENSOR_ATTR(in4_max, S_IWUSR | S_IRUGO, show_in_max, set_in_max, 4),
486 SENSOR_ATTR(in5_max, S_IWUSR | S_IRUGO, show_in_max, set_in_max, 5),
487 SENSOR_ATTR(in6_max, S_IWUSR | S_IRUGO, show_in_max, set_in_max, 6),
488 SENSOR_ATTR(in7_max, S_IWUSR | S_IRUGO, show_in_max, set_in_max, 7),
489 SENSOR_ATTR(in8_max, S_IWUSR | S_IRUGO, show_in_max, set_in_max, 8),
490 SENSOR_ATTR(in9_max, S_IWUSR | S_IRUGO, show_in_max, set_in_max, 9),
491 SENSOR_ATTR(in10_max, S_IWUSR | S_IRUGO, show_in_max, set_in_max, 10),
494 #define VIN_UNIT_ATTRS(X) \
495 &in_input[X].dev_attr.attr, \
496 &in_status[X].dev_attr.attr, \
497 &in_min[X].dev_attr.attr, \
498 &in_max[X].dev_attr.attr
500 static ssize_t show_vid(struct device *dev, struct device_attribute *attr, char *buf)
502 struct pc87360_data *data = pc87360_update_device(dev);
503 return sprintf(buf, "%u\n", vid_from_reg(data->vid, data->vrm));
505 static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL);
507 static ssize_t show_vrm(struct device *dev, struct device_attribute *attr, char *buf)
509 struct pc87360_data *data = pc87360_update_device(dev);
510 return sprintf(buf, "%u\n", data->vrm);
512 static ssize_t set_vrm(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
514 struct i2c_client *client = to_i2c_client(dev);
515 struct pc87360_data *data = i2c_get_clientdata(client);
516 data->vrm = simple_strtoul(buf, NULL, 10);
517 return count;
519 static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm, set_vrm);
521 static ssize_t show_in_alarms(struct device *dev, struct device_attribute *attr, char *buf)
523 struct pc87360_data *data = pc87360_update_device(dev);
524 return sprintf(buf, "%u\n", data->in_alarms);
526 static DEVICE_ATTR(alarms_in, S_IRUGO, show_in_alarms, NULL);
528 static struct attribute *pc8736x_vin_attr_array[] = {
529 VIN_UNIT_ATTRS(0),
530 VIN_UNIT_ATTRS(1),
531 VIN_UNIT_ATTRS(2),
532 VIN_UNIT_ATTRS(3),
533 VIN_UNIT_ATTRS(4),
534 VIN_UNIT_ATTRS(5),
535 VIN_UNIT_ATTRS(6),
536 VIN_UNIT_ATTRS(7),
537 VIN_UNIT_ATTRS(8),
538 VIN_UNIT_ATTRS(9),
539 VIN_UNIT_ATTRS(10),
540 &dev_attr_cpu0_vid.attr,
541 &dev_attr_vrm.attr,
542 &dev_attr_alarms_in.attr,
543 NULL
545 static const struct attribute_group pc8736x_vin_group = {
546 .attrs = pc8736x_vin_attr_array,
549 static ssize_t show_therm_input(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[attr->index],
554 data->in_vref));
556 static ssize_t show_therm_min(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_min[attr->index],
561 data->in_vref));
563 static ssize_t show_therm_max(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_max[attr->index],
568 data->in_vref));
570 static ssize_t show_therm_crit(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", IN_FROM_REG(data->in_crit[attr->index-11],
575 data->in_vref));
577 static ssize_t show_therm_status(struct device *dev, struct device_attribute *devattr, char *buf)
579 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
580 struct pc87360_data *data = pc87360_update_device(dev);
581 return sprintf(buf, "%u\n", data->in_status[attr->index]);
583 static ssize_t set_therm_min(struct device *dev, struct device_attribute *devattr, const char *buf,
584 size_t count)
586 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
587 struct i2c_client *client = to_i2c_client(dev);
588 struct pc87360_data *data = i2c_get_clientdata(client);
589 long val = simple_strtol(buf, NULL, 10);
591 mutex_lock(&data->update_lock);
592 data->in_min[attr->index] = IN_TO_REG(val, data->in_vref);
593 pc87360_write_value(data, LD_IN, attr->index, PC87365_REG_TEMP_MIN,
594 data->in_min[attr->index]);
595 mutex_unlock(&data->update_lock);
596 return count;
598 static ssize_t set_therm_max(struct device *dev, struct device_attribute *devattr, const char *buf,
599 size_t count)
601 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
602 struct i2c_client *client = to_i2c_client(dev);
603 struct pc87360_data *data = i2c_get_clientdata(client);
604 long val = simple_strtol(buf, NULL, 10);
606 mutex_lock(&data->update_lock);
607 data->in_max[attr->index] = IN_TO_REG(val, data->in_vref);
608 pc87360_write_value(data, LD_IN, attr->index, PC87365_REG_TEMP_MAX,
609 data->in_max[attr->index]);
610 mutex_unlock(&data->update_lock);
611 return count;
613 static ssize_t set_therm_crit(struct device *dev, struct device_attribute *devattr, const char *buf,
614 size_t count)
616 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
617 struct i2c_client *client = to_i2c_client(dev);
618 struct pc87360_data *data = i2c_get_clientdata(client);
619 long val = simple_strtol(buf, NULL, 10);
621 mutex_lock(&data->update_lock);
622 data->in_crit[attr->index-11] = IN_TO_REG(val, data->in_vref);
623 pc87360_write_value(data, LD_IN, attr->index, PC87365_REG_TEMP_CRIT,
624 data->in_crit[attr->index-11]);
625 mutex_unlock(&data->update_lock);
626 return count;
629 /* the +11 term below reflects the fact that VLM units 11,12,13 are
630 used in the chip to measure voltage across the thermistors
632 static struct sensor_device_attribute therm_input[] = {
633 SENSOR_ATTR(temp4_input, S_IRUGO, show_therm_input, NULL, 0+11),
634 SENSOR_ATTR(temp5_input, S_IRUGO, show_therm_input, NULL, 1+11),
635 SENSOR_ATTR(temp6_input, S_IRUGO, show_therm_input, NULL, 2+11),
637 static struct sensor_device_attribute therm_status[] = {
638 SENSOR_ATTR(temp4_status, S_IRUGO, show_therm_status, NULL, 0+11),
639 SENSOR_ATTR(temp5_status, S_IRUGO, show_therm_status, NULL, 1+11),
640 SENSOR_ATTR(temp6_status, S_IRUGO, show_therm_status, NULL, 2+11),
642 static struct sensor_device_attribute therm_min[] = {
643 SENSOR_ATTR(temp4_min, S_IRUGO | S_IWUSR,
644 show_therm_min, set_therm_min, 0+11),
645 SENSOR_ATTR(temp5_min, S_IRUGO | S_IWUSR,
646 show_therm_min, set_therm_min, 1+11),
647 SENSOR_ATTR(temp6_min, S_IRUGO | S_IWUSR,
648 show_therm_min, set_therm_min, 2+11),
650 static struct sensor_device_attribute therm_max[] = {
651 SENSOR_ATTR(temp4_max, S_IRUGO | S_IWUSR,
652 show_therm_max, set_therm_max, 0+11),
653 SENSOR_ATTR(temp5_max, S_IRUGO | S_IWUSR,
654 show_therm_max, set_therm_max, 1+11),
655 SENSOR_ATTR(temp6_max, S_IRUGO | S_IWUSR,
656 show_therm_max, set_therm_max, 2+11),
658 static struct sensor_device_attribute therm_crit[] = {
659 SENSOR_ATTR(temp4_crit, S_IRUGO | S_IWUSR,
660 show_therm_crit, set_therm_crit, 0+11),
661 SENSOR_ATTR(temp5_crit, S_IRUGO | S_IWUSR,
662 show_therm_crit, set_therm_crit, 1+11),
663 SENSOR_ATTR(temp6_crit, S_IRUGO | S_IWUSR,
664 show_therm_crit, set_therm_crit, 2+11),
667 #define THERM_UNIT_ATTRS(X) \
668 &therm_input[X].dev_attr.attr, \
669 &therm_status[X].dev_attr.attr, \
670 &therm_min[X].dev_attr.attr, \
671 &therm_max[X].dev_attr.attr, \
672 &therm_crit[X].dev_attr.attr
674 static struct attribute * pc8736x_therm_attr_array[] = {
675 THERM_UNIT_ATTRS(0),
676 THERM_UNIT_ATTRS(1),
677 THERM_UNIT_ATTRS(2),
678 NULL
680 static const struct attribute_group pc8736x_therm_group = {
681 .attrs = pc8736x_therm_attr_array,
684 static ssize_t show_temp_input(struct device *dev, struct device_attribute *devattr, char *buf)
686 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
687 struct pc87360_data *data = pc87360_update_device(dev);
688 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[attr->index]));
690 static ssize_t show_temp_min(struct device *dev, struct device_attribute *devattr, char *buf)
692 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
693 struct pc87360_data *data = pc87360_update_device(dev);
694 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_min[attr->index]));
696 static ssize_t show_temp_max(struct device *dev, struct device_attribute *devattr, char *buf)
698 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
699 struct pc87360_data *data = pc87360_update_device(dev);
700 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max[attr->index]));
702 static ssize_t show_temp_crit(struct device *dev, struct device_attribute *devattr, char *buf)
704 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
705 struct pc87360_data *data = pc87360_update_device(dev);
706 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_crit[attr->index]));
708 static ssize_t show_temp_status(struct device *dev, struct device_attribute *devattr, char *buf)
710 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
711 struct pc87360_data *data = pc87360_update_device(dev);
712 return sprintf(buf, "%d\n", data->temp_status[attr->index]);
714 static ssize_t set_temp_min(struct device *dev, struct device_attribute *devattr, const char *buf,
715 size_t count)
717 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
718 struct i2c_client *client = to_i2c_client(dev);
719 struct pc87360_data *data = i2c_get_clientdata(client);
720 long val = simple_strtol(buf, NULL, 10);
722 mutex_lock(&data->update_lock);
723 data->temp_min[attr->index] = TEMP_TO_REG(val);
724 pc87360_write_value(data, LD_TEMP, attr->index, PC87365_REG_TEMP_MIN,
725 data->temp_min[attr->index]);
726 mutex_unlock(&data->update_lock);
727 return count;
729 static ssize_t set_temp_max(struct device *dev, struct device_attribute *devattr, const char *buf,
730 size_t count)
732 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
733 struct i2c_client *client = to_i2c_client(dev);
734 struct pc87360_data *data = i2c_get_clientdata(client);
735 long val = simple_strtol(buf, NULL, 10);
737 mutex_lock(&data->update_lock);
738 data->temp_max[attr->index] = TEMP_TO_REG(val);
739 pc87360_write_value(data, LD_TEMP, attr->index, PC87365_REG_TEMP_MAX,
740 data->temp_max[attr->index]);
741 mutex_unlock(&data->update_lock);
742 return count;
744 static ssize_t set_temp_crit(struct device *dev, struct device_attribute *devattr, const char *buf,
745 size_t count)
747 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
748 struct i2c_client *client = to_i2c_client(dev);
749 struct pc87360_data *data = i2c_get_clientdata(client);
750 long val = simple_strtol(buf, NULL, 10);
752 mutex_lock(&data->update_lock);
753 data->temp_crit[attr->index] = TEMP_TO_REG(val);
754 pc87360_write_value(data, LD_TEMP, attr->index, PC87365_REG_TEMP_CRIT,
755 data->temp_crit[attr->index]);
756 mutex_unlock(&data->update_lock);
757 return count;
760 static struct sensor_device_attribute temp_input[] = {
761 SENSOR_ATTR(temp1_input, S_IRUGO, show_temp_input, NULL, 0),
762 SENSOR_ATTR(temp2_input, S_IRUGO, show_temp_input, NULL, 1),
763 SENSOR_ATTR(temp3_input, S_IRUGO, show_temp_input, NULL, 2),
765 static struct sensor_device_attribute temp_status[] = {
766 SENSOR_ATTR(temp1_status, S_IRUGO, show_temp_status, NULL, 0),
767 SENSOR_ATTR(temp2_status, S_IRUGO, show_temp_status, NULL, 1),
768 SENSOR_ATTR(temp3_status, S_IRUGO, show_temp_status, NULL, 2),
770 static struct sensor_device_attribute temp_min[] = {
771 SENSOR_ATTR(temp1_min, S_IRUGO | S_IWUSR,
772 show_temp_min, set_temp_min, 0),
773 SENSOR_ATTR(temp2_min, S_IRUGO | S_IWUSR,
774 show_temp_min, set_temp_min, 1),
775 SENSOR_ATTR(temp3_min, S_IRUGO | S_IWUSR,
776 show_temp_min, set_temp_min, 2),
778 static struct sensor_device_attribute temp_max[] = {
779 SENSOR_ATTR(temp1_max, S_IRUGO | S_IWUSR,
780 show_temp_max, set_temp_max, 0),
781 SENSOR_ATTR(temp2_max, S_IRUGO | S_IWUSR,
782 show_temp_max, set_temp_max, 1),
783 SENSOR_ATTR(temp3_max, S_IRUGO | S_IWUSR,
784 show_temp_max, set_temp_max, 2),
786 static struct sensor_device_attribute temp_crit[] = {
787 SENSOR_ATTR(temp1_crit, S_IRUGO | S_IWUSR,
788 show_temp_crit, set_temp_crit, 0),
789 SENSOR_ATTR(temp2_crit, S_IRUGO | S_IWUSR,
790 show_temp_crit, set_temp_crit, 1),
791 SENSOR_ATTR(temp3_crit, S_IRUGO | S_IWUSR,
792 show_temp_crit, set_temp_crit, 2),
795 static ssize_t show_temp_alarms(struct device *dev, struct device_attribute *attr, char *buf)
797 struct pc87360_data *data = pc87360_update_device(dev);
798 return sprintf(buf, "%u\n", data->temp_alarms);
800 static DEVICE_ATTR(alarms_temp, S_IRUGO, show_temp_alarms, NULL);
802 #define TEMP_UNIT_ATTRS(X) \
803 &temp_input[X].dev_attr.attr, \
804 &temp_status[X].dev_attr.attr, \
805 &temp_min[X].dev_attr.attr, \
806 &temp_max[X].dev_attr.attr, \
807 &temp_crit[X].dev_attr.attr
809 static struct attribute * pc8736x_temp_attr_array[] = {
810 TEMP_UNIT_ATTRS(0),
811 TEMP_UNIT_ATTRS(1),
812 TEMP_UNIT_ATTRS(2),
813 /* include the few miscellaneous atts here */
814 &dev_attr_alarms_temp.attr,
815 NULL
817 static const struct attribute_group pc8736x_temp_group = {
818 .attrs = pc8736x_temp_attr_array,
822 * Device detection, registration and update
825 static int __init pc87360_find(int sioaddr, u8 *devid, unsigned short *addresses)
827 u16 val;
828 int i;
829 int nrdev; /* logical device count */
831 /* No superio_enter */
833 /* Identify device */
834 val = superio_inb(sioaddr, DEVID);
835 switch (val) {
836 case 0xE1: /* PC87360 */
837 case 0xE8: /* PC87363 */
838 case 0xE4: /* PC87364 */
839 nrdev = 1;
840 break;
841 case 0xE5: /* PC87365 */
842 case 0xE9: /* PC87366 */
843 nrdev = 3;
844 break;
845 default:
846 superio_exit(sioaddr);
847 return -ENODEV;
849 /* Remember the device id */
850 *devid = val;
852 for (i = 0; i < nrdev; i++) {
853 /* select logical device */
854 superio_outb(sioaddr, DEV, logdev[i]);
856 val = superio_inb(sioaddr, ACT);
857 if (!(val & 0x01)) {
858 printk(KERN_INFO "pc87360: Device 0x%02x not "
859 "activated\n", logdev[i]);
860 continue;
863 val = (superio_inb(sioaddr, BASE) << 8)
864 | superio_inb(sioaddr, BASE + 1);
865 if (!val) {
866 printk(KERN_INFO "pc87360: Base address not set for "
867 "device 0x%02x\n", logdev[i]);
868 continue;
871 addresses[i] = val;
873 if (i==0) { /* Fans */
874 confreg[0] = superio_inb(sioaddr, 0xF0);
875 confreg[1] = superio_inb(sioaddr, 0xF1);
877 #ifdef DEBUG
878 printk(KERN_DEBUG "pc87360: Fan 1: mon=%d "
879 "ctrl=%d inv=%d\n", (confreg[0]>>2)&1,
880 (confreg[0]>>3)&1, (confreg[0]>>4)&1);
881 printk(KERN_DEBUG "pc87360: Fan 2: mon=%d "
882 "ctrl=%d inv=%d\n", (confreg[0]>>5)&1,
883 (confreg[0]>>6)&1, (confreg[0]>>7)&1);
884 printk(KERN_DEBUG "pc87360: Fan 3: mon=%d "
885 "ctrl=%d inv=%d\n", confreg[1]&1,
886 (confreg[1]>>1)&1, (confreg[1]>>2)&1);
887 #endif
888 } else if (i==1) { /* Voltages */
889 /* Are we using thermistors? */
890 if (*devid == 0xE9) { /* PC87366 */
891 /* These registers are not logical-device
892 specific, just that we won't need them if
893 we don't use the VLM device */
894 confreg[2] = superio_inb(sioaddr, 0x2B);
895 confreg[3] = superio_inb(sioaddr, 0x25);
897 if (confreg[2] & 0x40) {
898 printk(KERN_INFO "pc87360: Using "
899 "thermistors for temperature "
900 "monitoring\n");
902 if (confreg[3] & 0xE0) {
903 printk(KERN_INFO "pc87360: VID "
904 "inputs routed (mode %u)\n",
905 confreg[3] >> 5);
911 superio_exit(sioaddr);
912 return 0;
915 static int pc87360_detect(struct i2c_adapter *adapter)
917 int i;
918 struct i2c_client *client;
919 struct pc87360_data *data;
920 int err = 0;
921 const char *name = "pc87360";
922 int use_thermistors = 0;
923 struct device *dev;
925 if (!(data = kzalloc(sizeof(struct pc87360_data), GFP_KERNEL)))
926 return -ENOMEM;
928 client = &data->client;
929 dev = &client->dev;
930 i2c_set_clientdata(client, data);
931 client->addr = address;
932 mutex_init(&data->lock);
933 client->adapter = adapter;
934 client->driver = &pc87360_driver;
935 client->flags = 0;
937 data->fannr = 2;
938 data->innr = 0;
939 data->tempnr = 0;
941 switch (devid) {
942 case 0xe8:
943 name = "pc87363";
944 break;
945 case 0xe4:
946 name = "pc87364";
947 data->fannr = 3;
948 break;
949 case 0xe5:
950 name = "pc87365";
951 data->fannr = extra_isa[0] ? 3 : 0;
952 data->innr = extra_isa[1] ? 11 : 0;
953 data->tempnr = extra_isa[2] ? 2 : 0;
954 break;
955 case 0xe9:
956 name = "pc87366";
957 data->fannr = extra_isa[0] ? 3 : 0;
958 data->innr = extra_isa[1] ? 14 : 0;
959 data->tempnr = extra_isa[2] ? 3 : 0;
960 break;
963 strlcpy(client->name, name, sizeof(client->name));
964 data->valid = 0;
965 mutex_init(&data->update_lock);
967 for (i = 0; i < 3; i++) {
968 if (((data->address[i] = extra_isa[i]))
969 && !request_region(extra_isa[i], PC87360_EXTENT,
970 pc87360_driver.driver.name)) {
971 dev_err(&client->dev, "Region 0x%x-0x%x already "
972 "in use!\n", extra_isa[i],
973 extra_isa[i]+PC87360_EXTENT-1);
974 for (i--; i >= 0; i--)
975 release_region(extra_isa[i], PC87360_EXTENT);
976 err = -EBUSY;
977 goto ERROR1;
981 /* Retrieve the fans configuration from Super-I/O space */
982 if (data->fannr)
983 data->fan_conf = confreg[0] | (confreg[1] << 8);
985 if ((err = i2c_attach_client(client)))
986 goto ERROR2;
988 /* Use the correct reference voltage
989 Unless both the VLM and the TMS logical devices agree to
990 use an external Vref, the internal one is used. */
991 if (data->innr) {
992 i = pc87360_read_value(data, LD_IN, NO_BANK,
993 PC87365_REG_IN_CONFIG);
994 if (data->tempnr) {
995 i &= pc87360_read_value(data, LD_TEMP, NO_BANK,
996 PC87365_REG_TEMP_CONFIG);
998 data->in_vref = (i&0x02) ? 3025 : 2966;
999 dev_dbg(&client->dev, "Using %s reference voltage\n",
1000 (i&0x02) ? "external" : "internal");
1002 data->vid_conf = confreg[3];
1003 data->vrm = vid_which_vrm();
1006 /* Fan clock dividers may be needed before any data is read */
1007 for (i = 0; i < data->fannr; i++) {
1008 if (FAN_CONFIG_MONITOR(data->fan_conf, i))
1009 data->fan_status[i] = pc87360_read_value(data,
1010 LD_FAN, NO_BANK,
1011 PC87360_REG_FAN_STATUS(i));
1014 if (init > 0) {
1015 if (devid == 0xe9 && data->address[1]) /* PC87366 */
1016 use_thermistors = confreg[2] & 0x40;
1018 pc87360_init_client(client, use_thermistors);
1021 /* Register all-or-nothing sysfs groups */
1023 if (data->innr &&
1024 (err = sysfs_create_group(&client->dev.kobj,
1025 &pc8736x_vin_group)))
1026 goto ERROR3;
1028 if (data->innr == 14 &&
1029 (err = sysfs_create_group(&client->dev.kobj,
1030 &pc8736x_therm_group)))
1031 goto ERROR3;
1033 /* create device attr-files for varying sysfs groups */
1035 if (data->tempnr) {
1036 for (i = 0; i < data->tempnr; i++) {
1037 if ((err = device_create_file(dev,
1038 &temp_input[i].dev_attr))
1039 || (err = device_create_file(dev,
1040 &temp_min[i].dev_attr))
1041 || (err = device_create_file(dev,
1042 &temp_max[i].dev_attr))
1043 || (err = device_create_file(dev,
1044 &temp_crit[i].dev_attr))
1045 || (err = device_create_file(dev,
1046 &temp_status[i].dev_attr)))
1047 goto ERROR3;
1049 if ((err = device_create_file(dev, &dev_attr_alarms_temp)))
1050 goto ERROR3;
1053 for (i = 0; i < data->fannr; i++) {
1054 if (FAN_CONFIG_MONITOR(data->fan_conf, i)
1055 && ((err = device_create_file(dev,
1056 &fan_input[i].dev_attr))
1057 || (err = device_create_file(dev,
1058 &fan_min[i].dev_attr))
1059 || (err = device_create_file(dev,
1060 &fan_div[i].dev_attr))
1061 || (err = device_create_file(dev,
1062 &fan_status[i].dev_attr))))
1063 goto ERROR3;
1065 if (FAN_CONFIG_CONTROL(data->fan_conf, i)
1066 && (err = device_create_file(dev, &pwm[i].dev_attr)))
1067 goto ERROR3;
1070 data->class_dev = hwmon_device_register(&client->dev);
1071 if (IS_ERR(data->class_dev)) {
1072 err = PTR_ERR(data->class_dev);
1073 goto ERROR3;
1075 return 0;
1077 ERROR3:
1078 /* can still remove groups whose members were added individually */
1079 sysfs_remove_group(&client->dev.kobj, &pc8736x_temp_group);
1080 sysfs_remove_group(&client->dev.kobj, &pc8736x_fan_group);
1081 sysfs_remove_group(&client->dev.kobj, &pc8736x_therm_group);
1082 sysfs_remove_group(&client->dev.kobj, &pc8736x_vin_group);
1084 i2c_detach_client(client);
1085 ERROR2:
1086 for (i = 0; i < 3; i++) {
1087 if (data->address[i]) {
1088 release_region(data->address[i], PC87360_EXTENT);
1091 ERROR1:
1092 kfree(data);
1093 return err;
1096 static int pc87360_detach_client(struct i2c_client *client)
1098 struct pc87360_data *data = i2c_get_clientdata(client);
1099 int i;
1101 hwmon_device_unregister(data->class_dev);
1103 sysfs_remove_group(&client->dev.kobj, &pc8736x_temp_group);
1104 sysfs_remove_group(&client->dev.kobj, &pc8736x_fan_group);
1105 sysfs_remove_group(&client->dev.kobj, &pc8736x_therm_group);
1106 sysfs_remove_group(&client->dev.kobj, &pc8736x_vin_group);
1108 if ((i = i2c_detach_client(client)))
1109 return i;
1111 for (i = 0; i < 3; i++) {
1112 if (data->address[i]) {
1113 release_region(data->address[i], PC87360_EXTENT);
1116 kfree(data);
1118 return 0;
1121 /* ldi is the logical device index
1122 bank is for voltages and temperatures only */
1123 static int pc87360_read_value(struct pc87360_data *data, u8 ldi, u8 bank,
1124 u8 reg)
1126 int res;
1128 mutex_lock(&(data->lock));
1129 if (bank != NO_BANK)
1130 outb_p(bank, data->address[ldi] + PC87365_REG_BANK);
1131 res = inb_p(data->address[ldi] + reg);
1132 mutex_unlock(&(data->lock));
1134 return res;
1137 static void pc87360_write_value(struct pc87360_data *data, u8 ldi, u8 bank,
1138 u8 reg, u8 value)
1140 mutex_lock(&(data->lock));
1141 if (bank != NO_BANK)
1142 outb_p(bank, data->address[ldi] + PC87365_REG_BANK);
1143 outb_p(value, data->address[ldi] + reg);
1144 mutex_unlock(&(data->lock));
1147 static void pc87360_init_client(struct i2c_client *client, int use_thermistors)
1149 struct pc87360_data *data = i2c_get_clientdata(client);
1150 int i, nr;
1151 const u8 init_in[14] = { 2, 2, 2, 2, 2, 2, 2, 1, 1, 3, 1, 2, 2, 2 };
1152 const u8 init_temp[3] = { 2, 2, 1 };
1153 u8 reg;
1155 if (init >= 2 && data->innr) {
1156 reg = pc87360_read_value(data, LD_IN, NO_BANK,
1157 PC87365_REG_IN_CONVRATE);
1158 dev_info(&client->dev, "VLM conversion set to "
1159 "1s period, 160us delay\n");
1160 pc87360_write_value(data, LD_IN, NO_BANK,
1161 PC87365_REG_IN_CONVRATE,
1162 (reg & 0xC0) | 0x11);
1165 nr = data->innr < 11 ? data->innr : 11;
1166 for (i = 0; i < nr; i++) {
1167 if (init >= init_in[i]) {
1168 /* Forcibly enable voltage channel */
1169 reg = pc87360_read_value(data, LD_IN, i,
1170 PC87365_REG_IN_STATUS);
1171 if (!(reg & 0x01)) {
1172 dev_dbg(&client->dev, "Forcibly "
1173 "enabling in%d\n", i);
1174 pc87360_write_value(data, LD_IN, i,
1175 PC87365_REG_IN_STATUS,
1176 (reg & 0x68) | 0x87);
1181 /* We can't blindly trust the Super-I/O space configuration bit,
1182 most BIOS won't set it properly */
1183 for (i = 11; i < data->innr; i++) {
1184 reg = pc87360_read_value(data, LD_IN, i,
1185 PC87365_REG_TEMP_STATUS);
1186 use_thermistors = use_thermistors || (reg & 0x01);
1189 i = use_thermistors ? 2 : 0;
1190 for (; i < data->tempnr; i++) {
1191 if (init >= init_temp[i]) {
1192 /* Forcibly enable temperature channel */
1193 reg = pc87360_read_value(data, LD_TEMP, i,
1194 PC87365_REG_TEMP_STATUS);
1195 if (!(reg & 0x01)) {
1196 dev_dbg(&client->dev, "Forcibly "
1197 "enabling temp%d\n", i+1);
1198 pc87360_write_value(data, LD_TEMP, i,
1199 PC87365_REG_TEMP_STATUS,
1200 0xCF);
1205 if (use_thermistors) {
1206 for (i = 11; i < data->innr; i++) {
1207 if (init >= init_in[i]) {
1208 /* The pin may already be used by thermal
1209 diodes */
1210 reg = pc87360_read_value(data, LD_TEMP,
1211 (i-11)/2, PC87365_REG_TEMP_STATUS);
1212 if (reg & 0x01) {
1213 dev_dbg(&client->dev, "Skipping "
1214 "temp%d, pin already in use "
1215 "by temp%d\n", i-7, (i-11)/2);
1216 continue;
1219 /* Forcibly enable thermistor channel */
1220 reg = pc87360_read_value(data, LD_IN, i,
1221 PC87365_REG_IN_STATUS);
1222 if (!(reg & 0x01)) {
1223 dev_dbg(&client->dev, "Forcibly "
1224 "enabling temp%d\n", i-7);
1225 pc87360_write_value(data, LD_IN, i,
1226 PC87365_REG_TEMP_STATUS,
1227 (reg & 0x60) | 0x8F);
1233 if (data->innr) {
1234 reg = pc87360_read_value(data, LD_IN, NO_BANK,
1235 PC87365_REG_IN_CONFIG);
1236 if (reg & 0x01) {
1237 dev_dbg(&client->dev, "Forcibly "
1238 "enabling monitoring (VLM)\n");
1239 pc87360_write_value(data, LD_IN, NO_BANK,
1240 PC87365_REG_IN_CONFIG,
1241 reg & 0xFE);
1245 if (data->tempnr) {
1246 reg = pc87360_read_value(data, LD_TEMP, NO_BANK,
1247 PC87365_REG_TEMP_CONFIG);
1248 if (reg & 0x01) {
1249 dev_dbg(&client->dev, "Forcibly enabling "
1250 "monitoring (TMS)\n");
1251 pc87360_write_value(data, LD_TEMP, NO_BANK,
1252 PC87365_REG_TEMP_CONFIG,
1253 reg & 0xFE);
1256 if (init >= 2) {
1257 /* Chip config as documented by National Semi. */
1258 pc87360_write_value(data, LD_TEMP, 0xF, 0xA, 0x08);
1259 /* We voluntarily omit the bank here, in case the
1260 sequence itself matters. It shouldn't be a problem,
1261 since nobody else is supposed to access the
1262 device at that point. */
1263 pc87360_write_value(data, LD_TEMP, NO_BANK, 0xB, 0x04);
1264 pc87360_write_value(data, LD_TEMP, NO_BANK, 0xC, 0x35);
1265 pc87360_write_value(data, LD_TEMP, NO_BANK, 0xD, 0x05);
1266 pc87360_write_value(data, LD_TEMP, NO_BANK, 0xE, 0x05);
1271 static void pc87360_autodiv(struct i2c_client *client, int nr)
1273 struct pc87360_data *data = i2c_get_clientdata(client);
1274 u8 old_min = data->fan_min[nr];
1276 /* Increase clock divider if needed and possible */
1277 if ((data->fan_status[nr] & 0x04) /* overflow flag */
1278 || (data->fan[nr] >= 224)) { /* next to overflow */
1279 if ((data->fan_status[nr] & 0x60) != 0x60) {
1280 data->fan_status[nr] += 0x20;
1281 data->fan_min[nr] >>= 1;
1282 data->fan[nr] >>= 1;
1283 dev_dbg(&client->dev, "Increasing "
1284 "clock divider to %d for fan %d\n",
1285 FAN_DIV_FROM_REG(data->fan_status[nr]), nr+1);
1287 } else {
1288 /* Decrease clock divider if possible */
1289 while (!(data->fan_min[nr] & 0x80) /* min "nails" divider */
1290 && data->fan[nr] < 85 /* bad accuracy */
1291 && (data->fan_status[nr] & 0x60) != 0x00) {
1292 data->fan_status[nr] -= 0x20;
1293 data->fan_min[nr] <<= 1;
1294 data->fan[nr] <<= 1;
1295 dev_dbg(&client->dev, "Decreasing "
1296 "clock divider to %d for fan %d\n",
1297 FAN_DIV_FROM_REG(data->fan_status[nr]),
1298 nr+1);
1302 /* Write new fan min if it changed */
1303 if (old_min != data->fan_min[nr]) {
1304 pc87360_write_value(data, LD_FAN, NO_BANK,
1305 PC87360_REG_FAN_MIN(nr),
1306 data->fan_min[nr]);
1310 static struct pc87360_data *pc87360_update_device(struct device *dev)
1312 struct i2c_client *client = to_i2c_client(dev);
1313 struct pc87360_data *data = i2c_get_clientdata(client);
1314 u8 i;
1316 mutex_lock(&data->update_lock);
1318 if (time_after(jiffies, data->last_updated + HZ * 2) || !data->valid) {
1319 dev_dbg(&client->dev, "Data update\n");
1321 /* Fans */
1322 for (i = 0; i < data->fannr; i++) {
1323 if (FAN_CONFIG_MONITOR(data->fan_conf, i)) {
1324 data->fan_status[i] =
1325 pc87360_read_value(data, LD_FAN,
1326 NO_BANK, PC87360_REG_FAN_STATUS(i));
1327 data->fan[i] = pc87360_read_value(data, LD_FAN,
1328 NO_BANK, PC87360_REG_FAN(i));
1329 data->fan_min[i] = pc87360_read_value(data,
1330 LD_FAN, NO_BANK,
1331 PC87360_REG_FAN_MIN(i));
1332 /* Change clock divider if needed */
1333 pc87360_autodiv(client, i);
1334 /* Clear bits and write new divider */
1335 pc87360_write_value(data, LD_FAN, NO_BANK,
1336 PC87360_REG_FAN_STATUS(i),
1337 data->fan_status[i]);
1339 if (FAN_CONFIG_CONTROL(data->fan_conf, i))
1340 data->pwm[i] = pc87360_read_value(data, LD_FAN,
1341 NO_BANK, PC87360_REG_PWM(i));
1344 /* Voltages */
1345 for (i = 0; i < data->innr; i++) {
1346 data->in_status[i] = pc87360_read_value(data, LD_IN, i,
1347 PC87365_REG_IN_STATUS);
1348 /* Clear bits */
1349 pc87360_write_value(data, LD_IN, i,
1350 PC87365_REG_IN_STATUS,
1351 data->in_status[i]);
1352 if ((data->in_status[i] & 0x81) == 0x81) {
1353 data->in[i] = pc87360_read_value(data, LD_IN,
1354 i, PC87365_REG_IN);
1356 if (data->in_status[i] & 0x01) {
1357 data->in_min[i] = pc87360_read_value(data,
1358 LD_IN, i,
1359 PC87365_REG_IN_MIN);
1360 data->in_max[i] = pc87360_read_value(data,
1361 LD_IN, i,
1362 PC87365_REG_IN_MAX);
1363 if (i >= 11)
1364 data->in_crit[i-11] =
1365 pc87360_read_value(data, LD_IN,
1366 i, PC87365_REG_TEMP_CRIT);
1369 if (data->innr) {
1370 data->in_alarms = pc87360_read_value(data, LD_IN,
1371 NO_BANK, PC87365_REG_IN_ALARMS1)
1372 | ((pc87360_read_value(data, LD_IN,
1373 NO_BANK, PC87365_REG_IN_ALARMS2)
1374 & 0x07) << 8);
1375 data->vid = (data->vid_conf & 0xE0) ?
1376 pc87360_read_value(data, LD_IN,
1377 NO_BANK, PC87365_REG_VID) : 0x1F;
1380 /* Temperatures */
1381 for (i = 0; i < data->tempnr; i++) {
1382 data->temp_status[i] = pc87360_read_value(data,
1383 LD_TEMP, i,
1384 PC87365_REG_TEMP_STATUS);
1385 /* Clear bits */
1386 pc87360_write_value(data, LD_TEMP, i,
1387 PC87365_REG_TEMP_STATUS,
1388 data->temp_status[i]);
1389 if ((data->temp_status[i] & 0x81) == 0x81) {
1390 data->temp[i] = pc87360_read_value(data,
1391 LD_TEMP, i,
1392 PC87365_REG_TEMP);
1394 if (data->temp_status[i] & 0x01) {
1395 data->temp_min[i] = pc87360_read_value(data,
1396 LD_TEMP, i,
1397 PC87365_REG_TEMP_MIN);
1398 data->temp_max[i] = pc87360_read_value(data,
1399 LD_TEMP, i,
1400 PC87365_REG_TEMP_MAX);
1401 data->temp_crit[i] = pc87360_read_value(data,
1402 LD_TEMP, i,
1403 PC87365_REG_TEMP_CRIT);
1406 if (data->tempnr) {
1407 data->temp_alarms = pc87360_read_value(data, LD_TEMP,
1408 NO_BANK, PC87365_REG_TEMP_ALARMS)
1409 & 0x3F;
1412 data->last_updated = jiffies;
1413 data->valid = 1;
1416 mutex_unlock(&data->update_lock);
1418 return data;
1421 static int __init pc87360_init(void)
1423 int i;
1425 if (pc87360_find(0x2e, &devid, extra_isa)
1426 && pc87360_find(0x4e, &devid, extra_isa)) {
1427 printk(KERN_WARNING "pc87360: PC8736x not detected, "
1428 "module not inserted.\n");
1429 return -ENODEV;
1432 /* Arbitrarily pick one of the addresses */
1433 for (i = 0; i < 3; i++) {
1434 if (extra_isa[i] != 0x0000) {
1435 address = extra_isa[i];
1436 break;
1440 if (address == 0x0000) {
1441 printk(KERN_WARNING "pc87360: No active logical device, "
1442 "module not inserted.\n");
1443 return -ENODEV;
1446 return i2c_isa_add_driver(&pc87360_driver);
1449 static void __exit pc87360_exit(void)
1451 i2c_isa_del_driver(&pc87360_driver);
1455 MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
1456 MODULE_DESCRIPTION("PC8736x hardware monitor");
1457 MODULE_LICENSE("GPL");
1459 module_init(pc87360_init);
1460 module_exit(pc87360_exit);