From: Arun Saravanan Balachandran <Arun_Saravanan_Balac@xxxxxxxx> Update MAX6620 driver to comply with linux code guidelines Signed-off-by: Arun Saravanan Balachandran <Arun_Saravanan_Balac@xxxxxxxx> --- drivers/hwmon/max6620.c | 187 +++++++++++++--------------------------- 1 file changed, 59 insertions(+), 128 deletions(-) diff --git a/drivers/hwmon/max6620.c b/drivers/hwmon/max6620.c index 6f921bbf3831..a1b747081939 100644 --- a/drivers/hwmon/max6620.c +++ b/drivers/hwmon/max6620.c @@ -1,3 +1,4 @@ +// SPDX-License-Identifier: GPL-2.0-or-later /* * max6620.c - Linux Kernel module for hardware monitoring. * @@ -15,19 +16,6 @@ * * http://pdfserv.maxim-ic.com/en/ds/MAX6620.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; either version 2 of the License, or - * (at your option) any later version. - * - * 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> @@ -46,10 +34,9 @@ /* clock: The clock frequency of the chip the driver should assume */ static int clock = 8192; -static u32 sr = 2; static u32 np = 2; -module_param(clock, int, S_IRUGO); +module_param(clock, int, 0444); static const unsigned short normal_i2c[] = {0x0a, 0x1a, 0x2a, I2C_CLIENT_END}; @@ -211,8 +198,8 @@ struct max6620_data { u8 fault; }; -static ssize_t get_fan(struct device *dev, struct device_attribute *devattr, char *buf) { - +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 max6620_data *data = max6620_update_device(dev); struct i2c_client *client = to_i2c_client(dev); @@ -226,17 +213,16 @@ static ssize_t get_fan(struct device *dev, struct device_attribute *devattr, cha tach2 = i2c_smbus_read_byte_data(client, tach_reg[attr->index] + 1); tach2 = (tach2 >> 5) & 0x7; tach = tach1 | tach2; - if (tach == 0) { + if (tach == 0) rpm = 0; - } else { + else rpm = (60 * DIV_FROM_REG(data->fandyn[attr->index]) * clock)/(tach * np); - } return sprintf(buf, "%d\n", rpm); } -static ssize_t get_target(struct device *dev, struct device_attribute *devattr, char *buf) { - +static ssize_t get_target(struct device *dev, struct device_attribute *devattr, char *buf) +{ struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); struct max6620_data *data = max6620_update_device(dev); struct i2c_client *client = to_i2c_client(dev); @@ -250,16 +236,17 @@ static ssize_t get_target(struct device *dev, struct device_attribute *devattr, target2 = i2c_smbus_read_byte_data(client, target_reg[attr->index] + 1); target2 = (target2 >> 5) & 0x7; target = target1 | target2; - if (target == 0) { + if (target == 0) rpm = 0; - } else { + else rpm = (60 * DIV_FROM_REG(data->fandyn[attr->index]) * clock)/(target * np); - } + return sprintf(buf, "%d\n", rpm); } -static ssize_t set_target(struct device *dev, struct device_attribute *devattr, const char *buf, size_t count) { - +static ssize_t +set_target(struct device *dev, struct device_attribute *devattr, const char *buf, size_t count) +{ struct i2c_client *client = to_i2c_client(dev); struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); struct max6620_data *data = i2c_get_clientdata(client); @@ -300,8 +287,8 @@ static ssize_t set_target(struct device *dev, struct device_attribute *devattr, * back exactly the value you have set. */ -static ssize_t get_pwm(struct device *dev, struct device_attribute *devattr, char *buf) { - +static ssize_t get_pwm(struct device *dev, struct device_attribute *devattr, char *buf) +{ int pwm; struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); struct max6620_data *data = max6620_update_device(dev); @@ -318,8 +305,9 @@ static ssize_t get_pwm(struct device *dev, struct device_attribute *devattr, cha return sprintf(buf, "%d\n", pwm); } -static ssize_t set_pwm(struct device *dev, struct device_attribute *devattr, const char *buf, size_t count) { - +static ssize_t +set_pwm(struct device *dev, struct device_attribute *devattr, const char *buf, size_t count) +{ struct i2c_client *client = to_i2c_client(dev); struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); struct max6620_data *data = i2c_get_clientdata(client); @@ -334,7 +322,7 @@ static ssize_t set_pwm(struct device *dev, struct device_attribute *devattr, con mutex_lock(&data->update_lock); - data->dac[attr->index] = pwm; + data->dac[attr->index] = pwm; i2c_smbus_write_byte_data(client, dac_reg[attr->index], data->dac[attr->index]); @@ -345,53 +333,6 @@ static ssize_t set_pwm(struct device *dev, struct device_attribute *devattr, con return count; } -/* - * Get/Set controller mode: - * Possible values: - * 0 = Fan always on - * 1 = Open loop, Voltage is set according to speed, not regulated. - * 2 = Closed loop, RPM for all fans regulated by fan1 tachometer - */ - -static ssize_t get_enable(struct device *dev, struct device_attribute *devattr, char *buf) { - - struct max6620_data *data = max6620_update_device(dev); - struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); - int mode = (data->fancfg[attr->index] & 0x80 ) >> 7; - int sysfs_modes[2] = {1, 2}; - - 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 i2c_client *client = to_i2c_client(dev); - struct max6620_data *data = i2c_get_clientdata(client); - struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); - int max6620_modes[3] = {0, 1, 0}; - unsigned long mode; - int err; - - err = kstrtoul(buf, 10, &mode); - if (err) - return err; - - if (mode > 2) - return -EINVAL; - - mutex_lock(&data->update_lock); - - data->fancfg[attr->index] = i2c_smbus_read_byte_data(client, config_reg[attr->index]); - data->fancfg[attr->index] = (data->fancfg[attr->index] & ~0x80) - | (max6620_modes[mode] << 7); - - i2c_smbus_write_byte_data(client, config_reg[attr->index], data->fancfg[attr->index]); - - mutex_unlock(&data->update_lock); - - return count; -} - /* * Read/write functions for fan1_div sysfs file. The MAX6620 has no such * divider. We handle this by converting between divider and counttime: @@ -405,16 +346,17 @@ static ssize_t set_enable(struct device *dev, struct device_attribute *devattr, * defined for that. See the data sheet for details. */ -static ssize_t get_div(struct device *dev, struct device_attribute *devattr, char *buf) { - +static ssize_t get_div(struct device *dev, struct device_attribute *devattr, char *buf) +{ struct max6620_data *data = max6620_update_device(dev); struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); return sprintf(buf, "%d\n", DIV_FROM_REG(data->fandyn[attr->index])); } -static ssize_t set_div(struct device *dev, struct device_attribute *devattr, const char *buf, size_t count) { - +static ssize_t +set_div(struct device *dev, struct device_attribute *devattr, const char *buf, size_t count) +{ struct i2c_client *client = to_i2c_client(dev); struct max6620_data *data = i2c_get_clientdata(client); struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); @@ -465,46 +407,41 @@ static ssize_t set_div(struct device *dev, struct device_attribute *devattr, con * 1 = alarm */ -static ssize_t get_alarm(struct device *dev, struct device_attribute *devattr, char *buf) { - +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 max6620_data *data = max6620_update_device(dev); - struct i2c_client *client = to_i2c_client(dev); int alarm = 0; mutex_lock(&data->update_lock); - if (data->fault & (1 << attr->index)) { + if (data->fault & (1 << attr->index)) alarm = 1; - } + 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 SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, get_alarm, NULL, 0); -static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, get_alarm, NULL, 1); -static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, get_alarm, NULL, 2); -static SENSOR_DEVICE_ATTR(fan4_alarm, S_IRUGO, get_alarm, NULL, 3); -static SENSOR_DEVICE_ATTR(fan1_target, S_IWUSR | S_IRUGO, get_target, set_target, 0); -static SENSOR_DEVICE_ATTR(fan1_div, S_IWUSR | S_IRUGO, get_div, set_div, 0); -// static SENSOR_DEVICE_ATTR(pwm1_enable, S_IWUSR | S_IRUGO, get_enable, set_enable, 0); -static SENSOR_DEVICE_ATTR(pwm1, S_IWUSR | S_IRUGO, get_pwm, set_pwm, 0); -static SENSOR_DEVICE_ATTR(fan2_target, S_IWUSR | S_IRUGO, get_target, set_target, 1); -static SENSOR_DEVICE_ATTR(fan2_div, S_IWUSR | S_IRUGO, get_div, set_div, 1); -// static SENSOR_DEVICE_ATTR(pwm2_enable, S_IWUSR | S_IRUGO, get_enable, set_enable, 1); -static SENSOR_DEVICE_ATTR(pwm2, S_IWUSR | S_IRUGO, get_pwm, set_pwm, 1); -static SENSOR_DEVICE_ATTR(fan3_target, S_IWUSR | S_IRUGO, get_target, set_target, 2); -static SENSOR_DEVICE_ATTR(fan3_div, S_IWUSR | S_IRUGO, get_div, set_div, 2); -// static SENSOR_DEVICE_ATTR(pwm3_enable, S_IWUSR | S_IRUGO, get_enable, set_enable, 2); -static SENSOR_DEVICE_ATTR(pwm3, S_IWUSR | S_IRUGO, get_pwm, set_pwm, 2); -static SENSOR_DEVICE_ATTR(fan4_target, S_IWUSR | S_IRUGO, get_target, set_target, 3); -static SENSOR_DEVICE_ATTR(fan4_div, S_IWUSR | S_IRUGO, get_div, set_div, 3); -// static SENSOR_DEVICE_ATTR(pwm4_enable, S_IWUSR | S_IRUGO, get_enable, set_enable, 3); -static SENSOR_DEVICE_ATTR(pwm4, S_IWUSR | S_IRUGO, get_pwm, set_pwm, 3); +static SENSOR_DEVICE_ATTR(fan1_input, 0444, get_fan, NULL, 0); +static SENSOR_DEVICE_ATTR(fan2_input, 0444, get_fan, NULL, 1); +static SENSOR_DEVICE_ATTR(fan3_input, 0444, get_fan, NULL, 2); +static SENSOR_DEVICE_ATTR(fan4_input, 0444, get_fan, NULL, 3); +static SENSOR_DEVICE_ATTR(fan1_alarm, 0444, get_alarm, NULL, 0); +static SENSOR_DEVICE_ATTR(fan2_alarm, 0444, get_alarm, NULL, 1); +static SENSOR_DEVICE_ATTR(fan3_alarm, 0444, get_alarm, NULL, 2); +static SENSOR_DEVICE_ATTR(fan4_alarm, 0444, get_alarm, NULL, 3); +static SENSOR_DEVICE_ATTR(fan1_target, 0644, get_target, set_target, 0); +static SENSOR_DEVICE_ATTR(fan1_div, 0644, get_div, set_div, 0); +static SENSOR_DEVICE_ATTR(pwm1, 0644, get_pwm, set_pwm, 0); +static SENSOR_DEVICE_ATTR(fan2_target, 0644, get_target, set_target, 1); +static SENSOR_DEVICE_ATTR(fan2_div, 0644, get_div, set_div, 1); +static SENSOR_DEVICE_ATTR(pwm2, 0644, get_pwm, set_pwm, 1); +static SENSOR_DEVICE_ATTR(fan3_target, 0644, get_target, set_target, 2); +static SENSOR_DEVICE_ATTR(fan3_div, 0644, get_div, set_div, 2); +static SENSOR_DEVICE_ATTR(pwm3, 0644, get_pwm, set_pwm, 2); +static SENSOR_DEVICE_ATTR(fan4_target, 0644, get_target, set_target, 3); +static SENSOR_DEVICE_ATTR(fan4_div, 0644, get_div, set_div, 3); +static SENSOR_DEVICE_ATTR(pwm4, 0644, get_pwm, set_pwm, 3); static struct attribute *max6620_attrs[] = { &sensor_dev_attr_fan1_input.dev_attr.attr, @@ -517,19 +454,15 @@ static struct attribute *max6620_attrs[] = { &sensor_dev_attr_fan4_alarm.dev_attr.attr, &sensor_dev_attr_fan1_target.dev_attr.attr, &sensor_dev_attr_fan1_div.dev_attr.attr, -// &sensor_dev_attr_pwm1_enable.dev_attr.attr, &sensor_dev_attr_pwm1.dev_attr.attr, &sensor_dev_attr_fan2_target.dev_attr.attr, &sensor_dev_attr_fan2_div.dev_attr.attr, -// &sensor_dev_attr_pwm2_enable.dev_attr.attr, &sensor_dev_attr_pwm2.dev_attr.attr, &sensor_dev_attr_fan3_target.dev_attr.attr, &sensor_dev_attr_fan3_div.dev_attr.attr, -// &sensor_dev_attr_pwm3_enable.dev_attr.attr, &sensor_dev_attr_pwm3.dev_attr.attr, &sensor_dev_attr_fan4_target.dev_attr.attr, &sensor_dev_attr_fan4_div.dev_attr.attr, -// &sensor_dev_attr_pwm4_enable.dev_attr.attr, &sensor_dev_attr_pwm4.dev_attr.attr, NULL }; @@ -543,16 +476,14 @@ static struct attribute_group max6620_attr_grp = { * Real code */ -static int max6620_probe(struct i2c_client *client, const struct i2c_device_id *id) { - +static int max6620_probe(struct i2c_client *client, const struct i2c_device_id *id) +{ struct max6620_data *data; int err; data = devm_kzalloc(&client->dev, sizeof(struct max6620_data), GFP_KERNEL); - if (!data) { - dev_err(&client->dev, "out of memory.\n"); + if (!data) return -ENOMEM; - } i2c_set_clientdata(client, data); mutex_init(&data->update_lock); @@ -571,7 +502,7 @@ static int max6620_probe(struct i2c_client *client, const struct i2c_device_id * err = sysfs_create_group(&client->dev.kobj, &max6620_attr_grp); if (err) return err; -dev_info(&client->dev, "Sysfs entries created\n"); + dev_info(&client->dev, "Sysfs entries created\n"); data->hwmon_dev = hwmon_device_register(&client->dev); if (!IS_ERR(data->hwmon_dev)) @@ -584,8 +515,8 @@ dev_info(&client->dev, "Sysfs entries created\n"); return err; } -static int max6620_remove(struct i2c_client *client) { - +static int max6620_remove(struct i2c_client *client) +{ struct max6620_data *data = i2c_get_clientdata(client); hwmon_device_unregister(data->hwmon_dev); @@ -594,8 +525,8 @@ static int max6620_remove(struct i2c_client *client) { return 0; } -static int max6620_init_client(struct i2c_client *client) { - +static int max6620_init_client(struct i2c_client *client) +{ struct max6620_data *data = i2c_get_clientdata(client); int config; int err = -EIO; @@ -624,7 +555,7 @@ static int max6620_init_client(struct i2c_client *client) { i2c_smbus_write_byte_data(client, config_reg[i], data->fancfg[i]); data->fandyn[i] = i2c_smbus_read_byte_data(client, dyn_reg[i]); /* 2 counts (001) and Rate change 100 (0.125 secs) */ - data-> fandyn[i] = 0x30; + data->fandyn[i] = 0x30; i2c_smbus_write_byte_data(client, dyn_reg[i], data->fandyn[i]); data->tach[i] = i2c_smbus_read_byte_data(client, tach_reg[i]); data->volt[i] = i2c_smbus_read_byte_data(client, volt_reg[i]); @@ -637,6 +568,7 @@ static int max6620_init_client(struct i2c_client *client) { static struct max6620_data *max6620_update_device(struct device *dev) { int i; + u8 fault_reg; struct i2c_client *client = to_i2c_client(dev); struct max6620_data *data = i2c_get_clientdata(client); @@ -659,7 +591,6 @@ static struct max6620_data *max6620_update_device(struct device *dev) * caused the alarm is removed. Keep the value latched here * for providing the register through different alarm files. */ - u8 fault_reg; fault_reg = i2c_smbus_read_byte_data(client, MAX6620_REG_FAULT); data->fault |= (fault_reg >> 4) & (fault_reg & 0x0F); -- 2.32.0