blob: cf2a35799c7ce5939744ba91adf5b23a24a9289e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * pc87360.c - Part of lm_sensors, Linux kernel modules
3 * for hardware monitoring
4 * Copyright (C) 2004 Jean Delvare <khali@linux-fr.org>
5 *
6 * Copied from smsc47m1.c:
7 * Copyright (C) 2002 Mark D. Studebaker <mdsxyz123@yahoo.com>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 *
23 * Supports the following chips:
24 *
25 * Chip #vin #fan #pwm #temp devid
26 * PC87360 - 2 2 - 0xE1
27 * PC87363 - 2 2 - 0xE8
28 * PC87364 - 3 3 - 0xE4
29 * PC87365 11 3 3 2 0xE5
30 * PC87366 11 3 3 3-4 0xE9
31 *
32 * This driver assumes that no more than one chip is present, and one of
33 * the standard Super-I/O addresses is used (0x2E/0x2F or 0x4E/0x4F).
34 */
35
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/module.h>
37#include <linux/init.h>
38#include <linux/slab.h>
39#include <linux/jiffies.h>
40#include <linux/i2c.h>
Jean Delvarefde09502005-07-19 23:51:07 +020041#include <linux/i2c-isa.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040042#include <linux/hwmon.h>
Jim Cromief0986bd2005-09-02 22:52:43 +020043#include <linux/hwmon-sysfs.h>
Jean Delvare303760b2005-07-31 21:52:01 +020044#include <linux/hwmon-vid.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040045#include <linux/err.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <asm/io.h>
47
Linus Torvalds1da177e2005-04-16 15:20:36 -070048static u8 devid;
Jean Delvare2d8672c2005-07-19 23:56:35 +020049static unsigned short address;
50static unsigned short extra_isa[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -070051static u8 confreg[4];
52
53enum chips { any_chip, pc87360, pc87363, pc87364, pc87365, pc87366 };
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
55static int init = 1;
56module_param(init, int, 0);
57MODULE_PARM_DESC(init,
58 "Chip initialization level:\n"
59 " 0: None\n"
60 "*1: Forcibly enable internal voltage and temperature channels, except in9\n"
61 " 2: Forcibly enable all voltage and temperature channels, except in9\n"
62 " 3: Forcibly enable all voltage and temperature channels, including in9");
63
64/*
65 * Super-I/O registers and operations
66 */
67
68#define DEV 0x07 /* Register: Logical device select */
69#define DEVID 0x20 /* Register: Device ID */
70#define ACT 0x30 /* Register: Device activation */
71#define BASE 0x60 /* Register: Base address */
72
73#define FSCM 0x09 /* Logical device: fans */
74#define VLM 0x0d /* Logical device: voltages */
75#define TMS 0x0e /* Logical device: temperatures */
76static const u8 logdev[3] = { FSCM, VLM, TMS };
77
78#define LD_FAN 0
79#define LD_IN 1
80#define LD_TEMP 2
81
82static inline void superio_outb(int sioaddr, int reg, int val)
83{
84 outb(reg, sioaddr);
85 outb(val, sioaddr+1);
86}
87
88static inline int superio_inb(int sioaddr, int reg)
89{
90 outb(reg, sioaddr);
91 return inb(sioaddr+1);
92}
93
94static inline void superio_exit(int sioaddr)
95{
96 outb(0x02, sioaddr);
97 outb(0x02, sioaddr+1);
98}
99
100/*
101 * Logical devices
102 */
103
104#define PC87360_EXTENT 0x10
105#define PC87365_REG_BANK 0x09
106#define NO_BANK 0xff
107
108/*
109 * Fan registers and conversions
110 */
111
112/* nr has to be 0 or 1 (PC87360/87363) or 2 (PC87364/87365/87366) */
113#define PC87360_REG_PRESCALE(nr) (0x00 + 2 * (nr))
114#define PC87360_REG_PWM(nr) (0x01 + 2 * (nr))
115#define PC87360_REG_FAN_MIN(nr) (0x06 + 3 * (nr))
116#define PC87360_REG_FAN(nr) (0x07 + 3 * (nr))
117#define PC87360_REG_FAN_STATUS(nr) (0x08 + 3 * (nr))
118
119#define FAN_FROM_REG(val,div) ((val) == 0 ? 0: \
120 480000 / ((val)*(div)))
121#define FAN_TO_REG(val,div) ((val) <= 100 ? 0 : \
122 480000 / ((val)*(div)))
123#define FAN_DIV_FROM_REG(val) (1 << ((val >> 5) & 0x03))
124#define FAN_STATUS_FROM_REG(val) ((val) & 0x07)
125
126#define FAN_CONFIG_MONITOR(val,nr) (((val) >> (2 + nr * 3)) & 1)
127#define FAN_CONFIG_CONTROL(val,nr) (((val) >> (3 + nr * 3)) & 1)
128#define FAN_CONFIG_INVERT(val,nr) (((val) >> (4 + nr * 3)) & 1)
129
130#define PWM_FROM_REG(val,inv) ((inv) ? 255 - (val) : (val))
131static inline u8 PWM_TO_REG(int val, int inv)
132{
133 if (inv)
134 val = 255 - val;
135 if (val < 0)
136 return 0;
137 if (val > 255)
138 return 255;
139 return val;
140}
141
142/*
143 * Voltage registers and conversions
144 */
145
146#define PC87365_REG_IN_CONVRATE 0x07
147#define PC87365_REG_IN_CONFIG 0x08
148#define PC87365_REG_IN 0x0B
149#define PC87365_REG_IN_MIN 0x0D
150#define PC87365_REG_IN_MAX 0x0C
151#define PC87365_REG_IN_STATUS 0x0A
152#define PC87365_REG_IN_ALARMS1 0x00
153#define PC87365_REG_IN_ALARMS2 0x01
154#define PC87365_REG_VID 0x06
155
156#define IN_FROM_REG(val,ref) (((val) * (ref) + 128) / 256)
157#define IN_TO_REG(val,ref) ((val) < 0 ? 0 : \
158 (val)*256 >= (ref)*255 ? 255: \
159 ((val) * 256 + (ref)/2) / (ref))
160
161/*
162 * Temperature registers and conversions
163 */
164
165#define PC87365_REG_TEMP_CONFIG 0x08
166#define PC87365_REG_TEMP 0x0B
167#define PC87365_REG_TEMP_MIN 0x0D
168#define PC87365_REG_TEMP_MAX 0x0C
169#define PC87365_REG_TEMP_CRIT 0x0E
170#define PC87365_REG_TEMP_STATUS 0x0A
171#define PC87365_REG_TEMP_ALARMS 0x00
172
173#define TEMP_FROM_REG(val) ((val) * 1000)
174#define TEMP_TO_REG(val) ((val) < -55000 ? -55 : \
175 (val) > 127000 ? 127 : \
176 (val) < 0 ? ((val) - 500) / 1000 : \
177 ((val) + 500) / 1000)
178
179/*
180 * Client data (each client gets its own)
181 */
182
183struct pc87360_data {
184 struct i2c_client client;
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400185 struct class_device *class_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 struct semaphore lock;
187 struct semaphore update_lock;
188 char valid; /* !=0 if following fields are valid */
189 unsigned long last_updated; /* In jiffies */
190
191 int address[3];
192
193 u8 fannr, innr, tempnr;
194
195 u8 fan[3]; /* Register value */
196 u8 fan_min[3]; /* Register value */
197 u8 fan_status[3]; /* Register value */
198 u8 pwm[3]; /* Register value */
199 u16 fan_conf; /* Configuration register values, combined */
200
201 u16 in_vref; /* 1 mV/bit */
202 u8 in[14]; /* Register value */
203 u8 in_min[14]; /* Register value */
204 u8 in_max[14]; /* Register value */
205 u8 in_crit[3]; /* Register value */
206 u8 in_status[14]; /* Register value */
207 u16 in_alarms; /* Register values, combined, masked */
208 u8 vid_conf; /* Configuration register value */
209 u8 vrm;
210 u8 vid; /* Register value */
211
212 s8 temp[3]; /* Register value */
213 s8 temp_min[3]; /* Register value */
214 s8 temp_max[3]; /* Register value */
215 s8 temp_crit[3]; /* Register value */
216 u8 temp_status[3]; /* Register value */
217 u8 temp_alarms; /* Register value, masked */
218};
219
220/*
221 * Functions declaration
222 */
223
Jean Delvare2d8672c2005-07-19 23:56:35 +0200224static int pc87360_detect(struct i2c_adapter *adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225static int pc87360_detach_client(struct i2c_client *client);
226
227static int pc87360_read_value(struct pc87360_data *data, u8 ldi, u8 bank,
228 u8 reg);
229static void pc87360_write_value(struct pc87360_data *data, u8 ldi, u8 bank,
230 u8 reg, u8 value);
231static void pc87360_init_client(struct i2c_client *client, int use_thermistors);
232static struct pc87360_data *pc87360_update_device(struct device *dev);
233
234/*
235 * Driver data (common to all clients)
236 */
237
238static struct i2c_driver pc87360_driver = {
239 .owner = THIS_MODULE,
240 .name = "pc87360",
Jean Delvare2d8672c2005-07-19 23:56:35 +0200241 .attach_adapter = pc87360_detect,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 .detach_client = pc87360_detach_client,
243};
244
245/*
246 * Sysfs stuff
247 */
248
Jim Cromief0986bd2005-09-02 22:52:43 +0200249static ssize_t show_fan_input(struct device *dev, struct device_attribute *devattr, char *buf)
250{
251 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
252 struct pc87360_data *data = pc87360_update_device(dev);
Jim Cromie694fa052005-09-02 22:57:52 +0200253 return sprintf(buf, "%u\n", FAN_FROM_REG(data->fan[attr->index],
254 FAN_DIV_FROM_REG(data->fan_status[attr->index])));
Jim Cromief0986bd2005-09-02 22:52:43 +0200255}
256static ssize_t show_fan_min(struct device *dev, struct device_attribute *devattr, char *buf)
257{
258 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
259 struct pc87360_data *data = pc87360_update_device(dev);
Jim Cromie694fa052005-09-02 22:57:52 +0200260 return sprintf(buf, "%u\n", FAN_FROM_REG(data->fan_min[attr->index],
261 FAN_DIV_FROM_REG(data->fan_status[attr->index])));
Jim Cromief0986bd2005-09-02 22:52:43 +0200262}
263static ssize_t show_fan_div(struct device *dev, struct device_attribute *devattr, char *buf)
264{
265 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
266 struct pc87360_data *data = pc87360_update_device(dev);
267 return sprintf(buf, "%u\n",
Jim Cromie694fa052005-09-02 22:57:52 +0200268 FAN_DIV_FROM_REG(data->fan_status[attr->index]));
Jim Cromief0986bd2005-09-02 22:52:43 +0200269}
270static ssize_t show_fan_status(struct device *dev, struct device_attribute *devattr, char *buf)
271{
272 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
273 struct pc87360_data *data = pc87360_update_device(dev);
274 return sprintf(buf, "%u\n",
Jim Cromie694fa052005-09-02 22:57:52 +0200275 FAN_STATUS_FROM_REG(data->fan_status[attr->index]));
Jim Cromief0986bd2005-09-02 22:52:43 +0200276}
277static ssize_t set_fan_min(struct device *dev, struct device_attribute *devattr, const char *buf,
278 size_t count)
279{
280 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Jim Cromie11be27e2005-09-02 23:05:07 +0200281 struct i2c_client *client = to_i2c_client(dev);
282 struct pc87360_data *data = i2c_get_clientdata(client);
283 long fan_min = simple_strtol(buf, NULL, 10);
284
285 down(&data->update_lock);
286 fan_min = FAN_TO_REG(fan_min, FAN_DIV_FROM_REG(data->fan_status[attr->index]));
287
288 /* If it wouldn't fit, change clock divisor */
289 while (fan_min > 255
290 && (data->fan_status[attr->index] & 0x60) != 0x60) {
291 fan_min >>= 1;
292 data->fan[attr->index] >>= 1;
293 data->fan_status[attr->index] += 0x20;
294 }
295 data->fan_min[attr->index] = fan_min > 255 ? 255 : fan_min;
296 pc87360_write_value(data, LD_FAN, NO_BANK, PC87360_REG_FAN_MIN(attr->index),
297 data->fan_min[attr->index]);
298
299 /* Write new divider, preserve alarm bits */
300 pc87360_write_value(data, LD_FAN, NO_BANK, PC87360_REG_FAN_STATUS(attr->index),
301 data->fan_status[attr->index] & 0xF9);
302 up(&data->update_lock);
303
304 return count;
Jim Cromief0986bd2005-09-02 22:52:43 +0200305}
306
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307#define show_and_set_fan(offset) \
Jim Cromief0986bd2005-09-02 22:52:43 +0200308static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
Jim Cromie694fa052005-09-02 22:57:52 +0200309 show_fan_input, NULL, offset-1); \
Jim Cromief0986bd2005-09-02 22:52:43 +0200310static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IWUSR | S_IRUGO, \
Jim Cromie694fa052005-09-02 22:57:52 +0200311 show_fan_min, set_fan_min, offset-1); \
Jim Cromief0986bd2005-09-02 22:52:43 +0200312static SENSOR_DEVICE_ATTR(fan##offset##_div, S_IRUGO, \
Jim Cromie694fa052005-09-02 22:57:52 +0200313 show_fan_div, NULL, offset-1); \
Jim Cromief0986bd2005-09-02 22:52:43 +0200314static SENSOR_DEVICE_ATTR(fan##offset##_status, S_IRUGO, \
Jim Cromie694fa052005-09-02 22:57:52 +0200315 show_fan_status, NULL, offset-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316show_and_set_fan(1)
317show_and_set_fan(2)
318show_and_set_fan(3)
319
Jim Cromief0986bd2005-09-02 22:52:43 +0200320static ssize_t show_pwm(struct device *dev, struct device_attribute *devattr, char *buf)
321{
322 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
323 struct pc87360_data *data = pc87360_update_device(dev);
324 return sprintf(buf, "%u\n",
Jim Cromie694fa052005-09-02 22:57:52 +0200325 PWM_FROM_REG(data->pwm[attr->index],
Jim Cromief0986bd2005-09-02 22:52:43 +0200326 FAN_CONFIG_INVERT(data->fan_conf,
Jim Cromie694fa052005-09-02 22:57:52 +0200327 attr->index)));
Jim Cromief0986bd2005-09-02 22:52:43 +0200328}
329static ssize_t set_pwm(struct device *dev, struct device_attribute *devattr, const char *buf,
330 size_t count)
331{
332 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
333 struct i2c_client *client = to_i2c_client(dev);
334 struct pc87360_data *data = i2c_get_clientdata(client);
335 long val = simple_strtol(buf, NULL, 10);
336
337 down(&data->update_lock);
Jim Cromie694fa052005-09-02 22:57:52 +0200338 data->pwm[attr->index] = PWM_TO_REG(val,
339 FAN_CONFIG_INVERT(data->fan_conf, attr->index));
340 pc87360_write_value(data, LD_FAN, NO_BANK, PC87360_REG_PWM(attr->index),
341 data->pwm[attr->index]);
Jim Cromief0986bd2005-09-02 22:52:43 +0200342 up(&data->update_lock);
343 return count;
344}
345
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346#define show_and_set_pwm(offset) \
Jim Cromief0986bd2005-09-02 22:52:43 +0200347static SENSOR_DEVICE_ATTR(pwm##offset, S_IWUSR | S_IRUGO, \
Jim Cromie694fa052005-09-02 22:57:52 +0200348 show_pwm, set_pwm, offset-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349show_and_set_pwm(1)
350show_and_set_pwm(2)
351show_and_set_pwm(3)
352
Jim Cromief0986bd2005-09-02 22:52:43 +0200353static ssize_t show_in_input(struct device *dev, struct device_attribute *devattr, char *buf)
354{
355 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
356 struct pc87360_data *data = pc87360_update_device(dev);
357 return sprintf(buf, "%u\n", IN_FROM_REG(data->in[attr->index],
358 data->in_vref));
359}
360static ssize_t show_in_min(struct device *dev, struct device_attribute *devattr, char *buf)
361{
362 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
363 struct pc87360_data *data = pc87360_update_device(dev);
364 return sprintf(buf, "%u\n", IN_FROM_REG(data->in_min[attr->index],
365 data->in_vref));
366}
367static ssize_t show_in_max(struct device *dev, struct device_attribute *devattr, char *buf)
368{
369 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
370 struct pc87360_data *data = pc87360_update_device(dev);
371 return sprintf(buf, "%u\n", IN_FROM_REG(data->in_max[attr->index],
372 data->in_vref));
373}
374static ssize_t show_in_status(struct device *dev, struct device_attribute *devattr, char *buf)
375{
376 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
377 struct pc87360_data *data = pc87360_update_device(dev);
378 return sprintf(buf, "%u\n", data->in_status[attr->index]);
379}
380static ssize_t set_in_min(struct device *dev, struct device_attribute *devattr, const char *buf,
381 size_t count)
382{
383 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
384 struct i2c_client *client = to_i2c_client(dev);
385 struct pc87360_data *data = i2c_get_clientdata(client);
386 long val = simple_strtol(buf, NULL, 10);
387
388 down(&data->update_lock);
389 data->in_min[attr->index] = IN_TO_REG(val, data->in_vref);
390 pc87360_write_value(data, LD_IN, attr->index, PC87365_REG_IN_MIN,
391 data->in_min[attr->index]);
392 up(&data->update_lock);
393 return count;
394}
395static ssize_t set_in_max(struct device *dev, struct device_attribute *devattr, const char *buf,
396 size_t count)
397{
398 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
399 struct i2c_client *client = to_i2c_client(dev);
400 struct pc87360_data *data = i2c_get_clientdata(client);
401 long val = simple_strtol(buf, NULL, 10);
402
403 down(&data->update_lock);
404 data->in_max[attr->index] = IN_TO_REG(val,
405 data->in_vref);
406 pc87360_write_value(data, LD_IN, attr->index, PC87365_REG_IN_MAX,
407 data->in_max[attr->index]);
408 up(&data->update_lock);
409 return count;
410}
411
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412#define show_and_set_in(offset) \
Jim Cromief0986bd2005-09-02 22:52:43 +0200413static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \
414 show_in_input, NULL, offset); \
415static SENSOR_DEVICE_ATTR(in##offset##_min, S_IWUSR | S_IRUGO, \
416 show_in_min, set_in_min, offset); \
417static SENSOR_DEVICE_ATTR(in##offset##_max, S_IWUSR | S_IRUGO, \
418 show_in_max, set_in_max, offset); \
419static SENSOR_DEVICE_ATTR(in##offset##_status, S_IRUGO, \
420 show_in_status, NULL, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421show_and_set_in(0)
422show_and_set_in(1)
423show_and_set_in(2)
424show_and_set_in(3)
425show_and_set_in(4)
426show_and_set_in(5)
427show_and_set_in(6)
428show_and_set_in(7)
429show_and_set_in(8)
430show_and_set_in(9)
431show_and_set_in(10)
432
Jim Cromief0986bd2005-09-02 22:52:43 +0200433static ssize_t show_therm_input(struct device *dev, struct device_attribute *devattr, char *buf)
434{
435 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
436 struct pc87360_data *data = pc87360_update_device(dev);
Jim Cromie694fa052005-09-02 22:57:52 +0200437 return sprintf(buf, "%u\n", IN_FROM_REG(data->in[attr->index],
Jim Cromief0986bd2005-09-02 22:52:43 +0200438 data->in_vref));
439}
440static ssize_t show_therm_min(struct device *dev, struct device_attribute *devattr, char *buf)
441{
442 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
443 struct pc87360_data *data = pc87360_update_device(dev);
Jim Cromie694fa052005-09-02 22:57:52 +0200444 return sprintf(buf, "%u\n", IN_FROM_REG(data->in_min[attr->index],
Jim Cromief0986bd2005-09-02 22:52:43 +0200445 data->in_vref));
446}
447static ssize_t show_therm_max(struct device *dev, struct device_attribute *devattr, char *buf)
448{
449 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
450 struct pc87360_data *data = pc87360_update_device(dev);
Jim Cromie694fa052005-09-02 22:57:52 +0200451 return sprintf(buf, "%u\n", IN_FROM_REG(data->in_max[attr->index],
Jim Cromief0986bd2005-09-02 22:52:43 +0200452 data->in_vref));
453}
454static ssize_t show_therm_crit(struct device *dev, struct device_attribute *devattr, char *buf)
455{
456 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
457 struct pc87360_data *data = pc87360_update_device(dev);
Jim Cromie694fa052005-09-02 22:57:52 +0200458 return sprintf(buf, "%u\n", IN_FROM_REG(data->in_crit[attr->index-11],
Jim Cromief0986bd2005-09-02 22:52:43 +0200459 data->in_vref));
460}
461static ssize_t show_therm_status(struct device *dev, struct device_attribute *devattr, char *buf)
462{
463 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
464 struct pc87360_data *data = pc87360_update_device(dev);
Jim Cromie694fa052005-09-02 22:57:52 +0200465 return sprintf(buf, "%u\n", data->in_status[attr->index]);
Jim Cromief0986bd2005-09-02 22:52:43 +0200466}
467static ssize_t set_therm_min(struct device *dev, struct device_attribute *devattr, const char *buf,
468 size_t count)
469{
470 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
471 struct i2c_client *client = to_i2c_client(dev);
472 struct pc87360_data *data = i2c_get_clientdata(client);
473 long val = simple_strtol(buf, NULL, 10);
474
475 down(&data->update_lock);
Jim Cromie694fa052005-09-02 22:57:52 +0200476 data->in_min[attr->index] = IN_TO_REG(val, data->in_vref);
477 pc87360_write_value(data, LD_IN, attr->index, PC87365_REG_TEMP_MIN,
478 data->in_min[attr->index]);
Jim Cromief0986bd2005-09-02 22:52:43 +0200479 up(&data->update_lock);
480 return count;
481}
482static ssize_t set_therm_max(struct device *dev, struct device_attribute *devattr, const char *buf,
483 size_t count)
484{
485 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
486 struct i2c_client *client = to_i2c_client(dev);
487 struct pc87360_data *data = i2c_get_clientdata(client);
488 long val = simple_strtol(buf, NULL, 10);
489
490 down(&data->update_lock);
Jim Cromie694fa052005-09-02 22:57:52 +0200491 data->in_max[attr->index] = IN_TO_REG(val, data->in_vref);
492 pc87360_write_value(data, LD_IN, attr->index, PC87365_REG_TEMP_MAX,
493 data->in_max[attr->index]);
Jim Cromief0986bd2005-09-02 22:52:43 +0200494 up(&data->update_lock);
495 return count;
496}
497static ssize_t set_therm_crit(struct device *dev, struct device_attribute *devattr, const char *buf,
498 size_t count)
499{
500 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
501 struct i2c_client *client = to_i2c_client(dev);
502 struct pc87360_data *data = i2c_get_clientdata(client);
503 long val = simple_strtol(buf, NULL, 10);
504
505 down(&data->update_lock);
Jim Cromie694fa052005-09-02 22:57:52 +0200506 data->in_crit[attr->index-11] = IN_TO_REG(val, data->in_vref);
507 pc87360_write_value(data, LD_IN, attr->index, PC87365_REG_TEMP_CRIT,
508 data->in_crit[attr->index-11]);
Jim Cromief0986bd2005-09-02 22:52:43 +0200509 up(&data->update_lock);
510 return count;
511}
512
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513#define show_and_set_therm(offset) \
Jim Cromief0986bd2005-09-02 22:52:43 +0200514static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
Jim Cromie694fa052005-09-02 22:57:52 +0200515 show_therm_input, NULL, 11+offset-4); \
Jim Cromief0986bd2005-09-02 22:52:43 +0200516static SENSOR_DEVICE_ATTR(temp##offset##_min, S_IWUSR | S_IRUGO, \
Jim Cromie694fa052005-09-02 22:57:52 +0200517 show_therm_min, set_therm_min, 11+offset-4); \
Jim Cromief0986bd2005-09-02 22:52:43 +0200518static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IWUSR | S_IRUGO, \
Jim Cromie694fa052005-09-02 22:57:52 +0200519 show_therm_max, set_therm_max, 11+offset-4); \
Jim Cromief0986bd2005-09-02 22:52:43 +0200520static SENSOR_DEVICE_ATTR(temp##offset##_crit, S_IWUSR | S_IRUGO, \
Jim Cromie694fa052005-09-02 22:57:52 +0200521 show_therm_crit, set_therm_crit, 11+offset-4); \
Jim Cromief0986bd2005-09-02 22:52:43 +0200522static SENSOR_DEVICE_ATTR(temp##offset##_status, S_IRUGO, \
Jim Cromie694fa052005-09-02 22:57:52 +0200523 show_therm_status, NULL, 11+offset-4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524show_and_set_therm(4)
525show_and_set_therm(5)
526show_and_set_therm(6)
527
Yani Ioannoua5099cf2005-05-17 06:42:25 -0400528static ssize_t show_vid(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529{
530 struct pc87360_data *data = pc87360_update_device(dev);
531 return sprintf(buf, "%u\n", vid_from_reg(data->vid, data->vrm));
532}
533static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL);
534
Yani Ioannoua5099cf2005-05-17 06:42:25 -0400535static ssize_t show_vrm(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536{
537 struct pc87360_data *data = pc87360_update_device(dev);
538 return sprintf(buf, "%u\n", data->vrm);
539}
Yani Ioannoua5099cf2005-05-17 06:42:25 -0400540static ssize_t set_vrm(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541{
542 struct i2c_client *client = to_i2c_client(dev);
543 struct pc87360_data *data = i2c_get_clientdata(client);
544 data->vrm = simple_strtoul(buf, NULL, 10);
545 return count;
546}
547static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm, set_vrm);
548
Yani Ioannoua5099cf2005-05-17 06:42:25 -0400549static ssize_t show_in_alarms(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550{
551 struct pc87360_data *data = pc87360_update_device(dev);
552 return sprintf(buf, "%u\n", data->in_alarms);
553}
554static DEVICE_ATTR(alarms_in, S_IRUGO, show_in_alarms, NULL);
555
Jim Cromief0986bd2005-09-02 22:52:43 +0200556static ssize_t show_temp_input(struct device *dev, struct device_attribute *devattr, char *buf)
557{
558 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
559 struct pc87360_data *data = pc87360_update_device(dev);
Jim Cromie694fa052005-09-02 22:57:52 +0200560 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[attr->index]));
Jim Cromief0986bd2005-09-02 22:52:43 +0200561}
562static ssize_t show_temp_min(struct device *dev, struct device_attribute *devattr, char *buf)
563{
564 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
565 struct pc87360_data *data = pc87360_update_device(dev);
Jim Cromie694fa052005-09-02 22:57:52 +0200566 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_min[attr->index]));
Jim Cromief0986bd2005-09-02 22:52:43 +0200567}
568static ssize_t show_temp_max(struct device *dev, struct device_attribute *devattr, char *buf)
569{
570 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
571 struct pc87360_data *data = pc87360_update_device(dev);
Jim Cromie694fa052005-09-02 22:57:52 +0200572 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max[attr->index]));
Jim Cromief0986bd2005-09-02 22:52:43 +0200573}
574static ssize_t show_temp_crit(struct device *dev, struct device_attribute *devattr, char *buf)
575{
576 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
577 struct pc87360_data *data = pc87360_update_device(dev);
Jim Cromie694fa052005-09-02 22:57:52 +0200578 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_crit[attr->index]));
Jim Cromief0986bd2005-09-02 22:52:43 +0200579}
580static ssize_t show_temp_status(struct device *dev, struct device_attribute *devattr, char *buf)
581{
582 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
583 struct pc87360_data *data = pc87360_update_device(dev);
Jim Cromie694fa052005-09-02 22:57:52 +0200584 return sprintf(buf, "%d\n", data->temp_status[attr->index]);
Jim Cromief0986bd2005-09-02 22:52:43 +0200585}
586static ssize_t set_temp_min(struct device *dev, struct device_attribute *devattr, const char *buf,
587 size_t count)
588{
589 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
590 struct i2c_client *client = to_i2c_client(dev);
591 struct pc87360_data *data = i2c_get_clientdata(client);
592 long val = simple_strtol(buf, NULL, 10);
593
594 down(&data->update_lock);
Jim Cromie694fa052005-09-02 22:57:52 +0200595 data->temp_min[attr->index] = TEMP_TO_REG(val);
596 pc87360_write_value(data, LD_TEMP, attr->index, PC87365_REG_TEMP_MIN,
597 data->temp_min[attr->index]);
Jim Cromief0986bd2005-09-02 22:52:43 +0200598 up(&data->update_lock);
599 return count;
600}
601static ssize_t set_temp_max(struct device *dev, struct device_attribute *devattr, const char *buf,
602 size_t count)
603{
604 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
605 struct i2c_client *client = to_i2c_client(dev);
606 struct pc87360_data *data = i2c_get_clientdata(client);
607 long val = simple_strtol(buf, NULL, 10);
608
609 down(&data->update_lock);
Jim Cromie694fa052005-09-02 22:57:52 +0200610 data->temp_max[attr->index] = TEMP_TO_REG(val);
611 pc87360_write_value(data, LD_TEMP, attr->index, PC87365_REG_TEMP_MAX,
612 data->temp_max[attr->index]);
Jim Cromief0986bd2005-09-02 22:52:43 +0200613 up(&data->update_lock);
614 return count;
615}
616static ssize_t set_temp_crit(struct device *dev, struct device_attribute *devattr, const char *buf,
617 size_t count)
618{
619 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
620 struct i2c_client *client = to_i2c_client(dev);
621 struct pc87360_data *data = i2c_get_clientdata(client);
622 long val = simple_strtol(buf, NULL, 10);
623
624 down(&data->update_lock);
Jim Cromie694fa052005-09-02 22:57:52 +0200625 data->temp_crit[attr->index] = TEMP_TO_REG(val);
626 pc87360_write_value(data, LD_TEMP, attr->index, PC87365_REG_TEMP_CRIT,
627 data->temp_crit[attr->index]);
Jim Cromief0986bd2005-09-02 22:52:43 +0200628 up(&data->update_lock);
629 return count;
630}
631
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632#define show_and_set_temp(offset) \
Jim Cromief0986bd2005-09-02 22:52:43 +0200633static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
Jim Cromie694fa052005-09-02 22:57:52 +0200634 show_temp_input, NULL, offset-1); \
Jim Cromief0986bd2005-09-02 22:52:43 +0200635static SENSOR_DEVICE_ATTR(temp##offset##_min, S_IWUSR | S_IRUGO, \
Jim Cromie694fa052005-09-02 22:57:52 +0200636 show_temp_min, set_temp_min, offset-1); \
Jim Cromief0986bd2005-09-02 22:52:43 +0200637static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IWUSR | S_IRUGO, \
Jim Cromie694fa052005-09-02 22:57:52 +0200638 show_temp_max, set_temp_max, offset-1); \
Jim Cromief0986bd2005-09-02 22:52:43 +0200639static SENSOR_DEVICE_ATTR(temp##offset##_crit, S_IWUSR | S_IRUGO, \
Jim Cromie694fa052005-09-02 22:57:52 +0200640 show_temp_crit, set_temp_crit, offset-1); \
Jim Cromief0986bd2005-09-02 22:52:43 +0200641static SENSOR_DEVICE_ATTR(temp##offset##_status, S_IRUGO, \
Jim Cromie694fa052005-09-02 22:57:52 +0200642 show_temp_status, NULL, offset-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643show_and_set_temp(1)
644show_and_set_temp(2)
645show_and_set_temp(3)
646
Yani Ioannoua5099cf2005-05-17 06:42:25 -0400647static ssize_t show_temp_alarms(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648{
649 struct pc87360_data *data = pc87360_update_device(dev);
650 return sprintf(buf, "%u\n", data->temp_alarms);
651}
652static DEVICE_ATTR(alarms_temp, S_IRUGO, show_temp_alarms, NULL);
653
654/*
655 * Device detection, registration and update
656 */
657
Jean Delvaree6cfb3a2005-07-27 21:32:02 +0200658static int __init pc87360_find(int sioaddr, u8 *devid, unsigned short *addresses)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659{
660 u16 val;
661 int i;
662 int nrdev; /* logical device count */
663
664 /* No superio_enter */
665
666 /* Identify device */
667 val = superio_inb(sioaddr, DEVID);
668 switch (val) {
669 case 0xE1: /* PC87360 */
670 case 0xE8: /* PC87363 */
671 case 0xE4: /* PC87364 */
672 nrdev = 1;
673 break;
674 case 0xE5: /* PC87365 */
675 case 0xE9: /* PC87366 */
676 nrdev = 3;
677 break;
678 default:
679 superio_exit(sioaddr);
680 return -ENODEV;
681 }
682 /* Remember the device id */
683 *devid = val;
684
685 for (i = 0; i < nrdev; i++) {
686 /* select logical device */
687 superio_outb(sioaddr, DEV, logdev[i]);
688
689 val = superio_inb(sioaddr, ACT);
690 if (!(val & 0x01)) {
691 printk(KERN_INFO "pc87360: Device 0x%02x not "
692 "activated\n", logdev[i]);
693 continue;
694 }
695
696 val = (superio_inb(sioaddr, BASE) << 8)
697 | superio_inb(sioaddr, BASE + 1);
698 if (!val) {
699 printk(KERN_INFO "pc87360: Base address not set for "
700 "device 0x%02x\n", logdev[i]);
701 continue;
702 }
703
Jean Delvare2d8672c2005-07-19 23:56:35 +0200704 addresses[i] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
706 if (i==0) { /* Fans */
707 confreg[0] = superio_inb(sioaddr, 0xF0);
708 confreg[1] = superio_inb(sioaddr, 0xF1);
709
710#ifdef DEBUG
711 printk(KERN_DEBUG "pc87360: Fan 1: mon=%d "
712 "ctrl=%d inv=%d\n", (confreg[0]>>2)&1,
713 (confreg[0]>>3)&1, (confreg[0]>>4)&1);
714 printk(KERN_DEBUG "pc87360: Fan 2: mon=%d "
715 "ctrl=%d inv=%d\n", (confreg[0]>>5)&1,
716 (confreg[0]>>6)&1, (confreg[0]>>7)&1);
717 printk(KERN_DEBUG "pc87360: Fan 3: mon=%d "
718 "ctrl=%d inv=%d\n", confreg[1]&1,
719 (confreg[1]>>1)&1, (confreg[1]>>2)&1);
720#endif
721 } else if (i==1) { /* Voltages */
722 /* Are we using thermistors? */
723 if (*devid == 0xE9) { /* PC87366 */
724 /* These registers are not logical-device
725 specific, just that we won't need them if
726 we don't use the VLM device */
727 confreg[2] = superio_inb(sioaddr, 0x2B);
728 confreg[3] = superio_inb(sioaddr, 0x25);
729
730 if (confreg[2] & 0x40) {
731 printk(KERN_INFO "pc87360: Using "
732 "thermistors for temperature "
733 "monitoring\n");
734 }
735 if (confreg[3] & 0xE0) {
736 printk(KERN_INFO "pc87360: VID "
737 "inputs routed (mode %u)\n",
738 confreg[3] >> 5);
739 }
740 }
741 }
742 }
743
744 superio_exit(sioaddr);
745 return 0;
746}
747
Jean Delvare2d8672c2005-07-19 23:56:35 +0200748static int pc87360_detect(struct i2c_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749{
750 int i;
751 struct i2c_client *new_client;
752 struct pc87360_data *data;
753 int err = 0;
754 const char *name = "pc87360";
755 int use_thermistors = 0;
756
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 if (!(data = kmalloc(sizeof(struct pc87360_data), GFP_KERNEL)))
758 return -ENOMEM;
759 memset(data, 0x00, sizeof(struct pc87360_data));
760
761 new_client = &data->client;
762 i2c_set_clientdata(new_client, data);
763 new_client->addr = address;
764 init_MUTEX(&data->lock);
765 new_client->adapter = adapter;
766 new_client->driver = &pc87360_driver;
767 new_client->flags = 0;
768
769 data->fannr = 2;
770 data->innr = 0;
771 data->tempnr = 0;
772
773 switch (devid) {
774 case 0xe8:
775 name = "pc87363";
776 break;
777 case 0xe4:
778 name = "pc87364";
779 data->fannr = 3;
780 break;
781 case 0xe5:
782 name = "pc87365";
783 data->fannr = extra_isa[0] ? 3 : 0;
784 data->innr = extra_isa[1] ? 11 : 0;
785 data->tempnr = extra_isa[2] ? 2 : 0;
786 break;
787 case 0xe9:
788 name = "pc87366";
789 data->fannr = extra_isa[0] ? 3 : 0;
790 data->innr = extra_isa[1] ? 14 : 0;
791 data->tempnr = extra_isa[2] ? 3 : 0;
792 break;
793 }
794
795 strcpy(new_client->name, name);
796 data->valid = 0;
797 init_MUTEX(&data->update_lock);
798
799 for (i = 0; i < 3; i++) {
800 if (((data->address[i] = extra_isa[i]))
801 && !request_region(extra_isa[i], PC87360_EXTENT,
802 pc87360_driver.name)) {
803 dev_err(&new_client->dev, "Region 0x%x-0x%x already "
804 "in use!\n", extra_isa[i],
805 extra_isa[i]+PC87360_EXTENT-1);
806 for (i--; i >= 0; i--)
807 release_region(extra_isa[i], PC87360_EXTENT);
808 err = -EBUSY;
809 goto ERROR1;
810 }
811 }
812
813 /* Retrieve the fans configuration from Super-I/O space */
814 if (data->fannr)
815 data->fan_conf = confreg[0] | (confreg[1] << 8);
816
817 if ((err = i2c_attach_client(new_client)))
818 goto ERROR2;
819
820 /* Use the correct reference voltage
821 Unless both the VLM and the TMS logical devices agree to
822 use an external Vref, the internal one is used. */
823 if (data->innr) {
824 i = pc87360_read_value(data, LD_IN, NO_BANK,
825 PC87365_REG_IN_CONFIG);
826 if (data->tempnr) {
827 i &= pc87360_read_value(data, LD_TEMP, NO_BANK,
828 PC87365_REG_TEMP_CONFIG);
829 }
830 data->in_vref = (i&0x02) ? 3025 : 2966;
831 dev_dbg(&new_client->dev, "Using %s reference voltage\n",
832 (i&0x02) ? "external" : "internal");
833
834 data->vid_conf = confreg[3];
835 data->vrm = 90;
836 }
837
838 /* Fan clock dividers may be needed before any data is read */
839 for (i = 0; i < data->fannr; i++) {
840 if (FAN_CONFIG_MONITOR(data->fan_conf, i))
841 data->fan_status[i] = pc87360_read_value(data,
842 LD_FAN, NO_BANK,
843 PC87360_REG_FAN_STATUS(i));
844 }
845
846 if (init > 0) {
847 if (devid == 0xe9 && data->address[1]) /* PC87366 */
848 use_thermistors = confreg[2] & 0x40;
849
850 pc87360_init_client(new_client, use_thermistors);
851 }
852
853 /* Register sysfs hooks */
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400854 data->class_dev = hwmon_device_register(&new_client->dev);
855 if (IS_ERR(data->class_dev)) {
856 err = PTR_ERR(data->class_dev);
857 goto ERROR3;
858 }
859
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 if (data->innr) {
Jim Cromief0986bd2005-09-02 22:52:43 +0200861 device_create_file(&new_client->dev, &sensor_dev_attr_in0_input.dev_attr);
862 device_create_file(&new_client->dev, &sensor_dev_attr_in1_input.dev_attr);
863 device_create_file(&new_client->dev, &sensor_dev_attr_in2_input.dev_attr);
864 device_create_file(&new_client->dev, &sensor_dev_attr_in3_input.dev_attr);
865 device_create_file(&new_client->dev, &sensor_dev_attr_in4_input.dev_attr);
866 device_create_file(&new_client->dev, &sensor_dev_attr_in5_input.dev_attr);
867 device_create_file(&new_client->dev, &sensor_dev_attr_in6_input.dev_attr);
868 device_create_file(&new_client->dev, &sensor_dev_attr_in7_input.dev_attr);
869 device_create_file(&new_client->dev, &sensor_dev_attr_in8_input.dev_attr);
870 device_create_file(&new_client->dev, &sensor_dev_attr_in9_input.dev_attr);
871 device_create_file(&new_client->dev, &sensor_dev_attr_in10_input.dev_attr);
872 device_create_file(&new_client->dev, &sensor_dev_attr_in0_min.dev_attr);
873 device_create_file(&new_client->dev, &sensor_dev_attr_in1_min.dev_attr);
874 device_create_file(&new_client->dev, &sensor_dev_attr_in2_min.dev_attr);
875 device_create_file(&new_client->dev, &sensor_dev_attr_in3_min.dev_attr);
876 device_create_file(&new_client->dev, &sensor_dev_attr_in4_min.dev_attr);
877 device_create_file(&new_client->dev, &sensor_dev_attr_in5_min.dev_attr);
878 device_create_file(&new_client->dev, &sensor_dev_attr_in6_min.dev_attr);
879 device_create_file(&new_client->dev, &sensor_dev_attr_in7_min.dev_attr);
880 device_create_file(&new_client->dev, &sensor_dev_attr_in8_min.dev_attr);
881 device_create_file(&new_client->dev, &sensor_dev_attr_in9_min.dev_attr);
882 device_create_file(&new_client->dev, &sensor_dev_attr_in10_min.dev_attr);
883 device_create_file(&new_client->dev, &sensor_dev_attr_in0_max.dev_attr);
884 device_create_file(&new_client->dev, &sensor_dev_attr_in1_max.dev_attr);
885 device_create_file(&new_client->dev, &sensor_dev_attr_in2_max.dev_attr);
886 device_create_file(&new_client->dev, &sensor_dev_attr_in3_max.dev_attr);
887 device_create_file(&new_client->dev, &sensor_dev_attr_in4_max.dev_attr);
888 device_create_file(&new_client->dev, &sensor_dev_attr_in5_max.dev_attr);
889 device_create_file(&new_client->dev, &sensor_dev_attr_in6_max.dev_attr);
890 device_create_file(&new_client->dev, &sensor_dev_attr_in7_max.dev_attr);
891 device_create_file(&new_client->dev, &sensor_dev_attr_in8_max.dev_attr);
892 device_create_file(&new_client->dev, &sensor_dev_attr_in9_max.dev_attr);
893 device_create_file(&new_client->dev, &sensor_dev_attr_in10_max.dev_attr);
894 device_create_file(&new_client->dev, &sensor_dev_attr_in0_status.dev_attr);
895 device_create_file(&new_client->dev, &sensor_dev_attr_in1_status.dev_attr);
896 device_create_file(&new_client->dev, &sensor_dev_attr_in2_status.dev_attr);
897 device_create_file(&new_client->dev, &sensor_dev_attr_in3_status.dev_attr);
898 device_create_file(&new_client->dev, &sensor_dev_attr_in4_status.dev_attr);
899 device_create_file(&new_client->dev, &sensor_dev_attr_in5_status.dev_attr);
900 device_create_file(&new_client->dev, &sensor_dev_attr_in6_status.dev_attr);
901 device_create_file(&new_client->dev, &sensor_dev_attr_in7_status.dev_attr);
902 device_create_file(&new_client->dev, &sensor_dev_attr_in8_status.dev_attr);
903 device_create_file(&new_client->dev, &sensor_dev_attr_in9_status.dev_attr);
904 device_create_file(&new_client->dev, &sensor_dev_attr_in10_status.dev_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905
906 device_create_file(&new_client->dev, &dev_attr_cpu0_vid);
907 device_create_file(&new_client->dev, &dev_attr_vrm);
908 device_create_file(&new_client->dev, &dev_attr_alarms_in);
909 }
910
911 if (data->tempnr) {
Jim Cromief0986bd2005-09-02 22:52:43 +0200912 device_create_file(&new_client->dev, &sensor_dev_attr_temp1_input.dev_attr);
913 device_create_file(&new_client->dev, &sensor_dev_attr_temp2_input.dev_attr);
914 device_create_file(&new_client->dev, &sensor_dev_attr_temp1_min.dev_attr);
915 device_create_file(&new_client->dev, &sensor_dev_attr_temp2_min.dev_attr);
916 device_create_file(&new_client->dev, &sensor_dev_attr_temp1_max.dev_attr);
917 device_create_file(&new_client->dev, &sensor_dev_attr_temp2_max.dev_attr);
918 device_create_file(&new_client->dev, &sensor_dev_attr_temp1_crit.dev_attr);
919 device_create_file(&new_client->dev, &sensor_dev_attr_temp2_crit.dev_attr);
920 device_create_file(&new_client->dev, &sensor_dev_attr_temp1_status.dev_attr);
921 device_create_file(&new_client->dev, &sensor_dev_attr_temp2_status.dev_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
923 device_create_file(&new_client->dev, &dev_attr_alarms_temp);
924 }
925 if (data->tempnr == 3) {
Jim Cromief0986bd2005-09-02 22:52:43 +0200926 device_create_file(&new_client->dev, &sensor_dev_attr_temp3_input.dev_attr);
927 device_create_file(&new_client->dev, &sensor_dev_attr_temp3_min.dev_attr);
928 device_create_file(&new_client->dev, &sensor_dev_attr_temp3_max.dev_attr);
929 device_create_file(&new_client->dev, &sensor_dev_attr_temp3_crit.dev_attr);
930 device_create_file(&new_client->dev, &sensor_dev_attr_temp3_status.dev_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 }
932 if (data->innr == 14) {
Jim Cromief0986bd2005-09-02 22:52:43 +0200933 device_create_file(&new_client->dev, &sensor_dev_attr_temp4_input.dev_attr);
934 device_create_file(&new_client->dev, &sensor_dev_attr_temp5_input.dev_attr);
935 device_create_file(&new_client->dev, &sensor_dev_attr_temp6_input.dev_attr);
936 device_create_file(&new_client->dev, &sensor_dev_attr_temp4_min.dev_attr);
937 device_create_file(&new_client->dev, &sensor_dev_attr_temp5_min.dev_attr);
938 device_create_file(&new_client->dev, &sensor_dev_attr_temp6_min.dev_attr);
939 device_create_file(&new_client->dev, &sensor_dev_attr_temp4_max.dev_attr);
940 device_create_file(&new_client->dev, &sensor_dev_attr_temp5_max.dev_attr);
941 device_create_file(&new_client->dev, &sensor_dev_attr_temp6_max.dev_attr);
942 device_create_file(&new_client->dev, &sensor_dev_attr_temp4_crit.dev_attr);
943 device_create_file(&new_client->dev, &sensor_dev_attr_temp5_crit.dev_attr);
944 device_create_file(&new_client->dev, &sensor_dev_attr_temp6_crit.dev_attr);
945 device_create_file(&new_client->dev, &sensor_dev_attr_temp4_status.dev_attr);
946 device_create_file(&new_client->dev, &sensor_dev_attr_temp5_status.dev_attr);
947 device_create_file(&new_client->dev, &sensor_dev_attr_temp6_status.dev_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 }
949
950 if (data->fannr) {
951 if (FAN_CONFIG_MONITOR(data->fan_conf, 0)) {
952 device_create_file(&new_client->dev,
Jim Cromief0986bd2005-09-02 22:52:43 +0200953 &sensor_dev_attr_fan1_input.dev_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 device_create_file(&new_client->dev,
Jim Cromief0986bd2005-09-02 22:52:43 +0200955 &sensor_dev_attr_fan1_min.dev_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 device_create_file(&new_client->dev,
Jim Cromief0986bd2005-09-02 22:52:43 +0200957 &sensor_dev_attr_fan1_div.dev_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 device_create_file(&new_client->dev,
Jim Cromief0986bd2005-09-02 22:52:43 +0200959 &sensor_dev_attr_fan1_status.dev_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 }
961
962 if (FAN_CONFIG_MONITOR(data->fan_conf, 1)) {
963 device_create_file(&new_client->dev,
Jim Cromief0986bd2005-09-02 22:52:43 +0200964 &sensor_dev_attr_fan2_input.dev_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 device_create_file(&new_client->dev,
Jim Cromief0986bd2005-09-02 22:52:43 +0200966 &sensor_dev_attr_fan2_min.dev_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 device_create_file(&new_client->dev,
Jim Cromief0986bd2005-09-02 22:52:43 +0200968 &sensor_dev_attr_fan2_div.dev_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 device_create_file(&new_client->dev,
Jim Cromief0986bd2005-09-02 22:52:43 +0200970 &sensor_dev_attr_fan2_status.dev_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 }
972
973 if (FAN_CONFIG_CONTROL(data->fan_conf, 0))
Jim Cromief0986bd2005-09-02 22:52:43 +0200974 device_create_file(&new_client->dev, &sensor_dev_attr_pwm1.dev_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 if (FAN_CONFIG_CONTROL(data->fan_conf, 1))
Jim Cromief0986bd2005-09-02 22:52:43 +0200976 device_create_file(&new_client->dev, &sensor_dev_attr_pwm2.dev_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 }
978 if (data->fannr == 3) {
979 if (FAN_CONFIG_MONITOR(data->fan_conf, 2)) {
980 device_create_file(&new_client->dev,
Jim Cromief0986bd2005-09-02 22:52:43 +0200981 &sensor_dev_attr_fan3_input.dev_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 device_create_file(&new_client->dev,
Jim Cromief0986bd2005-09-02 22:52:43 +0200983 &sensor_dev_attr_fan3_min.dev_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 device_create_file(&new_client->dev,
Jim Cromief0986bd2005-09-02 22:52:43 +0200985 &sensor_dev_attr_fan3_div.dev_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 device_create_file(&new_client->dev,
Jim Cromief0986bd2005-09-02 22:52:43 +0200987 &sensor_dev_attr_fan3_status.dev_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 }
989
990 if (FAN_CONFIG_CONTROL(data->fan_conf, 2))
Jim Cromief0986bd2005-09-02 22:52:43 +0200991 device_create_file(&new_client->dev, &sensor_dev_attr_pwm3.dev_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 }
993
994 return 0;
995
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400996ERROR3:
997 i2c_detach_client(new_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998ERROR2:
999 for (i = 0; i < 3; i++) {
1000 if (data->address[i]) {
1001 release_region(data->address[i], PC87360_EXTENT);
1002 }
1003 }
1004ERROR1:
1005 kfree(data);
1006 return err;
1007}
1008
1009static int pc87360_detach_client(struct i2c_client *client)
1010{
1011 struct pc87360_data *data = i2c_get_clientdata(client);
1012 int i;
1013
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001014 hwmon_device_unregister(data->class_dev);
1015
Jean Delvare7bef5592005-07-27 22:14:49 +02001016 if ((i = i2c_detach_client(client)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018
1019 for (i = 0; i < 3; i++) {
1020 if (data->address[i]) {
1021 release_region(data->address[i], PC87360_EXTENT);
1022 }
1023 }
1024 kfree(data);
1025
1026 return 0;
1027}
1028
1029/* ldi is the logical device index
1030 bank is for voltages and temperatures only */
1031static int pc87360_read_value(struct pc87360_data *data, u8 ldi, u8 bank,
1032 u8 reg)
1033{
1034 int res;
1035
1036 down(&(data->lock));
1037 if (bank != NO_BANK)
1038 outb_p(bank, data->address[ldi] + PC87365_REG_BANK);
1039 res = inb_p(data->address[ldi] + reg);
1040 up(&(data->lock));
1041
1042 return res;
1043}
1044
1045static void pc87360_write_value(struct pc87360_data *data, u8 ldi, u8 bank,
1046 u8 reg, u8 value)
1047{
1048 down(&(data->lock));
1049 if (bank != NO_BANK)
1050 outb_p(bank, data->address[ldi] + PC87365_REG_BANK);
1051 outb_p(value, data->address[ldi] + reg);
1052 up(&(data->lock));
1053}
1054
1055static void pc87360_init_client(struct i2c_client *client, int use_thermistors)
1056{
1057 struct pc87360_data *data = i2c_get_clientdata(client);
1058 int i, nr;
1059 const u8 init_in[14] = { 2, 2, 2, 2, 2, 2, 2, 1, 1, 3, 1, 2, 2, 2 };
1060 const u8 init_temp[3] = { 2, 2, 1 };
1061 u8 reg;
1062
1063 if (init >= 2 && data->innr) {
1064 reg = pc87360_read_value(data, LD_IN, NO_BANK,
1065 PC87365_REG_IN_CONVRATE);
Jean Delvare368609c2005-07-29 12:15:07 -07001066 dev_info(&client->dev, "VLM conversion set to "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 "1s period, 160us delay\n");
1068 pc87360_write_value(data, LD_IN, NO_BANK,
1069 PC87365_REG_IN_CONVRATE,
1070 (reg & 0xC0) | 0x11);
1071 }
1072
1073 nr = data->innr < 11 ? data->innr : 11;
1074 for (i=0; i<nr; i++) {
1075 if (init >= init_in[i]) {
1076 /* Forcibly enable voltage channel */
1077 reg = pc87360_read_value(data, LD_IN, i,
1078 PC87365_REG_IN_STATUS);
1079 if (!(reg & 0x01)) {
1080 dev_dbg(&client->dev, "Forcibly "
1081 "enabling in%d\n", i);
1082 pc87360_write_value(data, LD_IN, i,
1083 PC87365_REG_IN_STATUS,
1084 (reg & 0x68) | 0x87);
1085 }
1086 }
1087 }
1088
1089 /* We can't blindly trust the Super-I/O space configuration bit,
1090 most BIOS won't set it properly */
1091 for (i=11; i<data->innr; i++) {
1092 reg = pc87360_read_value(data, LD_IN, i,
1093 PC87365_REG_TEMP_STATUS);
1094 use_thermistors = use_thermistors || (reg & 0x01);
1095 }
1096
1097 i = use_thermistors ? 2 : 0;
1098 for (; i<data->tempnr; i++) {
1099 if (init >= init_temp[i]) {
1100 /* Forcibly enable temperature channel */
1101 reg = pc87360_read_value(data, LD_TEMP, i,
1102 PC87365_REG_TEMP_STATUS);
1103 if (!(reg & 0x01)) {
1104 dev_dbg(&client->dev, "Forcibly "
1105 "enabling temp%d\n", i+1);
1106 pc87360_write_value(data, LD_TEMP, i,
1107 PC87365_REG_TEMP_STATUS,
1108 0xCF);
1109 }
1110 }
1111 }
1112
1113 if (use_thermistors) {
1114 for (i=11; i<data->innr; i++) {
1115 if (init >= init_in[i]) {
1116 /* The pin may already be used by thermal
1117 diodes */
1118 reg = pc87360_read_value(data, LD_TEMP,
1119 (i-11)/2, PC87365_REG_TEMP_STATUS);
1120 if (reg & 0x01) {
1121 dev_dbg(&client->dev, "Skipping "
1122 "temp%d, pin already in use "
1123 "by temp%d\n", i-7, (i-11)/2);
1124 continue;
1125 }
1126
1127 /* Forcibly enable thermistor channel */
1128 reg = pc87360_read_value(data, LD_IN, i,
1129 PC87365_REG_IN_STATUS);
1130 if (!(reg & 0x01)) {
1131 dev_dbg(&client->dev, "Forcibly "
1132 "enabling temp%d\n", i-7);
1133 pc87360_write_value(data, LD_IN, i,
1134 PC87365_REG_TEMP_STATUS,
1135 (reg & 0x60) | 0x8F);
1136 }
1137 }
1138 }
1139 }
1140
1141 if (data->innr) {
1142 reg = pc87360_read_value(data, LD_IN, NO_BANK,
1143 PC87365_REG_IN_CONFIG);
1144 if (reg & 0x01) {
1145 dev_dbg(&client->dev, "Forcibly "
1146 "enabling monitoring (VLM)\n");
1147 pc87360_write_value(data, LD_IN, NO_BANK,
1148 PC87365_REG_IN_CONFIG,
1149 reg & 0xFE);
1150 }
1151 }
1152
1153 if (data->tempnr) {
1154 reg = pc87360_read_value(data, LD_TEMP, NO_BANK,
1155 PC87365_REG_TEMP_CONFIG);
1156 if (reg & 0x01) {
1157 dev_dbg(&client->dev, "Forcibly enabling "
1158 "monitoring (TMS)\n");
1159 pc87360_write_value(data, LD_TEMP, NO_BANK,
1160 PC87365_REG_TEMP_CONFIG,
1161 reg & 0xFE);
1162 }
1163
1164 if (init >= 2) {
1165 /* Chip config as documented by National Semi. */
1166 pc87360_write_value(data, LD_TEMP, 0xF, 0xA, 0x08);
1167 /* We voluntarily omit the bank here, in case the
1168 sequence itself matters. It shouldn't be a problem,
1169 since nobody else is supposed to access the
1170 device at that point. */
1171 pc87360_write_value(data, LD_TEMP, NO_BANK, 0xB, 0x04);
1172 pc87360_write_value(data, LD_TEMP, NO_BANK, 0xC, 0x35);
1173 pc87360_write_value(data, LD_TEMP, NO_BANK, 0xD, 0x05);
1174 pc87360_write_value(data, LD_TEMP, NO_BANK, 0xE, 0x05);
1175 }
1176 }
1177}
1178
1179static void pc87360_autodiv(struct i2c_client *client, int nr)
1180{
1181 struct pc87360_data *data = i2c_get_clientdata(client);
1182 u8 old_min = data->fan_min[nr];
1183
1184 /* Increase clock divider if needed and possible */
1185 if ((data->fan_status[nr] & 0x04) /* overflow flag */
1186 || (data->fan[nr] >= 224)) { /* next to overflow */
1187 if ((data->fan_status[nr] & 0x60) != 0x60) {
1188 data->fan_status[nr] += 0x20;
1189 data->fan_min[nr] >>= 1;
1190 data->fan[nr] >>= 1;
1191 dev_dbg(&client->dev, "Increasing "
1192 "clock divider to %d for fan %d\n",
1193 FAN_DIV_FROM_REG(data->fan_status[nr]), nr+1);
1194 }
1195 } else {
1196 /* Decrease clock divider if possible */
1197 while (!(data->fan_min[nr] & 0x80) /* min "nails" divider */
1198 && data->fan[nr] < 85 /* bad accuracy */
1199 && (data->fan_status[nr] & 0x60) != 0x00) {
1200 data->fan_status[nr] -= 0x20;
1201 data->fan_min[nr] <<= 1;
1202 data->fan[nr] <<= 1;
1203 dev_dbg(&client->dev, "Decreasing "
1204 "clock divider to %d for fan %d\n",
1205 FAN_DIV_FROM_REG(data->fan_status[nr]),
1206 nr+1);
1207 }
1208 }
1209
1210 /* Write new fan min if it changed */
1211 if (old_min != data->fan_min[nr]) {
1212 pc87360_write_value(data, LD_FAN, NO_BANK,
1213 PC87360_REG_FAN_MIN(nr),
1214 data->fan_min[nr]);
1215 }
1216}
1217
1218static struct pc87360_data *pc87360_update_device(struct device *dev)
1219{
1220 struct i2c_client *client = to_i2c_client(dev);
1221 struct pc87360_data *data = i2c_get_clientdata(client);
1222 u8 i;
1223
1224 down(&data->update_lock);
1225
1226 if (time_after(jiffies, data->last_updated + HZ * 2) || !data->valid) {
1227 dev_dbg(&client->dev, "Data update\n");
1228
1229 /* Fans */
1230 for (i = 0; i < data->fannr; i++) {
1231 if (FAN_CONFIG_MONITOR(data->fan_conf, i)) {
1232 data->fan_status[i] =
1233 pc87360_read_value(data, LD_FAN,
1234 NO_BANK, PC87360_REG_FAN_STATUS(i));
1235 data->fan[i] = pc87360_read_value(data, LD_FAN,
1236 NO_BANK, PC87360_REG_FAN(i));
1237 data->fan_min[i] = pc87360_read_value(data,
1238 LD_FAN, NO_BANK,
1239 PC87360_REG_FAN_MIN(i));
1240 /* Change clock divider if needed */
1241 pc87360_autodiv(client, i);
1242 /* Clear bits and write new divider */
1243 pc87360_write_value(data, LD_FAN, NO_BANK,
1244 PC87360_REG_FAN_STATUS(i),
1245 data->fan_status[i]);
1246 }
1247 if (FAN_CONFIG_CONTROL(data->fan_conf, i))
1248 data->pwm[i] = pc87360_read_value(data, LD_FAN,
1249 NO_BANK, PC87360_REG_PWM(i));
1250 }
1251
1252 /* Voltages */
1253 for (i = 0; i < data->innr; i++) {
1254 data->in_status[i] = pc87360_read_value(data, LD_IN, i,
1255 PC87365_REG_IN_STATUS);
1256 /* Clear bits */
1257 pc87360_write_value(data, LD_IN, i,
1258 PC87365_REG_IN_STATUS,
1259 data->in_status[i]);
1260 if ((data->in_status[i] & 0x81) == 0x81) {
1261 data->in[i] = pc87360_read_value(data, LD_IN,
1262 i, PC87365_REG_IN);
1263 }
1264 if (data->in_status[i] & 0x01) {
1265 data->in_min[i] = pc87360_read_value(data,
1266 LD_IN, i,
1267 PC87365_REG_IN_MIN);
1268 data->in_max[i] = pc87360_read_value(data,
1269 LD_IN, i,
1270 PC87365_REG_IN_MAX);
1271 if (i >= 11)
1272 data->in_crit[i-11] =
1273 pc87360_read_value(data, LD_IN,
1274 i, PC87365_REG_TEMP_CRIT);
1275 }
1276 }
1277 if (data->innr) {
1278 data->in_alarms = pc87360_read_value(data, LD_IN,
1279 NO_BANK, PC87365_REG_IN_ALARMS1)
1280 | ((pc87360_read_value(data, LD_IN,
1281 NO_BANK, PC87365_REG_IN_ALARMS2)
1282 & 0x07) << 8);
1283 data->vid = (data->vid_conf & 0xE0) ?
1284 pc87360_read_value(data, LD_IN,
1285 NO_BANK, PC87365_REG_VID) : 0x1F;
1286 }
1287
1288 /* Temperatures */
1289 for (i = 0; i < data->tempnr; i++) {
1290 data->temp_status[i] = pc87360_read_value(data,
1291 LD_TEMP, i,
1292 PC87365_REG_TEMP_STATUS);
1293 /* Clear bits */
1294 pc87360_write_value(data, LD_TEMP, i,
1295 PC87365_REG_TEMP_STATUS,
1296 data->temp_status[i]);
1297 if ((data->temp_status[i] & 0x81) == 0x81) {
1298 data->temp[i] = pc87360_read_value(data,
1299 LD_TEMP, i,
1300 PC87365_REG_TEMP);
1301 }
1302 if (data->temp_status[i] & 0x01) {
1303 data->temp_min[i] = pc87360_read_value(data,
1304 LD_TEMP, i,
1305 PC87365_REG_TEMP_MIN);
1306 data->temp_max[i] = pc87360_read_value(data,
1307 LD_TEMP, i,
1308 PC87365_REG_TEMP_MAX);
1309 data->temp_crit[i] = pc87360_read_value(data,
1310 LD_TEMP, i,
1311 PC87365_REG_TEMP_CRIT);
1312 }
1313 }
1314 if (data->tempnr) {
1315 data->temp_alarms = pc87360_read_value(data, LD_TEMP,
1316 NO_BANK, PC87365_REG_TEMP_ALARMS)
1317 & 0x3F;
1318 }
1319
1320 data->last_updated = jiffies;
1321 data->valid = 1;
1322 }
1323
1324 up(&data->update_lock);
1325
1326 return data;
1327}
1328
1329static int __init pc87360_init(void)
1330{
1331 int i;
1332
1333 if (pc87360_find(0x2e, &devid, extra_isa)
1334 && pc87360_find(0x4e, &devid, extra_isa)) {
1335 printk(KERN_WARNING "pc87360: PC8736x not detected, "
1336 "module not inserted.\n");
1337 return -ENODEV;
1338 }
1339
1340 /* Arbitrarily pick one of the addresses */
1341 for (i = 0; i < 3; i++) {
1342 if (extra_isa[i] != 0x0000) {
Jean Delvare2d8672c2005-07-19 23:56:35 +02001343 address = extra_isa[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 break;
1345 }
1346 }
1347
Jean Delvare2d8672c2005-07-19 23:56:35 +02001348 if (address == 0x0000) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 printk(KERN_WARNING "pc87360: No active logical device, "
1350 "module not inserted.\n");
1351 return -ENODEV;
1352 }
1353
Jean Delvarefde09502005-07-19 23:51:07 +02001354 return i2c_isa_add_driver(&pc87360_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355}
1356
1357static void __exit pc87360_exit(void)
1358{
Jean Delvarefde09502005-07-19 23:51:07 +02001359 i2c_isa_del_driver(&pc87360_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360}
1361
1362
1363MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
1364MODULE_DESCRIPTION("PC8736x hardware monitor");
1365MODULE_LICENSE("GPL");
1366
1367module_init(pc87360_init);
1368module_exit(pc87360_exit);