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