blob: 6f60715f34f815021662f03c1ce605a60bf28827 [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 Delvarece7ee4e2007-05-08 17:21:59 +020038#include <linux/ioport.h>
Jean Delvaree53004e2006-01-09 23:26:14 +010039#include <asm/io.h>
40
41static struct platform_device *pdev;
42
43#define DRVNAME "f71805f"
Jean Delvare51c997d2006-12-12 18:18:29 +010044enum kinds { f71805f, f71872f };
Jean Delvaree53004e2006-01-09 23:26:14 +010045
46/*
47 * Super-I/O constants and functions
48 */
49
50#define F71805F_LD_HWM 0x04
51
52#define SIO_REG_LDSEL 0x07 /* Logical device select */
53#define SIO_REG_DEVID 0x20 /* Device ID (2 bytes) */
54#define SIO_REG_DEVREV 0x22 /* Device revision */
55#define SIO_REG_MANID 0x23 /* Fintek ID (2 bytes) */
Jean Delvare51c997d2006-12-12 18:18:29 +010056#define SIO_REG_FNSEL1 0x29 /* Multi Function Select 1 (F71872F) */
Jean Delvaree53004e2006-01-09 23:26:14 +010057#define SIO_REG_ENABLE 0x30 /* Logical device enable */
58#define SIO_REG_ADDR 0x60 /* Logical device address (2 bytes) */
59
60#define SIO_FINTEK_ID 0x1934
61#define SIO_F71805F_ID 0x0406
Jean Delvare51c997d2006-12-12 18:18:29 +010062#define SIO_F71872F_ID 0x0341
Jean Delvaree53004e2006-01-09 23:26:14 +010063
64static inline int
65superio_inb(int base, int reg)
66{
67 outb(reg, base);
68 return inb(base + 1);
69}
70
71static int
72superio_inw(int base, int reg)
73{
74 int val;
75 outb(reg++, base);
76 val = inb(base + 1) << 8;
77 outb(reg, base);
78 val |= inb(base + 1);
79 return val;
80}
81
82static inline void
83superio_select(int base, int ld)
84{
85 outb(SIO_REG_LDSEL, base);
86 outb(ld, base + 1);
87}
88
89static inline void
90superio_enter(int base)
91{
92 outb(0x87, base);
93 outb(0x87, base);
94}
95
96static inline void
97superio_exit(int base)
98{
99 outb(0xaa, base);
100}
101
102/*
103 * ISA constants
104 */
105
Jean Delvare75c99022006-12-12 18:18:29 +0100106#define REGION_LENGTH 8
107#define ADDR_REG_OFFSET 5
108#define DATA_REG_OFFSET 6
Jean Delvaree53004e2006-01-09 23:26:14 +0100109
Jean Delvaree53004e2006-01-09 23:26:14 +0100110/*
111 * Registers
112 */
113
Jean Delvare51c997d2006-12-12 18:18:29 +0100114/* in nr from 0 to 10 (8-bit values) */
Jean Delvaree53004e2006-01-09 23:26:14 +0100115#define F71805F_REG_IN(nr) (0x10 + (nr))
Jean Delvare51c997d2006-12-12 18:18:29 +0100116#define F71805F_REG_IN_HIGH(nr) ((nr) < 10 ? 0x40 + 2 * (nr) : 0x2E)
117#define F71805F_REG_IN_LOW(nr) ((nr) < 10 ? 0x41 + 2 * (nr) : 0x2F)
Jean Delvaree53004e2006-01-09 23:26:14 +0100118/* fan nr from 0 to 2 (12-bit values, two registers) */
119#define F71805F_REG_FAN(nr) (0x20 + 2 * (nr))
120#define F71805F_REG_FAN_LOW(nr) (0x28 + 2 * (nr))
Jean Delvare315c7112006-12-12 18:18:27 +0100121#define F71805F_REG_FAN_TARGET(nr) (0x69 + 16 * (nr))
Jean Delvaree53004e2006-01-09 23:26:14 +0100122#define F71805F_REG_FAN_CTRL(nr) (0x60 + 16 * (nr))
Jean Delvare6e2bc172006-12-12 18:18:27 +0100123#define F71805F_REG_PWM_FREQ(nr) (0x63 + 16 * (nr))
Jean Delvare95e35312006-12-12 18:18:26 +0100124#define F71805F_REG_PWM_DUTY(nr) (0x6B + 16 * (nr))
Jean Delvaree53004e2006-01-09 23:26:14 +0100125/* temp nr from 0 to 2 (8-bit values) */
126#define F71805F_REG_TEMP(nr) (0x1B + (nr))
127#define F71805F_REG_TEMP_HIGH(nr) (0x54 + 2 * (nr))
128#define F71805F_REG_TEMP_HYST(nr) (0x55 + 2 * (nr))
129#define F71805F_REG_TEMP_MODE 0x01
Phil Endecottaba50732007-06-29 09:19:14 +0200130/* pwm/fan pwmnr from 0 to 2, auto point apnr from 0 to 2 */
131/* map Fintek numbers to our numbers as follows: 9->0, 5->1, 1->2 */
132#define F71805F_REG_PWM_AUTO_POINT_TEMP(pwmnr, apnr) \
133 (0xA0 + 0x10 * (pwmnr) + (2 - (apnr)))
134#define F71805F_REG_PWM_AUTO_POINT_FAN(pwmnr, apnr) \
135 (0xA4 + 0x10 * (pwmnr) + \
136 2 * (2 - (apnr)))
Jean Delvaree53004e2006-01-09 23:26:14 +0100137
138#define F71805F_REG_START 0x00
139/* status nr from 0 to 2 */
140#define F71805F_REG_STATUS(nr) (0x36 + (nr))
141
Jean Delvare6b14a542006-12-12 18:18:26 +0100142/* individual register bits */
Jean Delvaree1967832006-12-12 18:18:27 +0100143#define FAN_CTRL_DC_MODE 0x10
Jean Delvare315c7112006-12-12 18:18:27 +0100144#define FAN_CTRL_LATCH_FULL 0x08
Jean Delvare95e35312006-12-12 18:18:26 +0100145#define FAN_CTRL_MODE_MASK 0x03
146#define FAN_CTRL_MODE_SPEED 0x00
147#define FAN_CTRL_MODE_TEMPERATURE 0x01
148#define FAN_CTRL_MODE_MANUAL 0x02
Jean Delvare6b14a542006-12-12 18:18:26 +0100149
Jean Delvaree53004e2006-01-09 23:26:14 +0100150/*
151 * Data structures and manipulation thereof
152 */
153
Phil Endecottaba50732007-06-29 09:19:14 +0200154struct f71805f_auto_point {
155 u8 temp[3];
156 u16 fan[3];
157};
158
Jean Delvaree53004e2006-01-09 23:26:14 +0100159struct f71805f_data {
160 unsigned short addr;
161 const char *name;
Jean Delvaree53004e2006-01-09 23:26:14 +0100162 struct class_device *class_dev;
163
Jean Delvaref0819182006-01-18 23:20:53 +0100164 struct mutex update_lock;
Jean Delvaree53004e2006-01-09 23:26:14 +0100165 char valid; /* !=0 if following fields are valid */
166 unsigned long last_updated; /* In jiffies */
167 unsigned long last_limits; /* In jiffies */
168
169 /* Register values */
Jean Delvare51c997d2006-12-12 18:18:29 +0100170 u8 in[11];
171 u8 in_high[11];
172 u8 in_low[11];
173 u16 has_in;
Jean Delvaree53004e2006-01-09 23:26:14 +0100174 u16 fan[3];
175 u16 fan_low[3];
Jean Delvare315c7112006-12-12 18:18:27 +0100176 u16 fan_target[3];
Jean Delvare6b14a542006-12-12 18:18:26 +0100177 u8 fan_ctrl[3];
Jean Delvare95e35312006-12-12 18:18:26 +0100178 u8 pwm[3];
Jean Delvare6e2bc172006-12-12 18:18:27 +0100179 u8 pwm_freq[3];
Jean Delvaree53004e2006-01-09 23:26:14 +0100180 u8 temp[3];
181 u8 temp_high[3];
182 u8 temp_hyst[3];
183 u8 temp_mode;
Jean Delvare2d457712006-09-24 20:52:15 +0200184 unsigned long alarms;
Phil Endecottaba50732007-06-29 09:19:14 +0200185 struct f71805f_auto_point auto_points[3];
Jean Delvaree53004e2006-01-09 23:26:14 +0100186};
187
Jean Delvare51c997d2006-12-12 18:18:29 +0100188struct f71805f_sio_data {
189 enum kinds kind;
190 u8 fnsel1;
191};
192
Jean Delvaree53004e2006-01-09 23:26:14 +0100193static inline long in_from_reg(u8 reg)
194{
195 return (reg * 8);
196}
197
198/* The 2 least significant bits are not used */
199static inline u8 in_to_reg(long val)
200{
201 if (val <= 0)
202 return 0;
203 if (val >= 2016)
204 return 0xfc;
205 return (((val + 16) / 32) << 2);
206}
207
208/* in0 is downscaled by a factor 2 internally */
209static inline long in0_from_reg(u8 reg)
210{
211 return (reg * 16);
212}
213
214static inline u8 in0_to_reg(long val)
215{
216 if (val <= 0)
217 return 0;
218 if (val >= 4032)
219 return 0xfc;
220 return (((val + 32) / 64) << 2);
221}
222
223/* The 4 most significant bits are not used */
224static inline long fan_from_reg(u16 reg)
225{
226 reg &= 0xfff;
227 if (!reg || reg == 0xfff)
228 return 0;
229 return (1500000 / reg);
230}
231
232static inline u16 fan_to_reg(long rpm)
233{
234 /* If the low limit is set below what the chip can measure,
235 store the largest possible 12-bit value in the registers,
236 so that no alarm will ever trigger. */
237 if (rpm < 367)
238 return 0xfff;
239 return (1500000 / rpm);
240}
241
Jean Delvare6e2bc172006-12-12 18:18:27 +0100242static inline unsigned long pwm_freq_from_reg(u8 reg)
243{
244 unsigned long clock = (reg & 0x80) ? 48000000UL : 1000000UL;
245
246 reg &= 0x7f;
247 if (reg == 0)
248 reg++;
249 return clock / (reg << 8);
250}
251
252static inline u8 pwm_freq_to_reg(unsigned long val)
253{
254 if (val >= 187500) /* The highest we can do */
255 return 0x80;
256 if (val >= 1475) /* Use 48 MHz clock */
257 return 0x80 | (48000000UL / (val << 8));
258 if (val < 31) /* The lowest we can do */
259 return 0x7f;
260 else /* Use 1 MHz clock */
261 return 1000000UL / (val << 8);
262}
263
Jean Delvaree1967832006-12-12 18:18:27 +0100264static inline int pwm_mode_from_reg(u8 reg)
265{
266 return !(reg & FAN_CTRL_DC_MODE);
267}
268
Jean Delvaree53004e2006-01-09 23:26:14 +0100269static inline long temp_from_reg(u8 reg)
270{
271 return (reg * 1000);
272}
273
274static inline u8 temp_to_reg(long val)
275{
276 if (val < 0)
277 val = 0;
278 else if (val > 1000 * 0xff)
279 val = 0xff;
280 return ((val + 500) / 1000);
281}
282
283/*
284 * Device I/O access
285 */
286
Jean Delvare7f999aa2007-02-14 21:15:03 +0100287/* Must be called with data->update_lock held, except during initialization */
Jean Delvaree53004e2006-01-09 23:26:14 +0100288static u8 f71805f_read8(struct f71805f_data *data, u8 reg)
289{
Jean Delvaree53004e2006-01-09 23:26:14 +0100290 outb(reg, data->addr + ADDR_REG_OFFSET);
Jean Delvare7f999aa2007-02-14 21:15:03 +0100291 return inb(data->addr + DATA_REG_OFFSET);
Jean Delvaree53004e2006-01-09 23:26:14 +0100292}
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 void f71805f_write8(struct f71805f_data *data, u8 reg, u8 val)
296{
Jean Delvaree53004e2006-01-09 23:26:14 +0100297 outb(reg, data->addr + ADDR_REG_OFFSET);
298 outb(val, data->addr + DATA_REG_OFFSET);
Jean Delvaree53004e2006-01-09 23:26:14 +0100299}
300
301/* It is important to read the MSB first, because doing so latches the
Jean Delvare7f999aa2007-02-14 21:15:03 +0100302 value of the LSB, so we are sure both bytes belong to the same value.
303 Must be called with data->update_lock held, except during initialization */
Jean Delvaree53004e2006-01-09 23:26:14 +0100304static u16 f71805f_read16(struct f71805f_data *data, u8 reg)
305{
306 u16 val;
307
Jean Delvaree53004e2006-01-09 23:26:14 +0100308 outb(reg, data->addr + ADDR_REG_OFFSET);
309 val = inb(data->addr + DATA_REG_OFFSET) << 8;
310 outb(++reg, data->addr + ADDR_REG_OFFSET);
311 val |= inb(data->addr + DATA_REG_OFFSET);
Jean Delvaree53004e2006-01-09 23:26:14 +0100312
313 return val;
314}
315
Jean Delvare7f999aa2007-02-14 21:15:03 +0100316/* Must be called with data->update_lock held, except during initialization */
Jean Delvaree53004e2006-01-09 23:26:14 +0100317static void f71805f_write16(struct f71805f_data *data, u8 reg, u16 val)
318{
Jean Delvaree53004e2006-01-09 23:26:14 +0100319 outb(reg, data->addr + ADDR_REG_OFFSET);
320 outb(val >> 8, data->addr + DATA_REG_OFFSET);
321 outb(++reg, data->addr + ADDR_REG_OFFSET);
322 outb(val & 0xff, data->addr + DATA_REG_OFFSET);
Jean Delvaree53004e2006-01-09 23:26:14 +0100323}
324
325static struct f71805f_data *f71805f_update_device(struct device *dev)
326{
327 struct f71805f_data *data = dev_get_drvdata(dev);
Phil Endecottaba50732007-06-29 09:19:14 +0200328 int nr, apnr;
Jean Delvaree53004e2006-01-09 23:26:14 +0100329
Jean Delvaref0819182006-01-18 23:20:53 +0100330 mutex_lock(&data->update_lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100331
332 /* Limit registers cache is refreshed after 60 seconds */
333 if (time_after(jiffies, data->last_updated + 60 * HZ)
334 || !data->valid) {
Jean Delvare51c997d2006-12-12 18:18:29 +0100335 for (nr = 0; nr < 11; nr++) {
336 if (!(data->has_in & (1 << nr)))
337 continue;
Jean Delvaree53004e2006-01-09 23:26:14 +0100338 data->in_high[nr] = f71805f_read8(data,
339 F71805F_REG_IN_HIGH(nr));
340 data->in_low[nr] = f71805f_read8(data,
341 F71805F_REG_IN_LOW(nr));
342 }
343 for (nr = 0; nr < 3; nr++) {
Jean Delvare6b14a542006-12-12 18:18:26 +0100344 data->fan_low[nr] = f71805f_read16(data,
345 F71805F_REG_FAN_LOW(nr));
Jean Delvare315c7112006-12-12 18:18:27 +0100346 data->fan_target[nr] = f71805f_read16(data,
347 F71805F_REG_FAN_TARGET(nr));
Jean Delvare6e2bc172006-12-12 18:18:27 +0100348 data->pwm_freq[nr] = f71805f_read8(data,
349 F71805F_REG_PWM_FREQ(nr));
Jean Delvaree53004e2006-01-09 23:26:14 +0100350 }
351 for (nr = 0; nr < 3; nr++) {
352 data->temp_high[nr] = f71805f_read8(data,
353 F71805F_REG_TEMP_HIGH(nr));
354 data->temp_hyst[nr] = f71805f_read8(data,
355 F71805F_REG_TEMP_HYST(nr));
356 }
357 data->temp_mode = f71805f_read8(data, F71805F_REG_TEMP_MODE);
Phil Endecottaba50732007-06-29 09:19:14 +0200358 for (nr = 0; nr < 3; nr++) {
359 for (apnr = 0; apnr < 3; apnr++) {
360 data->auto_points[nr].temp[apnr] =
361 f71805f_read8(data,
362 F71805F_REG_PWM_AUTO_POINT_TEMP(nr,
363 apnr));
364 data->auto_points[nr].fan[apnr] =
365 f71805f_read16(data,
366 F71805F_REG_PWM_AUTO_POINT_FAN(nr,
367 apnr));
368 }
369 }
Jean Delvaree53004e2006-01-09 23:26:14 +0100370
371 data->last_limits = jiffies;
372 }
373
374 /* Measurement registers cache is refreshed after 1 second */
375 if (time_after(jiffies, data->last_updated + HZ)
376 || !data->valid) {
Jean Delvare51c997d2006-12-12 18:18:29 +0100377 for (nr = 0; nr < 11; nr++) {
378 if (!(data->has_in & (1 << nr)))
379 continue;
Jean Delvaree53004e2006-01-09 23:26:14 +0100380 data->in[nr] = f71805f_read8(data,
381 F71805F_REG_IN(nr));
382 }
383 for (nr = 0; nr < 3; nr++) {
Jean Delvare6b14a542006-12-12 18:18:26 +0100384 data->fan[nr] = f71805f_read16(data,
385 F71805F_REG_FAN(nr));
Jean Delvare95e35312006-12-12 18:18:26 +0100386 data->fan_ctrl[nr] = f71805f_read8(data,
387 F71805F_REG_FAN_CTRL(nr));
388 data->pwm[nr] = f71805f_read8(data,
389 F71805F_REG_PWM_DUTY(nr));
Jean Delvaree53004e2006-01-09 23:26:14 +0100390 }
391 for (nr = 0; nr < 3; nr++) {
392 data->temp[nr] = f71805f_read8(data,
393 F71805F_REG_TEMP(nr));
394 }
Jean Delvare2d457712006-09-24 20:52:15 +0200395 data->alarms = f71805f_read8(data, F71805F_REG_STATUS(0))
396 + (f71805f_read8(data, F71805F_REG_STATUS(1)) << 8)
397 + (f71805f_read8(data, F71805F_REG_STATUS(2)) << 16);
Jean Delvaree53004e2006-01-09 23:26:14 +0100398
399 data->last_updated = jiffies;
400 data->valid = 1;
401 }
402
Jean Delvaref0819182006-01-18 23:20:53 +0100403 mutex_unlock(&data->update_lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100404
405 return data;
406}
407
408/*
409 * Sysfs interface
410 */
411
412static ssize_t show_in0(struct device *dev, struct device_attribute *devattr,
413 char *buf)
414{
415 struct f71805f_data *data = f71805f_update_device(dev);
Jean Delvare51c997d2006-12-12 18:18:29 +0100416 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
417 int nr = attr->index;
Jean Delvaree53004e2006-01-09 23:26:14 +0100418
Jean Delvare51c997d2006-12-12 18:18:29 +0100419 return sprintf(buf, "%ld\n", in0_from_reg(data->in[nr]));
Jean Delvaree53004e2006-01-09 23:26:14 +0100420}
421
422static ssize_t show_in0_max(struct device *dev, struct device_attribute
423 *devattr, char *buf)
424{
425 struct f71805f_data *data = f71805f_update_device(dev);
Jean Delvare51c997d2006-12-12 18:18:29 +0100426 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
427 int nr = attr->index;
Jean Delvaree53004e2006-01-09 23:26:14 +0100428
Jean Delvare51c997d2006-12-12 18:18:29 +0100429 return sprintf(buf, "%ld\n", in0_from_reg(data->in_high[nr]));
Jean Delvaree53004e2006-01-09 23:26:14 +0100430}
431
432static ssize_t show_in0_min(struct device *dev, struct device_attribute
433 *devattr, char *buf)
434{
435 struct f71805f_data *data = f71805f_update_device(dev);
Jean Delvare51c997d2006-12-12 18:18:29 +0100436 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
437 int nr = attr->index;
Jean Delvaree53004e2006-01-09 23:26:14 +0100438
Jean Delvare51c997d2006-12-12 18:18:29 +0100439 return sprintf(buf, "%ld\n", in0_from_reg(data->in_low[nr]));
Jean Delvaree53004e2006-01-09 23:26:14 +0100440}
441
442static ssize_t set_in0_max(struct device *dev, struct device_attribute
443 *devattr, const char *buf, size_t count)
444{
445 struct f71805f_data *data = dev_get_drvdata(dev);
Jean Delvare51c997d2006-12-12 18:18:29 +0100446 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
447 int nr = attr->index;
Jean Delvaree53004e2006-01-09 23:26:14 +0100448 long val = simple_strtol(buf, NULL, 10);
449
Jean Delvaref0819182006-01-18 23:20:53 +0100450 mutex_lock(&data->update_lock);
Jean Delvare51c997d2006-12-12 18:18:29 +0100451 data->in_high[nr] = in0_to_reg(val);
452 f71805f_write8(data, F71805F_REG_IN_HIGH(nr), data->in_high[nr]);
Jean Delvaref0819182006-01-18 23:20:53 +0100453 mutex_unlock(&data->update_lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100454
455 return count;
456}
457
458static ssize_t set_in0_min(struct device *dev, struct device_attribute
459 *devattr, const char *buf, size_t count)
460{
461 struct f71805f_data *data = dev_get_drvdata(dev);
Jean Delvare51c997d2006-12-12 18:18:29 +0100462 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
463 int nr = attr->index;
Jean Delvaree53004e2006-01-09 23:26:14 +0100464 long val = simple_strtol(buf, NULL, 10);
465
Jean Delvaref0819182006-01-18 23:20:53 +0100466 mutex_lock(&data->update_lock);
Jean Delvare51c997d2006-12-12 18:18:29 +0100467 data->in_low[nr] = in0_to_reg(val);
468 f71805f_write8(data, F71805F_REG_IN_LOW(nr), data->in_low[nr]);
Jean Delvaref0819182006-01-18 23:20:53 +0100469 mutex_unlock(&data->update_lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100470
471 return count;
472}
473
Jean Delvaree53004e2006-01-09 23:26:14 +0100474static ssize_t show_in(struct device *dev, struct device_attribute *devattr,
475 char *buf)
476{
477 struct f71805f_data *data = f71805f_update_device(dev);
478 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
479 int nr = attr->index;
480
481 return sprintf(buf, "%ld\n", in_from_reg(data->in[nr]));
482}
483
484static ssize_t show_in_max(struct device *dev, struct device_attribute
485 *devattr, char *buf)
486{
487 struct f71805f_data *data = f71805f_update_device(dev);
488 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
489 int nr = attr->index;
490
491 return sprintf(buf, "%ld\n", in_from_reg(data->in_high[nr]));
492}
493
494static ssize_t show_in_min(struct device *dev, struct device_attribute
495 *devattr, char *buf)
496{
497 struct f71805f_data *data = f71805f_update_device(dev);
498 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
499 int nr = attr->index;
500
501 return sprintf(buf, "%ld\n", in_from_reg(data->in_low[nr]));
502}
503
504static ssize_t set_in_max(struct device *dev, struct device_attribute
505 *devattr, const char *buf, size_t count)
506{
507 struct f71805f_data *data = dev_get_drvdata(dev);
508 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
509 int nr = attr->index;
510 long val = simple_strtol(buf, NULL, 10);
511
Jean Delvaref0819182006-01-18 23:20:53 +0100512 mutex_lock(&data->update_lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100513 data->in_high[nr] = in_to_reg(val);
514 f71805f_write8(data, F71805F_REG_IN_HIGH(nr), data->in_high[nr]);
Jean Delvaref0819182006-01-18 23:20:53 +0100515 mutex_unlock(&data->update_lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100516
517 return count;
518}
519
520static ssize_t set_in_min(struct device *dev, struct device_attribute
521 *devattr, const char *buf, size_t count)
522{
523 struct f71805f_data *data = dev_get_drvdata(dev);
524 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
525 int nr = attr->index;
526 long val = simple_strtol(buf, NULL, 10);
527
Jean Delvaref0819182006-01-18 23:20:53 +0100528 mutex_lock(&data->update_lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100529 data->in_low[nr] = in_to_reg(val);
530 f71805f_write8(data, F71805F_REG_IN_LOW(nr), data->in_low[nr]);
Jean Delvaref0819182006-01-18 23:20:53 +0100531 mutex_unlock(&data->update_lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100532
533 return count;
534}
535
Jean Delvaree53004e2006-01-09 23:26:14 +0100536static ssize_t show_fan(struct device *dev, struct device_attribute *devattr,
537 char *buf)
538{
539 struct f71805f_data *data = f71805f_update_device(dev);
540 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
541 int nr = attr->index;
542
543 return sprintf(buf, "%ld\n", fan_from_reg(data->fan[nr]));
544}
545
546static ssize_t show_fan_min(struct device *dev, struct device_attribute
547 *devattr, char *buf)
548{
549 struct f71805f_data *data = f71805f_update_device(dev);
550 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
551 int nr = attr->index;
552
553 return sprintf(buf, "%ld\n", fan_from_reg(data->fan_low[nr]));
554}
555
Jean Delvare315c7112006-12-12 18:18:27 +0100556static ssize_t show_fan_target(struct device *dev, struct device_attribute
557 *devattr, char *buf)
558{
559 struct f71805f_data *data = f71805f_update_device(dev);
560 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
561 int nr = attr->index;
562
563 return sprintf(buf, "%ld\n", fan_from_reg(data->fan_target[nr]));
564}
565
Jean Delvaree53004e2006-01-09 23:26:14 +0100566static ssize_t set_fan_min(struct device *dev, struct device_attribute
567 *devattr, const char *buf, size_t count)
568{
569 struct f71805f_data *data = dev_get_drvdata(dev);
570 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
571 int nr = attr->index;
572 long val = simple_strtol(buf, NULL, 10);
573
Jean Delvaref0819182006-01-18 23:20:53 +0100574 mutex_lock(&data->update_lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100575 data->fan_low[nr] = fan_to_reg(val);
576 f71805f_write16(data, F71805F_REG_FAN_LOW(nr), data->fan_low[nr]);
Jean Delvaref0819182006-01-18 23:20:53 +0100577 mutex_unlock(&data->update_lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100578
579 return count;
580}
581
Jean Delvare315c7112006-12-12 18:18:27 +0100582static ssize_t set_fan_target(struct device *dev, struct device_attribute
583 *devattr, const char *buf, size_t count)
584{
585 struct f71805f_data *data = dev_get_drvdata(dev);
586 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
587 int nr = attr->index;
588 long val = simple_strtol(buf, NULL, 10);
589
590 mutex_lock(&data->update_lock);
591 data->fan_target[nr] = fan_to_reg(val);
592 f71805f_write16(data, F71805F_REG_FAN_TARGET(nr),
593 data->fan_target[nr]);
594 mutex_unlock(&data->update_lock);
595
596 return count;
597}
598
Jean Delvare95e35312006-12-12 18:18:26 +0100599static ssize_t show_pwm(struct device *dev, struct device_attribute *devattr,
600 char *buf)
601{
602 struct f71805f_data *data = f71805f_update_device(dev);
603 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
604 int nr = attr->index;
605
606 return sprintf(buf, "%d\n", (int)data->pwm[nr]);
607}
608
609static ssize_t show_pwm_enable(struct device *dev, struct device_attribute
610 *devattr, char *buf)
611{
612 struct f71805f_data *data = f71805f_update_device(dev);
613 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
614 int nr = attr->index;
615 int mode;
616
617 switch (data->fan_ctrl[nr] & FAN_CTRL_MODE_MASK) {
618 case FAN_CTRL_MODE_SPEED:
619 mode = 3;
620 break;
621 case FAN_CTRL_MODE_TEMPERATURE:
622 mode = 2;
623 break;
624 default: /* MANUAL */
625 mode = 1;
626 }
627
628 return sprintf(buf, "%d\n", mode);
629}
630
Jean Delvare6e2bc172006-12-12 18:18:27 +0100631static ssize_t show_pwm_freq(struct device *dev, struct device_attribute
632 *devattr, char *buf)
633{
634 struct f71805f_data *data = f71805f_update_device(dev);
635 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
636 int nr = attr->index;
637
638 return sprintf(buf, "%lu\n", pwm_freq_from_reg(data->pwm_freq[nr]));
639}
640
Jean Delvaree1967832006-12-12 18:18:27 +0100641static ssize_t show_pwm_mode(struct device *dev, struct device_attribute
642 *devattr, char *buf)
643{
644 struct f71805f_data *data = f71805f_update_device(dev);
645 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
646 int nr = attr->index;
647
648 return sprintf(buf, "%d\n", pwm_mode_from_reg(data->fan_ctrl[nr]));
649}
650
Jean Delvare95e35312006-12-12 18:18:26 +0100651static ssize_t set_pwm(struct device *dev, struct device_attribute *devattr,
652 const char *buf, size_t count)
653{
654 struct f71805f_data *data = dev_get_drvdata(dev);
655 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
656 int nr = attr->index;
657 unsigned long val = simple_strtoul(buf, NULL, 10);
658
659 if (val > 255)
660 return -EINVAL;
661
662 mutex_lock(&data->update_lock);
663 data->pwm[nr] = val;
664 f71805f_write8(data, F71805F_REG_PWM_DUTY(nr), data->pwm[nr]);
665 mutex_unlock(&data->update_lock);
666
667 return count;
668}
669
670static struct attribute *f71805f_attr_pwm[];
671
672static ssize_t set_pwm_enable(struct device *dev, struct device_attribute
673 *devattr, const char *buf, size_t count)
674{
675 struct f71805f_data *data = dev_get_drvdata(dev);
676 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
677 int nr = attr->index;
678 unsigned long val = simple_strtoul(buf, NULL, 10);
679 u8 reg;
680
681 if (val < 1 || val > 3)
682 return -EINVAL;
683
684 if (val > 1) { /* Automatic mode, user can't set PWM value */
685 if (sysfs_chmod_file(&dev->kobj, f71805f_attr_pwm[nr],
686 S_IRUGO))
687 dev_dbg(dev, "chmod -w pwm%d failed\n", nr + 1);
688 }
689
690 mutex_lock(&data->update_lock);
691 reg = f71805f_read8(data, F71805F_REG_FAN_CTRL(nr))
692 & ~FAN_CTRL_MODE_MASK;
693 switch (val) {
694 case 1:
695 reg |= FAN_CTRL_MODE_MANUAL;
696 break;
697 case 2:
698 reg |= FAN_CTRL_MODE_TEMPERATURE;
699 break;
700 case 3:
701 reg |= FAN_CTRL_MODE_SPEED;
702 break;
703 }
704 data->fan_ctrl[nr] = reg;
705 f71805f_write8(data, F71805F_REG_FAN_CTRL(nr), reg);
706 mutex_unlock(&data->update_lock);
707
708 if (val == 1) { /* Manual mode, user can set PWM value */
709 if (sysfs_chmod_file(&dev->kobj, f71805f_attr_pwm[nr],
710 S_IRUGO | S_IWUSR))
711 dev_dbg(dev, "chmod +w pwm%d failed\n", nr + 1);
712 }
713
714 return count;
715}
716
Jean Delvare6e2bc172006-12-12 18:18:27 +0100717static ssize_t set_pwm_freq(struct device *dev, struct device_attribute
718 *devattr, const char *buf, size_t count)
719{
720 struct f71805f_data *data = dev_get_drvdata(dev);
721 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
722 int nr = attr->index;
723 unsigned long val = simple_strtoul(buf, NULL, 10);
724
725 mutex_lock(&data->update_lock);
726 data->pwm_freq[nr] = pwm_freq_to_reg(val);
727 f71805f_write8(data, F71805F_REG_PWM_FREQ(nr), data->pwm_freq[nr]);
728 mutex_unlock(&data->update_lock);
729
730 return count;
731}
732
Phil Endecottaba50732007-06-29 09:19:14 +0200733static ssize_t show_pwm_auto_point_temp(struct device *dev,
734 struct device_attribute *devattr,
735 char* buf)
736{
737 struct f71805f_data *data = dev_get_drvdata(dev);
738 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
739 int pwmnr = attr->nr;
740 int apnr = attr->index;
741
742 return sprintf(buf, "%ld\n",
743 temp_from_reg(data->auto_points[pwmnr].temp[apnr]));
744}
745
746static ssize_t set_pwm_auto_point_temp(struct device *dev,
747 struct device_attribute *devattr,
748 const char* buf, size_t count)
749{
750 struct f71805f_data *data = dev_get_drvdata(dev);
751 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
752 int pwmnr = attr->nr;
753 int apnr = attr->index;
754 unsigned long val = simple_strtol(buf, NULL, 10);
755
756 mutex_lock(&data->update_lock);
757 data->auto_points[pwmnr].temp[apnr] = temp_to_reg(val);
758 f71805f_write8(data, F71805F_REG_PWM_AUTO_POINT_TEMP(pwmnr, apnr),
759 data->auto_points[pwmnr].temp[apnr]);
760 mutex_unlock(&data->update_lock);
761
762 return count;
763}
764
765static ssize_t show_pwm_auto_point_fan(struct device *dev,
766 struct device_attribute *devattr,
767 char* buf)
768{
769 struct f71805f_data *data = dev_get_drvdata(dev);
770 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
771 int pwmnr = attr->nr;
772 int apnr = attr->index;
773
774 return sprintf(buf, "%ld\n",
775 fan_from_reg(data->auto_points[pwmnr].fan[apnr]));
776}
777
778static ssize_t set_pwm_auto_point_fan(struct device *dev,
779 struct device_attribute *devattr,
780 const char* buf, size_t count)
781{
782 struct f71805f_data *data = dev_get_drvdata(dev);
783 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
784 int pwmnr = attr->nr;
785 int apnr = attr->index;
786 unsigned long val = simple_strtoul(buf, NULL, 10);
787
788 mutex_lock(&data->update_lock);
789 data->auto_points[pwmnr].fan[apnr] = fan_to_reg(val);
790 f71805f_write16(data, F71805F_REG_PWM_AUTO_POINT_FAN(pwmnr, apnr),
791 data->auto_points[pwmnr].fan[apnr]);
792 mutex_unlock(&data->update_lock);
793
794 return count;
795}
796
Jean Delvaree53004e2006-01-09 23:26:14 +0100797static ssize_t show_temp(struct device *dev, struct device_attribute *devattr,
798 char *buf)
799{
800 struct f71805f_data *data = f71805f_update_device(dev);
801 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
802 int nr = attr->index;
803
804 return sprintf(buf, "%ld\n", temp_from_reg(data->temp[nr]));
805}
806
807static ssize_t show_temp_max(struct device *dev, struct device_attribute
808 *devattr, char *buf)
809{
810 struct f71805f_data *data = f71805f_update_device(dev);
811 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
812 int nr = attr->index;
813
814 return sprintf(buf, "%ld\n", temp_from_reg(data->temp_high[nr]));
815}
816
817static ssize_t show_temp_hyst(struct device *dev, struct device_attribute
818 *devattr, char *buf)
819{
820 struct f71805f_data *data = f71805f_update_device(dev);
821 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
822 int nr = attr->index;
823
824 return sprintf(buf, "%ld\n", temp_from_reg(data->temp_hyst[nr]));
825}
826
827static ssize_t show_temp_type(struct device *dev, struct device_attribute
828 *devattr, char *buf)
829{
830 struct f71805f_data *data = f71805f_update_device(dev);
831 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
832 int nr = attr->index;
833
834 /* 3 is diode, 4 is thermistor */
835 return sprintf(buf, "%u\n", (data->temp_mode & (1 << nr)) ? 3 : 4);
836}
837
838static ssize_t set_temp_max(struct device *dev, struct device_attribute
839 *devattr, const char *buf, size_t count)
840{
841 struct f71805f_data *data = dev_get_drvdata(dev);
842 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
843 int nr = attr->index;
844 long val = simple_strtol(buf, NULL, 10);
845
Jean Delvaref0819182006-01-18 23:20:53 +0100846 mutex_lock(&data->update_lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100847 data->temp_high[nr] = temp_to_reg(val);
848 f71805f_write8(data, F71805F_REG_TEMP_HIGH(nr), data->temp_high[nr]);
Jean Delvaref0819182006-01-18 23:20:53 +0100849 mutex_unlock(&data->update_lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100850
851 return count;
852}
853
854static ssize_t set_temp_hyst(struct device *dev, struct device_attribute
855 *devattr, const char *buf, size_t count)
856{
857 struct f71805f_data *data = dev_get_drvdata(dev);
858 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
859 int nr = attr->index;
860 long val = simple_strtol(buf, NULL, 10);
861
Jean Delvaref0819182006-01-18 23:20:53 +0100862 mutex_lock(&data->update_lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100863 data->temp_hyst[nr] = temp_to_reg(val);
864 f71805f_write8(data, F71805F_REG_TEMP_HYST(nr), data->temp_hyst[nr]);
Jean Delvaref0819182006-01-18 23:20:53 +0100865 mutex_unlock(&data->update_lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100866
867 return count;
868}
869
Jean Delvaree53004e2006-01-09 23:26:14 +0100870static ssize_t show_alarms_in(struct device *dev, struct device_attribute
871 *devattr, char *buf)
872{
873 struct f71805f_data *data = f71805f_update_device(dev);
874
Jean Delvare51c997d2006-12-12 18:18:29 +0100875 return sprintf(buf, "%lu\n", data->alarms & 0x7ff);
Jean Delvaree53004e2006-01-09 23:26:14 +0100876}
877
878static ssize_t show_alarms_fan(struct device *dev, struct device_attribute
879 *devattr, char *buf)
880{
881 struct f71805f_data *data = f71805f_update_device(dev);
882
Jean Delvare2d457712006-09-24 20:52:15 +0200883 return sprintf(buf, "%lu\n", (data->alarms >> 16) & 0x07);
Jean Delvaree53004e2006-01-09 23:26:14 +0100884}
885
886static ssize_t show_alarms_temp(struct device *dev, struct device_attribute
887 *devattr, char *buf)
888{
889 struct f71805f_data *data = f71805f_update_device(dev);
890
Jean Delvare2d457712006-09-24 20:52:15 +0200891 return sprintf(buf, "%lu\n", (data->alarms >> 11) & 0x07);
892}
893
894static ssize_t show_alarm(struct device *dev, struct device_attribute
895 *devattr, char *buf)
896{
897 struct f71805f_data *data = f71805f_update_device(dev);
898 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
899 int bitnr = attr->index;
900
901 return sprintf(buf, "%lu\n", (data->alarms >> bitnr) & 1);
Jean Delvaree53004e2006-01-09 23:26:14 +0100902}
903
Jean Delvaree53004e2006-01-09 23:26:14 +0100904static ssize_t show_name(struct device *dev, struct device_attribute
905 *devattr, char *buf)
906{
907 struct f71805f_data *data = dev_get_drvdata(dev);
908
909 return sprintf(buf, "%s\n", data->name);
910}
911
Jean Delvare51c997d2006-12-12 18:18:29 +0100912static SENSOR_DEVICE_ATTR(in0_input, S_IRUGO, show_in0, NULL, 0);
913static SENSOR_DEVICE_ATTR(in0_max, S_IRUGO| S_IWUSR,
914 show_in0_max, set_in0_max, 0);
915static SENSOR_DEVICE_ATTR(in0_min, S_IRUGO| S_IWUSR,
916 show_in0_min, set_in0_min, 0);
Jean Delvare0e39e012006-09-24 21:16:40 +0200917static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, show_in, NULL, 1);
918static SENSOR_DEVICE_ATTR(in1_max, S_IRUGO | S_IWUSR,
919 show_in_max, set_in_max, 1);
920static SENSOR_DEVICE_ATTR(in1_min, S_IRUGO | S_IWUSR,
921 show_in_min, set_in_min, 1);
922static SENSOR_DEVICE_ATTR(in2_input, S_IRUGO, show_in, NULL, 2);
923static SENSOR_DEVICE_ATTR(in2_max, S_IRUGO | S_IWUSR,
924 show_in_max, set_in_max, 2);
925static SENSOR_DEVICE_ATTR(in2_min, S_IRUGO | S_IWUSR,
926 show_in_min, set_in_min, 2);
927static SENSOR_DEVICE_ATTR(in3_input, S_IRUGO, show_in, NULL, 3);
928static SENSOR_DEVICE_ATTR(in3_max, S_IRUGO | S_IWUSR,
929 show_in_max, set_in_max, 3);
930static SENSOR_DEVICE_ATTR(in3_min, S_IRUGO | S_IWUSR,
931 show_in_min, set_in_min, 3);
932static SENSOR_DEVICE_ATTR(in4_input, S_IRUGO, show_in, NULL, 4);
933static SENSOR_DEVICE_ATTR(in4_max, S_IRUGO | S_IWUSR,
934 show_in_max, set_in_max, 4);
935static SENSOR_DEVICE_ATTR(in4_min, S_IRUGO | S_IWUSR,
936 show_in_min, set_in_min, 4);
937static SENSOR_DEVICE_ATTR(in5_input, S_IRUGO, show_in, NULL, 5);
938static SENSOR_DEVICE_ATTR(in5_max, S_IRUGO | S_IWUSR,
939 show_in_max, set_in_max, 5);
940static SENSOR_DEVICE_ATTR(in5_min, S_IRUGO | S_IWUSR,
941 show_in_min, set_in_min, 5);
942static SENSOR_DEVICE_ATTR(in6_input, S_IRUGO, show_in, NULL, 6);
943static SENSOR_DEVICE_ATTR(in6_max, S_IRUGO | S_IWUSR,
944 show_in_max, set_in_max, 6);
945static SENSOR_DEVICE_ATTR(in6_min, S_IRUGO | S_IWUSR,
946 show_in_min, set_in_min, 6);
947static SENSOR_DEVICE_ATTR(in7_input, S_IRUGO, show_in, NULL, 7);
948static SENSOR_DEVICE_ATTR(in7_max, S_IRUGO | S_IWUSR,
949 show_in_max, set_in_max, 7);
950static SENSOR_DEVICE_ATTR(in7_min, S_IRUGO | S_IWUSR,
951 show_in_min, set_in_min, 7);
952static SENSOR_DEVICE_ATTR(in8_input, S_IRUGO, show_in, NULL, 8);
953static SENSOR_DEVICE_ATTR(in8_max, S_IRUGO | S_IWUSR,
954 show_in_max, set_in_max, 8);
955static SENSOR_DEVICE_ATTR(in8_min, S_IRUGO | S_IWUSR,
956 show_in_min, set_in_min, 8);
Jean Delvare51c997d2006-12-12 18:18:29 +0100957static SENSOR_DEVICE_ATTR(in9_input, S_IRUGO, show_in0, NULL, 9);
958static SENSOR_DEVICE_ATTR(in9_max, S_IRUGO | S_IWUSR,
959 show_in0_max, set_in0_max, 9);
960static SENSOR_DEVICE_ATTR(in9_min, S_IRUGO | S_IWUSR,
961 show_in0_min, set_in0_min, 9);
962static SENSOR_DEVICE_ATTR(in10_input, S_IRUGO, show_in0, NULL, 10);
963static SENSOR_DEVICE_ATTR(in10_max, S_IRUGO | S_IWUSR,
964 show_in0_max, set_in0_max, 10);
965static SENSOR_DEVICE_ATTR(in10_min, S_IRUGO | S_IWUSR,
966 show_in0_min, set_in0_min, 10);
Jean Delvare0e39e012006-09-24 21:16:40 +0200967
968static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0);
969static SENSOR_DEVICE_ATTR(fan1_min, S_IRUGO | S_IWUSR,
970 show_fan_min, set_fan_min, 0);
Jean Delvare315c7112006-12-12 18:18:27 +0100971static SENSOR_DEVICE_ATTR(fan1_target, S_IRUGO | S_IWUSR,
972 show_fan_target, set_fan_target, 0);
Jean Delvare0e39e012006-09-24 21:16:40 +0200973static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, show_fan, NULL, 1);
974static SENSOR_DEVICE_ATTR(fan2_min, S_IRUGO | S_IWUSR,
975 show_fan_min, set_fan_min, 1);
Jean Delvare315c7112006-12-12 18:18:27 +0100976static SENSOR_DEVICE_ATTR(fan2_target, S_IRUGO | S_IWUSR,
977 show_fan_target, set_fan_target, 1);
Jean Delvare0e39e012006-09-24 21:16:40 +0200978static SENSOR_DEVICE_ATTR(fan3_input, S_IRUGO, show_fan, NULL, 2);
979static SENSOR_DEVICE_ATTR(fan3_min, S_IRUGO | S_IWUSR,
980 show_fan_min, set_fan_min, 2);
Jean Delvare315c7112006-12-12 18:18:27 +0100981static SENSOR_DEVICE_ATTR(fan3_target, S_IRUGO | S_IWUSR,
982 show_fan_target, set_fan_target, 2);
Jean Delvare0e39e012006-09-24 21:16:40 +0200983
984static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
985static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO | S_IWUSR,
986 show_temp_max, set_temp_max, 0);
987static SENSOR_DEVICE_ATTR(temp1_max_hyst, S_IRUGO | S_IWUSR,
988 show_temp_hyst, set_temp_hyst, 0);
989static SENSOR_DEVICE_ATTR(temp1_type, S_IRUGO, show_temp_type, NULL, 0);
990static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1);
991static SENSOR_DEVICE_ATTR(temp2_max, S_IRUGO | S_IWUSR,
992 show_temp_max, set_temp_max, 1);
993static SENSOR_DEVICE_ATTR(temp2_max_hyst, S_IRUGO | S_IWUSR,
994 show_temp_hyst, set_temp_hyst, 1);
995static SENSOR_DEVICE_ATTR(temp2_type, S_IRUGO, show_temp_type, NULL, 1);
996static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, show_temp, NULL, 2);
997static SENSOR_DEVICE_ATTR(temp3_max, S_IRUGO | S_IWUSR,
998 show_temp_max, set_temp_max, 2);
999static SENSOR_DEVICE_ATTR(temp3_max_hyst, S_IRUGO | S_IWUSR,
1000 show_temp_hyst, set_temp_hyst, 2);
1001static SENSOR_DEVICE_ATTR(temp3_type, S_IRUGO, show_temp_type, NULL, 2);
1002
Jean Delvare95e35312006-12-12 18:18:26 +01001003/* pwm (value) files are created read-only, write permission is
1004 then added or removed dynamically as needed */
1005static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO, show_pwm, set_pwm, 0);
1006static SENSOR_DEVICE_ATTR(pwm1_enable, S_IRUGO | S_IWUSR,
1007 show_pwm_enable, set_pwm_enable, 0);
Jean Delvare6e2bc172006-12-12 18:18:27 +01001008static SENSOR_DEVICE_ATTR(pwm1_freq, S_IRUGO | S_IWUSR,
1009 show_pwm_freq, set_pwm_freq, 0);
Jean Delvaree1967832006-12-12 18:18:27 +01001010static SENSOR_DEVICE_ATTR(pwm1_mode, S_IRUGO, show_pwm_mode, NULL, 0);
Jean Delvare95e35312006-12-12 18:18:26 +01001011static SENSOR_DEVICE_ATTR(pwm2, S_IRUGO, show_pwm, set_pwm, 1);
1012static SENSOR_DEVICE_ATTR(pwm2_enable, S_IRUGO | S_IWUSR,
1013 show_pwm_enable, set_pwm_enable, 1);
Jean Delvare6e2bc172006-12-12 18:18:27 +01001014static SENSOR_DEVICE_ATTR(pwm2_freq, S_IRUGO | S_IWUSR,
1015 show_pwm_freq, set_pwm_freq, 1);
Jean Delvaree1967832006-12-12 18:18:27 +01001016static SENSOR_DEVICE_ATTR(pwm2_mode, S_IRUGO, show_pwm_mode, NULL, 1);
Jean Delvare95e35312006-12-12 18:18:26 +01001017static SENSOR_DEVICE_ATTR(pwm3, S_IRUGO, show_pwm, set_pwm, 2);
1018static SENSOR_DEVICE_ATTR(pwm3_enable, S_IRUGO | S_IWUSR,
1019 show_pwm_enable, set_pwm_enable, 2);
Jean Delvare6e2bc172006-12-12 18:18:27 +01001020static SENSOR_DEVICE_ATTR(pwm3_freq, S_IRUGO | S_IWUSR,
1021 show_pwm_freq, set_pwm_freq, 2);
Jean Delvaree1967832006-12-12 18:18:27 +01001022static SENSOR_DEVICE_ATTR(pwm3_mode, S_IRUGO, show_pwm_mode, NULL, 2);
Jean Delvare95e35312006-12-12 18:18:26 +01001023
Phil Endecottaba50732007-06-29 09:19:14 +02001024static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_temp, S_IRUGO | S_IWUSR,
1025 show_pwm_auto_point_temp, set_pwm_auto_point_temp,
1026 0, 0);
1027static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_fan, S_IRUGO | S_IWUSR,
1028 show_pwm_auto_point_fan, set_pwm_auto_point_fan,
1029 0, 0);
1030static SENSOR_DEVICE_ATTR_2(pwm1_auto_point2_temp, S_IRUGO | S_IWUSR,
1031 show_pwm_auto_point_temp, set_pwm_auto_point_temp,
1032 0, 1);
1033static SENSOR_DEVICE_ATTR_2(pwm1_auto_point2_fan, S_IRUGO | S_IWUSR,
1034 show_pwm_auto_point_fan, set_pwm_auto_point_fan,
1035 0, 1);
1036static SENSOR_DEVICE_ATTR_2(pwm1_auto_point3_temp, S_IRUGO | S_IWUSR,
1037 show_pwm_auto_point_temp, set_pwm_auto_point_temp,
1038 0, 2);
1039static SENSOR_DEVICE_ATTR_2(pwm1_auto_point3_fan, S_IRUGO | S_IWUSR,
1040 show_pwm_auto_point_fan, set_pwm_auto_point_fan,
1041 0, 2);
1042
1043static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_temp, S_IRUGO | S_IWUSR,
1044 show_pwm_auto_point_temp, set_pwm_auto_point_temp,
1045 1, 0);
1046static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_fan, S_IRUGO | S_IWUSR,
1047 show_pwm_auto_point_fan, set_pwm_auto_point_fan,
1048 1, 0);
1049static SENSOR_DEVICE_ATTR_2(pwm2_auto_point2_temp, S_IRUGO | S_IWUSR,
1050 show_pwm_auto_point_temp, set_pwm_auto_point_temp,
1051 1, 1);
1052static SENSOR_DEVICE_ATTR_2(pwm2_auto_point2_fan, S_IRUGO | S_IWUSR,
1053 show_pwm_auto_point_fan, set_pwm_auto_point_fan,
1054 1, 1);
1055static SENSOR_DEVICE_ATTR_2(pwm2_auto_point3_temp, S_IRUGO | S_IWUSR,
1056 show_pwm_auto_point_temp, set_pwm_auto_point_temp,
1057 1, 2);
1058static SENSOR_DEVICE_ATTR_2(pwm2_auto_point3_fan, S_IRUGO | S_IWUSR,
1059 show_pwm_auto_point_fan, set_pwm_auto_point_fan,
1060 1, 2);
1061
1062static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_temp, S_IRUGO | S_IWUSR,
1063 show_pwm_auto_point_temp, set_pwm_auto_point_temp,
1064 2, 0);
1065static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_fan, S_IRUGO | S_IWUSR,
1066 show_pwm_auto_point_fan, set_pwm_auto_point_fan,
1067 2, 0);
1068static SENSOR_DEVICE_ATTR_2(pwm3_auto_point2_temp, S_IRUGO | S_IWUSR,
1069 show_pwm_auto_point_temp, set_pwm_auto_point_temp,
1070 2, 1);
1071static SENSOR_DEVICE_ATTR_2(pwm3_auto_point2_fan, S_IRUGO | S_IWUSR,
1072 show_pwm_auto_point_fan, set_pwm_auto_point_fan,
1073 2, 1);
1074static SENSOR_DEVICE_ATTR_2(pwm3_auto_point3_temp, S_IRUGO | S_IWUSR,
1075 show_pwm_auto_point_temp, set_pwm_auto_point_temp,
1076 2, 2);
1077static SENSOR_DEVICE_ATTR_2(pwm3_auto_point3_fan, S_IRUGO | S_IWUSR,
1078 show_pwm_auto_point_fan, set_pwm_auto_point_fan,
1079 2, 2);
1080
Jean Delvare0e39e012006-09-24 21:16:40 +02001081static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0);
1082static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1);
1083static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2);
1084static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3);
1085static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 4);
1086static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 5);
1087static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 6);
1088static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 7);
1089static SENSOR_DEVICE_ATTR(in8_alarm, S_IRUGO, show_alarm, NULL, 8);
Jean Delvare51c997d2006-12-12 18:18:29 +01001090static SENSOR_DEVICE_ATTR(in9_alarm, S_IRUGO, show_alarm, NULL, 9);
1091static SENSOR_DEVICE_ATTR(in10_alarm, S_IRUGO, show_alarm, NULL, 10);
Jean Delvare0e39e012006-09-24 21:16:40 +02001092static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 11);
1093static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 12);
1094static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 13);
1095static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 16);
1096static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 17);
1097static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 18);
1098static DEVICE_ATTR(alarms_in, S_IRUGO, show_alarms_in, NULL);
1099static DEVICE_ATTR(alarms_fan, S_IRUGO, show_alarms_fan, NULL);
1100static DEVICE_ATTR(alarms_temp, S_IRUGO, show_alarms_temp, NULL);
1101
1102static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
1103
1104static struct attribute *f71805f_attributes[] = {
Jean Delvare51c997d2006-12-12 18:18:29 +01001105 &sensor_dev_attr_in0_input.dev_attr.attr,
1106 &sensor_dev_attr_in0_max.dev_attr.attr,
1107 &sensor_dev_attr_in0_min.dev_attr.attr,
Jean Delvare0e39e012006-09-24 21:16:40 +02001108 &sensor_dev_attr_in1_input.dev_attr.attr,
1109 &sensor_dev_attr_in1_max.dev_attr.attr,
1110 &sensor_dev_attr_in1_min.dev_attr.attr,
1111 &sensor_dev_attr_in2_input.dev_attr.attr,
1112 &sensor_dev_attr_in2_max.dev_attr.attr,
1113 &sensor_dev_attr_in2_min.dev_attr.attr,
1114 &sensor_dev_attr_in3_input.dev_attr.attr,
1115 &sensor_dev_attr_in3_max.dev_attr.attr,
1116 &sensor_dev_attr_in3_min.dev_attr.attr,
Jean Delvare0e39e012006-09-24 21:16:40 +02001117 &sensor_dev_attr_in5_input.dev_attr.attr,
1118 &sensor_dev_attr_in5_max.dev_attr.attr,
1119 &sensor_dev_attr_in5_min.dev_attr.attr,
1120 &sensor_dev_attr_in6_input.dev_attr.attr,
1121 &sensor_dev_attr_in6_max.dev_attr.attr,
1122 &sensor_dev_attr_in6_min.dev_attr.attr,
1123 &sensor_dev_attr_in7_input.dev_attr.attr,
1124 &sensor_dev_attr_in7_max.dev_attr.attr,
1125 &sensor_dev_attr_in7_min.dev_attr.attr,
Jean Delvare0e39e012006-09-24 21:16:40 +02001126
Jean Delvarec7176cb2006-12-12 18:18:29 +01001127 &sensor_dev_attr_fan1_input.dev_attr.attr,
1128 &sensor_dev_attr_fan1_min.dev_attr.attr,
1129 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
1130 &sensor_dev_attr_fan1_target.dev_attr.attr,
1131 &sensor_dev_attr_fan2_input.dev_attr.attr,
1132 &sensor_dev_attr_fan2_min.dev_attr.attr,
1133 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
1134 &sensor_dev_attr_fan2_target.dev_attr.attr,
1135 &sensor_dev_attr_fan3_input.dev_attr.attr,
1136 &sensor_dev_attr_fan3_min.dev_attr.attr,
1137 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
1138 &sensor_dev_attr_fan3_target.dev_attr.attr,
1139
1140 &sensor_dev_attr_pwm1.dev_attr.attr,
1141 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
1142 &sensor_dev_attr_pwm1_mode.dev_attr.attr,
1143 &sensor_dev_attr_pwm2.dev_attr.attr,
1144 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
1145 &sensor_dev_attr_pwm2_mode.dev_attr.attr,
1146 &sensor_dev_attr_pwm3.dev_attr.attr,
1147 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
1148 &sensor_dev_attr_pwm3_mode.dev_attr.attr,
1149
Jean Delvare0e39e012006-09-24 21:16:40 +02001150 &sensor_dev_attr_temp1_input.dev_attr.attr,
1151 &sensor_dev_attr_temp1_max.dev_attr.attr,
1152 &sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
1153 &sensor_dev_attr_temp1_type.dev_attr.attr,
1154 &sensor_dev_attr_temp2_input.dev_attr.attr,
1155 &sensor_dev_attr_temp2_max.dev_attr.attr,
1156 &sensor_dev_attr_temp2_max_hyst.dev_attr.attr,
1157 &sensor_dev_attr_temp2_type.dev_attr.attr,
1158 &sensor_dev_attr_temp3_input.dev_attr.attr,
1159 &sensor_dev_attr_temp3_max.dev_attr.attr,
1160 &sensor_dev_attr_temp3_max_hyst.dev_attr.attr,
1161 &sensor_dev_attr_temp3_type.dev_attr.attr,
1162
Phil Endecottaba50732007-06-29 09:19:14 +02001163 &sensor_dev_attr_pwm1_auto_point1_temp.dev_attr.attr,
1164 &sensor_dev_attr_pwm1_auto_point1_fan.dev_attr.attr,
1165 &sensor_dev_attr_pwm1_auto_point2_temp.dev_attr.attr,
1166 &sensor_dev_attr_pwm1_auto_point2_fan.dev_attr.attr,
1167 &sensor_dev_attr_pwm1_auto_point3_temp.dev_attr.attr,
1168 &sensor_dev_attr_pwm1_auto_point3_fan.dev_attr.attr,
1169 &sensor_dev_attr_pwm2_auto_point1_temp.dev_attr.attr,
1170 &sensor_dev_attr_pwm2_auto_point1_fan.dev_attr.attr,
1171 &sensor_dev_attr_pwm2_auto_point2_temp.dev_attr.attr,
1172 &sensor_dev_attr_pwm2_auto_point2_fan.dev_attr.attr,
1173 &sensor_dev_attr_pwm2_auto_point3_temp.dev_attr.attr,
1174 &sensor_dev_attr_pwm2_auto_point3_fan.dev_attr.attr,
1175 &sensor_dev_attr_pwm3_auto_point1_temp.dev_attr.attr,
1176 &sensor_dev_attr_pwm3_auto_point1_fan.dev_attr.attr,
1177 &sensor_dev_attr_pwm3_auto_point2_temp.dev_attr.attr,
1178 &sensor_dev_attr_pwm3_auto_point2_fan.dev_attr.attr,
1179 &sensor_dev_attr_pwm3_auto_point3_temp.dev_attr.attr,
1180 &sensor_dev_attr_pwm3_auto_point3_fan.dev_attr.attr,
1181
Jean Delvare0e39e012006-09-24 21:16:40 +02001182 &sensor_dev_attr_in0_alarm.dev_attr.attr,
1183 &sensor_dev_attr_in1_alarm.dev_attr.attr,
1184 &sensor_dev_attr_in2_alarm.dev_attr.attr,
1185 &sensor_dev_attr_in3_alarm.dev_attr.attr,
Jean Delvare0e39e012006-09-24 21:16:40 +02001186 &sensor_dev_attr_in5_alarm.dev_attr.attr,
1187 &sensor_dev_attr_in6_alarm.dev_attr.attr,
1188 &sensor_dev_attr_in7_alarm.dev_attr.attr,
Jean Delvare0e39e012006-09-24 21:16:40 +02001189 &dev_attr_alarms_in.attr,
1190 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
1191 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
1192 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
1193 &dev_attr_alarms_temp.attr,
1194 &dev_attr_alarms_fan.attr,
1195
1196 &dev_attr_name.attr,
1197 NULL
Jean Delvare2488a392006-01-09 23:29:11 +01001198};
1199
Jean Delvare0e39e012006-09-24 21:16:40 +02001200static const struct attribute_group f71805f_group = {
1201 .attrs = f71805f_attributes,
Jean Delvare2488a392006-01-09 23:29:11 +01001202};
1203
Jean Delvare51c997d2006-12-12 18:18:29 +01001204static struct attribute *f71805f_attributes_optin[4][5] = {
1205 {
1206 &sensor_dev_attr_in4_input.dev_attr.attr,
1207 &sensor_dev_attr_in4_max.dev_attr.attr,
1208 &sensor_dev_attr_in4_min.dev_attr.attr,
1209 &sensor_dev_attr_in4_alarm.dev_attr.attr,
1210 NULL
1211 }, {
1212 &sensor_dev_attr_in8_input.dev_attr.attr,
1213 &sensor_dev_attr_in8_max.dev_attr.attr,
1214 &sensor_dev_attr_in8_min.dev_attr.attr,
1215 &sensor_dev_attr_in8_alarm.dev_attr.attr,
1216 NULL
1217 }, {
1218 &sensor_dev_attr_in9_input.dev_attr.attr,
1219 &sensor_dev_attr_in9_max.dev_attr.attr,
1220 &sensor_dev_attr_in9_min.dev_attr.attr,
1221 &sensor_dev_attr_in9_alarm.dev_attr.attr,
1222 NULL
1223 }, {
1224 &sensor_dev_attr_in10_input.dev_attr.attr,
1225 &sensor_dev_attr_in10_max.dev_attr.attr,
1226 &sensor_dev_attr_in10_min.dev_attr.attr,
1227 &sensor_dev_attr_in10_alarm.dev_attr.attr,
1228 NULL
1229 }
1230};
1231
1232static const struct attribute_group f71805f_group_optin[4] = {
1233 { .attrs = f71805f_attributes_optin[0] },
1234 { .attrs = f71805f_attributes_optin[1] },
1235 { .attrs = f71805f_attributes_optin[2] },
1236 { .attrs = f71805f_attributes_optin[3] },
1237};
1238
Jean Delvaree1967832006-12-12 18:18:27 +01001239/* We don't include pwm_freq files in the arrays above, because they must be
1240 created conditionally (only if pwm_mode is 1 == PWM) */
1241static struct attribute *f71805f_attributes_pwm_freq[] = {
1242 &sensor_dev_attr_pwm1_freq.dev_attr.attr,
1243 &sensor_dev_attr_pwm2_freq.dev_attr.attr,
1244 &sensor_dev_attr_pwm3_freq.dev_attr.attr,
1245 NULL
1246};
1247
1248static const struct attribute_group f71805f_group_pwm_freq = {
1249 .attrs = f71805f_attributes_pwm_freq,
1250};
1251
Jean Delvare95e35312006-12-12 18:18:26 +01001252/* We also need an indexed access to pwmN files to toggle writability */
1253static struct attribute *f71805f_attr_pwm[] = {
1254 &sensor_dev_attr_pwm1.dev_attr.attr,
1255 &sensor_dev_attr_pwm2.dev_attr.attr,
1256 &sensor_dev_attr_pwm3.dev_attr.attr,
1257};
1258
Jean Delvaree53004e2006-01-09 23:26:14 +01001259/*
1260 * Device registration and initialization
1261 */
1262
1263static void __devinit f71805f_init_device(struct f71805f_data *data)
1264{
1265 u8 reg;
1266 int i;
1267
1268 reg = f71805f_read8(data, F71805F_REG_START);
1269 if ((reg & 0x41) != 0x01) {
1270 printk(KERN_DEBUG DRVNAME ": Starting monitoring "
1271 "operations\n");
1272 f71805f_write8(data, F71805F_REG_START, (reg | 0x01) & ~0x40);
1273 }
1274
1275 /* Fan monitoring can be disabled. If it is, we won't be polling
1276 the register values, and won't create the related sysfs files. */
1277 for (i = 0; i < 3; i++) {
Jean Delvare6b14a542006-12-12 18:18:26 +01001278 data->fan_ctrl[i] = f71805f_read8(data,
1279 F71805F_REG_FAN_CTRL(i));
Jean Delvare315c7112006-12-12 18:18:27 +01001280 /* Clear latch full bit, else "speed mode" fan speed control
1281 doesn't work */
1282 if (data->fan_ctrl[i] & FAN_CTRL_LATCH_FULL) {
1283 data->fan_ctrl[i] &= ~FAN_CTRL_LATCH_FULL;
1284 f71805f_write8(data, F71805F_REG_FAN_CTRL(i),
1285 data->fan_ctrl[i]);
1286 }
Jean Delvaree53004e2006-01-09 23:26:14 +01001287 }
1288}
1289
1290static int __devinit f71805f_probe(struct platform_device *pdev)
1291{
Jean Delvare51c997d2006-12-12 18:18:29 +01001292 struct f71805f_sio_data *sio_data = pdev->dev.platform_data;
Jean Delvaree53004e2006-01-09 23:26:14 +01001293 struct f71805f_data *data;
1294 struct resource *res;
Jean Delvare2488a392006-01-09 23:29:11 +01001295 int i, err;
Jean Delvaree53004e2006-01-09 23:26:14 +01001296
Jean Delvare51c997d2006-12-12 18:18:29 +01001297 static const char *names[] = {
1298 "f71805f",
1299 "f71872f",
1300 };
1301
Jean Delvaree53004e2006-01-09 23:26:14 +01001302 if (!(data = kzalloc(sizeof(struct f71805f_data), GFP_KERNEL))) {
1303 err = -ENOMEM;
1304 printk(KERN_ERR DRVNAME ": Out of memory\n");
1305 goto exit;
1306 }
1307
1308 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
Jean Delvarece7ee4e2007-05-08 17:21:59 +02001309 if (!request_region(res->start + ADDR_REG_OFFSET, 2, DRVNAME)) {
1310 err = -EBUSY;
1311 dev_err(&pdev->dev, "Failed to request region 0x%lx-0x%lx\n",
1312 (unsigned long)(res->start + ADDR_REG_OFFSET),
1313 (unsigned long)(res->start + ADDR_REG_OFFSET + 1));
1314 goto exit_free;
1315 }
Jean Delvaree53004e2006-01-09 23:26:14 +01001316 data->addr = res->start;
Jean Delvare51c997d2006-12-12 18:18:29 +01001317 data->name = names[sio_data->kind];
Jean Delvaref0819182006-01-18 23:20:53 +01001318 mutex_init(&data->update_lock);
Jean Delvaree53004e2006-01-09 23:26:14 +01001319
1320 platform_set_drvdata(pdev, data);
1321
Jean Delvare51c997d2006-12-12 18:18:29 +01001322 /* Some voltage inputs depend on chip model and configuration */
1323 switch (sio_data->kind) {
1324 case f71805f:
1325 data->has_in = 0x1ff;
1326 break;
1327 case f71872f:
1328 data->has_in = 0x6ef;
1329 if (sio_data->fnsel1 & 0x01)
1330 data->has_in |= (1 << 4); /* in4 */
1331 if (sio_data->fnsel1 & 0x02)
1332 data->has_in |= (1 << 8); /* in8 */
1333 break;
1334 }
1335
Jean Delvaree53004e2006-01-09 23:26:14 +01001336 /* Initialize the F71805F chip */
1337 f71805f_init_device(data);
1338
1339 /* Register sysfs interface files */
Jean Delvare0e39e012006-09-24 21:16:40 +02001340 if ((err = sysfs_create_group(&pdev->dev.kobj, &f71805f_group)))
Jean Delvarece7ee4e2007-05-08 17:21:59 +02001341 goto exit_release_region;
Jean Delvare51c997d2006-12-12 18:18:29 +01001342 if (data->has_in & (1 << 4)) { /* in4 */
1343 if ((err = sysfs_create_group(&pdev->dev.kobj,
1344 &f71805f_group_optin[0])))
1345 goto exit_remove_files;
1346 }
1347 if (data->has_in & (1 << 8)) { /* in8 */
1348 if ((err = sysfs_create_group(&pdev->dev.kobj,
1349 &f71805f_group_optin[1])))
1350 goto exit_remove_files;
1351 }
1352 if (data->has_in & (1 << 9)) { /* in9 (F71872F/FG only) */
1353 if ((err = sysfs_create_group(&pdev->dev.kobj,
1354 &f71805f_group_optin[2])))
1355 goto exit_remove_files;
1356 }
1357 if (data->has_in & (1 << 10)) { /* in9 (F71872F/FG only) */
1358 if ((err = sysfs_create_group(&pdev->dev.kobj,
1359 &f71805f_group_optin[3])))
1360 goto exit_remove_files;
1361 }
Jean Delvare0e39e012006-09-24 21:16:40 +02001362 for (i = 0; i < 3; i++) {
Jean Delvaree1967832006-12-12 18:18:27 +01001363 /* If control mode is PWM, create pwm_freq file */
1364 if (!(data->fan_ctrl[i] & FAN_CTRL_DC_MODE)) {
1365 if ((err = sysfs_create_file(&pdev->dev.kobj,
1366 f71805f_attributes_pwm_freq[i])))
1367 goto exit_remove_files;
1368 }
Jean Delvare95e35312006-12-12 18:18:26 +01001369 /* If PWM is in manual mode, add write permission */
1370 if (data->fan_ctrl[i] & FAN_CTRL_MODE_MANUAL) {
1371 if ((err = sysfs_chmod_file(&pdev->dev.kobj,
1372 f71805f_attr_pwm[i],
1373 S_IRUGO | S_IWUSR))) {
1374 dev_err(&pdev->dev, "chmod +w pwm%d failed\n",
1375 i + 1);
1376 goto exit_remove_files;
1377 }
1378 }
Jean Delvare0e39e012006-09-24 21:16:40 +02001379 }
1380
1381 data->class_dev = hwmon_device_register(&pdev->dev);
1382 if (IS_ERR(data->class_dev)) {
1383 err = PTR_ERR(data->class_dev);
1384 dev_err(&pdev->dev, "Class registration failed (%d)\n", err);
1385 goto exit_remove_files;
Jean Delvaree53004e2006-01-09 23:26:14 +01001386 }
Jean Delvaree53004e2006-01-09 23:26:14 +01001387
1388 return 0;
1389
Jean Delvare0e39e012006-09-24 21:16:40 +02001390exit_remove_files:
1391 sysfs_remove_group(&pdev->dev.kobj, &f71805f_group);
Jean Delvare51c997d2006-12-12 18:18:29 +01001392 for (i = 0; i < 4; i++)
1393 sysfs_remove_group(&pdev->dev.kobj, &f71805f_group_optin[i]);
Jean Delvaree1967832006-12-12 18:18:27 +01001394 sysfs_remove_group(&pdev->dev.kobj, &f71805f_group_pwm_freq);
Jean Delvarece7ee4e2007-05-08 17:21:59 +02001395exit_release_region:
1396 release_region(res->start + ADDR_REG_OFFSET, 2);
Jean Delvaree53004e2006-01-09 23:26:14 +01001397exit_free:
Jean Delvare0e39e012006-09-24 21:16:40 +02001398 platform_set_drvdata(pdev, NULL);
Jean Delvaree53004e2006-01-09 23:26:14 +01001399 kfree(data);
1400exit:
1401 return err;
1402}
1403
1404static int __devexit f71805f_remove(struct platform_device *pdev)
1405{
1406 struct f71805f_data *data = platform_get_drvdata(pdev);
Jean Delvarece7ee4e2007-05-08 17:21:59 +02001407 struct resource *res;
Jean Delvare0e39e012006-09-24 21:16:40 +02001408 int i;
Jean Delvaree53004e2006-01-09 23:26:14 +01001409
Jean Delvaree53004e2006-01-09 23:26:14 +01001410 hwmon_device_unregister(data->class_dev);
Jean Delvare0e39e012006-09-24 21:16:40 +02001411 sysfs_remove_group(&pdev->dev.kobj, &f71805f_group);
Jean Delvare51c997d2006-12-12 18:18:29 +01001412 for (i = 0; i < 4; i++)
1413 sysfs_remove_group(&pdev->dev.kobj, &f71805f_group_optin[i]);
Jean Delvaree1967832006-12-12 18:18:27 +01001414 sysfs_remove_group(&pdev->dev.kobj, &f71805f_group_pwm_freq);
Jean Delvare04a62172007-06-12 13:57:19 +02001415 platform_set_drvdata(pdev, NULL);
Jean Delvaree53004e2006-01-09 23:26:14 +01001416 kfree(data);
1417
Jean Delvarece7ee4e2007-05-08 17:21:59 +02001418 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
1419 release_region(res->start + ADDR_REG_OFFSET, 2);
1420
Jean Delvaree53004e2006-01-09 23:26:14 +01001421 return 0;
1422}
1423
1424static struct platform_driver f71805f_driver = {
1425 .driver = {
1426 .owner = THIS_MODULE,
1427 .name = DRVNAME,
1428 },
1429 .probe = f71805f_probe,
1430 .remove = __devexit_p(f71805f_remove),
1431};
1432
Jean Delvare51c997d2006-12-12 18:18:29 +01001433static int __init f71805f_device_add(unsigned short address,
1434 const struct f71805f_sio_data *sio_data)
Jean Delvaree53004e2006-01-09 23:26:14 +01001435{
Jean Delvare568825c2006-03-23 16:40:23 +01001436 struct resource res = {
1437 .start = address,
1438 .end = address + REGION_LENGTH - 1,
1439 .flags = IORESOURCE_IO,
1440 };
Jean Delvaree53004e2006-01-09 23:26:14 +01001441 int err;
1442
1443 pdev = platform_device_alloc(DRVNAME, address);
1444 if (!pdev) {
1445 err = -ENOMEM;
1446 printk(KERN_ERR DRVNAME ": Device allocation failed\n");
1447 goto exit;
1448 }
1449
Jean Delvare568825c2006-03-23 16:40:23 +01001450 res.name = pdev->name;
1451 err = platform_device_add_resources(pdev, &res, 1);
Jean Delvaree53004e2006-01-09 23:26:14 +01001452 if (err) {
1453 printk(KERN_ERR DRVNAME ": Device resource addition failed "
1454 "(%d)\n", err);
1455 goto exit_device_put;
1456 }
1457
Jean Delvare2df6d812007-06-09 10:11:16 -04001458 err = platform_device_add_data(pdev, sio_data,
1459 sizeof(struct f71805f_sio_data));
1460 if (err) {
Jean Delvare51c997d2006-12-12 18:18:29 +01001461 printk(KERN_ERR DRVNAME ": Platform data allocation failed\n");
1462 goto exit_device_put;
1463 }
Jean Delvare51c997d2006-12-12 18:18:29 +01001464
Jean Delvaree53004e2006-01-09 23:26:14 +01001465 err = platform_device_add(pdev);
1466 if (err) {
1467 printk(KERN_ERR DRVNAME ": Device addition failed (%d)\n",
1468 err);
Jean Delvarea117ddd2007-02-14 21:15:05 +01001469 goto exit_device_put;
Jean Delvaree53004e2006-01-09 23:26:14 +01001470 }
1471
1472 return 0;
1473
1474exit_device_put:
1475 platform_device_put(pdev);
1476exit:
1477 return err;
1478}
1479
Jean Delvare51c997d2006-12-12 18:18:29 +01001480static int __init f71805f_find(int sioaddr, unsigned short *address,
1481 struct f71805f_sio_data *sio_data)
Jean Delvaree53004e2006-01-09 23:26:14 +01001482{
1483 int err = -ENODEV;
1484 u16 devid;
1485
Jean Delvare51c997d2006-12-12 18:18:29 +01001486 static const char *names[] = {
1487 "F71805F/FG",
1488 "F71872F/FG",
1489 };
1490
Jean Delvaree53004e2006-01-09 23:26:14 +01001491 superio_enter(sioaddr);
1492
1493 devid = superio_inw(sioaddr, SIO_REG_MANID);
1494 if (devid != SIO_FINTEK_ID)
1495 goto exit;
1496
1497 devid = superio_inw(sioaddr, SIO_REG_DEVID);
Jean Delvare51c997d2006-12-12 18:18:29 +01001498 switch (devid) {
1499 case SIO_F71805F_ID:
1500 sio_data->kind = f71805f;
1501 break;
1502 case SIO_F71872F_ID:
1503 sio_data->kind = f71872f;
1504 sio_data->fnsel1 = superio_inb(sioaddr, SIO_REG_FNSEL1);
1505 break;
1506 default:
Jean Delvaree53004e2006-01-09 23:26:14 +01001507 printk(KERN_INFO DRVNAME ": Unsupported Fintek device, "
1508 "skipping\n");
1509 goto exit;
1510 }
1511
1512 superio_select(sioaddr, F71805F_LD_HWM);
1513 if (!(superio_inb(sioaddr, SIO_REG_ENABLE) & 0x01)) {
1514 printk(KERN_WARNING DRVNAME ": Device not activated, "
1515 "skipping\n");
1516 goto exit;
1517 }
1518
1519 *address = superio_inw(sioaddr, SIO_REG_ADDR);
1520 if (*address == 0) {
1521 printk(KERN_WARNING DRVNAME ": Base address not set, "
1522 "skipping\n");
1523 goto exit;
1524 }
Jean Delvare75c99022006-12-12 18:18:29 +01001525 *address &= ~(REGION_LENGTH - 1); /* Ignore 3 LSB */
Jean Delvaree53004e2006-01-09 23:26:14 +01001526
1527 err = 0;
Jean Delvare51c997d2006-12-12 18:18:29 +01001528 printk(KERN_INFO DRVNAME ": Found %s chip at %#x, revision %u\n",
1529 names[sio_data->kind], *address,
1530 superio_inb(sioaddr, SIO_REG_DEVREV));
Jean Delvaree53004e2006-01-09 23:26:14 +01001531
1532exit:
1533 superio_exit(sioaddr);
1534 return err;
1535}
1536
1537static int __init f71805f_init(void)
1538{
1539 int err;
1540 unsigned short address;
Jean Delvare51c997d2006-12-12 18:18:29 +01001541 struct f71805f_sio_data sio_data;
Jean Delvaree53004e2006-01-09 23:26:14 +01001542
Jean Delvare51c997d2006-12-12 18:18:29 +01001543 if (f71805f_find(0x2e, &address, &sio_data)
1544 && f71805f_find(0x4e, &address, &sio_data))
Jean Delvaree53004e2006-01-09 23:26:14 +01001545 return -ENODEV;
1546
1547 err = platform_driver_register(&f71805f_driver);
1548 if (err)
1549 goto exit;
1550
1551 /* Sets global pdev as a side effect */
Jean Delvare51c997d2006-12-12 18:18:29 +01001552 err = f71805f_device_add(address, &sio_data);
Jean Delvaree53004e2006-01-09 23:26:14 +01001553 if (err)
1554 goto exit_driver;
1555
1556 return 0;
1557
1558exit_driver:
1559 platform_driver_unregister(&f71805f_driver);
1560exit:
1561 return err;
1562}
1563
1564static void __exit f71805f_exit(void)
1565{
1566 platform_device_unregister(pdev);
1567 platform_driver_unregister(&f71805f_driver);
1568}
1569
1570MODULE_AUTHOR("Jean Delvare <khali@linux-fr>");
1571MODULE_LICENSE("GPL");
Jean Delvare51c997d2006-12-12 18:18:29 +01001572MODULE_DESCRIPTION("F71805F/F71872F hardware monitoring driver");
Jean Delvaree53004e2006-01-09 23:26:14 +01001573
1574module_init(f71805f_init);
1575module_exit(f71805f_exit);