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