]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* i2c-core.c - a device driver for the iic-bus interface */ |
2 | /* ------------------------------------------------------------------------- */ | |
3 | /* Copyright (C) 1995-99 Simon G. Vogl | |
4 | ||
5 | This program is free software; you can redistribute it and/or modify | |
6 | it under the terms of the GNU General Public License as published by | |
7 | the Free Software Foundation; either version 2 of the License, or | |
8 | (at your option) any later version. | |
9 | ||
10 | This program is distributed in the hope that it will be useful, | |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | GNU General Public License for more details. | |
14 | ||
15 | You should have received a copy of the GNU General Public License | |
16 | along with this program; if not, write to the Free Software | |
17 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ | |
18 | /* ------------------------------------------------------------------------- */ | |
19 | ||
96de0e25 | 20 | /* With some changes from Kyösti Mälkki <kmalkki@cc.hut.fi>. |
1da177e4 | 21 | All SMBus-related things are written by Frodo Looijaard <frodol@dds.nl> |
421ef47b JD |
22 | SMBus 2.0 support by Mark Studebaker <mdsxyz123@yahoo.com> and |
23 | Jean Delvare <khali@linux-fr.org> */ | |
1da177e4 | 24 | |
1da177e4 LT |
25 | #include <linux/module.h> |
26 | #include <linux/kernel.h> | |
27 | #include <linux/errno.h> | |
28 | #include <linux/slab.h> | |
29 | #include <linux/i2c.h> | |
30 | #include <linux/init.h> | |
31 | #include <linux/idr.h> | |
b3585e4f | 32 | #include <linux/mutex.h> |
b8d6f45b | 33 | #include <linux/completion.h> |
cea443a8 MR |
34 | #include <linux/hardirq.h> |
35 | #include <linux/irqflags.h> | |
f18c41da | 36 | #include <linux/rwsem.h> |
6de468ae | 37 | #include <linux/pm_runtime.h> |
1da177e4 LT |
38 | #include <asm/uaccess.h> |
39 | ||
9c1600ed DB |
40 | #include "i2c-core.h" |
41 | ||
1da177e4 | 42 | |
6629dcff | 43 | /* core_lock protects i2c_adapter_idr, and guarantees |
35fc37f8 JD |
44 | that device detection, deletion of detected devices, and attach_adapter |
45 | and detach_adapter calls are serialized */ | |
caada32a | 46 | static DEFINE_MUTEX(core_lock); |
1da177e4 LT |
47 | static DEFINE_IDR(i2c_adapter_idr); |
48 | ||
4f8cf824 | 49 | static struct device_type i2c_client_type; |
f8a227e8 | 50 | static int i2c_check_addr(struct i2c_adapter *adapter, int addr); |
4735c98f | 51 | static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver); |
f37dd80a DB |
52 | |
53 | /* ------------------------------------------------------------------------- */ | |
54 | ||
d2653e92 JD |
55 | static const struct i2c_device_id *i2c_match_id(const struct i2c_device_id *id, |
56 | const struct i2c_client *client) | |
57 | { | |
58 | while (id->name[0]) { | |
59 | if (strcmp(client->name, id->name) == 0) | |
60 | return id; | |
61 | id++; | |
62 | } | |
63 | return NULL; | |
64 | } | |
65 | ||
1da177e4 LT |
66 | static int i2c_device_match(struct device *dev, struct device_driver *drv) |
67 | { | |
51298d12 JD |
68 | struct i2c_client *client = i2c_verify_client(dev); |
69 | struct i2c_driver *driver; | |
70 | ||
71 | if (!client) | |
72 | return 0; | |
7b4fbc50 | 73 | |
51298d12 | 74 | driver = to_i2c_driver(drv); |
d2653e92 JD |
75 | /* match on an id table if there is one */ |
76 | if (driver->id_table) | |
77 | return i2c_match_id(driver->id_table, client) != NULL; | |
78 | ||
eb8a7908 | 79 | return 0; |
1da177e4 LT |
80 | } |
81 | ||
7b4fbc50 DB |
82 | #ifdef CONFIG_HOTPLUG |
83 | ||
84 | /* uevent helps with hotplug: modprobe -q $(MODALIAS) */ | |
7eff2e7a | 85 | static int i2c_device_uevent(struct device *dev, struct kobj_uevent_env *env) |
7b4fbc50 DB |
86 | { |
87 | struct i2c_client *client = to_i2c_client(dev); | |
7b4fbc50 | 88 | |
eb8a7908 JD |
89 | if (add_uevent_var(env, "MODALIAS=%s%s", |
90 | I2C_MODULE_PREFIX, client->name)) | |
91 | return -ENOMEM; | |
7b4fbc50 DB |
92 | dev_dbg(dev, "uevent\n"); |
93 | return 0; | |
94 | } | |
95 | ||
96 | #else | |
97 | #define i2c_device_uevent NULL | |
98 | #endif /* CONFIG_HOTPLUG */ | |
99 | ||
f37dd80a | 100 | static int i2c_device_probe(struct device *dev) |
1da177e4 | 101 | { |
51298d12 JD |
102 | struct i2c_client *client = i2c_verify_client(dev); |
103 | struct i2c_driver *driver; | |
50c3304a | 104 | int status; |
7b4fbc50 | 105 | |
51298d12 JD |
106 | if (!client) |
107 | return 0; | |
108 | ||
109 | driver = to_i2c_driver(dev->driver); | |
e0457442 | 110 | if (!driver->probe || !driver->id_table) |
7b4fbc50 DB |
111 | return -ENODEV; |
112 | client->driver = driver; | |
ee35425c MP |
113 | if (!device_can_wakeup(&client->dev)) |
114 | device_init_wakeup(&client->dev, | |
115 | client->flags & I2C_CLIENT_WAKE); | |
7b4fbc50 | 116 | dev_dbg(dev, "probe\n"); |
d2653e92 | 117 | |
e0457442 | 118 | status = driver->probe(client, i2c_match_id(driver->id_table, client)); |
e4a7b9b0 | 119 | if (status) { |
50c3304a | 120 | client->driver = NULL; |
e4a7b9b0 WS |
121 | i2c_set_clientdata(client, NULL); |
122 | } | |
50c3304a | 123 | return status; |
f37dd80a | 124 | } |
1da177e4 | 125 | |
f37dd80a DB |
126 | static int i2c_device_remove(struct device *dev) |
127 | { | |
51298d12 | 128 | struct i2c_client *client = i2c_verify_client(dev); |
a1d9e6e4 DB |
129 | struct i2c_driver *driver; |
130 | int status; | |
131 | ||
51298d12 | 132 | if (!client || !dev->driver) |
a1d9e6e4 DB |
133 | return 0; |
134 | ||
135 | driver = to_i2c_driver(dev->driver); | |
136 | if (driver->remove) { | |
137 | dev_dbg(dev, "remove\n"); | |
138 | status = driver->remove(client); | |
139 | } else { | |
140 | dev->driver = NULL; | |
141 | status = 0; | |
142 | } | |
e4a7b9b0 | 143 | if (status == 0) { |
a1d9e6e4 | 144 | client->driver = NULL; |
e4a7b9b0 WS |
145 | i2c_set_clientdata(client, NULL); |
146 | } | |
a1d9e6e4 | 147 | return status; |
1da177e4 LT |
148 | } |
149 | ||
f37dd80a | 150 | static void i2c_device_shutdown(struct device *dev) |
1da177e4 | 151 | { |
51298d12 | 152 | struct i2c_client *client = i2c_verify_client(dev); |
f37dd80a DB |
153 | struct i2c_driver *driver; |
154 | ||
51298d12 | 155 | if (!client || !dev->driver) |
f37dd80a DB |
156 | return; |
157 | driver = to_i2c_driver(dev->driver); | |
158 | if (driver->shutdown) | |
51298d12 | 159 | driver->shutdown(client); |
1da177e4 LT |
160 | } |
161 | ||
54067ee2 | 162 | #ifdef CONFIG_SUSPEND |
163 | static int i2c_device_pm_suspend(struct device *dev) | |
164 | { | |
165 | const struct dev_pm_ops *pm; | |
166 | ||
167 | if (!dev->driver) | |
168 | return 0; | |
169 | pm = dev->driver->pm; | |
170 | if (!pm || !pm->suspend) | |
171 | return 0; | |
172 | return pm->suspend(dev); | |
173 | } | |
174 | ||
175 | static int i2c_device_pm_resume(struct device *dev) | |
176 | { | |
177 | const struct dev_pm_ops *pm; | |
178 | ||
179 | if (!dev->driver) | |
180 | return 0; | |
181 | pm = dev->driver->pm; | |
182 | if (!pm || !pm->resume) | |
183 | return 0; | |
184 | return pm->resume(dev); | |
185 | } | |
186 | #else | |
187 | #define i2c_device_pm_suspend NULL | |
188 | #define i2c_device_pm_resume NULL | |
189 | #endif | |
190 | ||
6de468ae MB |
191 | #ifdef CONFIG_PM_RUNTIME |
192 | static int i2c_device_runtime_suspend(struct device *dev) | |
193 | { | |
194 | const struct dev_pm_ops *pm; | |
195 | ||
196 | if (!dev->driver) | |
197 | return 0; | |
198 | pm = dev->driver->pm; | |
199 | if (!pm || !pm->runtime_suspend) | |
200 | return 0; | |
201 | return pm->runtime_suspend(dev); | |
202 | } | |
203 | ||
204 | static int i2c_device_runtime_resume(struct device *dev) | |
205 | { | |
206 | const struct dev_pm_ops *pm; | |
207 | ||
208 | if (!dev->driver) | |
209 | return 0; | |
210 | pm = dev->driver->pm; | |
211 | if (!pm || !pm->runtime_resume) | |
212 | return 0; | |
213 | return pm->runtime_resume(dev); | |
214 | } | |
215 | ||
216 | static int i2c_device_runtime_idle(struct device *dev) | |
217 | { | |
218 | const struct dev_pm_ops *pm = NULL; | |
219 | int ret; | |
220 | ||
221 | if (dev->driver) | |
222 | pm = dev->driver->pm; | |
223 | if (pm && pm->runtime_idle) { | |
224 | ret = pm->runtime_idle(dev); | |
225 | if (ret) | |
226 | return ret; | |
227 | } | |
228 | ||
229 | return pm_runtime_suspend(dev); | |
230 | } | |
231 | #else | |
232 | #define i2c_device_runtime_suspend NULL | |
233 | #define i2c_device_runtime_resume NULL | |
234 | #define i2c_device_runtime_idle NULL | |
235 | #endif | |
236 | ||
09b8ce0a | 237 | static int i2c_device_suspend(struct device *dev, pm_message_t mesg) |
1da177e4 | 238 | { |
51298d12 | 239 | struct i2c_client *client = i2c_verify_client(dev); |
f37dd80a DB |
240 | struct i2c_driver *driver; |
241 | ||
51298d12 | 242 | if (!client || !dev->driver) |
f37dd80a DB |
243 | return 0; |
244 | driver = to_i2c_driver(dev->driver); | |
245 | if (!driver->suspend) | |
246 | return 0; | |
51298d12 | 247 | return driver->suspend(client, mesg); |
1da177e4 LT |
248 | } |
249 | ||
09b8ce0a | 250 | static int i2c_device_resume(struct device *dev) |
1da177e4 | 251 | { |
51298d12 | 252 | struct i2c_client *client = i2c_verify_client(dev); |
f37dd80a DB |
253 | struct i2c_driver *driver; |
254 | ||
51298d12 | 255 | if (!client || !dev->driver) |
f37dd80a DB |
256 | return 0; |
257 | driver = to_i2c_driver(dev->driver); | |
258 | if (!driver->resume) | |
259 | return 0; | |
51298d12 | 260 | return driver->resume(client); |
1da177e4 LT |
261 | } |
262 | ||
9c1600ed DB |
263 | static void i2c_client_dev_release(struct device *dev) |
264 | { | |
265 | kfree(to_i2c_client(dev)); | |
266 | } | |
267 | ||
09b8ce0a | 268 | static ssize_t |
4f8cf824 | 269 | show_name(struct device *dev, struct device_attribute *attr, char *buf) |
7b4fbc50 | 270 | { |
4f8cf824 JD |
271 | return sprintf(buf, "%s\n", dev->type == &i2c_client_type ? |
272 | to_i2c_client(dev)->name : to_i2c_adapter(dev)->name); | |
7b4fbc50 DB |
273 | } |
274 | ||
09b8ce0a ZX |
275 | static ssize_t |
276 | show_modalias(struct device *dev, struct device_attribute *attr, char *buf) | |
7b4fbc50 DB |
277 | { |
278 | struct i2c_client *client = to_i2c_client(dev); | |
eb8a7908 | 279 | return sprintf(buf, "%s%s\n", I2C_MODULE_PREFIX, client->name); |
7b4fbc50 DB |
280 | } |
281 | ||
4f8cf824 | 282 | static DEVICE_ATTR(name, S_IRUGO, show_name, NULL); |
51298d12 JD |
283 | static DEVICE_ATTR(modalias, S_IRUGO, show_modalias, NULL); |
284 | ||
285 | static struct attribute *i2c_dev_attrs[] = { | |
286 | &dev_attr_name.attr, | |
7b4fbc50 | 287 | /* modalias helps coldplug: modprobe $(cat .../modalias) */ |
51298d12 JD |
288 | &dev_attr_modalias.attr, |
289 | NULL | |
290 | }; | |
291 | ||
292 | static struct attribute_group i2c_dev_attr_group = { | |
293 | .attrs = i2c_dev_attrs, | |
294 | }; | |
295 | ||
296 | static const struct attribute_group *i2c_dev_attr_groups[] = { | |
297 | &i2c_dev_attr_group, | |
298 | NULL | |
7b4fbc50 DB |
299 | }; |
300 | ||
0b2c3688 | 301 | static const struct dev_pm_ops i2c_device_pm_ops = { |
54067ee2 | 302 | .suspend = i2c_device_pm_suspend, |
303 | .resume = i2c_device_pm_resume, | |
6de468ae MB |
304 | .runtime_suspend = i2c_device_runtime_suspend, |
305 | .runtime_resume = i2c_device_runtime_resume, | |
306 | .runtime_idle = i2c_device_runtime_idle, | |
54067ee2 | 307 | }; |
308 | ||
e9ca9eb9 | 309 | struct bus_type i2c_bus_type = { |
f37dd80a DB |
310 | .name = "i2c", |
311 | .match = i2c_device_match, | |
312 | .probe = i2c_device_probe, | |
313 | .remove = i2c_device_remove, | |
314 | .shutdown = i2c_device_shutdown, | |
315 | .suspend = i2c_device_suspend, | |
316 | .resume = i2c_device_resume, | |
54067ee2 | 317 | .pm = &i2c_device_pm_ops, |
b864c7d5 | 318 | }; |
e9ca9eb9 | 319 | EXPORT_SYMBOL_GPL(i2c_bus_type); |
b864c7d5 | 320 | |
51298d12 JD |
321 | static struct device_type i2c_client_type = { |
322 | .groups = i2c_dev_attr_groups, | |
323 | .uevent = i2c_device_uevent, | |
324 | .release = i2c_client_dev_release, | |
325 | }; | |
326 | ||
9b766b81 DB |
327 | |
328 | /** | |
329 | * i2c_verify_client - return parameter as i2c_client, or NULL | |
330 | * @dev: device, probably from some driver model iterator | |
331 | * | |
332 | * When traversing the driver model tree, perhaps using driver model | |
333 | * iterators like @device_for_each_child(), you can't assume very much | |
334 | * about the nodes you find. Use this function to avoid oopses caused | |
335 | * by wrongly treating some non-I2C device as an i2c_client. | |
336 | */ | |
337 | struct i2c_client *i2c_verify_client(struct device *dev) | |
338 | { | |
51298d12 | 339 | return (dev->type == &i2c_client_type) |
9b766b81 DB |
340 | ? to_i2c_client(dev) |
341 | : NULL; | |
342 | } | |
343 | EXPORT_SYMBOL(i2c_verify_client); | |
344 | ||
345 | ||
9c1600ed | 346 | /** |
f8a227e8 | 347 | * i2c_new_device - instantiate an i2c device |
9c1600ed DB |
348 | * @adap: the adapter managing the device |
349 | * @info: describes one I2C device; bus_num is ignored | |
d64f73be | 350 | * Context: can sleep |
9c1600ed | 351 | * |
f8a227e8 JD |
352 | * Create an i2c device. Binding is handled through driver model |
353 | * probe()/remove() methods. A driver may be bound to this device when we | |
354 | * return from this function, or any later moment (e.g. maybe hotplugging will | |
355 | * load the driver module). This call is not appropriate for use by mainboard | |
356 | * initialization logic, which usually runs during an arch_initcall() long | |
357 | * before any i2c_adapter could exist. | |
9c1600ed DB |
358 | * |
359 | * This returns the new i2c client, which may be saved for later use with | |
360 | * i2c_unregister_device(); or NULL to indicate an error. | |
361 | */ | |
362 | struct i2c_client * | |
363 | i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info) | |
364 | { | |
365 | struct i2c_client *client; | |
366 | int status; | |
367 | ||
368 | client = kzalloc(sizeof *client, GFP_KERNEL); | |
369 | if (!client) | |
370 | return NULL; | |
371 | ||
372 | client->adapter = adap; | |
373 | ||
374 | client->dev.platform_data = info->platform_data; | |
3bbb835d | 375 | |
11f1f2af AV |
376 | if (info->archdata) |
377 | client->dev.archdata = *info->archdata; | |
378 | ||
ee35425c | 379 | client->flags = info->flags; |
9c1600ed DB |
380 | client->addr = info->addr; |
381 | client->irq = info->irq; | |
382 | ||
9c1600ed DB |
383 | strlcpy(client->name, info->type, sizeof(client->name)); |
384 | ||
f8a227e8 JD |
385 | /* Check for address business */ |
386 | status = i2c_check_addr(adap, client->addr); | |
387 | if (status) | |
388 | goto out_err; | |
389 | ||
390 | client->dev.parent = &client->adapter->dev; | |
391 | client->dev.bus = &i2c_bus_type; | |
51298d12 | 392 | client->dev.type = &i2c_client_type; |
f8a227e8 JD |
393 | |
394 | dev_set_name(&client->dev, "%d-%04x", i2c_adapter_id(adap), | |
395 | client->addr); | |
396 | status = device_register(&client->dev); | |
397 | if (status) | |
398 | goto out_err; | |
399 | ||
f8a227e8 JD |
400 | dev_dbg(&adap->dev, "client [%s] registered with bus id %s\n", |
401 | client->name, dev_name(&client->dev)); | |
402 | ||
9c1600ed | 403 | return client; |
f8a227e8 JD |
404 | |
405 | out_err: | |
406 | dev_err(&adap->dev, "Failed to register i2c client %s at 0x%02x " | |
407 | "(%d)\n", client->name, client->addr, status); | |
408 | kfree(client); | |
409 | return NULL; | |
9c1600ed DB |
410 | } |
411 | EXPORT_SYMBOL_GPL(i2c_new_device); | |
412 | ||
413 | ||
414 | /** | |
415 | * i2c_unregister_device - reverse effect of i2c_new_device() | |
416 | * @client: value returned from i2c_new_device() | |
d64f73be | 417 | * Context: can sleep |
9c1600ed DB |
418 | */ |
419 | void i2c_unregister_device(struct i2c_client *client) | |
a1d9e6e4 | 420 | { |
a1d9e6e4 DB |
421 | device_unregister(&client->dev); |
422 | } | |
9c1600ed | 423 | EXPORT_SYMBOL_GPL(i2c_unregister_device); |
a1d9e6e4 DB |
424 | |
425 | ||
60b129d7 JD |
426 | static const struct i2c_device_id dummy_id[] = { |
427 | { "dummy", 0 }, | |
428 | { }, | |
429 | }; | |
430 | ||
d2653e92 JD |
431 | static int dummy_probe(struct i2c_client *client, |
432 | const struct i2c_device_id *id) | |
433 | { | |
434 | return 0; | |
435 | } | |
436 | ||
437 | static int dummy_remove(struct i2c_client *client) | |
e9f1373b DB |
438 | { |
439 | return 0; | |
440 | } | |
441 | ||
442 | static struct i2c_driver dummy_driver = { | |
443 | .driver.name = "dummy", | |
d2653e92 JD |
444 | .probe = dummy_probe, |
445 | .remove = dummy_remove, | |
60b129d7 | 446 | .id_table = dummy_id, |
e9f1373b DB |
447 | }; |
448 | ||
449 | /** | |
450 | * i2c_new_dummy - return a new i2c device bound to a dummy driver | |
451 | * @adapter: the adapter managing the device | |
452 | * @address: seven bit address to be used | |
e9f1373b DB |
453 | * Context: can sleep |
454 | * | |
455 | * This returns an I2C client bound to the "dummy" driver, intended for use | |
456 | * with devices that consume multiple addresses. Examples of such chips | |
457 | * include various EEPROMS (like 24c04 and 24c08 models). | |
458 | * | |
459 | * These dummy devices have two main uses. First, most I2C and SMBus calls | |
460 | * except i2c_transfer() need a client handle; the dummy will be that handle. | |
461 | * And second, this prevents the specified address from being bound to a | |
462 | * different driver. | |
463 | * | |
464 | * This returns the new i2c client, which should be saved for later use with | |
465 | * i2c_unregister_device(); or NULL to indicate an error. | |
466 | */ | |
09b8ce0a | 467 | struct i2c_client *i2c_new_dummy(struct i2c_adapter *adapter, u16 address) |
e9f1373b DB |
468 | { |
469 | struct i2c_board_info info = { | |
60b129d7 | 470 | I2C_BOARD_INFO("dummy", address), |
e9f1373b DB |
471 | }; |
472 | ||
e9f1373b DB |
473 | return i2c_new_device(adapter, &info); |
474 | } | |
475 | EXPORT_SYMBOL_GPL(i2c_new_dummy); | |
476 | ||
f37dd80a DB |
477 | /* ------------------------------------------------------------------------- */ |
478 | ||
16ffadfc DB |
479 | /* I2C bus adapters -- one roots each I2C or SMBUS segment */ |
480 | ||
83eaaed0 | 481 | static void i2c_adapter_dev_release(struct device *dev) |
1da177e4 | 482 | { |
ef2c8321 | 483 | struct i2c_adapter *adap = to_i2c_adapter(dev); |
1da177e4 LT |
484 | complete(&adap->dev_released); |
485 | } | |
486 | ||
99cd8e25 JD |
487 | /* |
488 | * Let users instantiate I2C devices through sysfs. This can be used when | |
489 | * platform initialization code doesn't contain the proper data for | |
490 | * whatever reason. Also useful for drivers that do device detection and | |
491 | * detection fails, either because the device uses an unexpected address, | |
492 | * or this is a compatible device with different ID register values. | |
493 | * | |
494 | * Parameter checking may look overzealous, but we really don't want | |
495 | * the user to provide incorrect parameters. | |
496 | */ | |
497 | static ssize_t | |
498 | i2c_sysfs_new_device(struct device *dev, struct device_attribute *attr, | |
499 | const char *buf, size_t count) | |
500 | { | |
501 | struct i2c_adapter *adap = to_i2c_adapter(dev); | |
502 | struct i2c_board_info info; | |
503 | struct i2c_client *client; | |
504 | char *blank, end; | |
505 | int res; | |
506 | ||
507 | dev_warn(dev, "The new_device interface is still experimental " | |
508 | "and may change in a near future\n"); | |
509 | memset(&info, 0, sizeof(struct i2c_board_info)); | |
510 | ||
511 | blank = strchr(buf, ' '); | |
512 | if (!blank) { | |
513 | dev_err(dev, "%s: Missing parameters\n", "new_device"); | |
514 | return -EINVAL; | |
515 | } | |
516 | if (blank - buf > I2C_NAME_SIZE - 1) { | |
517 | dev_err(dev, "%s: Invalid device name\n", "new_device"); | |
518 | return -EINVAL; | |
519 | } | |
520 | memcpy(info.type, buf, blank - buf); | |
521 | ||
522 | /* Parse remaining parameters, reject extra parameters */ | |
523 | res = sscanf(++blank, "%hi%c", &info.addr, &end); | |
524 | if (res < 1) { | |
525 | dev_err(dev, "%s: Can't parse I2C address\n", "new_device"); | |
526 | return -EINVAL; | |
527 | } | |
528 | if (res > 1 && end != '\n') { | |
529 | dev_err(dev, "%s: Extra parameters\n", "new_device"); | |
530 | return -EINVAL; | |
531 | } | |
532 | ||
533 | if (info.addr < 0x03 || info.addr > 0x77) { | |
534 | dev_err(dev, "%s: Invalid I2C address 0x%hx\n", "new_device", | |
535 | info.addr); | |
536 | return -EINVAL; | |
537 | } | |
538 | ||
539 | client = i2c_new_device(adap, &info); | |
540 | if (!client) | |
541 | return -EEXIST; | |
542 | ||
543 | /* Keep track of the added device */ | |
6629dcff JD |
544 | i2c_lock_adapter(adap); |
545 | list_add_tail(&client->detected, &adap->userspace_clients); | |
546 | i2c_unlock_adapter(adap); | |
99cd8e25 JD |
547 | dev_info(dev, "%s: Instantiated device %s at 0x%02hx\n", "new_device", |
548 | info.type, info.addr); | |
549 | ||
550 | return count; | |
551 | } | |
552 | ||
553 | /* | |
554 | * And of course let the users delete the devices they instantiated, if | |
555 | * they got it wrong. This interface can only be used to delete devices | |
556 | * instantiated by i2c_sysfs_new_device above. This guarantees that we | |
557 | * don't delete devices to which some kernel code still has references. | |
558 | * | |
559 | * Parameter checking may look overzealous, but we really don't want | |
560 | * the user to delete the wrong device. | |
561 | */ | |
562 | static ssize_t | |
563 | i2c_sysfs_delete_device(struct device *dev, struct device_attribute *attr, | |
564 | const char *buf, size_t count) | |
565 | { | |
566 | struct i2c_adapter *adap = to_i2c_adapter(dev); | |
567 | struct i2c_client *client, *next; | |
568 | unsigned short addr; | |
569 | char end; | |
570 | int res; | |
571 | ||
572 | /* Parse parameters, reject extra parameters */ | |
573 | res = sscanf(buf, "%hi%c", &addr, &end); | |
574 | if (res < 1) { | |
575 | dev_err(dev, "%s: Can't parse I2C address\n", "delete_device"); | |
576 | return -EINVAL; | |
577 | } | |
578 | if (res > 1 && end != '\n') { | |
579 | dev_err(dev, "%s: Extra parameters\n", "delete_device"); | |
580 | return -EINVAL; | |
581 | } | |
582 | ||
583 | /* Make sure the device was added through sysfs */ | |
584 | res = -ENOENT; | |
6629dcff JD |
585 | i2c_lock_adapter(adap); |
586 | list_for_each_entry_safe(client, next, &adap->userspace_clients, | |
587 | detected) { | |
588 | if (client->addr == addr) { | |
99cd8e25 JD |
589 | dev_info(dev, "%s: Deleting device %s at 0x%02hx\n", |
590 | "delete_device", client->name, client->addr); | |
591 | ||
592 | list_del(&client->detected); | |
593 | i2c_unregister_device(client); | |
594 | res = count; | |
595 | break; | |
596 | } | |
597 | } | |
6629dcff | 598 | i2c_unlock_adapter(adap); |
99cd8e25 JD |
599 | |
600 | if (res < 0) | |
601 | dev_err(dev, "%s: Can't find device in list\n", | |
602 | "delete_device"); | |
603 | return res; | |
604 | } | |
605 | ||
4f8cf824 JD |
606 | static DEVICE_ATTR(new_device, S_IWUSR, NULL, i2c_sysfs_new_device); |
607 | static DEVICE_ATTR(delete_device, S_IWUSR, NULL, i2c_sysfs_delete_device); | |
608 | ||
609 | static struct attribute *i2c_adapter_attrs[] = { | |
610 | &dev_attr_name.attr, | |
611 | &dev_attr_new_device.attr, | |
612 | &dev_attr_delete_device.attr, | |
613 | NULL | |
614 | }; | |
615 | ||
616 | static struct attribute_group i2c_adapter_attr_group = { | |
617 | .attrs = i2c_adapter_attrs, | |
618 | }; | |
619 | ||
620 | static const struct attribute_group *i2c_adapter_attr_groups[] = { | |
621 | &i2c_adapter_attr_group, | |
622 | NULL | |
16ffadfc | 623 | }; |
b119dc3f | 624 | |
4f8cf824 JD |
625 | static struct device_type i2c_adapter_type = { |
626 | .groups = i2c_adapter_attr_groups, | |
627 | .release = i2c_adapter_dev_release, | |
1da177e4 LT |
628 | }; |
629 | ||
2bb5095a JD |
630 | #ifdef CONFIG_I2C_COMPAT |
631 | static struct class_compat *i2c_adapter_compat_class; | |
632 | #endif | |
633 | ||
9c1600ed DB |
634 | static void i2c_scan_static_board_info(struct i2c_adapter *adapter) |
635 | { | |
636 | struct i2c_devinfo *devinfo; | |
637 | ||
f18c41da | 638 | down_read(&__i2c_board_lock); |
9c1600ed DB |
639 | list_for_each_entry(devinfo, &__i2c_board_list, list) { |
640 | if (devinfo->busnum == adapter->nr | |
641 | && !i2c_new_device(adapter, | |
642 | &devinfo->board_info)) | |
09b8ce0a ZX |
643 | dev_err(&adapter->dev, |
644 | "Can't create device at 0x%02x\n", | |
9c1600ed DB |
645 | devinfo->board_info.addr); |
646 | } | |
f18c41da | 647 | up_read(&__i2c_board_lock); |
9c1600ed DB |
648 | } |
649 | ||
69b0089a JD |
650 | static int i2c_do_add_adapter(struct i2c_driver *driver, |
651 | struct i2c_adapter *adap) | |
026526f5 | 652 | { |
4735c98f JD |
653 | /* Detect supported devices on that bus, and instantiate them */ |
654 | i2c_detect(adap, driver); | |
655 | ||
656 | /* Let legacy drivers scan this bus for matching devices */ | |
026526f5 JD |
657 | if (driver->attach_adapter) { |
658 | /* We ignore the return code; if it fails, too bad */ | |
659 | driver->attach_adapter(adap); | |
660 | } | |
661 | return 0; | |
662 | } | |
663 | ||
69b0089a JD |
664 | static int __process_new_adapter(struct device_driver *d, void *data) |
665 | { | |
666 | return i2c_do_add_adapter(to_i2c_driver(d), data); | |
667 | } | |
668 | ||
6e13e641 | 669 | static int i2c_register_adapter(struct i2c_adapter *adap) |
1da177e4 | 670 | { |
026526f5 | 671 | int res = 0, dummy; |
1da177e4 | 672 | |
1d0b19c9 | 673 | /* Can't register until after driver model init */ |
35fc37f8 JD |
674 | if (unlikely(WARN_ON(!i2c_bus_type.p))) { |
675 | res = -EAGAIN; | |
676 | goto out_list; | |
677 | } | |
1d0b19c9 | 678 | |
194684e5 | 679 | rt_mutex_init(&adap->bus_lock); |
6629dcff | 680 | INIT_LIST_HEAD(&adap->userspace_clients); |
1da177e4 | 681 | |
8fcfef6e JD |
682 | /* Set default timeout to 1 second if not already set */ |
683 | if (adap->timeout == 0) | |
684 | adap->timeout = HZ; | |
685 | ||
27d9c183 | 686 | dev_set_name(&adap->dev, "i2c-%d", adap->nr); |
4f8cf824 JD |
687 | adap->dev.bus = &i2c_bus_type; |
688 | adap->dev.type = &i2c_adapter_type; | |
b119c6c9 JD |
689 | res = device_register(&adap->dev); |
690 | if (res) | |
691 | goto out_list; | |
1da177e4 | 692 | |
b6d7b3d1 JD |
693 | dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name); |
694 | ||
2bb5095a JD |
695 | #ifdef CONFIG_I2C_COMPAT |
696 | res = class_compat_create_link(i2c_adapter_compat_class, &adap->dev, | |
697 | adap->dev.parent); | |
698 | if (res) | |
699 | dev_warn(&adap->dev, | |
700 | "Failed to create compatibility class link\n"); | |
701 | #endif | |
702 | ||
729d6dd5 | 703 | /* create pre-declared device nodes */ |
6e13e641 DB |
704 | if (adap->nr < __i2c_first_dynamic_bus_num) |
705 | i2c_scan_static_board_info(adap); | |
706 | ||
4735c98f | 707 | /* Notify drivers */ |
35fc37f8 | 708 | mutex_lock(&core_lock); |
026526f5 | 709 | dummy = bus_for_each_drv(&i2c_bus_type, NULL, adap, |
69b0089a | 710 | __process_new_adapter); |
caada32a | 711 | mutex_unlock(&core_lock); |
35fc37f8 JD |
712 | |
713 | return 0; | |
b119c6c9 | 714 | |
b119c6c9 | 715 | out_list: |
35fc37f8 | 716 | mutex_lock(&core_lock); |
b119c6c9 | 717 | idr_remove(&i2c_adapter_idr, adap->nr); |
35fc37f8 JD |
718 | mutex_unlock(&core_lock); |
719 | return res; | |
1da177e4 LT |
720 | } |
721 | ||
6e13e641 DB |
722 | /** |
723 | * i2c_add_adapter - declare i2c adapter, use dynamic bus number | |
724 | * @adapter: the adapter to add | |
d64f73be | 725 | * Context: can sleep |
6e13e641 DB |
726 | * |
727 | * This routine is used to declare an I2C adapter when its bus number | |
728 | * doesn't matter. Examples: for I2C adapters dynamically added by | |
729 | * USB links or PCI plugin cards. | |
730 | * | |
731 | * When this returns zero, a new bus number was allocated and stored | |
732 | * in adap->nr, and the specified adapter became available for clients. | |
733 | * Otherwise, a negative errno value is returned. | |
734 | */ | |
735 | int i2c_add_adapter(struct i2c_adapter *adapter) | |
736 | { | |
737 | int id, res = 0; | |
738 | ||
739 | retry: | |
740 | if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0) | |
741 | return -ENOMEM; | |
742 | ||
caada32a | 743 | mutex_lock(&core_lock); |
6e13e641 DB |
744 | /* "above" here means "above or equal to", sigh */ |
745 | res = idr_get_new_above(&i2c_adapter_idr, adapter, | |
746 | __i2c_first_dynamic_bus_num, &id); | |
caada32a | 747 | mutex_unlock(&core_lock); |
6e13e641 DB |
748 | |
749 | if (res < 0) { | |
750 | if (res == -EAGAIN) | |
751 | goto retry; | |
752 | return res; | |
753 | } | |
754 | ||
755 | adapter->nr = id; | |
756 | return i2c_register_adapter(adapter); | |
757 | } | |
758 | EXPORT_SYMBOL(i2c_add_adapter); | |
759 | ||
760 | /** | |
761 | * i2c_add_numbered_adapter - declare i2c adapter, use static bus number | |
762 | * @adap: the adapter to register (with adap->nr initialized) | |
d64f73be | 763 | * Context: can sleep |
6e13e641 DB |
764 | * |
765 | * This routine is used to declare an I2C adapter when its bus number | |
8c07e46f RD |
766 | * matters. For example, use it for I2C adapters from system-on-chip CPUs, |
767 | * or otherwise built in to the system's mainboard, and where i2c_board_info | |
6e13e641 DB |
768 | * is used to properly configure I2C devices. |
769 | * | |
770 | * If no devices have pre-been declared for this bus, then be sure to | |
771 | * register the adapter before any dynamically allocated ones. Otherwise | |
772 | * the required bus ID may not be available. | |
773 | * | |
774 | * When this returns zero, the specified adapter became available for | |
775 | * clients using the bus number provided in adap->nr. Also, the table | |
776 | * of I2C devices pre-declared using i2c_register_board_info() is scanned, | |
777 | * and the appropriate driver model device nodes are created. Otherwise, a | |
778 | * negative errno value is returned. | |
779 | */ | |
780 | int i2c_add_numbered_adapter(struct i2c_adapter *adap) | |
781 | { | |
782 | int id; | |
783 | int status; | |
784 | ||
785 | if (adap->nr & ~MAX_ID_MASK) | |
786 | return -EINVAL; | |
787 | ||
788 | retry: | |
789 | if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0) | |
790 | return -ENOMEM; | |
791 | ||
caada32a | 792 | mutex_lock(&core_lock); |
6e13e641 DB |
793 | /* "above" here means "above or equal to", sigh; |
794 | * we need the "equal to" result to force the result | |
795 | */ | |
796 | status = idr_get_new_above(&i2c_adapter_idr, adap, adap->nr, &id); | |
797 | if (status == 0 && id != adap->nr) { | |
798 | status = -EBUSY; | |
799 | idr_remove(&i2c_adapter_idr, id); | |
800 | } | |
caada32a | 801 | mutex_unlock(&core_lock); |
6e13e641 DB |
802 | if (status == -EAGAIN) |
803 | goto retry; | |
804 | ||
805 | if (status == 0) | |
806 | status = i2c_register_adapter(adap); | |
807 | return status; | |
808 | } | |
809 | EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter); | |
810 | ||
69b0089a JD |
811 | static int i2c_do_del_adapter(struct i2c_driver *driver, |
812 | struct i2c_adapter *adapter) | |
026526f5 | 813 | { |
4735c98f | 814 | struct i2c_client *client, *_n; |
026526f5 JD |
815 | int res; |
816 | ||
acec211c JD |
817 | /* Remove the devices we created ourselves as the result of hardware |
818 | * probing (using a driver's detect method) */ | |
4735c98f JD |
819 | list_for_each_entry_safe(client, _n, &driver->clients, detected) { |
820 | if (client->adapter == adapter) { | |
821 | dev_dbg(&adapter->dev, "Removing %s at 0x%x\n", | |
822 | client->name, client->addr); | |
823 | list_del(&client->detected); | |
824 | i2c_unregister_device(client); | |
825 | } | |
826 | } | |
827 | ||
026526f5 JD |
828 | if (!driver->detach_adapter) |
829 | return 0; | |
830 | res = driver->detach_adapter(adapter); | |
831 | if (res) | |
832 | dev_err(&adapter->dev, "detach_adapter failed (%d) " | |
833 | "for driver [%s]\n", res, driver->driver.name); | |
834 | return res; | |
835 | } | |
836 | ||
e549c2b5 JD |
837 | static int __unregister_client(struct device *dev, void *dummy) |
838 | { | |
839 | struct i2c_client *client = i2c_verify_client(dev); | |
840 | if (client) | |
841 | i2c_unregister_device(client); | |
842 | return 0; | |
843 | } | |
844 | ||
69b0089a JD |
845 | static int __process_removed_adapter(struct device_driver *d, void *data) |
846 | { | |
847 | return i2c_do_del_adapter(to_i2c_driver(d), data); | |
848 | } | |
849 | ||
d64f73be DB |
850 | /** |
851 | * i2c_del_adapter - unregister I2C adapter | |
852 | * @adap: the adapter being unregistered | |
853 | * Context: can sleep | |
854 | * | |
855 | * This unregisters an I2C adapter which was previously registered | |
856 | * by @i2c_add_adapter or @i2c_add_numbered_adapter. | |
857 | */ | |
1da177e4 LT |
858 | int i2c_del_adapter(struct i2c_adapter *adap) |
859 | { | |
1da177e4 | 860 | int res = 0; |
35fc37f8 | 861 | struct i2c_adapter *found; |
bbd2d9c9 | 862 | struct i2c_client *client, *next; |
1da177e4 LT |
863 | |
864 | /* First make sure that this adapter was ever added */ | |
35fc37f8 JD |
865 | mutex_lock(&core_lock); |
866 | found = idr_find(&i2c_adapter_idr, adap->nr); | |
867 | mutex_unlock(&core_lock); | |
868 | if (found != adap) { | |
b6d7b3d1 JD |
869 | pr_debug("i2c-core: attempting to delete unregistered " |
870 | "adapter [%s]\n", adap->name); | |
35fc37f8 | 871 | return -EINVAL; |
1da177e4 LT |
872 | } |
873 | ||
026526f5 | 874 | /* Tell drivers about this removal */ |
35fc37f8 | 875 | mutex_lock(&core_lock); |
026526f5 | 876 | res = bus_for_each_drv(&i2c_bus_type, NULL, adap, |
69b0089a | 877 | __process_removed_adapter); |
35fc37f8 | 878 | mutex_unlock(&core_lock); |
026526f5 | 879 | if (res) |
35fc37f8 | 880 | return res; |
1da177e4 | 881 | |
bbd2d9c9 | 882 | /* Remove devices instantiated from sysfs */ |
6629dcff JD |
883 | i2c_lock_adapter(adap); |
884 | list_for_each_entry_safe(client, next, &adap->userspace_clients, | |
885 | detected) { | |
886 | dev_dbg(&adap->dev, "Removing %s at 0x%x\n", client->name, | |
887 | client->addr); | |
888 | list_del(&client->detected); | |
889 | i2c_unregister_device(client); | |
bbd2d9c9 | 890 | } |
6629dcff | 891 | i2c_unlock_adapter(adap); |
bbd2d9c9 | 892 | |
e549c2b5 JD |
893 | /* Detach any active clients. This can't fail, thus we do not |
894 | checking the returned value. */ | |
895 | res = device_for_each_child(&adap->dev, NULL, __unregister_client); | |
1da177e4 | 896 | |
2bb5095a JD |
897 | #ifdef CONFIG_I2C_COMPAT |
898 | class_compat_remove_link(i2c_adapter_compat_class, &adap->dev, | |
899 | adap->dev.parent); | |
900 | #endif | |
901 | ||
c5567521 TLSC |
902 | /* device name is gone after device_unregister */ |
903 | dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name); | |
904 | ||
1da177e4 LT |
905 | /* clean up the sysfs representation */ |
906 | init_completion(&adap->dev_released); | |
1da177e4 | 907 | device_unregister(&adap->dev); |
1da177e4 LT |
908 | |
909 | /* wait for sysfs to drop all references */ | |
910 | wait_for_completion(&adap->dev_released); | |
1da177e4 | 911 | |
6e13e641 | 912 | /* free bus id */ |
35fc37f8 | 913 | mutex_lock(&core_lock); |
1da177e4 | 914 | idr_remove(&i2c_adapter_idr, adap->nr); |
35fc37f8 | 915 | mutex_unlock(&core_lock); |
1da177e4 | 916 | |
bd4bc3db JD |
917 | /* Clear the device structure in case this adapter is ever going to be |
918 | added again */ | |
919 | memset(&adap->dev, 0, sizeof(adap->dev)); | |
920 | ||
35fc37f8 | 921 | return 0; |
1da177e4 | 922 | } |
c0564606 | 923 | EXPORT_SYMBOL(i2c_del_adapter); |
1da177e4 LT |
924 | |
925 | ||
7b4fbc50 DB |
926 | /* ------------------------------------------------------------------------- */ |
927 | ||
69b0089a | 928 | static int __process_new_driver(struct device *dev, void *data) |
7f101a97 | 929 | { |
4f8cf824 JD |
930 | if (dev->type != &i2c_adapter_type) |
931 | return 0; | |
69b0089a | 932 | return i2c_do_add_adapter(data, to_i2c_adapter(dev)); |
7f101a97 DY |
933 | } |
934 | ||
7b4fbc50 DB |
935 | /* |
936 | * An i2c_driver is used with one or more i2c_client (device) nodes to access | |
729d6dd5 | 937 | * i2c slave chips, on a bus instance associated with some i2c_adapter. |
1da177e4 LT |
938 | */ |
939 | ||
de59cf9e | 940 | int i2c_register_driver(struct module *owner, struct i2c_driver *driver) |
1da177e4 | 941 | { |
7eebcb7c | 942 | int res; |
1da177e4 | 943 | |
1d0b19c9 DB |
944 | /* Can't register until after driver model init */ |
945 | if (unlikely(WARN_ON(!i2c_bus_type.p))) | |
946 | return -EAGAIN; | |
947 | ||
1da177e4 | 948 | /* add the driver to the list of i2c drivers in the driver core */ |
de59cf9e | 949 | driver->driver.owner = owner; |
1da177e4 | 950 | driver->driver.bus = &i2c_bus_type; |
1da177e4 | 951 | |
729d6dd5 | 952 | /* When registration returns, the driver core |
6e13e641 DB |
953 | * will have called probe() for all matching-but-unbound devices. |
954 | */ | |
1da177e4 LT |
955 | res = driver_register(&driver->driver); |
956 | if (res) | |
7eebcb7c | 957 | return res; |
438d6c2c | 958 | |
35d8b2e6 | 959 | pr_debug("i2c-core: driver [%s] registered\n", driver->driver.name); |
1da177e4 | 960 | |
4735c98f JD |
961 | INIT_LIST_HEAD(&driver->clients); |
962 | /* Walk the adapters that are already present */ | |
35fc37f8 | 963 | mutex_lock(&core_lock); |
69b0089a | 964 | bus_for_each_dev(&i2c_bus_type, NULL, driver, __process_new_driver); |
7f101a97 | 965 | mutex_unlock(&core_lock); |
35fc37f8 | 966 | |
7f101a97 DY |
967 | return 0; |
968 | } | |
969 | EXPORT_SYMBOL(i2c_register_driver); | |
970 | ||
69b0089a | 971 | static int __process_removed_driver(struct device *dev, void *data) |
7f101a97 | 972 | { |
4f8cf824 JD |
973 | if (dev->type != &i2c_adapter_type) |
974 | return 0; | |
69b0089a | 975 | return i2c_do_del_adapter(data, to_i2c_adapter(dev)); |
1da177e4 LT |
976 | } |
977 | ||
a1d9e6e4 DB |
978 | /** |
979 | * i2c_del_driver - unregister I2C driver | |
980 | * @driver: the driver being unregistered | |
d64f73be | 981 | * Context: can sleep |
a1d9e6e4 | 982 | */ |
b3e82096 | 983 | void i2c_del_driver(struct i2c_driver *driver) |
1da177e4 | 984 | { |
caada32a | 985 | mutex_lock(&core_lock); |
69b0089a | 986 | bus_for_each_dev(&i2c_bus_type, NULL, driver, __process_removed_driver); |
35fc37f8 | 987 | mutex_unlock(&core_lock); |
1da177e4 LT |
988 | |
989 | driver_unregister(&driver->driver); | |
35d8b2e6 | 990 | pr_debug("i2c-core: driver [%s] unregistered\n", driver->driver.name); |
1da177e4 | 991 | } |
c0564606 | 992 | EXPORT_SYMBOL(i2c_del_driver); |
1da177e4 | 993 | |
7b4fbc50 DB |
994 | /* ------------------------------------------------------------------------- */ |
995 | ||
9b766b81 | 996 | static int __i2c_check_addr(struct device *dev, void *addrp) |
1da177e4 | 997 | { |
9b766b81 DB |
998 | struct i2c_client *client = i2c_verify_client(dev); |
999 | int addr = *(int *)addrp; | |
1da177e4 | 1000 | |
9b766b81 DB |
1001 | if (client && client->addr == addr) |
1002 | return -EBUSY; | |
1da177e4 LT |
1003 | return 0; |
1004 | } | |
1005 | ||
5e31c2bd | 1006 | static int i2c_check_addr(struct i2c_adapter *adapter, int addr) |
1da177e4 | 1007 | { |
9b766b81 | 1008 | return device_for_each_child(&adapter->dev, &addr, __i2c_check_addr); |
1da177e4 LT |
1009 | } |
1010 | ||
e48d3319 JD |
1011 | /** |
1012 | * i2c_use_client - increments the reference count of the i2c client structure | |
1013 | * @client: the client being referenced | |
1014 | * | |
1015 | * Each live reference to a client should be refcounted. The driver model does | |
1016 | * that automatically as part of driver binding, so that most drivers don't | |
1017 | * need to do this explicitly: they hold a reference until they're unbound | |
1018 | * from the device. | |
1019 | * | |
1020 | * A pointer to the client with the incremented reference counter is returned. | |
1021 | */ | |
1022 | struct i2c_client *i2c_use_client(struct i2c_client *client) | |
1da177e4 | 1023 | { |
6ea438ec DB |
1024 | if (client && get_device(&client->dev)) |
1025 | return client; | |
1026 | return NULL; | |
1da177e4 | 1027 | } |
c0564606 | 1028 | EXPORT_SYMBOL(i2c_use_client); |
1da177e4 | 1029 | |
e48d3319 JD |
1030 | /** |
1031 | * i2c_release_client - release a use of the i2c client structure | |
1032 | * @client: the client being no longer referenced | |
1033 | * | |
1034 | * Must be called when a user of a client is finished with it. | |
1035 | */ | |
1036 | void i2c_release_client(struct i2c_client *client) | |
1da177e4 | 1037 | { |
6ea438ec DB |
1038 | if (client) |
1039 | put_device(&client->dev); | |
1da177e4 | 1040 | } |
c0564606 | 1041 | EXPORT_SYMBOL(i2c_release_client); |
1da177e4 | 1042 | |
9b766b81 DB |
1043 | struct i2c_cmd_arg { |
1044 | unsigned cmd; | |
1045 | void *arg; | |
1046 | }; | |
1047 | ||
1048 | static int i2c_cmd(struct device *dev, void *_arg) | |
1049 | { | |
1050 | struct i2c_client *client = i2c_verify_client(dev); | |
1051 | struct i2c_cmd_arg *arg = _arg; | |
1052 | ||
1053 | if (client && client->driver && client->driver->command) | |
1054 | client->driver->command(client, arg->cmd, arg->arg); | |
1055 | return 0; | |
1056 | } | |
1057 | ||
1da177e4 LT |
1058 | void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg) |
1059 | { | |
9b766b81 | 1060 | struct i2c_cmd_arg cmd_arg; |
1da177e4 | 1061 | |
9b766b81 DB |
1062 | cmd_arg.cmd = cmd; |
1063 | cmd_arg.arg = arg; | |
1064 | device_for_each_child(&adap->dev, &cmd_arg, i2c_cmd); | |
1da177e4 | 1065 | } |
c0564606 | 1066 | EXPORT_SYMBOL(i2c_clients_command); |
1da177e4 LT |
1067 | |
1068 | static int __init i2c_init(void) | |
1069 | { | |
1070 | int retval; | |
1071 | ||
1072 | retval = bus_register(&i2c_bus_type); | |
1da177e4 LT |
1073 | if (retval) |
1074 | return retval; | |
2bb5095a JD |
1075 | #ifdef CONFIG_I2C_COMPAT |
1076 | i2c_adapter_compat_class = class_compat_register("i2c-adapter"); | |
1077 | if (!i2c_adapter_compat_class) { | |
1078 | retval = -ENOMEM; | |
1079 | goto bus_err; | |
1080 | } | |
1081 | #endif | |
e9f1373b DB |
1082 | retval = i2c_add_driver(&dummy_driver); |
1083 | if (retval) | |
2bb5095a | 1084 | goto class_err; |
e9f1373b DB |
1085 | return 0; |
1086 | ||
2bb5095a JD |
1087 | class_err: |
1088 | #ifdef CONFIG_I2C_COMPAT | |
1089 | class_compat_unregister(i2c_adapter_compat_class); | |
e9f1373b | 1090 | bus_err: |
2bb5095a | 1091 | #endif |
e9f1373b DB |
1092 | bus_unregister(&i2c_bus_type); |
1093 | return retval; | |
1da177e4 LT |
1094 | } |
1095 | ||
1096 | static void __exit i2c_exit(void) | |
1097 | { | |
e9f1373b | 1098 | i2c_del_driver(&dummy_driver); |
2bb5095a JD |
1099 | #ifdef CONFIG_I2C_COMPAT |
1100 | class_compat_unregister(i2c_adapter_compat_class); | |
1101 | #endif | |
1da177e4 LT |
1102 | bus_unregister(&i2c_bus_type); |
1103 | } | |
1104 | ||
a10f9e7c DB |
1105 | /* We must initialize early, because some subsystems register i2c drivers |
1106 | * in subsys_initcall() code, but are linked (and initialized) before i2c. | |
1107 | */ | |
1108 | postcore_initcall(i2c_init); | |
1da177e4 LT |
1109 | module_exit(i2c_exit); |
1110 | ||
1111 | /* ---------------------------------------------------- | |
1112 | * the functional interface to the i2c busses. | |
1113 | * ---------------------------------------------------- | |
1114 | */ | |
1115 | ||
a1cdedac DB |
1116 | /** |
1117 | * i2c_transfer - execute a single or combined I2C message | |
1118 | * @adap: Handle to I2C bus | |
1119 | * @msgs: One or more messages to execute before STOP is issued to | |
1120 | * terminate the operation; each message begins with a START. | |
1121 | * @num: Number of messages to be executed. | |
1122 | * | |
1123 | * Returns negative errno, else the number of messages executed. | |
1124 | * | |
1125 | * Note that there is no requirement that each message be sent to | |
1126 | * the same slave address, although that is the most common model. | |
1127 | */ | |
09b8ce0a | 1128 | int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num) |
1da177e4 | 1129 | { |
66b650f0 CW |
1130 | unsigned long orig_jiffies; |
1131 | int ret, try; | |
1da177e4 | 1132 | |
a1cdedac DB |
1133 | /* REVISIT the fault reporting model here is weak: |
1134 | * | |
1135 | * - When we get an error after receiving N bytes from a slave, | |
1136 | * there is no way to report "N". | |
1137 | * | |
1138 | * - When we get a NAK after transmitting N bytes to a slave, | |
1139 | * there is no way to report "N" ... or to let the master | |
1140 | * continue executing the rest of this combined message, if | |
1141 | * that's the appropriate response. | |
1142 | * | |
1143 | * - When for example "num" is two and we successfully complete | |
1144 | * the first message but get an error part way through the | |
1145 | * second, it's unclear whether that should be reported as | |
1146 | * one (discarding status on the second message) or errno | |
1147 | * (discarding status on the first one). | |
1148 | */ | |
1149 | ||
1da177e4 LT |
1150 | if (adap->algo->master_xfer) { |
1151 | #ifdef DEBUG | |
1152 | for (ret = 0; ret < num; ret++) { | |
1153 | dev_dbg(&adap->dev, "master_xfer[%d] %c, addr=0x%02x, " | |
209d27c3 JD |
1154 | "len=%d%s\n", ret, (msgs[ret].flags & I2C_M_RD) |
1155 | ? 'R' : 'W', msgs[ret].addr, msgs[ret].len, | |
1156 | (msgs[ret].flags & I2C_M_RECV_LEN) ? "+" : ""); | |
1da177e4 LT |
1157 | } |
1158 | #endif | |
1159 | ||
cea443a8 | 1160 | if (in_atomic() || irqs_disabled()) { |
194684e5 | 1161 | ret = rt_mutex_trylock(&adap->bus_lock); |
cea443a8 MR |
1162 | if (!ret) |
1163 | /* I2C activity is ongoing. */ | |
1164 | return -EAGAIN; | |
1165 | } else { | |
194684e5 | 1166 | rt_mutex_lock(&adap->bus_lock); |
cea443a8 MR |
1167 | } |
1168 | ||
66b650f0 CW |
1169 | /* Retry automatically on arbitration loss */ |
1170 | orig_jiffies = jiffies; | |
1171 | for (ret = 0, try = 0; try <= adap->retries; try++) { | |
1172 | ret = adap->algo->master_xfer(adap, msgs, num); | |
1173 | if (ret != -EAGAIN) | |
1174 | break; | |
1175 | if (time_after(jiffies, orig_jiffies + adap->timeout)) | |
1176 | break; | |
1177 | } | |
194684e5 | 1178 | rt_mutex_unlock(&adap->bus_lock); |
1da177e4 LT |
1179 | |
1180 | return ret; | |
1181 | } else { | |
1182 | dev_dbg(&adap->dev, "I2C level transfers not supported\n"); | |
24a5bb7b | 1183 | return -EOPNOTSUPP; |
1da177e4 LT |
1184 | } |
1185 | } | |
c0564606 | 1186 | EXPORT_SYMBOL(i2c_transfer); |
1da177e4 | 1187 | |
a1cdedac DB |
1188 | /** |
1189 | * i2c_master_send - issue a single I2C message in master transmit mode | |
1190 | * @client: Handle to slave device | |
1191 | * @buf: Data that will be written to the slave | |
0c43ea54 | 1192 | * @count: How many bytes to write, must be less than 64k since msg.len is u16 |
a1cdedac DB |
1193 | * |
1194 | * Returns negative errno, or else the number of bytes written. | |
1195 | */ | |
1da177e4 LT |
1196 | int i2c_master_send(struct i2c_client *client,const char *buf ,int count) |
1197 | { | |
1198 | int ret; | |
1199 | struct i2c_adapter *adap=client->adapter; | |
1200 | struct i2c_msg msg; | |
1201 | ||
815f55f2 JD |
1202 | msg.addr = client->addr; |
1203 | msg.flags = client->flags & I2C_M_TEN; | |
1204 | msg.len = count; | |
1205 | msg.buf = (char *)buf; | |
438d6c2c | 1206 | |
815f55f2 | 1207 | ret = i2c_transfer(adap, &msg, 1); |
1da177e4 | 1208 | |
815f55f2 JD |
1209 | /* If everything went ok (i.e. 1 msg transmitted), return #bytes |
1210 | transmitted, else error code. */ | |
1211 | return (ret == 1) ? count : ret; | |
1da177e4 | 1212 | } |
c0564606 | 1213 | EXPORT_SYMBOL(i2c_master_send); |
1da177e4 | 1214 | |
a1cdedac DB |
1215 | /** |
1216 | * i2c_master_recv - issue a single I2C message in master receive mode | |
1217 | * @client: Handle to slave device | |
1218 | * @buf: Where to store data read from slave | |
0c43ea54 | 1219 | * @count: How many bytes to read, must be less than 64k since msg.len is u16 |
a1cdedac DB |
1220 | * |
1221 | * Returns negative errno, or else the number of bytes read. | |
1222 | */ | |
1da177e4 LT |
1223 | int i2c_master_recv(struct i2c_client *client, char *buf ,int count) |
1224 | { | |
1225 | struct i2c_adapter *adap=client->adapter; | |
1226 | struct i2c_msg msg; | |
1227 | int ret; | |
815f55f2 JD |
1228 | |
1229 | msg.addr = client->addr; | |
1230 | msg.flags = client->flags & I2C_M_TEN; | |
1231 | msg.flags |= I2C_M_RD; | |
1232 | msg.len = count; | |
1233 | msg.buf = buf; | |
1234 | ||
1235 | ret = i2c_transfer(adap, &msg, 1); | |
1236 | ||
1237 | /* If everything went ok (i.e. 1 msg transmitted), return #bytes | |
1238 | transmitted, else error code. */ | |
1239 | return (ret == 1) ? count : ret; | |
1da177e4 | 1240 | } |
c0564606 | 1241 | EXPORT_SYMBOL(i2c_master_recv); |
1da177e4 | 1242 | |
1da177e4 LT |
1243 | /* ---------------------------------------------------- |
1244 | * the i2c address scanning function | |
1245 | * Will not work for 10-bit addresses! | |
1246 | * ---------------------------------------------------- | |
1247 | */ | |
1da177e4 | 1248 | |
ccfbbd08 | 1249 | static int i2c_detect_address(struct i2c_client *temp_client, |
4735c98f JD |
1250 | struct i2c_driver *driver) |
1251 | { | |
1252 | struct i2c_board_info info; | |
1253 | struct i2c_adapter *adapter = temp_client->adapter; | |
1254 | int addr = temp_client->addr; | |
1255 | int err; | |
1256 | ||
1257 | /* Make sure the address is valid */ | |
1258 | if (addr < 0x03 || addr > 0x77) { | |
1259 | dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n", | |
1260 | addr); | |
1261 | return -EINVAL; | |
1262 | } | |
1263 | ||
1264 | /* Skip if already in use */ | |
1265 | if (i2c_check_addr(adapter, addr)) | |
1266 | return 0; | |
1267 | ||
ccfbbd08 | 1268 | /* Make sure there is something at this address */ |
b1d4b390 JD |
1269 | if (addr == 0x73 && (adapter->class & I2C_CLASS_HWMON)) { |
1270 | /* Special probe for FSC hwmon chips */ | |
1271 | union i2c_smbus_data dummy; | |
4735c98f | 1272 | |
b1d4b390 JD |
1273 | if (i2c_smbus_xfer(adapter, addr, 0, I2C_SMBUS_READ, 0, |
1274 | I2C_SMBUS_BYTE_DATA, &dummy) < 0) | |
1275 | return 0; | |
1276 | } else { | |
1277 | if (i2c_smbus_xfer(adapter, addr, 0, I2C_SMBUS_WRITE, 0, | |
1278 | I2C_SMBUS_QUICK, NULL) < 0) | |
1279 | return 0; | |
1280 | ||
1281 | /* Prevent 24RF08 corruption */ | |
1282 | if ((addr & ~0x0f) == 0x50) | |
1283 | i2c_smbus_xfer(adapter, addr, 0, I2C_SMBUS_WRITE, 0, | |
1284 | I2C_SMBUS_QUICK, NULL); | |
1285 | } | |
4735c98f JD |
1286 | |
1287 | /* Finally call the custom detection function */ | |
1288 | memset(&info, 0, sizeof(struct i2c_board_info)); | |
1289 | info.addr = addr; | |
310ec792 | 1290 | err = driver->detect(temp_client, &info); |
4735c98f JD |
1291 | if (err) { |
1292 | /* -ENODEV is returned if the detection fails. We catch it | |
1293 | here as this isn't an error. */ | |
1294 | return err == -ENODEV ? 0 : err; | |
1295 | } | |
1296 | ||
1297 | /* Consistency check */ | |
1298 | if (info.type[0] == '\0') { | |
1299 | dev_err(&adapter->dev, "%s detection function provided " | |
1300 | "no name for 0x%x\n", driver->driver.name, | |
1301 | addr); | |
1302 | } else { | |
1303 | struct i2c_client *client; | |
1304 | ||
1305 | /* Detection succeeded, instantiate the device */ | |
1306 | dev_dbg(&adapter->dev, "Creating %s at 0x%02x\n", | |
1307 | info.type, info.addr); | |
1308 | client = i2c_new_device(adapter, &info); | |
1309 | if (client) | |
1310 | list_add_tail(&client->detected, &driver->clients); | |
1311 | else | |
1312 | dev_err(&adapter->dev, "Failed creating %s at 0x%02x\n", | |
1313 | info.type, info.addr); | |
1314 | } | |
1315 | return 0; | |
1316 | } | |
1317 | ||
1318 | static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver) | |
1319 | { | |
c3813d6a | 1320 | const unsigned short *address_list; |
4735c98f JD |
1321 | struct i2c_client *temp_client; |
1322 | int i, err = 0; | |
1323 | int adap_id = i2c_adapter_id(adapter); | |
1324 | ||
c3813d6a JD |
1325 | address_list = driver->address_list; |
1326 | if (!driver->detect || !address_list) | |
4735c98f JD |
1327 | return 0; |
1328 | ||
1329 | /* Set up a temporary client to help detect callback */ | |
1330 | temp_client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL); | |
1331 | if (!temp_client) | |
1332 | return -ENOMEM; | |
1333 | temp_client->adapter = adapter; | |
1334 | ||
4329cf86 JD |
1335 | /* Stop here if the classes do not match */ |
1336 | if (!(adapter->class & driver->class)) | |
1337 | goto exit_free; | |
1338 | ||
4735c98f JD |
1339 | /* Stop here if we can't use SMBUS_QUICK */ |
1340 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_QUICK)) { | |
c3813d6a | 1341 | if (address_list[0] == I2C_CLIENT_END) |
4735c98f JD |
1342 | goto exit_free; |
1343 | ||
1344 | dev_warn(&adapter->dev, "SMBus Quick command not supported, " | |
1345 | "can't probe for chips\n"); | |
1346 | err = -EOPNOTSUPP; | |
1347 | goto exit_free; | |
1348 | } | |
1349 | ||
c3813d6a | 1350 | for (i = 0; address_list[i] != I2C_CLIENT_END; i += 1) { |
4735c98f | 1351 | dev_dbg(&adapter->dev, "found normal entry for adapter %d, " |
c3813d6a JD |
1352 | "addr 0x%02x\n", adap_id, address_list[i]); |
1353 | temp_client->addr = address_list[i]; | |
ccfbbd08 | 1354 | err = i2c_detect_address(temp_client, driver); |
4735c98f JD |
1355 | if (err) |
1356 | goto exit_free; | |
1357 | } | |
1358 | ||
1359 | exit_free: | |
1360 | kfree(temp_client); | |
1361 | return err; | |
1362 | } | |
1363 | ||
12b5053a JD |
1364 | struct i2c_client * |
1365 | i2c_new_probed_device(struct i2c_adapter *adap, | |
1366 | struct i2c_board_info *info, | |
1367 | unsigned short const *addr_list) | |
1368 | { | |
1369 | int i; | |
1370 | ||
1371 | /* Stop here if the bus doesn't support probing */ | |
1372 | if (!i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE)) { | |
1373 | dev_err(&adap->dev, "Probing not supported\n"); | |
1374 | return NULL; | |
1375 | } | |
1376 | ||
12b5053a JD |
1377 | for (i = 0; addr_list[i] != I2C_CLIENT_END; i++) { |
1378 | /* Check address validity */ | |
1379 | if (addr_list[i] < 0x03 || addr_list[i] > 0x77) { | |
1380 | dev_warn(&adap->dev, "Invalid 7-bit address " | |
1381 | "0x%02x\n", addr_list[i]); | |
1382 | continue; | |
1383 | } | |
1384 | ||
1385 | /* Check address availability */ | |
9b766b81 | 1386 | if (i2c_check_addr(adap, addr_list[i])) { |
12b5053a JD |
1387 | dev_dbg(&adap->dev, "Address 0x%02x already in " |
1388 | "use, not probing\n", addr_list[i]); | |
1389 | continue; | |
1390 | } | |
1391 | ||
1392 | /* Test address responsiveness | |
1393 | The default probe method is a quick write, but it is known | |
1394 | to corrupt the 24RF08 EEPROMs due to a state machine bug, | |
1395 | and could also irreversibly write-protect some EEPROMs, so | |
1396 | for address ranges 0x30-0x37 and 0x50-0x5f, we use a byte | |
1397 | read instead. Also, some bus drivers don't implement | |
1398 | quick write, so we fallback to a byte read it that case | |
1399 | too. */ | |
1400 | if ((addr_list[i] & ~0x07) == 0x30 | |
1401 | || (addr_list[i] & ~0x0f) == 0x50 | |
1402 | || !i2c_check_functionality(adap, I2C_FUNC_SMBUS_QUICK)) { | |
b25b791b HV |
1403 | union i2c_smbus_data data; |
1404 | ||
12b5053a JD |
1405 | if (i2c_smbus_xfer(adap, addr_list[i], 0, |
1406 | I2C_SMBUS_READ, 0, | |
b25b791b | 1407 | I2C_SMBUS_BYTE, &data) >= 0) |
12b5053a JD |
1408 | break; |
1409 | } else { | |
1410 | if (i2c_smbus_xfer(adap, addr_list[i], 0, | |
1411 | I2C_SMBUS_WRITE, 0, | |
1412 | I2C_SMBUS_QUICK, NULL) >= 0) | |
1413 | break; | |
1414 | } | |
1415 | } | |
12b5053a JD |
1416 | |
1417 | if (addr_list[i] == I2C_CLIENT_END) { | |
1418 | dev_dbg(&adap->dev, "Probing failed, no device found\n"); | |
1419 | return NULL; | |
1420 | } | |
1421 | ||
1422 | info->addr = addr_list[i]; | |
1423 | return i2c_new_device(adap, info); | |
1424 | } | |
1425 | EXPORT_SYMBOL_GPL(i2c_new_probed_device); | |
1426 | ||
1da177e4 LT |
1427 | struct i2c_adapter* i2c_get_adapter(int id) |
1428 | { | |
1da177e4 | 1429 | struct i2c_adapter *adapter; |
438d6c2c | 1430 | |
caada32a | 1431 | mutex_lock(&core_lock); |
1cf92b45 | 1432 | adapter = idr_find(&i2c_adapter_idr, id); |
a0920e10 MH |
1433 | if (adapter && !try_module_get(adapter->owner)) |
1434 | adapter = NULL; | |
1435 | ||
caada32a | 1436 | mutex_unlock(&core_lock); |
a0920e10 | 1437 | return adapter; |
1da177e4 | 1438 | } |
c0564606 | 1439 | EXPORT_SYMBOL(i2c_get_adapter); |
1da177e4 LT |
1440 | |
1441 | void i2c_put_adapter(struct i2c_adapter *adap) | |
1442 | { | |
1443 | module_put(adap->owner); | |
1444 | } | |
c0564606 | 1445 | EXPORT_SYMBOL(i2c_put_adapter); |
1da177e4 LT |
1446 | |
1447 | /* The SMBus parts */ | |
1448 | ||
438d6c2c | 1449 | #define POLY (0x1070U << 3) |
09b8ce0a | 1450 | static u8 crc8(u16 data) |
1da177e4 LT |
1451 | { |
1452 | int i; | |
438d6c2c | 1453 | |
1da177e4 | 1454 | for(i = 0; i < 8; i++) { |
438d6c2c | 1455 | if (data & 0x8000) |
1da177e4 LT |
1456 | data = data ^ POLY; |
1457 | data = data << 1; | |
1458 | } | |
1459 | return (u8)(data >> 8); | |
1460 | } | |
1461 | ||
421ef47b JD |
1462 | /* Incremental CRC8 over count bytes in the array pointed to by p */ |
1463 | static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count) | |
1da177e4 LT |
1464 | { |
1465 | int i; | |
1466 | ||
1467 | for(i = 0; i < count; i++) | |
421ef47b | 1468 | crc = crc8((crc ^ p[i]) << 8); |
1da177e4 LT |
1469 | return crc; |
1470 | } | |
1471 | ||
421ef47b JD |
1472 | /* Assume a 7-bit address, which is reasonable for SMBus */ |
1473 | static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg) | |
1da177e4 | 1474 | { |
421ef47b JD |
1475 | /* The address will be sent first */ |
1476 | u8 addr = (msg->addr << 1) | !!(msg->flags & I2C_M_RD); | |
1477 | pec = i2c_smbus_pec(pec, &addr, 1); | |
1478 | ||
1479 | /* The data buffer follows */ | |
1480 | return i2c_smbus_pec(pec, msg->buf, msg->len); | |
1da177e4 LT |
1481 | } |
1482 | ||
421ef47b JD |
1483 | /* Used for write only transactions */ |
1484 | static inline void i2c_smbus_add_pec(struct i2c_msg *msg) | |
1da177e4 | 1485 | { |
421ef47b JD |
1486 | msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg); |
1487 | msg->len++; | |
1da177e4 LT |
1488 | } |
1489 | ||
421ef47b JD |
1490 | /* Return <0 on CRC error |
1491 | If there was a write before this read (most cases) we need to take the | |
1492 | partial CRC from the write part into account. | |
1493 | Note that this function does modify the message (we need to decrease the | |
1494 | message length to hide the CRC byte from the caller). */ | |
1495 | static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg) | |
1da177e4 | 1496 | { |
421ef47b JD |
1497 | u8 rpec = msg->buf[--msg->len]; |
1498 | cpec = i2c_smbus_msg_pec(cpec, msg); | |
1da177e4 | 1499 | |
1da177e4 LT |
1500 | if (rpec != cpec) { |
1501 | pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n", | |
1502 | rpec, cpec); | |
24a5bb7b | 1503 | return -EBADMSG; |
1da177e4 | 1504 | } |
438d6c2c | 1505 | return 0; |
1da177e4 LT |
1506 | } |
1507 | ||
a1cdedac DB |
1508 | /** |
1509 | * i2c_smbus_read_byte - SMBus "receive byte" protocol | |
1510 | * @client: Handle to slave device | |
1511 | * | |
1512 | * This executes the SMBus "receive byte" protocol, returning negative errno | |
1513 | * else the byte received from the device. | |
1514 | */ | |
1da177e4 LT |
1515 | s32 i2c_smbus_read_byte(struct i2c_client *client) |
1516 | { | |
1517 | union i2c_smbus_data data; | |
24a5bb7b DB |
1518 | int status; |
1519 | ||
1520 | status = i2c_smbus_xfer(client->adapter, client->addr, client->flags, | |
1521 | I2C_SMBUS_READ, 0, | |
1522 | I2C_SMBUS_BYTE, &data); | |
1523 | return (status < 0) ? status : data.byte; | |
1da177e4 | 1524 | } |
c0564606 | 1525 | EXPORT_SYMBOL(i2c_smbus_read_byte); |
1da177e4 | 1526 | |
a1cdedac DB |
1527 | /** |
1528 | * i2c_smbus_write_byte - SMBus "send byte" protocol | |
1529 | * @client: Handle to slave device | |
1530 | * @value: Byte to be sent | |
1531 | * | |
1532 | * This executes the SMBus "send byte" protocol, returning negative errno | |
1533 | * else zero on success. | |
1534 | */ | |
1da177e4 LT |
1535 | s32 i2c_smbus_write_byte(struct i2c_client *client, u8 value) |
1536 | { | |
1da177e4 | 1537 | return i2c_smbus_xfer(client->adapter,client->addr,client->flags, |
421ef47b | 1538 | I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL); |
1da177e4 | 1539 | } |
c0564606 | 1540 | EXPORT_SYMBOL(i2c_smbus_write_byte); |
1da177e4 | 1541 | |
a1cdedac DB |
1542 | /** |
1543 | * i2c_smbus_read_byte_data - SMBus "read byte" protocol | |
1544 | * @client: Handle to slave device | |
1545 | * @command: Byte interpreted by slave | |
1546 | * | |
1547 | * This executes the SMBus "read byte" protocol, returning negative errno | |
1548 | * else a data byte received from the device. | |
1549 | */ | |
1da177e4 LT |
1550 | s32 i2c_smbus_read_byte_data(struct i2c_client *client, u8 command) |
1551 | { | |
1552 | union i2c_smbus_data data; | |
24a5bb7b DB |
1553 | int status; |
1554 | ||
1555 | status = i2c_smbus_xfer(client->adapter, client->addr, client->flags, | |
1556 | I2C_SMBUS_READ, command, | |
1557 | I2C_SMBUS_BYTE_DATA, &data); | |
1558 | return (status < 0) ? status : data.byte; | |
1da177e4 | 1559 | } |
c0564606 | 1560 | EXPORT_SYMBOL(i2c_smbus_read_byte_data); |
1da177e4 | 1561 | |
a1cdedac DB |
1562 | /** |
1563 | * i2c_smbus_write_byte_data - SMBus "write byte" protocol | |
1564 | * @client: Handle to slave device | |
1565 | * @command: Byte interpreted by slave | |
1566 | * @value: Byte being written | |
1567 | * | |
1568 | * This executes the SMBus "write byte" protocol, returning negative errno | |
1569 | * else zero on success. | |
1570 | */ | |
1da177e4 LT |
1571 | s32 i2c_smbus_write_byte_data(struct i2c_client *client, u8 command, u8 value) |
1572 | { | |
1573 | union i2c_smbus_data data; | |
1574 | data.byte = value; | |
1575 | return i2c_smbus_xfer(client->adapter,client->addr,client->flags, | |
1576 | I2C_SMBUS_WRITE,command, | |
1577 | I2C_SMBUS_BYTE_DATA,&data); | |
1578 | } | |
c0564606 | 1579 | EXPORT_SYMBOL(i2c_smbus_write_byte_data); |
1da177e4 | 1580 | |
a1cdedac DB |
1581 | /** |
1582 | * i2c_smbus_read_word_data - SMBus "read word" protocol | |
1583 | * @client: Handle to slave device | |
1584 | * @command: Byte interpreted by slave | |
1585 | * | |
1586 | * This executes the SMBus "read word" protocol, returning negative errno | |
1587 | * else a 16-bit unsigned "word" received from the device. | |
1588 | */ | |
1da177e4 LT |
1589 | s32 i2c_smbus_read_word_data(struct i2c_client *client, u8 command) |
1590 | { | |
1591 | union i2c_smbus_data data; | |
24a5bb7b DB |
1592 | int status; |
1593 | ||
1594 | status = i2c_smbus_xfer(client->adapter, client->addr, client->flags, | |
1595 | I2C_SMBUS_READ, command, | |
1596 | I2C_SMBUS_WORD_DATA, &data); | |
1597 | return (status < 0) ? status : data.word; | |
1da177e4 | 1598 | } |
c0564606 | 1599 | EXPORT_SYMBOL(i2c_smbus_read_word_data); |
1da177e4 | 1600 | |
a1cdedac DB |
1601 | /** |
1602 | * i2c_smbus_write_word_data - SMBus "write word" protocol | |
1603 | * @client: Handle to slave device | |
1604 | * @command: Byte interpreted by slave | |
1605 | * @value: 16-bit "word" being written | |
1606 | * | |
1607 | * This executes the SMBus "write word" protocol, returning negative errno | |
1608 | * else zero on success. | |
1609 | */ | |
1da177e4 LT |
1610 | s32 i2c_smbus_write_word_data(struct i2c_client *client, u8 command, u16 value) |
1611 | { | |
1612 | union i2c_smbus_data data; | |
1613 | data.word = value; | |
1614 | return i2c_smbus_xfer(client->adapter,client->addr,client->flags, | |
1615 | I2C_SMBUS_WRITE,command, | |
1616 | I2C_SMBUS_WORD_DATA,&data); | |
1617 | } | |
c0564606 | 1618 | EXPORT_SYMBOL(i2c_smbus_write_word_data); |
1da177e4 | 1619 | |
596c88f4 PM |
1620 | /** |
1621 | * i2c_smbus_process_call - SMBus "process call" protocol | |
1622 | * @client: Handle to slave device | |
1623 | * @command: Byte interpreted by slave | |
1624 | * @value: 16-bit "word" being written | |
1625 | * | |
1626 | * This executes the SMBus "process call" protocol, returning negative errno | |
1627 | * else a 16-bit unsigned "word" received from the device. | |
1628 | */ | |
1629 | s32 i2c_smbus_process_call(struct i2c_client *client, u8 command, u16 value) | |
1630 | { | |
1631 | union i2c_smbus_data data; | |
1632 | int status; | |
1633 | data.word = value; | |
1634 | ||
1635 | status = i2c_smbus_xfer(client->adapter, client->addr, client->flags, | |
1636 | I2C_SMBUS_WRITE, command, | |
1637 | I2C_SMBUS_PROC_CALL, &data); | |
1638 | return (status < 0) ? status : data.word; | |
1639 | } | |
1640 | EXPORT_SYMBOL(i2c_smbus_process_call); | |
1641 | ||
a64ec07d | 1642 | /** |
a1cdedac | 1643 | * i2c_smbus_read_block_data - SMBus "block read" protocol |
a64ec07d | 1644 | * @client: Handle to slave device |
a1cdedac | 1645 | * @command: Byte interpreted by slave |
a64ec07d DB |
1646 | * @values: Byte array into which data will be read; big enough to hold |
1647 | * the data returned by the slave. SMBus allows at most 32 bytes. | |
1648 | * | |
a1cdedac DB |
1649 | * This executes the SMBus "block read" protocol, returning negative errno |
1650 | * else the number of data bytes in the slave's response. | |
a64ec07d DB |
1651 | * |
1652 | * Note that using this function requires that the client's adapter support | |
1653 | * the I2C_FUNC_SMBUS_READ_BLOCK_DATA functionality. Not all adapter drivers | |
1654 | * support this; its emulation through I2C messaging relies on a specific | |
1655 | * mechanism (I2C_M_RECV_LEN) which may not be implemented. | |
1656 | */ | |
b86a1bc8 JD |
1657 | s32 i2c_smbus_read_block_data(struct i2c_client *client, u8 command, |
1658 | u8 *values) | |
1659 | { | |
1660 | union i2c_smbus_data data; | |
24a5bb7b | 1661 | int status; |
b86a1bc8 | 1662 | |
24a5bb7b DB |
1663 | status = i2c_smbus_xfer(client->adapter, client->addr, client->flags, |
1664 | I2C_SMBUS_READ, command, | |
1665 | I2C_SMBUS_BLOCK_DATA, &data); | |
1666 | if (status) | |
1667 | return status; | |
b86a1bc8 JD |
1668 | |
1669 | memcpy(values, &data.block[1], data.block[0]); | |
1670 | return data.block[0]; | |
1671 | } | |
1672 | EXPORT_SYMBOL(i2c_smbus_read_block_data); | |
1673 | ||
a1cdedac DB |
1674 | /** |
1675 | * i2c_smbus_write_block_data - SMBus "block write" protocol | |
1676 | * @client: Handle to slave device | |
1677 | * @command: Byte interpreted by slave | |
1678 | * @length: Size of data block; SMBus allows at most 32 bytes | |
1679 | * @values: Byte array which will be written. | |
1680 | * | |
1681 | * This executes the SMBus "block write" protocol, returning negative errno | |
1682 | * else zero on success. | |
1683 | */ | |
1da177e4 | 1684 | s32 i2c_smbus_write_block_data(struct i2c_client *client, u8 command, |
46f5ed75 | 1685 | u8 length, const u8 *values) |
1da177e4 LT |
1686 | { |
1687 | union i2c_smbus_data data; | |
7656032b | 1688 | |
1da177e4 LT |
1689 | if (length > I2C_SMBUS_BLOCK_MAX) |
1690 | length = I2C_SMBUS_BLOCK_MAX; | |
1da177e4 | 1691 | data.block[0] = length; |
7656032b | 1692 | memcpy(&data.block[1], values, length); |
1da177e4 LT |
1693 | return i2c_smbus_xfer(client->adapter,client->addr,client->flags, |
1694 | I2C_SMBUS_WRITE,command, | |
1695 | I2C_SMBUS_BLOCK_DATA,&data); | |
1696 | } | |
c0564606 | 1697 | EXPORT_SYMBOL(i2c_smbus_write_block_data); |
1da177e4 LT |
1698 | |
1699 | /* Returns the number of read bytes */ | |
4b2643d7 JD |
1700 | s32 i2c_smbus_read_i2c_block_data(struct i2c_client *client, u8 command, |
1701 | u8 length, u8 *values) | |
1da177e4 LT |
1702 | { |
1703 | union i2c_smbus_data data; | |
24a5bb7b | 1704 | int status; |
7656032b | 1705 | |
4b2643d7 JD |
1706 | if (length > I2C_SMBUS_BLOCK_MAX) |
1707 | length = I2C_SMBUS_BLOCK_MAX; | |
1708 | data.block[0] = length; | |
24a5bb7b DB |
1709 | status = i2c_smbus_xfer(client->adapter, client->addr, client->flags, |
1710 | I2C_SMBUS_READ, command, | |
1711 | I2C_SMBUS_I2C_BLOCK_DATA, &data); | |
1712 | if (status < 0) | |
1713 | return status; | |
7656032b JD |
1714 | |
1715 | memcpy(values, &data.block[1], data.block[0]); | |
1716 | return data.block[0]; | |
1da177e4 | 1717 | } |
c0564606 | 1718 | EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data); |
1da177e4 | 1719 | |
21bbd691 | 1720 | s32 i2c_smbus_write_i2c_block_data(struct i2c_client *client, u8 command, |
46f5ed75 | 1721 | u8 length, const u8 *values) |
21bbd691 JD |
1722 | { |
1723 | union i2c_smbus_data data; | |
1724 | ||
1725 | if (length > I2C_SMBUS_BLOCK_MAX) | |
1726 | length = I2C_SMBUS_BLOCK_MAX; | |
1727 | data.block[0] = length; | |
1728 | memcpy(data.block + 1, values, length); | |
1729 | return i2c_smbus_xfer(client->adapter, client->addr, client->flags, | |
1730 | I2C_SMBUS_WRITE, command, | |
1731 | I2C_SMBUS_I2C_BLOCK_DATA, &data); | |
1732 | } | |
c0564606 | 1733 | EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data); |
21bbd691 | 1734 | |
438d6c2c | 1735 | /* Simulate a SMBus command using the i2c protocol |
1da177e4 | 1736 | No checking of parameters is done! */ |
438d6c2c | 1737 | static s32 i2c_smbus_xfer_emulated(struct i2c_adapter * adapter, u16 addr, |
1da177e4 | 1738 | unsigned short flags, |
438d6c2c | 1739 | char read_write, u8 command, int size, |
1da177e4 LT |
1740 | union i2c_smbus_data * data) |
1741 | { | |
1742 | /* So we need to generate a series of msgs. In the case of writing, we | |
1743 | need to use only one message; when reading, we need two. We initialize | |
1744 | most things with sane defaults, to keep the code below somewhat | |
1745 | simpler. */ | |
5c50d188 HI |
1746 | unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3]; |
1747 | unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2]; | |
1da177e4 | 1748 | int num = read_write == I2C_SMBUS_READ?2:1; |
438d6c2c | 1749 | struct i2c_msg msg[2] = { { addr, flags, 1, msgbuf0 }, |
1da177e4 LT |
1750 | { addr, flags | I2C_M_RD, 0, msgbuf1 } |
1751 | }; | |
1752 | int i; | |
421ef47b | 1753 | u8 partial_pec = 0; |
24a5bb7b | 1754 | int status; |
1da177e4 LT |
1755 | |
1756 | msgbuf0[0] = command; | |
1757 | switch(size) { | |
1758 | case I2C_SMBUS_QUICK: | |
1759 | msg[0].len = 0; | |
1760 | /* Special case: The read/write field is used as data */ | |
f29d2e02 RK |
1761 | msg[0].flags = flags | (read_write == I2C_SMBUS_READ ? |
1762 | I2C_M_RD : 0); | |
1da177e4 LT |
1763 | num = 1; |
1764 | break; | |
1765 | case I2C_SMBUS_BYTE: | |
1766 | if (read_write == I2C_SMBUS_READ) { | |
1767 | /* Special case: only a read! */ | |
1768 | msg[0].flags = I2C_M_RD | flags; | |
1769 | num = 1; | |
1770 | } | |
1771 | break; | |
1772 | case I2C_SMBUS_BYTE_DATA: | |
1773 | if (read_write == I2C_SMBUS_READ) | |
1774 | msg[1].len = 1; | |
1775 | else { | |
1776 | msg[0].len = 2; | |
1777 | msgbuf0[1] = data->byte; | |
1778 | } | |
1779 | break; | |
1780 | case I2C_SMBUS_WORD_DATA: | |
1781 | if (read_write == I2C_SMBUS_READ) | |
1782 | msg[1].len = 2; | |
1783 | else { | |
1784 | msg[0].len=3; | |
1785 | msgbuf0[1] = data->word & 0xff; | |
7eff82c8 | 1786 | msgbuf0[2] = data->word >> 8; |
1da177e4 LT |
1787 | } |
1788 | break; | |
1789 | case I2C_SMBUS_PROC_CALL: | |
1790 | num = 2; /* Special case */ | |
1791 | read_write = I2C_SMBUS_READ; | |
1792 | msg[0].len = 3; | |
1793 | msg[1].len = 2; | |
1794 | msgbuf0[1] = data->word & 0xff; | |
7eff82c8 | 1795 | msgbuf0[2] = data->word >> 8; |
1da177e4 LT |
1796 | break; |
1797 | case I2C_SMBUS_BLOCK_DATA: | |
1da177e4 | 1798 | if (read_write == I2C_SMBUS_READ) { |
209d27c3 JD |
1799 | msg[1].flags |= I2C_M_RECV_LEN; |
1800 | msg[1].len = 1; /* block length will be added by | |
1801 | the underlying bus driver */ | |
1da177e4 LT |
1802 | } else { |
1803 | msg[0].len = data->block[0] + 2; | |
1804 | if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) { | |
24a5bb7b DB |
1805 | dev_err(&adapter->dev, |
1806 | "Invalid block write size %d\n", | |
1807 | data->block[0]); | |
1808 | return -EINVAL; | |
1da177e4 | 1809 | } |
5c50d188 | 1810 | for (i = 1; i < msg[0].len; i++) |
1da177e4 LT |
1811 | msgbuf0[i] = data->block[i-1]; |
1812 | } | |
1813 | break; | |
1814 | case I2C_SMBUS_BLOCK_PROC_CALL: | |
209d27c3 JD |
1815 | num = 2; /* Another special case */ |
1816 | read_write = I2C_SMBUS_READ; | |
1817 | if (data->block[0] > I2C_SMBUS_BLOCK_MAX) { | |
24a5bb7b DB |
1818 | dev_err(&adapter->dev, |
1819 | "Invalid block write size %d\n", | |
209d27c3 | 1820 | data->block[0]); |
24a5bb7b | 1821 | return -EINVAL; |
209d27c3 JD |
1822 | } |
1823 | msg[0].len = data->block[0] + 2; | |
1824 | for (i = 1; i < msg[0].len; i++) | |
1825 | msgbuf0[i] = data->block[i-1]; | |
1826 | msg[1].flags |= I2C_M_RECV_LEN; | |
1827 | msg[1].len = 1; /* block length will be added by | |
1828 | the underlying bus driver */ | |
1829 | break; | |
1da177e4 LT |
1830 | case I2C_SMBUS_I2C_BLOCK_DATA: |
1831 | if (read_write == I2C_SMBUS_READ) { | |
4b2643d7 | 1832 | msg[1].len = data->block[0]; |
1da177e4 LT |
1833 | } else { |
1834 | msg[0].len = data->block[0] + 1; | |
30dac746 | 1835 | if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) { |
24a5bb7b DB |
1836 | dev_err(&adapter->dev, |
1837 | "Invalid block write size %d\n", | |
1838 | data->block[0]); | |
1839 | return -EINVAL; | |
1da177e4 LT |
1840 | } |
1841 | for (i = 1; i <= data->block[0]; i++) | |
1842 | msgbuf0[i] = data->block[i]; | |
1843 | } | |
1844 | break; | |
1845 | default: | |
24a5bb7b DB |
1846 | dev_err(&adapter->dev, "Unsupported transaction %d\n", size); |
1847 | return -EOPNOTSUPP; | |
1da177e4 LT |
1848 | } |
1849 | ||
421ef47b JD |
1850 | i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK |
1851 | && size != I2C_SMBUS_I2C_BLOCK_DATA); | |
1852 | if (i) { | |
1853 | /* Compute PEC if first message is a write */ | |
1854 | if (!(msg[0].flags & I2C_M_RD)) { | |
438d6c2c | 1855 | if (num == 1) /* Write only */ |
421ef47b JD |
1856 | i2c_smbus_add_pec(&msg[0]); |
1857 | else /* Write followed by read */ | |
1858 | partial_pec = i2c_smbus_msg_pec(0, &msg[0]); | |
1859 | } | |
1860 | /* Ask for PEC if last message is a read */ | |
1861 | if (msg[num-1].flags & I2C_M_RD) | |
438d6c2c | 1862 | msg[num-1].len++; |
421ef47b JD |
1863 | } |
1864 | ||
24a5bb7b DB |
1865 | status = i2c_transfer(adapter, msg, num); |
1866 | if (status < 0) | |
1867 | return status; | |
1da177e4 | 1868 | |
421ef47b JD |
1869 | /* Check PEC if last message is a read */ |
1870 | if (i && (msg[num-1].flags & I2C_M_RD)) { | |
24a5bb7b DB |
1871 | status = i2c_smbus_check_pec(partial_pec, &msg[num-1]); |
1872 | if (status < 0) | |
1873 | return status; | |
421ef47b JD |
1874 | } |
1875 | ||
1da177e4 LT |
1876 | if (read_write == I2C_SMBUS_READ) |
1877 | switch(size) { | |
1878 | case I2C_SMBUS_BYTE: | |
1879 | data->byte = msgbuf0[0]; | |
1880 | break; | |
1881 | case I2C_SMBUS_BYTE_DATA: | |
1882 | data->byte = msgbuf1[0]; | |
1883 | break; | |
438d6c2c | 1884 | case I2C_SMBUS_WORD_DATA: |
1da177e4 LT |
1885 | case I2C_SMBUS_PROC_CALL: |
1886 | data->word = msgbuf1[0] | (msgbuf1[1] << 8); | |
1887 | break; | |
1888 | case I2C_SMBUS_I2C_BLOCK_DATA: | |
4b2643d7 | 1889 | for (i = 0; i < data->block[0]; i++) |
1da177e4 LT |
1890 | data->block[i+1] = msgbuf1[i]; |
1891 | break; | |
209d27c3 JD |
1892 | case I2C_SMBUS_BLOCK_DATA: |
1893 | case I2C_SMBUS_BLOCK_PROC_CALL: | |
1894 | for (i = 0; i < msgbuf1[0] + 1; i++) | |
1895 | data->block[i] = msgbuf1[i]; | |
1896 | break; | |
1da177e4 LT |
1897 | } |
1898 | return 0; | |
1899 | } | |
1900 | ||
a1cdedac DB |
1901 | /** |
1902 | * i2c_smbus_xfer - execute SMBus protocol operations | |
1903 | * @adapter: Handle to I2C bus | |
1904 | * @addr: Address of SMBus slave on that bus | |
1905 | * @flags: I2C_CLIENT_* flags (usually zero or I2C_CLIENT_PEC) | |
1906 | * @read_write: I2C_SMBUS_READ or I2C_SMBUS_WRITE | |
1907 | * @command: Byte interpreted by slave, for protocols which use such bytes | |
1908 | * @protocol: SMBus protocol operation to execute, such as I2C_SMBUS_PROC_CALL | |
1909 | * @data: Data to be read or written | |
1910 | * | |
1911 | * This executes an SMBus protocol operation, and returns a negative | |
1912 | * errno code else zero on success. | |
1913 | */ | |
09b8ce0a | 1914 | s32 i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr, unsigned short flags, |
a1cdedac | 1915 | char read_write, u8 command, int protocol, |
09b8ce0a | 1916 | union i2c_smbus_data *data) |
1da177e4 | 1917 | { |
66b650f0 CW |
1918 | unsigned long orig_jiffies; |
1919 | int try; | |
1da177e4 | 1920 | s32 res; |
1da177e4 LT |
1921 | |
1922 | flags &= I2C_M_TEN | I2C_CLIENT_PEC; | |
1da177e4 LT |
1923 | |
1924 | if (adapter->algo->smbus_xfer) { | |
194684e5 | 1925 | rt_mutex_lock(&adapter->bus_lock); |
66b650f0 CW |
1926 | |
1927 | /* Retry automatically on arbitration loss */ | |
1928 | orig_jiffies = jiffies; | |
1929 | for (res = 0, try = 0; try <= adapter->retries; try++) { | |
1930 | res = adapter->algo->smbus_xfer(adapter, addr, flags, | |
1931 | read_write, command, | |
1932 | protocol, data); | |
1933 | if (res != -EAGAIN) | |
1934 | break; | |
1935 | if (time_after(jiffies, | |
1936 | orig_jiffies + adapter->timeout)) | |
1937 | break; | |
1938 | } | |
194684e5 | 1939 | rt_mutex_unlock(&adapter->bus_lock); |
1da177e4 LT |
1940 | } else |
1941 | res = i2c_smbus_xfer_emulated(adapter,addr,flags,read_write, | |
a1cdedac | 1942 | command, protocol, data); |
1da177e4 | 1943 | |
1da177e4 LT |
1944 | return res; |
1945 | } | |
1da177e4 | 1946 | EXPORT_SYMBOL(i2c_smbus_xfer); |
1da177e4 LT |
1947 | |
1948 | MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>"); | |
1949 | MODULE_DESCRIPTION("I2C-Bus main module"); | |
1950 | MODULE_LICENSE("GPL"); |