blob: 678bae43716d079309cf4fa0e6a3630b34e015ad [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 Delvaree53004e2006-01-09 23:26:14 +010034#include <asm/io.h>
35
36static struct platform_device *pdev;
37
38#define DRVNAME "f71805f"
39
40/*
41 * Super-I/O constants and functions
42 */
43
44#define F71805F_LD_HWM 0x04
45
46#define SIO_REG_LDSEL 0x07 /* Logical device select */
47#define SIO_REG_DEVID 0x20 /* Device ID (2 bytes) */
48#define SIO_REG_DEVREV 0x22 /* Device revision */
49#define SIO_REG_MANID 0x23 /* Fintek ID (2 bytes) */
50#define SIO_REG_ENABLE 0x30 /* Logical device enable */
51#define SIO_REG_ADDR 0x60 /* Logical device address (2 bytes) */
52
53#define SIO_FINTEK_ID 0x1934
54#define SIO_F71805F_ID 0x0406
55
56static inline int
57superio_inb(int base, int reg)
58{
59 outb(reg, base);
60 return inb(base + 1);
61}
62
63static int
64superio_inw(int base, int reg)
65{
66 int val;
67 outb(reg++, base);
68 val = inb(base + 1) << 8;
69 outb(reg, base);
70 val |= inb(base + 1);
71 return val;
72}
73
74static inline void
75superio_select(int base, int ld)
76{
77 outb(SIO_REG_LDSEL, base);
78 outb(ld, base + 1);
79}
80
81static inline void
82superio_enter(int base)
83{
84 outb(0x87, base);
85 outb(0x87, base);
86}
87
88static inline void
89superio_exit(int base)
90{
91 outb(0xaa, base);
92}
93
94/*
95 * ISA constants
96 */
97
98#define REGION_LENGTH 2
99#define ADDR_REG_OFFSET 0
100#define DATA_REG_OFFSET 1
101
Jean Delvaree53004e2006-01-09 23:26:14 +0100102/*
103 * Registers
104 */
105
106/* in nr from 0 to 8 (8-bit values) */
107#define F71805F_REG_IN(nr) (0x10 + (nr))
108#define F71805F_REG_IN_HIGH(nr) (0x40 + 2 * (nr))
109#define F71805F_REG_IN_LOW(nr) (0x41 + 2 * (nr))
110/* fan nr from 0 to 2 (12-bit values, two registers) */
111#define F71805F_REG_FAN(nr) (0x20 + 2 * (nr))
112#define F71805F_REG_FAN_LOW(nr) (0x28 + 2 * (nr))
113#define F71805F_REG_FAN_CTRL(nr) (0x60 + 16 * (nr))
114/* temp nr from 0 to 2 (8-bit values) */
115#define F71805F_REG_TEMP(nr) (0x1B + (nr))
116#define F71805F_REG_TEMP_HIGH(nr) (0x54 + 2 * (nr))
117#define F71805F_REG_TEMP_HYST(nr) (0x55 + 2 * (nr))
118#define F71805F_REG_TEMP_MODE 0x01
119
120#define F71805F_REG_START 0x00
121/* status nr from 0 to 2 */
122#define F71805F_REG_STATUS(nr) (0x36 + (nr))
123
124/*
125 * Data structures and manipulation thereof
126 */
127
128struct f71805f_data {
129 unsigned short addr;
130 const char *name;
Jean Delvaref0819182006-01-18 23:20:53 +0100131 struct mutex lock;
Jean Delvaree53004e2006-01-09 23:26:14 +0100132 struct class_device *class_dev;
133
Jean Delvaref0819182006-01-18 23:20:53 +0100134 struct mutex update_lock;
Jean Delvaree53004e2006-01-09 23:26:14 +0100135 char valid; /* !=0 if following fields are valid */
136 unsigned long last_updated; /* In jiffies */
137 unsigned long last_limits; /* In jiffies */
138
139 /* Register values */
140 u8 in[9];
141 u8 in_high[9];
142 u8 in_low[9];
143 u16 fan[3];
144 u16 fan_low[3];
145 u8 fan_enabled; /* Read once at init time */
146 u8 temp[3];
147 u8 temp_high[3];
148 u8 temp_hyst[3];
149 u8 temp_mode;
Jean Delvare2d457712006-09-24 20:52:15 +0200150 unsigned long alarms;
Jean Delvaree53004e2006-01-09 23:26:14 +0100151};
152
153static inline long in_from_reg(u8 reg)
154{
155 return (reg * 8);
156}
157
158/* The 2 least significant bits are not used */
159static inline u8 in_to_reg(long val)
160{
161 if (val <= 0)
162 return 0;
163 if (val >= 2016)
164 return 0xfc;
165 return (((val + 16) / 32) << 2);
166}
167
168/* in0 is downscaled by a factor 2 internally */
169static inline long in0_from_reg(u8 reg)
170{
171 return (reg * 16);
172}
173
174static inline u8 in0_to_reg(long val)
175{
176 if (val <= 0)
177 return 0;
178 if (val >= 4032)
179 return 0xfc;
180 return (((val + 32) / 64) << 2);
181}
182
183/* The 4 most significant bits are not used */
184static inline long fan_from_reg(u16 reg)
185{
186 reg &= 0xfff;
187 if (!reg || reg == 0xfff)
188 return 0;
189 return (1500000 / reg);
190}
191
192static inline u16 fan_to_reg(long rpm)
193{
194 /* If the low limit is set below what the chip can measure,
195 store the largest possible 12-bit value in the registers,
196 so that no alarm will ever trigger. */
197 if (rpm < 367)
198 return 0xfff;
199 return (1500000 / rpm);
200}
201
202static inline long temp_from_reg(u8 reg)
203{
204 return (reg * 1000);
205}
206
207static inline u8 temp_to_reg(long val)
208{
209 if (val < 0)
210 val = 0;
211 else if (val > 1000 * 0xff)
212 val = 0xff;
213 return ((val + 500) / 1000);
214}
215
216/*
217 * Device I/O access
218 */
219
220static u8 f71805f_read8(struct f71805f_data *data, u8 reg)
221{
222 u8 val;
223
Jean Delvaref0819182006-01-18 23:20:53 +0100224 mutex_lock(&data->lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100225 outb(reg, data->addr + ADDR_REG_OFFSET);
226 val = inb(data->addr + DATA_REG_OFFSET);
Jean Delvaref0819182006-01-18 23:20:53 +0100227 mutex_unlock(&data->lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100228
229 return val;
230}
231
232static void f71805f_write8(struct f71805f_data *data, u8 reg, u8 val)
233{
Jean Delvaref0819182006-01-18 23:20:53 +0100234 mutex_lock(&data->lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100235 outb(reg, data->addr + ADDR_REG_OFFSET);
236 outb(val, data->addr + DATA_REG_OFFSET);
Jean Delvaref0819182006-01-18 23:20:53 +0100237 mutex_unlock(&data->lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100238}
239
240/* It is important to read the MSB first, because doing so latches the
241 value of the LSB, so we are sure both bytes belong to the same value. */
242static u16 f71805f_read16(struct f71805f_data *data, u8 reg)
243{
244 u16 val;
245
Jean Delvaref0819182006-01-18 23:20:53 +0100246 mutex_lock(&data->lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100247 outb(reg, data->addr + ADDR_REG_OFFSET);
248 val = inb(data->addr + DATA_REG_OFFSET) << 8;
249 outb(++reg, data->addr + ADDR_REG_OFFSET);
250 val |= inb(data->addr + DATA_REG_OFFSET);
Jean Delvaref0819182006-01-18 23:20:53 +0100251 mutex_unlock(&data->lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100252
253 return val;
254}
255
256static void f71805f_write16(struct f71805f_data *data, u8 reg, u16 val)
257{
Jean Delvaref0819182006-01-18 23:20:53 +0100258 mutex_lock(&data->lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100259 outb(reg, data->addr + ADDR_REG_OFFSET);
260 outb(val >> 8, data->addr + DATA_REG_OFFSET);
261 outb(++reg, data->addr + ADDR_REG_OFFSET);
262 outb(val & 0xff, data->addr + DATA_REG_OFFSET);
Jean Delvaref0819182006-01-18 23:20:53 +0100263 mutex_unlock(&data->lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100264}
265
266static struct f71805f_data *f71805f_update_device(struct device *dev)
267{
268 struct f71805f_data *data = dev_get_drvdata(dev);
269 int nr;
270
Jean Delvaref0819182006-01-18 23:20:53 +0100271 mutex_lock(&data->update_lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100272
273 /* Limit registers cache is refreshed after 60 seconds */
274 if (time_after(jiffies, data->last_updated + 60 * HZ)
275 || !data->valid) {
276 for (nr = 0; nr < 9; nr++) {
277 data->in_high[nr] = f71805f_read8(data,
278 F71805F_REG_IN_HIGH(nr));
279 data->in_low[nr] = f71805f_read8(data,
280 F71805F_REG_IN_LOW(nr));
281 }
282 for (nr = 0; nr < 3; nr++) {
283 if (data->fan_enabled & (1 << nr))
284 data->fan_low[nr] = f71805f_read16(data,
285 F71805F_REG_FAN_LOW(nr));
286 }
287 for (nr = 0; nr < 3; nr++) {
288 data->temp_high[nr] = f71805f_read8(data,
289 F71805F_REG_TEMP_HIGH(nr));
290 data->temp_hyst[nr] = f71805f_read8(data,
291 F71805F_REG_TEMP_HYST(nr));
292 }
293 data->temp_mode = f71805f_read8(data, F71805F_REG_TEMP_MODE);
294
295 data->last_limits = jiffies;
296 }
297
298 /* Measurement registers cache is refreshed after 1 second */
299 if (time_after(jiffies, data->last_updated + HZ)
300 || !data->valid) {
301 for (nr = 0; nr < 9; nr++) {
302 data->in[nr] = f71805f_read8(data,
303 F71805F_REG_IN(nr));
304 }
305 for (nr = 0; nr < 3; nr++) {
306 if (data->fan_enabled & (1 << nr))
307 data->fan[nr] = f71805f_read16(data,
308 F71805F_REG_FAN(nr));
309 }
310 for (nr = 0; nr < 3; nr++) {
311 data->temp[nr] = f71805f_read8(data,
312 F71805F_REG_TEMP(nr));
313 }
Jean Delvare2d457712006-09-24 20:52:15 +0200314 data->alarms = f71805f_read8(data, F71805F_REG_STATUS(0))
315 + (f71805f_read8(data, F71805F_REG_STATUS(1)) << 8)
316 + (f71805f_read8(data, F71805F_REG_STATUS(2)) << 16);
Jean Delvaree53004e2006-01-09 23:26:14 +0100317
318 data->last_updated = jiffies;
319 data->valid = 1;
320 }
321
Jean Delvaref0819182006-01-18 23:20:53 +0100322 mutex_unlock(&data->update_lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100323
324 return data;
325}
326
327/*
328 * Sysfs interface
329 */
330
331static ssize_t show_in0(struct device *dev, struct device_attribute *devattr,
332 char *buf)
333{
334 struct f71805f_data *data = f71805f_update_device(dev);
335
336 return sprintf(buf, "%ld\n", in0_from_reg(data->in[0]));
337}
338
339static ssize_t show_in0_max(struct device *dev, struct device_attribute
340 *devattr, char *buf)
341{
342 struct f71805f_data *data = f71805f_update_device(dev);
343
344 return sprintf(buf, "%ld\n", in0_from_reg(data->in_high[0]));
345}
346
347static ssize_t show_in0_min(struct device *dev, struct device_attribute
348 *devattr, char *buf)
349{
350 struct f71805f_data *data = f71805f_update_device(dev);
351
352 return sprintf(buf, "%ld\n", in0_from_reg(data->in_low[0]));
353}
354
355static ssize_t set_in0_max(struct device *dev, struct device_attribute
356 *devattr, const char *buf, size_t count)
357{
358 struct f71805f_data *data = dev_get_drvdata(dev);
359 long val = simple_strtol(buf, NULL, 10);
360
Jean Delvaref0819182006-01-18 23:20:53 +0100361 mutex_lock(&data->update_lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100362 data->in_high[0] = in0_to_reg(val);
363 f71805f_write8(data, F71805F_REG_IN_HIGH(0), data->in_high[0]);
Jean Delvaref0819182006-01-18 23:20:53 +0100364 mutex_unlock(&data->update_lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100365
366 return count;
367}
368
369static ssize_t set_in0_min(struct device *dev, struct device_attribute
370 *devattr, const char *buf, size_t count)
371{
372 struct f71805f_data *data = dev_get_drvdata(dev);
373 long val = simple_strtol(buf, NULL, 10);
374
Jean Delvaref0819182006-01-18 23:20:53 +0100375 mutex_lock(&data->update_lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100376 data->in_low[0] = in0_to_reg(val);
377 f71805f_write8(data, F71805F_REG_IN_LOW(0), data->in_low[0]);
Jean Delvaref0819182006-01-18 23:20:53 +0100378 mutex_unlock(&data->update_lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100379
380 return count;
381}
382
Jean Delvaree53004e2006-01-09 23:26:14 +0100383static ssize_t show_in(struct device *dev, struct device_attribute *devattr,
384 char *buf)
385{
386 struct f71805f_data *data = f71805f_update_device(dev);
387 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
388 int nr = attr->index;
389
390 return sprintf(buf, "%ld\n", in_from_reg(data->in[nr]));
391}
392
393static ssize_t show_in_max(struct device *dev, struct device_attribute
394 *devattr, char *buf)
395{
396 struct f71805f_data *data = f71805f_update_device(dev);
397 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
398 int nr = attr->index;
399
400 return sprintf(buf, "%ld\n", in_from_reg(data->in_high[nr]));
401}
402
403static ssize_t show_in_min(struct device *dev, struct device_attribute
404 *devattr, char *buf)
405{
406 struct f71805f_data *data = f71805f_update_device(dev);
407 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
408 int nr = attr->index;
409
410 return sprintf(buf, "%ld\n", in_from_reg(data->in_low[nr]));
411}
412
413static ssize_t set_in_max(struct device *dev, struct device_attribute
414 *devattr, const char *buf, size_t count)
415{
416 struct f71805f_data *data = dev_get_drvdata(dev);
417 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
418 int nr = attr->index;
419 long val = simple_strtol(buf, NULL, 10);
420
Jean Delvaref0819182006-01-18 23:20:53 +0100421 mutex_lock(&data->update_lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100422 data->in_high[nr] = in_to_reg(val);
423 f71805f_write8(data, F71805F_REG_IN_HIGH(nr), data->in_high[nr]);
Jean Delvaref0819182006-01-18 23:20:53 +0100424 mutex_unlock(&data->update_lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100425
426 return count;
427}
428
429static ssize_t set_in_min(struct device *dev, struct device_attribute
430 *devattr, const char *buf, size_t count)
431{
432 struct f71805f_data *data = dev_get_drvdata(dev);
433 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
434 int nr = attr->index;
435 long val = simple_strtol(buf, NULL, 10);
436
Jean Delvaref0819182006-01-18 23:20:53 +0100437 mutex_lock(&data->update_lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100438 data->in_low[nr] = in_to_reg(val);
439 f71805f_write8(data, F71805F_REG_IN_LOW(nr), data->in_low[nr]);
Jean Delvaref0819182006-01-18 23:20:53 +0100440 mutex_unlock(&data->update_lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100441
442 return count;
443}
444
Jean Delvaree53004e2006-01-09 23:26:14 +0100445static ssize_t show_fan(struct device *dev, struct device_attribute *devattr,
446 char *buf)
447{
448 struct f71805f_data *data = f71805f_update_device(dev);
449 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
450 int nr = attr->index;
451
452 return sprintf(buf, "%ld\n", fan_from_reg(data->fan[nr]));
453}
454
455static ssize_t show_fan_min(struct device *dev, struct device_attribute
456 *devattr, char *buf)
457{
458 struct f71805f_data *data = f71805f_update_device(dev);
459 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
460 int nr = attr->index;
461
462 return sprintf(buf, "%ld\n", fan_from_reg(data->fan_low[nr]));
463}
464
465static ssize_t set_fan_min(struct device *dev, struct device_attribute
466 *devattr, const char *buf, size_t count)
467{
468 struct f71805f_data *data = dev_get_drvdata(dev);
469 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
470 int nr = attr->index;
471 long val = simple_strtol(buf, NULL, 10);
472
Jean Delvaref0819182006-01-18 23:20:53 +0100473 mutex_lock(&data->update_lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100474 data->fan_low[nr] = fan_to_reg(val);
475 f71805f_write16(data, F71805F_REG_FAN_LOW(nr), data->fan_low[nr]);
Jean Delvaref0819182006-01-18 23:20:53 +0100476 mutex_unlock(&data->update_lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100477
478 return count;
479}
480
Jean Delvaree53004e2006-01-09 23:26:14 +0100481static ssize_t show_temp(struct device *dev, struct device_attribute *devattr,
482 char *buf)
483{
484 struct f71805f_data *data = f71805f_update_device(dev);
485 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
486 int nr = attr->index;
487
488 return sprintf(buf, "%ld\n", temp_from_reg(data->temp[nr]));
489}
490
491static ssize_t show_temp_max(struct device *dev, struct device_attribute
492 *devattr, char *buf)
493{
494 struct f71805f_data *data = f71805f_update_device(dev);
495 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
496 int nr = attr->index;
497
498 return sprintf(buf, "%ld\n", temp_from_reg(data->temp_high[nr]));
499}
500
501static ssize_t show_temp_hyst(struct device *dev, struct device_attribute
502 *devattr, char *buf)
503{
504 struct f71805f_data *data = f71805f_update_device(dev);
505 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
506 int nr = attr->index;
507
508 return sprintf(buf, "%ld\n", temp_from_reg(data->temp_hyst[nr]));
509}
510
511static ssize_t show_temp_type(struct device *dev, struct device_attribute
512 *devattr, char *buf)
513{
514 struct f71805f_data *data = f71805f_update_device(dev);
515 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
516 int nr = attr->index;
517
518 /* 3 is diode, 4 is thermistor */
519 return sprintf(buf, "%u\n", (data->temp_mode & (1 << nr)) ? 3 : 4);
520}
521
522static ssize_t set_temp_max(struct device *dev, struct device_attribute
523 *devattr, const char *buf, size_t count)
524{
525 struct f71805f_data *data = dev_get_drvdata(dev);
526 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
527 int nr = attr->index;
528 long val = simple_strtol(buf, NULL, 10);
529
Jean Delvaref0819182006-01-18 23:20:53 +0100530 mutex_lock(&data->update_lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100531 data->temp_high[nr] = temp_to_reg(val);
532 f71805f_write8(data, F71805F_REG_TEMP_HIGH(nr), data->temp_high[nr]);
Jean Delvaref0819182006-01-18 23:20:53 +0100533 mutex_unlock(&data->update_lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100534
535 return count;
536}
537
538static ssize_t set_temp_hyst(struct device *dev, struct device_attribute
539 *devattr, const char *buf, size_t count)
540{
541 struct f71805f_data *data = dev_get_drvdata(dev);
542 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
543 int nr = attr->index;
544 long val = simple_strtol(buf, NULL, 10);
545
Jean Delvaref0819182006-01-18 23:20:53 +0100546 mutex_lock(&data->update_lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100547 data->temp_hyst[nr] = temp_to_reg(val);
548 f71805f_write8(data, F71805F_REG_TEMP_HYST(nr), data->temp_hyst[nr]);
Jean Delvaref0819182006-01-18 23:20:53 +0100549 mutex_unlock(&data->update_lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100550
551 return count;
552}
553
Jean Delvaree53004e2006-01-09 23:26:14 +0100554static ssize_t show_alarms_in(struct device *dev, struct device_attribute
555 *devattr, char *buf)
556{
557 struct f71805f_data *data = f71805f_update_device(dev);
558
Jean Delvare2d457712006-09-24 20:52:15 +0200559 return sprintf(buf, "%lu\n", data->alarms & 0x1ff);
Jean Delvaree53004e2006-01-09 23:26:14 +0100560}
561
562static ssize_t show_alarms_fan(struct device *dev, struct device_attribute
563 *devattr, char *buf)
564{
565 struct f71805f_data *data = f71805f_update_device(dev);
566
Jean Delvare2d457712006-09-24 20:52:15 +0200567 return sprintf(buf, "%lu\n", (data->alarms >> 16) & 0x07);
Jean Delvaree53004e2006-01-09 23:26:14 +0100568}
569
570static ssize_t show_alarms_temp(struct device *dev, struct device_attribute
571 *devattr, char *buf)
572{
573 struct f71805f_data *data = f71805f_update_device(dev);
574
Jean Delvare2d457712006-09-24 20:52:15 +0200575 return sprintf(buf, "%lu\n", (data->alarms >> 11) & 0x07);
576}
577
578static ssize_t show_alarm(struct device *dev, struct device_attribute
579 *devattr, char *buf)
580{
581 struct f71805f_data *data = f71805f_update_device(dev);
582 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
583 int bitnr = attr->index;
584
585 return sprintf(buf, "%lu\n", (data->alarms >> bitnr) & 1);
Jean Delvaree53004e2006-01-09 23:26:14 +0100586}
587
Jean Delvaree53004e2006-01-09 23:26:14 +0100588static ssize_t show_name(struct device *dev, struct device_attribute
589 *devattr, char *buf)
590{
591 struct f71805f_data *data = dev_get_drvdata(dev);
592
593 return sprintf(buf, "%s\n", data->name);
594}
595
Jean Delvare2488a392006-01-09 23:29:11 +0100596static struct device_attribute f71805f_dev_attr[] = {
597 __ATTR(in0_input, S_IRUGO, show_in0, NULL),
598 __ATTR(in0_max, S_IRUGO| S_IWUSR, show_in0_max, set_in0_max),
599 __ATTR(in0_min, S_IRUGO| S_IWUSR, show_in0_min, set_in0_min),
600 __ATTR(alarms_in, S_IRUGO, show_alarms_in, NULL),
601 __ATTR(alarms_fan, S_IRUGO, show_alarms_fan, NULL),
602 __ATTR(alarms_temp, S_IRUGO, show_alarms_temp, NULL),
603 __ATTR(name, S_IRUGO, show_name, NULL),
604};
605
606static struct sensor_device_attribute f71805f_sensor_attr[] = {
607 SENSOR_ATTR(in1_input, S_IRUGO, show_in, NULL, 1),
608 SENSOR_ATTR(in1_max, S_IRUGO | S_IWUSR,
609 show_in_max, set_in_max, 1),
610 SENSOR_ATTR(in1_min, S_IRUGO | S_IWUSR,
611 show_in_min, set_in_min, 1),
612 SENSOR_ATTR(in2_input, S_IRUGO, show_in, NULL, 2),
613 SENSOR_ATTR(in2_max, S_IRUGO | S_IWUSR,
614 show_in_max, set_in_max, 2),
615 SENSOR_ATTR(in2_min, S_IRUGO | S_IWUSR,
616 show_in_min, set_in_min, 2),
617 SENSOR_ATTR(in3_input, S_IRUGO, show_in, NULL, 3),
618 SENSOR_ATTR(in3_max, S_IRUGO | S_IWUSR,
619 show_in_max, set_in_max, 3),
620 SENSOR_ATTR(in3_min, S_IRUGO | S_IWUSR,
621 show_in_min, set_in_min, 3),
622 SENSOR_ATTR(in4_input, S_IRUGO, show_in, NULL, 4),
623 SENSOR_ATTR(in4_max, S_IRUGO | S_IWUSR,
624 show_in_max, set_in_max, 4),
625 SENSOR_ATTR(in4_min, S_IRUGO | S_IWUSR,
626 show_in_min, set_in_min, 4),
627 SENSOR_ATTR(in5_input, S_IRUGO, show_in, NULL, 5),
628 SENSOR_ATTR(in5_max, S_IRUGO | S_IWUSR,
629 show_in_max, set_in_max, 5),
630 SENSOR_ATTR(in5_min, S_IRUGO | S_IWUSR,
631 show_in_min, set_in_min, 5),
632 SENSOR_ATTR(in6_input, S_IRUGO, show_in, NULL, 6),
633 SENSOR_ATTR(in6_max, S_IRUGO | S_IWUSR,
634 show_in_max, set_in_max, 6),
635 SENSOR_ATTR(in6_min, S_IRUGO | S_IWUSR,
636 show_in_min, set_in_min, 6),
637 SENSOR_ATTR(in7_input, S_IRUGO, show_in, NULL, 7),
638 SENSOR_ATTR(in7_max, S_IRUGO | S_IWUSR,
639 show_in_max, set_in_max, 7),
640 SENSOR_ATTR(in7_min, S_IRUGO | S_IWUSR,
641 show_in_min, set_in_min, 7),
642 SENSOR_ATTR(in8_input, S_IRUGO, show_in, NULL, 8),
643 SENSOR_ATTR(in8_max, S_IRUGO | S_IWUSR,
644 show_in_max, set_in_max, 8),
645 SENSOR_ATTR(in8_min, S_IRUGO | S_IWUSR,
646 show_in_min, set_in_min, 8),
647
648 SENSOR_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0),
649 SENSOR_ATTR(temp1_max, S_IRUGO | S_IWUSR,
650 show_temp_max, set_temp_max, 0),
651 SENSOR_ATTR(temp1_max_hyst, S_IRUGO | S_IWUSR,
652 show_temp_hyst, set_temp_hyst, 0),
653 SENSOR_ATTR(temp1_type, S_IRUGO, show_temp_type, NULL, 0),
654 SENSOR_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1),
655 SENSOR_ATTR(temp2_max, S_IRUGO | S_IWUSR,
656 show_temp_max, set_temp_max, 1),
657 SENSOR_ATTR(temp2_max_hyst, S_IRUGO | S_IWUSR,
658 show_temp_hyst, set_temp_hyst, 1),
659 SENSOR_ATTR(temp2_type, S_IRUGO, show_temp_type, NULL, 1),
660 SENSOR_ATTR(temp3_input, S_IRUGO, show_temp, NULL, 2),
661 SENSOR_ATTR(temp3_max, S_IRUGO | S_IWUSR,
662 show_temp_max, set_temp_max, 2),
663 SENSOR_ATTR(temp3_max_hyst, S_IRUGO | S_IWUSR,
664 show_temp_hyst, set_temp_hyst, 2),
665 SENSOR_ATTR(temp3_type, S_IRUGO, show_temp_type, NULL, 2),
Jean Delvare2d457712006-09-24 20:52:15 +0200666
667 SENSOR_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0),
668 SENSOR_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1),
669 SENSOR_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2),
670 SENSOR_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3),
671 SENSOR_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 4),
672 SENSOR_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 5),
673 SENSOR_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 6),
674 SENSOR_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 7),
675 SENSOR_ATTR(in8_alarm, S_IRUGO, show_alarm, NULL, 8),
676 SENSOR_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 11),
677 SENSOR_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 12),
678 SENSOR_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 13),
Jean Delvare2488a392006-01-09 23:29:11 +0100679};
680
681static struct sensor_device_attribute f71805f_fan_attr[] = {
682 SENSOR_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0),
683 SENSOR_ATTR(fan1_min, S_IRUGO | S_IWUSR,
684 show_fan_min, set_fan_min, 0),
Jean Delvare2d457712006-09-24 20:52:15 +0200685 SENSOR_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 16),
Jean Delvare2488a392006-01-09 23:29:11 +0100686 SENSOR_ATTR(fan2_input, S_IRUGO, show_fan, NULL, 1),
687 SENSOR_ATTR(fan2_min, S_IRUGO | S_IWUSR,
688 show_fan_min, set_fan_min, 1),
Jean Delvare2d457712006-09-24 20:52:15 +0200689 SENSOR_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 17),
Jean Delvare2488a392006-01-09 23:29:11 +0100690 SENSOR_ATTR(fan3_input, S_IRUGO, show_fan, NULL, 2),
691 SENSOR_ATTR(fan3_min, S_IRUGO | S_IWUSR,
692 show_fan_min, set_fan_min, 2),
Jean Delvare2d457712006-09-24 20:52:15 +0200693 SENSOR_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 18),
Jean Delvare2488a392006-01-09 23:29:11 +0100694};
Jean Delvaree53004e2006-01-09 23:26:14 +0100695
696/*
697 * Device registration and initialization
698 */
699
700static void __devinit f71805f_init_device(struct f71805f_data *data)
701{
702 u8 reg;
703 int i;
704
705 reg = f71805f_read8(data, F71805F_REG_START);
706 if ((reg & 0x41) != 0x01) {
707 printk(KERN_DEBUG DRVNAME ": Starting monitoring "
708 "operations\n");
709 f71805f_write8(data, F71805F_REG_START, (reg | 0x01) & ~0x40);
710 }
711
712 /* Fan monitoring can be disabled. If it is, we won't be polling
713 the register values, and won't create the related sysfs files. */
714 for (i = 0; i < 3; i++) {
715 reg = f71805f_read8(data, F71805F_REG_FAN_CTRL(i));
716 if (!(reg & 0x80))
717 data->fan_enabled |= (1 << i);
718 }
719}
720
721static int __devinit f71805f_probe(struct platform_device *pdev)
722{
723 struct f71805f_data *data;
724 struct resource *res;
Jean Delvare2488a392006-01-09 23:29:11 +0100725 int i, err;
Jean Delvaree53004e2006-01-09 23:26:14 +0100726
727 if (!(data = kzalloc(sizeof(struct f71805f_data), GFP_KERNEL))) {
728 err = -ENOMEM;
729 printk(KERN_ERR DRVNAME ": Out of memory\n");
730 goto exit;
731 }
732
733 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
734 data->addr = res->start;
Jean Delvaref0819182006-01-18 23:20:53 +0100735 mutex_init(&data->lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100736 data->name = "f71805f";
Jean Delvaref0819182006-01-18 23:20:53 +0100737 mutex_init(&data->update_lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100738
739 platform_set_drvdata(pdev, data);
740
741 data->class_dev = hwmon_device_register(&pdev->dev);
742 if (IS_ERR(data->class_dev)) {
743 err = PTR_ERR(data->class_dev);
744 dev_err(&pdev->dev, "Class registration failed (%d)\n", err);
745 goto exit_free;
746 }
747
748 /* Initialize the F71805F chip */
749 f71805f_init_device(data);
750
751 /* Register sysfs interface files */
Jean Delvare2488a392006-01-09 23:29:11 +0100752 for (i = 0; i < ARRAY_SIZE(f71805f_dev_attr); i++) {
753 err = device_create_file(&pdev->dev, &f71805f_dev_attr[i]);
754 if (err)
755 goto exit_class;
Jean Delvaree53004e2006-01-09 23:26:14 +0100756 }
Jean Delvare2488a392006-01-09 23:29:11 +0100757 for (i = 0; i < ARRAY_SIZE(f71805f_sensor_attr); i++) {
758 err = device_create_file(&pdev->dev,
759 &f71805f_sensor_attr[i].dev_attr);
760 if (err)
761 goto exit_class;
Jean Delvaree53004e2006-01-09 23:26:14 +0100762 }
Jean Delvare2488a392006-01-09 23:29:11 +0100763 for (i = 0; i < ARRAY_SIZE(f71805f_fan_attr); i++) {
Jean Delvare2d457712006-09-24 20:52:15 +0200764 if (!(data->fan_enabled & (1 << (i / 3))))
Jean Delvare2488a392006-01-09 23:29:11 +0100765 continue;
766 err = device_create_file(&pdev->dev,
767 &f71805f_fan_attr[i].dev_attr);
768 if (err)
769 goto exit_class;
Jean Delvaree53004e2006-01-09 23:26:14 +0100770 }
Jean Delvaree53004e2006-01-09 23:26:14 +0100771
772 return 0;
773
Jean Delvare2488a392006-01-09 23:29:11 +0100774exit_class:
775 dev_err(&pdev->dev, "Sysfs interface creation failed\n");
776 hwmon_device_unregister(data->class_dev);
Jean Delvaree53004e2006-01-09 23:26:14 +0100777exit_free:
778 kfree(data);
779exit:
780 return err;
781}
782
783static int __devexit f71805f_remove(struct platform_device *pdev)
784{
785 struct f71805f_data *data = platform_get_drvdata(pdev);
786
787 platform_set_drvdata(pdev, NULL);
788 hwmon_device_unregister(data->class_dev);
789 kfree(data);
790
791 return 0;
792}
793
794static struct platform_driver f71805f_driver = {
795 .driver = {
796 .owner = THIS_MODULE,
797 .name = DRVNAME,
798 },
799 .probe = f71805f_probe,
800 .remove = __devexit_p(f71805f_remove),
801};
802
803static int __init f71805f_device_add(unsigned short address)
804{
Jean Delvare568825c2006-03-23 16:40:23 +0100805 struct resource res = {
806 .start = address,
807 .end = address + REGION_LENGTH - 1,
808 .flags = IORESOURCE_IO,
809 };
Jean Delvaree53004e2006-01-09 23:26:14 +0100810 int err;
811
812 pdev = platform_device_alloc(DRVNAME, address);
813 if (!pdev) {
814 err = -ENOMEM;
815 printk(KERN_ERR DRVNAME ": Device allocation failed\n");
816 goto exit;
817 }
818
Jean Delvare568825c2006-03-23 16:40:23 +0100819 res.name = pdev->name;
820 err = platform_device_add_resources(pdev, &res, 1);
Jean Delvaree53004e2006-01-09 23:26:14 +0100821 if (err) {
822 printk(KERN_ERR DRVNAME ": Device resource addition failed "
823 "(%d)\n", err);
824 goto exit_device_put;
825 }
826
827 err = platform_device_add(pdev);
828 if (err) {
829 printk(KERN_ERR DRVNAME ": Device addition failed (%d)\n",
830 err);
831 goto exit_device_put;
832 }
833
834 return 0;
835
836exit_device_put:
837 platform_device_put(pdev);
838exit:
839 return err;
840}
841
842static int __init f71805f_find(int sioaddr, unsigned short *address)
843{
844 int err = -ENODEV;
845 u16 devid;
846
847 superio_enter(sioaddr);
848
849 devid = superio_inw(sioaddr, SIO_REG_MANID);
850 if (devid != SIO_FINTEK_ID)
851 goto exit;
852
853 devid = superio_inw(sioaddr, SIO_REG_DEVID);
854 if (devid != SIO_F71805F_ID) {
855 printk(KERN_INFO DRVNAME ": Unsupported Fintek device, "
856 "skipping\n");
857 goto exit;
858 }
859
860 superio_select(sioaddr, F71805F_LD_HWM);
861 if (!(superio_inb(sioaddr, SIO_REG_ENABLE) & 0x01)) {
862 printk(KERN_WARNING DRVNAME ": Device not activated, "
863 "skipping\n");
864 goto exit;
865 }
866
867 *address = superio_inw(sioaddr, SIO_REG_ADDR);
868 if (*address == 0) {
869 printk(KERN_WARNING DRVNAME ": Base address not set, "
870 "skipping\n");
871 goto exit;
872 }
873
874 err = 0;
875 printk(KERN_INFO DRVNAME ": Found F71805F chip at %#x, revision %u\n",
876 *address, superio_inb(sioaddr, SIO_REG_DEVREV));
877
878exit:
879 superio_exit(sioaddr);
880 return err;
881}
882
883static int __init f71805f_init(void)
884{
885 int err;
886 unsigned short address;
887
888 if (f71805f_find(0x2e, &address)
889 && f71805f_find(0x4e, &address))
890 return -ENODEV;
891
892 err = platform_driver_register(&f71805f_driver);
893 if (err)
894 goto exit;
895
896 /* Sets global pdev as a side effect */
897 err = f71805f_device_add(address);
898 if (err)
899 goto exit_driver;
900
901 return 0;
902
903exit_driver:
904 platform_driver_unregister(&f71805f_driver);
905exit:
906 return err;
907}
908
909static void __exit f71805f_exit(void)
910{
911 platform_device_unregister(pdev);
912 platform_driver_unregister(&f71805f_driver);
913}
914
915MODULE_AUTHOR("Jean Delvare <khali@linux-fr>");
916MODULE_LICENSE("GPL");
917MODULE_DESCRIPTION("F71805F hardware monitoring driver");
918
919module_init(f71805f_init);
920module_exit(f71805f_exit);