Commit 7347cb38 authored by Jean Delvare's avatar Jean Delvare Committed by Jean Delvare

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

The new-style ads7828 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>
parent 7fae8283
...@@ -64,7 +64,6 @@ static unsigned int ads7828_lsb_resol; /* resolution of the ADC sample lsb */ ...@@ -64,7 +64,6 @@ static unsigned int ads7828_lsb_resol; /* resolution of the ADC sample lsb */
/* Each client has this additional data */ /* Each client has this additional data */
struct ads7828_data { struct ads7828_data {
struct i2c_client client;
struct device *hwmon_dev; struct device *hwmon_dev;
struct mutex update_lock; /* mutex protect updates */ struct mutex update_lock; /* mutex protect updates */
char valid; /* !=0 if following fields are valid */ char valid; /* !=0 if following fields are valid */
...@@ -73,7 +72,10 @@ struct ads7828_data { ...@@ -73,7 +72,10 @@ struct ads7828_data {
}; };
/* Function declaration - necessary due to function dependencies */ /* Function declaration - necessary due to function dependencies */
static int ads7828_detect(struct i2c_adapter *adapter, int address, int kind); static int ads7828_detect(struct i2c_client *client, int kind,
struct i2c_board_info *info);
static int ads7828_probe(struct i2c_client *client,
const struct i2c_device_id *id);
/* The ADS7828 returns the 12-bit sample in two bytes, /* The ADS7828 returns the 12-bit sample in two bytes,
these are read as a word then byte-swapped */ these are read as a word then byte-swapped */
...@@ -156,58 +158,43 @@ static const struct attribute_group ads7828_group = { ...@@ -156,58 +158,43 @@ static const struct attribute_group ads7828_group = {
.attrs = ads7828_attributes, .attrs = ads7828_attributes,
}; };
static int ads7828_attach_adapter(struct i2c_adapter *adapter) static int ads7828_remove(struct i2c_client *client)
{
if (!(adapter->class & I2C_CLASS_HWMON))
return 0;
return i2c_probe(adapter, &addr_data, ads7828_detect);
}
static int ads7828_detach_client(struct i2c_client *client)
{ {
struct ads7828_data *data = i2c_get_clientdata(client); struct ads7828_data *data = i2c_get_clientdata(client);
hwmon_device_unregister(data->hwmon_dev); hwmon_device_unregister(data->hwmon_dev);
sysfs_remove_group(&client->dev.kobj, &ads7828_group); sysfs_remove_group(&client->dev.kobj, &ads7828_group);
i2c_detach_client(client);
kfree(i2c_get_clientdata(client)); kfree(i2c_get_clientdata(client));
return 0; return 0;
} }
static const struct i2c_device_id ads7828_id[] = {
{ "ads7828", ads7828 },
{ }
};
MODULE_DEVICE_TABLE(i2c, ads7828_id);
/* This is the driver that will be inserted */ /* This is the driver that will be inserted */
static struct i2c_driver ads7828_driver = { static struct i2c_driver ads7828_driver = {
.class = I2C_CLASS_HWMON,
.driver = { .driver = {
.name = "ads7828", .name = "ads7828",
}, },
.attach_adapter = ads7828_attach_adapter, .probe = ads7828_probe,
.detach_client = ads7828_detach_client, .remove = ads7828_remove,
.id_table = ads7828_id,
.detect = ads7828_detect,
.address_data = &addr_data,
}; };
/* This function is called by i2c_probe */ /* Return 0 if detection is successful, -ENODEV otherwise */
static int ads7828_detect(struct i2c_adapter *adapter, int address, int kind) static int ads7828_detect(struct i2c_client *client, int kind,
struct i2c_board_info *info)
{ {
struct i2c_client *client; struct i2c_adapter *adapter = client->adapter;
struct ads7828_data *data;
int err = 0;
const char *name = "";
/* Check we have a valid client */ /* Check we have a valid client */
if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_READ_WORD_DATA)) if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_READ_WORD_DATA))
goto exit; return -ENODEV;
/* OK. For now, we presume we have a valid client. We now create the
client structure, even though we cannot fill it completely yet.
But it allows us to access ads7828_read_value. */
data = kzalloc(sizeof(struct ads7828_data), GFP_KERNEL);
if (!data) {
err = -ENOMEM;
goto exit;
}
client = &data->client;
i2c_set_clientdata(client, data);
client->addr = address;
client->adapter = adapter;
client->driver = &ads7828_driver;
/* Now, we do the remaining detection. There is no identification /* Now, we do the remaining detection. There is no identification
dedicated register so attempt to sanity check using knowledge of dedicated register so attempt to sanity check using knowledge of
...@@ -225,32 +212,34 @@ static int ads7828_detect(struct i2c_adapter *adapter, int address, int kind) ...@@ -225,32 +212,34 @@ static int ads7828_detect(struct i2c_adapter *adapter, int address, int kind)
printk(KERN_DEBUG printk(KERN_DEBUG
"%s : Doesn't look like an ads7828 device\n", "%s : Doesn't look like an ads7828 device\n",
__func__); __func__);
goto exit_free; return -ENODEV;
} }
} }
} }
strlcpy(info->type, "ads7828", I2C_NAME_SIZE);
/* Determine the chip type - only one kind supported! */ return 0;
if (kind <= 0) }
kind = ads7828;
if (kind == ads7828) static int ads7828_probe(struct i2c_client *client,
name = "ads7828"; const struct i2c_device_id *id)
{
struct ads7828_data *data;
int err;
/* Fill in the remaining client fields, put it into the global list */ data = kzalloc(sizeof(struct ads7828_data), GFP_KERNEL);
strlcpy(client->name, name, I2C_NAME_SIZE); if (!data) {
err = -ENOMEM;
goto exit;
}
i2c_set_clientdata(client, data);
mutex_init(&data->update_lock); mutex_init(&data->update_lock);
/* Tell the I2C layer a new client has arrived */
err = i2c_attach_client(client);
if (err)
goto exit_free;
/* Register sysfs hooks */ /* Register sysfs hooks */
err = sysfs_create_group(&client->dev.kobj, &ads7828_group); err = sysfs_create_group(&client->dev.kobj, &ads7828_group);
if (err) if (err)
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)) {
...@@ -262,8 +251,6 @@ static int ads7828_detect(struct i2c_adapter *adapter, int address, int kind) ...@@ -262,8 +251,6 @@ static int ads7828_detect(struct i2c_adapter *adapter, int address, int kind)
exit_remove: exit_remove:
sysfs_remove_group(&client->dev.kobj, &ads7828_group); sysfs_remove_group(&client->dev.kobj, &ads7828_group);
exit_detach:
i2c_detach_client(client);
exit_free: exit_free:
kfree(data); kfree(data);
exit: exit:
......
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