]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - drivers/i2c/i2c-core.c
Merge branches 'release' and 'wmi-2.6.25' into release
[mirror_ubuntu-hirsute-kernel.git] / drivers / i2c / i2c-core.c
CommitLineData
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>
32#include <linux/seq_file.h>
d052d1be 33#include <linux/platform_device.h>
b3585e4f 34#include <linux/mutex.h>
b8d6f45b 35#include <linux/completion.h>
cea443a8
MR
36#include <linux/hardirq.h>
37#include <linux/irqflags.h>
1da177e4 38#include <asm/uaccess.h>
87c6c229 39#include <asm/semaphore.h>
1da177e4 40
9c1600ed
DB
41#include "i2c-core.h"
42
1da177e4 43
caada32a 44static DEFINE_MUTEX(core_lock);
1da177e4
LT
45static DEFINE_IDR(i2c_adapter_idr);
46
a1d9e6e4 47#define is_newstyle_driver(d) ((d)->probe || (d)->remove)
f37dd80a
DB
48
49/* ------------------------------------------------------------------------- */
50
1da177e4
LT
51static int i2c_device_match(struct device *dev, struct device_driver *drv)
52{
7b4fbc50
DB
53 struct i2c_client *client = to_i2c_client(dev);
54 struct i2c_driver *driver = to_i2c_driver(drv);
55
56 /* make legacy i2c drivers bypass driver model probing entirely;
57 * such drivers scan each i2c adapter/bus themselves.
58 */
a1d9e6e4 59 if (!is_newstyle_driver(driver))
7b4fbc50
DB
60 return 0;
61
62 /* new style drivers use the same kind of driver matching policy
63 * as platform devices or SPI: compare device and driver IDs.
64 */
65 return strcmp(client->driver_name, drv->name) == 0;
1da177e4
LT
66}
67
7b4fbc50
DB
68#ifdef CONFIG_HOTPLUG
69
70/* uevent helps with hotplug: modprobe -q $(MODALIAS) */
7eff2e7a 71static int i2c_device_uevent(struct device *dev, struct kobj_uevent_env *env)
7b4fbc50
DB
72{
73 struct i2c_client *client = to_i2c_client(dev);
7b4fbc50
DB
74
75 /* by definition, legacy drivers can't hotplug */
76 if (dev->driver || !client->driver_name)
77 return 0;
78
7eff2e7a 79 if (add_uevent_var(env, "MODALIAS=%s", client->driver_name))
7b4fbc50 80 return -ENOMEM;
7b4fbc50
DB
81 dev_dbg(dev, "uevent\n");
82 return 0;
83}
84
85#else
86#define i2c_device_uevent NULL
87#endif /* CONFIG_HOTPLUG */
88
f37dd80a 89static int i2c_device_probe(struct device *dev)
1da177e4 90{
7b4fbc50
DB
91 struct i2c_client *client = to_i2c_client(dev);
92 struct i2c_driver *driver = to_i2c_driver(dev->driver);
93
94 if (!driver->probe)
95 return -ENODEV;
96 client->driver = driver;
97 dev_dbg(dev, "probe\n");
98 return driver->probe(client);
f37dd80a 99}
1da177e4 100
f37dd80a
DB
101static int i2c_device_remove(struct device *dev)
102{
a1d9e6e4
DB
103 struct i2c_client *client = to_i2c_client(dev);
104 struct i2c_driver *driver;
105 int status;
106
107 if (!dev->driver)
108 return 0;
109
110 driver = to_i2c_driver(dev->driver);
111 if (driver->remove) {
112 dev_dbg(dev, "remove\n");
113 status = driver->remove(client);
114 } else {
115 dev->driver = NULL;
116 status = 0;
117 }
118 if (status == 0)
119 client->driver = NULL;
120 return status;
1da177e4
LT
121}
122
f37dd80a 123static void i2c_device_shutdown(struct device *dev)
1da177e4 124{
f37dd80a
DB
125 struct i2c_driver *driver;
126
127 if (!dev->driver)
128 return;
129 driver = to_i2c_driver(dev->driver);
130 if (driver->shutdown)
131 driver->shutdown(to_i2c_client(dev));
1da177e4
LT
132}
133
f37dd80a 134static int i2c_device_suspend(struct device * dev, pm_message_t mesg)
1da177e4 135{
f37dd80a
DB
136 struct i2c_driver *driver;
137
138 if (!dev->driver)
139 return 0;
140 driver = to_i2c_driver(dev->driver);
141 if (!driver->suspend)
142 return 0;
143 return driver->suspend(to_i2c_client(dev), mesg);
1da177e4
LT
144}
145
f37dd80a 146static int i2c_device_resume(struct device * dev)
1da177e4 147{
f37dd80a
DB
148 struct i2c_driver *driver;
149
150 if (!dev->driver)
151 return 0;
152 driver = to_i2c_driver(dev->driver);
153 if (!driver->resume)
154 return 0;
155 return driver->resume(to_i2c_client(dev));
1da177e4
LT
156}
157
7b4fbc50
DB
158static void i2c_client_release(struct device *dev)
159{
160 struct i2c_client *client = to_i2c_client(dev);
161 complete(&client->released);
162}
163
9c1600ed
DB
164static void i2c_client_dev_release(struct device *dev)
165{
166 kfree(to_i2c_client(dev));
167}
168
7b4fbc50
DB
169static ssize_t show_client_name(struct device *dev, struct device_attribute *attr, char *buf)
170{
171 struct i2c_client *client = to_i2c_client(dev);
172 return sprintf(buf, "%s\n", client->name);
173}
174
175static ssize_t show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
176{
177 struct i2c_client *client = to_i2c_client(dev);
178 return client->driver_name
179 ? sprintf(buf, "%s\n", client->driver_name)
180 : 0;
181}
182
183static struct device_attribute i2c_dev_attrs[] = {
184 __ATTR(name, S_IRUGO, show_client_name, NULL),
185 /* modalias helps coldplug: modprobe $(cat .../modalias) */
186 __ATTR(modalias, S_IRUGO, show_modalias, NULL),
187 { },
188};
189
83eaaed0 190static struct bus_type i2c_bus_type = {
f37dd80a 191 .name = "i2c",
7b4fbc50 192 .dev_attrs = i2c_dev_attrs,
f37dd80a 193 .match = i2c_device_match,
7b4fbc50 194 .uevent = i2c_device_uevent,
f37dd80a
DB
195 .probe = i2c_device_probe,
196 .remove = i2c_device_remove,
197 .shutdown = i2c_device_shutdown,
198 .suspend = i2c_device_suspend,
199 .resume = i2c_device_resume,
b864c7d5
RK
200};
201
9b766b81
DB
202
203/**
204 * i2c_verify_client - return parameter as i2c_client, or NULL
205 * @dev: device, probably from some driver model iterator
206 *
207 * When traversing the driver model tree, perhaps using driver model
208 * iterators like @device_for_each_child(), you can't assume very much
209 * about the nodes you find. Use this function to avoid oopses caused
210 * by wrongly treating some non-I2C device as an i2c_client.
211 */
212struct i2c_client *i2c_verify_client(struct device *dev)
213{
214 return (dev->bus == &i2c_bus_type)
215 ? to_i2c_client(dev)
216 : NULL;
217}
218EXPORT_SYMBOL(i2c_verify_client);
219
220
9c1600ed
DB
221/**
222 * i2c_new_device - instantiate an i2c device for use with a new style driver
223 * @adap: the adapter managing the device
224 * @info: describes one I2C device; bus_num is ignored
d64f73be 225 * Context: can sleep
9c1600ed
DB
226 *
227 * Create a device to work with a new style i2c driver, where binding is
228 * handled through driver model probe()/remove() methods. This call is not
229 * appropriate for use by mainboad initialization logic, which usually runs
230 * during an arch_initcall() long before any i2c_adapter could exist.
231 *
232 * This returns the new i2c client, which may be saved for later use with
233 * i2c_unregister_device(); or NULL to indicate an error.
234 */
235struct i2c_client *
236i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
237{
238 struct i2c_client *client;
239 int status;
240
241 client = kzalloc(sizeof *client, GFP_KERNEL);
242 if (!client)
243 return NULL;
244
245 client->adapter = adap;
246
247 client->dev.platform_data = info->platform_data;
3bbb835d
DB
248 device_init_wakeup(&client->dev, info->flags & I2C_CLIENT_WAKE);
249
250 client->flags = info->flags & ~I2C_CLIENT_WAKE;
9c1600ed
DB
251 client->addr = info->addr;
252 client->irq = info->irq;
253
254 strlcpy(client->driver_name, info->driver_name,
255 sizeof(client->driver_name));
256 strlcpy(client->name, info->type, sizeof(client->name));
257
258 /* a new style driver may be bound to this device when we
259 * return from this function, or any later moment (e.g. maybe
260 * hotplugging will load the driver module). and the device
261 * refcount model is the standard driver model one.
262 */
263 status = i2c_attach_client(client);
264 if (status < 0) {
265 kfree(client);
266 client = NULL;
267 }
268 return client;
269}
270EXPORT_SYMBOL_GPL(i2c_new_device);
271
272
273/**
274 * i2c_unregister_device - reverse effect of i2c_new_device()
275 * @client: value returned from i2c_new_device()
d64f73be 276 * Context: can sleep
9c1600ed
DB
277 */
278void i2c_unregister_device(struct i2c_client *client)
a1d9e6e4
DB
279{
280 struct i2c_adapter *adapter = client->adapter;
281 struct i2c_driver *driver = client->driver;
282
283 if (driver && !is_newstyle_driver(driver)) {
284 dev_err(&client->dev, "can't unregister devices "
285 "with legacy drivers\n");
286 WARN_ON(1);
287 return;
288 }
289
290 mutex_lock(&adapter->clist_lock);
291 list_del(&client->list);
292 mutex_unlock(&adapter->clist_lock);
293
294 device_unregister(&client->dev);
295}
9c1600ed 296EXPORT_SYMBOL_GPL(i2c_unregister_device);
a1d9e6e4
DB
297
298
e9f1373b
DB
299static int dummy_nop(struct i2c_client *client)
300{
301 return 0;
302}
303
304static struct i2c_driver dummy_driver = {
305 .driver.name = "dummy",
306 .probe = dummy_nop,
307 .remove = dummy_nop,
308};
309
310/**
311 * i2c_new_dummy - return a new i2c device bound to a dummy driver
312 * @adapter: the adapter managing the device
313 * @address: seven bit address to be used
314 * @type: optional label used for i2c_client.name
315 * Context: can sleep
316 *
317 * This returns an I2C client bound to the "dummy" driver, intended for use
318 * with devices that consume multiple addresses. Examples of such chips
319 * include various EEPROMS (like 24c04 and 24c08 models).
320 *
321 * These dummy devices have two main uses. First, most I2C and SMBus calls
322 * except i2c_transfer() need a client handle; the dummy will be that handle.
323 * And second, this prevents the specified address from being bound to a
324 * different driver.
325 *
326 * This returns the new i2c client, which should be saved for later use with
327 * i2c_unregister_device(); or NULL to indicate an error.
328 */
329struct i2c_client *
330i2c_new_dummy(struct i2c_adapter *adapter, u16 address, const char *type)
331{
332 struct i2c_board_info info = {
333 .driver_name = "dummy",
334 .addr = address,
335 };
336
337 if (type)
338 strlcpy(info.type, type, sizeof info.type);
339 return i2c_new_device(adapter, &info);
340}
341EXPORT_SYMBOL_GPL(i2c_new_dummy);
342
f37dd80a
DB
343/* ------------------------------------------------------------------------- */
344
16ffadfc
DB
345/* I2C bus adapters -- one roots each I2C or SMBUS segment */
346
83eaaed0 347static void i2c_adapter_dev_release(struct device *dev)
1da177e4 348{
ef2c8321 349 struct i2c_adapter *adap = to_i2c_adapter(dev);
1da177e4
LT
350 complete(&adap->dev_released);
351}
352
16ffadfc
DB
353static ssize_t
354show_adapter_name(struct device *dev, struct device_attribute *attr, char *buf)
355{
ef2c8321 356 struct i2c_adapter *adap = to_i2c_adapter(dev);
16ffadfc
DB
357 return sprintf(buf, "%s\n", adap->name);
358}
b119dc3f 359
16ffadfc
DB
360static struct device_attribute i2c_adapter_attrs[] = {
361 __ATTR(name, S_IRUGO, show_adapter_name, NULL),
362 { },
363};
b119dc3f 364
83eaaed0 365static struct class i2c_adapter_class = {
b119dc3f
DB
366 .owner = THIS_MODULE,
367 .name = "i2c-adapter",
16ffadfc 368 .dev_attrs = i2c_adapter_attrs,
1da177e4
LT
369};
370
9c1600ed
DB
371static void i2c_scan_static_board_info(struct i2c_adapter *adapter)
372{
373 struct i2c_devinfo *devinfo;
374
375 mutex_lock(&__i2c_board_lock);
376 list_for_each_entry(devinfo, &__i2c_board_list, list) {
377 if (devinfo->busnum == adapter->nr
378 && !i2c_new_device(adapter,
379 &devinfo->board_info))
380 printk(KERN_ERR "i2c-core: can't create i2c%d-%04x\n",
381 i2c_adapter_id(adapter),
382 devinfo->board_info.addr);
383 }
384 mutex_unlock(&__i2c_board_lock);
385}
386
026526f5
JD
387static int i2c_do_add_adapter(struct device_driver *d, void *data)
388{
389 struct i2c_driver *driver = to_i2c_driver(d);
390 struct i2c_adapter *adap = data;
391
392 if (driver->attach_adapter) {
393 /* We ignore the return code; if it fails, too bad */
394 driver->attach_adapter(adap);
395 }
396 return 0;
397}
398
6e13e641 399static int i2c_register_adapter(struct i2c_adapter *adap)
1da177e4 400{
026526f5 401 int res = 0, dummy;
1da177e4 402
5c085d36
IM
403 mutex_init(&adap->bus_lock);
404 mutex_init(&adap->clist_lock);
1da177e4
LT
405 INIT_LIST_HEAD(&adap->clients);
406
caada32a 407 mutex_lock(&core_lock);
6e13e641 408
1da177e4
LT
409 /* Add the adapter to the driver core.
410 * If the parent pointer is not set up,
411 * we add this adapter to the host bus.
412 */
b119dc3f 413 if (adap->dev.parent == NULL) {
1da177e4 414 adap->dev.parent = &platform_bus;
fe2c8d51
JD
415 pr_debug("I2C adapter driver [%s] forgot to specify "
416 "physical device\n", adap->name);
b119dc3f 417 }
1da177e4 418 sprintf(adap->dev.bus_id, "i2c-%d", adap->nr);
1da177e4 419 adap->dev.release = &i2c_adapter_dev_release;
fccb56e4 420 adap->dev.class = &i2c_adapter_class;
b119c6c9
JD
421 res = device_register(&adap->dev);
422 if (res)
423 goto out_list;
1da177e4 424
b6d7b3d1
JD
425 dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
426
6e13e641
DB
427 /* create pre-declared device nodes for new-style drivers */
428 if (adap->nr < __i2c_first_dynamic_bus_num)
429 i2c_scan_static_board_info(adap);
430
7b4fbc50 431 /* let legacy drivers scan this bus for matching devices */
026526f5
JD
432 dummy = bus_for_each_drv(&i2c_bus_type, NULL, adap,
433 i2c_do_add_adapter);
1da177e4 434
1da177e4 435out_unlock:
caada32a 436 mutex_unlock(&core_lock);
1da177e4 437 return res;
b119c6c9 438
b119c6c9 439out_list:
b119c6c9
JD
440 idr_remove(&i2c_adapter_idr, adap->nr);
441 goto out_unlock;
1da177e4
LT
442}
443
6e13e641
DB
444/**
445 * i2c_add_adapter - declare i2c adapter, use dynamic bus number
446 * @adapter: the adapter to add
d64f73be 447 * Context: can sleep
6e13e641
DB
448 *
449 * This routine is used to declare an I2C adapter when its bus number
450 * doesn't matter. Examples: for I2C adapters dynamically added by
451 * USB links or PCI plugin cards.
452 *
453 * When this returns zero, a new bus number was allocated and stored
454 * in adap->nr, and the specified adapter became available for clients.
455 * Otherwise, a negative errno value is returned.
456 */
457int i2c_add_adapter(struct i2c_adapter *adapter)
458{
459 int id, res = 0;
460
461retry:
462 if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0)
463 return -ENOMEM;
464
caada32a 465 mutex_lock(&core_lock);
6e13e641
DB
466 /* "above" here means "above or equal to", sigh */
467 res = idr_get_new_above(&i2c_adapter_idr, adapter,
468 __i2c_first_dynamic_bus_num, &id);
caada32a 469 mutex_unlock(&core_lock);
6e13e641
DB
470
471 if (res < 0) {
472 if (res == -EAGAIN)
473 goto retry;
474 return res;
475 }
476
477 adapter->nr = id;
478 return i2c_register_adapter(adapter);
479}
480EXPORT_SYMBOL(i2c_add_adapter);
481
482/**
483 * i2c_add_numbered_adapter - declare i2c adapter, use static bus number
484 * @adap: the adapter to register (with adap->nr initialized)
d64f73be 485 * Context: can sleep
6e13e641
DB
486 *
487 * This routine is used to declare an I2C adapter when its bus number
488 * matters. Example: for I2C adapters from system-on-chip CPUs, or
489 * otherwise built in to the system's mainboard, and where i2c_board_info
490 * is used to properly configure I2C devices.
491 *
492 * If no devices have pre-been declared for this bus, then be sure to
493 * register the adapter before any dynamically allocated ones. Otherwise
494 * the required bus ID may not be available.
495 *
496 * When this returns zero, the specified adapter became available for
497 * clients using the bus number provided in adap->nr. Also, the table
498 * of I2C devices pre-declared using i2c_register_board_info() is scanned,
499 * and the appropriate driver model device nodes are created. Otherwise, a
500 * negative errno value is returned.
501 */
502int i2c_add_numbered_adapter(struct i2c_adapter *adap)
503{
504 int id;
505 int status;
506
507 if (adap->nr & ~MAX_ID_MASK)
508 return -EINVAL;
509
510retry:
511 if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0)
512 return -ENOMEM;
513
caada32a 514 mutex_lock(&core_lock);
6e13e641
DB
515 /* "above" here means "above or equal to", sigh;
516 * we need the "equal to" result to force the result
517 */
518 status = idr_get_new_above(&i2c_adapter_idr, adap, adap->nr, &id);
519 if (status == 0 && id != adap->nr) {
520 status = -EBUSY;
521 idr_remove(&i2c_adapter_idr, id);
522 }
caada32a 523 mutex_unlock(&core_lock);
6e13e641
DB
524 if (status == -EAGAIN)
525 goto retry;
526
527 if (status == 0)
528 status = i2c_register_adapter(adap);
529 return status;
530}
531EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter);
532
026526f5
JD
533static int i2c_do_del_adapter(struct device_driver *d, void *data)
534{
535 struct i2c_driver *driver = to_i2c_driver(d);
536 struct i2c_adapter *adapter = data;
537 int res;
538
539 if (!driver->detach_adapter)
540 return 0;
541 res = driver->detach_adapter(adapter);
542 if (res)
543 dev_err(&adapter->dev, "detach_adapter failed (%d) "
544 "for driver [%s]\n", res, driver->driver.name);
545 return res;
546}
547
d64f73be
DB
548/**
549 * i2c_del_adapter - unregister I2C adapter
550 * @adap: the adapter being unregistered
551 * Context: can sleep
552 *
553 * This unregisters an I2C adapter which was previously registered
554 * by @i2c_add_adapter or @i2c_add_numbered_adapter.
555 */
1da177e4
LT
556int i2c_del_adapter(struct i2c_adapter *adap)
557{
558 struct list_head *item, *_n;
1da177e4
LT
559 struct i2c_client *client;
560 int res = 0;
561
caada32a 562 mutex_lock(&core_lock);
1da177e4
LT
563
564 /* First make sure that this adapter was ever added */
87c6c229 565 if (idr_find(&i2c_adapter_idr, adap->nr) != adap) {
b6d7b3d1
JD
566 pr_debug("i2c-core: attempting to delete unregistered "
567 "adapter [%s]\n", adap->name);
1da177e4
LT
568 res = -EINVAL;
569 goto out_unlock;
570 }
571
026526f5
JD
572 /* Tell drivers about this removal */
573 res = bus_for_each_drv(&i2c_bus_type, NULL, adap,
574 i2c_do_del_adapter);
575 if (res)
576 goto out_unlock;
1da177e4
LT
577
578 /* detach any active clients. This must be done first, because
a551acc2 579 * it can fail; in which case we give up. */
1da177e4 580 list_for_each_safe(item, _n, &adap->clients) {
a1d9e6e4
DB
581 struct i2c_driver *driver;
582
1da177e4 583 client = list_entry(item, struct i2c_client, list);
a1d9e6e4
DB
584 driver = client->driver;
585
586 /* new style, follow standard driver model */
587 if (!driver || is_newstyle_driver(driver)) {
588 i2c_unregister_device(client);
589 continue;
590 }
1da177e4 591
a1d9e6e4
DB
592 /* legacy drivers create and remove clients themselves */
593 if ((res = driver->detach_client(client))) {
b6d7b3d1
JD
594 dev_err(&adap->dev, "detach_client failed for client "
595 "[%s] at address 0x%02x\n", client->name,
1da177e4
LT
596 client->addr);
597 goto out_unlock;
598 }
599 }
600
601 /* clean up the sysfs representation */
602 init_completion(&adap->dev_released);
1da177e4 603 device_unregister(&adap->dev);
1da177e4
LT
604
605 /* wait for sysfs to drop all references */
606 wait_for_completion(&adap->dev_released);
1da177e4 607
6e13e641 608 /* free bus id */
1da177e4
LT
609 idr_remove(&i2c_adapter_idr, adap->nr);
610
b6d7b3d1 611 dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
1da177e4
LT
612
613 out_unlock:
caada32a 614 mutex_unlock(&core_lock);
1da177e4
LT
615 return res;
616}
c0564606 617EXPORT_SYMBOL(i2c_del_adapter);
1da177e4
LT
618
619
7b4fbc50
DB
620/* ------------------------------------------------------------------------- */
621
622/*
623 * An i2c_driver is used with one or more i2c_client (device) nodes to access
624 * i2c slave chips, on a bus instance associated with some i2c_adapter. There
625 * are two models for binding the driver to its device: "new style" drivers
626 * follow the standard Linux driver model and just respond to probe() calls
627 * issued if the driver core sees they match(); "legacy" drivers create device
628 * nodes themselves.
1da177e4
LT
629 */
630
de59cf9e 631int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
1da177e4 632{
7eebcb7c 633 int res;
1da177e4 634
7b4fbc50 635 /* new style driver methods can't mix with legacy ones */
a1d9e6e4 636 if (is_newstyle_driver(driver)) {
7b4fbc50
DB
637 if (driver->attach_adapter || driver->detach_adapter
638 || driver->detach_client) {
639 printk(KERN_WARNING
640 "i2c-core: driver [%s] is confused\n",
641 driver->driver.name);
642 return -EINVAL;
643 }
644 }
645
1da177e4 646 /* add the driver to the list of i2c drivers in the driver core */
de59cf9e 647 driver->driver.owner = owner;
1da177e4 648 driver->driver.bus = &i2c_bus_type;
1da177e4 649
6e13e641
DB
650 /* for new style drivers, when registration returns the driver core
651 * will have called probe() for all matching-but-unbound devices.
652 */
1da177e4
LT
653 res = driver_register(&driver->driver);
654 if (res)
7eebcb7c 655 return res;
438d6c2c 656
caada32a 657 mutex_lock(&core_lock);
7eebcb7c 658
35d8b2e6 659 pr_debug("i2c-core: driver [%s] registered\n", driver->driver.name);
1da177e4 660
7b4fbc50 661 /* legacy drivers scan i2c busses directly */
8a994755 662 if (driver->attach_adapter) {
4ad4eac6
DB
663 struct i2c_adapter *adapter;
664
87c6c229
JD
665 down(&i2c_adapter_class.sem);
666 list_for_each_entry(adapter, &i2c_adapter_class.devices,
667 dev.node) {
1da177e4
LT
668 driver->attach_adapter(adapter);
669 }
87c6c229 670 up(&i2c_adapter_class.sem);
1da177e4
LT
671 }
672
caada32a 673 mutex_unlock(&core_lock);
7eebcb7c 674 return 0;
1da177e4 675}
de59cf9e 676EXPORT_SYMBOL(i2c_register_driver);
1da177e4 677
a1d9e6e4
DB
678/**
679 * i2c_del_driver - unregister I2C driver
680 * @driver: the driver being unregistered
d64f73be 681 * Context: can sleep
a1d9e6e4 682 */
b3e82096 683void i2c_del_driver(struct i2c_driver *driver)
1da177e4 684{
87c6c229 685 struct list_head *item2, *_n;
1da177e4
LT
686 struct i2c_client *client;
687 struct i2c_adapter *adap;
438d6c2c 688
caada32a 689 mutex_lock(&core_lock);
1da177e4 690
a1d9e6e4
DB
691 /* new-style driver? */
692 if (is_newstyle_driver(driver))
693 goto unregister;
694
1da177e4 695 /* Have a look at each adapter, if clients of this driver are still
438d6c2c 696 * attached. If so, detach them to be able to kill the driver
1da177e4 697 * afterwards.
1da177e4 698 */
87c6c229
JD
699 down(&i2c_adapter_class.sem);
700 list_for_each_entry(adap, &i2c_adapter_class.devices, dev.node) {
1da177e4 701 if (driver->detach_adapter) {
b3e82096 702 if (driver->detach_adapter(adap)) {
b6d7b3d1 703 dev_err(&adap->dev, "detach_adapter failed "
35d8b2e6
LR
704 "for driver [%s]\n",
705 driver->driver.name);
1da177e4
LT
706 }
707 } else {
708 list_for_each_safe(item2, _n, &adap->clients) {
709 client = list_entry(item2, struct i2c_client, list);
710 if (client->driver != driver)
711 continue;
b6d7b3d1
JD
712 dev_dbg(&adap->dev, "detaching client [%s] "
713 "at 0x%02x\n", client->name,
714 client->addr);
b3e82096 715 if (driver->detach_client(client)) {
b6d7b3d1
JD
716 dev_err(&adap->dev, "detach_client "
717 "failed for client [%s] at "
718 "0x%02x\n", client->name,
1da177e4 719 client->addr);
1da177e4
LT
720 }
721 }
722 }
723 }
87c6c229 724 up(&i2c_adapter_class.sem);
1da177e4 725
a1d9e6e4 726 unregister:
1da177e4 727 driver_unregister(&driver->driver);
35d8b2e6 728 pr_debug("i2c-core: driver [%s] unregistered\n", driver->driver.name);
1da177e4 729
caada32a 730 mutex_unlock(&core_lock);
1da177e4 731}
c0564606 732EXPORT_SYMBOL(i2c_del_driver);
1da177e4 733
7b4fbc50
DB
734/* ------------------------------------------------------------------------- */
735
9b766b81 736static int __i2c_check_addr(struct device *dev, void *addrp)
1da177e4 737{
9b766b81
DB
738 struct i2c_client *client = i2c_verify_client(dev);
739 int addr = *(int *)addrp;
1da177e4 740
9b766b81
DB
741 if (client && client->addr == addr)
742 return -EBUSY;
1da177e4
LT
743 return 0;
744}
745
5e31c2bd 746static int i2c_check_addr(struct i2c_adapter *adapter, int addr)
1da177e4 747{
9b766b81 748 return device_for_each_child(&adapter->dev, &addr, __i2c_check_addr);
1da177e4
LT
749}
750
751int i2c_attach_client(struct i2c_client *client)
752{
753 struct i2c_adapter *adapter = client->adapter;
b119c6c9 754 int res = 0;
1da177e4 755
1da177e4 756 client->dev.parent = &client->adapter->dev;
1da177e4 757 client->dev.bus = &i2c_bus_type;
9c1600ed
DB
758
759 if (client->driver)
760 client->dev.driver = &client->driver->driver;
761
de81d2aa 762 if (client->driver && !is_newstyle_driver(client->driver)) {
9c1600ed 763 client->dev.release = i2c_client_release;
de81d2aa
DB
764 client->dev.uevent_suppress = 1;
765 } else
9c1600ed 766 client->dev.release = i2c_client_dev_release;
438d6c2c 767
1da177e4
LT
768 snprintf(&client->dev.bus_id[0], sizeof(client->dev.bus_id),
769 "%d-%04x", i2c_adapter_id(adapter), client->addr);
b119c6c9
JD
770 res = device_register(&client->dev);
771 if (res)
86ec5ec8
DB
772 goto out_err;
773
774 mutex_lock(&adapter->clist_lock);
775 list_add_tail(&client->list, &adapter->clients);
b119c6c9 776 mutex_unlock(&adapter->clist_lock);
77ed74da 777
86ec5ec8
DB
778 dev_dbg(&adapter->dev, "client [%s] registered with bus id %s\n",
779 client->name, client->dev.bus_id);
780
77ed74da
JD
781 if (adapter->client_register) {
782 if (adapter->client_register(client)) {
783 dev_dbg(&adapter->dev, "client_register "
784 "failed for client [%s] at 0x%02x\n",
785 client->name, client->addr);
786 }
787 }
788
789 return 0;
b119c6c9 790
86ec5ec8 791out_err:
b119c6c9
JD
792 dev_err(&adapter->dev, "Failed to attach i2c client %s at 0x%02x "
793 "(%d)\n", client->name, client->addr, res);
77ed74da 794 return res;
1da177e4 795}
c0564606 796EXPORT_SYMBOL(i2c_attach_client);
1da177e4
LT
797
798int i2c_detach_client(struct i2c_client *client)
799{
800 struct i2c_adapter *adapter = client->adapter;
801 int res = 0;
438d6c2c 802
1da177e4
LT
803 if (adapter->client_unregister) {
804 res = adapter->client_unregister(client);
805 if (res) {
806 dev_err(&client->dev,
86749e85
JD
807 "client_unregister [%s] failed, "
808 "client not detached\n", client->name);
1da177e4
LT
809 goto out;
810 }
811 }
812
5c085d36 813 mutex_lock(&adapter->clist_lock);
1da177e4 814 list_del(&client->list);
9ddced16
JD
815 mutex_unlock(&adapter->clist_lock);
816
1da177e4 817 init_completion(&client->released);
1da177e4 818 device_unregister(&client->dev);
1da177e4
LT
819 wait_for_completion(&client->released);
820
821 out:
822 return res;
823}
c0564606 824EXPORT_SYMBOL(i2c_detach_client);
1da177e4 825
e48d3319
JD
826/**
827 * i2c_use_client - increments the reference count of the i2c client structure
828 * @client: the client being referenced
829 *
830 * Each live reference to a client should be refcounted. The driver model does
831 * that automatically as part of driver binding, so that most drivers don't
832 * need to do this explicitly: they hold a reference until they're unbound
833 * from the device.
834 *
835 * A pointer to the client with the incremented reference counter is returned.
836 */
837struct i2c_client *i2c_use_client(struct i2c_client *client)
1da177e4 838{
bdc511f4 839 get_device(&client->dev);
e48d3319 840 return client;
1da177e4 841}
c0564606 842EXPORT_SYMBOL(i2c_use_client);
1da177e4 843
e48d3319
JD
844/**
845 * i2c_release_client - release a use of the i2c client structure
846 * @client: the client being no longer referenced
847 *
848 * Must be called when a user of a client is finished with it.
849 */
850void i2c_release_client(struct i2c_client *client)
1da177e4 851{
bdc511f4 852 put_device(&client->dev);
1da177e4 853}
c0564606 854EXPORT_SYMBOL(i2c_release_client);
1da177e4 855
9b766b81
DB
856struct i2c_cmd_arg {
857 unsigned cmd;
858 void *arg;
859};
860
861static int i2c_cmd(struct device *dev, void *_arg)
862{
863 struct i2c_client *client = i2c_verify_client(dev);
864 struct i2c_cmd_arg *arg = _arg;
865
866 if (client && client->driver && client->driver->command)
867 client->driver->command(client, arg->cmd, arg->arg);
868 return 0;
869}
870
1da177e4
LT
871void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
872{
9b766b81 873 struct i2c_cmd_arg cmd_arg;
1da177e4 874
9b766b81
DB
875 cmd_arg.cmd = cmd;
876 cmd_arg.arg = arg;
877 device_for_each_child(&adap->dev, &cmd_arg, i2c_cmd);
1da177e4 878}
c0564606 879EXPORT_SYMBOL(i2c_clients_command);
1da177e4
LT
880
881static int __init i2c_init(void)
882{
883 int retval;
884
885 retval = bus_register(&i2c_bus_type);
1da177e4
LT
886 if (retval)
887 return retval;
e9f1373b
DB
888 retval = class_register(&i2c_adapter_class);
889 if (retval)
890 goto bus_err;
891 retval = i2c_add_driver(&dummy_driver);
892 if (retval)
893 goto class_err;
894 return 0;
895
896class_err:
897 class_unregister(&i2c_adapter_class);
898bus_err:
899 bus_unregister(&i2c_bus_type);
900 return retval;
1da177e4
LT
901}
902
903static void __exit i2c_exit(void)
904{
e9f1373b 905 i2c_del_driver(&dummy_driver);
1da177e4 906 class_unregister(&i2c_adapter_class);
1da177e4
LT
907 bus_unregister(&i2c_bus_type);
908}
909
910subsys_initcall(i2c_init);
911module_exit(i2c_exit);
912
913/* ----------------------------------------------------
914 * the functional interface to the i2c busses.
915 * ----------------------------------------------------
916 */
917
918int i2c_transfer(struct i2c_adapter * adap, struct i2c_msg *msgs, int num)
919{
920 int ret;
921
922 if (adap->algo->master_xfer) {
923#ifdef DEBUG
924 for (ret = 0; ret < num; ret++) {
925 dev_dbg(&adap->dev, "master_xfer[%d] %c, addr=0x%02x, "
209d27c3
JD
926 "len=%d%s\n", ret, (msgs[ret].flags & I2C_M_RD)
927 ? 'R' : 'W', msgs[ret].addr, msgs[ret].len,
928 (msgs[ret].flags & I2C_M_RECV_LEN) ? "+" : "");
1da177e4
LT
929 }
930#endif
931
cea443a8
MR
932 if (in_atomic() || irqs_disabled()) {
933 ret = mutex_trylock(&adap->bus_lock);
934 if (!ret)
935 /* I2C activity is ongoing. */
936 return -EAGAIN;
937 } else {
938 mutex_lock_nested(&adap->bus_lock, adap->level);
939 }
940
1da177e4 941 ret = adap->algo->master_xfer(adap,msgs,num);
5c085d36 942 mutex_unlock(&adap->bus_lock);
1da177e4
LT
943
944 return ret;
945 } else {
946 dev_dbg(&adap->dev, "I2C level transfers not supported\n");
947 return -ENOSYS;
948 }
949}
c0564606 950EXPORT_SYMBOL(i2c_transfer);
1da177e4
LT
951
952int i2c_master_send(struct i2c_client *client,const char *buf ,int count)
953{
954 int ret;
955 struct i2c_adapter *adap=client->adapter;
956 struct i2c_msg msg;
957
815f55f2
JD
958 msg.addr = client->addr;
959 msg.flags = client->flags & I2C_M_TEN;
960 msg.len = count;
961 msg.buf = (char *)buf;
438d6c2c 962
815f55f2 963 ret = i2c_transfer(adap, &msg, 1);
1da177e4 964
815f55f2
JD
965 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
966 transmitted, else error code. */
967 return (ret == 1) ? count : ret;
1da177e4 968}
c0564606 969EXPORT_SYMBOL(i2c_master_send);
1da177e4
LT
970
971int i2c_master_recv(struct i2c_client *client, char *buf ,int count)
972{
973 struct i2c_adapter *adap=client->adapter;
974 struct i2c_msg msg;
975 int ret;
815f55f2
JD
976
977 msg.addr = client->addr;
978 msg.flags = client->flags & I2C_M_TEN;
979 msg.flags |= I2C_M_RD;
980 msg.len = count;
981 msg.buf = buf;
982
983 ret = i2c_transfer(adap, &msg, 1);
984
985 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
986 transmitted, else error code. */
987 return (ret == 1) ? count : ret;
1da177e4 988}
c0564606 989EXPORT_SYMBOL(i2c_master_recv);
1da177e4 990
1da177e4
LT
991/* ----------------------------------------------------
992 * the i2c address scanning function
993 * Will not work for 10-bit addresses!
994 * ----------------------------------------------------
995 */
a89ba0bc
JD
996static int i2c_probe_address(struct i2c_adapter *adapter, int addr, int kind,
997 int (*found_proc) (struct i2c_adapter *, int, int))
998{
999 int err;
1000
1001 /* Make sure the address is valid */
1002 if (addr < 0x03 || addr > 0x77) {
1003 dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
1004 addr);
1005 return -EINVAL;
9fc6adfa
JD
1006 }
1007
a89ba0bc
JD
1008 /* Skip if already in use */
1009 if (i2c_check_addr(adapter, addr))
1010 return 0;
1011
1012 /* Make sure there is something at this address, unless forced */
4c9337da
JD
1013 if (kind < 0) {
1014 if (i2c_smbus_xfer(adapter, addr, 0, 0, 0,
1015 I2C_SMBUS_QUICK, NULL) < 0)
1016 return 0;
1017
1018 /* prevent 24RF08 corruption */
1019 if ((addr & ~0x0f) == 0x50)
1020 i2c_smbus_xfer(adapter, addr, 0, 0, 0,
1021 I2C_SMBUS_QUICK, NULL);
1022 }
a89ba0bc
JD
1023
1024 /* Finally call the custom detection function */
1025 err = found_proc(adapter, addr, kind);
a89ba0bc
JD
1026 /* -ENODEV can be returned if there is a chip at the given address
1027 but it isn't supported by this chip driver. We catch it here as
1028 this isn't an error. */
114fd183
JD
1029 if (err == -ENODEV)
1030 err = 0;
1031
1032 if (err)
1033 dev_warn(&adapter->dev, "Client creation failed at 0x%x (%d)\n",
1034 addr, err);
1035 return err;
9fc6adfa
JD
1036}
1037
1da177e4 1038int i2c_probe(struct i2c_adapter *adapter,
bfb6df24 1039 const struct i2c_client_address_data *address_data,
1da177e4
LT
1040 int (*found_proc) (struct i2c_adapter *, int, int))
1041{
a89ba0bc 1042 int i, err;
1da177e4
LT
1043 int adap_id = i2c_adapter_id(adapter);
1044
a89ba0bc
JD
1045 /* Force entries are done first, and are not affected by ignore
1046 entries */
1047 if (address_data->forces) {
bfb6df24 1048 const unsigned short * const *forces = address_data->forces;
a89ba0bc
JD
1049 int kind;
1050
1051 for (kind = 0; forces[kind]; kind++) {
1052 for (i = 0; forces[kind][i] != I2C_CLIENT_END;
1053 i += 2) {
1054 if (forces[kind][i] == adap_id
1055 || forces[kind][i] == ANY_I2C_BUS) {
1056 dev_dbg(&adapter->dev, "found force "
1057 "parameter for adapter %d, "
1058 "addr 0x%02x, kind %d\n",
1059 adap_id, forces[kind][i + 1],
1060 kind);
1061 err = i2c_probe_address(adapter,
1062 forces[kind][i + 1],
1063 kind, found_proc);
1064 if (err)
1065 return err;
1066 }
1da177e4
LT
1067 }
1068 }
a89ba0bc 1069 }
1da177e4 1070
4366dc94
JD
1071 /* Stop here if we can't use SMBUS_QUICK */
1072 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_QUICK)) {
1073 if (address_data->probe[0] == I2C_CLIENT_END
1074 && address_data->normal_i2c[0] == I2C_CLIENT_END)
438d6c2c 1075 return 0;
4366dc94
JD
1076
1077 dev_warn(&adapter->dev, "SMBus Quick command not supported, "
1078 "can't probe for chips\n");
1079 return -1;
1080 }
1081
a89ba0bc
JD
1082 /* Probe entries are done second, and are not affected by ignore
1083 entries either */
1084 for (i = 0; address_data->probe[i] != I2C_CLIENT_END; i += 2) {
1085 if (address_data->probe[i] == adap_id
1086 || address_data->probe[i] == ANY_I2C_BUS) {
1087 dev_dbg(&adapter->dev, "found probe parameter for "
1088 "adapter %d, addr 0x%02x\n", adap_id,
1089 address_data->probe[i + 1]);
1090 err = i2c_probe_address(adapter,
1091 address_data->probe[i + 1],
1092 -1, found_proc);
1093 if (err)
1094 return err;
1da177e4 1095 }
a89ba0bc 1096 }
1da177e4 1097
a89ba0bc
JD
1098 /* Normal entries are done last, unless shadowed by an ignore entry */
1099 for (i = 0; address_data->normal_i2c[i] != I2C_CLIENT_END; i += 1) {
1100 int j, ignore;
1101
1102 ignore = 0;
1103 for (j = 0; address_data->ignore[j] != I2C_CLIENT_END;
1104 j += 2) {
1105 if ((address_data->ignore[j] == adap_id ||
1106 address_data->ignore[j] == ANY_I2C_BUS)
1107 && address_data->ignore[j + 1]
1108 == address_data->normal_i2c[i]) {
1109 dev_dbg(&adapter->dev, "found ignore "
1110 "parameter for adapter %d, "
1111 "addr 0x%02x\n", adap_id,
1112 address_data->ignore[j + 1]);
2369df93
MH
1113 ignore = 1;
1114 break;
1da177e4
LT
1115 }
1116 }
a89ba0bc 1117 if (ignore)
1da177e4
LT
1118 continue;
1119
a89ba0bc
JD
1120 dev_dbg(&adapter->dev, "found normal entry for adapter %d, "
1121 "addr 0x%02x\n", adap_id,
1122 address_data->normal_i2c[i]);
1123 err = i2c_probe_address(adapter, address_data->normal_i2c[i],
1124 -1, found_proc);
1125 if (err)
1126 return err;
1da177e4 1127 }
a89ba0bc 1128
1da177e4
LT
1129 return 0;
1130}
c0564606 1131EXPORT_SYMBOL(i2c_probe);
1da177e4 1132
12b5053a
JD
1133struct i2c_client *
1134i2c_new_probed_device(struct i2c_adapter *adap,
1135 struct i2c_board_info *info,
1136 unsigned short const *addr_list)
1137{
1138 int i;
1139
1140 /* Stop here if the bus doesn't support probing */
1141 if (!i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE)) {
1142 dev_err(&adap->dev, "Probing not supported\n");
1143 return NULL;
1144 }
1145
12b5053a
JD
1146 for (i = 0; addr_list[i] != I2C_CLIENT_END; i++) {
1147 /* Check address validity */
1148 if (addr_list[i] < 0x03 || addr_list[i] > 0x77) {
1149 dev_warn(&adap->dev, "Invalid 7-bit address "
1150 "0x%02x\n", addr_list[i]);
1151 continue;
1152 }
1153
1154 /* Check address availability */
9b766b81 1155 if (i2c_check_addr(adap, addr_list[i])) {
12b5053a
JD
1156 dev_dbg(&adap->dev, "Address 0x%02x already in "
1157 "use, not probing\n", addr_list[i]);
1158 continue;
1159 }
1160
1161 /* Test address responsiveness
1162 The default probe method is a quick write, but it is known
1163 to corrupt the 24RF08 EEPROMs due to a state machine bug,
1164 and could also irreversibly write-protect some EEPROMs, so
1165 for address ranges 0x30-0x37 and 0x50-0x5f, we use a byte
1166 read instead. Also, some bus drivers don't implement
1167 quick write, so we fallback to a byte read it that case
1168 too. */
1169 if ((addr_list[i] & ~0x07) == 0x30
1170 || (addr_list[i] & ~0x0f) == 0x50
1171 || !i2c_check_functionality(adap, I2C_FUNC_SMBUS_QUICK)) {
1172 if (i2c_smbus_xfer(adap, addr_list[i], 0,
1173 I2C_SMBUS_READ, 0,
1174 I2C_SMBUS_BYTE, NULL) >= 0)
1175 break;
1176 } else {
1177 if (i2c_smbus_xfer(adap, addr_list[i], 0,
1178 I2C_SMBUS_WRITE, 0,
1179 I2C_SMBUS_QUICK, NULL) >= 0)
1180 break;
1181 }
1182 }
12b5053a
JD
1183
1184 if (addr_list[i] == I2C_CLIENT_END) {
1185 dev_dbg(&adap->dev, "Probing failed, no device found\n");
1186 return NULL;
1187 }
1188
1189 info->addr = addr_list[i];
1190 return i2c_new_device(adap, info);
1191}
1192EXPORT_SYMBOL_GPL(i2c_new_probed_device);
1193
1da177e4
LT
1194struct i2c_adapter* i2c_get_adapter(int id)
1195{
1da177e4 1196 struct i2c_adapter *adapter;
438d6c2c 1197
caada32a 1198 mutex_lock(&core_lock);
a0920e10
MH
1199 adapter = (struct i2c_adapter *)idr_find(&i2c_adapter_idr, id);
1200 if (adapter && !try_module_get(adapter->owner))
1201 adapter = NULL;
1202
caada32a 1203 mutex_unlock(&core_lock);
a0920e10 1204 return adapter;
1da177e4 1205}
c0564606 1206EXPORT_SYMBOL(i2c_get_adapter);
1da177e4
LT
1207
1208void i2c_put_adapter(struct i2c_adapter *adap)
1209{
1210 module_put(adap->owner);
1211}
c0564606 1212EXPORT_SYMBOL(i2c_put_adapter);
1da177e4
LT
1213
1214/* The SMBus parts */
1215
438d6c2c 1216#define POLY (0x1070U << 3)
1da177e4
LT
1217static u8
1218crc8(u16 data)
1219{
1220 int i;
438d6c2c 1221
1da177e4 1222 for(i = 0; i < 8; i++) {
438d6c2c 1223 if (data & 0x8000)
1da177e4
LT
1224 data = data ^ POLY;
1225 data = data << 1;
1226 }
1227 return (u8)(data >> 8);
1228}
1229
421ef47b
JD
1230/* Incremental CRC8 over count bytes in the array pointed to by p */
1231static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count)
1da177e4
LT
1232{
1233 int i;
1234
1235 for(i = 0; i < count; i++)
421ef47b 1236 crc = crc8((crc ^ p[i]) << 8);
1da177e4
LT
1237 return crc;
1238}
1239
421ef47b
JD
1240/* Assume a 7-bit address, which is reasonable for SMBus */
1241static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg)
1da177e4 1242{
421ef47b
JD
1243 /* The address will be sent first */
1244 u8 addr = (msg->addr << 1) | !!(msg->flags & I2C_M_RD);
1245 pec = i2c_smbus_pec(pec, &addr, 1);
1246
1247 /* The data buffer follows */
1248 return i2c_smbus_pec(pec, msg->buf, msg->len);
1da177e4
LT
1249}
1250
421ef47b
JD
1251/* Used for write only transactions */
1252static inline void i2c_smbus_add_pec(struct i2c_msg *msg)
1da177e4 1253{
421ef47b
JD
1254 msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg);
1255 msg->len++;
1da177e4
LT
1256}
1257
421ef47b
JD
1258/* Return <0 on CRC error
1259 If there was a write before this read (most cases) we need to take the
1260 partial CRC from the write part into account.
1261 Note that this function does modify the message (we need to decrease the
1262 message length to hide the CRC byte from the caller). */
1263static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg)
1da177e4 1264{
421ef47b
JD
1265 u8 rpec = msg->buf[--msg->len];
1266 cpec = i2c_smbus_msg_pec(cpec, msg);
1da177e4 1267
1da177e4
LT
1268 if (rpec != cpec) {
1269 pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n",
1270 rpec, cpec);
1271 return -1;
1272 }
438d6c2c 1273 return 0;
1da177e4
LT
1274}
1275
1276s32 i2c_smbus_write_quick(struct i2c_client *client, u8 value)
1277{
1278 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
438d6c2c 1279 value,0,I2C_SMBUS_QUICK,NULL);
1da177e4 1280}
c0564606 1281EXPORT_SYMBOL(i2c_smbus_write_quick);
1da177e4
LT
1282
1283s32 i2c_smbus_read_byte(struct i2c_client *client)
1284{
1285 union i2c_smbus_data data;
1286 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1287 I2C_SMBUS_READ,0,I2C_SMBUS_BYTE, &data))
1288 return -1;
1289 else
7eff82c8 1290 return data.byte;
1da177e4 1291}
c0564606 1292EXPORT_SYMBOL(i2c_smbus_read_byte);
1da177e4
LT
1293
1294s32 i2c_smbus_write_byte(struct i2c_client *client, u8 value)
1295{
1da177e4 1296 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
421ef47b 1297 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
1da177e4 1298}
c0564606 1299EXPORT_SYMBOL(i2c_smbus_write_byte);
1da177e4
LT
1300
1301s32 i2c_smbus_read_byte_data(struct i2c_client *client, u8 command)
1302{
1303 union i2c_smbus_data data;
1304 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1305 I2C_SMBUS_READ,command, I2C_SMBUS_BYTE_DATA,&data))
1306 return -1;
1307 else
7eff82c8 1308 return data.byte;
1da177e4 1309}
c0564606 1310EXPORT_SYMBOL(i2c_smbus_read_byte_data);
1da177e4
LT
1311
1312s32 i2c_smbus_write_byte_data(struct i2c_client *client, u8 command, u8 value)
1313{
1314 union i2c_smbus_data data;
1315 data.byte = value;
1316 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1317 I2C_SMBUS_WRITE,command,
1318 I2C_SMBUS_BYTE_DATA,&data);
1319}
c0564606 1320EXPORT_SYMBOL(i2c_smbus_write_byte_data);
1da177e4
LT
1321
1322s32 i2c_smbus_read_word_data(struct i2c_client *client, u8 command)
1323{
1324 union i2c_smbus_data data;
1325 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1326 I2C_SMBUS_READ,command, I2C_SMBUS_WORD_DATA, &data))
1327 return -1;
1328 else
7eff82c8 1329 return data.word;
1da177e4 1330}
c0564606 1331EXPORT_SYMBOL(i2c_smbus_read_word_data);
1da177e4
LT
1332
1333s32 i2c_smbus_write_word_data(struct i2c_client *client, u8 command, u16 value)
1334{
1335 union i2c_smbus_data data;
1336 data.word = value;
1337 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1338 I2C_SMBUS_WRITE,command,
1339 I2C_SMBUS_WORD_DATA,&data);
1340}
c0564606 1341EXPORT_SYMBOL(i2c_smbus_write_word_data);
1da177e4 1342
a64ec07d
DB
1343/**
1344 * i2c_smbus_read_block_data - SMBus block read request
1345 * @client: Handle to slave device
1346 * @command: Command byte issued to let the slave know what data should
1347 * be returned
1348 * @values: Byte array into which data will be read; big enough to hold
1349 * the data returned by the slave. SMBus allows at most 32 bytes.
1350 *
1351 * Returns the number of bytes read in the slave's response, else a
1352 * negative number to indicate some kind of error.
1353 *
1354 * Note that using this function requires that the client's adapter support
1355 * the I2C_FUNC_SMBUS_READ_BLOCK_DATA functionality. Not all adapter drivers
1356 * support this; its emulation through I2C messaging relies on a specific
1357 * mechanism (I2C_M_RECV_LEN) which may not be implemented.
1358 */
b86a1bc8
JD
1359s32 i2c_smbus_read_block_data(struct i2c_client *client, u8 command,
1360 u8 *values)
1361{
1362 union i2c_smbus_data data;
1363
1364 if (i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1365 I2C_SMBUS_READ, command,
1366 I2C_SMBUS_BLOCK_DATA, &data))
1367 return -1;
1368
1369 memcpy(values, &data.block[1], data.block[0]);
1370 return data.block[0];
1371}
1372EXPORT_SYMBOL(i2c_smbus_read_block_data);
1373
1da177e4 1374s32 i2c_smbus_write_block_data(struct i2c_client *client, u8 command,
46f5ed75 1375 u8 length, const u8 *values)
1da177e4
LT
1376{
1377 union i2c_smbus_data data;
7656032b 1378
1da177e4
LT
1379 if (length > I2C_SMBUS_BLOCK_MAX)
1380 length = I2C_SMBUS_BLOCK_MAX;
1da177e4 1381 data.block[0] = length;
7656032b 1382 memcpy(&data.block[1], values, length);
1da177e4
LT
1383 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1384 I2C_SMBUS_WRITE,command,
1385 I2C_SMBUS_BLOCK_DATA,&data);
1386}
c0564606 1387EXPORT_SYMBOL(i2c_smbus_write_block_data);
1da177e4
LT
1388
1389/* Returns the number of read bytes */
4b2643d7
JD
1390s32 i2c_smbus_read_i2c_block_data(struct i2c_client *client, u8 command,
1391 u8 length, u8 *values)
1da177e4
LT
1392{
1393 union i2c_smbus_data data;
7656032b 1394
4b2643d7
JD
1395 if (length > I2C_SMBUS_BLOCK_MAX)
1396 length = I2C_SMBUS_BLOCK_MAX;
1397 data.block[0] = length;
1da177e4
LT
1398 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1399 I2C_SMBUS_READ,command,
1400 I2C_SMBUS_I2C_BLOCK_DATA,&data))
1401 return -1;
7656032b
JD
1402
1403 memcpy(values, &data.block[1], data.block[0]);
1404 return data.block[0];
1da177e4 1405}
c0564606 1406EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
1da177e4 1407
21bbd691 1408s32 i2c_smbus_write_i2c_block_data(struct i2c_client *client, u8 command,
46f5ed75 1409 u8 length, const u8 *values)
21bbd691
JD
1410{
1411 union i2c_smbus_data data;
1412
1413 if (length > I2C_SMBUS_BLOCK_MAX)
1414 length = I2C_SMBUS_BLOCK_MAX;
1415 data.block[0] = length;
1416 memcpy(data.block + 1, values, length);
1417 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1418 I2C_SMBUS_WRITE, command,
1419 I2C_SMBUS_I2C_BLOCK_DATA, &data);
1420}
c0564606 1421EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data);
21bbd691 1422
438d6c2c 1423/* Simulate a SMBus command using the i2c protocol
1da177e4 1424 No checking of parameters is done! */
438d6c2c 1425static s32 i2c_smbus_xfer_emulated(struct i2c_adapter * adapter, u16 addr,
1da177e4 1426 unsigned short flags,
438d6c2c 1427 char read_write, u8 command, int size,
1da177e4
LT
1428 union i2c_smbus_data * data)
1429{
1430 /* So we need to generate a series of msgs. In the case of writing, we
1431 need to use only one message; when reading, we need two. We initialize
1432 most things with sane defaults, to keep the code below somewhat
1433 simpler. */
5c50d188
HI
1434 unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3];
1435 unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2];
1da177e4 1436 int num = read_write == I2C_SMBUS_READ?2:1;
438d6c2c 1437 struct i2c_msg msg[2] = { { addr, flags, 1, msgbuf0 },
1da177e4
LT
1438 { addr, flags | I2C_M_RD, 0, msgbuf1 }
1439 };
1440 int i;
421ef47b 1441 u8 partial_pec = 0;
1da177e4
LT
1442
1443 msgbuf0[0] = command;
1444 switch(size) {
1445 case I2C_SMBUS_QUICK:
1446 msg[0].len = 0;
1447 /* Special case: The read/write field is used as data */
1448 msg[0].flags = flags | (read_write==I2C_SMBUS_READ)?I2C_M_RD:0;
1449 num = 1;
1450 break;
1451 case I2C_SMBUS_BYTE:
1452 if (read_write == I2C_SMBUS_READ) {
1453 /* Special case: only a read! */
1454 msg[0].flags = I2C_M_RD | flags;
1455 num = 1;
1456 }
1457 break;
1458 case I2C_SMBUS_BYTE_DATA:
1459 if (read_write == I2C_SMBUS_READ)
1460 msg[1].len = 1;
1461 else {
1462 msg[0].len = 2;
1463 msgbuf0[1] = data->byte;
1464 }
1465 break;
1466 case I2C_SMBUS_WORD_DATA:
1467 if (read_write == I2C_SMBUS_READ)
1468 msg[1].len = 2;
1469 else {
1470 msg[0].len=3;
1471 msgbuf0[1] = data->word & 0xff;
7eff82c8 1472 msgbuf0[2] = data->word >> 8;
1da177e4
LT
1473 }
1474 break;
1475 case I2C_SMBUS_PROC_CALL:
1476 num = 2; /* Special case */
1477 read_write = I2C_SMBUS_READ;
1478 msg[0].len = 3;
1479 msg[1].len = 2;
1480 msgbuf0[1] = data->word & 0xff;
7eff82c8 1481 msgbuf0[2] = data->word >> 8;
1da177e4
LT
1482 break;
1483 case I2C_SMBUS_BLOCK_DATA:
1da177e4 1484 if (read_write == I2C_SMBUS_READ) {
209d27c3
JD
1485 msg[1].flags |= I2C_M_RECV_LEN;
1486 msg[1].len = 1; /* block length will be added by
1487 the underlying bus driver */
1da177e4
LT
1488 } else {
1489 msg[0].len = data->block[0] + 2;
1490 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
1491 dev_err(&adapter->dev, "smbus_access called with "
1492 "invalid block write size (%d)\n",
1493 data->block[0]);
1494 return -1;
1495 }
5c50d188 1496 for (i = 1; i < msg[0].len; i++)
1da177e4
LT
1497 msgbuf0[i] = data->block[i-1];
1498 }
1499 break;
1500 case I2C_SMBUS_BLOCK_PROC_CALL:
209d27c3
JD
1501 num = 2; /* Another special case */
1502 read_write = I2C_SMBUS_READ;
1503 if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
1504 dev_err(&adapter->dev, "%s called with invalid "
1505 "block proc call size (%d)\n", __FUNCTION__,
1506 data->block[0]);
1507 return -1;
1508 }
1509 msg[0].len = data->block[0] + 2;
1510 for (i = 1; i < msg[0].len; i++)
1511 msgbuf0[i] = data->block[i-1];
1512 msg[1].flags |= I2C_M_RECV_LEN;
1513 msg[1].len = 1; /* block length will be added by
1514 the underlying bus driver */
1515 break;
1da177e4
LT
1516 case I2C_SMBUS_I2C_BLOCK_DATA:
1517 if (read_write == I2C_SMBUS_READ) {
4b2643d7 1518 msg[1].len = data->block[0];
1da177e4
LT
1519 } else {
1520 msg[0].len = data->block[0] + 1;
30dac746 1521 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) {
1da177e4
LT
1522 dev_err(&adapter->dev, "i2c_smbus_xfer_emulated called with "
1523 "invalid block write size (%d)\n",
1524 data->block[0]);
1525 return -1;
1526 }
1527 for (i = 1; i <= data->block[0]; i++)
1528 msgbuf0[i] = data->block[i];
1529 }
1530 break;
1531 default:
1532 dev_err(&adapter->dev, "smbus_access called with invalid size (%d)\n",
1533 size);
1534 return -1;
1535 }
1536
421ef47b
JD
1537 i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK
1538 && size != I2C_SMBUS_I2C_BLOCK_DATA);
1539 if (i) {
1540 /* Compute PEC if first message is a write */
1541 if (!(msg[0].flags & I2C_M_RD)) {
438d6c2c 1542 if (num == 1) /* Write only */
421ef47b
JD
1543 i2c_smbus_add_pec(&msg[0]);
1544 else /* Write followed by read */
1545 partial_pec = i2c_smbus_msg_pec(0, &msg[0]);
1546 }
1547 /* Ask for PEC if last message is a read */
1548 if (msg[num-1].flags & I2C_M_RD)
438d6c2c 1549 msg[num-1].len++;
421ef47b
JD
1550 }
1551
1da177e4
LT
1552 if (i2c_transfer(adapter, msg, num) < 0)
1553 return -1;
1554
421ef47b
JD
1555 /* Check PEC if last message is a read */
1556 if (i && (msg[num-1].flags & I2C_M_RD)) {
1557 if (i2c_smbus_check_pec(partial_pec, &msg[num-1]) < 0)
1558 return -1;
1559 }
1560
1da177e4
LT
1561 if (read_write == I2C_SMBUS_READ)
1562 switch(size) {
1563 case I2C_SMBUS_BYTE:
1564 data->byte = msgbuf0[0];
1565 break;
1566 case I2C_SMBUS_BYTE_DATA:
1567 data->byte = msgbuf1[0];
1568 break;
438d6c2c 1569 case I2C_SMBUS_WORD_DATA:
1da177e4
LT
1570 case I2C_SMBUS_PROC_CALL:
1571 data->word = msgbuf1[0] | (msgbuf1[1] << 8);
1572 break;
1573 case I2C_SMBUS_I2C_BLOCK_DATA:
4b2643d7 1574 for (i = 0; i < data->block[0]; i++)
1da177e4
LT
1575 data->block[i+1] = msgbuf1[i];
1576 break;
209d27c3
JD
1577 case I2C_SMBUS_BLOCK_DATA:
1578 case I2C_SMBUS_BLOCK_PROC_CALL:
1579 for (i = 0; i < msgbuf1[0] + 1; i++)
1580 data->block[i] = msgbuf1[i];
1581 break;
1da177e4
LT
1582 }
1583 return 0;
1584}
1585
1586
1587s32 i2c_smbus_xfer(struct i2c_adapter * adapter, u16 addr, unsigned short flags,
438d6c2c 1588 char read_write, u8 command, int size,
1da177e4
LT
1589 union i2c_smbus_data * data)
1590{
1591 s32 res;
1da177e4
LT
1592
1593 flags &= I2C_M_TEN | I2C_CLIENT_PEC;
1da177e4
LT
1594
1595 if (adapter->algo->smbus_xfer) {
5c085d36 1596 mutex_lock(&adapter->bus_lock);
1da177e4
LT
1597 res = adapter->algo->smbus_xfer(adapter,addr,flags,read_write,
1598 command,size,data);
5c085d36 1599 mutex_unlock(&adapter->bus_lock);
1da177e4
LT
1600 } else
1601 res = i2c_smbus_xfer_emulated(adapter,addr,flags,read_write,
1602 command,size,data);
1603
1da177e4
LT
1604 return res;
1605}
1da177e4 1606EXPORT_SYMBOL(i2c_smbus_xfer);
1da177e4
LT
1607
1608MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
1609MODULE_DESCRIPTION("I2C-Bus main module");
1610MODULE_LICENSE("GPL");