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