blob: 9b462bb13fa33b3b9cc67d66e884628a7e2043ba [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
Jean Delvaref641b582007-06-09 10:11:16 -04004 * Copyright (C) 2004, 2007 Jean Delvare <khali@linux-fr.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
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>
Jean Delvaref641b582007-06-09 10:11:16 -040040#include <linux/platform_device.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040041#include <linux/hwmon.h>
Jim Cromief0986bd2005-09-02 22:52:43 +020042#include <linux/hwmon-sysfs.h>
Jean Delvare303760b2005-07-31 21:52:01 +020043#include <linux/hwmon-vid.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040044#include <linux/err.h>
Ingo Molnar9a61bf62006-01-18 23:19:26 +010045#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <asm/io.h>
47
Linus Torvalds1da177e2005-04-16 15:20:36 -070048static u8 devid;
Jean Delvaref641b582007-06-09 10:11:16 -040049static struct platform_device *pdev;
Jean Delvare2d8672c2005-07-19 23:56:35 +020050static unsigned short extra_isa[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -070051static u8 confreg[4];
52
Linus Torvalds1da177e2005-04-16 15:20:36 -070053static 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
Jean Delvare67b671b2007-12-06 23:13:42 +010062static unsigned short force_id;
63module_param(force_id, ushort, 0);
64MODULE_PARM_DESC(force_id, "Override the detected device ID");
65
Linus Torvalds1da177e2005-04-16 15:20:36 -070066/*
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/*
Jean Delvaref641b582007-06-09 10:11:16 -0400182 * Device data
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 */
184
185struct pc87360_data {
Jean Delvaref641b582007-06-09 10:11:16 -0400186 const char *name;
Tony Jones1beeffe2007-08-20 13:46:20 -0700187 struct device *hwmon_dev;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100188 struct mutex lock;
189 struct mutex update_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 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
Jean Delvaref641b582007-06-09 10:11:16 -0400226static int pc87360_probe(struct platform_device *pdev);
Jean Delvared0546122007-07-22 12:09:48 +0200227static int __devexit pc87360_remove(struct platform_device *pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
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);
Jean Delvaref641b582007-06-09 10:11:16 -0400233static void pc87360_init_device(struct platform_device *pdev,
234 int use_thermistors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235static struct pc87360_data *pc87360_update_device(struct device *dev);
236
237/*
Jean Delvaref641b582007-06-09 10:11:16 -0400238 * Driver data
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 */
240
Jean Delvaref641b582007-06-09 10:11:16 -0400241static struct platform_driver pc87360_driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100242 .driver = {
Jean Delvare87218842006-09-03 22:36:14 +0200243 .owner = THIS_MODULE,
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100244 .name = "pc87360",
245 },
Jean Delvaref641b582007-06-09 10:11:16 -0400246 .probe = pc87360_probe,
247 .remove = __devexit_p(pc87360_remove),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248};
249
250/*
251 * Sysfs stuff
252 */
253
Jim Cromief0986bd2005-09-02 22:52:43 +0200254static 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);
Jim Cromie694fa052005-09-02 22:57:52 +0200258 return sprintf(buf, "%u\n", FAN_FROM_REG(data->fan[attr->index],
259 FAN_DIV_FROM_REG(data->fan_status[attr->index])));
Jim Cromief0986bd2005-09-02 22:52:43 +0200260}
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);
Jim Cromie694fa052005-09-02 22:57:52 +0200265 return sprintf(buf, "%u\n", FAN_FROM_REG(data->fan_min[attr->index],
266 FAN_DIV_FROM_REG(data->fan_status[attr->index])));
Jim Cromief0986bd2005-09-02 22:52:43 +0200267}
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",
Jim Cromie694fa052005-09-02 22:57:52 +0200273 FAN_DIV_FROM_REG(data->fan_status[attr->index]));
Jim Cromief0986bd2005-09-02 22:52:43 +0200274}
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",
Jim Cromie694fa052005-09-02 22:57:52 +0200280 FAN_STATUS_FROM_REG(data->fan_status[attr->index]));
Jim Cromief0986bd2005-09-02 22:52:43 +0200281}
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);
Jean Delvaref641b582007-06-09 10:11:16 -0400286 struct pc87360_data *data = dev_get_drvdata(dev);
Jim Cromie11be27e2005-09-02 23:05:07 +0200287 long fan_min = simple_strtol(buf, NULL, 10);
288
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100289 mutex_lock(&data->update_lock);
Jim Cromie11be27e2005-09-02 23:05:07 +0200290 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);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100306 mutex_unlock(&data->update_lock);
Jim Cromie11be27e2005-09-02 23:05:07 +0200307
308 return count;
Jim Cromief0986bd2005-09-02 22:52:43 +0200309}
310
Jim Cromiededc6a72006-01-09 23:24:41 +0100311static 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};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
Jim Cromie941c5c02006-09-24 21:02:44 +0200332#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
Jim Cromief0986bd2005-09-02 22:52:43 +0200338static 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",
Jim Cromie694fa052005-09-02 22:57:52 +0200343 PWM_FROM_REG(data->pwm[attr->index],
Jim Cromief0986bd2005-09-02 22:52:43 +0200344 FAN_CONFIG_INVERT(data->fan_conf,
Jim Cromie694fa052005-09-02 22:57:52 +0200345 attr->index)));
Jim Cromief0986bd2005-09-02 22:52:43 +0200346}
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);
Jean Delvaref641b582007-06-09 10:11:16 -0400351 struct pc87360_data *data = dev_get_drvdata(dev);
Jim Cromief0986bd2005-09-02 22:52:43 +0200352 long val = simple_strtol(buf, NULL, 10);
353
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100354 mutex_lock(&data->update_lock);
Jim Cromie694fa052005-09-02 22:57:52 +0200355 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]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100359 mutex_unlock(&data->update_lock);
Jim Cromief0986bd2005-09-02 22:52:43 +0200360 return count;
361}
362
Jim Cromiededc6a72006-01-09 23:24:41 +0100363static 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};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
Jim Cromie941c5c02006-09-24 21:02:44 +0200369static 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
Jim Cromief0986bd2005-09-02 22:52:43 +0200382static 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);
Jean Delvaref641b582007-06-09 10:11:16 -0400413 struct pc87360_data *data = dev_get_drvdata(dev);
Jim Cromief0986bd2005-09-02 22:52:43 +0200414 long val = simple_strtol(buf, NULL, 10);
415
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100416 mutex_lock(&data->update_lock);
Jim Cromief0986bd2005-09-02 22:52:43 +0200417 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]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100420 mutex_unlock(&data->update_lock);
Jim Cromief0986bd2005-09-02 22:52:43 +0200421 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);
Jean Delvaref641b582007-06-09 10:11:16 -0400427 struct pc87360_data *data = dev_get_drvdata(dev);
Jim Cromief0986bd2005-09-02 22:52:43 +0200428 long val = simple_strtol(buf, NULL, 10);
429
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100430 mutex_lock(&data->update_lock);
Jim Cromief0986bd2005-09-02 22:52:43 +0200431 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]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100435 mutex_unlock(&data->update_lock);
Jim Cromief0986bd2005-09-02 22:52:43 +0200436 return count;
437}
438
Jim Cromiededc6a72006-01-09 23:24:41 +0100439static 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};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
Jim Cromie941c5c02006-09-24 21:02:44 +0200492#define VIN_UNIT_ATTRS(X) \
493 &in_input[X].dev_attr.attr, \
494 &in_status[X].dev_attr.attr, \
495 &in_min[X].dev_attr.attr, \
496 &in_max[X].dev_attr.attr
497
Jim Cromie44646c12006-09-24 21:01:56 +0200498static ssize_t show_vid(struct device *dev, struct device_attribute *attr, char *buf)
499{
500 struct pc87360_data *data = pc87360_update_device(dev);
501 return sprintf(buf, "%u\n", vid_from_reg(data->vid, data->vrm));
502}
503static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL);
504
505static ssize_t show_vrm(struct device *dev, struct device_attribute *attr, char *buf)
506{
Jean Delvare90d66192007-10-08 18:24:35 +0200507 struct pc87360_data *data = dev_get_drvdata(dev);
Jim Cromie44646c12006-09-24 21:01:56 +0200508 return sprintf(buf, "%u\n", data->vrm);
509}
510static ssize_t set_vrm(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
511{
Jean Delvaref641b582007-06-09 10:11:16 -0400512 struct pc87360_data *data = dev_get_drvdata(dev);
Jim Cromie44646c12006-09-24 21:01:56 +0200513 data->vrm = simple_strtoul(buf, NULL, 10);
514 return count;
515}
516static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm, set_vrm);
517
518static ssize_t show_in_alarms(struct device *dev, struct device_attribute *attr, char *buf)
519{
520 struct pc87360_data *data = pc87360_update_device(dev);
521 return sprintf(buf, "%u\n", data->in_alarms);
522}
523static DEVICE_ATTR(alarms_in, S_IRUGO, show_in_alarms, NULL);
524
Jim Cromie941c5c02006-09-24 21:02:44 +0200525static struct attribute *pc8736x_vin_attr_array[] = {
526 VIN_UNIT_ATTRS(0),
527 VIN_UNIT_ATTRS(1),
528 VIN_UNIT_ATTRS(2),
529 VIN_UNIT_ATTRS(3),
530 VIN_UNIT_ATTRS(4),
531 VIN_UNIT_ATTRS(5),
532 VIN_UNIT_ATTRS(6),
533 VIN_UNIT_ATTRS(7),
534 VIN_UNIT_ATTRS(8),
535 VIN_UNIT_ATTRS(9),
536 VIN_UNIT_ATTRS(10),
537 &dev_attr_cpu0_vid.attr,
538 &dev_attr_vrm.attr,
539 &dev_attr_alarms_in.attr,
540 NULL
541};
542static const struct attribute_group pc8736x_vin_group = {
543 .attrs = pc8736x_vin_attr_array,
544};
545
Jim Cromief0986bd2005-09-02 22:52:43 +0200546static ssize_t show_therm_input(struct device *dev, struct device_attribute *devattr, char *buf)
547{
548 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
549 struct pc87360_data *data = pc87360_update_device(dev);
Jim Cromie694fa052005-09-02 22:57:52 +0200550 return sprintf(buf, "%u\n", IN_FROM_REG(data->in[attr->index],
Jim Cromief0986bd2005-09-02 22:52:43 +0200551 data->in_vref));
552}
553static ssize_t show_therm_min(struct device *dev, struct device_attribute *devattr, char *buf)
554{
555 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
556 struct pc87360_data *data = pc87360_update_device(dev);
Jim Cromie694fa052005-09-02 22:57:52 +0200557 return sprintf(buf, "%u\n", IN_FROM_REG(data->in_min[attr->index],
Jim Cromief0986bd2005-09-02 22:52:43 +0200558 data->in_vref));
559}
560static ssize_t show_therm_max(struct device *dev, struct device_attribute *devattr, char *buf)
561{
562 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
563 struct pc87360_data *data = pc87360_update_device(dev);
Jim Cromie694fa052005-09-02 22:57:52 +0200564 return sprintf(buf, "%u\n", IN_FROM_REG(data->in_max[attr->index],
Jim Cromief0986bd2005-09-02 22:52:43 +0200565 data->in_vref));
566}
567static ssize_t show_therm_crit(struct device *dev, struct device_attribute *devattr, char *buf)
568{
569 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
570 struct pc87360_data *data = pc87360_update_device(dev);
Jim Cromie694fa052005-09-02 22:57:52 +0200571 return sprintf(buf, "%u\n", IN_FROM_REG(data->in_crit[attr->index-11],
Jim Cromief0986bd2005-09-02 22:52:43 +0200572 data->in_vref));
573}
574static ssize_t show_therm_status(struct device *dev, struct device_attribute *devattr, char *buf)
575{
576 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
577 struct pc87360_data *data = pc87360_update_device(dev);
Jim Cromie694fa052005-09-02 22:57:52 +0200578 return sprintf(buf, "%u\n", data->in_status[attr->index]);
Jim Cromief0986bd2005-09-02 22:52:43 +0200579}
580static ssize_t set_therm_min(struct device *dev, struct device_attribute *devattr, const char *buf,
581 size_t count)
582{
583 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Jean Delvaref641b582007-06-09 10:11:16 -0400584 struct pc87360_data *data = dev_get_drvdata(dev);
Jim Cromief0986bd2005-09-02 22:52:43 +0200585 long val = simple_strtol(buf, NULL, 10);
586
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100587 mutex_lock(&data->update_lock);
Jim Cromie694fa052005-09-02 22:57:52 +0200588 data->in_min[attr->index] = IN_TO_REG(val, data->in_vref);
589 pc87360_write_value(data, LD_IN, attr->index, PC87365_REG_TEMP_MIN,
590 data->in_min[attr->index]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100591 mutex_unlock(&data->update_lock);
Jim Cromief0986bd2005-09-02 22:52:43 +0200592 return count;
593}
594static ssize_t set_therm_max(struct device *dev, struct device_attribute *devattr, const char *buf,
595 size_t count)
596{
597 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Jean Delvaref641b582007-06-09 10:11:16 -0400598 struct pc87360_data *data = dev_get_drvdata(dev);
Jim Cromief0986bd2005-09-02 22:52:43 +0200599 long val = simple_strtol(buf, NULL, 10);
600
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100601 mutex_lock(&data->update_lock);
Jim Cromie694fa052005-09-02 22:57:52 +0200602 data->in_max[attr->index] = IN_TO_REG(val, data->in_vref);
603 pc87360_write_value(data, LD_IN, attr->index, PC87365_REG_TEMP_MAX,
604 data->in_max[attr->index]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100605 mutex_unlock(&data->update_lock);
Jim Cromief0986bd2005-09-02 22:52:43 +0200606 return count;
607}
608static ssize_t set_therm_crit(struct device *dev, struct device_attribute *devattr, const char *buf,
609 size_t count)
610{
611 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Jean Delvaref641b582007-06-09 10:11:16 -0400612 struct pc87360_data *data = dev_get_drvdata(dev);
Jim Cromief0986bd2005-09-02 22:52:43 +0200613 long val = simple_strtol(buf, NULL, 10);
614
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100615 mutex_lock(&data->update_lock);
Jim Cromie694fa052005-09-02 22:57:52 +0200616 data->in_crit[attr->index-11] = IN_TO_REG(val, data->in_vref);
617 pc87360_write_value(data, LD_IN, attr->index, PC87365_REG_TEMP_CRIT,
618 data->in_crit[attr->index-11]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100619 mutex_unlock(&data->update_lock);
Jim Cromief0986bd2005-09-02 22:52:43 +0200620 return count;
621}
622
Jim Cromiededc6a72006-01-09 23:24:41 +0100623/* the +11 term below reflects the fact that VLM units 11,12,13 are
624 used in the chip to measure voltage across the thermistors
625*/
626static struct sensor_device_attribute therm_input[] = {
627 SENSOR_ATTR(temp4_input, S_IRUGO, show_therm_input, NULL, 0+11),
628 SENSOR_ATTR(temp5_input, S_IRUGO, show_therm_input, NULL, 1+11),
629 SENSOR_ATTR(temp6_input, S_IRUGO, show_therm_input, NULL, 2+11),
630};
631static struct sensor_device_attribute therm_status[] = {
632 SENSOR_ATTR(temp4_status, S_IRUGO, show_therm_status, NULL, 0+11),
633 SENSOR_ATTR(temp5_status, S_IRUGO, show_therm_status, NULL, 1+11),
634 SENSOR_ATTR(temp6_status, S_IRUGO, show_therm_status, NULL, 2+11),
635};
636static struct sensor_device_attribute therm_min[] = {
637 SENSOR_ATTR(temp4_min, S_IRUGO | S_IWUSR,
638 show_therm_min, set_therm_min, 0+11),
639 SENSOR_ATTR(temp5_min, S_IRUGO | S_IWUSR,
640 show_therm_min, set_therm_min, 1+11),
641 SENSOR_ATTR(temp6_min, S_IRUGO | S_IWUSR,
642 show_therm_min, set_therm_min, 2+11),
643};
644static struct sensor_device_attribute therm_max[] = {
645 SENSOR_ATTR(temp4_max, S_IRUGO | S_IWUSR,
646 show_therm_max, set_therm_max, 0+11),
647 SENSOR_ATTR(temp5_max, S_IRUGO | S_IWUSR,
648 show_therm_max, set_therm_max, 1+11),
649 SENSOR_ATTR(temp6_max, S_IRUGO | S_IWUSR,
650 show_therm_max, set_therm_max, 2+11),
651};
652static struct sensor_device_attribute therm_crit[] = {
653 SENSOR_ATTR(temp4_crit, S_IRUGO | S_IWUSR,
654 show_therm_crit, set_therm_crit, 0+11),
655 SENSOR_ATTR(temp5_crit, S_IRUGO | S_IWUSR,
656 show_therm_crit, set_therm_crit, 1+11),
657 SENSOR_ATTR(temp6_crit, S_IRUGO | S_IWUSR,
658 show_therm_crit, set_therm_crit, 2+11),
659};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
Jim Cromie941c5c02006-09-24 21:02:44 +0200661#define THERM_UNIT_ATTRS(X) \
662 &therm_input[X].dev_attr.attr, \
663 &therm_status[X].dev_attr.attr, \
664 &therm_min[X].dev_attr.attr, \
665 &therm_max[X].dev_attr.attr, \
666 &therm_crit[X].dev_attr.attr
667
668static struct attribute * pc8736x_therm_attr_array[] = {
669 THERM_UNIT_ATTRS(0),
670 THERM_UNIT_ATTRS(1),
671 THERM_UNIT_ATTRS(2),
672 NULL
673};
674static const struct attribute_group pc8736x_therm_group = {
675 .attrs = pc8736x_therm_attr_array,
676};
677
Jim Cromief0986bd2005-09-02 22:52:43 +0200678static ssize_t show_temp_input(struct device *dev, struct device_attribute *devattr, char *buf)
679{
680 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
681 struct pc87360_data *data = pc87360_update_device(dev);
Jim Cromie694fa052005-09-02 22:57:52 +0200682 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[attr->index]));
Jim Cromief0986bd2005-09-02 22:52:43 +0200683}
684static ssize_t show_temp_min(struct device *dev, struct device_attribute *devattr, char *buf)
685{
686 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
687 struct pc87360_data *data = pc87360_update_device(dev);
Jim Cromie694fa052005-09-02 22:57:52 +0200688 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_min[attr->index]));
Jim Cromief0986bd2005-09-02 22:52:43 +0200689}
690static ssize_t show_temp_max(struct device *dev, struct device_attribute *devattr, char *buf)
691{
692 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
693 struct pc87360_data *data = pc87360_update_device(dev);
Jim Cromie694fa052005-09-02 22:57:52 +0200694 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max[attr->index]));
Jim Cromief0986bd2005-09-02 22:52:43 +0200695}
696static ssize_t show_temp_crit(struct device *dev, struct device_attribute *devattr, char *buf)
697{
698 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
699 struct pc87360_data *data = pc87360_update_device(dev);
Jim Cromie694fa052005-09-02 22:57:52 +0200700 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_crit[attr->index]));
Jim Cromief0986bd2005-09-02 22:52:43 +0200701}
702static ssize_t show_temp_status(struct device *dev, struct device_attribute *devattr, char *buf)
703{
704 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
705 struct pc87360_data *data = pc87360_update_device(dev);
Jim Cromie694fa052005-09-02 22:57:52 +0200706 return sprintf(buf, "%d\n", data->temp_status[attr->index]);
Jim Cromief0986bd2005-09-02 22:52:43 +0200707}
708static ssize_t set_temp_min(struct device *dev, struct device_attribute *devattr, const char *buf,
709 size_t count)
710{
711 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Jean Delvaref641b582007-06-09 10:11:16 -0400712 struct pc87360_data *data = dev_get_drvdata(dev);
Jim Cromief0986bd2005-09-02 22:52:43 +0200713 long val = simple_strtol(buf, NULL, 10);
714
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100715 mutex_lock(&data->update_lock);
Jim Cromie694fa052005-09-02 22:57:52 +0200716 data->temp_min[attr->index] = TEMP_TO_REG(val);
717 pc87360_write_value(data, LD_TEMP, attr->index, PC87365_REG_TEMP_MIN,
718 data->temp_min[attr->index]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100719 mutex_unlock(&data->update_lock);
Jim Cromief0986bd2005-09-02 22:52:43 +0200720 return count;
721}
722static ssize_t set_temp_max(struct device *dev, struct device_attribute *devattr, const char *buf,
723 size_t count)
724{
725 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Jean Delvaref641b582007-06-09 10:11:16 -0400726 struct pc87360_data *data = dev_get_drvdata(dev);
Jim Cromief0986bd2005-09-02 22:52:43 +0200727 long val = simple_strtol(buf, NULL, 10);
728
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100729 mutex_lock(&data->update_lock);
Jim Cromie694fa052005-09-02 22:57:52 +0200730 data->temp_max[attr->index] = TEMP_TO_REG(val);
731 pc87360_write_value(data, LD_TEMP, attr->index, PC87365_REG_TEMP_MAX,
732 data->temp_max[attr->index]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100733 mutex_unlock(&data->update_lock);
Jim Cromief0986bd2005-09-02 22:52:43 +0200734 return count;
735}
736static ssize_t set_temp_crit(struct device *dev, struct device_attribute *devattr, const char *buf,
737 size_t count)
738{
739 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Jean Delvaref641b582007-06-09 10:11:16 -0400740 struct pc87360_data *data = dev_get_drvdata(dev);
Jim Cromief0986bd2005-09-02 22:52:43 +0200741 long val = simple_strtol(buf, NULL, 10);
742
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100743 mutex_lock(&data->update_lock);
Jim Cromie694fa052005-09-02 22:57:52 +0200744 data->temp_crit[attr->index] = TEMP_TO_REG(val);
745 pc87360_write_value(data, LD_TEMP, attr->index, PC87365_REG_TEMP_CRIT,
746 data->temp_crit[attr->index]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100747 mutex_unlock(&data->update_lock);
Jim Cromief0986bd2005-09-02 22:52:43 +0200748 return count;
749}
750
Jim Cromiededc6a72006-01-09 23:24:41 +0100751static struct sensor_device_attribute temp_input[] = {
752 SENSOR_ATTR(temp1_input, S_IRUGO, show_temp_input, NULL, 0),
753 SENSOR_ATTR(temp2_input, S_IRUGO, show_temp_input, NULL, 1),
754 SENSOR_ATTR(temp3_input, S_IRUGO, show_temp_input, NULL, 2),
755};
756static struct sensor_device_attribute temp_status[] = {
757 SENSOR_ATTR(temp1_status, S_IRUGO, show_temp_status, NULL, 0),
758 SENSOR_ATTR(temp2_status, S_IRUGO, show_temp_status, NULL, 1),
759 SENSOR_ATTR(temp3_status, S_IRUGO, show_temp_status, NULL, 2),
760};
761static struct sensor_device_attribute temp_min[] = {
762 SENSOR_ATTR(temp1_min, S_IRUGO | S_IWUSR,
763 show_temp_min, set_temp_min, 0),
764 SENSOR_ATTR(temp2_min, S_IRUGO | S_IWUSR,
765 show_temp_min, set_temp_min, 1),
766 SENSOR_ATTR(temp3_min, S_IRUGO | S_IWUSR,
767 show_temp_min, set_temp_min, 2),
768};
769static struct sensor_device_attribute temp_max[] = {
770 SENSOR_ATTR(temp1_max, S_IRUGO | S_IWUSR,
771 show_temp_max, set_temp_max, 0),
772 SENSOR_ATTR(temp2_max, S_IRUGO | S_IWUSR,
773 show_temp_max, set_temp_max, 1),
774 SENSOR_ATTR(temp3_max, S_IRUGO | S_IWUSR,
775 show_temp_max, set_temp_max, 2),
776};
777static struct sensor_device_attribute temp_crit[] = {
778 SENSOR_ATTR(temp1_crit, S_IRUGO | S_IWUSR,
779 show_temp_crit, set_temp_crit, 0),
780 SENSOR_ATTR(temp2_crit, S_IRUGO | S_IWUSR,
781 show_temp_crit, set_temp_crit, 1),
782 SENSOR_ATTR(temp3_crit, S_IRUGO | S_IWUSR,
783 show_temp_crit, set_temp_crit, 2),
784};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
Yani Ioannoua5099cf2005-05-17 06:42:25 -0400786static ssize_t show_temp_alarms(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787{
788 struct pc87360_data *data = pc87360_update_device(dev);
789 return sprintf(buf, "%u\n", data->temp_alarms);
790}
791static DEVICE_ATTR(alarms_temp, S_IRUGO, show_temp_alarms, NULL);
792
Jim Cromie941c5c02006-09-24 21:02:44 +0200793#define TEMP_UNIT_ATTRS(X) \
794 &temp_input[X].dev_attr.attr, \
795 &temp_status[X].dev_attr.attr, \
796 &temp_min[X].dev_attr.attr, \
797 &temp_max[X].dev_attr.attr, \
798 &temp_crit[X].dev_attr.attr
799
800static struct attribute * pc8736x_temp_attr_array[] = {
801 TEMP_UNIT_ATTRS(0),
802 TEMP_UNIT_ATTRS(1),
803 TEMP_UNIT_ATTRS(2),
804 /* include the few miscellaneous atts here */
805 &dev_attr_alarms_temp.attr,
806 NULL
807};
808static const struct attribute_group pc8736x_temp_group = {
809 .attrs = pc8736x_temp_attr_array,
810};
811
Jean Delvaref641b582007-06-09 10:11:16 -0400812static ssize_t show_name(struct device *dev, struct device_attribute
813 *devattr, char *buf)
814{
815 struct pc87360_data *data = dev_get_drvdata(dev);
816 return sprintf(buf, "%s\n", data->name);
817}
818static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
819
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820/*
821 * Device detection, registration and update
822 */
823
Jean Delvaree6cfb3a2005-07-27 21:32:02 +0200824static int __init pc87360_find(int sioaddr, u8 *devid, unsigned short *addresses)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825{
826 u16 val;
827 int i;
828 int nrdev; /* logical device count */
829
830 /* No superio_enter */
831
832 /* Identify device */
Jean Delvare67b671b2007-12-06 23:13:42 +0100833 val = force_id ? force_id : superio_inb(sioaddr, DEVID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 switch (val) {
835 case 0xE1: /* PC87360 */
836 case 0xE8: /* PC87363 */
837 case 0xE4: /* PC87364 */
838 nrdev = 1;
839 break;
840 case 0xE5: /* PC87365 */
841 case 0xE9: /* PC87366 */
842 nrdev = 3;
843 break;
844 default:
845 superio_exit(sioaddr);
846 return -ENODEV;
847 }
848 /* Remember the device id */
849 *devid = val;
850
851 for (i = 0; i < nrdev; i++) {
852 /* select logical device */
853 superio_outb(sioaddr, DEV, logdev[i]);
854
855 val = superio_inb(sioaddr, ACT);
856 if (!(val & 0x01)) {
857 printk(KERN_INFO "pc87360: Device 0x%02x not "
858 "activated\n", logdev[i]);
859 continue;
860 }
861
862 val = (superio_inb(sioaddr, BASE) << 8)
863 | superio_inb(sioaddr, BASE + 1);
864 if (!val) {
865 printk(KERN_INFO "pc87360: Base address not set for "
866 "device 0x%02x\n", logdev[i]);
867 continue;
868 }
869
Jean Delvare2d8672c2005-07-19 23:56:35 +0200870 addresses[i] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871
872 if (i==0) { /* Fans */
873 confreg[0] = superio_inb(sioaddr, 0xF0);
874 confreg[1] = superio_inb(sioaddr, 0xF1);
875
876#ifdef DEBUG
877 printk(KERN_DEBUG "pc87360: Fan 1: mon=%d "
878 "ctrl=%d inv=%d\n", (confreg[0]>>2)&1,
879 (confreg[0]>>3)&1, (confreg[0]>>4)&1);
880 printk(KERN_DEBUG "pc87360: Fan 2: mon=%d "
881 "ctrl=%d inv=%d\n", (confreg[0]>>5)&1,
882 (confreg[0]>>6)&1, (confreg[0]>>7)&1);
883 printk(KERN_DEBUG "pc87360: Fan 3: mon=%d "
884 "ctrl=%d inv=%d\n", confreg[1]&1,
885 (confreg[1]>>1)&1, (confreg[1]>>2)&1);
886#endif
887 } else if (i==1) { /* Voltages */
888 /* Are we using thermistors? */
889 if (*devid == 0xE9) { /* PC87366 */
890 /* These registers are not logical-device
891 specific, just that we won't need them if
892 we don't use the VLM device */
893 confreg[2] = superio_inb(sioaddr, 0x2B);
894 confreg[3] = superio_inb(sioaddr, 0x25);
895
896 if (confreg[2] & 0x40) {
897 printk(KERN_INFO "pc87360: Using "
898 "thermistors for temperature "
899 "monitoring\n");
900 }
901 if (confreg[3] & 0xE0) {
902 printk(KERN_INFO "pc87360: VID "
903 "inputs routed (mode %u)\n",
904 confreg[3] >> 5);
905 }
906 }
907 }
908 }
909
910 superio_exit(sioaddr);
911 return 0;
912}
913
Jean Delvaref641b582007-06-09 10:11:16 -0400914static int __devinit pc87360_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915{
916 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 struct pc87360_data *data;
918 int err = 0;
919 const char *name = "pc87360";
920 int use_thermistors = 0;
Jean Delvaref641b582007-06-09 10:11:16 -0400921 struct device *dev = &pdev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
Deepak Saxenaba9c2e82005-10-17 23:08:32 +0200923 if (!(data = kzalloc(sizeof(struct pc87360_data), GFP_KERNEL)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 data->fannr = 2;
927 data->innr = 0;
928 data->tempnr = 0;
929
930 switch (devid) {
931 case 0xe8:
932 name = "pc87363";
933 break;
934 case 0xe4:
935 name = "pc87364";
936 data->fannr = 3;
937 break;
938 case 0xe5:
939 name = "pc87365";
940 data->fannr = extra_isa[0] ? 3 : 0;
941 data->innr = extra_isa[1] ? 11 : 0;
942 data->tempnr = extra_isa[2] ? 2 : 0;
943 break;
944 case 0xe9:
945 name = "pc87366";
946 data->fannr = extra_isa[0] ? 3 : 0;
947 data->innr = extra_isa[1] ? 14 : 0;
948 data->tempnr = extra_isa[2] ? 3 : 0;
949 break;
950 }
951
Jean Delvaref641b582007-06-09 10:11:16 -0400952 data->name = name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 data->valid = 0;
Jean Delvaref641b582007-06-09 10:11:16 -0400954 mutex_init(&data->lock);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100955 mutex_init(&data->update_lock);
Jean Delvaref641b582007-06-09 10:11:16 -0400956 platform_set_drvdata(pdev, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957
958 for (i = 0; i < 3; i++) {
959 if (((data->address[i] = extra_isa[i]))
960 && !request_region(extra_isa[i], PC87360_EXTENT,
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100961 pc87360_driver.driver.name)) {
Jean Delvaref641b582007-06-09 10:11:16 -0400962 dev_err(dev, "Region 0x%x-0x%x already "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 "in use!\n", extra_isa[i],
964 extra_isa[i]+PC87360_EXTENT-1);
965 for (i--; i >= 0; i--)
966 release_region(extra_isa[i], PC87360_EXTENT);
967 err = -EBUSY;
968 goto ERROR1;
969 }
970 }
971
972 /* Retrieve the fans configuration from Super-I/O space */
973 if (data->fannr)
974 data->fan_conf = confreg[0] | (confreg[1] << 8);
975
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 /* Use the correct reference voltage
977 Unless both the VLM and the TMS logical devices agree to
978 use an external Vref, the internal one is used. */
979 if (data->innr) {
980 i = pc87360_read_value(data, LD_IN, NO_BANK,
981 PC87365_REG_IN_CONFIG);
982 if (data->tempnr) {
983 i &= pc87360_read_value(data, LD_TEMP, NO_BANK,
984 PC87365_REG_TEMP_CONFIG);
985 }
986 data->in_vref = (i&0x02) ? 3025 : 2966;
Jean Delvaref641b582007-06-09 10:11:16 -0400987 dev_dbg(dev, "Using %s reference voltage\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 (i&0x02) ? "external" : "internal");
989
990 data->vid_conf = confreg[3];
Jim Cromie3d7f9a82006-12-12 18:18:28 +0100991 data->vrm = vid_which_vrm();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 }
993
994 /* Fan clock dividers may be needed before any data is read */
995 for (i = 0; i < data->fannr; i++) {
996 if (FAN_CONFIG_MONITOR(data->fan_conf, i))
997 data->fan_status[i] = pc87360_read_value(data,
998 LD_FAN, NO_BANK,
999 PC87360_REG_FAN_STATUS(i));
1000 }
1001
1002 if (init > 0) {
1003 if (devid == 0xe9 && data->address[1]) /* PC87366 */
1004 use_thermistors = confreg[2] & 0x40;
1005
Jean Delvaref641b582007-06-09 10:11:16 -04001006 pc87360_init_device(pdev, use_thermistors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 }
1008
Jim Cromief3722d5b2006-09-24 21:03:25 +02001009 /* Register all-or-nothing sysfs groups */
1010
1011 if (data->innr &&
Jean Delvaref641b582007-06-09 10:11:16 -04001012 (err = sysfs_create_group(&dev->kobj,
Jim Cromief3722d5b2006-09-24 21:03:25 +02001013 &pc8736x_vin_group)))
1014 goto ERROR3;
1015
1016 if (data->innr == 14 &&
Jean Delvaref641b582007-06-09 10:11:16 -04001017 (err = sysfs_create_group(&dev->kobj,
Jim Cromief3722d5b2006-09-24 21:03:25 +02001018 &pc8736x_therm_group)))
1019 goto ERROR3;
1020
1021 /* create device attr-files for varying sysfs groups */
1022
1023 if (data->tempnr) {
1024 for (i = 0; i < data->tempnr; i++) {
1025 if ((err = device_create_file(dev,
1026 &temp_input[i].dev_attr))
1027 || (err = device_create_file(dev,
1028 &temp_min[i].dev_attr))
1029 || (err = device_create_file(dev,
1030 &temp_max[i].dev_attr))
1031 || (err = device_create_file(dev,
1032 &temp_crit[i].dev_attr))
1033 || (err = device_create_file(dev,
1034 &temp_status[i].dev_attr)))
1035 goto ERROR3;
1036 }
1037 if ((err = device_create_file(dev, &dev_attr_alarms_temp)))
1038 goto ERROR3;
1039 }
1040
1041 for (i = 0; i < data->fannr; i++) {
1042 if (FAN_CONFIG_MONITOR(data->fan_conf, i)
1043 && ((err = device_create_file(dev,
1044 &fan_input[i].dev_attr))
1045 || (err = device_create_file(dev,
1046 &fan_min[i].dev_attr))
1047 || (err = device_create_file(dev,
1048 &fan_div[i].dev_attr))
1049 || (err = device_create_file(dev,
1050 &fan_status[i].dev_attr))))
1051 goto ERROR3;
1052
1053 if (FAN_CONFIG_CONTROL(data->fan_conf, i)
1054 && (err = device_create_file(dev, &pwm[i].dev_attr)))
1055 goto ERROR3;
1056 }
1057
Jean Delvaref641b582007-06-09 10:11:16 -04001058 if ((err = device_create_file(dev, &dev_attr_name)))
1059 goto ERROR3;
1060
Tony Jones1beeffe2007-08-20 13:46:20 -07001061 data->hwmon_dev = hwmon_device_register(dev);
1062 if (IS_ERR(data->hwmon_dev)) {
1063 err = PTR_ERR(data->hwmon_dev);
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001064 goto ERROR3;
1065 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 return 0;
1067
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001068ERROR3:
Jean Delvaref641b582007-06-09 10:11:16 -04001069 device_remove_file(dev, &dev_attr_name);
Jim Cromief3722d5b2006-09-24 21:03:25 +02001070 /* can still remove groups whose members were added individually */
Jean Delvaref641b582007-06-09 10:11:16 -04001071 sysfs_remove_group(&dev->kobj, &pc8736x_temp_group);
1072 sysfs_remove_group(&dev->kobj, &pc8736x_fan_group);
1073 sysfs_remove_group(&dev->kobj, &pc8736x_therm_group);
1074 sysfs_remove_group(&dev->kobj, &pc8736x_vin_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 for (i = 0; i < 3; i++) {
1076 if (data->address[i]) {
1077 release_region(data->address[i], PC87360_EXTENT);
1078 }
1079 }
1080ERROR1:
1081 kfree(data);
1082 return err;
1083}
1084
Jean Delvaref641b582007-06-09 10:11:16 -04001085static int __devexit pc87360_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086{
Jean Delvaref641b582007-06-09 10:11:16 -04001087 struct pc87360_data *data = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 int i;
1089
Tony Jones1beeffe2007-08-20 13:46:20 -07001090 hwmon_device_unregister(data->hwmon_dev);
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001091
Jean Delvaref641b582007-06-09 10:11:16 -04001092 device_remove_file(&pdev->dev, &dev_attr_name);
1093 sysfs_remove_group(&pdev->dev.kobj, &pc8736x_temp_group);
1094 sysfs_remove_group(&pdev->dev.kobj, &pc8736x_fan_group);
1095 sysfs_remove_group(&pdev->dev.kobj, &pc8736x_therm_group);
1096 sysfs_remove_group(&pdev->dev.kobj, &pc8736x_vin_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097
1098 for (i = 0; i < 3; i++) {
1099 if (data->address[i]) {
1100 release_region(data->address[i], PC87360_EXTENT);
1101 }
1102 }
1103 kfree(data);
1104
1105 return 0;
1106}
1107
1108/* ldi is the logical device index
1109 bank is for voltages and temperatures only */
1110static int pc87360_read_value(struct pc87360_data *data, u8 ldi, u8 bank,
1111 u8 reg)
1112{
1113 int res;
1114
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001115 mutex_lock(&(data->lock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 if (bank != NO_BANK)
1117 outb_p(bank, data->address[ldi] + PC87365_REG_BANK);
1118 res = inb_p(data->address[ldi] + reg);
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001119 mutex_unlock(&(data->lock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120
1121 return res;
1122}
1123
1124static void pc87360_write_value(struct pc87360_data *data, u8 ldi, u8 bank,
1125 u8 reg, u8 value)
1126{
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001127 mutex_lock(&(data->lock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 if (bank != NO_BANK)
1129 outb_p(bank, data->address[ldi] + PC87365_REG_BANK);
1130 outb_p(value, data->address[ldi] + reg);
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001131 mutex_unlock(&(data->lock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132}
1133
Jean Delvaref641b582007-06-09 10:11:16 -04001134static void pc87360_init_device(struct platform_device *pdev,
1135 int use_thermistors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136{
Jean Delvaref641b582007-06-09 10:11:16 -04001137 struct pc87360_data *data = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 int i, nr;
1139 const u8 init_in[14] = { 2, 2, 2, 2, 2, 2, 2, 1, 1, 3, 1, 2, 2, 2 };
1140 const u8 init_temp[3] = { 2, 2, 1 };
1141 u8 reg;
1142
1143 if (init >= 2 && data->innr) {
1144 reg = pc87360_read_value(data, LD_IN, NO_BANK,
1145 PC87365_REG_IN_CONVRATE);
Jean Delvaref641b582007-06-09 10:11:16 -04001146 dev_info(&pdev->dev, "VLM conversion set to "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 "1s period, 160us delay\n");
1148 pc87360_write_value(data, LD_IN, NO_BANK,
1149 PC87365_REG_IN_CONVRATE,
1150 (reg & 0xC0) | 0x11);
1151 }
1152
1153 nr = data->innr < 11 ? data->innr : 11;
Jim Cromiededc6a72006-01-09 23:24:41 +01001154 for (i = 0; i < nr; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 if (init >= init_in[i]) {
1156 /* Forcibly enable voltage channel */
1157 reg = pc87360_read_value(data, LD_IN, i,
1158 PC87365_REG_IN_STATUS);
1159 if (!(reg & 0x01)) {
Jean Delvaref641b582007-06-09 10:11:16 -04001160 dev_dbg(&pdev->dev, "Forcibly "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 "enabling in%d\n", i);
1162 pc87360_write_value(data, LD_IN, i,
1163 PC87365_REG_IN_STATUS,
1164 (reg & 0x68) | 0x87);
1165 }
1166 }
1167 }
1168
1169 /* We can't blindly trust the Super-I/O space configuration bit,
1170 most BIOS won't set it properly */
Jim Cromiededc6a72006-01-09 23:24:41 +01001171 for (i = 11; i < data->innr; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 reg = pc87360_read_value(data, LD_IN, i,
1173 PC87365_REG_TEMP_STATUS);
1174 use_thermistors = use_thermistors || (reg & 0x01);
1175 }
1176
1177 i = use_thermistors ? 2 : 0;
Jim Cromiededc6a72006-01-09 23:24:41 +01001178 for (; i < data->tempnr; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 if (init >= init_temp[i]) {
1180 /* Forcibly enable temperature channel */
1181 reg = pc87360_read_value(data, LD_TEMP, i,
1182 PC87365_REG_TEMP_STATUS);
1183 if (!(reg & 0x01)) {
Jean Delvaref641b582007-06-09 10:11:16 -04001184 dev_dbg(&pdev->dev, "Forcibly "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 "enabling temp%d\n", i+1);
1186 pc87360_write_value(data, LD_TEMP, i,
1187 PC87365_REG_TEMP_STATUS,
1188 0xCF);
1189 }
1190 }
1191 }
1192
1193 if (use_thermistors) {
Jim Cromiededc6a72006-01-09 23:24:41 +01001194 for (i = 11; i < data->innr; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 if (init >= init_in[i]) {
1196 /* The pin may already be used by thermal
1197 diodes */
1198 reg = pc87360_read_value(data, LD_TEMP,
1199 (i-11)/2, PC87365_REG_TEMP_STATUS);
1200 if (reg & 0x01) {
Jean Delvaref641b582007-06-09 10:11:16 -04001201 dev_dbg(&pdev->dev, "Skipping "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 "temp%d, pin already in use "
1203 "by temp%d\n", i-7, (i-11)/2);
1204 continue;
1205 }
1206
1207 /* Forcibly enable thermistor channel */
1208 reg = pc87360_read_value(data, LD_IN, i,
1209 PC87365_REG_IN_STATUS);
1210 if (!(reg & 0x01)) {
Jean Delvaref641b582007-06-09 10:11:16 -04001211 dev_dbg(&pdev->dev, "Forcibly "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 "enabling temp%d\n", i-7);
1213 pc87360_write_value(data, LD_IN, i,
1214 PC87365_REG_TEMP_STATUS,
1215 (reg & 0x60) | 0x8F);
1216 }
1217 }
1218 }
1219 }
1220
1221 if (data->innr) {
1222 reg = pc87360_read_value(data, LD_IN, NO_BANK,
1223 PC87365_REG_IN_CONFIG);
1224 if (reg & 0x01) {
Jean Delvaref641b582007-06-09 10:11:16 -04001225 dev_dbg(&pdev->dev, "Forcibly "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 "enabling monitoring (VLM)\n");
1227 pc87360_write_value(data, LD_IN, NO_BANK,
1228 PC87365_REG_IN_CONFIG,
1229 reg & 0xFE);
1230 }
1231 }
1232
1233 if (data->tempnr) {
1234 reg = pc87360_read_value(data, LD_TEMP, NO_BANK,
1235 PC87365_REG_TEMP_CONFIG);
1236 if (reg & 0x01) {
Jean Delvaref641b582007-06-09 10:11:16 -04001237 dev_dbg(&pdev->dev, "Forcibly enabling "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 "monitoring (TMS)\n");
1239 pc87360_write_value(data, LD_TEMP, NO_BANK,
1240 PC87365_REG_TEMP_CONFIG,
1241 reg & 0xFE);
1242 }
1243
1244 if (init >= 2) {
1245 /* Chip config as documented by National Semi. */
1246 pc87360_write_value(data, LD_TEMP, 0xF, 0xA, 0x08);
1247 /* We voluntarily omit the bank here, in case the
1248 sequence itself matters. It shouldn't be a problem,
1249 since nobody else is supposed to access the
1250 device at that point. */
1251 pc87360_write_value(data, LD_TEMP, NO_BANK, 0xB, 0x04);
1252 pc87360_write_value(data, LD_TEMP, NO_BANK, 0xC, 0x35);
1253 pc87360_write_value(data, LD_TEMP, NO_BANK, 0xD, 0x05);
1254 pc87360_write_value(data, LD_TEMP, NO_BANK, 0xE, 0x05);
1255 }
1256 }
1257}
1258
Jean Delvaref641b582007-06-09 10:11:16 -04001259static void pc87360_autodiv(struct device *dev, int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260{
Jean Delvaref641b582007-06-09 10:11:16 -04001261 struct pc87360_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 u8 old_min = data->fan_min[nr];
1263
1264 /* Increase clock divider if needed and possible */
1265 if ((data->fan_status[nr] & 0x04) /* overflow flag */
1266 || (data->fan[nr] >= 224)) { /* next to overflow */
1267 if ((data->fan_status[nr] & 0x60) != 0x60) {
1268 data->fan_status[nr] += 0x20;
1269 data->fan_min[nr] >>= 1;
1270 data->fan[nr] >>= 1;
Jean Delvaref641b582007-06-09 10:11:16 -04001271 dev_dbg(dev, "Increasing "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 "clock divider to %d for fan %d\n",
1273 FAN_DIV_FROM_REG(data->fan_status[nr]), nr+1);
1274 }
1275 } else {
1276 /* Decrease clock divider if possible */
1277 while (!(data->fan_min[nr] & 0x80) /* min "nails" divider */
1278 && data->fan[nr] < 85 /* bad accuracy */
1279 && (data->fan_status[nr] & 0x60) != 0x00) {
1280 data->fan_status[nr] -= 0x20;
1281 data->fan_min[nr] <<= 1;
1282 data->fan[nr] <<= 1;
Jean Delvaref641b582007-06-09 10:11:16 -04001283 dev_dbg(dev, "Decreasing "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 "clock divider to %d for fan %d\n",
1285 FAN_DIV_FROM_REG(data->fan_status[nr]),
1286 nr+1);
1287 }
1288 }
1289
1290 /* Write new fan min if it changed */
1291 if (old_min != data->fan_min[nr]) {
1292 pc87360_write_value(data, LD_FAN, NO_BANK,
1293 PC87360_REG_FAN_MIN(nr),
1294 data->fan_min[nr]);
1295 }
1296}
1297
1298static struct pc87360_data *pc87360_update_device(struct device *dev)
1299{
Jean Delvaref641b582007-06-09 10:11:16 -04001300 struct pc87360_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 u8 i;
1302
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001303 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304
1305 if (time_after(jiffies, data->last_updated + HZ * 2) || !data->valid) {
Jean Delvaref641b582007-06-09 10:11:16 -04001306 dev_dbg(dev, "Data update\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307
1308 /* Fans */
1309 for (i = 0; i < data->fannr; i++) {
1310 if (FAN_CONFIG_MONITOR(data->fan_conf, i)) {
1311 data->fan_status[i] =
1312 pc87360_read_value(data, LD_FAN,
1313 NO_BANK, PC87360_REG_FAN_STATUS(i));
1314 data->fan[i] = pc87360_read_value(data, LD_FAN,
1315 NO_BANK, PC87360_REG_FAN(i));
1316 data->fan_min[i] = pc87360_read_value(data,
1317 LD_FAN, NO_BANK,
1318 PC87360_REG_FAN_MIN(i));
1319 /* Change clock divider if needed */
Jean Delvaref641b582007-06-09 10:11:16 -04001320 pc87360_autodiv(dev, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 /* Clear bits and write new divider */
1322 pc87360_write_value(data, LD_FAN, NO_BANK,
1323 PC87360_REG_FAN_STATUS(i),
1324 data->fan_status[i]);
1325 }
1326 if (FAN_CONFIG_CONTROL(data->fan_conf, i))
1327 data->pwm[i] = pc87360_read_value(data, LD_FAN,
1328 NO_BANK, PC87360_REG_PWM(i));
1329 }
1330
1331 /* Voltages */
1332 for (i = 0; i < data->innr; i++) {
1333 data->in_status[i] = pc87360_read_value(data, LD_IN, i,
1334 PC87365_REG_IN_STATUS);
1335 /* Clear bits */
1336 pc87360_write_value(data, LD_IN, i,
1337 PC87365_REG_IN_STATUS,
1338 data->in_status[i]);
1339 if ((data->in_status[i] & 0x81) == 0x81) {
1340 data->in[i] = pc87360_read_value(data, LD_IN,
1341 i, PC87365_REG_IN);
1342 }
1343 if (data->in_status[i] & 0x01) {
1344 data->in_min[i] = pc87360_read_value(data,
1345 LD_IN, i,
1346 PC87365_REG_IN_MIN);
1347 data->in_max[i] = pc87360_read_value(data,
1348 LD_IN, i,
1349 PC87365_REG_IN_MAX);
1350 if (i >= 11)
1351 data->in_crit[i-11] =
1352 pc87360_read_value(data, LD_IN,
1353 i, PC87365_REG_TEMP_CRIT);
1354 }
1355 }
1356 if (data->innr) {
1357 data->in_alarms = pc87360_read_value(data, LD_IN,
1358 NO_BANK, PC87365_REG_IN_ALARMS1)
1359 | ((pc87360_read_value(data, LD_IN,
1360 NO_BANK, PC87365_REG_IN_ALARMS2)
1361 & 0x07) << 8);
1362 data->vid = (data->vid_conf & 0xE0) ?
1363 pc87360_read_value(data, LD_IN,
1364 NO_BANK, PC87365_REG_VID) : 0x1F;
1365 }
1366
1367 /* Temperatures */
1368 for (i = 0; i < data->tempnr; i++) {
1369 data->temp_status[i] = pc87360_read_value(data,
1370 LD_TEMP, i,
1371 PC87365_REG_TEMP_STATUS);
1372 /* Clear bits */
1373 pc87360_write_value(data, LD_TEMP, i,
1374 PC87365_REG_TEMP_STATUS,
1375 data->temp_status[i]);
1376 if ((data->temp_status[i] & 0x81) == 0x81) {
1377 data->temp[i] = pc87360_read_value(data,
1378 LD_TEMP, i,
1379 PC87365_REG_TEMP);
1380 }
1381 if (data->temp_status[i] & 0x01) {
1382 data->temp_min[i] = pc87360_read_value(data,
1383 LD_TEMP, i,
1384 PC87365_REG_TEMP_MIN);
1385 data->temp_max[i] = pc87360_read_value(data,
1386 LD_TEMP, i,
1387 PC87365_REG_TEMP_MAX);
1388 data->temp_crit[i] = pc87360_read_value(data,
1389 LD_TEMP, i,
1390 PC87365_REG_TEMP_CRIT);
1391 }
1392 }
1393 if (data->tempnr) {
1394 data->temp_alarms = pc87360_read_value(data, LD_TEMP,
1395 NO_BANK, PC87365_REG_TEMP_ALARMS)
1396 & 0x3F;
1397 }
1398
1399 data->last_updated = jiffies;
1400 data->valid = 1;
1401 }
1402
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001403 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404
1405 return data;
1406}
1407
Jean Delvaref641b582007-06-09 10:11:16 -04001408static int __init pc87360_device_add(unsigned short address)
1409{
1410 struct resource res = {
1411 .name = "pc87360",
1412 .flags = IORESOURCE_IO,
1413 };
1414 int err, i;
1415
1416 pdev = platform_device_alloc("pc87360", address);
1417 if (!pdev) {
1418 err = -ENOMEM;
1419 printk(KERN_ERR "pc87360: Device allocation failed\n");
1420 goto exit;
1421 }
1422
1423 for (i = 0; i < 3; i++) {
1424 if (!extra_isa[i])
1425 continue;
1426 res.start = extra_isa[i];
1427 res.end = extra_isa[i] + PC87360_EXTENT - 1;
1428 err = platform_device_add_resources(pdev, &res, 1);
1429 if (err) {
1430 printk(KERN_ERR "pc87360: Device resource[%d] "
1431 "addition failed (%d)\n", i, err);
1432 goto exit_device_put;
1433 }
1434 }
1435
1436 err = platform_device_add(pdev);
1437 if (err) {
1438 printk(KERN_ERR "pc87360: Device addition failed (%d)\n",
1439 err);
1440 goto exit_device_put;
1441 }
1442
1443 return 0;
1444
1445exit_device_put:
1446 platform_device_put(pdev);
1447exit:
1448 return err;
1449}
1450
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451static int __init pc87360_init(void)
1452{
Jean Delvaref641b582007-06-09 10:11:16 -04001453 int err, i;
1454 unsigned short address = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455
1456 if (pc87360_find(0x2e, &devid, extra_isa)
1457 && pc87360_find(0x4e, &devid, extra_isa)) {
1458 printk(KERN_WARNING "pc87360: PC8736x not detected, "
1459 "module not inserted.\n");
1460 return -ENODEV;
1461 }
1462
1463 /* Arbitrarily pick one of the addresses */
1464 for (i = 0; i < 3; i++) {
1465 if (extra_isa[i] != 0x0000) {
Jean Delvare2d8672c2005-07-19 23:56:35 +02001466 address = extra_isa[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 break;
1468 }
1469 }
1470
Jean Delvare2d8672c2005-07-19 23:56:35 +02001471 if (address == 0x0000) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 printk(KERN_WARNING "pc87360: No active logical device, "
1473 "module not inserted.\n");
1474 return -ENODEV;
1475 }
1476
Jean Delvaref641b582007-06-09 10:11:16 -04001477 err = platform_driver_register(&pc87360_driver);
1478 if (err)
1479 goto exit;
1480
1481 /* Sets global pdev as a side effect */
1482 err = pc87360_device_add(address);
1483 if (err)
1484 goto exit_driver;
1485
1486 return 0;
1487
1488 exit_driver:
1489 platform_driver_unregister(&pc87360_driver);
1490 exit:
1491 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492}
1493
1494static void __exit pc87360_exit(void)
1495{
Jean Delvaref641b582007-06-09 10:11:16 -04001496 platform_device_unregister(pdev);
1497 platform_driver_unregister(&pc87360_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498}
1499
1500
1501MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
1502MODULE_DESCRIPTION("PC8736x hardware monitor");
1503MODULE_LICENSE("GPL");
1504
1505module_init(pc87360_init);
1506module_exit(pc87360_exit);