Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[wrt350n-kernel.git] / drivers / hwmon / f75375s.c
blob2ce6b8dfe2deb508996ef97d01de7268ed5860bd
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>
6 * Datasheets available at:
8 * f75375:
9 * http://www.fintek.com.tw/files/productfiles/2005111152950.pdf
11 * f75373:
12 * http://www.fintek.com.tw/files/productfiles/2005111153128.pdf
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.
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.
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.
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>
39 /* Addresses to scan */
40 <<<<<<< HEAD:drivers/hwmon/f75375s.c
41 static unsigned short normal_i2c[] = { 0x2d, 0x2e, I2C_CLIENT_END };
42 =======
43 static const unsigned short normal_i2c[] = { 0x2d, 0x2e, I2C_CLIENT_END };
44 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/hwmon/f75375s.c
46 /* Insmod parameters */
47 I2C_CLIENT_INSMOD_2(f75373, f75375);
49 /* Fintek F75375 registers */
50 #define F75375_REG_CONFIG0 0x0
51 #define F75375_REG_CONFIG1 0x1
52 #define F75375_REG_CONFIG2 0x2
53 #define F75375_REG_CONFIG3 0x3
54 #define F75375_REG_ADDR 0x4
55 #define F75375_REG_INTR 0x31
56 #define F75375_CHIP_ID 0x5A
57 #define F75375_REG_VERSION 0x5C
58 #define F75375_REG_VENDOR 0x5D
59 #define F75375_REG_FAN_TIMER 0x60
61 #define F75375_REG_VOLT(nr) (0x10 + (nr))
62 #define F75375_REG_VOLT_HIGH(nr) (0x20 + (nr) * 2)
63 #define F75375_REG_VOLT_LOW(nr) (0x21 + (nr) * 2)
65 #define F75375_REG_TEMP(nr) (0x14 + (nr))
66 #define F75375_REG_TEMP_HIGH(nr) (0x28 + (nr) * 2)
67 #define F75375_REG_TEMP_HYST(nr) (0x29 + (nr) * 2)
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)
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)
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
85 #define FAN_CTRL_LINEAR(nr) (4 + nr)
86 #define FAN_CTRL_MODE(nr) (5 + ((nr) * 2))
89 * Data structures and manipulation thereof
92 struct f75375_data {
93 unsigned short addr;
94 struct i2c_client *client;
95 struct device *hwmon_dev;
97 const char *name;
98 int kind;
99 struct mutex update_lock; /* protect register access */
100 char valid;
101 unsigned long last_updated; /* In jiffies */
102 unsigned long last_limits; /* In jiffies */
104 /* Register values */
105 u8 in[4];
106 u8 in_max[4];
107 u8 in_min[4];
108 u16 fan[2];
109 u16 fan_min[2];
110 u16 fan_full[2];
111 u16 fan_exp[2];
112 u8 fan_timer;
113 u8 pwm[2];
114 u8 pwm_mode[2];
115 u8 pwm_enable[2];
116 s8 temp[2];
117 s8 temp_high[2];
118 s8 temp_max_hyst[2];
121 static int f75375_attach_adapter(struct i2c_adapter *adapter);
122 static int f75375_detect(struct i2c_adapter *adapter, int address, int kind);
123 static int f75375_detach_client(struct i2c_client *client);
124 static int f75375_probe(struct i2c_client *client);
125 static int f75375_remove(struct i2c_client *client);
127 static struct i2c_driver f75375_legacy_driver = {
128 .driver = {
129 .name = "f75375_legacy",
131 .attach_adapter = f75375_attach_adapter,
132 .detach_client = f75375_detach_client,
135 static struct i2c_driver f75375_driver = {
136 .driver = {
137 .name = "f75375",
139 .probe = f75375_probe,
140 .remove = f75375_remove,
143 static inline int f75375_read8(struct i2c_client *client, u8 reg)
145 return i2c_smbus_read_byte_data(client, reg);
148 /* in most cases, should be called while holding update_lock */
149 static inline u16 f75375_read16(struct i2c_client *client, u8 reg)
151 return ((i2c_smbus_read_byte_data(client, reg) << 8)
152 | i2c_smbus_read_byte_data(client, reg + 1));
155 static inline void f75375_write8(struct i2c_client *client, u8 reg,
156 u8 value)
158 i2c_smbus_write_byte_data(client, reg, value);
161 static inline void f75375_write16(struct i2c_client *client, u8 reg,
162 u16 value)
164 int err = i2c_smbus_write_byte_data(client, reg, (value << 8));
165 if (err)
166 return;
167 i2c_smbus_write_byte_data(client, reg + 1, (value & 0xFF));
170 static struct f75375_data *f75375_update_device(struct device *dev)
172 struct i2c_client *client = to_i2c_client(dev);
173 struct f75375_data *data = i2c_get_clientdata(client);
174 int nr;
176 mutex_lock(&data->update_lock);
178 /* Limit registers cache is refreshed after 60 seconds */
179 if (time_after(jiffies, data->last_limits + 60 * HZ)
180 || !data->valid) {
181 for (nr = 0; nr < 2; nr++) {
182 data->temp_high[nr] =
183 f75375_read8(client, F75375_REG_TEMP_HIGH(nr));
184 data->temp_max_hyst[nr] =
185 f75375_read8(client, F75375_REG_TEMP_HYST(nr));
186 data->fan_full[nr] =
187 f75375_read16(client, F75375_REG_FAN_FULL(nr));
188 data->fan_min[nr] =
189 f75375_read16(client, F75375_REG_FAN_MIN(nr));
190 data->fan_exp[nr] =
191 f75375_read16(client, F75375_REG_FAN_EXP(nr));
192 data->pwm[nr] = f75375_read8(client,
193 F75375_REG_FAN_PWM_DUTY(nr));
196 for (nr = 0; nr < 4; nr++) {
197 data->in_max[nr] =
198 f75375_read8(client, F75375_REG_VOLT_HIGH(nr));
199 data->in_min[nr] =
200 f75375_read8(client, F75375_REG_VOLT_LOW(nr));
202 data->fan_timer = f75375_read8(client, F75375_REG_FAN_TIMER);
203 data->last_limits = jiffies;
206 /* Measurement registers cache is refreshed after 2 second */
207 if (time_after(jiffies, data->last_updated + 2 * HZ)
208 || !data->valid) {
209 for (nr = 0; nr < 2; nr++) {
210 data->temp[nr] =
211 f75375_read8(client, F75375_REG_TEMP(nr));
212 data->fan[nr] =
213 f75375_read16(client, F75375_REG_FAN(nr));
215 for (nr = 0; nr < 4; nr++)
216 data->in[nr] =
217 f75375_read8(client, F75375_REG_VOLT(nr));
219 data->last_updated = jiffies;
220 data->valid = 1;
223 mutex_unlock(&data->update_lock);
224 return data;
227 static inline u16 rpm_from_reg(u16 reg)
229 if (reg == 0 || reg == 0xffff)
230 return 0;
231 return (1500000 / reg);
234 static inline u16 rpm_to_reg(int rpm)
236 if (rpm < 367 || rpm > 0xffff)
237 return 0xffff;
238 return (1500000 / rpm);
241 static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
242 const char *buf, size_t count)
244 int nr = to_sensor_dev_attr(attr)->index;
245 struct i2c_client *client = to_i2c_client(dev);
246 struct f75375_data *data = i2c_get_clientdata(client);
247 int val = simple_strtoul(buf, NULL, 10);
249 mutex_lock(&data->update_lock);
250 data->fan_min[nr] = rpm_to_reg(val);
251 f75375_write16(client, F75375_REG_FAN_MIN(nr), data->fan_min[nr]);
252 mutex_unlock(&data->update_lock);
253 return count;
256 static ssize_t set_fan_exp(struct device *dev, struct device_attribute *attr,
257 const char *buf, size_t count)
259 int nr = to_sensor_dev_attr(attr)->index;
260 struct i2c_client *client = to_i2c_client(dev);
261 struct f75375_data *data = i2c_get_clientdata(client);
262 int val = simple_strtoul(buf, NULL, 10);
264 mutex_lock(&data->update_lock);
265 data->fan_exp[nr] = rpm_to_reg(val);
266 f75375_write16(client, F75375_REG_FAN_EXP(nr), data->fan_exp[nr]);
267 mutex_unlock(&data->update_lock);
268 return count;
271 static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
272 const char *buf, size_t count)
274 int nr = to_sensor_dev_attr(attr)->index;
275 struct i2c_client *client = to_i2c_client(dev);
276 struct f75375_data *data = i2c_get_clientdata(client);
277 int val = simple_strtoul(buf, NULL, 10);
279 mutex_lock(&data->update_lock);
280 data->pwm[nr] = SENSORS_LIMIT(val, 0, 255);
281 f75375_write8(client, F75375_REG_FAN_PWM_DUTY(nr), data->pwm[nr]);
282 mutex_unlock(&data->update_lock);
283 return count;
286 static ssize_t show_pwm_enable(struct device *dev, struct device_attribute
287 *attr, char *buf)
289 int nr = to_sensor_dev_attr(attr)->index;
290 struct f75375_data *data = f75375_update_device(dev);
291 return sprintf(buf, "%d\n", data->pwm_enable[nr]);
294 static int set_pwm_enable_direct(struct i2c_client *client, int nr, int val)
296 struct f75375_data *data = i2c_get_clientdata(client);
297 u8 fanmode;
299 if (val < 0 || val > 4)
300 return -EINVAL;
302 fanmode = f75375_read8(client, F75375_REG_FAN_TIMER);
303 fanmode = ~(3 << FAN_CTRL_MODE(nr));
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;
321 f75375_write8(client, F75375_REG_FAN_TIMER, fanmode);
322 data->pwm_enable[nr] = val;
323 return 0;
326 static ssize_t set_pwm_enable(struct device *dev, struct device_attribute *attr,
327 const char *buf, size_t count)
329 int nr = to_sensor_dev_attr(attr)->index;
330 struct i2c_client *client = to_i2c_client(dev);
331 struct f75375_data *data = i2c_get_clientdata(client);
332 int val = simple_strtoul(buf, NULL, 10);
333 int err = 0;
335 mutex_lock(&data->update_lock);
336 err = set_pwm_enable_direct(client, nr, val);
337 mutex_unlock(&data->update_lock);
338 return err ? err : count;
341 static ssize_t set_pwm_mode(struct device *dev, struct device_attribute *attr,
342 const char *buf, size_t count)
344 int nr = to_sensor_dev_attr(attr)->index;
345 struct i2c_client *client = to_i2c_client(dev);
346 struct f75375_data *data = i2c_get_clientdata(client);
347 int val = simple_strtoul(buf, NULL, 10);
348 u8 conf = 0;
350 if (!(val == 0 || val == 1))
351 return -EINVAL;
353 mutex_lock(&data->update_lock);
354 conf = f75375_read8(client, F75375_REG_CONFIG1);
355 conf = ~(1 << FAN_CTRL_LINEAR(nr));
357 if (val == 0)
358 conf |= (1 << FAN_CTRL_LINEAR(nr)) ;
360 f75375_write8(client, F75375_REG_CONFIG1, conf);
361 data->pwm_mode[nr] = val;
362 mutex_unlock(&data->update_lock);
363 return count;
366 static ssize_t show_pwm(struct device *dev, struct device_attribute
367 *attr, char *buf)
369 int nr = to_sensor_dev_attr(attr)->index;
370 struct f75375_data *data = f75375_update_device(dev);
371 return sprintf(buf, "%d\n", data->pwm[nr]);
374 static ssize_t show_pwm_mode(struct device *dev, struct device_attribute
375 *attr, char *buf)
377 int nr = to_sensor_dev_attr(attr)->index;
378 struct f75375_data *data = f75375_update_device(dev);
379 return sprintf(buf, "%d\n", data->pwm_mode[nr]);
382 #define VOLT_FROM_REG(val) ((val) * 8)
383 #define VOLT_TO_REG(val) ((val) / 8)
385 static ssize_t show_in(struct device *dev, struct device_attribute *attr,
386 char *buf)
388 int nr = to_sensor_dev_attr(attr)->index;
389 struct f75375_data *data = f75375_update_device(dev);
390 return sprintf(buf, "%d\n", VOLT_FROM_REG(data->in[nr]));
393 static ssize_t show_in_max(struct device *dev, struct device_attribute *attr,
394 char *buf)
396 int nr = to_sensor_dev_attr(attr)->index;
397 struct f75375_data *data = f75375_update_device(dev);
398 return sprintf(buf, "%d\n", VOLT_FROM_REG(data->in_max[nr]));
401 static ssize_t show_in_min(struct device *dev, struct device_attribute *attr,
402 char *buf)
404 int nr = to_sensor_dev_attr(attr)->index;
405 struct f75375_data *data = f75375_update_device(dev);
406 return sprintf(buf, "%d\n", VOLT_FROM_REG(data->in_min[nr]));
409 static ssize_t set_in_max(struct device *dev, struct device_attribute *attr,
410 const char *buf, size_t count)
412 int nr = to_sensor_dev_attr(attr)->index;
413 struct i2c_client *client = to_i2c_client(dev);
414 struct f75375_data *data = i2c_get_clientdata(client);
415 int val = simple_strtoul(buf, NULL, 10);
416 val = SENSORS_LIMIT(VOLT_TO_REG(val), 0, 0xff);
417 mutex_lock(&data->update_lock);
418 data->in_max[nr] = val;
419 f75375_write8(client, F75375_REG_VOLT_HIGH(nr), data->in_max[nr]);
420 mutex_unlock(&data->update_lock);
421 return count;
424 static ssize_t set_in_min(struct device *dev, struct device_attribute *attr,
425 const char *buf, size_t count)
427 int nr = to_sensor_dev_attr(attr)->index;
428 struct i2c_client *client = to_i2c_client(dev);
429 struct f75375_data *data = i2c_get_clientdata(client);
430 int val = simple_strtoul(buf, NULL, 10);
431 val = SENSORS_LIMIT(VOLT_TO_REG(val), 0, 0xff);
432 mutex_lock(&data->update_lock);
433 data->in_min[nr] = val;
434 f75375_write8(client, F75375_REG_VOLT_LOW(nr), data->in_min[nr]);
435 mutex_unlock(&data->update_lock);
436 return count;
438 #define TEMP_FROM_REG(val) ((val) * 1000)
439 #define TEMP_TO_REG(val) ((val) / 1000)
441 static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
442 char *buf)
444 int nr = to_sensor_dev_attr(attr)->index;
445 struct f75375_data *data = f75375_update_device(dev);
446 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr]));
449 static ssize_t show_temp_max(struct device *dev, struct device_attribute *attr,
450 char *buf)
452 int nr = to_sensor_dev_attr(attr)->index;
453 struct f75375_data *data = f75375_update_device(dev);
454 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_high[nr]));
457 static ssize_t show_temp_max_hyst(struct device *dev,
458 struct device_attribute *attr, char *buf)
460 int nr = to_sensor_dev_attr(attr)->index;
461 struct f75375_data *data = f75375_update_device(dev);
462 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max_hyst[nr]));
465 static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
466 const char *buf, size_t count)
468 int nr = to_sensor_dev_attr(attr)->index;
469 struct i2c_client *client = to_i2c_client(dev);
470 struct f75375_data *data = i2c_get_clientdata(client);
471 int val = simple_strtol(buf, NULL, 10);
472 val = SENSORS_LIMIT(TEMP_TO_REG(val), 0, 127);
473 mutex_lock(&data->update_lock);
474 data->temp_high[nr] = val;
475 f75375_write8(client, F75375_REG_TEMP_HIGH(nr), data->temp_high[nr]);
476 mutex_unlock(&data->update_lock);
477 return count;
480 static ssize_t set_temp_max_hyst(struct device *dev,
481 struct device_attribute *attr, const char *buf, size_t count)
483 int nr = to_sensor_dev_attr(attr)->index;
484 struct i2c_client *client = to_i2c_client(dev);
485 struct f75375_data *data = i2c_get_clientdata(client);
486 int val = simple_strtol(buf, NULL, 10);
487 val = SENSORS_LIMIT(TEMP_TO_REG(val), 0, 127);
488 mutex_lock(&data->update_lock);
489 data->temp_max_hyst[nr] = val;
490 f75375_write8(client, F75375_REG_TEMP_HYST(nr),
491 data->temp_max_hyst[nr]);
492 mutex_unlock(&data->update_lock);
493 return count;
496 #define show_fan(thing) \
497 static ssize_t show_##thing(struct device *dev, struct device_attribute *attr, \
498 char *buf)\
500 int nr = to_sensor_dev_attr(attr)->index;\
501 struct f75375_data *data = f75375_update_device(dev); \
502 return sprintf(buf, "%d\n", rpm_from_reg(data->thing[nr])); \
505 show_fan(fan);
506 show_fan(fan_min);
507 show_fan(fan_full);
508 show_fan(fan_exp);
510 static SENSOR_DEVICE_ATTR(in0_input, S_IRUGO, show_in, NULL, 0);
511 static SENSOR_DEVICE_ATTR(in0_max, S_IRUGO|S_IWUSR,
512 show_in_max, set_in_max, 0);
513 static SENSOR_DEVICE_ATTR(in0_min, S_IRUGO|S_IWUSR,
514 show_in_min, set_in_min, 0);
515 static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, show_in, NULL, 1);
516 static SENSOR_DEVICE_ATTR(in1_max, S_IRUGO|S_IWUSR,
517 show_in_max, set_in_max, 1);
518 static SENSOR_DEVICE_ATTR(in1_min, S_IRUGO|S_IWUSR,
519 show_in_min, set_in_min, 1);
520 static SENSOR_DEVICE_ATTR(in2_input, S_IRUGO, show_in, NULL, 2);
521 static SENSOR_DEVICE_ATTR(in2_max, S_IRUGO|S_IWUSR,
522 show_in_max, set_in_max, 2);
523 static SENSOR_DEVICE_ATTR(in2_min, S_IRUGO|S_IWUSR,
524 show_in_min, set_in_min, 2);
525 static SENSOR_DEVICE_ATTR(in3_input, S_IRUGO, show_in, NULL, 3);
526 static SENSOR_DEVICE_ATTR(in3_max, S_IRUGO|S_IWUSR,
527 show_in_max, set_in_max, 3);
528 static SENSOR_DEVICE_ATTR(in3_min, S_IRUGO|S_IWUSR,
529 show_in_min, set_in_min, 3);
530 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
531 static SENSOR_DEVICE_ATTR(temp1_max_hyst, S_IRUGO|S_IWUSR,
532 show_temp_max_hyst, set_temp_max_hyst, 0);
533 static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO|S_IWUSR,
534 show_temp_max, set_temp_max, 0);
535 static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1);
536 static SENSOR_DEVICE_ATTR(temp2_max_hyst, S_IRUGO|S_IWUSR,
537 show_temp_max_hyst, set_temp_max_hyst, 1);
538 static SENSOR_DEVICE_ATTR(temp2_max, S_IRUGO|S_IWUSR,
539 show_temp_max, set_temp_max, 1);
540 static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0);
541 static SENSOR_DEVICE_ATTR(fan1_full, S_IRUGO, show_fan_full, NULL, 0);
542 static SENSOR_DEVICE_ATTR(fan1_min, S_IRUGO|S_IWUSR,
543 show_fan_min, set_fan_min, 0);
544 static SENSOR_DEVICE_ATTR(fan1_exp, S_IRUGO|S_IWUSR,
545 show_fan_exp, set_fan_exp, 0);
546 static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, show_fan, NULL, 1);
547 static SENSOR_DEVICE_ATTR(fan2_full, S_IRUGO, show_fan_full, NULL, 1);
548 static SENSOR_DEVICE_ATTR(fan2_min, S_IRUGO|S_IWUSR,
549 show_fan_min, set_fan_min, 1);
550 static SENSOR_DEVICE_ATTR(fan2_exp, S_IRUGO|S_IWUSR,
551 show_fan_exp, set_fan_exp, 1);
552 static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO|S_IWUSR,
553 show_pwm, set_pwm, 0);
554 static SENSOR_DEVICE_ATTR(pwm1_enable, S_IRUGO|S_IWUSR,
555 show_pwm_enable, set_pwm_enable, 0);
556 static SENSOR_DEVICE_ATTR(pwm1_mode, S_IRUGO,
557 show_pwm_mode, set_pwm_mode, 0);
558 static SENSOR_DEVICE_ATTR(pwm2, S_IRUGO | S_IWUSR,
559 show_pwm, set_pwm, 1);
560 static SENSOR_DEVICE_ATTR(pwm2_enable, S_IRUGO|S_IWUSR,
561 show_pwm_enable, set_pwm_enable, 1);
562 static SENSOR_DEVICE_ATTR(pwm2_mode, S_IRUGO,
563 show_pwm_mode, set_pwm_mode, 1);
565 static struct attribute *f75375_attributes[] = {
566 &sensor_dev_attr_temp1_input.dev_attr.attr,
567 &sensor_dev_attr_temp1_max.dev_attr.attr,
568 &sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
569 &sensor_dev_attr_temp2_input.dev_attr.attr,
570 &sensor_dev_attr_temp2_max.dev_attr.attr,
571 &sensor_dev_attr_temp2_max_hyst.dev_attr.attr,
572 &sensor_dev_attr_fan1_input.dev_attr.attr,
573 &sensor_dev_attr_fan1_full.dev_attr.attr,
574 &sensor_dev_attr_fan1_min.dev_attr.attr,
575 &sensor_dev_attr_fan1_exp.dev_attr.attr,
576 &sensor_dev_attr_fan2_input.dev_attr.attr,
577 &sensor_dev_attr_fan2_full.dev_attr.attr,
578 &sensor_dev_attr_fan2_min.dev_attr.attr,
579 &sensor_dev_attr_fan2_exp.dev_attr.attr,
580 &sensor_dev_attr_pwm1.dev_attr.attr,
581 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
582 &sensor_dev_attr_pwm1_mode.dev_attr.attr,
583 &sensor_dev_attr_pwm2.dev_attr.attr,
584 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
585 &sensor_dev_attr_pwm2_mode.dev_attr.attr,
586 &sensor_dev_attr_in0_input.dev_attr.attr,
587 &sensor_dev_attr_in0_max.dev_attr.attr,
588 &sensor_dev_attr_in0_min.dev_attr.attr,
589 &sensor_dev_attr_in1_input.dev_attr.attr,
590 &sensor_dev_attr_in1_max.dev_attr.attr,
591 &sensor_dev_attr_in1_min.dev_attr.attr,
592 &sensor_dev_attr_in2_input.dev_attr.attr,
593 &sensor_dev_attr_in2_max.dev_attr.attr,
594 &sensor_dev_attr_in2_min.dev_attr.attr,
595 &sensor_dev_attr_in3_input.dev_attr.attr,
596 &sensor_dev_attr_in3_max.dev_attr.attr,
597 &sensor_dev_attr_in3_min.dev_attr.attr,
598 NULL
601 static const struct attribute_group f75375_group = {
602 .attrs = f75375_attributes,
605 static int f75375_detach_client(struct i2c_client *client)
607 int err;
609 f75375_remove(client);
610 err = i2c_detach_client(client);
611 if (err) {
612 dev_err(&client->dev,
613 "Client deregistration failed, "
614 "client not detached.\n");
615 return err;
617 kfree(client);
618 return 0;
621 static void f75375_init(struct i2c_client *client, struct f75375_data *data,
622 struct f75375s_platform_data *f75375s_pdata)
624 int nr;
625 set_pwm_enable_direct(client, 0, f75375s_pdata->pwm_enable[0]);
626 set_pwm_enable_direct(client, 1, f75375s_pdata->pwm_enable[1]);
627 for (nr = 0; nr < 2; nr++) {
628 data->pwm[nr] = SENSORS_LIMIT(f75375s_pdata->pwm[nr], 0, 255);
629 f75375_write8(client, F75375_REG_FAN_PWM_DUTY(nr),
630 data->pwm[nr]);
635 static int f75375_probe(struct i2c_client *client)
637 struct f75375_data *data = i2c_get_clientdata(client);
638 struct f75375s_platform_data *f75375s_pdata = client->dev.platform_data;
639 int err;
641 if (!i2c_check_functionality(client->adapter,
642 I2C_FUNC_SMBUS_BYTE_DATA))
643 return -EIO;
644 if (!(data = kzalloc(sizeof(struct f75375_data), GFP_KERNEL)))
645 return -ENOMEM;
647 i2c_set_clientdata(client, data);
648 data->client = client;
649 mutex_init(&data->update_lock);
651 if (strcmp(client->name, "f75375") == 0)
652 data->kind = f75375;
653 else if (strcmp(client->name, "f75373") == 0)
654 data->kind = f75373;
655 else {
656 dev_err(&client->dev, "Unsupported device: %s\n", client->name);
657 return -ENODEV;
660 if ((err = sysfs_create_group(&client->dev.kobj, &f75375_group)))
661 goto exit_free;
663 if (data->kind == f75375) {
664 err = sysfs_chmod_file(&client->dev.kobj,
665 &sensor_dev_attr_pwm1_mode.dev_attr.attr,
666 S_IRUGO | S_IWUSR);
667 if (err)
668 goto exit_remove;
669 err = sysfs_chmod_file(&client->dev.kobj,
670 &sensor_dev_attr_pwm2_mode.dev_attr.attr,
671 S_IRUGO | S_IWUSR);
672 if (err)
673 goto exit_remove;
676 data->hwmon_dev = hwmon_device_register(&client->dev);
677 if (IS_ERR(data->hwmon_dev)) {
678 err = PTR_ERR(data->hwmon_dev);
679 goto exit_remove;
682 if (f75375s_pdata != NULL)
683 f75375_init(client, data, f75375s_pdata);
685 return 0;
687 exit_remove:
688 sysfs_remove_group(&client->dev.kobj, &f75375_group);
689 exit_free:
690 kfree(data);
691 i2c_set_clientdata(client, NULL);
692 return err;
695 static int f75375_remove(struct i2c_client *client)
697 struct f75375_data *data = i2c_get_clientdata(client);
698 hwmon_device_unregister(data->hwmon_dev);
699 sysfs_remove_group(&client->dev.kobj, &f75375_group);
700 kfree(data);
701 i2c_set_clientdata(client, NULL);
702 return 0;
705 static int f75375_attach_adapter(struct i2c_adapter *adapter)
707 if (!(adapter->class & I2C_CLASS_HWMON))
708 return 0;
709 return i2c_probe(adapter, &addr_data, f75375_detect);
712 /* This function is called by i2c_probe */
713 static int f75375_detect(struct i2c_adapter *adapter, int address, int kind)
715 struct i2c_client *client;
716 u8 version = 0;
717 int err = 0;
718 const char *name = "";
720 if (!(client = kzalloc(sizeof(*client), GFP_KERNEL))) {
721 err = -ENOMEM;
722 goto exit;
724 client->addr = address;
725 client->adapter = adapter;
726 client->driver = &f75375_legacy_driver;
728 if (kind < 0) {
729 u16 vendid = f75375_read16(client, F75375_REG_VENDOR);
730 u16 chipid = f75375_read16(client, F75375_CHIP_ID);
731 version = f75375_read8(client, F75375_REG_VERSION);
732 if (chipid == 0x0306 && vendid == 0x1934) {
733 kind = f75375;
734 } else if (chipid == 0x0204 && vendid == 0x1934) {
735 kind = f75373;
736 } else {
737 dev_err(&adapter->dev,
738 "failed,%02X,%02X,%02X\n",
739 chipid, version, vendid);
740 goto exit_free;
744 if (kind == f75375) {
745 name = "f75375";
746 } else if (kind == f75373) {
747 name = "f75373";
749 dev_info(&adapter->dev, "found %s version: %02X\n", name, version);
750 strlcpy(client->name, name, I2C_NAME_SIZE);
752 if ((err = i2c_attach_client(client)))
753 goto exit_free;
755 if ((err = f75375_probe(client)) < 0)
756 goto exit_detach;
758 return 0;
760 exit_detach:
761 i2c_detach_client(client);
762 exit_free:
763 kfree(client);
764 exit:
765 return err;
768 static int __init sensors_f75375_init(void)
770 int status;
771 status = i2c_add_driver(&f75375_driver);
772 if (status)
773 return status;
775 status = i2c_add_driver(&f75375_legacy_driver);
776 if (status)
777 i2c_del_driver(&f75375_driver);
779 return status;
782 static void __exit sensors_f75375_exit(void)
784 i2c_del_driver(&f75375_legacy_driver);
785 i2c_del_driver(&f75375_driver);
788 MODULE_AUTHOR("Riku Voipio <riku.voipio@movial.fi>");
789 MODULE_LICENSE("GPL");
790 MODULE_DESCRIPTION("F75373/F75375 hardware monitoring driver");
792 module_init(sensors_f75375_init);
793 module_exit(sensors_f75375_exit);