blob: 5469489c758a16931361b944d312a77122d1b294 [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 = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100239 .driver = {
240 .owner = THIS_MODULE,
241 .name = "pc87360",
242 },
Jean Delvare2d8672c2005-07-19 23:56:35 +0200243 .attach_adapter = pc87360_detect,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 .detach_client = pc87360_detach_client,
245};
246
247/*
248 * Sysfs stuff
249 */
250
Jim Cromief0986bd2005-09-02 22:52:43 +0200251static ssize_t show_fan_input(struct device *dev, struct device_attribute *devattr, char *buf)
252{
253 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
254 struct pc87360_data *data = pc87360_update_device(dev);
Jim Cromie694fa052005-09-02 22:57:52 +0200255 return sprintf(buf, "%u\n", FAN_FROM_REG(data->fan[attr->index],
256 FAN_DIV_FROM_REG(data->fan_status[attr->index])));
Jim Cromief0986bd2005-09-02 22:52:43 +0200257}
258static ssize_t show_fan_min(struct device *dev, struct device_attribute *devattr, char *buf)
259{
260 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
261 struct pc87360_data *data = pc87360_update_device(dev);
Jim Cromie694fa052005-09-02 22:57:52 +0200262 return sprintf(buf, "%u\n", FAN_FROM_REG(data->fan_min[attr->index],
263 FAN_DIV_FROM_REG(data->fan_status[attr->index])));
Jim Cromief0986bd2005-09-02 22:52:43 +0200264}
265static ssize_t show_fan_div(struct device *dev, struct device_attribute *devattr, char *buf)
266{
267 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
268 struct pc87360_data *data = pc87360_update_device(dev);
269 return sprintf(buf, "%u\n",
Jim Cromie694fa052005-09-02 22:57:52 +0200270 FAN_DIV_FROM_REG(data->fan_status[attr->index]));
Jim Cromief0986bd2005-09-02 22:52:43 +0200271}
272static ssize_t show_fan_status(struct device *dev, struct device_attribute *devattr, char *buf)
273{
274 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
275 struct pc87360_data *data = pc87360_update_device(dev);
276 return sprintf(buf, "%u\n",
Jim Cromie694fa052005-09-02 22:57:52 +0200277 FAN_STATUS_FROM_REG(data->fan_status[attr->index]));
Jim Cromief0986bd2005-09-02 22:52:43 +0200278}
279static ssize_t set_fan_min(struct device *dev, struct device_attribute *devattr, const char *buf,
280 size_t count)
281{
282 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Jim Cromie11be27e2005-09-02 23:05:07 +0200283 struct i2c_client *client = to_i2c_client(dev);
284 struct pc87360_data *data = i2c_get_clientdata(client);
285 long fan_min = simple_strtol(buf, NULL, 10);
286
287 down(&data->update_lock);
288 fan_min = FAN_TO_REG(fan_min, FAN_DIV_FROM_REG(data->fan_status[attr->index]));
289
290 /* If it wouldn't fit, change clock divisor */
291 while (fan_min > 255
292 && (data->fan_status[attr->index] & 0x60) != 0x60) {
293 fan_min >>= 1;
294 data->fan[attr->index] >>= 1;
295 data->fan_status[attr->index] += 0x20;
296 }
297 data->fan_min[attr->index] = fan_min > 255 ? 255 : fan_min;
298 pc87360_write_value(data, LD_FAN, NO_BANK, PC87360_REG_FAN_MIN(attr->index),
299 data->fan_min[attr->index]);
300
301 /* Write new divider, preserve alarm bits */
302 pc87360_write_value(data, LD_FAN, NO_BANK, PC87360_REG_FAN_STATUS(attr->index),
303 data->fan_status[attr->index] & 0xF9);
304 up(&data->update_lock);
305
306 return count;
Jim Cromief0986bd2005-09-02 22:52:43 +0200307}
308
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309#define show_and_set_fan(offset) \
Jim Cromief0986bd2005-09-02 22:52:43 +0200310static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
Jim Cromie694fa052005-09-02 22:57:52 +0200311 show_fan_input, NULL, offset-1); \
Jim Cromief0986bd2005-09-02 22:52:43 +0200312static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IWUSR | S_IRUGO, \
Jim Cromie694fa052005-09-02 22:57:52 +0200313 show_fan_min, set_fan_min, offset-1); \
Jim Cromief0986bd2005-09-02 22:52:43 +0200314static SENSOR_DEVICE_ATTR(fan##offset##_div, S_IRUGO, \
Jim Cromie694fa052005-09-02 22:57:52 +0200315 show_fan_div, NULL, offset-1); \
Jim Cromief0986bd2005-09-02 22:52:43 +0200316static SENSOR_DEVICE_ATTR(fan##offset##_status, S_IRUGO, \
Jim Cromie694fa052005-09-02 22:57:52 +0200317 show_fan_status, NULL, offset-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318show_and_set_fan(1)
319show_and_set_fan(2)
320show_and_set_fan(3)
321
Jim Cromief0986bd2005-09-02 22:52:43 +0200322static ssize_t show_pwm(struct device *dev, struct device_attribute *devattr, char *buf)
323{
324 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
325 struct pc87360_data *data = pc87360_update_device(dev);
326 return sprintf(buf, "%u\n",
Jim Cromie694fa052005-09-02 22:57:52 +0200327 PWM_FROM_REG(data->pwm[attr->index],
Jim Cromief0986bd2005-09-02 22:52:43 +0200328 FAN_CONFIG_INVERT(data->fan_conf,
Jim Cromie694fa052005-09-02 22:57:52 +0200329 attr->index)));
Jim Cromief0986bd2005-09-02 22:52:43 +0200330}
331static ssize_t set_pwm(struct device *dev, struct device_attribute *devattr, const char *buf,
332 size_t count)
333{
334 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
335 struct i2c_client *client = to_i2c_client(dev);
336 struct pc87360_data *data = i2c_get_clientdata(client);
337 long val = simple_strtol(buf, NULL, 10);
338
339 down(&data->update_lock);
Jim Cromie694fa052005-09-02 22:57:52 +0200340 data->pwm[attr->index] = PWM_TO_REG(val,
341 FAN_CONFIG_INVERT(data->fan_conf, attr->index));
342 pc87360_write_value(data, LD_FAN, NO_BANK, PC87360_REG_PWM(attr->index),
343 data->pwm[attr->index]);
Jim Cromief0986bd2005-09-02 22:52:43 +0200344 up(&data->update_lock);
345 return count;
346}
347
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348#define show_and_set_pwm(offset) \
Jim Cromief0986bd2005-09-02 22:52:43 +0200349static SENSOR_DEVICE_ATTR(pwm##offset, S_IWUSR | S_IRUGO, \
Jim Cromie694fa052005-09-02 22:57:52 +0200350 show_pwm, set_pwm, offset-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351show_and_set_pwm(1)
352show_and_set_pwm(2)
353show_and_set_pwm(3)
354
Jim Cromief0986bd2005-09-02 22:52:43 +0200355static ssize_t show_in_input(struct device *dev, struct device_attribute *devattr, char *buf)
356{
357 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
358 struct pc87360_data *data = pc87360_update_device(dev);
359 return sprintf(buf, "%u\n", IN_FROM_REG(data->in[attr->index],
360 data->in_vref));
361}
362static ssize_t show_in_min(struct device *dev, struct device_attribute *devattr, char *buf)
363{
364 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
365 struct pc87360_data *data = pc87360_update_device(dev);
366 return sprintf(buf, "%u\n", IN_FROM_REG(data->in_min[attr->index],
367 data->in_vref));
368}
369static ssize_t show_in_max(struct device *dev, struct device_attribute *devattr, char *buf)
370{
371 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
372 struct pc87360_data *data = pc87360_update_device(dev);
373 return sprintf(buf, "%u\n", IN_FROM_REG(data->in_max[attr->index],
374 data->in_vref));
375}
376static ssize_t show_in_status(struct device *dev, struct device_attribute *devattr, char *buf)
377{
378 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
379 struct pc87360_data *data = pc87360_update_device(dev);
380 return sprintf(buf, "%u\n", data->in_status[attr->index]);
381}
382static ssize_t set_in_min(struct device *dev, struct device_attribute *devattr, const char *buf,
383 size_t count)
384{
385 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
386 struct i2c_client *client = to_i2c_client(dev);
387 struct pc87360_data *data = i2c_get_clientdata(client);
388 long val = simple_strtol(buf, NULL, 10);
389
390 down(&data->update_lock);
391 data->in_min[attr->index] = IN_TO_REG(val, data->in_vref);
392 pc87360_write_value(data, LD_IN, attr->index, PC87365_REG_IN_MIN,
393 data->in_min[attr->index]);
394 up(&data->update_lock);
395 return count;
396}
397static ssize_t set_in_max(struct device *dev, struct device_attribute *devattr, const char *buf,
398 size_t count)
399{
400 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
401 struct i2c_client *client = to_i2c_client(dev);
402 struct pc87360_data *data = i2c_get_clientdata(client);
403 long val = simple_strtol(buf, NULL, 10);
404
405 down(&data->update_lock);
406 data->in_max[attr->index] = IN_TO_REG(val,
407 data->in_vref);
408 pc87360_write_value(data, LD_IN, attr->index, PC87365_REG_IN_MAX,
409 data->in_max[attr->index]);
410 up(&data->update_lock);
411 return count;
412}
413
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414#define show_and_set_in(offset) \
Jim Cromief0986bd2005-09-02 22:52:43 +0200415static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \
416 show_in_input, NULL, offset); \
417static SENSOR_DEVICE_ATTR(in##offset##_min, S_IWUSR | S_IRUGO, \
418 show_in_min, set_in_min, offset); \
419static SENSOR_DEVICE_ATTR(in##offset##_max, S_IWUSR | S_IRUGO, \
420 show_in_max, set_in_max, offset); \
421static SENSOR_DEVICE_ATTR(in##offset##_status, S_IRUGO, \
422 show_in_status, NULL, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423show_and_set_in(0)
424show_and_set_in(1)
425show_and_set_in(2)
426show_and_set_in(3)
427show_and_set_in(4)
428show_and_set_in(5)
429show_and_set_in(6)
430show_and_set_in(7)
431show_and_set_in(8)
432show_and_set_in(9)
433show_and_set_in(10)
434
Jim Cromief0986bd2005-09-02 22:52:43 +0200435static ssize_t show_therm_input(struct device *dev, struct device_attribute *devattr, char *buf)
436{
437 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
438 struct pc87360_data *data = pc87360_update_device(dev);
Jim Cromie694fa052005-09-02 22:57:52 +0200439 return sprintf(buf, "%u\n", IN_FROM_REG(data->in[attr->index],
Jim Cromief0986bd2005-09-02 22:52:43 +0200440 data->in_vref));
441}
442static ssize_t show_therm_min(struct device *dev, struct device_attribute *devattr, char *buf)
443{
444 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
445 struct pc87360_data *data = pc87360_update_device(dev);
Jim Cromie694fa052005-09-02 22:57:52 +0200446 return sprintf(buf, "%u\n", IN_FROM_REG(data->in_min[attr->index],
Jim Cromief0986bd2005-09-02 22:52:43 +0200447 data->in_vref));
448}
449static ssize_t show_therm_max(struct device *dev, struct device_attribute *devattr, char *buf)
450{
451 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
452 struct pc87360_data *data = pc87360_update_device(dev);
Jim Cromie694fa052005-09-02 22:57:52 +0200453 return sprintf(buf, "%u\n", IN_FROM_REG(data->in_max[attr->index],
Jim Cromief0986bd2005-09-02 22:52:43 +0200454 data->in_vref));
455}
456static ssize_t show_therm_crit(struct device *dev, struct device_attribute *devattr, char *buf)
457{
458 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
459 struct pc87360_data *data = pc87360_update_device(dev);
Jim Cromie694fa052005-09-02 22:57:52 +0200460 return sprintf(buf, "%u\n", IN_FROM_REG(data->in_crit[attr->index-11],
Jim Cromief0986bd2005-09-02 22:52:43 +0200461 data->in_vref));
462}
463static ssize_t show_therm_status(struct device *dev, struct device_attribute *devattr, char *buf)
464{
465 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
466 struct pc87360_data *data = pc87360_update_device(dev);
Jim Cromie694fa052005-09-02 22:57:52 +0200467 return sprintf(buf, "%u\n", data->in_status[attr->index]);
Jim Cromief0986bd2005-09-02 22:52:43 +0200468}
469static ssize_t set_therm_min(struct device *dev, struct device_attribute *devattr, const char *buf,
470 size_t count)
471{
472 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
473 struct i2c_client *client = to_i2c_client(dev);
474 struct pc87360_data *data = i2c_get_clientdata(client);
475 long val = simple_strtol(buf, NULL, 10);
476
477 down(&data->update_lock);
Jim Cromie694fa052005-09-02 22:57:52 +0200478 data->in_min[attr->index] = IN_TO_REG(val, data->in_vref);
479 pc87360_write_value(data, LD_IN, attr->index, PC87365_REG_TEMP_MIN,
480 data->in_min[attr->index]);
Jim Cromief0986bd2005-09-02 22:52:43 +0200481 up(&data->update_lock);
482 return count;
483}
484static ssize_t set_therm_max(struct device *dev, struct device_attribute *devattr, const char *buf,
485 size_t count)
486{
487 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
488 struct i2c_client *client = to_i2c_client(dev);
489 struct pc87360_data *data = i2c_get_clientdata(client);
490 long val = simple_strtol(buf, NULL, 10);
491
492 down(&data->update_lock);
Jim Cromie694fa052005-09-02 22:57:52 +0200493 data->in_max[attr->index] = IN_TO_REG(val, data->in_vref);
494 pc87360_write_value(data, LD_IN, attr->index, PC87365_REG_TEMP_MAX,
495 data->in_max[attr->index]);
Jim Cromief0986bd2005-09-02 22:52:43 +0200496 up(&data->update_lock);
497 return count;
498}
499static ssize_t set_therm_crit(struct device *dev, struct device_attribute *devattr, const char *buf,
500 size_t count)
501{
502 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
503 struct i2c_client *client = to_i2c_client(dev);
504 struct pc87360_data *data = i2c_get_clientdata(client);
505 long val = simple_strtol(buf, NULL, 10);
506
507 down(&data->update_lock);
Jim Cromie694fa052005-09-02 22:57:52 +0200508 data->in_crit[attr->index-11] = IN_TO_REG(val, data->in_vref);
509 pc87360_write_value(data, LD_IN, attr->index, PC87365_REG_TEMP_CRIT,
510 data->in_crit[attr->index-11]);
Jim Cromief0986bd2005-09-02 22:52:43 +0200511 up(&data->update_lock);
512 return count;
513}
514
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515#define show_and_set_therm(offset) \
Jim Cromief0986bd2005-09-02 22:52:43 +0200516static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
Jim Cromie694fa052005-09-02 22:57:52 +0200517 show_therm_input, NULL, 11+offset-4); \
Jim Cromief0986bd2005-09-02 22:52:43 +0200518static SENSOR_DEVICE_ATTR(temp##offset##_min, S_IWUSR | S_IRUGO, \
Jim Cromie694fa052005-09-02 22:57:52 +0200519 show_therm_min, set_therm_min, 11+offset-4); \
Jim Cromief0986bd2005-09-02 22:52:43 +0200520static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IWUSR | S_IRUGO, \
Jim Cromie694fa052005-09-02 22:57:52 +0200521 show_therm_max, set_therm_max, 11+offset-4); \
Jim Cromief0986bd2005-09-02 22:52:43 +0200522static SENSOR_DEVICE_ATTR(temp##offset##_crit, S_IWUSR | S_IRUGO, \
Jim Cromie694fa052005-09-02 22:57:52 +0200523 show_therm_crit, set_therm_crit, 11+offset-4); \
Jim Cromief0986bd2005-09-02 22:52:43 +0200524static SENSOR_DEVICE_ATTR(temp##offset##_status, S_IRUGO, \
Jim Cromie694fa052005-09-02 22:57:52 +0200525 show_therm_status, NULL, 11+offset-4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526show_and_set_therm(4)
527show_and_set_therm(5)
528show_and_set_therm(6)
529
Yani Ioannoua5099cf2005-05-17 06:42:25 -0400530static ssize_t show_vid(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531{
532 struct pc87360_data *data = pc87360_update_device(dev);
533 return sprintf(buf, "%u\n", vid_from_reg(data->vid, data->vrm));
534}
535static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL);
536
Yani Ioannoua5099cf2005-05-17 06:42:25 -0400537static ssize_t show_vrm(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538{
539 struct pc87360_data *data = pc87360_update_device(dev);
540 return sprintf(buf, "%u\n", data->vrm);
541}
Yani Ioannoua5099cf2005-05-17 06:42:25 -0400542static ssize_t set_vrm(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543{
544 struct i2c_client *client = to_i2c_client(dev);
545 struct pc87360_data *data = i2c_get_clientdata(client);
546 data->vrm = simple_strtoul(buf, NULL, 10);
547 return count;
548}
549static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm, set_vrm);
550
Yani Ioannoua5099cf2005-05-17 06:42:25 -0400551static ssize_t show_in_alarms(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552{
553 struct pc87360_data *data = pc87360_update_device(dev);
554 return sprintf(buf, "%u\n", data->in_alarms);
555}
556static DEVICE_ATTR(alarms_in, S_IRUGO, show_in_alarms, NULL);
557
Jim Cromief0986bd2005-09-02 22:52:43 +0200558static ssize_t show_temp_input(struct device *dev, struct device_attribute *devattr, char *buf)
559{
560 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
561 struct pc87360_data *data = pc87360_update_device(dev);
Jim Cromie694fa052005-09-02 22:57:52 +0200562 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[attr->index]));
Jim Cromief0986bd2005-09-02 22:52:43 +0200563}
564static ssize_t show_temp_min(struct device *dev, struct device_attribute *devattr, char *buf)
565{
566 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
567 struct pc87360_data *data = pc87360_update_device(dev);
Jim Cromie694fa052005-09-02 22:57:52 +0200568 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_min[attr->index]));
Jim Cromief0986bd2005-09-02 22:52:43 +0200569}
570static ssize_t show_temp_max(struct device *dev, struct device_attribute *devattr, char *buf)
571{
572 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
573 struct pc87360_data *data = pc87360_update_device(dev);
Jim Cromie694fa052005-09-02 22:57:52 +0200574 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max[attr->index]));
Jim Cromief0986bd2005-09-02 22:52:43 +0200575}
576static ssize_t show_temp_crit(struct device *dev, struct device_attribute *devattr, char *buf)
577{
578 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
579 struct pc87360_data *data = pc87360_update_device(dev);
Jim Cromie694fa052005-09-02 22:57:52 +0200580 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_crit[attr->index]));
Jim Cromief0986bd2005-09-02 22:52:43 +0200581}
582static ssize_t show_temp_status(struct device *dev, struct device_attribute *devattr, char *buf)
583{
584 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
585 struct pc87360_data *data = pc87360_update_device(dev);
Jim Cromie694fa052005-09-02 22:57:52 +0200586 return sprintf(buf, "%d\n", data->temp_status[attr->index]);
Jim Cromief0986bd2005-09-02 22:52:43 +0200587}
588static ssize_t set_temp_min(struct device *dev, struct device_attribute *devattr, const char *buf,
589 size_t count)
590{
591 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
592 struct i2c_client *client = to_i2c_client(dev);
593 struct pc87360_data *data = i2c_get_clientdata(client);
594 long val = simple_strtol(buf, NULL, 10);
595
596 down(&data->update_lock);
Jim Cromie694fa052005-09-02 22:57:52 +0200597 data->temp_min[attr->index] = TEMP_TO_REG(val);
598 pc87360_write_value(data, LD_TEMP, attr->index, PC87365_REG_TEMP_MIN,
599 data->temp_min[attr->index]);
Jim Cromief0986bd2005-09-02 22:52:43 +0200600 up(&data->update_lock);
601 return count;
602}
603static ssize_t set_temp_max(struct device *dev, struct device_attribute *devattr, const char *buf,
604 size_t count)
605{
606 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
607 struct i2c_client *client = to_i2c_client(dev);
608 struct pc87360_data *data = i2c_get_clientdata(client);
609 long val = simple_strtol(buf, NULL, 10);
610
611 down(&data->update_lock);
Jim Cromie694fa052005-09-02 22:57:52 +0200612 data->temp_max[attr->index] = TEMP_TO_REG(val);
613 pc87360_write_value(data, LD_TEMP, attr->index, PC87365_REG_TEMP_MAX,
614 data->temp_max[attr->index]);
Jim Cromief0986bd2005-09-02 22:52:43 +0200615 up(&data->update_lock);
616 return count;
617}
618static ssize_t set_temp_crit(struct device *dev, struct device_attribute *devattr, const char *buf,
619 size_t count)
620{
621 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
622 struct i2c_client *client = to_i2c_client(dev);
623 struct pc87360_data *data = i2c_get_clientdata(client);
624 long val = simple_strtol(buf, NULL, 10);
625
626 down(&data->update_lock);
Jim Cromie694fa052005-09-02 22:57:52 +0200627 data->temp_crit[attr->index] = TEMP_TO_REG(val);
628 pc87360_write_value(data, LD_TEMP, attr->index, PC87365_REG_TEMP_CRIT,
629 data->temp_crit[attr->index]);
Jim Cromief0986bd2005-09-02 22:52:43 +0200630 up(&data->update_lock);
631 return count;
632}
633
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634#define show_and_set_temp(offset) \
Jim Cromief0986bd2005-09-02 22:52:43 +0200635static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
Jim Cromie694fa052005-09-02 22:57:52 +0200636 show_temp_input, NULL, offset-1); \
Jim Cromief0986bd2005-09-02 22:52:43 +0200637static SENSOR_DEVICE_ATTR(temp##offset##_min, S_IWUSR | S_IRUGO, \
Jim Cromie694fa052005-09-02 22:57:52 +0200638 show_temp_min, set_temp_min, offset-1); \
Jim Cromief0986bd2005-09-02 22:52:43 +0200639static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IWUSR | S_IRUGO, \
Jim Cromie694fa052005-09-02 22:57:52 +0200640 show_temp_max, set_temp_max, offset-1); \
Jim Cromief0986bd2005-09-02 22:52:43 +0200641static SENSOR_DEVICE_ATTR(temp##offset##_crit, S_IWUSR | S_IRUGO, \
Jim Cromie694fa052005-09-02 22:57:52 +0200642 show_temp_crit, set_temp_crit, offset-1); \
Jim Cromief0986bd2005-09-02 22:52:43 +0200643static SENSOR_DEVICE_ATTR(temp##offset##_status, S_IRUGO, \
Jim Cromie694fa052005-09-02 22:57:52 +0200644 show_temp_status, NULL, offset-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645show_and_set_temp(1)
646show_and_set_temp(2)
647show_and_set_temp(3)
648
Yani Ioannoua5099cf2005-05-17 06:42:25 -0400649static ssize_t show_temp_alarms(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650{
651 struct pc87360_data *data = pc87360_update_device(dev);
652 return sprintf(buf, "%u\n", data->temp_alarms);
653}
654static DEVICE_ATTR(alarms_temp, S_IRUGO, show_temp_alarms, NULL);
655
656/*
657 * Device detection, registration and update
658 */
659
Jean Delvaree6cfb3a2005-07-27 21:32:02 +0200660static int __init pc87360_find(int sioaddr, u8 *devid, unsigned short *addresses)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661{
662 u16 val;
663 int i;
664 int nrdev; /* logical device count */
665
666 /* No superio_enter */
667
668 /* Identify device */
669 val = superio_inb(sioaddr, DEVID);
670 switch (val) {
671 case 0xE1: /* PC87360 */
672 case 0xE8: /* PC87363 */
673 case 0xE4: /* PC87364 */
674 nrdev = 1;
675 break;
676 case 0xE5: /* PC87365 */
677 case 0xE9: /* PC87366 */
678 nrdev = 3;
679 break;
680 default:
681 superio_exit(sioaddr);
682 return -ENODEV;
683 }
684 /* Remember the device id */
685 *devid = val;
686
687 for (i = 0; i < nrdev; i++) {
688 /* select logical device */
689 superio_outb(sioaddr, DEV, logdev[i]);
690
691 val = superio_inb(sioaddr, ACT);
692 if (!(val & 0x01)) {
693 printk(KERN_INFO "pc87360: Device 0x%02x not "
694 "activated\n", logdev[i]);
695 continue;
696 }
697
698 val = (superio_inb(sioaddr, BASE) << 8)
699 | superio_inb(sioaddr, BASE + 1);
700 if (!val) {
701 printk(KERN_INFO "pc87360: Base address not set for "
702 "device 0x%02x\n", logdev[i]);
703 continue;
704 }
705
Jean Delvare2d8672c2005-07-19 23:56:35 +0200706 addresses[i] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
708 if (i==0) { /* Fans */
709 confreg[0] = superio_inb(sioaddr, 0xF0);
710 confreg[1] = superio_inb(sioaddr, 0xF1);
711
712#ifdef DEBUG
713 printk(KERN_DEBUG "pc87360: Fan 1: mon=%d "
714 "ctrl=%d inv=%d\n", (confreg[0]>>2)&1,
715 (confreg[0]>>3)&1, (confreg[0]>>4)&1);
716 printk(KERN_DEBUG "pc87360: Fan 2: mon=%d "
717 "ctrl=%d inv=%d\n", (confreg[0]>>5)&1,
718 (confreg[0]>>6)&1, (confreg[0]>>7)&1);
719 printk(KERN_DEBUG "pc87360: Fan 3: mon=%d "
720 "ctrl=%d inv=%d\n", confreg[1]&1,
721 (confreg[1]>>1)&1, (confreg[1]>>2)&1);
722#endif
723 } else if (i==1) { /* Voltages */
724 /* Are we using thermistors? */
725 if (*devid == 0xE9) { /* PC87366 */
726 /* These registers are not logical-device
727 specific, just that we won't need them if
728 we don't use the VLM device */
729 confreg[2] = superio_inb(sioaddr, 0x2B);
730 confreg[3] = superio_inb(sioaddr, 0x25);
731
732 if (confreg[2] & 0x40) {
733 printk(KERN_INFO "pc87360: Using "
734 "thermistors for temperature "
735 "monitoring\n");
736 }
737 if (confreg[3] & 0xE0) {
738 printk(KERN_INFO "pc87360: VID "
739 "inputs routed (mode %u)\n",
740 confreg[3] >> 5);
741 }
742 }
743 }
744 }
745
746 superio_exit(sioaddr);
747 return 0;
748}
749
Jean Delvare2d8672c2005-07-19 23:56:35 +0200750static int pc87360_detect(struct i2c_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751{
752 int i;
753 struct i2c_client *new_client;
754 struct pc87360_data *data;
755 int err = 0;
756 const char *name = "pc87360";
757 int use_thermistors = 0;
758
Deepak Saxenaba9c2e82005-10-17 23:08:32 +0200759 if (!(data = kzalloc(sizeof(struct pc87360_data), GFP_KERNEL)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
762 new_client = &data->client;
763 i2c_set_clientdata(new_client, data);
764 new_client->addr = address;
765 init_MUTEX(&data->lock);
766 new_client->adapter = adapter;
767 new_client->driver = &pc87360_driver;
768 new_client->flags = 0;
769
770 data->fannr = 2;
771 data->innr = 0;
772 data->tempnr = 0;
773
774 switch (devid) {
775 case 0xe8:
776 name = "pc87363";
777 break;
778 case 0xe4:
779 name = "pc87364";
780 data->fannr = 3;
781 break;
782 case 0xe5:
783 name = "pc87365";
784 data->fannr = extra_isa[0] ? 3 : 0;
785 data->innr = extra_isa[1] ? 11 : 0;
786 data->tempnr = extra_isa[2] ? 2 : 0;
787 break;
788 case 0xe9:
789 name = "pc87366";
790 data->fannr = extra_isa[0] ? 3 : 0;
791 data->innr = extra_isa[1] ? 14 : 0;
792 data->tempnr = extra_isa[2] ? 3 : 0;
793 break;
794 }
795
796 strcpy(new_client->name, name);
797 data->valid = 0;
798 init_MUTEX(&data->update_lock);
799
800 for (i = 0; i < 3; i++) {
801 if (((data->address[i] = extra_isa[i]))
802 && !request_region(extra_isa[i], PC87360_EXTENT,
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100803 pc87360_driver.driver.name)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 dev_err(&new_client->dev, "Region 0x%x-0x%x already "
805 "in use!\n", extra_isa[i],
806 extra_isa[i]+PC87360_EXTENT-1);
807 for (i--; i >= 0; i--)
808 release_region(extra_isa[i], PC87360_EXTENT);
809 err = -EBUSY;
810 goto ERROR1;
811 }
812 }
813
814 /* Retrieve the fans configuration from Super-I/O space */
815 if (data->fannr)
816 data->fan_conf = confreg[0] | (confreg[1] << 8);
817
818 if ((err = i2c_attach_client(new_client)))
819 goto ERROR2;
820
821 /* Use the correct reference voltage
822 Unless both the VLM and the TMS logical devices agree to
823 use an external Vref, the internal one is used. */
824 if (data->innr) {
825 i = pc87360_read_value(data, LD_IN, NO_BANK,
826 PC87365_REG_IN_CONFIG);
827 if (data->tempnr) {
828 i &= pc87360_read_value(data, LD_TEMP, NO_BANK,
829 PC87365_REG_TEMP_CONFIG);
830 }
831 data->in_vref = (i&0x02) ? 3025 : 2966;
832 dev_dbg(&new_client->dev, "Using %s reference voltage\n",
833 (i&0x02) ? "external" : "internal");
834
835 data->vid_conf = confreg[3];
836 data->vrm = 90;
837 }
838
839 /* Fan clock dividers may be needed before any data is read */
840 for (i = 0; i < data->fannr; i++) {
841 if (FAN_CONFIG_MONITOR(data->fan_conf, i))
842 data->fan_status[i] = pc87360_read_value(data,
843 LD_FAN, NO_BANK,
844 PC87360_REG_FAN_STATUS(i));
845 }
846
847 if (init > 0) {
848 if (devid == 0xe9 && data->address[1]) /* PC87366 */
849 use_thermistors = confreg[2] & 0x40;
850
851 pc87360_init_client(new_client, use_thermistors);
852 }
853
854 /* Register sysfs hooks */
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400855 data->class_dev = hwmon_device_register(&new_client->dev);
856 if (IS_ERR(data->class_dev)) {
857 err = PTR_ERR(data->class_dev);
858 goto ERROR3;
859 }
860
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 if (data->innr) {
Jim Cromief0986bd2005-09-02 22:52:43 +0200862 device_create_file(&new_client->dev, &sensor_dev_attr_in0_input.dev_attr);
863 device_create_file(&new_client->dev, &sensor_dev_attr_in1_input.dev_attr);
864 device_create_file(&new_client->dev, &sensor_dev_attr_in2_input.dev_attr);
865 device_create_file(&new_client->dev, &sensor_dev_attr_in3_input.dev_attr);
866 device_create_file(&new_client->dev, &sensor_dev_attr_in4_input.dev_attr);
867 device_create_file(&new_client->dev, &sensor_dev_attr_in5_input.dev_attr);
868 device_create_file(&new_client->dev, &sensor_dev_attr_in6_input.dev_attr);
869 device_create_file(&new_client->dev, &sensor_dev_attr_in7_input.dev_attr);
870 device_create_file(&new_client->dev, &sensor_dev_attr_in8_input.dev_attr);
871 device_create_file(&new_client->dev, &sensor_dev_attr_in9_input.dev_attr);
872 device_create_file(&new_client->dev, &sensor_dev_attr_in10_input.dev_attr);
873 device_create_file(&new_client->dev, &sensor_dev_attr_in0_min.dev_attr);
874 device_create_file(&new_client->dev, &sensor_dev_attr_in1_min.dev_attr);
875 device_create_file(&new_client->dev, &sensor_dev_attr_in2_min.dev_attr);
876 device_create_file(&new_client->dev, &sensor_dev_attr_in3_min.dev_attr);
877 device_create_file(&new_client->dev, &sensor_dev_attr_in4_min.dev_attr);
878 device_create_file(&new_client->dev, &sensor_dev_attr_in5_min.dev_attr);
879 device_create_file(&new_client->dev, &sensor_dev_attr_in6_min.dev_attr);
880 device_create_file(&new_client->dev, &sensor_dev_attr_in7_min.dev_attr);
881 device_create_file(&new_client->dev, &sensor_dev_attr_in8_min.dev_attr);
882 device_create_file(&new_client->dev, &sensor_dev_attr_in9_min.dev_attr);
883 device_create_file(&new_client->dev, &sensor_dev_attr_in10_min.dev_attr);
884 device_create_file(&new_client->dev, &sensor_dev_attr_in0_max.dev_attr);
885 device_create_file(&new_client->dev, &sensor_dev_attr_in1_max.dev_attr);
886 device_create_file(&new_client->dev, &sensor_dev_attr_in2_max.dev_attr);
887 device_create_file(&new_client->dev, &sensor_dev_attr_in3_max.dev_attr);
888 device_create_file(&new_client->dev, &sensor_dev_attr_in4_max.dev_attr);
889 device_create_file(&new_client->dev, &sensor_dev_attr_in5_max.dev_attr);
890 device_create_file(&new_client->dev, &sensor_dev_attr_in6_max.dev_attr);
891 device_create_file(&new_client->dev, &sensor_dev_attr_in7_max.dev_attr);
892 device_create_file(&new_client->dev, &sensor_dev_attr_in8_max.dev_attr);
893 device_create_file(&new_client->dev, &sensor_dev_attr_in9_max.dev_attr);
894 device_create_file(&new_client->dev, &sensor_dev_attr_in10_max.dev_attr);
895 device_create_file(&new_client->dev, &sensor_dev_attr_in0_status.dev_attr);
896 device_create_file(&new_client->dev, &sensor_dev_attr_in1_status.dev_attr);
897 device_create_file(&new_client->dev, &sensor_dev_attr_in2_status.dev_attr);
898 device_create_file(&new_client->dev, &sensor_dev_attr_in3_status.dev_attr);
899 device_create_file(&new_client->dev, &sensor_dev_attr_in4_status.dev_attr);
900 device_create_file(&new_client->dev, &sensor_dev_attr_in5_status.dev_attr);
901 device_create_file(&new_client->dev, &sensor_dev_attr_in6_status.dev_attr);
902 device_create_file(&new_client->dev, &sensor_dev_attr_in7_status.dev_attr);
903 device_create_file(&new_client->dev, &sensor_dev_attr_in8_status.dev_attr);
904 device_create_file(&new_client->dev, &sensor_dev_attr_in9_status.dev_attr);
905 device_create_file(&new_client->dev, &sensor_dev_attr_in10_status.dev_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906
907 device_create_file(&new_client->dev, &dev_attr_cpu0_vid);
908 device_create_file(&new_client->dev, &dev_attr_vrm);
909 device_create_file(&new_client->dev, &dev_attr_alarms_in);
910 }
911
912 if (data->tempnr) {
Jim Cromief0986bd2005-09-02 22:52:43 +0200913 device_create_file(&new_client->dev, &sensor_dev_attr_temp1_input.dev_attr);
914 device_create_file(&new_client->dev, &sensor_dev_attr_temp2_input.dev_attr);
915 device_create_file(&new_client->dev, &sensor_dev_attr_temp1_min.dev_attr);
916 device_create_file(&new_client->dev, &sensor_dev_attr_temp2_min.dev_attr);
917 device_create_file(&new_client->dev, &sensor_dev_attr_temp1_max.dev_attr);
918 device_create_file(&new_client->dev, &sensor_dev_attr_temp2_max.dev_attr);
919 device_create_file(&new_client->dev, &sensor_dev_attr_temp1_crit.dev_attr);
920 device_create_file(&new_client->dev, &sensor_dev_attr_temp2_crit.dev_attr);
921 device_create_file(&new_client->dev, &sensor_dev_attr_temp1_status.dev_attr);
922 device_create_file(&new_client->dev, &sensor_dev_attr_temp2_status.dev_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923
924 device_create_file(&new_client->dev, &dev_attr_alarms_temp);
925 }
926 if (data->tempnr == 3) {
Jim Cromief0986bd2005-09-02 22:52:43 +0200927 device_create_file(&new_client->dev, &sensor_dev_attr_temp3_input.dev_attr);
928 device_create_file(&new_client->dev, &sensor_dev_attr_temp3_min.dev_attr);
929 device_create_file(&new_client->dev, &sensor_dev_attr_temp3_max.dev_attr);
930 device_create_file(&new_client->dev, &sensor_dev_attr_temp3_crit.dev_attr);
931 device_create_file(&new_client->dev, &sensor_dev_attr_temp3_status.dev_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 }
933 if (data->innr == 14) {
Jim Cromief0986bd2005-09-02 22:52:43 +0200934 device_create_file(&new_client->dev, &sensor_dev_attr_temp4_input.dev_attr);
935 device_create_file(&new_client->dev, &sensor_dev_attr_temp5_input.dev_attr);
936 device_create_file(&new_client->dev, &sensor_dev_attr_temp6_input.dev_attr);
937 device_create_file(&new_client->dev, &sensor_dev_attr_temp4_min.dev_attr);
938 device_create_file(&new_client->dev, &sensor_dev_attr_temp5_min.dev_attr);
939 device_create_file(&new_client->dev, &sensor_dev_attr_temp6_min.dev_attr);
940 device_create_file(&new_client->dev, &sensor_dev_attr_temp4_max.dev_attr);
941 device_create_file(&new_client->dev, &sensor_dev_attr_temp5_max.dev_attr);
942 device_create_file(&new_client->dev, &sensor_dev_attr_temp6_max.dev_attr);
943 device_create_file(&new_client->dev, &sensor_dev_attr_temp4_crit.dev_attr);
944 device_create_file(&new_client->dev, &sensor_dev_attr_temp5_crit.dev_attr);
945 device_create_file(&new_client->dev, &sensor_dev_attr_temp6_crit.dev_attr);
946 device_create_file(&new_client->dev, &sensor_dev_attr_temp4_status.dev_attr);
947 device_create_file(&new_client->dev, &sensor_dev_attr_temp5_status.dev_attr);
948 device_create_file(&new_client->dev, &sensor_dev_attr_temp6_status.dev_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 }
950
951 if (data->fannr) {
952 if (FAN_CONFIG_MONITOR(data->fan_conf, 0)) {
953 device_create_file(&new_client->dev,
Jim Cromief0986bd2005-09-02 22:52:43 +0200954 &sensor_dev_attr_fan1_input.dev_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 device_create_file(&new_client->dev,
Jim Cromief0986bd2005-09-02 22:52:43 +0200956 &sensor_dev_attr_fan1_min.dev_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 device_create_file(&new_client->dev,
Jim Cromief0986bd2005-09-02 22:52:43 +0200958 &sensor_dev_attr_fan1_div.dev_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 device_create_file(&new_client->dev,
Jim Cromief0986bd2005-09-02 22:52:43 +0200960 &sensor_dev_attr_fan1_status.dev_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 }
962
963 if (FAN_CONFIG_MONITOR(data->fan_conf, 1)) {
964 device_create_file(&new_client->dev,
Jim Cromief0986bd2005-09-02 22:52:43 +0200965 &sensor_dev_attr_fan2_input.dev_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 device_create_file(&new_client->dev,
Jim Cromief0986bd2005-09-02 22:52:43 +0200967 &sensor_dev_attr_fan2_min.dev_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 device_create_file(&new_client->dev,
Jim Cromief0986bd2005-09-02 22:52:43 +0200969 &sensor_dev_attr_fan2_div.dev_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 device_create_file(&new_client->dev,
Jim Cromief0986bd2005-09-02 22:52:43 +0200971 &sensor_dev_attr_fan2_status.dev_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 }
973
974 if (FAN_CONFIG_CONTROL(data->fan_conf, 0))
Jim Cromief0986bd2005-09-02 22:52:43 +0200975 device_create_file(&new_client->dev, &sensor_dev_attr_pwm1.dev_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 if (FAN_CONFIG_CONTROL(data->fan_conf, 1))
Jim Cromief0986bd2005-09-02 22:52:43 +0200977 device_create_file(&new_client->dev, &sensor_dev_attr_pwm2.dev_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 }
979 if (data->fannr == 3) {
980 if (FAN_CONFIG_MONITOR(data->fan_conf, 2)) {
981 device_create_file(&new_client->dev,
Jim Cromief0986bd2005-09-02 22:52:43 +0200982 &sensor_dev_attr_fan3_input.dev_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 device_create_file(&new_client->dev,
Jim Cromief0986bd2005-09-02 22:52:43 +0200984 &sensor_dev_attr_fan3_min.dev_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 device_create_file(&new_client->dev,
Jim Cromief0986bd2005-09-02 22:52:43 +0200986 &sensor_dev_attr_fan3_div.dev_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 device_create_file(&new_client->dev,
Jim Cromief0986bd2005-09-02 22:52:43 +0200988 &sensor_dev_attr_fan3_status.dev_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 }
990
991 if (FAN_CONFIG_CONTROL(data->fan_conf, 2))
Jim Cromief0986bd2005-09-02 22:52:43 +0200992 device_create_file(&new_client->dev, &sensor_dev_attr_pwm3.dev_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 }
994
995 return 0;
996
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400997ERROR3:
998 i2c_detach_client(new_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999ERROR2:
1000 for (i = 0; i < 3; i++) {
1001 if (data->address[i]) {
1002 release_region(data->address[i], PC87360_EXTENT);
1003 }
1004 }
1005ERROR1:
1006 kfree(data);
1007 return err;
1008}
1009
1010static int pc87360_detach_client(struct i2c_client *client)
1011{
1012 struct pc87360_data *data = i2c_get_clientdata(client);
1013 int i;
1014
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001015 hwmon_device_unregister(data->class_dev);
1016
Jean Delvare7bef5592005-07-27 22:14:49 +02001017 if ((i = i2c_detach_client(client)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019
1020 for (i = 0; i < 3; i++) {
1021 if (data->address[i]) {
1022 release_region(data->address[i], PC87360_EXTENT);
1023 }
1024 }
1025 kfree(data);
1026
1027 return 0;
1028}
1029
1030/* ldi is the logical device index
1031 bank is for voltages and temperatures only */
1032static int pc87360_read_value(struct pc87360_data *data, u8 ldi, u8 bank,
1033 u8 reg)
1034{
1035 int res;
1036
1037 down(&(data->lock));
1038 if (bank != NO_BANK)
1039 outb_p(bank, data->address[ldi] + PC87365_REG_BANK);
1040 res = inb_p(data->address[ldi] + reg);
1041 up(&(data->lock));
1042
1043 return res;
1044}
1045
1046static void pc87360_write_value(struct pc87360_data *data, u8 ldi, u8 bank,
1047 u8 reg, u8 value)
1048{
1049 down(&(data->lock));
1050 if (bank != NO_BANK)
1051 outb_p(bank, data->address[ldi] + PC87365_REG_BANK);
1052 outb_p(value, data->address[ldi] + reg);
1053 up(&(data->lock));
1054}
1055
1056static void pc87360_init_client(struct i2c_client *client, int use_thermistors)
1057{
1058 struct pc87360_data *data = i2c_get_clientdata(client);
1059 int i, nr;
1060 const u8 init_in[14] = { 2, 2, 2, 2, 2, 2, 2, 1, 1, 3, 1, 2, 2, 2 };
1061 const u8 init_temp[3] = { 2, 2, 1 };
1062 u8 reg;
1063
1064 if (init >= 2 && data->innr) {
1065 reg = pc87360_read_value(data, LD_IN, NO_BANK,
1066 PC87365_REG_IN_CONVRATE);
Jean Delvare368609c2005-07-29 12:15:07 -07001067 dev_info(&client->dev, "VLM conversion set to "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 "1s period, 160us delay\n");
1069 pc87360_write_value(data, LD_IN, NO_BANK,
1070 PC87365_REG_IN_CONVRATE,
1071 (reg & 0xC0) | 0x11);
1072 }
1073
1074 nr = data->innr < 11 ? data->innr : 11;
1075 for (i=0; i<nr; i++) {
1076 if (init >= init_in[i]) {
1077 /* Forcibly enable voltage channel */
1078 reg = pc87360_read_value(data, LD_IN, i,
1079 PC87365_REG_IN_STATUS);
1080 if (!(reg & 0x01)) {
1081 dev_dbg(&client->dev, "Forcibly "
1082 "enabling in%d\n", i);
1083 pc87360_write_value(data, LD_IN, i,
1084 PC87365_REG_IN_STATUS,
1085 (reg & 0x68) | 0x87);
1086 }
1087 }
1088 }
1089
1090 /* We can't blindly trust the Super-I/O space configuration bit,
1091 most BIOS won't set it properly */
1092 for (i=11; i<data->innr; i++) {
1093 reg = pc87360_read_value(data, LD_IN, i,
1094 PC87365_REG_TEMP_STATUS);
1095 use_thermistors = use_thermistors || (reg & 0x01);
1096 }
1097
1098 i = use_thermistors ? 2 : 0;
1099 for (; i<data->tempnr; i++) {
1100 if (init >= init_temp[i]) {
1101 /* Forcibly enable temperature channel */
1102 reg = pc87360_read_value(data, LD_TEMP, i,
1103 PC87365_REG_TEMP_STATUS);
1104 if (!(reg & 0x01)) {
1105 dev_dbg(&client->dev, "Forcibly "
1106 "enabling temp%d\n", i+1);
1107 pc87360_write_value(data, LD_TEMP, i,
1108 PC87365_REG_TEMP_STATUS,
1109 0xCF);
1110 }
1111 }
1112 }
1113
1114 if (use_thermistors) {
1115 for (i=11; i<data->innr; i++) {
1116 if (init >= init_in[i]) {
1117 /* The pin may already be used by thermal
1118 diodes */
1119 reg = pc87360_read_value(data, LD_TEMP,
1120 (i-11)/2, PC87365_REG_TEMP_STATUS);
1121 if (reg & 0x01) {
1122 dev_dbg(&client->dev, "Skipping "
1123 "temp%d, pin already in use "
1124 "by temp%d\n", i-7, (i-11)/2);
1125 continue;
1126 }
1127
1128 /* Forcibly enable thermistor channel */
1129 reg = pc87360_read_value(data, LD_IN, i,
1130 PC87365_REG_IN_STATUS);
1131 if (!(reg & 0x01)) {
1132 dev_dbg(&client->dev, "Forcibly "
1133 "enabling temp%d\n", i-7);
1134 pc87360_write_value(data, LD_IN, i,
1135 PC87365_REG_TEMP_STATUS,
1136 (reg & 0x60) | 0x8F);
1137 }
1138 }
1139 }
1140 }
1141
1142 if (data->innr) {
1143 reg = pc87360_read_value(data, LD_IN, NO_BANK,
1144 PC87365_REG_IN_CONFIG);
1145 if (reg & 0x01) {
1146 dev_dbg(&client->dev, "Forcibly "
1147 "enabling monitoring (VLM)\n");
1148 pc87360_write_value(data, LD_IN, NO_BANK,
1149 PC87365_REG_IN_CONFIG,
1150 reg & 0xFE);
1151 }
1152 }
1153
1154 if (data->tempnr) {
1155 reg = pc87360_read_value(data, LD_TEMP, NO_BANK,
1156 PC87365_REG_TEMP_CONFIG);
1157 if (reg & 0x01) {
1158 dev_dbg(&client->dev, "Forcibly enabling "
1159 "monitoring (TMS)\n");
1160 pc87360_write_value(data, LD_TEMP, NO_BANK,
1161 PC87365_REG_TEMP_CONFIG,
1162 reg & 0xFE);
1163 }
1164
1165 if (init >= 2) {
1166 /* Chip config as documented by National Semi. */
1167 pc87360_write_value(data, LD_TEMP, 0xF, 0xA, 0x08);
1168 /* We voluntarily omit the bank here, in case the
1169 sequence itself matters. It shouldn't be a problem,
1170 since nobody else is supposed to access the
1171 device at that point. */
1172 pc87360_write_value(data, LD_TEMP, NO_BANK, 0xB, 0x04);
1173 pc87360_write_value(data, LD_TEMP, NO_BANK, 0xC, 0x35);
1174 pc87360_write_value(data, LD_TEMP, NO_BANK, 0xD, 0x05);
1175 pc87360_write_value(data, LD_TEMP, NO_BANK, 0xE, 0x05);
1176 }
1177 }
1178}
1179
1180static void pc87360_autodiv(struct i2c_client *client, int nr)
1181{
1182 struct pc87360_data *data = i2c_get_clientdata(client);
1183 u8 old_min = data->fan_min[nr];
1184
1185 /* Increase clock divider if needed and possible */
1186 if ((data->fan_status[nr] & 0x04) /* overflow flag */
1187 || (data->fan[nr] >= 224)) { /* next to overflow */
1188 if ((data->fan_status[nr] & 0x60) != 0x60) {
1189 data->fan_status[nr] += 0x20;
1190 data->fan_min[nr] >>= 1;
1191 data->fan[nr] >>= 1;
1192 dev_dbg(&client->dev, "Increasing "
1193 "clock divider to %d for fan %d\n",
1194 FAN_DIV_FROM_REG(data->fan_status[nr]), nr+1);
1195 }
1196 } else {
1197 /* Decrease clock divider if possible */
1198 while (!(data->fan_min[nr] & 0x80) /* min "nails" divider */
1199 && data->fan[nr] < 85 /* bad accuracy */
1200 && (data->fan_status[nr] & 0x60) != 0x00) {
1201 data->fan_status[nr] -= 0x20;
1202 data->fan_min[nr] <<= 1;
1203 data->fan[nr] <<= 1;
1204 dev_dbg(&client->dev, "Decreasing "
1205 "clock divider to %d for fan %d\n",
1206 FAN_DIV_FROM_REG(data->fan_status[nr]),
1207 nr+1);
1208 }
1209 }
1210
1211 /* Write new fan min if it changed */
1212 if (old_min != data->fan_min[nr]) {
1213 pc87360_write_value(data, LD_FAN, NO_BANK,
1214 PC87360_REG_FAN_MIN(nr),
1215 data->fan_min[nr]);
1216 }
1217}
1218
1219static struct pc87360_data *pc87360_update_device(struct device *dev)
1220{
1221 struct i2c_client *client = to_i2c_client(dev);
1222 struct pc87360_data *data = i2c_get_clientdata(client);
1223 u8 i;
1224
1225 down(&data->update_lock);
1226
1227 if (time_after(jiffies, data->last_updated + HZ * 2) || !data->valid) {
1228 dev_dbg(&client->dev, "Data update\n");
1229
1230 /* Fans */
1231 for (i = 0; i < data->fannr; i++) {
1232 if (FAN_CONFIG_MONITOR(data->fan_conf, i)) {
1233 data->fan_status[i] =
1234 pc87360_read_value(data, LD_FAN,
1235 NO_BANK, PC87360_REG_FAN_STATUS(i));
1236 data->fan[i] = pc87360_read_value(data, LD_FAN,
1237 NO_BANK, PC87360_REG_FAN(i));
1238 data->fan_min[i] = pc87360_read_value(data,
1239 LD_FAN, NO_BANK,
1240 PC87360_REG_FAN_MIN(i));
1241 /* Change clock divider if needed */
1242 pc87360_autodiv(client, i);
1243 /* Clear bits and write new divider */
1244 pc87360_write_value(data, LD_FAN, NO_BANK,
1245 PC87360_REG_FAN_STATUS(i),
1246 data->fan_status[i]);
1247 }
1248 if (FAN_CONFIG_CONTROL(data->fan_conf, i))
1249 data->pwm[i] = pc87360_read_value(data, LD_FAN,
1250 NO_BANK, PC87360_REG_PWM(i));
1251 }
1252
1253 /* Voltages */
1254 for (i = 0; i < data->innr; i++) {
1255 data->in_status[i] = pc87360_read_value(data, LD_IN, i,
1256 PC87365_REG_IN_STATUS);
1257 /* Clear bits */
1258 pc87360_write_value(data, LD_IN, i,
1259 PC87365_REG_IN_STATUS,
1260 data->in_status[i]);
1261 if ((data->in_status[i] & 0x81) == 0x81) {
1262 data->in[i] = pc87360_read_value(data, LD_IN,
1263 i, PC87365_REG_IN);
1264 }
1265 if (data->in_status[i] & 0x01) {
1266 data->in_min[i] = pc87360_read_value(data,
1267 LD_IN, i,
1268 PC87365_REG_IN_MIN);
1269 data->in_max[i] = pc87360_read_value(data,
1270 LD_IN, i,
1271 PC87365_REG_IN_MAX);
1272 if (i >= 11)
1273 data->in_crit[i-11] =
1274 pc87360_read_value(data, LD_IN,
1275 i, PC87365_REG_TEMP_CRIT);
1276 }
1277 }
1278 if (data->innr) {
1279 data->in_alarms = pc87360_read_value(data, LD_IN,
1280 NO_BANK, PC87365_REG_IN_ALARMS1)
1281 | ((pc87360_read_value(data, LD_IN,
1282 NO_BANK, PC87365_REG_IN_ALARMS2)
1283 & 0x07) << 8);
1284 data->vid = (data->vid_conf & 0xE0) ?
1285 pc87360_read_value(data, LD_IN,
1286 NO_BANK, PC87365_REG_VID) : 0x1F;
1287 }
1288
1289 /* Temperatures */
1290 for (i = 0; i < data->tempnr; i++) {
1291 data->temp_status[i] = pc87360_read_value(data,
1292 LD_TEMP, i,
1293 PC87365_REG_TEMP_STATUS);
1294 /* Clear bits */
1295 pc87360_write_value(data, LD_TEMP, i,
1296 PC87365_REG_TEMP_STATUS,
1297 data->temp_status[i]);
1298 if ((data->temp_status[i] & 0x81) == 0x81) {
1299 data->temp[i] = pc87360_read_value(data,
1300 LD_TEMP, i,
1301 PC87365_REG_TEMP);
1302 }
1303 if (data->temp_status[i] & 0x01) {
1304 data->temp_min[i] = pc87360_read_value(data,
1305 LD_TEMP, i,
1306 PC87365_REG_TEMP_MIN);
1307 data->temp_max[i] = pc87360_read_value(data,
1308 LD_TEMP, i,
1309 PC87365_REG_TEMP_MAX);
1310 data->temp_crit[i] = pc87360_read_value(data,
1311 LD_TEMP, i,
1312 PC87365_REG_TEMP_CRIT);
1313 }
1314 }
1315 if (data->tempnr) {
1316 data->temp_alarms = pc87360_read_value(data, LD_TEMP,
1317 NO_BANK, PC87365_REG_TEMP_ALARMS)
1318 & 0x3F;
1319 }
1320
1321 data->last_updated = jiffies;
1322 data->valid = 1;
1323 }
1324
1325 up(&data->update_lock);
1326
1327 return data;
1328}
1329
1330static int __init pc87360_init(void)
1331{
1332 int i;
1333
1334 if (pc87360_find(0x2e, &devid, extra_isa)
1335 && pc87360_find(0x4e, &devid, extra_isa)) {
1336 printk(KERN_WARNING "pc87360: PC8736x not detected, "
1337 "module not inserted.\n");
1338 return -ENODEV;
1339 }
1340
1341 /* Arbitrarily pick one of the addresses */
1342 for (i = 0; i < 3; i++) {
1343 if (extra_isa[i] != 0x0000) {
Jean Delvare2d8672c2005-07-19 23:56:35 +02001344 address = extra_isa[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 break;
1346 }
1347 }
1348
Jean Delvare2d8672c2005-07-19 23:56:35 +02001349 if (address == 0x0000) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 printk(KERN_WARNING "pc87360: No active logical device, "
1351 "module not inserted.\n");
1352 return -ENODEV;
1353 }
1354
Jean Delvarefde09502005-07-19 23:51:07 +02001355 return i2c_isa_add_driver(&pc87360_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356}
1357
1358static void __exit pc87360_exit(void)
1359{
Jean Delvarefde09502005-07-19 23:51:07 +02001360 i2c_isa_del_driver(&pc87360_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361}
1362
1363
1364MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
1365MODULE_DESCRIPTION("PC8736x hardware monitor");
1366MODULE_LICENSE("GPL");
1367
1368module_init(pc87360_init);
1369module_exit(pc87360_exit);