blob: ba5e7b70f8cc0f8aa6e2137e1ecac240c9fdd30b [file] [log] [blame]
Jean Delvaree53004e2006-01-09 23:26:14 +01001/*
Jean Delvare51c997d2006-12-12 18:18:29 +01002 * f71805f.c - driver for the Fintek F71805F/FG and F71872F/FG Super-I/O
3 * chips integrated 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 *
Jean Delvare51c997d2006-12-12 18:18:29 +010010 * The F71872F/FG is almost the same, with two more voltages monitored,
11 * and 6 VID inputs.
12 *
Jean Delvaree53004e2006-01-09 23:26:14 +010013 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 */
27
28#include <linux/module.h>
29#include <linux/init.h>
30#include <linux/slab.h>
31#include <linux/jiffies.h>
32#include <linux/platform_device.h>
33#include <linux/hwmon.h>
34#include <linux/hwmon-sysfs.h>
35#include <linux/err.h>
Jean Delvaref0819182006-01-18 23:20:53 +010036#include <linux/mutex.h>
Jean Delvare0e39e012006-09-24 21:16:40 +020037#include <linux/sysfs.h>
Jean Delvaree53004e2006-01-09 23:26:14 +010038#include <asm/io.h>
39
40static struct platform_device *pdev;
41
42#define DRVNAME "f71805f"
Jean Delvare51c997d2006-12-12 18:18:29 +010043enum kinds { f71805f, f71872f };
Jean Delvaree53004e2006-01-09 23:26:14 +010044
45/*
46 * Super-I/O constants and functions
47 */
48
49#define F71805F_LD_HWM 0x04
50
51#define SIO_REG_LDSEL 0x07 /* Logical device select */
52#define SIO_REG_DEVID 0x20 /* Device ID (2 bytes) */
53#define SIO_REG_DEVREV 0x22 /* Device revision */
54#define SIO_REG_MANID 0x23 /* Fintek ID (2 bytes) */
Jean Delvare51c997d2006-12-12 18:18:29 +010055#define SIO_REG_FNSEL1 0x29 /* Multi Function Select 1 (F71872F) */
Jean Delvaree53004e2006-01-09 23:26:14 +010056#define SIO_REG_ENABLE 0x30 /* Logical device enable */
57#define SIO_REG_ADDR 0x60 /* Logical device address (2 bytes) */
58
59#define SIO_FINTEK_ID 0x1934
60#define SIO_F71805F_ID 0x0406
Jean Delvare51c997d2006-12-12 18:18:29 +010061#define SIO_F71872F_ID 0x0341
Jean Delvaree53004e2006-01-09 23:26:14 +010062
63static inline int
64superio_inb(int base, int reg)
65{
66 outb(reg, base);
67 return inb(base + 1);
68}
69
70static int
71superio_inw(int base, int reg)
72{
73 int val;
74 outb(reg++, base);
75 val = inb(base + 1) << 8;
76 outb(reg, base);
77 val |= inb(base + 1);
78 return val;
79}
80
81static inline void
82superio_select(int base, int ld)
83{
84 outb(SIO_REG_LDSEL, base);
85 outb(ld, base + 1);
86}
87
88static inline void
89superio_enter(int base)
90{
91 outb(0x87, base);
92 outb(0x87, base);
93}
94
95static inline void
96superio_exit(int base)
97{
98 outb(0xaa, base);
99}
100
101/*
102 * ISA constants
103 */
104
105#define REGION_LENGTH 2
106#define ADDR_REG_OFFSET 0
107#define DATA_REG_OFFSET 1
108
Jean Delvaree53004e2006-01-09 23:26:14 +0100109/*
110 * Registers
111 */
112
Jean Delvare51c997d2006-12-12 18:18:29 +0100113/* in nr from 0 to 10 (8-bit values) */
Jean Delvaree53004e2006-01-09 23:26:14 +0100114#define F71805F_REG_IN(nr) (0x10 + (nr))
Jean Delvare51c997d2006-12-12 18:18:29 +0100115#define F71805F_REG_IN_HIGH(nr) ((nr) < 10 ? 0x40 + 2 * (nr) : 0x2E)
116#define F71805F_REG_IN_LOW(nr) ((nr) < 10 ? 0x41 + 2 * (nr) : 0x2F)
Jean Delvaree53004e2006-01-09 23:26:14 +0100117/* fan nr from 0 to 2 (12-bit values, two registers) */
118#define F71805F_REG_FAN(nr) (0x20 + 2 * (nr))
119#define F71805F_REG_FAN_LOW(nr) (0x28 + 2 * (nr))
Jean Delvare315c7112006-12-12 18:18:27 +0100120#define F71805F_REG_FAN_TARGET(nr) (0x69 + 16 * (nr))
Jean Delvaree53004e2006-01-09 23:26:14 +0100121#define F71805F_REG_FAN_CTRL(nr) (0x60 + 16 * (nr))
Jean Delvare6e2bc172006-12-12 18:18:27 +0100122#define F71805F_REG_PWM_FREQ(nr) (0x63 + 16 * (nr))
Jean Delvare95e35312006-12-12 18:18:26 +0100123#define F71805F_REG_PWM_DUTY(nr) (0x6B + 16 * (nr))
Jean Delvaree53004e2006-01-09 23:26:14 +0100124/* temp nr from 0 to 2 (8-bit values) */
125#define F71805F_REG_TEMP(nr) (0x1B + (nr))
126#define F71805F_REG_TEMP_HIGH(nr) (0x54 + 2 * (nr))
127#define F71805F_REG_TEMP_HYST(nr) (0x55 + 2 * (nr))
128#define F71805F_REG_TEMP_MODE 0x01
129
130#define F71805F_REG_START 0x00
131/* status nr from 0 to 2 */
132#define F71805F_REG_STATUS(nr) (0x36 + (nr))
133
Jean Delvare6b14a542006-12-12 18:18:26 +0100134/* individual register bits */
135#define FAN_CTRL_SKIP 0x80
Jean Delvaree1967832006-12-12 18:18:27 +0100136#define FAN_CTRL_DC_MODE 0x10
Jean Delvare315c7112006-12-12 18:18:27 +0100137#define FAN_CTRL_LATCH_FULL 0x08
Jean Delvare95e35312006-12-12 18:18:26 +0100138#define FAN_CTRL_MODE_MASK 0x03
139#define FAN_CTRL_MODE_SPEED 0x00
140#define FAN_CTRL_MODE_TEMPERATURE 0x01
141#define FAN_CTRL_MODE_MANUAL 0x02
Jean Delvare6b14a542006-12-12 18:18:26 +0100142
Jean Delvaree53004e2006-01-09 23:26:14 +0100143/*
144 * Data structures and manipulation thereof
145 */
146
147struct f71805f_data {
148 unsigned short addr;
149 const char *name;
Jean Delvaref0819182006-01-18 23:20:53 +0100150 struct mutex lock;
Jean Delvaree53004e2006-01-09 23:26:14 +0100151 struct class_device *class_dev;
152
Jean Delvaref0819182006-01-18 23:20:53 +0100153 struct mutex update_lock;
Jean Delvaree53004e2006-01-09 23:26:14 +0100154 char valid; /* !=0 if following fields are valid */
155 unsigned long last_updated; /* In jiffies */
156 unsigned long last_limits; /* In jiffies */
157
158 /* Register values */
Jean Delvare51c997d2006-12-12 18:18:29 +0100159 u8 in[11];
160 u8 in_high[11];
161 u8 in_low[11];
162 u16 has_in;
Jean Delvaree53004e2006-01-09 23:26:14 +0100163 u16 fan[3];
164 u16 fan_low[3];
Jean Delvare315c7112006-12-12 18:18:27 +0100165 u16 fan_target[3];
Jean Delvare6b14a542006-12-12 18:18:26 +0100166 u8 fan_ctrl[3];
Jean Delvare95e35312006-12-12 18:18:26 +0100167 u8 pwm[3];
Jean Delvare6e2bc172006-12-12 18:18:27 +0100168 u8 pwm_freq[3];
Jean Delvaree53004e2006-01-09 23:26:14 +0100169 u8 temp[3];
170 u8 temp_high[3];
171 u8 temp_hyst[3];
172 u8 temp_mode;
Jean Delvare2d457712006-09-24 20:52:15 +0200173 unsigned long alarms;
Jean Delvaree53004e2006-01-09 23:26:14 +0100174};
175
Jean Delvare51c997d2006-12-12 18:18:29 +0100176struct f71805f_sio_data {
177 enum kinds kind;
178 u8 fnsel1;
179};
180
Jean Delvaree53004e2006-01-09 23:26:14 +0100181static inline long in_from_reg(u8 reg)
182{
183 return (reg * 8);
184}
185
186/* The 2 least significant bits are not used */
187static inline u8 in_to_reg(long val)
188{
189 if (val <= 0)
190 return 0;
191 if (val >= 2016)
192 return 0xfc;
193 return (((val + 16) / 32) << 2);
194}
195
196/* in0 is downscaled by a factor 2 internally */
197static inline long in0_from_reg(u8 reg)
198{
199 return (reg * 16);
200}
201
202static inline u8 in0_to_reg(long val)
203{
204 if (val <= 0)
205 return 0;
206 if (val >= 4032)
207 return 0xfc;
208 return (((val + 32) / 64) << 2);
209}
210
211/* The 4 most significant bits are not used */
212static inline long fan_from_reg(u16 reg)
213{
214 reg &= 0xfff;
215 if (!reg || reg == 0xfff)
216 return 0;
217 return (1500000 / reg);
218}
219
220static inline u16 fan_to_reg(long rpm)
221{
222 /* If the low limit is set below what the chip can measure,
223 store the largest possible 12-bit value in the registers,
224 so that no alarm will ever trigger. */
225 if (rpm < 367)
226 return 0xfff;
227 return (1500000 / rpm);
228}
229
Jean Delvare6e2bc172006-12-12 18:18:27 +0100230static inline unsigned long pwm_freq_from_reg(u8 reg)
231{
232 unsigned long clock = (reg & 0x80) ? 48000000UL : 1000000UL;
233
234 reg &= 0x7f;
235 if (reg == 0)
236 reg++;
237 return clock / (reg << 8);
238}
239
240static inline u8 pwm_freq_to_reg(unsigned long val)
241{
242 if (val >= 187500) /* The highest we can do */
243 return 0x80;
244 if (val >= 1475) /* Use 48 MHz clock */
245 return 0x80 | (48000000UL / (val << 8));
246 if (val < 31) /* The lowest we can do */
247 return 0x7f;
248 else /* Use 1 MHz clock */
249 return 1000000UL / (val << 8);
250}
251
Jean Delvaree1967832006-12-12 18:18:27 +0100252static inline int pwm_mode_from_reg(u8 reg)
253{
254 return !(reg & FAN_CTRL_DC_MODE);
255}
256
Jean Delvaree53004e2006-01-09 23:26:14 +0100257static inline long temp_from_reg(u8 reg)
258{
259 return (reg * 1000);
260}
261
262static inline u8 temp_to_reg(long val)
263{
264 if (val < 0)
265 val = 0;
266 else if (val > 1000 * 0xff)
267 val = 0xff;
268 return ((val + 500) / 1000);
269}
270
271/*
272 * Device I/O access
273 */
274
275static u8 f71805f_read8(struct f71805f_data *data, u8 reg)
276{
277 u8 val;
278
Jean Delvaref0819182006-01-18 23:20:53 +0100279 mutex_lock(&data->lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100280 outb(reg, data->addr + ADDR_REG_OFFSET);
281 val = inb(data->addr + DATA_REG_OFFSET);
Jean Delvaref0819182006-01-18 23:20:53 +0100282 mutex_unlock(&data->lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100283
284 return val;
285}
286
287static void f71805f_write8(struct f71805f_data *data, u8 reg, u8 val)
288{
Jean Delvaref0819182006-01-18 23:20:53 +0100289 mutex_lock(&data->lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100290 outb(reg, data->addr + ADDR_REG_OFFSET);
291 outb(val, data->addr + DATA_REG_OFFSET);
Jean Delvaref0819182006-01-18 23:20:53 +0100292 mutex_unlock(&data->lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100293}
294
295/* It is important to read the MSB first, because doing so latches the
296 value of the LSB, so we are sure both bytes belong to the same value. */
297static u16 f71805f_read16(struct f71805f_data *data, u8 reg)
298{
299 u16 val;
300
Jean Delvaref0819182006-01-18 23:20:53 +0100301 mutex_lock(&data->lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100302 outb(reg, data->addr + ADDR_REG_OFFSET);
303 val = inb(data->addr + DATA_REG_OFFSET) << 8;
304 outb(++reg, data->addr + ADDR_REG_OFFSET);
305 val |= inb(data->addr + DATA_REG_OFFSET);
Jean Delvaref0819182006-01-18 23:20:53 +0100306 mutex_unlock(&data->lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100307
308 return val;
309}
310
311static void f71805f_write16(struct f71805f_data *data, u8 reg, u16 val)
312{
Jean Delvaref0819182006-01-18 23:20:53 +0100313 mutex_lock(&data->lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100314 outb(reg, data->addr + ADDR_REG_OFFSET);
315 outb(val >> 8, data->addr + DATA_REG_OFFSET);
316 outb(++reg, data->addr + ADDR_REG_OFFSET);
317 outb(val & 0xff, data->addr + DATA_REG_OFFSET);
Jean Delvaref0819182006-01-18 23:20:53 +0100318 mutex_unlock(&data->lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100319}
320
321static struct f71805f_data *f71805f_update_device(struct device *dev)
322{
323 struct f71805f_data *data = dev_get_drvdata(dev);
324 int nr;
325
Jean Delvaref0819182006-01-18 23:20:53 +0100326 mutex_lock(&data->update_lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100327
328 /* Limit registers cache is refreshed after 60 seconds */
329 if (time_after(jiffies, data->last_updated + 60 * HZ)
330 || !data->valid) {
Jean Delvare51c997d2006-12-12 18:18:29 +0100331 for (nr = 0; nr < 11; nr++) {
332 if (!(data->has_in & (1 << nr)))
333 continue;
Jean Delvaree53004e2006-01-09 23:26:14 +0100334 data->in_high[nr] = f71805f_read8(data,
335 F71805F_REG_IN_HIGH(nr));
336 data->in_low[nr] = f71805f_read8(data,
337 F71805F_REG_IN_LOW(nr));
338 }
339 for (nr = 0; nr < 3; nr++) {
Jean Delvare6b14a542006-12-12 18:18:26 +0100340 if (data->fan_ctrl[nr] & FAN_CTRL_SKIP)
341 continue;
342 data->fan_low[nr] = f71805f_read16(data,
343 F71805F_REG_FAN_LOW(nr));
Jean Delvare315c7112006-12-12 18:18:27 +0100344 data->fan_target[nr] = f71805f_read16(data,
345 F71805F_REG_FAN_TARGET(nr));
Jean Delvare6e2bc172006-12-12 18:18:27 +0100346 data->pwm_freq[nr] = f71805f_read8(data,
347 F71805F_REG_PWM_FREQ(nr));
Jean Delvaree53004e2006-01-09 23:26:14 +0100348 }
349 for (nr = 0; nr < 3; nr++) {
350 data->temp_high[nr] = f71805f_read8(data,
351 F71805F_REG_TEMP_HIGH(nr));
352 data->temp_hyst[nr] = f71805f_read8(data,
353 F71805F_REG_TEMP_HYST(nr));
354 }
355 data->temp_mode = f71805f_read8(data, F71805F_REG_TEMP_MODE);
356
357 data->last_limits = jiffies;
358 }
359
360 /* Measurement registers cache is refreshed after 1 second */
361 if (time_after(jiffies, data->last_updated + HZ)
362 || !data->valid) {
Jean Delvare51c997d2006-12-12 18:18:29 +0100363 for (nr = 0; nr < 11; nr++) {
364 if (!(data->has_in & (1 << nr)))
365 continue;
Jean Delvaree53004e2006-01-09 23:26:14 +0100366 data->in[nr] = f71805f_read8(data,
367 F71805F_REG_IN(nr));
368 }
369 for (nr = 0; nr < 3; nr++) {
Jean Delvare6b14a542006-12-12 18:18:26 +0100370 if (data->fan_ctrl[nr] & FAN_CTRL_SKIP)
371 continue;
372 data->fan[nr] = f71805f_read16(data,
373 F71805F_REG_FAN(nr));
Jean Delvare95e35312006-12-12 18:18:26 +0100374 data->fan_ctrl[nr] = f71805f_read8(data,
375 F71805F_REG_FAN_CTRL(nr));
376 data->pwm[nr] = f71805f_read8(data,
377 F71805F_REG_PWM_DUTY(nr));
Jean Delvaree53004e2006-01-09 23:26:14 +0100378 }
379 for (nr = 0; nr < 3; nr++) {
380 data->temp[nr] = f71805f_read8(data,
381 F71805F_REG_TEMP(nr));
382 }
Jean Delvare2d457712006-09-24 20:52:15 +0200383 data->alarms = f71805f_read8(data, F71805F_REG_STATUS(0))
384 + (f71805f_read8(data, F71805F_REG_STATUS(1)) << 8)
385 + (f71805f_read8(data, F71805F_REG_STATUS(2)) << 16);
Jean Delvaree53004e2006-01-09 23:26:14 +0100386
387 data->last_updated = jiffies;
388 data->valid = 1;
389 }
390
Jean Delvaref0819182006-01-18 23:20:53 +0100391 mutex_unlock(&data->update_lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100392
393 return data;
394}
395
396/*
397 * Sysfs interface
398 */
399
400static ssize_t show_in0(struct device *dev, struct device_attribute *devattr,
401 char *buf)
402{
403 struct f71805f_data *data = f71805f_update_device(dev);
Jean Delvare51c997d2006-12-12 18:18:29 +0100404 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
405 int nr = attr->index;
Jean Delvaree53004e2006-01-09 23:26:14 +0100406
Jean Delvare51c997d2006-12-12 18:18:29 +0100407 return sprintf(buf, "%ld\n", in0_from_reg(data->in[nr]));
Jean Delvaree53004e2006-01-09 23:26:14 +0100408}
409
410static ssize_t show_in0_max(struct device *dev, struct device_attribute
411 *devattr, char *buf)
412{
413 struct f71805f_data *data = f71805f_update_device(dev);
Jean Delvare51c997d2006-12-12 18:18:29 +0100414 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
415 int nr = attr->index;
Jean Delvaree53004e2006-01-09 23:26:14 +0100416
Jean Delvare51c997d2006-12-12 18:18:29 +0100417 return sprintf(buf, "%ld\n", in0_from_reg(data->in_high[nr]));
Jean Delvaree53004e2006-01-09 23:26:14 +0100418}
419
420static ssize_t show_in0_min(struct device *dev, struct device_attribute
421 *devattr, char *buf)
422{
423 struct f71805f_data *data = f71805f_update_device(dev);
Jean Delvare51c997d2006-12-12 18:18:29 +0100424 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
425 int nr = attr->index;
Jean Delvaree53004e2006-01-09 23:26:14 +0100426
Jean Delvare51c997d2006-12-12 18:18:29 +0100427 return sprintf(buf, "%ld\n", in0_from_reg(data->in_low[nr]));
Jean Delvaree53004e2006-01-09 23:26:14 +0100428}
429
430static ssize_t set_in0_max(struct device *dev, struct device_attribute
431 *devattr, const char *buf, size_t count)
432{
433 struct f71805f_data *data = dev_get_drvdata(dev);
Jean Delvare51c997d2006-12-12 18:18:29 +0100434 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
435 int nr = attr->index;
Jean Delvaree53004e2006-01-09 23:26:14 +0100436 long val = simple_strtol(buf, NULL, 10);
437
Jean Delvaref0819182006-01-18 23:20:53 +0100438 mutex_lock(&data->update_lock);
Jean Delvare51c997d2006-12-12 18:18:29 +0100439 data->in_high[nr] = in0_to_reg(val);
440 f71805f_write8(data, F71805F_REG_IN_HIGH(nr), data->in_high[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
446static ssize_t set_in0_min(struct device *dev, struct device_attribute
447 *devattr, const char *buf, size_t count)
448{
449 struct f71805f_data *data = dev_get_drvdata(dev);
Jean Delvare51c997d2006-12-12 18:18:29 +0100450 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
451 int nr = attr->index;
Jean Delvaree53004e2006-01-09 23:26:14 +0100452 long val = simple_strtol(buf, NULL, 10);
453
Jean Delvaref0819182006-01-18 23:20:53 +0100454 mutex_lock(&data->update_lock);
Jean Delvare51c997d2006-12-12 18:18:29 +0100455 data->in_low[nr] = in0_to_reg(val);
456 f71805f_write8(data, F71805F_REG_IN_LOW(nr), data->in_low[nr]);
Jean Delvaref0819182006-01-18 23:20:53 +0100457 mutex_unlock(&data->update_lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100458
459 return count;
460}
461
Jean Delvaree53004e2006-01-09 23:26:14 +0100462static ssize_t show_in(struct device *dev, struct device_attribute *devattr,
463 char *buf)
464{
465 struct f71805f_data *data = f71805f_update_device(dev);
466 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
467 int nr = attr->index;
468
469 return sprintf(buf, "%ld\n", in_from_reg(data->in[nr]));
470}
471
472static ssize_t show_in_max(struct device *dev, struct device_attribute
473 *devattr, char *buf)
474{
475 struct f71805f_data *data = f71805f_update_device(dev);
476 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
477 int nr = attr->index;
478
479 return sprintf(buf, "%ld\n", in_from_reg(data->in_high[nr]));
480}
481
482static ssize_t show_in_min(struct device *dev, struct device_attribute
483 *devattr, 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", in_from_reg(data->in_low[nr]));
490}
491
492static ssize_t set_in_max(struct device *dev, struct device_attribute
493 *devattr, const char *buf, size_t count)
494{
495 struct f71805f_data *data = dev_get_drvdata(dev);
496 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
497 int nr = attr->index;
498 long val = simple_strtol(buf, NULL, 10);
499
Jean Delvaref0819182006-01-18 23:20:53 +0100500 mutex_lock(&data->update_lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100501 data->in_high[nr] = in_to_reg(val);
502 f71805f_write8(data, F71805F_REG_IN_HIGH(nr), data->in_high[nr]);
Jean Delvaref0819182006-01-18 23:20:53 +0100503 mutex_unlock(&data->update_lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100504
505 return count;
506}
507
508static ssize_t set_in_min(struct device *dev, struct device_attribute
509 *devattr, const char *buf, size_t count)
510{
511 struct f71805f_data *data = dev_get_drvdata(dev);
512 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
513 int nr = attr->index;
514 long val = simple_strtol(buf, NULL, 10);
515
Jean Delvaref0819182006-01-18 23:20:53 +0100516 mutex_lock(&data->update_lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100517 data->in_low[nr] = in_to_reg(val);
518 f71805f_write8(data, F71805F_REG_IN_LOW(nr), data->in_low[nr]);
Jean Delvaref0819182006-01-18 23:20:53 +0100519 mutex_unlock(&data->update_lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100520
521 return count;
522}
523
Jean Delvaree53004e2006-01-09 23:26:14 +0100524static ssize_t show_fan(struct device *dev, struct device_attribute *devattr,
525 char *buf)
526{
527 struct f71805f_data *data = f71805f_update_device(dev);
528 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
529 int nr = attr->index;
530
531 return sprintf(buf, "%ld\n", fan_from_reg(data->fan[nr]));
532}
533
534static ssize_t show_fan_min(struct device *dev, struct device_attribute
535 *devattr, char *buf)
536{
537 struct f71805f_data *data = f71805f_update_device(dev);
538 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
539 int nr = attr->index;
540
541 return sprintf(buf, "%ld\n", fan_from_reg(data->fan_low[nr]));
542}
543
Jean Delvare315c7112006-12-12 18:18:27 +0100544static ssize_t show_fan_target(struct device *dev, struct device_attribute
545 *devattr, char *buf)
546{
547 struct f71805f_data *data = f71805f_update_device(dev);
548 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
549 int nr = attr->index;
550
551 return sprintf(buf, "%ld\n", fan_from_reg(data->fan_target[nr]));
552}
553
Jean Delvaree53004e2006-01-09 23:26:14 +0100554static ssize_t set_fan_min(struct device *dev, struct device_attribute
555 *devattr, const char *buf, size_t count)
556{
557 struct f71805f_data *data = dev_get_drvdata(dev);
558 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
559 int nr = attr->index;
560 long val = simple_strtol(buf, NULL, 10);
561
Jean Delvaref0819182006-01-18 23:20:53 +0100562 mutex_lock(&data->update_lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100563 data->fan_low[nr] = fan_to_reg(val);
564 f71805f_write16(data, F71805F_REG_FAN_LOW(nr), data->fan_low[nr]);
Jean Delvaref0819182006-01-18 23:20:53 +0100565 mutex_unlock(&data->update_lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100566
567 return count;
568}
569
Jean Delvare315c7112006-12-12 18:18:27 +0100570static ssize_t set_fan_target(struct device *dev, struct device_attribute
571 *devattr, const char *buf, size_t count)
572{
573 struct f71805f_data *data = dev_get_drvdata(dev);
574 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
575 int nr = attr->index;
576 long val = simple_strtol(buf, NULL, 10);
577
578 mutex_lock(&data->update_lock);
579 data->fan_target[nr] = fan_to_reg(val);
580 f71805f_write16(data, F71805F_REG_FAN_TARGET(nr),
581 data->fan_target[nr]);
582 mutex_unlock(&data->update_lock);
583
584 return count;
585}
586
Jean Delvare95e35312006-12-12 18:18:26 +0100587static ssize_t show_pwm(struct device *dev, struct device_attribute *devattr,
588 char *buf)
589{
590 struct f71805f_data *data = f71805f_update_device(dev);
591 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
592 int nr = attr->index;
593
594 return sprintf(buf, "%d\n", (int)data->pwm[nr]);
595}
596
597static ssize_t show_pwm_enable(struct device *dev, struct device_attribute
598 *devattr, char *buf)
599{
600 struct f71805f_data *data = f71805f_update_device(dev);
601 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
602 int nr = attr->index;
603 int mode;
604
605 switch (data->fan_ctrl[nr] & FAN_CTRL_MODE_MASK) {
606 case FAN_CTRL_MODE_SPEED:
607 mode = 3;
608 break;
609 case FAN_CTRL_MODE_TEMPERATURE:
610 mode = 2;
611 break;
612 default: /* MANUAL */
613 mode = 1;
614 }
615
616 return sprintf(buf, "%d\n", mode);
617}
618
Jean Delvare6e2bc172006-12-12 18:18:27 +0100619static ssize_t show_pwm_freq(struct device *dev, struct device_attribute
620 *devattr, char *buf)
621{
622 struct f71805f_data *data = f71805f_update_device(dev);
623 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
624 int nr = attr->index;
625
626 return sprintf(buf, "%lu\n", pwm_freq_from_reg(data->pwm_freq[nr]));
627}
628
Jean Delvaree1967832006-12-12 18:18:27 +0100629static ssize_t show_pwm_mode(struct device *dev, struct device_attribute
630 *devattr, char *buf)
631{
632 struct f71805f_data *data = f71805f_update_device(dev);
633 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
634 int nr = attr->index;
635
636 return sprintf(buf, "%d\n", pwm_mode_from_reg(data->fan_ctrl[nr]));
637}
638
Jean Delvare95e35312006-12-12 18:18:26 +0100639static ssize_t set_pwm(struct device *dev, struct device_attribute *devattr,
640 const char *buf, size_t count)
641{
642 struct f71805f_data *data = dev_get_drvdata(dev);
643 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
644 int nr = attr->index;
645 unsigned long val = simple_strtoul(buf, NULL, 10);
646
647 if (val > 255)
648 return -EINVAL;
649
650 mutex_lock(&data->update_lock);
651 data->pwm[nr] = val;
652 f71805f_write8(data, F71805F_REG_PWM_DUTY(nr), data->pwm[nr]);
653 mutex_unlock(&data->update_lock);
654
655 return count;
656}
657
658static struct attribute *f71805f_attr_pwm[];
659
660static ssize_t set_pwm_enable(struct device *dev, struct device_attribute
661 *devattr, const char *buf, size_t count)
662{
663 struct f71805f_data *data = dev_get_drvdata(dev);
664 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
665 int nr = attr->index;
666 unsigned long val = simple_strtoul(buf, NULL, 10);
667 u8 reg;
668
669 if (val < 1 || val > 3)
670 return -EINVAL;
671
672 if (val > 1) { /* Automatic mode, user can't set PWM value */
673 if (sysfs_chmod_file(&dev->kobj, f71805f_attr_pwm[nr],
674 S_IRUGO))
675 dev_dbg(dev, "chmod -w pwm%d failed\n", nr + 1);
676 }
677
678 mutex_lock(&data->update_lock);
679 reg = f71805f_read8(data, F71805F_REG_FAN_CTRL(nr))
680 & ~FAN_CTRL_MODE_MASK;
681 switch (val) {
682 case 1:
683 reg |= FAN_CTRL_MODE_MANUAL;
684 break;
685 case 2:
686 reg |= FAN_CTRL_MODE_TEMPERATURE;
687 break;
688 case 3:
689 reg |= FAN_CTRL_MODE_SPEED;
690 break;
691 }
692 data->fan_ctrl[nr] = reg;
693 f71805f_write8(data, F71805F_REG_FAN_CTRL(nr), reg);
694 mutex_unlock(&data->update_lock);
695
696 if (val == 1) { /* Manual mode, user can set PWM value */
697 if (sysfs_chmod_file(&dev->kobj, f71805f_attr_pwm[nr],
698 S_IRUGO | S_IWUSR))
699 dev_dbg(dev, "chmod +w pwm%d failed\n", nr + 1);
700 }
701
702 return count;
703}
704
Jean Delvare6e2bc172006-12-12 18:18:27 +0100705static ssize_t set_pwm_freq(struct device *dev, struct device_attribute
706 *devattr, const char *buf, size_t count)
707{
708 struct f71805f_data *data = dev_get_drvdata(dev);
709 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
710 int nr = attr->index;
711 unsigned long val = simple_strtoul(buf, NULL, 10);
712
713 mutex_lock(&data->update_lock);
714 data->pwm_freq[nr] = pwm_freq_to_reg(val);
715 f71805f_write8(data, F71805F_REG_PWM_FREQ(nr), data->pwm_freq[nr]);
716 mutex_unlock(&data->update_lock);
717
718 return count;
719}
720
Jean Delvaree53004e2006-01-09 23:26:14 +0100721static ssize_t show_temp(struct device *dev, struct device_attribute *devattr,
722 char *buf)
723{
724 struct f71805f_data *data = f71805f_update_device(dev);
725 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
726 int nr = attr->index;
727
728 return sprintf(buf, "%ld\n", temp_from_reg(data->temp[nr]));
729}
730
731static ssize_t show_temp_max(struct device *dev, struct device_attribute
732 *devattr, char *buf)
733{
734 struct f71805f_data *data = f71805f_update_device(dev);
735 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
736 int nr = attr->index;
737
738 return sprintf(buf, "%ld\n", temp_from_reg(data->temp_high[nr]));
739}
740
741static ssize_t show_temp_hyst(struct device *dev, struct device_attribute
742 *devattr, char *buf)
743{
744 struct f71805f_data *data = f71805f_update_device(dev);
745 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
746 int nr = attr->index;
747
748 return sprintf(buf, "%ld\n", temp_from_reg(data->temp_hyst[nr]));
749}
750
751static ssize_t show_temp_type(struct device *dev, struct device_attribute
752 *devattr, char *buf)
753{
754 struct f71805f_data *data = f71805f_update_device(dev);
755 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
756 int nr = attr->index;
757
758 /* 3 is diode, 4 is thermistor */
759 return sprintf(buf, "%u\n", (data->temp_mode & (1 << nr)) ? 3 : 4);
760}
761
762static ssize_t set_temp_max(struct device *dev, struct device_attribute
763 *devattr, const char *buf, size_t count)
764{
765 struct f71805f_data *data = dev_get_drvdata(dev);
766 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
767 int nr = attr->index;
768 long val = simple_strtol(buf, NULL, 10);
769
Jean Delvaref0819182006-01-18 23:20:53 +0100770 mutex_lock(&data->update_lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100771 data->temp_high[nr] = temp_to_reg(val);
772 f71805f_write8(data, F71805F_REG_TEMP_HIGH(nr), data->temp_high[nr]);
Jean Delvaref0819182006-01-18 23:20:53 +0100773 mutex_unlock(&data->update_lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100774
775 return count;
776}
777
778static ssize_t set_temp_hyst(struct device *dev, struct device_attribute
779 *devattr, const char *buf, size_t count)
780{
781 struct f71805f_data *data = dev_get_drvdata(dev);
782 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
783 int nr = attr->index;
784 long val = simple_strtol(buf, NULL, 10);
785
Jean Delvaref0819182006-01-18 23:20:53 +0100786 mutex_lock(&data->update_lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100787 data->temp_hyst[nr] = temp_to_reg(val);
788 f71805f_write8(data, F71805F_REG_TEMP_HYST(nr), data->temp_hyst[nr]);
Jean Delvaref0819182006-01-18 23:20:53 +0100789 mutex_unlock(&data->update_lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100790
791 return count;
792}
793
Jean Delvaree53004e2006-01-09 23:26:14 +0100794static ssize_t show_alarms_in(struct device *dev, struct device_attribute
795 *devattr, char *buf)
796{
797 struct f71805f_data *data = f71805f_update_device(dev);
798
Jean Delvare51c997d2006-12-12 18:18:29 +0100799 return sprintf(buf, "%lu\n", data->alarms & 0x7ff);
Jean Delvaree53004e2006-01-09 23:26:14 +0100800}
801
802static ssize_t show_alarms_fan(struct device *dev, struct device_attribute
803 *devattr, char *buf)
804{
805 struct f71805f_data *data = f71805f_update_device(dev);
806
Jean Delvare2d457712006-09-24 20:52:15 +0200807 return sprintf(buf, "%lu\n", (data->alarms >> 16) & 0x07);
Jean Delvaree53004e2006-01-09 23:26:14 +0100808}
809
810static ssize_t show_alarms_temp(struct device *dev, struct device_attribute
811 *devattr, char *buf)
812{
813 struct f71805f_data *data = f71805f_update_device(dev);
814
Jean Delvare2d457712006-09-24 20:52:15 +0200815 return sprintf(buf, "%lu\n", (data->alarms >> 11) & 0x07);
816}
817
818static ssize_t show_alarm(struct device *dev, struct device_attribute
819 *devattr, char *buf)
820{
821 struct f71805f_data *data = f71805f_update_device(dev);
822 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
823 int bitnr = attr->index;
824
825 return sprintf(buf, "%lu\n", (data->alarms >> bitnr) & 1);
Jean Delvaree53004e2006-01-09 23:26:14 +0100826}
827
Jean Delvaree53004e2006-01-09 23:26:14 +0100828static ssize_t show_name(struct device *dev, struct device_attribute
829 *devattr, char *buf)
830{
831 struct f71805f_data *data = dev_get_drvdata(dev);
832
833 return sprintf(buf, "%s\n", data->name);
834}
835
Jean Delvare51c997d2006-12-12 18:18:29 +0100836static SENSOR_DEVICE_ATTR(in0_input, S_IRUGO, show_in0, NULL, 0);
837static SENSOR_DEVICE_ATTR(in0_max, S_IRUGO| S_IWUSR,
838 show_in0_max, set_in0_max, 0);
839static SENSOR_DEVICE_ATTR(in0_min, S_IRUGO| S_IWUSR,
840 show_in0_min, set_in0_min, 0);
Jean Delvare0e39e012006-09-24 21:16:40 +0200841static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, show_in, NULL, 1);
842static SENSOR_DEVICE_ATTR(in1_max, S_IRUGO | S_IWUSR,
843 show_in_max, set_in_max, 1);
844static SENSOR_DEVICE_ATTR(in1_min, S_IRUGO | S_IWUSR,
845 show_in_min, set_in_min, 1);
846static SENSOR_DEVICE_ATTR(in2_input, S_IRUGO, show_in, NULL, 2);
847static SENSOR_DEVICE_ATTR(in2_max, S_IRUGO | S_IWUSR,
848 show_in_max, set_in_max, 2);
849static SENSOR_DEVICE_ATTR(in2_min, S_IRUGO | S_IWUSR,
850 show_in_min, set_in_min, 2);
851static SENSOR_DEVICE_ATTR(in3_input, S_IRUGO, show_in, NULL, 3);
852static SENSOR_DEVICE_ATTR(in3_max, S_IRUGO | S_IWUSR,
853 show_in_max, set_in_max, 3);
854static SENSOR_DEVICE_ATTR(in3_min, S_IRUGO | S_IWUSR,
855 show_in_min, set_in_min, 3);
856static SENSOR_DEVICE_ATTR(in4_input, S_IRUGO, show_in, NULL, 4);
857static SENSOR_DEVICE_ATTR(in4_max, S_IRUGO | S_IWUSR,
858 show_in_max, set_in_max, 4);
859static SENSOR_DEVICE_ATTR(in4_min, S_IRUGO | S_IWUSR,
860 show_in_min, set_in_min, 4);
861static SENSOR_DEVICE_ATTR(in5_input, S_IRUGO, show_in, NULL, 5);
862static SENSOR_DEVICE_ATTR(in5_max, S_IRUGO | S_IWUSR,
863 show_in_max, set_in_max, 5);
864static SENSOR_DEVICE_ATTR(in5_min, S_IRUGO | S_IWUSR,
865 show_in_min, set_in_min, 5);
866static SENSOR_DEVICE_ATTR(in6_input, S_IRUGO, show_in, NULL, 6);
867static SENSOR_DEVICE_ATTR(in6_max, S_IRUGO | S_IWUSR,
868 show_in_max, set_in_max, 6);
869static SENSOR_DEVICE_ATTR(in6_min, S_IRUGO | S_IWUSR,
870 show_in_min, set_in_min, 6);
871static SENSOR_DEVICE_ATTR(in7_input, S_IRUGO, show_in, NULL, 7);
872static SENSOR_DEVICE_ATTR(in7_max, S_IRUGO | S_IWUSR,
873 show_in_max, set_in_max, 7);
874static SENSOR_DEVICE_ATTR(in7_min, S_IRUGO | S_IWUSR,
875 show_in_min, set_in_min, 7);
876static SENSOR_DEVICE_ATTR(in8_input, S_IRUGO, show_in, NULL, 8);
877static SENSOR_DEVICE_ATTR(in8_max, S_IRUGO | S_IWUSR,
878 show_in_max, set_in_max, 8);
879static SENSOR_DEVICE_ATTR(in8_min, S_IRUGO | S_IWUSR,
880 show_in_min, set_in_min, 8);
Jean Delvare51c997d2006-12-12 18:18:29 +0100881static SENSOR_DEVICE_ATTR(in9_input, S_IRUGO, show_in0, NULL, 9);
882static SENSOR_DEVICE_ATTR(in9_max, S_IRUGO | S_IWUSR,
883 show_in0_max, set_in0_max, 9);
884static SENSOR_DEVICE_ATTR(in9_min, S_IRUGO | S_IWUSR,
885 show_in0_min, set_in0_min, 9);
886static SENSOR_DEVICE_ATTR(in10_input, S_IRUGO, show_in0, NULL, 10);
887static SENSOR_DEVICE_ATTR(in10_max, S_IRUGO | S_IWUSR,
888 show_in0_max, set_in0_max, 10);
889static SENSOR_DEVICE_ATTR(in10_min, S_IRUGO | S_IWUSR,
890 show_in0_min, set_in0_min, 10);
Jean Delvare0e39e012006-09-24 21:16:40 +0200891
892static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0);
893static SENSOR_DEVICE_ATTR(fan1_min, S_IRUGO | S_IWUSR,
894 show_fan_min, set_fan_min, 0);
Jean Delvare315c7112006-12-12 18:18:27 +0100895static SENSOR_DEVICE_ATTR(fan1_target, S_IRUGO | S_IWUSR,
896 show_fan_target, set_fan_target, 0);
Jean Delvare0e39e012006-09-24 21:16:40 +0200897static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, show_fan, NULL, 1);
898static SENSOR_DEVICE_ATTR(fan2_min, S_IRUGO | S_IWUSR,
899 show_fan_min, set_fan_min, 1);
Jean Delvare315c7112006-12-12 18:18:27 +0100900static SENSOR_DEVICE_ATTR(fan2_target, S_IRUGO | S_IWUSR,
901 show_fan_target, set_fan_target, 1);
Jean Delvare0e39e012006-09-24 21:16:40 +0200902static SENSOR_DEVICE_ATTR(fan3_input, S_IRUGO, show_fan, NULL, 2);
903static SENSOR_DEVICE_ATTR(fan3_min, S_IRUGO | S_IWUSR,
904 show_fan_min, set_fan_min, 2);
Jean Delvare315c7112006-12-12 18:18:27 +0100905static SENSOR_DEVICE_ATTR(fan3_target, S_IRUGO | S_IWUSR,
906 show_fan_target, set_fan_target, 2);
Jean Delvare0e39e012006-09-24 21:16:40 +0200907
908static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
909static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO | S_IWUSR,
910 show_temp_max, set_temp_max, 0);
911static SENSOR_DEVICE_ATTR(temp1_max_hyst, S_IRUGO | S_IWUSR,
912 show_temp_hyst, set_temp_hyst, 0);
913static SENSOR_DEVICE_ATTR(temp1_type, S_IRUGO, show_temp_type, NULL, 0);
914static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1);
915static SENSOR_DEVICE_ATTR(temp2_max, S_IRUGO | S_IWUSR,
916 show_temp_max, set_temp_max, 1);
917static SENSOR_DEVICE_ATTR(temp2_max_hyst, S_IRUGO | S_IWUSR,
918 show_temp_hyst, set_temp_hyst, 1);
919static SENSOR_DEVICE_ATTR(temp2_type, S_IRUGO, show_temp_type, NULL, 1);
920static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, show_temp, NULL, 2);
921static SENSOR_DEVICE_ATTR(temp3_max, S_IRUGO | S_IWUSR,
922 show_temp_max, set_temp_max, 2);
923static SENSOR_DEVICE_ATTR(temp3_max_hyst, S_IRUGO | S_IWUSR,
924 show_temp_hyst, set_temp_hyst, 2);
925static SENSOR_DEVICE_ATTR(temp3_type, S_IRUGO, show_temp_type, NULL, 2);
926
Jean Delvare95e35312006-12-12 18:18:26 +0100927/* pwm (value) files are created read-only, write permission is
928 then added or removed dynamically as needed */
929static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO, show_pwm, set_pwm, 0);
930static SENSOR_DEVICE_ATTR(pwm1_enable, S_IRUGO | S_IWUSR,
931 show_pwm_enable, set_pwm_enable, 0);
Jean Delvare6e2bc172006-12-12 18:18:27 +0100932static SENSOR_DEVICE_ATTR(pwm1_freq, S_IRUGO | S_IWUSR,
933 show_pwm_freq, set_pwm_freq, 0);
Jean Delvaree1967832006-12-12 18:18:27 +0100934static SENSOR_DEVICE_ATTR(pwm1_mode, S_IRUGO, show_pwm_mode, NULL, 0);
Jean Delvare95e35312006-12-12 18:18:26 +0100935static SENSOR_DEVICE_ATTR(pwm2, S_IRUGO, show_pwm, set_pwm, 1);
936static SENSOR_DEVICE_ATTR(pwm2_enable, S_IRUGO | S_IWUSR,
937 show_pwm_enable, set_pwm_enable, 1);
Jean Delvare6e2bc172006-12-12 18:18:27 +0100938static SENSOR_DEVICE_ATTR(pwm2_freq, S_IRUGO | S_IWUSR,
939 show_pwm_freq, set_pwm_freq, 1);
Jean Delvaree1967832006-12-12 18:18:27 +0100940static SENSOR_DEVICE_ATTR(pwm2_mode, S_IRUGO, show_pwm_mode, NULL, 1);
Jean Delvare95e35312006-12-12 18:18:26 +0100941static SENSOR_DEVICE_ATTR(pwm3, S_IRUGO, show_pwm, set_pwm, 2);
942static SENSOR_DEVICE_ATTR(pwm3_enable, S_IRUGO | S_IWUSR,
943 show_pwm_enable, set_pwm_enable, 2);
Jean Delvare6e2bc172006-12-12 18:18:27 +0100944static SENSOR_DEVICE_ATTR(pwm3_freq, S_IRUGO | S_IWUSR,
945 show_pwm_freq, set_pwm_freq, 2);
Jean Delvaree1967832006-12-12 18:18:27 +0100946static SENSOR_DEVICE_ATTR(pwm3_mode, S_IRUGO, show_pwm_mode, NULL, 2);
Jean Delvare95e35312006-12-12 18:18:26 +0100947
Jean Delvare0e39e012006-09-24 21:16:40 +0200948static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0);
949static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1);
950static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2);
951static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3);
952static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 4);
953static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 5);
954static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 6);
955static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 7);
956static SENSOR_DEVICE_ATTR(in8_alarm, S_IRUGO, show_alarm, NULL, 8);
Jean Delvare51c997d2006-12-12 18:18:29 +0100957static SENSOR_DEVICE_ATTR(in9_alarm, S_IRUGO, show_alarm, NULL, 9);
958static SENSOR_DEVICE_ATTR(in10_alarm, S_IRUGO, show_alarm, NULL, 10);
Jean Delvare0e39e012006-09-24 21:16:40 +0200959static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 11);
960static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 12);
961static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 13);
962static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 16);
963static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 17);
964static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 18);
965static DEVICE_ATTR(alarms_in, S_IRUGO, show_alarms_in, NULL);
966static DEVICE_ATTR(alarms_fan, S_IRUGO, show_alarms_fan, NULL);
967static DEVICE_ATTR(alarms_temp, S_IRUGO, show_alarms_temp, NULL);
968
969static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
970
971static struct attribute *f71805f_attributes[] = {
Jean Delvare51c997d2006-12-12 18:18:29 +0100972 &sensor_dev_attr_in0_input.dev_attr.attr,
973 &sensor_dev_attr_in0_max.dev_attr.attr,
974 &sensor_dev_attr_in0_min.dev_attr.attr,
Jean Delvare0e39e012006-09-24 21:16:40 +0200975 &sensor_dev_attr_in1_input.dev_attr.attr,
976 &sensor_dev_attr_in1_max.dev_attr.attr,
977 &sensor_dev_attr_in1_min.dev_attr.attr,
978 &sensor_dev_attr_in2_input.dev_attr.attr,
979 &sensor_dev_attr_in2_max.dev_attr.attr,
980 &sensor_dev_attr_in2_min.dev_attr.attr,
981 &sensor_dev_attr_in3_input.dev_attr.attr,
982 &sensor_dev_attr_in3_max.dev_attr.attr,
983 &sensor_dev_attr_in3_min.dev_attr.attr,
Jean Delvare0e39e012006-09-24 21:16:40 +0200984 &sensor_dev_attr_in5_input.dev_attr.attr,
985 &sensor_dev_attr_in5_max.dev_attr.attr,
986 &sensor_dev_attr_in5_min.dev_attr.attr,
987 &sensor_dev_attr_in6_input.dev_attr.attr,
988 &sensor_dev_attr_in6_max.dev_attr.attr,
989 &sensor_dev_attr_in6_min.dev_attr.attr,
990 &sensor_dev_attr_in7_input.dev_attr.attr,
991 &sensor_dev_attr_in7_max.dev_attr.attr,
992 &sensor_dev_attr_in7_min.dev_attr.attr,
Jean Delvare0e39e012006-09-24 21:16:40 +0200993
994 &sensor_dev_attr_temp1_input.dev_attr.attr,
995 &sensor_dev_attr_temp1_max.dev_attr.attr,
996 &sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
997 &sensor_dev_attr_temp1_type.dev_attr.attr,
998 &sensor_dev_attr_temp2_input.dev_attr.attr,
999 &sensor_dev_attr_temp2_max.dev_attr.attr,
1000 &sensor_dev_attr_temp2_max_hyst.dev_attr.attr,
1001 &sensor_dev_attr_temp2_type.dev_attr.attr,
1002 &sensor_dev_attr_temp3_input.dev_attr.attr,
1003 &sensor_dev_attr_temp3_max.dev_attr.attr,
1004 &sensor_dev_attr_temp3_max_hyst.dev_attr.attr,
1005 &sensor_dev_attr_temp3_type.dev_attr.attr,
1006
1007 &sensor_dev_attr_in0_alarm.dev_attr.attr,
1008 &sensor_dev_attr_in1_alarm.dev_attr.attr,
1009 &sensor_dev_attr_in2_alarm.dev_attr.attr,
1010 &sensor_dev_attr_in3_alarm.dev_attr.attr,
Jean Delvare0e39e012006-09-24 21:16:40 +02001011 &sensor_dev_attr_in5_alarm.dev_attr.attr,
1012 &sensor_dev_attr_in6_alarm.dev_attr.attr,
1013 &sensor_dev_attr_in7_alarm.dev_attr.attr,
Jean Delvare0e39e012006-09-24 21:16:40 +02001014 &dev_attr_alarms_in.attr,
1015 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
1016 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
1017 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
1018 &dev_attr_alarms_temp.attr,
1019 &dev_attr_alarms_fan.attr,
1020
1021 &dev_attr_name.attr,
1022 NULL
Jean Delvare2488a392006-01-09 23:29:11 +01001023};
1024
Jean Delvare0e39e012006-09-24 21:16:40 +02001025static const struct attribute_group f71805f_group = {
1026 .attrs = f71805f_attributes,
Jean Delvare2488a392006-01-09 23:29:11 +01001027};
1028
Jean Delvare51c997d2006-12-12 18:18:29 +01001029static struct attribute *f71805f_attributes_optin[4][5] = {
1030 {
1031 &sensor_dev_attr_in4_input.dev_attr.attr,
1032 &sensor_dev_attr_in4_max.dev_attr.attr,
1033 &sensor_dev_attr_in4_min.dev_attr.attr,
1034 &sensor_dev_attr_in4_alarm.dev_attr.attr,
1035 NULL
1036 }, {
1037 &sensor_dev_attr_in8_input.dev_attr.attr,
1038 &sensor_dev_attr_in8_max.dev_attr.attr,
1039 &sensor_dev_attr_in8_min.dev_attr.attr,
1040 &sensor_dev_attr_in8_alarm.dev_attr.attr,
1041 NULL
1042 }, {
1043 &sensor_dev_attr_in9_input.dev_attr.attr,
1044 &sensor_dev_attr_in9_max.dev_attr.attr,
1045 &sensor_dev_attr_in9_min.dev_attr.attr,
1046 &sensor_dev_attr_in9_alarm.dev_attr.attr,
1047 NULL
1048 }, {
1049 &sensor_dev_attr_in10_input.dev_attr.attr,
1050 &sensor_dev_attr_in10_max.dev_attr.attr,
1051 &sensor_dev_attr_in10_min.dev_attr.attr,
1052 &sensor_dev_attr_in10_alarm.dev_attr.attr,
1053 NULL
1054 }
1055};
1056
1057static const struct attribute_group f71805f_group_optin[4] = {
1058 { .attrs = f71805f_attributes_optin[0] },
1059 { .attrs = f71805f_attributes_optin[1] },
1060 { .attrs = f71805f_attributes_optin[2] },
1061 { .attrs = f71805f_attributes_optin[3] },
1062};
1063
Jean Delvare315c7112006-12-12 18:18:27 +01001064static struct attribute *f71805f_attributes_fan[3][8] = {
Jean Delvare0e39e012006-09-24 21:16:40 +02001065 {
1066 &sensor_dev_attr_fan1_input.dev_attr.attr,
1067 &sensor_dev_attr_fan1_min.dev_attr.attr,
1068 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
Jean Delvare315c7112006-12-12 18:18:27 +01001069 &sensor_dev_attr_fan1_target.dev_attr.attr,
Jean Delvare95e35312006-12-12 18:18:26 +01001070 &sensor_dev_attr_pwm1.dev_attr.attr,
1071 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
Jean Delvaree1967832006-12-12 18:18:27 +01001072 &sensor_dev_attr_pwm1_mode.dev_attr.attr,
Jean Delvare0e39e012006-09-24 21:16:40 +02001073 NULL
1074 }, {
1075 &sensor_dev_attr_fan2_input.dev_attr.attr,
1076 &sensor_dev_attr_fan2_min.dev_attr.attr,
1077 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
Jean Delvare315c7112006-12-12 18:18:27 +01001078 &sensor_dev_attr_fan2_target.dev_attr.attr,
Jean Delvare95e35312006-12-12 18:18:26 +01001079 &sensor_dev_attr_pwm2.dev_attr.attr,
1080 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
Jean Delvaree1967832006-12-12 18:18:27 +01001081 &sensor_dev_attr_pwm2_mode.dev_attr.attr,
Jean Delvare0e39e012006-09-24 21:16:40 +02001082 NULL
1083 }, {
1084 &sensor_dev_attr_fan3_input.dev_attr.attr,
1085 &sensor_dev_attr_fan3_min.dev_attr.attr,
1086 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
Jean Delvare315c7112006-12-12 18:18:27 +01001087 &sensor_dev_attr_fan3_target.dev_attr.attr,
Jean Delvare95e35312006-12-12 18:18:26 +01001088 &sensor_dev_attr_pwm3.dev_attr.attr,
1089 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
Jean Delvaree1967832006-12-12 18:18:27 +01001090 &sensor_dev_attr_pwm3_mode.dev_attr.attr,
Jean Delvare0e39e012006-09-24 21:16:40 +02001091 NULL
1092 }
1093};
1094
1095static const struct attribute_group f71805f_group_fan[3] = {
1096 { .attrs = f71805f_attributes_fan[0] },
1097 { .attrs = f71805f_attributes_fan[1] },
1098 { .attrs = f71805f_attributes_fan[2] },
Jean Delvare2488a392006-01-09 23:29:11 +01001099};
Jean Delvaree53004e2006-01-09 23:26:14 +01001100
Jean Delvaree1967832006-12-12 18:18:27 +01001101/* We don't include pwm_freq files in the arrays above, because they must be
1102 created conditionally (only if pwm_mode is 1 == PWM) */
1103static struct attribute *f71805f_attributes_pwm_freq[] = {
1104 &sensor_dev_attr_pwm1_freq.dev_attr.attr,
1105 &sensor_dev_attr_pwm2_freq.dev_attr.attr,
1106 &sensor_dev_attr_pwm3_freq.dev_attr.attr,
1107 NULL
1108};
1109
1110static const struct attribute_group f71805f_group_pwm_freq = {
1111 .attrs = f71805f_attributes_pwm_freq,
1112};
1113
Jean Delvare95e35312006-12-12 18:18:26 +01001114/* We also need an indexed access to pwmN files to toggle writability */
1115static struct attribute *f71805f_attr_pwm[] = {
1116 &sensor_dev_attr_pwm1.dev_attr.attr,
1117 &sensor_dev_attr_pwm2.dev_attr.attr,
1118 &sensor_dev_attr_pwm3.dev_attr.attr,
1119};
1120
Jean Delvaree53004e2006-01-09 23:26:14 +01001121/*
1122 * Device registration and initialization
1123 */
1124
1125static void __devinit f71805f_init_device(struct f71805f_data *data)
1126{
1127 u8 reg;
1128 int i;
1129
1130 reg = f71805f_read8(data, F71805F_REG_START);
1131 if ((reg & 0x41) != 0x01) {
1132 printk(KERN_DEBUG DRVNAME ": Starting monitoring "
1133 "operations\n");
1134 f71805f_write8(data, F71805F_REG_START, (reg | 0x01) & ~0x40);
1135 }
1136
1137 /* Fan monitoring can be disabled. If it is, we won't be polling
1138 the register values, and won't create the related sysfs files. */
1139 for (i = 0; i < 3; i++) {
Jean Delvare6b14a542006-12-12 18:18:26 +01001140 data->fan_ctrl[i] = f71805f_read8(data,
1141 F71805F_REG_FAN_CTRL(i));
Jean Delvare315c7112006-12-12 18:18:27 +01001142 /* Clear latch full bit, else "speed mode" fan speed control
1143 doesn't work */
1144 if (data->fan_ctrl[i] & FAN_CTRL_LATCH_FULL) {
1145 data->fan_ctrl[i] &= ~FAN_CTRL_LATCH_FULL;
1146 f71805f_write8(data, F71805F_REG_FAN_CTRL(i),
1147 data->fan_ctrl[i]);
1148 }
Jean Delvaree53004e2006-01-09 23:26:14 +01001149 }
1150}
1151
1152static int __devinit f71805f_probe(struct platform_device *pdev)
1153{
Jean Delvare51c997d2006-12-12 18:18:29 +01001154 struct f71805f_sio_data *sio_data = pdev->dev.platform_data;
Jean Delvaree53004e2006-01-09 23:26:14 +01001155 struct f71805f_data *data;
1156 struct resource *res;
Jean Delvare2488a392006-01-09 23:29:11 +01001157 int i, err;
Jean Delvaree53004e2006-01-09 23:26:14 +01001158
Jean Delvare51c997d2006-12-12 18:18:29 +01001159 static const char *names[] = {
1160 "f71805f",
1161 "f71872f",
1162 };
1163
Jean Delvaree53004e2006-01-09 23:26:14 +01001164 if (!(data = kzalloc(sizeof(struct f71805f_data), GFP_KERNEL))) {
1165 err = -ENOMEM;
1166 printk(KERN_ERR DRVNAME ": Out of memory\n");
1167 goto exit;
1168 }
1169
1170 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
1171 data->addr = res->start;
Jean Delvaref0819182006-01-18 23:20:53 +01001172 mutex_init(&data->lock);
Jean Delvare51c997d2006-12-12 18:18:29 +01001173 data->name = names[sio_data->kind];
Jean Delvaref0819182006-01-18 23:20:53 +01001174 mutex_init(&data->update_lock);
Jean Delvaree53004e2006-01-09 23:26:14 +01001175
1176 platform_set_drvdata(pdev, data);
1177
Jean Delvare51c997d2006-12-12 18:18:29 +01001178 /* Some voltage inputs depend on chip model and configuration */
1179 switch (sio_data->kind) {
1180 case f71805f:
1181 data->has_in = 0x1ff;
1182 break;
1183 case f71872f:
1184 data->has_in = 0x6ef;
1185 if (sio_data->fnsel1 & 0x01)
1186 data->has_in |= (1 << 4); /* in4 */
1187 if (sio_data->fnsel1 & 0x02)
1188 data->has_in |= (1 << 8); /* in8 */
1189 break;
1190 }
1191
Jean Delvaree53004e2006-01-09 23:26:14 +01001192 /* Initialize the F71805F chip */
1193 f71805f_init_device(data);
1194
1195 /* Register sysfs interface files */
Jean Delvare0e39e012006-09-24 21:16:40 +02001196 if ((err = sysfs_create_group(&pdev->dev.kobj, &f71805f_group)))
1197 goto exit_free;
Jean Delvare51c997d2006-12-12 18:18:29 +01001198 if (data->has_in & (1 << 4)) { /* in4 */
1199 if ((err = sysfs_create_group(&pdev->dev.kobj,
1200 &f71805f_group_optin[0])))
1201 goto exit_remove_files;
1202 }
1203 if (data->has_in & (1 << 8)) { /* in8 */
1204 if ((err = sysfs_create_group(&pdev->dev.kobj,
1205 &f71805f_group_optin[1])))
1206 goto exit_remove_files;
1207 }
1208 if (data->has_in & (1 << 9)) { /* in9 (F71872F/FG only) */
1209 if ((err = sysfs_create_group(&pdev->dev.kobj,
1210 &f71805f_group_optin[2])))
1211 goto exit_remove_files;
1212 }
1213 if (data->has_in & (1 << 10)) { /* in9 (F71872F/FG only) */
1214 if ((err = sysfs_create_group(&pdev->dev.kobj,
1215 &f71805f_group_optin[3])))
1216 goto exit_remove_files;
1217 }
Jean Delvare0e39e012006-09-24 21:16:40 +02001218 for (i = 0; i < 3; i++) {
Jean Delvare6b14a542006-12-12 18:18:26 +01001219 if (data->fan_ctrl[i] & FAN_CTRL_SKIP)
Jean Delvare2488a392006-01-09 23:29:11 +01001220 continue;
Jean Delvare0e39e012006-09-24 21:16:40 +02001221 if ((err = sysfs_create_group(&pdev->dev.kobj,
1222 &f71805f_group_fan[i])))
1223 goto exit_remove_files;
Jean Delvaree1967832006-12-12 18:18:27 +01001224 /* If control mode is PWM, create pwm_freq file */
1225 if (!(data->fan_ctrl[i] & FAN_CTRL_DC_MODE)) {
1226 if ((err = sysfs_create_file(&pdev->dev.kobj,
1227 f71805f_attributes_pwm_freq[i])))
1228 goto exit_remove_files;
1229 }
Jean Delvare95e35312006-12-12 18:18:26 +01001230 /* If PWM is in manual mode, add write permission */
1231 if (data->fan_ctrl[i] & FAN_CTRL_MODE_MANUAL) {
1232 if ((err = sysfs_chmod_file(&pdev->dev.kobj,
1233 f71805f_attr_pwm[i],
1234 S_IRUGO | S_IWUSR))) {
1235 dev_err(&pdev->dev, "chmod +w pwm%d failed\n",
1236 i + 1);
1237 goto exit_remove_files;
1238 }
1239 }
Jean Delvare0e39e012006-09-24 21:16:40 +02001240 }
1241
1242 data->class_dev = hwmon_device_register(&pdev->dev);
1243 if (IS_ERR(data->class_dev)) {
1244 err = PTR_ERR(data->class_dev);
1245 dev_err(&pdev->dev, "Class registration failed (%d)\n", err);
1246 goto exit_remove_files;
Jean Delvaree53004e2006-01-09 23:26:14 +01001247 }
Jean Delvaree53004e2006-01-09 23:26:14 +01001248
1249 return 0;
1250
Jean Delvare0e39e012006-09-24 21:16:40 +02001251exit_remove_files:
1252 sysfs_remove_group(&pdev->dev.kobj, &f71805f_group);
Jean Delvare51c997d2006-12-12 18:18:29 +01001253 for (i = 0; i < 4; i++)
1254 sysfs_remove_group(&pdev->dev.kobj, &f71805f_group_optin[i]);
Jean Delvare0e39e012006-09-24 21:16:40 +02001255 for (i = 0; i < 3; i++)
1256 sysfs_remove_group(&pdev->dev.kobj, &f71805f_group_fan[i]);
Jean Delvaree1967832006-12-12 18:18:27 +01001257 sysfs_remove_group(&pdev->dev.kobj, &f71805f_group_pwm_freq);
Jean Delvaree53004e2006-01-09 23:26:14 +01001258exit_free:
Jean Delvare0e39e012006-09-24 21:16:40 +02001259 platform_set_drvdata(pdev, NULL);
Jean Delvaree53004e2006-01-09 23:26:14 +01001260 kfree(data);
1261exit:
1262 return err;
1263}
1264
1265static int __devexit f71805f_remove(struct platform_device *pdev)
1266{
1267 struct f71805f_data *data = platform_get_drvdata(pdev);
Jean Delvare0e39e012006-09-24 21:16:40 +02001268 int i;
Jean Delvaree53004e2006-01-09 23:26:14 +01001269
1270 platform_set_drvdata(pdev, NULL);
1271 hwmon_device_unregister(data->class_dev);
Jean Delvare0e39e012006-09-24 21:16:40 +02001272 sysfs_remove_group(&pdev->dev.kobj, &f71805f_group);
Jean Delvare51c997d2006-12-12 18:18:29 +01001273 for (i = 0; i < 4; i++)
1274 sysfs_remove_group(&pdev->dev.kobj, &f71805f_group_optin[i]);
Jean Delvare0e39e012006-09-24 21:16:40 +02001275 for (i = 0; i < 3; i++)
1276 sysfs_remove_group(&pdev->dev.kobj, &f71805f_group_fan[i]);
Jean Delvaree1967832006-12-12 18:18:27 +01001277 sysfs_remove_group(&pdev->dev.kobj, &f71805f_group_pwm_freq);
Jean Delvaree53004e2006-01-09 23:26:14 +01001278 kfree(data);
1279
1280 return 0;
1281}
1282
1283static struct platform_driver f71805f_driver = {
1284 .driver = {
1285 .owner = THIS_MODULE,
1286 .name = DRVNAME,
1287 },
1288 .probe = f71805f_probe,
1289 .remove = __devexit_p(f71805f_remove),
1290};
1291
Jean Delvare51c997d2006-12-12 18:18:29 +01001292static int __init f71805f_device_add(unsigned short address,
1293 const struct f71805f_sio_data *sio_data)
Jean Delvaree53004e2006-01-09 23:26:14 +01001294{
Jean Delvare568825c2006-03-23 16:40:23 +01001295 struct resource res = {
1296 .start = address,
1297 .end = address + REGION_LENGTH - 1,
1298 .flags = IORESOURCE_IO,
1299 };
Jean Delvaree53004e2006-01-09 23:26:14 +01001300 int err;
1301
1302 pdev = platform_device_alloc(DRVNAME, address);
1303 if (!pdev) {
1304 err = -ENOMEM;
1305 printk(KERN_ERR DRVNAME ": Device allocation failed\n");
1306 goto exit;
1307 }
1308
Jean Delvare568825c2006-03-23 16:40:23 +01001309 res.name = pdev->name;
1310 err = platform_device_add_resources(pdev, &res, 1);
Jean Delvaree53004e2006-01-09 23:26:14 +01001311 if (err) {
1312 printk(KERN_ERR DRVNAME ": Device resource addition failed "
1313 "(%d)\n", err);
1314 goto exit_device_put;
1315 }
1316
Jean Delvare51c997d2006-12-12 18:18:29 +01001317 pdev->dev.platform_data = kmalloc(sizeof(struct f71805f_sio_data),
1318 GFP_KERNEL);
1319 if (!pdev->dev.platform_data) {
1320 err = -ENOMEM;
1321 printk(KERN_ERR DRVNAME ": Platform data allocation failed\n");
1322 goto exit_device_put;
1323 }
1324 memcpy(pdev->dev.platform_data, sio_data,
1325 sizeof(struct f71805f_sio_data));
1326
Jean Delvaree53004e2006-01-09 23:26:14 +01001327 err = platform_device_add(pdev);
1328 if (err) {
1329 printk(KERN_ERR DRVNAME ": Device addition failed (%d)\n",
1330 err);
Jean Delvare51c997d2006-12-12 18:18:29 +01001331 goto exit_kfree_data;
Jean Delvaree53004e2006-01-09 23:26:14 +01001332 }
1333
1334 return 0;
1335
Jean Delvare51c997d2006-12-12 18:18:29 +01001336exit_kfree_data:
1337 kfree(pdev->dev.platform_data);
1338 pdev->dev.platform_data = NULL;
Jean Delvaree53004e2006-01-09 23:26:14 +01001339exit_device_put:
1340 platform_device_put(pdev);
1341exit:
1342 return err;
1343}
1344
Jean Delvare51c997d2006-12-12 18:18:29 +01001345static int __init f71805f_find(int sioaddr, unsigned short *address,
1346 struct f71805f_sio_data *sio_data)
Jean Delvaree53004e2006-01-09 23:26:14 +01001347{
1348 int err = -ENODEV;
1349 u16 devid;
1350
Jean Delvare51c997d2006-12-12 18:18:29 +01001351 static const char *names[] = {
1352 "F71805F/FG",
1353 "F71872F/FG",
1354 };
1355
Jean Delvaree53004e2006-01-09 23:26:14 +01001356 superio_enter(sioaddr);
1357
1358 devid = superio_inw(sioaddr, SIO_REG_MANID);
1359 if (devid != SIO_FINTEK_ID)
1360 goto exit;
1361
1362 devid = superio_inw(sioaddr, SIO_REG_DEVID);
Jean Delvare51c997d2006-12-12 18:18:29 +01001363 switch (devid) {
1364 case SIO_F71805F_ID:
1365 sio_data->kind = f71805f;
1366 break;
1367 case SIO_F71872F_ID:
1368 sio_data->kind = f71872f;
1369 sio_data->fnsel1 = superio_inb(sioaddr, SIO_REG_FNSEL1);
1370 break;
1371 default:
Jean Delvaree53004e2006-01-09 23:26:14 +01001372 printk(KERN_INFO DRVNAME ": Unsupported Fintek device, "
1373 "skipping\n");
1374 goto exit;
1375 }
1376
1377 superio_select(sioaddr, F71805F_LD_HWM);
1378 if (!(superio_inb(sioaddr, SIO_REG_ENABLE) & 0x01)) {
1379 printk(KERN_WARNING DRVNAME ": Device not activated, "
1380 "skipping\n");
1381 goto exit;
1382 }
1383
1384 *address = superio_inw(sioaddr, SIO_REG_ADDR);
1385 if (*address == 0) {
1386 printk(KERN_WARNING DRVNAME ": Base address not set, "
1387 "skipping\n");
1388 goto exit;
1389 }
1390
1391 err = 0;
Jean Delvare51c997d2006-12-12 18:18:29 +01001392 printk(KERN_INFO DRVNAME ": Found %s chip at %#x, revision %u\n",
1393 names[sio_data->kind], *address,
1394 superio_inb(sioaddr, SIO_REG_DEVREV));
Jean Delvaree53004e2006-01-09 23:26:14 +01001395
1396exit:
1397 superio_exit(sioaddr);
1398 return err;
1399}
1400
1401static int __init f71805f_init(void)
1402{
1403 int err;
1404 unsigned short address;
Jean Delvare51c997d2006-12-12 18:18:29 +01001405 struct f71805f_sio_data sio_data;
Jean Delvaree53004e2006-01-09 23:26:14 +01001406
Jean Delvare51c997d2006-12-12 18:18:29 +01001407 if (f71805f_find(0x2e, &address, &sio_data)
1408 && f71805f_find(0x4e, &address, &sio_data))
Jean Delvaree53004e2006-01-09 23:26:14 +01001409 return -ENODEV;
1410
1411 err = platform_driver_register(&f71805f_driver);
1412 if (err)
1413 goto exit;
1414
1415 /* Sets global pdev as a side effect */
Jean Delvare51c997d2006-12-12 18:18:29 +01001416 err = f71805f_device_add(address, &sio_data);
Jean Delvaree53004e2006-01-09 23:26:14 +01001417 if (err)
1418 goto exit_driver;
1419
1420 return 0;
1421
1422exit_driver:
1423 platform_driver_unregister(&f71805f_driver);
1424exit:
1425 return err;
1426}
1427
1428static void __exit f71805f_exit(void)
1429{
Jean Delvare51c997d2006-12-12 18:18:29 +01001430 kfree(pdev->dev.platform_data);
1431 pdev->dev.platform_data = NULL;
Jean Delvaree53004e2006-01-09 23:26:14 +01001432 platform_device_unregister(pdev);
Jean Delvare51c997d2006-12-12 18:18:29 +01001433
Jean Delvaree53004e2006-01-09 23:26:14 +01001434 platform_driver_unregister(&f71805f_driver);
1435}
1436
1437MODULE_AUTHOR("Jean Delvare <khali@linux-fr>");
1438MODULE_LICENSE("GPL");
Jean Delvare51c997d2006-12-12 18:18:29 +01001439MODULE_DESCRIPTION("F71805F/F71872F hardware monitoring driver");
Jean Delvaree53004e2006-01-09 23:26:14 +01001440
1441module_init(f71805f_init);
1442module_exit(f71805f_exit);