blob: de17a72149d98ffb999b156b91a4942ee9cec2aa [file] [log] [blame]
Jean Delvaree53004e2006-01-09 23:26:14 +01001/*
2 * f71805f.c - driver for the Fintek F71805F/FG Super-I/O chip integrated
3 * hardware monitoring features
Jean Delvare2d457712006-09-24 20:52:15 +02004 * Copyright (C) 2005-2006 Jean Delvare <khali@linux-fr.org>
Jean Delvaree53004e2006-01-09 23:26:14 +01005 *
6 * The F71805F/FG is a LPC Super-I/O chip made by Fintek. It integrates
7 * complete hardware monitoring features: voltage, fan and temperature
8 * sensors, and manual and automatic fan speed control.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 */
24
25#include <linux/module.h>
26#include <linux/init.h>
27#include <linux/slab.h>
28#include <linux/jiffies.h>
29#include <linux/platform_device.h>
30#include <linux/hwmon.h>
31#include <linux/hwmon-sysfs.h>
32#include <linux/err.h>
Jean Delvaref0819182006-01-18 23:20:53 +010033#include <linux/mutex.h>
Jean Delvare0e39e012006-09-24 21:16:40 +020034#include <linux/sysfs.h>
Jean Delvaree53004e2006-01-09 23:26:14 +010035#include <asm/io.h>
36
37static struct platform_device *pdev;
38
39#define DRVNAME "f71805f"
40
41/*
42 * Super-I/O constants and functions
43 */
44
45#define F71805F_LD_HWM 0x04
46
47#define SIO_REG_LDSEL 0x07 /* Logical device select */
48#define SIO_REG_DEVID 0x20 /* Device ID (2 bytes) */
49#define SIO_REG_DEVREV 0x22 /* Device revision */
50#define SIO_REG_MANID 0x23 /* Fintek ID (2 bytes) */
51#define SIO_REG_ENABLE 0x30 /* Logical device enable */
52#define SIO_REG_ADDR 0x60 /* Logical device address (2 bytes) */
53
54#define SIO_FINTEK_ID 0x1934
55#define SIO_F71805F_ID 0x0406
56
57static inline int
58superio_inb(int base, int reg)
59{
60 outb(reg, base);
61 return inb(base + 1);
62}
63
64static int
65superio_inw(int base, int reg)
66{
67 int val;
68 outb(reg++, base);
69 val = inb(base + 1) << 8;
70 outb(reg, base);
71 val |= inb(base + 1);
72 return val;
73}
74
75static inline void
76superio_select(int base, int ld)
77{
78 outb(SIO_REG_LDSEL, base);
79 outb(ld, base + 1);
80}
81
82static inline void
83superio_enter(int base)
84{
85 outb(0x87, base);
86 outb(0x87, base);
87}
88
89static inline void
90superio_exit(int base)
91{
92 outb(0xaa, base);
93}
94
95/*
96 * ISA constants
97 */
98
99#define REGION_LENGTH 2
100#define ADDR_REG_OFFSET 0
101#define DATA_REG_OFFSET 1
102
Jean Delvaree53004e2006-01-09 23:26:14 +0100103/*
104 * Registers
105 */
106
107/* in nr from 0 to 8 (8-bit values) */
108#define F71805F_REG_IN(nr) (0x10 + (nr))
109#define F71805F_REG_IN_HIGH(nr) (0x40 + 2 * (nr))
110#define F71805F_REG_IN_LOW(nr) (0x41 + 2 * (nr))
111/* fan nr from 0 to 2 (12-bit values, two registers) */
112#define F71805F_REG_FAN(nr) (0x20 + 2 * (nr))
113#define F71805F_REG_FAN_LOW(nr) (0x28 + 2 * (nr))
114#define F71805F_REG_FAN_CTRL(nr) (0x60 + 16 * (nr))
115/* temp nr from 0 to 2 (8-bit values) */
116#define F71805F_REG_TEMP(nr) (0x1B + (nr))
117#define F71805F_REG_TEMP_HIGH(nr) (0x54 + 2 * (nr))
118#define F71805F_REG_TEMP_HYST(nr) (0x55 + 2 * (nr))
119#define F71805F_REG_TEMP_MODE 0x01
120
121#define F71805F_REG_START 0x00
122/* status nr from 0 to 2 */
123#define F71805F_REG_STATUS(nr) (0x36 + (nr))
124
125/*
126 * Data structures and manipulation thereof
127 */
128
129struct f71805f_data {
130 unsigned short addr;
131 const char *name;
Jean Delvaref0819182006-01-18 23:20:53 +0100132 struct mutex lock;
Jean Delvaree53004e2006-01-09 23:26:14 +0100133 struct class_device *class_dev;
134
Jean Delvaref0819182006-01-18 23:20:53 +0100135 struct mutex update_lock;
Jean Delvaree53004e2006-01-09 23:26:14 +0100136 char valid; /* !=0 if following fields are valid */
137 unsigned long last_updated; /* In jiffies */
138 unsigned long last_limits; /* In jiffies */
139
140 /* Register values */
141 u8 in[9];
142 u8 in_high[9];
143 u8 in_low[9];
144 u16 fan[3];
145 u16 fan_low[3];
146 u8 fan_enabled; /* Read once at init time */
147 u8 temp[3];
148 u8 temp_high[3];
149 u8 temp_hyst[3];
150 u8 temp_mode;
Jean Delvare2d457712006-09-24 20:52:15 +0200151 unsigned long alarms;
Jean Delvaree53004e2006-01-09 23:26:14 +0100152};
153
154static inline long in_from_reg(u8 reg)
155{
156 return (reg * 8);
157}
158
159/* The 2 least significant bits are not used */
160static inline u8 in_to_reg(long val)
161{
162 if (val <= 0)
163 return 0;
164 if (val >= 2016)
165 return 0xfc;
166 return (((val + 16) / 32) << 2);
167}
168
169/* in0 is downscaled by a factor 2 internally */
170static inline long in0_from_reg(u8 reg)
171{
172 return (reg * 16);
173}
174
175static inline u8 in0_to_reg(long val)
176{
177 if (val <= 0)
178 return 0;
179 if (val >= 4032)
180 return 0xfc;
181 return (((val + 32) / 64) << 2);
182}
183
184/* The 4 most significant bits are not used */
185static inline long fan_from_reg(u16 reg)
186{
187 reg &= 0xfff;
188 if (!reg || reg == 0xfff)
189 return 0;
190 return (1500000 / reg);
191}
192
193static inline u16 fan_to_reg(long rpm)
194{
195 /* If the low limit is set below what the chip can measure,
196 store the largest possible 12-bit value in the registers,
197 so that no alarm will ever trigger. */
198 if (rpm < 367)
199 return 0xfff;
200 return (1500000 / rpm);
201}
202
203static inline long temp_from_reg(u8 reg)
204{
205 return (reg * 1000);
206}
207
208static inline u8 temp_to_reg(long val)
209{
210 if (val < 0)
211 val = 0;
212 else if (val > 1000 * 0xff)
213 val = 0xff;
214 return ((val + 500) / 1000);
215}
216
217/*
218 * Device I/O access
219 */
220
221static u8 f71805f_read8(struct f71805f_data *data, u8 reg)
222{
223 u8 val;
224
Jean Delvaref0819182006-01-18 23:20:53 +0100225 mutex_lock(&data->lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100226 outb(reg, data->addr + ADDR_REG_OFFSET);
227 val = inb(data->addr + DATA_REG_OFFSET);
Jean Delvaref0819182006-01-18 23:20:53 +0100228 mutex_unlock(&data->lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100229
230 return val;
231}
232
233static void f71805f_write8(struct f71805f_data *data, u8 reg, u8 val)
234{
Jean Delvaref0819182006-01-18 23:20:53 +0100235 mutex_lock(&data->lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100236 outb(reg, data->addr + ADDR_REG_OFFSET);
237 outb(val, data->addr + DATA_REG_OFFSET);
Jean Delvaref0819182006-01-18 23:20:53 +0100238 mutex_unlock(&data->lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100239}
240
241/* It is important to read the MSB first, because doing so latches the
242 value of the LSB, so we are sure both bytes belong to the same value. */
243static u16 f71805f_read16(struct f71805f_data *data, u8 reg)
244{
245 u16 val;
246
Jean Delvaref0819182006-01-18 23:20:53 +0100247 mutex_lock(&data->lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100248 outb(reg, data->addr + ADDR_REG_OFFSET);
249 val = inb(data->addr + DATA_REG_OFFSET) << 8;
250 outb(++reg, data->addr + ADDR_REG_OFFSET);
251 val |= inb(data->addr + DATA_REG_OFFSET);
Jean Delvaref0819182006-01-18 23:20:53 +0100252 mutex_unlock(&data->lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100253
254 return val;
255}
256
257static void f71805f_write16(struct f71805f_data *data, u8 reg, u16 val)
258{
Jean Delvaref0819182006-01-18 23:20:53 +0100259 mutex_lock(&data->lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100260 outb(reg, data->addr + ADDR_REG_OFFSET);
261 outb(val >> 8, data->addr + DATA_REG_OFFSET);
262 outb(++reg, data->addr + ADDR_REG_OFFSET);
263 outb(val & 0xff, data->addr + DATA_REG_OFFSET);
Jean Delvaref0819182006-01-18 23:20:53 +0100264 mutex_unlock(&data->lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100265}
266
267static struct f71805f_data *f71805f_update_device(struct device *dev)
268{
269 struct f71805f_data *data = dev_get_drvdata(dev);
270 int nr;
271
Jean Delvaref0819182006-01-18 23:20:53 +0100272 mutex_lock(&data->update_lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100273
274 /* Limit registers cache is refreshed after 60 seconds */
275 if (time_after(jiffies, data->last_updated + 60 * HZ)
276 || !data->valid) {
277 for (nr = 0; nr < 9; nr++) {
278 data->in_high[nr] = f71805f_read8(data,
279 F71805F_REG_IN_HIGH(nr));
280 data->in_low[nr] = f71805f_read8(data,
281 F71805F_REG_IN_LOW(nr));
282 }
283 for (nr = 0; nr < 3; nr++) {
284 if (data->fan_enabled & (1 << nr))
285 data->fan_low[nr] = f71805f_read16(data,
286 F71805F_REG_FAN_LOW(nr));
287 }
288 for (nr = 0; nr < 3; nr++) {
289 data->temp_high[nr] = f71805f_read8(data,
290 F71805F_REG_TEMP_HIGH(nr));
291 data->temp_hyst[nr] = f71805f_read8(data,
292 F71805F_REG_TEMP_HYST(nr));
293 }
294 data->temp_mode = f71805f_read8(data, F71805F_REG_TEMP_MODE);
295
296 data->last_limits = jiffies;
297 }
298
299 /* Measurement registers cache is refreshed after 1 second */
300 if (time_after(jiffies, data->last_updated + HZ)
301 || !data->valid) {
302 for (nr = 0; nr < 9; nr++) {
303 data->in[nr] = f71805f_read8(data,
304 F71805F_REG_IN(nr));
305 }
306 for (nr = 0; nr < 3; nr++) {
307 if (data->fan_enabled & (1 << nr))
308 data->fan[nr] = f71805f_read16(data,
309 F71805F_REG_FAN(nr));
310 }
311 for (nr = 0; nr < 3; nr++) {
312 data->temp[nr] = f71805f_read8(data,
313 F71805F_REG_TEMP(nr));
314 }
Jean Delvare2d457712006-09-24 20:52:15 +0200315 data->alarms = f71805f_read8(data, F71805F_REG_STATUS(0))
316 + (f71805f_read8(data, F71805F_REG_STATUS(1)) << 8)
317 + (f71805f_read8(data, F71805F_REG_STATUS(2)) << 16);
Jean Delvaree53004e2006-01-09 23:26:14 +0100318
319 data->last_updated = jiffies;
320 data->valid = 1;
321 }
322
Jean Delvaref0819182006-01-18 23:20:53 +0100323 mutex_unlock(&data->update_lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100324
325 return data;
326}
327
328/*
329 * Sysfs interface
330 */
331
332static ssize_t show_in0(struct device *dev, struct device_attribute *devattr,
333 char *buf)
334{
335 struct f71805f_data *data = f71805f_update_device(dev);
336
337 return sprintf(buf, "%ld\n", in0_from_reg(data->in[0]));
338}
339
340static ssize_t show_in0_max(struct device *dev, struct device_attribute
341 *devattr, char *buf)
342{
343 struct f71805f_data *data = f71805f_update_device(dev);
344
345 return sprintf(buf, "%ld\n", in0_from_reg(data->in_high[0]));
346}
347
348static ssize_t show_in0_min(struct device *dev, struct device_attribute
349 *devattr, char *buf)
350{
351 struct f71805f_data *data = f71805f_update_device(dev);
352
353 return sprintf(buf, "%ld\n", in0_from_reg(data->in_low[0]));
354}
355
356static ssize_t set_in0_max(struct device *dev, struct device_attribute
357 *devattr, const char *buf, size_t count)
358{
359 struct f71805f_data *data = dev_get_drvdata(dev);
360 long val = simple_strtol(buf, NULL, 10);
361
Jean Delvaref0819182006-01-18 23:20:53 +0100362 mutex_lock(&data->update_lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100363 data->in_high[0] = in0_to_reg(val);
364 f71805f_write8(data, F71805F_REG_IN_HIGH(0), data->in_high[0]);
Jean Delvaref0819182006-01-18 23:20:53 +0100365 mutex_unlock(&data->update_lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100366
367 return count;
368}
369
370static ssize_t set_in0_min(struct device *dev, struct device_attribute
371 *devattr, const char *buf, size_t count)
372{
373 struct f71805f_data *data = dev_get_drvdata(dev);
374 long val = simple_strtol(buf, NULL, 10);
375
Jean Delvaref0819182006-01-18 23:20:53 +0100376 mutex_lock(&data->update_lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100377 data->in_low[0] = in0_to_reg(val);
378 f71805f_write8(data, F71805F_REG_IN_LOW(0), data->in_low[0]);
Jean Delvaref0819182006-01-18 23:20:53 +0100379 mutex_unlock(&data->update_lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100380
381 return count;
382}
383
Jean Delvaree53004e2006-01-09 23:26:14 +0100384static ssize_t show_in(struct device *dev, struct device_attribute *devattr,
385 char *buf)
386{
387 struct f71805f_data *data = f71805f_update_device(dev);
388 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
389 int nr = attr->index;
390
391 return sprintf(buf, "%ld\n", in_from_reg(data->in[nr]));
392}
393
394static ssize_t show_in_max(struct device *dev, struct device_attribute
395 *devattr, char *buf)
396{
397 struct f71805f_data *data = f71805f_update_device(dev);
398 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
399 int nr = attr->index;
400
401 return sprintf(buf, "%ld\n", in_from_reg(data->in_high[nr]));
402}
403
404static ssize_t show_in_min(struct device *dev, struct device_attribute
405 *devattr, char *buf)
406{
407 struct f71805f_data *data = f71805f_update_device(dev);
408 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
409 int nr = attr->index;
410
411 return sprintf(buf, "%ld\n", in_from_reg(data->in_low[nr]));
412}
413
414static ssize_t set_in_max(struct device *dev, struct device_attribute
415 *devattr, const char *buf, size_t count)
416{
417 struct f71805f_data *data = dev_get_drvdata(dev);
418 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
419 int nr = attr->index;
420 long val = simple_strtol(buf, NULL, 10);
421
Jean Delvaref0819182006-01-18 23:20:53 +0100422 mutex_lock(&data->update_lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100423 data->in_high[nr] = in_to_reg(val);
424 f71805f_write8(data, F71805F_REG_IN_HIGH(nr), data->in_high[nr]);
Jean Delvaref0819182006-01-18 23:20:53 +0100425 mutex_unlock(&data->update_lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100426
427 return count;
428}
429
430static ssize_t set_in_min(struct device *dev, struct device_attribute
431 *devattr, const char *buf, size_t count)
432{
433 struct f71805f_data *data = dev_get_drvdata(dev);
434 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
435 int nr = attr->index;
436 long val = simple_strtol(buf, NULL, 10);
437
Jean Delvaref0819182006-01-18 23:20:53 +0100438 mutex_lock(&data->update_lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100439 data->in_low[nr] = in_to_reg(val);
440 f71805f_write8(data, F71805F_REG_IN_LOW(nr), data->in_low[nr]);
Jean Delvaref0819182006-01-18 23:20:53 +0100441 mutex_unlock(&data->update_lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100442
443 return count;
444}
445
Jean Delvaree53004e2006-01-09 23:26:14 +0100446static ssize_t show_fan(struct device *dev, struct device_attribute *devattr,
447 char *buf)
448{
449 struct f71805f_data *data = f71805f_update_device(dev);
450 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
451 int nr = attr->index;
452
453 return sprintf(buf, "%ld\n", fan_from_reg(data->fan[nr]));
454}
455
456static ssize_t show_fan_min(struct device *dev, struct device_attribute
457 *devattr, char *buf)
458{
459 struct f71805f_data *data = f71805f_update_device(dev);
460 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
461 int nr = attr->index;
462
463 return sprintf(buf, "%ld\n", fan_from_reg(data->fan_low[nr]));
464}
465
466static ssize_t set_fan_min(struct device *dev, struct device_attribute
467 *devattr, const char *buf, size_t count)
468{
469 struct f71805f_data *data = dev_get_drvdata(dev);
470 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
471 int nr = attr->index;
472 long val = simple_strtol(buf, NULL, 10);
473
Jean Delvaref0819182006-01-18 23:20:53 +0100474 mutex_lock(&data->update_lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100475 data->fan_low[nr] = fan_to_reg(val);
476 f71805f_write16(data, F71805F_REG_FAN_LOW(nr), data->fan_low[nr]);
Jean Delvaref0819182006-01-18 23:20:53 +0100477 mutex_unlock(&data->update_lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100478
479 return count;
480}
481
Jean Delvaree53004e2006-01-09 23:26:14 +0100482static ssize_t show_temp(struct device *dev, struct device_attribute *devattr,
483 char *buf)
484{
485 struct f71805f_data *data = f71805f_update_device(dev);
486 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
487 int nr = attr->index;
488
489 return sprintf(buf, "%ld\n", temp_from_reg(data->temp[nr]));
490}
491
492static ssize_t show_temp_max(struct device *dev, struct device_attribute
493 *devattr, char *buf)
494{
495 struct f71805f_data *data = f71805f_update_device(dev);
496 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
497 int nr = attr->index;
498
499 return sprintf(buf, "%ld\n", temp_from_reg(data->temp_high[nr]));
500}
501
502static ssize_t show_temp_hyst(struct device *dev, struct device_attribute
503 *devattr, char *buf)
504{
505 struct f71805f_data *data = f71805f_update_device(dev);
506 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
507 int nr = attr->index;
508
509 return sprintf(buf, "%ld\n", temp_from_reg(data->temp_hyst[nr]));
510}
511
512static ssize_t show_temp_type(struct device *dev, struct device_attribute
513 *devattr, char *buf)
514{
515 struct f71805f_data *data = f71805f_update_device(dev);
516 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
517 int nr = attr->index;
518
519 /* 3 is diode, 4 is thermistor */
520 return sprintf(buf, "%u\n", (data->temp_mode & (1 << nr)) ? 3 : 4);
521}
522
523static ssize_t set_temp_max(struct device *dev, struct device_attribute
524 *devattr, const char *buf, size_t count)
525{
526 struct f71805f_data *data = dev_get_drvdata(dev);
527 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
528 int nr = attr->index;
529 long val = simple_strtol(buf, NULL, 10);
530
Jean Delvaref0819182006-01-18 23:20:53 +0100531 mutex_lock(&data->update_lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100532 data->temp_high[nr] = temp_to_reg(val);
533 f71805f_write8(data, F71805F_REG_TEMP_HIGH(nr), data->temp_high[nr]);
Jean Delvaref0819182006-01-18 23:20:53 +0100534 mutex_unlock(&data->update_lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100535
536 return count;
537}
538
539static ssize_t set_temp_hyst(struct device *dev, struct device_attribute
540 *devattr, const char *buf, size_t count)
541{
542 struct f71805f_data *data = dev_get_drvdata(dev);
543 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
544 int nr = attr->index;
545 long val = simple_strtol(buf, NULL, 10);
546
Jean Delvaref0819182006-01-18 23:20:53 +0100547 mutex_lock(&data->update_lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100548 data->temp_hyst[nr] = temp_to_reg(val);
549 f71805f_write8(data, F71805F_REG_TEMP_HYST(nr), data->temp_hyst[nr]);
Jean Delvaref0819182006-01-18 23:20:53 +0100550 mutex_unlock(&data->update_lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100551
552 return count;
553}
554
Jean Delvaree53004e2006-01-09 23:26:14 +0100555static ssize_t show_alarms_in(struct device *dev, struct device_attribute
556 *devattr, char *buf)
557{
558 struct f71805f_data *data = f71805f_update_device(dev);
559
Jean Delvare2d457712006-09-24 20:52:15 +0200560 return sprintf(buf, "%lu\n", data->alarms & 0x1ff);
Jean Delvaree53004e2006-01-09 23:26:14 +0100561}
562
563static ssize_t show_alarms_fan(struct device *dev, struct device_attribute
564 *devattr, char *buf)
565{
566 struct f71805f_data *data = f71805f_update_device(dev);
567
Jean Delvare2d457712006-09-24 20:52:15 +0200568 return sprintf(buf, "%lu\n", (data->alarms >> 16) & 0x07);
Jean Delvaree53004e2006-01-09 23:26:14 +0100569}
570
571static ssize_t show_alarms_temp(struct device *dev, struct device_attribute
572 *devattr, char *buf)
573{
574 struct f71805f_data *data = f71805f_update_device(dev);
575
Jean Delvare2d457712006-09-24 20:52:15 +0200576 return sprintf(buf, "%lu\n", (data->alarms >> 11) & 0x07);
577}
578
579static ssize_t show_alarm(struct device *dev, struct device_attribute
580 *devattr, char *buf)
581{
582 struct f71805f_data *data = f71805f_update_device(dev);
583 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
584 int bitnr = attr->index;
585
586 return sprintf(buf, "%lu\n", (data->alarms >> bitnr) & 1);
Jean Delvaree53004e2006-01-09 23:26:14 +0100587}
588
Jean Delvaree53004e2006-01-09 23:26:14 +0100589static ssize_t show_name(struct device *dev, struct device_attribute
590 *devattr, char *buf)
591{
592 struct f71805f_data *data = dev_get_drvdata(dev);
593
594 return sprintf(buf, "%s\n", data->name);
595}
596
Jean Delvare0e39e012006-09-24 21:16:40 +0200597static DEVICE_ATTR(in0_input, S_IRUGO, show_in0, NULL);
598static DEVICE_ATTR(in0_max, S_IRUGO| S_IWUSR, show_in0_max, set_in0_max);
599static DEVICE_ATTR(in0_min, S_IRUGO| S_IWUSR, show_in0_min, set_in0_min);
600static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, show_in, NULL, 1);
601static SENSOR_DEVICE_ATTR(in1_max, S_IRUGO | S_IWUSR,
602 show_in_max, set_in_max, 1);
603static SENSOR_DEVICE_ATTR(in1_min, S_IRUGO | S_IWUSR,
604 show_in_min, set_in_min, 1);
605static SENSOR_DEVICE_ATTR(in2_input, S_IRUGO, show_in, NULL, 2);
606static SENSOR_DEVICE_ATTR(in2_max, S_IRUGO | S_IWUSR,
607 show_in_max, set_in_max, 2);
608static SENSOR_DEVICE_ATTR(in2_min, S_IRUGO | S_IWUSR,
609 show_in_min, set_in_min, 2);
610static SENSOR_DEVICE_ATTR(in3_input, S_IRUGO, show_in, NULL, 3);
611static SENSOR_DEVICE_ATTR(in3_max, S_IRUGO | S_IWUSR,
612 show_in_max, set_in_max, 3);
613static SENSOR_DEVICE_ATTR(in3_min, S_IRUGO | S_IWUSR,
614 show_in_min, set_in_min, 3);
615static SENSOR_DEVICE_ATTR(in4_input, S_IRUGO, show_in, NULL, 4);
616static SENSOR_DEVICE_ATTR(in4_max, S_IRUGO | S_IWUSR,
617 show_in_max, set_in_max, 4);
618static SENSOR_DEVICE_ATTR(in4_min, S_IRUGO | S_IWUSR,
619 show_in_min, set_in_min, 4);
620static SENSOR_DEVICE_ATTR(in5_input, S_IRUGO, show_in, NULL, 5);
621static SENSOR_DEVICE_ATTR(in5_max, S_IRUGO | S_IWUSR,
622 show_in_max, set_in_max, 5);
623static SENSOR_DEVICE_ATTR(in5_min, S_IRUGO | S_IWUSR,
624 show_in_min, set_in_min, 5);
625static SENSOR_DEVICE_ATTR(in6_input, S_IRUGO, show_in, NULL, 6);
626static SENSOR_DEVICE_ATTR(in6_max, S_IRUGO | S_IWUSR,
627 show_in_max, set_in_max, 6);
628static SENSOR_DEVICE_ATTR(in6_min, S_IRUGO | S_IWUSR,
629 show_in_min, set_in_min, 6);
630static SENSOR_DEVICE_ATTR(in7_input, S_IRUGO, show_in, NULL, 7);
631static SENSOR_DEVICE_ATTR(in7_max, S_IRUGO | S_IWUSR,
632 show_in_max, set_in_max, 7);
633static SENSOR_DEVICE_ATTR(in7_min, S_IRUGO | S_IWUSR,
634 show_in_min, set_in_min, 7);
635static SENSOR_DEVICE_ATTR(in8_input, S_IRUGO, show_in, NULL, 8);
636static SENSOR_DEVICE_ATTR(in8_max, S_IRUGO | S_IWUSR,
637 show_in_max, set_in_max, 8);
638static SENSOR_DEVICE_ATTR(in8_min, S_IRUGO | S_IWUSR,
639 show_in_min, set_in_min, 8);
640
641static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0);
642static SENSOR_DEVICE_ATTR(fan1_min, S_IRUGO | S_IWUSR,
643 show_fan_min, set_fan_min, 0);
644static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, show_fan, NULL, 1);
645static SENSOR_DEVICE_ATTR(fan2_min, S_IRUGO | S_IWUSR,
646 show_fan_min, set_fan_min, 1);
647static SENSOR_DEVICE_ATTR(fan3_input, S_IRUGO, show_fan, NULL, 2);
648static SENSOR_DEVICE_ATTR(fan3_min, S_IRUGO | S_IWUSR,
649 show_fan_min, set_fan_min, 2);
650
651static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
652static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO | S_IWUSR,
653 show_temp_max, set_temp_max, 0);
654static SENSOR_DEVICE_ATTR(temp1_max_hyst, S_IRUGO | S_IWUSR,
655 show_temp_hyst, set_temp_hyst, 0);
656static SENSOR_DEVICE_ATTR(temp1_type, S_IRUGO, show_temp_type, NULL, 0);
657static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1);
658static SENSOR_DEVICE_ATTR(temp2_max, S_IRUGO | S_IWUSR,
659 show_temp_max, set_temp_max, 1);
660static SENSOR_DEVICE_ATTR(temp2_max_hyst, S_IRUGO | S_IWUSR,
661 show_temp_hyst, set_temp_hyst, 1);
662static SENSOR_DEVICE_ATTR(temp2_type, S_IRUGO, show_temp_type, NULL, 1);
663static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, show_temp, NULL, 2);
664static SENSOR_DEVICE_ATTR(temp3_max, S_IRUGO | S_IWUSR,
665 show_temp_max, set_temp_max, 2);
666static SENSOR_DEVICE_ATTR(temp3_max_hyst, S_IRUGO | S_IWUSR,
667 show_temp_hyst, set_temp_hyst, 2);
668static SENSOR_DEVICE_ATTR(temp3_type, S_IRUGO, show_temp_type, NULL, 2);
669
670static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0);
671static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1);
672static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2);
673static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3);
674static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 4);
675static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 5);
676static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 6);
677static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 7);
678static SENSOR_DEVICE_ATTR(in8_alarm, S_IRUGO, show_alarm, NULL, 8);
679static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 11);
680static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 12);
681static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 13);
682static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 16);
683static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 17);
684static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 18);
685static DEVICE_ATTR(alarms_in, S_IRUGO, show_alarms_in, NULL);
686static DEVICE_ATTR(alarms_fan, S_IRUGO, show_alarms_fan, NULL);
687static DEVICE_ATTR(alarms_temp, S_IRUGO, show_alarms_temp, NULL);
688
689static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
690
691static struct attribute *f71805f_attributes[] = {
692 &dev_attr_in0_input.attr,
693 &dev_attr_in0_max.attr,
694 &dev_attr_in0_min.attr,
695 &sensor_dev_attr_in1_input.dev_attr.attr,
696 &sensor_dev_attr_in1_max.dev_attr.attr,
697 &sensor_dev_attr_in1_min.dev_attr.attr,
698 &sensor_dev_attr_in2_input.dev_attr.attr,
699 &sensor_dev_attr_in2_max.dev_attr.attr,
700 &sensor_dev_attr_in2_min.dev_attr.attr,
701 &sensor_dev_attr_in3_input.dev_attr.attr,
702 &sensor_dev_attr_in3_max.dev_attr.attr,
703 &sensor_dev_attr_in3_min.dev_attr.attr,
704 &sensor_dev_attr_in4_input.dev_attr.attr,
705 &sensor_dev_attr_in4_max.dev_attr.attr,
706 &sensor_dev_attr_in4_min.dev_attr.attr,
707 &sensor_dev_attr_in5_input.dev_attr.attr,
708 &sensor_dev_attr_in5_max.dev_attr.attr,
709 &sensor_dev_attr_in5_min.dev_attr.attr,
710 &sensor_dev_attr_in6_input.dev_attr.attr,
711 &sensor_dev_attr_in6_max.dev_attr.attr,
712 &sensor_dev_attr_in6_min.dev_attr.attr,
713 &sensor_dev_attr_in7_input.dev_attr.attr,
714 &sensor_dev_attr_in7_max.dev_attr.attr,
715 &sensor_dev_attr_in7_min.dev_attr.attr,
716 &sensor_dev_attr_in8_input.dev_attr.attr,
717 &sensor_dev_attr_in8_max.dev_attr.attr,
718 &sensor_dev_attr_in8_min.dev_attr.attr,
719
720 &sensor_dev_attr_temp1_input.dev_attr.attr,
721 &sensor_dev_attr_temp1_max.dev_attr.attr,
722 &sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
723 &sensor_dev_attr_temp1_type.dev_attr.attr,
724 &sensor_dev_attr_temp2_input.dev_attr.attr,
725 &sensor_dev_attr_temp2_max.dev_attr.attr,
726 &sensor_dev_attr_temp2_max_hyst.dev_attr.attr,
727 &sensor_dev_attr_temp2_type.dev_attr.attr,
728 &sensor_dev_attr_temp3_input.dev_attr.attr,
729 &sensor_dev_attr_temp3_max.dev_attr.attr,
730 &sensor_dev_attr_temp3_max_hyst.dev_attr.attr,
731 &sensor_dev_attr_temp3_type.dev_attr.attr,
732
733 &sensor_dev_attr_in0_alarm.dev_attr.attr,
734 &sensor_dev_attr_in1_alarm.dev_attr.attr,
735 &sensor_dev_attr_in2_alarm.dev_attr.attr,
736 &sensor_dev_attr_in3_alarm.dev_attr.attr,
737 &sensor_dev_attr_in4_alarm.dev_attr.attr,
738 &sensor_dev_attr_in5_alarm.dev_attr.attr,
739 &sensor_dev_attr_in6_alarm.dev_attr.attr,
740 &sensor_dev_attr_in7_alarm.dev_attr.attr,
741 &sensor_dev_attr_in8_alarm.dev_attr.attr,
742 &dev_attr_alarms_in.attr,
743 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
744 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
745 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
746 &dev_attr_alarms_temp.attr,
747 &dev_attr_alarms_fan.attr,
748
749 &dev_attr_name.attr,
750 NULL
Jean Delvare2488a392006-01-09 23:29:11 +0100751};
752
Jean Delvare0e39e012006-09-24 21:16:40 +0200753static const struct attribute_group f71805f_group = {
754 .attrs = f71805f_attributes,
Jean Delvare2488a392006-01-09 23:29:11 +0100755};
756
Jean Delvare0e39e012006-09-24 21:16:40 +0200757static struct attribute *f71805f_attributes_fan[3][4] = {
758 {
759 &sensor_dev_attr_fan1_input.dev_attr.attr,
760 &sensor_dev_attr_fan1_min.dev_attr.attr,
761 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
762 NULL
763 }, {
764 &sensor_dev_attr_fan2_input.dev_attr.attr,
765 &sensor_dev_attr_fan2_min.dev_attr.attr,
766 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
767 NULL
768 }, {
769 &sensor_dev_attr_fan3_input.dev_attr.attr,
770 &sensor_dev_attr_fan3_min.dev_attr.attr,
771 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
772 NULL
773 }
774};
775
776static const struct attribute_group f71805f_group_fan[3] = {
777 { .attrs = f71805f_attributes_fan[0] },
778 { .attrs = f71805f_attributes_fan[1] },
779 { .attrs = f71805f_attributes_fan[2] },
Jean Delvare2488a392006-01-09 23:29:11 +0100780};
Jean Delvaree53004e2006-01-09 23:26:14 +0100781
782/*
783 * Device registration and initialization
784 */
785
786static void __devinit f71805f_init_device(struct f71805f_data *data)
787{
788 u8 reg;
789 int i;
790
791 reg = f71805f_read8(data, F71805F_REG_START);
792 if ((reg & 0x41) != 0x01) {
793 printk(KERN_DEBUG DRVNAME ": Starting monitoring "
794 "operations\n");
795 f71805f_write8(data, F71805F_REG_START, (reg | 0x01) & ~0x40);
796 }
797
798 /* Fan monitoring can be disabled. If it is, we won't be polling
799 the register values, and won't create the related sysfs files. */
800 for (i = 0; i < 3; i++) {
801 reg = f71805f_read8(data, F71805F_REG_FAN_CTRL(i));
802 if (!(reg & 0x80))
803 data->fan_enabled |= (1 << i);
804 }
805}
806
807static int __devinit f71805f_probe(struct platform_device *pdev)
808{
809 struct f71805f_data *data;
810 struct resource *res;
Jean Delvare2488a392006-01-09 23:29:11 +0100811 int i, err;
Jean Delvaree53004e2006-01-09 23:26:14 +0100812
813 if (!(data = kzalloc(sizeof(struct f71805f_data), GFP_KERNEL))) {
814 err = -ENOMEM;
815 printk(KERN_ERR DRVNAME ": Out of memory\n");
816 goto exit;
817 }
818
819 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
820 data->addr = res->start;
Jean Delvaref0819182006-01-18 23:20:53 +0100821 mutex_init(&data->lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100822 data->name = "f71805f";
Jean Delvaref0819182006-01-18 23:20:53 +0100823 mutex_init(&data->update_lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100824
825 platform_set_drvdata(pdev, data);
826
Jean Delvaree53004e2006-01-09 23:26:14 +0100827 /* Initialize the F71805F chip */
828 f71805f_init_device(data);
829
830 /* Register sysfs interface files */
Jean Delvare0e39e012006-09-24 21:16:40 +0200831 if ((err = sysfs_create_group(&pdev->dev.kobj, &f71805f_group)))
832 goto exit_free;
833 for (i = 0; i < 3; i++) {
834 if (!(data->fan_enabled & (1 << i)))
Jean Delvare2488a392006-01-09 23:29:11 +0100835 continue;
Jean Delvare0e39e012006-09-24 21:16:40 +0200836 if ((err = sysfs_create_group(&pdev->dev.kobj,
837 &f71805f_group_fan[i])))
838 goto exit_remove_files;
839 }
840
841 data->class_dev = hwmon_device_register(&pdev->dev);
842 if (IS_ERR(data->class_dev)) {
843 err = PTR_ERR(data->class_dev);
844 dev_err(&pdev->dev, "Class registration failed (%d)\n", err);
845 goto exit_remove_files;
Jean Delvaree53004e2006-01-09 23:26:14 +0100846 }
Jean Delvaree53004e2006-01-09 23:26:14 +0100847
848 return 0;
849
Jean Delvare0e39e012006-09-24 21:16:40 +0200850exit_remove_files:
851 sysfs_remove_group(&pdev->dev.kobj, &f71805f_group);
852 for (i = 0; i < 3; i++)
853 sysfs_remove_group(&pdev->dev.kobj, &f71805f_group_fan[i]);
Jean Delvaree53004e2006-01-09 23:26:14 +0100854exit_free:
Jean Delvare0e39e012006-09-24 21:16:40 +0200855 platform_set_drvdata(pdev, NULL);
Jean Delvaree53004e2006-01-09 23:26:14 +0100856 kfree(data);
857exit:
858 return err;
859}
860
861static int __devexit f71805f_remove(struct platform_device *pdev)
862{
863 struct f71805f_data *data = platform_get_drvdata(pdev);
Jean Delvare0e39e012006-09-24 21:16:40 +0200864 int i;
Jean Delvaree53004e2006-01-09 23:26:14 +0100865
866 platform_set_drvdata(pdev, NULL);
867 hwmon_device_unregister(data->class_dev);
Jean Delvare0e39e012006-09-24 21:16:40 +0200868 sysfs_remove_group(&pdev->dev.kobj, &f71805f_group);
869 for (i = 0; i < 3; i++)
870 sysfs_remove_group(&pdev->dev.kobj, &f71805f_group_fan[i]);
Jean Delvaree53004e2006-01-09 23:26:14 +0100871 kfree(data);
872
873 return 0;
874}
875
876static struct platform_driver f71805f_driver = {
877 .driver = {
878 .owner = THIS_MODULE,
879 .name = DRVNAME,
880 },
881 .probe = f71805f_probe,
882 .remove = __devexit_p(f71805f_remove),
883};
884
885static int __init f71805f_device_add(unsigned short address)
886{
Jean Delvare568825c2006-03-23 16:40:23 +0100887 struct resource res = {
888 .start = address,
889 .end = address + REGION_LENGTH - 1,
890 .flags = IORESOURCE_IO,
891 };
Jean Delvaree53004e2006-01-09 23:26:14 +0100892 int err;
893
894 pdev = platform_device_alloc(DRVNAME, address);
895 if (!pdev) {
896 err = -ENOMEM;
897 printk(KERN_ERR DRVNAME ": Device allocation failed\n");
898 goto exit;
899 }
900
Jean Delvare568825c2006-03-23 16:40:23 +0100901 res.name = pdev->name;
902 err = platform_device_add_resources(pdev, &res, 1);
Jean Delvaree53004e2006-01-09 23:26:14 +0100903 if (err) {
904 printk(KERN_ERR DRVNAME ": Device resource addition failed "
905 "(%d)\n", err);
906 goto exit_device_put;
907 }
908
909 err = platform_device_add(pdev);
910 if (err) {
911 printk(KERN_ERR DRVNAME ": Device addition failed (%d)\n",
912 err);
913 goto exit_device_put;
914 }
915
916 return 0;
917
918exit_device_put:
919 platform_device_put(pdev);
920exit:
921 return err;
922}
923
924static int __init f71805f_find(int sioaddr, unsigned short *address)
925{
926 int err = -ENODEV;
927 u16 devid;
928
929 superio_enter(sioaddr);
930
931 devid = superio_inw(sioaddr, SIO_REG_MANID);
932 if (devid != SIO_FINTEK_ID)
933 goto exit;
934
935 devid = superio_inw(sioaddr, SIO_REG_DEVID);
936 if (devid != SIO_F71805F_ID) {
937 printk(KERN_INFO DRVNAME ": Unsupported Fintek device, "
938 "skipping\n");
939 goto exit;
940 }
941
942 superio_select(sioaddr, F71805F_LD_HWM);
943 if (!(superio_inb(sioaddr, SIO_REG_ENABLE) & 0x01)) {
944 printk(KERN_WARNING DRVNAME ": Device not activated, "
945 "skipping\n");
946 goto exit;
947 }
948
949 *address = superio_inw(sioaddr, SIO_REG_ADDR);
950 if (*address == 0) {
951 printk(KERN_WARNING DRVNAME ": Base address not set, "
952 "skipping\n");
953 goto exit;
954 }
955
956 err = 0;
957 printk(KERN_INFO DRVNAME ": Found F71805F chip at %#x, revision %u\n",
958 *address, superio_inb(sioaddr, SIO_REG_DEVREV));
959
960exit:
961 superio_exit(sioaddr);
962 return err;
963}
964
965static int __init f71805f_init(void)
966{
967 int err;
968 unsigned short address;
969
970 if (f71805f_find(0x2e, &address)
971 && f71805f_find(0x4e, &address))
972 return -ENODEV;
973
974 err = platform_driver_register(&f71805f_driver);
975 if (err)
976 goto exit;
977
978 /* Sets global pdev as a side effect */
979 err = f71805f_device_add(address);
980 if (err)
981 goto exit_driver;
982
983 return 0;
984
985exit_driver:
986 platform_driver_unregister(&f71805f_driver);
987exit:
988 return err;
989}
990
991static void __exit f71805f_exit(void)
992{
993 platform_device_unregister(pdev);
994 platform_driver_unregister(&f71805f_driver);
995}
996
997MODULE_AUTHOR("Jean Delvare <khali@linux-fr>");
998MODULE_LICENSE("GPL");
999MODULE_DESCRIPTION("F71805F hardware monitoring driver");
1000
1001module_init(f71805f_init);
1002module_exit(f71805f_exit);