Commit c6d3f6fa authored by Jean Delvare's avatar Jean Delvare Committed by Jean Delvare

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

The new-style max1619 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: Alexey Fisher <fishor@mail.ru>
parent 70b72406
...@@ -79,23 +79,34 @@ I2C_CLIENT_INSMOD_1(max1619); ...@@ -79,23 +79,34 @@ I2C_CLIENT_INSMOD_1(max1619);
* Functions declaration * Functions declaration
*/ */
static int max1619_attach_adapter(struct i2c_adapter *adapter); static int max1619_probe(struct i2c_client *client,
static int max1619_detect(struct i2c_adapter *adapter, int address, const struct i2c_device_id *id);
int kind); static int max1619_detect(struct i2c_client *client, int kind,
struct i2c_board_info *info);
static void max1619_init_client(struct i2c_client *client); static void max1619_init_client(struct i2c_client *client);
static int max1619_detach_client(struct i2c_client *client); static int max1619_remove(struct i2c_client *client);
static struct max1619_data *max1619_update_device(struct device *dev); static struct max1619_data *max1619_update_device(struct device *dev);
/* /*
* Driver data (common to all clients) * Driver data (common to all clients)
*/ */
static const struct i2c_device_id max1619_id[] = {
{ "max1619", max1619 },
{ }
};
MODULE_DEVICE_TABLE(i2c, max1619_id);
static struct i2c_driver max1619_driver = { static struct i2c_driver max1619_driver = {
.class = I2C_CLASS_HWMON,
.driver = { .driver = {
.name = "max1619", .name = "max1619",
}, },
.attach_adapter = max1619_attach_adapter, .probe = max1619_probe,
.detach_client = max1619_detach_client, .remove = max1619_remove,
.id_table = max1619_id,
.detect = max1619_detect,
.address_data = &addr_data,
}; };
/* /*
...@@ -103,7 +114,6 @@ static struct i2c_driver max1619_driver = { ...@@ -103,7 +114,6 @@ static struct i2c_driver max1619_driver = {
*/ */
struct max1619_data { struct max1619_data {
struct i2c_client client;
struct device *hwmon_dev; struct device *hwmon_dev;
struct mutex update_lock; struct mutex update_lock;
char valid; /* zero until following fields are valid */ char valid; /* zero until following fields are valid */
...@@ -208,41 +218,15 @@ static const struct attribute_group max1619_group = { ...@@ -208,41 +218,15 @@ static const struct attribute_group max1619_group = {
* Real code * Real code
*/ */
static int max1619_attach_adapter(struct i2c_adapter *adapter) /* Return 0 if detection is successful, -ENODEV otherwise */
static int max1619_detect(struct i2c_client *new_client, int kind,
struct i2c_board_info *info)
{ {
if (!(adapter->class & I2C_CLASS_HWMON)) struct i2c_adapter *adapter = new_client->adapter;
return 0;
return i2c_probe(adapter, &addr_data, max1619_detect);
}
/*
* The following function does more than just detection. If detection
* succeeds, it also registers the new chip.
*/
static int max1619_detect(struct i2c_adapter *adapter, int address, int kind)
{
struct i2c_client *new_client;
struct max1619_data *data;
int err = 0;
const char *name = "";
u8 reg_config=0, reg_convrate=0, reg_status=0; u8 reg_config=0, reg_convrate=0, reg_status=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 max1619_data), GFP_KERNEL))) {
err = -ENOMEM;
goto exit;
}
/* The common I2C client data is placed right before the
MAX1619-specific data. */
new_client = &data->client;
i2c_set_clientdata(new_client, data);
new_client->addr = address;
new_client->adapter = adapter;
new_client->driver = &max1619_driver;
new_client->flags = 0;
/* /*
* Now we do the remaining detection. A negative kind means that * Now we do the remaining detection. A negative kind means that
...@@ -265,8 +249,8 @@ static int max1619_detect(struct i2c_adapter *adapter, int address, int kind) ...@@ -265,8 +249,8 @@ static int max1619_detect(struct i2c_adapter *adapter, int address, int kind)
|| reg_convrate > 0x07 || (reg_status & 0x61 ) !=0x00) { || reg_convrate > 0x07 || (reg_status & 0x61 ) !=0x00) {
dev_dbg(&adapter->dev, dev_dbg(&adapter->dev,
"MAX1619 detection failed at 0x%02x.\n", "MAX1619 detection failed at 0x%02x.\n",
address); new_client->addr);
goto exit_free; return -ENODEV;
} }
} }
...@@ -285,28 +269,37 @@ static int max1619_detect(struct i2c_adapter *adapter, int address, int kind) ...@@ -285,28 +269,37 @@ static int max1619_detect(struct i2c_adapter *adapter, int address, int kind)
dev_info(&adapter->dev, dev_info(&adapter->dev,
"Unsupported chip (man_id=0x%02X, " "Unsupported chip (man_id=0x%02X, "
"chip_id=0x%02X).\n", man_id, chip_id); "chip_id=0x%02X).\n", man_id, chip_id);
goto exit_free; return -ENODEV;
} }
} }
if (kind == max1619) strlcpy(info->type, "max1619", I2C_NAME_SIZE);
name = "max1619";
/* We can fill in the remaining client fields */ return 0;
strlcpy(new_client->name, name, I2C_NAME_SIZE); }
static int max1619_probe(struct i2c_client *new_client,
const struct i2c_device_id *id)
{
struct max1619_data *data;
int err;
data = kzalloc(sizeof(struct max1619_data), GFP_KERNEL);
if (!data) {
err = -ENOMEM;
goto exit;
}
i2c_set_clientdata(new_client, data);
data->valid = 0; data->valid = 0;
mutex_init(&data->update_lock); mutex_init(&data->update_lock);
/* Tell the I2C layer a new client has arrived */
if ((err = i2c_attach_client(new_client)))
goto exit_free;
/* Initialize the MAX1619 chip */ /* Initialize the MAX1619 chip */
max1619_init_client(new_client); max1619_init_client(new_client);
/* Register sysfs hooks */ /* Register sysfs hooks */
if ((err = sysfs_create_group(&new_client->dev.kobj, &max1619_group))) if ((err = sysfs_create_group(&new_client->dev.kobj, &max1619_group)))
goto exit_detach; goto exit_free;
data->hwmon_dev = hwmon_device_register(&new_client->dev); data->hwmon_dev = hwmon_device_register(&new_client->dev);
if (IS_ERR(data->hwmon_dev)) { if (IS_ERR(data->hwmon_dev)) {
...@@ -318,8 +311,6 @@ static int max1619_detect(struct i2c_adapter *adapter, int address, int kind) ...@@ -318,8 +311,6 @@ static int max1619_detect(struct i2c_adapter *adapter, int address, int kind)
exit_remove_files: exit_remove_files:
sysfs_remove_group(&new_client->dev.kobj, &max1619_group); sysfs_remove_group(&new_client->dev.kobj, &max1619_group);
exit_detach:
i2c_detach_client(new_client);
exit_free: exit_free:
kfree(data); kfree(data);
exit: exit:
...@@ -341,17 +332,13 @@ static void max1619_init_client(struct i2c_client *client) ...@@ -341,17 +332,13 @@ static void max1619_init_client(struct i2c_client *client)
config & 0xBF); /* run */ config & 0xBF); /* run */
} }
static int max1619_detach_client(struct i2c_client *client) static int max1619_remove(struct i2c_client *client)
{ {
struct max1619_data *data = i2c_get_clientdata(client); struct max1619_data *data = i2c_get_clientdata(client);
int err;
hwmon_device_unregister(data->hwmon_dev); hwmon_device_unregister(data->hwmon_dev);
sysfs_remove_group(&client->dev.kobj, &max1619_group); sysfs_remove_group(&client->dev.kobj, &max1619_group);
if ((err = i2c_detach_client(client)))
return err;
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