i2c: Convert the pcf8591 driver to a new-style i2c driver

The new-style pcf8591 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:
Jean Delvare 2008-07-16 19:30:06 +02:00 committed by Jean Delvare
parent 68be336374
commit 8b77e6ac49
1 changed files with 40 additions and 54 deletions

View File

@ -72,28 +72,15 @@ MODULE_PARM_DESC(input_mode,
#define REG_TO_SIGNED(reg) (((reg) & 0x80)?((reg) - 256):(reg)) #define REG_TO_SIGNED(reg) (((reg) & 0x80)?((reg) - 256):(reg))
struct pcf8591_data { struct pcf8591_data {
struct i2c_client client;
struct mutex update_lock; struct mutex update_lock;
u8 control; u8 control;
u8 aout; u8 aout;
}; };
static int pcf8591_attach_adapter(struct i2c_adapter *adapter);
static int pcf8591_detect(struct i2c_adapter *adapter, int address, int kind);
static int pcf8591_detach_client(struct i2c_client *client);
static void pcf8591_init_client(struct i2c_client *client); static void pcf8591_init_client(struct i2c_client *client);
static int pcf8591_read_channel(struct device *dev, int channel); static int pcf8591_read_channel(struct device *dev, int channel);
/* This is the driver that will be inserted */
static struct i2c_driver pcf8591_driver = {
.driver = {
.name = "pcf8591",
},
.attach_adapter = pcf8591_attach_adapter,
.detach_client = pcf8591_detach_client,
};
/* following are the sysfs callback functions */ /* following are the sysfs callback functions */
#define show_in_channel(channel) \ #define show_in_channel(channel) \
static ssize_t show_in##channel##_input(struct device *dev, struct device_attribute *attr, char *buf) \ static ssize_t show_in##channel##_input(struct device *dev, struct device_attribute *attr, char *buf) \
@ -180,58 +167,46 @@ static const struct attribute_group pcf8591_attr_group_opt = {
/* /*
* Real code * Real code
*/ */
static int pcf8591_attach_adapter(struct i2c_adapter *adapter)
{
return i2c_probe(adapter, &addr_data, pcf8591_detect);
}
/* This function is called by i2c_probe */ /* Return 0 if detection is successful, -ENODEV otherwise */
static int pcf8591_detect(struct i2c_adapter *adapter, int address, int kind) static int pcf8591_detect(struct i2c_client *client, int kind,
struct i2c_board_info *info)
{ {
struct i2c_client *client; struct i2c_adapter *adapter = client->adapter;
struct pcf8591_data *data;
int err = 0;
if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE
| I2C_FUNC_SMBUS_WRITE_BYTE_DATA)) | I2C_FUNC_SMBUS_WRITE_BYTE_DATA))
goto exit; return -ENODEV;
/* Now, we would do the remaining detection. But the PCF8591 is plainly
impossible to detect! Stupid chip. */
strlcpy(info->type, "pcf8591", I2C_NAME_SIZE);
return 0;
}
static int pcf8591_probe(struct i2c_client *client,
const struct i2c_device_id *id)
{
struct pcf8591_data *data;
int err;
/* OK. For now, we presume we have a valid client. We now create the
client structure, even though we cannot fill it completely yet. */
if (!(data = kzalloc(sizeof(struct pcf8591_data), GFP_KERNEL))) { if (!(data = kzalloc(sizeof(struct pcf8591_data), GFP_KERNEL))) {
err = -ENOMEM; err = -ENOMEM;
goto exit; goto exit;
} }
client = &data->client;
i2c_set_clientdata(client, data); i2c_set_clientdata(client, data);
client->addr = address;
client->adapter = adapter;
client->driver = &pcf8591_driver;
/* Now, we would do the remaining detection. But the PCF8591 is plainly
impossible to detect! Stupid chip. */
/* Determine the chip type - only one kind supported! */
if (kind <= 0)
kind = pcf8591;
/* Fill in the remaining client fields and put it into the global
list */
strlcpy(client->name, "pcf8591", I2C_NAME_SIZE);
mutex_init(&data->update_lock); mutex_init(&data->update_lock);
/* Tell the I2C layer a new client has arrived */
if ((err = i2c_attach_client(client)))
goto exit_kfree;
/* Initialize the PCF8591 chip */ /* Initialize the PCF8591 chip */
pcf8591_init_client(client); pcf8591_init_client(client);
/* Register sysfs hooks */ /* Register sysfs hooks */
err = sysfs_create_group(&client->dev.kobj, &pcf8591_attr_group); err = sysfs_create_group(&client->dev.kobj, &pcf8591_attr_group);
if (err) if (err)
goto exit_detach; goto exit_kfree;
/* Register input2 if not in "two differential inputs" mode */ /* Register input2 if not in "two differential inputs" mode */
if (input_mode != 3) { if (input_mode != 3) {
@ -252,24 +227,16 @@ static int pcf8591_detect(struct i2c_adapter *adapter, int address, int kind)
exit_sysfs_remove: exit_sysfs_remove:
sysfs_remove_group(&client->dev.kobj, &pcf8591_attr_group_opt); sysfs_remove_group(&client->dev.kobj, &pcf8591_attr_group_opt);
sysfs_remove_group(&client->dev.kobj, &pcf8591_attr_group); sysfs_remove_group(&client->dev.kobj, &pcf8591_attr_group);
exit_detach:
i2c_detach_client(client);
exit_kfree: exit_kfree:
kfree(data); kfree(data);
exit: exit:
return err; return err;
} }
static int pcf8591_detach_client(struct i2c_client *client) static int pcf8591_remove(struct i2c_client *client)
{ {
int err;
sysfs_remove_group(&client->dev.kobj, &pcf8591_attr_group_opt); sysfs_remove_group(&client->dev.kobj, &pcf8591_attr_group_opt);
sysfs_remove_group(&client->dev.kobj, &pcf8591_attr_group); sysfs_remove_group(&client->dev.kobj, &pcf8591_attr_group);
if ((err = i2c_detach_client(client)))
return err;
kfree(i2c_get_clientdata(client)); kfree(i2c_get_clientdata(client));
return 0; return 0;
} }
@ -316,6 +283,25 @@ static int pcf8591_read_channel(struct device *dev, int channel)
return (10 * value); return (10 * value);
} }
static const struct i2c_device_id pcf8591_id[] = {
{ "pcf8591", 0 },
{ }
};
MODULE_DEVICE_TABLE(i2c, pcf8591_id);
static struct i2c_driver pcf8591_driver = {
.driver = {
.name = "pcf8591",
},
.probe = pcf8591_probe,
.remove = pcf8591_remove,
.id_table = pcf8591_id,
.class = I2C_CLASS_HWMON, /* Nearest choice */
.detect = pcf8591_detect,
.address_data = &addr_data,
};
static int __init pcf8591_init(void) static int __init pcf8591_init(void)
{ {
if (input_mode < 0 || input_mode > 3) { if (input_mode < 0 || input_mode > 3) {