ide-cris: fix DMA methods
[linux-2.6] / drivers / hwmon / f75375s.c
1 /*
2  * f75375s.c - driver for the Fintek F75375/SP and F75373
3  *             hardware monitoring features
4  * Copyright (C) 2006-2007  Riku Voipio <riku.voipio@movial.fi>
5  *
6  * Datasheets available at:
7  *
8  * f75375:
9  * http://www.fintek.com.tw/files/productfiles/2005111152950.pdf
10  *
11  * f75373:
12  * http://www.fintek.com.tw/files/productfiles/2005111153128.pdf
13  *
14  * This program is free software; you can redistribute it and/or modify
15  * it under the terms of the GNU General Public License as published by
16  * the Free Software Foundation; either version 2 of the License, or
17  * (at your option) any later version.
18  *
19  * This program is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  * GNU General Public License for more details.
23  *
24  * You should have received a copy of the GNU General Public License
25  * along with this program; if not, write to the Free Software
26  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27  *
28  */
29
30 #include <linux/module.h>
31 #include <linux/jiffies.h>
32 #include <linux/hwmon.h>
33 #include <linux/hwmon-sysfs.h>
34 #include <linux/i2c.h>
35 #include <linux/err.h>
36 #include <linux/mutex.h>
37 #include <linux/f75375s.h>
38
39 /* Addresses to scan */
40 static unsigned short normal_i2c[] = { 0x2d, 0x2e, I2C_CLIENT_END };
41
42 /* Insmod parameters */
43 I2C_CLIENT_INSMOD_2(f75373, f75375);
44
45 /* Fintek F75375 registers  */
46 #define F75375_REG_CONFIG0              0x0
47 #define F75375_REG_CONFIG1              0x1
48 #define F75375_REG_CONFIG2              0x2
49 #define F75375_REG_CONFIG3              0x3
50 #define F75375_REG_ADDR                 0x4
51 #define F75375_REG_INTR                 0x31
52 #define F75375_CHIP_ID                  0x5A
53 #define F75375_REG_VERSION              0x5C
54 #define F75375_REG_VENDOR               0x5D
55 #define F75375_REG_FAN_TIMER            0x60
56
57 #define F75375_REG_VOLT(nr)             (0x10 + (nr))
58 #define F75375_REG_VOLT_HIGH(nr)        (0x20 + (nr) * 2)
59 #define F75375_REG_VOLT_LOW(nr)         (0x21 + (nr) * 2)
60
61 #define F75375_REG_TEMP(nr)             (0x14 + (nr))
62 #define F75375_REG_TEMP_HIGH(nr)        (0x28 + (nr) * 2)
63 #define F75375_REG_TEMP_HYST(nr)        (0x29 + (nr) * 2)
64
65 #define F75375_REG_FAN(nr)              (0x16 + (nr) * 2)
66 #define F75375_REG_FAN_MIN(nr)          (0x2C + (nr) * 2)
67 #define F75375_REG_FAN_FULL(nr)         (0x70 + (nr) * 0x10)
68 #define F75375_REG_FAN_PWM_DUTY(nr)     (0x76 + (nr) * 0x10)
69 #define F75375_REG_FAN_PWM_CLOCK(nr)    (0x7D + (nr) * 0x10)
70
71 #define F75375_REG_FAN_EXP(nr)          (0x74 + (nr) * 0x10)
72 #define F75375_REG_FAN_B_TEMP(nr, step) ((0xA0 + (nr) * 0x10) + (step))
73 #define F75375_REG_FAN_B_SPEED(nr, step) \
74         ((0xA5 + (nr) * 0x10) + (step) * 2)
75
76 #define F75375_REG_PWM1_RAISE_DUTY      0x69
77 #define F75375_REG_PWM2_RAISE_DUTY      0x6A
78 #define F75375_REG_PWM1_DROP_DUTY       0x6B
79 #define F75375_REG_PWM2_DROP_DUTY       0x6C
80
81 #define FAN_CTRL_LINEAR(nr)             (4 + nr)
82 #define FAN_CTRL_MODE(nr)               (5 + ((nr) * 2))
83
84 /*
85  * Data structures and manipulation thereof
86  */
87
88 struct f75375_data {
89         unsigned short addr;
90         struct i2c_client *client;
91         struct device *hwmon_dev;
92
93         const char *name;
94         int kind;
95         struct mutex update_lock; /* protect register access */
96         char valid;
97         unsigned long last_updated;     /* In jiffies */
98         unsigned long last_limits;      /* In jiffies */
99
100         /* Register values */
101         u8 in[4];
102         u8 in_max[4];
103         u8 in_min[4];
104         u16 fan[2];
105         u16 fan_min[2];
106         u16 fan_full[2];
107         u16 fan_exp[2];
108         u8 fan_timer;
109         u8 pwm[2];
110         u8 pwm_mode[2];
111         u8 pwm_enable[2];
112         s8 temp[2];
113         s8 temp_high[2];
114         s8 temp_max_hyst[2];
115 };
116
117 static int f75375_attach_adapter(struct i2c_adapter *adapter);
118 static int f75375_detect(struct i2c_adapter *adapter, int address, int kind);
119 static int f75375_detach_client(struct i2c_client *client);
120 static int f75375_probe(struct i2c_client *client);
121 static int f75375_remove(struct i2c_client *client);
122
123 static struct i2c_driver f75375_legacy_driver = {
124         .driver = {
125                 .name = "f75375_legacy",
126         },
127         .attach_adapter = f75375_attach_adapter,
128         .detach_client = f75375_detach_client,
129 };
130
131 static struct i2c_driver f75375_driver = {
132         .driver = {
133                 .name = "f75375",
134         },
135         .probe = f75375_probe,
136         .remove = f75375_remove,
137 };
138
139 static inline int f75375_read8(struct i2c_client *client, u8 reg)
140 {
141         return i2c_smbus_read_byte_data(client, reg);
142 }
143
144 /* in most cases, should be called while holding update_lock */
145 static inline u16 f75375_read16(struct i2c_client *client, u8 reg)
146 {
147         return ((i2c_smbus_read_byte_data(client, reg) << 8)
148                 | i2c_smbus_read_byte_data(client, reg + 1));
149 }
150
151 static inline void f75375_write8(struct i2c_client *client, u8 reg,
152                 u8 value)
153 {
154         i2c_smbus_write_byte_data(client, reg, value);
155 }
156
157 static inline void f75375_write16(struct i2c_client *client, u8 reg,
158                 u16 value)
159 {
160         int err = i2c_smbus_write_byte_data(client, reg, (value << 8));
161         if (err)
162                 return;
163         i2c_smbus_write_byte_data(client, reg + 1, (value & 0xFF));
164 }
165
166 static struct f75375_data *f75375_update_device(struct device *dev)
167 {
168         struct i2c_client *client = to_i2c_client(dev);
169         struct f75375_data *data = i2c_get_clientdata(client);
170         int nr;
171
172         mutex_lock(&data->update_lock);
173
174         /* Limit registers cache is refreshed after 60 seconds */
175         if (time_after(jiffies, data->last_limits + 60 * HZ)
176                 || !data->valid) {
177                 for (nr = 0; nr < 2; nr++) {
178                         data->temp_high[nr] =
179                                 f75375_read8(client, F75375_REG_TEMP_HIGH(nr));
180                         data->temp_max_hyst[nr] =
181                                 f75375_read8(client, F75375_REG_TEMP_HYST(nr));
182                         data->fan_full[nr] =
183                                 f75375_read16(client, F75375_REG_FAN_FULL(nr));
184                         data->fan_min[nr] =
185                                 f75375_read16(client, F75375_REG_FAN_MIN(nr));
186                         data->fan_exp[nr] =
187                                 f75375_read16(client, F75375_REG_FAN_EXP(nr));
188                         data->pwm[nr] = f75375_read8(client,
189                                 F75375_REG_FAN_PWM_DUTY(nr));
190
191                 }
192                 for (nr = 0; nr < 4; nr++) {
193                         data->in_max[nr] =
194                                 f75375_read8(client, F75375_REG_VOLT_HIGH(nr));
195                         data->in_min[nr] =
196                                 f75375_read8(client, F75375_REG_VOLT_LOW(nr));
197                 }
198                 data->fan_timer = f75375_read8(client, F75375_REG_FAN_TIMER);
199                 data->last_limits = jiffies;
200         }
201
202         /* Measurement registers cache is refreshed after 2 second */
203         if (time_after(jiffies, data->last_updated + 2 * HZ)
204                 || !data->valid) {
205                 for (nr = 0; nr < 2; nr++) {
206                         data->temp[nr] =
207                                 f75375_read8(client, F75375_REG_TEMP(nr));
208                         data->fan[nr] =
209                                 f75375_read16(client, F75375_REG_FAN(nr));
210                 }
211                 for (nr = 0; nr < 4; nr++)
212                         data->in[nr] =
213                                 f75375_read8(client, F75375_REG_VOLT(nr));
214
215                 data->last_updated = jiffies;
216                 data->valid = 1;
217         }
218
219         mutex_unlock(&data->update_lock);
220         return data;
221 }
222
223 static inline u16 rpm_from_reg(u16 reg)
224 {
225         if (reg == 0 || reg == 0xffff)
226                 return 0;
227         return (1500000 / reg);
228 }
229
230 static inline u16 rpm_to_reg(int rpm)
231 {
232         if (rpm < 367 || rpm > 0xffff)
233                 return 0xffff;
234         return (1500000 / rpm);
235 }
236
237 static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
238                 const char *buf, size_t count)
239 {
240         int nr = to_sensor_dev_attr(attr)->index;
241         struct i2c_client *client = to_i2c_client(dev);
242         struct f75375_data *data = i2c_get_clientdata(client);
243         int val = simple_strtoul(buf, NULL, 10);
244
245         mutex_lock(&data->update_lock);
246         data->fan_min[nr] = rpm_to_reg(val);
247         f75375_write16(client, F75375_REG_FAN_MIN(nr), data->fan_min[nr]);
248         mutex_unlock(&data->update_lock);
249         return count;
250 }
251
252 static ssize_t set_fan_exp(struct device *dev, struct device_attribute *attr,
253                 const char *buf, size_t count)
254 {
255         int nr = to_sensor_dev_attr(attr)->index;
256         struct i2c_client *client = to_i2c_client(dev);
257         struct f75375_data *data = i2c_get_clientdata(client);
258         int val = simple_strtoul(buf, NULL, 10);
259
260         mutex_lock(&data->update_lock);
261         data->fan_exp[nr] = rpm_to_reg(val);
262         f75375_write16(client, F75375_REG_FAN_EXP(nr), data->fan_exp[nr]);
263         mutex_unlock(&data->update_lock);
264         return count;
265 }
266
267 static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
268                 const char *buf, size_t count)
269 {
270         int nr = to_sensor_dev_attr(attr)->index;
271         struct i2c_client *client = to_i2c_client(dev);
272         struct f75375_data *data = i2c_get_clientdata(client);
273         int val = simple_strtoul(buf, NULL, 10);
274
275         mutex_lock(&data->update_lock);
276         data->pwm[nr] = SENSORS_LIMIT(val, 0, 255);
277         f75375_write8(client, F75375_REG_FAN_PWM_DUTY(nr), data->pwm[nr]);
278         mutex_unlock(&data->update_lock);
279         return count;
280 }
281
282 static ssize_t show_pwm_enable(struct device *dev, struct device_attribute
283                 *attr, char *buf)
284 {
285         int nr = to_sensor_dev_attr(attr)->index;
286         struct f75375_data *data = f75375_update_device(dev);
287         return sprintf(buf, "%d\n", data->pwm_enable[nr]);
288 }
289
290 static int set_pwm_enable_direct(struct i2c_client *client, int nr, int val)
291 {
292         struct f75375_data *data = i2c_get_clientdata(client);
293         u8 fanmode;
294
295         if (val < 0 || val > 4)
296                 return -EINVAL;
297
298         fanmode = f75375_read8(client, F75375_REG_FAN_TIMER);
299         fanmode = ~(3 << FAN_CTRL_MODE(nr));
300
301         switch (val) {
302         case 0: /* Full speed */
303                 fanmode  |= (3 << FAN_CTRL_MODE(nr));
304                 data->pwm[nr] = 255;
305                 f75375_write8(client, F75375_REG_FAN_PWM_DUTY(nr),
306                                 data->pwm[nr]);
307                 break;
308         case 1: /* PWM */
309                 fanmode  |= (3 << FAN_CTRL_MODE(nr));
310                 break;
311         case 2: /* AUTOMATIC*/
312                 fanmode  |= (2 << FAN_CTRL_MODE(nr));
313                 break;
314         case 3: /* fan speed */
315                 break;
316         }
317         f75375_write8(client, F75375_REG_FAN_TIMER, fanmode);
318         data->pwm_enable[nr] = val;
319         return 0;
320 }
321
322 static ssize_t set_pwm_enable(struct device *dev, struct device_attribute *attr,
323                 const char *buf, size_t count)
324 {
325         int nr = to_sensor_dev_attr(attr)->index;
326         struct i2c_client *client = to_i2c_client(dev);
327         struct f75375_data *data = i2c_get_clientdata(client);
328         int val = simple_strtoul(buf, NULL, 10);
329         int err = 0;
330
331         mutex_lock(&data->update_lock);
332         err = set_pwm_enable_direct(client, nr, val);
333         mutex_unlock(&data->update_lock);
334         return err ? err : count;
335 }
336
337 static ssize_t set_pwm_mode(struct device *dev, struct device_attribute *attr,
338                 const char *buf, size_t count)
339 {
340         int nr = to_sensor_dev_attr(attr)->index;
341         struct i2c_client *client = to_i2c_client(dev);
342         struct f75375_data *data = i2c_get_clientdata(client);
343         int val = simple_strtoul(buf, NULL, 10);
344         u8 conf = 0;
345
346         if (!(val == 0 || val == 1))
347                 return -EINVAL;
348
349         mutex_lock(&data->update_lock);
350         conf = f75375_read8(client, F75375_REG_CONFIG1);
351         conf = ~(1 << FAN_CTRL_LINEAR(nr));
352
353         if (val == 0)
354                 conf |= (1 << FAN_CTRL_LINEAR(nr)) ;
355
356         f75375_write8(client, F75375_REG_CONFIG1, conf);
357         data->pwm_mode[nr] = val;
358         mutex_unlock(&data->update_lock);
359         return count;
360 }
361
362 static ssize_t show_pwm(struct device *dev, struct device_attribute
363                 *attr, char *buf)
364 {
365         int nr = to_sensor_dev_attr(attr)->index;
366         struct f75375_data *data = f75375_update_device(dev);
367         return sprintf(buf, "%d\n", data->pwm[nr]);
368 }
369
370 static ssize_t show_pwm_mode(struct device *dev, struct device_attribute
371                 *attr, char *buf)
372 {
373         int nr = to_sensor_dev_attr(attr)->index;
374         struct f75375_data *data = f75375_update_device(dev);
375         return sprintf(buf, "%d\n", data->pwm_mode[nr]);
376 }
377
378 #define VOLT_FROM_REG(val) ((val) * 8)
379 #define VOLT_TO_REG(val) ((val) / 8)
380
381 static ssize_t show_in(struct device *dev, struct device_attribute *attr,
382                 char *buf)
383 {
384         int nr = to_sensor_dev_attr(attr)->index;
385         struct f75375_data *data = f75375_update_device(dev);
386         return sprintf(buf, "%d\n", VOLT_FROM_REG(data->in[nr]));
387 }
388
389 static ssize_t show_in_max(struct device *dev, struct device_attribute *attr,
390                 char *buf)
391 {
392         int nr = to_sensor_dev_attr(attr)->index;
393         struct f75375_data *data = f75375_update_device(dev);
394         return sprintf(buf, "%d\n", VOLT_FROM_REG(data->in_max[nr]));
395 }
396
397 static ssize_t show_in_min(struct device *dev, struct device_attribute *attr,
398                 char *buf)
399 {
400         int nr = to_sensor_dev_attr(attr)->index;
401         struct f75375_data *data = f75375_update_device(dev);
402         return sprintf(buf, "%d\n", VOLT_FROM_REG(data->in_min[nr]));
403 }
404
405 static ssize_t set_in_max(struct device *dev, struct device_attribute *attr,
406                 const char *buf, size_t count)
407 {
408         int nr = to_sensor_dev_attr(attr)->index;
409         struct i2c_client *client = to_i2c_client(dev);
410         struct f75375_data *data = i2c_get_clientdata(client);
411         int val = simple_strtoul(buf, NULL, 10);
412         val = SENSORS_LIMIT(VOLT_TO_REG(val), 0, 0xff);
413         mutex_lock(&data->update_lock);
414         data->in_max[nr] = val;
415         f75375_write8(client, F75375_REG_VOLT_HIGH(nr), data->in_max[nr]);
416         mutex_unlock(&data->update_lock);
417         return count;
418 }
419
420 static ssize_t set_in_min(struct device *dev, struct device_attribute *attr,
421                 const char *buf, size_t count)
422 {
423         int nr = to_sensor_dev_attr(attr)->index;
424         struct i2c_client *client = to_i2c_client(dev);
425         struct f75375_data *data = i2c_get_clientdata(client);
426         int val = simple_strtoul(buf, NULL, 10);
427         val = SENSORS_LIMIT(VOLT_TO_REG(val), 0, 0xff);
428         mutex_lock(&data->update_lock);
429         data->in_min[nr] = val;
430         f75375_write8(client, F75375_REG_VOLT_LOW(nr), data->in_min[nr]);
431         mutex_unlock(&data->update_lock);
432         return count;
433 }
434 #define TEMP_FROM_REG(val) ((val) * 1000)
435 #define TEMP_TO_REG(val) ((val) / 1000)
436
437 static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
438                 char *buf)
439 {
440         int nr = to_sensor_dev_attr(attr)->index;
441         struct f75375_data *data = f75375_update_device(dev);
442         return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr]));
443 }
444
445 static ssize_t show_temp_max(struct device *dev, struct device_attribute *attr,
446                 char *buf)
447 {
448         int nr = to_sensor_dev_attr(attr)->index;
449         struct f75375_data *data = f75375_update_device(dev);
450         return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_high[nr]));
451 }
452
453 static ssize_t show_temp_max_hyst(struct device *dev,
454                 struct device_attribute *attr, char *buf)
455 {
456         int nr = to_sensor_dev_attr(attr)->index;
457         struct f75375_data *data = f75375_update_device(dev);
458         return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max_hyst[nr]));
459 }
460
461 static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
462                 const char *buf, size_t count)
463 {
464         int nr = to_sensor_dev_attr(attr)->index;
465         struct i2c_client *client = to_i2c_client(dev);
466         struct f75375_data *data = i2c_get_clientdata(client);
467         int val = simple_strtol(buf, NULL, 10);
468         val = SENSORS_LIMIT(TEMP_TO_REG(val), 0, 127);
469         mutex_lock(&data->update_lock);
470         data->temp_high[nr] = val;
471         f75375_write8(client, F75375_REG_TEMP_HIGH(nr), data->temp_high[nr]);
472         mutex_unlock(&data->update_lock);
473         return count;
474 }
475
476 static ssize_t set_temp_max_hyst(struct device *dev,
477         struct device_attribute *attr, const char *buf, size_t count)
478 {
479         int nr = to_sensor_dev_attr(attr)->index;
480         struct i2c_client *client = to_i2c_client(dev);
481         struct f75375_data *data = i2c_get_clientdata(client);
482         int val = simple_strtol(buf, NULL, 10);
483         val = SENSORS_LIMIT(TEMP_TO_REG(val), 0, 127);
484         mutex_lock(&data->update_lock);
485         data->temp_max_hyst[nr] = val;
486         f75375_write8(client, F75375_REG_TEMP_HYST(nr),
487                 data->temp_max_hyst[nr]);
488         mutex_unlock(&data->update_lock);
489         return count;
490 }
491
492 #define show_fan(thing) \
493 static ssize_t show_##thing(struct device *dev, struct device_attribute *attr, \
494                         char *buf)\
495 {\
496         int nr = to_sensor_dev_attr(attr)->index;\
497         struct f75375_data *data = f75375_update_device(dev); \
498         return sprintf(buf, "%d\n", rpm_from_reg(data->thing[nr])); \
499 }
500
501 show_fan(fan);
502 show_fan(fan_min);
503 show_fan(fan_full);
504 show_fan(fan_exp);
505
506 static SENSOR_DEVICE_ATTR(in0_input, S_IRUGO, show_in, NULL, 0);
507 static SENSOR_DEVICE_ATTR(in0_max, S_IRUGO|S_IWUSR,
508         show_in_max, set_in_max, 0);
509 static SENSOR_DEVICE_ATTR(in0_min, S_IRUGO|S_IWUSR,
510         show_in_min, set_in_min, 0);
511 static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, show_in, NULL, 1);
512 static SENSOR_DEVICE_ATTR(in1_max, S_IRUGO|S_IWUSR,
513         show_in_max, set_in_max, 1);
514 static SENSOR_DEVICE_ATTR(in1_min, S_IRUGO|S_IWUSR,
515         show_in_min, set_in_min, 1);
516 static SENSOR_DEVICE_ATTR(in2_input, S_IRUGO, show_in, NULL, 2);
517 static SENSOR_DEVICE_ATTR(in2_max, S_IRUGO|S_IWUSR,
518         show_in_max, set_in_max, 2);
519 static SENSOR_DEVICE_ATTR(in2_min, S_IRUGO|S_IWUSR,
520         show_in_min, set_in_min, 2);
521 static SENSOR_DEVICE_ATTR(in3_input, S_IRUGO, show_in, NULL, 3);
522 static SENSOR_DEVICE_ATTR(in3_max, S_IRUGO|S_IWUSR,
523         show_in_max, set_in_max, 3);
524 static SENSOR_DEVICE_ATTR(in3_min, S_IRUGO|S_IWUSR,
525         show_in_min, set_in_min, 3);
526 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
527 static SENSOR_DEVICE_ATTR(temp1_max_hyst, S_IRUGO|S_IWUSR,
528         show_temp_max_hyst, set_temp_max_hyst, 0);
529 static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO|S_IWUSR,
530         show_temp_max, set_temp_max, 0);
531 static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1);
532 static SENSOR_DEVICE_ATTR(temp2_max_hyst, S_IRUGO|S_IWUSR,
533         show_temp_max_hyst, set_temp_max_hyst, 1);
534 static SENSOR_DEVICE_ATTR(temp2_max, S_IRUGO|S_IWUSR,
535         show_temp_max, set_temp_max, 1);
536 static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0);
537 static SENSOR_DEVICE_ATTR(fan1_full, S_IRUGO, show_fan_full, NULL, 0);
538 static SENSOR_DEVICE_ATTR(fan1_min, S_IRUGO|S_IWUSR,
539         show_fan_min, set_fan_min, 0);
540 static SENSOR_DEVICE_ATTR(fan1_exp, S_IRUGO|S_IWUSR,
541         show_fan_exp, set_fan_exp, 0);
542 static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, show_fan, NULL, 1);
543 static SENSOR_DEVICE_ATTR(fan2_full, S_IRUGO, show_fan_full, NULL, 1);
544 static SENSOR_DEVICE_ATTR(fan2_min, S_IRUGO|S_IWUSR,
545         show_fan_min, set_fan_min, 1);
546 static SENSOR_DEVICE_ATTR(fan2_exp, S_IRUGO|S_IWUSR,
547         show_fan_exp, set_fan_exp, 1);
548 static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO|S_IWUSR,
549         show_pwm, set_pwm, 0);
550 static SENSOR_DEVICE_ATTR(pwm1_enable, S_IRUGO|S_IWUSR,
551         show_pwm_enable, set_pwm_enable, 0);
552 static SENSOR_DEVICE_ATTR(pwm1_mode, S_IRUGO,
553         show_pwm_mode, set_pwm_mode, 0);
554 static SENSOR_DEVICE_ATTR(pwm2, S_IRUGO | S_IWUSR,
555         show_pwm, set_pwm, 1);
556 static SENSOR_DEVICE_ATTR(pwm2_enable, S_IRUGO|S_IWUSR,
557         show_pwm_enable, set_pwm_enable, 1);
558 static SENSOR_DEVICE_ATTR(pwm2_mode, S_IRUGO,
559         show_pwm_mode, set_pwm_mode, 1);
560
561 static struct attribute *f75375_attributes[] = {
562         &sensor_dev_attr_temp1_input.dev_attr.attr,
563         &sensor_dev_attr_temp1_max.dev_attr.attr,
564         &sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
565         &sensor_dev_attr_temp2_input.dev_attr.attr,
566         &sensor_dev_attr_temp2_max.dev_attr.attr,
567         &sensor_dev_attr_temp2_max_hyst.dev_attr.attr,
568         &sensor_dev_attr_fan1_input.dev_attr.attr,
569         &sensor_dev_attr_fan1_full.dev_attr.attr,
570         &sensor_dev_attr_fan1_min.dev_attr.attr,
571         &sensor_dev_attr_fan1_exp.dev_attr.attr,
572         &sensor_dev_attr_fan2_input.dev_attr.attr,
573         &sensor_dev_attr_fan2_full.dev_attr.attr,
574         &sensor_dev_attr_fan2_min.dev_attr.attr,
575         &sensor_dev_attr_fan2_exp.dev_attr.attr,
576         &sensor_dev_attr_pwm1.dev_attr.attr,
577         &sensor_dev_attr_pwm1_enable.dev_attr.attr,
578         &sensor_dev_attr_pwm1_mode.dev_attr.attr,
579         &sensor_dev_attr_pwm2.dev_attr.attr,
580         &sensor_dev_attr_pwm2_enable.dev_attr.attr,
581         &sensor_dev_attr_pwm2_mode.dev_attr.attr,
582         &sensor_dev_attr_in0_input.dev_attr.attr,
583         &sensor_dev_attr_in0_max.dev_attr.attr,
584         &sensor_dev_attr_in0_min.dev_attr.attr,
585         &sensor_dev_attr_in1_input.dev_attr.attr,
586         &sensor_dev_attr_in1_max.dev_attr.attr,
587         &sensor_dev_attr_in1_min.dev_attr.attr,
588         &sensor_dev_attr_in2_input.dev_attr.attr,
589         &sensor_dev_attr_in2_max.dev_attr.attr,
590         &sensor_dev_attr_in2_min.dev_attr.attr,
591         &sensor_dev_attr_in3_input.dev_attr.attr,
592         &sensor_dev_attr_in3_max.dev_attr.attr,
593         &sensor_dev_attr_in3_min.dev_attr.attr,
594         NULL
595 };
596
597 static const struct attribute_group f75375_group = {
598         .attrs = f75375_attributes,
599 };
600
601 static int f75375_detach_client(struct i2c_client *client)
602 {
603         int err;
604
605         f75375_remove(client);
606         err = i2c_detach_client(client);
607         if (err) {
608                 dev_err(&client->dev,
609                         "Client deregistration failed, "
610                         "client not detached.\n");
611                 return err;
612         }
613         kfree(client);
614         return 0;
615 }
616
617 static void f75375_init(struct i2c_client *client, struct f75375_data *data,
618                 struct f75375s_platform_data *f75375s_pdata)
619 {
620         int nr;
621         set_pwm_enable_direct(client, 0, f75375s_pdata->pwm_enable[0]);
622         set_pwm_enable_direct(client, 1, f75375s_pdata->pwm_enable[1]);
623         for (nr = 0; nr < 2; nr++) {
624                 data->pwm[nr] = SENSORS_LIMIT(f75375s_pdata->pwm[nr], 0, 255);
625                 f75375_write8(client, F75375_REG_FAN_PWM_DUTY(nr),
626                         data->pwm[nr]);
627         }
628
629 }
630
631 static int f75375_probe(struct i2c_client *client)
632 {
633         struct f75375_data *data = i2c_get_clientdata(client);
634         struct f75375s_platform_data *f75375s_pdata = client->dev.platform_data;
635         int err;
636
637         if (!i2c_check_functionality(client->adapter,
638                                 I2C_FUNC_SMBUS_BYTE_DATA))
639                 return -EIO;
640         if (!(data = kzalloc(sizeof(struct f75375_data), GFP_KERNEL)))
641                 return -ENOMEM;
642
643         i2c_set_clientdata(client, data);
644         data->client = client;
645         mutex_init(&data->update_lock);
646
647         if (strcmp(client->name, "f75375") == 0)
648                 data->kind = f75375;
649         else if (strcmp(client->name, "f75373") == 0)
650                 data->kind = f75373;
651         else {
652                 dev_err(&client->dev, "Unsupported device: %s\n", client->name);
653                 return -ENODEV;
654         }
655
656         if ((err = sysfs_create_group(&client->dev.kobj, &f75375_group)))
657                 goto exit_free;
658
659         if (data->kind == f75375) {
660                 err = sysfs_chmod_file(&client->dev.kobj,
661                         &sensor_dev_attr_pwm1_mode.dev_attr.attr,
662                         S_IRUGO | S_IWUSR);
663                 if (err)
664                         goto exit_remove;
665                 err = sysfs_chmod_file(&client->dev.kobj,
666                         &sensor_dev_attr_pwm2_mode.dev_attr.attr,
667                         S_IRUGO | S_IWUSR);
668                 if (err)
669                         goto exit_remove;
670         }
671
672         data->hwmon_dev = hwmon_device_register(&client->dev);
673         if (IS_ERR(data->hwmon_dev)) {
674                 err = PTR_ERR(data->hwmon_dev);
675                 goto exit_remove;
676         }
677
678         if (f75375s_pdata != NULL)
679                 f75375_init(client, data, f75375s_pdata);
680
681         return 0;
682
683 exit_remove:
684         sysfs_remove_group(&client->dev.kobj, &f75375_group);
685 exit_free:
686         kfree(data);
687         i2c_set_clientdata(client, NULL);
688         return err;
689 }
690
691 static int f75375_remove(struct i2c_client *client)
692 {
693         struct f75375_data *data = i2c_get_clientdata(client);
694         hwmon_device_unregister(data->hwmon_dev);
695         sysfs_remove_group(&client->dev.kobj, &f75375_group);
696         kfree(data);
697         i2c_set_clientdata(client, NULL);
698         return 0;
699 }
700
701 static int f75375_attach_adapter(struct i2c_adapter *adapter)
702 {
703         if (!(adapter->class & I2C_CLASS_HWMON))
704                 return 0;
705         return i2c_probe(adapter, &addr_data, f75375_detect);
706 }
707
708 /* This function is called by i2c_probe */
709 static int f75375_detect(struct i2c_adapter *adapter, int address, int kind)
710 {
711         struct i2c_client *client;
712         u8 version = 0;
713         int err = 0;
714         const char *name = "";
715
716         if (!(client = kzalloc(sizeof(*client), GFP_KERNEL))) {
717                 err = -ENOMEM;
718                 goto exit;
719         }
720         client->addr = address;
721         client->adapter = adapter;
722         client->driver = &f75375_legacy_driver;
723
724         if (kind < 0) {
725                 u16 vendid = f75375_read16(client, F75375_REG_VENDOR);
726                 u16 chipid = f75375_read16(client, F75375_CHIP_ID);
727                 version = f75375_read8(client, F75375_REG_VERSION);
728                 if (chipid == 0x0306 && vendid == 0x1934) {
729                         kind = f75375;
730                 } else if (chipid == 0x0204 && vendid == 0x1934) {
731                         kind = f75373;
732                 } else {
733                         dev_err(&adapter->dev,
734                                 "failed,%02X,%02X,%02X\n",
735                                 chipid, version, vendid);
736                         goto exit_free;
737                 }
738         }
739
740         if (kind == f75375) {
741                 name = "f75375";
742         } else if (kind == f75373) {
743                 name = "f75373";
744         }
745         dev_info(&adapter->dev, "found %s version: %02X\n", name, version);
746         strlcpy(client->name, name, I2C_NAME_SIZE);
747
748         if ((err = i2c_attach_client(client)))
749                 goto exit_free;
750
751         if ((err = f75375_probe(client)) < 0)
752                 goto exit_detach;
753
754         return 0;
755
756 exit_detach:
757         i2c_detach_client(client);
758 exit_free:
759         kfree(client);
760 exit:
761         return err;
762 }
763
764 static int __init sensors_f75375_init(void)
765 {
766         int status;
767         status = i2c_add_driver(&f75375_driver);
768         if (status)
769                 return status;
770
771         status = i2c_add_driver(&f75375_legacy_driver);
772         if (status)
773                 i2c_del_driver(&f75375_driver);
774
775         return status;
776 }
777
778 static void __exit sensors_f75375_exit(void)
779 {
780         i2c_del_driver(&f75375_legacy_driver);
781         i2c_del_driver(&f75375_driver);
782 }
783
784 MODULE_AUTHOR("Riku Voipio <riku.voipio@movial.fi>");
785 MODULE_LICENSE("GPL");
786 MODULE_DESCRIPTION("F75373/F75375 hardware monitoring driver");
787
788 module_init(sensors_f75375_init);
789 module_exit(sensors_f75375_exit);