123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754 |
- #include <linux/module.h>
- #include <linux/init.h>
- #include <linux/slab.h>
- #include <linux/jiffies.h>
- #include <linux/i2c.h>
- #include <linux/hwmon.h>
- #include <linux/hwmon-sysfs.h>
- #include <linux/err.h>
- #include <linux/of_device.h>
- static int fan_voltage;
- static int prescaler;
- static int clock = 254000;
- module_param(fan_voltage, int, S_IRUGO);
- module_param(prescaler, int, S_IRUGO);
- module_param(clock, int, S_IRUGO);
- #define MAX6650_REG_SPEED 0x00
- #define MAX6650_REG_CONFIG 0x02
- #define MAX6650_REG_GPIO_DEF 0x04
- #define MAX6650_REG_DAC 0x06
- #define MAX6650_REG_ALARM_EN 0x08
- #define MAX6650_REG_ALARM 0x0A
- #define MAX6650_REG_TACH0 0x0C
- #define MAX6650_REG_TACH1 0x0E
- #define MAX6650_REG_TACH2 0x10
- #define MAX6650_REG_TACH3 0x12
- #define MAX6650_REG_GPIO_STAT 0x14
- #define MAX6650_REG_COUNT 0x16
- #define MAX6650_CFG_V12 0x08
- #define MAX6650_CFG_PRESCALER_MASK 0x07
- #define MAX6650_CFG_PRESCALER_2 0x01
- #define MAX6650_CFG_PRESCALER_4 0x02
- #define MAX6650_CFG_PRESCALER_8 0x03
- #define MAX6650_CFG_PRESCALER_16 0x04
- #define MAX6650_CFG_MODE_MASK 0x30
- #define MAX6650_CFG_MODE_ON 0x00
- #define MAX6650_CFG_MODE_OFF 0x10
- #define MAX6650_CFG_MODE_CLOSED_LOOP 0x20
- #define MAX6650_CFG_MODE_OPEN_LOOP 0x30
- #define MAX6650_COUNT_MASK 0x03
- #define MAX6650_ALRM_MAX 0x01
- #define MAX6650_ALRM_MIN 0x02
- #define MAX6650_ALRM_TACH 0x04
- #define MAX6650_ALRM_GPIO1 0x08
- #define MAX6650_ALRM_GPIO2 0x10
- #define FAN_RPM_MIN 240
- #define FAN_RPM_MAX 30000
- #define DIV_FROM_REG(reg) (1 << (reg & 7))
- struct max6650_data {
- struct i2c_client *client;
- const struct attribute_group *groups[3];
- struct mutex update_lock;
- int nr_fans;
- char valid;
- unsigned long last_updated;
-
- u8 speed;
- u8 config;
- u8 tach[4];
- u8 count;
- u8 dac;
- u8 alarm;
- };
- static const u8 tach_reg[] = {
- MAX6650_REG_TACH0,
- MAX6650_REG_TACH1,
- MAX6650_REG_TACH2,
- MAX6650_REG_TACH3,
- };
- static const struct of_device_id max6650_dt_match[] = {
- {
- .compatible = "maxim,max6650",
- .data = (void *)1
- },
- {
- .compatible = "maxim,max6651",
- .data = (void *)4
- },
- { },
- };
- MODULE_DEVICE_TABLE(of, max6650_dt_match);
- static struct max6650_data *max6650_update_device(struct device *dev)
- {
- struct max6650_data *data = dev_get_drvdata(dev);
- struct i2c_client *client = data->client;
- int i;
- mutex_lock(&data->update_lock);
- if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
- data->speed = i2c_smbus_read_byte_data(client,
- MAX6650_REG_SPEED);
- data->config = i2c_smbus_read_byte_data(client,
- MAX6650_REG_CONFIG);
- for (i = 0; i < data->nr_fans; i++) {
- data->tach[i] = i2c_smbus_read_byte_data(client,
- tach_reg[i]);
- }
- data->count = i2c_smbus_read_byte_data(client,
- MAX6650_REG_COUNT);
- data->dac = i2c_smbus_read_byte_data(client, MAX6650_REG_DAC);
-
- data->alarm |= i2c_smbus_read_byte_data(client,
- MAX6650_REG_ALARM);
- data->last_updated = jiffies;
- data->valid = 1;
- }
- mutex_unlock(&data->update_lock);
- return data;
- }
- static int max6650_set_operating_mode(struct max6650_data *data, u8 mode)
- {
- int result;
- u8 config = data->config;
- if (mode == (config & MAX6650_CFG_MODE_MASK))
- return 0;
- config = (config & ~MAX6650_CFG_MODE_MASK) | mode;
- result = i2c_smbus_write_byte_data(data->client, MAX6650_REG_CONFIG,
- config);
- if (result < 0)
- return result;
- data->config = config;
- return 0;
- }
- static ssize_t get_fan(struct device *dev, struct device_attribute *devattr,
- char *buf)
- {
- struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
- struct max6650_data *data = max6650_update_device(dev);
- int rpm;
-
- rpm = ((data->tach[attr->index] * 120) / DIV_FROM_REG(data->count));
- return sprintf(buf, "%d\n", rpm);
- }
- static ssize_t get_target(struct device *dev, struct device_attribute *devattr,
- char *buf)
- {
- struct max6650_data *data = max6650_update_device(dev);
- int kscale, ktach, rpm;
-
- kscale = DIV_FROM_REG(data->config);
- ktach = data->speed;
- rpm = 60 * kscale * clock / (256 * (ktach + 1));
- return sprintf(buf, "%d\n", rpm);
- }
- static int max6650_set_target(struct max6650_data *data, unsigned long rpm)
- {
- int kscale, ktach;
- if (rpm == 0)
- return max6650_set_operating_mode(data, MAX6650_CFG_MODE_OFF);
- rpm = clamp_val(rpm, FAN_RPM_MIN, FAN_RPM_MAX);
-
- kscale = DIV_FROM_REG(data->config);
- ktach = ((clock * kscale) / (256 * rpm / 60)) - 1;
- if (ktach < 0)
- ktach = 0;
- if (ktach > 255)
- ktach = 255;
- data->speed = ktach;
- return i2c_smbus_write_byte_data(data->client, MAX6650_REG_SPEED,
- data->speed);
- }
- static ssize_t set_target(struct device *dev, struct device_attribute *devattr,
- const char *buf, size_t count)
- {
- struct max6650_data *data = dev_get_drvdata(dev);
- unsigned long rpm;
- int err;
- err = kstrtoul(buf, 10, &rpm);
- if (err)
- return err;
- mutex_lock(&data->update_lock);
- err = max6650_set_target(data, rpm);
- mutex_unlock(&data->update_lock);
- if (err < 0)
- return err;
- return count;
- }
- static ssize_t get_pwm(struct device *dev, struct device_attribute *devattr,
- char *buf)
- {
- int pwm;
- struct max6650_data *data = max6650_update_device(dev);
-
- if (data->config & MAX6650_CFG_V12)
- pwm = 255 - (255 * (int)data->dac)/180;
- else
- pwm = 255 - (255 * (int)data->dac)/76;
- if (pwm < 0)
- pwm = 0;
- return sprintf(buf, "%d\n", pwm);
- }
- static ssize_t set_pwm(struct device *dev, struct device_attribute *devattr,
- const char *buf, size_t count)
- {
- struct max6650_data *data = dev_get_drvdata(dev);
- struct i2c_client *client = data->client;
- unsigned long pwm;
- int err;
- err = kstrtoul(buf, 10, &pwm);
- if (err)
- return err;
- pwm = clamp_val(pwm, 0, 255);
- mutex_lock(&data->update_lock);
- if (data->config & MAX6650_CFG_V12)
- data->dac = 180 - (180 * pwm)/255;
- else
- data->dac = 76 - (76 * pwm)/255;
- err = i2c_smbus_write_byte_data(client, MAX6650_REG_DAC, data->dac);
- mutex_unlock(&data->update_lock);
- return err < 0 ? err : count;
- }
- static ssize_t get_enable(struct device *dev, struct device_attribute *devattr,
- char *buf)
- {
- struct max6650_data *data = max6650_update_device(dev);
- int mode = (data->config & MAX6650_CFG_MODE_MASK) >> 4;
- int sysfs_modes[4] = {0, 3, 2, 1};
- return sprintf(buf, "%d\n", sysfs_modes[mode]);
- }
- static ssize_t set_enable(struct device *dev, struct device_attribute *devattr,
- const char *buf, size_t count)
- {
- struct max6650_data *data = dev_get_drvdata(dev);
- unsigned long mode;
- int err;
- const u8 max6650_modes[] = {
- MAX6650_CFG_MODE_ON,
- MAX6650_CFG_MODE_OPEN_LOOP,
- MAX6650_CFG_MODE_CLOSED_LOOP,
- MAX6650_CFG_MODE_OFF,
- };
- err = kstrtoul(buf, 10, &mode);
- if (err)
- return err;
- if (mode >= ARRAY_SIZE(max6650_modes))
- return -EINVAL;
- mutex_lock(&data->update_lock);
- max6650_set_operating_mode(data, max6650_modes[mode]);
- mutex_unlock(&data->update_lock);
- return count;
- }
- static ssize_t get_div(struct device *dev, struct device_attribute *devattr,
- char *buf)
- {
- struct max6650_data *data = max6650_update_device(dev);
- return sprintf(buf, "%d\n", DIV_FROM_REG(data->count));
- }
- static ssize_t set_div(struct device *dev, struct device_attribute *devattr,
- const char *buf, size_t count)
- {
- struct max6650_data *data = dev_get_drvdata(dev);
- struct i2c_client *client = data->client;
- unsigned long div;
- int err;
- err = kstrtoul(buf, 10, &div);
- if (err)
- return err;
- mutex_lock(&data->update_lock);
- switch (div) {
- case 1:
- data->count = 0;
- break;
- case 2:
- data->count = 1;
- break;
- case 4:
- data->count = 2;
- break;
- case 8:
- data->count = 3;
- break;
- default:
- mutex_unlock(&data->update_lock);
- return -EINVAL;
- }
- i2c_smbus_write_byte_data(client, MAX6650_REG_COUNT, data->count);
- mutex_unlock(&data->update_lock);
- return count;
- }
- static ssize_t get_alarm(struct device *dev, struct device_attribute *devattr,
- char *buf)
- {
- struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
- struct max6650_data *data = max6650_update_device(dev);
- struct i2c_client *client = data->client;
- int alarm = 0;
- if (data->alarm & attr->index) {
- mutex_lock(&data->update_lock);
- alarm = 1;
- data->alarm &= ~attr->index;
- data->alarm |= i2c_smbus_read_byte_data(client,
- MAX6650_REG_ALARM);
- mutex_unlock(&data->update_lock);
- }
- return sprintf(buf, "%d\n", alarm);
- }
- static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, get_fan, NULL, 0);
- static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, get_fan, NULL, 1);
- static SENSOR_DEVICE_ATTR(fan3_input, S_IRUGO, get_fan, NULL, 2);
- static SENSOR_DEVICE_ATTR(fan4_input, S_IRUGO, get_fan, NULL, 3);
- static DEVICE_ATTR(fan1_target, S_IWUSR | S_IRUGO, get_target, set_target);
- static DEVICE_ATTR(fan1_div, S_IWUSR | S_IRUGO, get_div, set_div);
- static DEVICE_ATTR(pwm1_enable, S_IWUSR | S_IRUGO, get_enable, set_enable);
- static DEVICE_ATTR(pwm1, S_IWUSR | S_IRUGO, get_pwm, set_pwm);
- static SENSOR_DEVICE_ATTR(fan1_max_alarm, S_IRUGO, get_alarm, NULL,
- MAX6650_ALRM_MAX);
- static SENSOR_DEVICE_ATTR(fan1_min_alarm, S_IRUGO, get_alarm, NULL,
- MAX6650_ALRM_MIN);
- static SENSOR_DEVICE_ATTR(fan1_fault, S_IRUGO, get_alarm, NULL,
- MAX6650_ALRM_TACH);
- static SENSOR_DEVICE_ATTR(gpio1_alarm, S_IRUGO, get_alarm, NULL,
- MAX6650_ALRM_GPIO1);
- static SENSOR_DEVICE_ATTR(gpio2_alarm, S_IRUGO, get_alarm, NULL,
- MAX6650_ALRM_GPIO2);
- static umode_t max6650_attrs_visible(struct kobject *kobj, struct attribute *a,
- int n)
- {
- struct device *dev = container_of(kobj, struct device, kobj);
- struct max6650_data *data = dev_get_drvdata(dev);
- struct i2c_client *client = data->client;
- u8 alarm_en = i2c_smbus_read_byte_data(client, MAX6650_REG_ALARM_EN);
- struct device_attribute *devattr;
-
- devattr = container_of(a, struct device_attribute, attr);
- if (devattr == &sensor_dev_attr_fan1_max_alarm.dev_attr
- || devattr == &sensor_dev_attr_fan1_min_alarm.dev_attr
- || devattr == &sensor_dev_attr_fan1_fault.dev_attr
- || devattr == &sensor_dev_attr_gpio1_alarm.dev_attr
- || devattr == &sensor_dev_attr_gpio2_alarm.dev_attr) {
- if (!(alarm_en & to_sensor_dev_attr(devattr)->index))
- return 0;
- }
- return a->mode;
- }
- static struct attribute *max6650_attrs[] = {
- &sensor_dev_attr_fan1_input.dev_attr.attr,
- &dev_attr_fan1_target.attr,
- &dev_attr_fan1_div.attr,
- &dev_attr_pwm1_enable.attr,
- &dev_attr_pwm1.attr,
- &sensor_dev_attr_fan1_max_alarm.dev_attr.attr,
- &sensor_dev_attr_fan1_min_alarm.dev_attr.attr,
- &sensor_dev_attr_fan1_fault.dev_attr.attr,
- &sensor_dev_attr_gpio1_alarm.dev_attr.attr,
- &sensor_dev_attr_gpio2_alarm.dev_attr.attr,
- NULL
- };
- static const struct attribute_group max6650_group = {
- .attrs = max6650_attrs,
- .is_visible = max6650_attrs_visible,
- };
- static struct attribute *max6651_attrs[] = {
- &sensor_dev_attr_fan2_input.dev_attr.attr,
- &sensor_dev_attr_fan3_input.dev_attr.attr,
- &sensor_dev_attr_fan4_input.dev_attr.attr,
- NULL
- };
- static const struct attribute_group max6651_group = {
- .attrs = max6651_attrs,
- };
- static int max6650_init_client(struct max6650_data *data,
- struct i2c_client *client)
- {
- struct device *dev = &client->dev;
- int config;
- int err = -EIO;
- u32 voltage;
- u32 prescale;
- u32 target_rpm;
- if (of_property_read_u32(dev->of_node, "maxim,fan-microvolt",
- &voltage))
- voltage = fan_voltage;
- else
- voltage /= 1000000;
- if (of_property_read_u32(dev->of_node, "maxim,fan-prescale",
- &prescale))
- prescale = prescaler;
- config = i2c_smbus_read_byte_data(client, MAX6650_REG_CONFIG);
- if (config < 0) {
- dev_err(dev, "Error reading config, aborting.\n");
- return err;
- }
- switch (voltage) {
- case 0:
- break;
- case 5:
- config &= ~MAX6650_CFG_V12;
- break;
- case 12:
- config |= MAX6650_CFG_V12;
- break;
- default:
- dev_err(dev, "illegal value for fan_voltage (%d)\n", voltage);
- }
- switch (prescale) {
- case 0:
- break;
- case 1:
- config &= ~MAX6650_CFG_PRESCALER_MASK;
- break;
- case 2:
- config = (config & ~MAX6650_CFG_PRESCALER_MASK)
- | MAX6650_CFG_PRESCALER_2;
- break;
- case 4:
- config = (config & ~MAX6650_CFG_PRESCALER_MASK)
- | MAX6650_CFG_PRESCALER_4;
- break;
- case 8:
- config = (config & ~MAX6650_CFG_PRESCALER_MASK)
- | MAX6650_CFG_PRESCALER_8;
- break;
- case 16:
- config = (config & ~MAX6650_CFG_PRESCALER_MASK)
- | MAX6650_CFG_PRESCALER_16;
- break;
- default:
- dev_err(dev, "illegal value for prescaler (%d)\n", prescale);
- }
- dev_info(dev, "Fan voltage: %dV, prescaler: %d.\n",
- (config & MAX6650_CFG_V12) ? 12 : 5,
- 1 << (config & MAX6650_CFG_PRESCALER_MASK));
- if (i2c_smbus_write_byte_data(client, MAX6650_REG_CONFIG, config)) {
- dev_err(dev, "Config write error, aborting.\n");
- return err;
- }
- data->config = config;
- data->count = i2c_smbus_read_byte_data(client, MAX6650_REG_COUNT);
- if (!of_property_read_u32(client->dev.of_node, "maxim,fan-target-rpm",
- &target_rpm)) {
- max6650_set_target(data, target_rpm);
- max6650_set_operating_mode(data, MAX6650_CFG_MODE_CLOSED_LOOP);
- }
- return 0;
- }
- static int max6650_probe(struct i2c_client *client,
- const struct i2c_device_id *id)
- {
- struct device *dev = &client->dev;
- const struct of_device_id *of_id =
- of_match_device(of_match_ptr(max6650_dt_match), dev);
- struct max6650_data *data;
- struct device *hwmon_dev;
- int err;
- data = devm_kzalloc(dev, sizeof(struct max6650_data), GFP_KERNEL);
- if (!data)
- return -ENOMEM;
- data->client = client;
- mutex_init(&data->update_lock);
- data->nr_fans = of_id ? (int)(uintptr_t)of_id->data : id->driver_data;
-
- err = max6650_init_client(data, client);
- if (err)
- return err;
- data->groups[0] = &max6650_group;
-
- if (data->nr_fans == 4)
- data->groups[1] = &max6651_group;
- hwmon_dev = devm_hwmon_device_register_with_groups(dev,
- client->name, data,
- data->groups);
- return PTR_ERR_OR_ZERO(hwmon_dev);
- }
- static const struct i2c_device_id max6650_id[] = {
- { "max6650", 1 },
- { "max6651", 4 },
- { }
- };
- MODULE_DEVICE_TABLE(i2c, max6650_id);
- static struct i2c_driver max6650_driver = {
- .driver = {
- .name = "max6650",
- .of_match_table = of_match_ptr(max6650_dt_match),
- },
- .probe = max6650_probe,
- .id_table = max6650_id,
- };
- module_i2c_driver(max6650_driver);
- MODULE_AUTHOR("Hans J. Koch");
- MODULE_DESCRIPTION("MAX6650 sensor driver");
- MODULE_LICENSE("GPL");
|