blob: 0815e10da7c6da3b5ed4974cdb505fc848c43d87 [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
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
18/* ------------------------------------------------------------------------- */
19
Jan Engelhardt96de0e22007-10-19 23:21:04 +020020/* With some changes from Kyösti Mälkki <kmalkki@cc.hut.fi>.
Linus Torvalds1da177e2005-04-16 15:20:36 -070021 All SMBus-related things are written by Frodo Looijaard <frodol@dds.nl>
Jean Delvare421ef472005-10-26 21:28:55 +020022 SMBus 2.0 support by Mark Studebaker <mdsxyz123@yahoo.com> and
23 Jean Delvare <khali@linux-fr.org> */
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/module.h>
26#include <linux/kernel.h>
27#include <linux/errno.h>
28#include <linux/slab.h>
29#include <linux/i2c.h>
30#include <linux/init.h>
31#include <linux/idr.h>
Arjan van de Venb3585e42006-01-11 10:50:26 +010032#include <linux/mutex.h>
Jean Delvareb8d6f452007-02-13 22:09:00 +010033#include <linux/completion.h>
Mike Rapoportcea443a2008-01-27 18:14:50 +010034#include <linux/hardirq.h>
35#include <linux/irqflags.h>
Rodolfo Giomettif18c41d2009-06-19 16:58:20 +020036#include <linux/rwsem.h>
Mark Brown6de468a2010-03-02 12:23:46 +010037#include <linux/pm_runtime.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <asm/uaccess.h>
39
David Brownell9c1600e2007-05-01 23:26:31 +020040#include "i2c-core.h"
41
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
Jean Delvare6629dcf2010-05-04 11:09:28 +020043/* core_lock protects i2c_adapter_idr, and guarantees
Jean Delvare35fc37f2009-06-19 16:58:19 +020044 that device detection, deletion of detected devices, and attach_adapter
45 and detach_adapter calls are serialized */
Jean Delvarecaada322008-01-27 18:14:49 +010046static DEFINE_MUTEX(core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070047static DEFINE_IDR(i2c_adapter_idr);
48
Jean Delvare4f8cf822009-09-18 22:45:46 +020049static struct device_type i2c_client_type;
Jean Delvare4735c982008-07-14 22:38:36 +020050static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver);
David Brownellf37dd802007-02-13 22:09:00 +010051
52/* ------------------------------------------------------------------------- */
53
Jean Delvared2653e92008-04-29 23:11:39 +020054static const struct i2c_device_id *i2c_match_id(const struct i2c_device_id *id,
55 const struct i2c_client *client)
56{
57 while (id->name[0]) {
58 if (strcmp(client->name, id->name) == 0)
59 return id;
60 id++;
61 }
62 return NULL;
63}
64
Linus Torvalds1da177e2005-04-16 15:20:36 -070065static int i2c_device_match(struct device *dev, struct device_driver *drv)
66{
Jean Delvare51298d12009-09-18 22:45:45 +020067 struct i2c_client *client = i2c_verify_client(dev);
68 struct i2c_driver *driver;
David Brownell7b4fbc52007-05-01 23:26:30 +020069
Jean Delvare51298d12009-09-18 22:45:45 +020070 if (!client)
71 return 0;
72
73 driver = to_i2c_driver(drv);
Jean Delvared2653e92008-04-29 23:11:39 +020074 /* match on an id table if there is one */
75 if (driver->id_table)
76 return i2c_match_id(driver->id_table, client) != NULL;
77
Jean Delvareeb8a7902008-05-18 20:49:41 +020078 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070079}
80
David Brownell7b4fbc52007-05-01 23:26:30 +020081#ifdef CONFIG_HOTPLUG
82
83/* uevent helps with hotplug: modprobe -q $(MODALIAS) */
Kay Sievers7eff2e72007-08-14 15:15:12 +020084static int i2c_device_uevent(struct device *dev, struct kobj_uevent_env *env)
David Brownell7b4fbc52007-05-01 23:26:30 +020085{
86 struct i2c_client *client = to_i2c_client(dev);
David Brownell7b4fbc52007-05-01 23:26:30 +020087
Jean Delvareeb8a7902008-05-18 20:49:41 +020088 if (add_uevent_var(env, "MODALIAS=%s%s",
89 I2C_MODULE_PREFIX, client->name))
90 return -ENOMEM;
David Brownell7b4fbc52007-05-01 23:26:30 +020091 dev_dbg(dev, "uevent\n");
92 return 0;
93}
94
95#else
96#define i2c_device_uevent NULL
97#endif /* CONFIG_HOTPLUG */
98
Linus Torvalds1da177e2005-04-16 15:20:36 -070099static int i2c_device_probe(struct device *dev)
100{
Jean Delvare51298d12009-09-18 22:45:45 +0200101 struct i2c_client *client = i2c_verify_client(dev);
102 struct i2c_driver *driver;
Hans Verkuil50c33042008-03-12 14:15:00 +0100103 int status;
David Brownell7b4fbc52007-05-01 23:26:30 +0200104
Jean Delvare51298d12009-09-18 22:45:45 +0200105 if (!client)
106 return 0;
107
108 driver = to_i2c_driver(dev->driver);
Jean Delvaree0457442008-07-14 22:38:30 +0200109 if (!driver->probe || !driver->id_table)
David Brownell7b4fbc52007-05-01 23:26:30 +0200110 return -ENODEV;
111 client->driver = driver;
Marc Pignatee354252008-08-28 08:33:22 +0200112 if (!device_can_wakeup(&client->dev))
113 device_init_wakeup(&client->dev,
114 client->flags & I2C_CLIENT_WAKE);
David Brownell7b4fbc52007-05-01 23:26:30 +0200115 dev_dbg(dev, "probe\n");
Jean Delvared2653e92008-04-29 23:11:39 +0200116
Jean Delvaree0457442008-07-14 22:38:30 +0200117 status = driver->probe(client, i2c_match_id(driver->id_table, client));
Wolfram Sange4a7b9b2010-05-04 11:09:27 +0200118 if (status) {
Hans Verkuil50c33042008-03-12 14:15:00 +0100119 client->driver = NULL;
Wolfram Sange4a7b9b2010-05-04 11:09:27 +0200120 i2c_set_clientdata(client, NULL);
121 }
Hans Verkuil50c33042008-03-12 14:15:00 +0100122 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123}
124
125static int i2c_device_remove(struct device *dev)
126{
Jean Delvare51298d12009-09-18 22:45:45 +0200127 struct i2c_client *client = i2c_verify_client(dev);
David Brownella1d9e6e2007-05-01 23:26:30 +0200128 struct i2c_driver *driver;
129 int status;
130
Jean Delvare51298d12009-09-18 22:45:45 +0200131 if (!client || !dev->driver)
David Brownella1d9e6e2007-05-01 23:26:30 +0200132 return 0;
133
134 driver = to_i2c_driver(dev->driver);
135 if (driver->remove) {
136 dev_dbg(dev, "remove\n");
137 status = driver->remove(client);
138 } else {
139 dev->driver = NULL;
140 status = 0;
141 }
Wolfram Sange4a7b9b2010-05-04 11:09:27 +0200142 if (status == 0) {
David Brownella1d9e6e2007-05-01 23:26:30 +0200143 client->driver = NULL;
Wolfram Sange4a7b9b2010-05-04 11:09:27 +0200144 i2c_set_clientdata(client, NULL);
145 }
David Brownella1d9e6e2007-05-01 23:26:30 +0200146 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147}
148
David Brownellf37dd802007-02-13 22:09:00 +0100149static void i2c_device_shutdown(struct device *dev)
150{
Jean Delvare51298d12009-09-18 22:45:45 +0200151 struct i2c_client *client = i2c_verify_client(dev);
David Brownellf37dd802007-02-13 22:09:00 +0100152 struct i2c_driver *driver;
153
Jean Delvare51298d12009-09-18 22:45:45 +0200154 if (!client || !dev->driver)
David Brownellf37dd802007-02-13 22:09:00 +0100155 return;
156 driver = to_i2c_driver(dev->driver);
157 if (driver->shutdown)
Jean Delvare51298d12009-09-18 22:45:45 +0200158 driver->shutdown(client);
David Brownellf37dd802007-02-13 22:09:00 +0100159}
160
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200161#ifdef CONFIG_PM_SLEEP
162static int i2c_legacy_suspend(struct device *dev, pm_message_t mesg)
David Brownellf37dd802007-02-13 22:09:00 +0100163{
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 0;
169 driver = to_i2c_driver(dev->driver);
170 if (!driver->suspend)
171 return 0;
Jean Delvare51298d12009-09-18 22:45:45 +0200172 return driver->suspend(client, mesg);
David Brownellf37dd802007-02-13 22:09:00 +0100173}
174
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200175static int i2c_legacy_resume(struct device *dev)
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->resume)
184 return 0;
Jean Delvare51298d12009-09-18 22:45:45 +0200185 return driver->resume(client);
David Brownellf37dd802007-02-13 22:09:00 +0100186}
187
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200188static int i2c_device_pm_suspend(struct device *dev)
189{
190 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
191
192 if (pm_runtime_suspended(dev))
193 return 0;
194
195 if (pm)
196 return pm->suspend ? pm->suspend(dev) : 0;
197
198 return i2c_legacy_suspend(dev, PMSG_SUSPEND);
199}
200
201static int i2c_device_pm_resume(struct device *dev)
202{
203 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
204 int ret;
205
206 if (pm)
207 ret = pm->resume ? pm->resume(dev) : 0;
208 else
209 ret = i2c_legacy_resume(dev);
210
211 if (!ret) {
212 pm_runtime_disable(dev);
213 pm_runtime_set_active(dev);
214 pm_runtime_enable(dev);
215 }
216
217 return ret;
218}
219
220static int i2c_device_pm_freeze(struct device *dev)
221{
222 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
223
224 if (pm_runtime_suspended(dev))
225 return 0;
226
227 if (pm)
228 return pm->freeze ? pm->freeze(dev) : 0;
229
230 return i2c_legacy_suspend(dev, PMSG_FREEZE);
231}
232
233static int i2c_device_pm_thaw(struct device *dev)
234{
235 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
236
237 if (pm_runtime_suspended(dev))
238 return 0;
239
240 if (pm)
241 return pm->thaw ? pm->thaw(dev) : 0;
242
243 return i2c_legacy_resume(dev);
244}
245
246static int i2c_device_pm_poweroff(struct device *dev)
247{
248 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
249
250 if (pm_runtime_suspended(dev))
251 return 0;
252
253 if (pm)
254 return pm->poweroff ? pm->poweroff(dev) : 0;
255
256 return i2c_legacy_suspend(dev, PMSG_HIBERNATE);
257}
258
259static int i2c_device_pm_restore(struct device *dev)
260{
261 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
262 int ret;
263
264 if (pm)
265 ret = pm->restore ? pm->restore(dev) : 0;
266 else
267 ret = i2c_legacy_resume(dev);
268
269 if (!ret) {
270 pm_runtime_disable(dev);
271 pm_runtime_set_active(dev);
272 pm_runtime_enable(dev);
273 }
274
275 return ret;
276}
277#else /* !CONFIG_PM_SLEEP */
278#define i2c_device_pm_suspend NULL
279#define i2c_device_pm_resume NULL
280#define i2c_device_pm_freeze NULL
281#define i2c_device_pm_thaw NULL
282#define i2c_device_pm_poweroff NULL
283#define i2c_device_pm_restore NULL
284#endif /* !CONFIG_PM_SLEEP */
285
David Brownell9c1600e2007-05-01 23:26:31 +0200286static void i2c_client_dev_release(struct device *dev)
287{
288 kfree(to_i2c_client(dev));
289}
290
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100291static ssize_t
Jean Delvare4f8cf822009-09-18 22:45:46 +0200292show_name(struct device *dev, struct device_attribute *attr, char *buf)
David Brownell7b4fbc52007-05-01 23:26:30 +0200293{
Jean Delvare4f8cf822009-09-18 22:45:46 +0200294 return sprintf(buf, "%s\n", dev->type == &i2c_client_type ?
295 to_i2c_client(dev)->name : to_i2c_adapter(dev)->name);
David Brownell7b4fbc52007-05-01 23:26:30 +0200296}
297
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100298static ssize_t
299show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
David Brownell7b4fbc52007-05-01 23:26:30 +0200300{
301 struct i2c_client *client = to_i2c_client(dev);
Jean Delvareeb8a7902008-05-18 20:49:41 +0200302 return sprintf(buf, "%s%s\n", I2C_MODULE_PREFIX, client->name);
David Brownell7b4fbc52007-05-01 23:26:30 +0200303}
304
Jean Delvare4f8cf822009-09-18 22:45:46 +0200305static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
Jean Delvare51298d12009-09-18 22:45:45 +0200306static DEVICE_ATTR(modalias, S_IRUGO, show_modalias, NULL);
307
308static struct attribute *i2c_dev_attrs[] = {
309 &dev_attr_name.attr,
David Brownell7b4fbc52007-05-01 23:26:30 +0200310 /* modalias helps coldplug: modprobe $(cat .../modalias) */
Jean Delvare51298d12009-09-18 22:45:45 +0200311 &dev_attr_modalias.attr,
312 NULL
313};
314
315static struct attribute_group i2c_dev_attr_group = {
316 .attrs = i2c_dev_attrs,
317};
318
319static const struct attribute_group *i2c_dev_attr_groups[] = {
320 &i2c_dev_attr_group,
321 NULL
David Brownell7b4fbc52007-05-01 23:26:30 +0200322};
323
Tobias Klauser0b2c3682010-01-16 20:43:12 +0100324static const struct dev_pm_ops i2c_device_pm_ops = {
sonic zhang54067ee2009-12-14 21:17:30 +0100325 .suspend = i2c_device_pm_suspend,
326 .resume = i2c_device_pm_resume,
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200327 .freeze = i2c_device_pm_freeze,
328 .thaw = i2c_device_pm_thaw,
329 .poweroff = i2c_device_pm_poweroff,
330 .restore = i2c_device_pm_restore,
331 SET_RUNTIME_PM_OPS(
332 pm_generic_runtime_suspend,
333 pm_generic_runtime_resume,
334 pm_generic_runtime_idle
335 )
sonic zhang54067ee2009-12-14 21:17:30 +0100336};
337
Jon Smirle9ca9eb2008-07-14 22:38:35 +0200338struct bus_type i2c_bus_type = {
David Brownellf37dd802007-02-13 22:09:00 +0100339 .name = "i2c",
340 .match = i2c_device_match,
341 .probe = i2c_device_probe,
342 .remove = i2c_device_remove,
343 .shutdown = i2c_device_shutdown,
sonic zhang54067ee2009-12-14 21:17:30 +0100344 .pm = &i2c_device_pm_ops,
Russell Kingb864c7d2006-01-05 14:37:50 +0000345};
Jon Smirle9ca9eb2008-07-14 22:38:35 +0200346EXPORT_SYMBOL_GPL(i2c_bus_type);
Russell Kingb864c7d2006-01-05 14:37:50 +0000347
Jean Delvare51298d12009-09-18 22:45:45 +0200348static struct device_type i2c_client_type = {
349 .groups = i2c_dev_attr_groups,
350 .uevent = i2c_device_uevent,
351 .release = i2c_client_dev_release,
352};
353
David Brownell9b766b82008-01-27 18:14:51 +0100354
355/**
356 * i2c_verify_client - return parameter as i2c_client, or NULL
357 * @dev: device, probably from some driver model iterator
358 *
359 * When traversing the driver model tree, perhaps using driver model
360 * iterators like @device_for_each_child(), you can't assume very much
361 * about the nodes you find. Use this function to avoid oopses caused
362 * by wrongly treating some non-I2C device as an i2c_client.
363 */
364struct i2c_client *i2c_verify_client(struct device *dev)
365{
Jean Delvare51298d12009-09-18 22:45:45 +0200366 return (dev->type == &i2c_client_type)
David Brownell9b766b82008-01-27 18:14:51 +0100367 ? to_i2c_client(dev)
368 : NULL;
369}
370EXPORT_SYMBOL(i2c_verify_client);
371
372
Jean Delvare3a89db52010-06-03 11:33:52 +0200373/* This is a permissive address validity check, I2C address map constraints
374 * are purposedly not enforced, except for the general call address. */
375static int i2c_check_client_addr_validity(const struct i2c_client *client)
376{
377 if (client->flags & I2C_CLIENT_TEN) {
378 /* 10-bit address, all values are valid */
379 if (client->addr > 0x3ff)
380 return -EINVAL;
381 } else {
382 /* 7-bit address, reject the general call address */
383 if (client->addr == 0x00 || client->addr > 0x7f)
384 return -EINVAL;
385 }
386 return 0;
387}
388
Jean Delvare656b8762010-06-03 11:33:53 +0200389/* And this is a strict address validity check, used when probing. If a
390 * device uses a reserved address, then it shouldn't be probed. 7-bit
391 * addressing is assumed, 10-bit address devices are rare and should be
392 * explicitly enumerated. */
393static int i2c_check_addr_validity(unsigned short addr)
394{
395 /*
396 * Reserved addresses per I2C specification:
397 * 0x00 General call address / START byte
398 * 0x01 CBUS address
399 * 0x02 Reserved for different bus format
400 * 0x03 Reserved for future purposes
401 * 0x04-0x07 Hs-mode master code
402 * 0x78-0x7b 10-bit slave addressing
403 * 0x7c-0x7f Reserved for future purposes
404 */
405 if (addr < 0x08 || addr > 0x77)
406 return -EINVAL;
407 return 0;
408}
409
Jean Delvare3b5f7942010-06-03 11:33:55 +0200410static int __i2c_check_addr_busy(struct device *dev, void *addrp)
411{
412 struct i2c_client *client = i2c_verify_client(dev);
413 int addr = *(int *)addrp;
414
415 if (client && client->addr == addr)
416 return -EBUSY;
417 return 0;
418}
419
420static int i2c_check_addr_busy(struct i2c_adapter *adapter, int addr)
421{
422 return device_for_each_child(&adapter->dev, &addr,
423 __i2c_check_addr_busy);
424}
425
David Brownell9c1600e2007-05-01 23:26:31 +0200426/**
Jean Delvaref8a227e2009-06-19 16:58:18 +0200427 * i2c_new_device - instantiate an i2c device
David Brownell9c1600e2007-05-01 23:26:31 +0200428 * @adap: the adapter managing the device
429 * @info: describes one I2C device; bus_num is ignored
David Brownelld64f73b2007-07-12 14:12:28 +0200430 * Context: can sleep
David Brownell9c1600e2007-05-01 23:26:31 +0200431 *
Jean Delvaref8a227e2009-06-19 16:58:18 +0200432 * Create an i2c device. Binding is handled through driver model
433 * probe()/remove() methods. A driver may be bound to this device when we
434 * return from this function, or any later moment (e.g. maybe hotplugging will
435 * load the driver module). This call is not appropriate for use by mainboard
436 * initialization logic, which usually runs during an arch_initcall() long
437 * before any i2c_adapter could exist.
David Brownell9c1600e2007-05-01 23:26:31 +0200438 *
439 * This returns the new i2c client, which may be saved for later use with
440 * i2c_unregister_device(); or NULL to indicate an error.
441 */
442struct i2c_client *
443i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
444{
445 struct i2c_client *client;
446 int status;
447
448 client = kzalloc(sizeof *client, GFP_KERNEL);
449 if (!client)
450 return NULL;
451
452 client->adapter = adap;
453
454 client->dev.platform_data = info->platform_data;
David Brownell3bbb8352007-10-13 23:56:29 +0200455
Anton Vorontsov11f1f2a2008-10-22 20:21:33 +0200456 if (info->archdata)
457 client->dev.archdata = *info->archdata;
458
Marc Pignatee354252008-08-28 08:33:22 +0200459 client->flags = info->flags;
David Brownell9c1600e2007-05-01 23:26:31 +0200460 client->addr = info->addr;
461 client->irq = info->irq;
462
David Brownell9c1600e2007-05-01 23:26:31 +0200463 strlcpy(client->name, info->type, sizeof(client->name));
464
Jean Delvare3a89db52010-06-03 11:33:52 +0200465 /* Check for address validity */
466 status = i2c_check_client_addr_validity(client);
467 if (status) {
468 dev_err(&adap->dev, "Invalid %d-bit I2C address 0x%02hx\n",
469 client->flags & I2C_CLIENT_TEN ? 10 : 7, client->addr);
470 goto out_err_silent;
471 }
472
Jean Delvaref8a227e2009-06-19 16:58:18 +0200473 /* Check for address business */
Jean Delvare3b5f7942010-06-03 11:33:55 +0200474 status = i2c_check_addr_busy(adap, client->addr);
Jean Delvaref8a227e2009-06-19 16:58:18 +0200475 if (status)
476 goto out_err;
477
478 client->dev.parent = &client->adapter->dev;
479 client->dev.bus = &i2c_bus_type;
Jean Delvare51298d12009-09-18 22:45:45 +0200480 client->dev.type = &i2c_client_type;
Grant Likelyd12d42f2010-04-13 16:12:28 -0700481#ifdef CONFIG_OF
482 client->dev.of_node = info->of_node;
483#endif
Jean Delvaref8a227e2009-06-19 16:58:18 +0200484
485 dev_set_name(&client->dev, "%d-%04x", i2c_adapter_id(adap),
486 client->addr);
487 status = device_register(&client->dev);
488 if (status)
489 goto out_err;
490
Jean Delvaref8a227e2009-06-19 16:58:18 +0200491 dev_dbg(&adap->dev, "client [%s] registered with bus id %s\n",
492 client->name, dev_name(&client->dev));
493
David Brownell9c1600e2007-05-01 23:26:31 +0200494 return client;
Jean Delvaref8a227e2009-06-19 16:58:18 +0200495
496out_err:
497 dev_err(&adap->dev, "Failed to register i2c client %s at 0x%02x "
498 "(%d)\n", client->name, client->addr, status);
Jean Delvare3a89db52010-06-03 11:33:52 +0200499out_err_silent:
Jean Delvaref8a227e2009-06-19 16:58:18 +0200500 kfree(client);
501 return NULL;
David Brownell9c1600e2007-05-01 23:26:31 +0200502}
503EXPORT_SYMBOL_GPL(i2c_new_device);
504
505
506/**
507 * i2c_unregister_device - reverse effect of i2c_new_device()
508 * @client: value returned from i2c_new_device()
David Brownelld64f73b2007-07-12 14:12:28 +0200509 * Context: can sleep
David Brownell9c1600e2007-05-01 23:26:31 +0200510 */
511void i2c_unregister_device(struct i2c_client *client)
David Brownella1d9e6e2007-05-01 23:26:30 +0200512{
David Brownella1d9e6e2007-05-01 23:26:30 +0200513 device_unregister(&client->dev);
514}
David Brownell9c1600e2007-05-01 23:26:31 +0200515EXPORT_SYMBOL_GPL(i2c_unregister_device);
David Brownella1d9e6e2007-05-01 23:26:30 +0200516
517
Jean Delvare60b129d2008-05-11 20:37:06 +0200518static const struct i2c_device_id dummy_id[] = {
519 { "dummy", 0 },
520 { },
521};
522
Jean Delvared2653e92008-04-29 23:11:39 +0200523static int dummy_probe(struct i2c_client *client,
524 const struct i2c_device_id *id)
525{
526 return 0;
527}
528
529static int dummy_remove(struct i2c_client *client)
David Brownelle9f13732008-01-27 18:14:52 +0100530{
531 return 0;
532}
533
534static struct i2c_driver dummy_driver = {
535 .driver.name = "dummy",
Jean Delvared2653e92008-04-29 23:11:39 +0200536 .probe = dummy_probe,
537 .remove = dummy_remove,
Jean Delvare60b129d2008-05-11 20:37:06 +0200538 .id_table = dummy_id,
David Brownelle9f13732008-01-27 18:14:52 +0100539};
540
541/**
542 * i2c_new_dummy - return a new i2c device bound to a dummy driver
543 * @adapter: the adapter managing the device
544 * @address: seven bit address to be used
David Brownelle9f13732008-01-27 18:14:52 +0100545 * Context: can sleep
546 *
547 * This returns an I2C client bound to the "dummy" driver, intended for use
548 * with devices that consume multiple addresses. Examples of such chips
549 * include various EEPROMS (like 24c04 and 24c08 models).
550 *
551 * These dummy devices have two main uses. First, most I2C and SMBus calls
552 * except i2c_transfer() need a client handle; the dummy will be that handle.
553 * And second, this prevents the specified address from being bound to a
554 * different driver.
555 *
556 * This returns the new i2c client, which should be saved for later use with
557 * i2c_unregister_device(); or NULL to indicate an error.
558 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100559struct i2c_client *i2c_new_dummy(struct i2c_adapter *adapter, u16 address)
David Brownelle9f13732008-01-27 18:14:52 +0100560{
561 struct i2c_board_info info = {
Jean Delvare60b129d2008-05-11 20:37:06 +0200562 I2C_BOARD_INFO("dummy", address),
David Brownelle9f13732008-01-27 18:14:52 +0100563 };
564
David Brownelle9f13732008-01-27 18:14:52 +0100565 return i2c_new_device(adapter, &info);
566}
567EXPORT_SYMBOL_GPL(i2c_new_dummy);
568
David Brownellf37dd802007-02-13 22:09:00 +0100569/* ------------------------------------------------------------------------- */
570
David Brownell16ffadf2007-05-01 23:26:28 +0200571/* I2C bus adapters -- one roots each I2C or SMBUS segment */
572
Adrian Bunk83eaaed2007-10-13 23:56:30 +0200573static void i2c_adapter_dev_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574{
David Brownellef2c83212007-05-01 23:26:28 +0200575 struct i2c_adapter *adap = to_i2c_adapter(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 complete(&adap->dev_released);
577}
578
Jean Delvare99cd8e22009-06-19 16:58:20 +0200579/*
580 * Let users instantiate I2C devices through sysfs. This can be used when
581 * platform initialization code doesn't contain the proper data for
582 * whatever reason. Also useful for drivers that do device detection and
583 * detection fails, either because the device uses an unexpected address,
584 * or this is a compatible device with different ID register values.
585 *
586 * Parameter checking may look overzealous, but we really don't want
587 * the user to provide incorrect parameters.
588 */
589static ssize_t
590i2c_sysfs_new_device(struct device *dev, struct device_attribute *attr,
591 const char *buf, size_t count)
592{
593 struct i2c_adapter *adap = to_i2c_adapter(dev);
594 struct i2c_board_info info;
595 struct i2c_client *client;
596 char *blank, end;
597 int res;
598
599 dev_warn(dev, "The new_device interface is still experimental "
600 "and may change in a near future\n");
601 memset(&info, 0, sizeof(struct i2c_board_info));
602
603 blank = strchr(buf, ' ');
604 if (!blank) {
605 dev_err(dev, "%s: Missing parameters\n", "new_device");
606 return -EINVAL;
607 }
608 if (blank - buf > I2C_NAME_SIZE - 1) {
609 dev_err(dev, "%s: Invalid device name\n", "new_device");
610 return -EINVAL;
611 }
612 memcpy(info.type, buf, blank - buf);
613
614 /* Parse remaining parameters, reject extra parameters */
615 res = sscanf(++blank, "%hi%c", &info.addr, &end);
616 if (res < 1) {
617 dev_err(dev, "%s: Can't parse I2C address\n", "new_device");
618 return -EINVAL;
619 }
620 if (res > 1 && end != '\n') {
621 dev_err(dev, "%s: Extra parameters\n", "new_device");
622 return -EINVAL;
623 }
624
Jean Delvare99cd8e22009-06-19 16:58:20 +0200625 client = i2c_new_device(adap, &info);
626 if (!client)
Jean Delvare3a89db52010-06-03 11:33:52 +0200627 return -EINVAL;
Jean Delvare99cd8e22009-06-19 16:58:20 +0200628
629 /* Keep track of the added device */
Jean Delvare6629dcf2010-05-04 11:09:28 +0200630 i2c_lock_adapter(adap);
631 list_add_tail(&client->detected, &adap->userspace_clients);
632 i2c_unlock_adapter(adap);
Jean Delvare99cd8e22009-06-19 16:58:20 +0200633 dev_info(dev, "%s: Instantiated device %s at 0x%02hx\n", "new_device",
634 info.type, info.addr);
635
636 return count;
637}
638
639/*
640 * And of course let the users delete the devices they instantiated, if
641 * they got it wrong. This interface can only be used to delete devices
642 * instantiated by i2c_sysfs_new_device above. This guarantees that we
643 * don't delete devices to which some kernel code still has references.
644 *
645 * Parameter checking may look overzealous, but we really don't want
646 * the user to delete the wrong device.
647 */
648static ssize_t
649i2c_sysfs_delete_device(struct device *dev, struct device_attribute *attr,
650 const char *buf, size_t count)
651{
652 struct i2c_adapter *adap = to_i2c_adapter(dev);
653 struct i2c_client *client, *next;
654 unsigned short addr;
655 char end;
656 int res;
657
658 /* Parse parameters, reject extra parameters */
659 res = sscanf(buf, "%hi%c", &addr, &end);
660 if (res < 1) {
661 dev_err(dev, "%s: Can't parse I2C address\n", "delete_device");
662 return -EINVAL;
663 }
664 if (res > 1 && end != '\n') {
665 dev_err(dev, "%s: Extra parameters\n", "delete_device");
666 return -EINVAL;
667 }
668
669 /* Make sure the device was added through sysfs */
670 res = -ENOENT;
Jean Delvare6629dcf2010-05-04 11:09:28 +0200671 i2c_lock_adapter(adap);
672 list_for_each_entry_safe(client, next, &adap->userspace_clients,
673 detected) {
674 if (client->addr == addr) {
Jean Delvare99cd8e22009-06-19 16:58:20 +0200675 dev_info(dev, "%s: Deleting device %s at 0x%02hx\n",
676 "delete_device", client->name, client->addr);
677
678 list_del(&client->detected);
679 i2c_unregister_device(client);
680 res = count;
681 break;
682 }
683 }
Jean Delvare6629dcf2010-05-04 11:09:28 +0200684 i2c_unlock_adapter(adap);
Jean Delvare99cd8e22009-06-19 16:58:20 +0200685
686 if (res < 0)
687 dev_err(dev, "%s: Can't find device in list\n",
688 "delete_device");
689 return res;
690}
691
Jean Delvare4f8cf822009-09-18 22:45:46 +0200692static DEVICE_ATTR(new_device, S_IWUSR, NULL, i2c_sysfs_new_device);
693static DEVICE_ATTR(delete_device, S_IWUSR, NULL, i2c_sysfs_delete_device);
694
695static struct attribute *i2c_adapter_attrs[] = {
696 &dev_attr_name.attr,
697 &dev_attr_new_device.attr,
698 &dev_attr_delete_device.attr,
699 NULL
David Brownell16ffadf2007-05-01 23:26:28 +0200700};
701
Jean Delvare4f8cf822009-09-18 22:45:46 +0200702static struct attribute_group i2c_adapter_attr_group = {
703 .attrs = i2c_adapter_attrs,
704};
705
706static const struct attribute_group *i2c_adapter_attr_groups[] = {
707 &i2c_adapter_attr_group,
708 NULL
709};
710
711static struct device_type i2c_adapter_type = {
712 .groups = i2c_adapter_attr_groups,
713 .release = i2c_adapter_dev_release,
David Brownell16ffadf2007-05-01 23:26:28 +0200714};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
Jean Delvare2bb50952009-09-18 22:45:46 +0200716#ifdef CONFIG_I2C_COMPAT
717static struct class_compat *i2c_adapter_compat_class;
718#endif
719
David Brownell9c1600e2007-05-01 23:26:31 +0200720static void i2c_scan_static_board_info(struct i2c_adapter *adapter)
721{
722 struct i2c_devinfo *devinfo;
723
Rodolfo Giomettif18c41d2009-06-19 16:58:20 +0200724 down_read(&__i2c_board_lock);
David Brownell9c1600e2007-05-01 23:26:31 +0200725 list_for_each_entry(devinfo, &__i2c_board_list, list) {
726 if (devinfo->busnum == adapter->nr
727 && !i2c_new_device(adapter,
728 &devinfo->board_info))
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100729 dev_err(&adapter->dev,
730 "Can't create device at 0x%02x\n",
David Brownell9c1600e2007-05-01 23:26:31 +0200731 devinfo->board_info.addr);
732 }
Rodolfo Giomettif18c41d2009-06-19 16:58:20 +0200733 up_read(&__i2c_board_lock);
David Brownell9c1600e2007-05-01 23:26:31 +0200734}
735
Jean Delvare69b00892009-12-06 17:06:27 +0100736static int i2c_do_add_adapter(struct i2c_driver *driver,
737 struct i2c_adapter *adap)
Jean Delvare026526f2008-01-27 18:14:49 +0100738{
Jean Delvare4735c982008-07-14 22:38:36 +0200739 /* Detect supported devices on that bus, and instantiate them */
740 i2c_detect(adap, driver);
741
742 /* Let legacy drivers scan this bus for matching devices */
Jean Delvare026526f2008-01-27 18:14:49 +0100743 if (driver->attach_adapter) {
744 /* We ignore the return code; if it fails, too bad */
745 driver->attach_adapter(adap);
746 }
747 return 0;
748}
749
Jean Delvare69b00892009-12-06 17:06:27 +0100750static int __process_new_adapter(struct device_driver *d, void *data)
751{
752 return i2c_do_add_adapter(to_i2c_driver(d), data);
753}
754
David Brownell6e13e642007-05-01 23:26:31 +0200755static int i2c_register_adapter(struct i2c_adapter *adap)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756{
Jean Delvare026526f2008-01-27 18:14:49 +0100757 int res = 0, dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758
David Brownell1d0b19c2008-10-14 17:30:05 +0200759 /* Can't register until after driver model init */
Jean Delvare35fc37f2009-06-19 16:58:19 +0200760 if (unlikely(WARN_ON(!i2c_bus_type.p))) {
761 res = -EAGAIN;
762 goto out_list;
763 }
David Brownell1d0b19c2008-10-14 17:30:05 +0200764
Mika Kuoppala194684e2009-12-06 17:06:22 +0100765 rt_mutex_init(&adap->bus_lock);
Jean Delvare6629dcf2010-05-04 11:09:28 +0200766 INIT_LIST_HEAD(&adap->userspace_clients);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767
Jean Delvare8fcfef62009-03-28 21:34:43 +0100768 /* Set default timeout to 1 second if not already set */
769 if (adap->timeout == 0)
770 adap->timeout = HZ;
771
Kay Sievers27d9c182009-01-07 14:29:16 +0100772 dev_set_name(&adap->dev, "i2c-%d", adap->nr);
Jean Delvare4f8cf822009-09-18 22:45:46 +0200773 adap->dev.bus = &i2c_bus_type;
774 adap->dev.type = &i2c_adapter_type;
Jean Delvareb119c6c2006-08-15 18:26:30 +0200775 res = device_register(&adap->dev);
776 if (res)
777 goto out_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200779 dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
780
Jean Delvare2bb50952009-09-18 22:45:46 +0200781#ifdef CONFIG_I2C_COMPAT
782 res = class_compat_create_link(i2c_adapter_compat_class, &adap->dev,
783 adap->dev.parent);
784 if (res)
785 dev_warn(&adap->dev,
786 "Failed to create compatibility class link\n");
787#endif
788
Jean Delvare729d6dd2009-06-19 16:58:18 +0200789 /* create pre-declared device nodes */
David Brownell6e13e642007-05-01 23:26:31 +0200790 if (adap->nr < __i2c_first_dynamic_bus_num)
791 i2c_scan_static_board_info(adap);
792
Jean Delvare4735c982008-07-14 22:38:36 +0200793 /* Notify drivers */
Jean Delvare35fc37f2009-06-19 16:58:19 +0200794 mutex_lock(&core_lock);
Jean Delvare026526f2008-01-27 18:14:49 +0100795 dummy = bus_for_each_drv(&i2c_bus_type, NULL, adap,
Jean Delvare69b00892009-12-06 17:06:27 +0100796 __process_new_adapter);
Jean Delvarecaada322008-01-27 18:14:49 +0100797 mutex_unlock(&core_lock);
Jean Delvare35fc37f2009-06-19 16:58:19 +0200798
799 return 0;
Jean Delvareb119c6c2006-08-15 18:26:30 +0200800
Jean Delvareb119c6c2006-08-15 18:26:30 +0200801out_list:
Jean Delvare35fc37f2009-06-19 16:58:19 +0200802 mutex_lock(&core_lock);
Jean Delvareb119c6c2006-08-15 18:26:30 +0200803 idr_remove(&i2c_adapter_idr, adap->nr);
Jean Delvare35fc37f2009-06-19 16:58:19 +0200804 mutex_unlock(&core_lock);
805 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806}
807
David Brownell6e13e642007-05-01 23:26:31 +0200808/**
809 * i2c_add_adapter - declare i2c adapter, use dynamic bus number
810 * @adapter: the adapter to add
David Brownelld64f73b2007-07-12 14:12:28 +0200811 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +0200812 *
813 * This routine is used to declare an I2C adapter when its bus number
814 * doesn't matter. Examples: for I2C adapters dynamically added by
815 * USB links or PCI plugin cards.
816 *
817 * When this returns zero, a new bus number was allocated and stored
818 * in adap->nr, and the specified adapter became available for clients.
819 * Otherwise, a negative errno value is returned.
820 */
821int i2c_add_adapter(struct i2c_adapter *adapter)
822{
823 int id, res = 0;
824
825retry:
826 if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0)
827 return -ENOMEM;
828
Jean Delvarecaada322008-01-27 18:14:49 +0100829 mutex_lock(&core_lock);
David Brownell6e13e642007-05-01 23:26:31 +0200830 /* "above" here means "above or equal to", sigh */
831 res = idr_get_new_above(&i2c_adapter_idr, adapter,
832 __i2c_first_dynamic_bus_num, &id);
Jean Delvarecaada322008-01-27 18:14:49 +0100833 mutex_unlock(&core_lock);
David Brownell6e13e642007-05-01 23:26:31 +0200834
835 if (res < 0) {
836 if (res == -EAGAIN)
837 goto retry;
838 return res;
839 }
840
841 adapter->nr = id;
842 return i2c_register_adapter(adapter);
843}
844EXPORT_SYMBOL(i2c_add_adapter);
845
846/**
847 * i2c_add_numbered_adapter - declare i2c adapter, use static bus number
848 * @adap: the adapter to register (with adap->nr initialized)
David Brownelld64f73b2007-07-12 14:12:28 +0200849 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +0200850 *
851 * This routine is used to declare an I2C adapter when its bus number
Randy Dunlap8c07e462008-03-23 20:28:20 +0100852 * matters. For example, use it for I2C adapters from system-on-chip CPUs,
853 * or otherwise built in to the system's mainboard, and where i2c_board_info
David Brownell6e13e642007-05-01 23:26:31 +0200854 * is used to properly configure I2C devices.
855 *
856 * If no devices have pre-been declared for this bus, then be sure to
857 * register the adapter before any dynamically allocated ones. Otherwise
858 * the required bus ID may not be available.
859 *
860 * When this returns zero, the specified adapter became available for
861 * clients using the bus number provided in adap->nr. Also, the table
862 * of I2C devices pre-declared using i2c_register_board_info() is scanned,
863 * and the appropriate driver model device nodes are created. Otherwise, a
864 * negative errno value is returned.
865 */
866int i2c_add_numbered_adapter(struct i2c_adapter *adap)
867{
868 int id;
869 int status;
870
871 if (adap->nr & ~MAX_ID_MASK)
872 return -EINVAL;
873
874retry:
875 if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0)
876 return -ENOMEM;
877
Jean Delvarecaada322008-01-27 18:14:49 +0100878 mutex_lock(&core_lock);
David Brownell6e13e642007-05-01 23:26:31 +0200879 /* "above" here means "above or equal to", sigh;
880 * we need the "equal to" result to force the result
881 */
882 status = idr_get_new_above(&i2c_adapter_idr, adap, adap->nr, &id);
883 if (status == 0 && id != adap->nr) {
884 status = -EBUSY;
885 idr_remove(&i2c_adapter_idr, id);
886 }
Jean Delvarecaada322008-01-27 18:14:49 +0100887 mutex_unlock(&core_lock);
David Brownell6e13e642007-05-01 23:26:31 +0200888 if (status == -EAGAIN)
889 goto retry;
890
891 if (status == 0)
892 status = i2c_register_adapter(adap);
893 return status;
894}
895EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter);
896
Jean Delvare69b00892009-12-06 17:06:27 +0100897static int i2c_do_del_adapter(struct i2c_driver *driver,
898 struct i2c_adapter *adapter)
Jean Delvare026526f2008-01-27 18:14:49 +0100899{
Jean Delvare4735c982008-07-14 22:38:36 +0200900 struct i2c_client *client, *_n;
Jean Delvare026526f2008-01-27 18:14:49 +0100901 int res;
902
Jean Delvareacec2112009-03-28 21:34:40 +0100903 /* Remove the devices we created ourselves as the result of hardware
904 * probing (using a driver's detect method) */
Jean Delvare4735c982008-07-14 22:38:36 +0200905 list_for_each_entry_safe(client, _n, &driver->clients, detected) {
906 if (client->adapter == adapter) {
907 dev_dbg(&adapter->dev, "Removing %s at 0x%x\n",
908 client->name, client->addr);
909 list_del(&client->detected);
910 i2c_unregister_device(client);
911 }
912 }
913
Jean Delvare026526f2008-01-27 18:14:49 +0100914 if (!driver->detach_adapter)
915 return 0;
916 res = driver->detach_adapter(adapter);
917 if (res)
918 dev_err(&adapter->dev, "detach_adapter failed (%d) "
919 "for driver [%s]\n", res, driver->driver.name);
920 return res;
921}
922
Jean Delvaree549c2b2009-06-19 16:58:19 +0200923static int __unregister_client(struct device *dev, void *dummy)
924{
925 struct i2c_client *client = i2c_verify_client(dev);
926 if (client)
927 i2c_unregister_device(client);
928 return 0;
929}
930
Jean Delvare69b00892009-12-06 17:06:27 +0100931static int __process_removed_adapter(struct device_driver *d, void *data)
932{
933 return i2c_do_del_adapter(to_i2c_driver(d), data);
934}
935
David Brownelld64f73b2007-07-12 14:12:28 +0200936/**
937 * i2c_del_adapter - unregister I2C adapter
938 * @adap: the adapter being unregistered
939 * Context: can sleep
940 *
941 * This unregisters an I2C adapter which was previously registered
942 * by @i2c_add_adapter or @i2c_add_numbered_adapter.
943 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944int i2c_del_adapter(struct i2c_adapter *adap)
945{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 int res = 0;
Jean Delvare35fc37f2009-06-19 16:58:19 +0200947 struct i2c_adapter *found;
Jean Delvarebbd2d9c2009-11-26 09:22:33 +0100948 struct i2c_client *client, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949
950 /* First make sure that this adapter was ever added */
Jean Delvare35fc37f2009-06-19 16:58:19 +0200951 mutex_lock(&core_lock);
952 found = idr_find(&i2c_adapter_idr, adap->nr);
953 mutex_unlock(&core_lock);
954 if (found != adap) {
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200955 pr_debug("i2c-core: attempting to delete unregistered "
956 "adapter [%s]\n", adap->name);
Jean Delvare35fc37f2009-06-19 16:58:19 +0200957 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 }
959
Jean Delvare026526f2008-01-27 18:14:49 +0100960 /* Tell drivers about this removal */
Jean Delvare35fc37f2009-06-19 16:58:19 +0200961 mutex_lock(&core_lock);
Jean Delvare026526f2008-01-27 18:14:49 +0100962 res = bus_for_each_drv(&i2c_bus_type, NULL, adap,
Jean Delvare69b00892009-12-06 17:06:27 +0100963 __process_removed_adapter);
Jean Delvare35fc37f2009-06-19 16:58:19 +0200964 mutex_unlock(&core_lock);
Jean Delvare026526f2008-01-27 18:14:49 +0100965 if (res)
Jean Delvare35fc37f2009-06-19 16:58:19 +0200966 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967
Jean Delvarebbd2d9c2009-11-26 09:22:33 +0100968 /* Remove devices instantiated from sysfs */
Jean Delvare6629dcf2010-05-04 11:09:28 +0200969 i2c_lock_adapter(adap);
970 list_for_each_entry_safe(client, next, &adap->userspace_clients,
971 detected) {
972 dev_dbg(&adap->dev, "Removing %s at 0x%x\n", client->name,
973 client->addr);
974 list_del(&client->detected);
975 i2c_unregister_device(client);
Jean Delvarebbd2d9c2009-11-26 09:22:33 +0100976 }
Jean Delvare6629dcf2010-05-04 11:09:28 +0200977 i2c_unlock_adapter(adap);
Jean Delvarebbd2d9c2009-11-26 09:22:33 +0100978
Jean Delvaree549c2b2009-06-19 16:58:19 +0200979 /* Detach any active clients. This can't fail, thus we do not
980 checking the returned value. */
981 res = device_for_each_child(&adap->dev, NULL, __unregister_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982
Jean Delvare2bb50952009-09-18 22:45:46 +0200983#ifdef CONFIG_I2C_COMPAT
984 class_compat_remove_link(i2c_adapter_compat_class, &adap->dev,
985 adap->dev.parent);
986#endif
987
Thadeu Lima de Souza Cascardoc5567522010-01-16 20:43:13 +0100988 /* device name is gone after device_unregister */
989 dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
990
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 /* clean up the sysfs representation */
992 init_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 device_unregister(&adap->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994
995 /* wait for sysfs to drop all references */
996 wait_for_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997
David Brownell6e13e642007-05-01 23:26:31 +0200998 /* free bus id */
Jean Delvare35fc37f2009-06-19 16:58:19 +0200999 mutex_lock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 idr_remove(&i2c_adapter_idr, adap->nr);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001001 mutex_unlock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002
Jean Delvarebd4bc3db2008-07-16 19:30:05 +02001003 /* Clear the device structure in case this adapter is ever going to be
1004 added again */
1005 memset(&adap->dev, 0, sizeof(adap->dev));
1006
Jean Delvare35fc37f2009-06-19 16:58:19 +02001007 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008}
David Brownellc0564602007-05-01 23:26:31 +02001009EXPORT_SYMBOL(i2c_del_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010
1011
David Brownell7b4fbc52007-05-01 23:26:30 +02001012/* ------------------------------------------------------------------------- */
1013
Jean Delvare69b00892009-12-06 17:06:27 +01001014static int __process_new_driver(struct device *dev, void *data)
Dave Young7f101a92008-07-14 22:38:19 +02001015{
Jean Delvare4f8cf822009-09-18 22:45:46 +02001016 if (dev->type != &i2c_adapter_type)
1017 return 0;
Jean Delvare69b00892009-12-06 17:06:27 +01001018 return i2c_do_add_adapter(data, to_i2c_adapter(dev));
Dave Young7f101a92008-07-14 22:38:19 +02001019}
1020
David Brownell7b4fbc52007-05-01 23:26:30 +02001021/*
1022 * An i2c_driver is used with one or more i2c_client (device) nodes to access
Jean Delvare729d6dd2009-06-19 16:58:18 +02001023 * i2c slave chips, on a bus instance associated with some i2c_adapter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 */
1025
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08001026int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027{
Jean Delvare7eebcb72006-02-05 23:28:21 +01001028 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029
David Brownell1d0b19c2008-10-14 17:30:05 +02001030 /* Can't register until after driver model init */
1031 if (unlikely(WARN_ON(!i2c_bus_type.p)))
1032 return -EAGAIN;
1033
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 /* add the driver to the list of i2c drivers in the driver core */
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08001035 driver->driver.owner = owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 driver->driver.bus = &i2c_bus_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037
Jean Delvare729d6dd2009-06-19 16:58:18 +02001038 /* When registration returns, the driver core
David Brownell6e13e642007-05-01 23:26:31 +02001039 * will have called probe() for all matching-but-unbound devices.
1040 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 res = driver_register(&driver->driver);
1042 if (res)
Jean Delvare7eebcb72006-02-05 23:28:21 +01001043 return res;
David Brownell438d6c22006-12-10 21:21:31 +01001044
Laurent Riffard35d8b2e2005-11-26 20:34:05 +01001045 pr_debug("i2c-core: driver [%s] registered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046
Jean Delvare4735c982008-07-14 22:38:36 +02001047 INIT_LIST_HEAD(&driver->clients);
1048 /* Walk the adapters that are already present */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001049 mutex_lock(&core_lock);
Jean Delvare69b00892009-12-06 17:06:27 +01001050 bus_for_each_dev(&i2c_bus_type, NULL, driver, __process_new_driver);
Jean Delvarecaada322008-01-27 18:14:49 +01001051 mutex_unlock(&core_lock);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001052
Jean Delvare7eebcb72006-02-05 23:28:21 +01001053 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054}
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08001055EXPORT_SYMBOL(i2c_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056
Jean Delvare69b00892009-12-06 17:06:27 +01001057static int __process_removed_driver(struct device *dev, void *data)
Dave Young7f101a92008-07-14 22:38:19 +02001058{
Jean Delvare4f8cf822009-09-18 22:45:46 +02001059 if (dev->type != &i2c_adapter_type)
1060 return 0;
Jean Delvare69b00892009-12-06 17:06:27 +01001061 return i2c_do_del_adapter(data, to_i2c_adapter(dev));
Dave Young7f101a92008-07-14 22:38:19 +02001062}
1063
David Brownella1d9e6e2007-05-01 23:26:30 +02001064/**
1065 * i2c_del_driver - unregister I2C driver
1066 * @driver: the driver being unregistered
David Brownelld64f73b2007-07-12 14:12:28 +02001067 * Context: can sleep
David Brownella1d9e6e2007-05-01 23:26:30 +02001068 */
Jean Delvareb3e82092007-05-01 23:26:32 +02001069void i2c_del_driver(struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070{
Jean Delvarecaada322008-01-27 18:14:49 +01001071 mutex_lock(&core_lock);
Jean Delvare69b00892009-12-06 17:06:27 +01001072 bus_for_each_dev(&i2c_bus_type, NULL, driver, __process_removed_driver);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001073 mutex_unlock(&core_lock);
David Brownella1d9e6e2007-05-01 23:26:30 +02001074
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 driver_unregister(&driver->driver);
Laurent Riffard35d8b2e2005-11-26 20:34:05 +01001076 pr_debug("i2c-core: driver [%s] unregistered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077}
David Brownellc0564602007-05-01 23:26:31 +02001078EXPORT_SYMBOL(i2c_del_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079
David Brownell7b4fbc52007-05-01 23:26:30 +02001080/* ------------------------------------------------------------------------- */
1081
Jean Delvaree48d3312008-01-27 18:14:48 +01001082/**
1083 * i2c_use_client - increments the reference count of the i2c client structure
1084 * @client: the client being referenced
1085 *
1086 * Each live reference to a client should be refcounted. The driver model does
1087 * that automatically as part of driver binding, so that most drivers don't
1088 * need to do this explicitly: they hold a reference until they're unbound
1089 * from the device.
1090 *
1091 * A pointer to the client with the incremented reference counter is returned.
1092 */
1093struct i2c_client *i2c_use_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094{
David Brownell6ea438e2008-07-14 22:38:24 +02001095 if (client && get_device(&client->dev))
1096 return client;
1097 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098}
David Brownellc0564602007-05-01 23:26:31 +02001099EXPORT_SYMBOL(i2c_use_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100
Jean Delvaree48d3312008-01-27 18:14:48 +01001101/**
1102 * i2c_release_client - release a use of the i2c client structure
1103 * @client: the client being no longer referenced
1104 *
1105 * Must be called when a user of a client is finished with it.
1106 */
1107void i2c_release_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108{
David Brownell6ea438e2008-07-14 22:38:24 +02001109 if (client)
1110 put_device(&client->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111}
David Brownellc0564602007-05-01 23:26:31 +02001112EXPORT_SYMBOL(i2c_release_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113
David Brownell9b766b82008-01-27 18:14:51 +01001114struct i2c_cmd_arg {
1115 unsigned cmd;
1116 void *arg;
1117};
1118
1119static int i2c_cmd(struct device *dev, void *_arg)
1120{
1121 struct i2c_client *client = i2c_verify_client(dev);
1122 struct i2c_cmd_arg *arg = _arg;
1123
1124 if (client && client->driver && client->driver->command)
1125 client->driver->command(client, arg->cmd, arg->arg);
1126 return 0;
1127}
1128
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
1130{
David Brownell9b766b82008-01-27 18:14:51 +01001131 struct i2c_cmd_arg cmd_arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132
David Brownell9b766b82008-01-27 18:14:51 +01001133 cmd_arg.cmd = cmd;
1134 cmd_arg.arg = arg;
1135 device_for_each_child(&adap->dev, &cmd_arg, i2c_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136}
David Brownellc0564602007-05-01 23:26:31 +02001137EXPORT_SYMBOL(i2c_clients_command);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138
1139static int __init i2c_init(void)
1140{
1141 int retval;
1142
1143 retval = bus_register(&i2c_bus_type);
1144 if (retval)
1145 return retval;
Jean Delvare2bb50952009-09-18 22:45:46 +02001146#ifdef CONFIG_I2C_COMPAT
1147 i2c_adapter_compat_class = class_compat_register("i2c-adapter");
1148 if (!i2c_adapter_compat_class) {
1149 retval = -ENOMEM;
1150 goto bus_err;
1151 }
1152#endif
David Brownelle9f13732008-01-27 18:14:52 +01001153 retval = i2c_add_driver(&dummy_driver);
1154 if (retval)
Jean Delvare2bb50952009-09-18 22:45:46 +02001155 goto class_err;
David Brownelle9f13732008-01-27 18:14:52 +01001156 return 0;
1157
Jean Delvare2bb50952009-09-18 22:45:46 +02001158class_err:
1159#ifdef CONFIG_I2C_COMPAT
1160 class_compat_unregister(i2c_adapter_compat_class);
David Brownelle9f13732008-01-27 18:14:52 +01001161bus_err:
Jean Delvare2bb50952009-09-18 22:45:46 +02001162#endif
David Brownelle9f13732008-01-27 18:14:52 +01001163 bus_unregister(&i2c_bus_type);
1164 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165}
1166
1167static void __exit i2c_exit(void)
1168{
David Brownelle9f13732008-01-27 18:14:52 +01001169 i2c_del_driver(&dummy_driver);
Jean Delvare2bb50952009-09-18 22:45:46 +02001170#ifdef CONFIG_I2C_COMPAT
1171 class_compat_unregister(i2c_adapter_compat_class);
1172#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 bus_unregister(&i2c_bus_type);
1174}
1175
David Brownella10f9e72008-10-14 17:30:06 +02001176/* We must initialize early, because some subsystems register i2c drivers
1177 * in subsys_initcall() code, but are linked (and initialized) before i2c.
1178 */
1179postcore_initcall(i2c_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180module_exit(i2c_exit);
1181
1182/* ----------------------------------------------------
1183 * the functional interface to the i2c busses.
1184 * ----------------------------------------------------
1185 */
1186
David Brownella1cdeda2008-07-14 22:38:24 +02001187/**
1188 * i2c_transfer - execute a single or combined I2C message
1189 * @adap: Handle to I2C bus
1190 * @msgs: One or more messages to execute before STOP is issued to
1191 * terminate the operation; each message begins with a START.
1192 * @num: Number of messages to be executed.
1193 *
1194 * Returns negative errno, else the number of messages executed.
1195 *
1196 * Note that there is no requirement that each message be sent to
1197 * the same slave address, although that is the most common model.
1198 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001199int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200{
Clifford Wolf66b650f2009-06-15 18:01:46 +02001201 unsigned long orig_jiffies;
1202 int ret, try;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203
David Brownella1cdeda2008-07-14 22:38:24 +02001204 /* REVISIT the fault reporting model here is weak:
1205 *
1206 * - When we get an error after receiving N bytes from a slave,
1207 * there is no way to report "N".
1208 *
1209 * - When we get a NAK after transmitting N bytes to a slave,
1210 * there is no way to report "N" ... or to let the master
1211 * continue executing the rest of this combined message, if
1212 * that's the appropriate response.
1213 *
1214 * - When for example "num" is two and we successfully complete
1215 * the first message but get an error part way through the
1216 * second, it's unclear whether that should be reported as
1217 * one (discarding status on the second message) or errno
1218 * (discarding status on the first one).
1219 */
1220
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 if (adap->algo->master_xfer) {
1222#ifdef DEBUG
1223 for (ret = 0; ret < num; ret++) {
1224 dev_dbg(&adap->dev, "master_xfer[%d] %c, addr=0x%02x, "
Jean Delvare209d27c2007-05-01 23:26:29 +02001225 "len=%d%s\n", ret, (msgs[ret].flags & I2C_M_RD)
1226 ? 'R' : 'W', msgs[ret].addr, msgs[ret].len,
1227 (msgs[ret].flags & I2C_M_RECV_LEN) ? "+" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 }
1229#endif
1230
Mike Rapoportcea443a2008-01-27 18:14:50 +01001231 if (in_atomic() || irqs_disabled()) {
Mika Kuoppala194684e2009-12-06 17:06:22 +01001232 ret = rt_mutex_trylock(&adap->bus_lock);
Mike Rapoportcea443a2008-01-27 18:14:50 +01001233 if (!ret)
1234 /* I2C activity is ongoing. */
1235 return -EAGAIN;
1236 } else {
Mika Kuoppala194684e2009-12-06 17:06:22 +01001237 rt_mutex_lock(&adap->bus_lock);
Mike Rapoportcea443a2008-01-27 18:14:50 +01001238 }
1239
Clifford Wolf66b650f2009-06-15 18:01:46 +02001240 /* Retry automatically on arbitration loss */
1241 orig_jiffies = jiffies;
1242 for (ret = 0, try = 0; try <= adap->retries; try++) {
1243 ret = adap->algo->master_xfer(adap, msgs, num);
1244 if (ret != -EAGAIN)
1245 break;
1246 if (time_after(jiffies, orig_jiffies + adap->timeout))
1247 break;
1248 }
Mika Kuoppala194684e2009-12-06 17:06:22 +01001249 rt_mutex_unlock(&adap->bus_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250
1251 return ret;
1252 } else {
1253 dev_dbg(&adap->dev, "I2C level transfers not supported\n");
David Brownell24a5bb72008-07-14 22:38:23 +02001254 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 }
1256}
David Brownellc0564602007-05-01 23:26:31 +02001257EXPORT_SYMBOL(i2c_transfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258
David Brownella1cdeda2008-07-14 22:38:24 +02001259/**
1260 * i2c_master_send - issue a single I2C message in master transmit mode
1261 * @client: Handle to slave device
1262 * @buf: Data that will be written to the slave
Zhangfei Gao0c43ea52010-03-02 12:23:49 +01001263 * @count: How many bytes to write, must be less than 64k since msg.len is u16
David Brownella1cdeda2008-07-14 22:38:24 +02001264 *
1265 * Returns negative errno, or else the number of bytes written.
1266 */
Farid Hammane7225acf2010-05-21 18:40:58 +02001267int i2c_master_send(struct i2c_client *client, const char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268{
1269 int ret;
Farid Hammane7225acf2010-05-21 18:40:58 +02001270 struct i2c_adapter *adap = client->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 struct i2c_msg msg;
1272
Jean Delvare815f55f2005-05-07 22:58:46 +02001273 msg.addr = client->addr;
1274 msg.flags = client->flags & I2C_M_TEN;
1275 msg.len = count;
1276 msg.buf = (char *)buf;
David Brownell438d6c22006-12-10 21:21:31 +01001277
Jean Delvare815f55f2005-05-07 22:58:46 +02001278 ret = i2c_transfer(adap, &msg, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279
Jean Delvare815f55f2005-05-07 22:58:46 +02001280 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
1281 transmitted, else error code. */
1282 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283}
David Brownellc0564602007-05-01 23:26:31 +02001284EXPORT_SYMBOL(i2c_master_send);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285
David Brownella1cdeda2008-07-14 22:38:24 +02001286/**
1287 * i2c_master_recv - issue a single I2C message in master receive mode
1288 * @client: Handle to slave device
1289 * @buf: Where to store data read from slave
Zhangfei Gao0c43ea52010-03-02 12:23:49 +01001290 * @count: How many bytes to read, must be less than 64k since msg.len is u16
David Brownella1cdeda2008-07-14 22:38:24 +02001291 *
1292 * Returns negative errno, or else the number of bytes read.
1293 */
Farid Hammane7225acf2010-05-21 18:40:58 +02001294int i2c_master_recv(struct i2c_client *client, char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295{
Farid Hammane7225acf2010-05-21 18:40:58 +02001296 struct i2c_adapter *adap = client->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 struct i2c_msg msg;
1298 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299
Jean Delvare815f55f2005-05-07 22:58:46 +02001300 msg.addr = client->addr;
1301 msg.flags = client->flags & I2C_M_TEN;
1302 msg.flags |= I2C_M_RD;
1303 msg.len = count;
1304 msg.buf = buf;
1305
1306 ret = i2c_transfer(adap, &msg, 1);
1307
1308 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
1309 transmitted, else error code. */
1310 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311}
David Brownellc0564602007-05-01 23:26:31 +02001312EXPORT_SYMBOL(i2c_master_recv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314/* ----------------------------------------------------
1315 * the i2c address scanning function
1316 * Will not work for 10-bit addresses!
1317 * ----------------------------------------------------
1318 */
Jean Delvare9fc6adf2005-07-31 21:20:43 +02001319
Jean Delvare63e4e802010-06-03 11:33:51 +02001320/*
1321 * Legacy default probe function, mostly relevant for SMBus. The default
1322 * probe method is a quick write, but it is known to corrupt the 24RF08
1323 * EEPROMs due to a state machine bug, and could also irreversibly
1324 * write-protect some EEPROMs, so for address ranges 0x30-0x37 and 0x50-0x5f,
1325 * we use a short byte read instead. Also, some bus drivers don't implement
1326 * quick write, so we fallback to a byte read in that case too.
1327 * On x86, there is another special case for FSC hardware monitoring chips,
1328 * which want regular byte reads (address 0x73.) Fortunately, these are the
1329 * only known chips using this I2C address on PC hardware.
1330 * Returns 1 if probe succeeded, 0 if not.
1331 */
1332static int i2c_default_probe(struct i2c_adapter *adap, unsigned short addr)
1333{
1334 int err;
1335 union i2c_smbus_data dummy;
1336
1337#ifdef CONFIG_X86
1338 if (addr == 0x73 && (adap->class & I2C_CLASS_HWMON)
1339 && i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE_DATA))
1340 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
1341 I2C_SMBUS_BYTE_DATA, &dummy);
1342 else
1343#endif
1344 if ((addr & ~0x07) == 0x30 || (addr & ~0x0f) == 0x50
1345 || !i2c_check_functionality(adap, I2C_FUNC_SMBUS_QUICK))
1346 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
1347 I2C_SMBUS_BYTE, &dummy);
1348 else
1349 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_WRITE, 0,
1350 I2C_SMBUS_QUICK, NULL);
1351
1352 return err >= 0;
1353}
1354
Jean Delvareccfbbd02009-12-06 17:06:25 +01001355static int i2c_detect_address(struct i2c_client *temp_client,
Jean Delvare4735c982008-07-14 22:38:36 +02001356 struct i2c_driver *driver)
1357{
1358 struct i2c_board_info info;
1359 struct i2c_adapter *adapter = temp_client->adapter;
1360 int addr = temp_client->addr;
1361 int err;
1362
1363 /* Make sure the address is valid */
Jean Delvare656b8762010-06-03 11:33:53 +02001364 err = i2c_check_addr_validity(addr);
1365 if (err) {
Jean Delvare4735c982008-07-14 22:38:36 +02001366 dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
1367 addr);
Jean Delvare656b8762010-06-03 11:33:53 +02001368 return err;
Jean Delvare4735c982008-07-14 22:38:36 +02001369 }
1370
1371 /* Skip if already in use */
Jean Delvare3b5f7942010-06-03 11:33:55 +02001372 if (i2c_check_addr_busy(adapter, addr))
Jean Delvare4735c982008-07-14 22:38:36 +02001373 return 0;
1374
Jean Delvareccfbbd02009-12-06 17:06:25 +01001375 /* Make sure there is something at this address */
Jean Delvare63e4e802010-06-03 11:33:51 +02001376 if (!i2c_default_probe(adapter, addr))
1377 return 0;
Jean Delvare4735c982008-07-14 22:38:36 +02001378
1379 /* Finally call the custom detection function */
1380 memset(&info, 0, sizeof(struct i2c_board_info));
1381 info.addr = addr;
Jean Delvare310ec792009-12-14 21:17:23 +01001382 err = driver->detect(temp_client, &info);
Jean Delvare4735c982008-07-14 22:38:36 +02001383 if (err) {
1384 /* -ENODEV is returned if the detection fails. We catch it
1385 here as this isn't an error. */
1386 return err == -ENODEV ? 0 : err;
1387 }
1388
1389 /* Consistency check */
1390 if (info.type[0] == '\0') {
1391 dev_err(&adapter->dev, "%s detection function provided "
1392 "no name for 0x%x\n", driver->driver.name,
1393 addr);
1394 } else {
1395 struct i2c_client *client;
1396
1397 /* Detection succeeded, instantiate the device */
1398 dev_dbg(&adapter->dev, "Creating %s at 0x%02x\n",
1399 info.type, info.addr);
1400 client = i2c_new_device(adapter, &info);
1401 if (client)
1402 list_add_tail(&client->detected, &driver->clients);
1403 else
1404 dev_err(&adapter->dev, "Failed creating %s at 0x%02x\n",
1405 info.type, info.addr);
1406 }
1407 return 0;
1408}
1409
1410static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver)
1411{
Jean Delvarec3813d62009-12-14 21:17:25 +01001412 const unsigned short *address_list;
Jean Delvare4735c982008-07-14 22:38:36 +02001413 struct i2c_client *temp_client;
1414 int i, err = 0;
1415 int adap_id = i2c_adapter_id(adapter);
1416
Jean Delvarec3813d62009-12-14 21:17:25 +01001417 address_list = driver->address_list;
1418 if (!driver->detect || !address_list)
Jean Delvare4735c982008-07-14 22:38:36 +02001419 return 0;
1420
1421 /* Set up a temporary client to help detect callback */
1422 temp_client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
1423 if (!temp_client)
1424 return -ENOMEM;
1425 temp_client->adapter = adapter;
1426
Jean Delvare4329cf82008-08-28 08:33:23 +02001427 /* Stop here if the classes do not match */
1428 if (!(adapter->class & driver->class))
1429 goto exit_free;
1430
Jean Delvare827900c2010-07-10 09:42:46 +02001431 /* Stop here if the bus doesn't support probing */
1432 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_READ_BYTE)) {
Jean Delvarec3813d62009-12-14 21:17:25 +01001433 if (address_list[0] == I2C_CLIENT_END)
Jean Delvare4735c982008-07-14 22:38:36 +02001434 goto exit_free;
1435
Jean Delvare827900c2010-07-10 09:42:46 +02001436 dev_warn(&adapter->dev, "Probing not supported\n");
Jean Delvare4735c982008-07-14 22:38:36 +02001437 err = -EOPNOTSUPP;
1438 goto exit_free;
1439 }
1440
Jean Delvarec3813d62009-12-14 21:17:25 +01001441 for (i = 0; address_list[i] != I2C_CLIENT_END; i += 1) {
Jean Delvare4735c982008-07-14 22:38:36 +02001442 dev_dbg(&adapter->dev, "found normal entry for adapter %d, "
Jean Delvarec3813d62009-12-14 21:17:25 +01001443 "addr 0x%02x\n", adap_id, address_list[i]);
1444 temp_client->addr = address_list[i];
Jean Delvareccfbbd02009-12-06 17:06:25 +01001445 err = i2c_detect_address(temp_client, driver);
Jean Delvare4735c982008-07-14 22:38:36 +02001446 if (err)
1447 goto exit_free;
1448 }
1449
1450 exit_free:
1451 kfree(temp_client);
1452 return err;
1453}
1454
Jean Delvare12b5053a2007-05-01 23:26:31 +02001455struct i2c_client *
1456i2c_new_probed_device(struct i2c_adapter *adap,
1457 struct i2c_board_info *info,
1458 unsigned short const *addr_list)
1459{
1460 int i;
1461
1462 /* Stop here if the bus doesn't support probing */
1463 if (!i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE)) {
1464 dev_err(&adap->dev, "Probing not supported\n");
1465 return NULL;
1466 }
1467
Jean Delvare12b5053a2007-05-01 23:26:31 +02001468 for (i = 0; addr_list[i] != I2C_CLIENT_END; i++) {
1469 /* Check address validity */
Jean Delvare656b8762010-06-03 11:33:53 +02001470 if (i2c_check_addr_validity(addr_list[i]) < 0) {
Jean Delvare12b5053a2007-05-01 23:26:31 +02001471 dev_warn(&adap->dev, "Invalid 7-bit address "
1472 "0x%02x\n", addr_list[i]);
1473 continue;
1474 }
1475
1476 /* Check address availability */
Jean Delvare3b5f7942010-06-03 11:33:55 +02001477 if (i2c_check_addr_busy(adap, addr_list[i])) {
Jean Delvare12b5053a2007-05-01 23:26:31 +02001478 dev_dbg(&adap->dev, "Address 0x%02x already in "
1479 "use, not probing\n", addr_list[i]);
1480 continue;
1481 }
1482
Jean Delvare63e4e802010-06-03 11:33:51 +02001483 /* Test address responsiveness */
1484 if (i2c_default_probe(adap, addr_list[i]))
1485 break;
Jean Delvare12b5053a2007-05-01 23:26:31 +02001486 }
Jean Delvare12b5053a2007-05-01 23:26:31 +02001487
1488 if (addr_list[i] == I2C_CLIENT_END) {
1489 dev_dbg(&adap->dev, "Probing failed, no device found\n");
1490 return NULL;
1491 }
1492
1493 info->addr = addr_list[i];
1494 return i2c_new_device(adap, info);
1495}
1496EXPORT_SYMBOL_GPL(i2c_new_probed_device);
1497
Farid Hammane7225acf2010-05-21 18:40:58 +02001498struct i2c_adapter *i2c_get_adapter(int id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 struct i2c_adapter *adapter;
David Brownell438d6c22006-12-10 21:21:31 +01001501
Jean Delvarecaada322008-01-27 18:14:49 +01001502 mutex_lock(&core_lock);
Jack Stone1cf92b42009-06-15 18:01:46 +02001503 adapter = idr_find(&i2c_adapter_idr, id);
Mark M. Hoffmana0920e12005-06-28 00:21:30 -04001504 if (adapter && !try_module_get(adapter->owner))
1505 adapter = NULL;
1506
Jean Delvarecaada322008-01-27 18:14:49 +01001507 mutex_unlock(&core_lock);
Mark M. Hoffmana0920e12005-06-28 00:21:30 -04001508 return adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509}
David Brownellc0564602007-05-01 23:26:31 +02001510EXPORT_SYMBOL(i2c_get_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511
1512void i2c_put_adapter(struct i2c_adapter *adap)
1513{
1514 module_put(adap->owner);
1515}
David Brownellc0564602007-05-01 23:26:31 +02001516EXPORT_SYMBOL(i2c_put_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517
1518/* The SMBus parts */
1519
David Brownell438d6c22006-12-10 21:21:31 +01001520#define POLY (0x1070U << 3)
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001521static u8 crc8(u16 data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522{
1523 int i;
David Brownell438d6c22006-12-10 21:21:31 +01001524
Farid Hammane7225acf2010-05-21 18:40:58 +02001525 for (i = 0; i < 8; i++) {
David Brownell438d6c22006-12-10 21:21:31 +01001526 if (data & 0x8000)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 data = data ^ POLY;
1528 data = data << 1;
1529 }
1530 return (u8)(data >> 8);
1531}
1532
Jean Delvare421ef472005-10-26 21:28:55 +02001533/* Incremental CRC8 over count bytes in the array pointed to by p */
1534static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535{
1536 int i;
1537
Farid Hammane7225acf2010-05-21 18:40:58 +02001538 for (i = 0; i < count; i++)
Jean Delvare421ef472005-10-26 21:28:55 +02001539 crc = crc8((crc ^ p[i]) << 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 return crc;
1541}
1542
Jean Delvare421ef472005-10-26 21:28:55 +02001543/* Assume a 7-bit address, which is reasonable for SMBus */
1544static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545{
Jean Delvare421ef472005-10-26 21:28:55 +02001546 /* The address will be sent first */
1547 u8 addr = (msg->addr << 1) | !!(msg->flags & I2C_M_RD);
1548 pec = i2c_smbus_pec(pec, &addr, 1);
1549
1550 /* The data buffer follows */
1551 return i2c_smbus_pec(pec, msg->buf, msg->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552}
1553
Jean Delvare421ef472005-10-26 21:28:55 +02001554/* Used for write only transactions */
1555static inline void i2c_smbus_add_pec(struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556{
Jean Delvare421ef472005-10-26 21:28:55 +02001557 msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg);
1558 msg->len++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559}
1560
Jean Delvare421ef472005-10-26 21:28:55 +02001561/* Return <0 on CRC error
1562 If there was a write before this read (most cases) we need to take the
1563 partial CRC from the write part into account.
1564 Note that this function does modify the message (we need to decrease the
1565 message length to hide the CRC byte from the caller). */
1566static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567{
Jean Delvare421ef472005-10-26 21:28:55 +02001568 u8 rpec = msg->buf[--msg->len];
1569 cpec = i2c_smbus_msg_pec(cpec, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 if (rpec != cpec) {
1572 pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n",
1573 rpec, cpec);
David Brownell24a5bb72008-07-14 22:38:23 +02001574 return -EBADMSG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 }
David Brownell438d6c22006-12-10 21:21:31 +01001576 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577}
1578
David Brownella1cdeda2008-07-14 22:38:24 +02001579/**
1580 * i2c_smbus_read_byte - SMBus "receive byte" protocol
1581 * @client: Handle to slave device
1582 *
1583 * This executes the SMBus "receive byte" protocol, returning negative errno
1584 * else the byte received from the device.
1585 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586s32 i2c_smbus_read_byte(struct i2c_client *client)
1587{
1588 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02001589 int status;
1590
1591 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1592 I2C_SMBUS_READ, 0,
1593 I2C_SMBUS_BYTE, &data);
1594 return (status < 0) ? status : data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595}
David Brownellc0564602007-05-01 23:26:31 +02001596EXPORT_SYMBOL(i2c_smbus_read_byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597
David Brownella1cdeda2008-07-14 22:38:24 +02001598/**
1599 * i2c_smbus_write_byte - SMBus "send byte" protocol
1600 * @client: Handle to slave device
1601 * @value: Byte to be sent
1602 *
1603 * This executes the SMBus "send byte" protocol, returning negative errno
1604 * else zero on success.
1605 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606s32 i2c_smbus_write_byte(struct i2c_client *client, u8 value)
1607{
Farid Hammane7225acf2010-05-21 18:40:58 +02001608 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
Jean Delvare421ef472005-10-26 21:28:55 +02001609 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610}
David Brownellc0564602007-05-01 23:26:31 +02001611EXPORT_SYMBOL(i2c_smbus_write_byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612
David Brownella1cdeda2008-07-14 22:38:24 +02001613/**
1614 * i2c_smbus_read_byte_data - SMBus "read byte" protocol
1615 * @client: Handle to slave device
1616 * @command: Byte interpreted by slave
1617 *
1618 * This executes the SMBus "read byte" protocol, returning negative errno
1619 * else a data byte received from the device.
1620 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621s32 i2c_smbus_read_byte_data(struct i2c_client *client, u8 command)
1622{
1623 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02001624 int status;
1625
1626 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1627 I2C_SMBUS_READ, command,
1628 I2C_SMBUS_BYTE_DATA, &data);
1629 return (status < 0) ? status : data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630}
David Brownellc0564602007-05-01 23:26:31 +02001631EXPORT_SYMBOL(i2c_smbus_read_byte_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632
David Brownella1cdeda2008-07-14 22:38:24 +02001633/**
1634 * i2c_smbus_write_byte_data - SMBus "write byte" protocol
1635 * @client: Handle to slave device
1636 * @command: Byte interpreted by slave
1637 * @value: Byte being written
1638 *
1639 * This executes the SMBus "write byte" protocol, returning negative errno
1640 * else zero on success.
1641 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642s32 i2c_smbus_write_byte_data(struct i2c_client *client, u8 command, u8 value)
1643{
1644 union i2c_smbus_data data;
1645 data.byte = value;
Farid Hammane7225acf2010-05-21 18:40:58 +02001646 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1647 I2C_SMBUS_WRITE, command,
1648 I2C_SMBUS_BYTE_DATA, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649}
David Brownellc0564602007-05-01 23:26:31 +02001650EXPORT_SYMBOL(i2c_smbus_write_byte_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651
David Brownella1cdeda2008-07-14 22:38:24 +02001652/**
1653 * i2c_smbus_read_word_data - SMBus "read word" protocol
1654 * @client: Handle to slave device
1655 * @command: Byte interpreted by slave
1656 *
1657 * This executes the SMBus "read word" protocol, returning negative errno
1658 * else a 16-bit unsigned "word" received from the device.
1659 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660s32 i2c_smbus_read_word_data(struct i2c_client *client, u8 command)
1661{
1662 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02001663 int status;
1664
1665 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1666 I2C_SMBUS_READ, command,
1667 I2C_SMBUS_WORD_DATA, &data);
1668 return (status < 0) ? status : data.word;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669}
David Brownellc0564602007-05-01 23:26:31 +02001670EXPORT_SYMBOL(i2c_smbus_read_word_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671
David Brownella1cdeda2008-07-14 22:38:24 +02001672/**
1673 * i2c_smbus_write_word_data - SMBus "write word" protocol
1674 * @client: Handle to slave device
1675 * @command: Byte interpreted by slave
1676 * @value: 16-bit "word" being written
1677 *
1678 * This executes the SMBus "write word" protocol, returning negative errno
1679 * else zero on success.
1680 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681s32 i2c_smbus_write_word_data(struct i2c_client *client, u8 command, u16 value)
1682{
1683 union i2c_smbus_data data;
1684 data.word = value;
Farid Hammane7225acf2010-05-21 18:40:58 +02001685 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1686 I2C_SMBUS_WRITE, command,
1687 I2C_SMBUS_WORD_DATA, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688}
David Brownellc0564602007-05-01 23:26:31 +02001689EXPORT_SYMBOL(i2c_smbus_write_word_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690
David Brownella64ec072007-10-13 23:56:31 +02001691/**
Prakash Mortha596c88f2008-10-14 17:30:06 +02001692 * i2c_smbus_process_call - SMBus "process call" protocol
1693 * @client: Handle to slave device
1694 * @command: Byte interpreted by slave
1695 * @value: 16-bit "word" being written
1696 *
1697 * This executes the SMBus "process call" protocol, returning negative errno
1698 * else a 16-bit unsigned "word" received from the device.
1699 */
1700s32 i2c_smbus_process_call(struct i2c_client *client, u8 command, u16 value)
1701{
1702 union i2c_smbus_data data;
1703 int status;
1704 data.word = value;
1705
1706 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1707 I2C_SMBUS_WRITE, command,
1708 I2C_SMBUS_PROC_CALL, &data);
1709 return (status < 0) ? status : data.word;
1710}
1711EXPORT_SYMBOL(i2c_smbus_process_call);
1712
1713/**
David Brownella1cdeda2008-07-14 22:38:24 +02001714 * i2c_smbus_read_block_data - SMBus "block read" protocol
David Brownella64ec072007-10-13 23:56:31 +02001715 * @client: Handle to slave device
David Brownella1cdeda2008-07-14 22:38:24 +02001716 * @command: Byte interpreted by slave
David Brownella64ec072007-10-13 23:56:31 +02001717 * @values: Byte array into which data will be read; big enough to hold
1718 * the data returned by the slave. SMBus allows at most 32 bytes.
1719 *
David Brownella1cdeda2008-07-14 22:38:24 +02001720 * This executes the SMBus "block read" protocol, returning negative errno
1721 * else the number of data bytes in the slave's response.
David Brownella64ec072007-10-13 23:56:31 +02001722 *
1723 * Note that using this function requires that the client's adapter support
1724 * the I2C_FUNC_SMBUS_READ_BLOCK_DATA functionality. Not all adapter drivers
1725 * support this; its emulation through I2C messaging relies on a specific
1726 * mechanism (I2C_M_RECV_LEN) which may not be implemented.
1727 */
Jean Delvareb86a1bc2007-05-01 23:26:34 +02001728s32 i2c_smbus_read_block_data(struct i2c_client *client, u8 command,
1729 u8 *values)
1730{
1731 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02001732 int status;
Jean Delvareb86a1bc2007-05-01 23:26:34 +02001733
David Brownell24a5bb72008-07-14 22:38:23 +02001734 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1735 I2C_SMBUS_READ, command,
1736 I2C_SMBUS_BLOCK_DATA, &data);
1737 if (status)
1738 return status;
Jean Delvareb86a1bc2007-05-01 23:26:34 +02001739
1740 memcpy(values, &data.block[1], data.block[0]);
1741 return data.block[0];
1742}
1743EXPORT_SYMBOL(i2c_smbus_read_block_data);
1744
David Brownella1cdeda2008-07-14 22:38:24 +02001745/**
1746 * i2c_smbus_write_block_data - SMBus "block write" protocol
1747 * @client: Handle to slave device
1748 * @command: Byte interpreted by slave
1749 * @length: Size of data block; SMBus allows at most 32 bytes
1750 * @values: Byte array which will be written.
1751 *
1752 * This executes the SMBus "block write" protocol, returning negative errno
1753 * else zero on success.
1754 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755s32 i2c_smbus_write_block_data(struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +02001756 u8 length, const u8 *values)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757{
1758 union i2c_smbus_data data;
Jean Delvare76560322006-01-18 23:14:55 +01001759
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 if (length > I2C_SMBUS_BLOCK_MAX)
1761 length = I2C_SMBUS_BLOCK_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 data.block[0] = length;
Jean Delvare76560322006-01-18 23:14:55 +01001763 memcpy(&data.block[1], values, length);
Farid Hammane7225acf2010-05-21 18:40:58 +02001764 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1765 I2C_SMBUS_WRITE, command,
1766 I2C_SMBUS_BLOCK_DATA, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767}
David Brownellc0564602007-05-01 23:26:31 +02001768EXPORT_SYMBOL(i2c_smbus_write_block_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769
1770/* Returns the number of read bytes */
Jean Delvare4b2643d2007-07-12 14:12:29 +02001771s32 i2c_smbus_read_i2c_block_data(struct i2c_client *client, u8 command,
1772 u8 length, u8 *values)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773{
1774 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02001775 int status;
Jean Delvare76560322006-01-18 23:14:55 +01001776
Jean Delvare4b2643d2007-07-12 14:12:29 +02001777 if (length > I2C_SMBUS_BLOCK_MAX)
1778 length = I2C_SMBUS_BLOCK_MAX;
1779 data.block[0] = length;
David Brownell24a5bb72008-07-14 22:38:23 +02001780 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1781 I2C_SMBUS_READ, command,
1782 I2C_SMBUS_I2C_BLOCK_DATA, &data);
1783 if (status < 0)
1784 return status;
Jean Delvare76560322006-01-18 23:14:55 +01001785
1786 memcpy(values, &data.block[1], data.block[0]);
1787 return data.block[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788}
David Brownellc0564602007-05-01 23:26:31 +02001789EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790
Jean Delvare21bbd692006-01-09 15:19:18 +11001791s32 i2c_smbus_write_i2c_block_data(struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +02001792 u8 length, const u8 *values)
Jean Delvare21bbd692006-01-09 15:19:18 +11001793{
1794 union i2c_smbus_data data;
1795
1796 if (length > I2C_SMBUS_BLOCK_MAX)
1797 length = I2C_SMBUS_BLOCK_MAX;
1798 data.block[0] = length;
1799 memcpy(data.block + 1, values, length);
1800 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1801 I2C_SMBUS_WRITE, command,
1802 I2C_SMBUS_I2C_BLOCK_DATA, &data);
1803}
David Brownellc0564602007-05-01 23:26:31 +02001804EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data);
Jean Delvare21bbd692006-01-09 15:19:18 +11001805
David Brownell438d6c22006-12-10 21:21:31 +01001806/* Simulate a SMBus command using the i2c protocol
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 No checking of parameters is done! */
Farid Hammane7225acf2010-05-21 18:40:58 +02001808static s32 i2c_smbus_xfer_emulated(struct i2c_adapter *adapter, u16 addr,
1809 unsigned short flags,
1810 char read_write, u8 command, int size,
1811 union i2c_smbus_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812{
1813 /* So we need to generate a series of msgs. In the case of writing, we
1814 need to use only one message; when reading, we need two. We initialize
1815 most things with sane defaults, to keep the code below somewhat
1816 simpler. */
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02001817 unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3];
1818 unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2];
Farid Hammane7225acf2010-05-21 18:40:58 +02001819 int num = read_write == I2C_SMBUS_READ ? 2 : 1;
David Brownell438d6c22006-12-10 21:21:31 +01001820 struct i2c_msg msg[2] = { { addr, flags, 1, msgbuf0 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 { addr, flags | I2C_M_RD, 0, msgbuf1 }
1822 };
1823 int i;
Jean Delvare421ef472005-10-26 21:28:55 +02001824 u8 partial_pec = 0;
David Brownell24a5bb72008-07-14 22:38:23 +02001825 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826
1827 msgbuf0[0] = command;
Farid Hammane7225acf2010-05-21 18:40:58 +02001828 switch (size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 case I2C_SMBUS_QUICK:
1830 msg[0].len = 0;
1831 /* Special case: The read/write field is used as data */
Roel Kluinf29d2e02009-02-24 19:19:48 +01001832 msg[0].flags = flags | (read_write == I2C_SMBUS_READ ?
1833 I2C_M_RD : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 num = 1;
1835 break;
1836 case I2C_SMBUS_BYTE:
1837 if (read_write == I2C_SMBUS_READ) {
1838 /* Special case: only a read! */
1839 msg[0].flags = I2C_M_RD | flags;
1840 num = 1;
1841 }
1842 break;
1843 case I2C_SMBUS_BYTE_DATA:
1844 if (read_write == I2C_SMBUS_READ)
1845 msg[1].len = 1;
1846 else {
1847 msg[0].len = 2;
1848 msgbuf0[1] = data->byte;
1849 }
1850 break;
1851 case I2C_SMBUS_WORD_DATA:
1852 if (read_write == I2C_SMBUS_READ)
1853 msg[1].len = 2;
1854 else {
Farid Hammane7225acf2010-05-21 18:40:58 +02001855 msg[0].len = 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02001857 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 }
1859 break;
1860 case I2C_SMBUS_PROC_CALL:
1861 num = 2; /* Special case */
1862 read_write = I2C_SMBUS_READ;
1863 msg[0].len = 3;
1864 msg[1].len = 2;
1865 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02001866 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 break;
1868 case I2C_SMBUS_BLOCK_DATA:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 if (read_write == I2C_SMBUS_READ) {
Jean Delvare209d27c2007-05-01 23:26:29 +02001870 msg[1].flags |= I2C_M_RECV_LEN;
1871 msg[1].len = 1; /* block length will be added by
1872 the underlying bus driver */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 } else {
1874 msg[0].len = data->block[0] + 2;
1875 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
David Brownell24a5bb72008-07-14 22:38:23 +02001876 dev_err(&adapter->dev,
1877 "Invalid block write size %d\n",
1878 data->block[0]);
1879 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 }
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02001881 for (i = 1; i < msg[0].len; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 msgbuf0[i] = data->block[i-1];
1883 }
1884 break;
1885 case I2C_SMBUS_BLOCK_PROC_CALL:
Jean Delvare209d27c2007-05-01 23:26:29 +02001886 num = 2; /* Another special case */
1887 read_write = I2C_SMBUS_READ;
1888 if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
David Brownell24a5bb72008-07-14 22:38:23 +02001889 dev_err(&adapter->dev,
1890 "Invalid block write size %d\n",
Jean Delvare209d27c2007-05-01 23:26:29 +02001891 data->block[0]);
David Brownell24a5bb72008-07-14 22:38:23 +02001892 return -EINVAL;
Jean Delvare209d27c2007-05-01 23:26:29 +02001893 }
1894 msg[0].len = data->block[0] + 2;
1895 for (i = 1; i < msg[0].len; i++)
1896 msgbuf0[i] = data->block[i-1];
1897 msg[1].flags |= I2C_M_RECV_LEN;
1898 msg[1].len = 1; /* block length will be added by
1899 the underlying bus driver */
1900 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 case I2C_SMBUS_I2C_BLOCK_DATA:
1902 if (read_write == I2C_SMBUS_READ) {
Jean Delvare4b2643d2007-07-12 14:12:29 +02001903 msg[1].len = data->block[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 } else {
1905 msg[0].len = data->block[0] + 1;
Jean Delvare30dac742005-10-08 00:15:59 +02001906 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) {
David Brownell24a5bb72008-07-14 22:38:23 +02001907 dev_err(&adapter->dev,
1908 "Invalid block write size %d\n",
1909 data->block[0]);
1910 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 }
1912 for (i = 1; i <= data->block[0]; i++)
1913 msgbuf0[i] = data->block[i];
1914 }
1915 break;
1916 default:
David Brownell24a5bb72008-07-14 22:38:23 +02001917 dev_err(&adapter->dev, "Unsupported transaction %d\n", size);
1918 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 }
1920
Jean Delvare421ef472005-10-26 21:28:55 +02001921 i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK
1922 && size != I2C_SMBUS_I2C_BLOCK_DATA);
1923 if (i) {
1924 /* Compute PEC if first message is a write */
1925 if (!(msg[0].flags & I2C_M_RD)) {
David Brownell438d6c22006-12-10 21:21:31 +01001926 if (num == 1) /* Write only */
Jean Delvare421ef472005-10-26 21:28:55 +02001927 i2c_smbus_add_pec(&msg[0]);
1928 else /* Write followed by read */
1929 partial_pec = i2c_smbus_msg_pec(0, &msg[0]);
1930 }
1931 /* Ask for PEC if last message is a read */
1932 if (msg[num-1].flags & I2C_M_RD)
David Brownell438d6c22006-12-10 21:21:31 +01001933 msg[num-1].len++;
Jean Delvare421ef472005-10-26 21:28:55 +02001934 }
1935
David Brownell24a5bb72008-07-14 22:38:23 +02001936 status = i2c_transfer(adapter, msg, num);
1937 if (status < 0)
1938 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939
Jean Delvare421ef472005-10-26 21:28:55 +02001940 /* Check PEC if last message is a read */
1941 if (i && (msg[num-1].flags & I2C_M_RD)) {
David Brownell24a5bb72008-07-14 22:38:23 +02001942 status = i2c_smbus_check_pec(partial_pec, &msg[num-1]);
1943 if (status < 0)
1944 return status;
Jean Delvare421ef472005-10-26 21:28:55 +02001945 }
1946
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 if (read_write == I2C_SMBUS_READ)
Farid Hammane7225acf2010-05-21 18:40:58 +02001948 switch (size) {
1949 case I2C_SMBUS_BYTE:
1950 data->byte = msgbuf0[0];
1951 break;
1952 case I2C_SMBUS_BYTE_DATA:
1953 data->byte = msgbuf1[0];
1954 break;
1955 case I2C_SMBUS_WORD_DATA:
1956 case I2C_SMBUS_PROC_CALL:
1957 data->word = msgbuf1[0] | (msgbuf1[1] << 8);
1958 break;
1959 case I2C_SMBUS_I2C_BLOCK_DATA:
1960 for (i = 0; i < data->block[0]; i++)
1961 data->block[i+1] = msgbuf1[i];
1962 break;
1963 case I2C_SMBUS_BLOCK_DATA:
1964 case I2C_SMBUS_BLOCK_PROC_CALL:
1965 for (i = 0; i < msgbuf1[0] + 1; i++)
1966 data->block[i] = msgbuf1[i];
1967 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 }
1969 return 0;
1970}
1971
David Brownella1cdeda2008-07-14 22:38:24 +02001972/**
1973 * i2c_smbus_xfer - execute SMBus protocol operations
1974 * @adapter: Handle to I2C bus
1975 * @addr: Address of SMBus slave on that bus
1976 * @flags: I2C_CLIENT_* flags (usually zero or I2C_CLIENT_PEC)
1977 * @read_write: I2C_SMBUS_READ or I2C_SMBUS_WRITE
1978 * @command: Byte interpreted by slave, for protocols which use such bytes
1979 * @protocol: SMBus protocol operation to execute, such as I2C_SMBUS_PROC_CALL
1980 * @data: Data to be read or written
1981 *
1982 * This executes an SMBus protocol operation, and returns a negative
1983 * errno code else zero on success.
1984 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001985s32 i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr, unsigned short flags,
David Brownella1cdeda2008-07-14 22:38:24 +02001986 char read_write, u8 command, int protocol,
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001987 union i2c_smbus_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988{
Clifford Wolf66b650f2009-06-15 18:01:46 +02001989 unsigned long orig_jiffies;
1990 int try;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 s32 res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992
1993 flags &= I2C_M_TEN | I2C_CLIENT_PEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994
1995 if (adapter->algo->smbus_xfer) {
Mika Kuoppala194684e2009-12-06 17:06:22 +01001996 rt_mutex_lock(&adapter->bus_lock);
Clifford Wolf66b650f2009-06-15 18:01:46 +02001997
1998 /* Retry automatically on arbitration loss */
1999 orig_jiffies = jiffies;
2000 for (res = 0, try = 0; try <= adapter->retries; try++) {
2001 res = adapter->algo->smbus_xfer(adapter, addr, flags,
2002 read_write, command,
2003 protocol, data);
2004 if (res != -EAGAIN)
2005 break;
2006 if (time_after(jiffies,
2007 orig_jiffies + adapter->timeout))
2008 break;
2009 }
Mika Kuoppala194684e2009-12-06 17:06:22 +01002010 rt_mutex_unlock(&adapter->bus_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 } else
Farid Hammane7225acf2010-05-21 18:40:58 +02002012 res = i2c_smbus_xfer_emulated(adapter, addr, flags, read_write,
David Brownella1cdeda2008-07-14 22:38:24 +02002013 command, protocol, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 return res;
2016}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017EXPORT_SYMBOL(i2c_smbus_xfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018
2019MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
2020MODULE_DESCRIPTION("I2C-Bus main module");
2021MODULE_LICENSE("GPL");