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