hwmon: (ds1621) Convert to a new-style i2c driver
The new-style ds1621 driver implements the optional detect() callback to cover the use cases of the legacy driver. Signed-off-by: Jean Delvare <khali@linux-fr.org>
This commit is contained in:
parent
71163c7c36
commit
70313eabfc
|
@ -72,7 +72,6 @@ static const u8 DS1621_REG_TEMP[3] = {
|
||||||
|
|
||||||
/* Each client has this additional data */
|
/* Each client has this additional data */
|
||||||
struct ds1621_data {
|
struct ds1621_data {
|
||||||
struct i2c_client client;
|
|
||||||
struct device *hwmon_dev;
|
struct device *hwmon_dev;
|
||||||
struct mutex update_lock;
|
struct mutex update_lock;
|
||||||
char valid; /* !=0 if following fields are valid */
|
char valid; /* !=0 if following fields are valid */
|
||||||
|
@ -82,20 +81,32 @@ struct ds1621_data {
|
||||||
u8 conf; /* Register encoding, combined */
|
u8 conf; /* Register encoding, combined */
|
||||||
};
|
};
|
||||||
|
|
||||||
static int ds1621_attach_adapter(struct i2c_adapter *adapter);
|
static int ds1621_probe(struct i2c_client *client,
|
||||||
static int ds1621_detect(struct i2c_adapter *adapter, int address,
|
const struct i2c_device_id *id);
|
||||||
int kind);
|
static int ds1621_detect(struct i2c_client *client, int kind,
|
||||||
|
struct i2c_board_info *info);
|
||||||
static void ds1621_init_client(struct i2c_client *client);
|
static void ds1621_init_client(struct i2c_client *client);
|
||||||
static int ds1621_detach_client(struct i2c_client *client);
|
static int ds1621_remove(struct i2c_client *client);
|
||||||
static struct ds1621_data *ds1621_update_client(struct device *dev);
|
static struct ds1621_data *ds1621_update_client(struct device *dev);
|
||||||
|
|
||||||
|
static const struct i2c_device_id ds1621_id[] = {
|
||||||
|
{ "ds1621", ds1621 },
|
||||||
|
{ "ds1625", ds1621 },
|
||||||
|
{ }
|
||||||
|
};
|
||||||
|
MODULE_DEVICE_TABLE(i2c, ds1621_id);
|
||||||
|
|
||||||
/* This is the driver that will be inserted */
|
/* This is the driver that will be inserted */
|
||||||
static struct i2c_driver ds1621_driver = {
|
static struct i2c_driver ds1621_driver = {
|
||||||
|
.class = I2C_CLASS_HWMON,
|
||||||
.driver = {
|
.driver = {
|
||||||
.name = "ds1621",
|
.name = "ds1621",
|
||||||
},
|
},
|
||||||
.attach_adapter = ds1621_attach_adapter,
|
.probe = ds1621_probe,
|
||||||
.detach_client = ds1621_detach_client,
|
.remove = ds1621_remove,
|
||||||
|
.id_table = ds1621_id,
|
||||||
|
.detect = ds1621_detect,
|
||||||
|
.address_data = &addr_data,
|
||||||
};
|
};
|
||||||
|
|
||||||
/* All registers are word-sized, except for the configuration register.
|
/* All registers are word-sized, except for the configuration register.
|
||||||
|
@ -199,40 +210,18 @@ static const struct attribute_group ds1621_group = {
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
static int ds1621_attach_adapter(struct i2c_adapter *adapter)
|
/* Return 0 if detection is successful, -ENODEV otherwise */
|
||||||
{
|
static int ds1621_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, ds1621_detect);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* This function is called by i2c_probe */
|
|
||||||
static int ds1621_detect(struct i2c_adapter *adapter, int address,
|
|
||||||
int kind)
|
|
||||||
{
|
{
|
||||||
|
struct i2c_adapter *adapter = client->adapter;
|
||||||
int conf, temp;
|
int conf, temp;
|
||||||
struct i2c_client *client;
|
int i;
|
||||||
struct ds1621_data *data;
|
|
||||||
int i, err = 0;
|
|
||||||
|
|
||||||
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
|
||||||
| I2C_FUNC_SMBUS_WRITE_BYTE))
|
| I2C_FUNC_SMBUS_WRITE_BYTE))
|
||||||
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 ds1621_{read,write}_value. */
|
|
||||||
if (!(data = kzalloc(sizeof(struct ds1621_data), GFP_KERNEL))) {
|
|
||||||
err = -ENOMEM;
|
|
||||||
goto exit;
|
|
||||||
}
|
|
||||||
|
|
||||||
client = &data->client;
|
|
||||||
i2c_set_clientdata(client, data);
|
|
||||||
client->addr = address;
|
|
||||||
client->adapter = adapter;
|
|
||||||
client->driver = &ds1621_driver;
|
|
||||||
|
|
||||||
/* Now, we do the remaining detection. It is lousy. */
|
/* Now, we do the remaining detection. It is lousy. */
|
||||||
if (kind < 0) {
|
if (kind < 0) {
|
||||||
|
@ -241,29 +230,41 @@ static int ds1621_detect(struct i2c_adapter *adapter, int address,
|
||||||
improbable in our case. */
|
improbable in our case. */
|
||||||
conf = ds1621_read_value(client, DS1621_REG_CONF);
|
conf = ds1621_read_value(client, DS1621_REG_CONF);
|
||||||
if (conf & DS1621_REG_CONFIG_NVB)
|
if (conf & DS1621_REG_CONFIG_NVB)
|
||||||
goto exit_free;
|
return -ENODEV;
|
||||||
/* The 7 lowest bits of a temperature should always be 0. */
|
/* The 7 lowest bits of a temperature should always be 0. */
|
||||||
for (i = 0; i < ARRAY_SIZE(data->temp); i++) {
|
for (i = 0; i < ARRAY_SIZE(DS1621_REG_TEMP); i++) {
|
||||||
temp = ds1621_read_value(client, DS1621_REG_TEMP[i]);
|
temp = ds1621_read_value(client, DS1621_REG_TEMP[i]);
|
||||||
if (temp & 0x007f)
|
if (temp & 0x007f)
|
||||||
goto exit_free;
|
return -ENODEV;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Fill in remaining client fields and put it into the global list */
|
strlcpy(info->type, "ds1621", I2C_NAME_SIZE);
|
||||||
strlcpy(client->name, "ds1621", I2C_NAME_SIZE);
|
|
||||||
mutex_init(&data->update_lock);
|
|
||||||
|
|
||||||
/* Tell the I2C layer a new client has arrived */
|
return 0;
|
||||||
if ((err = i2c_attach_client(client)))
|
}
|
||||||
goto exit_free;
|
|
||||||
|
static int ds1621_probe(struct i2c_client *client,
|
||||||
|
const struct i2c_device_id *id)
|
||||||
|
{
|
||||||
|
struct ds1621_data *data;
|
||||||
|
int err;
|
||||||
|
|
||||||
|
data = kzalloc(sizeof(struct ds1621_data), GFP_KERNEL);
|
||||||
|
if (!data) {
|
||||||
|
err = -ENOMEM;
|
||||||
|
goto exit;
|
||||||
|
}
|
||||||
|
|
||||||
|
i2c_set_clientdata(client, data);
|
||||||
|
mutex_init(&data->update_lock);
|
||||||
|
|
||||||
/* Initialize the DS1621 chip */
|
/* Initialize the DS1621 chip */
|
||||||
ds1621_init_client(client);
|
ds1621_init_client(client);
|
||||||
|
|
||||||
/* Register sysfs hooks */
|
/* Register sysfs hooks */
|
||||||
if ((err = sysfs_create_group(&client->dev.kobj, &ds1621_group)))
|
if ((err = sysfs_create_group(&client->dev.kobj, &ds1621_group)))
|
||||||
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)) {
|
||||||
|
@ -275,25 +276,19 @@ static int ds1621_detect(struct i2c_adapter *adapter, int address,
|
||||||
|
|
||||||
exit_remove_files:
|
exit_remove_files:
|
||||||
sysfs_remove_group(&client->dev.kobj, &ds1621_group);
|
sysfs_remove_group(&client->dev.kobj, &ds1621_group);
|
||||||
exit_detach:
|
|
||||||
i2c_detach_client(client);
|
|
||||||
exit_free:
|
exit_free:
|
||||||
kfree(data);
|
kfree(data);
|
||||||
exit:
|
exit:
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int ds1621_detach_client(struct i2c_client *client)
|
static int ds1621_remove(struct i2c_client *client)
|
||||||
{
|
{
|
||||||
struct ds1621_data *data = i2c_get_clientdata(client);
|
struct ds1621_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, &ds1621_group);
|
sysfs_remove_group(&client->dev.kobj, &ds1621_group);
|
||||||
|
|
||||||
if ((err = i2c_detach_client(client)))
|
|
||||||
return err;
|
|
||||||
|
|
||||||
kfree(data);
|
kfree(data);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|
Loading…
Reference in New Issue