1 =================================================
2 Upgrading I2C Drivers to the new 2.6 Driver Model
3 =================================================
5 Ben Dooks <ben-linux@fluff.org>
10 This guide outlines how to alter existing Linux 2.6 client drivers from
11 the old to the new new binding methods.
14 Example old-style driver
15 ------------------------
19 struct example_state {
20 struct i2c_client client;
24 static struct i2c_driver example_driver;
26 static unsigned short ignore[] = { I2C_CLIENT_END };
27 static unsigned short normal_addr[] = { OUR_ADDR, I2C_CLIENT_END };
31 static int example_attach(struct i2c_adapter *adap, int addr, int kind)
33 struct example_state *state;
34 struct device *dev = &adap->dev; /* to use for dev_ reports */
37 state = kzalloc(sizeof(struct example_state), GFP_KERNEL);
39 dev_err(dev, "failed to create our state\n");
43 example->client.addr = addr;
44 example->client.flags = 0;
45 example->client.adapter = adap;
47 i2c_set_clientdata(&state->i2c_client, state);
48 strscpy(client->i2c_client.name, "example", sizeof(client->i2c_client.name));
50 ret = i2c_attach_client(&state->i2c_client);
52 dev_err(dev, "failed to attach client\n");
57 dev = &state->i2c_client.dev;
59 /* rest of the initialisation goes here. */
61 dev_info(dev, "example client created\n");
66 static int example_detach(struct i2c_client *client)
68 struct example_state *state = i2c_get_clientdata(client);
70 i2c_detach_client(client);
75 static int example_attach_adapter(struct i2c_adapter *adap)
77 return i2c_probe(adap, &addr_data, example_attach);
80 static struct i2c_driver example_driver = {
84 .pm = &example_pm_ops,
86 .attach_adapter = example_attach_adapter,
87 .detach_client = example_detach,
94 The new style binding model will check against a list of supported
95 devices and their associated address supplied by the code registering
96 the busses. This means that the driver .attach_adapter and
97 .detach_client methods can be removed, along with the addr_data,
100 - static struct i2c_driver example_driver;
102 - static unsigned short ignore[] = { I2C_CLIENT_END };
103 - static unsigned short normal_addr[] = { OUR_ADDR, I2C_CLIENT_END };
107 - static int example_attach_adapter(struct i2c_adapter *adap)
109 - return i2c_probe(adap, &addr_data, example_attach);
112 static struct i2c_driver example_driver = {
113 - .attach_adapter = example_attach_adapter,
114 - .detach_client = example_detach,
117 Add the probe and remove methods to the i2c_driver, as so::
119 static struct i2c_driver example_driver = {
120 + .probe = example_probe,
121 + .remove = example_remove,
124 Change the example_attach method to accept the new parameters
125 which include the i2c_client that it will be working with::
127 - static int example_attach(struct i2c_adapter *adap, int addr, int kind)
128 + static int example_probe(struct i2c_client *client,
129 + const struct i2c_device_id *id)
131 Change the name of example_attach to example_probe to align it with the
132 i2c_driver entry names. The rest of the probe routine will now need to be
133 changed as the i2c_client has already been setup for use.
135 The necessary client fields have already been setup before
136 the probe function is called, so the following client setup
139 - example->client.addr = addr;
140 - example->client.flags = 0;
141 - example->client.adapter = adap;
143 - strscpy(client->i2c_client.name, "example", sizeof(client->i2c_client.name));
145 The i2c_set_clientdata is now::
147 - i2c_set_clientdata(&state->client, state);
148 + i2c_set_clientdata(client, state);
150 The call to i2c_attach_client is no longer needed, if the probe
151 routine exits successfully, then the driver will be automatically
152 attached by the core. Change the probe routine as so::
154 - ret = i2c_attach_client(&state->i2c_client);
156 - dev_err(dev, "failed to attach client\n");
162 Remove the storage of 'struct i2c_client' from the 'struct example_state'
163 as we are provided with the i2c_client in our example_probe. Instead we
164 store a pointer to it for when it is needed.
168 struct example_state {
169 - struct i2c_client client;
170 + struct i2c_client *client;
172 the new i2c client as so::
174 - struct device *dev = &adap->dev; /* to use for dev_ reports */
175 + struct device *dev = &i2c_client->dev; /* to use for dev_ reports */
177 And remove the change after our client is attached, as the driver no
178 longer needs to register a new client structure with the core::
180 - dev = &state->i2c_client.dev;
182 In the probe routine, ensure that the new state has the client stored
185 static int example_probe(struct i2c_client *i2c_client,
186 const struct i2c_device_id *id)
188 struct example_state *state;
189 struct device *dev = &i2c_client->dev;
192 state = kzalloc(sizeof(struct example_state), GFP_KERNEL);
194 dev_err(dev, "failed to create our state\n");
198 + state->client = i2c_client;
200 Update the detach method, by changing the name to _remove and
201 to delete the i2c_detach_client call. It is possible that you
202 can also remove the ret variable as it is not needed for any
203 of the core functions.
207 - static int example_detach(struct i2c_client *client)
208 + static int example_remove(struct i2c_client *client)
210 struct example_state *state = i2c_get_clientdata(client);
212 - i2c_detach_client(client);
214 And finally ensure that we have the correct ID table for the i2c-core
215 and other utilities::
217 + struct i2c_device_id example_idtable[] = {
222 +MODULE_DEVICE_TABLE(i2c, example_idtable);
224 static struct i2c_driver example_driver = {
226 .owner = THIS_MODULE,
229 + .id_table = example_ids,
232 Our driver should now look like this::
234 struct example_state {
235 struct i2c_client *client;
239 static int example_probe(struct i2c_client *client,
240 const struct i2c_device_id *id)
242 struct example_state *state;
243 struct device *dev = &client->dev;
245 state = kzalloc(sizeof(struct example_state), GFP_KERNEL);
247 dev_err(dev, "failed to create our state\n");
251 state->client = client;
252 i2c_set_clientdata(client, state);
254 /* rest of the initialisation goes here. */
256 dev_info(dev, "example client created\n");
261 static int example_remove(struct i2c_client *client)
263 struct example_state *state = i2c_get_clientdata(client);
269 static struct i2c_device_id example_idtable[] = {
274 MODULE_DEVICE_TABLE(i2c, example_idtable);
276 static struct i2c_driver example_driver = {
278 .owner = THIS_MODULE,
280 .pm = &example_pm_ops,
282 .id_table = example_idtable,
283 .probe = example_probe,
284 .remove = example_remove,