hwmon/pc87360 separate alarm files: add in-min/max-alarms
[linux-2.6-block.git] / drivers / hwmon / pc87360.c
CommitLineData
1da177e4
LT
1/*
2 * pc87360.c - Part of lm_sensors, Linux kernel modules
3 * for hardware monitoring
f641b588 4 * Copyright (C) 2004, 2007 Jean Delvare <khali@linux-fr.org>
1da177e4
LT
5 *
6 * Copied from smsc47m1.c:
7 * Copyright (C) 2002 Mark D. Studebaker <mdsxyz123@yahoo.com>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 *
23 * Supports the following chips:
24 *
25 * Chip #vin #fan #pwm #temp devid
26 * PC87360 - 2 2 - 0xE1
27 * PC87363 - 2 2 - 0xE8
28 * PC87364 - 3 3 - 0xE4
29 * PC87365 11 3 3 2 0xE5
30 * PC87366 11 3 3 3-4 0xE9
31 *
32 * This driver assumes that no more than one chip is present, and one of
33 * the standard Super-I/O addresses is used (0x2E/0x2F or 0x4E/0x4F).
34 */
35
1da177e4
LT
36#include <linux/module.h>
37#include <linux/init.h>
38#include <linux/slab.h>
39#include <linux/jiffies.h>
f641b588 40#include <linux/platform_device.h>
943b0830 41#include <linux/hwmon.h>
f0986bd8 42#include <linux/hwmon-sysfs.h>
303760b4 43#include <linux/hwmon-vid.h>
943b0830 44#include <linux/err.h>
9a61bf63 45#include <linux/mutex.h>
1da177e4
LT
46#include <asm/io.h>
47
1da177e4 48static u8 devid;
f641b588 49static struct platform_device *pdev;
2d8672c5 50static unsigned short extra_isa[3];
1da177e4
LT
51static u8 confreg[4];
52
1da177e4
LT
53static int init = 1;
54module_param(init, int, 0);
55MODULE_PARM_DESC(init,
56 "Chip initialization level:\n"
57 " 0: None\n"
58 "*1: Forcibly enable internal voltage and temperature channels, except in9\n"
59 " 2: Forcibly enable all voltage and temperature channels, except in9\n"
60 " 3: Forcibly enable all voltage and temperature channels, including in9");
61
67b671bc
JD
62static unsigned short force_id;
63module_param(force_id, ushort, 0);
64MODULE_PARM_DESC(force_id, "Override the detected device ID");
65
1da177e4
LT
66/*
67 * Super-I/O registers and operations
68 */
69
70#define DEV 0x07 /* Register: Logical device select */
71#define DEVID 0x20 /* Register: Device ID */
72#define ACT 0x30 /* Register: Device activation */
73#define BASE 0x60 /* Register: Base address */
74
75#define FSCM 0x09 /* Logical device: fans */
76#define VLM 0x0d /* Logical device: voltages */
77#define TMS 0x0e /* Logical device: temperatures */
78static const u8 logdev[3] = { FSCM, VLM, TMS };
79
80#define LD_FAN 0
81#define LD_IN 1
82#define LD_TEMP 2
83
84static inline void superio_outb(int sioaddr, int reg, int val)
85{
86 outb(reg, sioaddr);
87 outb(val, sioaddr+1);
88}
89
90static inline int superio_inb(int sioaddr, int reg)
91{
92 outb(reg, sioaddr);
93 return inb(sioaddr+1);
94}
95
96static inline void superio_exit(int sioaddr)
97{
98 outb(0x02, sioaddr);
99 outb(0x02, sioaddr+1);
100}
101
102/*
103 * Logical devices
104 */
105
106#define PC87360_EXTENT 0x10
107#define PC87365_REG_BANK 0x09
108#define NO_BANK 0xff
109
110/*
111 * Fan registers and conversions
112 */
113
114/* nr has to be 0 or 1 (PC87360/87363) or 2 (PC87364/87365/87366) */
115#define PC87360_REG_PRESCALE(nr) (0x00 + 2 * (nr))
116#define PC87360_REG_PWM(nr) (0x01 + 2 * (nr))
117#define PC87360_REG_FAN_MIN(nr) (0x06 + 3 * (nr))
118#define PC87360_REG_FAN(nr) (0x07 + 3 * (nr))
119#define PC87360_REG_FAN_STATUS(nr) (0x08 + 3 * (nr))
120
121#define FAN_FROM_REG(val,div) ((val) == 0 ? 0: \
122 480000 / ((val)*(div)))
123#define FAN_TO_REG(val,div) ((val) <= 100 ? 0 : \
124 480000 / ((val)*(div)))
125#define FAN_DIV_FROM_REG(val) (1 << ((val >> 5) & 0x03))
126#define FAN_STATUS_FROM_REG(val) ((val) & 0x07)
127
128#define FAN_CONFIG_MONITOR(val,nr) (((val) >> (2 + nr * 3)) & 1)
129#define FAN_CONFIG_CONTROL(val,nr) (((val) >> (3 + nr * 3)) & 1)
130#define FAN_CONFIG_INVERT(val,nr) (((val) >> (4 + nr * 3)) & 1)
131
132#define PWM_FROM_REG(val,inv) ((inv) ? 255 - (val) : (val))
133static inline u8 PWM_TO_REG(int val, int inv)
134{
135 if (inv)
136 val = 255 - val;
137 if (val < 0)
138 return 0;
139 if (val > 255)
140 return 255;
141 return val;
142}
143
144/*
145 * Voltage registers and conversions
146 */
147
148#define PC87365_REG_IN_CONVRATE 0x07
149#define PC87365_REG_IN_CONFIG 0x08
150#define PC87365_REG_IN 0x0B
151#define PC87365_REG_IN_MIN 0x0D
152#define PC87365_REG_IN_MAX 0x0C
153#define PC87365_REG_IN_STATUS 0x0A
154#define PC87365_REG_IN_ALARMS1 0x00
155#define PC87365_REG_IN_ALARMS2 0x01
156#define PC87365_REG_VID 0x06
157
158#define IN_FROM_REG(val,ref) (((val) * (ref) + 128) / 256)
159#define IN_TO_REG(val,ref) ((val) < 0 ? 0 : \
160 (val)*256 >= (ref)*255 ? 255: \
161 ((val) * 256 + (ref)/2) / (ref))
162
163/*
164 * Temperature registers and conversions
165 */
166
167#define PC87365_REG_TEMP_CONFIG 0x08
168#define PC87365_REG_TEMP 0x0B
169#define PC87365_REG_TEMP_MIN 0x0D
170#define PC87365_REG_TEMP_MAX 0x0C
171#define PC87365_REG_TEMP_CRIT 0x0E
172#define PC87365_REG_TEMP_STATUS 0x0A
173#define PC87365_REG_TEMP_ALARMS 0x00
174
175#define TEMP_FROM_REG(val) ((val) * 1000)
176#define TEMP_TO_REG(val) ((val) < -55000 ? -55 : \
177 (val) > 127000 ? 127 : \
178 (val) < 0 ? ((val) - 500) / 1000 : \
179 ((val) + 500) / 1000)
180
181/*
f641b588 182 * Device data
1da177e4
LT
183 */
184
185struct pc87360_data {
f641b588 186 const char *name;
1beeffe4 187 struct device *hwmon_dev;
9a61bf63
IM
188 struct mutex lock;
189 struct mutex update_lock;
1da177e4
LT
190 char valid; /* !=0 if following fields are valid */
191 unsigned long last_updated; /* In jiffies */
192
193 int address[3];
194
195 u8 fannr, innr, tempnr;
196
197 u8 fan[3]; /* Register value */
198 u8 fan_min[3]; /* Register value */
199 u8 fan_status[3]; /* Register value */
200 u8 pwm[3]; /* Register value */
201 u16 fan_conf; /* Configuration register values, combined */
202
203 u16 in_vref; /* 1 mV/bit */
204 u8 in[14]; /* Register value */
205 u8 in_min[14]; /* Register value */
206 u8 in_max[14]; /* Register value */
207 u8 in_crit[3]; /* Register value */
208 u8 in_status[14]; /* Register value */
209 u16 in_alarms; /* Register values, combined, masked */
210 u8 vid_conf; /* Configuration register value */
211 u8 vrm;
212 u8 vid; /* Register value */
213
214 s8 temp[3]; /* Register value */
215 s8 temp_min[3]; /* Register value */
216 s8 temp_max[3]; /* Register value */
217 s8 temp_crit[3]; /* Register value */
218 u8 temp_status[3]; /* Register value */
219 u8 temp_alarms; /* Register value, masked */
220};
221
222/*
223 * Functions declaration
224 */
225
f641b588 226static int pc87360_probe(struct platform_device *pdev);
d0546128 227static int __devexit pc87360_remove(struct platform_device *pdev);
1da177e4
LT
228
229static int pc87360_read_value(struct pc87360_data *data, u8 ldi, u8 bank,
230 u8 reg);
231static void pc87360_write_value(struct pc87360_data *data, u8 ldi, u8 bank,
232 u8 reg, u8 value);
f641b588
JD
233static void pc87360_init_device(struct platform_device *pdev,
234 int use_thermistors);
1da177e4
LT
235static struct pc87360_data *pc87360_update_device(struct device *dev);
236
237/*
f641b588 238 * Driver data
1da177e4
LT
239 */
240
f641b588 241static struct platform_driver pc87360_driver = {
cdaf7934 242 .driver = {
87218842 243 .owner = THIS_MODULE,
cdaf7934
LR
244 .name = "pc87360",
245 },
f641b588
JD
246 .probe = pc87360_probe,
247 .remove = __devexit_p(pc87360_remove),
1da177e4
LT
248};
249
250/*
251 * Sysfs stuff
252 */
253
f0986bd8
JC
254static ssize_t show_fan_input(struct device *dev, struct device_attribute *devattr, char *buf)
255{
256 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
257 struct pc87360_data *data = pc87360_update_device(dev);
694fa056
JC
258 return sprintf(buf, "%u\n", FAN_FROM_REG(data->fan[attr->index],
259 FAN_DIV_FROM_REG(data->fan_status[attr->index])));
f0986bd8
JC
260}
261static ssize_t show_fan_min(struct device *dev, struct device_attribute *devattr, char *buf)
262{
263 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
264 struct pc87360_data *data = pc87360_update_device(dev);
694fa056
JC
265 return sprintf(buf, "%u\n", FAN_FROM_REG(data->fan_min[attr->index],
266 FAN_DIV_FROM_REG(data->fan_status[attr->index])));
f0986bd8
JC
267}
268static ssize_t show_fan_div(struct device *dev, struct device_attribute *devattr, char *buf)
269{
270 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
271 struct pc87360_data *data = pc87360_update_device(dev);
272 return sprintf(buf, "%u\n",
694fa056 273 FAN_DIV_FROM_REG(data->fan_status[attr->index]));
f0986bd8
JC
274}
275static ssize_t show_fan_status(struct device *dev, struct device_attribute *devattr, char *buf)
276{
277 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
278 struct pc87360_data *data = pc87360_update_device(dev);
279 return sprintf(buf, "%u\n",
694fa056 280 FAN_STATUS_FROM_REG(data->fan_status[attr->index]));
f0986bd8
JC
281}
282static ssize_t set_fan_min(struct device *dev, struct device_attribute *devattr, const char *buf,
283 size_t count)
284{
285 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
f641b588 286 struct pc87360_data *data = dev_get_drvdata(dev);
11be27ea
JC
287 long fan_min = simple_strtol(buf, NULL, 10);
288
9a61bf63 289 mutex_lock(&data->update_lock);
11be27ea
JC
290 fan_min = FAN_TO_REG(fan_min, FAN_DIV_FROM_REG(data->fan_status[attr->index]));
291
292 /* If it wouldn't fit, change clock divisor */
293 while (fan_min > 255
294 && (data->fan_status[attr->index] & 0x60) != 0x60) {
295 fan_min >>= 1;
296 data->fan[attr->index] >>= 1;
297 data->fan_status[attr->index] += 0x20;
298 }
299 data->fan_min[attr->index] = fan_min > 255 ? 255 : fan_min;
300 pc87360_write_value(data, LD_FAN, NO_BANK, PC87360_REG_FAN_MIN(attr->index),
301 data->fan_min[attr->index]);
302
303 /* Write new divider, preserve alarm bits */
304 pc87360_write_value(data, LD_FAN, NO_BANK, PC87360_REG_FAN_STATUS(attr->index),
305 data->fan_status[attr->index] & 0xF9);
9a61bf63 306 mutex_unlock(&data->update_lock);
11be27ea
JC
307
308 return count;
f0986bd8
JC
309}
310
dedc6a78
JC
311static struct sensor_device_attribute fan_input[] = {
312 SENSOR_ATTR(fan1_input, S_IRUGO, show_fan_input, NULL, 0),
313 SENSOR_ATTR(fan2_input, S_IRUGO, show_fan_input, NULL, 1),
314 SENSOR_ATTR(fan3_input, S_IRUGO, show_fan_input, NULL, 2),
315};
316static struct sensor_device_attribute fan_status[] = {
317 SENSOR_ATTR(fan1_status, S_IRUGO, show_fan_status, NULL, 0),
318 SENSOR_ATTR(fan2_status, S_IRUGO, show_fan_status, NULL, 1),
319 SENSOR_ATTR(fan3_status, S_IRUGO, show_fan_status, NULL, 2),
320};
321static struct sensor_device_attribute fan_div[] = {
322 SENSOR_ATTR(fan1_div, S_IRUGO, show_fan_div, NULL, 0),
323 SENSOR_ATTR(fan2_div, S_IRUGO, show_fan_div, NULL, 1),
324 SENSOR_ATTR(fan3_div, S_IRUGO, show_fan_div, NULL, 2),
325};
326static struct sensor_device_attribute fan_min[] = {
327 SENSOR_ATTR(fan1_min, S_IWUSR | S_IRUGO, show_fan_min, set_fan_min, 0),
328 SENSOR_ATTR(fan2_min, S_IWUSR | S_IRUGO, show_fan_min, set_fan_min, 1),
329 SENSOR_ATTR(fan3_min, S_IWUSR | S_IRUGO, show_fan_min, set_fan_min, 2),
330};
1da177e4 331
941c5c05
JC
332#define FAN_UNIT_ATTRS(X) \
333 &fan_input[X].dev_attr.attr, \
334 &fan_status[X].dev_attr.attr, \
335 &fan_div[X].dev_attr.attr, \
336 &fan_min[X].dev_attr.attr
337
f0986bd8
JC
338static ssize_t show_pwm(struct device *dev, struct device_attribute *devattr, char *buf)
339{
340 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
341 struct pc87360_data *data = pc87360_update_device(dev);
342 return sprintf(buf, "%u\n",
694fa056 343 PWM_FROM_REG(data->pwm[attr->index],
f0986bd8 344 FAN_CONFIG_INVERT(data->fan_conf,
694fa056 345 attr->index)));
f0986bd8
JC
346}
347static ssize_t set_pwm(struct device *dev, struct device_attribute *devattr, const char *buf,
348 size_t count)
349{
350 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
f641b588 351 struct pc87360_data *data = dev_get_drvdata(dev);
f0986bd8
JC
352 long val = simple_strtol(buf, NULL, 10);
353
9a61bf63 354 mutex_lock(&data->update_lock);
694fa056
JC
355 data->pwm[attr->index] = PWM_TO_REG(val,
356 FAN_CONFIG_INVERT(data->fan_conf, attr->index));
357 pc87360_write_value(data, LD_FAN, NO_BANK, PC87360_REG_PWM(attr->index),
358 data->pwm[attr->index]);
9a61bf63 359 mutex_unlock(&data->update_lock);
f0986bd8
JC
360 return count;
361}
362
dedc6a78
JC
363static struct sensor_device_attribute pwm[] = {
364 SENSOR_ATTR(pwm1, S_IWUSR | S_IRUGO, show_pwm, set_pwm, 0),
365 SENSOR_ATTR(pwm2, S_IWUSR | S_IRUGO, show_pwm, set_pwm, 1),
366 SENSOR_ATTR(pwm3, S_IWUSR | S_IRUGO, show_pwm, set_pwm, 2),
367};
1da177e4 368
941c5c05
JC
369static struct attribute * pc8736x_fan_attr_array[] = {
370 FAN_UNIT_ATTRS(0),
371 FAN_UNIT_ATTRS(1),
372 FAN_UNIT_ATTRS(2),
373 &pwm[0].dev_attr.attr,
374 &pwm[1].dev_attr.attr,
375 &pwm[2].dev_attr.attr,
376 NULL
377};
378static const struct attribute_group pc8736x_fan_group = {
379 .attrs = pc8736x_fan_attr_array,
380};
381
f0986bd8
JC
382static ssize_t show_in_input(struct device *dev, struct device_attribute *devattr, char *buf)
383{
384 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
385 struct pc87360_data *data = pc87360_update_device(dev);
386 return sprintf(buf, "%u\n", IN_FROM_REG(data->in[attr->index],
387 data->in_vref));
388}
389static ssize_t show_in_min(struct device *dev, struct device_attribute *devattr, char *buf)
390{
391 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
392 struct pc87360_data *data = pc87360_update_device(dev);
393 return sprintf(buf, "%u\n", IN_FROM_REG(data->in_min[attr->index],
394 data->in_vref));
395}
396static ssize_t show_in_max(struct device *dev, struct device_attribute *devattr, char *buf)
397{
398 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
399 struct pc87360_data *data = pc87360_update_device(dev);
400 return sprintf(buf, "%u\n", IN_FROM_REG(data->in_max[attr->index],
401 data->in_vref));
402}
403static ssize_t show_in_status(struct device *dev, struct device_attribute *devattr, char *buf)
404{
405 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
406 struct pc87360_data *data = pc87360_update_device(dev);
407 return sprintf(buf, "%u\n", data->in_status[attr->index]);
408}
409static ssize_t set_in_min(struct device *dev, struct device_attribute *devattr, const char *buf,
410 size_t count)
411{
412 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
f641b588 413 struct pc87360_data *data = dev_get_drvdata(dev);
f0986bd8
JC
414 long val = simple_strtol(buf, NULL, 10);
415
9a61bf63 416 mutex_lock(&data->update_lock);
f0986bd8
JC
417 data->in_min[attr->index] = IN_TO_REG(val, data->in_vref);
418 pc87360_write_value(data, LD_IN, attr->index, PC87365_REG_IN_MIN,
419 data->in_min[attr->index]);
9a61bf63 420 mutex_unlock(&data->update_lock);
f0986bd8
JC
421 return count;
422}
423static ssize_t set_in_max(struct device *dev, struct device_attribute *devattr, const char *buf,
424 size_t count)
425{
426 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
f641b588 427 struct pc87360_data *data = dev_get_drvdata(dev);
f0986bd8
JC
428 long val = simple_strtol(buf, NULL, 10);
429
9a61bf63 430 mutex_lock(&data->update_lock);
f0986bd8
JC
431 data->in_max[attr->index] = IN_TO_REG(val,
432 data->in_vref);
433 pc87360_write_value(data, LD_IN, attr->index, PC87365_REG_IN_MAX,
434 data->in_max[attr->index]);
9a61bf63 435 mutex_unlock(&data->update_lock);
f0986bd8
JC
436 return count;
437}
438
dedc6a78
JC
439static struct sensor_device_attribute in_input[] = {
440 SENSOR_ATTR(in0_input, S_IRUGO, show_in_input, NULL, 0),
441 SENSOR_ATTR(in1_input, S_IRUGO, show_in_input, NULL, 1),
442 SENSOR_ATTR(in2_input, S_IRUGO, show_in_input, NULL, 2),
443 SENSOR_ATTR(in3_input, S_IRUGO, show_in_input, NULL, 3),
444 SENSOR_ATTR(in4_input, S_IRUGO, show_in_input, NULL, 4),
445 SENSOR_ATTR(in5_input, S_IRUGO, show_in_input, NULL, 5),
446 SENSOR_ATTR(in6_input, S_IRUGO, show_in_input, NULL, 6),
447 SENSOR_ATTR(in7_input, S_IRUGO, show_in_input, NULL, 7),
448 SENSOR_ATTR(in8_input, S_IRUGO, show_in_input, NULL, 8),
449 SENSOR_ATTR(in9_input, S_IRUGO, show_in_input, NULL, 9),
450 SENSOR_ATTR(in10_input, S_IRUGO, show_in_input, NULL, 10),
451};
452static struct sensor_device_attribute in_status[] = {
453 SENSOR_ATTR(in0_status, S_IRUGO, show_in_status, NULL, 0),
454 SENSOR_ATTR(in1_status, S_IRUGO, show_in_status, NULL, 1),
455 SENSOR_ATTR(in2_status, S_IRUGO, show_in_status, NULL, 2),
456 SENSOR_ATTR(in3_status, S_IRUGO, show_in_status, NULL, 3),
457 SENSOR_ATTR(in4_status, S_IRUGO, show_in_status, NULL, 4),
458 SENSOR_ATTR(in5_status, S_IRUGO, show_in_status, NULL, 5),
459 SENSOR_ATTR(in6_status, S_IRUGO, show_in_status, NULL, 6),
460 SENSOR_ATTR(in7_status, S_IRUGO, show_in_status, NULL, 7),
461 SENSOR_ATTR(in8_status, S_IRUGO, show_in_status, NULL, 8),
462 SENSOR_ATTR(in9_status, S_IRUGO, show_in_status, NULL, 9),
463 SENSOR_ATTR(in10_status, S_IRUGO, show_in_status, NULL, 10),
464};
465static struct sensor_device_attribute in_min[] = {
466 SENSOR_ATTR(in0_min, S_IWUSR | S_IRUGO, show_in_min, set_in_min, 0),
467 SENSOR_ATTR(in1_min, S_IWUSR | S_IRUGO, show_in_min, set_in_min, 1),
468 SENSOR_ATTR(in2_min, S_IWUSR | S_IRUGO, show_in_min, set_in_min, 2),
469 SENSOR_ATTR(in3_min, S_IWUSR | S_IRUGO, show_in_min, set_in_min, 3),
470 SENSOR_ATTR(in4_min, S_IWUSR | S_IRUGO, show_in_min, set_in_min, 4),
471 SENSOR_ATTR(in5_min, S_IWUSR | S_IRUGO, show_in_min, set_in_min, 5),
472 SENSOR_ATTR(in6_min, S_IWUSR | S_IRUGO, show_in_min, set_in_min, 6),
473 SENSOR_ATTR(in7_min, S_IWUSR | S_IRUGO, show_in_min, set_in_min, 7),
474 SENSOR_ATTR(in8_min, S_IWUSR | S_IRUGO, show_in_min, set_in_min, 8),
475 SENSOR_ATTR(in9_min, S_IWUSR | S_IRUGO, show_in_min, set_in_min, 9),
476 SENSOR_ATTR(in10_min, S_IWUSR | S_IRUGO, show_in_min, set_in_min, 10),
477};
478static struct sensor_device_attribute in_max[] = {
479 SENSOR_ATTR(in0_max, S_IWUSR | S_IRUGO, show_in_max, set_in_max, 0),
480 SENSOR_ATTR(in1_max, S_IWUSR | S_IRUGO, show_in_max, set_in_max, 1),
481 SENSOR_ATTR(in2_max, S_IWUSR | S_IRUGO, show_in_max, set_in_max, 2),
482 SENSOR_ATTR(in3_max, S_IWUSR | S_IRUGO, show_in_max, set_in_max, 3),
483 SENSOR_ATTR(in4_max, S_IWUSR | S_IRUGO, show_in_max, set_in_max, 4),
484 SENSOR_ATTR(in5_max, S_IWUSR | S_IRUGO, show_in_max, set_in_max, 5),
485 SENSOR_ATTR(in6_max, S_IWUSR | S_IRUGO, show_in_max, set_in_max, 6),
486 SENSOR_ATTR(in7_max, S_IWUSR | S_IRUGO, show_in_max, set_in_max, 7),
487 SENSOR_ATTR(in8_max, S_IWUSR | S_IRUGO, show_in_max, set_in_max, 8),
488 SENSOR_ATTR(in9_max, S_IWUSR | S_IRUGO, show_in_max, set_in_max, 9),
489 SENSOR_ATTR(in10_max, S_IWUSR | S_IRUGO, show_in_max, set_in_max, 10),
490};
1da177e4 491
28f74e71
JC
492/* (temp & vin) channel status register alarm bits (pdf sec.11.5.12) */
493#define CHAN_ALM_MIN 0x02 /* min limit crossed */
494#define CHAN_ALM_MAX 0x04 /* max limit exceeded */
495#define TEMP_ALM_CRIT 0x08 /* temp crit exceeded (temp only) */
496
492e9657
JC
497/* show_in_min/max_alarm() reads data from the per-channel status
498 register (sec 11.5.12), not the vin event status registers (sec
499 11.5.2) that (legacy) show_in_alarm() resds (via data->in_alarms) */
500
501static ssize_t show_in_min_alarm(struct device *dev,
502 struct device_attribute *devattr, char *buf)
503{
504 struct pc87360_data *data = pc87360_update_device(dev);
505 unsigned nr = to_sensor_dev_attr(devattr)->index;
506
507 return sprintf(buf, "%u\n", !!(data->in_status[nr] & CHAN_ALM_MIN));
508}
509static ssize_t show_in_max_alarm(struct device *dev,
510 struct device_attribute *devattr, char *buf)
511{
512 struct pc87360_data *data = pc87360_update_device(dev);
513 unsigned nr = to_sensor_dev_attr(devattr)->index;
514
515 return sprintf(buf, "%u\n", !!(data->in_status[nr] & CHAN_ALM_MAX));
516}
517
518static struct sensor_device_attribute in_min_alarm[] = {
519 SENSOR_ATTR(in0_min_alarm, S_IRUGO, show_in_min_alarm, NULL, 0),
520 SENSOR_ATTR(in1_min_alarm, S_IRUGO, show_in_min_alarm, NULL, 1),
521 SENSOR_ATTR(in2_min_alarm, S_IRUGO, show_in_min_alarm, NULL, 2),
522 SENSOR_ATTR(in3_min_alarm, S_IRUGO, show_in_min_alarm, NULL, 3),
523 SENSOR_ATTR(in4_min_alarm, S_IRUGO, show_in_min_alarm, NULL, 4),
524 SENSOR_ATTR(in5_min_alarm, S_IRUGO, show_in_min_alarm, NULL, 5),
525 SENSOR_ATTR(in6_min_alarm, S_IRUGO, show_in_min_alarm, NULL, 6),
526 SENSOR_ATTR(in7_min_alarm, S_IRUGO, show_in_min_alarm, NULL, 7),
527 SENSOR_ATTR(in8_min_alarm, S_IRUGO, show_in_min_alarm, NULL, 8),
528 SENSOR_ATTR(in9_min_alarm, S_IRUGO, show_in_min_alarm, NULL, 9),
529 SENSOR_ATTR(in10_min_alarm, S_IRUGO, show_in_min_alarm, NULL, 10),
530};
531static struct sensor_device_attribute in_max_alarm[] = {
532 SENSOR_ATTR(in0_max_alarm, S_IRUGO, show_in_max_alarm, NULL, 0),
533 SENSOR_ATTR(in1_max_alarm, S_IRUGO, show_in_max_alarm, NULL, 1),
534 SENSOR_ATTR(in2_max_alarm, S_IRUGO, show_in_max_alarm, NULL, 2),
535 SENSOR_ATTR(in3_max_alarm, S_IRUGO, show_in_max_alarm, NULL, 3),
536 SENSOR_ATTR(in4_max_alarm, S_IRUGO, show_in_max_alarm, NULL, 4),
537 SENSOR_ATTR(in5_max_alarm, S_IRUGO, show_in_max_alarm, NULL, 5),
538 SENSOR_ATTR(in6_max_alarm, S_IRUGO, show_in_max_alarm, NULL, 6),
539 SENSOR_ATTR(in7_max_alarm, S_IRUGO, show_in_max_alarm, NULL, 7),
540 SENSOR_ATTR(in8_max_alarm, S_IRUGO, show_in_max_alarm, NULL, 8),
541 SENSOR_ATTR(in9_max_alarm, S_IRUGO, show_in_max_alarm, NULL, 9),
542 SENSOR_ATTR(in10_max_alarm, S_IRUGO, show_in_max_alarm, NULL, 10),
543};
544
941c5c05
JC
545#define VIN_UNIT_ATTRS(X) \
546 &in_input[X].dev_attr.attr, \
547 &in_status[X].dev_attr.attr, \
548 &in_min[X].dev_attr.attr, \
492e9657
JC
549 &in_max[X].dev_attr.attr, \
550 &in_min_alarm[X].dev_attr.attr, \
551 &in_max_alarm[X].dev_attr.attr
941c5c05 552
44646c19
JC
553static ssize_t show_vid(struct device *dev, struct device_attribute *attr, char *buf)
554{
555 struct pc87360_data *data = pc87360_update_device(dev);
556 return sprintf(buf, "%u\n", vid_from_reg(data->vid, data->vrm));
557}
558static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL);
559
560static ssize_t show_vrm(struct device *dev, struct device_attribute *attr, char *buf)
561{
90d6619a 562 struct pc87360_data *data = dev_get_drvdata(dev);
44646c19
JC
563 return sprintf(buf, "%u\n", data->vrm);
564}
565static ssize_t set_vrm(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
566{
f641b588 567 struct pc87360_data *data = dev_get_drvdata(dev);
44646c19
JC
568 data->vrm = simple_strtoul(buf, NULL, 10);
569 return count;
570}
571static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm, set_vrm);
572
573static ssize_t show_in_alarms(struct device *dev, struct device_attribute *attr, char *buf)
574{
575 struct pc87360_data *data = pc87360_update_device(dev);
576 return sprintf(buf, "%u\n", data->in_alarms);
577}
578static DEVICE_ATTR(alarms_in, S_IRUGO, show_in_alarms, NULL);
579
941c5c05
JC
580static struct attribute *pc8736x_vin_attr_array[] = {
581 VIN_UNIT_ATTRS(0),
582 VIN_UNIT_ATTRS(1),
583 VIN_UNIT_ATTRS(2),
584 VIN_UNIT_ATTRS(3),
585 VIN_UNIT_ATTRS(4),
586 VIN_UNIT_ATTRS(5),
587 VIN_UNIT_ATTRS(6),
588 VIN_UNIT_ATTRS(7),
589 VIN_UNIT_ATTRS(8),
590 VIN_UNIT_ATTRS(9),
591 VIN_UNIT_ATTRS(10),
592 &dev_attr_cpu0_vid.attr,
593 &dev_attr_vrm.attr,
594 &dev_attr_alarms_in.attr,
595 NULL
596};
597static const struct attribute_group pc8736x_vin_group = {
598 .attrs = pc8736x_vin_attr_array,
599};
600
f0986bd8
JC
601static ssize_t show_therm_input(struct device *dev, struct device_attribute *devattr, char *buf)
602{
603 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
604 struct pc87360_data *data = pc87360_update_device(dev);
694fa056 605 return sprintf(buf, "%u\n", IN_FROM_REG(data->in[attr->index],
f0986bd8
JC
606 data->in_vref));
607}
608static ssize_t show_therm_min(struct device *dev, struct device_attribute *devattr, char *buf)
609{
610 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
611 struct pc87360_data *data = pc87360_update_device(dev);
694fa056 612 return sprintf(buf, "%u\n", IN_FROM_REG(data->in_min[attr->index],
f0986bd8
JC
613 data->in_vref));
614}
615static ssize_t show_therm_max(struct device *dev, struct device_attribute *devattr, char *buf)
616{
617 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
618 struct pc87360_data *data = pc87360_update_device(dev);
694fa056 619 return sprintf(buf, "%u\n", IN_FROM_REG(data->in_max[attr->index],
f0986bd8
JC
620 data->in_vref));
621}
622static ssize_t show_therm_crit(struct device *dev, struct device_attribute *devattr, char *buf)
623{
624 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
625 struct pc87360_data *data = pc87360_update_device(dev);
694fa056 626 return sprintf(buf, "%u\n", IN_FROM_REG(data->in_crit[attr->index-11],
f0986bd8
JC
627 data->in_vref));
628}
629static ssize_t show_therm_status(struct device *dev, struct device_attribute *devattr, char *buf)
630{
631 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
632 struct pc87360_data *data = pc87360_update_device(dev);
694fa056 633 return sprintf(buf, "%u\n", data->in_status[attr->index]);
f0986bd8
JC
634}
635static ssize_t set_therm_min(struct device *dev, struct device_attribute *devattr, const char *buf,
636 size_t count)
637{
638 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
f641b588 639 struct pc87360_data *data = dev_get_drvdata(dev);
f0986bd8
JC
640 long val = simple_strtol(buf, NULL, 10);
641
9a61bf63 642 mutex_lock(&data->update_lock);
694fa056
JC
643 data->in_min[attr->index] = IN_TO_REG(val, data->in_vref);
644 pc87360_write_value(data, LD_IN, attr->index, PC87365_REG_TEMP_MIN,
645 data->in_min[attr->index]);
9a61bf63 646 mutex_unlock(&data->update_lock);
f0986bd8
JC
647 return count;
648}
649static ssize_t set_therm_max(struct device *dev, struct device_attribute *devattr, const char *buf,
650 size_t count)
651{
652 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
f641b588 653 struct pc87360_data *data = dev_get_drvdata(dev);
f0986bd8
JC
654 long val = simple_strtol(buf, NULL, 10);
655
9a61bf63 656 mutex_lock(&data->update_lock);
694fa056
JC
657 data->in_max[attr->index] = IN_TO_REG(val, data->in_vref);
658 pc87360_write_value(data, LD_IN, attr->index, PC87365_REG_TEMP_MAX,
659 data->in_max[attr->index]);
9a61bf63 660 mutex_unlock(&data->update_lock);
f0986bd8
JC
661 return count;
662}
663static ssize_t set_therm_crit(struct device *dev, struct device_attribute *devattr, const char *buf,
664 size_t count)
665{
666 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
f641b588 667 struct pc87360_data *data = dev_get_drvdata(dev);
f0986bd8
JC
668 long val = simple_strtol(buf, NULL, 10);
669
9a61bf63 670 mutex_lock(&data->update_lock);
694fa056
JC
671 data->in_crit[attr->index-11] = IN_TO_REG(val, data->in_vref);
672 pc87360_write_value(data, LD_IN, attr->index, PC87365_REG_TEMP_CRIT,
673 data->in_crit[attr->index-11]);
9a61bf63 674 mutex_unlock(&data->update_lock);
f0986bd8
JC
675 return count;
676}
677
dedc6a78
JC
678/* the +11 term below reflects the fact that VLM units 11,12,13 are
679 used in the chip to measure voltage across the thermistors
680*/
681static struct sensor_device_attribute therm_input[] = {
682 SENSOR_ATTR(temp4_input, S_IRUGO, show_therm_input, NULL, 0+11),
683 SENSOR_ATTR(temp5_input, S_IRUGO, show_therm_input, NULL, 1+11),
684 SENSOR_ATTR(temp6_input, S_IRUGO, show_therm_input, NULL, 2+11),
685};
686static struct sensor_device_attribute therm_status[] = {
687 SENSOR_ATTR(temp4_status, S_IRUGO, show_therm_status, NULL, 0+11),
688 SENSOR_ATTR(temp5_status, S_IRUGO, show_therm_status, NULL, 1+11),
689 SENSOR_ATTR(temp6_status, S_IRUGO, show_therm_status, NULL, 2+11),
690};
691static struct sensor_device_attribute therm_min[] = {
692 SENSOR_ATTR(temp4_min, S_IRUGO | S_IWUSR,
693 show_therm_min, set_therm_min, 0+11),
694 SENSOR_ATTR(temp5_min, S_IRUGO | S_IWUSR,
695 show_therm_min, set_therm_min, 1+11),
696 SENSOR_ATTR(temp6_min, S_IRUGO | S_IWUSR,
697 show_therm_min, set_therm_min, 2+11),
698};
699static struct sensor_device_attribute therm_max[] = {
700 SENSOR_ATTR(temp4_max, S_IRUGO | S_IWUSR,
701 show_therm_max, set_therm_max, 0+11),
702 SENSOR_ATTR(temp5_max, S_IRUGO | S_IWUSR,
703 show_therm_max, set_therm_max, 1+11),
704 SENSOR_ATTR(temp6_max, S_IRUGO | S_IWUSR,
705 show_therm_max, set_therm_max, 2+11),
706};
707static struct sensor_device_attribute therm_crit[] = {
708 SENSOR_ATTR(temp4_crit, S_IRUGO | S_IWUSR,
709 show_therm_crit, set_therm_crit, 0+11),
710 SENSOR_ATTR(temp5_crit, S_IRUGO | S_IWUSR,
711 show_therm_crit, set_therm_crit, 1+11),
712 SENSOR_ATTR(temp6_crit, S_IRUGO | S_IWUSR,
713 show_therm_crit, set_therm_crit, 2+11),
714};
1da177e4 715
941c5c05
JC
716#define THERM_UNIT_ATTRS(X) \
717 &therm_input[X].dev_attr.attr, \
718 &therm_status[X].dev_attr.attr, \
719 &therm_min[X].dev_attr.attr, \
720 &therm_max[X].dev_attr.attr, \
721 &therm_crit[X].dev_attr.attr
722
723static struct attribute * pc8736x_therm_attr_array[] = {
724 THERM_UNIT_ATTRS(0),
725 THERM_UNIT_ATTRS(1),
726 THERM_UNIT_ATTRS(2),
727 NULL
728};
729static const struct attribute_group pc8736x_therm_group = {
730 .attrs = pc8736x_therm_attr_array,
731};
732
f0986bd8
JC
733static ssize_t show_temp_input(struct device *dev, struct device_attribute *devattr, char *buf)
734{
735 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
736 struct pc87360_data *data = pc87360_update_device(dev);
694fa056 737 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[attr->index]));
f0986bd8
JC
738}
739static ssize_t show_temp_min(struct device *dev, struct device_attribute *devattr, char *buf)
740{
741 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
742 struct pc87360_data *data = pc87360_update_device(dev);
694fa056 743 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_min[attr->index]));
f0986bd8
JC
744}
745static ssize_t show_temp_max(struct device *dev, struct device_attribute *devattr, char *buf)
746{
747 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
748 struct pc87360_data *data = pc87360_update_device(dev);
694fa056 749 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max[attr->index]));
f0986bd8
JC
750}
751static ssize_t show_temp_crit(struct device *dev, struct device_attribute *devattr, char *buf)
752{
753 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
754 struct pc87360_data *data = pc87360_update_device(dev);
694fa056 755 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_crit[attr->index]));
f0986bd8
JC
756}
757static ssize_t show_temp_status(struct device *dev, struct device_attribute *devattr, char *buf)
758{
759 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
760 struct pc87360_data *data = pc87360_update_device(dev);
694fa056 761 return sprintf(buf, "%d\n", data->temp_status[attr->index]);
f0986bd8
JC
762}
763static ssize_t set_temp_min(struct device *dev, struct device_attribute *devattr, const char *buf,
764 size_t count)
765{
766 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
f641b588 767 struct pc87360_data *data = dev_get_drvdata(dev);
f0986bd8
JC
768 long val = simple_strtol(buf, NULL, 10);
769
9a61bf63 770 mutex_lock(&data->update_lock);
694fa056
JC
771 data->temp_min[attr->index] = TEMP_TO_REG(val);
772 pc87360_write_value(data, LD_TEMP, attr->index, PC87365_REG_TEMP_MIN,
773 data->temp_min[attr->index]);
9a61bf63 774 mutex_unlock(&data->update_lock);
f0986bd8
JC
775 return count;
776}
777static ssize_t set_temp_max(struct device *dev, struct device_attribute *devattr, const char *buf,
778 size_t count)
779{
780 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
f641b588 781 struct pc87360_data *data = dev_get_drvdata(dev);
f0986bd8
JC
782 long val = simple_strtol(buf, NULL, 10);
783
9a61bf63 784 mutex_lock(&data->update_lock);
694fa056
JC
785 data->temp_max[attr->index] = TEMP_TO_REG(val);
786 pc87360_write_value(data, LD_TEMP, attr->index, PC87365_REG_TEMP_MAX,
787 data->temp_max[attr->index]);
9a61bf63 788 mutex_unlock(&data->update_lock);
f0986bd8
JC
789 return count;
790}
791static ssize_t set_temp_crit(struct device *dev, struct device_attribute *devattr, const char *buf,
792 size_t count)
793{
794 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
f641b588 795 struct pc87360_data *data = dev_get_drvdata(dev);
f0986bd8
JC
796 long val = simple_strtol(buf, NULL, 10);
797
9a61bf63 798 mutex_lock(&data->update_lock);
694fa056
JC
799 data->temp_crit[attr->index] = TEMP_TO_REG(val);
800 pc87360_write_value(data, LD_TEMP, attr->index, PC87365_REG_TEMP_CRIT,
801 data->temp_crit[attr->index]);
9a61bf63 802 mutex_unlock(&data->update_lock);
f0986bd8
JC
803 return count;
804}
805
dedc6a78
JC
806static struct sensor_device_attribute temp_input[] = {
807 SENSOR_ATTR(temp1_input, S_IRUGO, show_temp_input, NULL, 0),
808 SENSOR_ATTR(temp2_input, S_IRUGO, show_temp_input, NULL, 1),
809 SENSOR_ATTR(temp3_input, S_IRUGO, show_temp_input, NULL, 2),
810};
811static struct sensor_device_attribute temp_status[] = {
812 SENSOR_ATTR(temp1_status, S_IRUGO, show_temp_status, NULL, 0),
813 SENSOR_ATTR(temp2_status, S_IRUGO, show_temp_status, NULL, 1),
814 SENSOR_ATTR(temp3_status, S_IRUGO, show_temp_status, NULL, 2),
815};
816static struct sensor_device_attribute temp_min[] = {
817 SENSOR_ATTR(temp1_min, S_IRUGO | S_IWUSR,
818 show_temp_min, set_temp_min, 0),
819 SENSOR_ATTR(temp2_min, S_IRUGO | S_IWUSR,
820 show_temp_min, set_temp_min, 1),
821 SENSOR_ATTR(temp3_min, S_IRUGO | S_IWUSR,
822 show_temp_min, set_temp_min, 2),
823};
824static struct sensor_device_attribute temp_max[] = {
825 SENSOR_ATTR(temp1_max, S_IRUGO | S_IWUSR,
826 show_temp_max, set_temp_max, 0),
827 SENSOR_ATTR(temp2_max, S_IRUGO | S_IWUSR,
828 show_temp_max, set_temp_max, 1),
829 SENSOR_ATTR(temp3_max, S_IRUGO | S_IWUSR,
830 show_temp_max, set_temp_max, 2),
831};
832static struct sensor_device_attribute temp_crit[] = {
833 SENSOR_ATTR(temp1_crit, S_IRUGO | S_IWUSR,
834 show_temp_crit, set_temp_crit, 0),
835 SENSOR_ATTR(temp2_crit, S_IRUGO | S_IWUSR,
836 show_temp_crit, set_temp_crit, 1),
837 SENSOR_ATTR(temp3_crit, S_IRUGO | S_IWUSR,
838 show_temp_crit, set_temp_crit, 2),
839};
1da177e4 840
a5099cfc 841static ssize_t show_temp_alarms(struct device *dev, struct device_attribute *attr, char *buf)
1da177e4
LT
842{
843 struct pc87360_data *data = pc87360_update_device(dev);
844 return sprintf(buf, "%u\n", data->temp_alarms);
845}
846static DEVICE_ATTR(alarms_temp, S_IRUGO, show_temp_alarms, NULL);
847
941c5c05
JC
848#define TEMP_UNIT_ATTRS(X) \
849 &temp_input[X].dev_attr.attr, \
850 &temp_status[X].dev_attr.attr, \
851 &temp_min[X].dev_attr.attr, \
852 &temp_max[X].dev_attr.attr, \
853 &temp_crit[X].dev_attr.attr
854
855static struct attribute * pc8736x_temp_attr_array[] = {
856 TEMP_UNIT_ATTRS(0),
857 TEMP_UNIT_ATTRS(1),
858 TEMP_UNIT_ATTRS(2),
859 /* include the few miscellaneous atts here */
860 &dev_attr_alarms_temp.attr,
861 NULL
862};
863static const struct attribute_group pc8736x_temp_group = {
864 .attrs = pc8736x_temp_attr_array,
865};
866
f641b588
JD
867static ssize_t show_name(struct device *dev, struct device_attribute
868 *devattr, char *buf)
869{
870 struct pc87360_data *data = dev_get_drvdata(dev);
871 return sprintf(buf, "%s\n", data->name);
872}
873static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
874
1da177e4
LT
875/*
876 * Device detection, registration and update
877 */
878
e6cfb3ad 879static int __init pc87360_find(int sioaddr, u8 *devid, unsigned short *addresses)
1da177e4
LT
880{
881 u16 val;
882 int i;
883 int nrdev; /* logical device count */
884
885 /* No superio_enter */
886
887 /* Identify device */
67b671bc 888 val = force_id ? force_id : superio_inb(sioaddr, DEVID);
1da177e4
LT
889 switch (val) {
890 case 0xE1: /* PC87360 */
891 case 0xE8: /* PC87363 */
892 case 0xE4: /* PC87364 */
893 nrdev = 1;
894 break;
895 case 0xE5: /* PC87365 */
896 case 0xE9: /* PC87366 */
897 nrdev = 3;
898 break;
899 default:
900 superio_exit(sioaddr);
901 return -ENODEV;
902 }
903 /* Remember the device id */
904 *devid = val;
905
906 for (i = 0; i < nrdev; i++) {
907 /* select logical device */
908 superio_outb(sioaddr, DEV, logdev[i]);
909
910 val = superio_inb(sioaddr, ACT);
911 if (!(val & 0x01)) {
912 printk(KERN_INFO "pc87360: Device 0x%02x not "
913 "activated\n", logdev[i]);
914 continue;
915 }
916
917 val = (superio_inb(sioaddr, BASE) << 8)
918 | superio_inb(sioaddr, BASE + 1);
919 if (!val) {
920 printk(KERN_INFO "pc87360: Base address not set for "
921 "device 0x%02x\n", logdev[i]);
922 continue;
923 }
924
2d8672c5 925 addresses[i] = val;
1da177e4
LT
926
927 if (i==0) { /* Fans */
928 confreg[0] = superio_inb(sioaddr, 0xF0);
929 confreg[1] = superio_inb(sioaddr, 0xF1);
930
931#ifdef DEBUG
932 printk(KERN_DEBUG "pc87360: Fan 1: mon=%d "
933 "ctrl=%d inv=%d\n", (confreg[0]>>2)&1,
934 (confreg[0]>>3)&1, (confreg[0]>>4)&1);
935 printk(KERN_DEBUG "pc87360: Fan 2: mon=%d "
936 "ctrl=%d inv=%d\n", (confreg[0]>>5)&1,
937 (confreg[0]>>6)&1, (confreg[0]>>7)&1);
938 printk(KERN_DEBUG "pc87360: Fan 3: mon=%d "
939 "ctrl=%d inv=%d\n", confreg[1]&1,
940 (confreg[1]>>1)&1, (confreg[1]>>2)&1);
941#endif
942 } else if (i==1) { /* Voltages */
943 /* Are we using thermistors? */
944 if (*devid == 0xE9) { /* PC87366 */
945 /* These registers are not logical-device
946 specific, just that we won't need them if
947 we don't use the VLM device */
948 confreg[2] = superio_inb(sioaddr, 0x2B);
949 confreg[3] = superio_inb(sioaddr, 0x25);
950
951 if (confreg[2] & 0x40) {
952 printk(KERN_INFO "pc87360: Using "
953 "thermistors for temperature "
954 "monitoring\n");
955 }
956 if (confreg[3] & 0xE0) {
957 printk(KERN_INFO "pc87360: VID "
958 "inputs routed (mode %u)\n",
959 confreg[3] >> 5);
960 }
961 }
962 }
963 }
964
965 superio_exit(sioaddr);
966 return 0;
967}
968
f641b588 969static int __devinit pc87360_probe(struct platform_device *pdev)
1da177e4
LT
970{
971 int i;
1da177e4
LT
972 struct pc87360_data *data;
973 int err = 0;
974 const char *name = "pc87360";
975 int use_thermistors = 0;
f641b588 976 struct device *dev = &pdev->dev;
1da177e4 977
ba9c2e8d 978 if (!(data = kzalloc(sizeof(struct pc87360_data), GFP_KERNEL)))
1da177e4 979 return -ENOMEM;
1da177e4 980
1da177e4
LT
981 data->fannr = 2;
982 data->innr = 0;
983 data->tempnr = 0;
984
985 switch (devid) {
986 case 0xe8:
987 name = "pc87363";
988 break;
989 case 0xe4:
990 name = "pc87364";
991 data->fannr = 3;
992 break;
993 case 0xe5:
994 name = "pc87365";
995 data->fannr = extra_isa[0] ? 3 : 0;
996 data->innr = extra_isa[1] ? 11 : 0;
997 data->tempnr = extra_isa[2] ? 2 : 0;
998 break;
999 case 0xe9:
1000 name = "pc87366";
1001 data->fannr = extra_isa[0] ? 3 : 0;
1002 data->innr = extra_isa[1] ? 14 : 0;
1003 data->tempnr = extra_isa[2] ? 3 : 0;
1004 break;
1005 }
1006
f641b588 1007 data->name = name;
1da177e4 1008 data->valid = 0;
f641b588 1009 mutex_init(&data->lock);
9a61bf63 1010 mutex_init(&data->update_lock);
f641b588 1011 platform_set_drvdata(pdev, data);
1da177e4
LT
1012
1013 for (i = 0; i < 3; i++) {
1014 if (((data->address[i] = extra_isa[i]))
1015 && !request_region(extra_isa[i], PC87360_EXTENT,
cdaf7934 1016 pc87360_driver.driver.name)) {
f641b588 1017 dev_err(dev, "Region 0x%x-0x%x already "
1da177e4
LT
1018 "in use!\n", extra_isa[i],
1019 extra_isa[i]+PC87360_EXTENT-1);
1020 for (i--; i >= 0; i--)
1021 release_region(extra_isa[i], PC87360_EXTENT);
1022 err = -EBUSY;
1023 goto ERROR1;
1024 }
1025 }
1026
1027 /* Retrieve the fans configuration from Super-I/O space */
1028 if (data->fannr)
1029 data->fan_conf = confreg[0] | (confreg[1] << 8);
1030
1da177e4
LT
1031 /* Use the correct reference voltage
1032 Unless both the VLM and the TMS logical devices agree to
1033 use an external Vref, the internal one is used. */
1034 if (data->innr) {
1035 i = pc87360_read_value(data, LD_IN, NO_BANK,
1036 PC87365_REG_IN_CONFIG);
1037 if (data->tempnr) {
1038 i &= pc87360_read_value(data, LD_TEMP, NO_BANK,
1039 PC87365_REG_TEMP_CONFIG);
1040 }
1041 data->in_vref = (i&0x02) ? 3025 : 2966;
f641b588 1042 dev_dbg(dev, "Using %s reference voltage\n",
1da177e4
LT
1043 (i&0x02) ? "external" : "internal");
1044
1045 data->vid_conf = confreg[3];
3d7f9a86 1046 data->vrm = vid_which_vrm();
1da177e4
LT
1047 }
1048
1049 /* Fan clock dividers may be needed before any data is read */
1050 for (i = 0; i < data->fannr; i++) {
1051 if (FAN_CONFIG_MONITOR(data->fan_conf, i))
1052 data->fan_status[i] = pc87360_read_value(data,
1053 LD_FAN, NO_BANK,
1054 PC87360_REG_FAN_STATUS(i));
1055 }
1056
1057 if (init > 0) {
1058 if (devid == 0xe9 && data->address[1]) /* PC87366 */
1059 use_thermistors = confreg[2] & 0x40;
1060
f641b588 1061 pc87360_init_device(pdev, use_thermistors);
1da177e4
LT
1062 }
1063
f3722d5b
JC
1064 /* Register all-or-nothing sysfs groups */
1065
1066 if (data->innr &&
f641b588 1067 (err = sysfs_create_group(&dev->kobj,
f3722d5b 1068 &pc8736x_vin_group)))
943b0830 1069 goto ERROR3;
943b0830 1070
f3722d5b 1071 if (data->innr == 14 &&
f641b588 1072 (err = sysfs_create_group(&dev->kobj,
f3722d5b
JC
1073 &pc8736x_therm_group)))
1074 goto ERROR3;
1075
1076 /* create device attr-files for varying sysfs groups */
1da177e4
LT
1077
1078 if (data->tempnr) {
dedc6a78 1079 for (i = 0; i < data->tempnr; i++) {
f3722d5b
JC
1080 if ((err = device_create_file(dev,
1081 &temp_input[i].dev_attr))
1082 || (err = device_create_file(dev,
1083 &temp_min[i].dev_attr))
1084 || (err = device_create_file(dev,
1085 &temp_max[i].dev_attr))
1086 || (err = device_create_file(dev,
1087 &temp_crit[i].dev_attr))
1088 || (err = device_create_file(dev,
1089 &temp_status[i].dev_attr)))
1090 goto ERROR3;
1da177e4 1091 }
f3722d5b
JC
1092 if ((err = device_create_file(dev, &dev_attr_alarms_temp)))
1093 goto ERROR3;
1da177e4 1094 }
1da177e4 1095
dedc6a78 1096 for (i = 0; i < data->fannr; i++) {
f3722d5b
JC
1097 if (FAN_CONFIG_MONITOR(data->fan_conf, i)
1098 && ((err = device_create_file(dev,
1099 &fan_input[i].dev_attr))
1100 || (err = device_create_file(dev,
1101 &fan_min[i].dev_attr))
1102 || (err = device_create_file(dev,
1103 &fan_div[i].dev_attr))
1104 || (err = device_create_file(dev,
1105 &fan_status[i].dev_attr))))
1106 goto ERROR3;
1107
1108 if (FAN_CONFIG_CONTROL(data->fan_conf, i)
1109 && (err = device_create_file(dev, &pwm[i].dev_attr)))
1110 goto ERROR3;
1da177e4
LT
1111 }
1112
f641b588
JD
1113 if ((err = device_create_file(dev, &dev_attr_name)))
1114 goto ERROR3;
1115
1beeffe4
TJ
1116 data->hwmon_dev = hwmon_device_register(dev);
1117 if (IS_ERR(data->hwmon_dev)) {
1118 err = PTR_ERR(data->hwmon_dev);
f3722d5b
JC
1119 goto ERROR3;
1120 }
1da177e4
LT
1121 return 0;
1122
943b0830 1123ERROR3:
f641b588 1124 device_remove_file(dev, &dev_attr_name);
f3722d5b 1125 /* can still remove groups whose members were added individually */
f641b588
JD
1126 sysfs_remove_group(&dev->kobj, &pc8736x_temp_group);
1127 sysfs_remove_group(&dev->kobj, &pc8736x_fan_group);
1128 sysfs_remove_group(&dev->kobj, &pc8736x_therm_group);
1129 sysfs_remove_group(&dev->kobj, &pc8736x_vin_group);
1da177e4
LT
1130 for (i = 0; i < 3; i++) {
1131 if (data->address[i]) {
1132 release_region(data->address[i], PC87360_EXTENT);
1133 }
1134 }
1135ERROR1:
1136 kfree(data);
1137 return err;
1138}
1139
f641b588 1140static int __devexit pc87360_remove(struct platform_device *pdev)
1da177e4 1141{
f641b588 1142 struct pc87360_data *data = platform_get_drvdata(pdev);
1da177e4
LT
1143 int i;
1144
1beeffe4 1145 hwmon_device_unregister(data->hwmon_dev);
943b0830 1146
f641b588
JD
1147 device_remove_file(&pdev->dev, &dev_attr_name);
1148 sysfs_remove_group(&pdev->dev.kobj, &pc8736x_temp_group);
1149 sysfs_remove_group(&pdev->dev.kobj, &pc8736x_fan_group);
1150 sysfs_remove_group(&pdev->dev.kobj, &pc8736x_therm_group);
1151 sysfs_remove_group(&pdev->dev.kobj, &pc8736x_vin_group);
1da177e4
LT
1152
1153 for (i = 0; i < 3; i++) {
1154 if (data->address[i]) {
1155 release_region(data->address[i], PC87360_EXTENT);
1156 }
1157 }
1158 kfree(data);
1159
1160 return 0;
1161}
1162
1163/* ldi is the logical device index
1164 bank is for voltages and temperatures only */
1165static int pc87360_read_value(struct pc87360_data *data, u8 ldi, u8 bank,
1166 u8 reg)
1167{
1168 int res;
1169
9a61bf63 1170 mutex_lock(&(data->lock));
1da177e4
LT
1171 if (bank != NO_BANK)
1172 outb_p(bank, data->address[ldi] + PC87365_REG_BANK);
1173 res = inb_p(data->address[ldi] + reg);
9a61bf63 1174 mutex_unlock(&(data->lock));
1da177e4
LT
1175
1176 return res;
1177}
1178
1179static void pc87360_write_value(struct pc87360_data *data, u8 ldi, u8 bank,
1180 u8 reg, u8 value)
1181{
9a61bf63 1182 mutex_lock(&(data->lock));
1da177e4
LT
1183 if (bank != NO_BANK)
1184 outb_p(bank, data->address[ldi] + PC87365_REG_BANK);
1185 outb_p(value, data->address[ldi] + reg);
9a61bf63 1186 mutex_unlock(&(data->lock));
1da177e4
LT
1187}
1188
28f74e71
JC
1189/* (temp & vin) channel conversion status register flags (pdf sec.11.5.12) */
1190#define CHAN_CNVRTD 0x80 /* new data ready */
1191#define CHAN_ENA 0x01 /* enabled channel (temp or vin) */
1192#define CHAN_ALM_ENA 0x10 /* propagate to alarms-reg ?? (chk val!) */
1193#define CHAN_READY (CHAN_ENA|CHAN_CNVRTD) /* sample ready mask */
1194
f641b588
JD
1195static void pc87360_init_device(struct platform_device *pdev,
1196 int use_thermistors)
1da177e4 1197{
f641b588 1198 struct pc87360_data *data = platform_get_drvdata(pdev);
1da177e4
LT
1199 int i, nr;
1200 const u8 init_in[14] = { 2, 2, 2, 2, 2, 2, 2, 1, 1, 3, 1, 2, 2, 2 };
1201 const u8 init_temp[3] = { 2, 2, 1 };
1202 u8 reg;
1203
1204 if (init >= 2 && data->innr) {
1205 reg = pc87360_read_value(data, LD_IN, NO_BANK,
1206 PC87365_REG_IN_CONVRATE);
f641b588 1207 dev_info(&pdev->dev, "VLM conversion set to "
1da177e4
LT
1208 "1s period, 160us delay\n");
1209 pc87360_write_value(data, LD_IN, NO_BANK,
1210 PC87365_REG_IN_CONVRATE,
1211 (reg & 0xC0) | 0x11);
1212 }
1213
1214 nr = data->innr < 11 ? data->innr : 11;
dedc6a78 1215 for (i = 0; i < nr; i++) {
1da177e4
LT
1216 if (init >= init_in[i]) {
1217 /* Forcibly enable voltage channel */
1218 reg = pc87360_read_value(data, LD_IN, i,
1219 PC87365_REG_IN_STATUS);
28f74e71 1220 if (!(reg & CHAN_ENA)) {
f641b588 1221 dev_dbg(&pdev->dev, "Forcibly "
1da177e4
LT
1222 "enabling in%d\n", i);
1223 pc87360_write_value(data, LD_IN, i,
1224 PC87365_REG_IN_STATUS,
1225 (reg & 0x68) | 0x87);
1226 }
1227 }
1228 }
1229
1230 /* We can't blindly trust the Super-I/O space configuration bit,
1231 most BIOS won't set it properly */
dedc6a78 1232 for (i = 11; i < data->innr; i++) {
1da177e4
LT
1233 reg = pc87360_read_value(data, LD_IN, i,
1234 PC87365_REG_TEMP_STATUS);
28f74e71 1235 use_thermistors = use_thermistors || (reg & CHAN_ENA);
1da177e4
LT
1236 }
1237
1238 i = use_thermistors ? 2 : 0;
dedc6a78 1239 for (; i < data->tempnr; i++) {
1da177e4
LT
1240 if (init >= init_temp[i]) {
1241 /* Forcibly enable temperature channel */
1242 reg = pc87360_read_value(data, LD_TEMP, i,
1243 PC87365_REG_TEMP_STATUS);
28f74e71 1244 if (!(reg & CHAN_ENA)) {
f641b588 1245 dev_dbg(&pdev->dev, "Forcibly "
1da177e4
LT
1246 "enabling temp%d\n", i+1);
1247 pc87360_write_value(data, LD_TEMP, i,
1248 PC87365_REG_TEMP_STATUS,
1249 0xCF);
1250 }
1251 }
1252 }
1253
1254 if (use_thermistors) {
dedc6a78 1255 for (i = 11; i < data->innr; i++) {
1da177e4
LT
1256 if (init >= init_in[i]) {
1257 /* The pin may already be used by thermal
1258 diodes */
1259 reg = pc87360_read_value(data, LD_TEMP,
1260 (i-11)/2, PC87365_REG_TEMP_STATUS);
28f74e71 1261 if (reg & CHAN_ENA) {
f641b588 1262 dev_dbg(&pdev->dev, "Skipping "
1da177e4
LT
1263 "temp%d, pin already in use "
1264 "by temp%d\n", i-7, (i-11)/2);
1265 continue;
1266 }
1267
1268 /* Forcibly enable thermistor channel */
1269 reg = pc87360_read_value(data, LD_IN, i,
1270 PC87365_REG_IN_STATUS);
28f74e71 1271 if (!(reg & CHAN_ENA)) {
f641b588 1272 dev_dbg(&pdev->dev, "Forcibly "
1da177e4
LT
1273 "enabling temp%d\n", i-7);
1274 pc87360_write_value(data, LD_IN, i,
1275 PC87365_REG_TEMP_STATUS,
1276 (reg & 0x60) | 0x8F);
1277 }
1278 }
1279 }
1280 }
1281
1282 if (data->innr) {
1283 reg = pc87360_read_value(data, LD_IN, NO_BANK,
1284 PC87365_REG_IN_CONFIG);
28f74e71 1285 if (reg & CHAN_ENA) {
f641b588 1286 dev_dbg(&pdev->dev, "Forcibly "
1da177e4
LT
1287 "enabling monitoring (VLM)\n");
1288 pc87360_write_value(data, LD_IN, NO_BANK,
1289 PC87365_REG_IN_CONFIG,
1290 reg & 0xFE);
1291 }
1292 }
1293
1294 if (data->tempnr) {
1295 reg = pc87360_read_value(data, LD_TEMP, NO_BANK,
1296 PC87365_REG_TEMP_CONFIG);
28f74e71 1297 if (reg & CHAN_ENA) {
f641b588 1298 dev_dbg(&pdev->dev, "Forcibly enabling "
1da177e4
LT
1299 "monitoring (TMS)\n");
1300 pc87360_write_value(data, LD_TEMP, NO_BANK,
1301 PC87365_REG_TEMP_CONFIG,
1302 reg & 0xFE);
1303 }
1304
1305 if (init >= 2) {
1306 /* Chip config as documented by National Semi. */
1307 pc87360_write_value(data, LD_TEMP, 0xF, 0xA, 0x08);
1308 /* We voluntarily omit the bank here, in case the
1309 sequence itself matters. It shouldn't be a problem,
1310 since nobody else is supposed to access the
1311 device at that point. */
1312 pc87360_write_value(data, LD_TEMP, NO_BANK, 0xB, 0x04);
1313 pc87360_write_value(data, LD_TEMP, NO_BANK, 0xC, 0x35);
1314 pc87360_write_value(data, LD_TEMP, NO_BANK, 0xD, 0x05);
1315 pc87360_write_value(data, LD_TEMP, NO_BANK, 0xE, 0x05);
1316 }
1317 }
1318}
1319
f641b588 1320static void pc87360_autodiv(struct device *dev, int nr)
1da177e4 1321{
f641b588 1322 struct pc87360_data *data = dev_get_drvdata(dev);
1da177e4
LT
1323 u8 old_min = data->fan_min[nr];
1324
1325 /* Increase clock divider if needed and possible */
1326 if ((data->fan_status[nr] & 0x04) /* overflow flag */
1327 || (data->fan[nr] >= 224)) { /* next to overflow */
1328 if ((data->fan_status[nr] & 0x60) != 0x60) {
1329 data->fan_status[nr] += 0x20;
1330 data->fan_min[nr] >>= 1;
1331 data->fan[nr] >>= 1;
f641b588 1332 dev_dbg(dev, "Increasing "
1da177e4
LT
1333 "clock divider to %d for fan %d\n",
1334 FAN_DIV_FROM_REG(data->fan_status[nr]), nr+1);
1335 }
1336 } else {
1337 /* Decrease clock divider if possible */
1338 while (!(data->fan_min[nr] & 0x80) /* min "nails" divider */
1339 && data->fan[nr] < 85 /* bad accuracy */
1340 && (data->fan_status[nr] & 0x60) != 0x00) {
1341 data->fan_status[nr] -= 0x20;
1342 data->fan_min[nr] <<= 1;
1343 data->fan[nr] <<= 1;
f641b588 1344 dev_dbg(dev, "Decreasing "
1da177e4
LT
1345 "clock divider to %d for fan %d\n",
1346 FAN_DIV_FROM_REG(data->fan_status[nr]),
1347 nr+1);
1348 }
1349 }
1350
1351 /* Write new fan min if it changed */
1352 if (old_min != data->fan_min[nr]) {
1353 pc87360_write_value(data, LD_FAN, NO_BANK,
1354 PC87360_REG_FAN_MIN(nr),
1355 data->fan_min[nr]);
1356 }
1357}
1358
1359static struct pc87360_data *pc87360_update_device(struct device *dev)
1360{
f641b588 1361 struct pc87360_data *data = dev_get_drvdata(dev);
1da177e4
LT
1362 u8 i;
1363
9a61bf63 1364 mutex_lock(&data->update_lock);
1da177e4
LT
1365
1366 if (time_after(jiffies, data->last_updated + HZ * 2) || !data->valid) {
f641b588 1367 dev_dbg(dev, "Data update\n");
1da177e4
LT
1368
1369 /* Fans */
1370 for (i = 0; i < data->fannr; i++) {
1371 if (FAN_CONFIG_MONITOR(data->fan_conf, i)) {
1372 data->fan_status[i] =
1373 pc87360_read_value(data, LD_FAN,
1374 NO_BANK, PC87360_REG_FAN_STATUS(i));
1375 data->fan[i] = pc87360_read_value(data, LD_FAN,
1376 NO_BANK, PC87360_REG_FAN(i));
1377 data->fan_min[i] = pc87360_read_value(data,
1378 LD_FAN, NO_BANK,
1379 PC87360_REG_FAN_MIN(i));
1380 /* Change clock divider if needed */
f641b588 1381 pc87360_autodiv(dev, i);
1da177e4
LT
1382 /* Clear bits and write new divider */
1383 pc87360_write_value(data, LD_FAN, NO_BANK,
1384 PC87360_REG_FAN_STATUS(i),
1385 data->fan_status[i]);
1386 }
1387 if (FAN_CONFIG_CONTROL(data->fan_conf, i))
1388 data->pwm[i] = pc87360_read_value(data, LD_FAN,
1389 NO_BANK, PC87360_REG_PWM(i));
1390 }
1391
1392 /* Voltages */
1393 for (i = 0; i < data->innr; i++) {
1394 data->in_status[i] = pc87360_read_value(data, LD_IN, i,
1395 PC87365_REG_IN_STATUS);
1396 /* Clear bits */
1397 pc87360_write_value(data, LD_IN, i,
1398 PC87365_REG_IN_STATUS,
1399 data->in_status[i]);
28f74e71 1400 if ((data->in_status[i] & CHAN_READY) == CHAN_READY) {
1da177e4
LT
1401 data->in[i] = pc87360_read_value(data, LD_IN,
1402 i, PC87365_REG_IN);
1403 }
28f74e71 1404 if (data->in_status[i] & CHAN_ENA) {
1da177e4
LT
1405 data->in_min[i] = pc87360_read_value(data,
1406 LD_IN, i,
1407 PC87365_REG_IN_MIN);
1408 data->in_max[i] = pc87360_read_value(data,
1409 LD_IN, i,
1410 PC87365_REG_IN_MAX);
1411 if (i >= 11)
1412 data->in_crit[i-11] =
1413 pc87360_read_value(data, LD_IN,
1414 i, PC87365_REG_TEMP_CRIT);
1415 }
1416 }
1417 if (data->innr) {
1418 data->in_alarms = pc87360_read_value(data, LD_IN,
1419 NO_BANK, PC87365_REG_IN_ALARMS1)
1420 | ((pc87360_read_value(data, LD_IN,
1421 NO_BANK, PC87365_REG_IN_ALARMS2)
1422 & 0x07) << 8);
1423 data->vid = (data->vid_conf & 0xE0) ?
1424 pc87360_read_value(data, LD_IN,
1425 NO_BANK, PC87365_REG_VID) : 0x1F;
1426 }
1427
1428 /* Temperatures */
1429 for (i = 0; i < data->tempnr; i++) {
1430 data->temp_status[i] = pc87360_read_value(data,
1431 LD_TEMP, i,
1432 PC87365_REG_TEMP_STATUS);
1433 /* Clear bits */
1434 pc87360_write_value(data, LD_TEMP, i,
1435 PC87365_REG_TEMP_STATUS,
1436 data->temp_status[i]);
28f74e71 1437 if ((data->temp_status[i] & CHAN_READY) == CHAN_READY) {
1da177e4
LT
1438 data->temp[i] = pc87360_read_value(data,
1439 LD_TEMP, i,
1440 PC87365_REG_TEMP);
1441 }
28f74e71 1442 if (data->temp_status[i] & CHAN_ENA) {
1da177e4
LT
1443 data->temp_min[i] = pc87360_read_value(data,
1444 LD_TEMP, i,
1445 PC87365_REG_TEMP_MIN);
1446 data->temp_max[i] = pc87360_read_value(data,
1447 LD_TEMP, i,
1448 PC87365_REG_TEMP_MAX);
1449 data->temp_crit[i] = pc87360_read_value(data,
1450 LD_TEMP, i,
1451 PC87365_REG_TEMP_CRIT);
1452 }
1453 }
1454 if (data->tempnr) {
1455 data->temp_alarms = pc87360_read_value(data, LD_TEMP,
1456 NO_BANK, PC87365_REG_TEMP_ALARMS)
1457 & 0x3F;
1458 }
1459
1460 data->last_updated = jiffies;
1461 data->valid = 1;
1462 }
1463
9a61bf63 1464 mutex_unlock(&data->update_lock);
1da177e4
LT
1465
1466 return data;
1467}
1468
f641b588
JD
1469static int __init pc87360_device_add(unsigned short address)
1470{
1471 struct resource res = {
1472 .name = "pc87360",
1473 .flags = IORESOURCE_IO,
1474 };
1475 int err, i;
1476
1477 pdev = platform_device_alloc("pc87360", address);
1478 if (!pdev) {
1479 err = -ENOMEM;
1480 printk(KERN_ERR "pc87360: Device allocation failed\n");
1481 goto exit;
1482 }
1483
1484 for (i = 0; i < 3; i++) {
1485 if (!extra_isa[i])
1486 continue;
1487 res.start = extra_isa[i];
1488 res.end = extra_isa[i] + PC87360_EXTENT - 1;
1489 err = platform_device_add_resources(pdev, &res, 1);
1490 if (err) {
1491 printk(KERN_ERR "pc87360: Device resource[%d] "
1492 "addition failed (%d)\n", i, err);
1493 goto exit_device_put;
1494 }
1495 }
1496
1497 err = platform_device_add(pdev);
1498 if (err) {
1499 printk(KERN_ERR "pc87360: Device addition failed (%d)\n",
1500 err);
1501 goto exit_device_put;
1502 }
1503
1504 return 0;
1505
1506exit_device_put:
1507 platform_device_put(pdev);
1508exit:
1509 return err;
1510}
1511
1da177e4
LT
1512static int __init pc87360_init(void)
1513{
f641b588
JD
1514 int err, i;
1515 unsigned short address = 0;
1da177e4
LT
1516
1517 if (pc87360_find(0x2e, &devid, extra_isa)
1518 && pc87360_find(0x4e, &devid, extra_isa)) {
1519 printk(KERN_WARNING "pc87360: PC8736x not detected, "
1520 "module not inserted.\n");
1521 return -ENODEV;
1522 }
1523
1524 /* Arbitrarily pick one of the addresses */
1525 for (i = 0; i < 3; i++) {
1526 if (extra_isa[i] != 0x0000) {
2d8672c5 1527 address = extra_isa[i];
1da177e4
LT
1528 break;
1529 }
1530 }
1531
2d8672c5 1532 if (address == 0x0000) {
1da177e4
LT
1533 printk(KERN_WARNING "pc87360: No active logical device, "
1534 "module not inserted.\n");
1535 return -ENODEV;
1536 }
1537
f641b588
JD
1538 err = platform_driver_register(&pc87360_driver);
1539 if (err)
1540 goto exit;
1541
1542 /* Sets global pdev as a side effect */
1543 err = pc87360_device_add(address);
1544 if (err)
1545 goto exit_driver;
1546
1547 return 0;
1548
1549 exit_driver:
1550 platform_driver_unregister(&pc87360_driver);
1551 exit:
1552 return err;
1da177e4
LT
1553}
1554
1555static void __exit pc87360_exit(void)
1556{
f641b588
JD
1557 platform_device_unregister(pdev);
1558 platform_driver_unregister(&pc87360_driver);
1da177e4
LT
1559}
1560
1561
1562MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
1563MODULE_DESCRIPTION("PC8736x hardware monitor");
1564MODULE_LICENSE("GPL");
1565
1566module_init(pc87360_init);
1567module_exit(pc87360_exit);