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 int f75375_detect(struct i2c_client *client,
115 struct i2c_board_info *info);
116 static int f75375_probe(struct i2c_client *client);
117 static void f75375_remove(struct i2c_client *client);
119 static const struct i2c_device_id f75375_id[] = {
120 { "f75373", f75373 },
121 { "f75375", f75375 },
122 { "f75387", f75387 },
125 MODULE_DEVICE_TABLE(i2c, f75375_id);
127 static struct i2c_driver f75375_driver = {
128 .class = I2C_CLASS_HWMON,
132 .probe = f75375_probe,
133 .remove = f75375_remove,
134 .id_table = f75375_id,
135 .detect = f75375_detect,
136 .address_list = normal_i2c,
139 static inline int f75375_read8(struct i2c_client *client, u8 reg)
141 return i2c_smbus_read_byte_data(client, reg);
144 /* in most cases, should be called while holding update_lock */
145 static inline u16 f75375_read16(struct i2c_client *client, u8 reg)
147 return (i2c_smbus_read_byte_data(client, reg) << 8)
148 | i2c_smbus_read_byte_data(client, reg + 1);
151 static inline void f75375_write8(struct i2c_client *client, u8 reg,
154 i2c_smbus_write_byte_data(client, reg, value);
157 static inline void f75375_write16(struct i2c_client *client, u8 reg,
160 int err = i2c_smbus_write_byte_data(client, reg, (value >> 8));
163 i2c_smbus_write_byte_data(client, reg + 1, (value & 0xFF));
166 static void f75375_write_pwm(struct i2c_client *client, int nr)
168 struct f75375_data *data = i2c_get_clientdata(client);
169 if (data->kind == f75387)
170 f75375_write16(client, F75375_REG_FAN_EXP(nr), data->pwm[nr]);
172 f75375_write8(client, F75375_REG_FAN_PWM_DUTY(nr),
176 static struct f75375_data *f75375_update_device(struct device *dev)
178 struct i2c_client *client = to_i2c_client(dev);
179 struct f75375_data *data = i2c_get_clientdata(client);
182 mutex_lock(&data->update_lock);
184 /* Limit registers cache is refreshed after 60 seconds */
185 if (time_after(jiffies, data->last_limits + 60 * HZ)
187 for (nr = 0; nr < 2; nr++) {
188 data->temp_high[nr] =
189 f75375_read8(client, F75375_REG_TEMP_HIGH(nr));
190 data->temp_max_hyst[nr] =
191 f75375_read8(client, F75375_REG_TEMP_HYST(nr));
193 f75375_read16(client, F75375_REG_FAN_FULL(nr));
195 f75375_read16(client, F75375_REG_FAN_MIN(nr));
196 data->fan_target[nr] =
197 f75375_read16(client, F75375_REG_FAN_EXP(nr));
199 for (nr = 0; nr < 4; nr++) {
201 f75375_read8(client, F75375_REG_VOLT_HIGH(nr));
203 f75375_read8(client, F75375_REG_VOLT_LOW(nr));
205 data->fan_timer = f75375_read8(client, F75375_REG_FAN_TIMER);
206 data->last_limits = jiffies;
209 /* Measurement registers cache is refreshed after 2 second */
210 if (time_after(jiffies, data->last_updated + 2 * HZ)
212 for (nr = 0; nr < 2; nr++) {
213 data->pwm[nr] = f75375_read8(client,
214 F75375_REG_FAN_PWM_DUTY(nr));
215 /* assign MSB, therefore shift it by 8 bits */
217 f75375_read8(client, F75375_REG_TEMP(nr)) << 8;
218 if (data->kind == f75387)
219 /* merge F75387's temperature LSB (11-bit) */
222 F75387_REG_TEMP11_LSB(nr));
224 f75375_read16(client, F75375_REG_FAN(nr));
226 for (nr = 0; nr < 4; nr++)
228 f75375_read8(client, F75375_REG_VOLT(nr));
230 data->last_updated = jiffies;
234 mutex_unlock(&data->update_lock);
238 static inline u16 rpm_from_reg(u16 reg)
240 if (reg == 0 || reg == 0xffff)
242 return 1500000 / reg;
245 static inline u16 rpm_to_reg(int rpm)
247 if (rpm < 367 || rpm > 0xffff)
249 return 1500000 / rpm;
252 static bool duty_mode_enabled(u8 pwm_enable)
254 switch (pwm_enable) {
255 case 0: /* Manual, duty mode (full speed) */
256 case 1: /* Manual, duty mode */
257 case 4: /* Auto, duty mode */
259 case 2: /* Auto, speed mode */
260 case 3: /* Manual, speed mode */
263 WARN(1, "Unexpected pwm_enable value %d\n", pwm_enable);
268 static bool auto_mode_enabled(u8 pwm_enable)
270 switch (pwm_enable) {
271 case 0: /* Manual, duty mode (full speed) */
272 case 1: /* Manual, duty mode */
273 case 3: /* Manual, speed mode */
275 case 2: /* Auto, speed mode */
276 case 4: /* Auto, duty mode */
279 WARN(1, "Unexpected pwm_enable value %d\n", pwm_enable);
284 static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
285 const char *buf, size_t count)
287 int nr = to_sensor_dev_attr(attr)->index;
288 struct i2c_client *client = to_i2c_client(dev);
289 struct f75375_data *data = i2c_get_clientdata(client);
293 err = kstrtoul(buf, 10, &val);
297 mutex_lock(&data->update_lock);
298 data->fan_min[nr] = rpm_to_reg(val);
299 f75375_write16(client, F75375_REG_FAN_MIN(nr), data->fan_min[nr]);
300 mutex_unlock(&data->update_lock);
304 static ssize_t set_fan_target(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]))
319 if (data->kind == f75387 && duty_mode_enabled(data->pwm_enable[nr]))
322 mutex_lock(&data->update_lock);
323 data->fan_target[nr] = rpm_to_reg(val);
324 f75375_write16(client, F75375_REG_FAN_EXP(nr), data->fan_target[nr]);
325 mutex_unlock(&data->update_lock);
329 static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
330 const char *buf, size_t count)
332 int nr = to_sensor_dev_attr(attr)->index;
333 struct i2c_client *client = to_i2c_client(dev);
334 struct f75375_data *data = i2c_get_clientdata(client);
338 err = kstrtoul(buf, 10, &val);
342 if (auto_mode_enabled(data->pwm_enable[nr]) ||
343 !duty_mode_enabled(data->pwm_enable[nr]))
346 mutex_lock(&data->update_lock);
347 data->pwm[nr] = clamp_val(val, 0, 255);
348 f75375_write_pwm(client, nr);
349 mutex_unlock(&data->update_lock);
353 static ssize_t show_pwm_enable(struct device *dev, struct device_attribute
356 int nr = to_sensor_dev_attr(attr)->index;
357 struct f75375_data *data = f75375_update_device(dev);
358 return sprintf(buf, "%d\n", data->pwm_enable[nr]);
361 static int set_pwm_enable_direct(struct i2c_client *client, int nr, int val)
363 struct f75375_data *data = i2c_get_clientdata(client);
366 if (val < 0 || val > 4)
369 fanmode = f75375_read8(client, F75375_REG_FAN_TIMER);
370 if (data->kind == f75387) {
371 /* For now, deny dangerous toggling of duty mode */
372 if (duty_mode_enabled(data->pwm_enable[nr]) !=
373 duty_mode_enabled(val))
375 /* clear each fanX_mode bit before setting them properly */
376 fanmode &= ~(1 << F75387_FAN_DUTY_MODE(nr));
377 fanmode &= ~(1 << F75387_FAN_MANU_MODE(nr));
379 case 0: /* full speed */
380 fanmode |= (1 << F75387_FAN_MANU_MODE(nr));
381 fanmode |= (1 << F75387_FAN_DUTY_MODE(nr));
385 fanmode |= (1 << F75387_FAN_MANU_MODE(nr));
386 fanmode |= (1 << F75387_FAN_DUTY_MODE(nr));
388 case 2: /* Automatic, speed mode */
390 case 3: /* fan speed */
391 fanmode |= (1 << F75387_FAN_MANU_MODE(nr));
393 case 4: /* Automatic, pwm */
394 fanmode |= (1 << F75387_FAN_DUTY_MODE(nr));
398 /* clear each fanX_mode bit before setting them properly */
399 fanmode &= ~(3 << FAN_CTRL_MODE(nr));
401 case 0: /* full speed */
402 fanmode |= (3 << FAN_CTRL_MODE(nr));
406 fanmode |= (3 << FAN_CTRL_MODE(nr));
408 case 2: /* AUTOMATIC*/
409 fanmode |= (1 << FAN_CTRL_MODE(nr));
411 case 3: /* fan speed */
413 case 4: /* Automatic pwm */
418 f75375_write8(client, F75375_REG_FAN_TIMER, fanmode);
419 data->pwm_enable[nr] = val;
421 f75375_write_pwm(client, nr);
425 static ssize_t set_pwm_enable(struct device *dev, struct device_attribute *attr,
426 const char *buf, size_t count)
428 int nr = to_sensor_dev_attr(attr)->index;
429 struct i2c_client *client = to_i2c_client(dev);
430 struct f75375_data *data = i2c_get_clientdata(client);
434 err = kstrtoul(buf, 10, &val);
438 mutex_lock(&data->update_lock);
439 err = set_pwm_enable_direct(client, nr, val);
440 mutex_unlock(&data->update_lock);
441 return err ? err : count;
444 static ssize_t set_pwm_mode(struct device *dev, struct device_attribute *attr,
445 const char *buf, size_t count)
447 int nr = to_sensor_dev_attr(attr)->index;
448 struct i2c_client *client = to_i2c_client(dev);
449 struct f75375_data *data = i2c_get_clientdata(client);
455 err = kstrtoul(buf, 10, &val);
459 if (!(val == 0 || val == 1))
462 /* F75373 does not support DC (linear voltage) fan control mode */
463 if (data->kind == f75373 && val == 0)
466 /* take care for different registers */
467 if (data->kind == f75387) {
468 reg = F75375_REG_FAN_TIMER;
469 ctrl = F75387_FAN_CTRL_LINEAR(nr);
471 reg = F75375_REG_CONFIG1;
472 ctrl = F75375_FAN_CTRL_LINEAR(nr);
475 mutex_lock(&data->update_lock);
476 conf = f75375_read8(client, reg);
477 conf &= ~(1 << ctrl);
482 f75375_write8(client, reg, conf);
483 data->pwm_mode[nr] = val;
484 mutex_unlock(&data->update_lock);
488 static ssize_t show_pwm(struct device *dev, struct device_attribute
491 int nr = to_sensor_dev_attr(attr)->index;
492 struct f75375_data *data = f75375_update_device(dev);
493 return sprintf(buf, "%d\n", data->pwm[nr]);
496 static ssize_t show_pwm_mode(struct device *dev, struct device_attribute
499 int nr = to_sensor_dev_attr(attr)->index;
500 struct f75375_data *data = f75375_update_device(dev);
501 return sprintf(buf, "%d\n", data->pwm_mode[nr]);
504 #define VOLT_FROM_REG(val) ((val) * 8)
505 #define VOLT_TO_REG(val) ((val) / 8)
507 static ssize_t show_in(struct device *dev, struct device_attribute *attr,
510 int nr = to_sensor_dev_attr(attr)->index;
511 struct f75375_data *data = f75375_update_device(dev);
512 return sprintf(buf, "%d\n", VOLT_FROM_REG(data->in[nr]));
515 static ssize_t show_in_max(struct device *dev, struct device_attribute *attr,
518 int nr = to_sensor_dev_attr(attr)->index;
519 struct f75375_data *data = f75375_update_device(dev);
520 return sprintf(buf, "%d\n", VOLT_FROM_REG(data->in_max[nr]));
523 static ssize_t show_in_min(struct device *dev, struct device_attribute *attr,
526 int nr = to_sensor_dev_attr(attr)->index;
527 struct f75375_data *data = f75375_update_device(dev);
528 return sprintf(buf, "%d\n", VOLT_FROM_REG(data->in_min[nr]));
531 static ssize_t set_in_max(struct device *dev, struct device_attribute *attr,
532 const char *buf, size_t count)
534 int nr = to_sensor_dev_attr(attr)->index;
535 struct i2c_client *client = to_i2c_client(dev);
536 struct f75375_data *data = i2c_get_clientdata(client);
540 err = kstrtoul(buf, 10, &val);
544 val = clamp_val(VOLT_TO_REG(val), 0, 0xff);
545 mutex_lock(&data->update_lock);
546 data->in_max[nr] = val;
547 f75375_write8(client, F75375_REG_VOLT_HIGH(nr), data->in_max[nr]);
548 mutex_unlock(&data->update_lock);
552 static ssize_t set_in_min(struct device *dev, struct device_attribute *attr,
553 const char *buf, size_t count)
555 int nr = to_sensor_dev_attr(attr)->index;
556 struct i2c_client *client = to_i2c_client(dev);
557 struct f75375_data *data = i2c_get_clientdata(client);
561 err = kstrtoul(buf, 10, &val);
565 val = clamp_val(VOLT_TO_REG(val), 0, 0xff);
566 mutex_lock(&data->update_lock);
567 data->in_min[nr] = val;
568 f75375_write8(client, F75375_REG_VOLT_LOW(nr), data->in_min[nr]);
569 mutex_unlock(&data->update_lock);
572 #define TEMP_FROM_REG(val) ((val) * 1000)
573 #define TEMP_TO_REG(val) ((val) / 1000)
574 #define TEMP11_FROM_REG(reg) ((reg) / 32 * 125)
576 static ssize_t show_temp11(struct device *dev, struct device_attribute *attr,
579 int nr = to_sensor_dev_attr(attr)->index;
580 struct f75375_data *data = f75375_update_device(dev);
581 return sprintf(buf, "%d\n", TEMP11_FROM_REG(data->temp11[nr]));
584 static ssize_t show_temp_max(struct device *dev, struct device_attribute *attr,
587 int nr = to_sensor_dev_attr(attr)->index;
588 struct f75375_data *data = f75375_update_device(dev);
589 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_high[nr]));
592 static ssize_t show_temp_max_hyst(struct device *dev,
593 struct device_attribute *attr, char *buf)
595 int nr = to_sensor_dev_attr(attr)->index;
596 struct f75375_data *data = f75375_update_device(dev);
597 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max_hyst[nr]));
600 static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
601 const char *buf, size_t count)
603 int nr = to_sensor_dev_attr(attr)->index;
604 struct i2c_client *client = to_i2c_client(dev);
605 struct f75375_data *data = i2c_get_clientdata(client);
609 err = kstrtoul(buf, 10, &val);
613 val = clamp_val(TEMP_TO_REG(val), 0, 127);
614 mutex_lock(&data->update_lock);
615 data->temp_high[nr] = val;
616 f75375_write8(client, F75375_REG_TEMP_HIGH(nr), data->temp_high[nr]);
617 mutex_unlock(&data->update_lock);
621 static ssize_t set_temp_max_hyst(struct device *dev,
622 struct device_attribute *attr, const char *buf, size_t count)
624 int nr = to_sensor_dev_attr(attr)->index;
625 struct i2c_client *client = to_i2c_client(dev);
626 struct f75375_data *data = i2c_get_clientdata(client);
630 err = kstrtoul(buf, 10, &val);
634 val = clamp_val(TEMP_TO_REG(val), 0, 127);
635 mutex_lock(&data->update_lock);
636 data->temp_max_hyst[nr] = val;
637 f75375_write8(client, F75375_REG_TEMP_HYST(nr),
638 data->temp_max_hyst[nr]);
639 mutex_unlock(&data->update_lock);
643 #define show_fan(thing) \
644 static ssize_t show_##thing(struct device *dev, struct device_attribute *attr, \
647 int nr = to_sensor_dev_attr(attr)->index;\
648 struct f75375_data *data = f75375_update_device(dev); \
649 return sprintf(buf, "%d\n", rpm_from_reg(data->thing[nr])); \
655 show_fan(fan_target);
657 static SENSOR_DEVICE_ATTR(in0_input, S_IRUGO, show_in, NULL, 0);
658 static SENSOR_DEVICE_ATTR(in0_max, S_IRUGO|S_IWUSR,
659 show_in_max, set_in_max, 0);
660 static SENSOR_DEVICE_ATTR(in0_min, S_IRUGO|S_IWUSR,
661 show_in_min, set_in_min, 0);
662 static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, show_in, NULL, 1);
663 static SENSOR_DEVICE_ATTR(in1_max, S_IRUGO|S_IWUSR,
664 show_in_max, set_in_max, 1);
665 static SENSOR_DEVICE_ATTR(in1_min, S_IRUGO|S_IWUSR,
666 show_in_min, set_in_min, 1);
667 static SENSOR_DEVICE_ATTR(in2_input, S_IRUGO, show_in, NULL, 2);
668 static SENSOR_DEVICE_ATTR(in2_max, S_IRUGO|S_IWUSR,
669 show_in_max, set_in_max, 2);
670 static SENSOR_DEVICE_ATTR(in2_min, S_IRUGO|S_IWUSR,
671 show_in_min, set_in_min, 2);
672 static SENSOR_DEVICE_ATTR(in3_input, S_IRUGO, show_in, NULL, 3);
673 static SENSOR_DEVICE_ATTR(in3_max, S_IRUGO|S_IWUSR,
674 show_in_max, set_in_max, 3);
675 static SENSOR_DEVICE_ATTR(in3_min, S_IRUGO|S_IWUSR,
676 show_in_min, set_in_min, 3);
677 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp11, NULL, 0);
678 static SENSOR_DEVICE_ATTR(temp1_max_hyst, S_IRUGO|S_IWUSR,
679 show_temp_max_hyst, set_temp_max_hyst, 0);
680 static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO|S_IWUSR,
681 show_temp_max, set_temp_max, 0);
682 static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp11, NULL, 1);
683 static SENSOR_DEVICE_ATTR(temp2_max_hyst, S_IRUGO|S_IWUSR,
684 show_temp_max_hyst, set_temp_max_hyst, 1);
685 static SENSOR_DEVICE_ATTR(temp2_max, S_IRUGO|S_IWUSR,
686 show_temp_max, set_temp_max, 1);
687 static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0);
688 static SENSOR_DEVICE_ATTR(fan1_max, S_IRUGO, show_fan_max, NULL, 0);
689 static SENSOR_DEVICE_ATTR(fan1_min, S_IRUGO|S_IWUSR,
690 show_fan_min, set_fan_min, 0);
691 static SENSOR_DEVICE_ATTR(fan1_target, S_IRUGO|S_IWUSR,
692 show_fan_target, set_fan_target, 0);
693 static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, show_fan, NULL, 1);
694 static SENSOR_DEVICE_ATTR(fan2_max, S_IRUGO, show_fan_max, NULL, 1);
695 static SENSOR_DEVICE_ATTR(fan2_min, S_IRUGO|S_IWUSR,
696 show_fan_min, set_fan_min, 1);
697 static SENSOR_DEVICE_ATTR(fan2_target, S_IRUGO|S_IWUSR,
698 show_fan_target, set_fan_target, 1);
699 static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO|S_IWUSR,
700 show_pwm, set_pwm, 0);
701 static SENSOR_DEVICE_ATTR(pwm1_enable, S_IRUGO|S_IWUSR,
702 show_pwm_enable, set_pwm_enable, 0);
703 static SENSOR_DEVICE_ATTR(pwm1_mode, S_IRUGO,
704 show_pwm_mode, set_pwm_mode, 0);
705 static SENSOR_DEVICE_ATTR(pwm2, S_IRUGO | S_IWUSR,
706 show_pwm, set_pwm, 1);
707 static SENSOR_DEVICE_ATTR(pwm2_enable, S_IRUGO|S_IWUSR,
708 show_pwm_enable, set_pwm_enable, 1);
709 static SENSOR_DEVICE_ATTR(pwm2_mode, S_IRUGO,
710 show_pwm_mode, set_pwm_mode, 1);
712 static struct attribute *f75375_attributes[] = {
713 &sensor_dev_attr_temp1_input.dev_attr.attr,
714 &sensor_dev_attr_temp1_max.dev_attr.attr,
715 &sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
716 &sensor_dev_attr_temp2_input.dev_attr.attr,
717 &sensor_dev_attr_temp2_max.dev_attr.attr,
718 &sensor_dev_attr_temp2_max_hyst.dev_attr.attr,
719 &sensor_dev_attr_fan1_input.dev_attr.attr,
720 &sensor_dev_attr_fan1_max.dev_attr.attr,
721 &sensor_dev_attr_fan1_min.dev_attr.attr,
722 &sensor_dev_attr_fan1_target.dev_attr.attr,
723 &sensor_dev_attr_fan2_input.dev_attr.attr,
724 &sensor_dev_attr_fan2_max.dev_attr.attr,
725 &sensor_dev_attr_fan2_min.dev_attr.attr,
726 &sensor_dev_attr_fan2_target.dev_attr.attr,
727 &sensor_dev_attr_pwm1.dev_attr.attr,
728 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
729 &sensor_dev_attr_pwm1_mode.dev_attr.attr,
730 &sensor_dev_attr_pwm2.dev_attr.attr,
731 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
732 &sensor_dev_attr_pwm2_mode.dev_attr.attr,
733 &sensor_dev_attr_in0_input.dev_attr.attr,
734 &sensor_dev_attr_in0_max.dev_attr.attr,
735 &sensor_dev_attr_in0_min.dev_attr.attr,
736 &sensor_dev_attr_in1_input.dev_attr.attr,
737 &sensor_dev_attr_in1_max.dev_attr.attr,
738 &sensor_dev_attr_in1_min.dev_attr.attr,
739 &sensor_dev_attr_in2_input.dev_attr.attr,
740 &sensor_dev_attr_in2_max.dev_attr.attr,
741 &sensor_dev_attr_in2_min.dev_attr.attr,
742 &sensor_dev_attr_in3_input.dev_attr.attr,
743 &sensor_dev_attr_in3_max.dev_attr.attr,
744 &sensor_dev_attr_in3_min.dev_attr.attr,
748 static const struct attribute_group f75375_group = {
749 .attrs = f75375_attributes,
752 static void f75375_init(struct i2c_client *client, struct f75375_data *data,
753 struct f75375s_platform_data *f75375s_pdata)
757 if (!f75375s_pdata) {
761 conf = f75375_read8(client, F75375_REG_CONFIG1);
762 mode = f75375_read8(client, F75375_REG_FAN_TIMER);
763 for (nr = 0; nr < 2; nr++) {
764 if (data->kind == f75387) {
767 if (!(mode & (1 << F75387_FAN_CTRL_LINEAR(nr))))
768 data->pwm_mode[nr] = 1;
770 manu = ((mode >> F75387_FAN_MANU_MODE(nr)) & 1);
771 duty = ((mode >> F75387_FAN_DUTY_MODE(nr)) & 1);
774 data->pwm_enable[nr] = 4;
775 else if (manu && !duty)
777 data->pwm_enable[nr] = 3;
778 else if (!manu && !duty)
779 /* automatic, speed */
780 data->pwm_enable[nr] = 2;
783 data->pwm_enable[nr] = 1;
785 if (!(conf & (1 << F75375_FAN_CTRL_LINEAR(nr))))
786 data->pwm_mode[nr] = 1;
788 switch ((mode >> FAN_CTRL_MODE(nr)) & 3) {
790 data->pwm_enable[nr] = 3;
792 case 1: /* automatic */
793 data->pwm_enable[nr] = 2;
795 default: /* manual */
796 data->pwm_enable[nr] = 1;
804 set_pwm_enable_direct(client, 0, f75375s_pdata->pwm_enable[0]);
805 set_pwm_enable_direct(client, 1, f75375s_pdata->pwm_enable[1]);
806 for (nr = 0; nr < 2; nr++) {
807 if (auto_mode_enabled(f75375s_pdata->pwm_enable[nr]) ||
808 !duty_mode_enabled(f75375s_pdata->pwm_enable[nr]))
810 data->pwm[nr] = clamp_val(f75375s_pdata->pwm[nr], 0, 255);
811 f75375_write_pwm(client, nr);
816 static int f75375_probe(struct i2c_client *client)
818 struct f75375_data *data;
819 struct f75375s_platform_data *f75375s_pdata =
820 dev_get_platdata(&client->dev);
823 if (!i2c_check_functionality(client->adapter,
824 I2C_FUNC_SMBUS_BYTE_DATA))
826 data = devm_kzalloc(&client->dev, sizeof(struct f75375_data),
831 i2c_set_clientdata(client, data);
832 mutex_init(&data->update_lock);
833 data->kind = i2c_match_id(f75375_id, client)->driver_data;
835 err = sysfs_create_group(&client->dev.kobj, &f75375_group);
839 if (data->kind != f75373) {
840 err = sysfs_chmod_file(&client->dev.kobj,
841 &sensor_dev_attr_pwm1_mode.dev_attr.attr,
845 err = sysfs_chmod_file(&client->dev.kobj,
846 &sensor_dev_attr_pwm2_mode.dev_attr.attr,
852 data->hwmon_dev = hwmon_device_register(&client->dev);
853 if (IS_ERR(data->hwmon_dev)) {
854 err = PTR_ERR(data->hwmon_dev);
858 f75375_init(client, data, f75375s_pdata);
863 sysfs_remove_group(&client->dev.kobj, &f75375_group);
867 static void f75375_remove(struct i2c_client *client)
869 struct f75375_data *data = i2c_get_clientdata(client);
870 hwmon_device_unregister(data->hwmon_dev);
871 sysfs_remove_group(&client->dev.kobj, &f75375_group);
874 /* Return 0 if detection is successful, -ENODEV otherwise */
875 static int f75375_detect(struct i2c_client *client,
876 struct i2c_board_info *info)
878 struct i2c_adapter *adapter = client->adapter;
883 vendid = f75375_read16(client, F75375_REG_VENDOR);
884 chipid = f75375_read16(client, F75375_CHIP_ID);
885 if (vendid != 0x1934)
888 if (chipid == 0x0306)
890 else if (chipid == 0x0204)
892 else if (chipid == 0x0410)
897 version = f75375_read8(client, F75375_REG_VERSION);
898 dev_info(&adapter->dev, "found %s version: %02X\n", name, version);
899 strscpy(info->type, name, I2C_NAME_SIZE);
904 module_i2c_driver(f75375_driver);
906 MODULE_AUTHOR("Riku Voipio");
907 MODULE_LICENSE("GPL");
908 MODULE_DESCRIPTION("F75373/F75375/F75387 hardware monitoring driver");