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