1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * f75375s.c - driver for the Fintek F75375/SP, F75373 and
4 * F75387SG/RG hardware monitoring features
5 * Copyright (C) 2006-2007 Riku Voipio
7 * Datasheets available at:
10 * http://www.fintek.com.tw/files/productfiles/F75375_V026P.pdf
13 * http://www.fintek.com.tw/files/productfiles/F75373_V025P.pdf
16 * http://www.fintek.com.tw/files/productfiles/F75387_V027P.pdf
19 #include <linux/module.h>
20 #include <linux/jiffies.h>
21 #include <linux/hwmon.h>
22 #include <linux/hwmon-sysfs.h>
23 #include <linux/i2c.h>
24 #include <linux/err.h>
25 #include <linux/mutex.h>
26 #include <linux/f75375s.h>
27 #include <linux/slab.h>
29 /* Addresses to scan */
30 static const unsigned short normal_i2c
[] = { 0x2d, 0x2e, I2C_CLIENT_END
};
32 enum chips
{ f75373
, f75375
, f75387
};
34 /* Fintek F75375 registers */
35 #define F75375_REG_CONFIG0 0x0
36 #define F75375_REG_CONFIG1 0x1
37 #define F75375_REG_CONFIG2 0x2
38 #define F75375_REG_CONFIG3 0x3
39 #define F75375_REG_ADDR 0x4
40 #define F75375_REG_INTR 0x31
41 #define F75375_CHIP_ID 0x5A
42 #define F75375_REG_VERSION 0x5C
43 #define F75375_REG_VENDOR 0x5D
44 #define F75375_REG_FAN_TIMER 0x60
46 #define F75375_REG_VOLT(nr) (0x10 + (nr))
47 #define F75375_REG_VOLT_HIGH(nr) (0x20 + (nr) * 2)
48 #define F75375_REG_VOLT_LOW(nr) (0x21 + (nr) * 2)
50 #define F75375_REG_TEMP(nr) (0x14 + (nr))
51 #define F75387_REG_TEMP11_LSB(nr) (0x1a + (nr))
52 #define F75375_REG_TEMP_HIGH(nr) (0x28 + (nr) * 2)
53 #define F75375_REG_TEMP_HYST(nr) (0x29 + (nr) * 2)
55 #define F75375_REG_FAN(nr) (0x16 + (nr) * 2)
56 #define F75375_REG_FAN_MIN(nr) (0x2C + (nr) * 2)
57 #define F75375_REG_FAN_FULL(nr) (0x70 + (nr) * 0x10)
58 #define F75375_REG_FAN_PWM_DUTY(nr) (0x76 + (nr) * 0x10)
59 #define F75375_REG_FAN_PWM_CLOCK(nr) (0x7D + (nr) * 0x10)
61 #define F75375_REG_FAN_EXP(nr) (0x74 + (nr) * 0x10)
62 #define F75375_REG_FAN_B_TEMP(nr, step) ((0xA0 + (nr) * 0x10) + (step))
63 #define F75375_REG_FAN_B_SPEED(nr, step) \
64 ((0xA5 + (nr) * 0x10) + (step) * 2)
66 #define F75375_REG_PWM1_RAISE_DUTY 0x69
67 #define F75375_REG_PWM2_RAISE_DUTY 0x6A
68 #define F75375_REG_PWM1_DROP_DUTY 0x6B
69 #define F75375_REG_PWM2_DROP_DUTY 0x6C
71 #define F75375_FAN_CTRL_LINEAR(nr) (4 + nr)
72 #define F75387_FAN_CTRL_LINEAR(nr) (1 + ((nr) * 4))
73 #define FAN_CTRL_MODE(nr) (4 + ((nr) * 2))
74 #define F75387_FAN_DUTY_MODE(nr) (2 + ((nr) * 4))
75 #define F75387_FAN_MANU_MODE(nr) ((nr) * 4)
78 * Data structures and manipulation thereof
83 struct device
*hwmon_dev
;
87 struct mutex update_lock
; /* protect register access */
89 unsigned long last_updated
; /* In jiffies */
90 unsigned long last_limits
; /* In jiffies */
105 * f75387: For remote temperature reading, it uses signed 11-bit
106 * values with LSB = 0.125 degree Celsius, left-justified in 16-bit
107 * registers. For original 8-bit temp readings, the LSB just is 0.
114 static inline int f75375_read8(struct i2c_client
*client
, u8 reg
)
116 return i2c_smbus_read_byte_data(client
, reg
);
119 /* in most cases, should be called while holding update_lock */
120 static inline u16
f75375_read16(struct i2c_client
*client
, u8 reg
)
122 return (i2c_smbus_read_byte_data(client
, reg
) << 8)
123 | i2c_smbus_read_byte_data(client
, reg
+ 1);
126 static inline void f75375_write8(struct i2c_client
*client
, u8 reg
,
129 i2c_smbus_write_byte_data(client
, reg
, value
);
132 static inline void f75375_write16(struct i2c_client
*client
, u8 reg
,
135 int err
= i2c_smbus_write_byte_data(client
, reg
, (value
>> 8));
138 i2c_smbus_write_byte_data(client
, reg
+ 1, (value
& 0xFF));
141 static void f75375_write_pwm(struct i2c_client
*client
, int nr
)
143 struct f75375_data
*data
= i2c_get_clientdata(client
);
144 if (data
->kind
== f75387
)
145 f75375_write16(client
, F75375_REG_FAN_EXP(nr
), data
->pwm
[nr
]);
147 f75375_write8(client
, F75375_REG_FAN_PWM_DUTY(nr
),
151 static struct f75375_data
*f75375_update_device(struct device
*dev
)
153 struct i2c_client
*client
= to_i2c_client(dev
);
154 struct f75375_data
*data
= i2c_get_clientdata(client
);
157 mutex_lock(&data
->update_lock
);
159 /* Limit registers cache is refreshed after 60 seconds */
160 if (time_after(jiffies
, data
->last_limits
+ 60 * HZ
)
162 for (nr
= 0; nr
< 2; nr
++) {
163 data
->temp_high
[nr
] =
164 f75375_read8(client
, F75375_REG_TEMP_HIGH(nr
));
165 data
->temp_max_hyst
[nr
] =
166 f75375_read8(client
, F75375_REG_TEMP_HYST(nr
));
168 f75375_read16(client
, F75375_REG_FAN_FULL(nr
));
170 f75375_read16(client
, F75375_REG_FAN_MIN(nr
));
171 data
->fan_target
[nr
] =
172 f75375_read16(client
, F75375_REG_FAN_EXP(nr
));
174 for (nr
= 0; nr
< 4; nr
++) {
176 f75375_read8(client
, F75375_REG_VOLT_HIGH(nr
));
178 f75375_read8(client
, F75375_REG_VOLT_LOW(nr
));
180 data
->fan_timer
= f75375_read8(client
, F75375_REG_FAN_TIMER
);
181 data
->last_limits
= jiffies
;
184 /* Measurement registers cache is refreshed after 2 second */
185 if (time_after(jiffies
, data
->last_updated
+ 2 * HZ
)
187 for (nr
= 0; nr
< 2; nr
++) {
188 data
->pwm
[nr
] = f75375_read8(client
,
189 F75375_REG_FAN_PWM_DUTY(nr
));
190 /* assign MSB, therefore shift it by 8 bits */
192 f75375_read8(client
, F75375_REG_TEMP(nr
)) << 8;
193 if (data
->kind
== f75387
)
194 /* merge F75387's temperature LSB (11-bit) */
197 F75387_REG_TEMP11_LSB(nr
));
199 f75375_read16(client
, F75375_REG_FAN(nr
));
201 for (nr
= 0; nr
< 4; nr
++)
203 f75375_read8(client
, F75375_REG_VOLT(nr
));
205 data
->last_updated
= jiffies
;
209 mutex_unlock(&data
->update_lock
);
213 static inline u16
rpm_from_reg(u16 reg
)
215 if (reg
== 0 || reg
== 0xffff)
217 return 1500000 / reg
;
220 static inline u16
rpm_to_reg(int rpm
)
222 if (rpm
< 367 || rpm
> 0xffff)
224 return 1500000 / rpm
;
227 static bool duty_mode_enabled(u8 pwm_enable
)
229 switch (pwm_enable
) {
230 case 0: /* Manual, duty mode (full speed) */
231 case 1: /* Manual, duty mode */
232 case 4: /* Auto, duty mode */
234 case 2: /* Auto, speed mode */
235 case 3: /* Manual, speed mode */
238 WARN(1, "Unexpected pwm_enable value %d\n", pwm_enable
);
243 static bool auto_mode_enabled(u8 pwm_enable
)
245 switch (pwm_enable
) {
246 case 0: /* Manual, duty mode (full speed) */
247 case 1: /* Manual, duty mode */
248 case 3: /* Manual, speed mode */
250 case 2: /* Auto, speed mode */
251 case 4: /* Auto, duty mode */
254 WARN(1, "Unexpected pwm_enable value %d\n", pwm_enable
);
259 static ssize_t
set_fan_min(struct device
*dev
, struct device_attribute
*attr
,
260 const char *buf
, size_t count
)
262 int nr
= to_sensor_dev_attr(attr
)->index
;
263 struct i2c_client
*client
= to_i2c_client(dev
);
264 struct f75375_data
*data
= i2c_get_clientdata(client
);
268 err
= kstrtoul(buf
, 10, &val
);
272 mutex_lock(&data
->update_lock
);
273 data
->fan_min
[nr
] = rpm_to_reg(val
);
274 f75375_write16(client
, F75375_REG_FAN_MIN(nr
), data
->fan_min
[nr
]);
275 mutex_unlock(&data
->update_lock
);
279 static ssize_t
set_fan_target(struct device
*dev
, struct device_attribute
*attr
,
280 const char *buf
, size_t count
)
282 int nr
= to_sensor_dev_attr(attr
)->index
;
283 struct i2c_client
*client
= to_i2c_client(dev
);
284 struct f75375_data
*data
= i2c_get_clientdata(client
);
288 err
= kstrtoul(buf
, 10, &val
);
292 if (auto_mode_enabled(data
->pwm_enable
[nr
]))
294 if (data
->kind
== f75387
&& duty_mode_enabled(data
->pwm_enable
[nr
]))
297 mutex_lock(&data
->update_lock
);
298 data
->fan_target
[nr
] = rpm_to_reg(val
);
299 f75375_write16(client
, F75375_REG_FAN_EXP(nr
), data
->fan_target
[nr
]);
300 mutex_unlock(&data
->update_lock
);
304 static ssize_t
set_pwm(struct device
*dev
, struct device_attribute
*attr
,
305 const char *buf
, size_t count
)
307 int nr
= to_sensor_dev_attr(attr
)->index
;
308 struct i2c_client
*client
= to_i2c_client(dev
);
309 struct f75375_data
*data
= i2c_get_clientdata(client
);
313 err
= kstrtoul(buf
, 10, &val
);
317 if (auto_mode_enabled(data
->pwm_enable
[nr
]) ||
318 !duty_mode_enabled(data
->pwm_enable
[nr
]))
321 mutex_lock(&data
->update_lock
);
322 data
->pwm
[nr
] = clamp_val(val
, 0, 255);
323 f75375_write_pwm(client
, nr
);
324 mutex_unlock(&data
->update_lock
);
328 static ssize_t
show_pwm_enable(struct device
*dev
, struct device_attribute
331 int nr
= to_sensor_dev_attr(attr
)->index
;
332 struct f75375_data
*data
= f75375_update_device(dev
);
333 return sprintf(buf
, "%d\n", data
->pwm_enable
[nr
]);
336 static int set_pwm_enable_direct(struct i2c_client
*client
, int nr
, int val
)
338 struct f75375_data
*data
= i2c_get_clientdata(client
);
341 if (val
< 0 || val
> 4)
344 fanmode
= f75375_read8(client
, F75375_REG_FAN_TIMER
);
345 if (data
->kind
== f75387
) {
346 /* For now, deny dangerous toggling of duty mode */
347 if (duty_mode_enabled(data
->pwm_enable
[nr
]) !=
348 duty_mode_enabled(val
))
350 /* clear each fanX_mode bit before setting them properly */
351 fanmode
&= ~(1 << F75387_FAN_DUTY_MODE(nr
));
352 fanmode
&= ~(1 << F75387_FAN_MANU_MODE(nr
));
354 case 0: /* full speed */
355 fanmode
|= (1 << F75387_FAN_MANU_MODE(nr
));
356 fanmode
|= (1 << F75387_FAN_DUTY_MODE(nr
));
360 fanmode
|= (1 << F75387_FAN_MANU_MODE(nr
));
361 fanmode
|= (1 << F75387_FAN_DUTY_MODE(nr
));
363 case 2: /* Automatic, speed mode */
365 case 3: /* fan speed */
366 fanmode
|= (1 << F75387_FAN_MANU_MODE(nr
));
368 case 4: /* Automatic, pwm */
369 fanmode
|= (1 << F75387_FAN_DUTY_MODE(nr
));
373 /* clear each fanX_mode bit before setting them properly */
374 fanmode
&= ~(3 << FAN_CTRL_MODE(nr
));
376 case 0: /* full speed */
377 fanmode
|= (3 << FAN_CTRL_MODE(nr
));
381 fanmode
|= (3 << FAN_CTRL_MODE(nr
));
383 case 2: /* AUTOMATIC*/
384 fanmode
|= (1 << FAN_CTRL_MODE(nr
));
386 case 3: /* fan speed */
388 case 4: /* Automatic pwm */
393 f75375_write8(client
, F75375_REG_FAN_TIMER
, fanmode
);
394 data
->pwm_enable
[nr
] = val
;
396 f75375_write_pwm(client
, nr
);
400 static ssize_t
set_pwm_enable(struct device
*dev
, struct device_attribute
*attr
,
401 const char *buf
, size_t count
)
403 int nr
= to_sensor_dev_attr(attr
)->index
;
404 struct i2c_client
*client
= to_i2c_client(dev
);
405 struct f75375_data
*data
= i2c_get_clientdata(client
);
409 err
= kstrtoul(buf
, 10, &val
);
413 mutex_lock(&data
->update_lock
);
414 err
= set_pwm_enable_direct(client
, nr
, val
);
415 mutex_unlock(&data
->update_lock
);
416 return err
? err
: count
;
419 static ssize_t
set_pwm_mode(struct device
*dev
, struct device_attribute
*attr
,
420 const char *buf
, size_t count
)
422 int nr
= to_sensor_dev_attr(attr
)->index
;
423 struct i2c_client
*client
= to_i2c_client(dev
);
424 struct f75375_data
*data
= i2c_get_clientdata(client
);
430 err
= kstrtoul(buf
, 10, &val
);
434 if (!(val
== 0 || val
== 1))
437 /* F75373 does not support DC (linear voltage) fan control mode */
438 if (data
->kind
== f75373
&& val
== 0)
441 /* take care for different registers */
442 if (data
->kind
== f75387
) {
443 reg
= F75375_REG_FAN_TIMER
;
444 ctrl
= F75387_FAN_CTRL_LINEAR(nr
);
446 reg
= F75375_REG_CONFIG1
;
447 ctrl
= F75375_FAN_CTRL_LINEAR(nr
);
450 mutex_lock(&data
->update_lock
);
451 conf
= f75375_read8(client
, reg
);
452 conf
&= ~(1 << ctrl
);
457 f75375_write8(client
, reg
, conf
);
458 data
->pwm_mode
[nr
] = val
;
459 mutex_unlock(&data
->update_lock
);
463 static ssize_t
show_pwm(struct device
*dev
, struct device_attribute
466 int nr
= to_sensor_dev_attr(attr
)->index
;
467 struct f75375_data
*data
= f75375_update_device(dev
);
468 return sprintf(buf
, "%d\n", data
->pwm
[nr
]);
471 static ssize_t
show_pwm_mode(struct device
*dev
, struct device_attribute
474 int nr
= to_sensor_dev_attr(attr
)->index
;
475 struct f75375_data
*data
= f75375_update_device(dev
);
476 return sprintf(buf
, "%d\n", data
->pwm_mode
[nr
]);
479 #define VOLT_FROM_REG(val) ((val) * 8)
480 #define VOLT_TO_REG(val) ((val) / 8)
482 static ssize_t
show_in(struct device
*dev
, struct device_attribute
*attr
,
485 int nr
= to_sensor_dev_attr(attr
)->index
;
486 struct f75375_data
*data
= f75375_update_device(dev
);
487 return sprintf(buf
, "%d\n", VOLT_FROM_REG(data
->in
[nr
]));
490 static ssize_t
show_in_max(struct device
*dev
, struct device_attribute
*attr
,
493 int nr
= to_sensor_dev_attr(attr
)->index
;
494 struct f75375_data
*data
= f75375_update_device(dev
);
495 return sprintf(buf
, "%d\n", VOLT_FROM_REG(data
->in_max
[nr
]));
498 static ssize_t
show_in_min(struct device
*dev
, struct device_attribute
*attr
,
501 int nr
= to_sensor_dev_attr(attr
)->index
;
502 struct f75375_data
*data
= f75375_update_device(dev
);
503 return sprintf(buf
, "%d\n", VOLT_FROM_REG(data
->in_min
[nr
]));
506 static ssize_t
set_in_max(struct device
*dev
, struct device_attribute
*attr
,
507 const char *buf
, size_t count
)
509 int nr
= to_sensor_dev_attr(attr
)->index
;
510 struct i2c_client
*client
= to_i2c_client(dev
);
511 struct f75375_data
*data
= i2c_get_clientdata(client
);
515 err
= kstrtoul(buf
, 10, &val
);
519 val
= clamp_val(VOLT_TO_REG(val
), 0, 0xff);
520 mutex_lock(&data
->update_lock
);
521 data
->in_max
[nr
] = val
;
522 f75375_write8(client
, F75375_REG_VOLT_HIGH(nr
), data
->in_max
[nr
]);
523 mutex_unlock(&data
->update_lock
);
527 static ssize_t
set_in_min(struct device
*dev
, struct device_attribute
*attr
,
528 const char *buf
, size_t count
)
530 int nr
= to_sensor_dev_attr(attr
)->index
;
531 struct i2c_client
*client
= to_i2c_client(dev
);
532 struct f75375_data
*data
= i2c_get_clientdata(client
);
536 err
= kstrtoul(buf
, 10, &val
);
540 val
= clamp_val(VOLT_TO_REG(val
), 0, 0xff);
541 mutex_lock(&data
->update_lock
);
542 data
->in_min
[nr
] = val
;
543 f75375_write8(client
, F75375_REG_VOLT_LOW(nr
), data
->in_min
[nr
]);
544 mutex_unlock(&data
->update_lock
);
547 #define TEMP_FROM_REG(val) ((val) * 1000)
548 #define TEMP_TO_REG(val) ((val) / 1000)
549 #define TEMP11_FROM_REG(reg) ((reg) / 32 * 125)
551 static ssize_t
show_temp11(struct device
*dev
, struct device_attribute
*attr
,
554 int nr
= to_sensor_dev_attr(attr
)->index
;
555 struct f75375_data
*data
= f75375_update_device(dev
);
556 return sprintf(buf
, "%d\n", TEMP11_FROM_REG(data
->temp11
[nr
]));
559 static ssize_t
show_temp_max(struct device
*dev
, struct device_attribute
*attr
,
562 int nr
= to_sensor_dev_attr(attr
)->index
;
563 struct f75375_data
*data
= f75375_update_device(dev
);
564 return sprintf(buf
, "%d\n", TEMP_FROM_REG(data
->temp_high
[nr
]));
567 static ssize_t
show_temp_max_hyst(struct device
*dev
,
568 struct device_attribute
*attr
, char *buf
)
570 int nr
= to_sensor_dev_attr(attr
)->index
;
571 struct f75375_data
*data
= f75375_update_device(dev
);
572 return sprintf(buf
, "%d\n", TEMP_FROM_REG(data
->temp_max_hyst
[nr
]));
575 static ssize_t
set_temp_max(struct device
*dev
, struct device_attribute
*attr
,
576 const char *buf
, size_t count
)
578 int nr
= to_sensor_dev_attr(attr
)->index
;
579 struct i2c_client
*client
= to_i2c_client(dev
);
580 struct f75375_data
*data
= i2c_get_clientdata(client
);
584 err
= kstrtoul(buf
, 10, &val
);
588 val
= clamp_val(TEMP_TO_REG(val
), 0, 127);
589 mutex_lock(&data
->update_lock
);
590 data
->temp_high
[nr
] = val
;
591 f75375_write8(client
, F75375_REG_TEMP_HIGH(nr
), data
->temp_high
[nr
]);
592 mutex_unlock(&data
->update_lock
);
596 static ssize_t
set_temp_max_hyst(struct device
*dev
,
597 struct device_attribute
*attr
, const char *buf
, size_t count
)
599 int nr
= to_sensor_dev_attr(attr
)->index
;
600 struct i2c_client
*client
= to_i2c_client(dev
);
601 struct f75375_data
*data
= i2c_get_clientdata(client
);
605 err
= kstrtoul(buf
, 10, &val
);
609 val
= clamp_val(TEMP_TO_REG(val
), 0, 127);
610 mutex_lock(&data
->update_lock
);
611 data
->temp_max_hyst
[nr
] = val
;
612 f75375_write8(client
, F75375_REG_TEMP_HYST(nr
),
613 data
->temp_max_hyst
[nr
]);
614 mutex_unlock(&data
->update_lock
);
618 #define show_fan(thing) \
619 static ssize_t show_##thing(struct device *dev, struct device_attribute *attr, \
622 int nr = to_sensor_dev_attr(attr)->index;\
623 struct f75375_data *data = f75375_update_device(dev); \
624 return sprintf(buf, "%d\n", rpm_from_reg(data->thing[nr])); \
630 show_fan(fan_target
);
632 static SENSOR_DEVICE_ATTR(in0_input
, S_IRUGO
, show_in
, NULL
, 0);
633 static SENSOR_DEVICE_ATTR(in0_max
, S_IRUGO
|S_IWUSR
,
634 show_in_max
, set_in_max
, 0);
635 static SENSOR_DEVICE_ATTR(in0_min
, S_IRUGO
|S_IWUSR
,
636 show_in_min
, set_in_min
, 0);
637 static SENSOR_DEVICE_ATTR(in1_input
, S_IRUGO
, show_in
, NULL
, 1);
638 static SENSOR_DEVICE_ATTR(in1_max
, S_IRUGO
|S_IWUSR
,
639 show_in_max
, set_in_max
, 1);
640 static SENSOR_DEVICE_ATTR(in1_min
, S_IRUGO
|S_IWUSR
,
641 show_in_min
, set_in_min
, 1);
642 static SENSOR_DEVICE_ATTR(in2_input
, S_IRUGO
, show_in
, NULL
, 2);
643 static SENSOR_DEVICE_ATTR(in2_max
, S_IRUGO
|S_IWUSR
,
644 show_in_max
, set_in_max
, 2);
645 static SENSOR_DEVICE_ATTR(in2_min
, S_IRUGO
|S_IWUSR
,
646 show_in_min
, set_in_min
, 2);
647 static SENSOR_DEVICE_ATTR(in3_input
, S_IRUGO
, show_in
, NULL
, 3);
648 static SENSOR_DEVICE_ATTR(in3_max
, S_IRUGO
|S_IWUSR
,
649 show_in_max
, set_in_max
, 3);
650 static SENSOR_DEVICE_ATTR(in3_min
, S_IRUGO
|S_IWUSR
,
651 show_in_min
, set_in_min
, 3);
652 static SENSOR_DEVICE_ATTR(temp1_input
, S_IRUGO
, show_temp11
, NULL
, 0);
653 static SENSOR_DEVICE_ATTR(temp1_max_hyst
, S_IRUGO
|S_IWUSR
,
654 show_temp_max_hyst
, set_temp_max_hyst
, 0);
655 static SENSOR_DEVICE_ATTR(temp1_max
, S_IRUGO
|S_IWUSR
,
656 show_temp_max
, set_temp_max
, 0);
657 static SENSOR_DEVICE_ATTR(temp2_input
, S_IRUGO
, show_temp11
, NULL
, 1);
658 static SENSOR_DEVICE_ATTR(temp2_max_hyst
, S_IRUGO
|S_IWUSR
,
659 show_temp_max_hyst
, set_temp_max_hyst
, 1);
660 static SENSOR_DEVICE_ATTR(temp2_max
, S_IRUGO
|S_IWUSR
,
661 show_temp_max
, set_temp_max
, 1);
662 static SENSOR_DEVICE_ATTR(fan1_input
, S_IRUGO
, show_fan
, NULL
, 0);
663 static SENSOR_DEVICE_ATTR(fan1_max
, S_IRUGO
, show_fan_max
, NULL
, 0);
664 static SENSOR_DEVICE_ATTR(fan1_min
, S_IRUGO
|S_IWUSR
,
665 show_fan_min
, set_fan_min
, 0);
666 static SENSOR_DEVICE_ATTR(fan1_target
, S_IRUGO
|S_IWUSR
,
667 show_fan_target
, set_fan_target
, 0);
668 static SENSOR_DEVICE_ATTR(fan2_input
, S_IRUGO
, show_fan
, NULL
, 1);
669 static SENSOR_DEVICE_ATTR(fan2_max
, S_IRUGO
, show_fan_max
, NULL
, 1);
670 static SENSOR_DEVICE_ATTR(fan2_min
, S_IRUGO
|S_IWUSR
,
671 show_fan_min
, set_fan_min
, 1);
672 static SENSOR_DEVICE_ATTR(fan2_target
, S_IRUGO
|S_IWUSR
,
673 show_fan_target
, set_fan_target
, 1);
674 static SENSOR_DEVICE_ATTR(pwm1
, S_IRUGO
|S_IWUSR
,
675 show_pwm
, set_pwm
, 0);
676 static SENSOR_DEVICE_ATTR(pwm1_enable
, S_IRUGO
|S_IWUSR
,
677 show_pwm_enable
, set_pwm_enable
, 0);
678 static SENSOR_DEVICE_ATTR(pwm1_mode
, S_IRUGO
,
679 show_pwm_mode
, set_pwm_mode
, 0);
680 static SENSOR_DEVICE_ATTR(pwm2
, S_IRUGO
| S_IWUSR
,
681 show_pwm
, set_pwm
, 1);
682 static SENSOR_DEVICE_ATTR(pwm2_enable
, S_IRUGO
|S_IWUSR
,
683 show_pwm_enable
, set_pwm_enable
, 1);
684 static SENSOR_DEVICE_ATTR(pwm2_mode
, S_IRUGO
,
685 show_pwm_mode
, set_pwm_mode
, 1);
687 static struct attribute
*f75375_attributes
[] = {
688 &sensor_dev_attr_temp1_input
.dev_attr
.attr
,
689 &sensor_dev_attr_temp1_max
.dev_attr
.attr
,
690 &sensor_dev_attr_temp1_max_hyst
.dev_attr
.attr
,
691 &sensor_dev_attr_temp2_input
.dev_attr
.attr
,
692 &sensor_dev_attr_temp2_max
.dev_attr
.attr
,
693 &sensor_dev_attr_temp2_max_hyst
.dev_attr
.attr
,
694 &sensor_dev_attr_fan1_input
.dev_attr
.attr
,
695 &sensor_dev_attr_fan1_max
.dev_attr
.attr
,
696 &sensor_dev_attr_fan1_min
.dev_attr
.attr
,
697 &sensor_dev_attr_fan1_target
.dev_attr
.attr
,
698 &sensor_dev_attr_fan2_input
.dev_attr
.attr
,
699 &sensor_dev_attr_fan2_max
.dev_attr
.attr
,
700 &sensor_dev_attr_fan2_min
.dev_attr
.attr
,
701 &sensor_dev_attr_fan2_target
.dev_attr
.attr
,
702 &sensor_dev_attr_pwm1
.dev_attr
.attr
,
703 &sensor_dev_attr_pwm1_enable
.dev_attr
.attr
,
704 &sensor_dev_attr_pwm1_mode
.dev_attr
.attr
,
705 &sensor_dev_attr_pwm2
.dev_attr
.attr
,
706 &sensor_dev_attr_pwm2_enable
.dev_attr
.attr
,
707 &sensor_dev_attr_pwm2_mode
.dev_attr
.attr
,
708 &sensor_dev_attr_in0_input
.dev_attr
.attr
,
709 &sensor_dev_attr_in0_max
.dev_attr
.attr
,
710 &sensor_dev_attr_in0_min
.dev_attr
.attr
,
711 &sensor_dev_attr_in1_input
.dev_attr
.attr
,
712 &sensor_dev_attr_in1_max
.dev_attr
.attr
,
713 &sensor_dev_attr_in1_min
.dev_attr
.attr
,
714 &sensor_dev_attr_in2_input
.dev_attr
.attr
,
715 &sensor_dev_attr_in2_max
.dev_attr
.attr
,
716 &sensor_dev_attr_in2_min
.dev_attr
.attr
,
717 &sensor_dev_attr_in3_input
.dev_attr
.attr
,
718 &sensor_dev_attr_in3_max
.dev_attr
.attr
,
719 &sensor_dev_attr_in3_min
.dev_attr
.attr
,
723 static const struct attribute_group f75375_group
= {
724 .attrs
= f75375_attributes
,
727 static void f75375_init(struct i2c_client
*client
, struct f75375_data
*data
,
728 struct f75375s_platform_data
*f75375s_pdata
)
732 if (!f75375s_pdata
) {
736 conf
= f75375_read8(client
, F75375_REG_CONFIG1
);
737 mode
= f75375_read8(client
, F75375_REG_FAN_TIMER
);
738 for (nr
= 0; nr
< 2; nr
++) {
739 if (data
->kind
== f75387
) {
742 if (!(mode
& (1 << F75387_FAN_CTRL_LINEAR(nr
))))
743 data
->pwm_mode
[nr
] = 1;
745 manu
= ((mode
>> F75387_FAN_MANU_MODE(nr
)) & 1);
746 duty
= ((mode
>> F75387_FAN_DUTY_MODE(nr
)) & 1);
749 data
->pwm_enable
[nr
] = 4;
750 else if (manu
&& !duty
)
752 data
->pwm_enable
[nr
] = 3;
753 else if (!manu
&& !duty
)
754 /* automatic, speed */
755 data
->pwm_enable
[nr
] = 2;
758 data
->pwm_enable
[nr
] = 1;
760 if (!(conf
& (1 << F75375_FAN_CTRL_LINEAR(nr
))))
761 data
->pwm_mode
[nr
] = 1;
763 switch ((mode
>> FAN_CTRL_MODE(nr
)) & 3) {
765 data
->pwm_enable
[nr
] = 3;
767 case 1: /* automatic */
768 data
->pwm_enable
[nr
] = 2;
770 default: /* manual */
771 data
->pwm_enable
[nr
] = 1;
779 set_pwm_enable_direct(client
, 0, f75375s_pdata
->pwm_enable
[0]);
780 set_pwm_enable_direct(client
, 1, f75375s_pdata
->pwm_enable
[1]);
781 for (nr
= 0; nr
< 2; nr
++) {
782 if (auto_mode_enabled(f75375s_pdata
->pwm_enable
[nr
]) ||
783 !duty_mode_enabled(f75375s_pdata
->pwm_enable
[nr
]))
785 data
->pwm
[nr
] = clamp_val(f75375s_pdata
->pwm
[nr
], 0, 255);
786 f75375_write_pwm(client
, nr
);
791 static int f75375_probe(struct i2c_client
*client
)
793 struct f75375_data
*data
;
794 struct f75375s_platform_data
*f75375s_pdata
=
795 dev_get_platdata(&client
->dev
);
798 if (!i2c_check_functionality(client
->adapter
,
799 I2C_FUNC_SMBUS_BYTE_DATA
))
801 data
= devm_kzalloc(&client
->dev
, sizeof(struct f75375_data
),
806 i2c_set_clientdata(client
, data
);
807 mutex_init(&data
->update_lock
);
808 data
->kind
= (uintptr_t)i2c_get_match_data(client
);
810 err
= sysfs_create_group(&client
->dev
.kobj
, &f75375_group
);
814 if (data
->kind
!= f75373
) {
815 err
= sysfs_chmod_file(&client
->dev
.kobj
,
816 &sensor_dev_attr_pwm1_mode
.dev_attr
.attr
,
820 err
= sysfs_chmod_file(&client
->dev
.kobj
,
821 &sensor_dev_attr_pwm2_mode
.dev_attr
.attr
,
827 data
->hwmon_dev
= hwmon_device_register(&client
->dev
);
828 if (IS_ERR(data
->hwmon_dev
)) {
829 err
= PTR_ERR(data
->hwmon_dev
);
833 f75375_init(client
, data
, f75375s_pdata
);
838 sysfs_remove_group(&client
->dev
.kobj
, &f75375_group
);
842 static void f75375_remove(struct i2c_client
*client
)
844 struct f75375_data
*data
= i2c_get_clientdata(client
);
845 hwmon_device_unregister(data
->hwmon_dev
);
846 sysfs_remove_group(&client
->dev
.kobj
, &f75375_group
);
849 /* Return 0 if detection is successful, -ENODEV otherwise */
850 static int f75375_detect(struct i2c_client
*client
,
851 struct i2c_board_info
*info
)
853 struct i2c_adapter
*adapter
= client
->adapter
;
858 vendid
= f75375_read16(client
, F75375_REG_VENDOR
);
859 chipid
= f75375_read16(client
, F75375_CHIP_ID
);
860 if (vendid
!= 0x1934)
863 if (chipid
== 0x0306)
865 else if (chipid
== 0x0204)
867 else if (chipid
== 0x0410)
872 version
= f75375_read8(client
, F75375_REG_VERSION
);
873 dev_info(&adapter
->dev
, "found %s version: %02X\n", name
, version
);
874 strscpy(info
->type
, name
, I2C_NAME_SIZE
);
879 static const struct i2c_device_id f75375_id
[] = {
880 { "f75373", f75373
},
881 { "f75375", f75375
},
882 { "f75387", f75387
},
885 MODULE_DEVICE_TABLE(i2c
, f75375_id
);
887 static struct i2c_driver f75375_driver
= {
888 .class = I2C_CLASS_HWMON
,
892 .probe
= f75375_probe
,
893 .remove
= f75375_remove
,
894 .id_table
= f75375_id
,
895 .detect
= f75375_detect
,
896 .address_list
= normal_i2c
,
898 module_i2c_driver(f75375_driver
);
900 MODULE_AUTHOR("Riku Voipio");
901 MODULE_LICENSE("GPL");
902 MODULE_DESCRIPTION("F75373/F75375/F75387 hardware monitoring driver");