Commit 398e16db authored by Axel Lin's avatar Axel Lin Committed by Guenter Roeck

hwmon: (g762) Convert to hwmon_device_register_with_groups

Use ATTRIBUTE_GROUPS macro and hwmon_device_register_with_groups() to simplify
the code a bit.

Use hwmon_device_register_with_groups rather than the device managed version to
ensure g762_of_clock_disable() is called after hwmon_device_unregister().
Signed-off-by: default avatarAxel Lin <axel.lin@ingics.com>
Signed-off-by: default avatarGuenter Roeck <linux@roeck-us.net>
parent 9dd304f8
...@@ -128,8 +128,8 @@ enum g762_regs { ...@@ -128,8 +128,8 @@ enum g762_regs {
G762_REG_FAN_CMD2_GEAR_MODE_1)) >> 2)) G762_REG_FAN_CMD2_GEAR_MODE_1)) >> 2))
struct g762_data { struct g762_data {
struct i2c_client *client;
struct device *hwmon_dev; struct device *hwmon_dev;
struct i2c_client *client;
struct clk *clk; struct clk *clk;
/* update mutex */ /* update mutex */
...@@ -206,8 +206,8 @@ static inline unsigned char cnt_from_rpm(u32 rpm, u32 clk_freq, u16 p, ...@@ -206,8 +206,8 @@ static inline unsigned char cnt_from_rpm(u32 rpm, u32 clk_freq, u16 p,
/* helper to grab and cache data, at most one time per second */ /* helper to grab and cache data, at most one time per second */
static struct g762_data *g762_update_client(struct device *dev) static struct g762_data *g762_update_client(struct device *dev)
{ {
struct i2c_client *client = to_i2c_client(dev); struct g762_data *data = dev_get_drvdata(dev);
struct g762_data *data = i2c_get_clientdata(client); struct i2c_client *client = data->client;
int ret = 0; int ret = 0;
mutex_lock(&data->update_lock); mutex_lock(&data->update_lock);
...@@ -266,8 +266,7 @@ static struct g762_data *g762_update_client(struct device *dev) ...@@ -266,8 +266,7 @@ static struct g762_data *g762_update_client(struct device *dev)
*/ */
static int do_set_clk_freq(struct device *dev, unsigned long val) static int do_set_clk_freq(struct device *dev, unsigned long val)
{ {
struct i2c_client *client = to_i2c_client(dev); struct g762_data *data = dev_get_drvdata(dev);
struct g762_data *data = i2c_get_clientdata(client);
if (val > 0xffffff) if (val > 0xffffff)
return -EINVAL; return -EINVAL;
...@@ -282,7 +281,6 @@ static int do_set_clk_freq(struct device *dev, unsigned long val) ...@@ -282,7 +281,6 @@ static int do_set_clk_freq(struct device *dev, unsigned long val)
/* Set pwm mode. Accepts either 0 (PWM mode) or 1 (DC mode) */ /* Set pwm mode. Accepts either 0 (PWM mode) or 1 (DC mode) */
static int do_set_pwm_mode(struct device *dev, unsigned long val) static int do_set_pwm_mode(struct device *dev, unsigned long val)
{ {
struct i2c_client *client = to_i2c_client(dev);
struct g762_data *data = g762_update_client(dev); struct g762_data *data = g762_update_client(dev);
int ret; int ret;
...@@ -301,7 +299,7 @@ static int do_set_pwm_mode(struct device *dev, unsigned long val) ...@@ -301,7 +299,7 @@ static int do_set_pwm_mode(struct device *dev, unsigned long val)
ret = -EINVAL; ret = -EINVAL;
goto out; goto out;
} }
ret = i2c_smbus_write_byte_data(client, G762_REG_FAN_CMD1, ret = i2c_smbus_write_byte_data(data->client, G762_REG_FAN_CMD1,
data->fan_cmd1); data->fan_cmd1);
data->valid = false; data->valid = false;
out: out:
...@@ -313,7 +311,6 @@ static int do_set_pwm_mode(struct device *dev, unsigned long val) ...@@ -313,7 +311,6 @@ static int do_set_pwm_mode(struct device *dev, unsigned long val)
/* Set fan clock divisor. Accepts either 1, 2, 4 or 8. */ /* Set fan clock divisor. Accepts either 1, 2, 4 or 8. */
static int do_set_fan_div(struct device *dev, unsigned long val) static int do_set_fan_div(struct device *dev, unsigned long val)
{ {
struct i2c_client *client = to_i2c_client(dev);
struct g762_data *data = g762_update_client(dev); struct g762_data *data = g762_update_client(dev);
int ret; int ret;
...@@ -342,7 +339,7 @@ static int do_set_fan_div(struct device *dev, unsigned long val) ...@@ -342,7 +339,7 @@ static int do_set_fan_div(struct device *dev, unsigned long val)
ret = -EINVAL; ret = -EINVAL;
goto out; goto out;
} }
ret = i2c_smbus_write_byte_data(client, G762_REG_FAN_CMD1, ret = i2c_smbus_write_byte_data(data->client, G762_REG_FAN_CMD1,
data->fan_cmd1); data->fan_cmd1);
data->valid = false; data->valid = false;
out: out:
...@@ -354,7 +351,6 @@ static int do_set_fan_div(struct device *dev, unsigned long val) ...@@ -354,7 +351,6 @@ static int do_set_fan_div(struct device *dev, unsigned long val)
/* Set fan gear mode. Accepts either 0, 1 or 2. */ /* Set fan gear mode. Accepts either 0, 1 or 2. */
static int do_set_fan_gear_mode(struct device *dev, unsigned long val) static int do_set_fan_gear_mode(struct device *dev, unsigned long val)
{ {
struct i2c_client *client = to_i2c_client(dev);
struct g762_data *data = g762_update_client(dev); struct g762_data *data = g762_update_client(dev);
int ret; int ret;
...@@ -379,7 +375,7 @@ static int do_set_fan_gear_mode(struct device *dev, unsigned long val) ...@@ -379,7 +375,7 @@ static int do_set_fan_gear_mode(struct device *dev, unsigned long val)
ret = -EINVAL; ret = -EINVAL;
goto out; goto out;
} }
ret = i2c_smbus_write_byte_data(client, G762_REG_FAN_CMD2, ret = i2c_smbus_write_byte_data(data->client, G762_REG_FAN_CMD2,
data->fan_cmd2); data->fan_cmd2);
data->valid = false; data->valid = false;
out: out:
...@@ -391,7 +387,6 @@ static int do_set_fan_gear_mode(struct device *dev, unsigned long val) ...@@ -391,7 +387,6 @@ static int do_set_fan_gear_mode(struct device *dev, unsigned long val)
/* Set number of fan pulses per revolution. Accepts either 2 or 4. */ /* Set number of fan pulses per revolution. Accepts either 2 or 4. */
static int do_set_fan_pulses(struct device *dev, unsigned long val) static int do_set_fan_pulses(struct device *dev, unsigned long val)
{ {
struct i2c_client *client = to_i2c_client(dev);
struct g762_data *data = g762_update_client(dev); struct g762_data *data = g762_update_client(dev);
int ret; int ret;
...@@ -410,7 +405,7 @@ static int do_set_fan_pulses(struct device *dev, unsigned long val) ...@@ -410,7 +405,7 @@ static int do_set_fan_pulses(struct device *dev, unsigned long val)
ret = -EINVAL; ret = -EINVAL;
goto out; goto out;
} }
ret = i2c_smbus_write_byte_data(client, G762_REG_FAN_CMD1, ret = i2c_smbus_write_byte_data(data->client, G762_REG_FAN_CMD1,
data->fan_cmd1); data->fan_cmd1);
data->valid = false; data->valid = false;
out: out:
...@@ -422,7 +417,6 @@ static int do_set_fan_pulses(struct device *dev, unsigned long val) ...@@ -422,7 +417,6 @@ static int do_set_fan_pulses(struct device *dev, unsigned long val)
/* Set fan mode. Accepts either 1 (open-loop) or 2 (closed-loop). */ /* Set fan mode. Accepts either 1 (open-loop) or 2 (closed-loop). */
static int do_set_pwm_enable(struct device *dev, unsigned long val) static int do_set_pwm_enable(struct device *dev, unsigned long val)
{ {
struct i2c_client *client = to_i2c_client(dev);
struct g762_data *data = g762_update_client(dev); struct g762_data *data = g762_update_client(dev);
int ret; int ret;
...@@ -444,15 +438,15 @@ static int do_set_pwm_enable(struct device *dev, unsigned long val) ...@@ -444,15 +438,15 @@ static int do_set_pwm_enable(struct device *dev, unsigned long val)
* value of 254 if it is 255 when switching to open-loop. * value of 254 if it is 255 when switching to open-loop.
*/ */
if (data->set_cnt == 0xff) if (data->set_cnt == 0xff)
i2c_smbus_write_byte_data(client, G762_REG_SET_CNT, i2c_smbus_write_byte_data(data->client,
254); G762_REG_SET_CNT, 254);
break; break;
default: default:
ret = -EINVAL; ret = -EINVAL;
goto out; goto out;
} }
ret = i2c_smbus_write_byte_data(client, G762_REG_FAN_CMD1, ret = i2c_smbus_write_byte_data(data->client, G762_REG_FAN_CMD1,
data->fan_cmd1); data->fan_cmd1);
data->valid = false; data->valid = false;
out: out:
...@@ -464,7 +458,6 @@ static int do_set_pwm_enable(struct device *dev, unsigned long val) ...@@ -464,7 +458,6 @@ static int do_set_pwm_enable(struct device *dev, unsigned long val)
/* Set PWM polarity. Accepts either 0 (positive duty) or 1 (negative duty) */ /* Set PWM polarity. Accepts either 0 (positive duty) or 1 (negative duty) */
static int do_set_pwm_polarity(struct device *dev, unsigned long val) static int do_set_pwm_polarity(struct device *dev, unsigned long val)
{ {
struct i2c_client *client = to_i2c_client(dev);
struct g762_data *data = g762_update_client(dev); struct g762_data *data = g762_update_client(dev);
int ret; int ret;
...@@ -483,7 +476,7 @@ static int do_set_pwm_polarity(struct device *dev, unsigned long val) ...@@ -483,7 +476,7 @@ static int do_set_pwm_polarity(struct device *dev, unsigned long val)
ret = -EINVAL; ret = -EINVAL;
goto out; goto out;
} }
ret = i2c_smbus_write_byte_data(client, G762_REG_FAN_CMD1, ret = i2c_smbus_write_byte_data(data->client, G762_REG_FAN_CMD1,
data->fan_cmd1); data->fan_cmd1);
data->valid = false; data->valid = false;
out: out:
...@@ -498,8 +491,8 @@ static int do_set_pwm_polarity(struct device *dev, unsigned long val) ...@@ -498,8 +491,8 @@ static int do_set_pwm_polarity(struct device *dev, unsigned long val)
*/ */
static int do_set_pwm(struct device *dev, unsigned long val) static int do_set_pwm(struct device *dev, unsigned long val)
{ {
struct i2c_client *client = to_i2c_client(dev); struct g762_data *data = dev_get_drvdata(dev);
struct g762_data *data = i2c_get_clientdata(client); struct i2c_client *client = data->client;
int ret; int ret;
if (val > 255) if (val > 255)
...@@ -519,7 +512,6 @@ static int do_set_pwm(struct device *dev, unsigned long val) ...@@ -519,7 +512,6 @@ static int do_set_pwm(struct device *dev, unsigned long val)
*/ */
static int do_set_fan_target(struct device *dev, unsigned long val) static int do_set_fan_target(struct device *dev, unsigned long val)
{ {
struct i2c_client *client = to_i2c_client(dev);
struct g762_data *data = g762_update_client(dev); struct g762_data *data = g762_update_client(dev);
int ret; int ret;
...@@ -531,7 +523,7 @@ static int do_set_fan_target(struct device *dev, unsigned long val) ...@@ -531,7 +523,7 @@ static int do_set_fan_target(struct device *dev, unsigned long val)
G762_PULSE_FROM_REG(data->fan_cmd1), G762_PULSE_FROM_REG(data->fan_cmd1),
G762_CLKDIV_FROM_REG(data->fan_cmd1), G762_CLKDIV_FROM_REG(data->fan_cmd1),
G762_GEARMULT_FROM_REG(data->fan_cmd2)); G762_GEARMULT_FROM_REG(data->fan_cmd2));
ret = i2c_smbus_write_byte_data(client, G762_REG_SET_CNT, ret = i2c_smbus_write_byte_data(data->client, G762_REG_SET_CNT,
data->set_cnt); data->set_cnt);
data->valid = false; data->valid = false;
mutex_unlock(&data->update_lock); mutex_unlock(&data->update_lock);
...@@ -542,7 +534,6 @@ static int do_set_fan_target(struct device *dev, unsigned long val) ...@@ -542,7 +534,6 @@ static int do_set_fan_target(struct device *dev, unsigned long val)
/* Set fan startup voltage. Accepted values are either 0, 1, 2 or 3. */ /* Set fan startup voltage. Accepted values are either 0, 1, 2 or 3. */
static int do_set_fan_startv(struct device *dev, unsigned long val) static int do_set_fan_startv(struct device *dev, unsigned long val)
{ {
struct i2c_client *client = to_i2c_client(dev);
struct g762_data *data = g762_update_client(dev); struct g762_data *data = g762_update_client(dev);
int ret; int ret;
...@@ -571,7 +562,7 @@ static int do_set_fan_startv(struct device *dev, unsigned long val) ...@@ -571,7 +562,7 @@ static int do_set_fan_startv(struct device *dev, unsigned long val)
ret = -EINVAL; ret = -EINVAL;
goto out; goto out;
} }
ret = i2c_smbus_write_byte_data(client, G762_REG_FAN_CMD2, ret = i2c_smbus_write_byte_data(data->client, G762_REG_FAN_CMD2,
data->fan_cmd2); data->fan_cmd2);
data->valid = false; data->valid = false;
out: out:
...@@ -1026,7 +1017,7 @@ static DEVICE_ATTR(fan1_pulses, S_IWUSR | S_IRUGO, ...@@ -1026,7 +1017,7 @@ static DEVICE_ATTR(fan1_pulses, S_IWUSR | S_IRUGO,
get_fan_pulses, set_fan_pulses); get_fan_pulses, set_fan_pulses);
/* Driver data */ /* Driver data */
static struct attribute *g762_attributes[] = { static struct attribute *g762_attrs[] = {
&dev_attr_fan1_input.attr, &dev_attr_fan1_input.attr,
&dev_attr_fan1_alarm.attr, &dev_attr_fan1_alarm.attr,
&dev_attr_fan1_fault.attr, &dev_attr_fan1_fault.attr,
...@@ -1039,9 +1030,7 @@ static struct attribute *g762_attributes[] = { ...@@ -1039,9 +1030,7 @@ static struct attribute *g762_attributes[] = {
NULL NULL
}; };
static const struct attribute_group g762_group = { ATTRIBUTE_GROUPS(g762);
.attrs = g762_attributes,
};
/* /*
* Enable both fan failure detection and fan out of control protection. The * Enable both fan failure detection and fan out of control protection. The
...@@ -1050,7 +1039,6 @@ static const struct attribute_group g762_group = { ...@@ -1050,7 +1039,6 @@ static const struct attribute_group g762_group = {
*/ */
static inline int g762_fan_init(struct device *dev) static inline int g762_fan_init(struct device *dev)
{ {
struct i2c_client *client = to_i2c_client(dev);
struct g762_data *data = g762_update_client(dev); struct g762_data *data = g762_update_client(dev);
if (IS_ERR(data)) if (IS_ERR(data))
...@@ -1060,12 +1048,13 @@ static inline int g762_fan_init(struct device *dev) ...@@ -1060,12 +1048,13 @@ static inline int g762_fan_init(struct device *dev)
data->fan_cmd1 |= G762_REG_FAN_CMD1_DET_FAN_OOC; data->fan_cmd1 |= G762_REG_FAN_CMD1_DET_FAN_OOC;
data->valid = false; data->valid = false;
return i2c_smbus_write_byte_data(client, G762_REG_FAN_CMD1, return i2c_smbus_write_byte_data(data->client, G762_REG_FAN_CMD1,
data->fan_cmd1); data->fan_cmd1);
} }
static int g762_probe(struct i2c_client *client, const struct i2c_device_id *id) static int g762_probe(struct i2c_client *client, const struct i2c_device_id *id)
{ {
struct device *dev = &client->dev;
struct g762_data *data; struct g762_data *data;
int ret; int ret;
...@@ -1073,7 +1062,7 @@ static int g762_probe(struct i2c_client *client, const struct i2c_device_id *id) ...@@ -1073,7 +1062,7 @@ static int g762_probe(struct i2c_client *client, const struct i2c_device_id *id)
I2C_FUNC_SMBUS_BYTE_DATA)) I2C_FUNC_SMBUS_BYTE_DATA))
return -ENODEV; return -ENODEV;
data = devm_kzalloc(&client->dev, sizeof(struct g762_data), GFP_KERNEL); data = devm_kzalloc(dev, sizeof(struct g762_data), GFP_KERNEL);
if (!data) if (!data)
return -ENOMEM; return -ENOMEM;
...@@ -1082,7 +1071,7 @@ static int g762_probe(struct i2c_client *client, const struct i2c_device_id *id) ...@@ -1082,7 +1071,7 @@ static int g762_probe(struct i2c_client *client, const struct i2c_device_id *id)
mutex_init(&data->update_lock); mutex_init(&data->update_lock);
/* Enable fan failure detection and fan out of control protection */ /* Enable fan failure detection and fan out of control protection */
ret = g762_fan_init(&client->dev); ret = g762_fan_init(dev);
if (ret) if (ret)
return ret; return ret;
...@@ -1098,22 +1087,17 @@ static int g762_probe(struct i2c_client *client, const struct i2c_device_id *id) ...@@ -1098,22 +1087,17 @@ static int g762_probe(struct i2c_client *client, const struct i2c_device_id *id)
if (ret) if (ret)
goto clock_dis; goto clock_dis;
/* Register sysfs hooks */ data->hwmon_dev = devm_hwmon_device_register_with_groups(dev,
ret = sysfs_create_group(&client->dev.kobj, &g762_group); client->name,
if (ret) data,
goto clock_dis; g762_groups);
data->hwmon_dev = hwmon_device_register(&client->dev);
if (IS_ERR(data->hwmon_dev)) { if (IS_ERR(data->hwmon_dev)) {
ret = PTR_ERR(data->hwmon_dev); ret = PTR_ERR(data->hwmon_dev);
goto sysfs_rem; goto clock_dis;
} }
return 0; return 0;
sysfs_rem:
sysfs_remove_group(&client->dev.kobj, &g762_group);
clock_dis: clock_dis:
g762_of_clock_disable(client); g762_of_clock_disable(client);
...@@ -1125,7 +1109,6 @@ static int g762_remove(struct i2c_client *client) ...@@ -1125,7 +1109,6 @@ static int g762_remove(struct i2c_client *client)
struct g762_data *data = i2c_get_clientdata(client); struct g762_data *data = i2c_get_clientdata(client);
hwmon_device_unregister(data->hwmon_dev); hwmon_device_unregister(data->hwmon_dev);
sysfs_remove_group(&client->dev.kobj, &g762_group);
g762_of_clock_disable(client); g762_of_clock_disable(client);
return 0; return 0;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment