blob: 27d29032c138b77bf342e8ec6b29d47df7729b01 [file] [log] [blame]
Mauro Carvalho Chehabccf988b2019-07-26 09:51:16 -03001=================================================
2Upgrading I2C Drivers to the new 2.6 Driver Model
3=================================================
4
5Ben Dooks <ben-linux@fluff.org>
6
7Introduction
8------------
9
10This guide outlines how to alter existing Linux 2.6 client drivers from
11the old to the new new binding methods.
12
13
14Example old-style driver
15------------------------
16
17::
18
19 struct example_state {
20 struct i2c_client client;
21 ....
22 };
23
24 static struct i2c_driver example_driver;
25
26 static unsigned short ignore[] = { I2C_CLIENT_END };
27 static unsigned short normal_addr[] = { OUR_ADDR, I2C_CLIENT_END };
28
29 I2C_CLIENT_INSMOD;
30
31 static int example_attach(struct i2c_adapter *adap, int addr, int kind)
32 {
33 struct example_state *state;
34 struct device *dev = &adap->dev; /* to use for dev_ reports */
35 int ret;
36
37 state = kzalloc(sizeof(struct example_state), GFP_KERNEL);
38 if (state == NULL) {
39 dev_err(dev, "failed to create our state\n");
40 return -ENOMEM;
41 }
42
43 example->client.addr = addr;
44 example->client.flags = 0;
45 example->client.adapter = adap;
46
47 i2c_set_clientdata(&state->i2c_client, state);
48 strscpy(client->i2c_client.name, "example", sizeof(client->i2c_client.name));
49
50 ret = i2c_attach_client(&state->i2c_client);
51 if (ret < 0) {
52 dev_err(dev, "failed to attach client\n");
53 kfree(state);
54 return ret;
55 }
56
57 dev = &state->i2c_client.dev;
58
59 /* rest of the initialisation goes here. */
60
61 dev_info(dev, "example client created\n");
62
63 return 0;
64 }
65
66 static int example_detach(struct i2c_client *client)
67 {
68 struct example_state *state = i2c_get_clientdata(client);
69
70 i2c_detach_client(client);
71 kfree(state);
72 return 0;
73 }
74
75 static int example_attach_adapter(struct i2c_adapter *adap)
76 {
77 return i2c_probe(adap, &addr_data, example_attach);
78 }
79
80 static struct i2c_driver example_driver = {
81 .driver = {
82 .owner = THIS_MODULE,
83 .name = "example",
84 .pm = &example_pm_ops,
85 },
86 .attach_adapter = example_attach_adapter,
87 .detach_client = example_detach,
88 };
89
90
91Updating the client
92-------------------
93
94The new style binding model will check against a list of supported
95devices and their associated address supplied by the code registering
96the busses. This means that the driver .attach_adapter and
97.detach_client methods can be removed, along with the addr_data,
98as follows::
99
100 - static struct i2c_driver example_driver;
101
102 - static unsigned short ignore[] = { I2C_CLIENT_END };
103 - static unsigned short normal_addr[] = { OUR_ADDR, I2C_CLIENT_END };
104
105 - I2C_CLIENT_INSMOD;
106
107 - static int example_attach_adapter(struct i2c_adapter *adap)
108 - {
109 - return i2c_probe(adap, &addr_data, example_attach);
110 - }
111
112 static struct i2c_driver example_driver = {
113 - .attach_adapter = example_attach_adapter,
114 - .detach_client = example_detach,
115 }
116
117Add the probe and remove methods to the i2c_driver, as so::
118
119 static struct i2c_driver example_driver = {
120 + .probe = example_probe,
121 + .remove = example_remove,
122 }
123
124Change the example_attach method to accept the new parameters
125which include the i2c_client that it will be working with::
126
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)
130
131Change the name of example_attach to example_probe to align it with the
132i2c_driver entry names. The rest of the probe routine will now need to be
133changed as the i2c_client has already been setup for use.
134
135The necessary client fields have already been setup before
136the probe function is called, so the following client setup
137can be removed::
138
139 - example->client.addr = addr;
140 - example->client.flags = 0;
141 - example->client.adapter = adap;
142 -
143 - strscpy(client->i2c_client.name, "example", sizeof(client->i2c_client.name));
144
145The i2c_set_clientdata is now::
146
147 - i2c_set_clientdata(&state->client, state);
148 + i2c_set_clientdata(client, state);
149
150The call to i2c_attach_client is no longer needed, if the probe
151routine exits successfully, then the driver will be automatically
152attached by the core. Change the probe routine as so::
153
154 - ret = i2c_attach_client(&state->i2c_client);
155 - if (ret < 0) {
156 - dev_err(dev, "failed to attach client\n");
157 - kfree(state);
158 - return ret;
159 - }
160
161
162Remove the storage of 'struct i2c_client' from the 'struct example_state'
163as we are provided with the i2c_client in our example_probe. Instead we
164store a pointer to it for when it is needed.
165
166::
167
168 struct example_state {
169 - struct i2c_client client;
170 + struct i2c_client *client;
171
172the new i2c client as so::
173
174 - struct device *dev = &adap->dev; /* to use for dev_ reports */
175 + struct device *dev = &i2c_client->dev; /* to use for dev_ reports */
176
177And remove the change after our client is attached, as the driver no
178longer needs to register a new client structure with the core::
179
180 - dev = &state->i2c_client.dev;
181
182In the probe routine, ensure that the new state has the client stored
183in it::
184
185 static int example_probe(struct i2c_client *i2c_client,
186 const struct i2c_device_id *id)
187 {
188 struct example_state *state;
189 struct device *dev = &i2c_client->dev;
190 int ret;
191
192 state = kzalloc(sizeof(struct example_state), GFP_KERNEL);
193 if (state == NULL) {
194 dev_err(dev, "failed to create our state\n");
195 return -ENOMEM;
196 }
197
198 + state->client = i2c_client;
199
200Update the detach method, by changing the name to _remove and
201to delete the i2c_detach_client call. It is possible that you
202can also remove the ret variable as it is not needed for any
203of the core functions.
204
205::
206
207 - static int example_detach(struct i2c_client *client)
208 + static int example_remove(struct i2c_client *client)
209 {
210 struct example_state *state = i2c_get_clientdata(client);
211
212 - i2c_detach_client(client);
213
214And finally ensure that we have the correct ID table for the i2c-core
215and other utilities::
216
217 + struct i2c_device_id example_idtable[] = {
218 + { "example", 0 },
219 + { }
220 +};
221 +
222 +MODULE_DEVICE_TABLE(i2c, example_idtable);
223
224 static struct i2c_driver example_driver = {
225 .driver = {
226 .owner = THIS_MODULE,
227 .name = "example",
228 },
229 + .id_table = example_ids,
230
231
232Our driver should now look like this::
233
234 struct example_state {
235 struct i2c_client *client;
236 ....
237 };
238
239 static int example_probe(struct i2c_client *client,
240 const struct i2c_device_id *id)
241 {
242 struct example_state *state;
243 struct device *dev = &client->dev;
244
245 state = kzalloc(sizeof(struct example_state), GFP_KERNEL);
246 if (state == NULL) {
247 dev_err(dev, "failed to create our state\n");
248 return -ENOMEM;
249 }
250
251 state->client = client;
252 i2c_set_clientdata(client, state);
253
254 /* rest of the initialisation goes here. */
255
256 dev_info(dev, "example client created\n");
257
258 return 0;
259 }
260
261 static int example_remove(struct i2c_client *client)
262 {
263 struct example_state *state = i2c_get_clientdata(client);
264
265 kfree(state);
266 return 0;
267 }
268
269 static struct i2c_device_id example_idtable[] = {
270 { "example", 0 },
271 { }
272 };
273
274 MODULE_DEVICE_TABLE(i2c, example_idtable);
275
276 static struct i2c_driver example_driver = {
277 .driver = {
278 .owner = THIS_MODULE,
279 .name = "example",
280 .pm = &example_pm_ops,
281 },
282 .id_table = example_idtable,
283 .probe = example_probe,
284 .remove = example_remove,
285 };