Skip to content

Commit 33468e7

Browse files
Jean DelvareJean Delvare
authored andcommitted
hwmon: (w83l786ng) Convert to a new-style i2c driver
The new-style w83l786ng 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: Kevin Lo <kevlo@kevlo.org>
1 parent dc18a41 commit 33468e7

File tree

1 file changed

+45
-53
lines changed

1 file changed

+45
-53
lines changed

drivers/hwmon/w83l786ng.c

Lines changed: 45 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,6 @@ DIV_TO_REG(long val)
121121
}
122122

123123
struct w83l786ng_data {
124-
struct i2c_client client;
125124
struct device *hwmon_dev;
126125
struct mutex update_lock;
127126
char valid; /* !=0 if following fields are valid */
@@ -146,18 +145,30 @@ struct w83l786ng_data {
146145
u8 tolerance[2];
147146
};
148147

149-
static int w83l786ng_attach_adapter(struct i2c_adapter *adapter);
150-
static int w83l786ng_detect(struct i2c_adapter *adapter, int address, int kind);
151-
static int w83l786ng_detach_client(struct i2c_client *client);
148+
static int w83l786ng_probe(struct i2c_client *client,
149+
const struct i2c_device_id *id);
150+
static int w83l786ng_detect(struct i2c_client *client, int kind,
151+
struct i2c_board_info *info);
152+
static int w83l786ng_remove(struct i2c_client *client);
152153
static void w83l786ng_init_client(struct i2c_client *client);
153154
static struct w83l786ng_data *w83l786ng_update_device(struct device *dev);
154155

156+
static const struct i2c_device_id w83l786ng_id[] = {
157+
{ "w83l786ng", w83l786ng },
158+
{ }
159+
};
160+
MODULE_DEVICE_TABLE(i2c, w83l786ng_id);
161+
155162
static struct i2c_driver w83l786ng_driver = {
163+
.class = I2C_CLASS_HWMON,
156164
.driver = {
157165
.name = "w83l786ng",
158166
},
159-
.attach_adapter = w83l786ng_attach_adapter,
160-
.detach_client = w83l786ng_detach_client,
167+
.probe = w83l786ng_probe,
168+
.remove = w83l786ng_remove,
169+
.id_table = w83l786ng_id,
170+
.detect = w83l786ng_detect,
171+
.address_data = &addr_data,
161172
};
162173

163174
static u8
@@ -575,42 +586,15 @@ static const struct attribute_group w83l786ng_group = {
575586
};
576587

577588
static int
578-
w83l786ng_attach_adapter(struct i2c_adapter *adapter)
589+
w83l786ng_detect(struct i2c_client *client, int kind,
590+
struct i2c_board_info *info)
579591
{
580-
if (!(adapter->class & I2C_CLASS_HWMON))
581-
return 0;
582-
return i2c_probe(adapter, &addr_data, w83l786ng_detect);
583-
}
584-
585-
static int
586-
w83l786ng_detect(struct i2c_adapter *adapter, int address, int kind)
587-
{
588-
struct i2c_client *client;
589-
struct device *dev;
590-
struct w83l786ng_data *data;
591-
int i, err = 0;
592-
u8 reg_tmp;
592+
struct i2c_adapter *adapter = client->adapter;
593593

594594
if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
595-
goto exit;
596-
}
597-
598-
/* OK. For now, we presume we have a valid client. We now create the
599-
client structure, even though we cannot fill it completely yet.
600-
But it allows us to access w83l786ng_{read,write}_value. */
601-
602-
if (!(data = kzalloc(sizeof(struct w83l786ng_data), GFP_KERNEL))) {
603-
err = -ENOMEM;
604-
goto exit;
595+
return -ENODEV;
605596
}
606597

607-
client = &data->client;
608-
dev = &client->dev;
609-
i2c_set_clientdata(client, data);
610-
client->addr = address;
611-
client->adapter = adapter;
612-
client->driver = &w83l786ng_driver;
613-
614598
/*
615599
* Now we do the remaining detection. A negative kind means that
616600
* the driver was loaded with no force parameter (default), so we
@@ -627,8 +611,8 @@ w83l786ng_detect(struct i2c_adapter *adapter, int address, int kind)
627611
W83L786NG_REG_CONFIG) & 0x80) != 0x00)) {
628612
dev_dbg(&adapter->dev,
629613
"W83L786NG detection failed at 0x%02x.\n",
630-
address);
631-
goto exit_free;
614+
client->addr);
615+
return -ENODEV;
632616
}
633617
}
634618

@@ -651,17 +635,31 @@ w83l786ng_detect(struct i2c_adapter *adapter, int address, int kind)
651635
dev_info(&adapter->dev,
652636
"Unsupported chip (man_id=0x%04X, "
653637
"chip_id=0x%02X).\n", man_id, chip_id);
654-
goto exit_free;
638+
return -ENODEV;
655639
}
656640
}
657641

658-
/* Fill in the remaining client fields and put into the global list */
659-
strlcpy(client->name, "w83l786ng", I2C_NAME_SIZE);
660-
mutex_init(&data->update_lock);
642+
strlcpy(info->type, "w83l786ng", I2C_NAME_SIZE);
661643

662-
/* Tell the I2C layer a new client has arrived */
663-
if ((err = i2c_attach_client(client)))
664-
goto exit_free;
644+
return 0;
645+
}
646+
647+
static int
648+
w83l786ng_probe(struct i2c_client *client, const struct i2c_device_id *id)
649+
{
650+
struct device *dev = &client->dev;
651+
struct w83l786ng_data *data;
652+
int i, err = 0;
653+
u8 reg_tmp;
654+
655+
data = kzalloc(sizeof(struct w83l786ng_data), GFP_KERNEL);
656+
if (!data) {
657+
err = -ENOMEM;
658+
goto exit;
659+
}
660+
661+
i2c_set_clientdata(client, data);
662+
mutex_init(&data->update_lock);
665663

666664
/* Initialize the chip */
667665
w83l786ng_init_client(client);
@@ -693,25 +691,19 @@ w83l786ng_detect(struct i2c_adapter *adapter, int address, int kind)
693691

694692
exit_remove:
695693
sysfs_remove_group(&client->dev.kobj, &w83l786ng_group);
696-
i2c_detach_client(client);
697-
exit_free:
698694
kfree(data);
699695
exit:
700696
return err;
701697
}
702698

703699
static int
704-
w83l786ng_detach_client(struct i2c_client *client)
700+
w83l786ng_remove(struct i2c_client *client)
705701
{
706702
struct w83l786ng_data *data = i2c_get_clientdata(client);
707-
int err;
708703

709704
hwmon_device_unregister(data->hwmon_dev);
710705
sysfs_remove_group(&client->dev.kobj, &w83l786ng_group);
711706

712-
if ((err = i2c_detach_client(client)))
713-
return err;
714-
715707
kfree(data);
716708

717709
return 0;

0 commit comments

Comments
 (0)