hwmon: (lm85) Convert to devm_hwmon_device_register_with_groups
Use devm_hwmon_device_register_with_groups() to simplify the code a bit.
Signed-off-by: Axel Lin <axel.lin@ingics.com>
Signed-off-by: Guenter Roeck <linux@roeck-us.net>
diff --git a/drivers/hwmon/lm85.c b/drivers/hwmon/lm85.c
index 9fdf1c0..2b4b419 100644
--- a/drivers/hwmon/lm85.c
+++ b/drivers/hwmon/lm85.c
@@ -302,7 +302,8 @@
* The structure is dynamically allocated.
*/
struct lm85_data {
- struct device *hwmon_dev;
+ struct i2c_client *client;
+ const struct attribute_group *groups[6];
const int *freq_map;
enum chips type;
@@ -382,8 +383,8 @@
static struct lm85_data *lm85_update_device(struct device *dev)
{
- struct i2c_client *client = to_i2c_client(dev);
- struct lm85_data *data = i2c_get_clientdata(client);
+ struct lm85_data *data = dev_get_drvdata(dev);
+ struct i2c_client *client = data->client;
int i;
mutex_lock(&data->update_lock);
@@ -584,8 +585,8 @@
const char *buf, size_t count)
{
int nr = to_sensor_dev_attr(attr)->index;
- struct i2c_client *client = to_i2c_client(dev);
- struct lm85_data *data = i2c_get_clientdata(client);
+ struct lm85_data *data = dev_get_drvdata(dev);
+ struct i2c_client *client = data->client;
unsigned long val;
int err;
@@ -708,8 +709,8 @@
const char *buf, size_t count)
{
int nr = to_sensor_dev_attr(attr)->index;
- struct i2c_client *client = to_i2c_client(dev);
- struct lm85_data *data = i2c_get_clientdata(client);
+ struct lm85_data *data = dev_get_drvdata(dev);
+ struct i2c_client *client = data->client;
unsigned long val;
int err;
@@ -750,8 +751,8 @@
*attr, const char *buf, size_t count)
{
int nr = to_sensor_dev_attr(attr)->index;
- struct i2c_client *client = to_i2c_client(dev);
- struct lm85_data *data = i2c_get_clientdata(client);
+ struct lm85_data *data = dev_get_drvdata(dev);
+ struct i2c_client *client = data->client;
u8 config;
unsigned long val;
int err;
@@ -808,8 +809,8 @@
struct device_attribute *attr, const char *buf, size_t count)
{
int nr = to_sensor_dev_attr(attr)->index;
- struct i2c_client *client = to_i2c_client(dev);
- struct lm85_data *data = i2c_get_clientdata(client);
+ struct lm85_data *data = dev_get_drvdata(dev);
+ struct i2c_client *client = data->client;
unsigned long val;
int err;
@@ -875,8 +876,8 @@
const char *buf, size_t count)
{
int nr = to_sensor_dev_attr(attr)->index;
- struct i2c_client *client = to_i2c_client(dev);
- struct lm85_data *data = i2c_get_clientdata(client);
+ struct lm85_data *data = dev_get_drvdata(dev);
+ struct i2c_client *client = data->client;
long val;
int err;
@@ -903,8 +904,8 @@
const char *buf, size_t count)
{
int nr = to_sensor_dev_attr(attr)->index;
- struct i2c_client *client = to_i2c_client(dev);
- struct lm85_data *data = i2c_get_clientdata(client);
+ struct lm85_data *data = dev_get_drvdata(dev);
+ struct i2c_client *client = data->client;
long val;
int err;
@@ -959,8 +960,8 @@
const char *buf, size_t count)
{
int nr = to_sensor_dev_attr(attr)->index;
- struct i2c_client *client = to_i2c_client(dev);
- struct lm85_data *data = i2c_get_clientdata(client);
+ struct lm85_data *data = dev_get_drvdata(dev);
+ struct i2c_client *client = data->client;
long val;
int err;
@@ -990,8 +991,8 @@
const char *buf, size_t count)
{
int nr = to_sensor_dev_attr(attr)->index;
- struct i2c_client *client = to_i2c_client(dev);
- struct lm85_data *data = i2c_get_clientdata(client);
+ struct lm85_data *data = dev_get_drvdata(dev);
+ struct i2c_client *client = data->client;
long val;
int err;
@@ -1036,8 +1037,8 @@
struct device_attribute *attr, const char *buf, size_t count)
{
int nr = to_sensor_dev_attr(attr)->index;
- struct i2c_client *client = to_i2c_client(dev);
- struct lm85_data *data = i2c_get_clientdata(client);
+ struct lm85_data *data = dev_get_drvdata(dev);
+ struct i2c_client *client = data->client;
long val;
int err;
@@ -1066,8 +1067,8 @@
struct device_attribute *attr, const char *buf, size_t count)
{
int nr = to_sensor_dev_attr(attr)->index;
- struct i2c_client *client = to_i2c_client(dev);
- struct lm85_data *data = i2c_get_clientdata(client);
+ struct lm85_data *data = dev_get_drvdata(dev);
+ struct i2c_client *client = data->client;
unsigned long val;
int err;
@@ -1095,8 +1096,8 @@
struct device_attribute *attr, const char *buf, size_t count)
{
int nr = to_sensor_dev_attr(attr)->index;
- struct i2c_client *client = to_i2c_client(dev);
- struct lm85_data *data = i2c_get_clientdata(client);
+ struct lm85_data *data = dev_get_drvdata(dev);
+ struct i2c_client *client = data->client;
u8 tmp;
long val;
int err;
@@ -1146,8 +1147,8 @@
struct device_attribute *attr, const char *buf, size_t count)
{
int nr = to_sensor_dev_attr(attr)->index;
- struct i2c_client *client = to_i2c_client(dev);
- struct lm85_data *data = i2c_get_clientdata(client);
+ struct lm85_data *data = dev_get_drvdata(dev);
+ struct i2c_client *client = data->client;
int min;
long val;
int err;
@@ -1183,8 +1184,8 @@
struct device_attribute *attr, const char *buf, size_t count)
{
int nr = to_sensor_dev_attr(attr)->index;
- struct i2c_client *client = to_i2c_client(dev);
- struct lm85_data *data = i2c_get_clientdata(client);
+ struct lm85_data *data = dev_get_drvdata(dev);
+ struct i2c_client *client = data->client;
long val;
int err;
@@ -1222,8 +1223,8 @@
struct device_attribute *attr, const char *buf, size_t count)
{
int nr = to_sensor_dev_attr(attr)->index;
- struct i2c_client *client = to_i2c_client(dev);
- struct lm85_data *data = i2c_get_clientdata(client);
+ struct lm85_data *data = dev_get_drvdata(dev);
+ struct i2c_client *client = data->client;
int min;
long val;
int err;
@@ -1256,8 +1257,8 @@
struct device_attribute *attr, const char *buf, size_t count)
{
int nr = to_sensor_dev_attr(attr)->index;
- struct i2c_client *client = to_i2c_client(dev);
- struct lm85_data *data = i2c_get_clientdata(client);
+ struct lm85_data *data = dev_get_drvdata(dev);
+ struct i2c_client *client = data->client;
long val;
int err;
@@ -1548,30 +1549,18 @@
return 0;
}
-static void lm85_remove_files(struct i2c_client *client, struct lm85_data *data)
+static int lm85_probe(struct i2c_client *client, const struct i2c_device_id *id)
{
- sysfs_remove_group(&client->dev.kobj, &lm85_group);
- if (data->type != emc6d103s) {
- sysfs_remove_group(&client->dev.kobj, &lm85_group_minctl);
- sysfs_remove_group(&client->dev.kobj, &lm85_group_temp_off);
- }
- if (!data->has_vid5)
- sysfs_remove_group(&client->dev.kobj, &lm85_group_in4);
- if (data->type == emc6d100)
- sysfs_remove_group(&client->dev.kobj, &lm85_group_in567);
-}
-
-static int lm85_probe(struct i2c_client *client,
- const struct i2c_device_id *id)
-{
+ struct device *dev = &client->dev;
+ struct device *hwmon_dev;
struct lm85_data *data;
- int err;
+ int idx = 0;
- data = devm_kzalloc(&client->dev, sizeof(struct lm85_data), GFP_KERNEL);
+ data = devm_kzalloc(dev, sizeof(struct lm85_data), GFP_KERNEL);
if (!data)
return -ENOMEM;
- i2c_set_clientdata(client, data);
+ data->client = client;
data->type = id->driver_data;
mutex_init(&data->update_lock);
@@ -1596,20 +1585,13 @@
/* Initialize the LM85 chip */
lm85_init_client(client);
- /* Register sysfs hooks */
- err = sysfs_create_group(&client->dev.kobj, &lm85_group);
- if (err)
- return err;
+ /* sysfs hooks */
+ data->groups[idx++] = &lm85_group;
/* minctl and temp_off exist on all chips except emc6d103s */
if (data->type != emc6d103s) {
- err = sysfs_create_group(&client->dev.kobj, &lm85_group_minctl);
- if (err)
- goto err_remove_files;
- err = sysfs_create_group(&client->dev.kobj,
- &lm85_group_temp_off);
- if (err)
- goto err_remove_files;
+ data->groups[idx++] = &lm85_group_minctl;
+ data->groups[idx++] = &lm85_group_temp_off;
}
/*
@@ -1622,39 +1604,16 @@
data->has_vid5 = true;
}
- if (!data->has_vid5) {
- err = sysfs_create_group(&client->dev.kobj, &lm85_group_in4);
- if (err)
- goto err_remove_files;
- }
+ if (!data->has_vid5)
+ data->groups[idx++] = &lm85_group_in4;
/* The EMC6D100 has 3 additional voltage inputs */
- if (data->type == emc6d100) {
- err = sysfs_create_group(&client->dev.kobj, &lm85_group_in567);
- if (err)
- goto err_remove_files;
- }
+ if (data->type == emc6d100)
+ data->groups[idx++] = &lm85_group_in567;
- data->hwmon_dev = hwmon_device_register(&client->dev);
- if (IS_ERR(data->hwmon_dev)) {
- err = PTR_ERR(data->hwmon_dev);
- goto err_remove_files;
- }
-
- return 0;
-
- /* Error out and cleanup code */
- err_remove_files:
- lm85_remove_files(client, data);
- return err;
-}
-
-static int lm85_remove(struct i2c_client *client)
-{
- struct lm85_data *data = i2c_get_clientdata(client);
- hwmon_device_unregister(data->hwmon_dev);
- lm85_remove_files(client, data);
- return 0;
+ 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 lm85_id[] = {
@@ -1679,7 +1638,6 @@
.name = "lm85",
},
.probe = lm85_probe,
- .remove = lm85_remove,
.id_table = lm85_id,
.detect = lm85_detect,
.address_list = normal_i2c,