blob: cb72526c346a3e04af035503400ff4ae5fdfcbd4 [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
62/*
63 * Super-I/O registers and operations
64 */
65
66#define DEV 0x07 /* Register: Logical device select */
67#define DEVID 0x20 /* Register: Device ID */
68#define ACT 0x30 /* Register: Device activation */
69#define BASE 0x60 /* Register: Base address */
70
71#define FSCM 0x09 /* Logical device: fans */
72#define VLM 0x0d /* Logical device: voltages */
73#define TMS 0x0e /* Logical device: temperatures */
74static const u8 logdev[3] = { FSCM, VLM, TMS };
75
76#define LD_FAN 0
77#define LD_IN 1
78#define LD_TEMP 2
79
80static inline void superio_outb(int sioaddr, int reg, int val)
81{
82 outb(reg, sioaddr);
83 outb(val, sioaddr+1);
84}
85
86static inline int superio_inb(int sioaddr, int reg)
87{
88 outb(reg, sioaddr);
89 return inb(sioaddr+1);
90}
91
92static inline void superio_exit(int sioaddr)
93{
94 outb(0x02, sioaddr);
95 outb(0x02, sioaddr+1);
96}
97
98/*
99 * Logical devices
100 */
101
102#define PC87360_EXTENT 0x10
103#define PC87365_REG_BANK 0x09
104#define NO_BANK 0xff
105
106/*
107 * Fan registers and conversions
108 */
109
110/* nr has to be 0 or 1 (PC87360/87363) or 2 (PC87364/87365/87366) */
111#define PC87360_REG_PRESCALE(nr) (0x00 + 2 * (nr))
112#define PC87360_REG_PWM(nr) (0x01 + 2 * (nr))
113#define PC87360_REG_FAN_MIN(nr) (0x06 + 3 * (nr))
114#define PC87360_REG_FAN(nr) (0x07 + 3 * (nr))
115#define PC87360_REG_FAN_STATUS(nr) (0x08 + 3 * (nr))
116
117#define FAN_FROM_REG(val,div) ((val) == 0 ? 0: \
118 480000 / ((val)*(div)))
119#define FAN_TO_REG(val,div) ((val) <= 100 ? 0 : \
120 480000 / ((val)*(div)))
121#define FAN_DIV_FROM_REG(val) (1 << ((val >> 5) & 0x03))
122#define FAN_STATUS_FROM_REG(val) ((val) & 0x07)
123
124#define FAN_CONFIG_MONITOR(val,nr) (((val) >> (2 + nr * 3)) & 1)
125#define FAN_CONFIG_CONTROL(val,nr) (((val) >> (3 + nr * 3)) & 1)
126#define FAN_CONFIG_INVERT(val,nr) (((val) >> (4 + nr * 3)) & 1)
127
128#define PWM_FROM_REG(val,inv) ((inv) ? 255 - (val) : (val))
129static inline u8 PWM_TO_REG(int val, int inv)
130{
131 if (inv)
132 val = 255 - val;
133 if (val < 0)
134 return 0;
135 if (val > 255)
136 return 255;
137 return val;
138}
139
140/*
141 * Voltage registers and conversions
142 */
143
144#define PC87365_REG_IN_CONVRATE 0x07
145#define PC87365_REG_IN_CONFIG 0x08
146#define PC87365_REG_IN 0x0B
147#define PC87365_REG_IN_MIN 0x0D
148#define PC87365_REG_IN_MAX 0x0C
149#define PC87365_REG_IN_STATUS 0x0A
150#define PC87365_REG_IN_ALARMS1 0x00
151#define PC87365_REG_IN_ALARMS2 0x01
152#define PC87365_REG_VID 0x06
153
154#define IN_FROM_REG(val,ref) (((val) * (ref) + 128) / 256)
155#define IN_TO_REG(val,ref) ((val) < 0 ? 0 : \
156 (val)*256 >= (ref)*255 ? 255: \
157 ((val) * 256 + (ref)/2) / (ref))
158
159/*
160 * Temperature registers and conversions
161 */
162
163#define PC87365_REG_TEMP_CONFIG 0x08
164#define PC87365_REG_TEMP 0x0B
165#define PC87365_REG_TEMP_MIN 0x0D
166#define PC87365_REG_TEMP_MAX 0x0C
167#define PC87365_REG_TEMP_CRIT 0x0E
168#define PC87365_REG_TEMP_STATUS 0x0A
169#define PC87365_REG_TEMP_ALARMS 0x00
170
171#define TEMP_FROM_REG(val) ((val) * 1000)
172#define TEMP_TO_REG(val) ((val) < -55000 ? -55 : \
173 (val) > 127000 ? 127 : \
174 (val) < 0 ? ((val) - 500) / 1000 : \
175 ((val) + 500) / 1000)
176
177/*
Jean Delvaref641b582007-06-09 10:11:16 -0400178 * Device data
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 */
180
181struct pc87360_data {
Jean Delvaref641b582007-06-09 10:11:16 -0400182 const char *name;
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400183 struct class_device *class_dev;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100184 struct mutex lock;
185 struct mutex update_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 char valid; /* !=0 if following fields are valid */
187 unsigned long last_updated; /* In jiffies */
188
189 int address[3];
190
191 u8 fannr, innr, tempnr;
192
193 u8 fan[3]; /* Register value */
194 u8 fan_min[3]; /* Register value */
195 u8 fan_status[3]; /* Register value */
196 u8 pwm[3]; /* Register value */
197 u16 fan_conf; /* Configuration register values, combined */
198
199 u16 in_vref; /* 1 mV/bit */
200 u8 in[14]; /* Register value */
201 u8 in_min[14]; /* Register value */
202 u8 in_max[14]; /* Register value */
203 u8 in_crit[3]; /* Register value */
204 u8 in_status[14]; /* Register value */
205 u16 in_alarms; /* Register values, combined, masked */
206 u8 vid_conf; /* Configuration register value */
207 u8 vrm;
208 u8 vid; /* Register value */
209
210 s8 temp[3]; /* Register value */
211 s8 temp_min[3]; /* Register value */
212 s8 temp_max[3]; /* Register value */
213 s8 temp_crit[3]; /* Register value */
214 u8 temp_status[3]; /* Register value */
215 u8 temp_alarms; /* Register value, masked */
216};
217
218/*
219 * Functions declaration
220 */
221
Jean Delvaref641b582007-06-09 10:11:16 -0400222static int pc87360_probe(struct platform_device *pdev);
223static int pc87360_remove(struct platform_device *pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
225static int pc87360_read_value(struct pc87360_data *data, u8 ldi, u8 bank,
226 u8 reg);
227static void pc87360_write_value(struct pc87360_data *data, u8 ldi, u8 bank,
228 u8 reg, u8 value);
Jean Delvaref641b582007-06-09 10:11:16 -0400229static void pc87360_init_device(struct platform_device *pdev,
230 int use_thermistors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231static struct pc87360_data *pc87360_update_device(struct device *dev);
232
233/*
Jean Delvaref641b582007-06-09 10:11:16 -0400234 * Driver data
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 */
236
Jean Delvaref641b582007-06-09 10:11:16 -0400237static struct platform_driver pc87360_driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100238 .driver = {
Jean Delvare87218842006-09-03 22:36:14 +0200239 .owner = THIS_MODULE,
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100240 .name = "pc87360",
241 },
Jean Delvaref641b582007-06-09 10:11:16 -0400242 .probe = pc87360_probe,
243 .remove = __devexit_p(pc87360_remove),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244};
245
246/*
247 * Sysfs stuff
248 */
249
Jim Cromief0986bd2005-09-02 22:52:43 +0200250static ssize_t show_fan_input(struct device *dev, struct device_attribute *devattr, char *buf)
251{
252 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
253 struct pc87360_data *data = pc87360_update_device(dev);
Jim Cromie694fa052005-09-02 22:57:52 +0200254 return sprintf(buf, "%u\n", FAN_FROM_REG(data->fan[attr->index],
255 FAN_DIV_FROM_REG(data->fan_status[attr->index])));
Jim Cromief0986bd2005-09-02 22:52:43 +0200256}
257static ssize_t show_fan_min(struct device *dev, struct device_attribute *devattr, char *buf)
258{
259 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
260 struct pc87360_data *data = pc87360_update_device(dev);
Jim Cromie694fa052005-09-02 22:57:52 +0200261 return sprintf(buf, "%u\n", FAN_FROM_REG(data->fan_min[attr->index],
262 FAN_DIV_FROM_REG(data->fan_status[attr->index])));
Jim Cromief0986bd2005-09-02 22:52:43 +0200263}
264static ssize_t show_fan_div(struct device *dev, struct device_attribute *devattr, char *buf)
265{
266 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
267 struct pc87360_data *data = pc87360_update_device(dev);
268 return sprintf(buf, "%u\n",
Jim Cromie694fa052005-09-02 22:57:52 +0200269 FAN_DIV_FROM_REG(data->fan_status[attr->index]));
Jim Cromief0986bd2005-09-02 22:52:43 +0200270}
271static ssize_t show_fan_status(struct device *dev, struct device_attribute *devattr, char *buf)
272{
273 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
274 struct pc87360_data *data = pc87360_update_device(dev);
275 return sprintf(buf, "%u\n",
Jim Cromie694fa052005-09-02 22:57:52 +0200276 FAN_STATUS_FROM_REG(data->fan_status[attr->index]));
Jim Cromief0986bd2005-09-02 22:52:43 +0200277}
278static ssize_t set_fan_min(struct device *dev, struct device_attribute *devattr, const char *buf,
279 size_t count)
280{
281 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Jean Delvaref641b582007-06-09 10:11:16 -0400282 struct pc87360_data *data = dev_get_drvdata(dev);
Jim Cromie11be27e2005-09-02 23:05:07 +0200283 long fan_min = simple_strtol(buf, NULL, 10);
284
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100285 mutex_lock(&data->update_lock);
Jim Cromie11be27e2005-09-02 23:05:07 +0200286 fan_min = FAN_TO_REG(fan_min, FAN_DIV_FROM_REG(data->fan_status[attr->index]));
287
288 /* If it wouldn't fit, change clock divisor */
289 while (fan_min > 255
290 && (data->fan_status[attr->index] & 0x60) != 0x60) {
291 fan_min >>= 1;
292 data->fan[attr->index] >>= 1;
293 data->fan_status[attr->index] += 0x20;
294 }
295 data->fan_min[attr->index] = fan_min > 255 ? 255 : fan_min;
296 pc87360_write_value(data, LD_FAN, NO_BANK, PC87360_REG_FAN_MIN(attr->index),
297 data->fan_min[attr->index]);
298
299 /* Write new divider, preserve alarm bits */
300 pc87360_write_value(data, LD_FAN, NO_BANK, PC87360_REG_FAN_STATUS(attr->index),
301 data->fan_status[attr->index] & 0xF9);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100302 mutex_unlock(&data->update_lock);
Jim Cromie11be27e2005-09-02 23:05:07 +0200303
304 return count;
Jim Cromief0986bd2005-09-02 22:52:43 +0200305}
306
Jim Cromiededc6a72006-01-09 23:24:41 +0100307static struct sensor_device_attribute fan_input[] = {
308 SENSOR_ATTR(fan1_input, S_IRUGO, show_fan_input, NULL, 0),
309 SENSOR_ATTR(fan2_input, S_IRUGO, show_fan_input, NULL, 1),
310 SENSOR_ATTR(fan3_input, S_IRUGO, show_fan_input, NULL, 2),
311};
312static struct sensor_device_attribute fan_status[] = {
313 SENSOR_ATTR(fan1_status, S_IRUGO, show_fan_status, NULL, 0),
314 SENSOR_ATTR(fan2_status, S_IRUGO, show_fan_status, NULL, 1),
315 SENSOR_ATTR(fan3_status, S_IRUGO, show_fan_status, NULL, 2),
316};
317static struct sensor_device_attribute fan_div[] = {
318 SENSOR_ATTR(fan1_div, S_IRUGO, show_fan_div, NULL, 0),
319 SENSOR_ATTR(fan2_div, S_IRUGO, show_fan_div, NULL, 1),
320 SENSOR_ATTR(fan3_div, S_IRUGO, show_fan_div, NULL, 2),
321};
322static struct sensor_device_attribute fan_min[] = {
323 SENSOR_ATTR(fan1_min, S_IWUSR | S_IRUGO, show_fan_min, set_fan_min, 0),
324 SENSOR_ATTR(fan2_min, S_IWUSR | S_IRUGO, show_fan_min, set_fan_min, 1),
325 SENSOR_ATTR(fan3_min, S_IWUSR | S_IRUGO, show_fan_min, set_fan_min, 2),
326};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
Jim Cromie941c5c02006-09-24 21:02:44 +0200328#define FAN_UNIT_ATTRS(X) \
329 &fan_input[X].dev_attr.attr, \
330 &fan_status[X].dev_attr.attr, \
331 &fan_div[X].dev_attr.attr, \
332 &fan_min[X].dev_attr.attr
333
Jim Cromief0986bd2005-09-02 22:52:43 +0200334static ssize_t show_pwm(struct device *dev, struct device_attribute *devattr, char *buf)
335{
336 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
337 struct pc87360_data *data = pc87360_update_device(dev);
338 return sprintf(buf, "%u\n",
Jim Cromie694fa052005-09-02 22:57:52 +0200339 PWM_FROM_REG(data->pwm[attr->index],
Jim Cromief0986bd2005-09-02 22:52:43 +0200340 FAN_CONFIG_INVERT(data->fan_conf,
Jim Cromie694fa052005-09-02 22:57:52 +0200341 attr->index)));
Jim Cromief0986bd2005-09-02 22:52:43 +0200342}
343static ssize_t set_pwm(struct device *dev, struct device_attribute *devattr, const char *buf,
344 size_t count)
345{
346 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Jean Delvaref641b582007-06-09 10:11:16 -0400347 struct pc87360_data *data = dev_get_drvdata(dev);
Jim Cromief0986bd2005-09-02 22:52:43 +0200348 long val = simple_strtol(buf, NULL, 10);
349
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100350 mutex_lock(&data->update_lock);
Jim Cromie694fa052005-09-02 22:57:52 +0200351 data->pwm[attr->index] = PWM_TO_REG(val,
352 FAN_CONFIG_INVERT(data->fan_conf, attr->index));
353 pc87360_write_value(data, LD_FAN, NO_BANK, PC87360_REG_PWM(attr->index),
354 data->pwm[attr->index]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100355 mutex_unlock(&data->update_lock);
Jim Cromief0986bd2005-09-02 22:52:43 +0200356 return count;
357}
358
Jim Cromiededc6a72006-01-09 23:24:41 +0100359static struct sensor_device_attribute pwm[] = {
360 SENSOR_ATTR(pwm1, S_IWUSR | S_IRUGO, show_pwm, set_pwm, 0),
361 SENSOR_ATTR(pwm2, S_IWUSR | S_IRUGO, show_pwm, set_pwm, 1),
362 SENSOR_ATTR(pwm3, S_IWUSR | S_IRUGO, show_pwm, set_pwm, 2),
363};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
Jim Cromie941c5c02006-09-24 21:02:44 +0200365static struct attribute * pc8736x_fan_attr_array[] = {
366 FAN_UNIT_ATTRS(0),
367 FAN_UNIT_ATTRS(1),
368 FAN_UNIT_ATTRS(2),
369 &pwm[0].dev_attr.attr,
370 &pwm[1].dev_attr.attr,
371 &pwm[2].dev_attr.attr,
372 NULL
373};
374static const struct attribute_group pc8736x_fan_group = {
375 .attrs = pc8736x_fan_attr_array,
376};
377
Jim Cromief0986bd2005-09-02 22:52:43 +0200378static ssize_t show_in_input(struct device *dev, struct device_attribute *devattr, char *buf)
379{
380 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
381 struct pc87360_data *data = pc87360_update_device(dev);
382 return sprintf(buf, "%u\n", IN_FROM_REG(data->in[attr->index],
383 data->in_vref));
384}
385static ssize_t show_in_min(struct device *dev, struct device_attribute *devattr, char *buf)
386{
387 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
388 struct pc87360_data *data = pc87360_update_device(dev);
389 return sprintf(buf, "%u\n", IN_FROM_REG(data->in_min[attr->index],
390 data->in_vref));
391}
392static ssize_t show_in_max(struct device *dev, struct device_attribute *devattr, char *buf)
393{
394 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
395 struct pc87360_data *data = pc87360_update_device(dev);
396 return sprintf(buf, "%u\n", IN_FROM_REG(data->in_max[attr->index],
397 data->in_vref));
398}
399static ssize_t show_in_status(struct device *dev, struct device_attribute *devattr, char *buf)
400{
401 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
402 struct pc87360_data *data = pc87360_update_device(dev);
403 return sprintf(buf, "%u\n", data->in_status[attr->index]);
404}
405static ssize_t set_in_min(struct device *dev, struct device_attribute *devattr, const char *buf,
406 size_t count)
407{
408 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Jean Delvaref641b582007-06-09 10:11:16 -0400409 struct pc87360_data *data = dev_get_drvdata(dev);
Jim Cromief0986bd2005-09-02 22:52:43 +0200410 long val = simple_strtol(buf, NULL, 10);
411
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100412 mutex_lock(&data->update_lock);
Jim Cromief0986bd2005-09-02 22:52:43 +0200413 data->in_min[attr->index] = IN_TO_REG(val, data->in_vref);
414 pc87360_write_value(data, LD_IN, attr->index, PC87365_REG_IN_MIN,
415 data->in_min[attr->index]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100416 mutex_unlock(&data->update_lock);
Jim Cromief0986bd2005-09-02 22:52:43 +0200417 return count;
418}
419static ssize_t set_in_max(struct device *dev, struct device_attribute *devattr, const char *buf,
420 size_t count)
421{
422 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Jean Delvaref641b582007-06-09 10:11:16 -0400423 struct pc87360_data *data = dev_get_drvdata(dev);
Jim Cromief0986bd2005-09-02 22:52:43 +0200424 long val = simple_strtol(buf, NULL, 10);
425
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100426 mutex_lock(&data->update_lock);
Jim Cromief0986bd2005-09-02 22:52:43 +0200427 data->in_max[attr->index] = IN_TO_REG(val,
428 data->in_vref);
429 pc87360_write_value(data, LD_IN, attr->index, PC87365_REG_IN_MAX,
430 data->in_max[attr->index]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100431 mutex_unlock(&data->update_lock);
Jim Cromief0986bd2005-09-02 22:52:43 +0200432 return count;
433}
434
Jim Cromiededc6a72006-01-09 23:24:41 +0100435static struct sensor_device_attribute in_input[] = {
436 SENSOR_ATTR(in0_input, S_IRUGO, show_in_input, NULL, 0),
437 SENSOR_ATTR(in1_input, S_IRUGO, show_in_input, NULL, 1),
438 SENSOR_ATTR(in2_input, S_IRUGO, show_in_input, NULL, 2),
439 SENSOR_ATTR(in3_input, S_IRUGO, show_in_input, NULL, 3),
440 SENSOR_ATTR(in4_input, S_IRUGO, show_in_input, NULL, 4),
441 SENSOR_ATTR(in5_input, S_IRUGO, show_in_input, NULL, 5),
442 SENSOR_ATTR(in6_input, S_IRUGO, show_in_input, NULL, 6),
443 SENSOR_ATTR(in7_input, S_IRUGO, show_in_input, NULL, 7),
444 SENSOR_ATTR(in8_input, S_IRUGO, show_in_input, NULL, 8),
445 SENSOR_ATTR(in9_input, S_IRUGO, show_in_input, NULL, 9),
446 SENSOR_ATTR(in10_input, S_IRUGO, show_in_input, NULL, 10),
447};
448static struct sensor_device_attribute in_status[] = {
449 SENSOR_ATTR(in0_status, S_IRUGO, show_in_status, NULL, 0),
450 SENSOR_ATTR(in1_status, S_IRUGO, show_in_status, NULL, 1),
451 SENSOR_ATTR(in2_status, S_IRUGO, show_in_status, NULL, 2),
452 SENSOR_ATTR(in3_status, S_IRUGO, show_in_status, NULL, 3),
453 SENSOR_ATTR(in4_status, S_IRUGO, show_in_status, NULL, 4),
454 SENSOR_ATTR(in5_status, S_IRUGO, show_in_status, NULL, 5),
455 SENSOR_ATTR(in6_status, S_IRUGO, show_in_status, NULL, 6),
456 SENSOR_ATTR(in7_status, S_IRUGO, show_in_status, NULL, 7),
457 SENSOR_ATTR(in8_status, S_IRUGO, show_in_status, NULL, 8),
458 SENSOR_ATTR(in9_status, S_IRUGO, show_in_status, NULL, 9),
459 SENSOR_ATTR(in10_status, S_IRUGO, show_in_status, NULL, 10),
460};
461static struct sensor_device_attribute in_min[] = {
462 SENSOR_ATTR(in0_min, S_IWUSR | S_IRUGO, show_in_min, set_in_min, 0),
463 SENSOR_ATTR(in1_min, S_IWUSR | S_IRUGO, show_in_min, set_in_min, 1),
464 SENSOR_ATTR(in2_min, S_IWUSR | S_IRUGO, show_in_min, set_in_min, 2),
465 SENSOR_ATTR(in3_min, S_IWUSR | S_IRUGO, show_in_min, set_in_min, 3),
466 SENSOR_ATTR(in4_min, S_IWUSR | S_IRUGO, show_in_min, set_in_min, 4),
467 SENSOR_ATTR(in5_min, S_IWUSR | S_IRUGO, show_in_min, set_in_min, 5),
468 SENSOR_ATTR(in6_min, S_IWUSR | S_IRUGO, show_in_min, set_in_min, 6),
469 SENSOR_ATTR(in7_min, S_IWUSR | S_IRUGO, show_in_min, set_in_min, 7),
470 SENSOR_ATTR(in8_min, S_IWUSR | S_IRUGO, show_in_min, set_in_min, 8),
471 SENSOR_ATTR(in9_min, S_IWUSR | S_IRUGO, show_in_min, set_in_min, 9),
472 SENSOR_ATTR(in10_min, S_IWUSR | S_IRUGO, show_in_min, set_in_min, 10),
473};
474static struct sensor_device_attribute in_max[] = {
475 SENSOR_ATTR(in0_max, S_IWUSR | S_IRUGO, show_in_max, set_in_max, 0),
476 SENSOR_ATTR(in1_max, S_IWUSR | S_IRUGO, show_in_max, set_in_max, 1),
477 SENSOR_ATTR(in2_max, S_IWUSR | S_IRUGO, show_in_max, set_in_max, 2),
478 SENSOR_ATTR(in3_max, S_IWUSR | S_IRUGO, show_in_max, set_in_max, 3),
479 SENSOR_ATTR(in4_max, S_IWUSR | S_IRUGO, show_in_max, set_in_max, 4),
480 SENSOR_ATTR(in5_max, S_IWUSR | S_IRUGO, show_in_max, set_in_max, 5),
481 SENSOR_ATTR(in6_max, S_IWUSR | S_IRUGO, show_in_max, set_in_max, 6),
482 SENSOR_ATTR(in7_max, S_IWUSR | S_IRUGO, show_in_max, set_in_max, 7),
483 SENSOR_ATTR(in8_max, S_IWUSR | S_IRUGO, show_in_max, set_in_max, 8),
484 SENSOR_ATTR(in9_max, S_IWUSR | S_IRUGO, show_in_max, set_in_max, 9),
485 SENSOR_ATTR(in10_max, S_IWUSR | S_IRUGO, show_in_max, set_in_max, 10),
486};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
Jim Cromie941c5c02006-09-24 21:02:44 +0200488#define VIN_UNIT_ATTRS(X) \
489 &in_input[X].dev_attr.attr, \
490 &in_status[X].dev_attr.attr, \
491 &in_min[X].dev_attr.attr, \
492 &in_max[X].dev_attr.attr
493
Jim Cromie44646c12006-09-24 21:01:56 +0200494static ssize_t show_vid(struct device *dev, struct device_attribute *attr, char *buf)
495{
496 struct pc87360_data *data = pc87360_update_device(dev);
497 return sprintf(buf, "%u\n", vid_from_reg(data->vid, data->vrm));
498}
499static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL);
500
501static ssize_t show_vrm(struct device *dev, struct device_attribute *attr, char *buf)
502{
503 struct pc87360_data *data = pc87360_update_device(dev);
504 return sprintf(buf, "%u\n", data->vrm);
505}
506static ssize_t set_vrm(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
507{
Jean Delvaref641b582007-06-09 10:11:16 -0400508 struct pc87360_data *data = dev_get_drvdata(dev);
Jim Cromie44646c12006-09-24 21:01:56 +0200509 data->vrm = simple_strtoul(buf, NULL, 10);
510 return count;
511}
512static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm, set_vrm);
513
514static ssize_t show_in_alarms(struct device *dev, struct device_attribute *attr, char *buf)
515{
516 struct pc87360_data *data = pc87360_update_device(dev);
517 return sprintf(buf, "%u\n", data->in_alarms);
518}
519static DEVICE_ATTR(alarms_in, S_IRUGO, show_in_alarms, NULL);
520
Jim Cromie941c5c02006-09-24 21:02:44 +0200521static struct attribute *pc8736x_vin_attr_array[] = {
522 VIN_UNIT_ATTRS(0),
523 VIN_UNIT_ATTRS(1),
524 VIN_UNIT_ATTRS(2),
525 VIN_UNIT_ATTRS(3),
526 VIN_UNIT_ATTRS(4),
527 VIN_UNIT_ATTRS(5),
528 VIN_UNIT_ATTRS(6),
529 VIN_UNIT_ATTRS(7),
530 VIN_UNIT_ATTRS(8),
531 VIN_UNIT_ATTRS(9),
532 VIN_UNIT_ATTRS(10),
533 &dev_attr_cpu0_vid.attr,
534 &dev_attr_vrm.attr,
535 &dev_attr_alarms_in.attr,
536 NULL
537};
538static const struct attribute_group pc8736x_vin_group = {
539 .attrs = pc8736x_vin_attr_array,
540};
541
Jim Cromief0986bd2005-09-02 22:52:43 +0200542static ssize_t show_therm_input(struct device *dev, struct device_attribute *devattr, char *buf)
543{
544 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
545 struct pc87360_data *data = pc87360_update_device(dev);
Jim Cromie694fa052005-09-02 22:57:52 +0200546 return sprintf(buf, "%u\n", IN_FROM_REG(data->in[attr->index],
Jim Cromief0986bd2005-09-02 22:52:43 +0200547 data->in_vref));
548}
549static ssize_t show_therm_min(struct device *dev, struct device_attribute *devattr, char *buf)
550{
551 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
552 struct pc87360_data *data = pc87360_update_device(dev);
Jim Cromie694fa052005-09-02 22:57:52 +0200553 return sprintf(buf, "%u\n", IN_FROM_REG(data->in_min[attr->index],
Jim Cromief0986bd2005-09-02 22:52:43 +0200554 data->in_vref));
555}
556static ssize_t show_therm_max(struct device *dev, struct device_attribute *devattr, char *buf)
557{
558 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
559 struct pc87360_data *data = pc87360_update_device(dev);
Jim Cromie694fa052005-09-02 22:57:52 +0200560 return sprintf(buf, "%u\n", IN_FROM_REG(data->in_max[attr->index],
Jim Cromief0986bd2005-09-02 22:52:43 +0200561 data->in_vref));
562}
563static ssize_t show_therm_crit(struct device *dev, struct device_attribute *devattr, char *buf)
564{
565 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
566 struct pc87360_data *data = pc87360_update_device(dev);
Jim Cromie694fa052005-09-02 22:57:52 +0200567 return sprintf(buf, "%u\n", IN_FROM_REG(data->in_crit[attr->index-11],
Jim Cromief0986bd2005-09-02 22:52:43 +0200568 data->in_vref));
569}
570static ssize_t show_therm_status(struct device *dev, struct device_attribute *devattr, char *buf)
571{
572 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
573 struct pc87360_data *data = pc87360_update_device(dev);
Jim Cromie694fa052005-09-02 22:57:52 +0200574 return sprintf(buf, "%u\n", data->in_status[attr->index]);
Jim Cromief0986bd2005-09-02 22:52:43 +0200575}
576static ssize_t set_therm_min(struct device *dev, struct device_attribute *devattr, const char *buf,
577 size_t count)
578{
579 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Jean Delvaref641b582007-06-09 10:11:16 -0400580 struct pc87360_data *data = dev_get_drvdata(dev);
Jim Cromief0986bd2005-09-02 22:52:43 +0200581 long val = simple_strtol(buf, NULL, 10);
582
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100583 mutex_lock(&data->update_lock);
Jim Cromie694fa052005-09-02 22:57:52 +0200584 data->in_min[attr->index] = IN_TO_REG(val, data->in_vref);
585 pc87360_write_value(data, LD_IN, attr->index, PC87365_REG_TEMP_MIN,
586 data->in_min[attr->index]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100587 mutex_unlock(&data->update_lock);
Jim Cromief0986bd2005-09-02 22:52:43 +0200588 return count;
589}
590static ssize_t set_therm_max(struct device *dev, struct device_attribute *devattr, const char *buf,
591 size_t count)
592{
593 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Jean Delvaref641b582007-06-09 10:11:16 -0400594 struct pc87360_data *data = dev_get_drvdata(dev);
Jim Cromief0986bd2005-09-02 22:52:43 +0200595 long val = simple_strtol(buf, NULL, 10);
596
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100597 mutex_lock(&data->update_lock);
Jim Cromie694fa052005-09-02 22:57:52 +0200598 data->in_max[attr->index] = IN_TO_REG(val, data->in_vref);
599 pc87360_write_value(data, LD_IN, attr->index, PC87365_REG_TEMP_MAX,
600 data->in_max[attr->index]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100601 mutex_unlock(&data->update_lock);
Jim Cromief0986bd2005-09-02 22:52:43 +0200602 return count;
603}
604static ssize_t set_therm_crit(struct device *dev, struct device_attribute *devattr, const char *buf,
605 size_t count)
606{
607 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Jean Delvaref641b582007-06-09 10:11:16 -0400608 struct pc87360_data *data = dev_get_drvdata(dev);
Jim Cromief0986bd2005-09-02 22:52:43 +0200609 long val = simple_strtol(buf, NULL, 10);
610
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100611 mutex_lock(&data->update_lock);
Jim Cromie694fa052005-09-02 22:57:52 +0200612 data->in_crit[attr->index-11] = IN_TO_REG(val, data->in_vref);
613 pc87360_write_value(data, LD_IN, attr->index, PC87365_REG_TEMP_CRIT,
614 data->in_crit[attr->index-11]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100615 mutex_unlock(&data->update_lock);
Jim Cromief0986bd2005-09-02 22:52:43 +0200616 return count;
617}
618
Jim Cromiededc6a72006-01-09 23:24:41 +0100619/* the +11 term below reflects the fact that VLM units 11,12,13 are
620 used in the chip to measure voltage across the thermistors
621*/
622static struct sensor_device_attribute therm_input[] = {
623 SENSOR_ATTR(temp4_input, S_IRUGO, show_therm_input, NULL, 0+11),
624 SENSOR_ATTR(temp5_input, S_IRUGO, show_therm_input, NULL, 1+11),
625 SENSOR_ATTR(temp6_input, S_IRUGO, show_therm_input, NULL, 2+11),
626};
627static struct sensor_device_attribute therm_status[] = {
628 SENSOR_ATTR(temp4_status, S_IRUGO, show_therm_status, NULL, 0+11),
629 SENSOR_ATTR(temp5_status, S_IRUGO, show_therm_status, NULL, 1+11),
630 SENSOR_ATTR(temp6_status, S_IRUGO, show_therm_status, NULL, 2+11),
631};
632static struct sensor_device_attribute therm_min[] = {
633 SENSOR_ATTR(temp4_min, S_IRUGO | S_IWUSR,
634 show_therm_min, set_therm_min, 0+11),
635 SENSOR_ATTR(temp5_min, S_IRUGO | S_IWUSR,
636 show_therm_min, set_therm_min, 1+11),
637 SENSOR_ATTR(temp6_min, S_IRUGO | S_IWUSR,
638 show_therm_min, set_therm_min, 2+11),
639};
640static struct sensor_device_attribute therm_max[] = {
641 SENSOR_ATTR(temp4_max, S_IRUGO | S_IWUSR,
642 show_therm_max, set_therm_max, 0+11),
643 SENSOR_ATTR(temp5_max, S_IRUGO | S_IWUSR,
644 show_therm_max, set_therm_max, 1+11),
645 SENSOR_ATTR(temp6_max, S_IRUGO | S_IWUSR,
646 show_therm_max, set_therm_max, 2+11),
647};
648static struct sensor_device_attribute therm_crit[] = {
649 SENSOR_ATTR(temp4_crit, S_IRUGO | S_IWUSR,
650 show_therm_crit, set_therm_crit, 0+11),
651 SENSOR_ATTR(temp5_crit, S_IRUGO | S_IWUSR,
652 show_therm_crit, set_therm_crit, 1+11),
653 SENSOR_ATTR(temp6_crit, S_IRUGO | S_IWUSR,
654 show_therm_crit, set_therm_crit, 2+11),
655};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
Jim Cromie941c5c02006-09-24 21:02:44 +0200657#define THERM_UNIT_ATTRS(X) \
658 &therm_input[X].dev_attr.attr, \
659 &therm_status[X].dev_attr.attr, \
660 &therm_min[X].dev_attr.attr, \
661 &therm_max[X].dev_attr.attr, \
662 &therm_crit[X].dev_attr.attr
663
664static struct attribute * pc8736x_therm_attr_array[] = {
665 THERM_UNIT_ATTRS(0),
666 THERM_UNIT_ATTRS(1),
667 THERM_UNIT_ATTRS(2),
668 NULL
669};
670static const struct attribute_group pc8736x_therm_group = {
671 .attrs = pc8736x_therm_attr_array,
672};
673
Jim Cromief0986bd2005-09-02 22:52:43 +0200674static ssize_t show_temp_input(struct device *dev, struct device_attribute *devattr, char *buf)
675{
676 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
677 struct pc87360_data *data = pc87360_update_device(dev);
Jim Cromie694fa052005-09-02 22:57:52 +0200678 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[attr->index]));
Jim Cromief0986bd2005-09-02 22:52:43 +0200679}
680static ssize_t show_temp_min(struct device *dev, struct device_attribute *devattr, char *buf)
681{
682 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
683 struct pc87360_data *data = pc87360_update_device(dev);
Jim Cromie694fa052005-09-02 22:57:52 +0200684 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_min[attr->index]));
Jim Cromief0986bd2005-09-02 22:52:43 +0200685}
686static ssize_t show_temp_max(struct device *dev, struct device_attribute *devattr, char *buf)
687{
688 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
689 struct pc87360_data *data = pc87360_update_device(dev);
Jim Cromie694fa052005-09-02 22:57:52 +0200690 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max[attr->index]));
Jim Cromief0986bd2005-09-02 22:52:43 +0200691}
692static ssize_t show_temp_crit(struct device *dev, struct device_attribute *devattr, char *buf)
693{
694 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
695 struct pc87360_data *data = pc87360_update_device(dev);
Jim Cromie694fa052005-09-02 22:57:52 +0200696 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_crit[attr->index]));
Jim Cromief0986bd2005-09-02 22:52:43 +0200697}
698static ssize_t show_temp_status(struct device *dev, struct device_attribute *devattr, char *buf)
699{
700 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
701 struct pc87360_data *data = pc87360_update_device(dev);
Jim Cromie694fa052005-09-02 22:57:52 +0200702 return sprintf(buf, "%d\n", data->temp_status[attr->index]);
Jim Cromief0986bd2005-09-02 22:52:43 +0200703}
704static ssize_t set_temp_min(struct device *dev, struct device_attribute *devattr, const char *buf,
705 size_t count)
706{
707 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Jean Delvaref641b582007-06-09 10:11:16 -0400708 struct pc87360_data *data = dev_get_drvdata(dev);
Jim Cromief0986bd2005-09-02 22:52:43 +0200709 long val = simple_strtol(buf, NULL, 10);
710
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100711 mutex_lock(&data->update_lock);
Jim Cromie694fa052005-09-02 22:57:52 +0200712 data->temp_min[attr->index] = TEMP_TO_REG(val);
713 pc87360_write_value(data, LD_TEMP, attr->index, PC87365_REG_TEMP_MIN,
714 data->temp_min[attr->index]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100715 mutex_unlock(&data->update_lock);
Jim Cromief0986bd2005-09-02 22:52:43 +0200716 return count;
717}
718static ssize_t set_temp_max(struct device *dev, struct device_attribute *devattr, const char *buf,
719 size_t count)
720{
721 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Jean Delvaref641b582007-06-09 10:11:16 -0400722 struct pc87360_data *data = dev_get_drvdata(dev);
Jim Cromief0986bd2005-09-02 22:52:43 +0200723 long val = simple_strtol(buf, NULL, 10);
724
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100725 mutex_lock(&data->update_lock);
Jim Cromie694fa052005-09-02 22:57:52 +0200726 data->temp_max[attr->index] = TEMP_TO_REG(val);
727 pc87360_write_value(data, LD_TEMP, attr->index, PC87365_REG_TEMP_MAX,
728 data->temp_max[attr->index]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100729 mutex_unlock(&data->update_lock);
Jim Cromief0986bd2005-09-02 22:52:43 +0200730 return count;
731}
732static ssize_t set_temp_crit(struct device *dev, struct device_attribute *devattr, const char *buf,
733 size_t count)
734{
735 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Jean Delvaref641b582007-06-09 10:11:16 -0400736 struct pc87360_data *data = dev_get_drvdata(dev);
Jim Cromief0986bd2005-09-02 22:52:43 +0200737 long val = simple_strtol(buf, NULL, 10);
738
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100739 mutex_lock(&data->update_lock);
Jim Cromie694fa052005-09-02 22:57:52 +0200740 data->temp_crit[attr->index] = TEMP_TO_REG(val);
741 pc87360_write_value(data, LD_TEMP, attr->index, PC87365_REG_TEMP_CRIT,
742 data->temp_crit[attr->index]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100743 mutex_unlock(&data->update_lock);
Jim Cromief0986bd2005-09-02 22:52:43 +0200744 return count;
745}
746
Jim Cromiededc6a72006-01-09 23:24:41 +0100747static struct sensor_device_attribute temp_input[] = {
748 SENSOR_ATTR(temp1_input, S_IRUGO, show_temp_input, NULL, 0),
749 SENSOR_ATTR(temp2_input, S_IRUGO, show_temp_input, NULL, 1),
750 SENSOR_ATTR(temp3_input, S_IRUGO, show_temp_input, NULL, 2),
751};
752static struct sensor_device_attribute temp_status[] = {
753 SENSOR_ATTR(temp1_status, S_IRUGO, show_temp_status, NULL, 0),
754 SENSOR_ATTR(temp2_status, S_IRUGO, show_temp_status, NULL, 1),
755 SENSOR_ATTR(temp3_status, S_IRUGO, show_temp_status, NULL, 2),
756};
757static struct sensor_device_attribute temp_min[] = {
758 SENSOR_ATTR(temp1_min, S_IRUGO | S_IWUSR,
759 show_temp_min, set_temp_min, 0),
760 SENSOR_ATTR(temp2_min, S_IRUGO | S_IWUSR,
761 show_temp_min, set_temp_min, 1),
762 SENSOR_ATTR(temp3_min, S_IRUGO | S_IWUSR,
763 show_temp_min, set_temp_min, 2),
764};
765static struct sensor_device_attribute temp_max[] = {
766 SENSOR_ATTR(temp1_max, S_IRUGO | S_IWUSR,
767 show_temp_max, set_temp_max, 0),
768 SENSOR_ATTR(temp2_max, S_IRUGO | S_IWUSR,
769 show_temp_max, set_temp_max, 1),
770 SENSOR_ATTR(temp3_max, S_IRUGO | S_IWUSR,
771 show_temp_max, set_temp_max, 2),
772};
773static struct sensor_device_attribute temp_crit[] = {
774 SENSOR_ATTR(temp1_crit, S_IRUGO | S_IWUSR,
775 show_temp_crit, set_temp_crit, 0),
776 SENSOR_ATTR(temp2_crit, S_IRUGO | S_IWUSR,
777 show_temp_crit, set_temp_crit, 1),
778 SENSOR_ATTR(temp3_crit, S_IRUGO | S_IWUSR,
779 show_temp_crit, set_temp_crit, 2),
780};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781
Yani Ioannoua5099cf2005-05-17 06:42:25 -0400782static ssize_t show_temp_alarms(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783{
784 struct pc87360_data *data = pc87360_update_device(dev);
785 return sprintf(buf, "%u\n", data->temp_alarms);
786}
787static DEVICE_ATTR(alarms_temp, S_IRUGO, show_temp_alarms, NULL);
788
Jim Cromie941c5c02006-09-24 21:02:44 +0200789#define TEMP_UNIT_ATTRS(X) \
790 &temp_input[X].dev_attr.attr, \
791 &temp_status[X].dev_attr.attr, \
792 &temp_min[X].dev_attr.attr, \
793 &temp_max[X].dev_attr.attr, \
794 &temp_crit[X].dev_attr.attr
795
796static struct attribute * pc8736x_temp_attr_array[] = {
797 TEMP_UNIT_ATTRS(0),
798 TEMP_UNIT_ATTRS(1),
799 TEMP_UNIT_ATTRS(2),
800 /* include the few miscellaneous atts here */
801 &dev_attr_alarms_temp.attr,
802 NULL
803};
804static const struct attribute_group pc8736x_temp_group = {
805 .attrs = pc8736x_temp_attr_array,
806};
807
Jean Delvaref641b582007-06-09 10:11:16 -0400808static ssize_t show_name(struct device *dev, struct device_attribute
809 *devattr, char *buf)
810{
811 struct pc87360_data *data = dev_get_drvdata(dev);
812 return sprintf(buf, "%s\n", data->name);
813}
814static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
815
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816/*
817 * Device detection, registration and update
818 */
819
Jean Delvaree6cfb3a2005-07-27 21:32:02 +0200820static int __init pc87360_find(int sioaddr, u8 *devid, unsigned short *addresses)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821{
822 u16 val;
823 int i;
824 int nrdev; /* logical device count */
825
826 /* No superio_enter */
827
828 /* Identify device */
829 val = superio_inb(sioaddr, DEVID);
830 switch (val) {
831 case 0xE1: /* PC87360 */
832 case 0xE8: /* PC87363 */
833 case 0xE4: /* PC87364 */
834 nrdev = 1;
835 break;
836 case 0xE5: /* PC87365 */
837 case 0xE9: /* PC87366 */
838 nrdev = 3;
839 break;
840 default:
841 superio_exit(sioaddr);
842 return -ENODEV;
843 }
844 /* Remember the device id */
845 *devid = val;
846
847 for (i = 0; i < nrdev; i++) {
848 /* select logical device */
849 superio_outb(sioaddr, DEV, logdev[i]);
850
851 val = superio_inb(sioaddr, ACT);
852 if (!(val & 0x01)) {
853 printk(KERN_INFO "pc87360: Device 0x%02x not "
854 "activated\n", logdev[i]);
855 continue;
856 }
857
858 val = (superio_inb(sioaddr, BASE) << 8)
859 | superio_inb(sioaddr, BASE + 1);
860 if (!val) {
861 printk(KERN_INFO "pc87360: Base address not set for "
862 "device 0x%02x\n", logdev[i]);
863 continue;
864 }
865
Jean Delvare2d8672c2005-07-19 23:56:35 +0200866 addresses[i] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867
868 if (i==0) { /* Fans */
869 confreg[0] = superio_inb(sioaddr, 0xF0);
870 confreg[1] = superio_inb(sioaddr, 0xF1);
871
872#ifdef DEBUG
873 printk(KERN_DEBUG "pc87360: Fan 1: mon=%d "
874 "ctrl=%d inv=%d\n", (confreg[0]>>2)&1,
875 (confreg[0]>>3)&1, (confreg[0]>>4)&1);
876 printk(KERN_DEBUG "pc87360: Fan 2: mon=%d "
877 "ctrl=%d inv=%d\n", (confreg[0]>>5)&1,
878 (confreg[0]>>6)&1, (confreg[0]>>7)&1);
879 printk(KERN_DEBUG "pc87360: Fan 3: mon=%d "
880 "ctrl=%d inv=%d\n", confreg[1]&1,
881 (confreg[1]>>1)&1, (confreg[1]>>2)&1);
882#endif
883 } else if (i==1) { /* Voltages */
884 /* Are we using thermistors? */
885 if (*devid == 0xE9) { /* PC87366 */
886 /* These registers are not logical-device
887 specific, just that we won't need them if
888 we don't use the VLM device */
889 confreg[2] = superio_inb(sioaddr, 0x2B);
890 confreg[3] = superio_inb(sioaddr, 0x25);
891
892 if (confreg[2] & 0x40) {
893 printk(KERN_INFO "pc87360: Using "
894 "thermistors for temperature "
895 "monitoring\n");
896 }
897 if (confreg[3] & 0xE0) {
898 printk(KERN_INFO "pc87360: VID "
899 "inputs routed (mode %u)\n",
900 confreg[3] >> 5);
901 }
902 }
903 }
904 }
905
906 superio_exit(sioaddr);
907 return 0;
908}
909
Jean Delvaref641b582007-06-09 10:11:16 -0400910static int __devinit pc87360_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911{
912 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 struct pc87360_data *data;
914 int err = 0;
915 const char *name = "pc87360";
916 int use_thermistors = 0;
Jean Delvaref641b582007-06-09 10:11:16 -0400917 struct device *dev = &pdev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918
Deepak Saxenaba9c2e82005-10-17 23:08:32 +0200919 if (!(data = kzalloc(sizeof(struct pc87360_data), GFP_KERNEL)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 data->fannr = 2;
923 data->innr = 0;
924 data->tempnr = 0;
925
926 switch (devid) {
927 case 0xe8:
928 name = "pc87363";
929 break;
930 case 0xe4:
931 name = "pc87364";
932 data->fannr = 3;
933 break;
934 case 0xe5:
935 name = "pc87365";
936 data->fannr = extra_isa[0] ? 3 : 0;
937 data->innr = extra_isa[1] ? 11 : 0;
938 data->tempnr = extra_isa[2] ? 2 : 0;
939 break;
940 case 0xe9:
941 name = "pc87366";
942 data->fannr = extra_isa[0] ? 3 : 0;
943 data->innr = extra_isa[1] ? 14 : 0;
944 data->tempnr = extra_isa[2] ? 3 : 0;
945 break;
946 }
947
Jean Delvaref641b582007-06-09 10:11:16 -0400948 data->name = name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 data->valid = 0;
Jean Delvaref641b582007-06-09 10:11:16 -0400950 mutex_init(&data->lock);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100951 mutex_init(&data->update_lock);
Jean Delvaref641b582007-06-09 10:11:16 -0400952 platform_set_drvdata(pdev, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953
954 for (i = 0; i < 3; i++) {
955 if (((data->address[i] = extra_isa[i]))
956 && !request_region(extra_isa[i], PC87360_EXTENT,
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100957 pc87360_driver.driver.name)) {
Jean Delvaref641b582007-06-09 10:11:16 -0400958 dev_err(dev, "Region 0x%x-0x%x already "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 "in use!\n", extra_isa[i],
960 extra_isa[i]+PC87360_EXTENT-1);
961 for (i--; i >= 0; i--)
962 release_region(extra_isa[i], PC87360_EXTENT);
963 err = -EBUSY;
964 goto ERROR1;
965 }
966 }
967
968 /* Retrieve the fans configuration from Super-I/O space */
969 if (data->fannr)
970 data->fan_conf = confreg[0] | (confreg[1] << 8);
971
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 /* Use the correct reference voltage
973 Unless both the VLM and the TMS logical devices agree to
974 use an external Vref, the internal one is used. */
975 if (data->innr) {
976 i = pc87360_read_value(data, LD_IN, NO_BANK,
977 PC87365_REG_IN_CONFIG);
978 if (data->tempnr) {
979 i &= pc87360_read_value(data, LD_TEMP, NO_BANK,
980 PC87365_REG_TEMP_CONFIG);
981 }
982 data->in_vref = (i&0x02) ? 3025 : 2966;
Jean Delvaref641b582007-06-09 10:11:16 -0400983 dev_dbg(dev, "Using %s reference voltage\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 (i&0x02) ? "external" : "internal");
985
986 data->vid_conf = confreg[3];
Jim Cromie3d7f9a82006-12-12 18:18:28 +0100987 data->vrm = vid_which_vrm();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 }
989
990 /* Fan clock dividers may be needed before any data is read */
991 for (i = 0; i < data->fannr; i++) {
992 if (FAN_CONFIG_MONITOR(data->fan_conf, i))
993 data->fan_status[i] = pc87360_read_value(data,
994 LD_FAN, NO_BANK,
995 PC87360_REG_FAN_STATUS(i));
996 }
997
998 if (init > 0) {
999 if (devid == 0xe9 && data->address[1]) /* PC87366 */
1000 use_thermistors = confreg[2] & 0x40;
1001
Jean Delvaref641b582007-06-09 10:11:16 -04001002 pc87360_init_device(pdev, use_thermistors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 }
1004
Jim Cromief3722d5b2006-09-24 21:03:25 +02001005 /* Register all-or-nothing sysfs groups */
1006
1007 if (data->innr &&
Jean Delvaref641b582007-06-09 10:11:16 -04001008 (err = sysfs_create_group(&dev->kobj,
Jim Cromief3722d5b2006-09-24 21:03:25 +02001009 &pc8736x_vin_group)))
1010 goto ERROR3;
1011
1012 if (data->innr == 14 &&
Jean Delvaref641b582007-06-09 10:11:16 -04001013 (err = sysfs_create_group(&dev->kobj,
Jim Cromief3722d5b2006-09-24 21:03:25 +02001014 &pc8736x_therm_group)))
1015 goto ERROR3;
1016
1017 /* create device attr-files for varying sysfs groups */
1018
1019 if (data->tempnr) {
1020 for (i = 0; i < data->tempnr; i++) {
1021 if ((err = device_create_file(dev,
1022 &temp_input[i].dev_attr))
1023 || (err = device_create_file(dev,
1024 &temp_min[i].dev_attr))
1025 || (err = device_create_file(dev,
1026 &temp_max[i].dev_attr))
1027 || (err = device_create_file(dev,
1028 &temp_crit[i].dev_attr))
1029 || (err = device_create_file(dev,
1030 &temp_status[i].dev_attr)))
1031 goto ERROR3;
1032 }
1033 if ((err = device_create_file(dev, &dev_attr_alarms_temp)))
1034 goto ERROR3;
1035 }
1036
1037 for (i = 0; i < data->fannr; i++) {
1038 if (FAN_CONFIG_MONITOR(data->fan_conf, i)
1039 && ((err = device_create_file(dev,
1040 &fan_input[i].dev_attr))
1041 || (err = device_create_file(dev,
1042 &fan_min[i].dev_attr))
1043 || (err = device_create_file(dev,
1044 &fan_div[i].dev_attr))
1045 || (err = device_create_file(dev,
1046 &fan_status[i].dev_attr))))
1047 goto ERROR3;
1048
1049 if (FAN_CONFIG_CONTROL(data->fan_conf, i)
1050 && (err = device_create_file(dev, &pwm[i].dev_attr)))
1051 goto ERROR3;
1052 }
1053
Jean Delvaref641b582007-06-09 10:11:16 -04001054 if ((err = device_create_file(dev, &dev_attr_name)))
1055 goto ERROR3;
1056
1057 data->class_dev = hwmon_device_register(dev);
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001058 if (IS_ERR(data->class_dev)) {
1059 err = PTR_ERR(data->class_dev);
1060 goto ERROR3;
1061 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 return 0;
1063
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001064ERROR3:
Jean Delvaref641b582007-06-09 10:11:16 -04001065 device_remove_file(dev, &dev_attr_name);
Jim Cromief3722d5b2006-09-24 21:03:25 +02001066 /* can still remove groups whose members were added individually */
Jean Delvaref641b582007-06-09 10:11:16 -04001067 sysfs_remove_group(&dev->kobj, &pc8736x_temp_group);
1068 sysfs_remove_group(&dev->kobj, &pc8736x_fan_group);
1069 sysfs_remove_group(&dev->kobj, &pc8736x_therm_group);
1070 sysfs_remove_group(&dev->kobj, &pc8736x_vin_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 for (i = 0; i < 3; i++) {
1072 if (data->address[i]) {
1073 release_region(data->address[i], PC87360_EXTENT);
1074 }
1075 }
1076ERROR1:
1077 kfree(data);
1078 return err;
1079}
1080
Jean Delvaref641b582007-06-09 10:11:16 -04001081static int __devexit pc87360_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082{
Jean Delvaref641b582007-06-09 10:11:16 -04001083 struct pc87360_data *data = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 int i;
1085
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001086 hwmon_device_unregister(data->class_dev);
1087
Jean Delvaref641b582007-06-09 10:11:16 -04001088 device_remove_file(&pdev->dev, &dev_attr_name);
1089 sysfs_remove_group(&pdev->dev.kobj, &pc8736x_temp_group);
1090 sysfs_remove_group(&pdev->dev.kobj, &pc8736x_fan_group);
1091 sysfs_remove_group(&pdev->dev.kobj, &pc8736x_therm_group);
1092 sysfs_remove_group(&pdev->dev.kobj, &pc8736x_vin_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093
1094 for (i = 0; i < 3; i++) {
1095 if (data->address[i]) {
1096 release_region(data->address[i], PC87360_EXTENT);
1097 }
1098 }
1099 kfree(data);
1100
1101 return 0;
1102}
1103
1104/* ldi is the logical device index
1105 bank is for voltages and temperatures only */
1106static int pc87360_read_value(struct pc87360_data *data, u8 ldi, u8 bank,
1107 u8 reg)
1108{
1109 int res;
1110
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001111 mutex_lock(&(data->lock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 if (bank != NO_BANK)
1113 outb_p(bank, data->address[ldi] + PC87365_REG_BANK);
1114 res = inb_p(data->address[ldi] + reg);
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001115 mutex_unlock(&(data->lock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116
1117 return res;
1118}
1119
1120static void pc87360_write_value(struct pc87360_data *data, u8 ldi, u8 bank,
1121 u8 reg, u8 value)
1122{
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001123 mutex_lock(&(data->lock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 if (bank != NO_BANK)
1125 outb_p(bank, data->address[ldi] + PC87365_REG_BANK);
1126 outb_p(value, data->address[ldi] + reg);
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001127 mutex_unlock(&(data->lock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128}
1129
Jean Delvaref641b582007-06-09 10:11:16 -04001130static void pc87360_init_device(struct platform_device *pdev,
1131 int use_thermistors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132{
Jean Delvaref641b582007-06-09 10:11:16 -04001133 struct pc87360_data *data = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 int i, nr;
1135 const u8 init_in[14] = { 2, 2, 2, 2, 2, 2, 2, 1, 1, 3, 1, 2, 2, 2 };
1136 const u8 init_temp[3] = { 2, 2, 1 };
1137 u8 reg;
1138
1139 if (init >= 2 && data->innr) {
1140 reg = pc87360_read_value(data, LD_IN, NO_BANK,
1141 PC87365_REG_IN_CONVRATE);
Jean Delvaref641b582007-06-09 10:11:16 -04001142 dev_info(&pdev->dev, "VLM conversion set to "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 "1s period, 160us delay\n");
1144 pc87360_write_value(data, LD_IN, NO_BANK,
1145 PC87365_REG_IN_CONVRATE,
1146 (reg & 0xC0) | 0x11);
1147 }
1148
1149 nr = data->innr < 11 ? data->innr : 11;
Jim Cromiededc6a72006-01-09 23:24:41 +01001150 for (i = 0; i < nr; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 if (init >= init_in[i]) {
1152 /* Forcibly enable voltage channel */
1153 reg = pc87360_read_value(data, LD_IN, i,
1154 PC87365_REG_IN_STATUS);
1155 if (!(reg & 0x01)) {
Jean Delvaref641b582007-06-09 10:11:16 -04001156 dev_dbg(&pdev->dev, "Forcibly "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 "enabling in%d\n", i);
1158 pc87360_write_value(data, LD_IN, i,
1159 PC87365_REG_IN_STATUS,
1160 (reg & 0x68) | 0x87);
1161 }
1162 }
1163 }
1164
1165 /* We can't blindly trust the Super-I/O space configuration bit,
1166 most BIOS won't set it properly */
Jim Cromiededc6a72006-01-09 23:24:41 +01001167 for (i = 11; i < data->innr; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 reg = pc87360_read_value(data, LD_IN, i,
1169 PC87365_REG_TEMP_STATUS);
1170 use_thermistors = use_thermistors || (reg & 0x01);
1171 }
1172
1173 i = use_thermistors ? 2 : 0;
Jim Cromiededc6a72006-01-09 23:24:41 +01001174 for (; i < data->tempnr; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 if (init >= init_temp[i]) {
1176 /* Forcibly enable temperature channel */
1177 reg = pc87360_read_value(data, LD_TEMP, i,
1178 PC87365_REG_TEMP_STATUS);
1179 if (!(reg & 0x01)) {
Jean Delvaref641b582007-06-09 10:11:16 -04001180 dev_dbg(&pdev->dev, "Forcibly "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 "enabling temp%d\n", i+1);
1182 pc87360_write_value(data, LD_TEMP, i,
1183 PC87365_REG_TEMP_STATUS,
1184 0xCF);
1185 }
1186 }
1187 }
1188
1189 if (use_thermistors) {
Jim Cromiededc6a72006-01-09 23:24:41 +01001190 for (i = 11; i < data->innr; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 if (init >= init_in[i]) {
1192 /* The pin may already be used by thermal
1193 diodes */
1194 reg = pc87360_read_value(data, LD_TEMP,
1195 (i-11)/2, PC87365_REG_TEMP_STATUS);
1196 if (reg & 0x01) {
Jean Delvaref641b582007-06-09 10:11:16 -04001197 dev_dbg(&pdev->dev, "Skipping "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 "temp%d, pin already in use "
1199 "by temp%d\n", i-7, (i-11)/2);
1200 continue;
1201 }
1202
1203 /* Forcibly enable thermistor channel */
1204 reg = pc87360_read_value(data, LD_IN, i,
1205 PC87365_REG_IN_STATUS);
1206 if (!(reg & 0x01)) {
Jean Delvaref641b582007-06-09 10:11:16 -04001207 dev_dbg(&pdev->dev, "Forcibly "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 "enabling temp%d\n", i-7);
1209 pc87360_write_value(data, LD_IN, i,
1210 PC87365_REG_TEMP_STATUS,
1211 (reg & 0x60) | 0x8F);
1212 }
1213 }
1214 }
1215 }
1216
1217 if (data->innr) {
1218 reg = pc87360_read_value(data, LD_IN, NO_BANK,
1219 PC87365_REG_IN_CONFIG);
1220 if (reg & 0x01) {
Jean Delvaref641b582007-06-09 10:11:16 -04001221 dev_dbg(&pdev->dev, "Forcibly "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 "enabling monitoring (VLM)\n");
1223 pc87360_write_value(data, LD_IN, NO_BANK,
1224 PC87365_REG_IN_CONFIG,
1225 reg & 0xFE);
1226 }
1227 }
1228
1229 if (data->tempnr) {
1230 reg = pc87360_read_value(data, LD_TEMP, NO_BANK,
1231 PC87365_REG_TEMP_CONFIG);
1232 if (reg & 0x01) {
Jean Delvaref641b582007-06-09 10:11:16 -04001233 dev_dbg(&pdev->dev, "Forcibly enabling "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 "monitoring (TMS)\n");
1235 pc87360_write_value(data, LD_TEMP, NO_BANK,
1236 PC87365_REG_TEMP_CONFIG,
1237 reg & 0xFE);
1238 }
1239
1240 if (init >= 2) {
1241 /* Chip config as documented by National Semi. */
1242 pc87360_write_value(data, LD_TEMP, 0xF, 0xA, 0x08);
1243 /* We voluntarily omit the bank here, in case the
1244 sequence itself matters. It shouldn't be a problem,
1245 since nobody else is supposed to access the
1246 device at that point. */
1247 pc87360_write_value(data, LD_TEMP, NO_BANK, 0xB, 0x04);
1248 pc87360_write_value(data, LD_TEMP, NO_BANK, 0xC, 0x35);
1249 pc87360_write_value(data, LD_TEMP, NO_BANK, 0xD, 0x05);
1250 pc87360_write_value(data, LD_TEMP, NO_BANK, 0xE, 0x05);
1251 }
1252 }
1253}
1254
Jean Delvaref641b582007-06-09 10:11:16 -04001255static void pc87360_autodiv(struct device *dev, int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256{
Jean Delvaref641b582007-06-09 10:11:16 -04001257 struct pc87360_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 u8 old_min = data->fan_min[nr];
1259
1260 /* Increase clock divider if needed and possible */
1261 if ((data->fan_status[nr] & 0x04) /* overflow flag */
1262 || (data->fan[nr] >= 224)) { /* next to overflow */
1263 if ((data->fan_status[nr] & 0x60) != 0x60) {
1264 data->fan_status[nr] += 0x20;
1265 data->fan_min[nr] >>= 1;
1266 data->fan[nr] >>= 1;
Jean Delvaref641b582007-06-09 10:11:16 -04001267 dev_dbg(dev, "Increasing "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 "clock divider to %d for fan %d\n",
1269 FAN_DIV_FROM_REG(data->fan_status[nr]), nr+1);
1270 }
1271 } else {
1272 /* Decrease clock divider if possible */
1273 while (!(data->fan_min[nr] & 0x80) /* min "nails" divider */
1274 && data->fan[nr] < 85 /* bad accuracy */
1275 && (data->fan_status[nr] & 0x60) != 0x00) {
1276 data->fan_status[nr] -= 0x20;
1277 data->fan_min[nr] <<= 1;
1278 data->fan[nr] <<= 1;
Jean Delvaref641b582007-06-09 10:11:16 -04001279 dev_dbg(dev, "Decreasing "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 "clock divider to %d for fan %d\n",
1281 FAN_DIV_FROM_REG(data->fan_status[nr]),
1282 nr+1);
1283 }
1284 }
1285
1286 /* Write new fan min if it changed */
1287 if (old_min != data->fan_min[nr]) {
1288 pc87360_write_value(data, LD_FAN, NO_BANK,
1289 PC87360_REG_FAN_MIN(nr),
1290 data->fan_min[nr]);
1291 }
1292}
1293
1294static struct pc87360_data *pc87360_update_device(struct device *dev)
1295{
Jean Delvaref641b582007-06-09 10:11:16 -04001296 struct pc87360_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 u8 i;
1298
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001299 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300
1301 if (time_after(jiffies, data->last_updated + HZ * 2) || !data->valid) {
Jean Delvaref641b582007-06-09 10:11:16 -04001302 dev_dbg(dev, "Data update\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303
1304 /* Fans */
1305 for (i = 0; i < data->fannr; i++) {
1306 if (FAN_CONFIG_MONITOR(data->fan_conf, i)) {
1307 data->fan_status[i] =
1308 pc87360_read_value(data, LD_FAN,
1309 NO_BANK, PC87360_REG_FAN_STATUS(i));
1310 data->fan[i] = pc87360_read_value(data, LD_FAN,
1311 NO_BANK, PC87360_REG_FAN(i));
1312 data->fan_min[i] = pc87360_read_value(data,
1313 LD_FAN, NO_BANK,
1314 PC87360_REG_FAN_MIN(i));
1315 /* Change clock divider if needed */
Jean Delvaref641b582007-06-09 10:11:16 -04001316 pc87360_autodiv(dev, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 /* Clear bits and write new divider */
1318 pc87360_write_value(data, LD_FAN, NO_BANK,
1319 PC87360_REG_FAN_STATUS(i),
1320 data->fan_status[i]);
1321 }
1322 if (FAN_CONFIG_CONTROL(data->fan_conf, i))
1323 data->pwm[i] = pc87360_read_value(data, LD_FAN,
1324 NO_BANK, PC87360_REG_PWM(i));
1325 }
1326
1327 /* Voltages */
1328 for (i = 0; i < data->innr; i++) {
1329 data->in_status[i] = pc87360_read_value(data, LD_IN, i,
1330 PC87365_REG_IN_STATUS);
1331 /* Clear bits */
1332 pc87360_write_value(data, LD_IN, i,
1333 PC87365_REG_IN_STATUS,
1334 data->in_status[i]);
1335 if ((data->in_status[i] & 0x81) == 0x81) {
1336 data->in[i] = pc87360_read_value(data, LD_IN,
1337 i, PC87365_REG_IN);
1338 }
1339 if (data->in_status[i] & 0x01) {
1340 data->in_min[i] = pc87360_read_value(data,
1341 LD_IN, i,
1342 PC87365_REG_IN_MIN);
1343 data->in_max[i] = pc87360_read_value(data,
1344 LD_IN, i,
1345 PC87365_REG_IN_MAX);
1346 if (i >= 11)
1347 data->in_crit[i-11] =
1348 pc87360_read_value(data, LD_IN,
1349 i, PC87365_REG_TEMP_CRIT);
1350 }
1351 }
1352 if (data->innr) {
1353 data->in_alarms = pc87360_read_value(data, LD_IN,
1354 NO_BANK, PC87365_REG_IN_ALARMS1)
1355 | ((pc87360_read_value(data, LD_IN,
1356 NO_BANK, PC87365_REG_IN_ALARMS2)
1357 & 0x07) << 8);
1358 data->vid = (data->vid_conf & 0xE0) ?
1359 pc87360_read_value(data, LD_IN,
1360 NO_BANK, PC87365_REG_VID) : 0x1F;
1361 }
1362
1363 /* Temperatures */
1364 for (i = 0; i < data->tempnr; i++) {
1365 data->temp_status[i] = pc87360_read_value(data,
1366 LD_TEMP, i,
1367 PC87365_REG_TEMP_STATUS);
1368 /* Clear bits */
1369 pc87360_write_value(data, LD_TEMP, i,
1370 PC87365_REG_TEMP_STATUS,
1371 data->temp_status[i]);
1372 if ((data->temp_status[i] & 0x81) == 0x81) {
1373 data->temp[i] = pc87360_read_value(data,
1374 LD_TEMP, i,
1375 PC87365_REG_TEMP);
1376 }
1377 if (data->temp_status[i] & 0x01) {
1378 data->temp_min[i] = pc87360_read_value(data,
1379 LD_TEMP, i,
1380 PC87365_REG_TEMP_MIN);
1381 data->temp_max[i] = pc87360_read_value(data,
1382 LD_TEMP, i,
1383 PC87365_REG_TEMP_MAX);
1384 data->temp_crit[i] = pc87360_read_value(data,
1385 LD_TEMP, i,
1386 PC87365_REG_TEMP_CRIT);
1387 }
1388 }
1389 if (data->tempnr) {
1390 data->temp_alarms = pc87360_read_value(data, LD_TEMP,
1391 NO_BANK, PC87365_REG_TEMP_ALARMS)
1392 & 0x3F;
1393 }
1394
1395 data->last_updated = jiffies;
1396 data->valid = 1;
1397 }
1398
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001399 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400
1401 return data;
1402}
1403
Jean Delvaref641b582007-06-09 10:11:16 -04001404static int __init pc87360_device_add(unsigned short address)
1405{
1406 struct resource res = {
1407 .name = "pc87360",
1408 .flags = IORESOURCE_IO,
1409 };
1410 int err, i;
1411
1412 pdev = platform_device_alloc("pc87360", address);
1413 if (!pdev) {
1414 err = -ENOMEM;
1415 printk(KERN_ERR "pc87360: Device allocation failed\n");
1416 goto exit;
1417 }
1418
1419 for (i = 0; i < 3; i++) {
1420 if (!extra_isa[i])
1421 continue;
1422 res.start = extra_isa[i];
1423 res.end = extra_isa[i] + PC87360_EXTENT - 1;
1424 err = platform_device_add_resources(pdev, &res, 1);
1425 if (err) {
1426 printk(KERN_ERR "pc87360: Device resource[%d] "
1427 "addition failed (%d)\n", i, err);
1428 goto exit_device_put;
1429 }
1430 }
1431
1432 err = platform_device_add(pdev);
1433 if (err) {
1434 printk(KERN_ERR "pc87360: Device addition failed (%d)\n",
1435 err);
1436 goto exit_device_put;
1437 }
1438
1439 return 0;
1440
1441exit_device_put:
1442 platform_device_put(pdev);
1443exit:
1444 return err;
1445}
1446
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447static int __init pc87360_init(void)
1448{
Jean Delvaref641b582007-06-09 10:11:16 -04001449 int err, i;
1450 unsigned short address = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451
1452 if (pc87360_find(0x2e, &devid, extra_isa)
1453 && pc87360_find(0x4e, &devid, extra_isa)) {
1454 printk(KERN_WARNING "pc87360: PC8736x not detected, "
1455 "module not inserted.\n");
1456 return -ENODEV;
1457 }
1458
1459 /* Arbitrarily pick one of the addresses */
1460 for (i = 0; i < 3; i++) {
1461 if (extra_isa[i] != 0x0000) {
Jean Delvare2d8672c2005-07-19 23:56:35 +02001462 address = extra_isa[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 break;
1464 }
1465 }
1466
Jean Delvare2d8672c2005-07-19 23:56:35 +02001467 if (address == 0x0000) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 printk(KERN_WARNING "pc87360: No active logical device, "
1469 "module not inserted.\n");
1470 return -ENODEV;
1471 }
1472
Jean Delvaref641b582007-06-09 10:11:16 -04001473 err = platform_driver_register(&pc87360_driver);
1474 if (err)
1475 goto exit;
1476
1477 /* Sets global pdev as a side effect */
1478 err = pc87360_device_add(address);
1479 if (err)
1480 goto exit_driver;
1481
1482 return 0;
1483
1484 exit_driver:
1485 platform_driver_unregister(&pc87360_driver);
1486 exit:
1487 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488}
1489
1490static void __exit pc87360_exit(void)
1491{
Jean Delvaref641b582007-06-09 10:11:16 -04001492 platform_device_unregister(pdev);
1493 platform_driver_unregister(&pc87360_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494}
1495
1496
1497MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
1498MODULE_DESCRIPTION("PC8736x hardware monitor");
1499MODULE_LICENSE("GPL");
1500
1501module_init(pc87360_init);
1502module_exit(pc87360_exit);