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