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