Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[linux-2.6/linux-mips/linux-dm7025.git] / drivers / hwmon / f75375s.c
blobdc1f30e432eab96864d52af0706a2ac39f7338a7
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 static const unsigned short normal_i2c[] = { 0x2d, 0x2e, I2C_CLIENT_END };
42 /* Insmod parameters */
43 I2C_CLIENT_INSMOD_2(f75373, f75375);
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
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)
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)
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)
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)
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
81 #define FAN_CTRL_LINEAR(nr) (4 + nr)
82 #define FAN_CTRL_MODE(nr) (5 + ((nr) * 2))
85 * Data structures and manipulation thereof
88 struct f75375_data {
89 unsigned short addr;
90 struct i2c_client *client;
91 struct device *hwmon_dev;
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 */
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];
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);
124 static struct i2c_driver f75375_legacy_driver = {
125 .driver = {
126 .name = "f75375_legacy",
128 .attach_adapter = f75375_attach_adapter,
129 .detach_client = f75375_detach_client,
132 static const struct i2c_device_id f75375_id[] = {
133 { "f75373", f75373 },
134 { "f75375", f75375 },
137 MODULE_DEVICE_TABLE(i2c, f75375_id);
139 static struct i2c_driver f75375_driver = {
140 .driver = {
141 .name = "f75375",
143 .probe = f75375_probe,
144 .remove = f75375_remove,
145 .id_table = f75375_id,
148 static inline int f75375_read8(struct i2c_client *client, u8 reg)
150 return i2c_smbus_read_byte_data(client, reg);
153 /* in most cases, should be called while holding update_lock */
154 static inline u16 f75375_read16(struct i2c_client *client, u8 reg)
156 return ((i2c_smbus_read_byte_data(client, reg) << 8)
157 | i2c_smbus_read_byte_data(client, reg + 1));
160 static inline void f75375_write8(struct i2c_client *client, u8 reg,
161 u8 value)
163 i2c_smbus_write_byte_data(client, reg, value);
166 static inline void f75375_write16(struct i2c_client *client, u8 reg,
167 u16 value)
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));
175 static struct f75375_data *f75375_update_device(struct device *dev)
177 struct i2c_client *client = to_i2c_client(dev);
178 struct f75375_data *data = i2c_get_clientdata(client);
179 int nr;
181 mutex_lock(&data->update_lock);
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));
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));
207 data->fan_timer = f75375_read8(client, F75375_REG_FAN_TIMER);
208 data->last_limits = jiffies;
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));
220 for (nr = 0; nr < 4; nr++)
221 data->in[nr] =
222 f75375_read8(client, F75375_REG_VOLT(nr));
224 data->last_updated = jiffies;
225 data->valid = 1;
228 mutex_unlock(&data->update_lock);
229 return data;
232 static inline u16 rpm_from_reg(u16 reg)
234 if (reg == 0 || reg == 0xffff)
235 return 0;
236 return (1500000 / reg);
239 static inline u16 rpm_to_reg(int rpm)
241 if (rpm < 367 || rpm > 0xffff)
242 return 0xffff;
243 return (1500000 / rpm);
246 static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
247 const char *buf, size_t count)
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);
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;
261 static ssize_t set_fan_exp(struct device *dev, struct device_attribute *attr,
262 const char *buf, size_t count)
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);
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;
276 static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
277 const char *buf, size_t count)
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);
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;
291 static ssize_t show_pwm_enable(struct device *dev, struct device_attribute
292 *attr, char *buf)
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]);
299 static int set_pwm_enable_direct(struct i2c_client *client, int nr, int val)
301 struct f75375_data *data = i2c_get_clientdata(client);
302 u8 fanmode;
304 if (val < 0 || val > 4)
305 return -EINVAL;
307 fanmode = f75375_read8(client, F75375_REG_FAN_TIMER);
308 fanmode = ~(3 << FAN_CTRL_MODE(nr));
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;
326 f75375_write8(client, F75375_REG_FAN_TIMER, fanmode);
327 data->pwm_enable[nr] = val;
328 return 0;
331 static ssize_t set_pwm_enable(struct device *dev, struct device_attribute *attr,
332 const char *buf, size_t count)
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;
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;
346 static ssize_t set_pwm_mode(struct device *dev, struct device_attribute *attr,
347 const char *buf, size_t count)
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;
355 if (!(val == 0 || val == 1))
356 return -EINVAL;
358 mutex_lock(&data->update_lock);
359 conf = f75375_read8(client, F75375_REG_CONFIG1);
360 conf = ~(1 << FAN_CTRL_LINEAR(nr));
362 if (val == 0)
363 conf |= (1 << FAN_CTRL_LINEAR(nr)) ;
365 f75375_write8(client, F75375_REG_CONFIG1, conf);
366 data->pwm_mode[nr] = val;
367 mutex_unlock(&data->update_lock);
368 return count;
371 static ssize_t show_pwm(struct device *dev, struct device_attribute
372 *attr, char *buf)
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]);
379 static ssize_t show_pwm_mode(struct device *dev, struct device_attribute
380 *attr, char *buf)
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]);
387 #define VOLT_FROM_REG(val) ((val) * 8)
388 #define VOLT_TO_REG(val) ((val) / 8)
390 static ssize_t show_in(struct device *dev, struct device_attribute *attr,
391 char *buf)
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]));
398 static ssize_t show_in_max(struct device *dev, struct device_attribute *attr,
399 char *buf)
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]));
406 static ssize_t show_in_min(struct device *dev, struct device_attribute *attr,
407 char *buf)
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]));
414 static ssize_t set_in_max(struct device *dev, struct device_attribute *attr,
415 const char *buf, size_t count)
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;
429 static ssize_t set_in_min(struct device *dev, struct device_attribute *attr,
430 const char *buf, size_t count)
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;
443 #define TEMP_FROM_REG(val) ((val) * 1000)
444 #define TEMP_TO_REG(val) ((val) / 1000)
446 static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
447 char *buf)
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]));
454 static ssize_t show_temp_max(struct device *dev, struct device_attribute *attr,
455 char *buf)
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]));
462 static ssize_t show_temp_max_hyst(struct device *dev,
463 struct device_attribute *attr, char *buf)
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]));
470 static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
471 const char *buf, size_t count)
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;
485 static ssize_t set_temp_max_hyst(struct device *dev,
486 struct device_attribute *attr, const char *buf, size_t count)
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;
501 #define show_fan(thing) \
502 static ssize_t show_##thing(struct device *dev, struct device_attribute *attr, \
503 char *buf)\
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])); \
510 show_fan(fan);
511 show_fan(fan_min);
512 show_fan(fan_full);
513 show_fan(fan_exp);
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);
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
606 static const struct attribute_group f75375_group = {
607 .attrs = f75375_attributes,
610 static int f75375_detach_client(struct i2c_client *client)
612 int err;
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;
622 kfree(client);
623 return 0;
626 static void f75375_init(struct i2c_client *client, struct f75375_data *data,
627 struct f75375s_platform_data *f75375s_pdata)
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]);
640 static int f75375_probe(struct i2c_client *client,
641 const struct i2c_device_id *id)
643 struct f75375_data *data = i2c_get_clientdata(client);
644 struct f75375s_platform_data *f75375s_pdata = client->dev.platform_data;
645 int err;
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;
653 i2c_set_clientdata(client, data);
654 data->client = client;
655 mutex_init(&data->update_lock);
656 data->kind = id->driver_data;
658 if ((err = sysfs_create_group(&client->dev.kobj, &f75375_group)))
659 goto exit_free;
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;
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;
680 if (f75375s_pdata != NULL)
681 f75375_init(client, data, f75375s_pdata);
683 return 0;
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;
693 static int f75375_remove(struct i2c_client *client)
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;
703 static int f75375_attach_adapter(struct i2c_adapter *adapter)
705 if (!(adapter->class & I2C_CLASS_HWMON))
706 return 0;
707 return i2c_probe(adapter, &addr_data, f75375_detect);
710 /* This function is called by i2c_probe */
711 static int f75375_detect(struct i2c_adapter *adapter, int address, int kind)
713 struct i2c_client *client;
714 u8 version = 0;
715 int err = 0;
716 const char *name = "";
717 struct i2c_device_id id;
719 if (!(client = kzalloc(sizeof(*client), GFP_KERNEL))) {
720 err = -ENOMEM;
721 goto exit;
723 client->addr = address;
724 client->adapter = adapter;
725 client->driver = &f75375_legacy_driver;
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;
743 if (kind == f75375) {
744 name = "f75375";
745 } else if (kind == f75373) {
746 name = "f75373";
748 dev_info(&adapter->dev, "found %s version: %02X\n", name, version);
749 strlcpy(client->name, name, I2C_NAME_SIZE);
751 if ((err = i2c_attach_client(client)))
752 goto exit_free;
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;
759 return 0;
761 exit_detach:
762 i2c_detach_client(client);
763 exit_free:
764 kfree(client);
765 exit:
766 return err;
769 static int __init sensors_f75375_init(void)
771 int status;
772 status = i2c_add_driver(&f75375_driver);
773 if (status)
774 return status;
776 status = i2c_add_driver(&f75375_legacy_driver);
777 if (status)
778 i2c_del_driver(&f75375_driver);
780 return status;
783 static void __exit sensors_f75375_exit(void)
785 i2c_del_driver(&f75375_legacy_driver);
786 i2c_del_driver(&f75375_driver);
789 MODULE_AUTHOR("Riku Voipio <riku.voipio@movial.fi>");
790 MODULE_LICENSE("GPL");
791 MODULE_DESCRIPTION("F75373/F75375 hardware monitoring driver");
793 module_init(sensors_f75375_init);
794 module_exit(sensors_f75375_exit);