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

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

The new-style lm83 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>
parent 8c8bacc8
/* /*
* lm83.c - Part of lm_sensors, Linux kernel modules for hardware * lm83.c - Part of lm_sensors, Linux kernel modules for hardware
* monitoring * monitoring
* Copyright (C) 2003-2006 Jean Delvare <khali@linux-fr.org> * Copyright (C) 2003-2008 Jean Delvare <khali@linux-fr.org>
* *
* Heavily inspired from the lm78, lm75 and adm1021 drivers. The LM83 is * Heavily inspired from the lm78, lm75 and adm1021 drivers. The LM83 is
* a sensor chip made by National Semiconductor. It reports up to four * a sensor chip made by National Semiconductor. It reports up to four
...@@ -118,21 +118,34 @@ static const u8 LM83_REG_W_HIGH[] = { ...@@ -118,21 +118,34 @@ static const u8 LM83_REG_W_HIGH[] = {
* Functions declaration * Functions declaration
*/ */
static int lm83_attach_adapter(struct i2c_adapter *adapter); static int lm83_detect(struct i2c_client *new_client, int kind,
static int lm83_detect(struct i2c_adapter *adapter, int address, int kind); struct i2c_board_info *info);
static int lm83_detach_client(struct i2c_client *client); static int lm83_probe(struct i2c_client *client,
const struct i2c_device_id *id);
static int lm83_remove(struct i2c_client *client);
static struct lm83_data *lm83_update_device(struct device *dev); static struct lm83_data *lm83_update_device(struct device *dev);
/* /*
* Driver data (common to all clients) * Driver data (common to all clients)
*/ */
static const struct i2c_device_id lm83_id[] = {
{ "lm83", lm83 },
{ "lm82", lm82 },
{ }
};
MODULE_DEVICE_TABLE(i2c, lm83_id);
static struct i2c_driver lm83_driver = { static struct i2c_driver lm83_driver = {
.class = I2C_CLASS_HWMON,
.driver = { .driver = {
.name = "lm83", .name = "lm83",
}, },
.attach_adapter = lm83_attach_adapter, .probe = lm83_probe,
.detach_client = lm83_detach_client, .remove = lm83_remove,
.id_table = lm83_id,
.detect = lm83_detect,
.address_data = &addr_data,
}; };
/* /*
...@@ -140,7 +153,6 @@ static struct i2c_driver lm83_driver = { ...@@ -140,7 +153,6 @@ static struct i2c_driver lm83_driver = {
*/ */
struct lm83_data { struct lm83_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 */
...@@ -278,40 +290,15 @@ static const struct attribute_group lm83_group_opt = { ...@@ -278,40 +290,15 @@ static const struct attribute_group lm83_group_opt = {
* Real code * Real code
*/ */
static int lm83_attach_adapter(struct i2c_adapter *adapter) /* Return 0 if detection is successful, -ENODEV otherwise */
static int lm83_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, lm83_detect);
}
/*
* The following function does more than just detection. If detection
* succeeds, it also registers the new chip.
*/
static int lm83_detect(struct i2c_adapter *adapter, int address, int kind)
{
struct i2c_client *new_client;
struct lm83_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 lm83_data), GFP_KERNEL))) {
err = -ENOMEM;
goto exit;
}
/* The common I2C client data is placed right after the
* LM83-specific data. */
new_client = &data->client;
i2c_set_clientdata(new_client, data);
new_client->addr = address;
new_client->adapter = adapter;
new_client->driver = &lm83_driver;
new_client->flags = 0;
/* Now we do the detection and identification. A negative kind /* Now we do the detection and identification. A negative kind
* means that the driver was loaded with no force parameter * means that the driver was loaded with no force parameter
...@@ -335,8 +322,9 @@ static int lm83_detect(struct i2c_adapter *adapter, int address, int kind) ...@@ -335,8 +322,9 @@ static int lm83_detect(struct i2c_adapter *adapter, int address, int kind)
((i2c_smbus_read_byte_data(new_client, LM83_REG_R_CONFIG) ((i2c_smbus_read_byte_data(new_client, LM83_REG_R_CONFIG)
& 0x41) != 0x00)) { & 0x41) != 0x00)) {
dev_dbg(&adapter->dev, dev_dbg(&adapter->dev,
"LM83 detection failed at 0x%02x.\n", address); "LM83 detection failed at 0x%02x.\n",
goto exit_free; new_client->addr);
return -ENODEV;
} }
} }
...@@ -361,7 +349,7 @@ static int lm83_detect(struct i2c_adapter *adapter, int address, int kind) ...@@ -361,7 +349,7 @@ static int lm83_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;
} }
} }
...@@ -372,15 +360,27 @@ static int lm83_detect(struct i2c_adapter *adapter, int address, int kind) ...@@ -372,15 +360,27 @@ static int lm83_detect(struct i2c_adapter *adapter, int address, int kind)
name = "lm82"; name = "lm82";
} }
/* We can fill in the remaining client fields */ strlcpy(info->type, name, I2C_NAME_SIZE);
strlcpy(new_client->name, name, I2C_NAME_SIZE);
return 0;
}
static int lm83_probe(struct i2c_client *new_client,
const struct i2c_device_id *id)
{
struct lm83_data *data;
int err;
data = kzalloc(sizeof(struct lm83_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;
/* /*
* Register sysfs hooks * Register sysfs hooks
* The LM82 can only monitor one external diode which is * The LM82 can only monitor one external diode which is
...@@ -389,9 +389,9 @@ static int lm83_detect(struct i2c_adapter *adapter, int address, int kind) ...@@ -389,9 +389,9 @@ static int lm83_detect(struct i2c_adapter *adapter, int address, int kind)
*/ */
if ((err = sysfs_create_group(&new_client->dev.kobj, &lm83_group))) if ((err = sysfs_create_group(&new_client->dev.kobj, &lm83_group)))
goto exit_detach; goto exit_free;
if (kind == lm83) { if (id->driver_data == lm83) {
if ((err = sysfs_create_group(&new_client->dev.kobj, if ((err = sysfs_create_group(&new_client->dev.kobj,
&lm83_group_opt))) &lm83_group_opt)))
goto exit_remove_files; goto exit_remove_files;
...@@ -408,26 +408,20 @@ static int lm83_detect(struct i2c_adapter *adapter, int address, int kind) ...@@ -408,26 +408,20 @@ static int lm83_detect(struct i2c_adapter *adapter, int address, int kind)
exit_remove_files: exit_remove_files:
sysfs_remove_group(&new_client->dev.kobj, &lm83_group); sysfs_remove_group(&new_client->dev.kobj, &lm83_group);
sysfs_remove_group(&new_client->dev.kobj, &lm83_group_opt); sysfs_remove_group(&new_client->dev.kobj, &lm83_group_opt);
exit_detach:
i2c_detach_client(new_client);
exit_free: exit_free:
kfree(data); kfree(data);
exit: exit:
return err; return err;
} }
static int lm83_detach_client(struct i2c_client *client) static int lm83_remove(struct i2c_client *client)
{ {
struct lm83_data *data = i2c_get_clientdata(client); struct lm83_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, &lm83_group); sysfs_remove_group(&client->dev.kobj, &lm83_group);
sysfs_remove_group(&client->dev.kobj, &lm83_group_opt); sysfs_remove_group(&client->dev.kobj, &lm83_group_opt);
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