hwmon: (jc42) Convert to use devm_kzalloc
Marginally less code and eliminate the possibility of memory leaks. Also replace new_client variable with client and introduce dev variable to make the code a bit easier to read. Signed-off-by: Guenter Roeck <linux@roeck-us.net>
This commit is contained in:
parent
918ddef35f
commit
f15df57ded
@ -469,20 +469,19 @@ static const struct attribute_group jc42_group = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
/* Return 0 if detection is successful, -ENODEV otherwise */
|
/* Return 0 if detection is successful, -ENODEV otherwise */
|
||||||
static int jc42_detect(struct i2c_client *new_client,
|
static int jc42_detect(struct i2c_client *client, struct i2c_board_info *info)
|
||||||
struct i2c_board_info *info)
|
|
||||||
{
|
{
|
||||||
struct i2c_adapter *adapter = new_client->adapter;
|
struct i2c_adapter *adapter = client->adapter;
|
||||||
int i, config, cap, manid, devid;
|
int i, config, cap, manid, devid;
|
||||||
|
|
||||||
if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
|
if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
|
||||||
I2C_FUNC_SMBUS_WORD_DATA))
|
I2C_FUNC_SMBUS_WORD_DATA))
|
||||||
return -ENODEV;
|
return -ENODEV;
|
||||||
|
|
||||||
cap = i2c_smbus_read_word_swapped(new_client, JC42_REG_CAP);
|
cap = i2c_smbus_read_word_swapped(client, JC42_REG_CAP);
|
||||||
config = i2c_smbus_read_word_swapped(new_client, JC42_REG_CONFIG);
|
config = i2c_smbus_read_word_swapped(client, JC42_REG_CONFIG);
|
||||||
manid = i2c_smbus_read_word_swapped(new_client, JC42_REG_MANID);
|
manid = i2c_smbus_read_word_swapped(client, JC42_REG_MANID);
|
||||||
devid = i2c_smbus_read_word_swapped(new_client, JC42_REG_DEVICEID);
|
devid = i2c_smbus_read_word_swapped(client, JC42_REG_DEVICEID);
|
||||||
|
|
||||||
if (cap < 0 || config < 0 || manid < 0 || devid < 0)
|
if (cap < 0 || config < 0 || manid < 0 || devid < 0)
|
||||||
return -ENODEV;
|
return -ENODEV;
|
||||||
@ -501,47 +500,42 @@ static int jc42_detect(struct i2c_client *new_client,
|
|||||||
return -ENODEV;
|
return -ENODEV;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int jc42_probe(struct i2c_client *new_client,
|
static int jc42_probe(struct i2c_client *client, const struct i2c_device_id *id)
|
||||||
const struct i2c_device_id *id)
|
|
||||||
{
|
{
|
||||||
struct jc42_data *data;
|
struct jc42_data *data;
|
||||||
int config, cap, err;
|
int config, cap, err;
|
||||||
|
struct device *dev = &client->dev;
|
||||||
|
|
||||||
data = kzalloc(sizeof(struct jc42_data), GFP_KERNEL);
|
data = devm_kzalloc(dev, sizeof(struct jc42_data), GFP_KERNEL);
|
||||||
if (!data) {
|
if (!data)
|
||||||
err = -ENOMEM;
|
return -ENOMEM;
|
||||||
goto exit;
|
|
||||||
}
|
|
||||||
|
|
||||||
i2c_set_clientdata(new_client, data);
|
i2c_set_clientdata(client, data);
|
||||||
mutex_init(&data->update_lock);
|
mutex_init(&data->update_lock);
|
||||||
|
|
||||||
cap = i2c_smbus_read_word_swapped(new_client, JC42_REG_CAP);
|
cap = i2c_smbus_read_word_swapped(client, JC42_REG_CAP);
|
||||||
if (cap < 0) {
|
if (cap < 0)
|
||||||
err = -EINVAL;
|
return cap;
|
||||||
goto exit_free;
|
|
||||||
}
|
|
||||||
data->extended = !!(cap & JC42_CAP_RANGE);
|
data->extended = !!(cap & JC42_CAP_RANGE);
|
||||||
|
|
||||||
config = i2c_smbus_read_word_swapped(new_client, JC42_REG_CONFIG);
|
config = i2c_smbus_read_word_swapped(client, JC42_REG_CONFIG);
|
||||||
if (config < 0) {
|
if (config < 0)
|
||||||
err = -EINVAL;
|
return config;
|
||||||
goto exit_free;
|
|
||||||
}
|
|
||||||
data->orig_config = config;
|
data->orig_config = config;
|
||||||
if (config & JC42_CFG_SHUTDOWN) {
|
if (config & JC42_CFG_SHUTDOWN) {
|
||||||
config &= ~JC42_CFG_SHUTDOWN;
|
config &= ~JC42_CFG_SHUTDOWN;
|
||||||
i2c_smbus_write_word_swapped(new_client, JC42_REG_CONFIG,
|
i2c_smbus_write_word_swapped(client, JC42_REG_CONFIG, config);
|
||||||
config);
|
|
||||||
}
|
}
|
||||||
data->config = config;
|
data->config = config;
|
||||||
|
|
||||||
/* Register sysfs hooks */
|
/* Register sysfs hooks */
|
||||||
err = sysfs_create_group(&new_client->dev.kobj, &jc42_group);
|
err = sysfs_create_group(&dev->kobj, &jc42_group);
|
||||||
if (err)
|
if (err)
|
||||||
goto exit_free;
|
return err;
|
||||||
|
|
||||||
data->hwmon_dev = hwmon_device_register(&new_client->dev);
|
data->hwmon_dev = hwmon_device_register(dev);
|
||||||
if (IS_ERR(data->hwmon_dev)) {
|
if (IS_ERR(data->hwmon_dev)) {
|
||||||
err = PTR_ERR(data->hwmon_dev);
|
err = PTR_ERR(data->hwmon_dev);
|
||||||
goto exit_remove;
|
goto exit_remove;
|
||||||
@ -550,10 +544,7 @@ static int jc42_probe(struct i2c_client *new_client,
|
|||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
exit_remove:
|
exit_remove:
|
||||||
sysfs_remove_group(&new_client->dev.kobj, &jc42_group);
|
sysfs_remove_group(&dev->kobj, &jc42_group);
|
||||||
exit_free:
|
|
||||||
kfree(data);
|
|
||||||
exit:
|
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -565,7 +556,6 @@ static int jc42_remove(struct i2c_client *client)
|
|||||||
if (data->config != data->orig_config)
|
if (data->config != data->orig_config)
|
||||||
i2c_smbus_write_word_swapped(client, JC42_REG_CONFIG,
|
i2c_smbus_write_word_swapped(client, JC42_REG_CONFIG,
|
||||||
data->orig_config);
|
data->orig_config);
|
||||||
kfree(data);
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user