Commit 008f1ca5 authored by Jean Delvare's avatar Jean Delvare Committed by Jean Delvare

hwmon: (adt7470) Convert to a new-style i2c driver

The new-style adt7470 driver implements the optional detect() callback
to cover the use cases of the legacy driver.
Signed-off-by: default avatarJean Delvare <khali@linux-fr.org>
Cc: Darrick J. Wong <djwong@us.ibm.com>
parent 7347cb38
...@@ -138,7 +138,6 @@ I2C_CLIENT_INSMOD_1(adt7470); ...@@ -138,7 +138,6 @@ I2C_CLIENT_INSMOD_1(adt7470);
#define FAN_DATA_VALID(x) ((x) && (x) != FAN_PERIOD_INVALID) #define FAN_DATA_VALID(x) ((x) && (x) != FAN_PERIOD_INVALID)
struct adt7470_data { struct adt7470_data {
struct i2c_client client;
struct device *hwmon_dev; struct device *hwmon_dev;
struct attribute_group attrs; struct attribute_group attrs;
struct mutex lock; struct mutex lock;
...@@ -164,16 +163,28 @@ struct adt7470_data { ...@@ -164,16 +163,28 @@ struct adt7470_data {
u8 pwm_auto_temp[ADT7470_PWM_COUNT]; u8 pwm_auto_temp[ADT7470_PWM_COUNT];
}; };
static int adt7470_attach_adapter(struct i2c_adapter *adapter); static int adt7470_probe(struct i2c_client *client,
static int adt7470_detect(struct i2c_adapter *adapter, int address, int kind); const struct i2c_device_id *id);
static int adt7470_detach_client(struct i2c_client *client); static int adt7470_detect(struct i2c_client *client, int kind,
struct i2c_board_info *info);
static int adt7470_remove(struct i2c_client *client);
static const struct i2c_device_id adt7470_id[] = {
{ "adt7470", adt7470 },
{ }
};
MODULE_DEVICE_TABLE(i2c, adt7470_id);
static struct i2c_driver adt7470_driver = { static struct i2c_driver adt7470_driver = {
.class = I2C_CLASS_HWMON,
.driver = { .driver = {
.name = "adt7470", .name = "adt7470",
}, },
.attach_adapter = adt7470_attach_adapter, .probe = adt7470_probe,
.detach_client = adt7470_detach_client, .remove = adt7470_remove,
.id_table = adt7470_id,
.detect = adt7470_detect,
.address_data = &addr_data,
}; };
/* /*
...@@ -1004,64 +1015,52 @@ static struct attribute *adt7470_attr[] = ...@@ -1004,64 +1015,52 @@ static struct attribute *adt7470_attr[] =
NULL NULL
}; };
static int adt7470_attach_adapter(struct i2c_adapter *adapter) /* Return 0 if detection is successful, -ENODEV otherwise */
{ static int adt7470_detect(struct i2c_client *client, int kind,
if (!(adapter->class & I2C_CLASS_HWMON)) struct i2c_board_info *info)
return 0;
return i2c_probe(adapter, &addr_data, adt7470_detect);
}
static int adt7470_detect(struct i2c_adapter *adapter, int address, int kind)
{ {
struct i2c_client *client; struct i2c_adapter *adapter = client->adapter;
struct adt7470_data *data;
int err = 0;
if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
goto exit; return -ENODEV;
if (!(data = kzalloc(sizeof(struct adt7470_data), GFP_KERNEL))) {
err = -ENOMEM;
goto exit;
}
client = &data->client;
client->addr = address;
client->adapter = adapter;
client->driver = &adt7470_driver;
i2c_set_clientdata(client, data);
mutex_init(&data->lock);
if (kind <= 0) { if (kind <= 0) {
int vendor, device, revision; int vendor, device, revision;
vendor = i2c_smbus_read_byte_data(client, ADT7470_REG_VENDOR); vendor = i2c_smbus_read_byte_data(client, ADT7470_REG_VENDOR);
if (vendor != ADT7470_VENDOR) { if (vendor != ADT7470_VENDOR)
err = -ENODEV; return -ENODEV;
goto exit_free;
}
device = i2c_smbus_read_byte_data(client, ADT7470_REG_DEVICE); device = i2c_smbus_read_byte_data(client, ADT7470_REG_DEVICE);
if (device != ADT7470_DEVICE) { if (device != ADT7470_DEVICE)
err = -ENODEV; return -ENODEV;
goto exit_free;
}
revision = i2c_smbus_read_byte_data(client, revision = i2c_smbus_read_byte_data(client,
ADT7470_REG_REVISION); ADT7470_REG_REVISION);
if (revision != ADT7470_REVISION) { if (revision != ADT7470_REVISION)
err = -ENODEV; return -ENODEV;
goto exit_free;
}
} else } else
dev_dbg(&adapter->dev, "detection forced\n"); dev_dbg(&adapter->dev, "detection forced\n");
strlcpy(client->name, "adt7470", I2C_NAME_SIZE); strlcpy(info->type, "adt7470", I2C_NAME_SIZE);
if ((err = i2c_attach_client(client))) return 0;
goto exit_free; }
static int adt7470_probe(struct i2c_client *client,
const struct i2c_device_id *id)
{
struct adt7470_data *data;
int err;
data = kzalloc(sizeof(struct adt7470_data), GFP_KERNEL);
if (!data) {
err = -ENOMEM;
goto exit;
}
i2c_set_clientdata(client, data);
mutex_init(&data->lock);
dev_info(&client->dev, "%s chip found\n", client->name); dev_info(&client->dev, "%s chip found\n", client->name);
...@@ -1071,7 +1070,7 @@ static int adt7470_detect(struct i2c_adapter *adapter, int address, int kind) ...@@ -1071,7 +1070,7 @@ static int adt7470_detect(struct i2c_adapter *adapter, int address, int kind)
/* Register sysfs hooks */ /* Register sysfs hooks */
data->attrs.attrs = adt7470_attr; data->attrs.attrs = adt7470_attr;
if ((err = sysfs_create_group(&client->dev.kobj, &data->attrs))) if ((err = sysfs_create_group(&client->dev.kobj, &data->attrs)))
goto exit_detach; goto exit_free;
data->hwmon_dev = hwmon_device_register(&client->dev); data->hwmon_dev = hwmon_device_register(&client->dev);
if (IS_ERR(data->hwmon_dev)) { if (IS_ERR(data->hwmon_dev)) {
...@@ -1083,21 +1082,18 @@ static int adt7470_detect(struct i2c_adapter *adapter, int address, int kind) ...@@ -1083,21 +1082,18 @@ static int adt7470_detect(struct i2c_adapter *adapter, int address, int kind)
exit_remove: exit_remove:
sysfs_remove_group(&client->dev.kobj, &data->attrs); sysfs_remove_group(&client->dev.kobj, &data->attrs);
exit_detach:
i2c_detach_client(client);
exit_free: exit_free:
kfree(data); kfree(data);
exit: exit:
return err; return err;
} }
static int adt7470_detach_client(struct i2c_client *client) static int adt7470_remove(struct i2c_client *client)
{ {
struct adt7470_data *data = i2c_get_clientdata(client); struct adt7470_data *data = i2c_get_clientdata(client);
hwmon_device_unregister(data->hwmon_dev); hwmon_device_unregister(data->hwmon_dev);
sysfs_remove_group(&client->dev.kobj, &data->attrs); sysfs_remove_group(&client->dev.kobj, &data->attrs);
i2c_detach_client(client);
kfree(data); kfree(data);
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