blob: 173e2d2cb49c76fa9801b471791979c56ab3ebb6 [file] [log] [blame]
Jean Delvaree53004e2006-01-09 23:26:14 +01001/*
2 * f71805f.c - driver for the Fintek F71805F/FG Super-I/O chip integrated
3 * hardware monitoring features
Jean Delvare2d457712006-09-24 20:52:15 +02004 * Copyright (C) 2005-2006 Jean Delvare <khali@linux-fr.org>
Jean Delvaree53004e2006-01-09 23:26:14 +01005 *
6 * The F71805F/FG is a LPC Super-I/O chip made by Fintek. It integrates
7 * complete hardware monitoring features: voltage, fan and temperature
8 * sensors, and manual and automatic fan speed control.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 */
24
25#include <linux/module.h>
26#include <linux/init.h>
27#include <linux/slab.h>
28#include <linux/jiffies.h>
29#include <linux/platform_device.h>
30#include <linux/hwmon.h>
31#include <linux/hwmon-sysfs.h>
32#include <linux/err.h>
Jean Delvaref0819182006-01-18 23:20:53 +010033#include <linux/mutex.h>
Jean Delvare0e39e012006-09-24 21:16:40 +020034#include <linux/sysfs.h>
Jean Delvaree53004e2006-01-09 23:26:14 +010035#include <asm/io.h>
36
37static struct platform_device *pdev;
38
39#define DRVNAME "f71805f"
40
41/*
42 * Super-I/O constants and functions
43 */
44
45#define F71805F_LD_HWM 0x04
46
47#define SIO_REG_LDSEL 0x07 /* Logical device select */
48#define SIO_REG_DEVID 0x20 /* Device ID (2 bytes) */
49#define SIO_REG_DEVREV 0x22 /* Device revision */
50#define SIO_REG_MANID 0x23 /* Fintek ID (2 bytes) */
51#define SIO_REG_ENABLE 0x30 /* Logical device enable */
52#define SIO_REG_ADDR 0x60 /* Logical device address (2 bytes) */
53
54#define SIO_FINTEK_ID 0x1934
55#define SIO_F71805F_ID 0x0406
56
57static inline int
58superio_inb(int base, int reg)
59{
60 outb(reg, base);
61 return inb(base + 1);
62}
63
64static int
65superio_inw(int base, int reg)
66{
67 int val;
68 outb(reg++, base);
69 val = inb(base + 1) << 8;
70 outb(reg, base);
71 val |= inb(base + 1);
72 return val;
73}
74
75static inline void
76superio_select(int base, int ld)
77{
78 outb(SIO_REG_LDSEL, base);
79 outb(ld, base + 1);
80}
81
82static inline void
83superio_enter(int base)
84{
85 outb(0x87, base);
86 outb(0x87, base);
87}
88
89static inline void
90superio_exit(int base)
91{
92 outb(0xaa, base);
93}
94
95/*
96 * ISA constants
97 */
98
99#define REGION_LENGTH 2
100#define ADDR_REG_OFFSET 0
101#define DATA_REG_OFFSET 1
102
Jean Delvaree53004e2006-01-09 23:26:14 +0100103/*
104 * Registers
105 */
106
107/* in nr from 0 to 8 (8-bit values) */
108#define F71805F_REG_IN(nr) (0x10 + (nr))
109#define F71805F_REG_IN_HIGH(nr) (0x40 + 2 * (nr))
110#define F71805F_REG_IN_LOW(nr) (0x41 + 2 * (nr))
111/* fan nr from 0 to 2 (12-bit values, two registers) */
112#define F71805F_REG_FAN(nr) (0x20 + 2 * (nr))
113#define F71805F_REG_FAN_LOW(nr) (0x28 + 2 * (nr))
114#define F71805F_REG_FAN_CTRL(nr) (0x60 + 16 * (nr))
Jean Delvare6e2bc172006-12-12 18:18:27 +0100115#define F71805F_REG_PWM_FREQ(nr) (0x63 + 16 * (nr))
Jean Delvare95e35312006-12-12 18:18:26 +0100116#define F71805F_REG_PWM_DUTY(nr) (0x6B + 16 * (nr))
Jean Delvaree53004e2006-01-09 23:26:14 +0100117/* temp nr from 0 to 2 (8-bit values) */
118#define F71805F_REG_TEMP(nr) (0x1B + (nr))
119#define F71805F_REG_TEMP_HIGH(nr) (0x54 + 2 * (nr))
120#define F71805F_REG_TEMP_HYST(nr) (0x55 + 2 * (nr))
121#define F71805F_REG_TEMP_MODE 0x01
122
123#define F71805F_REG_START 0x00
124/* status nr from 0 to 2 */
125#define F71805F_REG_STATUS(nr) (0x36 + (nr))
126
Jean Delvare6b14a542006-12-12 18:18:26 +0100127/* individual register bits */
128#define FAN_CTRL_SKIP 0x80
Jean Delvare95e35312006-12-12 18:18:26 +0100129#define FAN_CTRL_MODE_MASK 0x03
130#define FAN_CTRL_MODE_SPEED 0x00
131#define FAN_CTRL_MODE_TEMPERATURE 0x01
132#define FAN_CTRL_MODE_MANUAL 0x02
Jean Delvare6b14a542006-12-12 18:18:26 +0100133
Jean Delvaree53004e2006-01-09 23:26:14 +0100134/*
135 * Data structures and manipulation thereof
136 */
137
138struct f71805f_data {
139 unsigned short addr;
140 const char *name;
Jean Delvaref0819182006-01-18 23:20:53 +0100141 struct mutex lock;
Jean Delvaree53004e2006-01-09 23:26:14 +0100142 struct class_device *class_dev;
143
Jean Delvaref0819182006-01-18 23:20:53 +0100144 struct mutex update_lock;
Jean Delvaree53004e2006-01-09 23:26:14 +0100145 char valid; /* !=0 if following fields are valid */
146 unsigned long last_updated; /* In jiffies */
147 unsigned long last_limits; /* In jiffies */
148
149 /* Register values */
150 u8 in[9];
151 u8 in_high[9];
152 u8 in_low[9];
153 u16 fan[3];
154 u16 fan_low[3];
Jean Delvare6b14a542006-12-12 18:18:26 +0100155 u8 fan_ctrl[3];
Jean Delvare95e35312006-12-12 18:18:26 +0100156 u8 pwm[3];
Jean Delvare6e2bc172006-12-12 18:18:27 +0100157 u8 pwm_freq[3];
Jean Delvaree53004e2006-01-09 23:26:14 +0100158 u8 temp[3];
159 u8 temp_high[3];
160 u8 temp_hyst[3];
161 u8 temp_mode;
Jean Delvare2d457712006-09-24 20:52:15 +0200162 unsigned long alarms;
Jean Delvaree53004e2006-01-09 23:26:14 +0100163};
164
165static inline long in_from_reg(u8 reg)
166{
167 return (reg * 8);
168}
169
170/* The 2 least significant bits are not used */
171static inline u8 in_to_reg(long val)
172{
173 if (val <= 0)
174 return 0;
175 if (val >= 2016)
176 return 0xfc;
177 return (((val + 16) / 32) << 2);
178}
179
180/* in0 is downscaled by a factor 2 internally */
181static inline long in0_from_reg(u8 reg)
182{
183 return (reg * 16);
184}
185
186static inline u8 in0_to_reg(long val)
187{
188 if (val <= 0)
189 return 0;
190 if (val >= 4032)
191 return 0xfc;
192 return (((val + 32) / 64) << 2);
193}
194
195/* The 4 most significant bits are not used */
196static inline long fan_from_reg(u16 reg)
197{
198 reg &= 0xfff;
199 if (!reg || reg == 0xfff)
200 return 0;
201 return (1500000 / reg);
202}
203
204static inline u16 fan_to_reg(long rpm)
205{
206 /* If the low limit is set below what the chip can measure,
207 store the largest possible 12-bit value in the registers,
208 so that no alarm will ever trigger. */
209 if (rpm < 367)
210 return 0xfff;
211 return (1500000 / rpm);
212}
213
Jean Delvare6e2bc172006-12-12 18:18:27 +0100214static inline unsigned long pwm_freq_from_reg(u8 reg)
215{
216 unsigned long clock = (reg & 0x80) ? 48000000UL : 1000000UL;
217
218 reg &= 0x7f;
219 if (reg == 0)
220 reg++;
221 return clock / (reg << 8);
222}
223
224static inline u8 pwm_freq_to_reg(unsigned long val)
225{
226 if (val >= 187500) /* The highest we can do */
227 return 0x80;
228 if (val >= 1475) /* Use 48 MHz clock */
229 return 0x80 | (48000000UL / (val << 8));
230 if (val < 31) /* The lowest we can do */
231 return 0x7f;
232 else /* Use 1 MHz clock */
233 return 1000000UL / (val << 8);
234}
235
Jean Delvaree53004e2006-01-09 23:26:14 +0100236static inline long temp_from_reg(u8 reg)
237{
238 return (reg * 1000);
239}
240
241static inline u8 temp_to_reg(long val)
242{
243 if (val < 0)
244 val = 0;
245 else if (val > 1000 * 0xff)
246 val = 0xff;
247 return ((val + 500) / 1000);
248}
249
250/*
251 * Device I/O access
252 */
253
254static u8 f71805f_read8(struct f71805f_data *data, u8 reg)
255{
256 u8 val;
257
Jean Delvaref0819182006-01-18 23:20:53 +0100258 mutex_lock(&data->lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100259 outb(reg, data->addr + ADDR_REG_OFFSET);
260 val = inb(data->addr + DATA_REG_OFFSET);
Jean Delvaref0819182006-01-18 23:20:53 +0100261 mutex_unlock(&data->lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100262
263 return val;
264}
265
266static void f71805f_write8(struct f71805f_data *data, u8 reg, u8 val)
267{
Jean Delvaref0819182006-01-18 23:20:53 +0100268 mutex_lock(&data->lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100269 outb(reg, data->addr + ADDR_REG_OFFSET);
270 outb(val, data->addr + DATA_REG_OFFSET);
Jean Delvaref0819182006-01-18 23:20:53 +0100271 mutex_unlock(&data->lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100272}
273
274/* It is important to read the MSB first, because doing so latches the
275 value of the LSB, so we are sure both bytes belong to the same value. */
276static u16 f71805f_read16(struct f71805f_data *data, u8 reg)
277{
278 u16 val;
279
Jean Delvaref0819182006-01-18 23:20:53 +0100280 mutex_lock(&data->lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100281 outb(reg, data->addr + ADDR_REG_OFFSET);
282 val = inb(data->addr + DATA_REG_OFFSET) << 8;
283 outb(++reg, data->addr + ADDR_REG_OFFSET);
284 val |= inb(data->addr + DATA_REG_OFFSET);
Jean Delvaref0819182006-01-18 23:20:53 +0100285 mutex_unlock(&data->lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100286
287 return val;
288}
289
290static void f71805f_write16(struct f71805f_data *data, u8 reg, u16 val)
291{
Jean Delvaref0819182006-01-18 23:20:53 +0100292 mutex_lock(&data->lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100293 outb(reg, data->addr + ADDR_REG_OFFSET);
294 outb(val >> 8, data->addr + DATA_REG_OFFSET);
295 outb(++reg, data->addr + ADDR_REG_OFFSET);
296 outb(val & 0xff, data->addr + DATA_REG_OFFSET);
Jean Delvaref0819182006-01-18 23:20:53 +0100297 mutex_unlock(&data->lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100298}
299
300static struct f71805f_data *f71805f_update_device(struct device *dev)
301{
302 struct f71805f_data *data = dev_get_drvdata(dev);
303 int nr;
304
Jean Delvaref0819182006-01-18 23:20:53 +0100305 mutex_lock(&data->update_lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100306
307 /* Limit registers cache is refreshed after 60 seconds */
308 if (time_after(jiffies, data->last_updated + 60 * HZ)
309 || !data->valid) {
310 for (nr = 0; nr < 9; nr++) {
311 data->in_high[nr] = f71805f_read8(data,
312 F71805F_REG_IN_HIGH(nr));
313 data->in_low[nr] = f71805f_read8(data,
314 F71805F_REG_IN_LOW(nr));
315 }
316 for (nr = 0; nr < 3; nr++) {
Jean Delvare6b14a542006-12-12 18:18:26 +0100317 if (data->fan_ctrl[nr] & FAN_CTRL_SKIP)
318 continue;
319 data->fan_low[nr] = f71805f_read16(data,
320 F71805F_REG_FAN_LOW(nr));
Jean Delvare6e2bc172006-12-12 18:18:27 +0100321 data->pwm_freq[nr] = f71805f_read8(data,
322 F71805F_REG_PWM_FREQ(nr));
Jean Delvaree53004e2006-01-09 23:26:14 +0100323 }
324 for (nr = 0; nr < 3; nr++) {
325 data->temp_high[nr] = f71805f_read8(data,
326 F71805F_REG_TEMP_HIGH(nr));
327 data->temp_hyst[nr] = f71805f_read8(data,
328 F71805F_REG_TEMP_HYST(nr));
329 }
330 data->temp_mode = f71805f_read8(data, F71805F_REG_TEMP_MODE);
331
332 data->last_limits = jiffies;
333 }
334
335 /* Measurement registers cache is refreshed after 1 second */
336 if (time_after(jiffies, data->last_updated + HZ)
337 || !data->valid) {
338 for (nr = 0; nr < 9; nr++) {
339 data->in[nr] = f71805f_read8(data,
340 F71805F_REG_IN(nr));
341 }
342 for (nr = 0; nr < 3; nr++) {
Jean Delvare6b14a542006-12-12 18:18:26 +0100343 if (data->fan_ctrl[nr] & FAN_CTRL_SKIP)
344 continue;
345 data->fan[nr] = f71805f_read16(data,
346 F71805F_REG_FAN(nr));
Jean Delvare95e35312006-12-12 18:18:26 +0100347 data->fan_ctrl[nr] = f71805f_read8(data,
348 F71805F_REG_FAN_CTRL(nr));
349 data->pwm[nr] = f71805f_read8(data,
350 F71805F_REG_PWM_DUTY(nr));
Jean Delvaree53004e2006-01-09 23:26:14 +0100351 }
352 for (nr = 0; nr < 3; nr++) {
353 data->temp[nr] = f71805f_read8(data,
354 F71805F_REG_TEMP(nr));
355 }
Jean Delvare2d457712006-09-24 20:52:15 +0200356 data->alarms = f71805f_read8(data, F71805F_REG_STATUS(0))
357 + (f71805f_read8(data, F71805F_REG_STATUS(1)) << 8)
358 + (f71805f_read8(data, F71805F_REG_STATUS(2)) << 16);
Jean Delvaree53004e2006-01-09 23:26:14 +0100359
360 data->last_updated = jiffies;
361 data->valid = 1;
362 }
363
Jean Delvaref0819182006-01-18 23:20:53 +0100364 mutex_unlock(&data->update_lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100365
366 return data;
367}
368
369/*
370 * Sysfs interface
371 */
372
373static ssize_t show_in0(struct device *dev, struct device_attribute *devattr,
374 char *buf)
375{
376 struct f71805f_data *data = f71805f_update_device(dev);
377
378 return sprintf(buf, "%ld\n", in0_from_reg(data->in[0]));
379}
380
381static ssize_t show_in0_max(struct device *dev, struct device_attribute
382 *devattr, char *buf)
383{
384 struct f71805f_data *data = f71805f_update_device(dev);
385
386 return sprintf(buf, "%ld\n", in0_from_reg(data->in_high[0]));
387}
388
389static ssize_t show_in0_min(struct device *dev, struct device_attribute
390 *devattr, char *buf)
391{
392 struct f71805f_data *data = f71805f_update_device(dev);
393
394 return sprintf(buf, "%ld\n", in0_from_reg(data->in_low[0]));
395}
396
397static ssize_t set_in0_max(struct device *dev, struct device_attribute
398 *devattr, const char *buf, size_t count)
399{
400 struct f71805f_data *data = dev_get_drvdata(dev);
401 long val = simple_strtol(buf, NULL, 10);
402
Jean Delvaref0819182006-01-18 23:20:53 +0100403 mutex_lock(&data->update_lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100404 data->in_high[0] = in0_to_reg(val);
405 f71805f_write8(data, F71805F_REG_IN_HIGH(0), data->in_high[0]);
Jean Delvaref0819182006-01-18 23:20:53 +0100406 mutex_unlock(&data->update_lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100407
408 return count;
409}
410
411static ssize_t set_in0_min(struct device *dev, struct device_attribute
412 *devattr, const char *buf, size_t count)
413{
414 struct f71805f_data *data = dev_get_drvdata(dev);
415 long val = simple_strtol(buf, NULL, 10);
416
Jean Delvaref0819182006-01-18 23:20:53 +0100417 mutex_lock(&data->update_lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100418 data->in_low[0] = in0_to_reg(val);
419 f71805f_write8(data, F71805F_REG_IN_LOW(0), data->in_low[0]);
Jean Delvaref0819182006-01-18 23:20:53 +0100420 mutex_unlock(&data->update_lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100421
422 return count;
423}
424
Jean Delvaree53004e2006-01-09 23:26:14 +0100425static ssize_t show_in(struct device *dev, struct device_attribute *devattr,
426 char *buf)
427{
428 struct f71805f_data *data = f71805f_update_device(dev);
429 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
430 int nr = attr->index;
431
432 return sprintf(buf, "%ld\n", in_from_reg(data->in[nr]));
433}
434
435static ssize_t show_in_max(struct device *dev, struct device_attribute
436 *devattr, char *buf)
437{
438 struct f71805f_data *data = f71805f_update_device(dev);
439 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
440 int nr = attr->index;
441
442 return sprintf(buf, "%ld\n", in_from_reg(data->in_high[nr]));
443}
444
445static ssize_t show_in_min(struct device *dev, struct device_attribute
446 *devattr, char *buf)
447{
448 struct f71805f_data *data = f71805f_update_device(dev);
449 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
450 int nr = attr->index;
451
452 return sprintf(buf, "%ld\n", in_from_reg(data->in_low[nr]));
453}
454
455static ssize_t set_in_max(struct device *dev, struct device_attribute
456 *devattr, const char *buf, size_t count)
457{
458 struct f71805f_data *data = dev_get_drvdata(dev);
459 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
460 int nr = attr->index;
461 long val = simple_strtol(buf, NULL, 10);
462
Jean Delvaref0819182006-01-18 23:20:53 +0100463 mutex_lock(&data->update_lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100464 data->in_high[nr] = in_to_reg(val);
465 f71805f_write8(data, F71805F_REG_IN_HIGH(nr), data->in_high[nr]);
Jean Delvaref0819182006-01-18 23:20:53 +0100466 mutex_unlock(&data->update_lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100467
468 return count;
469}
470
471static ssize_t set_in_min(struct device *dev, struct device_attribute
472 *devattr, const char *buf, size_t count)
473{
474 struct f71805f_data *data = dev_get_drvdata(dev);
475 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
476 int nr = attr->index;
477 long val = simple_strtol(buf, NULL, 10);
478
Jean Delvaref0819182006-01-18 23:20:53 +0100479 mutex_lock(&data->update_lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100480 data->in_low[nr] = in_to_reg(val);
481 f71805f_write8(data, F71805F_REG_IN_LOW(nr), data->in_low[nr]);
Jean Delvaref0819182006-01-18 23:20:53 +0100482 mutex_unlock(&data->update_lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100483
484 return count;
485}
486
Jean Delvaree53004e2006-01-09 23:26:14 +0100487static ssize_t show_fan(struct device *dev, struct device_attribute *devattr,
488 char *buf)
489{
490 struct f71805f_data *data = f71805f_update_device(dev);
491 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
492 int nr = attr->index;
493
494 return sprintf(buf, "%ld\n", fan_from_reg(data->fan[nr]));
495}
496
497static ssize_t show_fan_min(struct device *dev, struct device_attribute
498 *devattr, char *buf)
499{
500 struct f71805f_data *data = f71805f_update_device(dev);
501 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
502 int nr = attr->index;
503
504 return sprintf(buf, "%ld\n", fan_from_reg(data->fan_low[nr]));
505}
506
507static ssize_t set_fan_min(struct device *dev, struct device_attribute
508 *devattr, const char *buf, size_t count)
509{
510 struct f71805f_data *data = dev_get_drvdata(dev);
511 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
512 int nr = attr->index;
513 long val = simple_strtol(buf, NULL, 10);
514
Jean Delvaref0819182006-01-18 23:20:53 +0100515 mutex_lock(&data->update_lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100516 data->fan_low[nr] = fan_to_reg(val);
517 f71805f_write16(data, F71805F_REG_FAN_LOW(nr), data->fan_low[nr]);
Jean Delvaref0819182006-01-18 23:20:53 +0100518 mutex_unlock(&data->update_lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100519
520 return count;
521}
522
Jean Delvare95e35312006-12-12 18:18:26 +0100523static ssize_t show_pwm(struct device *dev, struct device_attribute *devattr,
524 char *buf)
525{
526 struct f71805f_data *data = f71805f_update_device(dev);
527 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
528 int nr = attr->index;
529
530 return sprintf(buf, "%d\n", (int)data->pwm[nr]);
531}
532
533static ssize_t show_pwm_enable(struct device *dev, struct device_attribute
534 *devattr, char *buf)
535{
536 struct f71805f_data *data = f71805f_update_device(dev);
537 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
538 int nr = attr->index;
539 int mode;
540
541 switch (data->fan_ctrl[nr] & FAN_CTRL_MODE_MASK) {
542 case FAN_CTRL_MODE_SPEED:
543 mode = 3;
544 break;
545 case FAN_CTRL_MODE_TEMPERATURE:
546 mode = 2;
547 break;
548 default: /* MANUAL */
549 mode = 1;
550 }
551
552 return sprintf(buf, "%d\n", mode);
553}
554
Jean Delvare6e2bc172006-12-12 18:18:27 +0100555static ssize_t show_pwm_freq(struct device *dev, struct device_attribute
556 *devattr, char *buf)
557{
558 struct f71805f_data *data = f71805f_update_device(dev);
559 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
560 int nr = attr->index;
561
562 return sprintf(buf, "%lu\n", pwm_freq_from_reg(data->pwm_freq[nr]));
563}
564
Jean Delvare95e35312006-12-12 18:18:26 +0100565static ssize_t set_pwm(struct device *dev, struct device_attribute *devattr,
566 const char *buf, size_t count)
567{
568 struct f71805f_data *data = dev_get_drvdata(dev);
569 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
570 int nr = attr->index;
571 unsigned long val = simple_strtoul(buf, NULL, 10);
572
573 if (val > 255)
574 return -EINVAL;
575
576 mutex_lock(&data->update_lock);
577 data->pwm[nr] = val;
578 f71805f_write8(data, F71805F_REG_PWM_DUTY(nr), data->pwm[nr]);
579 mutex_unlock(&data->update_lock);
580
581 return count;
582}
583
584static struct attribute *f71805f_attr_pwm[];
585
586static ssize_t set_pwm_enable(struct device *dev, struct device_attribute
587 *devattr, const char *buf, size_t count)
588{
589 struct f71805f_data *data = dev_get_drvdata(dev);
590 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
591 int nr = attr->index;
592 unsigned long val = simple_strtoul(buf, NULL, 10);
593 u8 reg;
594
595 if (val < 1 || val > 3)
596 return -EINVAL;
597
598 if (val > 1) { /* Automatic mode, user can't set PWM value */
599 if (sysfs_chmod_file(&dev->kobj, f71805f_attr_pwm[nr],
600 S_IRUGO))
601 dev_dbg(dev, "chmod -w pwm%d failed\n", nr + 1);
602 }
603
604 mutex_lock(&data->update_lock);
605 reg = f71805f_read8(data, F71805F_REG_FAN_CTRL(nr))
606 & ~FAN_CTRL_MODE_MASK;
607 switch (val) {
608 case 1:
609 reg |= FAN_CTRL_MODE_MANUAL;
610 break;
611 case 2:
612 reg |= FAN_CTRL_MODE_TEMPERATURE;
613 break;
614 case 3:
615 reg |= FAN_CTRL_MODE_SPEED;
616 break;
617 }
618 data->fan_ctrl[nr] = reg;
619 f71805f_write8(data, F71805F_REG_FAN_CTRL(nr), reg);
620 mutex_unlock(&data->update_lock);
621
622 if (val == 1) { /* Manual mode, user can set PWM value */
623 if (sysfs_chmod_file(&dev->kobj, f71805f_attr_pwm[nr],
624 S_IRUGO | S_IWUSR))
625 dev_dbg(dev, "chmod +w pwm%d failed\n", nr + 1);
626 }
627
628 return count;
629}
630
Jean Delvare6e2bc172006-12-12 18:18:27 +0100631static ssize_t set_pwm_freq(struct device *dev, struct device_attribute
632 *devattr, const char *buf, size_t count)
633{
634 struct f71805f_data *data = dev_get_drvdata(dev);
635 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
636 int nr = attr->index;
637 unsigned long val = simple_strtoul(buf, NULL, 10);
638
639 mutex_lock(&data->update_lock);
640 data->pwm_freq[nr] = pwm_freq_to_reg(val);
641 f71805f_write8(data, F71805F_REG_PWM_FREQ(nr), data->pwm_freq[nr]);
642 mutex_unlock(&data->update_lock);
643
644 return count;
645}
646
Jean Delvaree53004e2006-01-09 23:26:14 +0100647static ssize_t show_temp(struct device *dev, struct device_attribute *devattr,
648 char *buf)
649{
650 struct f71805f_data *data = f71805f_update_device(dev);
651 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
652 int nr = attr->index;
653
654 return sprintf(buf, "%ld\n", temp_from_reg(data->temp[nr]));
655}
656
657static ssize_t show_temp_max(struct device *dev, struct device_attribute
658 *devattr, char *buf)
659{
660 struct f71805f_data *data = f71805f_update_device(dev);
661 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
662 int nr = attr->index;
663
664 return sprintf(buf, "%ld\n", temp_from_reg(data->temp_high[nr]));
665}
666
667static ssize_t show_temp_hyst(struct device *dev, struct device_attribute
668 *devattr, char *buf)
669{
670 struct f71805f_data *data = f71805f_update_device(dev);
671 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
672 int nr = attr->index;
673
674 return sprintf(buf, "%ld\n", temp_from_reg(data->temp_hyst[nr]));
675}
676
677static ssize_t show_temp_type(struct device *dev, struct device_attribute
678 *devattr, char *buf)
679{
680 struct f71805f_data *data = f71805f_update_device(dev);
681 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
682 int nr = attr->index;
683
684 /* 3 is diode, 4 is thermistor */
685 return sprintf(buf, "%u\n", (data->temp_mode & (1 << nr)) ? 3 : 4);
686}
687
688static ssize_t set_temp_max(struct device *dev, struct device_attribute
689 *devattr, const char *buf, size_t count)
690{
691 struct f71805f_data *data = dev_get_drvdata(dev);
692 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
693 int nr = attr->index;
694 long val = simple_strtol(buf, NULL, 10);
695
Jean Delvaref0819182006-01-18 23:20:53 +0100696 mutex_lock(&data->update_lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100697 data->temp_high[nr] = temp_to_reg(val);
698 f71805f_write8(data, F71805F_REG_TEMP_HIGH(nr), data->temp_high[nr]);
Jean Delvaref0819182006-01-18 23:20:53 +0100699 mutex_unlock(&data->update_lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100700
701 return count;
702}
703
704static ssize_t set_temp_hyst(struct device *dev, struct device_attribute
705 *devattr, const char *buf, size_t count)
706{
707 struct f71805f_data *data = dev_get_drvdata(dev);
708 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
709 int nr = attr->index;
710 long val = simple_strtol(buf, NULL, 10);
711
Jean Delvaref0819182006-01-18 23:20:53 +0100712 mutex_lock(&data->update_lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100713 data->temp_hyst[nr] = temp_to_reg(val);
714 f71805f_write8(data, F71805F_REG_TEMP_HYST(nr), data->temp_hyst[nr]);
Jean Delvaref0819182006-01-18 23:20:53 +0100715 mutex_unlock(&data->update_lock);
Jean Delvaree53004e2006-01-09 23:26:14 +0100716
717 return count;
718}
719
Jean Delvaree53004e2006-01-09 23:26:14 +0100720static ssize_t show_alarms_in(struct device *dev, struct device_attribute
721 *devattr, char *buf)
722{
723 struct f71805f_data *data = f71805f_update_device(dev);
724
Jean Delvare2d457712006-09-24 20:52:15 +0200725 return sprintf(buf, "%lu\n", data->alarms & 0x1ff);
Jean Delvaree53004e2006-01-09 23:26:14 +0100726}
727
728static ssize_t show_alarms_fan(struct device *dev, struct device_attribute
729 *devattr, char *buf)
730{
731 struct f71805f_data *data = f71805f_update_device(dev);
732
Jean Delvare2d457712006-09-24 20:52:15 +0200733 return sprintf(buf, "%lu\n", (data->alarms >> 16) & 0x07);
Jean Delvaree53004e2006-01-09 23:26:14 +0100734}
735
736static ssize_t show_alarms_temp(struct device *dev, struct device_attribute
737 *devattr, char *buf)
738{
739 struct f71805f_data *data = f71805f_update_device(dev);
740
Jean Delvare2d457712006-09-24 20:52:15 +0200741 return sprintf(buf, "%lu\n", (data->alarms >> 11) & 0x07);
742}
743
744static ssize_t show_alarm(struct device *dev, struct device_attribute
745 *devattr, char *buf)
746{
747 struct f71805f_data *data = f71805f_update_device(dev);
748 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
749 int bitnr = attr->index;
750
751 return sprintf(buf, "%lu\n", (data->alarms >> bitnr) & 1);
Jean Delvaree53004e2006-01-09 23:26:14 +0100752}
753
Jean Delvaree53004e2006-01-09 23:26:14 +0100754static ssize_t show_name(struct device *dev, struct device_attribute
755 *devattr, char *buf)
756{
757 struct f71805f_data *data = dev_get_drvdata(dev);
758
759 return sprintf(buf, "%s\n", data->name);
760}
761
Jean Delvare0e39e012006-09-24 21:16:40 +0200762static DEVICE_ATTR(in0_input, S_IRUGO, show_in0, NULL);
763static DEVICE_ATTR(in0_max, S_IRUGO| S_IWUSR, show_in0_max, set_in0_max);
764static DEVICE_ATTR(in0_min, S_IRUGO| S_IWUSR, show_in0_min, set_in0_min);
765static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, show_in, NULL, 1);
766static SENSOR_DEVICE_ATTR(in1_max, S_IRUGO | S_IWUSR,
767 show_in_max, set_in_max, 1);
768static SENSOR_DEVICE_ATTR(in1_min, S_IRUGO | S_IWUSR,
769 show_in_min, set_in_min, 1);
770static SENSOR_DEVICE_ATTR(in2_input, S_IRUGO, show_in, NULL, 2);
771static SENSOR_DEVICE_ATTR(in2_max, S_IRUGO | S_IWUSR,
772 show_in_max, set_in_max, 2);
773static SENSOR_DEVICE_ATTR(in2_min, S_IRUGO | S_IWUSR,
774 show_in_min, set_in_min, 2);
775static SENSOR_DEVICE_ATTR(in3_input, S_IRUGO, show_in, NULL, 3);
776static SENSOR_DEVICE_ATTR(in3_max, S_IRUGO | S_IWUSR,
777 show_in_max, set_in_max, 3);
778static SENSOR_DEVICE_ATTR(in3_min, S_IRUGO | S_IWUSR,
779 show_in_min, set_in_min, 3);
780static SENSOR_DEVICE_ATTR(in4_input, S_IRUGO, show_in, NULL, 4);
781static SENSOR_DEVICE_ATTR(in4_max, S_IRUGO | S_IWUSR,
782 show_in_max, set_in_max, 4);
783static SENSOR_DEVICE_ATTR(in4_min, S_IRUGO | S_IWUSR,
784 show_in_min, set_in_min, 4);
785static SENSOR_DEVICE_ATTR(in5_input, S_IRUGO, show_in, NULL, 5);
786static SENSOR_DEVICE_ATTR(in5_max, S_IRUGO | S_IWUSR,
787 show_in_max, set_in_max, 5);
788static SENSOR_DEVICE_ATTR(in5_min, S_IRUGO | S_IWUSR,
789 show_in_min, set_in_min, 5);
790static SENSOR_DEVICE_ATTR(in6_input, S_IRUGO, show_in, NULL, 6);
791static SENSOR_DEVICE_ATTR(in6_max, S_IRUGO | S_IWUSR,
792 show_in_max, set_in_max, 6);
793static SENSOR_DEVICE_ATTR(in6_min, S_IRUGO | S_IWUSR,
794 show_in_min, set_in_min, 6);
795static SENSOR_DEVICE_ATTR(in7_input, S_IRUGO, show_in, NULL, 7);
796static SENSOR_DEVICE_ATTR(in7_max, S_IRUGO | S_IWUSR,
797 show_in_max, set_in_max, 7);
798static SENSOR_DEVICE_ATTR(in7_min, S_IRUGO | S_IWUSR,
799 show_in_min, set_in_min, 7);
800static SENSOR_DEVICE_ATTR(in8_input, S_IRUGO, show_in, NULL, 8);
801static SENSOR_DEVICE_ATTR(in8_max, S_IRUGO | S_IWUSR,
802 show_in_max, set_in_max, 8);
803static SENSOR_DEVICE_ATTR(in8_min, S_IRUGO | S_IWUSR,
804 show_in_min, set_in_min, 8);
805
806static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0);
807static SENSOR_DEVICE_ATTR(fan1_min, S_IRUGO | S_IWUSR,
808 show_fan_min, set_fan_min, 0);
809static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, show_fan, NULL, 1);
810static SENSOR_DEVICE_ATTR(fan2_min, S_IRUGO | S_IWUSR,
811 show_fan_min, set_fan_min, 1);
812static SENSOR_DEVICE_ATTR(fan3_input, S_IRUGO, show_fan, NULL, 2);
813static SENSOR_DEVICE_ATTR(fan3_min, S_IRUGO | S_IWUSR,
814 show_fan_min, set_fan_min, 2);
815
816static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
817static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO | S_IWUSR,
818 show_temp_max, set_temp_max, 0);
819static SENSOR_DEVICE_ATTR(temp1_max_hyst, S_IRUGO | S_IWUSR,
820 show_temp_hyst, set_temp_hyst, 0);
821static SENSOR_DEVICE_ATTR(temp1_type, S_IRUGO, show_temp_type, NULL, 0);
822static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1);
823static SENSOR_DEVICE_ATTR(temp2_max, S_IRUGO | S_IWUSR,
824 show_temp_max, set_temp_max, 1);
825static SENSOR_DEVICE_ATTR(temp2_max_hyst, S_IRUGO | S_IWUSR,
826 show_temp_hyst, set_temp_hyst, 1);
827static SENSOR_DEVICE_ATTR(temp2_type, S_IRUGO, show_temp_type, NULL, 1);
828static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, show_temp, NULL, 2);
829static SENSOR_DEVICE_ATTR(temp3_max, S_IRUGO | S_IWUSR,
830 show_temp_max, set_temp_max, 2);
831static SENSOR_DEVICE_ATTR(temp3_max_hyst, S_IRUGO | S_IWUSR,
832 show_temp_hyst, set_temp_hyst, 2);
833static SENSOR_DEVICE_ATTR(temp3_type, S_IRUGO, show_temp_type, NULL, 2);
834
Jean Delvare95e35312006-12-12 18:18:26 +0100835/* pwm (value) files are created read-only, write permission is
836 then added or removed dynamically as needed */
837static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO, show_pwm, set_pwm, 0);
838static SENSOR_DEVICE_ATTR(pwm1_enable, S_IRUGO | S_IWUSR,
839 show_pwm_enable, set_pwm_enable, 0);
Jean Delvare6e2bc172006-12-12 18:18:27 +0100840static SENSOR_DEVICE_ATTR(pwm1_freq, S_IRUGO | S_IWUSR,
841 show_pwm_freq, set_pwm_freq, 0);
Jean Delvare95e35312006-12-12 18:18:26 +0100842static SENSOR_DEVICE_ATTR(pwm2, S_IRUGO, show_pwm, set_pwm, 1);
843static SENSOR_DEVICE_ATTR(pwm2_enable, S_IRUGO | S_IWUSR,
844 show_pwm_enable, set_pwm_enable, 1);
Jean Delvare6e2bc172006-12-12 18:18:27 +0100845static SENSOR_DEVICE_ATTR(pwm2_freq, S_IRUGO | S_IWUSR,
846 show_pwm_freq, set_pwm_freq, 1);
Jean Delvare95e35312006-12-12 18:18:26 +0100847static SENSOR_DEVICE_ATTR(pwm3, S_IRUGO, show_pwm, set_pwm, 2);
848static SENSOR_DEVICE_ATTR(pwm3_enable, S_IRUGO | S_IWUSR,
849 show_pwm_enable, set_pwm_enable, 2);
Jean Delvare6e2bc172006-12-12 18:18:27 +0100850static SENSOR_DEVICE_ATTR(pwm3_freq, S_IRUGO | S_IWUSR,
851 show_pwm_freq, set_pwm_freq, 2);
Jean Delvare95e35312006-12-12 18:18:26 +0100852
Jean Delvare0e39e012006-09-24 21:16:40 +0200853static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0);
854static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1);
855static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2);
856static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3);
857static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 4);
858static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 5);
859static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 6);
860static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 7);
861static SENSOR_DEVICE_ATTR(in8_alarm, S_IRUGO, show_alarm, NULL, 8);
862static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 11);
863static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 12);
864static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 13);
865static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 16);
866static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 17);
867static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 18);
868static DEVICE_ATTR(alarms_in, S_IRUGO, show_alarms_in, NULL);
869static DEVICE_ATTR(alarms_fan, S_IRUGO, show_alarms_fan, NULL);
870static DEVICE_ATTR(alarms_temp, S_IRUGO, show_alarms_temp, NULL);
871
872static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
873
874static struct attribute *f71805f_attributes[] = {
875 &dev_attr_in0_input.attr,
876 &dev_attr_in0_max.attr,
877 &dev_attr_in0_min.attr,
878 &sensor_dev_attr_in1_input.dev_attr.attr,
879 &sensor_dev_attr_in1_max.dev_attr.attr,
880 &sensor_dev_attr_in1_min.dev_attr.attr,
881 &sensor_dev_attr_in2_input.dev_attr.attr,
882 &sensor_dev_attr_in2_max.dev_attr.attr,
883 &sensor_dev_attr_in2_min.dev_attr.attr,
884 &sensor_dev_attr_in3_input.dev_attr.attr,
885 &sensor_dev_attr_in3_max.dev_attr.attr,
886 &sensor_dev_attr_in3_min.dev_attr.attr,
887 &sensor_dev_attr_in4_input.dev_attr.attr,
888 &sensor_dev_attr_in4_max.dev_attr.attr,
889 &sensor_dev_attr_in4_min.dev_attr.attr,
890 &sensor_dev_attr_in5_input.dev_attr.attr,
891 &sensor_dev_attr_in5_max.dev_attr.attr,
892 &sensor_dev_attr_in5_min.dev_attr.attr,
893 &sensor_dev_attr_in6_input.dev_attr.attr,
894 &sensor_dev_attr_in6_max.dev_attr.attr,
895 &sensor_dev_attr_in6_min.dev_attr.attr,
896 &sensor_dev_attr_in7_input.dev_attr.attr,
897 &sensor_dev_attr_in7_max.dev_attr.attr,
898 &sensor_dev_attr_in7_min.dev_attr.attr,
899 &sensor_dev_attr_in8_input.dev_attr.attr,
900 &sensor_dev_attr_in8_max.dev_attr.attr,
901 &sensor_dev_attr_in8_min.dev_attr.attr,
902
903 &sensor_dev_attr_temp1_input.dev_attr.attr,
904 &sensor_dev_attr_temp1_max.dev_attr.attr,
905 &sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
906 &sensor_dev_attr_temp1_type.dev_attr.attr,
907 &sensor_dev_attr_temp2_input.dev_attr.attr,
908 &sensor_dev_attr_temp2_max.dev_attr.attr,
909 &sensor_dev_attr_temp2_max_hyst.dev_attr.attr,
910 &sensor_dev_attr_temp2_type.dev_attr.attr,
911 &sensor_dev_attr_temp3_input.dev_attr.attr,
912 &sensor_dev_attr_temp3_max.dev_attr.attr,
913 &sensor_dev_attr_temp3_max_hyst.dev_attr.attr,
914 &sensor_dev_attr_temp3_type.dev_attr.attr,
915
916 &sensor_dev_attr_in0_alarm.dev_attr.attr,
917 &sensor_dev_attr_in1_alarm.dev_attr.attr,
918 &sensor_dev_attr_in2_alarm.dev_attr.attr,
919 &sensor_dev_attr_in3_alarm.dev_attr.attr,
920 &sensor_dev_attr_in4_alarm.dev_attr.attr,
921 &sensor_dev_attr_in5_alarm.dev_attr.attr,
922 &sensor_dev_attr_in6_alarm.dev_attr.attr,
923 &sensor_dev_attr_in7_alarm.dev_attr.attr,
924 &sensor_dev_attr_in8_alarm.dev_attr.attr,
925 &dev_attr_alarms_in.attr,
926 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
927 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
928 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
929 &dev_attr_alarms_temp.attr,
930 &dev_attr_alarms_fan.attr,
931
932 &dev_attr_name.attr,
933 NULL
Jean Delvare2488a392006-01-09 23:29:11 +0100934};
935
Jean Delvare0e39e012006-09-24 21:16:40 +0200936static const struct attribute_group f71805f_group = {
937 .attrs = f71805f_attributes,
Jean Delvare2488a392006-01-09 23:29:11 +0100938};
939
Jean Delvare6e2bc172006-12-12 18:18:27 +0100940static struct attribute *f71805f_attributes_fan[3][7] = {
Jean Delvare0e39e012006-09-24 21:16:40 +0200941 {
942 &sensor_dev_attr_fan1_input.dev_attr.attr,
943 &sensor_dev_attr_fan1_min.dev_attr.attr,
944 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
Jean Delvare95e35312006-12-12 18:18:26 +0100945 &sensor_dev_attr_pwm1.dev_attr.attr,
946 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
Jean Delvare6e2bc172006-12-12 18:18:27 +0100947 &sensor_dev_attr_pwm1_freq.dev_attr.attr,
Jean Delvare0e39e012006-09-24 21:16:40 +0200948 NULL
949 }, {
950 &sensor_dev_attr_fan2_input.dev_attr.attr,
951 &sensor_dev_attr_fan2_min.dev_attr.attr,
952 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
Jean Delvare95e35312006-12-12 18:18:26 +0100953 &sensor_dev_attr_pwm2.dev_attr.attr,
954 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
Jean Delvare6e2bc172006-12-12 18:18:27 +0100955 &sensor_dev_attr_pwm2_freq.dev_attr.attr,
Jean Delvare0e39e012006-09-24 21:16:40 +0200956 NULL
957 }, {
958 &sensor_dev_attr_fan3_input.dev_attr.attr,
959 &sensor_dev_attr_fan3_min.dev_attr.attr,
960 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
Jean Delvare95e35312006-12-12 18:18:26 +0100961 &sensor_dev_attr_pwm3.dev_attr.attr,
962 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
Jean Delvare6e2bc172006-12-12 18:18:27 +0100963 &sensor_dev_attr_pwm3_freq.dev_attr.attr,
Jean Delvare0e39e012006-09-24 21:16:40 +0200964 NULL
965 }
966};
967
968static const struct attribute_group f71805f_group_fan[3] = {
969 { .attrs = f71805f_attributes_fan[0] },
970 { .attrs = f71805f_attributes_fan[1] },
971 { .attrs = f71805f_attributes_fan[2] },
Jean Delvare2488a392006-01-09 23:29:11 +0100972};
Jean Delvaree53004e2006-01-09 23:26:14 +0100973
Jean Delvare95e35312006-12-12 18:18:26 +0100974/* We also need an indexed access to pwmN files to toggle writability */
975static struct attribute *f71805f_attr_pwm[] = {
976 &sensor_dev_attr_pwm1.dev_attr.attr,
977 &sensor_dev_attr_pwm2.dev_attr.attr,
978 &sensor_dev_attr_pwm3.dev_attr.attr,
979};
980
Jean Delvaree53004e2006-01-09 23:26:14 +0100981/*
982 * Device registration and initialization
983 */
984
985static void __devinit f71805f_init_device(struct f71805f_data *data)
986{
987 u8 reg;
988 int i;
989
990 reg = f71805f_read8(data, F71805F_REG_START);
991 if ((reg & 0x41) != 0x01) {
992 printk(KERN_DEBUG DRVNAME ": Starting monitoring "
993 "operations\n");
994 f71805f_write8(data, F71805F_REG_START, (reg | 0x01) & ~0x40);
995 }
996
997 /* Fan monitoring can be disabled. If it is, we won't be polling
998 the register values, and won't create the related sysfs files. */
999 for (i = 0; i < 3; i++) {
Jean Delvare6b14a542006-12-12 18:18:26 +01001000 data->fan_ctrl[i] = f71805f_read8(data,
1001 F71805F_REG_FAN_CTRL(i));
Jean Delvaree53004e2006-01-09 23:26:14 +01001002 }
1003}
1004
1005static int __devinit f71805f_probe(struct platform_device *pdev)
1006{
1007 struct f71805f_data *data;
1008 struct resource *res;
Jean Delvare2488a392006-01-09 23:29:11 +01001009 int i, err;
Jean Delvaree53004e2006-01-09 23:26:14 +01001010
1011 if (!(data = kzalloc(sizeof(struct f71805f_data), GFP_KERNEL))) {
1012 err = -ENOMEM;
1013 printk(KERN_ERR DRVNAME ": Out of memory\n");
1014 goto exit;
1015 }
1016
1017 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
1018 data->addr = res->start;
Jean Delvaref0819182006-01-18 23:20:53 +01001019 mutex_init(&data->lock);
Jean Delvaree53004e2006-01-09 23:26:14 +01001020 data->name = "f71805f";
Jean Delvaref0819182006-01-18 23:20:53 +01001021 mutex_init(&data->update_lock);
Jean Delvaree53004e2006-01-09 23:26:14 +01001022
1023 platform_set_drvdata(pdev, data);
1024
Jean Delvaree53004e2006-01-09 23:26:14 +01001025 /* Initialize the F71805F chip */
1026 f71805f_init_device(data);
1027
1028 /* Register sysfs interface files */
Jean Delvare0e39e012006-09-24 21:16:40 +02001029 if ((err = sysfs_create_group(&pdev->dev.kobj, &f71805f_group)))
1030 goto exit_free;
1031 for (i = 0; i < 3; i++) {
Jean Delvare6b14a542006-12-12 18:18:26 +01001032 if (data->fan_ctrl[i] & FAN_CTRL_SKIP)
Jean Delvare2488a392006-01-09 23:29:11 +01001033 continue;
Jean Delvare0e39e012006-09-24 21:16:40 +02001034 if ((err = sysfs_create_group(&pdev->dev.kobj,
1035 &f71805f_group_fan[i])))
1036 goto exit_remove_files;
Jean Delvare95e35312006-12-12 18:18:26 +01001037 /* If PWM is in manual mode, add write permission */
1038 if (data->fan_ctrl[i] & FAN_CTRL_MODE_MANUAL) {
1039 if ((err = sysfs_chmod_file(&pdev->dev.kobj,
1040 f71805f_attr_pwm[i],
1041 S_IRUGO | S_IWUSR))) {
1042 dev_err(&pdev->dev, "chmod +w pwm%d failed\n",
1043 i + 1);
1044 goto exit_remove_files;
1045 }
1046 }
Jean Delvare0e39e012006-09-24 21:16:40 +02001047 }
1048
1049 data->class_dev = hwmon_device_register(&pdev->dev);
1050 if (IS_ERR(data->class_dev)) {
1051 err = PTR_ERR(data->class_dev);
1052 dev_err(&pdev->dev, "Class registration failed (%d)\n", err);
1053 goto exit_remove_files;
Jean Delvaree53004e2006-01-09 23:26:14 +01001054 }
Jean Delvaree53004e2006-01-09 23:26:14 +01001055
1056 return 0;
1057
Jean Delvare0e39e012006-09-24 21:16:40 +02001058exit_remove_files:
1059 sysfs_remove_group(&pdev->dev.kobj, &f71805f_group);
1060 for (i = 0; i < 3; i++)
1061 sysfs_remove_group(&pdev->dev.kobj, &f71805f_group_fan[i]);
Jean Delvaree53004e2006-01-09 23:26:14 +01001062exit_free:
Jean Delvare0e39e012006-09-24 21:16:40 +02001063 platform_set_drvdata(pdev, NULL);
Jean Delvaree53004e2006-01-09 23:26:14 +01001064 kfree(data);
1065exit:
1066 return err;
1067}
1068
1069static int __devexit f71805f_remove(struct platform_device *pdev)
1070{
1071 struct f71805f_data *data = platform_get_drvdata(pdev);
Jean Delvare0e39e012006-09-24 21:16:40 +02001072 int i;
Jean Delvaree53004e2006-01-09 23:26:14 +01001073
1074 platform_set_drvdata(pdev, NULL);
1075 hwmon_device_unregister(data->class_dev);
Jean Delvare0e39e012006-09-24 21:16:40 +02001076 sysfs_remove_group(&pdev->dev.kobj, &f71805f_group);
1077 for (i = 0; i < 3; i++)
1078 sysfs_remove_group(&pdev->dev.kobj, &f71805f_group_fan[i]);
Jean Delvaree53004e2006-01-09 23:26:14 +01001079 kfree(data);
1080
1081 return 0;
1082}
1083
1084static struct platform_driver f71805f_driver = {
1085 .driver = {
1086 .owner = THIS_MODULE,
1087 .name = DRVNAME,
1088 },
1089 .probe = f71805f_probe,
1090 .remove = __devexit_p(f71805f_remove),
1091};
1092
1093static int __init f71805f_device_add(unsigned short address)
1094{
Jean Delvare568825c2006-03-23 16:40:23 +01001095 struct resource res = {
1096 .start = address,
1097 .end = address + REGION_LENGTH - 1,
1098 .flags = IORESOURCE_IO,
1099 };
Jean Delvaree53004e2006-01-09 23:26:14 +01001100 int err;
1101
1102 pdev = platform_device_alloc(DRVNAME, address);
1103 if (!pdev) {
1104 err = -ENOMEM;
1105 printk(KERN_ERR DRVNAME ": Device allocation failed\n");
1106 goto exit;
1107 }
1108
Jean Delvare568825c2006-03-23 16:40:23 +01001109 res.name = pdev->name;
1110 err = platform_device_add_resources(pdev, &res, 1);
Jean Delvaree53004e2006-01-09 23:26:14 +01001111 if (err) {
1112 printk(KERN_ERR DRVNAME ": Device resource addition failed "
1113 "(%d)\n", err);
1114 goto exit_device_put;
1115 }
1116
1117 err = platform_device_add(pdev);
1118 if (err) {
1119 printk(KERN_ERR DRVNAME ": Device addition failed (%d)\n",
1120 err);
1121 goto exit_device_put;
1122 }
1123
1124 return 0;
1125
1126exit_device_put:
1127 platform_device_put(pdev);
1128exit:
1129 return err;
1130}
1131
1132static int __init f71805f_find(int sioaddr, unsigned short *address)
1133{
1134 int err = -ENODEV;
1135 u16 devid;
1136
1137 superio_enter(sioaddr);
1138
1139 devid = superio_inw(sioaddr, SIO_REG_MANID);
1140 if (devid != SIO_FINTEK_ID)
1141 goto exit;
1142
1143 devid = superio_inw(sioaddr, SIO_REG_DEVID);
1144 if (devid != SIO_F71805F_ID) {
1145 printk(KERN_INFO DRVNAME ": Unsupported Fintek device, "
1146 "skipping\n");
1147 goto exit;
1148 }
1149
1150 superio_select(sioaddr, F71805F_LD_HWM);
1151 if (!(superio_inb(sioaddr, SIO_REG_ENABLE) & 0x01)) {
1152 printk(KERN_WARNING DRVNAME ": Device not activated, "
1153 "skipping\n");
1154 goto exit;
1155 }
1156
1157 *address = superio_inw(sioaddr, SIO_REG_ADDR);
1158 if (*address == 0) {
1159 printk(KERN_WARNING DRVNAME ": Base address not set, "
1160 "skipping\n");
1161 goto exit;
1162 }
1163
1164 err = 0;
1165 printk(KERN_INFO DRVNAME ": Found F71805F chip at %#x, revision %u\n",
1166 *address, superio_inb(sioaddr, SIO_REG_DEVREV));
1167
1168exit:
1169 superio_exit(sioaddr);
1170 return err;
1171}
1172
1173static int __init f71805f_init(void)
1174{
1175 int err;
1176 unsigned short address;
1177
1178 if (f71805f_find(0x2e, &address)
1179 && f71805f_find(0x4e, &address))
1180 return -ENODEV;
1181
1182 err = platform_driver_register(&f71805f_driver);
1183 if (err)
1184 goto exit;
1185
1186 /* Sets global pdev as a side effect */
1187 err = f71805f_device_add(address);
1188 if (err)
1189 goto exit_driver;
1190
1191 return 0;
1192
1193exit_driver:
1194 platform_driver_unregister(&f71805f_driver);
1195exit:
1196 return err;
1197}
1198
1199static void __exit f71805f_exit(void)
1200{
1201 platform_device_unregister(pdev);
1202 platform_driver_unregister(&f71805f_driver);
1203}
1204
1205MODULE_AUTHOR("Jean Delvare <khali@linux-fr>");
1206MODULE_LICENSE("GPL");
1207MODULE_DESCRIPTION("F71805F hardware monitoring driver");
1208
1209module_init(f71805f_init);
1210module_exit(f71805f_exit);