2019-07-26 20:51:16 +08:00
|
|
|
=================================================
|
2008-07-28 19:04:08 +08:00
|
|
|
Upgrading I2C Drivers to the new 2.6 Driver Model
|
|
|
|
=================================================
|
|
|
|
|
|
|
|
Ben Dooks <ben-linux@fluff.org>
|
|
|
|
|
|
|
|
Introduction
|
|
|
|
------------
|
|
|
|
|
|
|
|
This guide outlines how to alter existing Linux 2.6 client drivers from
|
|
|
|
the old to the new new binding methods.
|
|
|
|
|
|
|
|
|
|
|
|
Example old-style driver
|
|
|
|
------------------------
|
|
|
|
|
2019-07-26 20:51:16 +08:00
|
|
|
::
|
2008-07-28 19:04:08 +08:00
|
|
|
|
2019-07-26 20:51:16 +08:00
|
|
|
struct example_state {
|
2008-07-28 19:04:08 +08:00
|
|
|
struct i2c_client client;
|
|
|
|
....
|
2019-07-26 20:51:16 +08:00
|
|
|
};
|
2008-07-28 19:04:08 +08:00
|
|
|
|
2019-07-26 20:51:16 +08:00
|
|
|
static struct i2c_driver example_driver;
|
2008-07-28 19:04:08 +08:00
|
|
|
|
2019-07-26 20:51:16 +08:00
|
|
|
static unsigned short ignore[] = { I2C_CLIENT_END };
|
|
|
|
static unsigned short normal_addr[] = { OUR_ADDR, I2C_CLIENT_END };
|
2008-07-28 19:04:08 +08:00
|
|
|
|
2019-07-26 20:51:16 +08:00
|
|
|
I2C_CLIENT_INSMOD;
|
2008-07-28 19:04:08 +08:00
|
|
|
|
2019-07-26 20:51:16 +08:00
|
|
|
static int example_attach(struct i2c_adapter *adap, int addr, int kind)
|
|
|
|
{
|
2008-07-28 19:04:08 +08:00
|
|
|
struct example_state *state;
|
|
|
|
struct device *dev = &adap->dev; /* to use for dev_ reports */
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
state = kzalloc(sizeof(struct example_state), GFP_KERNEL);
|
|
|
|
if (state == NULL) {
|
|
|
|
dev_err(dev, "failed to create our state\n");
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
example->client.addr = addr;
|
|
|
|
example->client.flags = 0;
|
|
|
|
example->client.adapter = adap;
|
|
|
|
|
|
|
|
i2c_set_clientdata(&state->i2c_client, state);
|
2019-06-14 00:25:48 +08:00
|
|
|
strscpy(client->i2c_client.name, "example", sizeof(client->i2c_client.name));
|
2008-07-28 19:04:08 +08:00
|
|
|
|
|
|
|
ret = i2c_attach_client(&state->i2c_client);
|
|
|
|
if (ret < 0) {
|
|
|
|
dev_err(dev, "failed to attach client\n");
|
|
|
|
kfree(state);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
dev = &state->i2c_client.dev;
|
|
|
|
|
|
|
|
/* rest of the initialisation goes here. */
|
|
|
|
|
|
|
|
dev_info(dev, "example client created\n");
|
|
|
|
|
|
|
|
return 0;
|
2019-07-26 20:51:16 +08:00
|
|
|
}
|
2008-07-28 19:04:08 +08:00
|
|
|
|
2019-07-26 20:51:16 +08:00
|
|
|
static int example_detach(struct i2c_client *client)
|
|
|
|
{
|
2008-07-28 19:04:08 +08:00
|
|
|
struct example_state *state = i2c_get_clientdata(client);
|
|
|
|
|
|
|
|
i2c_detach_client(client);
|
|
|
|
kfree(state);
|
|
|
|
return 0;
|
2019-07-26 20:51:16 +08:00
|
|
|
}
|
2008-07-28 19:04:08 +08:00
|
|
|
|
2019-07-26 20:51:16 +08:00
|
|
|
static int example_attach_adapter(struct i2c_adapter *adap)
|
|
|
|
{
|
2008-07-28 19:04:08 +08:00
|
|
|
return i2c_probe(adap, &addr_data, example_attach);
|
2019-07-26 20:51:16 +08:00
|
|
|
}
|
2008-07-28 19:04:08 +08:00
|
|
|
|
2019-07-26 20:51:16 +08:00
|
|
|
static struct i2c_driver example_driver = {
|
|
|
|
.driver = {
|
2008-07-28 19:04:08 +08:00
|
|
|
.owner = THIS_MODULE,
|
|
|
|
.name = "example",
|
2014-12-01 00:52:31 +08:00
|
|
|
.pm = &example_pm_ops,
|
2008-07-28 19:04:08 +08:00
|
|
|
},
|
|
|
|
.attach_adapter = example_attach_adapter,
|
2011-03-20 21:50:52 +08:00
|
|
|
.detach_client = example_detach,
|
2019-07-26 20:51:16 +08:00
|
|
|
};
|
2008-07-28 19:04:08 +08:00
|
|
|
|
|
|
|
|
|
|
|
Updating the client
|
|
|
|
-------------------
|
|
|
|
|
|
|
|
The new style binding model will check against a list of supported
|
|
|
|
devices and their associated address supplied by the code registering
|
|
|
|
the busses. This means that the driver .attach_adapter and
|
2011-03-20 21:50:52 +08:00
|
|
|
.detach_client methods can be removed, along with the addr_data,
|
2019-07-26 20:51:16 +08:00
|
|
|
as follows::
|
2008-07-28 19:04:08 +08:00
|
|
|
|
2019-07-26 20:51:16 +08:00
|
|
|
- static struct i2c_driver example_driver;
|
2008-07-28 19:04:08 +08:00
|
|
|
|
2019-07-26 20:51:16 +08:00
|
|
|
- static unsigned short ignore[] = { I2C_CLIENT_END };
|
|
|
|
- static unsigned short normal_addr[] = { OUR_ADDR, I2C_CLIENT_END };
|
2008-07-28 19:04:08 +08:00
|
|
|
|
2019-07-26 20:51:16 +08:00
|
|
|
- I2C_CLIENT_INSMOD;
|
2008-07-28 19:04:08 +08:00
|
|
|
|
2019-07-26 20:51:16 +08:00
|
|
|
- static int example_attach_adapter(struct i2c_adapter *adap)
|
|
|
|
- {
|
|
|
|
- return i2c_probe(adap, &addr_data, example_attach);
|
|
|
|
- }
|
2008-07-28 19:04:08 +08:00
|
|
|
|
2019-07-26 20:51:16 +08:00
|
|
|
static struct i2c_driver example_driver = {
|
|
|
|
- .attach_adapter = example_attach_adapter,
|
|
|
|
- .detach_client = example_detach,
|
|
|
|
}
|
2008-07-28 19:04:08 +08:00
|
|
|
|
2019-07-26 20:51:16 +08:00
|
|
|
Add the probe and remove methods to the i2c_driver, as so::
|
2008-07-28 19:04:08 +08:00
|
|
|
|
2019-07-26 20:51:16 +08:00
|
|
|
static struct i2c_driver example_driver = {
|
|
|
|
+ .probe = example_probe,
|
|
|
|
+ .remove = example_remove,
|
|
|
|
}
|
2008-07-28 19:04:08 +08:00
|
|
|
|
|
|
|
Change the example_attach method to accept the new parameters
|
2019-07-26 20:51:16 +08:00
|
|
|
which include the i2c_client that it will be working with::
|
2008-07-28 19:04:08 +08:00
|
|
|
|
2019-07-26 20:51:16 +08:00
|
|
|
- static int example_attach(struct i2c_adapter *adap, int addr, int kind)
|
|
|
|
+ static int example_probe(struct i2c_client *client,
|
|
|
|
+ const struct i2c_device_id *id)
|
2008-07-28 19:04:08 +08:00
|
|
|
|
|
|
|
Change the name of example_attach to example_probe to align it with the
|
|
|
|
i2c_driver entry names. The rest of the probe routine will now need to be
|
|
|
|
changed as the i2c_client has already been setup for use.
|
|
|
|
|
|
|
|
The necessary client fields have already been setup before
|
|
|
|
the probe function is called, so the following client setup
|
2019-07-26 20:51:16 +08:00
|
|
|
can be removed::
|
2008-07-28 19:04:08 +08:00
|
|
|
|
2019-07-26 20:51:16 +08:00
|
|
|
- example->client.addr = addr;
|
|
|
|
- example->client.flags = 0;
|
|
|
|
- example->client.adapter = adap;
|
|
|
|
-
|
|
|
|
- strscpy(client->i2c_client.name, "example", sizeof(client->i2c_client.name));
|
2008-07-28 19:04:08 +08:00
|
|
|
|
2019-07-26 20:51:16 +08:00
|
|
|
The i2c_set_clientdata is now::
|
2008-07-28 19:04:08 +08:00
|
|
|
|
2019-07-26 20:51:16 +08:00
|
|
|
- i2c_set_clientdata(&state->client, state);
|
|
|
|
+ i2c_set_clientdata(client, state);
|
2008-07-28 19:04:08 +08:00
|
|
|
|
|
|
|
The call to i2c_attach_client is no longer needed, if the probe
|
|
|
|
routine exits successfully, then the driver will be automatically
|
2019-07-26 20:51:16 +08:00
|
|
|
attached by the core. Change the probe routine as so::
|
2008-07-28 19:04:08 +08:00
|
|
|
|
2019-07-26 20:51:16 +08:00
|
|
|
- ret = i2c_attach_client(&state->i2c_client);
|
|
|
|
- if (ret < 0) {
|
|
|
|
- dev_err(dev, "failed to attach client\n");
|
|
|
|
- kfree(state);
|
|
|
|
- return ret;
|
|
|
|
- }
|
2008-07-28 19:04:08 +08:00
|
|
|
|
|
|
|
|
|
|
|
Remove the storage of 'struct i2c_client' from the 'struct example_state'
|
|
|
|
as we are provided with the i2c_client in our example_probe. Instead we
|
|
|
|
store a pointer to it for when it is needed.
|
|
|
|
|
2019-07-26 20:51:16 +08:00
|
|
|
::
|
|
|
|
|
|
|
|
struct example_state {
|
|
|
|
- struct i2c_client client;
|
|
|
|
+ struct i2c_client *client;
|
2008-07-28 19:04:08 +08:00
|
|
|
|
2019-07-26 20:51:16 +08:00
|
|
|
the new i2c client as so::
|
2008-07-28 19:04:08 +08:00
|
|
|
|
2019-07-26 20:51:16 +08:00
|
|
|
- struct device *dev = &adap->dev; /* to use for dev_ reports */
|
|
|
|
+ struct device *dev = &i2c_client->dev; /* to use for dev_ reports */
|
2008-07-28 19:04:08 +08:00
|
|
|
|
|
|
|
And remove the change after our client is attached, as the driver no
|
2019-07-26 20:51:16 +08:00
|
|
|
longer needs to register a new client structure with the core::
|
2008-07-28 19:04:08 +08:00
|
|
|
|
2019-07-26 20:51:16 +08:00
|
|
|
- dev = &state->i2c_client.dev;
|
2008-07-28 19:04:08 +08:00
|
|
|
|
|
|
|
In the probe routine, ensure that the new state has the client stored
|
2019-07-26 20:51:16 +08:00
|
|
|
in it::
|
2008-07-28 19:04:08 +08:00
|
|
|
|
2019-07-26 20:51:16 +08:00
|
|
|
static int example_probe(struct i2c_client *i2c_client,
|
2008-07-28 19:04:08 +08:00
|
|
|
const struct i2c_device_id *id)
|
2019-07-26 20:51:16 +08:00
|
|
|
{
|
2008-07-28 19:04:08 +08:00
|
|
|
struct example_state *state;
|
2019-07-26 20:51:16 +08:00
|
|
|
struct device *dev = &i2c_client->dev;
|
2008-07-28 19:04:08 +08:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
state = kzalloc(sizeof(struct example_state), GFP_KERNEL);
|
|
|
|
if (state == NULL) {
|
|
|
|
dev_err(dev, "failed to create our state\n");
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
2019-07-26 20:51:16 +08:00
|
|
|
+ state->client = i2c_client;
|
2008-07-28 19:04:08 +08:00
|
|
|
|
|
|
|
Update the detach method, by changing the name to _remove and
|
|
|
|
to delete the i2c_detach_client call. It is possible that you
|
2013-07-18 00:29:12 +08:00
|
|
|
can also remove the ret variable as it is not needed for any
|
|
|
|
of the core functions.
|
2008-07-28 19:04:08 +08:00
|
|
|
|
2019-07-26 20:51:16 +08:00
|
|
|
::
|
|
|
|
|
|
|
|
- static int example_detach(struct i2c_client *client)
|
|
|
|
+ static int example_remove(struct i2c_client *client)
|
|
|
|
{
|
2008-07-28 19:04:08 +08:00
|
|
|
struct example_state *state = i2c_get_clientdata(client);
|
|
|
|
|
2019-07-26 20:51:16 +08:00
|
|
|
- i2c_detach_client(client);
|
2008-07-28 19:04:08 +08:00
|
|
|
|
|
|
|
And finally ensure that we have the correct ID table for the i2c-core
|
2019-07-26 20:51:16 +08:00
|
|
|
and other utilities::
|
2008-07-28 19:04:08 +08:00
|
|
|
|
2019-07-26 20:51:16 +08:00
|
|
|
+ struct i2c_device_id example_idtable[] = {
|
|
|
|
+ { "example", 0 },
|
|
|
|
+ { }
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+MODULE_DEVICE_TABLE(i2c, example_idtable);
|
2008-07-28 19:04:08 +08:00
|
|
|
|
2019-07-26 20:51:16 +08:00
|
|
|
static struct i2c_driver example_driver = {
|
|
|
|
.driver = {
|
2008-07-28 19:04:08 +08:00
|
|
|
.owner = THIS_MODULE,
|
|
|
|
.name = "example",
|
|
|
|
},
|
2019-07-26 20:51:16 +08:00
|
|
|
+ .id_table = example_ids,
|
2008-07-28 19:04:08 +08:00
|
|
|
|
|
|
|
|
2019-07-26 20:51:16 +08:00
|
|
|
Our driver should now look like this::
|
2008-07-28 19:04:08 +08:00
|
|
|
|
2019-07-26 20:51:16 +08:00
|
|
|
struct example_state {
|
2008-07-28 19:04:08 +08:00
|
|
|
struct i2c_client *client;
|
|
|
|
....
|
2019-07-26 20:51:16 +08:00
|
|
|
};
|
2008-07-28 19:04:08 +08:00
|
|
|
|
2019-07-26 20:51:16 +08:00
|
|
|
static int example_probe(struct i2c_client *client,
|
|
|
|
const struct i2c_device_id *id)
|
|
|
|
{
|
2008-07-28 19:04:08 +08:00
|
|
|
struct example_state *state;
|
|
|
|
struct device *dev = &client->dev;
|
|
|
|
|
|
|
|
state = kzalloc(sizeof(struct example_state), GFP_KERNEL);
|
|
|
|
if (state == NULL) {
|
|
|
|
dev_err(dev, "failed to create our state\n");
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
state->client = client;
|
|
|
|
i2c_set_clientdata(client, state);
|
|
|
|
|
|
|
|
/* rest of the initialisation goes here. */
|
|
|
|
|
|
|
|
dev_info(dev, "example client created\n");
|
|
|
|
|
|
|
|
return 0;
|
2019-07-26 20:51:16 +08:00
|
|
|
}
|
2008-07-28 19:04:08 +08:00
|
|
|
|
2019-07-26 20:51:16 +08:00
|
|
|
static int example_remove(struct i2c_client *client)
|
|
|
|
{
|
2008-07-28 19:04:08 +08:00
|
|
|
struct example_state *state = i2c_get_clientdata(client);
|
|
|
|
|
|
|
|
kfree(state);
|
|
|
|
return 0;
|
2019-07-26 20:51:16 +08:00
|
|
|
}
|
2008-07-28 19:04:08 +08:00
|
|
|
|
2019-07-26 20:51:16 +08:00
|
|
|
static struct i2c_device_id example_idtable[] = {
|
2008-07-28 19:04:08 +08:00
|
|
|
{ "example", 0 },
|
|
|
|
{ }
|
2019-07-26 20:51:16 +08:00
|
|
|
};
|
2008-07-28 19:04:08 +08:00
|
|
|
|
2019-07-26 20:51:16 +08:00
|
|
|
MODULE_DEVICE_TABLE(i2c, example_idtable);
|
2008-07-28 19:04:08 +08:00
|
|
|
|
2019-07-26 20:51:16 +08:00
|
|
|
static struct i2c_driver example_driver = {
|
|
|
|
.driver = {
|
2008-07-28 19:04:08 +08:00
|
|
|
.owner = THIS_MODULE,
|
|
|
|
.name = "example",
|
2014-12-01 00:52:31 +08:00
|
|
|
.pm = &example_pm_ops,
|
2008-07-28 19:04:08 +08:00
|
|
|
},
|
|
|
|
.id_table = example_idtable,
|
|
|
|
.probe = example_probe,
|
2011-03-20 21:50:52 +08:00
|
|
|
.remove = example_remove,
|
2019-07-26 20:51:16 +08:00
|
|
|
};
|