blob: 4041488de6f14719660c790fdbb1bbc1395ad69d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * pc87360.c - Part of lm_sensors, Linux kernel modules
3 * for hardware monitoring
4 * Copyright (C) 2004 Jean Delvare <khali@linux-fr.org>
5 *
6 * Copied from smsc47m1.c:
7 * Copyright (C) 2002 Mark D. Studebaker <mdsxyz123@yahoo.com>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 *
23 * Supports the following chips:
24 *
25 * Chip #vin #fan #pwm #temp devid
26 * PC87360 - 2 2 - 0xE1
27 * PC87363 - 2 2 - 0xE8
28 * PC87364 - 3 3 - 0xE4
29 * PC87365 11 3 3 2 0xE5
30 * PC87366 11 3 3 3-4 0xE9
31 *
32 * This driver assumes that no more than one chip is present, and one of
33 * the standard Super-I/O addresses is used (0x2E/0x2F or 0x4E/0x4F).
34 */
35
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/module.h>
37#include <linux/init.h>
38#include <linux/slab.h>
39#include <linux/jiffies.h>
40#include <linux/i2c.h>
Jean Delvarefde09502005-07-19 23:51:07 +020041#include <linux/i2c-isa.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#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
Linus Torvalds1da177e2005-04-16 15:20:36 -070047static u8 devid;
Jean Delvare2d8672c2005-07-19 23:56:35 +020048static unsigned short address;
49static unsigned short extra_isa[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -070050static u8 confreg[4];
51
52enum chips { any_chip, pc87360, pc87363, pc87364, pc87365, pc87366 };
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
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;
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400184 struct class_device *class_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 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
Jean Delvare2d8672c2005-07-19 23:56:35 +0200223static int pc87360_detect(struct i2c_adapter *adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224static 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",
Jean Delvare2d8672c2005-07-19 23:56:35 +0200240 .attach_adapter = pc87360_detect,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 .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) \
Yani Ioannoua5099cf2005-05-17 06:42:25 -0400278static ssize_t show_fan##offset##_input(struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279{ \
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} \
Yani Ioannoua5099cf2005-05-17 06:42:25 -0400284static ssize_t show_fan##offset##_min(struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285{ \
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} \
Yani Ioannoua5099cf2005-05-17 06:42:25 -0400290static ssize_t show_fan##offset##_div(struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291{ \
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} \
Yani Ioannoua5099cf2005-05-17 06:42:25 -0400296static ssize_t show_fan##offset##_status(struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297{ \
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} \
Yani Ioannoua5099cf2005-05-17 06:42:25 -0400302static ssize_t set_fan##offset##_min(struct device *dev, struct device_attribute *attr, const char *buf, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 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) \
Yani Ioannoua5099cf2005-05-17 06:42:25 -0400320static ssize_t show_pwm##offset(struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321{ \
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} \
Yani Ioannoua5099cf2005-05-17 06:42:25 -0400328static ssize_t set_pwm##offset(struct device *dev, struct device_attribute *attr, const char *buf, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 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) \
Yani Ioannoua5099cf2005-05-17 06:42:25 -0400350static ssize_t show_in##offset##_input(struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351{ \
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} \
Yani Ioannoua5099cf2005-05-17 06:42:25 -0400356static ssize_t show_in##offset##_min(struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357{ \
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} \
Yani Ioannoua5099cf2005-05-17 06:42:25 -0400362static ssize_t show_in##offset##_max(struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363{ \
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} \
Yani Ioannoua5099cf2005-05-17 06:42:25 -0400368static ssize_t show_in##offset##_status(struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369{ \
370 struct pc87360_data *data = pc87360_update_device(dev); \
371 return sprintf(buf, "%u\n", data->in_status[offset]); \
372} \
Yani Ioannoua5099cf2005-05-17 06:42:25 -0400373static ssize_t set_in##offset##_min(struct device *dev, struct device_attribute *attr, const char *buf, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 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} \
Yani Ioannoua5099cf2005-05-17 06:42:25 -0400387static ssize_t set_in##offset##_max(struct device *dev, struct device_attribute *attr, const char *buf, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 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) \
Yani Ioannoua5099cf2005-05-17 06:42:25 -0400423static ssize_t show_temp##offset##_input(struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424{ \
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} \
Yani Ioannoua5099cf2005-05-17 06:42:25 -0400429static ssize_t show_temp##offset##_min(struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430{ \
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} \
Yani Ioannoua5099cf2005-05-17 06:42:25 -0400435static ssize_t show_temp##offset##_max(struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436{ \
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} \
Yani Ioannoua5099cf2005-05-17 06:42:25 -0400441static ssize_t show_temp##offset##_crit(struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442{ \
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} \
Yani Ioannoua5099cf2005-05-17 06:42:25 -0400447static ssize_t show_temp##offset##_status(struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448{ \
449 struct pc87360_data *data = pc87360_update_device(dev); \
450 return sprintf(buf, "%u\n", data->in_status[offset+7]); \
451} \
Yani Ioannoua5099cf2005-05-17 06:42:25 -0400452static ssize_t set_temp##offset##_min(struct device *dev, struct device_attribute *attr, const char *buf, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 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} \
Yani Ioannoua5099cf2005-05-17 06:42:25 -0400466static ssize_t set_temp##offset##_max(struct device *dev, struct device_attribute *attr, const char *buf, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 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} \
Yani Ioannoua5099cf2005-05-17 06:42:25 -0400480static ssize_t set_temp##offset##_crit(struct device *dev, struct device_attribute *attr, const char *buf, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 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
Yani Ioannoua5099cf2005-05-17 06:42:25 -0400508static ssize_t show_vid(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509{
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
Yani Ioannoua5099cf2005-05-17 06:42:25 -0400515static ssize_t show_vrm(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516{
517 struct pc87360_data *data = pc87360_update_device(dev);
518 return sprintf(buf, "%u\n", data->vrm);
519}
Yani Ioannoua5099cf2005-05-17 06:42:25 -0400520static ssize_t set_vrm(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521{
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
Yani Ioannoua5099cf2005-05-17 06:42:25 -0400529static ssize_t show_in_alarms(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530{
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) \
Yani Ioannoua5099cf2005-05-17 06:42:25 -0400537static ssize_t show_temp##offset##_input(struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538{ \
539 struct pc87360_data *data = pc87360_update_device(dev); \
540 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[offset-1])); \
541} \
Yani Ioannoua5099cf2005-05-17 06:42:25 -0400542static ssize_t show_temp##offset##_min(struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543{ \
544 struct pc87360_data *data = pc87360_update_device(dev); \
545 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_min[offset-1])); \
546} \
Yani Ioannoua5099cf2005-05-17 06:42:25 -0400547static ssize_t show_temp##offset##_max(struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548{ \
549 struct pc87360_data *data = pc87360_update_device(dev); \
550 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max[offset-1])); \
551}\
Yani Ioannoua5099cf2005-05-17 06:42:25 -0400552static ssize_t show_temp##offset##_crit(struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553{ \
554 struct pc87360_data *data = pc87360_update_device(dev); \
555 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_crit[offset-1])); \
556}\
Yani Ioannoua5099cf2005-05-17 06:42:25 -0400557static ssize_t show_temp##offset##_status(struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558{ \
559 struct pc87360_data *data = pc87360_update_device(dev); \
560 return sprintf(buf, "%d\n", data->temp_status[offset-1]); \
561}\
Yani Ioannoua5099cf2005-05-17 06:42:25 -0400562static ssize_t set_temp##offset##_min(struct device *dev, struct device_attribute *attr, const char *buf, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 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} \
Yani Ioannoua5099cf2005-05-17 06:42:25 -0400576static ssize_t set_temp##offset##_max(struct device *dev, struct device_attribute *attr, const char *buf, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 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} \
Yani Ioannoua5099cf2005-05-17 06:42:25 -0400590static ssize_t set_temp##offset##_crit(struct device *dev, struct device_attribute *attr, const char *buf, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 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
Yani Ioannoua5099cf2005-05-17 06:42:25 -0400618static ssize_t show_temp_alarms(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619{
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
Jean Delvare2d8672c2005-07-19 23:56:35 +0200629static int pc87360_find(int sioaddr, u8 *devid, unsigned short *addresses)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630{
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
Jean Delvare2d8672c2005-07-19 23:56:35 +0200675 addresses[i] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
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
Jean Delvare2d8672c2005-07-19 23:56:35 +0200719static int pc87360_detect(struct i2c_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720{
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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 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 */
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400825 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 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
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400967ERROR3:
968 i2c_detach_client(new_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969ERROR2:
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
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400985 hwmon_device_unregister(data->class_dev);
986
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 if ((i = i2c_detach_client(client))) {
988 dev_err(&client->dev, "Client deregistration failed, "
989 "client not detached.\n");
990 return i;
991 }
992
993 for (i = 0; i < 3; i++) {
994 if (data->address[i]) {
995 release_region(data->address[i], PC87360_EXTENT);
996 }
997 }
998 kfree(data);
999
1000 return 0;
1001}
1002
1003/* ldi is the logical device index
1004 bank is for voltages and temperatures only */
1005static int pc87360_read_value(struct pc87360_data *data, u8 ldi, u8 bank,
1006 u8 reg)
1007{
1008 int res;
1009
1010 down(&(data->lock));
1011 if (bank != NO_BANK)
1012 outb_p(bank, data->address[ldi] + PC87365_REG_BANK);
1013 res = inb_p(data->address[ldi] + reg);
1014 up(&(data->lock));
1015
1016 return res;
1017}
1018
1019static void pc87360_write_value(struct pc87360_data *data, u8 ldi, u8 bank,
1020 u8 reg, u8 value)
1021{
1022 down(&(data->lock));
1023 if (bank != NO_BANK)
1024 outb_p(bank, data->address[ldi] + PC87365_REG_BANK);
1025 outb_p(value, data->address[ldi] + reg);
1026 up(&(data->lock));
1027}
1028
1029static void pc87360_init_client(struct i2c_client *client, int use_thermistors)
1030{
1031 struct pc87360_data *data = i2c_get_clientdata(client);
1032 int i, nr;
1033 const u8 init_in[14] = { 2, 2, 2, 2, 2, 2, 2, 1, 1, 3, 1, 2, 2, 2 };
1034 const u8 init_temp[3] = { 2, 2, 1 };
1035 u8 reg;
1036
1037 if (init >= 2 && data->innr) {
1038 reg = pc87360_read_value(data, LD_IN, NO_BANK,
1039 PC87365_REG_IN_CONVRATE);
Jean Delvare368609c2005-07-29 12:15:07 -07001040 dev_info(&client->dev, "VLM conversion set to "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 "1s period, 160us delay\n");
1042 pc87360_write_value(data, LD_IN, NO_BANK,
1043 PC87365_REG_IN_CONVRATE,
1044 (reg & 0xC0) | 0x11);
1045 }
1046
1047 nr = data->innr < 11 ? data->innr : 11;
1048 for (i=0; i<nr; i++) {
1049 if (init >= init_in[i]) {
1050 /* Forcibly enable voltage channel */
1051 reg = pc87360_read_value(data, LD_IN, i,
1052 PC87365_REG_IN_STATUS);
1053 if (!(reg & 0x01)) {
1054 dev_dbg(&client->dev, "Forcibly "
1055 "enabling in%d\n", i);
1056 pc87360_write_value(data, LD_IN, i,
1057 PC87365_REG_IN_STATUS,
1058 (reg & 0x68) | 0x87);
1059 }
1060 }
1061 }
1062
1063 /* We can't blindly trust the Super-I/O space configuration bit,
1064 most BIOS won't set it properly */
1065 for (i=11; i<data->innr; i++) {
1066 reg = pc87360_read_value(data, LD_IN, i,
1067 PC87365_REG_TEMP_STATUS);
1068 use_thermistors = use_thermistors || (reg & 0x01);
1069 }
1070
1071 i = use_thermistors ? 2 : 0;
1072 for (; i<data->tempnr; i++) {
1073 if (init >= init_temp[i]) {
1074 /* Forcibly enable temperature channel */
1075 reg = pc87360_read_value(data, LD_TEMP, i,
1076 PC87365_REG_TEMP_STATUS);
1077 if (!(reg & 0x01)) {
1078 dev_dbg(&client->dev, "Forcibly "
1079 "enabling temp%d\n", i+1);
1080 pc87360_write_value(data, LD_TEMP, i,
1081 PC87365_REG_TEMP_STATUS,
1082 0xCF);
1083 }
1084 }
1085 }
1086
1087 if (use_thermistors) {
1088 for (i=11; i<data->innr; i++) {
1089 if (init >= init_in[i]) {
1090 /* The pin may already be used by thermal
1091 diodes */
1092 reg = pc87360_read_value(data, LD_TEMP,
1093 (i-11)/2, PC87365_REG_TEMP_STATUS);
1094 if (reg & 0x01) {
1095 dev_dbg(&client->dev, "Skipping "
1096 "temp%d, pin already in use "
1097 "by temp%d\n", i-7, (i-11)/2);
1098 continue;
1099 }
1100
1101 /* Forcibly enable thermistor channel */
1102 reg = pc87360_read_value(data, LD_IN, i,
1103 PC87365_REG_IN_STATUS);
1104 if (!(reg & 0x01)) {
1105 dev_dbg(&client->dev, "Forcibly "
1106 "enabling temp%d\n", i-7);
1107 pc87360_write_value(data, LD_IN, i,
1108 PC87365_REG_TEMP_STATUS,
1109 (reg & 0x60) | 0x8F);
1110 }
1111 }
1112 }
1113 }
1114
1115 if (data->innr) {
1116 reg = pc87360_read_value(data, LD_IN, NO_BANK,
1117 PC87365_REG_IN_CONFIG);
1118 if (reg & 0x01) {
1119 dev_dbg(&client->dev, "Forcibly "
1120 "enabling monitoring (VLM)\n");
1121 pc87360_write_value(data, LD_IN, NO_BANK,
1122 PC87365_REG_IN_CONFIG,
1123 reg & 0xFE);
1124 }
1125 }
1126
1127 if (data->tempnr) {
1128 reg = pc87360_read_value(data, LD_TEMP, NO_BANK,
1129 PC87365_REG_TEMP_CONFIG);
1130 if (reg & 0x01) {
1131 dev_dbg(&client->dev, "Forcibly enabling "
1132 "monitoring (TMS)\n");
1133 pc87360_write_value(data, LD_TEMP, NO_BANK,
1134 PC87365_REG_TEMP_CONFIG,
1135 reg & 0xFE);
1136 }
1137
1138 if (init >= 2) {
1139 /* Chip config as documented by National Semi. */
1140 pc87360_write_value(data, LD_TEMP, 0xF, 0xA, 0x08);
1141 /* We voluntarily omit the bank here, in case the
1142 sequence itself matters. It shouldn't be a problem,
1143 since nobody else is supposed to access the
1144 device at that point. */
1145 pc87360_write_value(data, LD_TEMP, NO_BANK, 0xB, 0x04);
1146 pc87360_write_value(data, LD_TEMP, NO_BANK, 0xC, 0x35);
1147 pc87360_write_value(data, LD_TEMP, NO_BANK, 0xD, 0x05);
1148 pc87360_write_value(data, LD_TEMP, NO_BANK, 0xE, 0x05);
1149 }
1150 }
1151}
1152
1153static void pc87360_autodiv(struct i2c_client *client, int nr)
1154{
1155 struct pc87360_data *data = i2c_get_clientdata(client);
1156 u8 old_min = data->fan_min[nr];
1157
1158 /* Increase clock divider if needed and possible */
1159 if ((data->fan_status[nr] & 0x04) /* overflow flag */
1160 || (data->fan[nr] >= 224)) { /* next to overflow */
1161 if ((data->fan_status[nr] & 0x60) != 0x60) {
1162 data->fan_status[nr] += 0x20;
1163 data->fan_min[nr] >>= 1;
1164 data->fan[nr] >>= 1;
1165 dev_dbg(&client->dev, "Increasing "
1166 "clock divider to %d for fan %d\n",
1167 FAN_DIV_FROM_REG(data->fan_status[nr]), nr+1);
1168 }
1169 } else {
1170 /* Decrease clock divider if possible */
1171 while (!(data->fan_min[nr] & 0x80) /* min "nails" divider */
1172 && data->fan[nr] < 85 /* bad accuracy */
1173 && (data->fan_status[nr] & 0x60) != 0x00) {
1174 data->fan_status[nr] -= 0x20;
1175 data->fan_min[nr] <<= 1;
1176 data->fan[nr] <<= 1;
1177 dev_dbg(&client->dev, "Decreasing "
1178 "clock divider to %d for fan %d\n",
1179 FAN_DIV_FROM_REG(data->fan_status[nr]),
1180 nr+1);
1181 }
1182 }
1183
1184 /* Write new fan min if it changed */
1185 if (old_min != data->fan_min[nr]) {
1186 pc87360_write_value(data, LD_FAN, NO_BANK,
1187 PC87360_REG_FAN_MIN(nr),
1188 data->fan_min[nr]);
1189 }
1190}
1191
1192static struct pc87360_data *pc87360_update_device(struct device *dev)
1193{
1194 struct i2c_client *client = to_i2c_client(dev);
1195 struct pc87360_data *data = i2c_get_clientdata(client);
1196 u8 i;
1197
1198 down(&data->update_lock);
1199
1200 if (time_after(jiffies, data->last_updated + HZ * 2) || !data->valid) {
1201 dev_dbg(&client->dev, "Data update\n");
1202
1203 /* Fans */
1204 for (i = 0; i < data->fannr; i++) {
1205 if (FAN_CONFIG_MONITOR(data->fan_conf, i)) {
1206 data->fan_status[i] =
1207 pc87360_read_value(data, LD_FAN,
1208 NO_BANK, PC87360_REG_FAN_STATUS(i));
1209 data->fan[i] = pc87360_read_value(data, LD_FAN,
1210 NO_BANK, PC87360_REG_FAN(i));
1211 data->fan_min[i] = pc87360_read_value(data,
1212 LD_FAN, NO_BANK,
1213 PC87360_REG_FAN_MIN(i));
1214 /* Change clock divider if needed */
1215 pc87360_autodiv(client, i);
1216 /* Clear bits and write new divider */
1217 pc87360_write_value(data, LD_FAN, NO_BANK,
1218 PC87360_REG_FAN_STATUS(i),
1219 data->fan_status[i]);
1220 }
1221 if (FAN_CONFIG_CONTROL(data->fan_conf, i))
1222 data->pwm[i] = pc87360_read_value(data, LD_FAN,
1223 NO_BANK, PC87360_REG_PWM(i));
1224 }
1225
1226 /* Voltages */
1227 for (i = 0; i < data->innr; i++) {
1228 data->in_status[i] = pc87360_read_value(data, LD_IN, i,
1229 PC87365_REG_IN_STATUS);
1230 /* Clear bits */
1231 pc87360_write_value(data, LD_IN, i,
1232 PC87365_REG_IN_STATUS,
1233 data->in_status[i]);
1234 if ((data->in_status[i] & 0x81) == 0x81) {
1235 data->in[i] = pc87360_read_value(data, LD_IN,
1236 i, PC87365_REG_IN);
1237 }
1238 if (data->in_status[i] & 0x01) {
1239 data->in_min[i] = pc87360_read_value(data,
1240 LD_IN, i,
1241 PC87365_REG_IN_MIN);
1242 data->in_max[i] = pc87360_read_value(data,
1243 LD_IN, i,
1244 PC87365_REG_IN_MAX);
1245 if (i >= 11)
1246 data->in_crit[i-11] =
1247 pc87360_read_value(data, LD_IN,
1248 i, PC87365_REG_TEMP_CRIT);
1249 }
1250 }
1251 if (data->innr) {
1252 data->in_alarms = pc87360_read_value(data, LD_IN,
1253 NO_BANK, PC87365_REG_IN_ALARMS1)
1254 | ((pc87360_read_value(data, LD_IN,
1255 NO_BANK, PC87365_REG_IN_ALARMS2)
1256 & 0x07) << 8);
1257 data->vid = (data->vid_conf & 0xE0) ?
1258 pc87360_read_value(data, LD_IN,
1259 NO_BANK, PC87365_REG_VID) : 0x1F;
1260 }
1261
1262 /* Temperatures */
1263 for (i = 0; i < data->tempnr; i++) {
1264 data->temp_status[i] = pc87360_read_value(data,
1265 LD_TEMP, i,
1266 PC87365_REG_TEMP_STATUS);
1267 /* Clear bits */
1268 pc87360_write_value(data, LD_TEMP, i,
1269 PC87365_REG_TEMP_STATUS,
1270 data->temp_status[i]);
1271 if ((data->temp_status[i] & 0x81) == 0x81) {
1272 data->temp[i] = pc87360_read_value(data,
1273 LD_TEMP, i,
1274 PC87365_REG_TEMP);
1275 }
1276 if (data->temp_status[i] & 0x01) {
1277 data->temp_min[i] = pc87360_read_value(data,
1278 LD_TEMP, i,
1279 PC87365_REG_TEMP_MIN);
1280 data->temp_max[i] = pc87360_read_value(data,
1281 LD_TEMP, i,
1282 PC87365_REG_TEMP_MAX);
1283 data->temp_crit[i] = pc87360_read_value(data,
1284 LD_TEMP, i,
1285 PC87365_REG_TEMP_CRIT);
1286 }
1287 }
1288 if (data->tempnr) {
1289 data->temp_alarms = pc87360_read_value(data, LD_TEMP,
1290 NO_BANK, PC87365_REG_TEMP_ALARMS)
1291 & 0x3F;
1292 }
1293
1294 data->last_updated = jiffies;
1295 data->valid = 1;
1296 }
1297
1298 up(&data->update_lock);
1299
1300 return data;
1301}
1302
1303static int __init pc87360_init(void)
1304{
1305 int i;
1306
1307 if (pc87360_find(0x2e, &devid, extra_isa)
1308 && pc87360_find(0x4e, &devid, extra_isa)) {
1309 printk(KERN_WARNING "pc87360: PC8736x not detected, "
1310 "module not inserted.\n");
1311 return -ENODEV;
1312 }
1313
1314 /* Arbitrarily pick one of the addresses */
1315 for (i = 0; i < 3; i++) {
1316 if (extra_isa[i] != 0x0000) {
Jean Delvare2d8672c2005-07-19 23:56:35 +02001317 address = extra_isa[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 break;
1319 }
1320 }
1321
Jean Delvare2d8672c2005-07-19 23:56:35 +02001322 if (address == 0x0000) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 printk(KERN_WARNING "pc87360: No active logical device, "
1324 "module not inserted.\n");
1325 return -ENODEV;
1326 }
1327
Jean Delvarefde09502005-07-19 23:51:07 +02001328 return i2c_isa_add_driver(&pc87360_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329}
1330
1331static void __exit pc87360_exit(void)
1332{
Jean Delvarefde09502005-07-19 23:51:07 +02001333 i2c_isa_del_driver(&pc87360_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334}
1335
1336
1337MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
1338MODULE_DESCRIPTION("PC8736x hardware monitor");
1339MODULE_LICENSE("GPL");
1340
1341module_init(pc87360_init);
1342module_exit(pc87360_exit);