blob: 6804a3570f39de38bf2884ecb56d4b8f463e0c57 [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 */
Jim Cromie2a32ec22008-10-18 20:27:33 -070078#define LDNI_MAX 3
79static const u8 logdev[LDNI_MAX] = { FSCM, VLM, TMS };
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
81#define LD_FAN 0
82#define LD_IN 1
83#define LD_TEMP 2
84
85static inline void superio_outb(int sioaddr, int reg, int val)
86{
87 outb(reg, sioaddr);
88 outb(val, sioaddr+1);
89}
90
91static inline int superio_inb(int sioaddr, int reg)
92{
93 outb(reg, sioaddr);
94 return inb(sioaddr+1);
95}
96
97static inline void superio_exit(int sioaddr)
98{
99 outb(0x02, sioaddr);
100 outb(0x02, sioaddr+1);
101}
102
103/*
104 * Logical devices
105 */
106
107#define PC87360_EXTENT 0x10
108#define PC87365_REG_BANK 0x09
109#define NO_BANK 0xff
110
111/*
112 * Fan registers and conversions
113 */
114
115/* nr has to be 0 or 1 (PC87360/87363) or 2 (PC87364/87365/87366) */
116#define PC87360_REG_PRESCALE(nr) (0x00 + 2 * (nr))
117#define PC87360_REG_PWM(nr) (0x01 + 2 * (nr))
118#define PC87360_REG_FAN_MIN(nr) (0x06 + 3 * (nr))
119#define PC87360_REG_FAN(nr) (0x07 + 3 * (nr))
120#define PC87360_REG_FAN_STATUS(nr) (0x08 + 3 * (nr))
121
122#define FAN_FROM_REG(val,div) ((val) == 0 ? 0: \
123 480000 / ((val)*(div)))
124#define FAN_TO_REG(val,div) ((val) <= 100 ? 0 : \
125 480000 / ((val)*(div)))
126#define FAN_DIV_FROM_REG(val) (1 << ((val >> 5) & 0x03))
127#define FAN_STATUS_FROM_REG(val) ((val) & 0x07)
128
129#define FAN_CONFIG_MONITOR(val,nr) (((val) >> (2 + nr * 3)) & 1)
130#define FAN_CONFIG_CONTROL(val,nr) (((val) >> (3 + nr * 3)) & 1)
131#define FAN_CONFIG_INVERT(val,nr) (((val) >> (4 + nr * 3)) & 1)
132
133#define PWM_FROM_REG(val,inv) ((inv) ? 255 - (val) : (val))
134static inline u8 PWM_TO_REG(int val, int inv)
135{
136 if (inv)
137 val = 255 - val;
138 if (val < 0)
139 return 0;
140 if (val > 255)
141 return 255;
142 return val;
143}
144
145/*
146 * Voltage registers and conversions
147 */
148
149#define PC87365_REG_IN_CONVRATE 0x07
150#define PC87365_REG_IN_CONFIG 0x08
151#define PC87365_REG_IN 0x0B
152#define PC87365_REG_IN_MIN 0x0D
153#define PC87365_REG_IN_MAX 0x0C
154#define PC87365_REG_IN_STATUS 0x0A
155#define PC87365_REG_IN_ALARMS1 0x00
156#define PC87365_REG_IN_ALARMS2 0x01
157#define PC87365_REG_VID 0x06
158
159#define IN_FROM_REG(val,ref) (((val) * (ref) + 128) / 256)
160#define IN_TO_REG(val,ref) ((val) < 0 ? 0 : \
161 (val)*256 >= (ref)*255 ? 255: \
162 ((val) * 256 + (ref)/2) / (ref))
163
164/*
165 * Temperature registers and conversions
166 */
167
168#define PC87365_REG_TEMP_CONFIG 0x08
169#define PC87365_REG_TEMP 0x0B
170#define PC87365_REG_TEMP_MIN 0x0D
171#define PC87365_REG_TEMP_MAX 0x0C
172#define PC87365_REG_TEMP_CRIT 0x0E
173#define PC87365_REG_TEMP_STATUS 0x0A
174#define PC87365_REG_TEMP_ALARMS 0x00
175
176#define TEMP_FROM_REG(val) ((val) * 1000)
177#define TEMP_TO_REG(val) ((val) < -55000 ? -55 : \
178 (val) > 127000 ? 127 : \
179 (val) < 0 ? ((val) - 500) / 1000 : \
180 ((val) + 500) / 1000)
181
182/*
Jean Delvaref641b582007-06-09 10:11:16 -0400183 * Device data
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 */
185
186struct pc87360_data {
Jean Delvaref641b582007-06-09 10:11:16 -0400187 const char *name;
Tony Jones1beeffe2007-08-20 13:46:20 -0700188 struct device *hwmon_dev;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100189 struct mutex lock;
190 struct mutex update_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 char valid; /* !=0 if following fields are valid */
192 unsigned long last_updated; /* In jiffies */
193
194 int address[3];
195
196 u8 fannr, innr, tempnr;
197
198 u8 fan[3]; /* Register value */
199 u8 fan_min[3]; /* Register value */
200 u8 fan_status[3]; /* Register value */
201 u8 pwm[3]; /* Register value */
202 u16 fan_conf; /* Configuration register values, combined */
203
204 u16 in_vref; /* 1 mV/bit */
205 u8 in[14]; /* Register value */
206 u8 in_min[14]; /* Register value */
207 u8 in_max[14]; /* Register value */
208 u8 in_crit[3]; /* Register value */
209 u8 in_status[14]; /* Register value */
210 u16 in_alarms; /* Register values, combined, masked */
211 u8 vid_conf; /* Configuration register value */
212 u8 vrm;
213 u8 vid; /* Register value */
214
215 s8 temp[3]; /* Register value */
216 s8 temp_min[3]; /* Register value */
217 s8 temp_max[3]; /* Register value */
218 s8 temp_crit[3]; /* Register value */
219 u8 temp_status[3]; /* Register value */
220 u8 temp_alarms; /* Register value, masked */
221};
222
223/*
224 * Functions declaration
225 */
226
Jean Delvaref641b582007-06-09 10:11:16 -0400227static int pc87360_probe(struct platform_device *pdev);
Jean Delvared0546122007-07-22 12:09:48 +0200228static int __devexit pc87360_remove(struct platform_device *pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
230static int pc87360_read_value(struct pc87360_data *data, u8 ldi, u8 bank,
231 u8 reg);
232static void pc87360_write_value(struct pc87360_data *data, u8 ldi, u8 bank,
233 u8 reg, u8 value);
Jean Delvaref641b582007-06-09 10:11:16 -0400234static void pc87360_init_device(struct platform_device *pdev,
235 int use_thermistors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236static struct pc87360_data *pc87360_update_device(struct device *dev);
237
238/*
Jean Delvaref641b582007-06-09 10:11:16 -0400239 * Driver data
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 */
241
Jean Delvaref641b582007-06-09 10:11:16 -0400242static struct platform_driver pc87360_driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100243 .driver = {
Jean Delvare87218842006-09-03 22:36:14 +0200244 .owner = THIS_MODULE,
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100245 .name = "pc87360",
246 },
Jean Delvaref641b582007-06-09 10:11:16 -0400247 .probe = pc87360_probe,
248 .remove = __devexit_p(pc87360_remove),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249};
250
251/*
252 * Sysfs stuff
253 */
254
Jim Cromief0986bd2005-09-02 22:52:43 +0200255static ssize_t show_fan_input(struct device *dev, struct device_attribute *devattr, char *buf)
256{
257 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
258 struct pc87360_data *data = pc87360_update_device(dev);
Jim Cromie694fa052005-09-02 22:57:52 +0200259 return sprintf(buf, "%u\n", FAN_FROM_REG(data->fan[attr->index],
260 FAN_DIV_FROM_REG(data->fan_status[attr->index])));
Jim Cromief0986bd2005-09-02 22:52:43 +0200261}
262static ssize_t show_fan_min(struct device *dev, struct device_attribute *devattr, char *buf)
263{
264 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
265 struct pc87360_data *data = pc87360_update_device(dev);
Jim Cromie694fa052005-09-02 22:57:52 +0200266 return sprintf(buf, "%u\n", FAN_FROM_REG(data->fan_min[attr->index],
267 FAN_DIV_FROM_REG(data->fan_status[attr->index])));
Jim Cromief0986bd2005-09-02 22:52:43 +0200268}
269static ssize_t show_fan_div(struct device *dev, struct device_attribute *devattr, char *buf)
270{
271 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
272 struct pc87360_data *data = pc87360_update_device(dev);
273 return sprintf(buf, "%u\n",
Jim Cromie694fa052005-09-02 22:57:52 +0200274 FAN_DIV_FROM_REG(data->fan_status[attr->index]));
Jim Cromief0986bd2005-09-02 22:52:43 +0200275}
276static ssize_t show_fan_status(struct device *dev, struct device_attribute *devattr, char *buf)
277{
278 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
279 struct pc87360_data *data = pc87360_update_device(dev);
280 return sprintf(buf, "%u\n",
Jim Cromie694fa052005-09-02 22:57:52 +0200281 FAN_STATUS_FROM_REG(data->fan_status[attr->index]));
Jim Cromief0986bd2005-09-02 22:52:43 +0200282}
283static ssize_t set_fan_min(struct device *dev, struct device_attribute *devattr, const char *buf,
284 size_t count)
285{
286 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Jean Delvaref641b582007-06-09 10:11:16 -0400287 struct pc87360_data *data = dev_get_drvdata(dev);
Jim Cromie11be27e2005-09-02 23:05:07 +0200288 long fan_min = simple_strtol(buf, NULL, 10);
289
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100290 mutex_lock(&data->update_lock);
Jim Cromie11be27e2005-09-02 23:05:07 +0200291 fan_min = FAN_TO_REG(fan_min, FAN_DIV_FROM_REG(data->fan_status[attr->index]));
292
293 /* If it wouldn't fit, change clock divisor */
294 while (fan_min > 255
295 && (data->fan_status[attr->index] & 0x60) != 0x60) {
296 fan_min >>= 1;
297 data->fan[attr->index] >>= 1;
298 data->fan_status[attr->index] += 0x20;
299 }
300 data->fan_min[attr->index] = fan_min > 255 ? 255 : fan_min;
301 pc87360_write_value(data, LD_FAN, NO_BANK, PC87360_REG_FAN_MIN(attr->index),
302 data->fan_min[attr->index]);
303
304 /* Write new divider, preserve alarm bits */
305 pc87360_write_value(data, LD_FAN, NO_BANK, PC87360_REG_FAN_STATUS(attr->index),
306 data->fan_status[attr->index] & 0xF9);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100307 mutex_unlock(&data->update_lock);
Jim Cromie11be27e2005-09-02 23:05:07 +0200308
309 return count;
Jim Cromief0986bd2005-09-02 22:52:43 +0200310}
311
Jim Cromiededc6a72006-01-09 23:24:41 +0100312static struct sensor_device_attribute fan_input[] = {
313 SENSOR_ATTR(fan1_input, S_IRUGO, show_fan_input, NULL, 0),
314 SENSOR_ATTR(fan2_input, S_IRUGO, show_fan_input, NULL, 1),
315 SENSOR_ATTR(fan3_input, S_IRUGO, show_fan_input, NULL, 2),
316};
317static struct sensor_device_attribute fan_status[] = {
318 SENSOR_ATTR(fan1_status, S_IRUGO, show_fan_status, NULL, 0),
319 SENSOR_ATTR(fan2_status, S_IRUGO, show_fan_status, NULL, 1),
320 SENSOR_ATTR(fan3_status, S_IRUGO, show_fan_status, NULL, 2),
321};
322static struct sensor_device_attribute fan_div[] = {
323 SENSOR_ATTR(fan1_div, S_IRUGO, show_fan_div, NULL, 0),
324 SENSOR_ATTR(fan2_div, S_IRUGO, show_fan_div, NULL, 1),
325 SENSOR_ATTR(fan3_div, S_IRUGO, show_fan_div, NULL, 2),
326};
327static struct sensor_device_attribute fan_min[] = {
328 SENSOR_ATTR(fan1_min, S_IWUSR | S_IRUGO, show_fan_min, set_fan_min, 0),
329 SENSOR_ATTR(fan2_min, S_IWUSR | S_IRUGO, show_fan_min, set_fan_min, 1),
330 SENSOR_ATTR(fan3_min, S_IWUSR | S_IRUGO, show_fan_min, set_fan_min, 2),
331};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
Jim Cromie941c5c02006-09-24 21:02:44 +0200333#define FAN_UNIT_ATTRS(X) \
334 &fan_input[X].dev_attr.attr, \
335 &fan_status[X].dev_attr.attr, \
336 &fan_div[X].dev_attr.attr, \
337 &fan_min[X].dev_attr.attr
338
Jim Cromief0986bd2005-09-02 22:52:43 +0200339static ssize_t show_pwm(struct device *dev, struct device_attribute *devattr, char *buf)
340{
341 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
342 struct pc87360_data *data = pc87360_update_device(dev);
343 return sprintf(buf, "%u\n",
Jim Cromie694fa052005-09-02 22:57:52 +0200344 PWM_FROM_REG(data->pwm[attr->index],
Jim Cromief0986bd2005-09-02 22:52:43 +0200345 FAN_CONFIG_INVERT(data->fan_conf,
Jim Cromie694fa052005-09-02 22:57:52 +0200346 attr->index)));
Jim Cromief0986bd2005-09-02 22:52:43 +0200347}
348static ssize_t set_pwm(struct device *dev, struct device_attribute *devattr, const char *buf,
349 size_t count)
350{
351 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Jean Delvaref641b582007-06-09 10:11:16 -0400352 struct pc87360_data *data = dev_get_drvdata(dev);
Jim Cromief0986bd2005-09-02 22:52:43 +0200353 long val = simple_strtol(buf, NULL, 10);
354
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100355 mutex_lock(&data->update_lock);
Jim Cromie694fa052005-09-02 22:57:52 +0200356 data->pwm[attr->index] = PWM_TO_REG(val,
357 FAN_CONFIG_INVERT(data->fan_conf, attr->index));
358 pc87360_write_value(data, LD_FAN, NO_BANK, PC87360_REG_PWM(attr->index),
359 data->pwm[attr->index]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100360 mutex_unlock(&data->update_lock);
Jim Cromief0986bd2005-09-02 22:52:43 +0200361 return count;
362}
363
Jim Cromiededc6a72006-01-09 23:24:41 +0100364static struct sensor_device_attribute pwm[] = {
365 SENSOR_ATTR(pwm1, S_IWUSR | S_IRUGO, show_pwm, set_pwm, 0),
366 SENSOR_ATTR(pwm2, S_IWUSR | S_IRUGO, show_pwm, set_pwm, 1),
367 SENSOR_ATTR(pwm3, S_IWUSR | S_IRUGO, show_pwm, set_pwm, 2),
368};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
Jim Cromie941c5c02006-09-24 21:02:44 +0200370static struct attribute * pc8736x_fan_attr_array[] = {
371 FAN_UNIT_ATTRS(0),
372 FAN_UNIT_ATTRS(1),
373 FAN_UNIT_ATTRS(2),
374 &pwm[0].dev_attr.attr,
375 &pwm[1].dev_attr.attr,
376 &pwm[2].dev_attr.attr,
377 NULL
378};
379static const struct attribute_group pc8736x_fan_group = {
380 .attrs = pc8736x_fan_attr_array,
381};
382
Jim Cromief0986bd2005-09-02 22:52:43 +0200383static ssize_t show_in_input(struct device *dev, struct device_attribute *devattr, char *buf)
384{
385 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
386 struct pc87360_data *data = pc87360_update_device(dev);
387 return sprintf(buf, "%u\n", IN_FROM_REG(data->in[attr->index],
388 data->in_vref));
389}
390static ssize_t show_in_min(struct device *dev, struct device_attribute *devattr, char *buf)
391{
392 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
393 struct pc87360_data *data = pc87360_update_device(dev);
394 return sprintf(buf, "%u\n", IN_FROM_REG(data->in_min[attr->index],
395 data->in_vref));
396}
397static ssize_t show_in_max(struct device *dev, struct device_attribute *devattr, char *buf)
398{
399 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
400 struct pc87360_data *data = pc87360_update_device(dev);
401 return sprintf(buf, "%u\n", IN_FROM_REG(data->in_max[attr->index],
402 data->in_vref));
403}
404static ssize_t show_in_status(struct device *dev, struct device_attribute *devattr, char *buf)
405{
406 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
407 struct pc87360_data *data = pc87360_update_device(dev);
408 return sprintf(buf, "%u\n", data->in_status[attr->index]);
409}
410static ssize_t set_in_min(struct device *dev, struct device_attribute *devattr, const char *buf,
411 size_t count)
412{
413 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Jean Delvaref641b582007-06-09 10:11:16 -0400414 struct pc87360_data *data = dev_get_drvdata(dev);
Jim Cromief0986bd2005-09-02 22:52:43 +0200415 long val = simple_strtol(buf, NULL, 10);
416
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100417 mutex_lock(&data->update_lock);
Jim Cromief0986bd2005-09-02 22:52:43 +0200418 data->in_min[attr->index] = IN_TO_REG(val, data->in_vref);
419 pc87360_write_value(data, LD_IN, attr->index, PC87365_REG_IN_MIN,
420 data->in_min[attr->index]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100421 mutex_unlock(&data->update_lock);
Jim Cromief0986bd2005-09-02 22:52:43 +0200422 return count;
423}
424static ssize_t set_in_max(struct device *dev, struct device_attribute *devattr, const char *buf,
425 size_t count)
426{
427 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Jean Delvaref641b582007-06-09 10:11:16 -0400428 struct pc87360_data *data = dev_get_drvdata(dev);
Jim Cromief0986bd2005-09-02 22:52:43 +0200429 long val = simple_strtol(buf, NULL, 10);
430
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100431 mutex_lock(&data->update_lock);
Jim Cromief0986bd2005-09-02 22:52:43 +0200432 data->in_max[attr->index] = IN_TO_REG(val,
433 data->in_vref);
434 pc87360_write_value(data, LD_IN, attr->index, PC87365_REG_IN_MAX,
435 data->in_max[attr->index]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100436 mutex_unlock(&data->update_lock);
Jim Cromief0986bd2005-09-02 22:52:43 +0200437 return count;
438}
439
Jim Cromiededc6a72006-01-09 23:24:41 +0100440static struct sensor_device_attribute in_input[] = {
441 SENSOR_ATTR(in0_input, S_IRUGO, show_in_input, NULL, 0),
442 SENSOR_ATTR(in1_input, S_IRUGO, show_in_input, NULL, 1),
443 SENSOR_ATTR(in2_input, S_IRUGO, show_in_input, NULL, 2),
444 SENSOR_ATTR(in3_input, S_IRUGO, show_in_input, NULL, 3),
445 SENSOR_ATTR(in4_input, S_IRUGO, show_in_input, NULL, 4),
446 SENSOR_ATTR(in5_input, S_IRUGO, show_in_input, NULL, 5),
447 SENSOR_ATTR(in6_input, S_IRUGO, show_in_input, NULL, 6),
448 SENSOR_ATTR(in7_input, S_IRUGO, show_in_input, NULL, 7),
449 SENSOR_ATTR(in8_input, S_IRUGO, show_in_input, NULL, 8),
450 SENSOR_ATTR(in9_input, S_IRUGO, show_in_input, NULL, 9),
451 SENSOR_ATTR(in10_input, S_IRUGO, show_in_input, NULL, 10),
452};
453static struct sensor_device_attribute in_status[] = {
454 SENSOR_ATTR(in0_status, S_IRUGO, show_in_status, NULL, 0),
455 SENSOR_ATTR(in1_status, S_IRUGO, show_in_status, NULL, 1),
456 SENSOR_ATTR(in2_status, S_IRUGO, show_in_status, NULL, 2),
457 SENSOR_ATTR(in3_status, S_IRUGO, show_in_status, NULL, 3),
458 SENSOR_ATTR(in4_status, S_IRUGO, show_in_status, NULL, 4),
459 SENSOR_ATTR(in5_status, S_IRUGO, show_in_status, NULL, 5),
460 SENSOR_ATTR(in6_status, S_IRUGO, show_in_status, NULL, 6),
461 SENSOR_ATTR(in7_status, S_IRUGO, show_in_status, NULL, 7),
462 SENSOR_ATTR(in8_status, S_IRUGO, show_in_status, NULL, 8),
463 SENSOR_ATTR(in9_status, S_IRUGO, show_in_status, NULL, 9),
464 SENSOR_ATTR(in10_status, S_IRUGO, show_in_status, NULL, 10),
465};
466static struct sensor_device_attribute in_min[] = {
467 SENSOR_ATTR(in0_min, S_IWUSR | S_IRUGO, show_in_min, set_in_min, 0),
468 SENSOR_ATTR(in1_min, S_IWUSR | S_IRUGO, show_in_min, set_in_min, 1),
469 SENSOR_ATTR(in2_min, S_IWUSR | S_IRUGO, show_in_min, set_in_min, 2),
470 SENSOR_ATTR(in3_min, S_IWUSR | S_IRUGO, show_in_min, set_in_min, 3),
471 SENSOR_ATTR(in4_min, S_IWUSR | S_IRUGO, show_in_min, set_in_min, 4),
472 SENSOR_ATTR(in5_min, S_IWUSR | S_IRUGO, show_in_min, set_in_min, 5),
473 SENSOR_ATTR(in6_min, S_IWUSR | S_IRUGO, show_in_min, set_in_min, 6),
474 SENSOR_ATTR(in7_min, S_IWUSR | S_IRUGO, show_in_min, set_in_min, 7),
475 SENSOR_ATTR(in8_min, S_IWUSR | S_IRUGO, show_in_min, set_in_min, 8),
476 SENSOR_ATTR(in9_min, S_IWUSR | S_IRUGO, show_in_min, set_in_min, 9),
477 SENSOR_ATTR(in10_min, S_IWUSR | S_IRUGO, show_in_min, set_in_min, 10),
478};
479static struct sensor_device_attribute in_max[] = {
480 SENSOR_ATTR(in0_max, S_IWUSR | S_IRUGO, show_in_max, set_in_max, 0),
481 SENSOR_ATTR(in1_max, S_IWUSR | S_IRUGO, show_in_max, set_in_max, 1),
482 SENSOR_ATTR(in2_max, S_IWUSR | S_IRUGO, show_in_max, set_in_max, 2),
483 SENSOR_ATTR(in3_max, S_IWUSR | S_IRUGO, show_in_max, set_in_max, 3),
484 SENSOR_ATTR(in4_max, S_IWUSR | S_IRUGO, show_in_max, set_in_max, 4),
485 SENSOR_ATTR(in5_max, S_IWUSR | S_IRUGO, show_in_max, set_in_max, 5),
486 SENSOR_ATTR(in6_max, S_IWUSR | S_IRUGO, show_in_max, set_in_max, 6),
487 SENSOR_ATTR(in7_max, S_IWUSR | S_IRUGO, show_in_max, set_in_max, 7),
488 SENSOR_ATTR(in8_max, S_IWUSR | S_IRUGO, show_in_max, set_in_max, 8),
489 SENSOR_ATTR(in9_max, S_IWUSR | S_IRUGO, show_in_max, set_in_max, 9),
490 SENSOR_ATTR(in10_max, S_IWUSR | S_IRUGO, show_in_max, set_in_max, 10),
491};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
Jim Cromie28f74e72008-10-18 20:27:30 -0700493/* (temp & vin) channel status register alarm bits (pdf sec.11.5.12) */
494#define CHAN_ALM_MIN 0x02 /* min limit crossed */
495#define CHAN_ALM_MAX 0x04 /* max limit exceeded */
496#define TEMP_ALM_CRIT 0x08 /* temp crit exceeded (temp only) */
497
Jim Cromie492e9652008-10-18 20:27:32 -0700498/* show_in_min/max_alarm() reads data from the per-channel status
499 register (sec 11.5.12), not the vin event status registers (sec
500 11.5.2) that (legacy) show_in_alarm() resds (via data->in_alarms) */
501
502static ssize_t show_in_min_alarm(struct device *dev,
503 struct device_attribute *devattr, char *buf)
504{
505 struct pc87360_data *data = pc87360_update_device(dev);
506 unsigned nr = to_sensor_dev_attr(devattr)->index;
507
508 return sprintf(buf, "%u\n", !!(data->in_status[nr] & CHAN_ALM_MIN));
509}
510static ssize_t show_in_max_alarm(struct device *dev,
511 struct device_attribute *devattr, char *buf)
512{
513 struct pc87360_data *data = pc87360_update_device(dev);
514 unsigned nr = to_sensor_dev_attr(devattr)->index;
515
516 return sprintf(buf, "%u\n", !!(data->in_status[nr] & CHAN_ALM_MAX));
517}
518
519static struct sensor_device_attribute in_min_alarm[] = {
520 SENSOR_ATTR(in0_min_alarm, S_IRUGO, show_in_min_alarm, NULL, 0),
521 SENSOR_ATTR(in1_min_alarm, S_IRUGO, show_in_min_alarm, NULL, 1),
522 SENSOR_ATTR(in2_min_alarm, S_IRUGO, show_in_min_alarm, NULL, 2),
523 SENSOR_ATTR(in3_min_alarm, S_IRUGO, show_in_min_alarm, NULL, 3),
524 SENSOR_ATTR(in4_min_alarm, S_IRUGO, show_in_min_alarm, NULL, 4),
525 SENSOR_ATTR(in5_min_alarm, S_IRUGO, show_in_min_alarm, NULL, 5),
526 SENSOR_ATTR(in6_min_alarm, S_IRUGO, show_in_min_alarm, NULL, 6),
527 SENSOR_ATTR(in7_min_alarm, S_IRUGO, show_in_min_alarm, NULL, 7),
528 SENSOR_ATTR(in8_min_alarm, S_IRUGO, show_in_min_alarm, NULL, 8),
529 SENSOR_ATTR(in9_min_alarm, S_IRUGO, show_in_min_alarm, NULL, 9),
530 SENSOR_ATTR(in10_min_alarm, S_IRUGO, show_in_min_alarm, NULL, 10),
531};
532static struct sensor_device_attribute in_max_alarm[] = {
533 SENSOR_ATTR(in0_max_alarm, S_IRUGO, show_in_max_alarm, NULL, 0),
534 SENSOR_ATTR(in1_max_alarm, S_IRUGO, show_in_max_alarm, NULL, 1),
535 SENSOR_ATTR(in2_max_alarm, S_IRUGO, show_in_max_alarm, NULL, 2),
536 SENSOR_ATTR(in3_max_alarm, S_IRUGO, show_in_max_alarm, NULL, 3),
537 SENSOR_ATTR(in4_max_alarm, S_IRUGO, show_in_max_alarm, NULL, 4),
538 SENSOR_ATTR(in5_max_alarm, S_IRUGO, show_in_max_alarm, NULL, 5),
539 SENSOR_ATTR(in6_max_alarm, S_IRUGO, show_in_max_alarm, NULL, 6),
540 SENSOR_ATTR(in7_max_alarm, S_IRUGO, show_in_max_alarm, NULL, 7),
541 SENSOR_ATTR(in8_max_alarm, S_IRUGO, show_in_max_alarm, NULL, 8),
542 SENSOR_ATTR(in9_max_alarm, S_IRUGO, show_in_max_alarm, NULL, 9),
543 SENSOR_ATTR(in10_max_alarm, S_IRUGO, show_in_max_alarm, NULL, 10),
544};
545
Jim Cromie941c5c02006-09-24 21:02:44 +0200546#define VIN_UNIT_ATTRS(X) \
547 &in_input[X].dev_attr.attr, \
548 &in_status[X].dev_attr.attr, \
549 &in_min[X].dev_attr.attr, \
Jim Cromie492e9652008-10-18 20:27:32 -0700550 &in_max[X].dev_attr.attr, \
551 &in_min_alarm[X].dev_attr.attr, \
552 &in_max_alarm[X].dev_attr.attr
Jim Cromie941c5c02006-09-24 21:02:44 +0200553
Jim Cromie44646c12006-09-24 21:01:56 +0200554static ssize_t show_vid(struct device *dev, struct device_attribute *attr, char *buf)
555{
556 struct pc87360_data *data = pc87360_update_device(dev);
557 return sprintf(buf, "%u\n", vid_from_reg(data->vid, data->vrm));
558}
559static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL);
560
561static ssize_t show_vrm(struct device *dev, struct device_attribute *attr, char *buf)
562{
Jean Delvare90d66192007-10-08 18:24:35 +0200563 struct pc87360_data *data = dev_get_drvdata(dev);
Jim Cromie44646c12006-09-24 21:01:56 +0200564 return sprintf(buf, "%u\n", data->vrm);
565}
566static ssize_t set_vrm(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
567{
Jean Delvaref641b582007-06-09 10:11:16 -0400568 struct pc87360_data *data = dev_get_drvdata(dev);
Jim Cromie44646c12006-09-24 21:01:56 +0200569 data->vrm = simple_strtoul(buf, NULL, 10);
570 return count;
571}
572static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm, set_vrm);
573
574static ssize_t show_in_alarms(struct device *dev, struct device_attribute *attr, char *buf)
575{
576 struct pc87360_data *data = pc87360_update_device(dev);
577 return sprintf(buf, "%u\n", data->in_alarms);
578}
579static DEVICE_ATTR(alarms_in, S_IRUGO, show_in_alarms, NULL);
580
Jim Cromie941c5c02006-09-24 21:02:44 +0200581static struct attribute *pc8736x_vin_attr_array[] = {
582 VIN_UNIT_ATTRS(0),
583 VIN_UNIT_ATTRS(1),
584 VIN_UNIT_ATTRS(2),
585 VIN_UNIT_ATTRS(3),
586 VIN_UNIT_ATTRS(4),
587 VIN_UNIT_ATTRS(5),
588 VIN_UNIT_ATTRS(6),
589 VIN_UNIT_ATTRS(7),
590 VIN_UNIT_ATTRS(8),
591 VIN_UNIT_ATTRS(9),
592 VIN_UNIT_ATTRS(10),
593 &dev_attr_cpu0_vid.attr,
594 &dev_attr_vrm.attr,
595 &dev_attr_alarms_in.attr,
596 NULL
597};
598static const struct attribute_group pc8736x_vin_group = {
599 .attrs = pc8736x_vin_attr_array,
600};
601
Jim Cromief0986bd2005-09-02 22:52:43 +0200602static ssize_t show_therm_input(struct device *dev, struct device_attribute *devattr, char *buf)
603{
604 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
605 struct pc87360_data *data = pc87360_update_device(dev);
Jim Cromie694fa052005-09-02 22:57:52 +0200606 return sprintf(buf, "%u\n", IN_FROM_REG(data->in[attr->index],
Jim Cromief0986bd2005-09-02 22:52:43 +0200607 data->in_vref));
608}
609static ssize_t show_therm_min(struct device *dev, struct device_attribute *devattr, char *buf)
610{
611 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
612 struct pc87360_data *data = pc87360_update_device(dev);
Jim Cromie694fa052005-09-02 22:57:52 +0200613 return sprintf(buf, "%u\n", IN_FROM_REG(data->in_min[attr->index],
Jim Cromief0986bd2005-09-02 22:52:43 +0200614 data->in_vref));
615}
616static ssize_t show_therm_max(struct device *dev, struct device_attribute *devattr, char *buf)
617{
618 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
619 struct pc87360_data *data = pc87360_update_device(dev);
Jim Cromie694fa052005-09-02 22:57:52 +0200620 return sprintf(buf, "%u\n", IN_FROM_REG(data->in_max[attr->index],
Jim Cromief0986bd2005-09-02 22:52:43 +0200621 data->in_vref));
622}
623static ssize_t show_therm_crit(struct device *dev, struct device_attribute *devattr, char *buf)
624{
625 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
626 struct pc87360_data *data = pc87360_update_device(dev);
Jim Cromie694fa052005-09-02 22:57:52 +0200627 return sprintf(buf, "%u\n", IN_FROM_REG(data->in_crit[attr->index-11],
Jim Cromief0986bd2005-09-02 22:52:43 +0200628 data->in_vref));
629}
630static ssize_t show_therm_status(struct device *dev, struct device_attribute *devattr, char *buf)
631{
632 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
633 struct pc87360_data *data = pc87360_update_device(dev);
Jim Cromie694fa052005-09-02 22:57:52 +0200634 return sprintf(buf, "%u\n", data->in_status[attr->index]);
Jim Cromief0986bd2005-09-02 22:52:43 +0200635}
636static ssize_t set_therm_min(struct device *dev, struct device_attribute *devattr, const char *buf,
637 size_t count)
638{
639 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Jean Delvaref641b582007-06-09 10:11:16 -0400640 struct pc87360_data *data = dev_get_drvdata(dev);
Jim Cromief0986bd2005-09-02 22:52:43 +0200641 long val = simple_strtol(buf, NULL, 10);
642
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100643 mutex_lock(&data->update_lock);
Jim Cromie694fa052005-09-02 22:57:52 +0200644 data->in_min[attr->index] = IN_TO_REG(val, data->in_vref);
645 pc87360_write_value(data, LD_IN, attr->index, PC87365_REG_TEMP_MIN,
646 data->in_min[attr->index]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100647 mutex_unlock(&data->update_lock);
Jim Cromief0986bd2005-09-02 22:52:43 +0200648 return count;
649}
650static ssize_t set_therm_max(struct device *dev, struct device_attribute *devattr, const char *buf,
651 size_t count)
652{
653 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Jean Delvaref641b582007-06-09 10:11:16 -0400654 struct pc87360_data *data = dev_get_drvdata(dev);
Jim Cromief0986bd2005-09-02 22:52:43 +0200655 long val = simple_strtol(buf, NULL, 10);
656
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100657 mutex_lock(&data->update_lock);
Jim Cromie694fa052005-09-02 22:57:52 +0200658 data->in_max[attr->index] = IN_TO_REG(val, data->in_vref);
659 pc87360_write_value(data, LD_IN, attr->index, PC87365_REG_TEMP_MAX,
660 data->in_max[attr->index]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100661 mutex_unlock(&data->update_lock);
Jim Cromief0986bd2005-09-02 22:52:43 +0200662 return count;
663}
664static ssize_t set_therm_crit(struct device *dev, struct device_attribute *devattr, const char *buf,
665 size_t count)
666{
667 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Jean Delvaref641b582007-06-09 10:11:16 -0400668 struct pc87360_data *data = dev_get_drvdata(dev);
Jim Cromief0986bd2005-09-02 22:52:43 +0200669 long val = simple_strtol(buf, NULL, 10);
670
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100671 mutex_lock(&data->update_lock);
Jim Cromie694fa052005-09-02 22:57:52 +0200672 data->in_crit[attr->index-11] = IN_TO_REG(val, data->in_vref);
673 pc87360_write_value(data, LD_IN, attr->index, PC87365_REG_TEMP_CRIT,
674 data->in_crit[attr->index-11]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100675 mutex_unlock(&data->update_lock);
Jim Cromief0986bd2005-09-02 22:52:43 +0200676 return count;
677}
678
Jim Cromiededc6a72006-01-09 23:24:41 +0100679/* the +11 term below reflects the fact that VLM units 11,12,13 are
680 used in the chip to measure voltage across the thermistors
681*/
682static struct sensor_device_attribute therm_input[] = {
683 SENSOR_ATTR(temp4_input, S_IRUGO, show_therm_input, NULL, 0+11),
684 SENSOR_ATTR(temp5_input, S_IRUGO, show_therm_input, NULL, 1+11),
685 SENSOR_ATTR(temp6_input, S_IRUGO, show_therm_input, NULL, 2+11),
686};
687static struct sensor_device_attribute therm_status[] = {
688 SENSOR_ATTR(temp4_status, S_IRUGO, show_therm_status, NULL, 0+11),
689 SENSOR_ATTR(temp5_status, S_IRUGO, show_therm_status, NULL, 1+11),
690 SENSOR_ATTR(temp6_status, S_IRUGO, show_therm_status, NULL, 2+11),
691};
692static struct sensor_device_attribute therm_min[] = {
693 SENSOR_ATTR(temp4_min, S_IRUGO | S_IWUSR,
694 show_therm_min, set_therm_min, 0+11),
695 SENSOR_ATTR(temp5_min, S_IRUGO | S_IWUSR,
696 show_therm_min, set_therm_min, 1+11),
697 SENSOR_ATTR(temp6_min, S_IRUGO | S_IWUSR,
698 show_therm_min, set_therm_min, 2+11),
699};
700static struct sensor_device_attribute therm_max[] = {
701 SENSOR_ATTR(temp4_max, S_IRUGO | S_IWUSR,
702 show_therm_max, set_therm_max, 0+11),
703 SENSOR_ATTR(temp5_max, S_IRUGO | S_IWUSR,
704 show_therm_max, set_therm_max, 1+11),
705 SENSOR_ATTR(temp6_max, S_IRUGO | S_IWUSR,
706 show_therm_max, set_therm_max, 2+11),
707};
708static struct sensor_device_attribute therm_crit[] = {
709 SENSOR_ATTR(temp4_crit, S_IRUGO | S_IWUSR,
710 show_therm_crit, set_therm_crit, 0+11),
711 SENSOR_ATTR(temp5_crit, S_IRUGO | S_IWUSR,
712 show_therm_crit, set_therm_crit, 1+11),
713 SENSOR_ATTR(temp6_crit, S_IRUGO | S_IWUSR,
714 show_therm_crit, set_therm_crit, 2+11),
715};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
Jim Cromie941c5c02006-09-24 21:02:44 +0200717#define THERM_UNIT_ATTRS(X) \
718 &therm_input[X].dev_attr.attr, \
719 &therm_status[X].dev_attr.attr, \
720 &therm_min[X].dev_attr.attr, \
721 &therm_max[X].dev_attr.attr, \
722 &therm_crit[X].dev_attr.attr
723
724static struct attribute * pc8736x_therm_attr_array[] = {
725 THERM_UNIT_ATTRS(0),
726 THERM_UNIT_ATTRS(1),
727 THERM_UNIT_ATTRS(2),
728 NULL
729};
730static const struct attribute_group pc8736x_therm_group = {
731 .attrs = pc8736x_therm_attr_array,
732};
733
Jim Cromief0986bd2005-09-02 22:52:43 +0200734static ssize_t show_temp_input(struct device *dev, struct device_attribute *devattr, char *buf)
735{
736 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
737 struct pc87360_data *data = pc87360_update_device(dev);
Jim Cromie694fa052005-09-02 22:57:52 +0200738 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[attr->index]));
Jim Cromief0986bd2005-09-02 22:52:43 +0200739}
740static ssize_t show_temp_min(struct device *dev, struct device_attribute *devattr, char *buf)
741{
742 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
743 struct pc87360_data *data = pc87360_update_device(dev);
Jim Cromie694fa052005-09-02 22:57:52 +0200744 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_min[attr->index]));
Jim Cromief0986bd2005-09-02 22:52:43 +0200745}
746static ssize_t show_temp_max(struct device *dev, struct device_attribute *devattr, char *buf)
747{
748 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
749 struct pc87360_data *data = pc87360_update_device(dev);
Jim Cromie694fa052005-09-02 22:57:52 +0200750 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max[attr->index]));
Jim Cromief0986bd2005-09-02 22:52:43 +0200751}
752static ssize_t show_temp_crit(struct device *dev, struct device_attribute *devattr, char *buf)
753{
754 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
755 struct pc87360_data *data = pc87360_update_device(dev);
Jim Cromie694fa052005-09-02 22:57:52 +0200756 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_crit[attr->index]));
Jim Cromief0986bd2005-09-02 22:52:43 +0200757}
758static ssize_t show_temp_status(struct device *dev, struct device_attribute *devattr, char *buf)
759{
760 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
761 struct pc87360_data *data = pc87360_update_device(dev);
Jim Cromie694fa052005-09-02 22:57:52 +0200762 return sprintf(buf, "%d\n", data->temp_status[attr->index]);
Jim Cromief0986bd2005-09-02 22:52:43 +0200763}
764static ssize_t set_temp_min(struct device *dev, struct device_attribute *devattr, const char *buf,
765 size_t count)
766{
767 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Jean Delvaref641b582007-06-09 10:11:16 -0400768 struct pc87360_data *data = dev_get_drvdata(dev);
Jim Cromief0986bd2005-09-02 22:52:43 +0200769 long val = simple_strtol(buf, NULL, 10);
770
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100771 mutex_lock(&data->update_lock);
Jim Cromie694fa052005-09-02 22:57:52 +0200772 data->temp_min[attr->index] = TEMP_TO_REG(val);
773 pc87360_write_value(data, LD_TEMP, attr->index, PC87365_REG_TEMP_MIN,
774 data->temp_min[attr->index]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100775 mutex_unlock(&data->update_lock);
Jim Cromief0986bd2005-09-02 22:52:43 +0200776 return count;
777}
778static ssize_t set_temp_max(struct device *dev, struct device_attribute *devattr, const char *buf,
779 size_t count)
780{
781 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Jean Delvaref641b582007-06-09 10:11:16 -0400782 struct pc87360_data *data = dev_get_drvdata(dev);
Jim Cromief0986bd2005-09-02 22:52:43 +0200783 long val = simple_strtol(buf, NULL, 10);
784
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100785 mutex_lock(&data->update_lock);
Jim Cromie694fa052005-09-02 22:57:52 +0200786 data->temp_max[attr->index] = TEMP_TO_REG(val);
787 pc87360_write_value(data, LD_TEMP, attr->index, PC87365_REG_TEMP_MAX,
788 data->temp_max[attr->index]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100789 mutex_unlock(&data->update_lock);
Jim Cromief0986bd2005-09-02 22:52:43 +0200790 return count;
791}
792static ssize_t set_temp_crit(struct device *dev, struct device_attribute *devattr, const char *buf,
793 size_t count)
794{
795 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Jean Delvaref641b582007-06-09 10:11:16 -0400796 struct pc87360_data *data = dev_get_drvdata(dev);
Jim Cromief0986bd2005-09-02 22:52:43 +0200797 long val = simple_strtol(buf, NULL, 10);
798
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100799 mutex_lock(&data->update_lock);
Jim Cromie694fa052005-09-02 22:57:52 +0200800 data->temp_crit[attr->index] = TEMP_TO_REG(val);
801 pc87360_write_value(data, LD_TEMP, attr->index, PC87365_REG_TEMP_CRIT,
802 data->temp_crit[attr->index]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100803 mutex_unlock(&data->update_lock);
Jim Cromief0986bd2005-09-02 22:52:43 +0200804 return count;
805}
806
Jim Cromiededc6a72006-01-09 23:24:41 +0100807static struct sensor_device_attribute temp_input[] = {
808 SENSOR_ATTR(temp1_input, S_IRUGO, show_temp_input, NULL, 0),
809 SENSOR_ATTR(temp2_input, S_IRUGO, show_temp_input, NULL, 1),
810 SENSOR_ATTR(temp3_input, S_IRUGO, show_temp_input, NULL, 2),
811};
812static struct sensor_device_attribute temp_status[] = {
813 SENSOR_ATTR(temp1_status, S_IRUGO, show_temp_status, NULL, 0),
814 SENSOR_ATTR(temp2_status, S_IRUGO, show_temp_status, NULL, 1),
815 SENSOR_ATTR(temp3_status, S_IRUGO, show_temp_status, NULL, 2),
816};
817static struct sensor_device_attribute temp_min[] = {
818 SENSOR_ATTR(temp1_min, S_IRUGO | S_IWUSR,
819 show_temp_min, set_temp_min, 0),
820 SENSOR_ATTR(temp2_min, S_IRUGO | S_IWUSR,
821 show_temp_min, set_temp_min, 1),
822 SENSOR_ATTR(temp3_min, S_IRUGO | S_IWUSR,
823 show_temp_min, set_temp_min, 2),
824};
825static struct sensor_device_attribute temp_max[] = {
826 SENSOR_ATTR(temp1_max, S_IRUGO | S_IWUSR,
827 show_temp_max, set_temp_max, 0),
828 SENSOR_ATTR(temp2_max, S_IRUGO | S_IWUSR,
829 show_temp_max, set_temp_max, 1),
830 SENSOR_ATTR(temp3_max, S_IRUGO | S_IWUSR,
831 show_temp_max, set_temp_max, 2),
832};
833static struct sensor_device_attribute temp_crit[] = {
834 SENSOR_ATTR(temp1_crit, S_IRUGO | S_IWUSR,
835 show_temp_crit, set_temp_crit, 0),
836 SENSOR_ATTR(temp2_crit, S_IRUGO | S_IWUSR,
837 show_temp_crit, set_temp_crit, 1),
838 SENSOR_ATTR(temp3_crit, S_IRUGO | S_IWUSR,
839 show_temp_crit, set_temp_crit, 2),
840};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841
Yani Ioannoua5099cf2005-05-17 06:42:25 -0400842static ssize_t show_temp_alarms(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843{
844 struct pc87360_data *data = pc87360_update_device(dev);
845 return sprintf(buf, "%u\n", data->temp_alarms);
846}
847static DEVICE_ATTR(alarms_temp, S_IRUGO, show_temp_alarms, NULL);
848
Jim Cromieb267e8c2008-10-18 20:27:32 -0700849/* show_temp_min/max_alarm() reads data from the per-channel status
850 register (sec 12.3.7), not the temp event status registers (sec
851 12.3.2) that show_temp_alarm() reads (via data->temp_alarms) */
852
853static ssize_t show_temp_min_alarm(struct device *dev,
854 struct device_attribute *devattr, char *buf)
855{
856 struct pc87360_data *data = pc87360_update_device(dev);
857 unsigned nr = to_sensor_dev_attr(devattr)->index;
858
859 return sprintf(buf, "%u\n", !!(data->temp_status[nr] & CHAN_ALM_MIN));
860}
861static ssize_t show_temp_max_alarm(struct device *dev,
862 struct device_attribute *devattr, char *buf)
863{
864 struct pc87360_data *data = pc87360_update_device(dev);
865 unsigned nr = to_sensor_dev_attr(devattr)->index;
866
867 return sprintf(buf, "%u\n", !!(data->temp_status[nr] & CHAN_ALM_MAX));
868}
869static ssize_t show_temp_crit_alarm(struct device *dev,
870 struct device_attribute *devattr, char *buf)
871{
872 struct pc87360_data *data = pc87360_update_device(dev);
873 unsigned nr = to_sensor_dev_attr(devattr)->index;
874
875 return sprintf(buf, "%u\n", !!(data->temp_status[nr] & TEMP_ALM_CRIT));
876}
877
878static struct sensor_device_attribute temp_min_alarm[] = {
879 SENSOR_ATTR(temp1_min_alarm, S_IRUGO, show_temp_min_alarm, NULL, 0),
880 SENSOR_ATTR(temp2_min_alarm, S_IRUGO, show_temp_min_alarm, NULL, 1),
881 SENSOR_ATTR(temp3_min_alarm, S_IRUGO, show_temp_min_alarm, NULL, 2),
882};
883static struct sensor_device_attribute temp_max_alarm[] = {
884 SENSOR_ATTR(temp1_max_alarm, S_IRUGO, show_temp_max_alarm, NULL, 0),
885 SENSOR_ATTR(temp2_max_alarm, S_IRUGO, show_temp_max_alarm, NULL, 1),
886 SENSOR_ATTR(temp3_max_alarm, S_IRUGO, show_temp_max_alarm, NULL, 2),
887};
888static struct sensor_device_attribute temp_crit_alarm[] = {
889 SENSOR_ATTR(temp1_crit_alarm, S_IRUGO, show_temp_crit_alarm, NULL, 0),
890 SENSOR_ATTR(temp2_crit_alarm, S_IRUGO, show_temp_crit_alarm, NULL, 1),
891 SENSOR_ATTR(temp3_crit_alarm, S_IRUGO, show_temp_crit_alarm, NULL, 2),
892};
893
894#define TEMP_FAULT 0x40 /* open diode */
895static ssize_t show_temp_fault(struct device *dev,
896 struct device_attribute *devattr, char *buf)
897{
898 struct pc87360_data *data = pc87360_update_device(dev);
899 unsigned nr = to_sensor_dev_attr(devattr)->index;
900
901 return sprintf(buf, "%u\n", !!(data->temp_status[nr] & TEMP_FAULT));
902}
903static struct sensor_device_attribute temp_fault[] = {
904 SENSOR_ATTR(temp1_fault, S_IRUGO, show_temp_fault, NULL, 0),
905 SENSOR_ATTR(temp2_fault, S_IRUGO, show_temp_fault, NULL, 1),
906 SENSOR_ATTR(temp3_fault, S_IRUGO, show_temp_fault, NULL, 2),
907};
908
Jim Cromie941c5c02006-09-24 21:02:44 +0200909#define TEMP_UNIT_ATTRS(X) \
910 &temp_input[X].dev_attr.attr, \
911 &temp_status[X].dev_attr.attr, \
912 &temp_min[X].dev_attr.attr, \
913 &temp_max[X].dev_attr.attr, \
Jim Cromieb267e8c2008-10-18 20:27:32 -0700914 &temp_crit[X].dev_attr.attr, \
915 &temp_min_alarm[X].dev_attr.attr, \
916 &temp_max_alarm[X].dev_attr.attr, \
917 &temp_crit_alarm[X].dev_attr.attr, \
918 &temp_fault[X].dev_attr.attr
Jim Cromie941c5c02006-09-24 21:02:44 +0200919
920static struct attribute * pc8736x_temp_attr_array[] = {
921 TEMP_UNIT_ATTRS(0),
922 TEMP_UNIT_ATTRS(1),
923 TEMP_UNIT_ATTRS(2),
924 /* include the few miscellaneous atts here */
925 &dev_attr_alarms_temp.attr,
926 NULL
927};
928static const struct attribute_group pc8736x_temp_group = {
929 .attrs = pc8736x_temp_attr_array,
930};
931
Jim Cromieb267e8c2008-10-18 20:27:32 -0700932static ssize_t show_name(struct device *dev,
933 struct device_attribute *devattr, char *buf)
Jean Delvaref641b582007-06-09 10:11:16 -0400934{
935 struct pc87360_data *data = dev_get_drvdata(dev);
936 return sprintf(buf, "%s\n", data->name);
937}
938static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
939
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940/*
941 * Device detection, registration and update
942 */
943
Jean Delvaree6cfb3a2005-07-27 21:32:02 +0200944static int __init pc87360_find(int sioaddr, u8 *devid, unsigned short *addresses)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945{
946 u16 val;
947 int i;
948 int nrdev; /* logical device count */
949
950 /* No superio_enter */
951
952 /* Identify device */
Jean Delvare67b671b2007-12-06 23:13:42 +0100953 val = force_id ? force_id : superio_inb(sioaddr, DEVID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 switch (val) {
955 case 0xE1: /* PC87360 */
956 case 0xE8: /* PC87363 */
957 case 0xE4: /* PC87364 */
958 nrdev = 1;
959 break;
960 case 0xE5: /* PC87365 */
961 case 0xE9: /* PC87366 */
962 nrdev = 3;
963 break;
964 default:
965 superio_exit(sioaddr);
966 return -ENODEV;
967 }
968 /* Remember the device id */
969 *devid = val;
970
971 for (i = 0; i < nrdev; i++) {
972 /* select logical device */
973 superio_outb(sioaddr, DEV, logdev[i]);
974
975 val = superio_inb(sioaddr, ACT);
976 if (!(val & 0x01)) {
977 printk(KERN_INFO "pc87360: Device 0x%02x not "
978 "activated\n", logdev[i]);
979 continue;
980 }
981
982 val = (superio_inb(sioaddr, BASE) << 8)
983 | superio_inb(sioaddr, BASE + 1);
984 if (!val) {
985 printk(KERN_INFO "pc87360: Base address not set for "
986 "device 0x%02x\n", logdev[i]);
987 continue;
988 }
989
Jean Delvare2d8672c2005-07-19 23:56:35 +0200990 addresses[i] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991
992 if (i==0) { /* Fans */
993 confreg[0] = superio_inb(sioaddr, 0xF0);
994 confreg[1] = superio_inb(sioaddr, 0xF1);
995
996#ifdef DEBUG
997 printk(KERN_DEBUG "pc87360: Fan 1: mon=%d "
998 "ctrl=%d inv=%d\n", (confreg[0]>>2)&1,
999 (confreg[0]>>3)&1, (confreg[0]>>4)&1);
1000 printk(KERN_DEBUG "pc87360: Fan 2: mon=%d "
1001 "ctrl=%d inv=%d\n", (confreg[0]>>5)&1,
1002 (confreg[0]>>6)&1, (confreg[0]>>7)&1);
1003 printk(KERN_DEBUG "pc87360: Fan 3: mon=%d "
1004 "ctrl=%d inv=%d\n", confreg[1]&1,
1005 (confreg[1]>>1)&1, (confreg[1]>>2)&1);
1006#endif
1007 } else if (i==1) { /* Voltages */
1008 /* Are we using thermistors? */
1009 if (*devid == 0xE9) { /* PC87366 */
1010 /* These registers are not logical-device
1011 specific, just that we won't need them if
1012 we don't use the VLM device */
1013 confreg[2] = superio_inb(sioaddr, 0x2B);
1014 confreg[3] = superio_inb(sioaddr, 0x25);
1015
1016 if (confreg[2] & 0x40) {
1017 printk(KERN_INFO "pc87360: Using "
1018 "thermistors for temperature "
1019 "monitoring\n");
1020 }
1021 if (confreg[3] & 0xE0) {
1022 printk(KERN_INFO "pc87360: VID "
1023 "inputs routed (mode %u)\n",
1024 confreg[3] >> 5);
1025 }
1026 }
1027 }
1028 }
1029
1030 superio_exit(sioaddr);
1031 return 0;
1032}
1033
Jean Delvaref641b582007-06-09 10:11:16 -04001034static int __devinit pc87360_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035{
1036 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 struct pc87360_data *data;
1038 int err = 0;
1039 const char *name = "pc87360";
1040 int use_thermistors = 0;
Jean Delvaref641b582007-06-09 10:11:16 -04001041 struct device *dev = &pdev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042
Deepak Saxenaba9c2e82005-10-17 23:08:32 +02001043 if (!(data = kzalloc(sizeof(struct pc87360_data), GFP_KERNEL)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 data->fannr = 2;
1047 data->innr = 0;
1048 data->tempnr = 0;
1049
1050 switch (devid) {
1051 case 0xe8:
1052 name = "pc87363";
1053 break;
1054 case 0xe4:
1055 name = "pc87364";
1056 data->fannr = 3;
1057 break;
1058 case 0xe5:
1059 name = "pc87365";
1060 data->fannr = extra_isa[0] ? 3 : 0;
1061 data->innr = extra_isa[1] ? 11 : 0;
1062 data->tempnr = extra_isa[2] ? 2 : 0;
1063 break;
1064 case 0xe9:
1065 name = "pc87366";
1066 data->fannr = extra_isa[0] ? 3 : 0;
1067 data->innr = extra_isa[1] ? 14 : 0;
1068 data->tempnr = extra_isa[2] ? 3 : 0;
1069 break;
1070 }
1071
Jean Delvaref641b582007-06-09 10:11:16 -04001072 data->name = name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 data->valid = 0;
Jean Delvaref641b582007-06-09 10:11:16 -04001074 mutex_init(&data->lock);
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001075 mutex_init(&data->update_lock);
Jean Delvaref641b582007-06-09 10:11:16 -04001076 platform_set_drvdata(pdev, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077
Jim Cromie2a32ec22008-10-18 20:27:33 -07001078 for (i = 0; i < LDNI_MAX; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 if (((data->address[i] = extra_isa[i]))
1080 && !request_region(extra_isa[i], PC87360_EXTENT,
Laurent Riffardcdaf7932005-11-26 20:37:41 +01001081 pc87360_driver.driver.name)) {
Jean Delvaref641b582007-06-09 10:11:16 -04001082 dev_err(dev, "Region 0x%x-0x%x already "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 "in use!\n", extra_isa[i],
1084 extra_isa[i]+PC87360_EXTENT-1);
1085 for (i--; i >= 0; i--)
1086 release_region(extra_isa[i], PC87360_EXTENT);
1087 err = -EBUSY;
1088 goto ERROR1;
1089 }
1090 }
1091
1092 /* Retrieve the fans configuration from Super-I/O space */
1093 if (data->fannr)
1094 data->fan_conf = confreg[0] | (confreg[1] << 8);
1095
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 /* Use the correct reference voltage
1097 Unless both the VLM and the TMS logical devices agree to
1098 use an external Vref, the internal one is used. */
1099 if (data->innr) {
1100 i = pc87360_read_value(data, LD_IN, NO_BANK,
1101 PC87365_REG_IN_CONFIG);
1102 if (data->tempnr) {
1103 i &= pc87360_read_value(data, LD_TEMP, NO_BANK,
1104 PC87365_REG_TEMP_CONFIG);
1105 }
1106 data->in_vref = (i&0x02) ? 3025 : 2966;
Jean Delvaref641b582007-06-09 10:11:16 -04001107 dev_dbg(dev, "Using %s reference voltage\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 (i&0x02) ? "external" : "internal");
1109
1110 data->vid_conf = confreg[3];
Jim Cromie3d7f9a82006-12-12 18:18:28 +01001111 data->vrm = vid_which_vrm();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 }
1113
1114 /* Fan clock dividers may be needed before any data is read */
1115 for (i = 0; i < data->fannr; i++) {
1116 if (FAN_CONFIG_MONITOR(data->fan_conf, i))
1117 data->fan_status[i] = pc87360_read_value(data,
1118 LD_FAN, NO_BANK,
1119 PC87360_REG_FAN_STATUS(i));
1120 }
1121
1122 if (init > 0) {
1123 if (devid == 0xe9 && data->address[1]) /* PC87366 */
1124 use_thermistors = confreg[2] & 0x40;
1125
Jean Delvaref641b582007-06-09 10:11:16 -04001126 pc87360_init_device(pdev, use_thermistors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 }
1128
Jim Cromief3722d5b2006-09-24 21:03:25 +02001129 /* Register all-or-nothing sysfs groups */
1130
1131 if (data->innr &&
Jean Delvaref641b582007-06-09 10:11:16 -04001132 (err = sysfs_create_group(&dev->kobj,
Jim Cromief3722d5b2006-09-24 21:03:25 +02001133 &pc8736x_vin_group)))
1134 goto ERROR3;
1135
1136 if (data->innr == 14 &&
Jean Delvaref641b582007-06-09 10:11:16 -04001137 (err = sysfs_create_group(&dev->kobj,
Jim Cromief3722d5b2006-09-24 21:03:25 +02001138 &pc8736x_therm_group)))
1139 goto ERROR3;
1140
1141 /* create device attr-files for varying sysfs groups */
1142
1143 if (data->tempnr) {
1144 for (i = 0; i < data->tempnr; i++) {
1145 if ((err = device_create_file(dev,
1146 &temp_input[i].dev_attr))
1147 || (err = device_create_file(dev,
1148 &temp_min[i].dev_attr))
1149 || (err = device_create_file(dev,
1150 &temp_max[i].dev_attr))
1151 || (err = device_create_file(dev,
1152 &temp_crit[i].dev_attr))
1153 || (err = device_create_file(dev,
Jim Cromieb267e8c2008-10-18 20:27:32 -07001154 &temp_status[i].dev_attr))
1155 || (err = device_create_file(dev,
1156 &temp_min_alarm[i].dev_attr))
1157 || (err = device_create_file(dev,
1158 &temp_max_alarm[i].dev_attr))
1159 || (err = device_create_file(dev,
1160 &temp_crit_alarm[i].dev_attr))
1161 || (err = device_create_file(dev,
1162 &temp_fault[i].dev_attr)))
Jim Cromief3722d5b2006-09-24 21:03:25 +02001163 goto ERROR3;
1164 }
1165 if ((err = device_create_file(dev, &dev_attr_alarms_temp)))
1166 goto ERROR3;
1167 }
1168
1169 for (i = 0; i < data->fannr; i++) {
1170 if (FAN_CONFIG_MONITOR(data->fan_conf, i)
1171 && ((err = device_create_file(dev,
1172 &fan_input[i].dev_attr))
1173 || (err = device_create_file(dev,
1174 &fan_min[i].dev_attr))
1175 || (err = device_create_file(dev,
1176 &fan_div[i].dev_attr))
1177 || (err = device_create_file(dev,
1178 &fan_status[i].dev_attr))))
1179 goto ERROR3;
1180
1181 if (FAN_CONFIG_CONTROL(data->fan_conf, i)
1182 && (err = device_create_file(dev, &pwm[i].dev_attr)))
1183 goto ERROR3;
1184 }
1185
Jean Delvaref641b582007-06-09 10:11:16 -04001186 if ((err = device_create_file(dev, &dev_attr_name)))
1187 goto ERROR3;
1188
Tony Jones1beeffe2007-08-20 13:46:20 -07001189 data->hwmon_dev = hwmon_device_register(dev);
1190 if (IS_ERR(data->hwmon_dev)) {
1191 err = PTR_ERR(data->hwmon_dev);
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001192 goto ERROR3;
1193 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 return 0;
1195
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001196ERROR3:
Jean Delvaref641b582007-06-09 10:11:16 -04001197 device_remove_file(dev, &dev_attr_name);
Jim Cromief3722d5b2006-09-24 21:03:25 +02001198 /* can still remove groups whose members were added individually */
Jean Delvaref641b582007-06-09 10:11:16 -04001199 sysfs_remove_group(&dev->kobj, &pc8736x_temp_group);
1200 sysfs_remove_group(&dev->kobj, &pc8736x_fan_group);
1201 sysfs_remove_group(&dev->kobj, &pc8736x_therm_group);
1202 sysfs_remove_group(&dev->kobj, &pc8736x_vin_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 for (i = 0; i < 3; i++) {
1204 if (data->address[i]) {
1205 release_region(data->address[i], PC87360_EXTENT);
1206 }
1207 }
1208ERROR1:
1209 kfree(data);
1210 return err;
1211}
1212
Jean Delvaref641b582007-06-09 10:11:16 -04001213static int __devexit pc87360_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214{
Jean Delvaref641b582007-06-09 10:11:16 -04001215 struct pc87360_data *data = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 int i;
1217
Tony Jones1beeffe2007-08-20 13:46:20 -07001218 hwmon_device_unregister(data->hwmon_dev);
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001219
Jean Delvaref641b582007-06-09 10:11:16 -04001220 device_remove_file(&pdev->dev, &dev_attr_name);
1221 sysfs_remove_group(&pdev->dev.kobj, &pc8736x_temp_group);
1222 sysfs_remove_group(&pdev->dev.kobj, &pc8736x_fan_group);
1223 sysfs_remove_group(&pdev->dev.kobj, &pc8736x_therm_group);
1224 sysfs_remove_group(&pdev->dev.kobj, &pc8736x_vin_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225
1226 for (i = 0; i < 3; i++) {
1227 if (data->address[i]) {
1228 release_region(data->address[i], PC87360_EXTENT);
1229 }
1230 }
1231 kfree(data);
1232
1233 return 0;
1234}
1235
1236/* ldi is the logical device index
1237 bank is for voltages and temperatures only */
1238static int pc87360_read_value(struct pc87360_data *data, u8 ldi, u8 bank,
1239 u8 reg)
1240{
1241 int res;
1242
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001243 mutex_lock(&(data->lock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 if (bank != NO_BANK)
1245 outb_p(bank, data->address[ldi] + PC87365_REG_BANK);
1246 res = inb_p(data->address[ldi] + reg);
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001247 mutex_unlock(&(data->lock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248
1249 return res;
1250}
1251
1252static void pc87360_write_value(struct pc87360_data *data, u8 ldi, u8 bank,
1253 u8 reg, u8 value)
1254{
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001255 mutex_lock(&(data->lock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 if (bank != NO_BANK)
1257 outb_p(bank, data->address[ldi] + PC87365_REG_BANK);
1258 outb_p(value, data->address[ldi] + reg);
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001259 mutex_unlock(&(data->lock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260}
1261
Jim Cromie28f74e72008-10-18 20:27:30 -07001262/* (temp & vin) channel conversion status register flags (pdf sec.11.5.12) */
1263#define CHAN_CNVRTD 0x80 /* new data ready */
1264#define CHAN_ENA 0x01 /* enabled channel (temp or vin) */
1265#define CHAN_ALM_ENA 0x10 /* propagate to alarms-reg ?? (chk val!) */
1266#define CHAN_READY (CHAN_ENA|CHAN_CNVRTD) /* sample ready mask */
1267
Jim Cromieb267e8c2008-10-18 20:27:32 -07001268#define TEMP_OTS_OE 0x20 /* OTS Output Enable */
1269#define VIN_RW1C_MASK (CHAN_READY|CHAN_ALM_MAX|CHAN_ALM_MIN) /* 0x87 */
1270#define TEMP_RW1C_MASK (VIN_RW1C_MASK|TEMP_ALM_CRIT|TEMP_FAULT) /* 0xCF */
1271
Jean Delvaref641b582007-06-09 10:11:16 -04001272static void pc87360_init_device(struct platform_device *pdev,
1273 int use_thermistors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274{
Jean Delvaref641b582007-06-09 10:11:16 -04001275 struct pc87360_data *data = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 int i, nr;
1277 const u8 init_in[14] = { 2, 2, 2, 2, 2, 2, 2, 1, 1, 3, 1, 2, 2, 2 };
1278 const u8 init_temp[3] = { 2, 2, 1 };
1279 u8 reg;
1280
1281 if (init >= 2 && data->innr) {
1282 reg = pc87360_read_value(data, LD_IN, NO_BANK,
1283 PC87365_REG_IN_CONVRATE);
Jean Delvaref641b582007-06-09 10:11:16 -04001284 dev_info(&pdev->dev, "VLM conversion set to "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 "1s period, 160us delay\n");
1286 pc87360_write_value(data, LD_IN, NO_BANK,
1287 PC87365_REG_IN_CONVRATE,
1288 (reg & 0xC0) | 0x11);
1289 }
1290
1291 nr = data->innr < 11 ? data->innr : 11;
Jim Cromiededc6a72006-01-09 23:24:41 +01001292 for (i = 0; i < nr; i++) {
Jim Cromie8ca13672008-10-18 20:27:34 -07001293 reg = pc87360_read_value(data, LD_IN, i,
1294 PC87365_REG_IN_STATUS);
1295 dev_dbg(&pdev->dev, "bios in%d status:0x%02x\n", i, reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 if (init >= init_in[i]) {
1297 /* Forcibly enable voltage channel */
Jim Cromie28f74e72008-10-18 20:27:30 -07001298 if (!(reg & CHAN_ENA)) {
Jean Delvaref641b582007-06-09 10:11:16 -04001299 dev_dbg(&pdev->dev, "Forcibly "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 "enabling in%d\n", i);
1301 pc87360_write_value(data, LD_IN, i,
1302 PC87365_REG_IN_STATUS,
1303 (reg & 0x68) | 0x87);
1304 }
1305 }
1306 }
1307
1308 /* We can't blindly trust the Super-I/O space configuration bit,
1309 most BIOS won't set it properly */
Jim Cromie8ca13672008-10-18 20:27:34 -07001310 dev_dbg(&pdev->dev, "bios thermistors:%d\n", use_thermistors);
Jim Cromiededc6a72006-01-09 23:24:41 +01001311 for (i = 11; i < data->innr; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 reg = pc87360_read_value(data, LD_IN, i,
1313 PC87365_REG_TEMP_STATUS);
Jim Cromie28f74e72008-10-18 20:27:30 -07001314 use_thermistors = use_thermistors || (reg & CHAN_ENA);
Jim Cromie8ca13672008-10-18 20:27:34 -07001315 /* thermistors are temp[4-6], measured on vin[11-14] */
1316 dev_dbg(&pdev->dev, "bios temp%d_status:0x%02x\n", i-7, reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 }
Jim Cromie8ca13672008-10-18 20:27:34 -07001318 dev_dbg(&pdev->dev, "using thermistors:%d\n", use_thermistors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319
1320 i = use_thermistors ? 2 : 0;
Jim Cromiededc6a72006-01-09 23:24:41 +01001321 for (; i < data->tempnr; i++) {
Jim Cromie8ca13672008-10-18 20:27:34 -07001322 reg = pc87360_read_value(data, LD_TEMP, i,
1323 PC87365_REG_TEMP_STATUS);
1324 dev_dbg(&pdev->dev, "bios temp%d_status:0x%02x\n", i+1, reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 if (init >= init_temp[i]) {
1326 /* Forcibly enable temperature channel */
Jim Cromie28f74e72008-10-18 20:27:30 -07001327 if (!(reg & CHAN_ENA)) {
Jean Delvaref641b582007-06-09 10:11:16 -04001328 dev_dbg(&pdev->dev, "Forcibly "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 "enabling temp%d\n", i+1);
1330 pc87360_write_value(data, LD_TEMP, i,
1331 PC87365_REG_TEMP_STATUS,
1332 0xCF);
1333 }
1334 }
1335 }
1336
1337 if (use_thermistors) {
Jim Cromiededc6a72006-01-09 23:24:41 +01001338 for (i = 11; i < data->innr; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 if (init >= init_in[i]) {
1340 /* The pin may already be used by thermal
1341 diodes */
1342 reg = pc87360_read_value(data, LD_TEMP,
1343 (i-11)/2, PC87365_REG_TEMP_STATUS);
Jim Cromie28f74e72008-10-18 20:27:30 -07001344 if (reg & CHAN_ENA) {
Jean Delvaref641b582007-06-09 10:11:16 -04001345 dev_dbg(&pdev->dev, "Skipping "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 "temp%d, pin already in use "
1347 "by temp%d\n", i-7, (i-11)/2);
1348 continue;
1349 }
1350
1351 /* Forcibly enable thermistor channel */
1352 reg = pc87360_read_value(data, LD_IN, i,
1353 PC87365_REG_IN_STATUS);
Jim Cromie28f74e72008-10-18 20:27:30 -07001354 if (!(reg & CHAN_ENA)) {
Jean Delvaref641b582007-06-09 10:11:16 -04001355 dev_dbg(&pdev->dev, "Forcibly "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 "enabling temp%d\n", i-7);
1357 pc87360_write_value(data, LD_IN, i,
1358 PC87365_REG_TEMP_STATUS,
1359 (reg & 0x60) | 0x8F);
1360 }
1361 }
1362 }
1363 }
1364
1365 if (data->innr) {
1366 reg = pc87360_read_value(data, LD_IN, NO_BANK,
1367 PC87365_REG_IN_CONFIG);
Jim Cromie8ca13672008-10-18 20:27:34 -07001368 dev_dbg(&pdev->dev, "bios vin-cfg:0x%02x\n", reg);
Jim Cromie28f74e72008-10-18 20:27:30 -07001369 if (reg & CHAN_ENA) {
Jean Delvaref641b582007-06-09 10:11:16 -04001370 dev_dbg(&pdev->dev, "Forcibly "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 "enabling monitoring (VLM)\n");
1372 pc87360_write_value(data, LD_IN, NO_BANK,
1373 PC87365_REG_IN_CONFIG,
1374 reg & 0xFE);
1375 }
1376 }
1377
1378 if (data->tempnr) {
1379 reg = pc87360_read_value(data, LD_TEMP, NO_BANK,
1380 PC87365_REG_TEMP_CONFIG);
Jim Cromie8ca13672008-10-18 20:27:34 -07001381 dev_dbg(&pdev->dev, "bios temp-cfg:0x%02x\n", reg);
Jim Cromie28f74e72008-10-18 20:27:30 -07001382 if (reg & CHAN_ENA) {
Jean Delvaref641b582007-06-09 10:11:16 -04001383 dev_dbg(&pdev->dev, "Forcibly enabling "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 "monitoring (TMS)\n");
1385 pc87360_write_value(data, LD_TEMP, NO_BANK,
1386 PC87365_REG_TEMP_CONFIG,
1387 reg & 0xFE);
1388 }
1389
1390 if (init >= 2) {
1391 /* Chip config as documented by National Semi. */
1392 pc87360_write_value(data, LD_TEMP, 0xF, 0xA, 0x08);
1393 /* We voluntarily omit the bank here, in case the
1394 sequence itself matters. It shouldn't be a problem,
1395 since nobody else is supposed to access the
1396 device at that point. */
1397 pc87360_write_value(data, LD_TEMP, NO_BANK, 0xB, 0x04);
1398 pc87360_write_value(data, LD_TEMP, NO_BANK, 0xC, 0x35);
1399 pc87360_write_value(data, LD_TEMP, NO_BANK, 0xD, 0x05);
1400 pc87360_write_value(data, LD_TEMP, NO_BANK, 0xE, 0x05);
1401 }
1402 }
1403}
1404
Jean Delvaref641b582007-06-09 10:11:16 -04001405static void pc87360_autodiv(struct device *dev, int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406{
Jean Delvaref641b582007-06-09 10:11:16 -04001407 struct pc87360_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 u8 old_min = data->fan_min[nr];
1409
1410 /* Increase clock divider if needed and possible */
1411 if ((data->fan_status[nr] & 0x04) /* overflow flag */
1412 || (data->fan[nr] >= 224)) { /* next to overflow */
1413 if ((data->fan_status[nr] & 0x60) != 0x60) {
1414 data->fan_status[nr] += 0x20;
1415 data->fan_min[nr] >>= 1;
1416 data->fan[nr] >>= 1;
Jean Delvaref641b582007-06-09 10:11:16 -04001417 dev_dbg(dev, "Increasing "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 "clock divider to %d for fan %d\n",
1419 FAN_DIV_FROM_REG(data->fan_status[nr]), nr+1);
1420 }
1421 } else {
1422 /* Decrease clock divider if possible */
1423 while (!(data->fan_min[nr] & 0x80) /* min "nails" divider */
1424 && data->fan[nr] < 85 /* bad accuracy */
1425 && (data->fan_status[nr] & 0x60) != 0x00) {
1426 data->fan_status[nr] -= 0x20;
1427 data->fan_min[nr] <<= 1;
1428 data->fan[nr] <<= 1;
Jean Delvaref641b582007-06-09 10:11:16 -04001429 dev_dbg(dev, "Decreasing "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 "clock divider to %d for fan %d\n",
1431 FAN_DIV_FROM_REG(data->fan_status[nr]),
1432 nr+1);
1433 }
1434 }
1435
1436 /* Write new fan min if it changed */
1437 if (old_min != data->fan_min[nr]) {
1438 pc87360_write_value(data, LD_FAN, NO_BANK,
1439 PC87360_REG_FAN_MIN(nr),
1440 data->fan_min[nr]);
1441 }
1442}
1443
1444static struct pc87360_data *pc87360_update_device(struct device *dev)
1445{
Jean Delvaref641b582007-06-09 10:11:16 -04001446 struct pc87360_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 u8 i;
1448
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001449 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450
1451 if (time_after(jiffies, data->last_updated + HZ * 2) || !data->valid) {
Jean Delvaref641b582007-06-09 10:11:16 -04001452 dev_dbg(dev, "Data update\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453
1454 /* Fans */
1455 for (i = 0; i < data->fannr; i++) {
1456 if (FAN_CONFIG_MONITOR(data->fan_conf, i)) {
1457 data->fan_status[i] =
1458 pc87360_read_value(data, LD_FAN,
1459 NO_BANK, PC87360_REG_FAN_STATUS(i));
1460 data->fan[i] = pc87360_read_value(data, LD_FAN,
1461 NO_BANK, PC87360_REG_FAN(i));
1462 data->fan_min[i] = pc87360_read_value(data,
1463 LD_FAN, NO_BANK,
1464 PC87360_REG_FAN_MIN(i));
1465 /* Change clock divider if needed */
Jean Delvaref641b582007-06-09 10:11:16 -04001466 pc87360_autodiv(dev, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 /* Clear bits and write new divider */
1468 pc87360_write_value(data, LD_FAN, NO_BANK,
1469 PC87360_REG_FAN_STATUS(i),
1470 data->fan_status[i]);
1471 }
1472 if (FAN_CONFIG_CONTROL(data->fan_conf, i))
1473 data->pwm[i] = pc87360_read_value(data, LD_FAN,
1474 NO_BANK, PC87360_REG_PWM(i));
1475 }
1476
1477 /* Voltages */
1478 for (i = 0; i < data->innr; i++) {
1479 data->in_status[i] = pc87360_read_value(data, LD_IN, i,
1480 PC87365_REG_IN_STATUS);
1481 /* Clear bits */
1482 pc87360_write_value(data, LD_IN, i,
1483 PC87365_REG_IN_STATUS,
1484 data->in_status[i]);
Jim Cromie28f74e72008-10-18 20:27:30 -07001485 if ((data->in_status[i] & CHAN_READY) == CHAN_READY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 data->in[i] = pc87360_read_value(data, LD_IN,
1487 i, PC87365_REG_IN);
1488 }
Jim Cromie28f74e72008-10-18 20:27:30 -07001489 if (data->in_status[i] & CHAN_ENA) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 data->in_min[i] = pc87360_read_value(data,
1491 LD_IN, i,
1492 PC87365_REG_IN_MIN);
1493 data->in_max[i] = pc87360_read_value(data,
1494 LD_IN, i,
1495 PC87365_REG_IN_MAX);
1496 if (i >= 11)
1497 data->in_crit[i-11] =
1498 pc87360_read_value(data, LD_IN,
1499 i, PC87365_REG_TEMP_CRIT);
1500 }
1501 }
1502 if (data->innr) {
1503 data->in_alarms = pc87360_read_value(data, LD_IN,
1504 NO_BANK, PC87365_REG_IN_ALARMS1)
1505 | ((pc87360_read_value(data, LD_IN,
1506 NO_BANK, PC87365_REG_IN_ALARMS2)
1507 & 0x07) << 8);
1508 data->vid = (data->vid_conf & 0xE0) ?
1509 pc87360_read_value(data, LD_IN,
1510 NO_BANK, PC87365_REG_VID) : 0x1F;
1511 }
1512
1513 /* Temperatures */
1514 for (i = 0; i < data->tempnr; i++) {
1515 data->temp_status[i] = pc87360_read_value(data,
1516 LD_TEMP, i,
1517 PC87365_REG_TEMP_STATUS);
1518 /* Clear bits */
1519 pc87360_write_value(data, LD_TEMP, i,
1520 PC87365_REG_TEMP_STATUS,
1521 data->temp_status[i]);
Jim Cromie28f74e72008-10-18 20:27:30 -07001522 if ((data->temp_status[i] & CHAN_READY) == CHAN_READY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 data->temp[i] = pc87360_read_value(data,
1524 LD_TEMP, i,
1525 PC87365_REG_TEMP);
1526 }
Jim Cromie28f74e72008-10-18 20:27:30 -07001527 if (data->temp_status[i] & CHAN_ENA) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 data->temp_min[i] = pc87360_read_value(data,
1529 LD_TEMP, i,
1530 PC87365_REG_TEMP_MIN);
1531 data->temp_max[i] = pc87360_read_value(data,
1532 LD_TEMP, i,
1533 PC87365_REG_TEMP_MAX);
1534 data->temp_crit[i] = pc87360_read_value(data,
1535 LD_TEMP, i,
1536 PC87365_REG_TEMP_CRIT);
1537 }
1538 }
1539 if (data->tempnr) {
1540 data->temp_alarms = pc87360_read_value(data, LD_TEMP,
1541 NO_BANK, PC87365_REG_TEMP_ALARMS)
1542 & 0x3F;
1543 }
1544
1545 data->last_updated = jiffies;
1546 data->valid = 1;
1547 }
1548
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001549 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550
1551 return data;
1552}
1553
Jean Delvaref641b582007-06-09 10:11:16 -04001554static int __init pc87360_device_add(unsigned short address)
1555{
1556 struct resource res = {
1557 .name = "pc87360",
1558 .flags = IORESOURCE_IO,
1559 };
1560 int err, i;
1561
1562 pdev = platform_device_alloc("pc87360", address);
1563 if (!pdev) {
1564 err = -ENOMEM;
1565 printk(KERN_ERR "pc87360: Device allocation failed\n");
1566 goto exit;
1567 }
1568
1569 for (i = 0; i < 3; i++) {
1570 if (!extra_isa[i])
1571 continue;
1572 res.start = extra_isa[i];
1573 res.end = extra_isa[i] + PC87360_EXTENT - 1;
1574 err = platform_device_add_resources(pdev, &res, 1);
1575 if (err) {
1576 printk(KERN_ERR "pc87360: Device resource[%d] "
1577 "addition failed (%d)\n", i, err);
1578 goto exit_device_put;
1579 }
1580 }
1581
1582 err = platform_device_add(pdev);
1583 if (err) {
1584 printk(KERN_ERR "pc87360: Device addition failed (%d)\n",
1585 err);
1586 goto exit_device_put;
1587 }
1588
1589 return 0;
1590
1591exit_device_put:
1592 platform_device_put(pdev);
1593exit:
1594 return err;
1595}
1596
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597static int __init pc87360_init(void)
1598{
Jean Delvaref641b582007-06-09 10:11:16 -04001599 int err, i;
1600 unsigned short address = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601
1602 if (pc87360_find(0x2e, &devid, extra_isa)
1603 && pc87360_find(0x4e, &devid, extra_isa)) {
1604 printk(KERN_WARNING "pc87360: PC8736x not detected, "
1605 "module not inserted.\n");
1606 return -ENODEV;
1607 }
1608
1609 /* Arbitrarily pick one of the addresses */
1610 for (i = 0; i < 3; i++) {
1611 if (extra_isa[i] != 0x0000) {
Jean Delvare2d8672c2005-07-19 23:56:35 +02001612 address = extra_isa[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 break;
1614 }
1615 }
1616
Jean Delvare2d8672c2005-07-19 23:56:35 +02001617 if (address == 0x0000) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 printk(KERN_WARNING "pc87360: No active logical device, "
1619 "module not inserted.\n");
1620 return -ENODEV;
1621 }
1622
Jean Delvaref641b582007-06-09 10:11:16 -04001623 err = platform_driver_register(&pc87360_driver);
1624 if (err)
1625 goto exit;
1626
1627 /* Sets global pdev as a side effect */
1628 err = pc87360_device_add(address);
1629 if (err)
1630 goto exit_driver;
1631
1632 return 0;
1633
1634 exit_driver:
1635 platform_driver_unregister(&pc87360_driver);
1636 exit:
1637 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638}
1639
1640static void __exit pc87360_exit(void)
1641{
Jean Delvaref641b582007-06-09 10:11:16 -04001642 platform_device_unregister(pdev);
1643 platform_driver_unregister(&pc87360_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644}
1645
1646
1647MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
1648MODULE_DESCRIPTION("PC8736x hardware monitor");
1649MODULE_LICENSE("GPL");
1650
1651module_init(pc87360_init);
1652module_exit(pc87360_exit);