blob: e09e143379b1545a2487c99ace1123c6c021ed80 [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>
Grant Likely959e85f2010-06-08 07:48:19 -060033#include <linux/of_i2c.h>
34#include <linux/of_device.h>
Jean Delvareb8d6f452007-02-13 22:09:00 +010035#include <linux/completion.h>
Mike Rapoportcea443a2008-01-27 18:14:50 +010036#include <linux/hardirq.h>
37#include <linux/irqflags.h>
Rodolfo Giomettif18c41d2009-06-19 16:58:20 +020038#include <linux/rwsem.h>
Mark Brown6de468a2010-03-02 12:23:46 +010039#include <linux/pm_runtime.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <asm/uaccess.h>
41
David Brownell9c1600e2007-05-01 23:26:31 +020042#include "i2c-core.h"
43
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
Jean Delvare6629dcf2010-05-04 11:09:28 +020045/* core_lock protects i2c_adapter_idr, and guarantees
Jean Delvare35fc37f2009-06-19 16:58:19 +020046 that device detection, deletion of detected devices, and attach_adapter
47 and detach_adapter calls are serialized */
Jean Delvarecaada322008-01-27 18:14:49 +010048static DEFINE_MUTEX(core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070049static DEFINE_IDR(i2c_adapter_idr);
50
Jean Delvare4f8cf822009-09-18 22:45:46 +020051static struct device_type i2c_client_type;
Jean Delvare4735c982008-07-14 22:38:36 +020052static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver);
David Brownellf37dd802007-02-13 22:09:00 +010053
54/* ------------------------------------------------------------------------- */
55
Jean Delvared2653e92008-04-29 23:11:39 +020056static const struct i2c_device_id *i2c_match_id(const struct i2c_device_id *id,
57 const struct i2c_client *client)
58{
59 while (id->name[0]) {
60 if (strcmp(client->name, id->name) == 0)
61 return id;
62 id++;
63 }
64 return NULL;
65}
66
Linus Torvalds1da177e2005-04-16 15:20:36 -070067static int i2c_device_match(struct device *dev, struct device_driver *drv)
68{
Jean Delvare51298d12009-09-18 22:45:45 +020069 struct i2c_client *client = i2c_verify_client(dev);
70 struct i2c_driver *driver;
David Brownell7b4fbc52007-05-01 23:26:30 +020071
Jean Delvare51298d12009-09-18 22:45:45 +020072 if (!client)
73 return 0;
74
Grant Likely959e85f2010-06-08 07:48:19 -060075 /* Attempt an OF style match */
76 if (of_driver_match_device(dev, drv))
77 return 1;
78
Jean Delvare51298d12009-09-18 22:45:45 +020079 driver = to_i2c_driver(drv);
Jean Delvared2653e92008-04-29 23:11:39 +020080 /* match on an id table if there is one */
81 if (driver->id_table)
82 return i2c_match_id(driver->id_table, client) != NULL;
83
Jean Delvareeb8a7902008-05-18 20:49:41 +020084 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085}
86
David Brownell7b4fbc52007-05-01 23:26:30 +020087#ifdef CONFIG_HOTPLUG
88
89/* uevent helps with hotplug: modprobe -q $(MODALIAS) */
Kay Sievers7eff2e72007-08-14 15:15:12 +020090static int i2c_device_uevent(struct device *dev, struct kobj_uevent_env *env)
David Brownell7b4fbc52007-05-01 23:26:30 +020091{
92 struct i2c_client *client = to_i2c_client(dev);
David Brownell7b4fbc52007-05-01 23:26:30 +020093
Jean Delvareeb8a7902008-05-18 20:49:41 +020094 if (add_uevent_var(env, "MODALIAS=%s%s",
95 I2C_MODULE_PREFIX, client->name))
96 return -ENOMEM;
David Brownell7b4fbc52007-05-01 23:26:30 +020097 dev_dbg(dev, "uevent\n");
98 return 0;
99}
100
101#else
102#define i2c_device_uevent NULL
103#endif /* CONFIG_HOTPLUG */
104
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105static int i2c_device_probe(struct device *dev)
106{
Jean Delvare51298d12009-09-18 22:45:45 +0200107 struct i2c_client *client = i2c_verify_client(dev);
108 struct i2c_driver *driver;
Hans Verkuil50c33042008-03-12 14:15:00 +0100109 int status;
David Brownell7b4fbc52007-05-01 23:26:30 +0200110
Jean Delvare51298d12009-09-18 22:45:45 +0200111 if (!client)
112 return 0;
113
114 driver = to_i2c_driver(dev->driver);
Jean Delvaree0457442008-07-14 22:38:30 +0200115 if (!driver->probe || !driver->id_table)
David Brownell7b4fbc52007-05-01 23:26:30 +0200116 return -ENODEV;
117 client->driver = driver;
Marc Pignatee354252008-08-28 08:33:22 +0200118 if (!device_can_wakeup(&client->dev))
119 device_init_wakeup(&client->dev,
120 client->flags & I2C_CLIENT_WAKE);
David Brownell7b4fbc52007-05-01 23:26:30 +0200121 dev_dbg(dev, "probe\n");
Jean Delvared2653e92008-04-29 23:11:39 +0200122
Jean Delvaree0457442008-07-14 22:38:30 +0200123 status = driver->probe(client, i2c_match_id(driver->id_table, client));
Wolfram Sange4a7b9b2010-05-04 11:09:27 +0200124 if (status) {
Hans Verkuil50c33042008-03-12 14:15:00 +0100125 client->driver = NULL;
Wolfram Sange4a7b9b2010-05-04 11:09:27 +0200126 i2c_set_clientdata(client, NULL);
127 }
Hans Verkuil50c33042008-03-12 14:15:00 +0100128 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129}
130
131static int i2c_device_remove(struct device *dev)
132{
Jean Delvare51298d12009-09-18 22:45:45 +0200133 struct i2c_client *client = i2c_verify_client(dev);
David Brownella1d9e6e2007-05-01 23:26:30 +0200134 struct i2c_driver *driver;
135 int status;
136
Jean Delvare51298d12009-09-18 22:45:45 +0200137 if (!client || !dev->driver)
David Brownella1d9e6e2007-05-01 23:26:30 +0200138 return 0;
139
140 driver = to_i2c_driver(dev->driver);
141 if (driver->remove) {
142 dev_dbg(dev, "remove\n");
143 status = driver->remove(client);
144 } else {
145 dev->driver = NULL;
146 status = 0;
147 }
Wolfram Sange4a7b9b2010-05-04 11:09:27 +0200148 if (status == 0) {
David Brownella1d9e6e2007-05-01 23:26:30 +0200149 client->driver = NULL;
Wolfram Sange4a7b9b2010-05-04 11:09:27 +0200150 i2c_set_clientdata(client, NULL);
151 }
David Brownella1d9e6e2007-05-01 23:26:30 +0200152 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153}
154
David Brownellf37dd802007-02-13 22:09:00 +0100155static void i2c_device_shutdown(struct device *dev)
156{
Jean Delvare51298d12009-09-18 22:45:45 +0200157 struct i2c_client *client = i2c_verify_client(dev);
David Brownellf37dd802007-02-13 22:09:00 +0100158 struct i2c_driver *driver;
159
Jean Delvare51298d12009-09-18 22:45:45 +0200160 if (!client || !dev->driver)
David Brownellf37dd802007-02-13 22:09:00 +0100161 return;
162 driver = to_i2c_driver(dev->driver);
163 if (driver->shutdown)
Jean Delvare51298d12009-09-18 22:45:45 +0200164 driver->shutdown(client);
David Brownellf37dd802007-02-13 22:09:00 +0100165}
166
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200167#ifdef CONFIG_PM_SLEEP
168static int i2c_legacy_suspend(struct device *dev, pm_message_t mesg)
David Brownellf37dd802007-02-13 22:09:00 +0100169{
Jean Delvare51298d12009-09-18 22:45:45 +0200170 struct i2c_client *client = i2c_verify_client(dev);
David Brownellf37dd802007-02-13 22:09:00 +0100171 struct i2c_driver *driver;
172
Jean Delvare51298d12009-09-18 22:45:45 +0200173 if (!client || !dev->driver)
David Brownellf37dd802007-02-13 22:09:00 +0100174 return 0;
175 driver = to_i2c_driver(dev->driver);
176 if (!driver->suspend)
177 return 0;
Jean Delvare51298d12009-09-18 22:45:45 +0200178 return driver->suspend(client, mesg);
David Brownellf37dd802007-02-13 22:09:00 +0100179}
180
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200181static int i2c_legacy_resume(struct device *dev)
David Brownellf37dd802007-02-13 22:09:00 +0100182{
Jean Delvare51298d12009-09-18 22:45:45 +0200183 struct i2c_client *client = i2c_verify_client(dev);
David Brownellf37dd802007-02-13 22:09:00 +0100184 struct i2c_driver *driver;
185
Jean Delvare51298d12009-09-18 22:45:45 +0200186 if (!client || !dev->driver)
David Brownellf37dd802007-02-13 22:09:00 +0100187 return 0;
188 driver = to_i2c_driver(dev->driver);
189 if (!driver->resume)
190 return 0;
Jean Delvare51298d12009-09-18 22:45:45 +0200191 return driver->resume(client);
David Brownellf37dd802007-02-13 22:09:00 +0100192}
193
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200194static int i2c_device_pm_suspend(struct device *dev)
195{
196 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
197
198 if (pm_runtime_suspended(dev))
199 return 0;
200
201 if (pm)
202 return pm->suspend ? pm->suspend(dev) : 0;
203
204 return i2c_legacy_suspend(dev, PMSG_SUSPEND);
205}
206
207static int i2c_device_pm_resume(struct device *dev)
208{
209 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
210 int ret;
211
212 if (pm)
213 ret = pm->resume ? pm->resume(dev) : 0;
214 else
215 ret = i2c_legacy_resume(dev);
216
217 if (!ret) {
218 pm_runtime_disable(dev);
219 pm_runtime_set_active(dev);
220 pm_runtime_enable(dev);
221 }
222
223 return ret;
224}
225
226static int i2c_device_pm_freeze(struct device *dev)
227{
228 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
229
230 if (pm_runtime_suspended(dev))
231 return 0;
232
233 if (pm)
234 return pm->freeze ? pm->freeze(dev) : 0;
235
236 return i2c_legacy_suspend(dev, PMSG_FREEZE);
237}
238
239static int i2c_device_pm_thaw(struct device *dev)
240{
241 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
242
243 if (pm_runtime_suspended(dev))
244 return 0;
245
246 if (pm)
247 return pm->thaw ? pm->thaw(dev) : 0;
248
249 return i2c_legacy_resume(dev);
250}
251
252static int i2c_device_pm_poweroff(struct device *dev)
253{
254 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
255
256 if (pm_runtime_suspended(dev))
257 return 0;
258
259 if (pm)
260 return pm->poweroff ? pm->poweroff(dev) : 0;
261
262 return i2c_legacy_suspend(dev, PMSG_HIBERNATE);
263}
264
265static int i2c_device_pm_restore(struct device *dev)
266{
267 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
268 int ret;
269
270 if (pm)
271 ret = pm->restore ? pm->restore(dev) : 0;
272 else
273 ret = i2c_legacy_resume(dev);
274
275 if (!ret) {
276 pm_runtime_disable(dev);
277 pm_runtime_set_active(dev);
278 pm_runtime_enable(dev);
279 }
280
281 return ret;
282}
283#else /* !CONFIG_PM_SLEEP */
284#define i2c_device_pm_suspend NULL
285#define i2c_device_pm_resume NULL
286#define i2c_device_pm_freeze NULL
287#define i2c_device_pm_thaw NULL
288#define i2c_device_pm_poweroff NULL
289#define i2c_device_pm_restore NULL
290#endif /* !CONFIG_PM_SLEEP */
291
David Brownell9c1600e2007-05-01 23:26:31 +0200292static void i2c_client_dev_release(struct device *dev)
293{
294 kfree(to_i2c_client(dev));
295}
296
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100297static ssize_t
Jean Delvare4f8cf822009-09-18 22:45:46 +0200298show_name(struct device *dev, struct device_attribute *attr, char *buf)
David Brownell7b4fbc52007-05-01 23:26:30 +0200299{
Jean Delvare4f8cf822009-09-18 22:45:46 +0200300 return sprintf(buf, "%s\n", dev->type == &i2c_client_type ?
301 to_i2c_client(dev)->name : to_i2c_adapter(dev)->name);
David Brownell7b4fbc52007-05-01 23:26:30 +0200302}
303
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100304static ssize_t
305show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
David Brownell7b4fbc52007-05-01 23:26:30 +0200306{
307 struct i2c_client *client = to_i2c_client(dev);
Jean Delvareeb8a7902008-05-18 20:49:41 +0200308 return sprintf(buf, "%s%s\n", I2C_MODULE_PREFIX, client->name);
David Brownell7b4fbc52007-05-01 23:26:30 +0200309}
310
Jean Delvare4f8cf822009-09-18 22:45:46 +0200311static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
Jean Delvare51298d12009-09-18 22:45:45 +0200312static DEVICE_ATTR(modalias, S_IRUGO, show_modalias, NULL);
313
314static struct attribute *i2c_dev_attrs[] = {
315 &dev_attr_name.attr,
David Brownell7b4fbc52007-05-01 23:26:30 +0200316 /* modalias helps coldplug: modprobe $(cat .../modalias) */
Jean Delvare51298d12009-09-18 22:45:45 +0200317 &dev_attr_modalias.attr,
318 NULL
319};
320
321static struct attribute_group i2c_dev_attr_group = {
322 .attrs = i2c_dev_attrs,
323};
324
325static const struct attribute_group *i2c_dev_attr_groups[] = {
326 &i2c_dev_attr_group,
327 NULL
David Brownell7b4fbc52007-05-01 23:26:30 +0200328};
329
Tobias Klauser0b2c3682010-01-16 20:43:12 +0100330static const struct dev_pm_ops i2c_device_pm_ops = {
sonic zhang54067ee2009-12-14 21:17:30 +0100331 .suspend = i2c_device_pm_suspend,
332 .resume = i2c_device_pm_resume,
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200333 .freeze = i2c_device_pm_freeze,
334 .thaw = i2c_device_pm_thaw,
335 .poweroff = i2c_device_pm_poweroff,
336 .restore = i2c_device_pm_restore,
337 SET_RUNTIME_PM_OPS(
338 pm_generic_runtime_suspend,
339 pm_generic_runtime_resume,
340 pm_generic_runtime_idle
341 )
sonic zhang54067ee2009-12-14 21:17:30 +0100342};
343
Jon Smirle9ca9eb2008-07-14 22:38:35 +0200344struct bus_type i2c_bus_type = {
David Brownellf37dd802007-02-13 22:09:00 +0100345 .name = "i2c",
346 .match = i2c_device_match,
347 .probe = i2c_device_probe,
348 .remove = i2c_device_remove,
349 .shutdown = i2c_device_shutdown,
sonic zhang54067ee2009-12-14 21:17:30 +0100350 .pm = &i2c_device_pm_ops,
Russell Kingb864c7d2006-01-05 14:37:50 +0000351};
Jon Smirle9ca9eb2008-07-14 22:38:35 +0200352EXPORT_SYMBOL_GPL(i2c_bus_type);
Russell Kingb864c7d2006-01-05 14:37:50 +0000353
Jean Delvare51298d12009-09-18 22:45:45 +0200354static struct device_type i2c_client_type = {
355 .groups = i2c_dev_attr_groups,
356 .uevent = i2c_device_uevent,
357 .release = i2c_client_dev_release,
358};
359
David Brownell9b766b82008-01-27 18:14:51 +0100360
361/**
362 * i2c_verify_client - return parameter as i2c_client, or NULL
363 * @dev: device, probably from some driver model iterator
364 *
365 * When traversing the driver model tree, perhaps using driver model
366 * iterators like @device_for_each_child(), you can't assume very much
367 * about the nodes you find. Use this function to avoid oopses caused
368 * by wrongly treating some non-I2C device as an i2c_client.
369 */
370struct i2c_client *i2c_verify_client(struct device *dev)
371{
Jean Delvare51298d12009-09-18 22:45:45 +0200372 return (dev->type == &i2c_client_type)
David Brownell9b766b82008-01-27 18:14:51 +0100373 ? to_i2c_client(dev)
374 : NULL;
375}
376EXPORT_SYMBOL(i2c_verify_client);
377
378
Jean Delvare3a89db52010-06-03 11:33:52 +0200379/* This is a permissive address validity check, I2C address map constraints
380 * are purposedly not enforced, except for the general call address. */
381static int i2c_check_client_addr_validity(const struct i2c_client *client)
382{
383 if (client->flags & I2C_CLIENT_TEN) {
384 /* 10-bit address, all values are valid */
385 if (client->addr > 0x3ff)
386 return -EINVAL;
387 } else {
388 /* 7-bit address, reject the general call address */
389 if (client->addr == 0x00 || client->addr > 0x7f)
390 return -EINVAL;
391 }
392 return 0;
393}
394
Jean Delvare656b8762010-06-03 11:33:53 +0200395/* And this is a strict address validity check, used when probing. If a
396 * device uses a reserved address, then it shouldn't be probed. 7-bit
397 * addressing is assumed, 10-bit address devices are rare and should be
398 * explicitly enumerated. */
399static int i2c_check_addr_validity(unsigned short addr)
400{
401 /*
402 * Reserved addresses per I2C specification:
403 * 0x00 General call address / START byte
404 * 0x01 CBUS address
405 * 0x02 Reserved for different bus format
406 * 0x03 Reserved for future purposes
407 * 0x04-0x07 Hs-mode master code
408 * 0x78-0x7b 10-bit slave addressing
409 * 0x7c-0x7f Reserved for future purposes
410 */
411 if (addr < 0x08 || addr > 0x77)
412 return -EINVAL;
413 return 0;
414}
415
Jean Delvare3b5f7942010-06-03 11:33:55 +0200416static int __i2c_check_addr_busy(struct device *dev, void *addrp)
417{
418 struct i2c_client *client = i2c_verify_client(dev);
419 int addr = *(int *)addrp;
420
421 if (client && client->addr == addr)
422 return -EBUSY;
423 return 0;
424}
425
426static int i2c_check_addr_busy(struct i2c_adapter *adapter, int addr)
427{
428 return device_for_each_child(&adapter->dev, &addr,
429 __i2c_check_addr_busy);
430}
431
David Brownell9c1600e2007-05-01 23:26:31 +0200432/**
Jean Delvarefe61e072010-08-11 18:20:58 +0200433 * i2c_lock_adapter - Get exclusive access to an I2C bus segment
434 * @adapter: Target I2C bus segment
435 */
436void i2c_lock_adapter(struct i2c_adapter *adapter)
437{
438 rt_mutex_lock(&adapter->bus_lock);
439}
440EXPORT_SYMBOL_GPL(i2c_lock_adapter);
441
442/**
443 * i2c_trylock_adapter - Try to get exclusive access to an I2C bus segment
444 * @adapter: Target I2C bus segment
445 */
446static int i2c_trylock_adapter(struct i2c_adapter *adapter)
447{
448 return rt_mutex_trylock(&adapter->bus_lock);
449}
450
451/**
452 * i2c_unlock_adapter - Release exclusive access to an I2C bus segment
453 * @adapter: Target I2C bus segment
454 */
455void i2c_unlock_adapter(struct i2c_adapter *adapter)
456{
457 rt_mutex_unlock(&adapter->bus_lock);
458}
459EXPORT_SYMBOL_GPL(i2c_unlock_adapter);
460
461/**
Jean Delvaref8a227e2009-06-19 16:58:18 +0200462 * i2c_new_device - instantiate an i2c device
David Brownell9c1600e2007-05-01 23:26:31 +0200463 * @adap: the adapter managing the device
464 * @info: describes one I2C device; bus_num is ignored
David Brownelld64f73b2007-07-12 14:12:28 +0200465 * Context: can sleep
David Brownell9c1600e2007-05-01 23:26:31 +0200466 *
Jean Delvaref8a227e2009-06-19 16:58:18 +0200467 * Create an i2c device. Binding is handled through driver model
468 * probe()/remove() methods. A driver may be bound to this device when we
469 * return from this function, or any later moment (e.g. maybe hotplugging will
470 * load the driver module). This call is not appropriate for use by mainboard
471 * initialization logic, which usually runs during an arch_initcall() long
472 * before any i2c_adapter could exist.
David Brownell9c1600e2007-05-01 23:26:31 +0200473 *
474 * This returns the new i2c client, which may be saved for later use with
475 * i2c_unregister_device(); or NULL to indicate an error.
476 */
477struct i2c_client *
478i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
479{
480 struct i2c_client *client;
481 int status;
482
483 client = kzalloc(sizeof *client, GFP_KERNEL);
484 if (!client)
485 return NULL;
486
487 client->adapter = adap;
488
489 client->dev.platform_data = info->platform_data;
David Brownell3bbb8352007-10-13 23:56:29 +0200490
Anton Vorontsov11f1f2a2008-10-22 20:21:33 +0200491 if (info->archdata)
492 client->dev.archdata = *info->archdata;
493
Marc Pignatee354252008-08-28 08:33:22 +0200494 client->flags = info->flags;
David Brownell9c1600e2007-05-01 23:26:31 +0200495 client->addr = info->addr;
496 client->irq = info->irq;
497
David Brownell9c1600e2007-05-01 23:26:31 +0200498 strlcpy(client->name, info->type, sizeof(client->name));
499
Jean Delvare3a89db52010-06-03 11:33:52 +0200500 /* Check for address validity */
501 status = i2c_check_client_addr_validity(client);
502 if (status) {
503 dev_err(&adap->dev, "Invalid %d-bit I2C address 0x%02hx\n",
504 client->flags & I2C_CLIENT_TEN ? 10 : 7, client->addr);
505 goto out_err_silent;
506 }
507
Jean Delvaref8a227e2009-06-19 16:58:18 +0200508 /* Check for address business */
Jean Delvare3b5f7942010-06-03 11:33:55 +0200509 status = i2c_check_addr_busy(adap, client->addr);
Jean Delvaref8a227e2009-06-19 16:58:18 +0200510 if (status)
511 goto out_err;
512
513 client->dev.parent = &client->adapter->dev;
514 client->dev.bus = &i2c_bus_type;
Jean Delvare51298d12009-09-18 22:45:45 +0200515 client->dev.type = &i2c_client_type;
Grant Likelyd12d42f2010-04-13 16:12:28 -0700516#ifdef CONFIG_OF
517 client->dev.of_node = info->of_node;
518#endif
Jean Delvaref8a227e2009-06-19 16:58:18 +0200519
520 dev_set_name(&client->dev, "%d-%04x", i2c_adapter_id(adap),
521 client->addr);
522 status = device_register(&client->dev);
523 if (status)
524 goto out_err;
525
Jean Delvaref8a227e2009-06-19 16:58:18 +0200526 dev_dbg(&adap->dev, "client [%s] registered with bus id %s\n",
527 client->name, dev_name(&client->dev));
528
David Brownell9c1600e2007-05-01 23:26:31 +0200529 return client;
Jean Delvaref8a227e2009-06-19 16:58:18 +0200530
531out_err:
532 dev_err(&adap->dev, "Failed to register i2c client %s at 0x%02x "
533 "(%d)\n", client->name, client->addr, status);
Jean Delvare3a89db52010-06-03 11:33:52 +0200534out_err_silent:
Jean Delvaref8a227e2009-06-19 16:58:18 +0200535 kfree(client);
536 return NULL;
David Brownell9c1600e2007-05-01 23:26:31 +0200537}
538EXPORT_SYMBOL_GPL(i2c_new_device);
539
540
541/**
542 * i2c_unregister_device - reverse effect of i2c_new_device()
543 * @client: value returned from i2c_new_device()
David Brownelld64f73b2007-07-12 14:12:28 +0200544 * Context: can sleep
David Brownell9c1600e2007-05-01 23:26:31 +0200545 */
546void i2c_unregister_device(struct i2c_client *client)
David Brownella1d9e6e2007-05-01 23:26:30 +0200547{
David Brownella1d9e6e2007-05-01 23:26:30 +0200548 device_unregister(&client->dev);
549}
David Brownell9c1600e2007-05-01 23:26:31 +0200550EXPORT_SYMBOL_GPL(i2c_unregister_device);
David Brownella1d9e6e2007-05-01 23:26:30 +0200551
552
Jean Delvare60b129d2008-05-11 20:37:06 +0200553static const struct i2c_device_id dummy_id[] = {
554 { "dummy", 0 },
555 { },
556};
557
Jean Delvared2653e92008-04-29 23:11:39 +0200558static int dummy_probe(struct i2c_client *client,
559 const struct i2c_device_id *id)
560{
561 return 0;
562}
563
564static int dummy_remove(struct i2c_client *client)
David Brownelle9f13732008-01-27 18:14:52 +0100565{
566 return 0;
567}
568
569static struct i2c_driver dummy_driver = {
570 .driver.name = "dummy",
Jean Delvared2653e92008-04-29 23:11:39 +0200571 .probe = dummy_probe,
572 .remove = dummy_remove,
Jean Delvare60b129d2008-05-11 20:37:06 +0200573 .id_table = dummy_id,
David Brownelle9f13732008-01-27 18:14:52 +0100574};
575
576/**
577 * i2c_new_dummy - return a new i2c device bound to a dummy driver
578 * @adapter: the adapter managing the device
579 * @address: seven bit address to be used
David Brownelle9f13732008-01-27 18:14:52 +0100580 * Context: can sleep
581 *
582 * This returns an I2C client bound to the "dummy" driver, intended for use
583 * with devices that consume multiple addresses. Examples of such chips
584 * include various EEPROMS (like 24c04 and 24c08 models).
585 *
586 * These dummy devices have two main uses. First, most I2C and SMBus calls
587 * except i2c_transfer() need a client handle; the dummy will be that handle.
588 * And second, this prevents the specified address from being bound to a
589 * different driver.
590 *
591 * This returns the new i2c client, which should be saved for later use with
592 * i2c_unregister_device(); or NULL to indicate an error.
593 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100594struct i2c_client *i2c_new_dummy(struct i2c_adapter *adapter, u16 address)
David Brownelle9f13732008-01-27 18:14:52 +0100595{
596 struct i2c_board_info info = {
Jean Delvare60b129d2008-05-11 20:37:06 +0200597 I2C_BOARD_INFO("dummy", address),
David Brownelle9f13732008-01-27 18:14:52 +0100598 };
599
David Brownelle9f13732008-01-27 18:14:52 +0100600 return i2c_new_device(adapter, &info);
601}
602EXPORT_SYMBOL_GPL(i2c_new_dummy);
603
David Brownellf37dd802007-02-13 22:09:00 +0100604/* ------------------------------------------------------------------------- */
605
David Brownell16ffadf2007-05-01 23:26:28 +0200606/* I2C bus adapters -- one roots each I2C or SMBUS segment */
607
Adrian Bunk83eaaed2007-10-13 23:56:30 +0200608static void i2c_adapter_dev_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609{
David Brownellef2c83212007-05-01 23:26:28 +0200610 struct i2c_adapter *adap = to_i2c_adapter(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 complete(&adap->dev_released);
612}
613
Jean Delvare99cd8e22009-06-19 16:58:20 +0200614/*
615 * Let users instantiate I2C devices through sysfs. This can be used when
616 * platform initialization code doesn't contain the proper data for
617 * whatever reason. Also useful for drivers that do device detection and
618 * detection fails, either because the device uses an unexpected address,
619 * or this is a compatible device with different ID register values.
620 *
621 * Parameter checking may look overzealous, but we really don't want
622 * the user to provide incorrect parameters.
623 */
624static ssize_t
625i2c_sysfs_new_device(struct device *dev, struct device_attribute *attr,
626 const char *buf, size_t count)
627{
628 struct i2c_adapter *adap = to_i2c_adapter(dev);
629 struct i2c_board_info info;
630 struct i2c_client *client;
631 char *blank, end;
632 int res;
633
634 dev_warn(dev, "The new_device interface is still experimental "
635 "and may change in a near future\n");
636 memset(&info, 0, sizeof(struct i2c_board_info));
637
638 blank = strchr(buf, ' ');
639 if (!blank) {
640 dev_err(dev, "%s: Missing parameters\n", "new_device");
641 return -EINVAL;
642 }
643 if (blank - buf > I2C_NAME_SIZE - 1) {
644 dev_err(dev, "%s: Invalid device name\n", "new_device");
645 return -EINVAL;
646 }
647 memcpy(info.type, buf, blank - buf);
648
649 /* Parse remaining parameters, reject extra parameters */
650 res = sscanf(++blank, "%hi%c", &info.addr, &end);
651 if (res < 1) {
652 dev_err(dev, "%s: Can't parse I2C address\n", "new_device");
653 return -EINVAL;
654 }
655 if (res > 1 && end != '\n') {
656 dev_err(dev, "%s: Extra parameters\n", "new_device");
657 return -EINVAL;
658 }
659
Jean Delvare99cd8e22009-06-19 16:58:20 +0200660 client = i2c_new_device(adap, &info);
661 if (!client)
Jean Delvare3a89db52010-06-03 11:33:52 +0200662 return -EINVAL;
Jean Delvare99cd8e22009-06-19 16:58:20 +0200663
664 /* Keep track of the added device */
Jean Delvare6629dcf2010-05-04 11:09:28 +0200665 i2c_lock_adapter(adap);
666 list_add_tail(&client->detected, &adap->userspace_clients);
667 i2c_unlock_adapter(adap);
Jean Delvare99cd8e22009-06-19 16:58:20 +0200668 dev_info(dev, "%s: Instantiated device %s at 0x%02hx\n", "new_device",
669 info.type, info.addr);
670
671 return count;
672}
673
674/*
675 * And of course let the users delete the devices they instantiated, if
676 * they got it wrong. This interface can only be used to delete devices
677 * instantiated by i2c_sysfs_new_device above. This guarantees that we
678 * don't delete devices to which some kernel code still has references.
679 *
680 * Parameter checking may look overzealous, but we really don't want
681 * the user to delete the wrong device.
682 */
683static ssize_t
684i2c_sysfs_delete_device(struct device *dev, struct device_attribute *attr,
685 const char *buf, size_t count)
686{
687 struct i2c_adapter *adap = to_i2c_adapter(dev);
688 struct i2c_client *client, *next;
689 unsigned short addr;
690 char end;
691 int res;
692
693 /* Parse parameters, reject extra parameters */
694 res = sscanf(buf, "%hi%c", &addr, &end);
695 if (res < 1) {
696 dev_err(dev, "%s: Can't parse I2C address\n", "delete_device");
697 return -EINVAL;
698 }
699 if (res > 1 && end != '\n') {
700 dev_err(dev, "%s: Extra parameters\n", "delete_device");
701 return -EINVAL;
702 }
703
704 /* Make sure the device was added through sysfs */
705 res = -ENOENT;
Jean Delvare6629dcf2010-05-04 11:09:28 +0200706 i2c_lock_adapter(adap);
707 list_for_each_entry_safe(client, next, &adap->userspace_clients,
708 detected) {
709 if (client->addr == addr) {
Jean Delvare99cd8e22009-06-19 16:58:20 +0200710 dev_info(dev, "%s: Deleting device %s at 0x%02hx\n",
711 "delete_device", client->name, client->addr);
712
713 list_del(&client->detected);
714 i2c_unregister_device(client);
715 res = count;
716 break;
717 }
718 }
Jean Delvare6629dcf2010-05-04 11:09:28 +0200719 i2c_unlock_adapter(adap);
Jean Delvare99cd8e22009-06-19 16:58:20 +0200720
721 if (res < 0)
722 dev_err(dev, "%s: Can't find device in list\n",
723 "delete_device");
724 return res;
725}
726
Jean Delvare4f8cf822009-09-18 22:45:46 +0200727static DEVICE_ATTR(new_device, S_IWUSR, NULL, i2c_sysfs_new_device);
728static DEVICE_ATTR(delete_device, S_IWUSR, NULL, i2c_sysfs_delete_device);
729
730static struct attribute *i2c_adapter_attrs[] = {
731 &dev_attr_name.attr,
732 &dev_attr_new_device.attr,
733 &dev_attr_delete_device.attr,
734 NULL
David Brownell16ffadf2007-05-01 23:26:28 +0200735};
736
Jean Delvare4f8cf822009-09-18 22:45:46 +0200737static struct attribute_group i2c_adapter_attr_group = {
738 .attrs = i2c_adapter_attrs,
739};
740
741static const struct attribute_group *i2c_adapter_attr_groups[] = {
742 &i2c_adapter_attr_group,
743 NULL
744};
745
746static struct device_type i2c_adapter_type = {
747 .groups = i2c_adapter_attr_groups,
748 .release = i2c_adapter_dev_release,
David Brownell16ffadf2007-05-01 23:26:28 +0200749};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750
Jean Delvare2bb50952009-09-18 22:45:46 +0200751#ifdef CONFIG_I2C_COMPAT
752static struct class_compat *i2c_adapter_compat_class;
753#endif
754
David Brownell9c1600e2007-05-01 23:26:31 +0200755static void i2c_scan_static_board_info(struct i2c_adapter *adapter)
756{
757 struct i2c_devinfo *devinfo;
758
Rodolfo Giomettif18c41d2009-06-19 16:58:20 +0200759 down_read(&__i2c_board_lock);
David Brownell9c1600e2007-05-01 23:26:31 +0200760 list_for_each_entry(devinfo, &__i2c_board_list, list) {
761 if (devinfo->busnum == adapter->nr
762 && !i2c_new_device(adapter,
763 &devinfo->board_info))
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100764 dev_err(&adapter->dev,
765 "Can't create device at 0x%02x\n",
David Brownell9c1600e2007-05-01 23:26:31 +0200766 devinfo->board_info.addr);
767 }
Rodolfo Giomettif18c41d2009-06-19 16:58:20 +0200768 up_read(&__i2c_board_lock);
David Brownell9c1600e2007-05-01 23:26:31 +0200769}
770
Jean Delvare69b00892009-12-06 17:06:27 +0100771static int i2c_do_add_adapter(struct i2c_driver *driver,
772 struct i2c_adapter *adap)
Jean Delvare026526f2008-01-27 18:14:49 +0100773{
Jean Delvare4735c982008-07-14 22:38:36 +0200774 /* Detect supported devices on that bus, and instantiate them */
775 i2c_detect(adap, driver);
776
777 /* Let legacy drivers scan this bus for matching devices */
Jean Delvare026526f2008-01-27 18:14:49 +0100778 if (driver->attach_adapter) {
779 /* We ignore the return code; if it fails, too bad */
780 driver->attach_adapter(adap);
781 }
782 return 0;
783}
784
Jean Delvare69b00892009-12-06 17:06:27 +0100785static int __process_new_adapter(struct device_driver *d, void *data)
786{
787 return i2c_do_add_adapter(to_i2c_driver(d), data);
788}
789
David Brownell6e13e642007-05-01 23:26:31 +0200790static int i2c_register_adapter(struct i2c_adapter *adap)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791{
Jean Delvare026526f2008-01-27 18:14:49 +0100792 int res = 0, dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793
David Brownell1d0b19c2008-10-14 17:30:05 +0200794 /* Can't register until after driver model init */
Jean Delvare35fc37f2009-06-19 16:58:19 +0200795 if (unlikely(WARN_ON(!i2c_bus_type.p))) {
796 res = -EAGAIN;
797 goto out_list;
798 }
David Brownell1d0b19c2008-10-14 17:30:05 +0200799
Mika Kuoppala194684e2009-12-06 17:06:22 +0100800 rt_mutex_init(&adap->bus_lock);
Jean Delvare6629dcf2010-05-04 11:09:28 +0200801 INIT_LIST_HEAD(&adap->userspace_clients);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802
Jean Delvare8fcfef62009-03-28 21:34:43 +0100803 /* Set default timeout to 1 second if not already set */
804 if (adap->timeout == 0)
805 adap->timeout = HZ;
806
Kay Sievers27d9c182009-01-07 14:29:16 +0100807 dev_set_name(&adap->dev, "i2c-%d", adap->nr);
Jean Delvare4f8cf822009-09-18 22:45:46 +0200808 adap->dev.bus = &i2c_bus_type;
809 adap->dev.type = &i2c_adapter_type;
Jean Delvareb119c6c2006-08-15 18:26:30 +0200810 res = device_register(&adap->dev);
811 if (res)
812 goto out_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200814 dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
815
Jean Delvare2bb50952009-09-18 22:45:46 +0200816#ifdef CONFIG_I2C_COMPAT
817 res = class_compat_create_link(i2c_adapter_compat_class, &adap->dev,
818 adap->dev.parent);
819 if (res)
820 dev_warn(&adap->dev,
821 "Failed to create compatibility class link\n");
822#endif
823
Jean Delvare729d6dd2009-06-19 16:58:18 +0200824 /* create pre-declared device nodes */
David Brownell6e13e642007-05-01 23:26:31 +0200825 if (adap->nr < __i2c_first_dynamic_bus_num)
826 i2c_scan_static_board_info(adap);
827
Grant Likely959e85f2010-06-08 07:48:19 -0600828 /* Register devices from the device tree */
829 of_i2c_register_devices(adap);
830
Jean Delvare4735c982008-07-14 22:38:36 +0200831 /* Notify drivers */
Jean Delvare35fc37f2009-06-19 16:58:19 +0200832 mutex_lock(&core_lock);
Jean Delvare026526f2008-01-27 18:14:49 +0100833 dummy = bus_for_each_drv(&i2c_bus_type, NULL, adap,
Jean Delvare69b00892009-12-06 17:06:27 +0100834 __process_new_adapter);
Jean Delvarecaada322008-01-27 18:14:49 +0100835 mutex_unlock(&core_lock);
Jean Delvare35fc37f2009-06-19 16:58:19 +0200836
837 return 0;
Jean Delvareb119c6c2006-08-15 18:26:30 +0200838
Jean Delvareb119c6c2006-08-15 18:26:30 +0200839out_list:
Jean Delvare35fc37f2009-06-19 16:58:19 +0200840 mutex_lock(&core_lock);
Jean Delvareb119c6c2006-08-15 18:26:30 +0200841 idr_remove(&i2c_adapter_idr, adap->nr);
Jean Delvare35fc37f2009-06-19 16:58:19 +0200842 mutex_unlock(&core_lock);
843 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844}
845
David Brownell6e13e642007-05-01 23:26:31 +0200846/**
847 * i2c_add_adapter - declare i2c adapter, use dynamic bus number
848 * @adapter: the adapter to add
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
852 * doesn't matter. Examples: for I2C adapters dynamically added by
853 * USB links or PCI plugin cards.
854 *
855 * When this returns zero, a new bus number was allocated and stored
856 * in adap->nr, and the specified adapter became available for clients.
857 * Otherwise, a negative errno value is returned.
858 */
859int i2c_add_adapter(struct i2c_adapter *adapter)
860{
861 int id, res = 0;
862
863retry:
864 if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0)
865 return -ENOMEM;
866
Jean Delvarecaada322008-01-27 18:14:49 +0100867 mutex_lock(&core_lock);
David Brownell6e13e642007-05-01 23:26:31 +0200868 /* "above" here means "above or equal to", sigh */
869 res = idr_get_new_above(&i2c_adapter_idr, adapter,
870 __i2c_first_dynamic_bus_num, &id);
Jean Delvarecaada322008-01-27 18:14:49 +0100871 mutex_unlock(&core_lock);
David Brownell6e13e642007-05-01 23:26:31 +0200872
873 if (res < 0) {
874 if (res == -EAGAIN)
875 goto retry;
876 return res;
877 }
878
879 adapter->nr = id;
880 return i2c_register_adapter(adapter);
881}
882EXPORT_SYMBOL(i2c_add_adapter);
883
884/**
885 * i2c_add_numbered_adapter - declare i2c adapter, use static bus number
886 * @adap: the adapter to register (with adap->nr initialized)
David Brownelld64f73b2007-07-12 14:12:28 +0200887 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +0200888 *
889 * This routine is used to declare an I2C adapter when its bus number
Randy Dunlap8c07e462008-03-23 20:28:20 +0100890 * matters. For example, use it for I2C adapters from system-on-chip CPUs,
891 * or otherwise built in to the system's mainboard, and where i2c_board_info
David Brownell6e13e642007-05-01 23:26:31 +0200892 * is used to properly configure I2C devices.
893 *
894 * If no devices have pre-been declared for this bus, then be sure to
895 * register the adapter before any dynamically allocated ones. Otherwise
896 * the required bus ID may not be available.
897 *
898 * When this returns zero, the specified adapter became available for
899 * clients using the bus number provided in adap->nr. Also, the table
900 * of I2C devices pre-declared using i2c_register_board_info() is scanned,
901 * and the appropriate driver model device nodes are created. Otherwise, a
902 * negative errno value is returned.
903 */
904int i2c_add_numbered_adapter(struct i2c_adapter *adap)
905{
906 int id;
907 int status;
908
909 if (adap->nr & ~MAX_ID_MASK)
910 return -EINVAL;
911
912retry:
913 if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0)
914 return -ENOMEM;
915
Jean Delvarecaada322008-01-27 18:14:49 +0100916 mutex_lock(&core_lock);
David Brownell6e13e642007-05-01 23:26:31 +0200917 /* "above" here means "above or equal to", sigh;
918 * we need the "equal to" result to force the result
919 */
920 status = idr_get_new_above(&i2c_adapter_idr, adap, adap->nr, &id);
921 if (status == 0 && id != adap->nr) {
922 status = -EBUSY;
923 idr_remove(&i2c_adapter_idr, id);
924 }
Jean Delvarecaada322008-01-27 18:14:49 +0100925 mutex_unlock(&core_lock);
David Brownell6e13e642007-05-01 23:26:31 +0200926 if (status == -EAGAIN)
927 goto retry;
928
929 if (status == 0)
930 status = i2c_register_adapter(adap);
931 return status;
932}
933EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter);
934
Jean Delvare69b00892009-12-06 17:06:27 +0100935static int i2c_do_del_adapter(struct i2c_driver *driver,
936 struct i2c_adapter *adapter)
Jean Delvare026526f2008-01-27 18:14:49 +0100937{
Jean Delvare4735c982008-07-14 22:38:36 +0200938 struct i2c_client *client, *_n;
Jean Delvare026526f2008-01-27 18:14:49 +0100939 int res;
940
Jean Delvareacec2112009-03-28 21:34:40 +0100941 /* Remove the devices we created ourselves as the result of hardware
942 * probing (using a driver's detect method) */
Jean Delvare4735c982008-07-14 22:38:36 +0200943 list_for_each_entry_safe(client, _n, &driver->clients, detected) {
944 if (client->adapter == adapter) {
945 dev_dbg(&adapter->dev, "Removing %s at 0x%x\n",
946 client->name, client->addr);
947 list_del(&client->detected);
948 i2c_unregister_device(client);
949 }
950 }
951
Jean Delvare026526f2008-01-27 18:14:49 +0100952 if (!driver->detach_adapter)
953 return 0;
954 res = driver->detach_adapter(adapter);
955 if (res)
956 dev_err(&adapter->dev, "detach_adapter failed (%d) "
957 "for driver [%s]\n", res, driver->driver.name);
958 return res;
959}
960
Jean Delvaree549c2b2009-06-19 16:58:19 +0200961static int __unregister_client(struct device *dev, void *dummy)
962{
963 struct i2c_client *client = i2c_verify_client(dev);
964 if (client)
965 i2c_unregister_device(client);
966 return 0;
967}
968
Jean Delvare69b00892009-12-06 17:06:27 +0100969static int __process_removed_adapter(struct device_driver *d, void *data)
970{
971 return i2c_do_del_adapter(to_i2c_driver(d), data);
972}
973
David Brownelld64f73b2007-07-12 14:12:28 +0200974/**
975 * i2c_del_adapter - unregister I2C adapter
976 * @adap: the adapter being unregistered
977 * Context: can sleep
978 *
979 * This unregisters an I2C adapter which was previously registered
980 * by @i2c_add_adapter or @i2c_add_numbered_adapter.
981 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982int i2c_del_adapter(struct i2c_adapter *adap)
983{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 int res = 0;
Jean Delvare35fc37f2009-06-19 16:58:19 +0200985 struct i2c_adapter *found;
Jean Delvarebbd2d9c2009-11-26 09:22:33 +0100986 struct i2c_client *client, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987
988 /* First make sure that this adapter was ever added */
Jean Delvare35fc37f2009-06-19 16:58:19 +0200989 mutex_lock(&core_lock);
990 found = idr_find(&i2c_adapter_idr, adap->nr);
991 mutex_unlock(&core_lock);
992 if (found != adap) {
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200993 pr_debug("i2c-core: attempting to delete unregistered "
994 "adapter [%s]\n", adap->name);
Jean Delvare35fc37f2009-06-19 16:58:19 +0200995 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 }
997
Jean Delvare026526f2008-01-27 18:14:49 +0100998 /* Tell drivers about this removal */
Jean Delvare35fc37f2009-06-19 16:58:19 +0200999 mutex_lock(&core_lock);
Jean Delvare026526f2008-01-27 18:14:49 +01001000 res = bus_for_each_drv(&i2c_bus_type, NULL, adap,
Jean Delvare69b00892009-12-06 17:06:27 +01001001 __process_removed_adapter);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001002 mutex_unlock(&core_lock);
Jean Delvare026526f2008-01-27 18:14:49 +01001003 if (res)
Jean Delvare35fc37f2009-06-19 16:58:19 +02001004 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001006 /* Remove devices instantiated from sysfs */
Jean Delvare6629dcf2010-05-04 11:09:28 +02001007 i2c_lock_adapter(adap);
1008 list_for_each_entry_safe(client, next, &adap->userspace_clients,
1009 detected) {
1010 dev_dbg(&adap->dev, "Removing %s at 0x%x\n", client->name,
1011 client->addr);
1012 list_del(&client->detected);
1013 i2c_unregister_device(client);
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001014 }
Jean Delvare6629dcf2010-05-04 11:09:28 +02001015 i2c_unlock_adapter(adap);
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001016
Jean Delvaree549c2b2009-06-19 16:58:19 +02001017 /* Detach any active clients. This can't fail, thus we do not
1018 checking the returned value. */
1019 res = device_for_each_child(&adap->dev, NULL, __unregister_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020
Jean Delvare2bb50952009-09-18 22:45:46 +02001021#ifdef CONFIG_I2C_COMPAT
1022 class_compat_remove_link(i2c_adapter_compat_class, &adap->dev,
1023 adap->dev.parent);
1024#endif
1025
Thadeu Lima de Souza Cascardoc5567522010-01-16 20:43:13 +01001026 /* device name is gone after device_unregister */
1027 dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
1028
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 /* clean up the sysfs representation */
1030 init_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 device_unregister(&adap->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032
1033 /* wait for sysfs to drop all references */
1034 wait_for_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035
David Brownell6e13e642007-05-01 23:26:31 +02001036 /* free bus id */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001037 mutex_lock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 idr_remove(&i2c_adapter_idr, adap->nr);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001039 mutex_unlock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040
Jean Delvarebd4bc3db2008-07-16 19:30:05 +02001041 /* Clear the device structure in case this adapter is ever going to be
1042 added again */
1043 memset(&adap->dev, 0, sizeof(adap->dev));
1044
Jean Delvare35fc37f2009-06-19 16:58:19 +02001045 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046}
David Brownellc0564602007-05-01 23:26:31 +02001047EXPORT_SYMBOL(i2c_del_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048
1049
David Brownell7b4fbc52007-05-01 23:26:30 +02001050/* ------------------------------------------------------------------------- */
1051
Jean Delvare69b00892009-12-06 17:06:27 +01001052static int __process_new_driver(struct device *dev, void *data)
Dave Young7f101a92008-07-14 22:38:19 +02001053{
Jean Delvare4f8cf822009-09-18 22:45:46 +02001054 if (dev->type != &i2c_adapter_type)
1055 return 0;
Jean Delvare69b00892009-12-06 17:06:27 +01001056 return i2c_do_add_adapter(data, to_i2c_adapter(dev));
Dave Young7f101a92008-07-14 22:38:19 +02001057}
1058
David Brownell7b4fbc52007-05-01 23:26:30 +02001059/*
1060 * An i2c_driver is used with one or more i2c_client (device) nodes to access
Jean Delvare729d6dd2009-06-19 16:58:18 +02001061 * i2c slave chips, on a bus instance associated with some i2c_adapter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 */
1063
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08001064int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065{
Jean Delvare7eebcb72006-02-05 23:28:21 +01001066 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067
David Brownell1d0b19c2008-10-14 17:30:05 +02001068 /* Can't register until after driver model init */
1069 if (unlikely(WARN_ON(!i2c_bus_type.p)))
1070 return -EAGAIN;
1071
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 /* add the driver to the list of i2c drivers in the driver core */
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08001073 driver->driver.owner = owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 driver->driver.bus = &i2c_bus_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075
Jean Delvare729d6dd2009-06-19 16:58:18 +02001076 /* When registration returns, the driver core
David Brownell6e13e642007-05-01 23:26:31 +02001077 * will have called probe() for all matching-but-unbound devices.
1078 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 res = driver_register(&driver->driver);
1080 if (res)
Jean Delvare7eebcb72006-02-05 23:28:21 +01001081 return res;
David Brownell438d6c22006-12-10 21:21:31 +01001082
Laurent Riffard35d8b2e2005-11-26 20:34:05 +01001083 pr_debug("i2c-core: driver [%s] registered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084
Jean Delvare4735c982008-07-14 22:38:36 +02001085 INIT_LIST_HEAD(&driver->clients);
1086 /* Walk the adapters that are already present */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001087 mutex_lock(&core_lock);
Jean Delvare69b00892009-12-06 17:06:27 +01001088 bus_for_each_dev(&i2c_bus_type, NULL, driver, __process_new_driver);
Jean Delvarecaada322008-01-27 18:14:49 +01001089 mutex_unlock(&core_lock);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001090
Jean Delvare7eebcb72006-02-05 23:28:21 +01001091 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092}
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08001093EXPORT_SYMBOL(i2c_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094
Jean Delvare69b00892009-12-06 17:06:27 +01001095static int __process_removed_driver(struct device *dev, void *data)
Dave Young7f101a92008-07-14 22:38:19 +02001096{
Jean Delvare4f8cf822009-09-18 22:45:46 +02001097 if (dev->type != &i2c_adapter_type)
1098 return 0;
Jean Delvare69b00892009-12-06 17:06:27 +01001099 return i2c_do_del_adapter(data, to_i2c_adapter(dev));
Dave Young7f101a92008-07-14 22:38:19 +02001100}
1101
David Brownella1d9e6e2007-05-01 23:26:30 +02001102/**
1103 * i2c_del_driver - unregister I2C driver
1104 * @driver: the driver being unregistered
David Brownelld64f73b2007-07-12 14:12:28 +02001105 * Context: can sleep
David Brownella1d9e6e2007-05-01 23:26:30 +02001106 */
Jean Delvareb3e82092007-05-01 23:26:32 +02001107void i2c_del_driver(struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108{
Jean Delvarecaada322008-01-27 18:14:49 +01001109 mutex_lock(&core_lock);
Jean Delvare69b00892009-12-06 17:06:27 +01001110 bus_for_each_dev(&i2c_bus_type, NULL, driver, __process_removed_driver);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001111 mutex_unlock(&core_lock);
David Brownella1d9e6e2007-05-01 23:26:30 +02001112
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 driver_unregister(&driver->driver);
Laurent Riffard35d8b2e2005-11-26 20:34:05 +01001114 pr_debug("i2c-core: driver [%s] unregistered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115}
David Brownellc0564602007-05-01 23:26:31 +02001116EXPORT_SYMBOL(i2c_del_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117
David Brownell7b4fbc52007-05-01 23:26:30 +02001118/* ------------------------------------------------------------------------- */
1119
Jean Delvaree48d3312008-01-27 18:14:48 +01001120/**
1121 * i2c_use_client - increments the reference count of the i2c client structure
1122 * @client: the client being referenced
1123 *
1124 * Each live reference to a client should be refcounted. The driver model does
1125 * that automatically as part of driver binding, so that most drivers don't
1126 * need to do this explicitly: they hold a reference until they're unbound
1127 * from the device.
1128 *
1129 * A pointer to the client with the incremented reference counter is returned.
1130 */
1131struct i2c_client *i2c_use_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132{
David Brownell6ea438e2008-07-14 22:38:24 +02001133 if (client && get_device(&client->dev))
1134 return client;
1135 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136}
David Brownellc0564602007-05-01 23:26:31 +02001137EXPORT_SYMBOL(i2c_use_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138
Jean Delvaree48d3312008-01-27 18:14:48 +01001139/**
1140 * i2c_release_client - release a use of the i2c client structure
1141 * @client: the client being no longer referenced
1142 *
1143 * Must be called when a user of a client is finished with it.
1144 */
1145void i2c_release_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146{
David Brownell6ea438e2008-07-14 22:38:24 +02001147 if (client)
1148 put_device(&client->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149}
David Brownellc0564602007-05-01 23:26:31 +02001150EXPORT_SYMBOL(i2c_release_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151
David Brownell9b766b82008-01-27 18:14:51 +01001152struct i2c_cmd_arg {
1153 unsigned cmd;
1154 void *arg;
1155};
1156
1157static int i2c_cmd(struct device *dev, void *_arg)
1158{
1159 struct i2c_client *client = i2c_verify_client(dev);
1160 struct i2c_cmd_arg *arg = _arg;
1161
1162 if (client && client->driver && client->driver->command)
1163 client->driver->command(client, arg->cmd, arg->arg);
1164 return 0;
1165}
1166
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
1168{
David Brownell9b766b82008-01-27 18:14:51 +01001169 struct i2c_cmd_arg cmd_arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170
David Brownell9b766b82008-01-27 18:14:51 +01001171 cmd_arg.cmd = cmd;
1172 cmd_arg.arg = arg;
1173 device_for_each_child(&adap->dev, &cmd_arg, i2c_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174}
David Brownellc0564602007-05-01 23:26:31 +02001175EXPORT_SYMBOL(i2c_clients_command);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176
1177static int __init i2c_init(void)
1178{
1179 int retval;
1180
1181 retval = bus_register(&i2c_bus_type);
1182 if (retval)
1183 return retval;
Jean Delvare2bb50952009-09-18 22:45:46 +02001184#ifdef CONFIG_I2C_COMPAT
1185 i2c_adapter_compat_class = class_compat_register("i2c-adapter");
1186 if (!i2c_adapter_compat_class) {
1187 retval = -ENOMEM;
1188 goto bus_err;
1189 }
1190#endif
David Brownelle9f13732008-01-27 18:14:52 +01001191 retval = i2c_add_driver(&dummy_driver);
1192 if (retval)
Jean Delvare2bb50952009-09-18 22:45:46 +02001193 goto class_err;
David Brownelle9f13732008-01-27 18:14:52 +01001194 return 0;
1195
Jean Delvare2bb50952009-09-18 22:45:46 +02001196class_err:
1197#ifdef CONFIG_I2C_COMPAT
1198 class_compat_unregister(i2c_adapter_compat_class);
David Brownelle9f13732008-01-27 18:14:52 +01001199bus_err:
Jean Delvare2bb50952009-09-18 22:45:46 +02001200#endif
David Brownelle9f13732008-01-27 18:14:52 +01001201 bus_unregister(&i2c_bus_type);
1202 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203}
1204
1205static void __exit i2c_exit(void)
1206{
David Brownelle9f13732008-01-27 18:14:52 +01001207 i2c_del_driver(&dummy_driver);
Jean Delvare2bb50952009-09-18 22:45:46 +02001208#ifdef CONFIG_I2C_COMPAT
1209 class_compat_unregister(i2c_adapter_compat_class);
1210#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 bus_unregister(&i2c_bus_type);
1212}
1213
David Brownella10f9e72008-10-14 17:30:06 +02001214/* We must initialize early, because some subsystems register i2c drivers
1215 * in subsys_initcall() code, but are linked (and initialized) before i2c.
1216 */
1217postcore_initcall(i2c_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218module_exit(i2c_exit);
1219
1220/* ----------------------------------------------------
1221 * the functional interface to the i2c busses.
1222 * ----------------------------------------------------
1223 */
1224
David Brownella1cdeda2008-07-14 22:38:24 +02001225/**
1226 * i2c_transfer - execute a single or combined I2C message
1227 * @adap: Handle to I2C bus
1228 * @msgs: One or more messages to execute before STOP is issued to
1229 * terminate the operation; each message begins with a START.
1230 * @num: Number of messages to be executed.
1231 *
1232 * Returns negative errno, else the number of messages executed.
1233 *
1234 * Note that there is no requirement that each message be sent to
1235 * the same slave address, although that is the most common model.
1236 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001237int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238{
Clifford Wolf66b650f2009-06-15 18:01:46 +02001239 unsigned long orig_jiffies;
1240 int ret, try;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241
David Brownella1cdeda2008-07-14 22:38:24 +02001242 /* REVISIT the fault reporting model here is weak:
1243 *
1244 * - When we get an error after receiving N bytes from a slave,
1245 * there is no way to report "N".
1246 *
1247 * - When we get a NAK after transmitting N bytes to a slave,
1248 * there is no way to report "N" ... or to let the master
1249 * continue executing the rest of this combined message, if
1250 * that's the appropriate response.
1251 *
1252 * - When for example "num" is two and we successfully complete
1253 * the first message but get an error part way through the
1254 * second, it's unclear whether that should be reported as
1255 * one (discarding status on the second message) or errno
1256 * (discarding status on the first one).
1257 */
1258
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 if (adap->algo->master_xfer) {
1260#ifdef DEBUG
1261 for (ret = 0; ret < num; ret++) {
1262 dev_dbg(&adap->dev, "master_xfer[%d] %c, addr=0x%02x, "
Jean Delvare209d27c2007-05-01 23:26:29 +02001263 "len=%d%s\n", ret, (msgs[ret].flags & I2C_M_RD)
1264 ? 'R' : 'W', msgs[ret].addr, msgs[ret].len,
1265 (msgs[ret].flags & I2C_M_RECV_LEN) ? "+" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 }
1267#endif
1268
Mike Rapoportcea443a2008-01-27 18:14:50 +01001269 if (in_atomic() || irqs_disabled()) {
Jean Delvarefe61e072010-08-11 18:20:58 +02001270 ret = i2c_trylock_adapter(adap);
Mike Rapoportcea443a2008-01-27 18:14:50 +01001271 if (!ret)
1272 /* I2C activity is ongoing. */
1273 return -EAGAIN;
1274 } else {
Jean Delvarefe61e072010-08-11 18:20:58 +02001275 i2c_lock_adapter(adap);
Mike Rapoportcea443a2008-01-27 18:14:50 +01001276 }
1277
Clifford Wolf66b650f2009-06-15 18:01:46 +02001278 /* Retry automatically on arbitration loss */
1279 orig_jiffies = jiffies;
1280 for (ret = 0, try = 0; try <= adap->retries; try++) {
1281 ret = adap->algo->master_xfer(adap, msgs, num);
1282 if (ret != -EAGAIN)
1283 break;
1284 if (time_after(jiffies, orig_jiffies + adap->timeout))
1285 break;
1286 }
Jean Delvarefe61e072010-08-11 18:20:58 +02001287 i2c_unlock_adapter(adap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288
1289 return ret;
1290 } else {
1291 dev_dbg(&adap->dev, "I2C level transfers not supported\n");
David Brownell24a5bb72008-07-14 22:38:23 +02001292 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 }
1294}
David Brownellc0564602007-05-01 23:26:31 +02001295EXPORT_SYMBOL(i2c_transfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296
David Brownella1cdeda2008-07-14 22:38:24 +02001297/**
1298 * i2c_master_send - issue a single I2C message in master transmit mode
1299 * @client: Handle to slave device
1300 * @buf: Data that will be written to the slave
Zhangfei Gao0c43ea52010-03-02 12:23:49 +01001301 * @count: How many bytes to write, must be less than 64k since msg.len is u16
David Brownella1cdeda2008-07-14 22:38:24 +02001302 *
1303 * Returns negative errno, or else the number of bytes written.
1304 */
Farid Hammane7225acf2010-05-21 18:40:58 +02001305int i2c_master_send(struct i2c_client *client, const char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306{
1307 int ret;
Farid Hammane7225acf2010-05-21 18:40:58 +02001308 struct i2c_adapter *adap = client->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 struct i2c_msg msg;
1310
Jean Delvare815f55f2005-05-07 22:58:46 +02001311 msg.addr = client->addr;
1312 msg.flags = client->flags & I2C_M_TEN;
1313 msg.len = count;
1314 msg.buf = (char *)buf;
David Brownell438d6c22006-12-10 21:21:31 +01001315
Jean Delvare815f55f2005-05-07 22:58:46 +02001316 ret = i2c_transfer(adap, &msg, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317
Jean Delvare815f55f2005-05-07 22:58:46 +02001318 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
1319 transmitted, else error code. */
1320 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321}
David Brownellc0564602007-05-01 23:26:31 +02001322EXPORT_SYMBOL(i2c_master_send);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323
David Brownella1cdeda2008-07-14 22:38:24 +02001324/**
1325 * i2c_master_recv - issue a single I2C message in master receive mode
1326 * @client: Handle to slave device
1327 * @buf: Where to store data read from slave
Zhangfei Gao0c43ea52010-03-02 12:23:49 +01001328 * @count: How many bytes to read, must be less than 64k since msg.len is u16
David Brownella1cdeda2008-07-14 22:38:24 +02001329 *
1330 * Returns negative errno, or else the number of bytes read.
1331 */
Farid Hammane7225acf2010-05-21 18:40:58 +02001332int i2c_master_recv(struct i2c_client *client, char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333{
Farid Hammane7225acf2010-05-21 18:40:58 +02001334 struct i2c_adapter *adap = client->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 struct i2c_msg msg;
1336 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337
Jean Delvare815f55f2005-05-07 22:58:46 +02001338 msg.addr = client->addr;
1339 msg.flags = client->flags & I2C_M_TEN;
1340 msg.flags |= I2C_M_RD;
1341 msg.len = count;
1342 msg.buf = buf;
1343
1344 ret = i2c_transfer(adap, &msg, 1);
1345
1346 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
1347 transmitted, else error code. */
1348 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349}
David Brownellc0564602007-05-01 23:26:31 +02001350EXPORT_SYMBOL(i2c_master_recv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352/* ----------------------------------------------------
1353 * the i2c address scanning function
1354 * Will not work for 10-bit addresses!
1355 * ----------------------------------------------------
1356 */
Jean Delvare9fc6adf2005-07-31 21:20:43 +02001357
Jean Delvare63e4e802010-06-03 11:33:51 +02001358/*
1359 * Legacy default probe function, mostly relevant for SMBus. The default
1360 * probe method is a quick write, but it is known to corrupt the 24RF08
1361 * EEPROMs due to a state machine bug, and could also irreversibly
1362 * write-protect some EEPROMs, so for address ranges 0x30-0x37 and 0x50-0x5f,
1363 * we use a short byte read instead. Also, some bus drivers don't implement
1364 * quick write, so we fallback to a byte read in that case too.
1365 * On x86, there is another special case for FSC hardware monitoring chips,
1366 * which want regular byte reads (address 0x73.) Fortunately, these are the
1367 * only known chips using this I2C address on PC hardware.
1368 * Returns 1 if probe succeeded, 0 if not.
1369 */
1370static int i2c_default_probe(struct i2c_adapter *adap, unsigned short addr)
1371{
1372 int err;
1373 union i2c_smbus_data dummy;
1374
1375#ifdef CONFIG_X86
1376 if (addr == 0x73 && (adap->class & I2C_CLASS_HWMON)
1377 && i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE_DATA))
1378 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
1379 I2C_SMBUS_BYTE_DATA, &dummy);
1380 else
1381#endif
1382 if ((addr & ~0x07) == 0x30 || (addr & ~0x0f) == 0x50
1383 || !i2c_check_functionality(adap, I2C_FUNC_SMBUS_QUICK))
1384 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
1385 I2C_SMBUS_BYTE, &dummy);
1386 else
1387 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_WRITE, 0,
1388 I2C_SMBUS_QUICK, NULL);
1389
1390 return err >= 0;
1391}
1392
Jean Delvareccfbbd02009-12-06 17:06:25 +01001393static int i2c_detect_address(struct i2c_client *temp_client,
Jean Delvare4735c982008-07-14 22:38:36 +02001394 struct i2c_driver *driver)
1395{
1396 struct i2c_board_info info;
1397 struct i2c_adapter *adapter = temp_client->adapter;
1398 int addr = temp_client->addr;
1399 int err;
1400
1401 /* Make sure the address is valid */
Jean Delvare656b8762010-06-03 11:33:53 +02001402 err = i2c_check_addr_validity(addr);
1403 if (err) {
Jean Delvare4735c982008-07-14 22:38:36 +02001404 dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
1405 addr);
Jean Delvare656b8762010-06-03 11:33:53 +02001406 return err;
Jean Delvare4735c982008-07-14 22:38:36 +02001407 }
1408
1409 /* Skip if already in use */
Jean Delvare3b5f7942010-06-03 11:33:55 +02001410 if (i2c_check_addr_busy(adapter, addr))
Jean Delvare4735c982008-07-14 22:38:36 +02001411 return 0;
1412
Jean Delvareccfbbd02009-12-06 17:06:25 +01001413 /* Make sure there is something at this address */
Jean Delvare63e4e802010-06-03 11:33:51 +02001414 if (!i2c_default_probe(adapter, addr))
1415 return 0;
Jean Delvare4735c982008-07-14 22:38:36 +02001416
1417 /* Finally call the custom detection function */
1418 memset(&info, 0, sizeof(struct i2c_board_info));
1419 info.addr = addr;
Jean Delvare310ec792009-12-14 21:17:23 +01001420 err = driver->detect(temp_client, &info);
Jean Delvare4735c982008-07-14 22:38:36 +02001421 if (err) {
1422 /* -ENODEV is returned if the detection fails. We catch it
1423 here as this isn't an error. */
1424 return err == -ENODEV ? 0 : err;
1425 }
1426
1427 /* Consistency check */
1428 if (info.type[0] == '\0') {
1429 dev_err(&adapter->dev, "%s detection function provided "
1430 "no name for 0x%x\n", driver->driver.name,
1431 addr);
1432 } else {
1433 struct i2c_client *client;
1434
1435 /* Detection succeeded, instantiate the device */
1436 dev_dbg(&adapter->dev, "Creating %s at 0x%02x\n",
1437 info.type, info.addr);
1438 client = i2c_new_device(adapter, &info);
1439 if (client)
1440 list_add_tail(&client->detected, &driver->clients);
1441 else
1442 dev_err(&adapter->dev, "Failed creating %s at 0x%02x\n",
1443 info.type, info.addr);
1444 }
1445 return 0;
1446}
1447
1448static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver)
1449{
Jean Delvarec3813d62009-12-14 21:17:25 +01001450 const unsigned short *address_list;
Jean Delvare4735c982008-07-14 22:38:36 +02001451 struct i2c_client *temp_client;
1452 int i, err = 0;
1453 int adap_id = i2c_adapter_id(adapter);
1454
Jean Delvarec3813d62009-12-14 21:17:25 +01001455 address_list = driver->address_list;
1456 if (!driver->detect || !address_list)
Jean Delvare4735c982008-07-14 22:38:36 +02001457 return 0;
1458
1459 /* Set up a temporary client to help detect callback */
1460 temp_client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
1461 if (!temp_client)
1462 return -ENOMEM;
1463 temp_client->adapter = adapter;
1464
Jean Delvare4329cf82008-08-28 08:33:23 +02001465 /* Stop here if the classes do not match */
1466 if (!(adapter->class & driver->class))
1467 goto exit_free;
1468
Jean Delvare827900c2010-07-10 09:42:46 +02001469 /* Stop here if the bus doesn't support probing */
1470 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_READ_BYTE)) {
Jean Delvarec3813d62009-12-14 21:17:25 +01001471 if (address_list[0] == I2C_CLIENT_END)
Jean Delvare4735c982008-07-14 22:38:36 +02001472 goto exit_free;
1473
Jean Delvare827900c2010-07-10 09:42:46 +02001474 dev_warn(&adapter->dev, "Probing not supported\n");
Jean Delvare4735c982008-07-14 22:38:36 +02001475 err = -EOPNOTSUPP;
1476 goto exit_free;
1477 }
1478
Jean Delvarec3813d62009-12-14 21:17:25 +01001479 for (i = 0; address_list[i] != I2C_CLIENT_END; i += 1) {
Jean Delvare4735c982008-07-14 22:38:36 +02001480 dev_dbg(&adapter->dev, "found normal entry for adapter %d, "
Jean Delvarec3813d62009-12-14 21:17:25 +01001481 "addr 0x%02x\n", adap_id, address_list[i]);
1482 temp_client->addr = address_list[i];
Jean Delvareccfbbd02009-12-06 17:06:25 +01001483 err = i2c_detect_address(temp_client, driver);
Jean Delvare4735c982008-07-14 22:38:36 +02001484 if (err)
1485 goto exit_free;
1486 }
1487
1488 exit_free:
1489 kfree(temp_client);
1490 return err;
1491}
1492
Jean Delvared44f19d2010-08-11 18:20:57 +02001493int i2c_probe_func_quick_read(struct i2c_adapter *adap, unsigned short addr)
1494{
1495 return i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
1496 I2C_SMBUS_QUICK, NULL) >= 0;
1497}
1498EXPORT_SYMBOL_GPL(i2c_probe_func_quick_read);
1499
Jean Delvare12b5053a2007-05-01 23:26:31 +02001500struct i2c_client *
1501i2c_new_probed_device(struct i2c_adapter *adap,
1502 struct i2c_board_info *info,
Jean Delvare9a942412010-08-11 18:20:56 +02001503 unsigned short const *addr_list,
1504 int (*probe)(struct i2c_adapter *, unsigned short addr))
Jean Delvare12b5053a2007-05-01 23:26:31 +02001505{
1506 int i;
1507
Jean Delvare9a942412010-08-11 18:20:56 +02001508 if (!probe) {
1509 /* Stop here if the bus doesn't support probing */
1510 if (!i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE)) {
1511 dev_err(&adap->dev, "Probing not supported\n");
1512 return NULL;
1513 }
1514 probe = i2c_default_probe;
Jean Delvare12b5053a2007-05-01 23:26:31 +02001515 }
1516
Jean Delvare12b5053a2007-05-01 23:26:31 +02001517 for (i = 0; addr_list[i] != I2C_CLIENT_END; i++) {
1518 /* Check address validity */
Jean Delvare656b8762010-06-03 11:33:53 +02001519 if (i2c_check_addr_validity(addr_list[i]) < 0) {
Jean Delvare12b5053a2007-05-01 23:26:31 +02001520 dev_warn(&adap->dev, "Invalid 7-bit address "
1521 "0x%02x\n", addr_list[i]);
1522 continue;
1523 }
1524
1525 /* Check address availability */
Jean Delvare3b5f7942010-06-03 11:33:55 +02001526 if (i2c_check_addr_busy(adap, addr_list[i])) {
Jean Delvare12b5053a2007-05-01 23:26:31 +02001527 dev_dbg(&adap->dev, "Address 0x%02x already in "
1528 "use, not probing\n", addr_list[i]);
1529 continue;
1530 }
1531
Jean Delvare63e4e802010-06-03 11:33:51 +02001532 /* Test address responsiveness */
Jean Delvare9a942412010-08-11 18:20:56 +02001533 if (probe(adap, addr_list[i]))
Jean Delvare63e4e802010-06-03 11:33:51 +02001534 break;
Jean Delvare12b5053a2007-05-01 23:26:31 +02001535 }
Jean Delvare12b5053a2007-05-01 23:26:31 +02001536
1537 if (addr_list[i] == I2C_CLIENT_END) {
1538 dev_dbg(&adap->dev, "Probing failed, no device found\n");
1539 return NULL;
1540 }
1541
1542 info->addr = addr_list[i];
1543 return i2c_new_device(adap, info);
1544}
1545EXPORT_SYMBOL_GPL(i2c_new_probed_device);
1546
Farid Hammane7225acf2010-05-21 18:40:58 +02001547struct i2c_adapter *i2c_get_adapter(int id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 struct i2c_adapter *adapter;
David Brownell438d6c22006-12-10 21:21:31 +01001550
Jean Delvarecaada322008-01-27 18:14:49 +01001551 mutex_lock(&core_lock);
Jack Stone1cf92b42009-06-15 18:01:46 +02001552 adapter = idr_find(&i2c_adapter_idr, id);
Mark M. Hoffmana0920e12005-06-28 00:21:30 -04001553 if (adapter && !try_module_get(adapter->owner))
1554 adapter = NULL;
1555
Jean Delvarecaada322008-01-27 18:14:49 +01001556 mutex_unlock(&core_lock);
Mark M. Hoffmana0920e12005-06-28 00:21:30 -04001557 return adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558}
David Brownellc0564602007-05-01 23:26:31 +02001559EXPORT_SYMBOL(i2c_get_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560
1561void i2c_put_adapter(struct i2c_adapter *adap)
1562{
1563 module_put(adap->owner);
1564}
David Brownellc0564602007-05-01 23:26:31 +02001565EXPORT_SYMBOL(i2c_put_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566
1567/* The SMBus parts */
1568
David Brownell438d6c22006-12-10 21:21:31 +01001569#define POLY (0x1070U << 3)
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001570static u8 crc8(u16 data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571{
1572 int i;
David Brownell438d6c22006-12-10 21:21:31 +01001573
Farid Hammane7225acf2010-05-21 18:40:58 +02001574 for (i = 0; i < 8; i++) {
David Brownell438d6c22006-12-10 21:21:31 +01001575 if (data & 0x8000)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 data = data ^ POLY;
1577 data = data << 1;
1578 }
1579 return (u8)(data >> 8);
1580}
1581
Jean Delvare421ef472005-10-26 21:28:55 +02001582/* Incremental CRC8 over count bytes in the array pointed to by p */
1583static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584{
1585 int i;
1586
Farid Hammane7225acf2010-05-21 18:40:58 +02001587 for (i = 0; i < count; i++)
Jean Delvare421ef472005-10-26 21:28:55 +02001588 crc = crc8((crc ^ p[i]) << 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 return crc;
1590}
1591
Jean Delvare421ef472005-10-26 21:28:55 +02001592/* Assume a 7-bit address, which is reasonable for SMBus */
1593static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594{
Jean Delvare421ef472005-10-26 21:28:55 +02001595 /* The address will be sent first */
1596 u8 addr = (msg->addr << 1) | !!(msg->flags & I2C_M_RD);
1597 pec = i2c_smbus_pec(pec, &addr, 1);
1598
1599 /* The data buffer follows */
1600 return i2c_smbus_pec(pec, msg->buf, msg->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601}
1602
Jean Delvare421ef472005-10-26 21:28:55 +02001603/* Used for write only transactions */
1604static inline void i2c_smbus_add_pec(struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605{
Jean Delvare421ef472005-10-26 21:28:55 +02001606 msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg);
1607 msg->len++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608}
1609
Jean Delvare421ef472005-10-26 21:28:55 +02001610/* Return <0 on CRC error
1611 If there was a write before this read (most cases) we need to take the
1612 partial CRC from the write part into account.
1613 Note that this function does modify the message (we need to decrease the
1614 message length to hide the CRC byte from the caller). */
1615static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616{
Jean Delvare421ef472005-10-26 21:28:55 +02001617 u8 rpec = msg->buf[--msg->len];
1618 cpec = i2c_smbus_msg_pec(cpec, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 if (rpec != cpec) {
1621 pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n",
1622 rpec, cpec);
David Brownell24a5bb72008-07-14 22:38:23 +02001623 return -EBADMSG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 }
David Brownell438d6c22006-12-10 21:21:31 +01001625 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626}
1627
David Brownella1cdeda2008-07-14 22:38:24 +02001628/**
1629 * i2c_smbus_read_byte - SMBus "receive byte" protocol
1630 * @client: Handle to slave device
1631 *
1632 * This executes the SMBus "receive byte" protocol, returning negative errno
1633 * else the byte received from the device.
1634 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635s32 i2c_smbus_read_byte(struct i2c_client *client)
1636{
1637 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02001638 int status;
1639
1640 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1641 I2C_SMBUS_READ, 0,
1642 I2C_SMBUS_BYTE, &data);
1643 return (status < 0) ? status : data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644}
David Brownellc0564602007-05-01 23:26:31 +02001645EXPORT_SYMBOL(i2c_smbus_read_byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646
David Brownella1cdeda2008-07-14 22:38:24 +02001647/**
1648 * i2c_smbus_write_byte - SMBus "send byte" protocol
1649 * @client: Handle to slave device
1650 * @value: Byte to be sent
1651 *
1652 * This executes the SMBus "send byte" protocol, returning negative errno
1653 * else zero on success.
1654 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655s32 i2c_smbus_write_byte(struct i2c_client *client, u8 value)
1656{
Farid Hammane7225acf2010-05-21 18:40:58 +02001657 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
Jean Delvare421ef472005-10-26 21:28:55 +02001658 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659}
David Brownellc0564602007-05-01 23:26:31 +02001660EXPORT_SYMBOL(i2c_smbus_write_byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661
David Brownella1cdeda2008-07-14 22:38:24 +02001662/**
1663 * i2c_smbus_read_byte_data - SMBus "read byte" protocol
1664 * @client: Handle to slave device
1665 * @command: Byte interpreted by slave
1666 *
1667 * This executes the SMBus "read byte" protocol, returning negative errno
1668 * else a data byte received from the device.
1669 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670s32 i2c_smbus_read_byte_data(struct i2c_client *client, u8 command)
1671{
1672 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02001673 int status;
1674
1675 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1676 I2C_SMBUS_READ, command,
1677 I2C_SMBUS_BYTE_DATA, &data);
1678 return (status < 0) ? status : data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679}
David Brownellc0564602007-05-01 23:26:31 +02001680EXPORT_SYMBOL(i2c_smbus_read_byte_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681
David Brownella1cdeda2008-07-14 22:38:24 +02001682/**
1683 * i2c_smbus_write_byte_data - SMBus "write byte" protocol
1684 * @client: Handle to slave device
1685 * @command: Byte interpreted by slave
1686 * @value: Byte being written
1687 *
1688 * This executes the SMBus "write byte" protocol, returning negative errno
1689 * else zero on success.
1690 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691s32 i2c_smbus_write_byte_data(struct i2c_client *client, u8 command, u8 value)
1692{
1693 union i2c_smbus_data data;
1694 data.byte = value;
Farid Hammane7225acf2010-05-21 18:40:58 +02001695 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1696 I2C_SMBUS_WRITE, command,
1697 I2C_SMBUS_BYTE_DATA, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698}
David Brownellc0564602007-05-01 23:26:31 +02001699EXPORT_SYMBOL(i2c_smbus_write_byte_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700
David Brownella1cdeda2008-07-14 22:38:24 +02001701/**
1702 * i2c_smbus_read_word_data - SMBus "read word" protocol
1703 * @client: Handle to slave device
1704 * @command: Byte interpreted by slave
1705 *
1706 * This executes the SMBus "read word" protocol, returning negative errno
1707 * else a 16-bit unsigned "word" received from the device.
1708 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709s32 i2c_smbus_read_word_data(struct i2c_client *client, u8 command)
1710{
1711 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02001712 int status;
1713
1714 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1715 I2C_SMBUS_READ, command,
1716 I2C_SMBUS_WORD_DATA, &data);
1717 return (status < 0) ? status : data.word;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718}
David Brownellc0564602007-05-01 23:26:31 +02001719EXPORT_SYMBOL(i2c_smbus_read_word_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720
David Brownella1cdeda2008-07-14 22:38:24 +02001721/**
1722 * i2c_smbus_write_word_data - SMBus "write word" protocol
1723 * @client: Handle to slave device
1724 * @command: Byte interpreted by slave
1725 * @value: 16-bit "word" being written
1726 *
1727 * This executes the SMBus "write word" protocol, returning negative errno
1728 * else zero on success.
1729 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730s32 i2c_smbus_write_word_data(struct i2c_client *client, u8 command, u16 value)
1731{
1732 union i2c_smbus_data data;
1733 data.word = value;
Farid Hammane7225acf2010-05-21 18:40:58 +02001734 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1735 I2C_SMBUS_WRITE, command,
1736 I2C_SMBUS_WORD_DATA, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737}
David Brownellc0564602007-05-01 23:26:31 +02001738EXPORT_SYMBOL(i2c_smbus_write_word_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739
David Brownella64ec072007-10-13 23:56:31 +02001740/**
Prakash Mortha596c88f2008-10-14 17:30:06 +02001741 * i2c_smbus_process_call - SMBus "process call" protocol
1742 * @client: Handle to slave device
1743 * @command: Byte interpreted by slave
1744 * @value: 16-bit "word" being written
1745 *
1746 * This executes the SMBus "process call" protocol, returning negative errno
1747 * else a 16-bit unsigned "word" received from the device.
1748 */
1749s32 i2c_smbus_process_call(struct i2c_client *client, u8 command, u16 value)
1750{
1751 union i2c_smbus_data data;
1752 int status;
1753 data.word = value;
1754
1755 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1756 I2C_SMBUS_WRITE, command,
1757 I2C_SMBUS_PROC_CALL, &data);
1758 return (status < 0) ? status : data.word;
1759}
1760EXPORT_SYMBOL(i2c_smbus_process_call);
1761
1762/**
David Brownella1cdeda2008-07-14 22:38:24 +02001763 * i2c_smbus_read_block_data - SMBus "block read" protocol
David Brownella64ec072007-10-13 23:56:31 +02001764 * @client: Handle to slave device
David Brownella1cdeda2008-07-14 22:38:24 +02001765 * @command: Byte interpreted by slave
David Brownella64ec072007-10-13 23:56:31 +02001766 * @values: Byte array into which data will be read; big enough to hold
1767 * the data returned by the slave. SMBus allows at most 32 bytes.
1768 *
David Brownella1cdeda2008-07-14 22:38:24 +02001769 * This executes the SMBus "block read" protocol, returning negative errno
1770 * else the number of data bytes in the slave's response.
David Brownella64ec072007-10-13 23:56:31 +02001771 *
1772 * Note that using this function requires that the client's adapter support
1773 * the I2C_FUNC_SMBUS_READ_BLOCK_DATA functionality. Not all adapter drivers
1774 * support this; its emulation through I2C messaging relies on a specific
1775 * mechanism (I2C_M_RECV_LEN) which may not be implemented.
1776 */
Jean Delvareb86a1bc2007-05-01 23:26:34 +02001777s32 i2c_smbus_read_block_data(struct i2c_client *client, u8 command,
1778 u8 *values)
1779{
1780 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02001781 int status;
Jean Delvareb86a1bc2007-05-01 23:26:34 +02001782
David Brownell24a5bb72008-07-14 22:38:23 +02001783 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1784 I2C_SMBUS_READ, command,
1785 I2C_SMBUS_BLOCK_DATA, &data);
1786 if (status)
1787 return status;
Jean Delvareb86a1bc2007-05-01 23:26:34 +02001788
1789 memcpy(values, &data.block[1], data.block[0]);
1790 return data.block[0];
1791}
1792EXPORT_SYMBOL(i2c_smbus_read_block_data);
1793
David Brownella1cdeda2008-07-14 22:38:24 +02001794/**
1795 * i2c_smbus_write_block_data - SMBus "block write" protocol
1796 * @client: Handle to slave device
1797 * @command: Byte interpreted by slave
1798 * @length: Size of data block; SMBus allows at most 32 bytes
1799 * @values: Byte array which will be written.
1800 *
1801 * This executes the SMBus "block write" protocol, returning negative errno
1802 * else zero on success.
1803 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804s32 i2c_smbus_write_block_data(struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +02001805 u8 length, const u8 *values)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806{
1807 union i2c_smbus_data data;
Jean Delvare76560322006-01-18 23:14:55 +01001808
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 if (length > I2C_SMBUS_BLOCK_MAX)
1810 length = I2C_SMBUS_BLOCK_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 data.block[0] = length;
Jean Delvare76560322006-01-18 23:14:55 +01001812 memcpy(&data.block[1], values, length);
Farid Hammane7225acf2010-05-21 18:40:58 +02001813 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1814 I2C_SMBUS_WRITE, command,
1815 I2C_SMBUS_BLOCK_DATA, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816}
David Brownellc0564602007-05-01 23:26:31 +02001817EXPORT_SYMBOL(i2c_smbus_write_block_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818
1819/* Returns the number of read bytes */
Jean Delvare4b2643d2007-07-12 14:12:29 +02001820s32 i2c_smbus_read_i2c_block_data(struct i2c_client *client, u8 command,
1821 u8 length, u8 *values)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822{
1823 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02001824 int status;
Jean Delvare76560322006-01-18 23:14:55 +01001825
Jean Delvare4b2643d2007-07-12 14:12:29 +02001826 if (length > I2C_SMBUS_BLOCK_MAX)
1827 length = I2C_SMBUS_BLOCK_MAX;
1828 data.block[0] = length;
David Brownell24a5bb72008-07-14 22:38:23 +02001829 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1830 I2C_SMBUS_READ, command,
1831 I2C_SMBUS_I2C_BLOCK_DATA, &data);
1832 if (status < 0)
1833 return status;
Jean Delvare76560322006-01-18 23:14:55 +01001834
1835 memcpy(values, &data.block[1], data.block[0]);
1836 return data.block[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837}
David Brownellc0564602007-05-01 23:26:31 +02001838EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839
Jean Delvare21bbd692006-01-09 15:19:18 +11001840s32 i2c_smbus_write_i2c_block_data(struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +02001841 u8 length, const u8 *values)
Jean Delvare21bbd692006-01-09 15:19:18 +11001842{
1843 union i2c_smbus_data data;
1844
1845 if (length > I2C_SMBUS_BLOCK_MAX)
1846 length = I2C_SMBUS_BLOCK_MAX;
1847 data.block[0] = length;
1848 memcpy(data.block + 1, values, length);
1849 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1850 I2C_SMBUS_WRITE, command,
1851 I2C_SMBUS_I2C_BLOCK_DATA, &data);
1852}
David Brownellc0564602007-05-01 23:26:31 +02001853EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data);
Jean Delvare21bbd692006-01-09 15:19:18 +11001854
David Brownell438d6c22006-12-10 21:21:31 +01001855/* Simulate a SMBus command using the i2c protocol
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 No checking of parameters is done! */
Farid Hammane7225acf2010-05-21 18:40:58 +02001857static s32 i2c_smbus_xfer_emulated(struct i2c_adapter *adapter, u16 addr,
1858 unsigned short flags,
1859 char read_write, u8 command, int size,
1860 union i2c_smbus_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861{
1862 /* So we need to generate a series of msgs. In the case of writing, we
1863 need to use only one message; when reading, we need two. We initialize
1864 most things with sane defaults, to keep the code below somewhat
1865 simpler. */
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02001866 unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3];
1867 unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2];
Farid Hammane7225acf2010-05-21 18:40:58 +02001868 int num = read_write == I2C_SMBUS_READ ? 2 : 1;
David Brownell438d6c22006-12-10 21:21:31 +01001869 struct i2c_msg msg[2] = { { addr, flags, 1, msgbuf0 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 { addr, flags | I2C_M_RD, 0, msgbuf1 }
1871 };
1872 int i;
Jean Delvare421ef472005-10-26 21:28:55 +02001873 u8 partial_pec = 0;
David Brownell24a5bb72008-07-14 22:38:23 +02001874 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875
1876 msgbuf0[0] = command;
Farid Hammane7225acf2010-05-21 18:40:58 +02001877 switch (size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 case I2C_SMBUS_QUICK:
1879 msg[0].len = 0;
1880 /* Special case: The read/write field is used as data */
Roel Kluinf29d2e02009-02-24 19:19:48 +01001881 msg[0].flags = flags | (read_write == I2C_SMBUS_READ ?
1882 I2C_M_RD : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 num = 1;
1884 break;
1885 case I2C_SMBUS_BYTE:
1886 if (read_write == I2C_SMBUS_READ) {
1887 /* Special case: only a read! */
1888 msg[0].flags = I2C_M_RD | flags;
1889 num = 1;
1890 }
1891 break;
1892 case I2C_SMBUS_BYTE_DATA:
1893 if (read_write == I2C_SMBUS_READ)
1894 msg[1].len = 1;
1895 else {
1896 msg[0].len = 2;
1897 msgbuf0[1] = data->byte;
1898 }
1899 break;
1900 case I2C_SMBUS_WORD_DATA:
1901 if (read_write == I2C_SMBUS_READ)
1902 msg[1].len = 2;
1903 else {
Farid Hammane7225acf2010-05-21 18:40:58 +02001904 msg[0].len = 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02001906 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 }
1908 break;
1909 case I2C_SMBUS_PROC_CALL:
1910 num = 2; /* Special case */
1911 read_write = I2C_SMBUS_READ;
1912 msg[0].len = 3;
1913 msg[1].len = 2;
1914 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02001915 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 break;
1917 case I2C_SMBUS_BLOCK_DATA:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 if (read_write == I2C_SMBUS_READ) {
Jean Delvare209d27c2007-05-01 23:26:29 +02001919 msg[1].flags |= I2C_M_RECV_LEN;
1920 msg[1].len = 1; /* block length will be added by
1921 the underlying bus driver */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 } else {
1923 msg[0].len = data->block[0] + 2;
1924 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
David Brownell24a5bb72008-07-14 22:38:23 +02001925 dev_err(&adapter->dev,
1926 "Invalid block write size %d\n",
1927 data->block[0]);
1928 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 }
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02001930 for (i = 1; i < msg[0].len; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 msgbuf0[i] = data->block[i-1];
1932 }
1933 break;
1934 case I2C_SMBUS_BLOCK_PROC_CALL:
Jean Delvare209d27c2007-05-01 23:26:29 +02001935 num = 2; /* Another special case */
1936 read_write = I2C_SMBUS_READ;
1937 if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
David Brownell24a5bb72008-07-14 22:38:23 +02001938 dev_err(&adapter->dev,
1939 "Invalid block write size %d\n",
Jean Delvare209d27c2007-05-01 23:26:29 +02001940 data->block[0]);
David Brownell24a5bb72008-07-14 22:38:23 +02001941 return -EINVAL;
Jean Delvare209d27c2007-05-01 23:26:29 +02001942 }
1943 msg[0].len = data->block[0] + 2;
1944 for (i = 1; i < msg[0].len; i++)
1945 msgbuf0[i] = data->block[i-1];
1946 msg[1].flags |= I2C_M_RECV_LEN;
1947 msg[1].len = 1; /* block length will be added by
1948 the underlying bus driver */
1949 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 case I2C_SMBUS_I2C_BLOCK_DATA:
1951 if (read_write == I2C_SMBUS_READ) {
Jean Delvare4b2643d2007-07-12 14:12:29 +02001952 msg[1].len = data->block[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 } else {
1954 msg[0].len = data->block[0] + 1;
Jean Delvare30dac742005-10-08 00:15:59 +02001955 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) {
David Brownell24a5bb72008-07-14 22:38:23 +02001956 dev_err(&adapter->dev,
1957 "Invalid block write size %d\n",
1958 data->block[0]);
1959 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 }
1961 for (i = 1; i <= data->block[0]; i++)
1962 msgbuf0[i] = data->block[i];
1963 }
1964 break;
1965 default:
David Brownell24a5bb72008-07-14 22:38:23 +02001966 dev_err(&adapter->dev, "Unsupported transaction %d\n", size);
1967 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 }
1969
Jean Delvare421ef472005-10-26 21:28:55 +02001970 i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK
1971 && size != I2C_SMBUS_I2C_BLOCK_DATA);
1972 if (i) {
1973 /* Compute PEC if first message is a write */
1974 if (!(msg[0].flags & I2C_M_RD)) {
David Brownell438d6c22006-12-10 21:21:31 +01001975 if (num == 1) /* Write only */
Jean Delvare421ef472005-10-26 21:28:55 +02001976 i2c_smbus_add_pec(&msg[0]);
1977 else /* Write followed by read */
1978 partial_pec = i2c_smbus_msg_pec(0, &msg[0]);
1979 }
1980 /* Ask for PEC if last message is a read */
1981 if (msg[num-1].flags & I2C_M_RD)
David Brownell438d6c22006-12-10 21:21:31 +01001982 msg[num-1].len++;
Jean Delvare421ef472005-10-26 21:28:55 +02001983 }
1984
David Brownell24a5bb72008-07-14 22:38:23 +02001985 status = i2c_transfer(adapter, msg, num);
1986 if (status < 0)
1987 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988
Jean Delvare421ef472005-10-26 21:28:55 +02001989 /* Check PEC if last message is a read */
1990 if (i && (msg[num-1].flags & I2C_M_RD)) {
David Brownell24a5bb72008-07-14 22:38:23 +02001991 status = i2c_smbus_check_pec(partial_pec, &msg[num-1]);
1992 if (status < 0)
1993 return status;
Jean Delvare421ef472005-10-26 21:28:55 +02001994 }
1995
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 if (read_write == I2C_SMBUS_READ)
Farid Hammane7225acf2010-05-21 18:40:58 +02001997 switch (size) {
1998 case I2C_SMBUS_BYTE:
1999 data->byte = msgbuf0[0];
2000 break;
2001 case I2C_SMBUS_BYTE_DATA:
2002 data->byte = msgbuf1[0];
2003 break;
2004 case I2C_SMBUS_WORD_DATA:
2005 case I2C_SMBUS_PROC_CALL:
2006 data->word = msgbuf1[0] | (msgbuf1[1] << 8);
2007 break;
2008 case I2C_SMBUS_I2C_BLOCK_DATA:
2009 for (i = 0; i < data->block[0]; i++)
2010 data->block[i+1] = msgbuf1[i];
2011 break;
2012 case I2C_SMBUS_BLOCK_DATA:
2013 case I2C_SMBUS_BLOCK_PROC_CALL:
2014 for (i = 0; i < msgbuf1[0] + 1; i++)
2015 data->block[i] = msgbuf1[i];
2016 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 }
2018 return 0;
2019}
2020
David Brownella1cdeda2008-07-14 22:38:24 +02002021/**
2022 * i2c_smbus_xfer - execute SMBus protocol operations
2023 * @adapter: Handle to I2C bus
2024 * @addr: Address of SMBus slave on that bus
2025 * @flags: I2C_CLIENT_* flags (usually zero or I2C_CLIENT_PEC)
2026 * @read_write: I2C_SMBUS_READ or I2C_SMBUS_WRITE
2027 * @command: Byte interpreted by slave, for protocols which use such bytes
2028 * @protocol: SMBus protocol operation to execute, such as I2C_SMBUS_PROC_CALL
2029 * @data: Data to be read or written
2030 *
2031 * This executes an SMBus protocol operation, and returns a negative
2032 * errno code else zero on success.
2033 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01002034s32 i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr, unsigned short flags,
David Brownella1cdeda2008-07-14 22:38:24 +02002035 char read_write, u8 command, int protocol,
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01002036 union i2c_smbus_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037{
Clifford Wolf66b650f2009-06-15 18:01:46 +02002038 unsigned long orig_jiffies;
2039 int try;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 s32 res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041
2042 flags &= I2C_M_TEN | I2C_CLIENT_PEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043
2044 if (adapter->algo->smbus_xfer) {
Jean Delvarefe61e072010-08-11 18:20:58 +02002045 i2c_lock_adapter(adapter);
Clifford Wolf66b650f2009-06-15 18:01:46 +02002046
2047 /* Retry automatically on arbitration loss */
2048 orig_jiffies = jiffies;
2049 for (res = 0, try = 0; try <= adapter->retries; try++) {
2050 res = adapter->algo->smbus_xfer(adapter, addr, flags,
2051 read_write, command,
2052 protocol, data);
2053 if (res != -EAGAIN)
2054 break;
2055 if (time_after(jiffies,
2056 orig_jiffies + adapter->timeout))
2057 break;
2058 }
Jean Delvarefe61e072010-08-11 18:20:58 +02002059 i2c_unlock_adapter(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 } else
Farid Hammane7225acf2010-05-21 18:40:58 +02002061 res = i2c_smbus_xfer_emulated(adapter, addr, flags, read_write,
David Brownella1cdeda2008-07-14 22:38:24 +02002062 command, protocol, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 return res;
2065}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066EXPORT_SYMBOL(i2c_smbus_xfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067
2068MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
2069MODULE_DESCRIPTION("I2C-Bus main module");
2070MODULE_LICENSE("GPL");