hwmon: (adm1031) Convert to a new-style i2c driver
The new-style adm1031 driver implements the optional detect() callback to cover the use cases of the legacy driver. Signed-off-by: Jean Delvare <khali@linux-fr.org> Cc: Alexandre d'Alton <alex@alexdalton.org>
This commit is contained in:
parent
9c97fb4d25
commit
af200f881d
1 changed files with 49 additions and 47 deletions
|
@ -70,7 +70,6 @@ typedef u8 auto_chan_table_t[8][2];
|
||||||
|
|
||||||
/* Each client has this additional data */
|
/* Each client has this additional data */
|
||||||
struct adm1031_data {
|
struct adm1031_data {
|
||||||
struct i2c_client client;
|
|
||||||
struct device *hwmon_dev;
|
struct device *hwmon_dev;
|
||||||
struct mutex update_lock;
|
struct mutex update_lock;
|
||||||
int chip_type;
|
int chip_type;
|
||||||
|
@ -99,19 +98,32 @@ struct adm1031_data {
|
||||||
s8 temp_crit[3];
|
s8 temp_crit[3];
|
||||||
};
|
};
|
||||||
|
|
||||||
static int adm1031_attach_adapter(struct i2c_adapter *adapter);
|
static int adm1031_probe(struct i2c_client *client,
|
||||||
static int adm1031_detect(struct i2c_adapter *adapter, int address, int kind);
|
const struct i2c_device_id *id);
|
||||||
|
static int adm1031_detect(struct i2c_client *client, int kind,
|
||||||
|
struct i2c_board_info *info);
|
||||||
static void adm1031_init_client(struct i2c_client *client);
|
static void adm1031_init_client(struct i2c_client *client);
|
||||||
static int adm1031_detach_client(struct i2c_client *client);
|
static int adm1031_remove(struct i2c_client *client);
|
||||||
static struct adm1031_data *adm1031_update_device(struct device *dev);
|
static struct adm1031_data *adm1031_update_device(struct device *dev);
|
||||||
|
|
||||||
|
static const struct i2c_device_id adm1031_id[] = {
|
||||||
|
{ "adm1030", adm1030 },
|
||||||
|
{ "adm1031", adm1031 },
|
||||||
|
{ }
|
||||||
|
};
|
||||||
|
MODULE_DEVICE_TABLE(i2c, adm1031_id);
|
||||||
|
|
||||||
/* This is the driver that will be inserted */
|
/* This is the driver that will be inserted */
|
||||||
static struct i2c_driver adm1031_driver = {
|
static struct i2c_driver adm1031_driver = {
|
||||||
|
.class = I2C_CLASS_HWMON,
|
||||||
.driver = {
|
.driver = {
|
||||||
.name = "adm1031",
|
.name = "adm1031",
|
||||||
},
|
},
|
||||||
.attach_adapter = adm1031_attach_adapter,
|
.probe = adm1031_probe,
|
||||||
.detach_client = adm1031_detach_client,
|
.remove = adm1031_remove,
|
||||||
|
.id_table = adm1031_id,
|
||||||
|
.detect = adm1031_detect,
|
||||||
|
.address_data = &addr_data,
|
||||||
};
|
};
|
||||||
|
|
||||||
static inline u8 adm1031_read_value(struct i2c_client *client, u8 reg)
|
static inline u8 adm1031_read_value(struct i2c_client *client, u8 reg)
|
||||||
|
@ -693,13 +705,6 @@ static SENSOR_DEVICE_ATTR(temp3_crit_alarm, S_IRUGO, show_alarm, NULL, 12);
|
||||||
static SENSOR_DEVICE_ATTR(temp3_fault, S_IRUGO, show_alarm, NULL, 13);
|
static SENSOR_DEVICE_ATTR(temp3_fault, S_IRUGO, show_alarm, NULL, 13);
|
||||||
static SENSOR_DEVICE_ATTR(temp1_crit_alarm, S_IRUGO, show_alarm, NULL, 14);
|
static SENSOR_DEVICE_ATTR(temp1_crit_alarm, S_IRUGO, show_alarm, NULL, 14);
|
||||||
|
|
||||||
static int adm1031_attach_adapter(struct i2c_adapter *adapter)
|
|
||||||
{
|
|
||||||
if (!(adapter->class & I2C_CLASS_HWMON))
|
|
||||||
return 0;
|
|
||||||
return i2c_probe(adapter, &addr_data, adm1031_detect);
|
|
||||||
}
|
|
||||||
|
|
||||||
static struct attribute *adm1031_attributes[] = {
|
static struct attribute *adm1031_attributes[] = {
|
||||||
&sensor_dev_attr_fan1_input.dev_attr.attr,
|
&sensor_dev_attr_fan1_input.dev_attr.attr,
|
||||||
&sensor_dev_attr_fan1_div.dev_attr.attr,
|
&sensor_dev_attr_fan1_div.dev_attr.attr,
|
||||||
|
@ -770,27 +775,15 @@ static const struct attribute_group adm1031_group_opt = {
|
||||||
.attrs = adm1031_attributes_opt,
|
.attrs = adm1031_attributes_opt,
|
||||||
};
|
};
|
||||||
|
|
||||||
/* This function is called by i2c_probe */
|
/* Return 0 if detection is successful, -ENODEV otherwise */
|
||||||
static int adm1031_detect(struct i2c_adapter *adapter, int address, int kind)
|
static int adm1031_detect(struct i2c_client *client, int kind,
|
||||||
|
struct i2c_board_info *info)
|
||||||
{
|
{
|
||||||
struct i2c_client *client;
|
struct i2c_adapter *adapter = client->adapter;
|
||||||
struct adm1031_data *data;
|
|
||||||
int err = 0;
|
|
||||||
const char *name = "";
|
const char *name = "";
|
||||||
|
|
||||||
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 adm1031_data), GFP_KERNEL))) {
|
|
||||||
err = -ENOMEM;
|
|
||||||
goto exit;
|
|
||||||
}
|
|
||||||
|
|
||||||
client = &data->client;
|
|
||||||
i2c_set_clientdata(client, data);
|
|
||||||
client->addr = address;
|
|
||||||
client->adapter = adapter;
|
|
||||||
client->driver = &adm1031_driver;
|
|
||||||
|
|
||||||
if (kind < 0) {
|
if (kind < 0) {
|
||||||
int id, co;
|
int id, co;
|
||||||
|
@ -798,7 +791,7 @@ static int adm1031_detect(struct i2c_adapter *adapter, int address, int kind)
|
||||||
co = i2c_smbus_read_byte_data(client, 0x3e);
|
co = i2c_smbus_read_byte_data(client, 0x3e);
|
||||||
|
|
||||||
if (!((id == 0x31 || id == 0x30) && co == 0x41))
|
if (!((id == 0x31 || id == 0x30) && co == 0x41))
|
||||||
goto exit_free;
|
return -ENODEV;
|
||||||
kind = (id == 0x30) ? adm1030 : adm1031;
|
kind = (id == 0x30) ? adm1030 : adm1031;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -809,28 +802,43 @@ static int adm1031_detect(struct i2c_adapter *adapter, int address, int kind)
|
||||||
* auto fan control helper table. */
|
* auto fan control helper table. */
|
||||||
if (kind == adm1030) {
|
if (kind == adm1030) {
|
||||||
name = "adm1030";
|
name = "adm1030";
|
||||||
data->chan_select_table = &auto_channel_select_table_adm1030;
|
|
||||||
} else if (kind == adm1031) {
|
} else if (kind == adm1031) {
|
||||||
name = "adm1031";
|
name = "adm1031";
|
||||||
data->chan_select_table = &auto_channel_select_table_adm1031;
|
|
||||||
}
|
}
|
||||||
data->chip_type = kind;
|
strlcpy(info->type, name, I2C_NAME_SIZE);
|
||||||
|
|
||||||
strlcpy(client->name, name, I2C_NAME_SIZE);
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int adm1031_probe(struct i2c_client *client,
|
||||||
|
const struct i2c_device_id *id)
|
||||||
|
{
|
||||||
|
struct adm1031_data *data;
|
||||||
|
int err;
|
||||||
|
|
||||||
|
data = kzalloc(sizeof(struct adm1031_data), GFP_KERNEL);
|
||||||
|
if (!data) {
|
||||||
|
err = -ENOMEM;
|
||||||
|
goto exit;
|
||||||
|
}
|
||||||
|
|
||||||
|
i2c_set_clientdata(client, data);
|
||||||
|
data->chip_type = id->driver_data;
|
||||||
mutex_init(&data->update_lock);
|
mutex_init(&data->update_lock);
|
||||||
|
|
||||||
/* Tell the I2C layer a new client has arrived */
|
if (data->chip_type == adm1030)
|
||||||
if ((err = i2c_attach_client(client)))
|
data->chan_select_table = &auto_channel_select_table_adm1030;
|
||||||
goto exit_free;
|
else
|
||||||
|
data->chan_select_table = &auto_channel_select_table_adm1031;
|
||||||
|
|
||||||
/* Initialize the ADM1031 chip */
|
/* Initialize the ADM1031 chip */
|
||||||
adm1031_init_client(client);
|
adm1031_init_client(client);
|
||||||
|
|
||||||
/* Register sysfs hooks */
|
/* Register sysfs hooks */
|
||||||
if ((err = sysfs_create_group(&client->dev.kobj, &adm1031_group)))
|
if ((err = sysfs_create_group(&client->dev.kobj, &adm1031_group)))
|
||||||
goto exit_detach;
|
goto exit_free;
|
||||||
|
|
||||||
if (kind == adm1031) {
|
if (data->chip_type == adm1031) {
|
||||||
if ((err = sysfs_create_group(&client->dev.kobj,
|
if ((err = sysfs_create_group(&client->dev.kobj,
|
||||||
&adm1031_group_opt)))
|
&adm1031_group_opt)))
|
||||||
goto exit_remove;
|
goto exit_remove;
|
||||||
|
@ -847,25 +855,19 @@ static int adm1031_detect(struct i2c_adapter *adapter, int address, int kind)
|
||||||
exit_remove:
|
exit_remove:
|
||||||
sysfs_remove_group(&client->dev.kobj, &adm1031_group);
|
sysfs_remove_group(&client->dev.kobj, &adm1031_group);
|
||||||
sysfs_remove_group(&client->dev.kobj, &adm1031_group_opt);
|
sysfs_remove_group(&client->dev.kobj, &adm1031_group_opt);
|
||||||
exit_detach:
|
|
||||||
i2c_detach_client(client);
|
|
||||||
exit_free:
|
exit_free:
|
||||||
kfree(data);
|
kfree(data);
|
||||||
exit:
|
exit:
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int adm1031_detach_client(struct i2c_client *client)
|
static int adm1031_remove(struct i2c_client *client)
|
||||||
{
|
{
|
||||||
struct adm1031_data *data = i2c_get_clientdata(client);
|
struct adm1031_data *data = i2c_get_clientdata(client);
|
||||||
int ret;
|
|
||||||
|
|
||||||
hwmon_device_unregister(data->hwmon_dev);
|
hwmon_device_unregister(data->hwmon_dev);
|
||||||
sysfs_remove_group(&client->dev.kobj, &adm1031_group);
|
sysfs_remove_group(&client->dev.kobj, &adm1031_group);
|
||||||
sysfs_remove_group(&client->dev.kobj, &adm1031_group_opt);
|
sysfs_remove_group(&client->dev.kobj, &adm1031_group_opt);
|
||||||
if ((ret = i2c_detach_client(client)) != 0) {
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
kfree(data);
|
kfree(data);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue