[PATCH] hwmon: tag super-i/o find functions __init
[linux-block.git] / drivers / hwmon / pc87360.c
CommitLineData
1da177e4
LT
1/*
2 * pc87360.c - Part of lm_sensors, Linux kernel modules
3 * for hardware monitoring
4 * Copyright (C) 2004 Jean Delvare <khali@linux-fr.org>
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
1da177e4
LT
36#include <linux/module.h>
37#include <linux/init.h>
38#include <linux/slab.h>
39#include <linux/jiffies.h>
40#include <linux/i2c.h>
fde09509 41#include <linux/i2c-isa.h>
1da177e4 42#include <linux/i2c-vid.h>
943b0830
MH
43#include <linux/hwmon.h>
44#include <linux/err.h>
1da177e4
LT
45#include <asm/io.h>
46
1da177e4 47static u8 devid;
2d8672c5
JD
48static unsigned short address;
49static unsigned short extra_isa[3];
1da177e4
LT
50static u8 confreg[4];
51
52enum chips { any_chip, pc87360, pc87363, pc87364, pc87365, pc87366 };
1da177e4
LT
53
54static int init = 1;
55module_param(init, int, 0);
56MODULE_PARM_DESC(init,
57 "Chip initialization level:\n"
58 " 0: None\n"
59 "*1: Forcibly enable internal voltage and temperature channels, except in9\n"
60 " 2: Forcibly enable all voltage and temperature channels, except in9\n"
61 " 3: Forcibly enable all voltage and temperature channels, including in9");
62
63/*
64 * Super-I/O registers and operations
65 */
66
67#define DEV 0x07 /* Register: Logical device select */
68#define DEVID 0x20 /* Register: Device ID */
69#define ACT 0x30 /* Register: Device activation */
70#define BASE 0x60 /* Register: Base address */
71
72#define FSCM 0x09 /* Logical device: fans */
73#define VLM 0x0d /* Logical device: voltages */
74#define TMS 0x0e /* Logical device: temperatures */
75static const u8 logdev[3] = { FSCM, VLM, TMS };
76
77#define LD_FAN 0
78#define LD_IN 1
79#define LD_TEMP 2
80
81static inline void superio_outb(int sioaddr, int reg, int val)
82{
83 outb(reg, sioaddr);
84 outb(val, sioaddr+1);
85}
86
87static inline int superio_inb(int sioaddr, int reg)
88{
89 outb(reg, sioaddr);
90 return inb(sioaddr+1);
91}
92
93static inline void superio_exit(int sioaddr)
94{
95 outb(0x02, sioaddr);
96 outb(0x02, sioaddr+1);
97}
98
99/*
100 * Logical devices
101 */
102
103#define PC87360_EXTENT 0x10
104#define PC87365_REG_BANK 0x09
105#define NO_BANK 0xff
106
107/*
108 * Fan registers and conversions
109 */
110
111/* nr has to be 0 or 1 (PC87360/87363) or 2 (PC87364/87365/87366) */
112#define PC87360_REG_PRESCALE(nr) (0x00 + 2 * (nr))
113#define PC87360_REG_PWM(nr) (0x01 + 2 * (nr))
114#define PC87360_REG_FAN_MIN(nr) (0x06 + 3 * (nr))
115#define PC87360_REG_FAN(nr) (0x07 + 3 * (nr))
116#define PC87360_REG_FAN_STATUS(nr) (0x08 + 3 * (nr))
117
118#define FAN_FROM_REG(val,div) ((val) == 0 ? 0: \
119 480000 / ((val)*(div)))
120#define FAN_TO_REG(val,div) ((val) <= 100 ? 0 : \
121 480000 / ((val)*(div)))
122#define FAN_DIV_FROM_REG(val) (1 << ((val >> 5) & 0x03))
123#define FAN_STATUS_FROM_REG(val) ((val) & 0x07)
124
125#define FAN_CONFIG_MONITOR(val,nr) (((val) >> (2 + nr * 3)) & 1)
126#define FAN_CONFIG_CONTROL(val,nr) (((val) >> (3 + nr * 3)) & 1)
127#define FAN_CONFIG_INVERT(val,nr) (((val) >> (4 + nr * 3)) & 1)
128
129#define PWM_FROM_REG(val,inv) ((inv) ? 255 - (val) : (val))
130static inline u8 PWM_TO_REG(int val, int inv)
131{
132 if (inv)
133 val = 255 - val;
134 if (val < 0)
135 return 0;
136 if (val > 255)
137 return 255;
138 return val;
139}
140
141/*
142 * Voltage registers and conversions
143 */
144
145#define PC87365_REG_IN_CONVRATE 0x07
146#define PC87365_REG_IN_CONFIG 0x08
147#define PC87365_REG_IN 0x0B
148#define PC87365_REG_IN_MIN 0x0D
149#define PC87365_REG_IN_MAX 0x0C
150#define PC87365_REG_IN_STATUS 0x0A
151#define PC87365_REG_IN_ALARMS1 0x00
152#define PC87365_REG_IN_ALARMS2 0x01
153#define PC87365_REG_VID 0x06
154
155#define IN_FROM_REG(val,ref) (((val) * (ref) + 128) / 256)
156#define IN_TO_REG(val,ref) ((val) < 0 ? 0 : \
157 (val)*256 >= (ref)*255 ? 255: \
158 ((val) * 256 + (ref)/2) / (ref))
159
160/*
161 * Temperature registers and conversions
162 */
163
164#define PC87365_REG_TEMP_CONFIG 0x08
165#define PC87365_REG_TEMP 0x0B
166#define PC87365_REG_TEMP_MIN 0x0D
167#define PC87365_REG_TEMP_MAX 0x0C
168#define PC87365_REG_TEMP_CRIT 0x0E
169#define PC87365_REG_TEMP_STATUS 0x0A
170#define PC87365_REG_TEMP_ALARMS 0x00
171
172#define TEMP_FROM_REG(val) ((val) * 1000)
173#define TEMP_TO_REG(val) ((val) < -55000 ? -55 : \
174 (val) > 127000 ? 127 : \
175 (val) < 0 ? ((val) - 500) / 1000 : \
176 ((val) + 500) / 1000)
177
178/*
179 * Client data (each client gets its own)
180 */
181
182struct pc87360_data {
183 struct i2c_client client;
943b0830 184 struct class_device *class_dev;
1da177e4
LT
185 struct semaphore lock;
186 struct semaphore update_lock;
187 char valid; /* !=0 if following fields are valid */
188 unsigned long last_updated; /* In jiffies */
189
190 int address[3];
191
192 u8 fannr, innr, tempnr;
193
194 u8 fan[3]; /* Register value */
195 u8 fan_min[3]; /* Register value */
196 u8 fan_status[3]; /* Register value */
197 u8 pwm[3]; /* Register value */
198 u16 fan_conf; /* Configuration register values, combined */
199
200 u16 in_vref; /* 1 mV/bit */
201 u8 in[14]; /* Register value */
202 u8 in_min[14]; /* Register value */
203 u8 in_max[14]; /* Register value */
204 u8 in_crit[3]; /* Register value */
205 u8 in_status[14]; /* Register value */
206 u16 in_alarms; /* Register values, combined, masked */
207 u8 vid_conf; /* Configuration register value */
208 u8 vrm;
209 u8 vid; /* Register value */
210
211 s8 temp[3]; /* Register value */
212 s8 temp_min[3]; /* Register value */
213 s8 temp_max[3]; /* Register value */
214 s8 temp_crit[3]; /* Register value */
215 u8 temp_status[3]; /* Register value */
216 u8 temp_alarms; /* Register value, masked */
217};
218
219/*
220 * Functions declaration
221 */
222
2d8672c5 223static int pc87360_detect(struct i2c_adapter *adapter);
1da177e4
LT
224static int pc87360_detach_client(struct i2c_client *client);
225
226static int pc87360_read_value(struct pc87360_data *data, u8 ldi, u8 bank,
227 u8 reg);
228static void pc87360_write_value(struct pc87360_data *data, u8 ldi, u8 bank,
229 u8 reg, u8 value);
230static void pc87360_init_client(struct i2c_client *client, int use_thermistors);
231static struct pc87360_data *pc87360_update_device(struct device *dev);
232
233/*
234 * Driver data (common to all clients)
235 */
236
237static struct i2c_driver pc87360_driver = {
238 .owner = THIS_MODULE,
239 .name = "pc87360",
2d8672c5 240 .attach_adapter = pc87360_detect,
1da177e4
LT
241 .detach_client = pc87360_detach_client,
242};
243
244/*
245 * Sysfs stuff
246 */
247
248static ssize_t set_fan_min(struct device *dev, const char *buf,
249 size_t count, int nr)
250{
251 struct i2c_client *client = to_i2c_client(dev);
252 struct pc87360_data *data = i2c_get_clientdata(client);
253 long fan_min = simple_strtol(buf, NULL, 10);
254
255 down(&data->update_lock);
256 fan_min = FAN_TO_REG(fan_min, FAN_DIV_FROM_REG(data->fan_status[nr]));
257
258 /* If it wouldn't fit, change clock divisor */
259 while (fan_min > 255
260 && (data->fan_status[nr] & 0x60) != 0x60) {
261 fan_min >>= 1;
262 data->fan[nr] >>= 1;
263 data->fan_status[nr] += 0x20;
264 }
265 data->fan_min[nr] = fan_min > 255 ? 255 : fan_min;
266 pc87360_write_value(data, LD_FAN, NO_BANK, PC87360_REG_FAN_MIN(nr),
267 data->fan_min[nr]);
268
269 /* Write new divider, preserve alarm bits */
270 pc87360_write_value(data, LD_FAN, NO_BANK, PC87360_REG_FAN_STATUS(nr),
271 data->fan_status[nr] & 0xF9);
272 up(&data->update_lock);
273
274 return count;
275}
276
277#define show_and_set_fan(offset) \
a5099cfc 278static ssize_t show_fan##offset##_input(struct device *dev, struct device_attribute *attr, char *buf) \
1da177e4
LT
279{ \
280 struct pc87360_data *data = pc87360_update_device(dev); \
281 return sprintf(buf, "%u\n", FAN_FROM_REG(data->fan[offset-1], \
282 FAN_DIV_FROM_REG(data->fan_status[offset-1]))); \
283} \
a5099cfc 284static ssize_t show_fan##offset##_min(struct device *dev, struct device_attribute *attr, char *buf) \
1da177e4
LT
285{ \
286 struct pc87360_data *data = pc87360_update_device(dev); \
287 return sprintf(buf, "%u\n", FAN_FROM_REG(data->fan_min[offset-1], \
288 FAN_DIV_FROM_REG(data->fan_status[offset-1]))); \
289} \
a5099cfc 290static ssize_t show_fan##offset##_div(struct device *dev, struct device_attribute *attr, char *buf) \
1da177e4
LT
291{ \
292 struct pc87360_data *data = pc87360_update_device(dev); \
293 return sprintf(buf, "%u\n", \
294 FAN_DIV_FROM_REG(data->fan_status[offset-1])); \
295} \
a5099cfc 296static ssize_t show_fan##offset##_status(struct device *dev, struct device_attribute *attr, char *buf) \
1da177e4
LT
297{ \
298 struct pc87360_data *data = pc87360_update_device(dev); \
299 return sprintf(buf, "%u\n", \
300 FAN_STATUS_FROM_REG(data->fan_status[offset-1])); \
301} \
a5099cfc 302static ssize_t set_fan##offset##_min(struct device *dev, struct device_attribute *attr, const char *buf, \
1da177e4
LT
303 size_t count) \
304{ \
305 return set_fan_min(dev, buf, count, offset-1); \
306} \
307static DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
308 show_fan##offset##_input, NULL); \
309static DEVICE_ATTR(fan##offset##_min, S_IWUSR | S_IRUGO, \
310 show_fan##offset##_min, set_fan##offset##_min); \
311static DEVICE_ATTR(fan##offset##_div, S_IRUGO, \
312 show_fan##offset##_div, NULL); \
313static DEVICE_ATTR(fan##offset##_status, S_IRUGO, \
314 show_fan##offset##_status, NULL);
315show_and_set_fan(1)
316show_and_set_fan(2)
317show_and_set_fan(3)
318
319#define show_and_set_pwm(offset) \
a5099cfc 320static ssize_t show_pwm##offset(struct device *dev, struct device_attribute *attr, char *buf) \
1da177e4
LT
321{ \
322 struct pc87360_data *data = pc87360_update_device(dev); \
323 return sprintf(buf, "%u\n", \
324 PWM_FROM_REG(data->pwm[offset-1], \
325 FAN_CONFIG_INVERT(data->fan_conf, \
326 offset-1))); \
327} \
a5099cfc 328static ssize_t set_pwm##offset(struct device *dev, struct device_attribute *attr, const char *buf, \
1da177e4
LT
329 size_t count) \
330{ \
331 struct i2c_client *client = to_i2c_client(dev); \
332 struct pc87360_data *data = i2c_get_clientdata(client); \
333 long val = simple_strtol(buf, NULL, 10); \
334 \
335 down(&data->update_lock); \
336 data->pwm[offset-1] = PWM_TO_REG(val, \
337 FAN_CONFIG_INVERT(data->fan_conf, offset-1)); \
338 pc87360_write_value(data, LD_FAN, NO_BANK, PC87360_REG_PWM(offset-1), \
339 data->pwm[offset-1]); \
340 up(&data->update_lock); \
341 return count; \
342} \
343static DEVICE_ATTR(pwm##offset, S_IWUSR | S_IRUGO, \
344 show_pwm##offset, set_pwm##offset);
345show_and_set_pwm(1)
346show_and_set_pwm(2)
347show_and_set_pwm(3)
348
349#define show_and_set_in(offset) \
a5099cfc 350static ssize_t show_in##offset##_input(struct device *dev, struct device_attribute *attr, char *buf) \
1da177e4
LT
351{ \
352 struct pc87360_data *data = pc87360_update_device(dev); \
353 return sprintf(buf, "%u\n", IN_FROM_REG(data->in[offset], \
354 data->in_vref)); \
355} \
a5099cfc 356static ssize_t show_in##offset##_min(struct device *dev, struct device_attribute *attr, char *buf) \
1da177e4
LT
357{ \
358 struct pc87360_data *data = pc87360_update_device(dev); \
359 return sprintf(buf, "%u\n", IN_FROM_REG(data->in_min[offset], \
360 data->in_vref)); \
361} \
a5099cfc 362static ssize_t show_in##offset##_max(struct device *dev, struct device_attribute *attr, char *buf) \
1da177e4
LT
363{ \
364 struct pc87360_data *data = pc87360_update_device(dev); \
365 return sprintf(buf, "%u\n", IN_FROM_REG(data->in_max[offset], \
366 data->in_vref)); \
367} \
a5099cfc 368static ssize_t show_in##offset##_status(struct device *dev, struct device_attribute *attr, char *buf) \
1da177e4
LT
369{ \
370 struct pc87360_data *data = pc87360_update_device(dev); \
371 return sprintf(buf, "%u\n", data->in_status[offset]); \
372} \
a5099cfc 373static ssize_t set_in##offset##_min(struct device *dev, struct device_attribute *attr, const char *buf, \
1da177e4
LT
374 size_t count) \
375{ \
376 struct i2c_client *client = to_i2c_client(dev); \
377 struct pc87360_data *data = i2c_get_clientdata(client); \
378 long val = simple_strtol(buf, NULL, 10); \
379 \
380 down(&data->update_lock); \
381 data->in_min[offset] = IN_TO_REG(val, data->in_vref); \
382 pc87360_write_value(data, LD_IN, offset, PC87365_REG_IN_MIN, \
383 data->in_min[offset]); \
384 up(&data->update_lock); \
385 return count; \
386} \
a5099cfc 387static ssize_t set_in##offset##_max(struct device *dev, struct device_attribute *attr, const char *buf, \
1da177e4
LT
388 size_t count) \
389{ \
390 struct i2c_client *client = to_i2c_client(dev); \
391 struct pc87360_data *data = i2c_get_clientdata(client); \
392 long val = simple_strtol(buf, NULL, 10); \
393 \
394 down(&data->update_lock); \
395 data->in_max[offset] = IN_TO_REG(val, \
396 data->in_vref); \
397 pc87360_write_value(data, LD_IN, offset, PC87365_REG_IN_MAX, \
398 data->in_max[offset]); \
399 up(&data->update_lock); \
400 return count; \
401} \
402static DEVICE_ATTR(in##offset##_input, S_IRUGO, \
403 show_in##offset##_input, NULL); \
404static DEVICE_ATTR(in##offset##_min, S_IWUSR | S_IRUGO, \
405 show_in##offset##_min, set_in##offset##_min); \
406static DEVICE_ATTR(in##offset##_max, S_IWUSR | S_IRUGO, \
407 show_in##offset##_max, set_in##offset##_max); \
408static DEVICE_ATTR(in##offset##_status, S_IRUGO, \
409 show_in##offset##_status, NULL);
410show_and_set_in(0)
411show_and_set_in(1)
412show_and_set_in(2)
413show_and_set_in(3)
414show_and_set_in(4)
415show_and_set_in(5)
416show_and_set_in(6)
417show_and_set_in(7)
418show_and_set_in(8)
419show_and_set_in(9)
420show_and_set_in(10)
421
422#define show_and_set_therm(offset) \
a5099cfc 423static ssize_t show_temp##offset##_input(struct device *dev, struct device_attribute *attr, char *buf) \
1da177e4
LT
424{ \
425 struct pc87360_data *data = pc87360_update_device(dev); \
426 return sprintf(buf, "%u\n", IN_FROM_REG(data->in[offset+7], \
427 data->in_vref)); \
428} \
a5099cfc 429static ssize_t show_temp##offset##_min(struct device *dev, struct device_attribute *attr, char *buf) \
1da177e4
LT
430{ \
431 struct pc87360_data *data = pc87360_update_device(dev); \
432 return sprintf(buf, "%u\n", IN_FROM_REG(data->in_min[offset+7], \
433 data->in_vref)); \
434} \
a5099cfc 435static ssize_t show_temp##offset##_max(struct device *dev, struct device_attribute *attr, char *buf) \
1da177e4
LT
436{ \
437 struct pc87360_data *data = pc87360_update_device(dev); \
438 return sprintf(buf, "%u\n", IN_FROM_REG(data->in_max[offset+7], \
439 data->in_vref)); \
440} \
a5099cfc 441static ssize_t show_temp##offset##_crit(struct device *dev, struct device_attribute *attr, char *buf) \
1da177e4
LT
442{ \
443 struct pc87360_data *data = pc87360_update_device(dev); \
444 return sprintf(buf, "%u\n", IN_FROM_REG(data->in_crit[offset-4], \
445 data->in_vref)); \
446} \
a5099cfc 447static ssize_t show_temp##offset##_status(struct device *dev, struct device_attribute *attr, char *buf) \
1da177e4
LT
448{ \
449 struct pc87360_data *data = pc87360_update_device(dev); \
450 return sprintf(buf, "%u\n", data->in_status[offset+7]); \
451} \
a5099cfc 452static ssize_t set_temp##offset##_min(struct device *dev, struct device_attribute *attr, const char *buf, \
1da177e4
LT
453 size_t count) \
454{ \
455 struct i2c_client *client = to_i2c_client(dev); \
456 struct pc87360_data *data = i2c_get_clientdata(client); \
457 long val = simple_strtol(buf, NULL, 10); \
458 \
459 down(&data->update_lock); \
460 data->in_min[offset+7] = IN_TO_REG(val, data->in_vref); \
461 pc87360_write_value(data, LD_IN, offset+7, PC87365_REG_TEMP_MIN, \
462 data->in_min[offset+7]); \
463 up(&data->update_lock); \
464 return count; \
465} \
a5099cfc 466static ssize_t set_temp##offset##_max(struct device *dev, struct device_attribute *attr, const char *buf, \
1da177e4
LT
467 size_t count) \
468{ \
469 struct i2c_client *client = to_i2c_client(dev); \
470 struct pc87360_data *data = i2c_get_clientdata(client); \
471 long val = simple_strtol(buf, NULL, 10); \
472 \
473 down(&data->update_lock); \
474 data->in_max[offset+7] = IN_TO_REG(val, data->in_vref); \
475 pc87360_write_value(data, LD_IN, offset+7, PC87365_REG_TEMP_MAX, \
476 data->in_max[offset+7]); \
477 up(&data->update_lock); \
478 return count; \
479} \
a5099cfc 480static ssize_t set_temp##offset##_crit(struct device *dev, struct device_attribute *attr, const char *buf, \
1da177e4
LT
481 size_t count) \
482{ \
483 struct i2c_client *client = to_i2c_client(dev); \
484 struct pc87360_data *data = i2c_get_clientdata(client); \
485 long val = simple_strtol(buf, NULL, 10); \
486 \
487 down(&data->update_lock); \
488 data->in_crit[offset-4] = IN_TO_REG(val, data->in_vref); \
489 pc87360_write_value(data, LD_IN, offset+7, PC87365_REG_TEMP_CRIT, \
490 data->in_crit[offset-4]); \
491 up(&data->update_lock); \
492 return count; \
493} \
494static DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
495 show_temp##offset##_input, NULL); \
496static DEVICE_ATTR(temp##offset##_min, S_IWUSR | S_IRUGO, \
497 show_temp##offset##_min, set_temp##offset##_min); \
498static DEVICE_ATTR(temp##offset##_max, S_IWUSR | S_IRUGO, \
499 show_temp##offset##_max, set_temp##offset##_max); \
500static DEVICE_ATTR(temp##offset##_crit, S_IWUSR | S_IRUGO, \
501 show_temp##offset##_crit, set_temp##offset##_crit); \
502static DEVICE_ATTR(temp##offset##_status, S_IRUGO, \
503 show_temp##offset##_status, NULL);
504show_and_set_therm(4)
505show_and_set_therm(5)
506show_and_set_therm(6)
507
a5099cfc 508static ssize_t show_vid(struct device *dev, struct device_attribute *attr, char *buf)
1da177e4
LT
509{
510 struct pc87360_data *data = pc87360_update_device(dev);
511 return sprintf(buf, "%u\n", vid_from_reg(data->vid, data->vrm));
512}
513static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL);
514
a5099cfc 515static ssize_t show_vrm(struct device *dev, struct device_attribute *attr, char *buf)
1da177e4
LT
516{
517 struct pc87360_data *data = pc87360_update_device(dev);
518 return sprintf(buf, "%u\n", data->vrm);
519}
a5099cfc 520static ssize_t set_vrm(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
1da177e4
LT
521{
522 struct i2c_client *client = to_i2c_client(dev);
523 struct pc87360_data *data = i2c_get_clientdata(client);
524 data->vrm = simple_strtoul(buf, NULL, 10);
525 return count;
526}
527static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm, set_vrm);
528
a5099cfc 529static ssize_t show_in_alarms(struct device *dev, struct device_attribute *attr, char *buf)
1da177e4
LT
530{
531 struct pc87360_data *data = pc87360_update_device(dev);
532 return sprintf(buf, "%u\n", data->in_alarms);
533}
534static DEVICE_ATTR(alarms_in, S_IRUGO, show_in_alarms, NULL);
535
536#define show_and_set_temp(offset) \
a5099cfc 537static ssize_t show_temp##offset##_input(struct device *dev, struct device_attribute *attr, char *buf) \
1da177e4
LT
538{ \
539 struct pc87360_data *data = pc87360_update_device(dev); \
540 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[offset-1])); \
541} \
a5099cfc 542static ssize_t show_temp##offset##_min(struct device *dev, struct device_attribute *attr, char *buf) \
1da177e4
LT
543{ \
544 struct pc87360_data *data = pc87360_update_device(dev); \
545 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_min[offset-1])); \
546} \
a5099cfc 547static ssize_t show_temp##offset##_max(struct device *dev, struct device_attribute *attr, char *buf) \
1da177e4
LT
548{ \
549 struct pc87360_data *data = pc87360_update_device(dev); \
550 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max[offset-1])); \
551}\
a5099cfc 552static ssize_t show_temp##offset##_crit(struct device *dev, struct device_attribute *attr, char *buf) \
1da177e4
LT
553{ \
554 struct pc87360_data *data = pc87360_update_device(dev); \
555 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_crit[offset-1])); \
556}\
a5099cfc 557static ssize_t show_temp##offset##_status(struct device *dev, struct device_attribute *attr, char *buf) \
1da177e4
LT
558{ \
559 struct pc87360_data *data = pc87360_update_device(dev); \
560 return sprintf(buf, "%d\n", data->temp_status[offset-1]); \
561}\
a5099cfc 562static ssize_t set_temp##offset##_min(struct device *dev, struct device_attribute *attr, const char *buf, \
1da177e4
LT
563 size_t count) \
564{ \
565 struct i2c_client *client = to_i2c_client(dev); \
566 struct pc87360_data *data = i2c_get_clientdata(client); \
567 long val = simple_strtol(buf, NULL, 10); \
568 \
569 down(&data->update_lock); \
570 data->temp_min[offset-1] = TEMP_TO_REG(val); \
571 pc87360_write_value(data, LD_TEMP, offset-1, PC87365_REG_TEMP_MIN, \
572 data->temp_min[offset-1]); \
573 up(&data->update_lock); \
574 return count; \
575} \
a5099cfc 576static ssize_t set_temp##offset##_max(struct device *dev, struct device_attribute *attr, const char *buf, \
1da177e4
LT
577 size_t count) \
578{ \
579 struct i2c_client *client = to_i2c_client(dev); \
580 struct pc87360_data *data = i2c_get_clientdata(client); \
581 long val = simple_strtol(buf, NULL, 10); \
582 \
583 down(&data->update_lock); \
584 data->temp_max[offset-1] = TEMP_TO_REG(val); \
585 pc87360_write_value(data, LD_TEMP, offset-1, PC87365_REG_TEMP_MAX, \
586 data->temp_max[offset-1]); \
587 up(&data->update_lock); \
588 return count; \
589} \
a5099cfc 590static ssize_t set_temp##offset##_crit(struct device *dev, struct device_attribute *attr, const char *buf, \
1da177e4
LT
591 size_t count) \
592{ \
593 struct i2c_client *client = to_i2c_client(dev); \
594 struct pc87360_data *data = i2c_get_clientdata(client); \
595 long val = simple_strtol(buf, NULL, 10); \
596 \
597 down(&data->update_lock); \
598 data->temp_crit[offset-1] = TEMP_TO_REG(val); \
599 pc87360_write_value(data, LD_TEMP, offset-1, PC87365_REG_TEMP_CRIT, \
600 data->temp_crit[offset-1]); \
601 up(&data->update_lock); \
602 return count; \
603} \
604static DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
605 show_temp##offset##_input, NULL); \
606static DEVICE_ATTR(temp##offset##_min, S_IWUSR | S_IRUGO, \
607 show_temp##offset##_min, set_temp##offset##_min); \
608static DEVICE_ATTR(temp##offset##_max, S_IWUSR | S_IRUGO, \
609 show_temp##offset##_max, set_temp##offset##_max); \
610static DEVICE_ATTR(temp##offset##_crit, S_IWUSR | S_IRUGO, \
611 show_temp##offset##_crit, set_temp##offset##_crit); \
612static DEVICE_ATTR(temp##offset##_status, S_IRUGO, \
613 show_temp##offset##_status, NULL);
614show_and_set_temp(1)
615show_and_set_temp(2)
616show_and_set_temp(3)
617
a5099cfc 618static ssize_t show_temp_alarms(struct device *dev, struct device_attribute *attr, char *buf)
1da177e4
LT
619{
620 struct pc87360_data *data = pc87360_update_device(dev);
621 return sprintf(buf, "%u\n", data->temp_alarms);
622}
623static DEVICE_ATTR(alarms_temp, S_IRUGO, show_temp_alarms, NULL);
624
625/*
626 * Device detection, registration and update
627 */
628
e6cfb3ad 629static int __init pc87360_find(int sioaddr, u8 *devid, unsigned short *addresses)
1da177e4
LT
630{
631 u16 val;
632 int i;
633 int nrdev; /* logical device count */
634
635 /* No superio_enter */
636
637 /* Identify device */
638 val = superio_inb(sioaddr, DEVID);
639 switch (val) {
640 case 0xE1: /* PC87360 */
641 case 0xE8: /* PC87363 */
642 case 0xE4: /* PC87364 */
643 nrdev = 1;
644 break;
645 case 0xE5: /* PC87365 */
646 case 0xE9: /* PC87366 */
647 nrdev = 3;
648 break;
649 default:
650 superio_exit(sioaddr);
651 return -ENODEV;
652 }
653 /* Remember the device id */
654 *devid = val;
655
656 for (i = 0; i < nrdev; i++) {
657 /* select logical device */
658 superio_outb(sioaddr, DEV, logdev[i]);
659
660 val = superio_inb(sioaddr, ACT);
661 if (!(val & 0x01)) {
662 printk(KERN_INFO "pc87360: Device 0x%02x not "
663 "activated\n", logdev[i]);
664 continue;
665 }
666
667 val = (superio_inb(sioaddr, BASE) << 8)
668 | superio_inb(sioaddr, BASE + 1);
669 if (!val) {
670 printk(KERN_INFO "pc87360: Base address not set for "
671 "device 0x%02x\n", logdev[i]);
672 continue;
673 }
674
2d8672c5 675 addresses[i] = val;
1da177e4
LT
676
677 if (i==0) { /* Fans */
678 confreg[0] = superio_inb(sioaddr, 0xF0);
679 confreg[1] = superio_inb(sioaddr, 0xF1);
680
681#ifdef DEBUG
682 printk(KERN_DEBUG "pc87360: Fan 1: mon=%d "
683 "ctrl=%d inv=%d\n", (confreg[0]>>2)&1,
684 (confreg[0]>>3)&1, (confreg[0]>>4)&1);
685 printk(KERN_DEBUG "pc87360: Fan 2: mon=%d "
686 "ctrl=%d inv=%d\n", (confreg[0]>>5)&1,
687 (confreg[0]>>6)&1, (confreg[0]>>7)&1);
688 printk(KERN_DEBUG "pc87360: Fan 3: mon=%d "
689 "ctrl=%d inv=%d\n", confreg[1]&1,
690 (confreg[1]>>1)&1, (confreg[1]>>2)&1);
691#endif
692 } else if (i==1) { /* Voltages */
693 /* Are we using thermistors? */
694 if (*devid == 0xE9) { /* PC87366 */
695 /* These registers are not logical-device
696 specific, just that we won't need them if
697 we don't use the VLM device */
698 confreg[2] = superio_inb(sioaddr, 0x2B);
699 confreg[3] = superio_inb(sioaddr, 0x25);
700
701 if (confreg[2] & 0x40) {
702 printk(KERN_INFO "pc87360: Using "
703 "thermistors for temperature "
704 "monitoring\n");
705 }
706 if (confreg[3] & 0xE0) {
707 printk(KERN_INFO "pc87360: VID "
708 "inputs routed (mode %u)\n",
709 confreg[3] >> 5);
710 }
711 }
712 }
713 }
714
715 superio_exit(sioaddr);
716 return 0;
717}
718
2d8672c5 719static int pc87360_detect(struct i2c_adapter *adapter)
1da177e4
LT
720{
721 int i;
722 struct i2c_client *new_client;
723 struct pc87360_data *data;
724 int err = 0;
725 const char *name = "pc87360";
726 int use_thermistors = 0;
727
1da177e4
LT
728 if (!(data = kmalloc(sizeof(struct pc87360_data), GFP_KERNEL)))
729 return -ENOMEM;
730 memset(data, 0x00, sizeof(struct pc87360_data));
731
732 new_client = &data->client;
733 i2c_set_clientdata(new_client, data);
734 new_client->addr = address;
735 init_MUTEX(&data->lock);
736 new_client->adapter = adapter;
737 new_client->driver = &pc87360_driver;
738 new_client->flags = 0;
739
740 data->fannr = 2;
741 data->innr = 0;
742 data->tempnr = 0;
743
744 switch (devid) {
745 case 0xe8:
746 name = "pc87363";
747 break;
748 case 0xe4:
749 name = "pc87364";
750 data->fannr = 3;
751 break;
752 case 0xe5:
753 name = "pc87365";
754 data->fannr = extra_isa[0] ? 3 : 0;
755 data->innr = extra_isa[1] ? 11 : 0;
756 data->tempnr = extra_isa[2] ? 2 : 0;
757 break;
758 case 0xe9:
759 name = "pc87366";
760 data->fannr = extra_isa[0] ? 3 : 0;
761 data->innr = extra_isa[1] ? 14 : 0;
762 data->tempnr = extra_isa[2] ? 3 : 0;
763 break;
764 }
765
766 strcpy(new_client->name, name);
767 data->valid = 0;
768 init_MUTEX(&data->update_lock);
769
770 for (i = 0; i < 3; i++) {
771 if (((data->address[i] = extra_isa[i]))
772 && !request_region(extra_isa[i], PC87360_EXTENT,
773 pc87360_driver.name)) {
774 dev_err(&new_client->dev, "Region 0x%x-0x%x already "
775 "in use!\n", extra_isa[i],
776 extra_isa[i]+PC87360_EXTENT-1);
777 for (i--; i >= 0; i--)
778 release_region(extra_isa[i], PC87360_EXTENT);
779 err = -EBUSY;
780 goto ERROR1;
781 }
782 }
783
784 /* Retrieve the fans configuration from Super-I/O space */
785 if (data->fannr)
786 data->fan_conf = confreg[0] | (confreg[1] << 8);
787
788 if ((err = i2c_attach_client(new_client)))
789 goto ERROR2;
790
791 /* Use the correct reference voltage
792 Unless both the VLM and the TMS logical devices agree to
793 use an external Vref, the internal one is used. */
794 if (data->innr) {
795 i = pc87360_read_value(data, LD_IN, NO_BANK,
796 PC87365_REG_IN_CONFIG);
797 if (data->tempnr) {
798 i &= pc87360_read_value(data, LD_TEMP, NO_BANK,
799 PC87365_REG_TEMP_CONFIG);
800 }
801 data->in_vref = (i&0x02) ? 3025 : 2966;
802 dev_dbg(&new_client->dev, "Using %s reference voltage\n",
803 (i&0x02) ? "external" : "internal");
804
805 data->vid_conf = confreg[3];
806 data->vrm = 90;
807 }
808
809 /* Fan clock dividers may be needed before any data is read */
810 for (i = 0; i < data->fannr; i++) {
811 if (FAN_CONFIG_MONITOR(data->fan_conf, i))
812 data->fan_status[i] = pc87360_read_value(data,
813 LD_FAN, NO_BANK,
814 PC87360_REG_FAN_STATUS(i));
815 }
816
817 if (init > 0) {
818 if (devid == 0xe9 && data->address[1]) /* PC87366 */
819 use_thermistors = confreg[2] & 0x40;
820
821 pc87360_init_client(new_client, use_thermistors);
822 }
823
824 /* Register sysfs hooks */
943b0830
MH
825 data->class_dev = hwmon_device_register(&new_client->dev);
826 if (IS_ERR(data->class_dev)) {
827 err = PTR_ERR(data->class_dev);
828 goto ERROR3;
829 }
830
1da177e4
LT
831 if (data->innr) {
832 device_create_file(&new_client->dev, &dev_attr_in0_input);
833 device_create_file(&new_client->dev, &dev_attr_in1_input);
834 device_create_file(&new_client->dev, &dev_attr_in2_input);
835 device_create_file(&new_client->dev, &dev_attr_in3_input);
836 device_create_file(&new_client->dev, &dev_attr_in4_input);
837 device_create_file(&new_client->dev, &dev_attr_in5_input);
838 device_create_file(&new_client->dev, &dev_attr_in6_input);
839 device_create_file(&new_client->dev, &dev_attr_in7_input);
840 device_create_file(&new_client->dev, &dev_attr_in8_input);
841 device_create_file(&new_client->dev, &dev_attr_in9_input);
842 device_create_file(&new_client->dev, &dev_attr_in10_input);
843 device_create_file(&new_client->dev, &dev_attr_in0_min);
844 device_create_file(&new_client->dev, &dev_attr_in1_min);
845 device_create_file(&new_client->dev, &dev_attr_in2_min);
846 device_create_file(&new_client->dev, &dev_attr_in3_min);
847 device_create_file(&new_client->dev, &dev_attr_in4_min);
848 device_create_file(&new_client->dev, &dev_attr_in5_min);
849 device_create_file(&new_client->dev, &dev_attr_in6_min);
850 device_create_file(&new_client->dev, &dev_attr_in7_min);
851 device_create_file(&new_client->dev, &dev_attr_in8_min);
852 device_create_file(&new_client->dev, &dev_attr_in9_min);
853 device_create_file(&new_client->dev, &dev_attr_in10_min);
854 device_create_file(&new_client->dev, &dev_attr_in0_max);
855 device_create_file(&new_client->dev, &dev_attr_in1_max);
856 device_create_file(&new_client->dev, &dev_attr_in2_max);
857 device_create_file(&new_client->dev, &dev_attr_in3_max);
858 device_create_file(&new_client->dev, &dev_attr_in4_max);
859 device_create_file(&new_client->dev, &dev_attr_in5_max);
860 device_create_file(&new_client->dev, &dev_attr_in6_max);
861 device_create_file(&new_client->dev, &dev_attr_in7_max);
862 device_create_file(&new_client->dev, &dev_attr_in8_max);
863 device_create_file(&new_client->dev, &dev_attr_in9_max);
864 device_create_file(&new_client->dev, &dev_attr_in10_max);
865 device_create_file(&new_client->dev, &dev_attr_in0_status);
866 device_create_file(&new_client->dev, &dev_attr_in1_status);
867 device_create_file(&new_client->dev, &dev_attr_in2_status);
868 device_create_file(&new_client->dev, &dev_attr_in3_status);
869 device_create_file(&new_client->dev, &dev_attr_in4_status);
870 device_create_file(&new_client->dev, &dev_attr_in5_status);
871 device_create_file(&new_client->dev, &dev_attr_in6_status);
872 device_create_file(&new_client->dev, &dev_attr_in7_status);
873 device_create_file(&new_client->dev, &dev_attr_in8_status);
874 device_create_file(&new_client->dev, &dev_attr_in9_status);
875 device_create_file(&new_client->dev, &dev_attr_in10_status);
876
877 device_create_file(&new_client->dev, &dev_attr_cpu0_vid);
878 device_create_file(&new_client->dev, &dev_attr_vrm);
879 device_create_file(&new_client->dev, &dev_attr_alarms_in);
880 }
881
882 if (data->tempnr) {
883 device_create_file(&new_client->dev, &dev_attr_temp1_input);
884 device_create_file(&new_client->dev, &dev_attr_temp2_input);
885 device_create_file(&new_client->dev, &dev_attr_temp1_min);
886 device_create_file(&new_client->dev, &dev_attr_temp2_min);
887 device_create_file(&new_client->dev, &dev_attr_temp1_max);
888 device_create_file(&new_client->dev, &dev_attr_temp2_max);
889 device_create_file(&new_client->dev, &dev_attr_temp1_crit);
890 device_create_file(&new_client->dev, &dev_attr_temp2_crit);
891 device_create_file(&new_client->dev, &dev_attr_temp1_status);
892 device_create_file(&new_client->dev, &dev_attr_temp2_status);
893
894 device_create_file(&new_client->dev, &dev_attr_alarms_temp);
895 }
896 if (data->tempnr == 3) {
897 device_create_file(&new_client->dev, &dev_attr_temp3_input);
898 device_create_file(&new_client->dev, &dev_attr_temp3_min);
899 device_create_file(&new_client->dev, &dev_attr_temp3_max);
900 device_create_file(&new_client->dev, &dev_attr_temp3_crit);
901 device_create_file(&new_client->dev, &dev_attr_temp3_status);
902 }
903 if (data->innr == 14) {
904 device_create_file(&new_client->dev, &dev_attr_temp4_input);
905 device_create_file(&new_client->dev, &dev_attr_temp5_input);
906 device_create_file(&new_client->dev, &dev_attr_temp6_input);
907 device_create_file(&new_client->dev, &dev_attr_temp4_min);
908 device_create_file(&new_client->dev, &dev_attr_temp5_min);
909 device_create_file(&new_client->dev, &dev_attr_temp6_min);
910 device_create_file(&new_client->dev, &dev_attr_temp4_max);
911 device_create_file(&new_client->dev, &dev_attr_temp5_max);
912 device_create_file(&new_client->dev, &dev_attr_temp6_max);
913 device_create_file(&new_client->dev, &dev_attr_temp4_crit);
914 device_create_file(&new_client->dev, &dev_attr_temp5_crit);
915 device_create_file(&new_client->dev, &dev_attr_temp6_crit);
916 device_create_file(&new_client->dev, &dev_attr_temp4_status);
917 device_create_file(&new_client->dev, &dev_attr_temp5_status);
918 device_create_file(&new_client->dev, &dev_attr_temp6_status);
919 }
920
921 if (data->fannr) {
922 if (FAN_CONFIG_MONITOR(data->fan_conf, 0)) {
923 device_create_file(&new_client->dev,
924 &dev_attr_fan1_input);
925 device_create_file(&new_client->dev,
926 &dev_attr_fan1_min);
927 device_create_file(&new_client->dev,
928 &dev_attr_fan1_div);
929 device_create_file(&new_client->dev,
930 &dev_attr_fan1_status);
931 }
932
933 if (FAN_CONFIG_MONITOR(data->fan_conf, 1)) {
934 device_create_file(&new_client->dev,
935 &dev_attr_fan2_input);
936 device_create_file(&new_client->dev,
937 &dev_attr_fan2_min);
938 device_create_file(&new_client->dev,
939 &dev_attr_fan2_div);
940 device_create_file(&new_client->dev,
941 &dev_attr_fan2_status);
942 }
943
944 if (FAN_CONFIG_CONTROL(data->fan_conf, 0))
945 device_create_file(&new_client->dev, &dev_attr_pwm1);
946 if (FAN_CONFIG_CONTROL(data->fan_conf, 1))
947 device_create_file(&new_client->dev, &dev_attr_pwm2);
948 }
949 if (data->fannr == 3) {
950 if (FAN_CONFIG_MONITOR(data->fan_conf, 2)) {
951 device_create_file(&new_client->dev,
952 &dev_attr_fan3_input);
953 device_create_file(&new_client->dev,
954 &dev_attr_fan3_min);
955 device_create_file(&new_client->dev,
956 &dev_attr_fan3_div);
957 device_create_file(&new_client->dev,
958 &dev_attr_fan3_status);
959 }
960
961 if (FAN_CONFIG_CONTROL(data->fan_conf, 2))
962 device_create_file(&new_client->dev, &dev_attr_pwm3);
963 }
964
965 return 0;
966
943b0830
MH
967ERROR3:
968 i2c_detach_client(new_client);
1da177e4
LT
969ERROR2:
970 for (i = 0; i < 3; i++) {
971 if (data->address[i]) {
972 release_region(data->address[i], PC87360_EXTENT);
973 }
974 }
975ERROR1:
976 kfree(data);
977 return err;
978}
979
980static int pc87360_detach_client(struct i2c_client *client)
981{
982 struct pc87360_data *data = i2c_get_clientdata(client);
983 int i;
984
943b0830
MH
985 hwmon_device_unregister(data->class_dev);
986
7bef5594 987 if ((i = i2c_detach_client(client)))
1da177e4 988 return i;
1da177e4
LT
989
990 for (i = 0; i < 3; i++) {
991 if (data->address[i]) {
992 release_region(data->address[i], PC87360_EXTENT);
993 }
994 }
995 kfree(data);
996
997 return 0;
998}
999
1000/* ldi is the logical device index
1001 bank is for voltages and temperatures only */
1002static int pc87360_read_value(struct pc87360_data *data, u8 ldi, u8 bank,
1003 u8 reg)
1004{
1005 int res;
1006
1007 down(&(data->lock));
1008 if (bank != NO_BANK)
1009 outb_p(bank, data->address[ldi] + PC87365_REG_BANK);
1010 res = inb_p(data->address[ldi] + reg);
1011 up(&(data->lock));
1012
1013 return res;
1014}
1015
1016static void pc87360_write_value(struct pc87360_data *data, u8 ldi, u8 bank,
1017 u8 reg, u8 value)
1018{
1019 down(&(data->lock));
1020 if (bank != NO_BANK)
1021 outb_p(bank, data->address[ldi] + PC87365_REG_BANK);
1022 outb_p(value, data->address[ldi] + reg);
1023 up(&(data->lock));
1024}
1025
1026static void pc87360_init_client(struct i2c_client *client, int use_thermistors)
1027{
1028 struct pc87360_data *data = i2c_get_clientdata(client);
1029 int i, nr;
1030 const u8 init_in[14] = { 2, 2, 2, 2, 2, 2, 2, 1, 1, 3, 1, 2, 2, 2 };
1031 const u8 init_temp[3] = { 2, 2, 1 };
1032 u8 reg;
1033
1034 if (init >= 2 && data->innr) {
1035 reg = pc87360_read_value(data, LD_IN, NO_BANK,
1036 PC87365_REG_IN_CONVRATE);
368609c5 1037 dev_info(&client->dev, "VLM conversion set to "
1da177e4
LT
1038 "1s period, 160us delay\n");
1039 pc87360_write_value(data, LD_IN, NO_BANK,
1040 PC87365_REG_IN_CONVRATE,
1041 (reg & 0xC0) | 0x11);
1042 }
1043
1044 nr = data->innr < 11 ? data->innr : 11;
1045 for (i=0; i<nr; i++) {
1046 if (init >= init_in[i]) {
1047 /* Forcibly enable voltage channel */
1048 reg = pc87360_read_value(data, LD_IN, i,
1049 PC87365_REG_IN_STATUS);
1050 if (!(reg & 0x01)) {
1051 dev_dbg(&client->dev, "Forcibly "
1052 "enabling in%d\n", i);
1053 pc87360_write_value(data, LD_IN, i,
1054 PC87365_REG_IN_STATUS,
1055 (reg & 0x68) | 0x87);
1056 }
1057 }
1058 }
1059
1060 /* We can't blindly trust the Super-I/O space configuration bit,
1061 most BIOS won't set it properly */
1062 for (i=11; i<data->innr; i++) {
1063 reg = pc87360_read_value(data, LD_IN, i,
1064 PC87365_REG_TEMP_STATUS);
1065 use_thermistors = use_thermistors || (reg & 0x01);
1066 }
1067
1068 i = use_thermistors ? 2 : 0;
1069 for (; i<data->tempnr; i++) {
1070 if (init >= init_temp[i]) {
1071 /* Forcibly enable temperature channel */
1072 reg = pc87360_read_value(data, LD_TEMP, i,
1073 PC87365_REG_TEMP_STATUS);
1074 if (!(reg & 0x01)) {
1075 dev_dbg(&client->dev, "Forcibly "
1076 "enabling temp%d\n", i+1);
1077 pc87360_write_value(data, LD_TEMP, i,
1078 PC87365_REG_TEMP_STATUS,
1079 0xCF);
1080 }
1081 }
1082 }
1083
1084 if (use_thermistors) {
1085 for (i=11; i<data->innr; i++) {
1086 if (init >= init_in[i]) {
1087 /* The pin may already be used by thermal
1088 diodes */
1089 reg = pc87360_read_value(data, LD_TEMP,
1090 (i-11)/2, PC87365_REG_TEMP_STATUS);
1091 if (reg & 0x01) {
1092 dev_dbg(&client->dev, "Skipping "
1093 "temp%d, pin already in use "
1094 "by temp%d\n", i-7, (i-11)/2);
1095 continue;
1096 }
1097
1098 /* Forcibly enable thermistor channel */
1099 reg = pc87360_read_value(data, LD_IN, i,
1100 PC87365_REG_IN_STATUS);
1101 if (!(reg & 0x01)) {
1102 dev_dbg(&client->dev, "Forcibly "
1103 "enabling temp%d\n", i-7);
1104 pc87360_write_value(data, LD_IN, i,
1105 PC87365_REG_TEMP_STATUS,
1106 (reg & 0x60) | 0x8F);
1107 }
1108 }
1109 }
1110 }
1111
1112 if (data->innr) {
1113 reg = pc87360_read_value(data, LD_IN, NO_BANK,
1114 PC87365_REG_IN_CONFIG);
1115 if (reg & 0x01) {
1116 dev_dbg(&client->dev, "Forcibly "
1117 "enabling monitoring (VLM)\n");
1118 pc87360_write_value(data, LD_IN, NO_BANK,
1119 PC87365_REG_IN_CONFIG,
1120 reg & 0xFE);
1121 }
1122 }
1123
1124 if (data->tempnr) {
1125 reg = pc87360_read_value(data, LD_TEMP, NO_BANK,
1126 PC87365_REG_TEMP_CONFIG);
1127 if (reg & 0x01) {
1128 dev_dbg(&client->dev, "Forcibly enabling "
1129 "monitoring (TMS)\n");
1130 pc87360_write_value(data, LD_TEMP, NO_BANK,
1131 PC87365_REG_TEMP_CONFIG,
1132 reg & 0xFE);
1133 }
1134
1135 if (init >= 2) {
1136 /* Chip config as documented by National Semi. */
1137 pc87360_write_value(data, LD_TEMP, 0xF, 0xA, 0x08);
1138 /* We voluntarily omit the bank here, in case the
1139 sequence itself matters. It shouldn't be a problem,
1140 since nobody else is supposed to access the
1141 device at that point. */
1142 pc87360_write_value(data, LD_TEMP, NO_BANK, 0xB, 0x04);
1143 pc87360_write_value(data, LD_TEMP, NO_BANK, 0xC, 0x35);
1144 pc87360_write_value(data, LD_TEMP, NO_BANK, 0xD, 0x05);
1145 pc87360_write_value(data, LD_TEMP, NO_BANK, 0xE, 0x05);
1146 }
1147 }
1148}
1149
1150static void pc87360_autodiv(struct i2c_client *client, int nr)
1151{
1152 struct pc87360_data *data = i2c_get_clientdata(client);
1153 u8 old_min = data->fan_min[nr];
1154
1155 /* Increase clock divider if needed and possible */
1156 if ((data->fan_status[nr] & 0x04) /* overflow flag */
1157 || (data->fan[nr] >= 224)) { /* next to overflow */
1158 if ((data->fan_status[nr] & 0x60) != 0x60) {
1159 data->fan_status[nr] += 0x20;
1160 data->fan_min[nr] >>= 1;
1161 data->fan[nr] >>= 1;
1162 dev_dbg(&client->dev, "Increasing "
1163 "clock divider to %d for fan %d\n",
1164 FAN_DIV_FROM_REG(data->fan_status[nr]), nr+1);
1165 }
1166 } else {
1167 /* Decrease clock divider if possible */
1168 while (!(data->fan_min[nr] & 0x80) /* min "nails" divider */
1169 && data->fan[nr] < 85 /* bad accuracy */
1170 && (data->fan_status[nr] & 0x60) != 0x00) {
1171 data->fan_status[nr] -= 0x20;
1172 data->fan_min[nr] <<= 1;
1173 data->fan[nr] <<= 1;
1174 dev_dbg(&client->dev, "Decreasing "
1175 "clock divider to %d for fan %d\n",
1176 FAN_DIV_FROM_REG(data->fan_status[nr]),
1177 nr+1);
1178 }
1179 }
1180
1181 /* Write new fan min if it changed */
1182 if (old_min != data->fan_min[nr]) {
1183 pc87360_write_value(data, LD_FAN, NO_BANK,
1184 PC87360_REG_FAN_MIN(nr),
1185 data->fan_min[nr]);
1186 }
1187}
1188
1189static struct pc87360_data *pc87360_update_device(struct device *dev)
1190{
1191 struct i2c_client *client = to_i2c_client(dev);
1192 struct pc87360_data *data = i2c_get_clientdata(client);
1193 u8 i;
1194
1195 down(&data->update_lock);
1196
1197 if (time_after(jiffies, data->last_updated + HZ * 2) || !data->valid) {
1198 dev_dbg(&client->dev, "Data update\n");
1199
1200 /* Fans */
1201 for (i = 0; i < data->fannr; i++) {
1202 if (FAN_CONFIG_MONITOR(data->fan_conf, i)) {
1203 data->fan_status[i] =
1204 pc87360_read_value(data, LD_FAN,
1205 NO_BANK, PC87360_REG_FAN_STATUS(i));
1206 data->fan[i] = pc87360_read_value(data, LD_FAN,
1207 NO_BANK, PC87360_REG_FAN(i));
1208 data->fan_min[i] = pc87360_read_value(data,
1209 LD_FAN, NO_BANK,
1210 PC87360_REG_FAN_MIN(i));
1211 /* Change clock divider if needed */
1212 pc87360_autodiv(client, i);
1213 /* Clear bits and write new divider */
1214 pc87360_write_value(data, LD_FAN, NO_BANK,
1215 PC87360_REG_FAN_STATUS(i),
1216 data->fan_status[i]);
1217 }
1218 if (FAN_CONFIG_CONTROL(data->fan_conf, i))
1219 data->pwm[i] = pc87360_read_value(data, LD_FAN,
1220 NO_BANK, PC87360_REG_PWM(i));
1221 }
1222
1223 /* Voltages */
1224 for (i = 0; i < data->innr; i++) {
1225 data->in_status[i] = pc87360_read_value(data, LD_IN, i,
1226 PC87365_REG_IN_STATUS);
1227 /* Clear bits */
1228 pc87360_write_value(data, LD_IN, i,
1229 PC87365_REG_IN_STATUS,
1230 data->in_status[i]);
1231 if ((data->in_status[i] & 0x81) == 0x81) {
1232 data->in[i] = pc87360_read_value(data, LD_IN,
1233 i, PC87365_REG_IN);
1234 }
1235 if (data->in_status[i] & 0x01) {
1236 data->in_min[i] = pc87360_read_value(data,
1237 LD_IN, i,
1238 PC87365_REG_IN_MIN);
1239 data->in_max[i] = pc87360_read_value(data,
1240 LD_IN, i,
1241 PC87365_REG_IN_MAX);
1242 if (i >= 11)
1243 data->in_crit[i-11] =
1244 pc87360_read_value(data, LD_IN,
1245 i, PC87365_REG_TEMP_CRIT);
1246 }
1247 }
1248 if (data->innr) {
1249 data->in_alarms = pc87360_read_value(data, LD_IN,
1250 NO_BANK, PC87365_REG_IN_ALARMS1)
1251 | ((pc87360_read_value(data, LD_IN,
1252 NO_BANK, PC87365_REG_IN_ALARMS2)
1253 & 0x07) << 8);
1254 data->vid = (data->vid_conf & 0xE0) ?
1255 pc87360_read_value(data, LD_IN,
1256 NO_BANK, PC87365_REG_VID) : 0x1F;
1257 }
1258
1259 /* Temperatures */
1260 for (i = 0; i < data->tempnr; i++) {
1261 data->temp_status[i] = pc87360_read_value(data,
1262 LD_TEMP, i,
1263 PC87365_REG_TEMP_STATUS);
1264 /* Clear bits */
1265 pc87360_write_value(data, LD_TEMP, i,
1266 PC87365_REG_TEMP_STATUS,
1267 data->temp_status[i]);
1268 if ((data->temp_status[i] & 0x81) == 0x81) {
1269 data->temp[i] = pc87360_read_value(data,
1270 LD_TEMP, i,
1271 PC87365_REG_TEMP);
1272 }
1273 if (data->temp_status[i] & 0x01) {
1274 data->temp_min[i] = pc87360_read_value(data,
1275 LD_TEMP, i,
1276 PC87365_REG_TEMP_MIN);
1277 data->temp_max[i] = pc87360_read_value(data,
1278 LD_TEMP, i,
1279 PC87365_REG_TEMP_MAX);
1280 data->temp_crit[i] = pc87360_read_value(data,
1281 LD_TEMP, i,
1282 PC87365_REG_TEMP_CRIT);
1283 }
1284 }
1285 if (data->tempnr) {
1286 data->temp_alarms = pc87360_read_value(data, LD_TEMP,
1287 NO_BANK, PC87365_REG_TEMP_ALARMS)
1288 & 0x3F;
1289 }
1290
1291 data->last_updated = jiffies;
1292 data->valid = 1;
1293 }
1294
1295 up(&data->update_lock);
1296
1297 return data;
1298}
1299
1300static int __init pc87360_init(void)
1301{
1302 int i;
1303
1304 if (pc87360_find(0x2e, &devid, extra_isa)
1305 && pc87360_find(0x4e, &devid, extra_isa)) {
1306 printk(KERN_WARNING "pc87360: PC8736x not detected, "
1307 "module not inserted.\n");
1308 return -ENODEV;
1309 }
1310
1311 /* Arbitrarily pick one of the addresses */
1312 for (i = 0; i < 3; i++) {
1313 if (extra_isa[i] != 0x0000) {
2d8672c5 1314 address = extra_isa[i];
1da177e4
LT
1315 break;
1316 }
1317 }
1318
2d8672c5 1319 if (address == 0x0000) {
1da177e4
LT
1320 printk(KERN_WARNING "pc87360: No active logical device, "
1321 "module not inserted.\n");
1322 return -ENODEV;
1323 }
1324
fde09509 1325 return i2c_isa_add_driver(&pc87360_driver);
1da177e4
LT
1326}
1327
1328static void __exit pc87360_exit(void)
1329{
fde09509 1330 i2c_isa_del_driver(&pc87360_driver);
1da177e4
LT
1331}
1332
1333
1334MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
1335MODULE_DESCRIPTION("PC8736x hardware monitor");
1336MODULE_LICENSE("GPL");
1337
1338module_init(pc87360_init);
1339module_exit(pc87360_exit);