34 #include <linux/module.h>
36 #include <linux/slab.h>
38 #include <linux/i2c.h>
48 static int fan_voltage;
52 static int clock = 254000;
62 #define MAX6650_REG_SPEED 0x00
63 #define MAX6650_REG_CONFIG 0x02
64 #define MAX6650_REG_GPIO_DEF 0x04
65 #define MAX6650_REG_DAC 0x06
66 #define MAX6650_REG_ALARM_EN 0x08
67 #define MAX6650_REG_ALARM 0x0A
68 #define MAX6650_REG_TACH0 0x0C
69 #define MAX6650_REG_TACH1 0x0E
70 #define MAX6650_REG_TACH2 0x10
71 #define MAX6650_REG_TACH3 0x12
72 #define MAX6650_REG_GPIO_STAT 0x14
73 #define MAX6650_REG_COUNT 0x16
79 #define MAX6650_CFG_V12 0x08
80 #define MAX6650_CFG_PRESCALER_MASK 0x07
81 #define MAX6650_CFG_PRESCALER_2 0x01
82 #define MAX6650_CFG_PRESCALER_4 0x02
83 #define MAX6650_CFG_PRESCALER_8 0x03
84 #define MAX6650_CFG_PRESCALER_16 0x04
85 #define MAX6650_CFG_MODE_MASK 0x30
86 #define MAX6650_CFG_MODE_ON 0x00
87 #define MAX6650_CFG_MODE_OFF 0x10
88 #define MAX6650_CFG_MODE_CLOSED_LOOP 0x20
89 #define MAX6650_CFG_MODE_OPEN_LOOP 0x30
90 #define MAX6650_COUNT_MASK 0x03
96 #define MAX6650_ALRM_MAX 0x01
97 #define MAX6650_ALRM_MIN 0x02
98 #define MAX6650_ALRM_TACH 0x04
99 #define MAX6650_ALRM_GPIO1 0x08
100 #define MAX6650_ALRM_GPIO2 0x10
103 #define FAN_RPM_MIN 240
104 #define FAN_RPM_MAX 30000
106 #define DIV_FROM_REG(reg) (1 << (reg & 7))
129 .probe = max6650_probe,
130 .remove = max6650_remove,
131 .id_table = max6650_id,
171 return sprintf(buf,
"%d\n", rpm);
219 int kscale, ktach, rpm;
231 rpm = 60 * kscale *
clock / (256 * (ktach + 1));
232 return sprintf(buf,
"%d\n", rpm);
244 err = kstrtoul(buf, 10, &rpm);
260 ktach = ((
clock * kscale) / (256 * rpm / 60)) - 1;
294 pwm = 255 - (255 * (
int)data->
dac)/180;
296 pwm = 255 - (255 * (
int)data->
dac)/76;
301 return sprintf(buf,
"%d\n", pwm);
305 const char *buf,
size_t count)
312 err = kstrtoul(buf, 10, &pwm);
316 pwm = SENSORS_LIMIT(pwm, 0, 255);
321 data->
dac = 180 - (180 *
pwm)/255;
323 data->
dac = 76 - (76 *
pwm)/255;
345 int sysfs_modes[4] = {0, 1, 2, 1};
347 return sprintf(buf,
"%d\n", sysfs_modes[mode]);
351 const char *buf,
size_t count)
355 int max6650_modes[3] = {0, 3, 2};
359 err = kstrtoul(buf, 10, &mode);
370 | (max6650_modes[mode] << 4);
401 const char *buf,
size_t count)
408 err = kstrtoul(buf, 10, &div);
461 return sprintf(buf,
"%d\n", alarm);
496 if (devattr == &sensor_dev_attr_fan1_max_alarm.dev_attr
497 || devattr == &sensor_dev_attr_fan1_min_alarm.dev_attr
498 || devattr == &sensor_dev_attr_fan1_fault.dev_attr
499 || devattr == &sensor_dev_attr_gpio1_alarm.dev_attr
500 || devattr == &sensor_dev_attr_gpio2_alarm.dev_attr) {
508 static struct attribute *max6650_attrs[] = {
509 &sensor_dev_attr_fan1_input.dev_attr.attr,
510 &dev_attr_fan1_target.attr,
511 &dev_attr_fan1_div.attr,
512 &dev_attr_pwm1_enable.attr,
514 &sensor_dev_attr_fan1_max_alarm.dev_attr.attr,
515 &sensor_dev_attr_fan1_min_alarm.dev_attr.attr,
516 &sensor_dev_attr_fan1_fault.dev_attr.attr,
517 &sensor_dev_attr_gpio1_alarm.dev_attr.attr,
518 &sensor_dev_attr_gpio2_alarm.dev_attr.attr,
523 .attrs = max6650_attrs,
524 .is_visible = max6650_attrs_visible,
527 static struct attribute *max6651_attrs[] = {
528 &sensor_dev_attr_fan2_input.dev_attr.attr,
529 &sensor_dev_attr_fan3_input.dev_attr.attr,
530 &sensor_dev_attr_fan4_input.dev_attr.attr,
535 .attrs = max6651_attrs,
542 static int max6650_probe(
struct i2c_client *client,
555 i2c_set_clientdata(client, data);
557 data->
nr_fans =
id->driver_data;
562 err = max6650_init_client(client);
581 dev_err(&client->
dev,
"error registering hwmon device.\n");
589 static int max6650_remove(
struct i2c_client *client)
600 static int max6650_init_client(
struct i2c_client *client)
609 dev_err(&client->
dev,
"Error reading config, aborting.\n");
613 switch (fan_voltage) {
623 dev_err(&client->
dev,
"illegal value for fan_voltage (%d)\n",
627 dev_info(&client->
dev,
"Fan voltage is set to %dV.\n",
653 dev_err(&client->
dev,
"illegal value for prescaler (%d)\n",
657 dev_info(&client->
dev,
"Prescaler is set to %d.\n",
667 dev_dbg(&client->
dev,
"Change mode to open loop, full off.\n");
668 config = (config & ~MAX6650_CFG_MODE_MASK)
671 dev_err(&client->
dev,
"DAC write error, aborting.\n");
677 dev_err(&client->
dev,
"Config write error, aborting.\n");
687 static const u8 tach_reg[] = {
707 for (i = 0; i < data->
nr_fans; i++) {