blob: 8d1f644a7fdc55bae4cfef394f6e06a8cbd8d9b6 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* 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
Jean Delvare5694f8a2012-03-26 21:47:19 +020017 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
18 MA 02110-1301 USA. */
Linus Torvalds1da177e2005-04-16 15:20:36 -070019/* ------------------------------------------------------------------------- */
20
Jan Engelhardt96de0e22007-10-19 23:21:04 +020021/* With some changes from Kyösti Mälkki <kmalkki@cc.hut.fi>.
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 All SMBus-related things are written by Frodo Looijaard <frodol@dds.nl>
Jean Delvare421ef472005-10-26 21:28:55 +020023 SMBus 2.0 support by Mark Studebaker <mdsxyz123@yahoo.com> and
Michael Lawnick08263742010-08-11 18:21:02 +020024 Jean Delvare <khali@linux-fr.org>
25 Mux support by Rodolfo Giometti <giometti@enneenne.com> and
26 Michael Lawnick <michael.lawnick.ext@nsn.com> */
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/module.h>
29#include <linux/kernel.h>
30#include <linux/errno.h>
31#include <linux/slab.h>
32#include <linux/i2c.h>
33#include <linux/init.h>
34#include <linux/idr.h>
Arjan van de Venb3585e42006-01-11 10:50:26 +010035#include <linux/mutex.h>
Grant Likely959e85f2010-06-08 07:48:19 -060036#include <linux/of_device.h>
Jean Delvareb8d6f452007-02-13 22:09:00 +010037#include <linux/completion.h>
Mike Rapoportcea443a2008-01-27 18:14:50 +010038#include <linux/hardirq.h>
39#include <linux/irqflags.h>
Rodolfo Giomettif18c41d2009-06-19 16:58:20 +020040#include <linux/rwsem.h>
Mark Brown6de468a2010-03-02 12:23:46 +010041#include <linux/pm_runtime.h>
Mika Westerberg907ddf82012-11-23 12:23:40 +010042#include <linux/acpi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <asm/uaccess.h>
44
David Brownell9c1600e2007-05-01 23:26:31 +020045#include "i2c-core.h"
46
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Jean Delvare6629dcf2010-05-04 11:09:28 +020048/* core_lock protects i2c_adapter_idr, and guarantees
Jean Delvare35fc37f2009-06-19 16:58:19 +020049 that device detection, deletion of detected devices, and attach_adapter
50 and detach_adapter calls are serialized */
Jean Delvarecaada322008-01-27 18:14:49 +010051static DEFINE_MUTEX(core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070052static DEFINE_IDR(i2c_adapter_idr);
53
Jean Delvare4f8cf822009-09-18 22:45:46 +020054static struct device_type i2c_client_type;
Jean Delvare4735c982008-07-14 22:38:36 +020055static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver);
David Brownellf37dd802007-02-13 22:09:00 +010056
57/* ------------------------------------------------------------------------- */
58
Jean Delvared2653e92008-04-29 23:11:39 +020059static const struct i2c_device_id *i2c_match_id(const struct i2c_device_id *id,
60 const struct i2c_client *client)
61{
62 while (id->name[0]) {
63 if (strcmp(client->name, id->name) == 0)
64 return id;
65 id++;
66 }
67 return NULL;
68}
69
Linus Torvalds1da177e2005-04-16 15:20:36 -070070static int i2c_device_match(struct device *dev, struct device_driver *drv)
71{
Jean Delvare51298d12009-09-18 22:45:45 +020072 struct i2c_client *client = i2c_verify_client(dev);
73 struct i2c_driver *driver;
David Brownell7b4fbc52007-05-01 23:26:30 +020074
Jean Delvare51298d12009-09-18 22:45:45 +020075 if (!client)
76 return 0;
77
Grant Likely959e85f2010-06-08 07:48:19 -060078 /* Attempt an OF style match */
79 if (of_driver_match_device(dev, drv))
80 return 1;
81
Mika Westerberg907ddf82012-11-23 12:23:40 +010082 /* Then ACPI style match */
83 if (acpi_driver_match_device(dev, drv))
84 return 1;
85
Jean Delvare51298d12009-09-18 22:45:45 +020086 driver = to_i2c_driver(drv);
Jean Delvared2653e92008-04-29 23:11:39 +020087 /* match on an id table if there is one */
88 if (driver->id_table)
89 return i2c_match_id(driver->id_table, client) != NULL;
90
Jean Delvareeb8a7902008-05-18 20:49:41 +020091 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092}
93
David Brownell7b4fbc52007-05-01 23:26:30 +020094#ifdef CONFIG_HOTPLUG
95
96/* uevent helps with hotplug: modprobe -q $(MODALIAS) */
Kay Sievers7eff2e72007-08-14 15:15:12 +020097static int i2c_device_uevent(struct device *dev, struct kobj_uevent_env *env)
David Brownell7b4fbc52007-05-01 23:26:30 +020098{
99 struct i2c_client *client = to_i2c_client(dev);
David Brownell7b4fbc52007-05-01 23:26:30 +0200100
Jean Delvareeb8a7902008-05-18 20:49:41 +0200101 if (add_uevent_var(env, "MODALIAS=%s%s",
102 I2C_MODULE_PREFIX, client->name))
103 return -ENOMEM;
David Brownell7b4fbc52007-05-01 23:26:30 +0200104 dev_dbg(dev, "uevent\n");
105 return 0;
106}
107
108#else
109#define i2c_device_uevent NULL
110#endif /* CONFIG_HOTPLUG */
111
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112static int i2c_device_probe(struct device *dev)
113{
Jean Delvare51298d12009-09-18 22:45:45 +0200114 struct i2c_client *client = i2c_verify_client(dev);
115 struct i2c_driver *driver;
Hans Verkuil50c33042008-03-12 14:15:00 +0100116 int status;
David Brownell7b4fbc52007-05-01 23:26:30 +0200117
Jean Delvare51298d12009-09-18 22:45:45 +0200118 if (!client)
119 return 0;
120
121 driver = to_i2c_driver(dev->driver);
Jean Delvaree0457442008-07-14 22:38:30 +0200122 if (!driver->probe || !driver->id_table)
David Brownell7b4fbc52007-05-01 23:26:30 +0200123 return -ENODEV;
124 client->driver = driver;
Marc Pignatee354252008-08-28 08:33:22 +0200125 if (!device_can_wakeup(&client->dev))
126 device_init_wakeup(&client->dev,
127 client->flags & I2C_CLIENT_WAKE);
David Brownell7b4fbc52007-05-01 23:26:30 +0200128 dev_dbg(dev, "probe\n");
Jean Delvared2653e92008-04-29 23:11:39 +0200129
Jean Delvaree0457442008-07-14 22:38:30 +0200130 status = driver->probe(client, i2c_match_id(driver->id_table, client));
Wolfram Sange4a7b9b2010-05-04 11:09:27 +0200131 if (status) {
Hans Verkuil50c33042008-03-12 14:15:00 +0100132 client->driver = NULL;
Wolfram Sange4a7b9b2010-05-04 11:09:27 +0200133 i2c_set_clientdata(client, NULL);
134 }
Hans Verkuil50c33042008-03-12 14:15:00 +0100135 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136}
137
138static int i2c_device_remove(struct device *dev)
139{
Jean Delvare51298d12009-09-18 22:45:45 +0200140 struct i2c_client *client = i2c_verify_client(dev);
David Brownella1d9e6e2007-05-01 23:26:30 +0200141 struct i2c_driver *driver;
142 int status;
143
Jean Delvare51298d12009-09-18 22:45:45 +0200144 if (!client || !dev->driver)
David Brownella1d9e6e2007-05-01 23:26:30 +0200145 return 0;
146
147 driver = to_i2c_driver(dev->driver);
148 if (driver->remove) {
149 dev_dbg(dev, "remove\n");
150 status = driver->remove(client);
151 } else {
152 dev->driver = NULL;
153 status = 0;
154 }
Wolfram Sange4a7b9b2010-05-04 11:09:27 +0200155 if (status == 0) {
David Brownella1d9e6e2007-05-01 23:26:30 +0200156 client->driver = NULL;
Wolfram Sange4a7b9b2010-05-04 11:09:27 +0200157 i2c_set_clientdata(client, NULL);
158 }
David Brownella1d9e6e2007-05-01 23:26:30 +0200159 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160}
161
David Brownellf37dd802007-02-13 22:09:00 +0100162static void i2c_device_shutdown(struct device *dev)
163{
Jean Delvare51298d12009-09-18 22:45:45 +0200164 struct i2c_client *client = i2c_verify_client(dev);
David Brownellf37dd802007-02-13 22:09:00 +0100165 struct i2c_driver *driver;
166
Jean Delvare51298d12009-09-18 22:45:45 +0200167 if (!client || !dev->driver)
David Brownellf37dd802007-02-13 22:09:00 +0100168 return;
169 driver = to_i2c_driver(dev->driver);
170 if (driver->shutdown)
Jean Delvare51298d12009-09-18 22:45:45 +0200171 driver->shutdown(client);
David Brownellf37dd802007-02-13 22:09:00 +0100172}
173
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200174#ifdef CONFIG_PM_SLEEP
175static int i2c_legacy_suspend(struct device *dev, pm_message_t mesg)
David Brownellf37dd802007-02-13 22:09:00 +0100176{
Jean Delvare51298d12009-09-18 22:45:45 +0200177 struct i2c_client *client = i2c_verify_client(dev);
David Brownellf37dd802007-02-13 22:09:00 +0100178 struct i2c_driver *driver;
179
Jean Delvare51298d12009-09-18 22:45:45 +0200180 if (!client || !dev->driver)
David Brownellf37dd802007-02-13 22:09:00 +0100181 return 0;
182 driver = to_i2c_driver(dev->driver);
183 if (!driver->suspend)
184 return 0;
Jean Delvare51298d12009-09-18 22:45:45 +0200185 return driver->suspend(client, mesg);
David Brownellf37dd802007-02-13 22:09:00 +0100186}
187
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200188static int i2c_legacy_resume(struct device *dev)
David Brownellf37dd802007-02-13 22:09:00 +0100189{
Jean Delvare51298d12009-09-18 22:45:45 +0200190 struct i2c_client *client = i2c_verify_client(dev);
David Brownellf37dd802007-02-13 22:09:00 +0100191 struct i2c_driver *driver;
192
Jean Delvare51298d12009-09-18 22:45:45 +0200193 if (!client || !dev->driver)
David Brownellf37dd802007-02-13 22:09:00 +0100194 return 0;
195 driver = to_i2c_driver(dev->driver);
196 if (!driver->resume)
197 return 0;
Jean Delvare51298d12009-09-18 22:45:45 +0200198 return driver->resume(client);
David Brownellf37dd802007-02-13 22:09:00 +0100199}
200
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200201static int i2c_device_pm_suspend(struct device *dev)
202{
203 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
204
Mark Brownd529de22011-01-14 22:03:49 +0100205 if (pm)
206 return pm_generic_suspend(dev);
207 else
208 return i2c_legacy_suspend(dev, PMSG_SUSPEND);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200209}
210
211static int i2c_device_pm_resume(struct device *dev)
212{
213 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200214
215 if (pm)
Mark Brownd529de22011-01-14 22:03:49 +0100216 return pm_generic_resume(dev);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200217 else
Mark Brownd529de22011-01-14 22:03:49 +0100218 return i2c_legacy_resume(dev);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200219}
220
221static int i2c_device_pm_freeze(struct device *dev)
222{
223 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
224
Mark Brownd529de22011-01-14 22:03:49 +0100225 if (pm)
226 return pm_generic_freeze(dev);
227 else
228 return i2c_legacy_suspend(dev, PMSG_FREEZE);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200229}
230
231static int i2c_device_pm_thaw(struct device *dev)
232{
233 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
234
Mark Brownd529de22011-01-14 22:03:49 +0100235 if (pm)
236 return pm_generic_thaw(dev);
237 else
238 return i2c_legacy_resume(dev);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200239}
240
241static int i2c_device_pm_poweroff(struct device *dev)
242{
243 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
244
Mark Brownd529de22011-01-14 22:03:49 +0100245 if (pm)
246 return pm_generic_poweroff(dev);
247 else
248 return i2c_legacy_suspend(dev, PMSG_HIBERNATE);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200249}
250
251static int i2c_device_pm_restore(struct device *dev)
252{
253 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200254
255 if (pm)
Mark Brownd529de22011-01-14 22:03:49 +0100256 return pm_generic_restore(dev);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200257 else
Mark Brownd529de22011-01-14 22:03:49 +0100258 return i2c_legacy_resume(dev);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200259}
260#else /* !CONFIG_PM_SLEEP */
261#define i2c_device_pm_suspend NULL
262#define i2c_device_pm_resume NULL
263#define i2c_device_pm_freeze NULL
264#define i2c_device_pm_thaw NULL
265#define i2c_device_pm_poweroff NULL
266#define i2c_device_pm_restore NULL
267#endif /* !CONFIG_PM_SLEEP */
268
David Brownell9c1600e2007-05-01 23:26:31 +0200269static void i2c_client_dev_release(struct device *dev)
270{
271 kfree(to_i2c_client(dev));
272}
273
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100274static ssize_t
Jean Delvare4f8cf822009-09-18 22:45:46 +0200275show_name(struct device *dev, struct device_attribute *attr, char *buf)
David Brownell7b4fbc52007-05-01 23:26:30 +0200276{
Jean Delvare4f8cf822009-09-18 22:45:46 +0200277 return sprintf(buf, "%s\n", dev->type == &i2c_client_type ?
278 to_i2c_client(dev)->name : to_i2c_adapter(dev)->name);
David Brownell7b4fbc52007-05-01 23:26:30 +0200279}
280
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100281static ssize_t
282show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
David Brownell7b4fbc52007-05-01 23:26:30 +0200283{
284 struct i2c_client *client = to_i2c_client(dev);
Jean Delvareeb8a7902008-05-18 20:49:41 +0200285 return sprintf(buf, "%s%s\n", I2C_MODULE_PREFIX, client->name);
David Brownell7b4fbc52007-05-01 23:26:30 +0200286}
287
Jean Delvare4f8cf822009-09-18 22:45:46 +0200288static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
Jean Delvare51298d12009-09-18 22:45:45 +0200289static DEVICE_ATTR(modalias, S_IRUGO, show_modalias, NULL);
290
291static struct attribute *i2c_dev_attrs[] = {
292 &dev_attr_name.attr,
David Brownell7b4fbc52007-05-01 23:26:30 +0200293 /* modalias helps coldplug: modprobe $(cat .../modalias) */
Jean Delvare51298d12009-09-18 22:45:45 +0200294 &dev_attr_modalias.attr,
295 NULL
296};
297
298static struct attribute_group i2c_dev_attr_group = {
299 .attrs = i2c_dev_attrs,
300};
301
302static const struct attribute_group *i2c_dev_attr_groups[] = {
303 &i2c_dev_attr_group,
304 NULL
David Brownell7b4fbc52007-05-01 23:26:30 +0200305};
306
Tobias Klauser0b2c3682010-01-16 20:43:12 +0100307static const struct dev_pm_ops i2c_device_pm_ops = {
sonic zhang54067ee2009-12-14 21:17:30 +0100308 .suspend = i2c_device_pm_suspend,
309 .resume = i2c_device_pm_resume,
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200310 .freeze = i2c_device_pm_freeze,
311 .thaw = i2c_device_pm_thaw,
312 .poweroff = i2c_device_pm_poweroff,
313 .restore = i2c_device_pm_restore,
314 SET_RUNTIME_PM_OPS(
315 pm_generic_runtime_suspend,
316 pm_generic_runtime_resume,
317 pm_generic_runtime_idle
318 )
sonic zhang54067ee2009-12-14 21:17:30 +0100319};
320
Jon Smirle9ca9eb2008-07-14 22:38:35 +0200321struct bus_type i2c_bus_type = {
David Brownellf37dd802007-02-13 22:09:00 +0100322 .name = "i2c",
323 .match = i2c_device_match,
324 .probe = i2c_device_probe,
325 .remove = i2c_device_remove,
326 .shutdown = i2c_device_shutdown,
sonic zhang54067ee2009-12-14 21:17:30 +0100327 .pm = &i2c_device_pm_ops,
Russell Kingb864c7d2006-01-05 14:37:50 +0000328};
Jon Smirle9ca9eb2008-07-14 22:38:35 +0200329EXPORT_SYMBOL_GPL(i2c_bus_type);
Russell Kingb864c7d2006-01-05 14:37:50 +0000330
Jean Delvare51298d12009-09-18 22:45:45 +0200331static struct device_type i2c_client_type = {
332 .groups = i2c_dev_attr_groups,
333 .uevent = i2c_device_uevent,
334 .release = i2c_client_dev_release,
335};
336
David Brownell9b766b82008-01-27 18:14:51 +0100337
338/**
339 * i2c_verify_client - return parameter as i2c_client, or NULL
340 * @dev: device, probably from some driver model iterator
341 *
342 * When traversing the driver model tree, perhaps using driver model
343 * iterators like @device_for_each_child(), you can't assume very much
344 * about the nodes you find. Use this function to avoid oopses caused
345 * by wrongly treating some non-I2C device as an i2c_client.
346 */
347struct i2c_client *i2c_verify_client(struct device *dev)
348{
Jean Delvare51298d12009-09-18 22:45:45 +0200349 return (dev->type == &i2c_client_type)
David Brownell9b766b82008-01-27 18:14:51 +0100350 ? to_i2c_client(dev)
351 : NULL;
352}
353EXPORT_SYMBOL(i2c_verify_client);
354
355
Jean Delvare3a89db52010-06-03 11:33:52 +0200356/* This is a permissive address validity check, I2C address map constraints
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300357 * are purposely not enforced, except for the general call address. */
Jean Delvare3a89db52010-06-03 11:33:52 +0200358static int i2c_check_client_addr_validity(const struct i2c_client *client)
359{
360 if (client->flags & I2C_CLIENT_TEN) {
361 /* 10-bit address, all values are valid */
362 if (client->addr > 0x3ff)
363 return -EINVAL;
364 } else {
365 /* 7-bit address, reject the general call address */
366 if (client->addr == 0x00 || client->addr > 0x7f)
367 return -EINVAL;
368 }
369 return 0;
370}
371
Jean Delvare656b8762010-06-03 11:33:53 +0200372/* And this is a strict address validity check, used when probing. If a
373 * device uses a reserved address, then it shouldn't be probed. 7-bit
374 * addressing is assumed, 10-bit address devices are rare and should be
375 * explicitly enumerated. */
376static int i2c_check_addr_validity(unsigned short addr)
377{
378 /*
379 * Reserved addresses per I2C specification:
380 * 0x00 General call address / START byte
381 * 0x01 CBUS address
382 * 0x02 Reserved for different bus format
383 * 0x03 Reserved for future purposes
384 * 0x04-0x07 Hs-mode master code
385 * 0x78-0x7b 10-bit slave addressing
386 * 0x7c-0x7f Reserved for future purposes
387 */
388 if (addr < 0x08 || addr > 0x77)
389 return -EINVAL;
390 return 0;
391}
392
Jean Delvare3b5f7942010-06-03 11:33:55 +0200393static int __i2c_check_addr_busy(struct device *dev, void *addrp)
394{
395 struct i2c_client *client = i2c_verify_client(dev);
396 int addr = *(int *)addrp;
397
398 if (client && client->addr == addr)
399 return -EBUSY;
400 return 0;
401}
402
Michael Lawnick08263742010-08-11 18:21:02 +0200403/* walk up mux tree */
404static int i2c_check_mux_parents(struct i2c_adapter *adapter, int addr)
405{
Jean Delvare97cc4d42010-10-24 18:16:57 +0200406 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
Michael Lawnick08263742010-08-11 18:21:02 +0200407 int result;
408
409 result = device_for_each_child(&adapter->dev, &addr,
410 __i2c_check_addr_busy);
411
Jean Delvare97cc4d42010-10-24 18:16:57 +0200412 if (!result && parent)
413 result = i2c_check_mux_parents(parent, addr);
Michael Lawnick08263742010-08-11 18:21:02 +0200414
415 return result;
416}
417
418/* recurse down mux tree */
419static int i2c_check_mux_children(struct device *dev, void *addrp)
420{
421 int result;
422
423 if (dev->type == &i2c_adapter_type)
424 result = device_for_each_child(dev, addrp,
425 i2c_check_mux_children);
426 else
427 result = __i2c_check_addr_busy(dev, addrp);
428
429 return result;
430}
431
Jean Delvare3b5f7942010-06-03 11:33:55 +0200432static int i2c_check_addr_busy(struct i2c_adapter *adapter, int addr)
433{
Jean Delvare97cc4d42010-10-24 18:16:57 +0200434 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
Michael Lawnick08263742010-08-11 18:21:02 +0200435 int result = 0;
436
Jean Delvare97cc4d42010-10-24 18:16:57 +0200437 if (parent)
438 result = i2c_check_mux_parents(parent, addr);
Michael Lawnick08263742010-08-11 18:21:02 +0200439
440 if (!result)
441 result = device_for_each_child(&adapter->dev, &addr,
442 i2c_check_mux_children);
443
444 return result;
Jean Delvare3b5f7942010-06-03 11:33:55 +0200445}
446
David Brownell9c1600e2007-05-01 23:26:31 +0200447/**
Jean Delvarefe61e072010-08-11 18:20:58 +0200448 * i2c_lock_adapter - Get exclusive access to an I2C bus segment
449 * @adapter: Target I2C bus segment
450 */
451void i2c_lock_adapter(struct i2c_adapter *adapter)
452{
Jean Delvare97cc4d42010-10-24 18:16:57 +0200453 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
454
455 if (parent)
456 i2c_lock_adapter(parent);
Michael Lawnick08263742010-08-11 18:21:02 +0200457 else
458 rt_mutex_lock(&adapter->bus_lock);
Jean Delvarefe61e072010-08-11 18:20:58 +0200459}
460EXPORT_SYMBOL_GPL(i2c_lock_adapter);
461
462/**
463 * i2c_trylock_adapter - Try to get exclusive access to an I2C bus segment
464 * @adapter: Target I2C bus segment
465 */
466static int i2c_trylock_adapter(struct i2c_adapter *adapter)
467{
Jean Delvare97cc4d42010-10-24 18:16:57 +0200468 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
469
470 if (parent)
471 return i2c_trylock_adapter(parent);
Michael Lawnick08263742010-08-11 18:21:02 +0200472 else
473 return rt_mutex_trylock(&adapter->bus_lock);
Jean Delvarefe61e072010-08-11 18:20:58 +0200474}
475
476/**
477 * i2c_unlock_adapter - Release exclusive access to an I2C bus segment
478 * @adapter: Target I2C bus segment
479 */
480void i2c_unlock_adapter(struct i2c_adapter *adapter)
481{
Jean Delvare97cc4d42010-10-24 18:16:57 +0200482 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
483
484 if (parent)
485 i2c_unlock_adapter(parent);
Michael Lawnick08263742010-08-11 18:21:02 +0200486 else
487 rt_mutex_unlock(&adapter->bus_lock);
Jean Delvarefe61e072010-08-11 18:20:58 +0200488}
489EXPORT_SYMBOL_GPL(i2c_unlock_adapter);
490
491/**
Jean Delvaref8a227e2009-06-19 16:58:18 +0200492 * i2c_new_device - instantiate an i2c device
David Brownell9c1600e2007-05-01 23:26:31 +0200493 * @adap: the adapter managing the device
494 * @info: describes one I2C device; bus_num is ignored
David Brownelld64f73b2007-07-12 14:12:28 +0200495 * Context: can sleep
David Brownell9c1600e2007-05-01 23:26:31 +0200496 *
Jean Delvaref8a227e2009-06-19 16:58:18 +0200497 * Create an i2c device. Binding is handled through driver model
498 * probe()/remove() methods. A driver may be bound to this device when we
499 * return from this function, or any later moment (e.g. maybe hotplugging will
500 * load the driver module). This call is not appropriate for use by mainboard
501 * initialization logic, which usually runs during an arch_initcall() long
502 * before any i2c_adapter could exist.
David Brownell9c1600e2007-05-01 23:26:31 +0200503 *
504 * This returns the new i2c client, which may be saved for later use with
505 * i2c_unregister_device(); or NULL to indicate an error.
506 */
507struct i2c_client *
508i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
509{
510 struct i2c_client *client;
511 int status;
512
513 client = kzalloc(sizeof *client, GFP_KERNEL);
514 if (!client)
515 return NULL;
516
517 client->adapter = adap;
518
519 client->dev.platform_data = info->platform_data;
David Brownell3bbb8352007-10-13 23:56:29 +0200520
Anton Vorontsov11f1f2a2008-10-22 20:21:33 +0200521 if (info->archdata)
522 client->dev.archdata = *info->archdata;
523
Marc Pignatee354252008-08-28 08:33:22 +0200524 client->flags = info->flags;
David Brownell9c1600e2007-05-01 23:26:31 +0200525 client->addr = info->addr;
526 client->irq = info->irq;
527
David Brownell9c1600e2007-05-01 23:26:31 +0200528 strlcpy(client->name, info->type, sizeof(client->name));
529
Jean Delvare3a89db52010-06-03 11:33:52 +0200530 /* Check for address validity */
531 status = i2c_check_client_addr_validity(client);
532 if (status) {
533 dev_err(&adap->dev, "Invalid %d-bit I2C address 0x%02hx\n",
534 client->flags & I2C_CLIENT_TEN ? 10 : 7, client->addr);
535 goto out_err_silent;
536 }
537
Jean Delvaref8a227e2009-06-19 16:58:18 +0200538 /* Check for address business */
Jean Delvare3b5f7942010-06-03 11:33:55 +0200539 status = i2c_check_addr_busy(adap, client->addr);
Jean Delvaref8a227e2009-06-19 16:58:18 +0200540 if (status)
541 goto out_err;
542
543 client->dev.parent = &client->adapter->dev;
544 client->dev.bus = &i2c_bus_type;
Jean Delvare51298d12009-09-18 22:45:45 +0200545 client->dev.type = &i2c_client_type;
Grant Likelyd12d42f2010-04-13 16:12:28 -0700546 client->dev.of_node = info->of_node;
Mika Westerberg907ddf82012-11-23 12:23:40 +0100547 ACPI_HANDLE_SET(&client->dev, info->acpi_node.handle);
Jean Delvaref8a227e2009-06-19 16:58:18 +0200548
Jean Delvarecbb44512011-11-23 11:33:07 +0100549 /* For 10-bit clients, add an arbitrary offset to avoid collisions */
Jean Delvaref8a227e2009-06-19 16:58:18 +0200550 dev_set_name(&client->dev, "%d-%04x", i2c_adapter_id(adap),
Jean Delvarecbb44512011-11-23 11:33:07 +0100551 client->addr | ((client->flags & I2C_CLIENT_TEN)
552 ? 0xa000 : 0));
Jean Delvaref8a227e2009-06-19 16:58:18 +0200553 status = device_register(&client->dev);
554 if (status)
555 goto out_err;
556
Jean Delvaref8a227e2009-06-19 16:58:18 +0200557 dev_dbg(&adap->dev, "client [%s] registered with bus id %s\n",
558 client->name, dev_name(&client->dev));
559
David Brownell9c1600e2007-05-01 23:26:31 +0200560 return client;
Jean Delvaref8a227e2009-06-19 16:58:18 +0200561
562out_err:
563 dev_err(&adap->dev, "Failed to register i2c client %s at 0x%02x "
564 "(%d)\n", client->name, client->addr, status);
Jean Delvare3a89db52010-06-03 11:33:52 +0200565out_err_silent:
Jean Delvaref8a227e2009-06-19 16:58:18 +0200566 kfree(client);
567 return NULL;
David Brownell9c1600e2007-05-01 23:26:31 +0200568}
569EXPORT_SYMBOL_GPL(i2c_new_device);
570
571
572/**
573 * i2c_unregister_device - reverse effect of i2c_new_device()
574 * @client: value returned from i2c_new_device()
David Brownelld64f73b2007-07-12 14:12:28 +0200575 * Context: can sleep
David Brownell9c1600e2007-05-01 23:26:31 +0200576 */
577void i2c_unregister_device(struct i2c_client *client)
David Brownella1d9e6e2007-05-01 23:26:30 +0200578{
David Brownella1d9e6e2007-05-01 23:26:30 +0200579 device_unregister(&client->dev);
580}
David Brownell9c1600e2007-05-01 23:26:31 +0200581EXPORT_SYMBOL_GPL(i2c_unregister_device);
David Brownella1d9e6e2007-05-01 23:26:30 +0200582
583
Jean Delvare60b129d2008-05-11 20:37:06 +0200584static const struct i2c_device_id dummy_id[] = {
585 { "dummy", 0 },
586 { },
587};
588
Jean Delvared2653e92008-04-29 23:11:39 +0200589static int dummy_probe(struct i2c_client *client,
590 const struct i2c_device_id *id)
591{
592 return 0;
593}
594
595static int dummy_remove(struct i2c_client *client)
David Brownelle9f13732008-01-27 18:14:52 +0100596{
597 return 0;
598}
599
600static struct i2c_driver dummy_driver = {
601 .driver.name = "dummy",
Jean Delvared2653e92008-04-29 23:11:39 +0200602 .probe = dummy_probe,
603 .remove = dummy_remove,
Jean Delvare60b129d2008-05-11 20:37:06 +0200604 .id_table = dummy_id,
David Brownelle9f13732008-01-27 18:14:52 +0100605};
606
607/**
608 * i2c_new_dummy - return a new i2c device bound to a dummy driver
609 * @adapter: the adapter managing the device
610 * @address: seven bit address to be used
David Brownelle9f13732008-01-27 18:14:52 +0100611 * Context: can sleep
612 *
613 * This returns an I2C client bound to the "dummy" driver, intended for use
614 * with devices that consume multiple addresses. Examples of such chips
615 * include various EEPROMS (like 24c04 and 24c08 models).
616 *
617 * These dummy devices have two main uses. First, most I2C and SMBus calls
618 * except i2c_transfer() need a client handle; the dummy will be that handle.
619 * And second, this prevents the specified address from being bound to a
620 * different driver.
621 *
622 * This returns the new i2c client, which should be saved for later use with
623 * i2c_unregister_device(); or NULL to indicate an error.
624 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100625struct i2c_client *i2c_new_dummy(struct i2c_adapter *adapter, u16 address)
David Brownelle9f13732008-01-27 18:14:52 +0100626{
627 struct i2c_board_info info = {
Jean Delvare60b129d2008-05-11 20:37:06 +0200628 I2C_BOARD_INFO("dummy", address),
David Brownelle9f13732008-01-27 18:14:52 +0100629 };
630
David Brownelle9f13732008-01-27 18:14:52 +0100631 return i2c_new_device(adapter, &info);
632}
633EXPORT_SYMBOL_GPL(i2c_new_dummy);
634
David Brownellf37dd802007-02-13 22:09:00 +0100635/* ------------------------------------------------------------------------- */
636
David Brownell16ffadf2007-05-01 23:26:28 +0200637/* I2C bus adapters -- one roots each I2C or SMBUS segment */
638
Adrian Bunk83eaaed2007-10-13 23:56:30 +0200639static void i2c_adapter_dev_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640{
David Brownellef2c83212007-05-01 23:26:28 +0200641 struct i2c_adapter *adap = to_i2c_adapter(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 complete(&adap->dev_released);
643}
644
Jean Delvare99cd8e22009-06-19 16:58:20 +0200645/*
Jean Delvare390946b2012-09-10 10:14:02 +0200646 * This function is only needed for mutex_lock_nested, so it is never
647 * called unless locking correctness checking is enabled. Thus we
648 * make it inline to avoid a compiler warning. That's what gcc ends up
649 * doing anyway.
650 */
651static inline unsigned int i2c_adapter_depth(struct i2c_adapter *adapter)
652{
653 unsigned int depth = 0;
654
655 while ((adapter = i2c_parent_is_i2c_adapter(adapter)))
656 depth++;
657
658 return depth;
659}
660
661/*
Jean Delvare99cd8e22009-06-19 16:58:20 +0200662 * Let users instantiate I2C devices through sysfs. This can be used when
663 * platform initialization code doesn't contain the proper data for
664 * whatever reason. Also useful for drivers that do device detection and
665 * detection fails, either because the device uses an unexpected address,
666 * or this is a compatible device with different ID register values.
667 *
668 * Parameter checking may look overzealous, but we really don't want
669 * the user to provide incorrect parameters.
670 */
671static ssize_t
672i2c_sysfs_new_device(struct device *dev, struct device_attribute *attr,
673 const char *buf, size_t count)
674{
675 struct i2c_adapter *adap = to_i2c_adapter(dev);
676 struct i2c_board_info info;
677 struct i2c_client *client;
678 char *blank, end;
679 int res;
680
Jean Delvare99cd8e22009-06-19 16:58:20 +0200681 memset(&info, 0, sizeof(struct i2c_board_info));
682
683 blank = strchr(buf, ' ');
684 if (!blank) {
685 dev_err(dev, "%s: Missing parameters\n", "new_device");
686 return -EINVAL;
687 }
688 if (blank - buf > I2C_NAME_SIZE - 1) {
689 dev_err(dev, "%s: Invalid device name\n", "new_device");
690 return -EINVAL;
691 }
692 memcpy(info.type, buf, blank - buf);
693
694 /* Parse remaining parameters, reject extra parameters */
695 res = sscanf(++blank, "%hi%c", &info.addr, &end);
696 if (res < 1) {
697 dev_err(dev, "%s: Can't parse I2C address\n", "new_device");
698 return -EINVAL;
699 }
700 if (res > 1 && end != '\n') {
701 dev_err(dev, "%s: Extra parameters\n", "new_device");
702 return -EINVAL;
703 }
704
Jean Delvare99cd8e22009-06-19 16:58:20 +0200705 client = i2c_new_device(adap, &info);
706 if (!client)
Jean Delvare3a89db52010-06-03 11:33:52 +0200707 return -EINVAL;
Jean Delvare99cd8e22009-06-19 16:58:20 +0200708
709 /* Keep track of the added device */
Jean Delvaredafc50d2010-08-11 18:21:01 +0200710 mutex_lock(&adap->userspace_clients_lock);
Jean Delvare6629dcf2010-05-04 11:09:28 +0200711 list_add_tail(&client->detected, &adap->userspace_clients);
Jean Delvaredafc50d2010-08-11 18:21:01 +0200712 mutex_unlock(&adap->userspace_clients_lock);
Jean Delvare99cd8e22009-06-19 16:58:20 +0200713 dev_info(dev, "%s: Instantiated device %s at 0x%02hx\n", "new_device",
714 info.type, info.addr);
715
716 return count;
717}
718
719/*
720 * And of course let the users delete the devices they instantiated, if
721 * they got it wrong. This interface can only be used to delete devices
722 * instantiated by i2c_sysfs_new_device above. This guarantees that we
723 * don't delete devices to which some kernel code still has references.
724 *
725 * Parameter checking may look overzealous, but we really don't want
726 * the user to delete the wrong device.
727 */
728static ssize_t
729i2c_sysfs_delete_device(struct device *dev, struct device_attribute *attr,
730 const char *buf, size_t count)
731{
732 struct i2c_adapter *adap = to_i2c_adapter(dev);
733 struct i2c_client *client, *next;
734 unsigned short addr;
735 char end;
736 int res;
737
738 /* Parse parameters, reject extra parameters */
739 res = sscanf(buf, "%hi%c", &addr, &end);
740 if (res < 1) {
741 dev_err(dev, "%s: Can't parse I2C address\n", "delete_device");
742 return -EINVAL;
743 }
744 if (res > 1 && end != '\n') {
745 dev_err(dev, "%s: Extra parameters\n", "delete_device");
746 return -EINVAL;
747 }
748
749 /* Make sure the device was added through sysfs */
750 res = -ENOENT;
Jean Delvare390946b2012-09-10 10:14:02 +0200751 mutex_lock_nested(&adap->userspace_clients_lock,
752 i2c_adapter_depth(adap));
Jean Delvare6629dcf2010-05-04 11:09:28 +0200753 list_for_each_entry_safe(client, next, &adap->userspace_clients,
754 detected) {
755 if (client->addr == addr) {
Jean Delvare99cd8e22009-06-19 16:58:20 +0200756 dev_info(dev, "%s: Deleting device %s at 0x%02hx\n",
757 "delete_device", client->name, client->addr);
758
759 list_del(&client->detected);
760 i2c_unregister_device(client);
761 res = count;
762 break;
763 }
764 }
Jean Delvaredafc50d2010-08-11 18:21:01 +0200765 mutex_unlock(&adap->userspace_clients_lock);
Jean Delvare99cd8e22009-06-19 16:58:20 +0200766
767 if (res < 0)
768 dev_err(dev, "%s: Can't find device in list\n",
769 "delete_device");
770 return res;
771}
772
Jean Delvare4f8cf822009-09-18 22:45:46 +0200773static DEVICE_ATTR(new_device, S_IWUSR, NULL, i2c_sysfs_new_device);
774static DEVICE_ATTR(delete_device, S_IWUSR, NULL, i2c_sysfs_delete_device);
775
776static struct attribute *i2c_adapter_attrs[] = {
777 &dev_attr_name.attr,
778 &dev_attr_new_device.attr,
779 &dev_attr_delete_device.attr,
780 NULL
David Brownell16ffadf2007-05-01 23:26:28 +0200781};
782
Jean Delvare4f8cf822009-09-18 22:45:46 +0200783static struct attribute_group i2c_adapter_attr_group = {
784 .attrs = i2c_adapter_attrs,
785};
786
787static const struct attribute_group *i2c_adapter_attr_groups[] = {
788 &i2c_adapter_attr_group,
789 NULL
790};
791
Michael Lawnick08263742010-08-11 18:21:02 +0200792struct device_type i2c_adapter_type = {
Jean Delvare4f8cf822009-09-18 22:45:46 +0200793 .groups = i2c_adapter_attr_groups,
794 .release = i2c_adapter_dev_release,
David Brownell16ffadf2007-05-01 23:26:28 +0200795};
Michael Lawnick08263742010-08-11 18:21:02 +0200796EXPORT_SYMBOL_GPL(i2c_adapter_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
Stephen Warren643dd092012-04-17 12:43:33 -0600798/**
799 * i2c_verify_adapter - return parameter as i2c_adapter or NULL
800 * @dev: device, probably from some driver model iterator
801 *
802 * When traversing the driver model tree, perhaps using driver model
803 * iterators like @device_for_each_child(), you can't assume very much
804 * about the nodes you find. Use this function to avoid oopses caused
805 * by wrongly treating some non-I2C device as an i2c_adapter.
806 */
807struct i2c_adapter *i2c_verify_adapter(struct device *dev)
808{
809 return (dev->type == &i2c_adapter_type)
810 ? to_i2c_adapter(dev)
811 : NULL;
812}
813EXPORT_SYMBOL(i2c_verify_adapter);
814
Jean Delvare2bb50952009-09-18 22:45:46 +0200815#ifdef CONFIG_I2C_COMPAT
816static struct class_compat *i2c_adapter_compat_class;
817#endif
818
David Brownell9c1600e2007-05-01 23:26:31 +0200819static void i2c_scan_static_board_info(struct i2c_adapter *adapter)
820{
821 struct i2c_devinfo *devinfo;
822
Rodolfo Giomettif18c41d2009-06-19 16:58:20 +0200823 down_read(&__i2c_board_lock);
David Brownell9c1600e2007-05-01 23:26:31 +0200824 list_for_each_entry(devinfo, &__i2c_board_list, list) {
825 if (devinfo->busnum == adapter->nr
826 && !i2c_new_device(adapter,
827 &devinfo->board_info))
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100828 dev_err(&adapter->dev,
829 "Can't create device at 0x%02x\n",
David Brownell9c1600e2007-05-01 23:26:31 +0200830 devinfo->board_info.addr);
831 }
Rodolfo Giomettif18c41d2009-06-19 16:58:20 +0200832 up_read(&__i2c_board_lock);
David Brownell9c1600e2007-05-01 23:26:31 +0200833}
834
Jean Delvare69b00892009-12-06 17:06:27 +0100835static int i2c_do_add_adapter(struct i2c_driver *driver,
836 struct i2c_adapter *adap)
Jean Delvare026526f2008-01-27 18:14:49 +0100837{
Jean Delvare4735c982008-07-14 22:38:36 +0200838 /* Detect supported devices on that bus, and instantiate them */
839 i2c_detect(adap, driver);
840
841 /* Let legacy drivers scan this bus for matching devices */
Jean Delvare026526f2008-01-27 18:14:49 +0100842 if (driver->attach_adapter) {
Jean Delvarea920ff42011-04-17 10:20:19 +0200843 dev_warn(&adap->dev, "%s: attach_adapter method is deprecated\n",
844 driver->driver.name);
Jean Delvarefe6fc252011-03-20 14:50:53 +0100845 dev_warn(&adap->dev, "Please use another way to instantiate "
846 "your i2c_client\n");
Jean Delvare026526f2008-01-27 18:14:49 +0100847 /* We ignore the return code; if it fails, too bad */
848 driver->attach_adapter(adap);
849 }
850 return 0;
851}
852
Jean Delvare69b00892009-12-06 17:06:27 +0100853static int __process_new_adapter(struct device_driver *d, void *data)
854{
855 return i2c_do_add_adapter(to_i2c_driver(d), data);
856}
857
David Brownell6e13e642007-05-01 23:26:31 +0200858static int i2c_register_adapter(struct i2c_adapter *adap)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859{
Jean Delvared6703282010-08-11 18:20:59 +0200860 int res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861
David Brownell1d0b19c2008-10-14 17:30:05 +0200862 /* Can't register until after driver model init */
Jean Delvare35fc37f2009-06-19 16:58:19 +0200863 if (unlikely(WARN_ON(!i2c_bus_type.p))) {
864 res = -EAGAIN;
865 goto out_list;
866 }
David Brownell1d0b19c2008-10-14 17:30:05 +0200867
Jean Delvare2236baa2010-11-15 22:40:38 +0100868 /* Sanity checks */
869 if (unlikely(adap->name[0] == '\0')) {
870 pr_err("i2c-core: Attempt to register an adapter with "
871 "no name!\n");
872 return -EINVAL;
873 }
874 if (unlikely(!adap->algo)) {
875 pr_err("i2c-core: Attempt to register adapter '%s' with "
876 "no algo!\n", adap->name);
877 return -EINVAL;
878 }
879
Mika Kuoppala194684e2009-12-06 17:06:22 +0100880 rt_mutex_init(&adap->bus_lock);
Jean Delvaredafc50d2010-08-11 18:21:01 +0200881 mutex_init(&adap->userspace_clients_lock);
Jean Delvare6629dcf2010-05-04 11:09:28 +0200882 INIT_LIST_HEAD(&adap->userspace_clients);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883
Jean Delvare8fcfef62009-03-28 21:34:43 +0100884 /* Set default timeout to 1 second if not already set */
885 if (adap->timeout == 0)
886 adap->timeout = HZ;
887
Kay Sievers27d9c182009-01-07 14:29:16 +0100888 dev_set_name(&adap->dev, "i2c-%d", adap->nr);
Jean Delvare4f8cf822009-09-18 22:45:46 +0200889 adap->dev.bus = &i2c_bus_type;
890 adap->dev.type = &i2c_adapter_type;
Jean Delvareb119c6c2006-08-15 18:26:30 +0200891 res = device_register(&adap->dev);
892 if (res)
893 goto out_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200895 dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
896
Jean Delvare2bb50952009-09-18 22:45:46 +0200897#ifdef CONFIG_I2C_COMPAT
898 res = class_compat_create_link(i2c_adapter_compat_class, &adap->dev,
899 adap->dev.parent);
900 if (res)
901 dev_warn(&adap->dev,
902 "Failed to create compatibility class link\n");
903#endif
904
Jean Delvare729d6dd2009-06-19 16:58:18 +0200905 /* create pre-declared device nodes */
David Brownell6e13e642007-05-01 23:26:31 +0200906 if (adap->nr < __i2c_first_dynamic_bus_num)
907 i2c_scan_static_board_info(adap);
908
Jean Delvare4735c982008-07-14 22:38:36 +0200909 /* Notify drivers */
Jean Delvare35fc37f2009-06-19 16:58:19 +0200910 mutex_lock(&core_lock);
Jean Delvared6703282010-08-11 18:20:59 +0200911 bus_for_each_drv(&i2c_bus_type, NULL, adap, __process_new_adapter);
Jean Delvarecaada322008-01-27 18:14:49 +0100912 mutex_unlock(&core_lock);
Jean Delvare35fc37f2009-06-19 16:58:19 +0200913
914 return 0;
Jean Delvareb119c6c2006-08-15 18:26:30 +0200915
Jean Delvareb119c6c2006-08-15 18:26:30 +0200916out_list:
Jean Delvare35fc37f2009-06-19 16:58:19 +0200917 mutex_lock(&core_lock);
Jean Delvareb119c6c2006-08-15 18:26:30 +0200918 idr_remove(&i2c_adapter_idr, adap->nr);
Jean Delvare35fc37f2009-06-19 16:58:19 +0200919 mutex_unlock(&core_lock);
920 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921}
922
David Brownell6e13e642007-05-01 23:26:31 +0200923/**
924 * i2c_add_adapter - declare i2c adapter, use dynamic bus number
925 * @adapter: the adapter to add
David Brownelld64f73b2007-07-12 14:12:28 +0200926 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +0200927 *
928 * This routine is used to declare an I2C adapter when its bus number
929 * doesn't matter. Examples: for I2C adapters dynamically added by
930 * USB links or PCI plugin cards.
931 *
932 * When this returns zero, a new bus number was allocated and stored
933 * in adap->nr, and the specified adapter became available for clients.
934 * Otherwise, a negative errno value is returned.
935 */
936int i2c_add_adapter(struct i2c_adapter *adapter)
937{
Tejun Heo4ae42b0f2013-02-27 17:04:15 -0800938 int id;
David Brownell6e13e642007-05-01 23:26:31 +0200939
Jean Delvarecaada322008-01-27 18:14:49 +0100940 mutex_lock(&core_lock);
Tejun Heo4ae42b0f2013-02-27 17:04:15 -0800941 id = idr_alloc(&i2c_adapter_idr, adapter,
942 __i2c_first_dynamic_bus_num, 0, GFP_KERNEL);
Jean Delvarecaada322008-01-27 18:14:49 +0100943 mutex_unlock(&core_lock);
Tejun Heo4ae42b0f2013-02-27 17:04:15 -0800944 if (id < 0)
945 return id;
David Brownell6e13e642007-05-01 23:26:31 +0200946
947 adapter->nr = id;
Tejun Heo4ae42b0f2013-02-27 17:04:15 -0800948
David Brownell6e13e642007-05-01 23:26:31 +0200949 return i2c_register_adapter(adapter);
950}
951EXPORT_SYMBOL(i2c_add_adapter);
952
953/**
954 * i2c_add_numbered_adapter - declare i2c adapter, use static bus number
955 * @adap: the adapter to register (with adap->nr initialized)
David Brownelld64f73b2007-07-12 14:12:28 +0200956 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +0200957 *
958 * This routine is used to declare an I2C adapter when its bus number
Randy Dunlap8c07e462008-03-23 20:28:20 +0100959 * matters. For example, use it for I2C adapters from system-on-chip CPUs,
960 * or otherwise built in to the system's mainboard, and where i2c_board_info
David Brownell6e13e642007-05-01 23:26:31 +0200961 * is used to properly configure I2C devices.
962 *
Grant Likely488bf312011-07-25 17:49:43 +0200963 * If the requested bus number is set to -1, then this function will behave
964 * identically to i2c_add_adapter, and will dynamically assign a bus number.
965 *
David Brownell6e13e642007-05-01 23:26:31 +0200966 * If no devices have pre-been declared for this bus, then be sure to
967 * register the adapter before any dynamically allocated ones. Otherwise
968 * the required bus ID may not be available.
969 *
970 * When this returns zero, the specified adapter became available for
971 * clients using the bus number provided in adap->nr. Also, the table
972 * of I2C devices pre-declared using i2c_register_board_info() is scanned,
973 * and the appropriate driver model device nodes are created. Otherwise, a
974 * negative errno value is returned.
975 */
976int i2c_add_numbered_adapter(struct i2c_adapter *adap)
977{
978 int id;
David Brownell6e13e642007-05-01 23:26:31 +0200979
Grant Likely488bf312011-07-25 17:49:43 +0200980 if (adap->nr == -1) /* -1 means dynamically assign bus id */
981 return i2c_add_adapter(adap);
Fengguang Wu125c4c72012-10-04 17:13:15 -0700982 if (adap->nr & ~MAX_IDR_MASK)
David Brownell6e13e642007-05-01 23:26:31 +0200983 return -EINVAL;
984
Jean Delvarecaada322008-01-27 18:14:49 +0100985 mutex_lock(&core_lock);
Tejun Heo4ae42b0f2013-02-27 17:04:15 -0800986 id = idr_alloc(&i2c_adapter_idr, adap, adap->nr, adap->nr + 1,
987 GFP_KERNEL);
Jean Delvarecaada322008-01-27 18:14:49 +0100988 mutex_unlock(&core_lock);
Tejun Heo4ae42b0f2013-02-27 17:04:15 -0800989 if (id < 0)
990 return id == -ENOSPC ? -EBUSY : id;
991 return i2c_register_adapter(adap);
David Brownell6e13e642007-05-01 23:26:31 +0200992}
993EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter);
994
Jean Delvare69b00892009-12-06 17:06:27 +0100995static int i2c_do_del_adapter(struct i2c_driver *driver,
996 struct i2c_adapter *adapter)
Jean Delvare026526f2008-01-27 18:14:49 +0100997{
Jean Delvare4735c982008-07-14 22:38:36 +0200998 struct i2c_client *client, *_n;
Jean Delvare026526f2008-01-27 18:14:49 +0100999 int res;
1000
Jean Delvareacec2112009-03-28 21:34:40 +01001001 /* Remove the devices we created ourselves as the result of hardware
1002 * probing (using a driver's detect method) */
Jean Delvare4735c982008-07-14 22:38:36 +02001003 list_for_each_entry_safe(client, _n, &driver->clients, detected) {
1004 if (client->adapter == adapter) {
1005 dev_dbg(&adapter->dev, "Removing %s at 0x%x\n",
1006 client->name, client->addr);
1007 list_del(&client->detected);
1008 i2c_unregister_device(client);
1009 }
1010 }
1011
Jean Delvare026526f2008-01-27 18:14:49 +01001012 if (!driver->detach_adapter)
1013 return 0;
Jean Delvarea920ff42011-04-17 10:20:19 +02001014 dev_warn(&adapter->dev, "%s: detach_adapter method is deprecated\n",
1015 driver->driver.name);
Jean Delvare026526f2008-01-27 18:14:49 +01001016 res = driver->detach_adapter(adapter);
1017 if (res)
1018 dev_err(&adapter->dev, "detach_adapter failed (%d) "
1019 "for driver [%s]\n", res, driver->driver.name);
1020 return res;
1021}
1022
Jean Delvaree549c2b2009-06-19 16:58:19 +02001023static int __unregister_client(struct device *dev, void *dummy)
1024{
1025 struct i2c_client *client = i2c_verify_client(dev);
Jean Delvare5219bf82011-01-14 22:03:49 +01001026 if (client && strcmp(client->name, "dummy"))
1027 i2c_unregister_device(client);
1028 return 0;
1029}
1030
1031static int __unregister_dummy(struct device *dev, void *dummy)
1032{
1033 struct i2c_client *client = i2c_verify_client(dev);
Jean Delvaree549c2b2009-06-19 16:58:19 +02001034 if (client)
1035 i2c_unregister_device(client);
1036 return 0;
1037}
1038
Jean Delvare69b00892009-12-06 17:06:27 +01001039static int __process_removed_adapter(struct device_driver *d, void *data)
1040{
1041 return i2c_do_del_adapter(to_i2c_driver(d), data);
1042}
1043
David Brownelld64f73b2007-07-12 14:12:28 +02001044/**
1045 * i2c_del_adapter - unregister I2C adapter
1046 * @adap: the adapter being unregistered
1047 * Context: can sleep
1048 *
1049 * This unregisters an I2C adapter which was previously registered
1050 * by @i2c_add_adapter or @i2c_add_numbered_adapter.
1051 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052int i2c_del_adapter(struct i2c_adapter *adap)
1053{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 int res = 0;
Jean Delvare35fc37f2009-06-19 16:58:19 +02001055 struct i2c_adapter *found;
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001056 struct i2c_client *client, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057
1058 /* First make sure that this adapter was ever added */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001059 mutex_lock(&core_lock);
1060 found = idr_find(&i2c_adapter_idr, adap->nr);
1061 mutex_unlock(&core_lock);
1062 if (found != adap) {
Jean Delvareb6d7b3d2005-07-31 19:02:53 +02001063 pr_debug("i2c-core: attempting to delete unregistered "
1064 "adapter [%s]\n", adap->name);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001065 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 }
1067
Jean Delvare026526f2008-01-27 18:14:49 +01001068 /* Tell drivers about this removal */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001069 mutex_lock(&core_lock);
Jean Delvare026526f2008-01-27 18:14:49 +01001070 res = bus_for_each_drv(&i2c_bus_type, NULL, adap,
Jean Delvare69b00892009-12-06 17:06:27 +01001071 __process_removed_adapter);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001072 mutex_unlock(&core_lock);
Jean Delvare026526f2008-01-27 18:14:49 +01001073 if (res)
Jean Delvare35fc37f2009-06-19 16:58:19 +02001074 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001076 /* Remove devices instantiated from sysfs */
Jean Delvare390946b2012-09-10 10:14:02 +02001077 mutex_lock_nested(&adap->userspace_clients_lock,
1078 i2c_adapter_depth(adap));
Jean Delvare6629dcf2010-05-04 11:09:28 +02001079 list_for_each_entry_safe(client, next, &adap->userspace_clients,
1080 detected) {
1081 dev_dbg(&adap->dev, "Removing %s at 0x%x\n", client->name,
1082 client->addr);
1083 list_del(&client->detected);
1084 i2c_unregister_device(client);
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001085 }
Jean Delvaredafc50d2010-08-11 18:21:01 +02001086 mutex_unlock(&adap->userspace_clients_lock);
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001087
Jean Delvaree549c2b2009-06-19 16:58:19 +02001088 /* Detach any active clients. This can't fail, thus we do not
Jean Delvare5219bf82011-01-14 22:03:49 +01001089 * check the returned value. This is a two-pass process, because
1090 * we can't remove the dummy devices during the first pass: they
1091 * could have been instantiated by real devices wishing to clean
1092 * them up properly, so we give them a chance to do that first. */
Jean Delvaree549c2b2009-06-19 16:58:19 +02001093 res = device_for_each_child(&adap->dev, NULL, __unregister_client);
Jean Delvare5219bf82011-01-14 22:03:49 +01001094 res = device_for_each_child(&adap->dev, NULL, __unregister_dummy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095
Jean Delvare2bb50952009-09-18 22:45:46 +02001096#ifdef CONFIG_I2C_COMPAT
1097 class_compat_remove_link(i2c_adapter_compat_class, &adap->dev,
1098 adap->dev.parent);
1099#endif
1100
Thadeu Lima de Souza Cascardoc5567522010-01-16 20:43:13 +01001101 /* device name is gone after device_unregister */
1102 dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
1103
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 /* clean up the sysfs representation */
1105 init_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 device_unregister(&adap->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107
1108 /* wait for sysfs to drop all references */
1109 wait_for_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110
David Brownell6e13e642007-05-01 23:26:31 +02001111 /* free bus id */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001112 mutex_lock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 idr_remove(&i2c_adapter_idr, adap->nr);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001114 mutex_unlock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115
Jean Delvarebd4bc3db2008-07-16 19:30:05 +02001116 /* Clear the device structure in case this adapter is ever going to be
1117 added again */
1118 memset(&adap->dev, 0, sizeof(adap->dev));
1119
Jean Delvare35fc37f2009-06-19 16:58:19 +02001120 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121}
David Brownellc0564602007-05-01 23:26:31 +02001122EXPORT_SYMBOL(i2c_del_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123
1124
David Brownell7b4fbc52007-05-01 23:26:30 +02001125/* ------------------------------------------------------------------------- */
1126
Jean Delvare7ae31482011-03-20 14:50:52 +01001127int i2c_for_each_dev(void *data, int (*fn)(struct device *, void *))
1128{
1129 int res;
1130
1131 mutex_lock(&core_lock);
1132 res = bus_for_each_dev(&i2c_bus_type, NULL, data, fn);
1133 mutex_unlock(&core_lock);
1134
1135 return res;
1136}
1137EXPORT_SYMBOL_GPL(i2c_for_each_dev);
1138
Jean Delvare69b00892009-12-06 17:06:27 +01001139static int __process_new_driver(struct device *dev, void *data)
Dave Young7f101a92008-07-14 22:38:19 +02001140{
Jean Delvare4f8cf822009-09-18 22:45:46 +02001141 if (dev->type != &i2c_adapter_type)
1142 return 0;
Jean Delvare69b00892009-12-06 17:06:27 +01001143 return i2c_do_add_adapter(data, to_i2c_adapter(dev));
Dave Young7f101a92008-07-14 22:38:19 +02001144}
1145
David Brownell7b4fbc52007-05-01 23:26:30 +02001146/*
1147 * An i2c_driver is used with one or more i2c_client (device) nodes to access
Jean Delvare729d6dd2009-06-19 16:58:18 +02001148 * i2c slave chips, on a bus instance associated with some i2c_adapter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 */
1150
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08001151int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152{
Jean Delvare7eebcb72006-02-05 23:28:21 +01001153 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154
David Brownell1d0b19c2008-10-14 17:30:05 +02001155 /* Can't register until after driver model init */
1156 if (unlikely(WARN_ON(!i2c_bus_type.p)))
1157 return -EAGAIN;
1158
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 /* add the driver to the list of i2c drivers in the driver core */
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08001160 driver->driver.owner = owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 driver->driver.bus = &i2c_bus_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162
Jean Delvare729d6dd2009-06-19 16:58:18 +02001163 /* When registration returns, the driver core
David Brownell6e13e642007-05-01 23:26:31 +02001164 * will have called probe() for all matching-but-unbound devices.
1165 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 res = driver_register(&driver->driver);
1167 if (res)
Jean Delvare7eebcb72006-02-05 23:28:21 +01001168 return res;
David Brownell438d6c22006-12-10 21:21:31 +01001169
Mark Brownf4e8db32011-01-14 22:03:50 +01001170 /* Drivers should switch to dev_pm_ops instead. */
1171 if (driver->suspend)
1172 pr_warn("i2c-core: driver [%s] using legacy suspend method\n",
1173 driver->driver.name);
1174 if (driver->resume)
1175 pr_warn("i2c-core: driver [%s] using legacy resume method\n",
1176 driver->driver.name);
1177
Laurent Riffard35d8b2e2005-11-26 20:34:05 +01001178 pr_debug("i2c-core: driver [%s] registered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179
Jean Delvare4735c982008-07-14 22:38:36 +02001180 INIT_LIST_HEAD(&driver->clients);
1181 /* Walk the adapters that are already present */
Jean Delvare7ae31482011-03-20 14:50:52 +01001182 i2c_for_each_dev(driver, __process_new_driver);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001183
Jean Delvare7eebcb72006-02-05 23:28:21 +01001184 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185}
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08001186EXPORT_SYMBOL(i2c_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187
Jean Delvare69b00892009-12-06 17:06:27 +01001188static int __process_removed_driver(struct device *dev, void *data)
Dave Young7f101a92008-07-14 22:38:19 +02001189{
Jean Delvare4f8cf822009-09-18 22:45:46 +02001190 if (dev->type != &i2c_adapter_type)
1191 return 0;
Jean Delvare69b00892009-12-06 17:06:27 +01001192 return i2c_do_del_adapter(data, to_i2c_adapter(dev));
Dave Young7f101a92008-07-14 22:38:19 +02001193}
1194
David Brownella1d9e6e2007-05-01 23:26:30 +02001195/**
1196 * i2c_del_driver - unregister I2C driver
1197 * @driver: the driver being unregistered
David Brownelld64f73b2007-07-12 14:12:28 +02001198 * Context: can sleep
David Brownella1d9e6e2007-05-01 23:26:30 +02001199 */
Jean Delvareb3e82092007-05-01 23:26:32 +02001200void i2c_del_driver(struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201{
Jean Delvare7ae31482011-03-20 14:50:52 +01001202 i2c_for_each_dev(driver, __process_removed_driver);
David Brownella1d9e6e2007-05-01 23:26:30 +02001203
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 driver_unregister(&driver->driver);
Laurent Riffard35d8b2e2005-11-26 20:34:05 +01001205 pr_debug("i2c-core: driver [%s] unregistered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206}
David Brownellc0564602007-05-01 23:26:31 +02001207EXPORT_SYMBOL(i2c_del_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208
David Brownell7b4fbc52007-05-01 23:26:30 +02001209/* ------------------------------------------------------------------------- */
1210
Jean Delvaree48d3312008-01-27 18:14:48 +01001211/**
1212 * i2c_use_client - increments the reference count of the i2c client structure
1213 * @client: the client being referenced
1214 *
1215 * Each live reference to a client should be refcounted. The driver model does
1216 * that automatically as part of driver binding, so that most drivers don't
1217 * need to do this explicitly: they hold a reference until they're unbound
1218 * from the device.
1219 *
1220 * A pointer to the client with the incremented reference counter is returned.
1221 */
1222struct i2c_client *i2c_use_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223{
David Brownell6ea438e2008-07-14 22:38:24 +02001224 if (client && get_device(&client->dev))
1225 return client;
1226 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227}
David Brownellc0564602007-05-01 23:26:31 +02001228EXPORT_SYMBOL(i2c_use_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229
Jean Delvaree48d3312008-01-27 18:14:48 +01001230/**
1231 * i2c_release_client - release a use of the i2c client structure
1232 * @client: the client being no longer referenced
1233 *
1234 * Must be called when a user of a client is finished with it.
1235 */
1236void i2c_release_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237{
David Brownell6ea438e2008-07-14 22:38:24 +02001238 if (client)
1239 put_device(&client->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240}
David Brownellc0564602007-05-01 23:26:31 +02001241EXPORT_SYMBOL(i2c_release_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242
David Brownell9b766b82008-01-27 18:14:51 +01001243struct i2c_cmd_arg {
1244 unsigned cmd;
1245 void *arg;
1246};
1247
1248static int i2c_cmd(struct device *dev, void *_arg)
1249{
1250 struct i2c_client *client = i2c_verify_client(dev);
1251 struct i2c_cmd_arg *arg = _arg;
1252
1253 if (client && client->driver && client->driver->command)
1254 client->driver->command(client, arg->cmd, arg->arg);
1255 return 0;
1256}
1257
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
1259{
David Brownell9b766b82008-01-27 18:14:51 +01001260 struct i2c_cmd_arg cmd_arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261
David Brownell9b766b82008-01-27 18:14:51 +01001262 cmd_arg.cmd = cmd;
1263 cmd_arg.arg = arg;
1264 device_for_each_child(&adap->dev, &cmd_arg, i2c_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265}
David Brownellc0564602007-05-01 23:26:31 +02001266EXPORT_SYMBOL(i2c_clients_command);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267
1268static int __init i2c_init(void)
1269{
1270 int retval;
1271
1272 retval = bus_register(&i2c_bus_type);
1273 if (retval)
1274 return retval;
Jean Delvare2bb50952009-09-18 22:45:46 +02001275#ifdef CONFIG_I2C_COMPAT
1276 i2c_adapter_compat_class = class_compat_register("i2c-adapter");
1277 if (!i2c_adapter_compat_class) {
1278 retval = -ENOMEM;
1279 goto bus_err;
1280 }
1281#endif
David Brownelle9f13732008-01-27 18:14:52 +01001282 retval = i2c_add_driver(&dummy_driver);
1283 if (retval)
Jean Delvare2bb50952009-09-18 22:45:46 +02001284 goto class_err;
David Brownelle9f13732008-01-27 18:14:52 +01001285 return 0;
1286
Jean Delvare2bb50952009-09-18 22:45:46 +02001287class_err:
1288#ifdef CONFIG_I2C_COMPAT
1289 class_compat_unregister(i2c_adapter_compat_class);
David Brownelle9f13732008-01-27 18:14:52 +01001290bus_err:
Jean Delvare2bb50952009-09-18 22:45:46 +02001291#endif
David Brownelle9f13732008-01-27 18:14:52 +01001292 bus_unregister(&i2c_bus_type);
1293 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294}
1295
1296static void __exit i2c_exit(void)
1297{
David Brownelle9f13732008-01-27 18:14:52 +01001298 i2c_del_driver(&dummy_driver);
Jean Delvare2bb50952009-09-18 22:45:46 +02001299#ifdef CONFIG_I2C_COMPAT
1300 class_compat_unregister(i2c_adapter_compat_class);
1301#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 bus_unregister(&i2c_bus_type);
1303}
1304
David Brownella10f9e72008-10-14 17:30:06 +02001305/* We must initialize early, because some subsystems register i2c drivers
1306 * in subsys_initcall() code, but are linked (and initialized) before i2c.
1307 */
1308postcore_initcall(i2c_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309module_exit(i2c_exit);
1310
1311/* ----------------------------------------------------
1312 * the functional interface to the i2c busses.
1313 * ----------------------------------------------------
1314 */
1315
David Brownella1cdeda2008-07-14 22:38:24 +02001316/**
Jean Delvareb37d2a32012-06-29 07:47:19 -03001317 * __i2c_transfer - unlocked flavor of i2c_transfer
1318 * @adap: Handle to I2C bus
1319 * @msgs: One or more messages to execute before STOP is issued to
1320 * terminate the operation; each message begins with a START.
1321 * @num: Number of messages to be executed.
1322 *
1323 * Returns negative errno, else the number of messages executed.
1324 *
1325 * Adapter lock must be held when calling this function. No debug logging
1326 * takes place. adap->algo->master_xfer existence isn't checked.
1327 */
1328int __i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
1329{
1330 unsigned long orig_jiffies;
1331 int ret, try;
1332
1333 /* Retry automatically on arbitration loss */
1334 orig_jiffies = jiffies;
1335 for (ret = 0, try = 0; try <= adap->retries; try++) {
1336 ret = adap->algo->master_xfer(adap, msgs, num);
1337 if (ret != -EAGAIN)
1338 break;
1339 if (time_after(jiffies, orig_jiffies + adap->timeout))
1340 break;
1341 }
1342
1343 return ret;
1344}
1345EXPORT_SYMBOL(__i2c_transfer);
1346
1347/**
David Brownella1cdeda2008-07-14 22:38:24 +02001348 * i2c_transfer - execute a single or combined I2C message
1349 * @adap: Handle to I2C bus
1350 * @msgs: One or more messages to execute before STOP is issued to
1351 * terminate the operation; each message begins with a START.
1352 * @num: Number of messages to be executed.
1353 *
1354 * Returns negative errno, else the number of messages executed.
1355 *
1356 * Note that there is no requirement that each message be sent to
1357 * the same slave address, although that is the most common model.
1358 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001359int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360{
Jean Delvareb37d2a32012-06-29 07:47:19 -03001361 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362
David Brownella1cdeda2008-07-14 22:38:24 +02001363 /* REVISIT the fault reporting model here is weak:
1364 *
1365 * - When we get an error after receiving N bytes from a slave,
1366 * there is no way to report "N".
1367 *
1368 * - When we get a NAK after transmitting N bytes to a slave,
1369 * there is no way to report "N" ... or to let the master
1370 * continue executing the rest of this combined message, if
1371 * that's the appropriate response.
1372 *
1373 * - When for example "num" is two and we successfully complete
1374 * the first message but get an error part way through the
1375 * second, it's unclear whether that should be reported as
1376 * one (discarding status on the second message) or errno
1377 * (discarding status on the first one).
1378 */
1379
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 if (adap->algo->master_xfer) {
1381#ifdef DEBUG
1382 for (ret = 0; ret < num; ret++) {
1383 dev_dbg(&adap->dev, "master_xfer[%d] %c, addr=0x%02x, "
Jean Delvare209d27c2007-05-01 23:26:29 +02001384 "len=%d%s\n", ret, (msgs[ret].flags & I2C_M_RD)
1385 ? 'R' : 'W', msgs[ret].addr, msgs[ret].len,
1386 (msgs[ret].flags & I2C_M_RECV_LEN) ? "+" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 }
1388#endif
1389
Mike Rapoportcea443a2008-01-27 18:14:50 +01001390 if (in_atomic() || irqs_disabled()) {
Jean Delvarefe61e072010-08-11 18:20:58 +02001391 ret = i2c_trylock_adapter(adap);
Mike Rapoportcea443a2008-01-27 18:14:50 +01001392 if (!ret)
1393 /* I2C activity is ongoing. */
1394 return -EAGAIN;
1395 } else {
Jean Delvarefe61e072010-08-11 18:20:58 +02001396 i2c_lock_adapter(adap);
Mike Rapoportcea443a2008-01-27 18:14:50 +01001397 }
1398
Jean Delvareb37d2a32012-06-29 07:47:19 -03001399 ret = __i2c_transfer(adap, msgs, num);
Jean Delvarefe61e072010-08-11 18:20:58 +02001400 i2c_unlock_adapter(adap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401
1402 return ret;
1403 } else {
1404 dev_dbg(&adap->dev, "I2C level transfers not supported\n");
David Brownell24a5bb72008-07-14 22:38:23 +02001405 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 }
1407}
David Brownellc0564602007-05-01 23:26:31 +02001408EXPORT_SYMBOL(i2c_transfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409
David Brownella1cdeda2008-07-14 22:38:24 +02001410/**
1411 * i2c_master_send - issue a single I2C message in master transmit mode
1412 * @client: Handle to slave device
1413 * @buf: Data that will be written to the slave
Zhangfei Gao0c43ea52010-03-02 12:23:49 +01001414 * @count: How many bytes to write, must be less than 64k since msg.len is u16
David Brownella1cdeda2008-07-14 22:38:24 +02001415 *
1416 * Returns negative errno, or else the number of bytes written.
1417 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01001418int i2c_master_send(const struct i2c_client *client, const char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419{
1420 int ret;
Farid Hammane7225acf2010-05-21 18:40:58 +02001421 struct i2c_adapter *adap = client->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 struct i2c_msg msg;
1423
Jean Delvare815f55f2005-05-07 22:58:46 +02001424 msg.addr = client->addr;
1425 msg.flags = client->flags & I2C_M_TEN;
1426 msg.len = count;
1427 msg.buf = (char *)buf;
David Brownell438d6c22006-12-10 21:21:31 +01001428
Jean Delvare815f55f2005-05-07 22:58:46 +02001429 ret = i2c_transfer(adap, &msg, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430
Wolfram Sang834aa6f2012-03-15 18:11:05 +01001431 /*
1432 * If everything went ok (i.e. 1 msg transmitted), return #bytes
1433 * transmitted, else error code.
1434 */
Jean Delvare815f55f2005-05-07 22:58:46 +02001435 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436}
David Brownellc0564602007-05-01 23:26:31 +02001437EXPORT_SYMBOL(i2c_master_send);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438
David Brownella1cdeda2008-07-14 22:38:24 +02001439/**
1440 * i2c_master_recv - issue a single I2C message in master receive mode
1441 * @client: Handle to slave device
1442 * @buf: Where to store data read from slave
Zhangfei Gao0c43ea52010-03-02 12:23:49 +01001443 * @count: How many bytes to read, must be less than 64k since msg.len is u16
David Brownella1cdeda2008-07-14 22:38:24 +02001444 *
1445 * Returns negative errno, or else the number of bytes read.
1446 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01001447int i2c_master_recv(const struct i2c_client *client, char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448{
Farid Hammane7225acf2010-05-21 18:40:58 +02001449 struct i2c_adapter *adap = client->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 struct i2c_msg msg;
1451 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452
Jean Delvare815f55f2005-05-07 22:58:46 +02001453 msg.addr = client->addr;
1454 msg.flags = client->flags & I2C_M_TEN;
1455 msg.flags |= I2C_M_RD;
1456 msg.len = count;
1457 msg.buf = buf;
1458
1459 ret = i2c_transfer(adap, &msg, 1);
1460
Wolfram Sang834aa6f2012-03-15 18:11:05 +01001461 /*
1462 * If everything went ok (i.e. 1 msg received), return #bytes received,
1463 * else error code.
1464 */
Jean Delvare815f55f2005-05-07 22:58:46 +02001465 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466}
David Brownellc0564602007-05-01 23:26:31 +02001467EXPORT_SYMBOL(i2c_master_recv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469/* ----------------------------------------------------
1470 * the i2c address scanning function
1471 * Will not work for 10-bit addresses!
1472 * ----------------------------------------------------
1473 */
Jean Delvare9fc6adf2005-07-31 21:20:43 +02001474
Jean Delvare63e4e802010-06-03 11:33:51 +02001475/*
1476 * Legacy default probe function, mostly relevant for SMBus. The default
1477 * probe method is a quick write, but it is known to corrupt the 24RF08
1478 * EEPROMs due to a state machine bug, and could also irreversibly
1479 * write-protect some EEPROMs, so for address ranges 0x30-0x37 and 0x50-0x5f,
1480 * we use a short byte read instead. Also, some bus drivers don't implement
1481 * quick write, so we fallback to a byte read in that case too.
1482 * On x86, there is another special case for FSC hardware monitoring chips,
1483 * which want regular byte reads (address 0x73.) Fortunately, these are the
1484 * only known chips using this I2C address on PC hardware.
1485 * Returns 1 if probe succeeded, 0 if not.
1486 */
1487static int i2c_default_probe(struct i2c_adapter *adap, unsigned short addr)
1488{
1489 int err;
1490 union i2c_smbus_data dummy;
1491
1492#ifdef CONFIG_X86
1493 if (addr == 0x73 && (adap->class & I2C_CLASS_HWMON)
1494 && i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE_DATA))
1495 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
1496 I2C_SMBUS_BYTE_DATA, &dummy);
1497 else
1498#endif
Jean Delvare8031d792010-08-11 18:21:00 +02001499 if (!((addr & ~0x07) == 0x30 || (addr & ~0x0f) == 0x50)
1500 && i2c_check_functionality(adap, I2C_FUNC_SMBUS_QUICK))
Jean Delvare63e4e802010-06-03 11:33:51 +02001501 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_WRITE, 0,
1502 I2C_SMBUS_QUICK, NULL);
Jean Delvare8031d792010-08-11 18:21:00 +02001503 else if (i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE))
1504 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
1505 I2C_SMBUS_BYTE, &dummy);
1506 else {
1507 dev_warn(&adap->dev, "No suitable probing method supported\n");
1508 err = -EOPNOTSUPP;
1509 }
Jean Delvare63e4e802010-06-03 11:33:51 +02001510
1511 return err >= 0;
1512}
1513
Jean Delvareccfbbd02009-12-06 17:06:25 +01001514static int i2c_detect_address(struct i2c_client *temp_client,
Jean Delvare4735c982008-07-14 22:38:36 +02001515 struct i2c_driver *driver)
1516{
1517 struct i2c_board_info info;
1518 struct i2c_adapter *adapter = temp_client->adapter;
1519 int addr = temp_client->addr;
1520 int err;
1521
1522 /* Make sure the address is valid */
Jean Delvare656b8762010-06-03 11:33:53 +02001523 err = i2c_check_addr_validity(addr);
1524 if (err) {
Jean Delvare4735c982008-07-14 22:38:36 +02001525 dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
1526 addr);
Jean Delvare656b8762010-06-03 11:33:53 +02001527 return err;
Jean Delvare4735c982008-07-14 22:38:36 +02001528 }
1529
1530 /* Skip if already in use */
Jean Delvare3b5f7942010-06-03 11:33:55 +02001531 if (i2c_check_addr_busy(adapter, addr))
Jean Delvare4735c982008-07-14 22:38:36 +02001532 return 0;
1533
Jean Delvareccfbbd02009-12-06 17:06:25 +01001534 /* Make sure there is something at this address */
Jean Delvare63e4e802010-06-03 11:33:51 +02001535 if (!i2c_default_probe(adapter, addr))
1536 return 0;
Jean Delvare4735c982008-07-14 22:38:36 +02001537
1538 /* Finally call the custom detection function */
1539 memset(&info, 0, sizeof(struct i2c_board_info));
1540 info.addr = addr;
Jean Delvare310ec792009-12-14 21:17:23 +01001541 err = driver->detect(temp_client, &info);
Jean Delvare4735c982008-07-14 22:38:36 +02001542 if (err) {
1543 /* -ENODEV is returned if the detection fails. We catch it
1544 here as this isn't an error. */
1545 return err == -ENODEV ? 0 : err;
1546 }
1547
1548 /* Consistency check */
1549 if (info.type[0] == '\0') {
1550 dev_err(&adapter->dev, "%s detection function provided "
1551 "no name for 0x%x\n", driver->driver.name,
1552 addr);
1553 } else {
1554 struct i2c_client *client;
1555
1556 /* Detection succeeded, instantiate the device */
1557 dev_dbg(&adapter->dev, "Creating %s at 0x%02x\n",
1558 info.type, info.addr);
1559 client = i2c_new_device(adapter, &info);
1560 if (client)
1561 list_add_tail(&client->detected, &driver->clients);
1562 else
1563 dev_err(&adapter->dev, "Failed creating %s at 0x%02x\n",
1564 info.type, info.addr);
1565 }
1566 return 0;
1567}
1568
1569static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver)
1570{
Jean Delvarec3813d62009-12-14 21:17:25 +01001571 const unsigned short *address_list;
Jean Delvare4735c982008-07-14 22:38:36 +02001572 struct i2c_client *temp_client;
1573 int i, err = 0;
1574 int adap_id = i2c_adapter_id(adapter);
1575
Jean Delvarec3813d62009-12-14 21:17:25 +01001576 address_list = driver->address_list;
1577 if (!driver->detect || !address_list)
Jean Delvare4735c982008-07-14 22:38:36 +02001578 return 0;
1579
Jean Delvare51b54ba2010-10-24 18:16:58 +02001580 /* Stop here if the classes do not match */
1581 if (!(adapter->class & driver->class))
1582 return 0;
1583
Jean Delvare4735c982008-07-14 22:38:36 +02001584 /* Set up a temporary client to help detect callback */
1585 temp_client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
1586 if (!temp_client)
1587 return -ENOMEM;
1588 temp_client->adapter = adapter;
1589
Jean Delvarec3813d62009-12-14 21:17:25 +01001590 for (i = 0; address_list[i] != I2C_CLIENT_END; i += 1) {
Jean Delvare4735c982008-07-14 22:38:36 +02001591 dev_dbg(&adapter->dev, "found normal entry for adapter %d, "
Jean Delvarec3813d62009-12-14 21:17:25 +01001592 "addr 0x%02x\n", adap_id, address_list[i]);
1593 temp_client->addr = address_list[i];
Jean Delvareccfbbd02009-12-06 17:06:25 +01001594 err = i2c_detect_address(temp_client, driver);
Jean Delvare51b54ba2010-10-24 18:16:58 +02001595 if (unlikely(err))
1596 break;
Jean Delvare4735c982008-07-14 22:38:36 +02001597 }
1598
Jean Delvare4735c982008-07-14 22:38:36 +02001599 kfree(temp_client);
1600 return err;
1601}
1602
Jean Delvared44f19d2010-08-11 18:20:57 +02001603int i2c_probe_func_quick_read(struct i2c_adapter *adap, unsigned short addr)
1604{
1605 return i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
1606 I2C_SMBUS_QUICK, NULL) >= 0;
1607}
1608EXPORT_SYMBOL_GPL(i2c_probe_func_quick_read);
1609
Jean Delvare12b5053a2007-05-01 23:26:31 +02001610struct i2c_client *
1611i2c_new_probed_device(struct i2c_adapter *adap,
1612 struct i2c_board_info *info,
Jean Delvare9a942412010-08-11 18:20:56 +02001613 unsigned short const *addr_list,
1614 int (*probe)(struct i2c_adapter *, unsigned short addr))
Jean Delvare12b5053a2007-05-01 23:26:31 +02001615{
1616 int i;
1617
Jean Delvare8031d792010-08-11 18:21:00 +02001618 if (!probe)
Jean Delvare9a942412010-08-11 18:20:56 +02001619 probe = i2c_default_probe;
Jean Delvare12b5053a2007-05-01 23:26:31 +02001620
Jean Delvare12b5053a2007-05-01 23:26:31 +02001621 for (i = 0; addr_list[i] != I2C_CLIENT_END; i++) {
1622 /* Check address validity */
Jean Delvare656b8762010-06-03 11:33:53 +02001623 if (i2c_check_addr_validity(addr_list[i]) < 0) {
Jean Delvare12b5053a2007-05-01 23:26:31 +02001624 dev_warn(&adap->dev, "Invalid 7-bit address "
1625 "0x%02x\n", addr_list[i]);
1626 continue;
1627 }
1628
1629 /* Check address availability */
Jean Delvare3b5f7942010-06-03 11:33:55 +02001630 if (i2c_check_addr_busy(adap, addr_list[i])) {
Jean Delvare12b5053a2007-05-01 23:26:31 +02001631 dev_dbg(&adap->dev, "Address 0x%02x already in "
1632 "use, not probing\n", addr_list[i]);
1633 continue;
1634 }
1635
Jean Delvare63e4e802010-06-03 11:33:51 +02001636 /* Test address responsiveness */
Jean Delvare9a942412010-08-11 18:20:56 +02001637 if (probe(adap, addr_list[i]))
Jean Delvare63e4e802010-06-03 11:33:51 +02001638 break;
Jean Delvare12b5053a2007-05-01 23:26:31 +02001639 }
Jean Delvare12b5053a2007-05-01 23:26:31 +02001640
1641 if (addr_list[i] == I2C_CLIENT_END) {
1642 dev_dbg(&adap->dev, "Probing failed, no device found\n");
1643 return NULL;
1644 }
1645
1646 info->addr = addr_list[i];
1647 return i2c_new_device(adap, info);
1648}
1649EXPORT_SYMBOL_GPL(i2c_new_probed_device);
1650
Jean Delvared735b342011-03-20 14:50:52 +01001651struct i2c_adapter *i2c_get_adapter(int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 struct i2c_adapter *adapter;
David Brownell438d6c22006-12-10 21:21:31 +01001654
Jean Delvarecaada322008-01-27 18:14:49 +01001655 mutex_lock(&core_lock);
Jean Delvared735b342011-03-20 14:50:52 +01001656 adapter = idr_find(&i2c_adapter_idr, nr);
Mark M. Hoffmana0920e12005-06-28 00:21:30 -04001657 if (adapter && !try_module_get(adapter->owner))
1658 adapter = NULL;
1659
Jean Delvarecaada322008-01-27 18:14:49 +01001660 mutex_unlock(&core_lock);
Mark M. Hoffmana0920e12005-06-28 00:21:30 -04001661 return adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662}
David Brownellc0564602007-05-01 23:26:31 +02001663EXPORT_SYMBOL(i2c_get_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664
1665void i2c_put_adapter(struct i2c_adapter *adap)
1666{
1667 module_put(adap->owner);
1668}
David Brownellc0564602007-05-01 23:26:31 +02001669EXPORT_SYMBOL(i2c_put_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670
1671/* The SMBus parts */
1672
David Brownell438d6c22006-12-10 21:21:31 +01001673#define POLY (0x1070U << 3)
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001674static u8 crc8(u16 data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675{
1676 int i;
David Brownell438d6c22006-12-10 21:21:31 +01001677
Farid Hammane7225acf2010-05-21 18:40:58 +02001678 for (i = 0; i < 8; i++) {
David Brownell438d6c22006-12-10 21:21:31 +01001679 if (data & 0x8000)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 data = data ^ POLY;
1681 data = data << 1;
1682 }
1683 return (u8)(data >> 8);
1684}
1685
Jean Delvare421ef472005-10-26 21:28:55 +02001686/* Incremental CRC8 over count bytes in the array pointed to by p */
1687static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688{
1689 int i;
1690
Farid Hammane7225acf2010-05-21 18:40:58 +02001691 for (i = 0; i < count; i++)
Jean Delvare421ef472005-10-26 21:28:55 +02001692 crc = crc8((crc ^ p[i]) << 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 return crc;
1694}
1695
Jean Delvare421ef472005-10-26 21:28:55 +02001696/* Assume a 7-bit address, which is reasonable for SMBus */
1697static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698{
Jean Delvare421ef472005-10-26 21:28:55 +02001699 /* The address will be sent first */
1700 u8 addr = (msg->addr << 1) | !!(msg->flags & I2C_M_RD);
1701 pec = i2c_smbus_pec(pec, &addr, 1);
1702
1703 /* The data buffer follows */
1704 return i2c_smbus_pec(pec, msg->buf, msg->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705}
1706
Jean Delvare421ef472005-10-26 21:28:55 +02001707/* Used for write only transactions */
1708static inline void i2c_smbus_add_pec(struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709{
Jean Delvare421ef472005-10-26 21:28:55 +02001710 msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg);
1711 msg->len++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712}
1713
Jean Delvare421ef472005-10-26 21:28:55 +02001714/* Return <0 on CRC error
1715 If there was a write before this read (most cases) we need to take the
1716 partial CRC from the write part into account.
1717 Note that this function does modify the message (we need to decrease the
1718 message length to hide the CRC byte from the caller). */
1719static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720{
Jean Delvare421ef472005-10-26 21:28:55 +02001721 u8 rpec = msg->buf[--msg->len];
1722 cpec = i2c_smbus_msg_pec(cpec, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 if (rpec != cpec) {
1725 pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n",
1726 rpec, cpec);
David Brownell24a5bb72008-07-14 22:38:23 +02001727 return -EBADMSG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 }
David Brownell438d6c22006-12-10 21:21:31 +01001729 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730}
1731
David Brownella1cdeda2008-07-14 22:38:24 +02001732/**
1733 * i2c_smbus_read_byte - SMBus "receive byte" protocol
1734 * @client: Handle to slave device
1735 *
1736 * This executes the SMBus "receive byte" protocol, returning negative errno
1737 * else the byte received from the device.
1738 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01001739s32 i2c_smbus_read_byte(const struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740{
1741 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02001742 int status;
1743
1744 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1745 I2C_SMBUS_READ, 0,
1746 I2C_SMBUS_BYTE, &data);
1747 return (status < 0) ? status : data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748}
David Brownellc0564602007-05-01 23:26:31 +02001749EXPORT_SYMBOL(i2c_smbus_read_byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750
David Brownella1cdeda2008-07-14 22:38:24 +02001751/**
1752 * i2c_smbus_write_byte - SMBus "send byte" protocol
1753 * @client: Handle to slave device
1754 * @value: Byte to be sent
1755 *
1756 * This executes the SMBus "send byte" protocol, returning negative errno
1757 * else zero on success.
1758 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01001759s32 i2c_smbus_write_byte(const struct i2c_client *client, u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760{
Farid Hammane7225acf2010-05-21 18:40:58 +02001761 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
Jean Delvare421ef472005-10-26 21:28:55 +02001762 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763}
David Brownellc0564602007-05-01 23:26:31 +02001764EXPORT_SYMBOL(i2c_smbus_write_byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765
David Brownella1cdeda2008-07-14 22:38:24 +02001766/**
1767 * i2c_smbus_read_byte_data - SMBus "read byte" protocol
1768 * @client: Handle to slave device
1769 * @command: Byte interpreted by slave
1770 *
1771 * This executes the SMBus "read byte" protocol, returning negative errno
1772 * else a data byte received from the device.
1773 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01001774s32 i2c_smbus_read_byte_data(const struct i2c_client *client, u8 command)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775{
1776 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02001777 int status;
1778
1779 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1780 I2C_SMBUS_READ, command,
1781 I2C_SMBUS_BYTE_DATA, &data);
1782 return (status < 0) ? status : data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783}
David Brownellc0564602007-05-01 23:26:31 +02001784EXPORT_SYMBOL(i2c_smbus_read_byte_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785
David Brownella1cdeda2008-07-14 22:38:24 +02001786/**
1787 * i2c_smbus_write_byte_data - SMBus "write byte" protocol
1788 * @client: Handle to slave device
1789 * @command: Byte interpreted by slave
1790 * @value: Byte being written
1791 *
1792 * This executes the SMBus "write byte" protocol, returning negative errno
1793 * else zero on success.
1794 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01001795s32 i2c_smbus_write_byte_data(const struct i2c_client *client, u8 command,
1796 u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797{
1798 union i2c_smbus_data data;
1799 data.byte = value;
Farid Hammane7225acf2010-05-21 18:40:58 +02001800 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1801 I2C_SMBUS_WRITE, command,
1802 I2C_SMBUS_BYTE_DATA, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803}
David Brownellc0564602007-05-01 23:26:31 +02001804EXPORT_SYMBOL(i2c_smbus_write_byte_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805
David Brownella1cdeda2008-07-14 22:38:24 +02001806/**
1807 * i2c_smbus_read_word_data - SMBus "read word" protocol
1808 * @client: Handle to slave device
1809 * @command: Byte interpreted by slave
1810 *
1811 * This executes the SMBus "read word" protocol, returning negative errno
1812 * else a 16-bit unsigned "word" received from the device.
1813 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01001814s32 i2c_smbus_read_word_data(const struct i2c_client *client, u8 command)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815{
1816 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02001817 int status;
1818
1819 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1820 I2C_SMBUS_READ, command,
1821 I2C_SMBUS_WORD_DATA, &data);
1822 return (status < 0) ? status : data.word;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823}
David Brownellc0564602007-05-01 23:26:31 +02001824EXPORT_SYMBOL(i2c_smbus_read_word_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825
David Brownella1cdeda2008-07-14 22:38:24 +02001826/**
1827 * i2c_smbus_write_word_data - SMBus "write word" protocol
1828 * @client: Handle to slave device
1829 * @command: Byte interpreted by slave
1830 * @value: 16-bit "word" being written
1831 *
1832 * This executes the SMBus "write word" protocol, returning negative errno
1833 * else zero on success.
1834 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01001835s32 i2c_smbus_write_word_data(const struct i2c_client *client, u8 command,
1836 u16 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837{
1838 union i2c_smbus_data data;
1839 data.word = value;
Farid Hammane7225acf2010-05-21 18:40:58 +02001840 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1841 I2C_SMBUS_WRITE, command,
1842 I2C_SMBUS_WORD_DATA, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843}
David Brownellc0564602007-05-01 23:26:31 +02001844EXPORT_SYMBOL(i2c_smbus_write_word_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845
David Brownella64ec072007-10-13 23:56:31 +02001846/**
David Brownella1cdeda2008-07-14 22:38:24 +02001847 * i2c_smbus_read_block_data - SMBus "block read" protocol
David Brownella64ec072007-10-13 23:56:31 +02001848 * @client: Handle to slave device
David Brownella1cdeda2008-07-14 22:38:24 +02001849 * @command: Byte interpreted by slave
David Brownella64ec072007-10-13 23:56:31 +02001850 * @values: Byte array into which data will be read; big enough to hold
1851 * the data returned by the slave. SMBus allows at most 32 bytes.
1852 *
David Brownella1cdeda2008-07-14 22:38:24 +02001853 * This executes the SMBus "block read" protocol, returning negative errno
1854 * else the number of data bytes in the slave's response.
David Brownella64ec072007-10-13 23:56:31 +02001855 *
1856 * Note that using this function requires that the client's adapter support
1857 * the I2C_FUNC_SMBUS_READ_BLOCK_DATA functionality. Not all adapter drivers
1858 * support this; its emulation through I2C messaging relies on a specific
1859 * mechanism (I2C_M_RECV_LEN) which may not be implemented.
1860 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01001861s32 i2c_smbus_read_block_data(const struct i2c_client *client, u8 command,
Jean Delvareb86a1bc2007-05-01 23:26:34 +02001862 u8 *values)
1863{
1864 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02001865 int status;
Jean Delvareb86a1bc2007-05-01 23:26:34 +02001866
David Brownell24a5bb72008-07-14 22:38:23 +02001867 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1868 I2C_SMBUS_READ, command,
1869 I2C_SMBUS_BLOCK_DATA, &data);
1870 if (status)
1871 return status;
Jean Delvareb86a1bc2007-05-01 23:26:34 +02001872
1873 memcpy(values, &data.block[1], data.block[0]);
1874 return data.block[0];
1875}
1876EXPORT_SYMBOL(i2c_smbus_read_block_data);
1877
David Brownella1cdeda2008-07-14 22:38:24 +02001878/**
1879 * i2c_smbus_write_block_data - SMBus "block write" protocol
1880 * @client: Handle to slave device
1881 * @command: Byte interpreted by slave
1882 * @length: Size of data block; SMBus allows at most 32 bytes
1883 * @values: Byte array which will be written.
1884 *
1885 * This executes the SMBus "block write" protocol, returning negative errno
1886 * else zero on success.
1887 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01001888s32 i2c_smbus_write_block_data(const struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +02001889 u8 length, const u8 *values)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890{
1891 union i2c_smbus_data data;
Jean Delvare76560322006-01-18 23:14:55 +01001892
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 if (length > I2C_SMBUS_BLOCK_MAX)
1894 length = I2C_SMBUS_BLOCK_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 data.block[0] = length;
Jean Delvare76560322006-01-18 23:14:55 +01001896 memcpy(&data.block[1], values, length);
Farid Hammane7225acf2010-05-21 18:40:58 +02001897 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1898 I2C_SMBUS_WRITE, command,
1899 I2C_SMBUS_BLOCK_DATA, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900}
David Brownellc0564602007-05-01 23:26:31 +02001901EXPORT_SYMBOL(i2c_smbus_write_block_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902
1903/* Returns the number of read bytes */
Jean Delvare0cc43a12011-01-10 22:11:23 +01001904s32 i2c_smbus_read_i2c_block_data(const struct i2c_client *client, u8 command,
Jean Delvare4b2643d2007-07-12 14:12:29 +02001905 u8 length, u8 *values)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906{
1907 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02001908 int status;
Jean Delvare76560322006-01-18 23:14:55 +01001909
Jean Delvare4b2643d2007-07-12 14:12:29 +02001910 if (length > I2C_SMBUS_BLOCK_MAX)
1911 length = I2C_SMBUS_BLOCK_MAX;
1912 data.block[0] = length;
David Brownell24a5bb72008-07-14 22:38:23 +02001913 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1914 I2C_SMBUS_READ, command,
1915 I2C_SMBUS_I2C_BLOCK_DATA, &data);
1916 if (status < 0)
1917 return status;
Jean Delvare76560322006-01-18 23:14:55 +01001918
1919 memcpy(values, &data.block[1], data.block[0]);
1920 return data.block[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921}
David Brownellc0564602007-05-01 23:26:31 +02001922EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923
Jean Delvare0cc43a12011-01-10 22:11:23 +01001924s32 i2c_smbus_write_i2c_block_data(const struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +02001925 u8 length, const u8 *values)
Jean Delvare21bbd692006-01-09 15:19:18 +11001926{
1927 union i2c_smbus_data data;
1928
1929 if (length > I2C_SMBUS_BLOCK_MAX)
1930 length = I2C_SMBUS_BLOCK_MAX;
1931 data.block[0] = length;
1932 memcpy(data.block + 1, values, length);
1933 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1934 I2C_SMBUS_WRITE, command,
1935 I2C_SMBUS_I2C_BLOCK_DATA, &data);
1936}
David Brownellc0564602007-05-01 23:26:31 +02001937EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data);
Jean Delvare21bbd692006-01-09 15:19:18 +11001938
David Brownell438d6c22006-12-10 21:21:31 +01001939/* Simulate a SMBus command using the i2c protocol
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 No checking of parameters is done! */
Farid Hammane7225acf2010-05-21 18:40:58 +02001941static s32 i2c_smbus_xfer_emulated(struct i2c_adapter *adapter, u16 addr,
1942 unsigned short flags,
1943 char read_write, u8 command, int size,
1944 union i2c_smbus_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945{
1946 /* So we need to generate a series of msgs. In the case of writing, we
1947 need to use only one message; when reading, we need two. We initialize
1948 most things with sane defaults, to keep the code below somewhat
1949 simpler. */
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02001950 unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3];
1951 unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2];
Farid Hammane7225acf2010-05-21 18:40:58 +02001952 int num = read_write == I2C_SMBUS_READ ? 2 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 int i;
Jean Delvare421ef472005-10-26 21:28:55 +02001954 u8 partial_pec = 0;
David Brownell24a5bb72008-07-14 22:38:23 +02001955 int status;
Shubhrajyoti D230da092012-10-05 22:23:52 +02001956 struct i2c_msg msg[2] = {
1957 {
1958 .addr = addr,
1959 .flags = flags,
1960 .len = 1,
1961 .buf = msgbuf0,
1962 }, {
1963 .addr = addr,
1964 .flags = flags | I2C_M_RD,
1965 .len = 0,
1966 .buf = msgbuf1,
1967 },
1968 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969
1970 msgbuf0[0] = command;
Farid Hammane7225acf2010-05-21 18:40:58 +02001971 switch (size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 case I2C_SMBUS_QUICK:
1973 msg[0].len = 0;
1974 /* Special case: The read/write field is used as data */
Roel Kluinf29d2e02009-02-24 19:19:48 +01001975 msg[0].flags = flags | (read_write == I2C_SMBUS_READ ?
1976 I2C_M_RD : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 num = 1;
1978 break;
1979 case I2C_SMBUS_BYTE:
1980 if (read_write == I2C_SMBUS_READ) {
1981 /* Special case: only a read! */
1982 msg[0].flags = I2C_M_RD | flags;
1983 num = 1;
1984 }
1985 break;
1986 case I2C_SMBUS_BYTE_DATA:
1987 if (read_write == I2C_SMBUS_READ)
1988 msg[1].len = 1;
1989 else {
1990 msg[0].len = 2;
1991 msgbuf0[1] = data->byte;
1992 }
1993 break;
1994 case I2C_SMBUS_WORD_DATA:
1995 if (read_write == I2C_SMBUS_READ)
1996 msg[1].len = 2;
1997 else {
Farid Hammane7225acf2010-05-21 18:40:58 +02001998 msg[0].len = 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02002000 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 }
2002 break;
2003 case I2C_SMBUS_PROC_CALL:
2004 num = 2; /* Special case */
2005 read_write = I2C_SMBUS_READ;
2006 msg[0].len = 3;
2007 msg[1].len = 2;
2008 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02002009 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 break;
2011 case I2C_SMBUS_BLOCK_DATA:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 if (read_write == I2C_SMBUS_READ) {
Jean Delvare209d27c2007-05-01 23:26:29 +02002013 msg[1].flags |= I2C_M_RECV_LEN;
2014 msg[1].len = 1; /* block length will be added by
2015 the underlying bus driver */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 } else {
2017 msg[0].len = data->block[0] + 2;
2018 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
David Brownell24a5bb72008-07-14 22:38:23 +02002019 dev_err(&adapter->dev,
2020 "Invalid block write size %d\n",
2021 data->block[0]);
2022 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 }
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02002024 for (i = 1; i < msg[0].len; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 msgbuf0[i] = data->block[i-1];
2026 }
2027 break;
2028 case I2C_SMBUS_BLOCK_PROC_CALL:
Jean Delvare209d27c2007-05-01 23:26:29 +02002029 num = 2; /* Another special case */
2030 read_write = I2C_SMBUS_READ;
2031 if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
David Brownell24a5bb72008-07-14 22:38:23 +02002032 dev_err(&adapter->dev,
2033 "Invalid block write size %d\n",
Jean Delvare209d27c2007-05-01 23:26:29 +02002034 data->block[0]);
David Brownell24a5bb72008-07-14 22:38:23 +02002035 return -EINVAL;
Jean Delvare209d27c2007-05-01 23:26:29 +02002036 }
2037 msg[0].len = data->block[0] + 2;
2038 for (i = 1; i < msg[0].len; i++)
2039 msgbuf0[i] = data->block[i-1];
2040 msg[1].flags |= I2C_M_RECV_LEN;
2041 msg[1].len = 1; /* block length will be added by
2042 the underlying bus driver */
2043 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 case I2C_SMBUS_I2C_BLOCK_DATA:
2045 if (read_write == I2C_SMBUS_READ) {
Jean Delvare4b2643d2007-07-12 14:12:29 +02002046 msg[1].len = data->block[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 } else {
2048 msg[0].len = data->block[0] + 1;
Jean Delvare30dac742005-10-08 00:15:59 +02002049 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) {
David Brownell24a5bb72008-07-14 22:38:23 +02002050 dev_err(&adapter->dev,
2051 "Invalid block write size %d\n",
2052 data->block[0]);
2053 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 }
2055 for (i = 1; i <= data->block[0]; i++)
2056 msgbuf0[i] = data->block[i];
2057 }
2058 break;
2059 default:
David Brownell24a5bb72008-07-14 22:38:23 +02002060 dev_err(&adapter->dev, "Unsupported transaction %d\n", size);
2061 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 }
2063
Jean Delvare421ef472005-10-26 21:28:55 +02002064 i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK
2065 && size != I2C_SMBUS_I2C_BLOCK_DATA);
2066 if (i) {
2067 /* Compute PEC if first message is a write */
2068 if (!(msg[0].flags & I2C_M_RD)) {
David Brownell438d6c22006-12-10 21:21:31 +01002069 if (num == 1) /* Write only */
Jean Delvare421ef472005-10-26 21:28:55 +02002070 i2c_smbus_add_pec(&msg[0]);
2071 else /* Write followed by read */
2072 partial_pec = i2c_smbus_msg_pec(0, &msg[0]);
2073 }
2074 /* Ask for PEC if last message is a read */
2075 if (msg[num-1].flags & I2C_M_RD)
David Brownell438d6c22006-12-10 21:21:31 +01002076 msg[num-1].len++;
Jean Delvare421ef472005-10-26 21:28:55 +02002077 }
2078
David Brownell24a5bb72008-07-14 22:38:23 +02002079 status = i2c_transfer(adapter, msg, num);
2080 if (status < 0)
2081 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082
Jean Delvare421ef472005-10-26 21:28:55 +02002083 /* Check PEC if last message is a read */
2084 if (i && (msg[num-1].flags & I2C_M_RD)) {
David Brownell24a5bb72008-07-14 22:38:23 +02002085 status = i2c_smbus_check_pec(partial_pec, &msg[num-1]);
2086 if (status < 0)
2087 return status;
Jean Delvare421ef472005-10-26 21:28:55 +02002088 }
2089
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 if (read_write == I2C_SMBUS_READ)
Farid Hammane7225acf2010-05-21 18:40:58 +02002091 switch (size) {
2092 case I2C_SMBUS_BYTE:
2093 data->byte = msgbuf0[0];
2094 break;
2095 case I2C_SMBUS_BYTE_DATA:
2096 data->byte = msgbuf1[0];
2097 break;
2098 case I2C_SMBUS_WORD_DATA:
2099 case I2C_SMBUS_PROC_CALL:
2100 data->word = msgbuf1[0] | (msgbuf1[1] << 8);
2101 break;
2102 case I2C_SMBUS_I2C_BLOCK_DATA:
2103 for (i = 0; i < data->block[0]; i++)
2104 data->block[i+1] = msgbuf1[i];
2105 break;
2106 case I2C_SMBUS_BLOCK_DATA:
2107 case I2C_SMBUS_BLOCK_PROC_CALL:
2108 for (i = 0; i < msgbuf1[0] + 1; i++)
2109 data->block[i] = msgbuf1[i];
2110 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 }
2112 return 0;
2113}
2114
David Brownella1cdeda2008-07-14 22:38:24 +02002115/**
2116 * i2c_smbus_xfer - execute SMBus protocol operations
2117 * @adapter: Handle to I2C bus
2118 * @addr: Address of SMBus slave on that bus
2119 * @flags: I2C_CLIENT_* flags (usually zero or I2C_CLIENT_PEC)
2120 * @read_write: I2C_SMBUS_READ or I2C_SMBUS_WRITE
2121 * @command: Byte interpreted by slave, for protocols which use such bytes
2122 * @protocol: SMBus protocol operation to execute, such as I2C_SMBUS_PROC_CALL
2123 * @data: Data to be read or written
2124 *
2125 * This executes an SMBus protocol operation, and returns a negative
2126 * errno code else zero on success.
2127 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01002128s32 i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr, unsigned short flags,
David Brownella1cdeda2008-07-14 22:38:24 +02002129 char read_write, u8 command, int protocol,
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01002130 union i2c_smbus_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131{
Clifford Wolf66b650f2009-06-15 18:01:46 +02002132 unsigned long orig_jiffies;
2133 int try;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 s32 res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135
Laurent Pinchartd47726c2012-07-24 14:13:59 +02002136 flags &= I2C_M_TEN | I2C_CLIENT_PEC | I2C_CLIENT_SCCB;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137
2138 if (adapter->algo->smbus_xfer) {
Jean Delvarefe61e072010-08-11 18:20:58 +02002139 i2c_lock_adapter(adapter);
Clifford Wolf66b650f2009-06-15 18:01:46 +02002140
2141 /* Retry automatically on arbitration loss */
2142 orig_jiffies = jiffies;
2143 for (res = 0, try = 0; try <= adapter->retries; try++) {
2144 res = adapter->algo->smbus_xfer(adapter, addr, flags,
2145 read_write, command,
2146 protocol, data);
2147 if (res != -EAGAIN)
2148 break;
2149 if (time_after(jiffies,
2150 orig_jiffies + adapter->timeout))
2151 break;
2152 }
Jean Delvarefe61e072010-08-11 18:20:58 +02002153 i2c_unlock_adapter(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154
Laurent Pinchart72fc2c72012-07-24 14:13:59 +02002155 if (res != -EOPNOTSUPP || !adapter->algo->master_xfer)
2156 return res;
2157 /*
2158 * Fall back to i2c_smbus_xfer_emulated if the adapter doesn't
2159 * implement native support for the SMBus operation.
2160 */
2161 }
2162
2163 return i2c_smbus_xfer_emulated(adapter, addr, flags, read_write,
2164 command, protocol, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166EXPORT_SYMBOL(i2c_smbus_xfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167
2168MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
2169MODULE_DESCRIPTION("I2C-Bus main module");
2170MODULE_LICENSE("GPL");