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