i2c: Convert most new-style drivers to use module aliasing
[safe/jmp/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 const 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                         const struct i2c_device_id *id);
122 static int f75375_remove(struct i2c_client *client);
123
124 static struct i2c_driver f75375_legacy_driver = {
125         .driver = {
126                 .name = "f75375_legacy",
127         },
128         .attach_adapter = f75375_attach_adapter,
129         .detach_client = f75375_detach_client,
130 };
131
132 static const struct i2c_device_id f75375_id[] = {
133         { "f75373", f75373 },
134         { "f75375", f75375 },
135         { }
136 };
137 MODULE_DEVICE_TABLE(i2c, f75375_id);
138
139 static struct i2c_driver f75375_driver = {
140         .driver = {
141                 .name = "f75375",
142         },
143         .probe = f75375_probe,
144         .remove = f75375_remove,
145         .id_table = f75375_id,
146 };
147
148 static inline int f75375_read8(struct i2c_client *client, u8 reg)
149 {
150         return i2c_smbus_read_byte_data(client, reg);
151 }
152
153 /* in most cases, should be called while holding update_lock */
154 static inline u16 f75375_read16(struct i2c_client *client, u8 reg)
155 {
156         return ((i2c_smbus_read_byte_data(client, reg) << 8)
157                 | i2c_smbus_read_byte_data(client, reg + 1));
158 }
159
160 static inline void f75375_write8(struct i2c_client *client, u8 reg,
161                 u8 value)
162 {
163         i2c_smbus_write_byte_data(client, reg, value);
164 }
165
166 static inline void f75375_write16(struct i2c_client *client, u8 reg,
167                 u16 value)
168 {
169         int err = i2c_smbus_write_byte_data(client, reg, (value << 8));
170         if (err)
171                 return;
172         i2c_smbus_write_byte_data(client, reg + 1, (value & 0xFF));
173 }
174
175 static struct f75375_data *f75375_update_device(struct device *dev)
176 {
177         struct i2c_client *client = to_i2c_client(dev);
178         struct f75375_data *data = i2c_get_clientdata(client);
179         int nr;
180
181         mutex_lock(&data->update_lock);
182
183         /* Limit registers cache is refreshed after 60 seconds */
184         if (time_after(jiffies, data->last_limits + 60 * HZ)
185                 || !data->valid) {
186                 for (nr = 0; nr < 2; nr++) {
187                         data->temp_high[nr] =
188                                 f75375_read8(client, F75375_REG_TEMP_HIGH(nr));
189                         data->temp_max_hyst[nr] =
190                                 f75375_read8(client, F75375_REG_TEMP_HYST(nr));
191                         data->fan_full[nr] =
192                                 f75375_read16(client, F75375_REG_FAN_FULL(nr));
193                         data->fan_min[nr] =
194                                 f75375_read16(client, F75375_REG_FAN_MIN(nr));
195                         data->fan_exp[nr] =
196                                 f75375_read16(client, F75375_REG_FAN_EXP(nr));
197                         data->pwm[nr] = f75375_read8(client,
198                                 F75375_REG_FAN_PWM_DUTY(nr));
199
200                 }
201                 for (nr = 0; nr < 4; nr++) {
202                         data->in_max[nr] =
203                                 f75375_read8(client, F75375_REG_VOLT_HIGH(nr));
204                         data->in_min[nr] =
205                                 f75375_read8(client, F75375_REG_VOLT_LOW(nr));
206                 }
207                 data->fan_timer = f75375_read8(client, F75375_REG_FAN_TIMER);
208                 data->last_limits = jiffies;
209         }
210
211         /* Measurement registers cache is refreshed after 2 second */
212         if (time_after(jiffies, data->last_updated + 2 * HZ)
213                 || !data->valid) {
214                 for (nr = 0; nr < 2; nr++) {
215                         data->temp[nr] =
216                                 f75375_read8(client, F75375_REG_TEMP(nr));
217                         data->fan[nr] =
218                                 f75375_read16(client, F75375_REG_FAN(nr));
219                 }
220                 for (nr = 0; nr < 4; nr++)
221                         data->in[nr] =
222                                 f75375_read8(client, F75375_REG_VOLT(nr));
223
224                 data->last_updated = jiffies;
225                 data->valid = 1;
226         }
227
228         mutex_unlock(&data->update_lock);
229         return data;
230 }
231
232 static inline u16 rpm_from_reg(u16 reg)
233 {
234         if (reg == 0 || reg == 0xffff)
235                 return 0;
236         return (1500000 / reg);
237 }
238
239 static inline u16 rpm_to_reg(int rpm)
240 {
241         if (rpm < 367 || rpm > 0xffff)
242                 return 0xffff;
243         return (1500000 / rpm);
244 }
245
246 static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
247                 const char *buf, size_t count)
248 {
249         int nr = to_sensor_dev_attr(attr)->index;
250         struct i2c_client *client = to_i2c_client(dev);
251         struct f75375_data *data = i2c_get_clientdata(client);
252         int val = simple_strtoul(buf, NULL, 10);
253
254         mutex_lock(&data->update_lock);
255         data->fan_min[nr] = rpm_to_reg(val);
256         f75375_write16(client, F75375_REG_FAN_MIN(nr), data->fan_min[nr]);
257         mutex_unlock(&data->update_lock);
258         return count;
259 }
260
261 static ssize_t set_fan_exp(struct device *dev, struct device_attribute *attr,
262                 const char *buf, size_t count)
263 {
264         int nr = to_sensor_dev_attr(attr)->index;
265         struct i2c_client *client = to_i2c_client(dev);
266         struct f75375_data *data = i2c_get_clientdata(client);
267         int val = simple_strtoul(buf, NULL, 10);
268
269         mutex_lock(&data->update_lock);
270         data->fan_exp[nr] = rpm_to_reg(val);
271         f75375_write16(client, F75375_REG_FAN_EXP(nr), data->fan_exp[nr]);
272         mutex_unlock(&data->update_lock);
273         return count;
274 }
275
276 static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
277                 const char *buf, size_t count)
278 {
279         int nr = to_sensor_dev_attr(attr)->index;
280         struct i2c_client *client = to_i2c_client(dev);
281         struct f75375_data *data = i2c_get_clientdata(client);
282         int val = simple_strtoul(buf, NULL, 10);
283
284         mutex_lock(&data->update_lock);
285         data->pwm[nr] = SENSORS_LIMIT(val, 0, 255);
286         f75375_write8(client, F75375_REG_FAN_PWM_DUTY(nr), data->pwm[nr]);
287         mutex_unlock(&data->update_lock);
288         return count;
289 }
290
291 static ssize_t show_pwm_enable(struct device *dev, struct device_attribute
292                 *attr, char *buf)
293 {
294         int nr = to_sensor_dev_attr(attr)->index;
295         struct f75375_data *data = f75375_update_device(dev);
296         return sprintf(buf, "%d\n", data->pwm_enable[nr]);
297 }
298
299 static int set_pwm_enable_direct(struct i2c_client *client, int nr, int val)
300 {
301         struct f75375_data *data = i2c_get_clientdata(client);
302         u8 fanmode;
303
304         if (val < 0 || val > 4)
305                 return -EINVAL;
306
307         fanmode = f75375_read8(client, F75375_REG_FAN_TIMER);
308         fanmode = ~(3 << FAN_CTRL_MODE(nr));
309
310         switch (val) {
311         case 0: /* Full speed */
312                 fanmode  |= (3 << FAN_CTRL_MODE(nr));
313                 data->pwm[nr] = 255;
314                 f75375_write8(client, F75375_REG_FAN_PWM_DUTY(nr),
315                                 data->pwm[nr]);
316                 break;
317         case 1: /* PWM */
318                 fanmode  |= (3 << FAN_CTRL_MODE(nr));
319                 break;
320         case 2: /* AUTOMATIC*/
321                 fanmode  |= (2 << FAN_CTRL_MODE(nr));
322                 break;
323         case 3: /* fan speed */
324                 break;
325         }
326         f75375_write8(client, F75375_REG_FAN_TIMER, fanmode);
327         data->pwm_enable[nr] = val;
328         return 0;
329 }
330
331 static ssize_t set_pwm_enable(struct device *dev, struct device_attribute *attr,
332                 const char *buf, size_t count)
333 {
334         int nr = to_sensor_dev_attr(attr)->index;
335         struct i2c_client *client = to_i2c_client(dev);
336         struct f75375_data *data = i2c_get_clientdata(client);
337         int val = simple_strtoul(buf, NULL, 10);
338         int err = 0;
339
340         mutex_lock(&data->update_lock);
341         err = set_pwm_enable_direct(client, nr, val);
342         mutex_unlock(&data->update_lock);
343         return err ? err : count;
344 }
345
346 static ssize_t set_pwm_mode(struct device *dev, struct device_attribute *attr,
347                 const char *buf, size_t count)
348 {
349         int nr = to_sensor_dev_attr(attr)->index;
350         struct i2c_client *client = to_i2c_client(dev);
351         struct f75375_data *data = i2c_get_clientdata(client);
352         int val = simple_strtoul(buf, NULL, 10);
353         u8 conf = 0;
354
355         if (!(val == 0 || val == 1))
356                 return -EINVAL;
357
358         mutex_lock(&data->update_lock);
359         conf = f75375_read8(client, F75375_REG_CONFIG1);
360         conf = ~(1 << FAN_CTRL_LINEAR(nr));
361
362         if (val == 0)
363                 conf |= (1 << FAN_CTRL_LINEAR(nr)) ;
364
365         f75375_write8(client, F75375_REG_CONFIG1, conf);
366         data->pwm_mode[nr] = val;
367         mutex_unlock(&data->update_lock);
368         return count;
369 }
370
371 static ssize_t show_pwm(struct device *dev, struct device_attribute
372                 *attr, char *buf)
373 {
374         int nr = to_sensor_dev_attr(attr)->index;
375         struct f75375_data *data = f75375_update_device(dev);
376         return sprintf(buf, "%d\n", data->pwm[nr]);
377 }
378
379 static ssize_t show_pwm_mode(struct device *dev, struct device_attribute
380                 *attr, char *buf)
381 {
382         int nr = to_sensor_dev_attr(attr)->index;
383         struct f75375_data *data = f75375_update_device(dev);
384         return sprintf(buf, "%d\n", data->pwm_mode[nr]);
385 }
386
387 #define VOLT_FROM_REG(val) ((val) * 8)
388 #define VOLT_TO_REG(val) ((val) / 8)
389
390 static ssize_t show_in(struct device *dev, struct device_attribute *attr,
391                 char *buf)
392 {
393         int nr = to_sensor_dev_attr(attr)->index;
394         struct f75375_data *data = f75375_update_device(dev);
395         return sprintf(buf, "%d\n", VOLT_FROM_REG(data->in[nr]));
396 }
397
398 static ssize_t show_in_max(struct device *dev, struct device_attribute *attr,
399                 char *buf)
400 {
401         int nr = to_sensor_dev_attr(attr)->index;
402         struct f75375_data *data = f75375_update_device(dev);
403         return sprintf(buf, "%d\n", VOLT_FROM_REG(data->in_max[nr]));
404 }
405
406 static ssize_t show_in_min(struct device *dev, struct device_attribute *attr,
407                 char *buf)
408 {
409         int nr = to_sensor_dev_attr(attr)->index;
410         struct f75375_data *data = f75375_update_device(dev);
411         return sprintf(buf, "%d\n", VOLT_FROM_REG(data->in_min[nr]));
412 }
413
414 static ssize_t set_in_max(struct device *dev, struct device_attribute *attr,
415                 const char *buf, size_t count)
416 {
417         int nr = to_sensor_dev_attr(attr)->index;
418         struct i2c_client *client = to_i2c_client(dev);
419         struct f75375_data *data = i2c_get_clientdata(client);
420         int val = simple_strtoul(buf, NULL, 10);
421         val = SENSORS_LIMIT(VOLT_TO_REG(val), 0, 0xff);
422         mutex_lock(&data->update_lock);
423         data->in_max[nr] = val;
424         f75375_write8(client, F75375_REG_VOLT_HIGH(nr), data->in_max[nr]);
425         mutex_unlock(&data->update_lock);
426         return count;
427 }
428
429 static ssize_t set_in_min(struct device *dev, struct device_attribute *attr,
430                 const char *buf, size_t count)
431 {
432         int nr = to_sensor_dev_attr(attr)->index;
433         struct i2c_client *client = to_i2c_client(dev);
434         struct f75375_data *data = i2c_get_clientdata(client);
435         int val = simple_strtoul(buf, NULL, 10);
436         val = SENSORS_LIMIT(VOLT_TO_REG(val), 0, 0xff);
437         mutex_lock(&data->update_lock);
438         data->in_min[nr] = val;
439         f75375_write8(client, F75375_REG_VOLT_LOW(nr), data->in_min[nr]);
440         mutex_unlock(&data->update_lock);
441         return count;
442 }
443 #define TEMP_FROM_REG(val) ((val) * 1000)
444 #define TEMP_TO_REG(val) ((val) / 1000)
445
446 static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
447                 char *buf)
448 {
449         int nr = to_sensor_dev_attr(attr)->index;
450         struct f75375_data *data = f75375_update_device(dev);
451         return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr]));
452 }
453
454 static ssize_t show_temp_max(struct device *dev, struct device_attribute *attr,
455                 char *buf)
456 {
457         int nr = to_sensor_dev_attr(attr)->index;
458         struct f75375_data *data = f75375_update_device(dev);
459         return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_high[nr]));
460 }
461
462 static ssize_t show_temp_max_hyst(struct device *dev,
463                 struct device_attribute *attr, char *buf)
464 {
465         int nr = to_sensor_dev_attr(attr)->index;
466         struct f75375_data *data = f75375_update_device(dev);
467         return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max_hyst[nr]));
468 }
469
470 static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
471                 const char *buf, size_t count)
472 {
473         int nr = to_sensor_dev_attr(attr)->index;
474         struct i2c_client *client = to_i2c_client(dev);
475         struct f75375_data *data = i2c_get_clientdata(client);
476         int val = simple_strtol(buf, NULL, 10);
477         val = SENSORS_LIMIT(TEMP_TO_REG(val), 0, 127);
478         mutex_lock(&data->update_lock);
479         data->temp_high[nr] = val;
480         f75375_write8(client, F75375_REG_TEMP_HIGH(nr), data->temp_high[nr]);
481         mutex_unlock(&data->update_lock);
482         return count;
483 }
484
485 static ssize_t set_temp_max_hyst(struct device *dev,
486         struct device_attribute *attr, const char *buf, size_t count)
487 {
488         int nr = to_sensor_dev_attr(attr)->index;
489         struct i2c_client *client = to_i2c_client(dev);
490         struct f75375_data *data = i2c_get_clientdata(client);
491         int val = simple_strtol(buf, NULL, 10);
492         val = SENSORS_LIMIT(TEMP_TO_REG(val), 0, 127);
493         mutex_lock(&data->update_lock);
494         data->temp_max_hyst[nr] = val;
495         f75375_write8(client, F75375_REG_TEMP_HYST(nr),
496                 data->temp_max_hyst[nr]);
497         mutex_unlock(&data->update_lock);
498         return count;
499 }
500
501 #define show_fan(thing) \
502 static ssize_t show_##thing(struct device *dev, struct device_attribute *attr, \
503                         char *buf)\
504 {\
505         int nr = to_sensor_dev_attr(attr)->index;\
506         struct f75375_data *data = f75375_update_device(dev); \
507         return sprintf(buf, "%d\n", rpm_from_reg(data->thing[nr])); \
508 }
509
510 show_fan(fan);
511 show_fan(fan_min);
512 show_fan(fan_full);
513 show_fan(fan_exp);
514
515 static SENSOR_DEVICE_ATTR(in0_input, S_IRUGO, show_in, NULL, 0);
516 static SENSOR_DEVICE_ATTR(in0_max, S_IRUGO|S_IWUSR,
517         show_in_max, set_in_max, 0);
518 static SENSOR_DEVICE_ATTR(in0_min, S_IRUGO|S_IWUSR,
519         show_in_min, set_in_min, 0);
520 static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, show_in, NULL, 1);
521 static SENSOR_DEVICE_ATTR(in1_max, S_IRUGO|S_IWUSR,
522         show_in_max, set_in_max, 1);
523 static SENSOR_DEVICE_ATTR(in1_min, S_IRUGO|S_IWUSR,
524         show_in_min, set_in_min, 1);
525 static SENSOR_DEVICE_ATTR(in2_input, S_IRUGO, show_in, NULL, 2);
526 static SENSOR_DEVICE_ATTR(in2_max, S_IRUGO|S_IWUSR,
527         show_in_max, set_in_max, 2);
528 static SENSOR_DEVICE_ATTR(in2_min, S_IRUGO|S_IWUSR,
529         show_in_min, set_in_min, 2);
530 static SENSOR_DEVICE_ATTR(in3_input, S_IRUGO, show_in, NULL, 3);
531 static SENSOR_DEVICE_ATTR(in3_max, S_IRUGO|S_IWUSR,
532         show_in_max, set_in_max, 3);
533 static SENSOR_DEVICE_ATTR(in3_min, S_IRUGO|S_IWUSR,
534         show_in_min, set_in_min, 3);
535 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
536 static SENSOR_DEVICE_ATTR(temp1_max_hyst, S_IRUGO|S_IWUSR,
537         show_temp_max_hyst, set_temp_max_hyst, 0);
538 static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO|S_IWUSR,
539         show_temp_max, set_temp_max, 0);
540 static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1);
541 static SENSOR_DEVICE_ATTR(temp2_max_hyst, S_IRUGO|S_IWUSR,
542         show_temp_max_hyst, set_temp_max_hyst, 1);
543 static SENSOR_DEVICE_ATTR(temp2_max, S_IRUGO|S_IWUSR,
544         show_temp_max, set_temp_max, 1);
545 static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0);
546 static SENSOR_DEVICE_ATTR(fan1_full, S_IRUGO, show_fan_full, NULL, 0);
547 static SENSOR_DEVICE_ATTR(fan1_min, S_IRUGO|S_IWUSR,
548         show_fan_min, set_fan_min, 0);
549 static SENSOR_DEVICE_ATTR(fan1_exp, S_IRUGO|S_IWUSR,
550         show_fan_exp, set_fan_exp, 0);
551 static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, show_fan, NULL, 1);
552 static SENSOR_DEVICE_ATTR(fan2_full, S_IRUGO, show_fan_full, NULL, 1);
553 static SENSOR_DEVICE_ATTR(fan2_min, S_IRUGO|S_IWUSR,
554         show_fan_min, set_fan_min, 1);
555 static SENSOR_DEVICE_ATTR(fan2_exp, S_IRUGO|S_IWUSR,
556         show_fan_exp, set_fan_exp, 1);
557 static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO|S_IWUSR,
558         show_pwm, set_pwm, 0);
559 static SENSOR_DEVICE_ATTR(pwm1_enable, S_IRUGO|S_IWUSR,
560         show_pwm_enable, set_pwm_enable, 0);
561 static SENSOR_DEVICE_ATTR(pwm1_mode, S_IRUGO,
562         show_pwm_mode, set_pwm_mode, 0);
563 static SENSOR_DEVICE_ATTR(pwm2, S_IRUGO | S_IWUSR,
564         show_pwm, set_pwm, 1);
565 static SENSOR_DEVICE_ATTR(pwm2_enable, S_IRUGO|S_IWUSR,
566         show_pwm_enable, set_pwm_enable, 1);
567 static SENSOR_DEVICE_ATTR(pwm2_mode, S_IRUGO,
568         show_pwm_mode, set_pwm_mode, 1);
569
570 static struct attribute *f75375_attributes[] = {
571         &sensor_dev_attr_temp1_input.dev_attr.attr,
572         &sensor_dev_attr_temp1_max.dev_attr.attr,
573         &sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
574         &sensor_dev_attr_temp2_input.dev_attr.attr,
575         &sensor_dev_attr_temp2_max.dev_attr.attr,
576         &sensor_dev_attr_temp2_max_hyst.dev_attr.attr,
577         &sensor_dev_attr_fan1_input.dev_attr.attr,
578         &sensor_dev_attr_fan1_full.dev_attr.attr,
579         &sensor_dev_attr_fan1_min.dev_attr.attr,
580         &sensor_dev_attr_fan1_exp.dev_attr.attr,
581         &sensor_dev_attr_fan2_input.dev_attr.attr,
582         &sensor_dev_attr_fan2_full.dev_attr.attr,
583         &sensor_dev_attr_fan2_min.dev_attr.attr,
584         &sensor_dev_attr_fan2_exp.dev_attr.attr,
585         &sensor_dev_attr_pwm1.dev_attr.attr,
586         &sensor_dev_attr_pwm1_enable.dev_attr.attr,
587         &sensor_dev_attr_pwm1_mode.dev_attr.attr,
588         &sensor_dev_attr_pwm2.dev_attr.attr,
589         &sensor_dev_attr_pwm2_enable.dev_attr.attr,
590         &sensor_dev_attr_pwm2_mode.dev_attr.attr,
591         &sensor_dev_attr_in0_input.dev_attr.attr,
592         &sensor_dev_attr_in0_max.dev_attr.attr,
593         &sensor_dev_attr_in0_min.dev_attr.attr,
594         &sensor_dev_attr_in1_input.dev_attr.attr,
595         &sensor_dev_attr_in1_max.dev_attr.attr,
596         &sensor_dev_attr_in1_min.dev_attr.attr,
597         &sensor_dev_attr_in2_input.dev_attr.attr,
598         &sensor_dev_attr_in2_max.dev_attr.attr,
599         &sensor_dev_attr_in2_min.dev_attr.attr,
600         &sensor_dev_attr_in3_input.dev_attr.attr,
601         &sensor_dev_attr_in3_max.dev_attr.attr,
602         &sensor_dev_attr_in3_min.dev_attr.attr,
603         NULL
604 };
605
606 static const struct attribute_group f75375_group = {
607         .attrs = f75375_attributes,
608 };
609
610 static int f75375_detach_client(struct i2c_client *client)
611 {
612         int err;
613
614         f75375_remove(client);
615         err = i2c_detach_client(client);
616         if (err) {
617                 dev_err(&client->dev,
618                         "Client deregistration failed, "
619                         "client not detached.\n");
620                 return err;
621         }
622         kfree(client);
623         return 0;
624 }
625
626 static void f75375_init(struct i2c_client *client, struct f75375_data *data,
627                 struct f75375s_platform_data *f75375s_pdata)
628 {
629         int nr;
630         set_pwm_enable_direct(client, 0, f75375s_pdata->pwm_enable[0]);
631         set_pwm_enable_direct(client, 1, f75375s_pdata->pwm_enable[1]);
632         for (nr = 0; nr < 2; nr++) {
633                 data->pwm[nr] = SENSORS_LIMIT(f75375s_pdata->pwm[nr], 0, 255);
634                 f75375_write8(client, F75375_REG_FAN_PWM_DUTY(nr),
635                         data->pwm[nr]);
636         }
637
638 }
639
640 static int f75375_probe(struct i2c_client *client,
641                 const struct i2c_device_id *id)
642 {
643         struct f75375_data *data = i2c_get_clientdata(client);
644         struct f75375s_platform_data *f75375s_pdata = client->dev.platform_data;
645         int err;
646
647         if (!i2c_check_functionality(client->adapter,
648                                 I2C_FUNC_SMBUS_BYTE_DATA))
649                 return -EIO;
650         if (!(data = kzalloc(sizeof(struct f75375_data), GFP_KERNEL)))
651                 return -ENOMEM;
652
653         i2c_set_clientdata(client, data);
654         data->client = client;
655         mutex_init(&data->update_lock);
656         data->kind = id->driver_data;
657
658         if ((err = sysfs_create_group(&client->dev.kobj, &f75375_group)))
659                 goto exit_free;
660
661         if (data->kind == f75375) {
662                 err = sysfs_chmod_file(&client->dev.kobj,
663                         &sensor_dev_attr_pwm1_mode.dev_attr.attr,
664                         S_IRUGO | S_IWUSR);
665                 if (err)
666                         goto exit_remove;
667                 err = sysfs_chmod_file(&client->dev.kobj,
668                         &sensor_dev_attr_pwm2_mode.dev_attr.attr,
669                         S_IRUGO | S_IWUSR);
670                 if (err)
671                         goto exit_remove;
672         }
673
674         data->hwmon_dev = hwmon_device_register(&client->dev);
675         if (IS_ERR(data->hwmon_dev)) {
676                 err = PTR_ERR(data->hwmon_dev);
677                 goto exit_remove;
678         }
679
680         if (f75375s_pdata != NULL)
681                 f75375_init(client, data, f75375s_pdata);
682
683         return 0;
684
685 exit_remove:
686         sysfs_remove_group(&client->dev.kobj, &f75375_group);
687 exit_free:
688         kfree(data);
689         i2c_set_clientdata(client, NULL);
690         return err;
691 }
692
693 static int f75375_remove(struct i2c_client *client)
694 {
695         struct f75375_data *data = i2c_get_clientdata(client);
696         hwmon_device_unregister(data->hwmon_dev);
697         sysfs_remove_group(&client->dev.kobj, &f75375_group);
698         kfree(data);
699         i2c_set_clientdata(client, NULL);
700         return 0;
701 }
702
703 static int f75375_attach_adapter(struct i2c_adapter *adapter)
704 {
705         if (!(adapter->class & I2C_CLASS_HWMON))
706                 return 0;
707         return i2c_probe(adapter, &addr_data, f75375_detect);
708 }
709
710 /* This function is called by i2c_probe */
711 static int f75375_detect(struct i2c_adapter *adapter, int address, int kind)
712 {
713         struct i2c_client *client;
714         u8 version = 0;
715         int err = 0;
716         const char *name = "";
717         struct i2c_device_id id;
718
719         if (!(client = kzalloc(sizeof(*client), GFP_KERNEL))) {
720                 err = -ENOMEM;
721                 goto exit;
722         }
723         client->addr = address;
724         client->adapter = adapter;
725         client->driver = &f75375_legacy_driver;
726
727         if (kind < 0) {
728                 u16 vendid = f75375_read16(client, F75375_REG_VENDOR);
729                 u16 chipid = f75375_read16(client, F75375_CHIP_ID);
730                 version = f75375_read8(client, F75375_REG_VERSION);
731                 if (chipid == 0x0306 && vendid == 0x1934) {
732                         kind = f75375;
733                 } else if (chipid == 0x0204 && vendid == 0x1934) {
734                         kind = f75373;
735                 } else {
736                         dev_err(&adapter->dev,
737                                 "failed,%02X,%02X,%02X\n",
738                                 chipid, version, vendid);
739                         goto exit_free;
740                 }
741         }
742
743         if (kind == f75375) {
744                 name = "f75375";
745         } else if (kind == f75373) {
746                 name = "f75373";
747         }
748         dev_info(&adapter->dev, "found %s version: %02X\n", name, version);
749         strlcpy(client->name, name, I2C_NAME_SIZE);
750
751         if ((err = i2c_attach_client(client)))
752                 goto exit_free;
753
754         strlcpy(id.name, name, I2C_NAME_SIZE);
755         id.driver_data = kind;
756         if ((err = f75375_probe(client, &id)) < 0)
757                 goto exit_detach;
758
759         return 0;
760
761 exit_detach:
762         i2c_detach_client(client);
763 exit_free:
764         kfree(client);
765 exit:
766         return err;
767 }
768
769 static int __init sensors_f75375_init(void)
770 {
771         int status;
772         status = i2c_add_driver(&f75375_driver);
773         if (status)
774                 return status;
775
776         status = i2c_add_driver(&f75375_legacy_driver);
777         if (status)
778                 i2c_del_driver(&f75375_driver);
779
780         return status;
781 }
782
783 static void __exit sensors_f75375_exit(void)
784 {
785         i2c_del_driver(&f75375_legacy_driver);
786         i2c_del_driver(&f75375_driver);
787 }
788
789 MODULE_AUTHOR("Riku Voipio <riku.voipio@movial.fi>");
790 MODULE_LICENSE("GPL");
791 MODULE_DESCRIPTION("F75373/F75375 hardware monitoring driver");
792
793 module_init(sensors_f75375_init);
794 module_exit(sensors_f75375_exit);