diff options
Diffstat (limited to 'drivers/hwmon/adm1029.c')
| -rw-r--r-- | drivers/hwmon/adm1029.c | 303 |
1 files changed, 127 insertions, 176 deletions
diff --git a/drivers/hwmon/adm1029.c b/drivers/hwmon/adm1029.c index 9ee5e066423b..71eea8ae51b9 100644 --- a/drivers/hwmon/adm1029.c +++ b/drivers/hwmon/adm1029.c @@ -1,28 +1,15 @@ +// SPDX-License-Identifier: GPL-2.0 /* * adm1029.c - Part of lm_sensors, Linux kernel modules for hardware monitoring * - * Copyright (C) 2006 Corentin LABBE <corentin.labbe@geomatys.fr> + * Copyright (C) 2006 Corentin LABBE <clabbe.montjoie@gmail.com> * - * Based on LM83 Driver by Jean Delvare <khali@linux-fr.org> + * Based on LM83 Driver by Jean Delvare <jdelvare@suse.de> * * Give only processor, motherboard temperatures and fan tachs * Very rare chip please let me know if you use it * * http://www.analog.com/UploadedFiles/Data_Sheets/ADM1029.pdf - * - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation version 2 of the License - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ #include <linux/module.h> @@ -106,47 +93,13 @@ static const u8 ADM1029_REG_FAN_DIV[] = { }; /* - * Functions declaration - */ - -static int adm1029_probe(struct i2c_client *client, - const struct i2c_device_id *id); -static int adm1029_detect(struct i2c_client *client, - struct i2c_board_info *info); -static int adm1029_remove(struct i2c_client *client); -static struct adm1029_data *adm1029_update_device(struct device *dev); -static int adm1029_init_client(struct i2c_client *client); - -/* - * Driver data (common to all clients) - */ - -static const struct i2c_device_id adm1029_id[] = { - { "adm1029", 0 }, - { } -}; -MODULE_DEVICE_TABLE(i2c, adm1029_id); - -static struct i2c_driver adm1029_driver = { - .class = I2C_CLASS_HWMON, - .driver = { - .name = "adm1029", - }, - .probe = adm1029_probe, - .remove = adm1029_remove, - .id_table = adm1029_id, - .detect = adm1029_detect, - .address_list = normal_i2c, -}; - -/* * Client data (each client gets its own) */ struct adm1029_data { - struct device *hwmon_dev; - struct mutex update_lock; - char valid; /* zero until following fields are valid */ + struct i2c_client *client; + struct mutex update_lock; /* protect register access */ + bool valid; /* false until following fields are valid */ unsigned long last_updated; /* in jiffies */ /* registers values, signed for temperature, unsigned for other stuff */ @@ -156,53 +109,104 @@ struct adm1029_data { }; /* + * function that update the status of the chips (temperature for example) + */ +static struct adm1029_data *adm1029_update_device(struct device *dev) +{ + struct adm1029_data *data = dev_get_drvdata(dev); + struct i2c_client *client = data->client; + + mutex_lock(&data->update_lock); + /* + * Use the "cache" Luke, don't recheck values + * if there are already checked not a long time later + */ + if (time_after(jiffies, data->last_updated + HZ * 2) || !data->valid) { + int nr; + + dev_dbg(&client->dev, "Updating adm1029 data\n"); + + for (nr = 0; nr < ARRAY_SIZE(ADM1029_REG_TEMP); nr++) { + data->temp[nr] = + i2c_smbus_read_byte_data(client, + ADM1029_REG_TEMP[nr]); + } + for (nr = 0; nr < ARRAY_SIZE(ADM1029_REG_FAN); nr++) { + data->fan[nr] = + i2c_smbus_read_byte_data(client, + ADM1029_REG_FAN[nr]); + } + for (nr = 0; nr < ARRAY_SIZE(ADM1029_REG_FAN_DIV); nr++) { + data->fan_div[nr] = + i2c_smbus_read_byte_data(client, + ADM1029_REG_FAN_DIV[nr]); + } + + data->last_updated = jiffies; + data->valid = true; + } + + mutex_unlock(&data->update_lock); + + return data; +} + +/* * Sysfs stuff */ static ssize_t -show_temp(struct device *dev, struct device_attribute *devattr, char *buf) +temp_show(struct device *dev, struct device_attribute *devattr, char *buf) { struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); struct adm1029_data *data = adm1029_update_device(dev); + return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[attr->index])); } static ssize_t -show_fan(struct device *dev, struct device_attribute *devattr, char *buf) +fan_show(struct device *dev, struct device_attribute *devattr, char *buf) { struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); struct adm1029_data *data = adm1029_update_device(dev); u16 val; - if (data->fan[attr->index] == 0 - || (data->fan_div[attr->index] & 0xC0) == 0 - || data->fan[attr->index] == 255) { + + mutex_lock(&data->update_lock); + if (data->fan[attr->index] == 0 || + (data->fan_div[attr->index] & 0xC0) == 0 || + data->fan[attr->index] == 255) { + mutex_unlock(&data->update_lock); return sprintf(buf, "0\n"); } val = 1880 * 120 / DIV_FROM_REG(data->fan_div[attr->index]) / data->fan[attr->index]; + mutex_unlock(&data->update_lock); return sprintf(buf, "%d\n", val); } static ssize_t -show_fan_div(struct device *dev, struct device_attribute *devattr, char *buf) +fan_div_show(struct device *dev, struct device_attribute *devattr, char *buf) { struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); struct adm1029_data *data = adm1029_update_device(dev); + if ((data->fan_div[attr->index] & 0xC0) == 0) return sprintf(buf, "0\n"); return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[attr->index])); } -static ssize_t set_fan_div(struct device *dev, - struct device_attribute *devattr, const char *buf, size_t count) +static ssize_t fan_div_store(struct device *dev, + struct device_attribute *devattr, + const char *buf, size_t count) { - struct i2c_client *client = to_i2c_client(dev); - struct adm1029_data *data = i2c_get_clientdata(client); + struct adm1029_data *data = dev_get_drvdata(dev); + struct i2c_client *client = data->client; struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); u8 reg; long val; int ret = kstrtol(buf, 10, &val); + if (ret < 0) return ret; @@ -232,6 +236,9 @@ static ssize_t set_fan_div(struct device *dev, /* Update the value */ reg = (reg & 0x3F) | (val << 6); + /* Update the cache */ + data->fan_div[attr->index] = reg; + /* Write value */ i2c_smbus_write_byte_data(client, ADM1029_REG_FAN_DIV[attr->index], reg); @@ -240,34 +247,29 @@ static ssize_t set_fan_div(struct device *dev, return count; } -/* - * Access rights on sysfs. S_IRUGO: Is Readable by User, Group and Others - * S_IWUSR: Is Writable by User. - */ -static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0); -static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1); -static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, show_temp, NULL, 2); +/* Access rights on sysfs. */ +static SENSOR_DEVICE_ATTR_RO(temp1_input, temp, 0); +static SENSOR_DEVICE_ATTR_RO(temp2_input, temp, 1); +static SENSOR_DEVICE_ATTR_RO(temp3_input, temp, 2); -static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO, show_temp, NULL, 3); -static SENSOR_DEVICE_ATTR(temp2_max, S_IRUGO, show_temp, NULL, 4); -static SENSOR_DEVICE_ATTR(temp3_max, S_IRUGO, show_temp, NULL, 5); +static SENSOR_DEVICE_ATTR_RO(temp1_max, temp, 3); +static SENSOR_DEVICE_ATTR_RO(temp2_max, temp, 4); +static SENSOR_DEVICE_ATTR_RO(temp3_max, temp, 5); -static SENSOR_DEVICE_ATTR(temp1_min, S_IRUGO, show_temp, NULL, 6); -static SENSOR_DEVICE_ATTR(temp2_min, S_IRUGO, show_temp, NULL, 7); -static SENSOR_DEVICE_ATTR(temp3_min, S_IRUGO, show_temp, NULL, 8); +static SENSOR_DEVICE_ATTR_RO(temp1_min, temp, 6); +static SENSOR_DEVICE_ATTR_RO(temp2_min, temp, 7); +static SENSOR_DEVICE_ATTR_RO(temp3_min, temp, 8); -static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0); -static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, show_fan, NULL, 1); +static SENSOR_DEVICE_ATTR_RO(fan1_input, fan, 0); +static SENSOR_DEVICE_ATTR_RO(fan2_input, fan, 1); -static SENSOR_DEVICE_ATTR(fan1_min, S_IRUGO, show_fan, NULL, 2); -static SENSOR_DEVICE_ATTR(fan2_min, S_IRUGO, show_fan, NULL, 3); +static SENSOR_DEVICE_ATTR_RO(fan1_min, fan, 2); +static SENSOR_DEVICE_ATTR_RO(fan2_min, fan, 3); -static SENSOR_DEVICE_ATTR(fan1_div, S_IRUGO | S_IWUSR, - show_fan_div, set_fan_div, 0); -static SENSOR_DEVICE_ATTR(fan2_div, S_IRUGO | S_IWUSR, - show_fan_div, set_fan_div, 1); +static SENSOR_DEVICE_ATTR_RW(fan1_div, fan_div, 0); +static SENSOR_DEVICE_ATTR_RW(fan2_div, fan_div, 1); -static struct attribute *adm1029_attributes[] = { +static struct attribute *adm1029_attrs[] = { &sensor_dev_attr_temp1_input.dev_attr.attr, &sensor_dev_attr_temp1_min.dev_attr.attr, &sensor_dev_attr_temp1_max.dev_attr.attr, @@ -286,9 +288,7 @@ static struct attribute *adm1029_attributes[] = { NULL }; -static const struct attribute_group adm1029_group = { - .attrs = adm1029_attributes, -}; +ATTRIBUTE_GROUPS(adm1029); /* * Real code @@ -316,10 +316,10 @@ static int adm1029_detect(struct i2c_client *client, temp_devices_installed = i2c_smbus_read_byte_data(client, ADM1029_REG_TEMP_DEVICES_INSTALLED); nb_fan_support = i2c_smbus_read_byte_data(client, - ADM1029_REG_NB_FAN_SUPPORT); + ADM1029_REG_NB_FAN_SUPPORT); /* 0x41 is Analog Devices */ - if (man_id != 0x41 || (temp_devices_installed & 0xf9) != 0x01 - || nb_fan_support != 0x03) + if (man_id != 0x41 || (temp_devices_installed & 0xf9) != 0x01 || + nb_fan_support != 0x03) return -ENODEV; if ((chip_id & 0xF0) != 0x00) { @@ -332,53 +332,15 @@ static int adm1029_detect(struct i2c_client *client, return -ENODEV; } - strlcpy(info->type, "adm1029", I2C_NAME_SIZE); + strscpy(info->type, "adm1029", I2C_NAME_SIZE); return 0; } -static int adm1029_probe(struct i2c_client *client, - const struct i2c_device_id *id) -{ - struct adm1029_data *data; - int err; - - data = devm_kzalloc(&client->dev, sizeof(struct adm1029_data), - GFP_KERNEL); - if (!data) - return -ENOMEM; - - i2c_set_clientdata(client, data); - mutex_init(&data->update_lock); - - /* - * Initialize the ADM1029 chip - * Check config register - */ - if (adm1029_init_client(client) == 0) - return -ENODEV; - - /* Register sysfs hooks */ - err = sysfs_create_group(&client->dev.kobj, &adm1029_group); - if (err) - return err; - - data->hwmon_dev = hwmon_device_register(&client->dev); - if (IS_ERR(data->hwmon_dev)) { - err = PTR_ERR(data->hwmon_dev); - goto exit_remove_files; - } - - return 0; - - exit_remove_files: - sysfs_remove_group(&client->dev.kobj, &adm1029_group); - return err; -} - static int adm1029_init_client(struct i2c_client *client) { u8 config; + config = i2c_smbus_read_byte_data(client, ADM1029_REG_CONFIG); if ((config & 0x10) == 0) { i2c_smbus_write_byte_data(client, ADM1029_REG_CONFIG, @@ -393,62 +355,51 @@ static int adm1029_init_client(struct i2c_client *client) return 1; } -static int adm1029_remove(struct i2c_client *client) +static int adm1029_probe(struct i2c_client *client) { - struct adm1029_data *data = i2c_get_clientdata(client); - - hwmon_device_unregister(data->hwmon_dev); - sysfs_remove_group(&client->dev.kobj, &adm1029_group); + struct device *dev = &client->dev; + struct adm1029_data *data; + struct device *hwmon_dev; - return 0; -} + data = devm_kzalloc(dev, sizeof(struct adm1029_data), GFP_KERNEL); + if (!data) + return -ENOMEM; -/* - * function that update the status of the chips (temperature for example) - */ -static struct adm1029_data *adm1029_update_device(struct device *dev) -{ - struct i2c_client *client = to_i2c_client(dev); - struct adm1029_data *data = i2c_get_clientdata(client); + data->client = client; + mutex_init(&data->update_lock); - mutex_lock(&data->update_lock); /* - * Use the "cache" Luke, don't recheck values - * if there are already checked not a long time later + * Initialize the ADM1029 chip + * Check config register */ - if (time_after(jiffies, data->last_updated + HZ * 2) - || !data->valid) { - int nr; - - dev_dbg(&client->dev, "Updating adm1029 data\n"); - - for (nr = 0; nr < ARRAY_SIZE(ADM1029_REG_TEMP); nr++) { - data->temp[nr] = - i2c_smbus_read_byte_data(client, - ADM1029_REG_TEMP[nr]); - } - for (nr = 0; nr < ARRAY_SIZE(ADM1029_REG_FAN); nr++) { - data->fan[nr] = - i2c_smbus_read_byte_data(client, - ADM1029_REG_FAN[nr]); - } - for (nr = 0; nr < ARRAY_SIZE(ADM1029_REG_FAN_DIV); nr++) { - data->fan_div[nr] = - i2c_smbus_read_byte_data(client, - ADM1029_REG_FAN_DIV[nr]); - } + if (adm1029_init_client(client) == 0) + return -ENODEV; - data->last_updated = jiffies; - data->valid = 1; - } + hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name, + data, + adm1029_groups); + return PTR_ERR_OR_ZERO(hwmon_dev); +} - mutex_unlock(&data->update_lock); +static const struct i2c_device_id adm1029_id[] = { + { "adm1029" }, + { } +}; +MODULE_DEVICE_TABLE(i2c, adm1029_id); - return data; -} +static struct i2c_driver adm1029_driver = { + .class = I2C_CLASS_HWMON, + .driver = { + .name = "adm1029", + }, + .probe = adm1029_probe, + .id_table = adm1029_id, + .detect = adm1029_detect, + .address_list = normal_i2c, +}; module_i2c_driver(adm1029_driver); -MODULE_AUTHOR("Corentin LABBE <corentin.labbe@geomatys.fr>"); +MODULE_AUTHOR("Corentin LABBE <clabbe.montjoie@gmail.com>"); MODULE_DESCRIPTION("adm1029 driver"); MODULE_LICENSE("GPL v2"); |
