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

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

The new-style lm77 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: Andras Bali <drewie@freemail.hu>
parent d5957be2
...@@ -52,7 +52,6 @@ I2C_CLIENT_INSMOD_1(lm77); ...@@ -52,7 +52,6 @@ I2C_CLIENT_INSMOD_1(lm77);
/* Each client has this additional data */ /* Each client has this additional data */
struct lm77_data { struct lm77_data {
struct i2c_client client;
struct device *hwmon_dev; struct device *hwmon_dev;
struct mutex update_lock; struct mutex update_lock;
char valid; char valid;
...@@ -65,23 +64,35 @@ struct lm77_data { ...@@ -65,23 +64,35 @@ struct lm77_data {
u8 alarms; u8 alarms;
}; };
static int lm77_attach_adapter(struct i2c_adapter *adapter); static int lm77_probe(struct i2c_client *client,
static int lm77_detect(struct i2c_adapter *adapter, int address, int kind); const struct i2c_device_id *id);
static int lm77_detect(struct i2c_client *client, int kind,
struct i2c_board_info *info);
static void lm77_init_client(struct i2c_client *client); static void lm77_init_client(struct i2c_client *client);
static int lm77_detach_client(struct i2c_client *client); static int lm77_remove(struct i2c_client *client);
static u16 lm77_read_value(struct i2c_client *client, u8 reg); static u16 lm77_read_value(struct i2c_client *client, u8 reg);
static int lm77_write_value(struct i2c_client *client, u8 reg, u16 value); static int lm77_write_value(struct i2c_client *client, u8 reg, u16 value);
static struct lm77_data *lm77_update_device(struct device *dev); static struct lm77_data *lm77_update_device(struct device *dev);
static const struct i2c_device_id lm77_id[] = {
{ "lm77", lm77 },
{ }
};
MODULE_DEVICE_TABLE(i2c, lm77_id);
/* This is the driver that will be inserted */ /* This is the driver that will be inserted */
static struct i2c_driver lm77_driver = { static struct i2c_driver lm77_driver = {
.class = I2C_CLASS_HWMON,
.driver = { .driver = {
.name = "lm77", .name = "lm77",
}, },
.attach_adapter = lm77_attach_adapter, .probe = lm77_probe,
.detach_client = lm77_detach_client, .remove = lm77_remove,
.id_table = lm77_id,
.detect = lm77_detect,
.address_data = &addr_data,
}; };
/* straight from the datasheet */ /* straight from the datasheet */
...@@ -215,13 +226,6 @@ static SENSOR_DEVICE_ATTR(temp1_crit_alarm, S_IRUGO, show_alarm, NULL, 2); ...@@ -215,13 +226,6 @@ static SENSOR_DEVICE_ATTR(temp1_crit_alarm, S_IRUGO, show_alarm, NULL, 2);
static SENSOR_DEVICE_ATTR(temp1_min_alarm, S_IRUGO, show_alarm, NULL, 0); static SENSOR_DEVICE_ATTR(temp1_min_alarm, S_IRUGO, show_alarm, NULL, 0);
static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 1); static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 1);
static int lm77_attach_adapter(struct i2c_adapter *adapter)
{
if (!(adapter->class & I2C_CLASS_HWMON))
return 0;
return i2c_probe(adapter, &addr_data, lm77_detect);
}
static struct attribute *lm77_attributes[] = { static struct attribute *lm77_attributes[] = {
&dev_attr_temp1_input.attr, &dev_attr_temp1_input.attr,
&dev_attr_temp1_crit.attr, &dev_attr_temp1_crit.attr,
...@@ -240,32 +244,15 @@ static const struct attribute_group lm77_group = { ...@@ -240,32 +244,15 @@ static const struct attribute_group lm77_group = {
.attrs = lm77_attributes, .attrs = lm77_attributes,
}; };
/* This function is called by i2c_probe */ /* Return 0 if detection is successful, -ENODEV otherwise */
static int lm77_detect(struct i2c_adapter *adapter, int address, int kind) static int lm77_detect(struct i2c_client *new_client, int kind,
struct i2c_board_info *info)
{ {
struct i2c_client *new_client; struct i2c_adapter *adapter = new_client->adapter;
struct lm77_data *data;
int err = 0;
const char *name = "";
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))
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 lm77_{read,write}_value. */
if (!(data = kzalloc(sizeof(struct lm77_data), GFP_KERNEL))) {
err = -ENOMEM;
goto exit;
}
new_client = &data->client;
i2c_set_clientdata(new_client, data);
new_client->addr = address;
new_client->adapter = adapter;
new_client->driver = &lm77_driver;
new_client->flags = 0;
/* Here comes the remaining detection. Since the LM77 has no /* Here comes the remaining detection. Since the LM77 has no
register dedicated to identification, we have to rely on the register dedicated to identification, we have to rely on the
...@@ -294,7 +281,7 @@ static int lm77_detect(struct i2c_adapter *adapter, int address, int kind) ...@@ -294,7 +281,7 @@ static int lm77_detect(struct i2c_adapter *adapter, int address, int kind)
|| i2c_smbus_read_word_data(new_client, i + 3) != crit || i2c_smbus_read_word_data(new_client, i + 3) != crit
|| i2c_smbus_read_word_data(new_client, i + 4) != min || i2c_smbus_read_word_data(new_client, i + 4) != min
|| i2c_smbus_read_word_data(new_client, i + 5) != max) || i2c_smbus_read_word_data(new_client, i + 5) != max)
goto exit_free; return -ENODEV;
/* sign bits */ /* sign bits */
if (((cur & 0x00f0) != 0xf0 && (cur & 0x00f0) != 0x0) if (((cur & 0x00f0) != 0xf0 && (cur & 0x00f0) != 0x0)
...@@ -302,51 +289,55 @@ static int lm77_detect(struct i2c_adapter *adapter, int address, int kind) ...@@ -302,51 +289,55 @@ static int lm77_detect(struct i2c_adapter *adapter, int address, int kind)
|| ((crit & 0x00f0) != 0xf0 && (crit & 0x00f0) != 0x0) || ((crit & 0x00f0) != 0xf0 && (crit & 0x00f0) != 0x0)
|| ((min & 0x00f0) != 0xf0 && (min & 0x00f0) != 0x0) || ((min & 0x00f0) != 0xf0 && (min & 0x00f0) != 0x0)
|| ((max & 0x00f0) != 0xf0 && (max & 0x00f0) != 0x0)) || ((max & 0x00f0) != 0xf0 && (max & 0x00f0) != 0x0))
goto exit_free; return -ENODEV;
/* unused bits */ /* unused bits */
if (conf & 0xe0) if (conf & 0xe0)
goto exit_free; return -ENODEV;
/* 0x06 and 0x07 return the last read value */ /* 0x06 and 0x07 return the last read value */
cur = i2c_smbus_read_word_data(new_client, 0); cur = i2c_smbus_read_word_data(new_client, 0);
if (i2c_smbus_read_word_data(new_client, 6) != cur if (i2c_smbus_read_word_data(new_client, 6) != cur
|| i2c_smbus_read_word_data(new_client, 7) != cur) || i2c_smbus_read_word_data(new_client, 7) != cur)
goto exit_free; return -ENODEV;
hyst = i2c_smbus_read_word_data(new_client, 2); hyst = i2c_smbus_read_word_data(new_client, 2);
if (i2c_smbus_read_word_data(new_client, 6) != hyst if (i2c_smbus_read_word_data(new_client, 6) != hyst
|| i2c_smbus_read_word_data(new_client, 7) != hyst) || i2c_smbus_read_word_data(new_client, 7) != hyst)
goto exit_free; return -ENODEV;
min = i2c_smbus_read_word_data(new_client, 4); min = i2c_smbus_read_word_data(new_client, 4);
if (i2c_smbus_read_word_data(new_client, 6) != min if (i2c_smbus_read_word_data(new_client, 6) != min
|| i2c_smbus_read_word_data(new_client, 7) != min) || i2c_smbus_read_word_data(new_client, 7) != min)
goto exit_free; return -ENODEV;
} }
/* Determine the chip type - only one kind supported! */ strlcpy(info->type, "lm77", I2C_NAME_SIZE);
if (kind <= 0)
kind = lm77; return 0;
}
static int lm77_probe(struct i2c_client *new_client,
const struct i2c_device_id *id)
{
struct lm77_data *data;
int err;
if (kind == lm77) { data = kzalloc(sizeof(struct lm77_data), GFP_KERNEL);
name = "lm77"; if (!data) {
err = -ENOMEM;
goto exit;
} }
/* Fill in the remaining client fields and put it into the global list */ i2c_set_clientdata(new_client, data);
strlcpy(new_client->name, name, I2C_NAME_SIZE);
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 LM77 chip */ /* Initialize the LM77 chip */
lm77_init_client(new_client); lm77_init_client(new_client);
/* Register sysfs hooks */ /* Register sysfs hooks */
if ((err = sysfs_create_group(&new_client->dev.kobj, &lm77_group))) if ((err = sysfs_create_group(&new_client->dev.kobj, &lm77_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)) {
...@@ -358,20 +349,17 @@ static int lm77_detect(struct i2c_adapter *adapter, int address, int kind) ...@@ -358,20 +349,17 @@ static int lm77_detect(struct i2c_adapter *adapter, int address, int kind)
exit_remove: exit_remove:
sysfs_remove_group(&new_client->dev.kobj, &lm77_group); sysfs_remove_group(&new_client->dev.kobj, &lm77_group);
exit_detach:
i2c_detach_client(new_client);
exit_free: exit_free:
kfree(data); kfree(data);
exit: exit:
return err; return err;
} }
static int lm77_detach_client(struct i2c_client *client) static int lm77_remove(struct i2c_client *client)
{ {
struct lm77_data *data = i2c_get_clientdata(client); struct lm77_data *data = i2c_get_clientdata(client);
hwmon_device_unregister(data->hwmon_dev); hwmon_device_unregister(data->hwmon_dev);
sysfs_remove_group(&client->dev.kobj, &lm77_group); sysfs_remove_group(&client->dev.kobj, &lm77_group);
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