blob: dfca856854c365a5c5ef373ba65b4a5ccec2396f [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
Jean Delvare6b14a542006-12-12 18:18:26 +0100125/* individual register bits */
126#define FAN_CTRL_SKIP 0x80
127
Jean Delvaree53004e2006-01-09 23:26:14 +0100128/*
129 * Data structures and manipulation thereof
130 */
131
132struct f71805f_data {
133 unsigned short addr;
134 const char *name;
Jean Delvaref0819182006-01-18 23:20:53 +0100135 struct mutex lock;
Jean Delvaree53004e2006-01-09 23:26:14 +0100136 struct class_device *class_dev;
137
Jean Delvaref0819182006-01-18 23:20:53 +0100138 struct mutex update_lock;
Jean Delvaree53004e2006-01-09 23:26:14 +0100139 char valid; /* !=0 if following fields are valid */
140 unsigned long last_updated; /* In jiffies */
141 unsigned long last_limits; /* In jiffies */
142
143 /* Register values */
144 u8 in[9];
145 u8 in_high[9];
146 u8 in_low[9];
147 u16 fan[3];
148 u16 fan_low[3];
Jean Delvare6b14a542006-12-12 18:18:26 +0100149 u8 fan_ctrl[3];
Jean Delvaree53004e2006-01-09 23:26:14 +0100150 u8 temp[3];
151 u8 temp_high[3];
152 u8 temp_hyst[3];
153 u8 temp_mode;
Jean Delvare2d457712006-09-24 20:52:15 +0200154 unsigned long alarms;
Jean Delvaree53004e2006-01-09 23:26:14 +0100155};
156
157static inline long in_from_reg(u8 reg)
158{
159 return (reg * 8);
160}
161
162/* The 2 least significant bits are not used */
163static inline u8 in_to_reg(long val)
164{
165 if (val <= 0)
166 return 0;
167 if (val >= 2016)
168 return 0xfc;
169 return (((val + 16) / 32) << 2);
170}
171
172/* in0 is downscaled by a factor 2 internally */
173static inline long in0_from_reg(u8 reg)
174{
175 return (reg * 16);
176}
177
178static inline u8 in0_to_reg(long val)
179{
180 if (val <= 0)
181 return 0;
182 if (val >= 4032)
183 return 0xfc;
184 return (((val + 32) / 64) << 2);
185}
186
187/* The 4 most significant bits are not used */
188static inline long fan_from_reg(u16 reg)
189{
190 reg &= 0xfff;
191 if (!reg || reg == 0xfff)
192 return 0;
193 return (1500000 / reg);
194}
195
196static inline u16 fan_to_reg(long rpm)
197{
198 /* If the low limit is set below what the chip can measure,
199 store the largest possible 12-bit value in the registers,
200 so that no alarm will ever trigger. */
201 if (rpm < 367)
202 return 0xfff;
203 return (1500000 / rpm);
204}
205
206static inline long temp_from_reg(u8 reg)
207{
208 return (reg * 1000);
209}
210
211static inline u8 temp_to_reg(long val)
212{
213 if (val < 0)
214 val = 0;
215 else if (val > 1000 * 0xff)
216 val = 0xff;
217 return ((val + 500) / 1000);
218}
219
220/*
221 * Device I/O access
222 */
223
224static u8 f71805f_read8(struct f71805f_data *data, u8 reg)
225{
226 u8 val;
227
Jean Delvaref0819182006-01-18 23:20:53 +0100228 mutex_lock(&data->lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100229 outb(reg, data->addr + ADDR_REG_OFFSET);
230 val = inb(data->addr + DATA_REG_OFFSET);
Jean Delvaref0819182006-01-18 23:20:53 +0100231 mutex_unlock(&data->lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100232
233 return val;
234}
235
236static void f71805f_write8(struct f71805f_data *data, u8 reg, u8 val)
237{
Jean Delvaref0819182006-01-18 23:20:53 +0100238 mutex_lock(&data->lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100239 outb(reg, data->addr + ADDR_REG_OFFSET);
240 outb(val, data->addr + DATA_REG_OFFSET);
Jean Delvaref0819182006-01-18 23:20:53 +0100241 mutex_unlock(&data->lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100242}
243
244/* It is important to read the MSB first, because doing so latches the
245 value of the LSB, so we are sure both bytes belong to the same value. */
246static u16 f71805f_read16(struct f71805f_data *data, u8 reg)
247{
248 u16 val;
249
Jean Delvaref0819182006-01-18 23:20:53 +0100250 mutex_lock(&data->lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100251 outb(reg, data->addr + ADDR_REG_OFFSET);
252 val = inb(data->addr + DATA_REG_OFFSET) << 8;
253 outb(++reg, data->addr + ADDR_REG_OFFSET);
254 val |= inb(data->addr + DATA_REG_OFFSET);
Jean Delvaref0819182006-01-18 23:20:53 +0100255 mutex_unlock(&data->lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100256
257 return val;
258}
259
260static void f71805f_write16(struct f71805f_data *data, u8 reg, u16 val)
261{
Jean Delvaref0819182006-01-18 23:20:53 +0100262 mutex_lock(&data->lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100263 outb(reg, data->addr + ADDR_REG_OFFSET);
264 outb(val >> 8, data->addr + DATA_REG_OFFSET);
265 outb(++reg, data->addr + ADDR_REG_OFFSET);
266 outb(val & 0xff, data->addr + DATA_REG_OFFSET);
Jean Delvaref0819182006-01-18 23:20:53 +0100267 mutex_unlock(&data->lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100268}
269
270static struct f71805f_data *f71805f_update_device(struct device *dev)
271{
272 struct f71805f_data *data = dev_get_drvdata(dev);
273 int nr;
274
Jean Delvaref0819182006-01-18 23:20:53 +0100275 mutex_lock(&data->update_lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100276
277 /* Limit registers cache is refreshed after 60 seconds */
278 if (time_after(jiffies, data->last_updated + 60 * HZ)
279 || !data->valid) {
280 for (nr = 0; nr < 9; nr++) {
281 data->in_high[nr] = f71805f_read8(data,
282 F71805F_REG_IN_HIGH(nr));
283 data->in_low[nr] = f71805f_read8(data,
284 F71805F_REG_IN_LOW(nr));
285 }
286 for (nr = 0; nr < 3; nr++) {
Jean Delvare6b14a542006-12-12 18:18:26 +0100287 if (data->fan_ctrl[nr] & FAN_CTRL_SKIP)
288 continue;
289 data->fan_low[nr] = f71805f_read16(data,
290 F71805F_REG_FAN_LOW(nr));
Jean Delvaree53004e2006-01-09 23:26:14 +0100291 }
292 for (nr = 0; nr < 3; nr++) {
293 data->temp_high[nr] = f71805f_read8(data,
294 F71805F_REG_TEMP_HIGH(nr));
295 data->temp_hyst[nr] = f71805f_read8(data,
296 F71805F_REG_TEMP_HYST(nr));
297 }
298 data->temp_mode = f71805f_read8(data, F71805F_REG_TEMP_MODE);
299
300 data->last_limits = jiffies;
301 }
302
303 /* Measurement registers cache is refreshed after 1 second */
304 if (time_after(jiffies, data->last_updated + HZ)
305 || !data->valid) {
306 for (nr = 0; nr < 9; nr++) {
307 data->in[nr] = f71805f_read8(data,
308 F71805F_REG_IN(nr));
309 }
310 for (nr = 0; nr < 3; nr++) {
Jean Delvare6b14a542006-12-12 18:18:26 +0100311 if (data->fan_ctrl[nr] & FAN_CTRL_SKIP)
312 continue;
313 data->fan[nr] = f71805f_read16(data,
314 F71805F_REG_FAN(nr));
Jean Delvaree53004e2006-01-09 23:26:14 +0100315 }
316 for (nr = 0; nr < 3; nr++) {
317 data->temp[nr] = f71805f_read8(data,
318 F71805F_REG_TEMP(nr));
319 }
Jean Delvare2d457712006-09-24 20:52:15 +0200320 data->alarms = f71805f_read8(data, F71805F_REG_STATUS(0))
321 + (f71805f_read8(data, F71805F_REG_STATUS(1)) << 8)
322 + (f71805f_read8(data, F71805F_REG_STATUS(2)) << 16);
Jean Delvaree53004e2006-01-09 23:26:14 +0100323
324 data->last_updated = jiffies;
325 data->valid = 1;
326 }
327
Jean Delvaref0819182006-01-18 23:20:53 +0100328 mutex_unlock(&data->update_lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100329
330 return data;
331}
332
333/*
334 * Sysfs interface
335 */
336
337static ssize_t show_in0(struct device *dev, struct device_attribute *devattr,
338 char *buf)
339{
340 struct f71805f_data *data = f71805f_update_device(dev);
341
342 return sprintf(buf, "%ld\n", in0_from_reg(data->in[0]));
343}
344
345static ssize_t show_in0_max(struct device *dev, struct device_attribute
346 *devattr, char *buf)
347{
348 struct f71805f_data *data = f71805f_update_device(dev);
349
350 return sprintf(buf, "%ld\n", in0_from_reg(data->in_high[0]));
351}
352
353static ssize_t show_in0_min(struct device *dev, struct device_attribute
354 *devattr, char *buf)
355{
356 struct f71805f_data *data = f71805f_update_device(dev);
357
358 return sprintf(buf, "%ld\n", in0_from_reg(data->in_low[0]));
359}
360
361static ssize_t set_in0_max(struct device *dev, struct device_attribute
362 *devattr, const char *buf, size_t count)
363{
364 struct f71805f_data *data = dev_get_drvdata(dev);
365 long val = simple_strtol(buf, NULL, 10);
366
Jean Delvaref0819182006-01-18 23:20:53 +0100367 mutex_lock(&data->update_lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100368 data->in_high[0] = in0_to_reg(val);
369 f71805f_write8(data, F71805F_REG_IN_HIGH(0), data->in_high[0]);
Jean Delvaref0819182006-01-18 23:20:53 +0100370 mutex_unlock(&data->update_lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100371
372 return count;
373}
374
375static ssize_t set_in0_min(struct device *dev, struct device_attribute
376 *devattr, const char *buf, size_t count)
377{
378 struct f71805f_data *data = dev_get_drvdata(dev);
379 long val = simple_strtol(buf, NULL, 10);
380
Jean Delvaref0819182006-01-18 23:20:53 +0100381 mutex_lock(&data->update_lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100382 data->in_low[0] = in0_to_reg(val);
383 f71805f_write8(data, F71805F_REG_IN_LOW(0), data->in_low[0]);
Jean Delvaref0819182006-01-18 23:20:53 +0100384 mutex_unlock(&data->update_lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100385
386 return count;
387}
388
Jean Delvaree53004e2006-01-09 23:26:14 +0100389static ssize_t show_in(struct device *dev, struct device_attribute *devattr,
390 char *buf)
391{
392 struct f71805f_data *data = f71805f_update_device(dev);
393 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
394 int nr = attr->index;
395
396 return sprintf(buf, "%ld\n", in_from_reg(data->in[nr]));
397}
398
399static ssize_t show_in_max(struct device *dev, struct device_attribute
400 *devattr, char *buf)
401{
402 struct f71805f_data *data = f71805f_update_device(dev);
403 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
404 int nr = attr->index;
405
406 return sprintf(buf, "%ld\n", in_from_reg(data->in_high[nr]));
407}
408
409static ssize_t show_in_min(struct device *dev, struct device_attribute
410 *devattr, char *buf)
411{
412 struct f71805f_data *data = f71805f_update_device(dev);
413 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
414 int nr = attr->index;
415
416 return sprintf(buf, "%ld\n", in_from_reg(data->in_low[nr]));
417}
418
419static ssize_t set_in_max(struct device *dev, struct device_attribute
420 *devattr, const char *buf, size_t count)
421{
422 struct f71805f_data *data = dev_get_drvdata(dev);
423 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
424 int nr = attr->index;
425 long val = simple_strtol(buf, NULL, 10);
426
Jean Delvaref0819182006-01-18 23:20:53 +0100427 mutex_lock(&data->update_lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100428 data->in_high[nr] = in_to_reg(val);
429 f71805f_write8(data, F71805F_REG_IN_HIGH(nr), data->in_high[nr]);
Jean Delvaref0819182006-01-18 23:20:53 +0100430 mutex_unlock(&data->update_lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100431
432 return count;
433}
434
435static ssize_t set_in_min(struct device *dev, struct device_attribute
436 *devattr, const char *buf, size_t count)
437{
438 struct f71805f_data *data = dev_get_drvdata(dev);
439 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
440 int nr = attr->index;
441 long val = simple_strtol(buf, NULL, 10);
442
Jean Delvaref0819182006-01-18 23:20:53 +0100443 mutex_lock(&data->update_lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100444 data->in_low[nr] = in_to_reg(val);
445 f71805f_write8(data, F71805F_REG_IN_LOW(nr), data->in_low[nr]);
Jean Delvaref0819182006-01-18 23:20:53 +0100446 mutex_unlock(&data->update_lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100447
448 return count;
449}
450
Jean Delvaree53004e2006-01-09 23:26:14 +0100451static ssize_t show_fan(struct device *dev, struct device_attribute *devattr,
452 char *buf)
453{
454 struct f71805f_data *data = f71805f_update_device(dev);
455 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
456 int nr = attr->index;
457
458 return sprintf(buf, "%ld\n", fan_from_reg(data->fan[nr]));
459}
460
461static ssize_t show_fan_min(struct device *dev, struct device_attribute
462 *devattr, char *buf)
463{
464 struct f71805f_data *data = f71805f_update_device(dev);
465 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
466 int nr = attr->index;
467
468 return sprintf(buf, "%ld\n", fan_from_reg(data->fan_low[nr]));
469}
470
471static ssize_t set_fan_min(struct device *dev, struct device_attribute
472 *devattr, const char *buf, size_t count)
473{
474 struct f71805f_data *data = dev_get_drvdata(dev);
475 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
476 int nr = attr->index;
477 long val = simple_strtol(buf, NULL, 10);
478
Jean Delvaref0819182006-01-18 23:20:53 +0100479 mutex_lock(&data->update_lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100480 data->fan_low[nr] = fan_to_reg(val);
481 f71805f_write16(data, F71805F_REG_FAN_LOW(nr), data->fan_low[nr]);
Jean Delvaref0819182006-01-18 23:20:53 +0100482 mutex_unlock(&data->update_lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100483
484 return count;
485}
486
Jean Delvaree53004e2006-01-09 23:26:14 +0100487static ssize_t show_temp(struct device *dev, struct device_attribute *devattr,
488 char *buf)
489{
490 struct f71805f_data *data = f71805f_update_device(dev);
491 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
492 int nr = attr->index;
493
494 return sprintf(buf, "%ld\n", temp_from_reg(data->temp[nr]));
495}
496
497static ssize_t show_temp_max(struct device *dev, struct device_attribute
498 *devattr, char *buf)
499{
500 struct f71805f_data *data = f71805f_update_device(dev);
501 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
502 int nr = attr->index;
503
504 return sprintf(buf, "%ld\n", temp_from_reg(data->temp_high[nr]));
505}
506
507static ssize_t show_temp_hyst(struct device *dev, struct device_attribute
508 *devattr, char *buf)
509{
510 struct f71805f_data *data = f71805f_update_device(dev);
511 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
512 int nr = attr->index;
513
514 return sprintf(buf, "%ld\n", temp_from_reg(data->temp_hyst[nr]));
515}
516
517static ssize_t show_temp_type(struct device *dev, struct device_attribute
518 *devattr, char *buf)
519{
520 struct f71805f_data *data = f71805f_update_device(dev);
521 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
522 int nr = attr->index;
523
524 /* 3 is diode, 4 is thermistor */
525 return sprintf(buf, "%u\n", (data->temp_mode & (1 << nr)) ? 3 : 4);
526}
527
528static ssize_t set_temp_max(struct device *dev, struct device_attribute
529 *devattr, const char *buf, size_t count)
530{
531 struct f71805f_data *data = dev_get_drvdata(dev);
532 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
533 int nr = attr->index;
534 long val = simple_strtol(buf, NULL, 10);
535
Jean Delvaref0819182006-01-18 23:20:53 +0100536 mutex_lock(&data->update_lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100537 data->temp_high[nr] = temp_to_reg(val);
538 f71805f_write8(data, F71805F_REG_TEMP_HIGH(nr), data->temp_high[nr]);
Jean Delvaref0819182006-01-18 23:20:53 +0100539 mutex_unlock(&data->update_lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100540
541 return count;
542}
543
544static ssize_t set_temp_hyst(struct device *dev, struct device_attribute
545 *devattr, const char *buf, size_t count)
546{
547 struct f71805f_data *data = dev_get_drvdata(dev);
548 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
549 int nr = attr->index;
550 long val = simple_strtol(buf, NULL, 10);
551
Jean Delvaref0819182006-01-18 23:20:53 +0100552 mutex_lock(&data->update_lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100553 data->temp_hyst[nr] = temp_to_reg(val);
554 f71805f_write8(data, F71805F_REG_TEMP_HYST(nr), data->temp_hyst[nr]);
Jean Delvaref0819182006-01-18 23:20:53 +0100555 mutex_unlock(&data->update_lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100556
557 return count;
558}
559
Jean Delvaree53004e2006-01-09 23:26:14 +0100560static ssize_t show_alarms_in(struct device *dev, struct device_attribute
561 *devattr, char *buf)
562{
563 struct f71805f_data *data = f71805f_update_device(dev);
564
Jean Delvare2d457712006-09-24 20:52:15 +0200565 return sprintf(buf, "%lu\n", data->alarms & 0x1ff);
Jean Delvaree53004e2006-01-09 23:26:14 +0100566}
567
568static ssize_t show_alarms_fan(struct device *dev, struct device_attribute
569 *devattr, char *buf)
570{
571 struct f71805f_data *data = f71805f_update_device(dev);
572
Jean Delvare2d457712006-09-24 20:52:15 +0200573 return sprintf(buf, "%lu\n", (data->alarms >> 16) & 0x07);
Jean Delvaree53004e2006-01-09 23:26:14 +0100574}
575
576static ssize_t show_alarms_temp(struct device *dev, struct device_attribute
577 *devattr, char *buf)
578{
579 struct f71805f_data *data = f71805f_update_device(dev);
580
Jean Delvare2d457712006-09-24 20:52:15 +0200581 return sprintf(buf, "%lu\n", (data->alarms >> 11) & 0x07);
582}
583
584static ssize_t show_alarm(struct device *dev, struct device_attribute
585 *devattr, char *buf)
586{
587 struct f71805f_data *data = f71805f_update_device(dev);
588 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
589 int bitnr = attr->index;
590
591 return sprintf(buf, "%lu\n", (data->alarms >> bitnr) & 1);
Jean Delvaree53004e2006-01-09 23:26:14 +0100592}
593
Jean Delvaree53004e2006-01-09 23:26:14 +0100594static ssize_t show_name(struct device *dev, struct device_attribute
595 *devattr, char *buf)
596{
597 struct f71805f_data *data = dev_get_drvdata(dev);
598
599 return sprintf(buf, "%s\n", data->name);
600}
601
Jean Delvare0e39e012006-09-24 21:16:40 +0200602static DEVICE_ATTR(in0_input, S_IRUGO, show_in0, NULL);
603static DEVICE_ATTR(in0_max, S_IRUGO| S_IWUSR, show_in0_max, set_in0_max);
604static DEVICE_ATTR(in0_min, S_IRUGO| S_IWUSR, show_in0_min, set_in0_min);
605static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, show_in, NULL, 1);
606static SENSOR_DEVICE_ATTR(in1_max, S_IRUGO | S_IWUSR,
607 show_in_max, set_in_max, 1);
608static SENSOR_DEVICE_ATTR(in1_min, S_IRUGO | S_IWUSR,
609 show_in_min, set_in_min, 1);
610static SENSOR_DEVICE_ATTR(in2_input, S_IRUGO, show_in, NULL, 2);
611static SENSOR_DEVICE_ATTR(in2_max, S_IRUGO | S_IWUSR,
612 show_in_max, set_in_max, 2);
613static SENSOR_DEVICE_ATTR(in2_min, S_IRUGO | S_IWUSR,
614 show_in_min, set_in_min, 2);
615static SENSOR_DEVICE_ATTR(in3_input, S_IRUGO, show_in, NULL, 3);
616static SENSOR_DEVICE_ATTR(in3_max, S_IRUGO | S_IWUSR,
617 show_in_max, set_in_max, 3);
618static SENSOR_DEVICE_ATTR(in3_min, S_IRUGO | S_IWUSR,
619 show_in_min, set_in_min, 3);
620static SENSOR_DEVICE_ATTR(in4_input, S_IRUGO, show_in, NULL, 4);
621static SENSOR_DEVICE_ATTR(in4_max, S_IRUGO | S_IWUSR,
622 show_in_max, set_in_max, 4);
623static SENSOR_DEVICE_ATTR(in4_min, S_IRUGO | S_IWUSR,
624 show_in_min, set_in_min, 4);
625static SENSOR_DEVICE_ATTR(in5_input, S_IRUGO, show_in, NULL, 5);
626static SENSOR_DEVICE_ATTR(in5_max, S_IRUGO | S_IWUSR,
627 show_in_max, set_in_max, 5);
628static SENSOR_DEVICE_ATTR(in5_min, S_IRUGO | S_IWUSR,
629 show_in_min, set_in_min, 5);
630static SENSOR_DEVICE_ATTR(in6_input, S_IRUGO, show_in, NULL, 6);
631static SENSOR_DEVICE_ATTR(in6_max, S_IRUGO | S_IWUSR,
632 show_in_max, set_in_max, 6);
633static SENSOR_DEVICE_ATTR(in6_min, S_IRUGO | S_IWUSR,
634 show_in_min, set_in_min, 6);
635static SENSOR_DEVICE_ATTR(in7_input, S_IRUGO, show_in, NULL, 7);
636static SENSOR_DEVICE_ATTR(in7_max, S_IRUGO | S_IWUSR,
637 show_in_max, set_in_max, 7);
638static SENSOR_DEVICE_ATTR(in7_min, S_IRUGO | S_IWUSR,
639 show_in_min, set_in_min, 7);
640static SENSOR_DEVICE_ATTR(in8_input, S_IRUGO, show_in, NULL, 8);
641static SENSOR_DEVICE_ATTR(in8_max, S_IRUGO | S_IWUSR,
642 show_in_max, set_in_max, 8);
643static SENSOR_DEVICE_ATTR(in8_min, S_IRUGO | S_IWUSR,
644 show_in_min, set_in_min, 8);
645
646static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0);
647static SENSOR_DEVICE_ATTR(fan1_min, S_IRUGO | S_IWUSR,
648 show_fan_min, set_fan_min, 0);
649static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, show_fan, NULL, 1);
650static SENSOR_DEVICE_ATTR(fan2_min, S_IRUGO | S_IWUSR,
651 show_fan_min, set_fan_min, 1);
652static SENSOR_DEVICE_ATTR(fan3_input, S_IRUGO, show_fan, NULL, 2);
653static SENSOR_DEVICE_ATTR(fan3_min, S_IRUGO | S_IWUSR,
654 show_fan_min, set_fan_min, 2);
655
656static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
657static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO | S_IWUSR,
658 show_temp_max, set_temp_max, 0);
659static SENSOR_DEVICE_ATTR(temp1_max_hyst, S_IRUGO | S_IWUSR,
660 show_temp_hyst, set_temp_hyst, 0);
661static SENSOR_DEVICE_ATTR(temp1_type, S_IRUGO, show_temp_type, NULL, 0);
662static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1);
663static SENSOR_DEVICE_ATTR(temp2_max, S_IRUGO | S_IWUSR,
664 show_temp_max, set_temp_max, 1);
665static SENSOR_DEVICE_ATTR(temp2_max_hyst, S_IRUGO | S_IWUSR,
666 show_temp_hyst, set_temp_hyst, 1);
667static SENSOR_DEVICE_ATTR(temp2_type, S_IRUGO, show_temp_type, NULL, 1);
668static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, show_temp, NULL, 2);
669static SENSOR_DEVICE_ATTR(temp3_max, S_IRUGO | S_IWUSR,
670 show_temp_max, set_temp_max, 2);
671static SENSOR_DEVICE_ATTR(temp3_max_hyst, S_IRUGO | S_IWUSR,
672 show_temp_hyst, set_temp_hyst, 2);
673static SENSOR_DEVICE_ATTR(temp3_type, S_IRUGO, show_temp_type, NULL, 2);
674
675static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0);
676static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1);
677static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2);
678static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3);
679static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 4);
680static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 5);
681static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 6);
682static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 7);
683static SENSOR_DEVICE_ATTR(in8_alarm, S_IRUGO, show_alarm, NULL, 8);
684static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 11);
685static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 12);
686static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 13);
687static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 16);
688static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 17);
689static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 18);
690static DEVICE_ATTR(alarms_in, S_IRUGO, show_alarms_in, NULL);
691static DEVICE_ATTR(alarms_fan, S_IRUGO, show_alarms_fan, NULL);
692static DEVICE_ATTR(alarms_temp, S_IRUGO, show_alarms_temp, NULL);
693
694static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
695
696static struct attribute *f71805f_attributes[] = {
697 &dev_attr_in0_input.attr,
698 &dev_attr_in0_max.attr,
699 &dev_attr_in0_min.attr,
700 &sensor_dev_attr_in1_input.dev_attr.attr,
701 &sensor_dev_attr_in1_max.dev_attr.attr,
702 &sensor_dev_attr_in1_min.dev_attr.attr,
703 &sensor_dev_attr_in2_input.dev_attr.attr,
704 &sensor_dev_attr_in2_max.dev_attr.attr,
705 &sensor_dev_attr_in2_min.dev_attr.attr,
706 &sensor_dev_attr_in3_input.dev_attr.attr,
707 &sensor_dev_attr_in3_max.dev_attr.attr,
708 &sensor_dev_attr_in3_min.dev_attr.attr,
709 &sensor_dev_attr_in4_input.dev_attr.attr,
710 &sensor_dev_attr_in4_max.dev_attr.attr,
711 &sensor_dev_attr_in4_min.dev_attr.attr,
712 &sensor_dev_attr_in5_input.dev_attr.attr,
713 &sensor_dev_attr_in5_max.dev_attr.attr,
714 &sensor_dev_attr_in5_min.dev_attr.attr,
715 &sensor_dev_attr_in6_input.dev_attr.attr,
716 &sensor_dev_attr_in6_max.dev_attr.attr,
717 &sensor_dev_attr_in6_min.dev_attr.attr,
718 &sensor_dev_attr_in7_input.dev_attr.attr,
719 &sensor_dev_attr_in7_max.dev_attr.attr,
720 &sensor_dev_attr_in7_min.dev_attr.attr,
721 &sensor_dev_attr_in8_input.dev_attr.attr,
722 &sensor_dev_attr_in8_max.dev_attr.attr,
723 &sensor_dev_attr_in8_min.dev_attr.attr,
724
725 &sensor_dev_attr_temp1_input.dev_attr.attr,
726 &sensor_dev_attr_temp1_max.dev_attr.attr,
727 &sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
728 &sensor_dev_attr_temp1_type.dev_attr.attr,
729 &sensor_dev_attr_temp2_input.dev_attr.attr,
730 &sensor_dev_attr_temp2_max.dev_attr.attr,
731 &sensor_dev_attr_temp2_max_hyst.dev_attr.attr,
732 &sensor_dev_attr_temp2_type.dev_attr.attr,
733 &sensor_dev_attr_temp3_input.dev_attr.attr,
734 &sensor_dev_attr_temp3_max.dev_attr.attr,
735 &sensor_dev_attr_temp3_max_hyst.dev_attr.attr,
736 &sensor_dev_attr_temp3_type.dev_attr.attr,
737
738 &sensor_dev_attr_in0_alarm.dev_attr.attr,
739 &sensor_dev_attr_in1_alarm.dev_attr.attr,
740 &sensor_dev_attr_in2_alarm.dev_attr.attr,
741 &sensor_dev_attr_in3_alarm.dev_attr.attr,
742 &sensor_dev_attr_in4_alarm.dev_attr.attr,
743 &sensor_dev_attr_in5_alarm.dev_attr.attr,
744 &sensor_dev_attr_in6_alarm.dev_attr.attr,
745 &sensor_dev_attr_in7_alarm.dev_attr.attr,
746 &sensor_dev_attr_in8_alarm.dev_attr.attr,
747 &dev_attr_alarms_in.attr,
748 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
749 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
750 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
751 &dev_attr_alarms_temp.attr,
752 &dev_attr_alarms_fan.attr,
753
754 &dev_attr_name.attr,
755 NULL
Jean Delvare2488a392006-01-09 23:29:11 +0100756};
757
Jean Delvare0e39e012006-09-24 21:16:40 +0200758static const struct attribute_group f71805f_group = {
759 .attrs = f71805f_attributes,
Jean Delvare2488a392006-01-09 23:29:11 +0100760};
761
Jean Delvare0e39e012006-09-24 21:16:40 +0200762static struct attribute *f71805f_attributes_fan[3][4] = {
763 {
764 &sensor_dev_attr_fan1_input.dev_attr.attr,
765 &sensor_dev_attr_fan1_min.dev_attr.attr,
766 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
767 NULL
768 }, {
769 &sensor_dev_attr_fan2_input.dev_attr.attr,
770 &sensor_dev_attr_fan2_min.dev_attr.attr,
771 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
772 NULL
773 }, {
774 &sensor_dev_attr_fan3_input.dev_attr.attr,
775 &sensor_dev_attr_fan3_min.dev_attr.attr,
776 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
777 NULL
778 }
779};
780
781static const struct attribute_group f71805f_group_fan[3] = {
782 { .attrs = f71805f_attributes_fan[0] },
783 { .attrs = f71805f_attributes_fan[1] },
784 { .attrs = f71805f_attributes_fan[2] },
Jean Delvare2488a392006-01-09 23:29:11 +0100785};
Jean Delvaree53004e2006-01-09 23:26:14 +0100786
787/*
788 * Device registration and initialization
789 */
790
791static void __devinit f71805f_init_device(struct f71805f_data *data)
792{
793 u8 reg;
794 int i;
795
796 reg = f71805f_read8(data, F71805F_REG_START);
797 if ((reg & 0x41) != 0x01) {
798 printk(KERN_DEBUG DRVNAME ": Starting monitoring "
799 "operations\n");
800 f71805f_write8(data, F71805F_REG_START, (reg | 0x01) & ~0x40);
801 }
802
803 /* Fan monitoring can be disabled. If it is, we won't be polling
804 the register values, and won't create the related sysfs files. */
805 for (i = 0; i < 3; i++) {
Jean Delvare6b14a542006-12-12 18:18:26 +0100806 data->fan_ctrl[i] = f71805f_read8(data,
807 F71805F_REG_FAN_CTRL(i));
Jean Delvaree53004e2006-01-09 23:26:14 +0100808 }
809}
810
811static int __devinit f71805f_probe(struct platform_device *pdev)
812{
813 struct f71805f_data *data;
814 struct resource *res;
Jean Delvare2488a392006-01-09 23:29:11 +0100815 int i, err;
Jean Delvaree53004e2006-01-09 23:26:14 +0100816
817 if (!(data = kzalloc(sizeof(struct f71805f_data), GFP_KERNEL))) {
818 err = -ENOMEM;
819 printk(KERN_ERR DRVNAME ": Out of memory\n");
820 goto exit;
821 }
822
823 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
824 data->addr = res->start;
Jean Delvaref0819182006-01-18 23:20:53 +0100825 mutex_init(&data->lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100826 data->name = "f71805f";
Jean Delvaref0819182006-01-18 23:20:53 +0100827 mutex_init(&data->update_lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100828
829 platform_set_drvdata(pdev, data);
830
Jean Delvaree53004e2006-01-09 23:26:14 +0100831 /* Initialize the F71805F chip */
832 f71805f_init_device(data);
833
834 /* Register sysfs interface files */
Jean Delvare0e39e012006-09-24 21:16:40 +0200835 if ((err = sysfs_create_group(&pdev->dev.kobj, &f71805f_group)))
836 goto exit_free;
837 for (i = 0; i < 3; i++) {
Jean Delvare6b14a542006-12-12 18:18:26 +0100838 if (data->fan_ctrl[i] & FAN_CTRL_SKIP)
Jean Delvare2488a392006-01-09 23:29:11 +0100839 continue;
Jean Delvare0e39e012006-09-24 21:16:40 +0200840 if ((err = sysfs_create_group(&pdev->dev.kobj,
841 &f71805f_group_fan[i])))
842 goto exit_remove_files;
843 }
844
845 data->class_dev = hwmon_device_register(&pdev->dev);
846 if (IS_ERR(data->class_dev)) {
847 err = PTR_ERR(data->class_dev);
848 dev_err(&pdev->dev, "Class registration failed (%d)\n", err);
849 goto exit_remove_files;
Jean Delvaree53004e2006-01-09 23:26:14 +0100850 }
Jean Delvaree53004e2006-01-09 23:26:14 +0100851
852 return 0;
853
Jean Delvare0e39e012006-09-24 21:16:40 +0200854exit_remove_files:
855 sysfs_remove_group(&pdev->dev.kobj, &f71805f_group);
856 for (i = 0; i < 3; i++)
857 sysfs_remove_group(&pdev->dev.kobj, &f71805f_group_fan[i]);
Jean Delvaree53004e2006-01-09 23:26:14 +0100858exit_free:
Jean Delvare0e39e012006-09-24 21:16:40 +0200859 platform_set_drvdata(pdev, NULL);
Jean Delvaree53004e2006-01-09 23:26:14 +0100860 kfree(data);
861exit:
862 return err;
863}
864
865static int __devexit f71805f_remove(struct platform_device *pdev)
866{
867 struct f71805f_data *data = platform_get_drvdata(pdev);
Jean Delvare0e39e012006-09-24 21:16:40 +0200868 int i;
Jean Delvaree53004e2006-01-09 23:26:14 +0100869
870 platform_set_drvdata(pdev, NULL);
871 hwmon_device_unregister(data->class_dev);
Jean Delvare0e39e012006-09-24 21:16:40 +0200872 sysfs_remove_group(&pdev->dev.kobj, &f71805f_group);
873 for (i = 0; i < 3; i++)
874 sysfs_remove_group(&pdev->dev.kobj, &f71805f_group_fan[i]);
Jean Delvaree53004e2006-01-09 23:26:14 +0100875 kfree(data);
876
877 return 0;
878}
879
880static struct platform_driver f71805f_driver = {
881 .driver = {
882 .owner = THIS_MODULE,
883 .name = DRVNAME,
884 },
885 .probe = f71805f_probe,
886 .remove = __devexit_p(f71805f_remove),
887};
888
889static int __init f71805f_device_add(unsigned short address)
890{
Jean Delvare568825c2006-03-23 16:40:23 +0100891 struct resource res = {
892 .start = address,
893 .end = address + REGION_LENGTH - 1,
894 .flags = IORESOURCE_IO,
895 };
Jean Delvaree53004e2006-01-09 23:26:14 +0100896 int err;
897
898 pdev = platform_device_alloc(DRVNAME, address);
899 if (!pdev) {
900 err = -ENOMEM;
901 printk(KERN_ERR DRVNAME ": Device allocation failed\n");
902 goto exit;
903 }
904
Jean Delvare568825c2006-03-23 16:40:23 +0100905 res.name = pdev->name;
906 err = platform_device_add_resources(pdev, &res, 1);
Jean Delvaree53004e2006-01-09 23:26:14 +0100907 if (err) {
908 printk(KERN_ERR DRVNAME ": Device resource addition failed "
909 "(%d)\n", err);
910 goto exit_device_put;
911 }
912
913 err = platform_device_add(pdev);
914 if (err) {
915 printk(KERN_ERR DRVNAME ": Device addition failed (%d)\n",
916 err);
917 goto exit_device_put;
918 }
919
920 return 0;
921
922exit_device_put:
923 platform_device_put(pdev);
924exit:
925 return err;
926}
927
928static int __init f71805f_find(int sioaddr, unsigned short *address)
929{
930 int err = -ENODEV;
931 u16 devid;
932
933 superio_enter(sioaddr);
934
935 devid = superio_inw(sioaddr, SIO_REG_MANID);
936 if (devid != SIO_FINTEK_ID)
937 goto exit;
938
939 devid = superio_inw(sioaddr, SIO_REG_DEVID);
940 if (devid != SIO_F71805F_ID) {
941 printk(KERN_INFO DRVNAME ": Unsupported Fintek device, "
942 "skipping\n");
943 goto exit;
944 }
945
946 superio_select(sioaddr, F71805F_LD_HWM);
947 if (!(superio_inb(sioaddr, SIO_REG_ENABLE) & 0x01)) {
948 printk(KERN_WARNING DRVNAME ": Device not activated, "
949 "skipping\n");
950 goto exit;
951 }
952
953 *address = superio_inw(sioaddr, SIO_REG_ADDR);
954 if (*address == 0) {
955 printk(KERN_WARNING DRVNAME ": Base address not set, "
956 "skipping\n");
957 goto exit;
958 }
959
960 err = 0;
961 printk(KERN_INFO DRVNAME ": Found F71805F chip at %#x, revision %u\n",
962 *address, superio_inb(sioaddr, SIO_REG_DEVREV));
963
964exit:
965 superio_exit(sioaddr);
966 return err;
967}
968
969static int __init f71805f_init(void)
970{
971 int err;
972 unsigned short address;
973
974 if (f71805f_find(0x2e, &address)
975 && f71805f_find(0x4e, &address))
976 return -ENODEV;
977
978 err = platform_driver_register(&f71805f_driver);
979 if (err)
980 goto exit;
981
982 /* Sets global pdev as a side effect */
983 err = f71805f_device_add(address);
984 if (err)
985 goto exit_driver;
986
987 return 0;
988
989exit_driver:
990 platform_driver_unregister(&f71805f_driver);
991exit:
992 return err;
993}
994
995static void __exit f71805f_exit(void)
996{
997 platform_device_unregister(pdev);
998 platform_driver_unregister(&f71805f_driver);
999}
1000
1001MODULE_AUTHOR("Jean Delvare <khali@linux-fr>");
1002MODULE_LICENSE("GPL");
1003MODULE_DESCRIPTION("F71805F hardware monitoring driver");
1004
1005module_init(f71805f_init);
1006module_exit(f71805f_exit);