blob: 7a14a2dbb752e8835fcc652f97d9f57277f53783 [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 Delvare9cab0212007-07-15 10:36:06 +020013 * The F71806F/FG is essentially the same as the F71872F/FG. It even has
14 * the same chip ID, so the driver can't differentiate between.
15 *
Jean Delvaree53004e2006-01-09 23:26:14 +010016 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or
19 * (at your option) any later version.
20 *
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
25 *
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 */
30
31#include <linux/module.h>
32#include <linux/init.h>
33#include <linux/slab.h>
34#include <linux/jiffies.h>
35#include <linux/platform_device.h>
36#include <linux/hwmon.h>
37#include <linux/hwmon-sysfs.h>
38#include <linux/err.h>
Jean Delvaref0819182006-01-18 23:20:53 +010039#include <linux/mutex.h>
Jean Delvare0e39e012006-09-24 21:16:40 +020040#include <linux/sysfs.h>
Jean Delvarece7ee4e2007-05-08 17:21:59 +020041#include <linux/ioport.h>
Jean Delvaree53004e2006-01-09 23:26:14 +010042#include <asm/io.h>
43
Jean Delvare67b671b2007-12-06 23:13:42 +010044static unsigned short force_id;
45module_param(force_id, ushort, 0);
46MODULE_PARM_DESC(force_id, "Override the detected device ID");
47
Jean Delvaree53004e2006-01-09 23:26:14 +010048static struct platform_device *pdev;
49
50#define DRVNAME "f71805f"
Jean Delvare51c997d2006-12-12 18:18:29 +010051enum kinds { f71805f, f71872f };
Jean Delvaree53004e2006-01-09 23:26:14 +010052
53/*
54 * Super-I/O constants and functions
55 */
56
57#define F71805F_LD_HWM 0x04
58
59#define SIO_REG_LDSEL 0x07 /* Logical device select */
60#define SIO_REG_DEVID 0x20 /* Device ID (2 bytes) */
61#define SIO_REG_DEVREV 0x22 /* Device revision */
62#define SIO_REG_MANID 0x23 /* Fintek ID (2 bytes) */
Jean Delvare51c997d2006-12-12 18:18:29 +010063#define SIO_REG_FNSEL1 0x29 /* Multi Function Select 1 (F71872F) */
Jean Delvaree53004e2006-01-09 23:26:14 +010064#define SIO_REG_ENABLE 0x30 /* Logical device enable */
65#define SIO_REG_ADDR 0x60 /* Logical device address (2 bytes) */
66
67#define SIO_FINTEK_ID 0x1934
68#define SIO_F71805F_ID 0x0406
Jean Delvare51c997d2006-12-12 18:18:29 +010069#define SIO_F71872F_ID 0x0341
Jean Delvaree53004e2006-01-09 23:26:14 +010070
71static inline int
72superio_inb(int base, int reg)
73{
74 outb(reg, base);
75 return inb(base + 1);
76}
77
78static int
79superio_inw(int base, int reg)
80{
81 int val;
82 outb(reg++, base);
83 val = inb(base + 1) << 8;
84 outb(reg, base);
85 val |= inb(base + 1);
86 return val;
87}
88
89static inline void
90superio_select(int base, int ld)
91{
92 outb(SIO_REG_LDSEL, base);
93 outb(ld, base + 1);
94}
95
96static inline void
97superio_enter(int base)
98{
99 outb(0x87, base);
100 outb(0x87, base);
101}
102
103static inline void
104superio_exit(int base)
105{
106 outb(0xaa, base);
107}
108
109/*
110 * ISA constants
111 */
112
Jean Delvare75c99022006-12-12 18:18:29 +0100113#define REGION_LENGTH 8
114#define ADDR_REG_OFFSET 5
115#define DATA_REG_OFFSET 6
Jean Delvaree53004e2006-01-09 23:26:14 +0100116
Jean Delvaree53004e2006-01-09 23:26:14 +0100117/*
118 * Registers
119 */
120
Jean Delvare51c997d2006-12-12 18:18:29 +0100121/* in nr from 0 to 10 (8-bit values) */
Jean Delvaree53004e2006-01-09 23:26:14 +0100122#define F71805F_REG_IN(nr) (0x10 + (nr))
Jean Delvare51c997d2006-12-12 18:18:29 +0100123#define F71805F_REG_IN_HIGH(nr) ((nr) < 10 ? 0x40 + 2 * (nr) : 0x2E)
124#define F71805F_REG_IN_LOW(nr) ((nr) < 10 ? 0x41 + 2 * (nr) : 0x2F)
Jean Delvaree53004e2006-01-09 23:26:14 +0100125/* fan nr from 0 to 2 (12-bit values, two registers) */
126#define F71805F_REG_FAN(nr) (0x20 + 2 * (nr))
127#define F71805F_REG_FAN_LOW(nr) (0x28 + 2 * (nr))
Jean Delvare315c7112006-12-12 18:18:27 +0100128#define F71805F_REG_FAN_TARGET(nr) (0x69 + 16 * (nr))
Jean Delvaree53004e2006-01-09 23:26:14 +0100129#define F71805F_REG_FAN_CTRL(nr) (0x60 + 16 * (nr))
Jean Delvare6e2bc172006-12-12 18:18:27 +0100130#define F71805F_REG_PWM_FREQ(nr) (0x63 + 16 * (nr))
Jean Delvare95e35312006-12-12 18:18:26 +0100131#define F71805F_REG_PWM_DUTY(nr) (0x6B + 16 * (nr))
Jean Delvaree53004e2006-01-09 23:26:14 +0100132/* temp nr from 0 to 2 (8-bit values) */
133#define F71805F_REG_TEMP(nr) (0x1B + (nr))
134#define F71805F_REG_TEMP_HIGH(nr) (0x54 + 2 * (nr))
135#define F71805F_REG_TEMP_HYST(nr) (0x55 + 2 * (nr))
136#define F71805F_REG_TEMP_MODE 0x01
Phil Endecottaba50732007-06-29 09:19:14 +0200137/* pwm/fan pwmnr from 0 to 2, auto point apnr from 0 to 2 */
138/* map Fintek numbers to our numbers as follows: 9->0, 5->1, 1->2 */
139#define F71805F_REG_PWM_AUTO_POINT_TEMP(pwmnr, apnr) \
140 (0xA0 + 0x10 * (pwmnr) + (2 - (apnr)))
141#define F71805F_REG_PWM_AUTO_POINT_FAN(pwmnr, apnr) \
142 (0xA4 + 0x10 * (pwmnr) + \
143 2 * (2 - (apnr)))
Jean Delvaree53004e2006-01-09 23:26:14 +0100144
145#define F71805F_REG_START 0x00
146/* status nr from 0 to 2 */
147#define F71805F_REG_STATUS(nr) (0x36 + (nr))
148
Jean Delvare6b14a542006-12-12 18:18:26 +0100149/* individual register bits */
Jean Delvaree1967832006-12-12 18:18:27 +0100150#define FAN_CTRL_DC_MODE 0x10
Jean Delvare315c7112006-12-12 18:18:27 +0100151#define FAN_CTRL_LATCH_FULL 0x08
Jean Delvare95e35312006-12-12 18:18:26 +0100152#define FAN_CTRL_MODE_MASK 0x03
153#define FAN_CTRL_MODE_SPEED 0x00
154#define FAN_CTRL_MODE_TEMPERATURE 0x01
155#define FAN_CTRL_MODE_MANUAL 0x02
Jean Delvare6b14a542006-12-12 18:18:26 +0100156
Jean Delvaree53004e2006-01-09 23:26:14 +0100157/*
158 * Data structures and manipulation thereof
159 */
160
Phil Endecottaba50732007-06-29 09:19:14 +0200161struct f71805f_auto_point {
162 u8 temp[3];
163 u16 fan[3];
164};
165
Jean Delvaree53004e2006-01-09 23:26:14 +0100166struct f71805f_data {
167 unsigned short addr;
168 const char *name;
Tony Jones1beeffe2007-08-20 13:46:20 -0700169 struct device *hwmon_dev;
Jean Delvaree53004e2006-01-09 23:26:14 +0100170
Jean Delvaref0819182006-01-18 23:20:53 +0100171 struct mutex update_lock;
Jean Delvaree53004e2006-01-09 23:26:14 +0100172 char valid; /* !=0 if following fields are valid */
173 unsigned long last_updated; /* In jiffies */
174 unsigned long last_limits; /* In jiffies */
175
176 /* Register values */
Jean Delvare51c997d2006-12-12 18:18:29 +0100177 u8 in[11];
178 u8 in_high[11];
179 u8 in_low[11];
180 u16 has_in;
Jean Delvaree53004e2006-01-09 23:26:14 +0100181 u16 fan[3];
182 u16 fan_low[3];
Jean Delvare315c7112006-12-12 18:18:27 +0100183 u16 fan_target[3];
Jean Delvare6b14a542006-12-12 18:18:26 +0100184 u8 fan_ctrl[3];
Jean Delvare95e35312006-12-12 18:18:26 +0100185 u8 pwm[3];
Jean Delvare6e2bc172006-12-12 18:18:27 +0100186 u8 pwm_freq[3];
Jean Delvaree53004e2006-01-09 23:26:14 +0100187 u8 temp[3];
188 u8 temp_high[3];
189 u8 temp_hyst[3];
190 u8 temp_mode;
Jean Delvare2d457712006-09-24 20:52:15 +0200191 unsigned long alarms;
Phil Endecottaba50732007-06-29 09:19:14 +0200192 struct f71805f_auto_point auto_points[3];
Jean Delvaree53004e2006-01-09 23:26:14 +0100193};
194
Jean Delvare51c997d2006-12-12 18:18:29 +0100195struct f71805f_sio_data {
196 enum kinds kind;
197 u8 fnsel1;
198};
199
Jean Delvaree53004e2006-01-09 23:26:14 +0100200static inline long in_from_reg(u8 reg)
201{
202 return (reg * 8);
203}
204
205/* The 2 least significant bits are not used */
206static inline u8 in_to_reg(long val)
207{
208 if (val <= 0)
209 return 0;
210 if (val >= 2016)
211 return 0xfc;
212 return (((val + 16) / 32) << 2);
213}
214
215/* in0 is downscaled by a factor 2 internally */
216static inline long in0_from_reg(u8 reg)
217{
218 return (reg * 16);
219}
220
221static inline u8 in0_to_reg(long val)
222{
223 if (val <= 0)
224 return 0;
225 if (val >= 4032)
226 return 0xfc;
227 return (((val + 32) / 64) << 2);
228}
229
230/* The 4 most significant bits are not used */
231static inline long fan_from_reg(u16 reg)
232{
233 reg &= 0xfff;
234 if (!reg || reg == 0xfff)
235 return 0;
236 return (1500000 / reg);
237}
238
239static inline u16 fan_to_reg(long rpm)
240{
241 /* If the low limit is set below what the chip can measure,
242 store the largest possible 12-bit value in the registers,
243 so that no alarm will ever trigger. */
244 if (rpm < 367)
245 return 0xfff;
246 return (1500000 / rpm);
247}
248
Jean Delvare6e2bc172006-12-12 18:18:27 +0100249static inline unsigned long pwm_freq_from_reg(u8 reg)
250{
251 unsigned long clock = (reg & 0x80) ? 48000000UL : 1000000UL;
252
253 reg &= 0x7f;
254 if (reg == 0)
255 reg++;
256 return clock / (reg << 8);
257}
258
259static inline u8 pwm_freq_to_reg(unsigned long val)
260{
261 if (val >= 187500) /* The highest we can do */
262 return 0x80;
263 if (val >= 1475) /* Use 48 MHz clock */
264 return 0x80 | (48000000UL / (val << 8));
265 if (val < 31) /* The lowest we can do */
266 return 0x7f;
267 else /* Use 1 MHz clock */
268 return 1000000UL / (val << 8);
269}
270
Jean Delvaree1967832006-12-12 18:18:27 +0100271static inline int pwm_mode_from_reg(u8 reg)
272{
273 return !(reg & FAN_CTRL_DC_MODE);
274}
275
Jean Delvaree53004e2006-01-09 23:26:14 +0100276static inline long temp_from_reg(u8 reg)
277{
278 return (reg * 1000);
279}
280
281static inline u8 temp_to_reg(long val)
282{
283 if (val < 0)
284 val = 0;
285 else if (val > 1000 * 0xff)
286 val = 0xff;
287 return ((val + 500) / 1000);
288}
289
290/*
291 * Device I/O access
292 */
293
Jean Delvare7f999aa2007-02-14 21:15:03 +0100294/* Must be called with data->update_lock held, except during initialization */
Jean Delvaree53004e2006-01-09 23:26:14 +0100295static u8 f71805f_read8(struct f71805f_data *data, u8 reg)
296{
Jean Delvaree53004e2006-01-09 23:26:14 +0100297 outb(reg, data->addr + ADDR_REG_OFFSET);
Jean Delvare7f999aa2007-02-14 21:15:03 +0100298 return inb(data->addr + DATA_REG_OFFSET);
Jean Delvaree53004e2006-01-09 23:26:14 +0100299}
300
Jean Delvare7f999aa2007-02-14 21:15:03 +0100301/* Must be called with data->update_lock held, except during initialization */
Jean Delvaree53004e2006-01-09 23:26:14 +0100302static void f71805f_write8(struct f71805f_data *data, u8 reg, u8 val)
303{
Jean Delvaree53004e2006-01-09 23:26:14 +0100304 outb(reg, data->addr + ADDR_REG_OFFSET);
305 outb(val, data->addr + DATA_REG_OFFSET);
Jean Delvaree53004e2006-01-09 23:26:14 +0100306}
307
308/* It is important to read the MSB first, because doing so latches the
Jean Delvare7f999aa2007-02-14 21:15:03 +0100309 value of the LSB, so we are sure both bytes belong to the same value.
310 Must be called with data->update_lock held, except during initialization */
Jean Delvaree53004e2006-01-09 23:26:14 +0100311static u16 f71805f_read16(struct f71805f_data *data, u8 reg)
312{
313 u16 val;
314
Jean Delvaree53004e2006-01-09 23:26:14 +0100315 outb(reg, data->addr + ADDR_REG_OFFSET);
316 val = inb(data->addr + DATA_REG_OFFSET) << 8;
317 outb(++reg, data->addr + ADDR_REG_OFFSET);
318 val |= inb(data->addr + DATA_REG_OFFSET);
Jean Delvaree53004e2006-01-09 23:26:14 +0100319
320 return val;
321}
322
Jean Delvare7f999aa2007-02-14 21:15:03 +0100323/* Must be called with data->update_lock held, except during initialization */
Jean Delvaree53004e2006-01-09 23:26:14 +0100324static void f71805f_write16(struct f71805f_data *data, u8 reg, u16 val)
325{
Jean Delvaree53004e2006-01-09 23:26:14 +0100326 outb(reg, data->addr + ADDR_REG_OFFSET);
327 outb(val >> 8, data->addr + DATA_REG_OFFSET);
328 outb(++reg, data->addr + ADDR_REG_OFFSET);
329 outb(val & 0xff, data->addr + DATA_REG_OFFSET);
Jean Delvaree53004e2006-01-09 23:26:14 +0100330}
331
332static struct f71805f_data *f71805f_update_device(struct device *dev)
333{
334 struct f71805f_data *data = dev_get_drvdata(dev);
Phil Endecottaba50732007-06-29 09:19:14 +0200335 int nr, apnr;
Jean Delvaree53004e2006-01-09 23:26:14 +0100336
Jean Delvaref0819182006-01-18 23:20:53 +0100337 mutex_lock(&data->update_lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100338
339 /* Limit registers cache is refreshed after 60 seconds */
340 if (time_after(jiffies, data->last_updated + 60 * HZ)
341 || !data->valid) {
Jean Delvare51c997d2006-12-12 18:18:29 +0100342 for (nr = 0; nr < 11; nr++) {
343 if (!(data->has_in & (1 << nr)))
344 continue;
Jean Delvaree53004e2006-01-09 23:26:14 +0100345 data->in_high[nr] = f71805f_read8(data,
346 F71805F_REG_IN_HIGH(nr));
347 data->in_low[nr] = f71805f_read8(data,
348 F71805F_REG_IN_LOW(nr));
349 }
350 for (nr = 0; nr < 3; nr++) {
Jean Delvare6b14a542006-12-12 18:18:26 +0100351 data->fan_low[nr] = f71805f_read16(data,
352 F71805F_REG_FAN_LOW(nr));
Jean Delvare315c7112006-12-12 18:18:27 +0100353 data->fan_target[nr] = f71805f_read16(data,
354 F71805F_REG_FAN_TARGET(nr));
Jean Delvare6e2bc172006-12-12 18:18:27 +0100355 data->pwm_freq[nr] = f71805f_read8(data,
356 F71805F_REG_PWM_FREQ(nr));
Jean Delvaree53004e2006-01-09 23:26:14 +0100357 }
358 for (nr = 0; nr < 3; nr++) {
359 data->temp_high[nr] = f71805f_read8(data,
360 F71805F_REG_TEMP_HIGH(nr));
361 data->temp_hyst[nr] = f71805f_read8(data,
362 F71805F_REG_TEMP_HYST(nr));
363 }
364 data->temp_mode = f71805f_read8(data, F71805F_REG_TEMP_MODE);
Phil Endecottaba50732007-06-29 09:19:14 +0200365 for (nr = 0; nr < 3; nr++) {
366 for (apnr = 0; apnr < 3; apnr++) {
367 data->auto_points[nr].temp[apnr] =
368 f71805f_read8(data,
369 F71805F_REG_PWM_AUTO_POINT_TEMP(nr,
370 apnr));
371 data->auto_points[nr].fan[apnr] =
372 f71805f_read16(data,
373 F71805F_REG_PWM_AUTO_POINT_FAN(nr,
374 apnr));
375 }
376 }
Jean Delvaree53004e2006-01-09 23:26:14 +0100377
378 data->last_limits = jiffies;
379 }
380
381 /* Measurement registers cache is refreshed after 1 second */
382 if (time_after(jiffies, data->last_updated + HZ)
383 || !data->valid) {
Jean Delvare51c997d2006-12-12 18:18:29 +0100384 for (nr = 0; nr < 11; nr++) {
385 if (!(data->has_in & (1 << nr)))
386 continue;
Jean Delvaree53004e2006-01-09 23:26:14 +0100387 data->in[nr] = f71805f_read8(data,
388 F71805F_REG_IN(nr));
389 }
390 for (nr = 0; nr < 3; nr++) {
Jean Delvare6b14a542006-12-12 18:18:26 +0100391 data->fan[nr] = f71805f_read16(data,
392 F71805F_REG_FAN(nr));
Jean Delvare95e35312006-12-12 18:18:26 +0100393 data->fan_ctrl[nr] = f71805f_read8(data,
394 F71805F_REG_FAN_CTRL(nr));
395 data->pwm[nr] = f71805f_read8(data,
396 F71805F_REG_PWM_DUTY(nr));
Jean Delvaree53004e2006-01-09 23:26:14 +0100397 }
398 for (nr = 0; nr < 3; nr++) {
399 data->temp[nr] = f71805f_read8(data,
400 F71805F_REG_TEMP(nr));
401 }
Jean Delvare2d457712006-09-24 20:52:15 +0200402 data->alarms = f71805f_read8(data, F71805F_REG_STATUS(0))
403 + (f71805f_read8(data, F71805F_REG_STATUS(1)) << 8)
404 + (f71805f_read8(data, F71805F_REG_STATUS(2)) << 16);
Jean Delvaree53004e2006-01-09 23:26:14 +0100405
406 data->last_updated = jiffies;
407 data->valid = 1;
408 }
409
Jean Delvaref0819182006-01-18 23:20:53 +0100410 mutex_unlock(&data->update_lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100411
412 return data;
413}
414
415/*
416 * Sysfs interface
417 */
418
419static ssize_t show_in0(struct device *dev, struct device_attribute *devattr,
420 char *buf)
421{
422 struct f71805f_data *data = f71805f_update_device(dev);
Jean Delvare51c997d2006-12-12 18:18:29 +0100423 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
424 int nr = attr->index;
Jean Delvaree53004e2006-01-09 23:26:14 +0100425
Jean Delvare51c997d2006-12-12 18:18:29 +0100426 return sprintf(buf, "%ld\n", in0_from_reg(data->in[nr]));
Jean Delvaree53004e2006-01-09 23:26:14 +0100427}
428
429static ssize_t show_in0_max(struct device *dev, struct device_attribute
430 *devattr, char *buf)
431{
432 struct f71805f_data *data = f71805f_update_device(dev);
Jean Delvare51c997d2006-12-12 18:18:29 +0100433 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
434 int nr = attr->index;
Jean Delvaree53004e2006-01-09 23:26:14 +0100435
Jean Delvare51c997d2006-12-12 18:18:29 +0100436 return sprintf(buf, "%ld\n", in0_from_reg(data->in_high[nr]));
Jean Delvaree53004e2006-01-09 23:26:14 +0100437}
438
439static ssize_t show_in0_min(struct device *dev, struct device_attribute
440 *devattr, char *buf)
441{
442 struct f71805f_data *data = f71805f_update_device(dev);
Jean Delvare51c997d2006-12-12 18:18:29 +0100443 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
444 int nr = attr->index;
Jean Delvaree53004e2006-01-09 23:26:14 +0100445
Jean Delvare51c997d2006-12-12 18:18:29 +0100446 return sprintf(buf, "%ld\n", in0_from_reg(data->in_low[nr]));
Jean Delvaree53004e2006-01-09 23:26:14 +0100447}
448
449static ssize_t set_in0_max(struct device *dev, struct device_attribute
450 *devattr, const char *buf, size_t count)
451{
452 struct f71805f_data *data = dev_get_drvdata(dev);
Jean Delvare51c997d2006-12-12 18:18:29 +0100453 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
454 int nr = attr->index;
Jean Delvaree53004e2006-01-09 23:26:14 +0100455 long val = simple_strtol(buf, NULL, 10);
456
Jean Delvaref0819182006-01-18 23:20:53 +0100457 mutex_lock(&data->update_lock);
Jean Delvare51c997d2006-12-12 18:18:29 +0100458 data->in_high[nr] = in0_to_reg(val);
459 f71805f_write8(data, F71805F_REG_IN_HIGH(nr), data->in_high[nr]);
Jean Delvaref0819182006-01-18 23:20:53 +0100460 mutex_unlock(&data->update_lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100461
462 return count;
463}
464
465static ssize_t set_in0_min(struct device *dev, struct device_attribute
466 *devattr, const char *buf, size_t count)
467{
468 struct f71805f_data *data = dev_get_drvdata(dev);
Jean Delvare51c997d2006-12-12 18:18:29 +0100469 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
470 int nr = attr->index;
Jean Delvaree53004e2006-01-09 23:26:14 +0100471 long val = simple_strtol(buf, NULL, 10);
472
Jean Delvaref0819182006-01-18 23:20:53 +0100473 mutex_lock(&data->update_lock);
Jean Delvare51c997d2006-12-12 18:18:29 +0100474 data->in_low[nr] = in0_to_reg(val);
475 f71805f_write8(data, F71805F_REG_IN_LOW(nr), data->in_low[nr]);
Jean Delvaref0819182006-01-18 23:20:53 +0100476 mutex_unlock(&data->update_lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100477
478 return count;
479}
480
Jean Delvaree53004e2006-01-09 23:26:14 +0100481static ssize_t show_in(struct device *dev, struct device_attribute *devattr,
482 char *buf)
483{
484 struct f71805f_data *data = f71805f_update_device(dev);
485 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
486 int nr = attr->index;
487
488 return sprintf(buf, "%ld\n", in_from_reg(data->in[nr]));
489}
490
491static ssize_t show_in_max(struct device *dev, struct device_attribute
492 *devattr, char *buf)
493{
494 struct f71805f_data *data = f71805f_update_device(dev);
495 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
496 int nr = attr->index;
497
498 return sprintf(buf, "%ld\n", in_from_reg(data->in_high[nr]));
499}
500
501static ssize_t show_in_min(struct device *dev, struct device_attribute
502 *devattr, char *buf)
503{
504 struct f71805f_data *data = f71805f_update_device(dev);
505 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
506 int nr = attr->index;
507
508 return sprintf(buf, "%ld\n", in_from_reg(data->in_low[nr]));
509}
510
511static ssize_t set_in_max(struct device *dev, struct device_attribute
512 *devattr, const char *buf, size_t count)
513{
514 struct f71805f_data *data = dev_get_drvdata(dev);
515 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
516 int nr = attr->index;
517 long val = simple_strtol(buf, NULL, 10);
518
Jean Delvaref0819182006-01-18 23:20:53 +0100519 mutex_lock(&data->update_lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100520 data->in_high[nr] = in_to_reg(val);
521 f71805f_write8(data, F71805F_REG_IN_HIGH(nr), data->in_high[nr]);
Jean Delvaref0819182006-01-18 23:20:53 +0100522 mutex_unlock(&data->update_lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100523
524 return count;
525}
526
527static ssize_t set_in_min(struct device *dev, struct device_attribute
528 *devattr, const char *buf, size_t count)
529{
530 struct f71805f_data *data = dev_get_drvdata(dev);
531 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
532 int nr = attr->index;
533 long val = simple_strtol(buf, NULL, 10);
534
Jean Delvaref0819182006-01-18 23:20:53 +0100535 mutex_lock(&data->update_lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100536 data->in_low[nr] = in_to_reg(val);
537 f71805f_write8(data, F71805F_REG_IN_LOW(nr), data->in_low[nr]);
Jean Delvaref0819182006-01-18 23:20:53 +0100538 mutex_unlock(&data->update_lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100539
540 return count;
541}
542
Jean Delvaree53004e2006-01-09 23:26:14 +0100543static ssize_t show_fan(struct device *dev, struct device_attribute *devattr,
544 char *buf)
545{
546 struct f71805f_data *data = f71805f_update_device(dev);
547 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
548 int nr = attr->index;
549
550 return sprintf(buf, "%ld\n", fan_from_reg(data->fan[nr]));
551}
552
553static ssize_t show_fan_min(struct device *dev, struct device_attribute
554 *devattr, char *buf)
555{
556 struct f71805f_data *data = f71805f_update_device(dev);
557 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
558 int nr = attr->index;
559
560 return sprintf(buf, "%ld\n", fan_from_reg(data->fan_low[nr]));
561}
562
Jean Delvare315c7112006-12-12 18:18:27 +0100563static ssize_t show_fan_target(struct device *dev, struct device_attribute
564 *devattr, char *buf)
565{
566 struct f71805f_data *data = f71805f_update_device(dev);
567 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
568 int nr = attr->index;
569
570 return sprintf(buf, "%ld\n", fan_from_reg(data->fan_target[nr]));
571}
572
Jean Delvaree53004e2006-01-09 23:26:14 +0100573static ssize_t set_fan_min(struct device *dev, struct device_attribute
574 *devattr, const char *buf, size_t count)
575{
576 struct f71805f_data *data = dev_get_drvdata(dev);
577 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
578 int nr = attr->index;
579 long val = simple_strtol(buf, NULL, 10);
580
Jean Delvaref0819182006-01-18 23:20:53 +0100581 mutex_lock(&data->update_lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100582 data->fan_low[nr] = fan_to_reg(val);
583 f71805f_write16(data, F71805F_REG_FAN_LOW(nr), data->fan_low[nr]);
Jean Delvaref0819182006-01-18 23:20:53 +0100584 mutex_unlock(&data->update_lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100585
586 return count;
587}
588
Jean Delvare315c7112006-12-12 18:18:27 +0100589static ssize_t set_fan_target(struct device *dev, struct device_attribute
590 *devattr, const char *buf, size_t count)
591{
592 struct f71805f_data *data = dev_get_drvdata(dev);
593 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
594 int nr = attr->index;
595 long val = simple_strtol(buf, NULL, 10);
596
597 mutex_lock(&data->update_lock);
598 data->fan_target[nr] = fan_to_reg(val);
599 f71805f_write16(data, F71805F_REG_FAN_TARGET(nr),
600 data->fan_target[nr]);
601 mutex_unlock(&data->update_lock);
602
603 return count;
604}
605
Jean Delvare95e35312006-12-12 18:18:26 +0100606static ssize_t show_pwm(struct device *dev, struct device_attribute *devattr,
607 char *buf)
608{
609 struct f71805f_data *data = f71805f_update_device(dev);
610 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
611 int nr = attr->index;
612
613 return sprintf(buf, "%d\n", (int)data->pwm[nr]);
614}
615
616static ssize_t show_pwm_enable(struct device *dev, struct device_attribute
617 *devattr, char *buf)
618{
619 struct f71805f_data *data = f71805f_update_device(dev);
620 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
621 int nr = attr->index;
622 int mode;
623
624 switch (data->fan_ctrl[nr] & FAN_CTRL_MODE_MASK) {
625 case FAN_CTRL_MODE_SPEED:
626 mode = 3;
627 break;
628 case FAN_CTRL_MODE_TEMPERATURE:
629 mode = 2;
630 break;
631 default: /* MANUAL */
632 mode = 1;
633 }
634
635 return sprintf(buf, "%d\n", mode);
636}
637
Jean Delvare6e2bc172006-12-12 18:18:27 +0100638static ssize_t show_pwm_freq(struct device *dev, struct device_attribute
639 *devattr, char *buf)
640{
641 struct f71805f_data *data = f71805f_update_device(dev);
642 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
643 int nr = attr->index;
644
645 return sprintf(buf, "%lu\n", pwm_freq_from_reg(data->pwm_freq[nr]));
646}
647
Jean Delvaree1967832006-12-12 18:18:27 +0100648static ssize_t show_pwm_mode(struct device *dev, struct device_attribute
649 *devattr, char *buf)
650{
651 struct f71805f_data *data = f71805f_update_device(dev);
652 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
653 int nr = attr->index;
654
655 return sprintf(buf, "%d\n", pwm_mode_from_reg(data->fan_ctrl[nr]));
656}
657
Jean Delvare95e35312006-12-12 18:18:26 +0100658static ssize_t set_pwm(struct device *dev, struct device_attribute *devattr,
659 const char *buf, size_t count)
660{
661 struct f71805f_data *data = dev_get_drvdata(dev);
662 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
663 int nr = attr->index;
664 unsigned long val = simple_strtoul(buf, NULL, 10);
665
666 if (val > 255)
667 return -EINVAL;
668
669 mutex_lock(&data->update_lock);
670 data->pwm[nr] = val;
671 f71805f_write8(data, F71805F_REG_PWM_DUTY(nr), data->pwm[nr]);
672 mutex_unlock(&data->update_lock);
673
674 return count;
675}
676
677static struct attribute *f71805f_attr_pwm[];
678
679static ssize_t set_pwm_enable(struct device *dev, struct device_attribute
680 *devattr, const char *buf, size_t count)
681{
682 struct f71805f_data *data = dev_get_drvdata(dev);
683 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
684 int nr = attr->index;
685 unsigned long val = simple_strtoul(buf, NULL, 10);
686 u8 reg;
687
688 if (val < 1 || val > 3)
689 return -EINVAL;
690
691 if (val > 1) { /* Automatic mode, user can't set PWM value */
692 if (sysfs_chmod_file(&dev->kobj, f71805f_attr_pwm[nr],
693 S_IRUGO))
694 dev_dbg(dev, "chmod -w pwm%d failed\n", nr + 1);
695 }
696
697 mutex_lock(&data->update_lock);
698 reg = f71805f_read8(data, F71805F_REG_FAN_CTRL(nr))
699 & ~FAN_CTRL_MODE_MASK;
700 switch (val) {
701 case 1:
702 reg |= FAN_CTRL_MODE_MANUAL;
703 break;
704 case 2:
705 reg |= FAN_CTRL_MODE_TEMPERATURE;
706 break;
707 case 3:
708 reg |= FAN_CTRL_MODE_SPEED;
709 break;
710 }
711 data->fan_ctrl[nr] = reg;
712 f71805f_write8(data, F71805F_REG_FAN_CTRL(nr), reg);
713 mutex_unlock(&data->update_lock);
714
715 if (val == 1) { /* Manual mode, user can set PWM value */
716 if (sysfs_chmod_file(&dev->kobj, f71805f_attr_pwm[nr],
717 S_IRUGO | S_IWUSR))
718 dev_dbg(dev, "chmod +w pwm%d failed\n", nr + 1);
719 }
720
721 return count;
722}
723
Jean Delvare6e2bc172006-12-12 18:18:27 +0100724static ssize_t set_pwm_freq(struct device *dev, struct device_attribute
725 *devattr, const char *buf, size_t count)
726{
727 struct f71805f_data *data = dev_get_drvdata(dev);
728 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
729 int nr = attr->index;
730 unsigned long val = simple_strtoul(buf, NULL, 10);
731
732 mutex_lock(&data->update_lock);
733 data->pwm_freq[nr] = pwm_freq_to_reg(val);
734 f71805f_write8(data, F71805F_REG_PWM_FREQ(nr), data->pwm_freq[nr]);
735 mutex_unlock(&data->update_lock);
736
737 return count;
738}
739
Phil Endecottaba50732007-06-29 09:19:14 +0200740static ssize_t show_pwm_auto_point_temp(struct device *dev,
741 struct device_attribute *devattr,
742 char* buf)
743{
744 struct f71805f_data *data = dev_get_drvdata(dev);
745 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
746 int pwmnr = attr->nr;
747 int apnr = attr->index;
748
749 return sprintf(buf, "%ld\n",
750 temp_from_reg(data->auto_points[pwmnr].temp[apnr]));
751}
752
753static ssize_t set_pwm_auto_point_temp(struct device *dev,
754 struct device_attribute *devattr,
755 const char* buf, size_t count)
756{
757 struct f71805f_data *data = dev_get_drvdata(dev);
758 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
759 int pwmnr = attr->nr;
760 int apnr = attr->index;
761 unsigned long val = simple_strtol(buf, NULL, 10);
762
763 mutex_lock(&data->update_lock);
764 data->auto_points[pwmnr].temp[apnr] = temp_to_reg(val);
765 f71805f_write8(data, F71805F_REG_PWM_AUTO_POINT_TEMP(pwmnr, apnr),
766 data->auto_points[pwmnr].temp[apnr]);
767 mutex_unlock(&data->update_lock);
768
769 return count;
770}
771
772static ssize_t show_pwm_auto_point_fan(struct device *dev,
773 struct device_attribute *devattr,
774 char* buf)
775{
776 struct f71805f_data *data = dev_get_drvdata(dev);
777 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
778 int pwmnr = attr->nr;
779 int apnr = attr->index;
780
781 return sprintf(buf, "%ld\n",
782 fan_from_reg(data->auto_points[pwmnr].fan[apnr]));
783}
784
785static ssize_t set_pwm_auto_point_fan(struct device *dev,
786 struct device_attribute *devattr,
787 const char* buf, size_t count)
788{
789 struct f71805f_data *data = dev_get_drvdata(dev);
790 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
791 int pwmnr = attr->nr;
792 int apnr = attr->index;
793 unsigned long val = simple_strtoul(buf, NULL, 10);
794
795 mutex_lock(&data->update_lock);
796 data->auto_points[pwmnr].fan[apnr] = fan_to_reg(val);
797 f71805f_write16(data, F71805F_REG_PWM_AUTO_POINT_FAN(pwmnr, apnr),
798 data->auto_points[pwmnr].fan[apnr]);
799 mutex_unlock(&data->update_lock);
800
801 return count;
802}
803
Jean Delvaree53004e2006-01-09 23:26:14 +0100804static ssize_t show_temp(struct device *dev, struct device_attribute *devattr,
805 char *buf)
806{
807 struct f71805f_data *data = f71805f_update_device(dev);
808 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
809 int nr = attr->index;
810
811 return sprintf(buf, "%ld\n", temp_from_reg(data->temp[nr]));
812}
813
814static ssize_t show_temp_max(struct device *dev, struct device_attribute
815 *devattr, char *buf)
816{
817 struct f71805f_data *data = f71805f_update_device(dev);
818 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
819 int nr = attr->index;
820
821 return sprintf(buf, "%ld\n", temp_from_reg(data->temp_high[nr]));
822}
823
824static ssize_t show_temp_hyst(struct device *dev, struct device_attribute
825 *devattr, char *buf)
826{
827 struct f71805f_data *data = f71805f_update_device(dev);
828 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
829 int nr = attr->index;
830
831 return sprintf(buf, "%ld\n", temp_from_reg(data->temp_hyst[nr]));
832}
833
834static ssize_t show_temp_type(struct device *dev, struct device_attribute
835 *devattr, char *buf)
836{
837 struct f71805f_data *data = f71805f_update_device(dev);
838 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
839 int nr = attr->index;
840
841 /* 3 is diode, 4 is thermistor */
842 return sprintf(buf, "%u\n", (data->temp_mode & (1 << nr)) ? 3 : 4);
843}
844
845static ssize_t set_temp_max(struct device *dev, struct device_attribute
846 *devattr, const char *buf, size_t count)
847{
848 struct f71805f_data *data = dev_get_drvdata(dev);
849 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
850 int nr = attr->index;
851 long val = simple_strtol(buf, NULL, 10);
852
Jean Delvaref0819182006-01-18 23:20:53 +0100853 mutex_lock(&data->update_lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100854 data->temp_high[nr] = temp_to_reg(val);
855 f71805f_write8(data, F71805F_REG_TEMP_HIGH(nr), data->temp_high[nr]);
Jean Delvaref0819182006-01-18 23:20:53 +0100856 mutex_unlock(&data->update_lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100857
858 return count;
859}
860
861static ssize_t set_temp_hyst(struct device *dev, struct device_attribute
862 *devattr, const char *buf, size_t count)
863{
864 struct f71805f_data *data = dev_get_drvdata(dev);
865 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
866 int nr = attr->index;
867 long val = simple_strtol(buf, NULL, 10);
868
Jean Delvaref0819182006-01-18 23:20:53 +0100869 mutex_lock(&data->update_lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100870 data->temp_hyst[nr] = temp_to_reg(val);
871 f71805f_write8(data, F71805F_REG_TEMP_HYST(nr), data->temp_hyst[nr]);
Jean Delvaref0819182006-01-18 23:20:53 +0100872 mutex_unlock(&data->update_lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100873
874 return count;
875}
876
Jean Delvaree53004e2006-01-09 23:26:14 +0100877static ssize_t show_alarms_in(struct device *dev, struct device_attribute
878 *devattr, char *buf)
879{
880 struct f71805f_data *data = f71805f_update_device(dev);
881
Jean Delvare51c997d2006-12-12 18:18:29 +0100882 return sprintf(buf, "%lu\n", data->alarms & 0x7ff);
Jean Delvaree53004e2006-01-09 23:26:14 +0100883}
884
885static ssize_t show_alarms_fan(struct device *dev, struct device_attribute
886 *devattr, char *buf)
887{
888 struct f71805f_data *data = f71805f_update_device(dev);
889
Jean Delvare2d457712006-09-24 20:52:15 +0200890 return sprintf(buf, "%lu\n", (data->alarms >> 16) & 0x07);
Jean Delvaree53004e2006-01-09 23:26:14 +0100891}
892
893static ssize_t show_alarms_temp(struct device *dev, struct device_attribute
894 *devattr, char *buf)
895{
896 struct f71805f_data *data = f71805f_update_device(dev);
897
Jean Delvare2d457712006-09-24 20:52:15 +0200898 return sprintf(buf, "%lu\n", (data->alarms >> 11) & 0x07);
899}
900
901static ssize_t show_alarm(struct device *dev, struct device_attribute
902 *devattr, char *buf)
903{
904 struct f71805f_data *data = f71805f_update_device(dev);
905 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
906 int bitnr = attr->index;
907
908 return sprintf(buf, "%lu\n", (data->alarms >> bitnr) & 1);
Jean Delvaree53004e2006-01-09 23:26:14 +0100909}
910
Jean Delvaree53004e2006-01-09 23:26:14 +0100911static ssize_t show_name(struct device *dev, struct device_attribute
912 *devattr, char *buf)
913{
914 struct f71805f_data *data = dev_get_drvdata(dev);
915
916 return sprintf(buf, "%s\n", data->name);
917}
918
Jean Delvare51c997d2006-12-12 18:18:29 +0100919static SENSOR_DEVICE_ATTR(in0_input, S_IRUGO, show_in0, NULL, 0);
920static SENSOR_DEVICE_ATTR(in0_max, S_IRUGO| S_IWUSR,
921 show_in0_max, set_in0_max, 0);
922static SENSOR_DEVICE_ATTR(in0_min, S_IRUGO| S_IWUSR,
923 show_in0_min, set_in0_min, 0);
Jean Delvare0e39e012006-09-24 21:16:40 +0200924static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, show_in, NULL, 1);
925static SENSOR_DEVICE_ATTR(in1_max, S_IRUGO | S_IWUSR,
926 show_in_max, set_in_max, 1);
927static SENSOR_DEVICE_ATTR(in1_min, S_IRUGO | S_IWUSR,
928 show_in_min, set_in_min, 1);
929static SENSOR_DEVICE_ATTR(in2_input, S_IRUGO, show_in, NULL, 2);
930static SENSOR_DEVICE_ATTR(in2_max, S_IRUGO | S_IWUSR,
931 show_in_max, set_in_max, 2);
932static SENSOR_DEVICE_ATTR(in2_min, S_IRUGO | S_IWUSR,
933 show_in_min, set_in_min, 2);
934static SENSOR_DEVICE_ATTR(in3_input, S_IRUGO, show_in, NULL, 3);
935static SENSOR_DEVICE_ATTR(in3_max, S_IRUGO | S_IWUSR,
936 show_in_max, set_in_max, 3);
937static SENSOR_DEVICE_ATTR(in3_min, S_IRUGO | S_IWUSR,
938 show_in_min, set_in_min, 3);
939static SENSOR_DEVICE_ATTR(in4_input, S_IRUGO, show_in, NULL, 4);
940static SENSOR_DEVICE_ATTR(in4_max, S_IRUGO | S_IWUSR,
941 show_in_max, set_in_max, 4);
942static SENSOR_DEVICE_ATTR(in4_min, S_IRUGO | S_IWUSR,
943 show_in_min, set_in_min, 4);
944static SENSOR_DEVICE_ATTR(in5_input, S_IRUGO, show_in, NULL, 5);
945static SENSOR_DEVICE_ATTR(in5_max, S_IRUGO | S_IWUSR,
946 show_in_max, set_in_max, 5);
947static SENSOR_DEVICE_ATTR(in5_min, S_IRUGO | S_IWUSR,
948 show_in_min, set_in_min, 5);
949static SENSOR_DEVICE_ATTR(in6_input, S_IRUGO, show_in, NULL, 6);
950static SENSOR_DEVICE_ATTR(in6_max, S_IRUGO | S_IWUSR,
951 show_in_max, set_in_max, 6);
952static SENSOR_DEVICE_ATTR(in6_min, S_IRUGO | S_IWUSR,
953 show_in_min, set_in_min, 6);
954static SENSOR_DEVICE_ATTR(in7_input, S_IRUGO, show_in, NULL, 7);
955static SENSOR_DEVICE_ATTR(in7_max, S_IRUGO | S_IWUSR,
956 show_in_max, set_in_max, 7);
957static SENSOR_DEVICE_ATTR(in7_min, S_IRUGO | S_IWUSR,
958 show_in_min, set_in_min, 7);
959static SENSOR_DEVICE_ATTR(in8_input, S_IRUGO, show_in, NULL, 8);
960static SENSOR_DEVICE_ATTR(in8_max, S_IRUGO | S_IWUSR,
961 show_in_max, set_in_max, 8);
962static SENSOR_DEVICE_ATTR(in8_min, S_IRUGO | S_IWUSR,
963 show_in_min, set_in_min, 8);
Jean Delvare51c997d2006-12-12 18:18:29 +0100964static SENSOR_DEVICE_ATTR(in9_input, S_IRUGO, show_in0, NULL, 9);
965static SENSOR_DEVICE_ATTR(in9_max, S_IRUGO | S_IWUSR,
966 show_in0_max, set_in0_max, 9);
967static SENSOR_DEVICE_ATTR(in9_min, S_IRUGO | S_IWUSR,
968 show_in0_min, set_in0_min, 9);
969static SENSOR_DEVICE_ATTR(in10_input, S_IRUGO, show_in0, NULL, 10);
970static SENSOR_DEVICE_ATTR(in10_max, S_IRUGO | S_IWUSR,
971 show_in0_max, set_in0_max, 10);
972static SENSOR_DEVICE_ATTR(in10_min, S_IRUGO | S_IWUSR,
973 show_in0_min, set_in0_min, 10);
Jean Delvare0e39e012006-09-24 21:16:40 +0200974
975static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0);
976static SENSOR_DEVICE_ATTR(fan1_min, S_IRUGO | S_IWUSR,
977 show_fan_min, set_fan_min, 0);
Jean Delvare315c7112006-12-12 18:18:27 +0100978static SENSOR_DEVICE_ATTR(fan1_target, S_IRUGO | S_IWUSR,
979 show_fan_target, set_fan_target, 0);
Jean Delvare0e39e012006-09-24 21:16:40 +0200980static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, show_fan, NULL, 1);
981static SENSOR_DEVICE_ATTR(fan2_min, S_IRUGO | S_IWUSR,
982 show_fan_min, set_fan_min, 1);
Jean Delvare315c7112006-12-12 18:18:27 +0100983static SENSOR_DEVICE_ATTR(fan2_target, S_IRUGO | S_IWUSR,
984 show_fan_target, set_fan_target, 1);
Jean Delvare0e39e012006-09-24 21:16:40 +0200985static SENSOR_DEVICE_ATTR(fan3_input, S_IRUGO, show_fan, NULL, 2);
986static SENSOR_DEVICE_ATTR(fan3_min, S_IRUGO | S_IWUSR,
987 show_fan_min, set_fan_min, 2);
Jean Delvare315c7112006-12-12 18:18:27 +0100988static SENSOR_DEVICE_ATTR(fan3_target, S_IRUGO | S_IWUSR,
989 show_fan_target, set_fan_target, 2);
Jean Delvare0e39e012006-09-24 21:16:40 +0200990
991static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
992static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO | S_IWUSR,
993 show_temp_max, set_temp_max, 0);
994static SENSOR_DEVICE_ATTR(temp1_max_hyst, S_IRUGO | S_IWUSR,
995 show_temp_hyst, set_temp_hyst, 0);
996static SENSOR_DEVICE_ATTR(temp1_type, S_IRUGO, show_temp_type, NULL, 0);
997static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1);
998static SENSOR_DEVICE_ATTR(temp2_max, S_IRUGO | S_IWUSR,
999 show_temp_max, set_temp_max, 1);
1000static SENSOR_DEVICE_ATTR(temp2_max_hyst, S_IRUGO | S_IWUSR,
1001 show_temp_hyst, set_temp_hyst, 1);
1002static SENSOR_DEVICE_ATTR(temp2_type, S_IRUGO, show_temp_type, NULL, 1);
1003static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, show_temp, NULL, 2);
1004static SENSOR_DEVICE_ATTR(temp3_max, S_IRUGO | S_IWUSR,
1005 show_temp_max, set_temp_max, 2);
1006static SENSOR_DEVICE_ATTR(temp3_max_hyst, S_IRUGO | S_IWUSR,
1007 show_temp_hyst, set_temp_hyst, 2);
1008static SENSOR_DEVICE_ATTR(temp3_type, S_IRUGO, show_temp_type, NULL, 2);
1009
Jean Delvare95e35312006-12-12 18:18:26 +01001010/* pwm (value) files are created read-only, write permission is
1011 then added or removed dynamically as needed */
1012static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO, show_pwm, set_pwm, 0);
1013static SENSOR_DEVICE_ATTR(pwm1_enable, S_IRUGO | S_IWUSR,
1014 show_pwm_enable, set_pwm_enable, 0);
Jean Delvare6e2bc172006-12-12 18:18:27 +01001015static SENSOR_DEVICE_ATTR(pwm1_freq, S_IRUGO | S_IWUSR,
1016 show_pwm_freq, set_pwm_freq, 0);
Jean Delvaree1967832006-12-12 18:18:27 +01001017static SENSOR_DEVICE_ATTR(pwm1_mode, S_IRUGO, show_pwm_mode, NULL, 0);
Jean Delvare95e35312006-12-12 18:18:26 +01001018static SENSOR_DEVICE_ATTR(pwm2, S_IRUGO, show_pwm, set_pwm, 1);
1019static SENSOR_DEVICE_ATTR(pwm2_enable, S_IRUGO | S_IWUSR,
1020 show_pwm_enable, set_pwm_enable, 1);
Jean Delvare6e2bc172006-12-12 18:18:27 +01001021static SENSOR_DEVICE_ATTR(pwm2_freq, S_IRUGO | S_IWUSR,
1022 show_pwm_freq, set_pwm_freq, 1);
Jean Delvaree1967832006-12-12 18:18:27 +01001023static SENSOR_DEVICE_ATTR(pwm2_mode, S_IRUGO, show_pwm_mode, NULL, 1);
Jean Delvare95e35312006-12-12 18:18:26 +01001024static SENSOR_DEVICE_ATTR(pwm3, S_IRUGO, show_pwm, set_pwm, 2);
1025static SENSOR_DEVICE_ATTR(pwm3_enable, S_IRUGO | S_IWUSR,
1026 show_pwm_enable, set_pwm_enable, 2);
Jean Delvare6e2bc172006-12-12 18:18:27 +01001027static SENSOR_DEVICE_ATTR(pwm3_freq, S_IRUGO | S_IWUSR,
1028 show_pwm_freq, set_pwm_freq, 2);
Jean Delvaree1967832006-12-12 18:18:27 +01001029static SENSOR_DEVICE_ATTR(pwm3_mode, S_IRUGO, show_pwm_mode, NULL, 2);
Jean Delvare95e35312006-12-12 18:18:26 +01001030
Phil Endecottaba50732007-06-29 09:19:14 +02001031static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_temp, S_IRUGO | S_IWUSR,
1032 show_pwm_auto_point_temp, set_pwm_auto_point_temp,
1033 0, 0);
1034static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_fan, S_IRUGO | S_IWUSR,
1035 show_pwm_auto_point_fan, set_pwm_auto_point_fan,
1036 0, 0);
1037static SENSOR_DEVICE_ATTR_2(pwm1_auto_point2_temp, S_IRUGO | S_IWUSR,
1038 show_pwm_auto_point_temp, set_pwm_auto_point_temp,
1039 0, 1);
1040static SENSOR_DEVICE_ATTR_2(pwm1_auto_point2_fan, S_IRUGO | S_IWUSR,
1041 show_pwm_auto_point_fan, set_pwm_auto_point_fan,
1042 0, 1);
1043static SENSOR_DEVICE_ATTR_2(pwm1_auto_point3_temp, S_IRUGO | S_IWUSR,
1044 show_pwm_auto_point_temp, set_pwm_auto_point_temp,
1045 0, 2);
1046static SENSOR_DEVICE_ATTR_2(pwm1_auto_point3_fan, S_IRUGO | S_IWUSR,
1047 show_pwm_auto_point_fan, set_pwm_auto_point_fan,
1048 0, 2);
1049
1050static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_temp, S_IRUGO | S_IWUSR,
1051 show_pwm_auto_point_temp, set_pwm_auto_point_temp,
1052 1, 0);
1053static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_fan, S_IRUGO | S_IWUSR,
1054 show_pwm_auto_point_fan, set_pwm_auto_point_fan,
1055 1, 0);
1056static SENSOR_DEVICE_ATTR_2(pwm2_auto_point2_temp, S_IRUGO | S_IWUSR,
1057 show_pwm_auto_point_temp, set_pwm_auto_point_temp,
1058 1, 1);
1059static SENSOR_DEVICE_ATTR_2(pwm2_auto_point2_fan, S_IRUGO | S_IWUSR,
1060 show_pwm_auto_point_fan, set_pwm_auto_point_fan,
1061 1, 1);
1062static SENSOR_DEVICE_ATTR_2(pwm2_auto_point3_temp, S_IRUGO | S_IWUSR,
1063 show_pwm_auto_point_temp, set_pwm_auto_point_temp,
1064 1, 2);
1065static SENSOR_DEVICE_ATTR_2(pwm2_auto_point3_fan, S_IRUGO | S_IWUSR,
1066 show_pwm_auto_point_fan, set_pwm_auto_point_fan,
1067 1, 2);
1068
1069static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_temp, S_IRUGO | S_IWUSR,
1070 show_pwm_auto_point_temp, set_pwm_auto_point_temp,
1071 2, 0);
1072static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_fan, S_IRUGO | S_IWUSR,
1073 show_pwm_auto_point_fan, set_pwm_auto_point_fan,
1074 2, 0);
1075static SENSOR_DEVICE_ATTR_2(pwm3_auto_point2_temp, S_IRUGO | S_IWUSR,
1076 show_pwm_auto_point_temp, set_pwm_auto_point_temp,
1077 2, 1);
1078static SENSOR_DEVICE_ATTR_2(pwm3_auto_point2_fan, S_IRUGO | S_IWUSR,
1079 show_pwm_auto_point_fan, set_pwm_auto_point_fan,
1080 2, 1);
1081static SENSOR_DEVICE_ATTR_2(pwm3_auto_point3_temp, S_IRUGO | S_IWUSR,
1082 show_pwm_auto_point_temp, set_pwm_auto_point_temp,
1083 2, 2);
1084static SENSOR_DEVICE_ATTR_2(pwm3_auto_point3_fan, S_IRUGO | S_IWUSR,
1085 show_pwm_auto_point_fan, set_pwm_auto_point_fan,
1086 2, 2);
1087
Jean Delvare0e39e012006-09-24 21:16:40 +02001088static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0);
1089static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1);
1090static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2);
1091static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3);
1092static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 4);
1093static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 5);
1094static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 6);
1095static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 7);
1096static SENSOR_DEVICE_ATTR(in8_alarm, S_IRUGO, show_alarm, NULL, 8);
Jean Delvare51c997d2006-12-12 18:18:29 +01001097static SENSOR_DEVICE_ATTR(in9_alarm, S_IRUGO, show_alarm, NULL, 9);
1098static SENSOR_DEVICE_ATTR(in10_alarm, S_IRUGO, show_alarm, NULL, 10);
Jean Delvare0e39e012006-09-24 21:16:40 +02001099static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 11);
1100static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 12);
1101static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 13);
1102static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 16);
1103static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 17);
1104static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 18);
1105static DEVICE_ATTR(alarms_in, S_IRUGO, show_alarms_in, NULL);
1106static DEVICE_ATTR(alarms_fan, S_IRUGO, show_alarms_fan, NULL);
1107static DEVICE_ATTR(alarms_temp, S_IRUGO, show_alarms_temp, NULL);
1108
1109static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
1110
1111static struct attribute *f71805f_attributes[] = {
Jean Delvare51c997d2006-12-12 18:18:29 +01001112 &sensor_dev_attr_in0_input.dev_attr.attr,
1113 &sensor_dev_attr_in0_max.dev_attr.attr,
1114 &sensor_dev_attr_in0_min.dev_attr.attr,
Jean Delvare0e39e012006-09-24 21:16:40 +02001115 &sensor_dev_attr_in1_input.dev_attr.attr,
1116 &sensor_dev_attr_in1_max.dev_attr.attr,
1117 &sensor_dev_attr_in1_min.dev_attr.attr,
1118 &sensor_dev_attr_in2_input.dev_attr.attr,
1119 &sensor_dev_attr_in2_max.dev_attr.attr,
1120 &sensor_dev_attr_in2_min.dev_attr.attr,
1121 &sensor_dev_attr_in3_input.dev_attr.attr,
1122 &sensor_dev_attr_in3_max.dev_attr.attr,
1123 &sensor_dev_attr_in3_min.dev_attr.attr,
Jean Delvare0e39e012006-09-24 21:16:40 +02001124 &sensor_dev_attr_in5_input.dev_attr.attr,
1125 &sensor_dev_attr_in5_max.dev_attr.attr,
1126 &sensor_dev_attr_in5_min.dev_attr.attr,
1127 &sensor_dev_attr_in6_input.dev_attr.attr,
1128 &sensor_dev_attr_in6_max.dev_attr.attr,
1129 &sensor_dev_attr_in6_min.dev_attr.attr,
1130 &sensor_dev_attr_in7_input.dev_attr.attr,
1131 &sensor_dev_attr_in7_max.dev_attr.attr,
1132 &sensor_dev_attr_in7_min.dev_attr.attr,
Jean Delvare0e39e012006-09-24 21:16:40 +02001133
Jean Delvarec7176cb2006-12-12 18:18:29 +01001134 &sensor_dev_attr_fan1_input.dev_attr.attr,
1135 &sensor_dev_attr_fan1_min.dev_attr.attr,
1136 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
1137 &sensor_dev_attr_fan1_target.dev_attr.attr,
1138 &sensor_dev_attr_fan2_input.dev_attr.attr,
1139 &sensor_dev_attr_fan2_min.dev_attr.attr,
1140 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
1141 &sensor_dev_attr_fan2_target.dev_attr.attr,
1142 &sensor_dev_attr_fan3_input.dev_attr.attr,
1143 &sensor_dev_attr_fan3_min.dev_attr.attr,
1144 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
1145 &sensor_dev_attr_fan3_target.dev_attr.attr,
1146
1147 &sensor_dev_attr_pwm1.dev_attr.attr,
1148 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
1149 &sensor_dev_attr_pwm1_mode.dev_attr.attr,
1150 &sensor_dev_attr_pwm2.dev_attr.attr,
1151 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
1152 &sensor_dev_attr_pwm2_mode.dev_attr.attr,
1153 &sensor_dev_attr_pwm3.dev_attr.attr,
1154 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
1155 &sensor_dev_attr_pwm3_mode.dev_attr.attr,
1156
Jean Delvare0e39e012006-09-24 21:16:40 +02001157 &sensor_dev_attr_temp1_input.dev_attr.attr,
1158 &sensor_dev_attr_temp1_max.dev_attr.attr,
1159 &sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
1160 &sensor_dev_attr_temp1_type.dev_attr.attr,
1161 &sensor_dev_attr_temp2_input.dev_attr.attr,
1162 &sensor_dev_attr_temp2_max.dev_attr.attr,
1163 &sensor_dev_attr_temp2_max_hyst.dev_attr.attr,
1164 &sensor_dev_attr_temp2_type.dev_attr.attr,
1165 &sensor_dev_attr_temp3_input.dev_attr.attr,
1166 &sensor_dev_attr_temp3_max.dev_attr.attr,
1167 &sensor_dev_attr_temp3_max_hyst.dev_attr.attr,
1168 &sensor_dev_attr_temp3_type.dev_attr.attr,
1169
Phil Endecottaba50732007-06-29 09:19:14 +02001170 &sensor_dev_attr_pwm1_auto_point1_temp.dev_attr.attr,
1171 &sensor_dev_attr_pwm1_auto_point1_fan.dev_attr.attr,
1172 &sensor_dev_attr_pwm1_auto_point2_temp.dev_attr.attr,
1173 &sensor_dev_attr_pwm1_auto_point2_fan.dev_attr.attr,
1174 &sensor_dev_attr_pwm1_auto_point3_temp.dev_attr.attr,
1175 &sensor_dev_attr_pwm1_auto_point3_fan.dev_attr.attr,
1176 &sensor_dev_attr_pwm2_auto_point1_temp.dev_attr.attr,
1177 &sensor_dev_attr_pwm2_auto_point1_fan.dev_attr.attr,
1178 &sensor_dev_attr_pwm2_auto_point2_temp.dev_attr.attr,
1179 &sensor_dev_attr_pwm2_auto_point2_fan.dev_attr.attr,
1180 &sensor_dev_attr_pwm2_auto_point3_temp.dev_attr.attr,
1181 &sensor_dev_attr_pwm2_auto_point3_fan.dev_attr.attr,
1182 &sensor_dev_attr_pwm3_auto_point1_temp.dev_attr.attr,
1183 &sensor_dev_attr_pwm3_auto_point1_fan.dev_attr.attr,
1184 &sensor_dev_attr_pwm3_auto_point2_temp.dev_attr.attr,
1185 &sensor_dev_attr_pwm3_auto_point2_fan.dev_attr.attr,
1186 &sensor_dev_attr_pwm3_auto_point3_temp.dev_attr.attr,
1187 &sensor_dev_attr_pwm3_auto_point3_fan.dev_attr.attr,
1188
Jean Delvare0e39e012006-09-24 21:16:40 +02001189 &sensor_dev_attr_in0_alarm.dev_attr.attr,
1190 &sensor_dev_attr_in1_alarm.dev_attr.attr,
1191 &sensor_dev_attr_in2_alarm.dev_attr.attr,
1192 &sensor_dev_attr_in3_alarm.dev_attr.attr,
Jean Delvare0e39e012006-09-24 21:16:40 +02001193 &sensor_dev_attr_in5_alarm.dev_attr.attr,
1194 &sensor_dev_attr_in6_alarm.dev_attr.attr,
1195 &sensor_dev_attr_in7_alarm.dev_attr.attr,
Jean Delvare0e39e012006-09-24 21:16:40 +02001196 &dev_attr_alarms_in.attr,
1197 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
1198 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
1199 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
1200 &dev_attr_alarms_temp.attr,
1201 &dev_attr_alarms_fan.attr,
1202
1203 &dev_attr_name.attr,
1204 NULL
Jean Delvare2488a392006-01-09 23:29:11 +01001205};
1206
Jean Delvare0e39e012006-09-24 21:16:40 +02001207static const struct attribute_group f71805f_group = {
1208 .attrs = f71805f_attributes,
Jean Delvare2488a392006-01-09 23:29:11 +01001209};
1210
Jean Delvare51c997d2006-12-12 18:18:29 +01001211static struct attribute *f71805f_attributes_optin[4][5] = {
1212 {
1213 &sensor_dev_attr_in4_input.dev_attr.attr,
1214 &sensor_dev_attr_in4_max.dev_attr.attr,
1215 &sensor_dev_attr_in4_min.dev_attr.attr,
1216 &sensor_dev_attr_in4_alarm.dev_attr.attr,
1217 NULL
1218 }, {
1219 &sensor_dev_attr_in8_input.dev_attr.attr,
1220 &sensor_dev_attr_in8_max.dev_attr.attr,
1221 &sensor_dev_attr_in8_min.dev_attr.attr,
1222 &sensor_dev_attr_in8_alarm.dev_attr.attr,
1223 NULL
1224 }, {
1225 &sensor_dev_attr_in9_input.dev_attr.attr,
1226 &sensor_dev_attr_in9_max.dev_attr.attr,
1227 &sensor_dev_attr_in9_min.dev_attr.attr,
1228 &sensor_dev_attr_in9_alarm.dev_attr.attr,
1229 NULL
1230 }, {
1231 &sensor_dev_attr_in10_input.dev_attr.attr,
1232 &sensor_dev_attr_in10_max.dev_attr.attr,
1233 &sensor_dev_attr_in10_min.dev_attr.attr,
1234 &sensor_dev_attr_in10_alarm.dev_attr.attr,
1235 NULL
1236 }
1237};
1238
1239static const struct attribute_group f71805f_group_optin[4] = {
1240 { .attrs = f71805f_attributes_optin[0] },
1241 { .attrs = f71805f_attributes_optin[1] },
1242 { .attrs = f71805f_attributes_optin[2] },
1243 { .attrs = f71805f_attributes_optin[3] },
1244};
1245
Jean Delvaree1967832006-12-12 18:18:27 +01001246/* We don't include pwm_freq files in the arrays above, because they must be
1247 created conditionally (only if pwm_mode is 1 == PWM) */
1248static struct attribute *f71805f_attributes_pwm_freq[] = {
1249 &sensor_dev_attr_pwm1_freq.dev_attr.attr,
1250 &sensor_dev_attr_pwm2_freq.dev_attr.attr,
1251 &sensor_dev_attr_pwm3_freq.dev_attr.attr,
1252 NULL
1253};
1254
1255static const struct attribute_group f71805f_group_pwm_freq = {
1256 .attrs = f71805f_attributes_pwm_freq,
1257};
1258
Jean Delvare95e35312006-12-12 18:18:26 +01001259/* We also need an indexed access to pwmN files to toggle writability */
1260static struct attribute *f71805f_attr_pwm[] = {
1261 &sensor_dev_attr_pwm1.dev_attr.attr,
1262 &sensor_dev_attr_pwm2.dev_attr.attr,
1263 &sensor_dev_attr_pwm3.dev_attr.attr,
1264};
1265
Jean Delvaree53004e2006-01-09 23:26:14 +01001266/*
1267 * Device registration and initialization
1268 */
1269
1270static void __devinit f71805f_init_device(struct f71805f_data *data)
1271{
1272 u8 reg;
1273 int i;
1274
1275 reg = f71805f_read8(data, F71805F_REG_START);
1276 if ((reg & 0x41) != 0x01) {
1277 printk(KERN_DEBUG DRVNAME ": Starting monitoring "
1278 "operations\n");
1279 f71805f_write8(data, F71805F_REG_START, (reg | 0x01) & ~0x40);
1280 }
1281
1282 /* Fan monitoring can be disabled. If it is, we won't be polling
1283 the register values, and won't create the related sysfs files. */
1284 for (i = 0; i < 3; i++) {
Jean Delvare6b14a542006-12-12 18:18:26 +01001285 data->fan_ctrl[i] = f71805f_read8(data,
1286 F71805F_REG_FAN_CTRL(i));
Jean Delvare315c7112006-12-12 18:18:27 +01001287 /* Clear latch full bit, else "speed mode" fan speed control
1288 doesn't work */
1289 if (data->fan_ctrl[i] & FAN_CTRL_LATCH_FULL) {
1290 data->fan_ctrl[i] &= ~FAN_CTRL_LATCH_FULL;
1291 f71805f_write8(data, F71805F_REG_FAN_CTRL(i),
1292 data->fan_ctrl[i]);
1293 }
Jean Delvaree53004e2006-01-09 23:26:14 +01001294 }
1295}
1296
1297static int __devinit f71805f_probe(struct platform_device *pdev)
1298{
Jean Delvare51c997d2006-12-12 18:18:29 +01001299 struct f71805f_sio_data *sio_data = pdev->dev.platform_data;
Jean Delvaree53004e2006-01-09 23:26:14 +01001300 struct f71805f_data *data;
1301 struct resource *res;
Jean Delvare2488a392006-01-09 23:29:11 +01001302 int i, err;
Jean Delvaree53004e2006-01-09 23:26:14 +01001303
Jean Delvare51c997d2006-12-12 18:18:29 +01001304 static const char *names[] = {
1305 "f71805f",
1306 "f71872f",
1307 };
1308
Jean Delvaree53004e2006-01-09 23:26:14 +01001309 if (!(data = kzalloc(sizeof(struct f71805f_data), GFP_KERNEL))) {
1310 err = -ENOMEM;
1311 printk(KERN_ERR DRVNAME ": Out of memory\n");
1312 goto exit;
1313 }
1314
1315 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
Jean Delvarece7ee4e2007-05-08 17:21:59 +02001316 if (!request_region(res->start + ADDR_REG_OFFSET, 2, DRVNAME)) {
1317 err = -EBUSY;
1318 dev_err(&pdev->dev, "Failed to request region 0x%lx-0x%lx\n",
1319 (unsigned long)(res->start + ADDR_REG_OFFSET),
1320 (unsigned long)(res->start + ADDR_REG_OFFSET + 1));
1321 goto exit_free;
1322 }
Jean Delvaree53004e2006-01-09 23:26:14 +01001323 data->addr = res->start;
Jean Delvare51c997d2006-12-12 18:18:29 +01001324 data->name = names[sio_data->kind];
Jean Delvaref0819182006-01-18 23:20:53 +01001325 mutex_init(&data->update_lock);
Jean Delvaree53004e2006-01-09 23:26:14 +01001326
1327 platform_set_drvdata(pdev, data);
1328
Jean Delvare51c997d2006-12-12 18:18:29 +01001329 /* Some voltage inputs depend on chip model and configuration */
1330 switch (sio_data->kind) {
1331 case f71805f:
1332 data->has_in = 0x1ff;
1333 break;
1334 case f71872f:
1335 data->has_in = 0x6ef;
1336 if (sio_data->fnsel1 & 0x01)
1337 data->has_in |= (1 << 4); /* in4 */
1338 if (sio_data->fnsel1 & 0x02)
1339 data->has_in |= (1 << 8); /* in8 */
1340 break;
1341 }
1342
Jean Delvaree53004e2006-01-09 23:26:14 +01001343 /* Initialize the F71805F chip */
1344 f71805f_init_device(data);
1345
1346 /* Register sysfs interface files */
Jean Delvare0e39e012006-09-24 21:16:40 +02001347 if ((err = sysfs_create_group(&pdev->dev.kobj, &f71805f_group)))
Jean Delvarece7ee4e2007-05-08 17:21:59 +02001348 goto exit_release_region;
Jean Delvare51c997d2006-12-12 18:18:29 +01001349 if (data->has_in & (1 << 4)) { /* in4 */
1350 if ((err = sysfs_create_group(&pdev->dev.kobj,
1351 &f71805f_group_optin[0])))
1352 goto exit_remove_files;
1353 }
1354 if (data->has_in & (1 << 8)) { /* in8 */
1355 if ((err = sysfs_create_group(&pdev->dev.kobj,
1356 &f71805f_group_optin[1])))
1357 goto exit_remove_files;
1358 }
1359 if (data->has_in & (1 << 9)) { /* in9 (F71872F/FG only) */
1360 if ((err = sysfs_create_group(&pdev->dev.kobj,
1361 &f71805f_group_optin[2])))
1362 goto exit_remove_files;
1363 }
1364 if (data->has_in & (1 << 10)) { /* in9 (F71872F/FG only) */
1365 if ((err = sysfs_create_group(&pdev->dev.kobj,
1366 &f71805f_group_optin[3])))
1367 goto exit_remove_files;
1368 }
Jean Delvare0e39e012006-09-24 21:16:40 +02001369 for (i = 0; i < 3; i++) {
Jean Delvaree1967832006-12-12 18:18:27 +01001370 /* If control mode is PWM, create pwm_freq file */
1371 if (!(data->fan_ctrl[i] & FAN_CTRL_DC_MODE)) {
1372 if ((err = sysfs_create_file(&pdev->dev.kobj,
1373 f71805f_attributes_pwm_freq[i])))
1374 goto exit_remove_files;
1375 }
Jean Delvare95e35312006-12-12 18:18:26 +01001376 /* If PWM is in manual mode, add write permission */
1377 if (data->fan_ctrl[i] & FAN_CTRL_MODE_MANUAL) {
1378 if ((err = sysfs_chmod_file(&pdev->dev.kobj,
1379 f71805f_attr_pwm[i],
1380 S_IRUGO | S_IWUSR))) {
1381 dev_err(&pdev->dev, "chmod +w pwm%d failed\n",
1382 i + 1);
1383 goto exit_remove_files;
1384 }
1385 }
Jean Delvare0e39e012006-09-24 21:16:40 +02001386 }
1387
Tony Jones1beeffe2007-08-20 13:46:20 -07001388 data->hwmon_dev = hwmon_device_register(&pdev->dev);
1389 if (IS_ERR(data->hwmon_dev)) {
1390 err = PTR_ERR(data->hwmon_dev);
Jean Delvare0e39e012006-09-24 21:16:40 +02001391 dev_err(&pdev->dev, "Class registration failed (%d)\n", err);
1392 goto exit_remove_files;
Jean Delvaree53004e2006-01-09 23:26:14 +01001393 }
Jean Delvaree53004e2006-01-09 23:26:14 +01001394
1395 return 0;
1396
Jean Delvare0e39e012006-09-24 21:16:40 +02001397exit_remove_files:
1398 sysfs_remove_group(&pdev->dev.kobj, &f71805f_group);
Jean Delvare51c997d2006-12-12 18:18:29 +01001399 for (i = 0; i < 4; i++)
1400 sysfs_remove_group(&pdev->dev.kobj, &f71805f_group_optin[i]);
Jean Delvaree1967832006-12-12 18:18:27 +01001401 sysfs_remove_group(&pdev->dev.kobj, &f71805f_group_pwm_freq);
Jean Delvarece7ee4e2007-05-08 17:21:59 +02001402exit_release_region:
1403 release_region(res->start + ADDR_REG_OFFSET, 2);
Jean Delvaree53004e2006-01-09 23:26:14 +01001404exit_free:
Jean Delvare0e39e012006-09-24 21:16:40 +02001405 platform_set_drvdata(pdev, NULL);
Jean Delvaree53004e2006-01-09 23:26:14 +01001406 kfree(data);
1407exit:
1408 return err;
1409}
1410
1411static int __devexit f71805f_remove(struct platform_device *pdev)
1412{
1413 struct f71805f_data *data = platform_get_drvdata(pdev);
Jean Delvarece7ee4e2007-05-08 17:21:59 +02001414 struct resource *res;
Jean Delvare0e39e012006-09-24 21:16:40 +02001415 int i;
Jean Delvaree53004e2006-01-09 23:26:14 +01001416
Tony Jones1beeffe2007-08-20 13:46:20 -07001417 hwmon_device_unregister(data->hwmon_dev);
Jean Delvare0e39e012006-09-24 21:16:40 +02001418 sysfs_remove_group(&pdev->dev.kobj, &f71805f_group);
Jean Delvare51c997d2006-12-12 18:18:29 +01001419 for (i = 0; i < 4; i++)
1420 sysfs_remove_group(&pdev->dev.kobj, &f71805f_group_optin[i]);
Jean Delvaree1967832006-12-12 18:18:27 +01001421 sysfs_remove_group(&pdev->dev.kobj, &f71805f_group_pwm_freq);
Jean Delvare04a62172007-06-12 13:57:19 +02001422 platform_set_drvdata(pdev, NULL);
Jean Delvaree53004e2006-01-09 23:26:14 +01001423 kfree(data);
1424
Jean Delvarece7ee4e2007-05-08 17:21:59 +02001425 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
1426 release_region(res->start + ADDR_REG_OFFSET, 2);
1427
Jean Delvaree53004e2006-01-09 23:26:14 +01001428 return 0;
1429}
1430
1431static struct platform_driver f71805f_driver = {
1432 .driver = {
1433 .owner = THIS_MODULE,
1434 .name = DRVNAME,
1435 },
1436 .probe = f71805f_probe,
1437 .remove = __devexit_p(f71805f_remove),
1438};
1439
Jean Delvare51c997d2006-12-12 18:18:29 +01001440static int __init f71805f_device_add(unsigned short address,
1441 const struct f71805f_sio_data *sio_data)
Jean Delvaree53004e2006-01-09 23:26:14 +01001442{
Jean Delvare568825c2006-03-23 16:40:23 +01001443 struct resource res = {
1444 .start = address,
1445 .end = address + REGION_LENGTH - 1,
1446 .flags = IORESOURCE_IO,
1447 };
Jean Delvaree53004e2006-01-09 23:26:14 +01001448 int err;
1449
1450 pdev = platform_device_alloc(DRVNAME, address);
1451 if (!pdev) {
1452 err = -ENOMEM;
1453 printk(KERN_ERR DRVNAME ": Device allocation failed\n");
1454 goto exit;
1455 }
1456
Jean Delvare568825c2006-03-23 16:40:23 +01001457 res.name = pdev->name;
1458 err = platform_device_add_resources(pdev, &res, 1);
Jean Delvaree53004e2006-01-09 23:26:14 +01001459 if (err) {
1460 printk(KERN_ERR DRVNAME ": Device resource addition failed "
1461 "(%d)\n", err);
1462 goto exit_device_put;
1463 }
1464
Jean Delvare2df6d812007-06-09 10:11:16 -04001465 err = platform_device_add_data(pdev, sio_data,
1466 sizeof(struct f71805f_sio_data));
1467 if (err) {
Jean Delvare51c997d2006-12-12 18:18:29 +01001468 printk(KERN_ERR DRVNAME ": Platform data allocation failed\n");
1469 goto exit_device_put;
1470 }
Jean Delvare51c997d2006-12-12 18:18:29 +01001471
Jean Delvaree53004e2006-01-09 23:26:14 +01001472 err = platform_device_add(pdev);
1473 if (err) {
1474 printk(KERN_ERR DRVNAME ": Device addition failed (%d)\n",
1475 err);
Jean Delvarea117ddd2007-02-14 21:15:05 +01001476 goto exit_device_put;
Jean Delvaree53004e2006-01-09 23:26:14 +01001477 }
1478
1479 return 0;
1480
1481exit_device_put:
1482 platform_device_put(pdev);
1483exit:
1484 return err;
1485}
1486
Jean Delvare51c997d2006-12-12 18:18:29 +01001487static int __init f71805f_find(int sioaddr, unsigned short *address,
1488 struct f71805f_sio_data *sio_data)
Jean Delvaree53004e2006-01-09 23:26:14 +01001489{
1490 int err = -ENODEV;
1491 u16 devid;
1492
Jean Delvare51c997d2006-12-12 18:18:29 +01001493 static const char *names[] = {
1494 "F71805F/FG",
Jean Delvare9cab0212007-07-15 10:36:06 +02001495 "F71872F/FG or F71806F/FG",
Jean Delvare51c997d2006-12-12 18:18:29 +01001496 };
1497
Jean Delvaree53004e2006-01-09 23:26:14 +01001498 superio_enter(sioaddr);
1499
1500 devid = superio_inw(sioaddr, SIO_REG_MANID);
1501 if (devid != SIO_FINTEK_ID)
1502 goto exit;
1503
Jean Delvare67b671b2007-12-06 23:13:42 +01001504 devid = force_id ? force_id : superio_inw(sioaddr, SIO_REG_DEVID);
Jean Delvare51c997d2006-12-12 18:18:29 +01001505 switch (devid) {
1506 case SIO_F71805F_ID:
1507 sio_data->kind = f71805f;
1508 break;
1509 case SIO_F71872F_ID:
1510 sio_data->kind = f71872f;
1511 sio_data->fnsel1 = superio_inb(sioaddr, SIO_REG_FNSEL1);
1512 break;
1513 default:
Jean Delvaree53004e2006-01-09 23:26:14 +01001514 printk(KERN_INFO DRVNAME ": Unsupported Fintek device, "
1515 "skipping\n");
1516 goto exit;
1517 }
1518
1519 superio_select(sioaddr, F71805F_LD_HWM);
1520 if (!(superio_inb(sioaddr, SIO_REG_ENABLE) & 0x01)) {
1521 printk(KERN_WARNING DRVNAME ": Device not activated, "
1522 "skipping\n");
1523 goto exit;
1524 }
1525
1526 *address = superio_inw(sioaddr, SIO_REG_ADDR);
1527 if (*address == 0) {
1528 printk(KERN_WARNING DRVNAME ": Base address not set, "
1529 "skipping\n");
1530 goto exit;
1531 }
Jean Delvare75c99022006-12-12 18:18:29 +01001532 *address &= ~(REGION_LENGTH - 1); /* Ignore 3 LSB */
Jean Delvaree53004e2006-01-09 23:26:14 +01001533
1534 err = 0;
Jean Delvare51c997d2006-12-12 18:18:29 +01001535 printk(KERN_INFO DRVNAME ": Found %s chip at %#x, revision %u\n",
1536 names[sio_data->kind], *address,
1537 superio_inb(sioaddr, SIO_REG_DEVREV));
Jean Delvaree53004e2006-01-09 23:26:14 +01001538
1539exit:
1540 superio_exit(sioaddr);
1541 return err;
1542}
1543
1544static int __init f71805f_init(void)
1545{
1546 int err;
1547 unsigned short address;
Jean Delvare51c997d2006-12-12 18:18:29 +01001548 struct f71805f_sio_data sio_data;
Jean Delvaree53004e2006-01-09 23:26:14 +01001549
Jean Delvare51c997d2006-12-12 18:18:29 +01001550 if (f71805f_find(0x2e, &address, &sio_data)
1551 && f71805f_find(0x4e, &address, &sio_data))
Jean Delvaree53004e2006-01-09 23:26:14 +01001552 return -ENODEV;
1553
1554 err = platform_driver_register(&f71805f_driver);
1555 if (err)
1556 goto exit;
1557
1558 /* Sets global pdev as a side effect */
Jean Delvare51c997d2006-12-12 18:18:29 +01001559 err = f71805f_device_add(address, &sio_data);
Jean Delvaree53004e2006-01-09 23:26:14 +01001560 if (err)
1561 goto exit_driver;
1562
1563 return 0;
1564
1565exit_driver:
1566 platform_driver_unregister(&f71805f_driver);
1567exit:
1568 return err;
1569}
1570
1571static void __exit f71805f_exit(void)
1572{
1573 platform_device_unregister(pdev);
1574 platform_driver_unregister(&f71805f_driver);
1575}
1576
1577MODULE_AUTHOR("Jean Delvare <khali@linux-fr>");
1578MODULE_LICENSE("GPL");
Jean Delvare51c997d2006-12-12 18:18:29 +01001579MODULE_DESCRIPTION("F71805F/F71872F hardware monitoring driver");
Jean Delvaree53004e2006-01-09 23:26:14 +01001580
1581module_init(f71805f_init);
1582module_exit(f71805f_exit);