mac80211: fix virtual interfaces vs. injection
[linux/fpc-iii.git] / drivers / hwmon / pc87360.c
blob9b462bb13fa33b3b9cc67d66e884628a7e2043ba
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");
62 static unsigned short force_id;
63 module_param(force_id, ushort, 0);
64 MODULE_PARM_DESC(force_id, "Override the detected device ID");
67 * Super-I/O registers and operations
70 #define DEV 0x07 /* Register: Logical device select */
71 #define DEVID 0x20 /* Register: Device ID */
72 #define ACT 0x30 /* Register: Device activation */
73 #define BASE 0x60 /* Register: Base address */
75 #define FSCM 0x09 /* Logical device: fans */
76 #define VLM 0x0d /* Logical device: voltages */
77 #define TMS 0x0e /* Logical device: temperatures */
78 static const u8 logdev[3] = { FSCM, VLM, TMS };
80 #define LD_FAN 0
81 #define LD_IN 1
82 #define LD_TEMP 2
84 static inline void superio_outb(int sioaddr, int reg, int val)
86 outb(reg, sioaddr);
87 outb(val, sioaddr+1);
90 static inline int superio_inb(int sioaddr, int reg)
92 outb(reg, sioaddr);
93 return inb(sioaddr+1);
96 static inline void superio_exit(int sioaddr)
98 outb(0x02, sioaddr);
99 outb(0x02, sioaddr+1);
103 * Logical devices
106 #define PC87360_EXTENT 0x10
107 #define PC87365_REG_BANK 0x09
108 #define NO_BANK 0xff
111 * Fan registers and conversions
114 /* nr has to be 0 or 1 (PC87360/87363) or 2 (PC87364/87365/87366) */
115 #define PC87360_REG_PRESCALE(nr) (0x00 + 2 * (nr))
116 #define PC87360_REG_PWM(nr) (0x01 + 2 * (nr))
117 #define PC87360_REG_FAN_MIN(nr) (0x06 + 3 * (nr))
118 #define PC87360_REG_FAN(nr) (0x07 + 3 * (nr))
119 #define PC87360_REG_FAN_STATUS(nr) (0x08 + 3 * (nr))
121 #define FAN_FROM_REG(val,div) ((val) == 0 ? 0: \
122 480000 / ((val)*(div)))
123 #define FAN_TO_REG(val,div) ((val) <= 100 ? 0 : \
124 480000 / ((val)*(div)))
125 #define FAN_DIV_FROM_REG(val) (1 << ((val >> 5) & 0x03))
126 #define FAN_STATUS_FROM_REG(val) ((val) & 0x07)
128 #define FAN_CONFIG_MONITOR(val,nr) (((val) >> (2 + nr * 3)) & 1)
129 #define FAN_CONFIG_CONTROL(val,nr) (((val) >> (3 + nr * 3)) & 1)
130 #define FAN_CONFIG_INVERT(val,nr) (((val) >> (4 + nr * 3)) & 1)
132 #define PWM_FROM_REG(val,inv) ((inv) ? 255 - (val) : (val))
133 static inline u8 PWM_TO_REG(int val, int inv)
135 if (inv)
136 val = 255 - val;
137 if (val < 0)
138 return 0;
139 if (val > 255)
140 return 255;
141 return val;
145 * Voltage registers and conversions
148 #define PC87365_REG_IN_CONVRATE 0x07
149 #define PC87365_REG_IN_CONFIG 0x08
150 #define PC87365_REG_IN 0x0B
151 #define PC87365_REG_IN_MIN 0x0D
152 #define PC87365_REG_IN_MAX 0x0C
153 #define PC87365_REG_IN_STATUS 0x0A
154 #define PC87365_REG_IN_ALARMS1 0x00
155 #define PC87365_REG_IN_ALARMS2 0x01
156 #define PC87365_REG_VID 0x06
158 #define IN_FROM_REG(val,ref) (((val) * (ref) + 128) / 256)
159 #define IN_TO_REG(val,ref) ((val) < 0 ? 0 : \
160 (val)*256 >= (ref)*255 ? 255: \
161 ((val) * 256 + (ref)/2) / (ref))
164 * Temperature registers and conversions
167 #define PC87365_REG_TEMP_CONFIG 0x08
168 #define PC87365_REG_TEMP 0x0B
169 #define PC87365_REG_TEMP_MIN 0x0D
170 #define PC87365_REG_TEMP_MAX 0x0C
171 #define PC87365_REG_TEMP_CRIT 0x0E
172 #define PC87365_REG_TEMP_STATUS 0x0A
173 #define PC87365_REG_TEMP_ALARMS 0x00
175 #define TEMP_FROM_REG(val) ((val) * 1000)
176 #define TEMP_TO_REG(val) ((val) < -55000 ? -55 : \
177 (val) > 127000 ? 127 : \
178 (val) < 0 ? ((val) - 500) / 1000 : \
179 ((val) + 500) / 1000)
182 * Device data
185 struct pc87360_data {
186 const char *name;
187 struct device *hwmon_dev;
188 struct mutex lock;
189 struct mutex update_lock;
190 char valid; /* !=0 if following fields are valid */
191 unsigned long last_updated; /* In jiffies */
193 int address[3];
195 u8 fannr, innr, tempnr;
197 u8 fan[3]; /* Register value */
198 u8 fan_min[3]; /* Register value */
199 u8 fan_status[3]; /* Register value */
200 u8 pwm[3]; /* Register value */
201 u16 fan_conf; /* Configuration register values, combined */
203 u16 in_vref; /* 1 mV/bit */
204 u8 in[14]; /* Register value */
205 u8 in_min[14]; /* Register value */
206 u8 in_max[14]; /* Register value */
207 u8 in_crit[3]; /* Register value */
208 u8 in_status[14]; /* Register value */
209 u16 in_alarms; /* Register values, combined, masked */
210 u8 vid_conf; /* Configuration register value */
211 u8 vrm;
212 u8 vid; /* Register value */
214 s8 temp[3]; /* Register value */
215 s8 temp_min[3]; /* Register value */
216 s8 temp_max[3]; /* Register value */
217 s8 temp_crit[3]; /* Register value */
218 u8 temp_status[3]; /* Register value */
219 u8 temp_alarms; /* Register value, masked */
223 * Functions declaration
226 static int pc87360_probe(struct platform_device *pdev);
227 static int __devexit pc87360_remove(struct platform_device *pdev);
229 static int pc87360_read_value(struct pc87360_data *data, u8 ldi, u8 bank,
230 u8 reg);
231 static void pc87360_write_value(struct pc87360_data *data, u8 ldi, u8 bank,
232 u8 reg, u8 value);
233 static void pc87360_init_device(struct platform_device *pdev,
234 int use_thermistors);
235 static struct pc87360_data *pc87360_update_device(struct device *dev);
238 * Driver data
241 static struct platform_driver pc87360_driver = {
242 .driver = {
243 .owner = THIS_MODULE,
244 .name = "pc87360",
246 .probe = pc87360_probe,
247 .remove = __devexit_p(pc87360_remove),
251 * Sysfs stuff
254 static ssize_t show_fan_input(struct device *dev, struct device_attribute *devattr, char *buf)
256 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
257 struct pc87360_data *data = pc87360_update_device(dev);
258 return sprintf(buf, "%u\n", FAN_FROM_REG(data->fan[attr->index],
259 FAN_DIV_FROM_REG(data->fan_status[attr->index])));
261 static ssize_t show_fan_min(struct device *dev, struct device_attribute *devattr, char *buf)
263 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
264 struct pc87360_data *data = pc87360_update_device(dev);
265 return sprintf(buf, "%u\n", FAN_FROM_REG(data->fan_min[attr->index],
266 FAN_DIV_FROM_REG(data->fan_status[attr->index])));
268 static ssize_t show_fan_div(struct device *dev, struct device_attribute *devattr, char *buf)
270 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
271 struct pc87360_data *data = pc87360_update_device(dev);
272 return sprintf(buf, "%u\n",
273 FAN_DIV_FROM_REG(data->fan_status[attr->index]));
275 static ssize_t show_fan_status(struct device *dev, struct device_attribute *devattr, char *buf)
277 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
278 struct pc87360_data *data = pc87360_update_device(dev);
279 return sprintf(buf, "%u\n",
280 FAN_STATUS_FROM_REG(data->fan_status[attr->index]));
282 static ssize_t set_fan_min(struct device *dev, struct device_attribute *devattr, const char *buf,
283 size_t count)
285 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
286 struct pc87360_data *data = dev_get_drvdata(dev);
287 long fan_min = simple_strtol(buf, NULL, 10);
289 mutex_lock(&data->update_lock);
290 fan_min = FAN_TO_REG(fan_min, FAN_DIV_FROM_REG(data->fan_status[attr->index]));
292 /* If it wouldn't fit, change clock divisor */
293 while (fan_min > 255
294 && (data->fan_status[attr->index] & 0x60) != 0x60) {
295 fan_min >>= 1;
296 data->fan[attr->index] >>= 1;
297 data->fan_status[attr->index] += 0x20;
299 data->fan_min[attr->index] = fan_min > 255 ? 255 : fan_min;
300 pc87360_write_value(data, LD_FAN, NO_BANK, PC87360_REG_FAN_MIN(attr->index),
301 data->fan_min[attr->index]);
303 /* Write new divider, preserve alarm bits */
304 pc87360_write_value(data, LD_FAN, NO_BANK, PC87360_REG_FAN_STATUS(attr->index),
305 data->fan_status[attr->index] & 0xF9);
306 mutex_unlock(&data->update_lock);
308 return count;
311 static struct sensor_device_attribute fan_input[] = {
312 SENSOR_ATTR(fan1_input, S_IRUGO, show_fan_input, NULL, 0),
313 SENSOR_ATTR(fan2_input, S_IRUGO, show_fan_input, NULL, 1),
314 SENSOR_ATTR(fan3_input, S_IRUGO, show_fan_input, NULL, 2),
316 static struct sensor_device_attribute fan_status[] = {
317 SENSOR_ATTR(fan1_status, S_IRUGO, show_fan_status, NULL, 0),
318 SENSOR_ATTR(fan2_status, S_IRUGO, show_fan_status, NULL, 1),
319 SENSOR_ATTR(fan3_status, S_IRUGO, show_fan_status, NULL, 2),
321 static struct sensor_device_attribute fan_div[] = {
322 SENSOR_ATTR(fan1_div, S_IRUGO, show_fan_div, NULL, 0),
323 SENSOR_ATTR(fan2_div, S_IRUGO, show_fan_div, NULL, 1),
324 SENSOR_ATTR(fan3_div, S_IRUGO, show_fan_div, NULL, 2),
326 static struct sensor_device_attribute fan_min[] = {
327 SENSOR_ATTR(fan1_min, S_IWUSR | S_IRUGO, show_fan_min, set_fan_min, 0),
328 SENSOR_ATTR(fan2_min, S_IWUSR | S_IRUGO, show_fan_min, set_fan_min, 1),
329 SENSOR_ATTR(fan3_min, S_IWUSR | S_IRUGO, show_fan_min, set_fan_min, 2),
332 #define FAN_UNIT_ATTRS(X) \
333 &fan_input[X].dev_attr.attr, \
334 &fan_status[X].dev_attr.attr, \
335 &fan_div[X].dev_attr.attr, \
336 &fan_min[X].dev_attr.attr
338 static ssize_t show_pwm(struct device *dev, struct device_attribute *devattr, char *buf)
340 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
341 struct pc87360_data *data = pc87360_update_device(dev);
342 return sprintf(buf, "%u\n",
343 PWM_FROM_REG(data->pwm[attr->index],
344 FAN_CONFIG_INVERT(data->fan_conf,
345 attr->index)));
347 static ssize_t set_pwm(struct device *dev, struct device_attribute *devattr, const char *buf,
348 size_t count)
350 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
351 struct pc87360_data *data = dev_get_drvdata(dev);
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 pc87360_data *data = dev_get_drvdata(dev);
414 long val = simple_strtol(buf, NULL, 10);
416 mutex_lock(&data->update_lock);
417 data->in_min[attr->index] = IN_TO_REG(val, data->in_vref);
418 pc87360_write_value(data, LD_IN, attr->index, PC87365_REG_IN_MIN,
419 data->in_min[attr->index]);
420 mutex_unlock(&data->update_lock);
421 return count;
423 static ssize_t set_in_max(struct device *dev, struct device_attribute *devattr, const char *buf,
424 size_t count)
426 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
427 struct pc87360_data *data = dev_get_drvdata(dev);
428 long val = simple_strtol(buf, NULL, 10);
430 mutex_lock(&data->update_lock);
431 data->in_max[attr->index] = IN_TO_REG(val,
432 data->in_vref);
433 pc87360_write_value(data, LD_IN, attr->index, PC87365_REG_IN_MAX,
434 data->in_max[attr->index]);
435 mutex_unlock(&data->update_lock);
436 return count;
439 static struct sensor_device_attribute in_input[] = {
440 SENSOR_ATTR(in0_input, S_IRUGO, show_in_input, NULL, 0),
441 SENSOR_ATTR(in1_input, S_IRUGO, show_in_input, NULL, 1),
442 SENSOR_ATTR(in2_input, S_IRUGO, show_in_input, NULL, 2),
443 SENSOR_ATTR(in3_input, S_IRUGO, show_in_input, NULL, 3),
444 SENSOR_ATTR(in4_input, S_IRUGO, show_in_input, NULL, 4),
445 SENSOR_ATTR(in5_input, S_IRUGO, show_in_input, NULL, 5),
446 SENSOR_ATTR(in6_input, S_IRUGO, show_in_input, NULL, 6),
447 SENSOR_ATTR(in7_input, S_IRUGO, show_in_input, NULL, 7),
448 SENSOR_ATTR(in8_input, S_IRUGO, show_in_input, NULL, 8),
449 SENSOR_ATTR(in9_input, S_IRUGO, show_in_input, NULL, 9),
450 SENSOR_ATTR(in10_input, S_IRUGO, show_in_input, NULL, 10),
452 static struct sensor_device_attribute in_status[] = {
453 SENSOR_ATTR(in0_status, S_IRUGO, show_in_status, NULL, 0),
454 SENSOR_ATTR(in1_status, S_IRUGO, show_in_status, NULL, 1),
455 SENSOR_ATTR(in2_status, S_IRUGO, show_in_status, NULL, 2),
456 SENSOR_ATTR(in3_status, S_IRUGO, show_in_status, NULL, 3),
457 SENSOR_ATTR(in4_status, S_IRUGO, show_in_status, NULL, 4),
458 SENSOR_ATTR(in5_status, S_IRUGO, show_in_status, NULL, 5),
459 SENSOR_ATTR(in6_status, S_IRUGO, show_in_status, NULL, 6),
460 SENSOR_ATTR(in7_status, S_IRUGO, show_in_status, NULL, 7),
461 SENSOR_ATTR(in8_status, S_IRUGO, show_in_status, NULL, 8),
462 SENSOR_ATTR(in9_status, S_IRUGO, show_in_status, NULL, 9),
463 SENSOR_ATTR(in10_status, S_IRUGO, show_in_status, NULL, 10),
465 static struct sensor_device_attribute in_min[] = {
466 SENSOR_ATTR(in0_min, S_IWUSR | S_IRUGO, show_in_min, set_in_min, 0),
467 SENSOR_ATTR(in1_min, S_IWUSR | S_IRUGO, show_in_min, set_in_min, 1),
468 SENSOR_ATTR(in2_min, S_IWUSR | S_IRUGO, show_in_min, set_in_min, 2),
469 SENSOR_ATTR(in3_min, S_IWUSR | S_IRUGO, show_in_min, set_in_min, 3),
470 SENSOR_ATTR(in4_min, S_IWUSR | S_IRUGO, show_in_min, set_in_min, 4),
471 SENSOR_ATTR(in5_min, S_IWUSR | S_IRUGO, show_in_min, set_in_min, 5),
472 SENSOR_ATTR(in6_min, S_IWUSR | S_IRUGO, show_in_min, set_in_min, 6),
473 SENSOR_ATTR(in7_min, S_IWUSR | S_IRUGO, show_in_min, set_in_min, 7),
474 SENSOR_ATTR(in8_min, S_IWUSR | S_IRUGO, show_in_min, set_in_min, 8),
475 SENSOR_ATTR(in9_min, S_IWUSR | S_IRUGO, show_in_min, set_in_min, 9),
476 SENSOR_ATTR(in10_min, S_IWUSR | S_IRUGO, show_in_min, set_in_min, 10),
478 static struct sensor_device_attribute in_max[] = {
479 SENSOR_ATTR(in0_max, S_IWUSR | S_IRUGO, show_in_max, set_in_max, 0),
480 SENSOR_ATTR(in1_max, S_IWUSR | S_IRUGO, show_in_max, set_in_max, 1),
481 SENSOR_ATTR(in2_max, S_IWUSR | S_IRUGO, show_in_max, set_in_max, 2),
482 SENSOR_ATTR(in3_max, S_IWUSR | S_IRUGO, show_in_max, set_in_max, 3),
483 SENSOR_ATTR(in4_max, S_IWUSR | S_IRUGO, show_in_max, set_in_max, 4),
484 SENSOR_ATTR(in5_max, S_IWUSR | S_IRUGO, show_in_max, set_in_max, 5),
485 SENSOR_ATTR(in6_max, S_IWUSR | S_IRUGO, show_in_max, set_in_max, 6),
486 SENSOR_ATTR(in7_max, S_IWUSR | S_IRUGO, show_in_max, set_in_max, 7),
487 SENSOR_ATTR(in8_max, S_IWUSR | S_IRUGO, show_in_max, set_in_max, 8),
488 SENSOR_ATTR(in9_max, S_IWUSR | S_IRUGO, show_in_max, set_in_max, 9),
489 SENSOR_ATTR(in10_max, S_IWUSR | S_IRUGO, show_in_max, set_in_max, 10),
492 #define VIN_UNIT_ATTRS(X) \
493 &in_input[X].dev_attr.attr, \
494 &in_status[X].dev_attr.attr, \
495 &in_min[X].dev_attr.attr, \
496 &in_max[X].dev_attr.attr
498 static ssize_t show_vid(struct device *dev, struct device_attribute *attr, char *buf)
500 struct pc87360_data *data = pc87360_update_device(dev);
501 return sprintf(buf, "%u\n", vid_from_reg(data->vid, data->vrm));
503 static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL);
505 static ssize_t show_vrm(struct device *dev, struct device_attribute *attr, char *buf)
507 struct pc87360_data *data = dev_get_drvdata(dev);
508 return sprintf(buf, "%u\n", data->vrm);
510 static ssize_t set_vrm(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
512 struct pc87360_data *data = dev_get_drvdata(dev);
513 data->vrm = simple_strtoul(buf, NULL, 10);
514 return count;
516 static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm, set_vrm);
518 static ssize_t show_in_alarms(struct device *dev, struct device_attribute *attr, char *buf)
520 struct pc87360_data *data = pc87360_update_device(dev);
521 return sprintf(buf, "%u\n", data->in_alarms);
523 static DEVICE_ATTR(alarms_in, S_IRUGO, show_in_alarms, NULL);
525 static struct attribute *pc8736x_vin_attr_array[] = {
526 VIN_UNIT_ATTRS(0),
527 VIN_UNIT_ATTRS(1),
528 VIN_UNIT_ATTRS(2),
529 VIN_UNIT_ATTRS(3),
530 VIN_UNIT_ATTRS(4),
531 VIN_UNIT_ATTRS(5),
532 VIN_UNIT_ATTRS(6),
533 VIN_UNIT_ATTRS(7),
534 VIN_UNIT_ATTRS(8),
535 VIN_UNIT_ATTRS(9),
536 VIN_UNIT_ATTRS(10),
537 &dev_attr_cpu0_vid.attr,
538 &dev_attr_vrm.attr,
539 &dev_attr_alarms_in.attr,
540 NULL
542 static const struct attribute_group pc8736x_vin_group = {
543 .attrs = pc8736x_vin_attr_array,
546 static ssize_t show_therm_input(struct device *dev, struct device_attribute *devattr, char *buf)
548 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
549 struct pc87360_data *data = pc87360_update_device(dev);
550 return sprintf(buf, "%u\n", IN_FROM_REG(data->in[attr->index],
551 data->in_vref));
553 static ssize_t show_therm_min(struct device *dev, struct device_attribute *devattr, char *buf)
555 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
556 struct pc87360_data *data = pc87360_update_device(dev);
557 return sprintf(buf, "%u\n", IN_FROM_REG(data->in_min[attr->index],
558 data->in_vref));
560 static ssize_t show_therm_max(struct device *dev, struct device_attribute *devattr, char *buf)
562 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
563 struct pc87360_data *data = pc87360_update_device(dev);
564 return sprintf(buf, "%u\n", IN_FROM_REG(data->in_max[attr->index],
565 data->in_vref));
567 static ssize_t show_therm_crit(struct device *dev, struct device_attribute *devattr, char *buf)
569 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
570 struct pc87360_data *data = pc87360_update_device(dev);
571 return sprintf(buf, "%u\n", IN_FROM_REG(data->in_crit[attr->index-11],
572 data->in_vref));
574 static ssize_t show_therm_status(struct device *dev, struct device_attribute *devattr, char *buf)
576 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
577 struct pc87360_data *data = pc87360_update_device(dev);
578 return sprintf(buf, "%u\n", data->in_status[attr->index]);
580 static ssize_t set_therm_min(struct device *dev, struct device_attribute *devattr, const char *buf,
581 size_t count)
583 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
584 struct pc87360_data *data = dev_get_drvdata(dev);
585 long val = simple_strtol(buf, NULL, 10);
587 mutex_lock(&data->update_lock);
588 data->in_min[attr->index] = IN_TO_REG(val, data->in_vref);
589 pc87360_write_value(data, LD_IN, attr->index, PC87365_REG_TEMP_MIN,
590 data->in_min[attr->index]);
591 mutex_unlock(&data->update_lock);
592 return count;
594 static ssize_t set_therm_max(struct device *dev, struct device_attribute *devattr, const char *buf,
595 size_t count)
597 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
598 struct pc87360_data *data = dev_get_drvdata(dev);
599 long val = simple_strtol(buf, NULL, 10);
601 mutex_lock(&data->update_lock);
602 data->in_max[attr->index] = IN_TO_REG(val, data->in_vref);
603 pc87360_write_value(data, LD_IN, attr->index, PC87365_REG_TEMP_MAX,
604 data->in_max[attr->index]);
605 mutex_unlock(&data->update_lock);
606 return count;
608 static ssize_t set_therm_crit(struct device *dev, struct device_attribute *devattr, const char *buf,
609 size_t count)
611 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
612 struct pc87360_data *data = dev_get_drvdata(dev);
613 long val = simple_strtol(buf, NULL, 10);
615 mutex_lock(&data->update_lock);
616 data->in_crit[attr->index-11] = IN_TO_REG(val, data->in_vref);
617 pc87360_write_value(data, LD_IN, attr->index, PC87365_REG_TEMP_CRIT,
618 data->in_crit[attr->index-11]);
619 mutex_unlock(&data->update_lock);
620 return count;
623 /* the +11 term below reflects the fact that VLM units 11,12,13 are
624 used in the chip to measure voltage across the thermistors
626 static struct sensor_device_attribute therm_input[] = {
627 SENSOR_ATTR(temp4_input, S_IRUGO, show_therm_input, NULL, 0+11),
628 SENSOR_ATTR(temp5_input, S_IRUGO, show_therm_input, NULL, 1+11),
629 SENSOR_ATTR(temp6_input, S_IRUGO, show_therm_input, NULL, 2+11),
631 static struct sensor_device_attribute therm_status[] = {
632 SENSOR_ATTR(temp4_status, S_IRUGO, show_therm_status, NULL, 0+11),
633 SENSOR_ATTR(temp5_status, S_IRUGO, show_therm_status, NULL, 1+11),
634 SENSOR_ATTR(temp6_status, S_IRUGO, show_therm_status, NULL, 2+11),
636 static struct sensor_device_attribute therm_min[] = {
637 SENSOR_ATTR(temp4_min, S_IRUGO | S_IWUSR,
638 show_therm_min, set_therm_min, 0+11),
639 SENSOR_ATTR(temp5_min, S_IRUGO | S_IWUSR,
640 show_therm_min, set_therm_min, 1+11),
641 SENSOR_ATTR(temp6_min, S_IRUGO | S_IWUSR,
642 show_therm_min, set_therm_min, 2+11),
644 static struct sensor_device_attribute therm_max[] = {
645 SENSOR_ATTR(temp4_max, S_IRUGO | S_IWUSR,
646 show_therm_max, set_therm_max, 0+11),
647 SENSOR_ATTR(temp5_max, S_IRUGO | S_IWUSR,
648 show_therm_max, set_therm_max, 1+11),
649 SENSOR_ATTR(temp6_max, S_IRUGO | S_IWUSR,
650 show_therm_max, set_therm_max, 2+11),
652 static struct sensor_device_attribute therm_crit[] = {
653 SENSOR_ATTR(temp4_crit, S_IRUGO | S_IWUSR,
654 show_therm_crit, set_therm_crit, 0+11),
655 SENSOR_ATTR(temp5_crit, S_IRUGO | S_IWUSR,
656 show_therm_crit, set_therm_crit, 1+11),
657 SENSOR_ATTR(temp6_crit, S_IRUGO | S_IWUSR,
658 show_therm_crit, set_therm_crit, 2+11),
661 #define THERM_UNIT_ATTRS(X) \
662 &therm_input[X].dev_attr.attr, \
663 &therm_status[X].dev_attr.attr, \
664 &therm_min[X].dev_attr.attr, \
665 &therm_max[X].dev_attr.attr, \
666 &therm_crit[X].dev_attr.attr
668 static struct attribute * pc8736x_therm_attr_array[] = {
669 THERM_UNIT_ATTRS(0),
670 THERM_UNIT_ATTRS(1),
671 THERM_UNIT_ATTRS(2),
672 NULL
674 static const struct attribute_group pc8736x_therm_group = {
675 .attrs = pc8736x_therm_attr_array,
678 static ssize_t show_temp_input(struct device *dev, struct device_attribute *devattr, char *buf)
680 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
681 struct pc87360_data *data = pc87360_update_device(dev);
682 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[attr->index]));
684 static ssize_t show_temp_min(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_min[attr->index]));
690 static ssize_t show_temp_max(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_max[attr->index]));
696 static ssize_t show_temp_crit(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_crit[attr->index]));
702 static ssize_t show_temp_status(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", data->temp_status[attr->index]);
708 static ssize_t set_temp_min(struct device *dev, struct device_attribute *devattr, const char *buf,
709 size_t count)
711 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
712 struct pc87360_data *data = dev_get_drvdata(dev);
713 long val = simple_strtol(buf, NULL, 10);
715 mutex_lock(&data->update_lock);
716 data->temp_min[attr->index] = TEMP_TO_REG(val);
717 pc87360_write_value(data, LD_TEMP, attr->index, PC87365_REG_TEMP_MIN,
718 data->temp_min[attr->index]);
719 mutex_unlock(&data->update_lock);
720 return count;
722 static ssize_t set_temp_max(struct device *dev, struct device_attribute *devattr, const char *buf,
723 size_t count)
725 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
726 struct pc87360_data *data = dev_get_drvdata(dev);
727 long val = simple_strtol(buf, NULL, 10);
729 mutex_lock(&data->update_lock);
730 data->temp_max[attr->index] = TEMP_TO_REG(val);
731 pc87360_write_value(data, LD_TEMP, attr->index, PC87365_REG_TEMP_MAX,
732 data->temp_max[attr->index]);
733 mutex_unlock(&data->update_lock);
734 return count;
736 static ssize_t set_temp_crit(struct device *dev, struct device_attribute *devattr, const char *buf,
737 size_t count)
739 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
740 struct pc87360_data *data = dev_get_drvdata(dev);
741 long val = simple_strtol(buf, NULL, 10);
743 mutex_lock(&data->update_lock);
744 data->temp_crit[attr->index] = TEMP_TO_REG(val);
745 pc87360_write_value(data, LD_TEMP, attr->index, PC87365_REG_TEMP_CRIT,
746 data->temp_crit[attr->index]);
747 mutex_unlock(&data->update_lock);
748 return count;
751 static struct sensor_device_attribute temp_input[] = {
752 SENSOR_ATTR(temp1_input, S_IRUGO, show_temp_input, NULL, 0),
753 SENSOR_ATTR(temp2_input, S_IRUGO, show_temp_input, NULL, 1),
754 SENSOR_ATTR(temp3_input, S_IRUGO, show_temp_input, NULL, 2),
756 static struct sensor_device_attribute temp_status[] = {
757 SENSOR_ATTR(temp1_status, S_IRUGO, show_temp_status, NULL, 0),
758 SENSOR_ATTR(temp2_status, S_IRUGO, show_temp_status, NULL, 1),
759 SENSOR_ATTR(temp3_status, S_IRUGO, show_temp_status, NULL, 2),
761 static struct sensor_device_attribute temp_min[] = {
762 SENSOR_ATTR(temp1_min, S_IRUGO | S_IWUSR,
763 show_temp_min, set_temp_min, 0),
764 SENSOR_ATTR(temp2_min, S_IRUGO | S_IWUSR,
765 show_temp_min, set_temp_min, 1),
766 SENSOR_ATTR(temp3_min, S_IRUGO | S_IWUSR,
767 show_temp_min, set_temp_min, 2),
769 static struct sensor_device_attribute temp_max[] = {
770 SENSOR_ATTR(temp1_max, S_IRUGO | S_IWUSR,
771 show_temp_max, set_temp_max, 0),
772 SENSOR_ATTR(temp2_max, S_IRUGO | S_IWUSR,
773 show_temp_max, set_temp_max, 1),
774 SENSOR_ATTR(temp3_max, S_IRUGO | S_IWUSR,
775 show_temp_max, set_temp_max, 2),
777 static struct sensor_device_attribute temp_crit[] = {
778 SENSOR_ATTR(temp1_crit, S_IRUGO | S_IWUSR,
779 show_temp_crit, set_temp_crit, 0),
780 SENSOR_ATTR(temp2_crit, S_IRUGO | S_IWUSR,
781 show_temp_crit, set_temp_crit, 1),
782 SENSOR_ATTR(temp3_crit, S_IRUGO | S_IWUSR,
783 show_temp_crit, set_temp_crit, 2),
786 static ssize_t show_temp_alarms(struct device *dev, struct device_attribute *attr, char *buf)
788 struct pc87360_data *data = pc87360_update_device(dev);
789 return sprintf(buf, "%u\n", data->temp_alarms);
791 static DEVICE_ATTR(alarms_temp, S_IRUGO, show_temp_alarms, NULL);
793 #define TEMP_UNIT_ATTRS(X) \
794 &temp_input[X].dev_attr.attr, \
795 &temp_status[X].dev_attr.attr, \
796 &temp_min[X].dev_attr.attr, \
797 &temp_max[X].dev_attr.attr, \
798 &temp_crit[X].dev_attr.attr
800 static struct attribute * pc8736x_temp_attr_array[] = {
801 TEMP_UNIT_ATTRS(0),
802 TEMP_UNIT_ATTRS(1),
803 TEMP_UNIT_ATTRS(2),
804 /* include the few miscellaneous atts here */
805 &dev_attr_alarms_temp.attr,
806 NULL
808 static const struct attribute_group pc8736x_temp_group = {
809 .attrs = pc8736x_temp_attr_array,
812 static ssize_t show_name(struct device *dev, struct device_attribute
813 *devattr, char *buf)
815 struct pc87360_data *data = dev_get_drvdata(dev);
816 return sprintf(buf, "%s\n", data->name);
818 static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
821 * Device detection, registration and update
824 static int __init pc87360_find(int sioaddr, u8 *devid, unsigned short *addresses)
826 u16 val;
827 int i;
828 int nrdev; /* logical device count */
830 /* No superio_enter */
832 /* Identify device */
833 val = force_id ? force_id : superio_inb(sioaddr, DEVID);
834 switch (val) {
835 case 0xE1: /* PC87360 */
836 case 0xE8: /* PC87363 */
837 case 0xE4: /* PC87364 */
838 nrdev = 1;
839 break;
840 case 0xE5: /* PC87365 */
841 case 0xE9: /* PC87366 */
842 nrdev = 3;
843 break;
844 default:
845 superio_exit(sioaddr);
846 return -ENODEV;
848 /* Remember the device id */
849 *devid = val;
851 for (i = 0; i < nrdev; i++) {
852 /* select logical device */
853 superio_outb(sioaddr, DEV, logdev[i]);
855 val = superio_inb(sioaddr, ACT);
856 if (!(val & 0x01)) {
857 printk(KERN_INFO "pc87360: Device 0x%02x not "
858 "activated\n", logdev[i]);
859 continue;
862 val = (superio_inb(sioaddr, BASE) << 8)
863 | superio_inb(sioaddr, BASE + 1);
864 if (!val) {
865 printk(KERN_INFO "pc87360: Base address not set for "
866 "device 0x%02x\n", logdev[i]);
867 continue;
870 addresses[i] = val;
872 if (i==0) { /* Fans */
873 confreg[0] = superio_inb(sioaddr, 0xF0);
874 confreg[1] = superio_inb(sioaddr, 0xF1);
876 #ifdef DEBUG
877 printk(KERN_DEBUG "pc87360: Fan 1: mon=%d "
878 "ctrl=%d inv=%d\n", (confreg[0]>>2)&1,
879 (confreg[0]>>3)&1, (confreg[0]>>4)&1);
880 printk(KERN_DEBUG "pc87360: Fan 2: mon=%d "
881 "ctrl=%d inv=%d\n", (confreg[0]>>5)&1,
882 (confreg[0]>>6)&1, (confreg[0]>>7)&1);
883 printk(KERN_DEBUG "pc87360: Fan 3: mon=%d "
884 "ctrl=%d inv=%d\n", confreg[1]&1,
885 (confreg[1]>>1)&1, (confreg[1]>>2)&1);
886 #endif
887 } else if (i==1) { /* Voltages */
888 /* Are we using thermistors? */
889 if (*devid == 0xE9) { /* PC87366 */
890 /* These registers are not logical-device
891 specific, just that we won't need them if
892 we don't use the VLM device */
893 confreg[2] = superio_inb(sioaddr, 0x2B);
894 confreg[3] = superio_inb(sioaddr, 0x25);
896 if (confreg[2] & 0x40) {
897 printk(KERN_INFO "pc87360: Using "
898 "thermistors for temperature "
899 "monitoring\n");
901 if (confreg[3] & 0xE0) {
902 printk(KERN_INFO "pc87360: VID "
903 "inputs routed (mode %u)\n",
904 confreg[3] >> 5);
910 superio_exit(sioaddr);
911 return 0;
914 static int __devinit pc87360_probe(struct platform_device *pdev)
916 int i;
917 struct pc87360_data *data;
918 int err = 0;
919 const char *name = "pc87360";
920 int use_thermistors = 0;
921 struct device *dev = &pdev->dev;
923 if (!(data = kzalloc(sizeof(struct pc87360_data), GFP_KERNEL)))
924 return -ENOMEM;
926 data->fannr = 2;
927 data->innr = 0;
928 data->tempnr = 0;
930 switch (devid) {
931 case 0xe8:
932 name = "pc87363";
933 break;
934 case 0xe4:
935 name = "pc87364";
936 data->fannr = 3;
937 break;
938 case 0xe5:
939 name = "pc87365";
940 data->fannr = extra_isa[0] ? 3 : 0;
941 data->innr = extra_isa[1] ? 11 : 0;
942 data->tempnr = extra_isa[2] ? 2 : 0;
943 break;
944 case 0xe9:
945 name = "pc87366";
946 data->fannr = extra_isa[0] ? 3 : 0;
947 data->innr = extra_isa[1] ? 14 : 0;
948 data->tempnr = extra_isa[2] ? 3 : 0;
949 break;
952 data->name = name;
953 data->valid = 0;
954 mutex_init(&data->lock);
955 mutex_init(&data->update_lock);
956 platform_set_drvdata(pdev, data);
958 for (i = 0; i < 3; i++) {
959 if (((data->address[i] = extra_isa[i]))
960 && !request_region(extra_isa[i], PC87360_EXTENT,
961 pc87360_driver.driver.name)) {
962 dev_err(dev, "Region 0x%x-0x%x already "
963 "in use!\n", extra_isa[i],
964 extra_isa[i]+PC87360_EXTENT-1);
965 for (i--; i >= 0; i--)
966 release_region(extra_isa[i], PC87360_EXTENT);
967 err = -EBUSY;
968 goto ERROR1;
972 /* Retrieve the fans configuration from Super-I/O space */
973 if (data->fannr)
974 data->fan_conf = confreg[0] | (confreg[1] << 8);
976 /* Use the correct reference voltage
977 Unless both the VLM and the TMS logical devices agree to
978 use an external Vref, the internal one is used. */
979 if (data->innr) {
980 i = pc87360_read_value(data, LD_IN, NO_BANK,
981 PC87365_REG_IN_CONFIG);
982 if (data->tempnr) {
983 i &= pc87360_read_value(data, LD_TEMP, NO_BANK,
984 PC87365_REG_TEMP_CONFIG);
986 data->in_vref = (i&0x02) ? 3025 : 2966;
987 dev_dbg(dev, "Using %s reference voltage\n",
988 (i&0x02) ? "external" : "internal");
990 data->vid_conf = confreg[3];
991 data->vrm = vid_which_vrm();
994 /* Fan clock dividers may be needed before any data is read */
995 for (i = 0; i < data->fannr; i++) {
996 if (FAN_CONFIG_MONITOR(data->fan_conf, i))
997 data->fan_status[i] = pc87360_read_value(data,
998 LD_FAN, NO_BANK,
999 PC87360_REG_FAN_STATUS(i));
1002 if (init > 0) {
1003 if (devid == 0xe9 && data->address[1]) /* PC87366 */
1004 use_thermistors = confreg[2] & 0x40;
1006 pc87360_init_device(pdev, use_thermistors);
1009 /* Register all-or-nothing sysfs groups */
1011 if (data->innr &&
1012 (err = sysfs_create_group(&dev->kobj,
1013 &pc8736x_vin_group)))
1014 goto ERROR3;
1016 if (data->innr == 14 &&
1017 (err = sysfs_create_group(&dev->kobj,
1018 &pc8736x_therm_group)))
1019 goto ERROR3;
1021 /* create device attr-files for varying sysfs groups */
1023 if (data->tempnr) {
1024 for (i = 0; i < data->tempnr; i++) {
1025 if ((err = device_create_file(dev,
1026 &temp_input[i].dev_attr))
1027 || (err = device_create_file(dev,
1028 &temp_min[i].dev_attr))
1029 || (err = device_create_file(dev,
1030 &temp_max[i].dev_attr))
1031 || (err = device_create_file(dev,
1032 &temp_crit[i].dev_attr))
1033 || (err = device_create_file(dev,
1034 &temp_status[i].dev_attr)))
1035 goto ERROR3;
1037 if ((err = device_create_file(dev, &dev_attr_alarms_temp)))
1038 goto ERROR3;
1041 for (i = 0; i < data->fannr; i++) {
1042 if (FAN_CONFIG_MONITOR(data->fan_conf, i)
1043 && ((err = device_create_file(dev,
1044 &fan_input[i].dev_attr))
1045 || (err = device_create_file(dev,
1046 &fan_min[i].dev_attr))
1047 || (err = device_create_file(dev,
1048 &fan_div[i].dev_attr))
1049 || (err = device_create_file(dev,
1050 &fan_status[i].dev_attr))))
1051 goto ERROR3;
1053 if (FAN_CONFIG_CONTROL(data->fan_conf, i)
1054 && (err = device_create_file(dev, &pwm[i].dev_attr)))
1055 goto ERROR3;
1058 if ((err = device_create_file(dev, &dev_attr_name)))
1059 goto ERROR3;
1061 data->hwmon_dev = hwmon_device_register(dev);
1062 if (IS_ERR(data->hwmon_dev)) {
1063 err = PTR_ERR(data->hwmon_dev);
1064 goto ERROR3;
1066 return 0;
1068 ERROR3:
1069 device_remove_file(dev, &dev_attr_name);
1070 /* can still remove groups whose members were added individually */
1071 sysfs_remove_group(&dev->kobj, &pc8736x_temp_group);
1072 sysfs_remove_group(&dev->kobj, &pc8736x_fan_group);
1073 sysfs_remove_group(&dev->kobj, &pc8736x_therm_group);
1074 sysfs_remove_group(&dev->kobj, &pc8736x_vin_group);
1075 for (i = 0; i < 3; i++) {
1076 if (data->address[i]) {
1077 release_region(data->address[i], PC87360_EXTENT);
1080 ERROR1:
1081 kfree(data);
1082 return err;
1085 static int __devexit pc87360_remove(struct platform_device *pdev)
1087 struct pc87360_data *data = platform_get_drvdata(pdev);
1088 int i;
1090 hwmon_device_unregister(data->hwmon_dev);
1092 device_remove_file(&pdev->dev, &dev_attr_name);
1093 sysfs_remove_group(&pdev->dev.kobj, &pc8736x_temp_group);
1094 sysfs_remove_group(&pdev->dev.kobj, &pc8736x_fan_group);
1095 sysfs_remove_group(&pdev->dev.kobj, &pc8736x_therm_group);
1096 sysfs_remove_group(&pdev->dev.kobj, &pc8736x_vin_group);
1098 for (i = 0; i < 3; i++) {
1099 if (data->address[i]) {
1100 release_region(data->address[i], PC87360_EXTENT);
1103 kfree(data);
1105 return 0;
1108 /* ldi is the logical device index
1109 bank is for voltages and temperatures only */
1110 static int pc87360_read_value(struct pc87360_data *data, u8 ldi, u8 bank,
1111 u8 reg)
1113 int res;
1115 mutex_lock(&(data->lock));
1116 if (bank != NO_BANK)
1117 outb_p(bank, data->address[ldi] + PC87365_REG_BANK);
1118 res = inb_p(data->address[ldi] + reg);
1119 mutex_unlock(&(data->lock));
1121 return res;
1124 static void pc87360_write_value(struct pc87360_data *data, u8 ldi, u8 bank,
1125 u8 reg, u8 value)
1127 mutex_lock(&(data->lock));
1128 if (bank != NO_BANK)
1129 outb_p(bank, data->address[ldi] + PC87365_REG_BANK);
1130 outb_p(value, data->address[ldi] + reg);
1131 mutex_unlock(&(data->lock));
1134 static void pc87360_init_device(struct platform_device *pdev,
1135 int use_thermistors)
1137 struct pc87360_data *data = platform_get_drvdata(pdev);
1138 int i, nr;
1139 const u8 init_in[14] = { 2, 2, 2, 2, 2, 2, 2, 1, 1, 3, 1, 2, 2, 2 };
1140 const u8 init_temp[3] = { 2, 2, 1 };
1141 u8 reg;
1143 if (init >= 2 && data->innr) {
1144 reg = pc87360_read_value(data, LD_IN, NO_BANK,
1145 PC87365_REG_IN_CONVRATE);
1146 dev_info(&pdev->dev, "VLM conversion set to "
1147 "1s period, 160us delay\n");
1148 pc87360_write_value(data, LD_IN, NO_BANK,
1149 PC87365_REG_IN_CONVRATE,
1150 (reg & 0xC0) | 0x11);
1153 nr = data->innr < 11 ? data->innr : 11;
1154 for (i = 0; i < nr; i++) {
1155 if (init >= init_in[i]) {
1156 /* Forcibly enable voltage channel */
1157 reg = pc87360_read_value(data, LD_IN, i,
1158 PC87365_REG_IN_STATUS);
1159 if (!(reg & 0x01)) {
1160 dev_dbg(&pdev->dev, "Forcibly "
1161 "enabling in%d\n", i);
1162 pc87360_write_value(data, LD_IN, i,
1163 PC87365_REG_IN_STATUS,
1164 (reg & 0x68) | 0x87);
1169 /* We can't blindly trust the Super-I/O space configuration bit,
1170 most BIOS won't set it properly */
1171 for (i = 11; i < data->innr; i++) {
1172 reg = pc87360_read_value(data, LD_IN, i,
1173 PC87365_REG_TEMP_STATUS);
1174 use_thermistors = use_thermistors || (reg & 0x01);
1177 i = use_thermistors ? 2 : 0;
1178 for (; i < data->tempnr; i++) {
1179 if (init >= init_temp[i]) {
1180 /* Forcibly enable temperature channel */
1181 reg = pc87360_read_value(data, LD_TEMP, i,
1182 PC87365_REG_TEMP_STATUS);
1183 if (!(reg & 0x01)) {
1184 dev_dbg(&pdev->dev, "Forcibly "
1185 "enabling temp%d\n", i+1);
1186 pc87360_write_value(data, LD_TEMP, i,
1187 PC87365_REG_TEMP_STATUS,
1188 0xCF);
1193 if (use_thermistors) {
1194 for (i = 11; i < data->innr; i++) {
1195 if (init >= init_in[i]) {
1196 /* The pin may already be used by thermal
1197 diodes */
1198 reg = pc87360_read_value(data, LD_TEMP,
1199 (i-11)/2, PC87365_REG_TEMP_STATUS);
1200 if (reg & 0x01) {
1201 dev_dbg(&pdev->dev, "Skipping "
1202 "temp%d, pin already in use "
1203 "by temp%d\n", i-7, (i-11)/2);
1204 continue;
1207 /* Forcibly enable thermistor channel */
1208 reg = pc87360_read_value(data, LD_IN, i,
1209 PC87365_REG_IN_STATUS);
1210 if (!(reg & 0x01)) {
1211 dev_dbg(&pdev->dev, "Forcibly "
1212 "enabling temp%d\n", i-7);
1213 pc87360_write_value(data, LD_IN, i,
1214 PC87365_REG_TEMP_STATUS,
1215 (reg & 0x60) | 0x8F);
1221 if (data->innr) {
1222 reg = pc87360_read_value(data, LD_IN, NO_BANK,
1223 PC87365_REG_IN_CONFIG);
1224 if (reg & 0x01) {
1225 dev_dbg(&pdev->dev, "Forcibly "
1226 "enabling monitoring (VLM)\n");
1227 pc87360_write_value(data, LD_IN, NO_BANK,
1228 PC87365_REG_IN_CONFIG,
1229 reg & 0xFE);
1233 if (data->tempnr) {
1234 reg = pc87360_read_value(data, LD_TEMP, NO_BANK,
1235 PC87365_REG_TEMP_CONFIG);
1236 if (reg & 0x01) {
1237 dev_dbg(&pdev->dev, "Forcibly enabling "
1238 "monitoring (TMS)\n");
1239 pc87360_write_value(data, LD_TEMP, NO_BANK,
1240 PC87365_REG_TEMP_CONFIG,
1241 reg & 0xFE);
1244 if (init >= 2) {
1245 /* Chip config as documented by National Semi. */
1246 pc87360_write_value(data, LD_TEMP, 0xF, 0xA, 0x08);
1247 /* We voluntarily omit the bank here, in case the
1248 sequence itself matters. It shouldn't be a problem,
1249 since nobody else is supposed to access the
1250 device at that point. */
1251 pc87360_write_value(data, LD_TEMP, NO_BANK, 0xB, 0x04);
1252 pc87360_write_value(data, LD_TEMP, NO_BANK, 0xC, 0x35);
1253 pc87360_write_value(data, LD_TEMP, NO_BANK, 0xD, 0x05);
1254 pc87360_write_value(data, LD_TEMP, NO_BANK, 0xE, 0x05);
1259 static void pc87360_autodiv(struct device *dev, int nr)
1261 struct pc87360_data *data = dev_get_drvdata(dev);
1262 u8 old_min = data->fan_min[nr];
1264 /* Increase clock divider if needed and possible */
1265 if ((data->fan_status[nr] & 0x04) /* overflow flag */
1266 || (data->fan[nr] >= 224)) { /* next to overflow */
1267 if ((data->fan_status[nr] & 0x60) != 0x60) {
1268 data->fan_status[nr] += 0x20;
1269 data->fan_min[nr] >>= 1;
1270 data->fan[nr] >>= 1;
1271 dev_dbg(dev, "Increasing "
1272 "clock divider to %d for fan %d\n",
1273 FAN_DIV_FROM_REG(data->fan_status[nr]), nr+1);
1275 } else {
1276 /* Decrease clock divider if possible */
1277 while (!(data->fan_min[nr] & 0x80) /* min "nails" divider */
1278 && data->fan[nr] < 85 /* bad accuracy */
1279 && (data->fan_status[nr] & 0x60) != 0x00) {
1280 data->fan_status[nr] -= 0x20;
1281 data->fan_min[nr] <<= 1;
1282 data->fan[nr] <<= 1;
1283 dev_dbg(dev, "Decreasing "
1284 "clock divider to %d for fan %d\n",
1285 FAN_DIV_FROM_REG(data->fan_status[nr]),
1286 nr+1);
1290 /* Write new fan min if it changed */
1291 if (old_min != data->fan_min[nr]) {
1292 pc87360_write_value(data, LD_FAN, NO_BANK,
1293 PC87360_REG_FAN_MIN(nr),
1294 data->fan_min[nr]);
1298 static struct pc87360_data *pc87360_update_device(struct device *dev)
1300 struct pc87360_data *data = dev_get_drvdata(dev);
1301 u8 i;
1303 mutex_lock(&data->update_lock);
1305 if (time_after(jiffies, data->last_updated + HZ * 2) || !data->valid) {
1306 dev_dbg(dev, "Data update\n");
1308 /* Fans */
1309 for (i = 0; i < data->fannr; i++) {
1310 if (FAN_CONFIG_MONITOR(data->fan_conf, i)) {
1311 data->fan_status[i] =
1312 pc87360_read_value(data, LD_FAN,
1313 NO_BANK, PC87360_REG_FAN_STATUS(i));
1314 data->fan[i] = pc87360_read_value(data, LD_FAN,
1315 NO_BANK, PC87360_REG_FAN(i));
1316 data->fan_min[i] = pc87360_read_value(data,
1317 LD_FAN, NO_BANK,
1318 PC87360_REG_FAN_MIN(i));
1319 /* Change clock divider if needed */
1320 pc87360_autodiv(dev, i);
1321 /* Clear bits and write new divider */
1322 pc87360_write_value(data, LD_FAN, NO_BANK,
1323 PC87360_REG_FAN_STATUS(i),
1324 data->fan_status[i]);
1326 if (FAN_CONFIG_CONTROL(data->fan_conf, i))
1327 data->pwm[i] = pc87360_read_value(data, LD_FAN,
1328 NO_BANK, PC87360_REG_PWM(i));
1331 /* Voltages */
1332 for (i = 0; i < data->innr; i++) {
1333 data->in_status[i] = pc87360_read_value(data, LD_IN, i,
1334 PC87365_REG_IN_STATUS);
1335 /* Clear bits */
1336 pc87360_write_value(data, LD_IN, i,
1337 PC87365_REG_IN_STATUS,
1338 data->in_status[i]);
1339 if ((data->in_status[i] & 0x81) == 0x81) {
1340 data->in[i] = pc87360_read_value(data, LD_IN,
1341 i, PC87365_REG_IN);
1343 if (data->in_status[i] & 0x01) {
1344 data->in_min[i] = pc87360_read_value(data,
1345 LD_IN, i,
1346 PC87365_REG_IN_MIN);
1347 data->in_max[i] = pc87360_read_value(data,
1348 LD_IN, i,
1349 PC87365_REG_IN_MAX);
1350 if (i >= 11)
1351 data->in_crit[i-11] =
1352 pc87360_read_value(data, LD_IN,
1353 i, PC87365_REG_TEMP_CRIT);
1356 if (data->innr) {
1357 data->in_alarms = pc87360_read_value(data, LD_IN,
1358 NO_BANK, PC87365_REG_IN_ALARMS1)
1359 | ((pc87360_read_value(data, LD_IN,
1360 NO_BANK, PC87365_REG_IN_ALARMS2)
1361 & 0x07) << 8);
1362 data->vid = (data->vid_conf & 0xE0) ?
1363 pc87360_read_value(data, LD_IN,
1364 NO_BANK, PC87365_REG_VID) : 0x1F;
1367 /* Temperatures */
1368 for (i = 0; i < data->tempnr; i++) {
1369 data->temp_status[i] = pc87360_read_value(data,
1370 LD_TEMP, i,
1371 PC87365_REG_TEMP_STATUS);
1372 /* Clear bits */
1373 pc87360_write_value(data, LD_TEMP, i,
1374 PC87365_REG_TEMP_STATUS,
1375 data->temp_status[i]);
1376 if ((data->temp_status[i] & 0x81) == 0x81) {
1377 data->temp[i] = pc87360_read_value(data,
1378 LD_TEMP, i,
1379 PC87365_REG_TEMP);
1381 if (data->temp_status[i] & 0x01) {
1382 data->temp_min[i] = pc87360_read_value(data,
1383 LD_TEMP, i,
1384 PC87365_REG_TEMP_MIN);
1385 data->temp_max[i] = pc87360_read_value(data,
1386 LD_TEMP, i,
1387 PC87365_REG_TEMP_MAX);
1388 data->temp_crit[i] = pc87360_read_value(data,
1389 LD_TEMP, i,
1390 PC87365_REG_TEMP_CRIT);
1393 if (data->tempnr) {
1394 data->temp_alarms = pc87360_read_value(data, LD_TEMP,
1395 NO_BANK, PC87365_REG_TEMP_ALARMS)
1396 & 0x3F;
1399 data->last_updated = jiffies;
1400 data->valid = 1;
1403 mutex_unlock(&data->update_lock);
1405 return data;
1408 static int __init pc87360_device_add(unsigned short address)
1410 struct resource res = {
1411 .name = "pc87360",
1412 .flags = IORESOURCE_IO,
1414 int err, i;
1416 pdev = platform_device_alloc("pc87360", address);
1417 if (!pdev) {
1418 err = -ENOMEM;
1419 printk(KERN_ERR "pc87360: Device allocation failed\n");
1420 goto exit;
1423 for (i = 0; i < 3; i++) {
1424 if (!extra_isa[i])
1425 continue;
1426 res.start = extra_isa[i];
1427 res.end = extra_isa[i] + PC87360_EXTENT - 1;
1428 err = platform_device_add_resources(pdev, &res, 1);
1429 if (err) {
1430 printk(KERN_ERR "pc87360: Device resource[%d] "
1431 "addition failed (%d)\n", i, err);
1432 goto exit_device_put;
1436 err = platform_device_add(pdev);
1437 if (err) {
1438 printk(KERN_ERR "pc87360: Device addition failed (%d)\n",
1439 err);
1440 goto exit_device_put;
1443 return 0;
1445 exit_device_put:
1446 platform_device_put(pdev);
1447 exit:
1448 return err;
1451 static int __init pc87360_init(void)
1453 int err, i;
1454 unsigned short address = 0;
1456 if (pc87360_find(0x2e, &devid, extra_isa)
1457 && pc87360_find(0x4e, &devid, extra_isa)) {
1458 printk(KERN_WARNING "pc87360: PC8736x not detected, "
1459 "module not inserted.\n");
1460 return -ENODEV;
1463 /* Arbitrarily pick one of the addresses */
1464 for (i = 0; i < 3; i++) {
1465 if (extra_isa[i] != 0x0000) {
1466 address = extra_isa[i];
1467 break;
1471 if (address == 0x0000) {
1472 printk(KERN_WARNING "pc87360: No active logical device, "
1473 "module not inserted.\n");
1474 return -ENODEV;
1477 err = platform_driver_register(&pc87360_driver);
1478 if (err)
1479 goto exit;
1481 /* Sets global pdev as a side effect */
1482 err = pc87360_device_add(address);
1483 if (err)
1484 goto exit_driver;
1486 return 0;
1488 exit_driver:
1489 platform_driver_unregister(&pc87360_driver);
1490 exit:
1491 return err;
1494 static void __exit pc87360_exit(void)
1496 platform_device_unregister(pdev);
1497 platform_driver_unregister(&pc87360_driver);
1501 MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
1502 MODULE_DESCRIPTION("PC8736x hardware monitor");
1503 MODULE_LICENSE("GPL");
1505 module_init(pc87360_init);
1506 module_exit(pc87360_exit);