blob: cf14ca063181b0abdb55fe8d33dd863edc8d4d48 [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 Rapoportcea443a82008-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 Delvaref8a227e2009-06-19 16:58:18 +0200433 * i2c_new_device - instantiate an i2c device
David Brownell9c1600e2007-05-01 23:26:31 +0200434 * @adap: the adapter managing the device
435 * @info: describes one I2C device; bus_num is ignored
David Brownelld64f73b2007-07-12 14:12:28 +0200436 * Context: can sleep
David Brownell9c1600e2007-05-01 23:26:31 +0200437 *
Jean Delvaref8a227e2009-06-19 16:58:18 +0200438 * Create an i2c device. Binding is handled through driver model
439 * probe()/remove() methods. A driver may be bound to this device when we
440 * return from this function, or any later moment (e.g. maybe hotplugging will
441 * load the driver module). This call is not appropriate for use by mainboard
442 * initialization logic, which usually runs during an arch_initcall() long
443 * before any i2c_adapter could exist.
David Brownell9c1600e2007-05-01 23:26:31 +0200444 *
445 * This returns the new i2c client, which may be saved for later use with
446 * i2c_unregister_device(); or NULL to indicate an error.
447 */
448struct i2c_client *
449i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
450{
451 struct i2c_client *client;
452 int status;
453
454 client = kzalloc(sizeof *client, GFP_KERNEL);
455 if (!client)
456 return NULL;
457
458 client->adapter = adap;
459
460 client->dev.platform_data = info->platform_data;
David Brownell3bbb8352007-10-13 23:56:29 +0200461
Anton Vorontsov11f1f2a2008-10-22 20:21:33 +0200462 if (info->archdata)
463 client->dev.archdata = *info->archdata;
464
Marc Pignatee354252008-08-28 08:33:22 +0200465 client->flags = info->flags;
David Brownell9c1600e2007-05-01 23:26:31 +0200466 client->addr = info->addr;
467 client->irq = info->irq;
468
David Brownell9c1600e2007-05-01 23:26:31 +0200469 strlcpy(client->name, info->type, sizeof(client->name));
470
Jean Delvare3a89db52010-06-03 11:33:52 +0200471 /* Check for address validity */
472 status = i2c_check_client_addr_validity(client);
473 if (status) {
474 dev_err(&adap->dev, "Invalid %d-bit I2C address 0x%02hx\n",
475 client->flags & I2C_CLIENT_TEN ? 10 : 7, client->addr);
476 goto out_err_silent;
477 }
478
Jean Delvaref8a227e2009-06-19 16:58:18 +0200479 /* Check for address business */
Jean Delvare3b5f7942010-06-03 11:33:55 +0200480 status = i2c_check_addr_busy(adap, client->addr);
Jean Delvaref8a227e2009-06-19 16:58:18 +0200481 if (status)
482 goto out_err;
483
484 client->dev.parent = &client->adapter->dev;
485 client->dev.bus = &i2c_bus_type;
Jean Delvare51298d12009-09-18 22:45:45 +0200486 client->dev.type = &i2c_client_type;
Grant Likelyd12d42f2010-04-13 16:12:28 -0700487#ifdef CONFIG_OF
488 client->dev.of_node = info->of_node;
489#endif
Jean Delvaref8a227e2009-06-19 16:58:18 +0200490
491 dev_set_name(&client->dev, "%d-%04x", i2c_adapter_id(adap),
492 client->addr);
493 status = device_register(&client->dev);
494 if (status)
495 goto out_err;
496
Jean Delvaref8a227e2009-06-19 16:58:18 +0200497 dev_dbg(&adap->dev, "client [%s] registered with bus id %s\n",
498 client->name, dev_name(&client->dev));
499
David Brownell9c1600e2007-05-01 23:26:31 +0200500 return client;
Jean Delvaref8a227e2009-06-19 16:58:18 +0200501
502out_err:
503 dev_err(&adap->dev, "Failed to register i2c client %s at 0x%02x "
504 "(%d)\n", client->name, client->addr, status);
Jean Delvare3a89db52010-06-03 11:33:52 +0200505out_err_silent:
Jean Delvaref8a227e2009-06-19 16:58:18 +0200506 kfree(client);
507 return NULL;
David Brownell9c1600e2007-05-01 23:26:31 +0200508}
509EXPORT_SYMBOL_GPL(i2c_new_device);
510
511
512/**
513 * i2c_unregister_device - reverse effect of i2c_new_device()
514 * @client: value returned from i2c_new_device()
David Brownelld64f73b2007-07-12 14:12:28 +0200515 * Context: can sleep
David Brownell9c1600e2007-05-01 23:26:31 +0200516 */
517void i2c_unregister_device(struct i2c_client *client)
David Brownella1d9e6e2007-05-01 23:26:30 +0200518{
David Brownella1d9e6e2007-05-01 23:26:30 +0200519 device_unregister(&client->dev);
520}
David Brownell9c1600e2007-05-01 23:26:31 +0200521EXPORT_SYMBOL_GPL(i2c_unregister_device);
David Brownella1d9e6e2007-05-01 23:26:30 +0200522
523
Jean Delvare60b129d2008-05-11 20:37:06 +0200524static const struct i2c_device_id dummy_id[] = {
525 { "dummy", 0 },
526 { },
527};
528
Jean Delvared2653e92008-04-29 23:11:39 +0200529static int dummy_probe(struct i2c_client *client,
530 const struct i2c_device_id *id)
531{
532 return 0;
533}
534
535static int dummy_remove(struct i2c_client *client)
David Brownelle9f13732008-01-27 18:14:52 +0100536{
537 return 0;
538}
539
540static struct i2c_driver dummy_driver = {
541 .driver.name = "dummy",
Jean Delvared2653e92008-04-29 23:11:39 +0200542 .probe = dummy_probe,
543 .remove = dummy_remove,
Jean Delvare60b129d2008-05-11 20:37:06 +0200544 .id_table = dummy_id,
David Brownelle9f13732008-01-27 18:14:52 +0100545};
546
547/**
548 * i2c_new_dummy - return a new i2c device bound to a dummy driver
549 * @adapter: the adapter managing the device
550 * @address: seven bit address to be used
David Brownelle9f13732008-01-27 18:14:52 +0100551 * Context: can sleep
552 *
553 * This returns an I2C client bound to the "dummy" driver, intended for use
554 * with devices that consume multiple addresses. Examples of such chips
555 * include various EEPROMS (like 24c04 and 24c08 models).
556 *
557 * These dummy devices have two main uses. First, most I2C and SMBus calls
558 * except i2c_transfer() need a client handle; the dummy will be that handle.
559 * And second, this prevents the specified address from being bound to a
560 * different driver.
561 *
562 * This returns the new i2c client, which should be saved for later use with
563 * i2c_unregister_device(); or NULL to indicate an error.
564 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100565struct i2c_client *i2c_new_dummy(struct i2c_adapter *adapter, u16 address)
David Brownelle9f13732008-01-27 18:14:52 +0100566{
567 struct i2c_board_info info = {
Jean Delvare60b129d2008-05-11 20:37:06 +0200568 I2C_BOARD_INFO("dummy", address),
David Brownelle9f13732008-01-27 18:14:52 +0100569 };
570
David Brownelle9f13732008-01-27 18:14:52 +0100571 return i2c_new_device(adapter, &info);
572}
573EXPORT_SYMBOL_GPL(i2c_new_dummy);
574
David Brownellf37dd802007-02-13 22:09:00 +0100575/* ------------------------------------------------------------------------- */
576
David Brownell16ffadf2007-05-01 23:26:28 +0200577/* I2C bus adapters -- one roots each I2C or SMBUS segment */
578
Adrian Bunk83eaaed2007-10-13 23:56:30 +0200579static void i2c_adapter_dev_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580{
David Brownellef2c83212007-05-01 23:26:28 +0200581 struct i2c_adapter *adap = to_i2c_adapter(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 complete(&adap->dev_released);
583}
584
Jean Delvare99cd8e22009-06-19 16:58:20 +0200585/*
586 * Let users instantiate I2C devices through sysfs. This can be used when
587 * platform initialization code doesn't contain the proper data for
588 * whatever reason. Also useful for drivers that do device detection and
589 * detection fails, either because the device uses an unexpected address,
590 * or this is a compatible device with different ID register values.
591 *
592 * Parameter checking may look overzealous, but we really don't want
593 * the user to provide incorrect parameters.
594 */
595static ssize_t
596i2c_sysfs_new_device(struct device *dev, struct device_attribute *attr,
597 const char *buf, size_t count)
598{
599 struct i2c_adapter *adap = to_i2c_adapter(dev);
600 struct i2c_board_info info;
601 struct i2c_client *client;
602 char *blank, end;
603 int res;
604
605 dev_warn(dev, "The new_device interface is still experimental "
606 "and may change in a near future\n");
607 memset(&info, 0, sizeof(struct i2c_board_info));
608
609 blank = strchr(buf, ' ');
610 if (!blank) {
611 dev_err(dev, "%s: Missing parameters\n", "new_device");
612 return -EINVAL;
613 }
614 if (blank - buf > I2C_NAME_SIZE - 1) {
615 dev_err(dev, "%s: Invalid device name\n", "new_device");
616 return -EINVAL;
617 }
618 memcpy(info.type, buf, blank - buf);
619
620 /* Parse remaining parameters, reject extra parameters */
621 res = sscanf(++blank, "%hi%c", &info.addr, &end);
622 if (res < 1) {
623 dev_err(dev, "%s: Can't parse I2C address\n", "new_device");
624 return -EINVAL;
625 }
626 if (res > 1 && end != '\n') {
627 dev_err(dev, "%s: Extra parameters\n", "new_device");
628 return -EINVAL;
629 }
630
Jean Delvare99cd8e22009-06-19 16:58:20 +0200631 client = i2c_new_device(adap, &info);
632 if (!client)
Jean Delvare3a89db52010-06-03 11:33:52 +0200633 return -EINVAL;
Jean Delvare99cd8e22009-06-19 16:58:20 +0200634
635 /* Keep track of the added device */
Jean Delvare6629dcf2010-05-04 11:09:28 +0200636 i2c_lock_adapter(adap);
637 list_add_tail(&client->detected, &adap->userspace_clients);
638 i2c_unlock_adapter(adap);
Jean Delvare99cd8e22009-06-19 16:58:20 +0200639 dev_info(dev, "%s: Instantiated device %s at 0x%02hx\n", "new_device",
640 info.type, info.addr);
641
642 return count;
643}
644
645/*
646 * And of course let the users delete the devices they instantiated, if
647 * they got it wrong. This interface can only be used to delete devices
648 * instantiated by i2c_sysfs_new_device above. This guarantees that we
649 * don't delete devices to which some kernel code still has references.
650 *
651 * Parameter checking may look overzealous, but we really don't want
652 * the user to delete the wrong device.
653 */
654static ssize_t
655i2c_sysfs_delete_device(struct device *dev, struct device_attribute *attr,
656 const char *buf, size_t count)
657{
658 struct i2c_adapter *adap = to_i2c_adapter(dev);
659 struct i2c_client *client, *next;
660 unsigned short addr;
661 char end;
662 int res;
663
664 /* Parse parameters, reject extra parameters */
665 res = sscanf(buf, "%hi%c", &addr, &end);
666 if (res < 1) {
667 dev_err(dev, "%s: Can't parse I2C address\n", "delete_device");
668 return -EINVAL;
669 }
670 if (res > 1 && end != '\n') {
671 dev_err(dev, "%s: Extra parameters\n", "delete_device");
672 return -EINVAL;
673 }
674
675 /* Make sure the device was added through sysfs */
676 res = -ENOENT;
Jean Delvare6629dcf2010-05-04 11:09:28 +0200677 i2c_lock_adapter(adap);
678 list_for_each_entry_safe(client, next, &adap->userspace_clients,
679 detected) {
680 if (client->addr == addr) {
Jean Delvare99cd8e22009-06-19 16:58:20 +0200681 dev_info(dev, "%s: Deleting device %s at 0x%02hx\n",
682 "delete_device", client->name, client->addr);
683
684 list_del(&client->detected);
685 i2c_unregister_device(client);
686 res = count;
687 break;
688 }
689 }
Jean Delvare6629dcf2010-05-04 11:09:28 +0200690 i2c_unlock_adapter(adap);
Jean Delvare99cd8e22009-06-19 16:58:20 +0200691
692 if (res < 0)
693 dev_err(dev, "%s: Can't find device in list\n",
694 "delete_device");
695 return res;
696}
697
Jean Delvare4f8cf822009-09-18 22:45:46 +0200698static DEVICE_ATTR(new_device, S_IWUSR, NULL, i2c_sysfs_new_device);
699static DEVICE_ATTR(delete_device, S_IWUSR, NULL, i2c_sysfs_delete_device);
700
701static struct attribute *i2c_adapter_attrs[] = {
702 &dev_attr_name.attr,
703 &dev_attr_new_device.attr,
704 &dev_attr_delete_device.attr,
705 NULL
David Brownell16ffadf2007-05-01 23:26:28 +0200706};
707
Jean Delvare4f8cf822009-09-18 22:45:46 +0200708static struct attribute_group i2c_adapter_attr_group = {
709 .attrs = i2c_adapter_attrs,
710};
711
712static const struct attribute_group *i2c_adapter_attr_groups[] = {
713 &i2c_adapter_attr_group,
714 NULL
715};
716
717static struct device_type i2c_adapter_type = {
718 .groups = i2c_adapter_attr_groups,
719 .release = i2c_adapter_dev_release,
David Brownell16ffadf2007-05-01 23:26:28 +0200720};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721
Jean Delvare2bb50952009-09-18 22:45:46 +0200722#ifdef CONFIG_I2C_COMPAT
723static struct class_compat *i2c_adapter_compat_class;
724#endif
725
David Brownell9c1600e2007-05-01 23:26:31 +0200726static void i2c_scan_static_board_info(struct i2c_adapter *adapter)
727{
728 struct i2c_devinfo *devinfo;
729
Rodolfo Giomettif18c41d2009-06-19 16:58:20 +0200730 down_read(&__i2c_board_lock);
David Brownell9c1600e2007-05-01 23:26:31 +0200731 list_for_each_entry(devinfo, &__i2c_board_list, list) {
732 if (devinfo->busnum == adapter->nr
733 && !i2c_new_device(adapter,
734 &devinfo->board_info))
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100735 dev_err(&adapter->dev,
736 "Can't create device at 0x%02x\n",
David Brownell9c1600e2007-05-01 23:26:31 +0200737 devinfo->board_info.addr);
738 }
Rodolfo Giomettif18c41d2009-06-19 16:58:20 +0200739 up_read(&__i2c_board_lock);
David Brownell9c1600e2007-05-01 23:26:31 +0200740}
741
Jean Delvare69b00892009-12-06 17:06:27 +0100742static int i2c_do_add_adapter(struct i2c_driver *driver,
743 struct i2c_adapter *adap)
Jean Delvare026526f2008-01-27 18:14:49 +0100744{
Jean Delvare4735c982008-07-14 22:38:36 +0200745 /* Detect supported devices on that bus, and instantiate them */
746 i2c_detect(adap, driver);
747
748 /* Let legacy drivers scan this bus for matching devices */
Jean Delvare026526f2008-01-27 18:14:49 +0100749 if (driver->attach_adapter) {
750 /* We ignore the return code; if it fails, too bad */
751 driver->attach_adapter(adap);
752 }
753 return 0;
754}
755
Jean Delvare69b00892009-12-06 17:06:27 +0100756static int __process_new_adapter(struct device_driver *d, void *data)
757{
758 return i2c_do_add_adapter(to_i2c_driver(d), data);
759}
760
David Brownell6e13e642007-05-01 23:26:31 +0200761static int i2c_register_adapter(struct i2c_adapter *adap)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762{
Jean Delvare026526f2008-01-27 18:14:49 +0100763 int res = 0, dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764
David Brownell1d0b19c2008-10-14 17:30:05 +0200765 /* Can't register until after driver model init */
Jean Delvare35fc37f2009-06-19 16:58:19 +0200766 if (unlikely(WARN_ON(!i2c_bus_type.p))) {
767 res = -EAGAIN;
768 goto out_list;
769 }
David Brownell1d0b19c2008-10-14 17:30:05 +0200770
Mika Kuoppala194684e2009-12-06 17:06:22 +0100771 rt_mutex_init(&adap->bus_lock);
Jean Delvare6629dcf2010-05-04 11:09:28 +0200772 INIT_LIST_HEAD(&adap->userspace_clients);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
Jean Delvare8fcfef62009-03-28 21:34:43 +0100774 /* Set default timeout to 1 second if not already set */
775 if (adap->timeout == 0)
776 adap->timeout = HZ;
777
Kay Sievers27d9c182009-01-07 14:29:16 +0100778 dev_set_name(&adap->dev, "i2c-%d", adap->nr);
Jean Delvare4f8cf822009-09-18 22:45:46 +0200779 adap->dev.bus = &i2c_bus_type;
780 adap->dev.type = &i2c_adapter_type;
Jean Delvareb119c6c2006-08-15 18:26:30 +0200781 res = device_register(&adap->dev);
782 if (res)
783 goto out_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200785 dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
786
Jean Delvare2bb50952009-09-18 22:45:46 +0200787#ifdef CONFIG_I2C_COMPAT
788 res = class_compat_create_link(i2c_adapter_compat_class, &adap->dev,
789 adap->dev.parent);
790 if (res)
791 dev_warn(&adap->dev,
792 "Failed to create compatibility class link\n");
793#endif
794
Jean Delvare729d6dd2009-06-19 16:58:18 +0200795 /* create pre-declared device nodes */
David Brownell6e13e642007-05-01 23:26:31 +0200796 if (adap->nr < __i2c_first_dynamic_bus_num)
797 i2c_scan_static_board_info(adap);
798
Grant Likely959e85f2010-06-08 07:48:19 -0600799 /* Register devices from the device tree */
800 of_i2c_register_devices(adap);
801
Jean Delvare4735c982008-07-14 22:38:36 +0200802 /* Notify drivers */
Jean Delvare35fc37f2009-06-19 16:58:19 +0200803 mutex_lock(&core_lock);
Jean Delvare026526f2008-01-27 18:14:49 +0100804 dummy = bus_for_each_drv(&i2c_bus_type, NULL, adap,
Jean Delvare69b00892009-12-06 17:06:27 +0100805 __process_new_adapter);
Jean Delvarecaada322008-01-27 18:14:49 +0100806 mutex_unlock(&core_lock);
Jean Delvare35fc37f2009-06-19 16:58:19 +0200807
808 return 0;
Jean Delvareb119c6c2006-08-15 18:26:30 +0200809
Jean Delvareb119c6c2006-08-15 18:26:30 +0200810out_list:
Jean Delvare35fc37f2009-06-19 16:58:19 +0200811 mutex_lock(&core_lock);
Jean Delvareb119c6c2006-08-15 18:26:30 +0200812 idr_remove(&i2c_adapter_idr, adap->nr);
Jean Delvare35fc37f2009-06-19 16:58:19 +0200813 mutex_unlock(&core_lock);
814 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815}
816
David Brownell6e13e642007-05-01 23:26:31 +0200817/**
818 * i2c_add_adapter - declare i2c adapter, use dynamic bus number
819 * @adapter: the adapter to add
David Brownelld64f73b2007-07-12 14:12:28 +0200820 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +0200821 *
822 * This routine is used to declare an I2C adapter when its bus number
823 * doesn't matter. Examples: for I2C adapters dynamically added by
824 * USB links or PCI plugin cards.
825 *
826 * When this returns zero, a new bus number was allocated and stored
827 * in adap->nr, and the specified adapter became available for clients.
828 * Otherwise, a negative errno value is returned.
829 */
830int i2c_add_adapter(struct i2c_adapter *adapter)
831{
832 int id, res = 0;
833
834retry:
835 if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0)
836 return -ENOMEM;
837
Jean Delvarecaada322008-01-27 18:14:49 +0100838 mutex_lock(&core_lock);
David Brownell6e13e642007-05-01 23:26:31 +0200839 /* "above" here means "above or equal to", sigh */
840 res = idr_get_new_above(&i2c_adapter_idr, adapter,
841 __i2c_first_dynamic_bus_num, &id);
Jean Delvarecaada322008-01-27 18:14:49 +0100842 mutex_unlock(&core_lock);
David Brownell6e13e642007-05-01 23:26:31 +0200843
844 if (res < 0) {
845 if (res == -EAGAIN)
846 goto retry;
847 return res;
848 }
849
850 adapter->nr = id;
851 return i2c_register_adapter(adapter);
852}
853EXPORT_SYMBOL(i2c_add_adapter);
854
855/**
856 * i2c_add_numbered_adapter - declare i2c adapter, use static bus number
857 * @adap: the adapter to register (with adap->nr initialized)
David Brownelld64f73b2007-07-12 14:12:28 +0200858 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +0200859 *
860 * This routine is used to declare an I2C adapter when its bus number
Randy Dunlap8c07e462008-03-23 20:28:20 +0100861 * matters. For example, use it for I2C adapters from system-on-chip CPUs,
862 * or otherwise built in to the system's mainboard, and where i2c_board_info
David Brownell6e13e642007-05-01 23:26:31 +0200863 * is used to properly configure I2C devices.
864 *
865 * If no devices have pre-been declared for this bus, then be sure to
866 * register the adapter before any dynamically allocated ones. Otherwise
867 * the required bus ID may not be available.
868 *
869 * When this returns zero, the specified adapter became available for
870 * clients using the bus number provided in adap->nr. Also, the table
871 * of I2C devices pre-declared using i2c_register_board_info() is scanned,
872 * and the appropriate driver model device nodes are created. Otherwise, a
873 * negative errno value is returned.
874 */
875int i2c_add_numbered_adapter(struct i2c_adapter *adap)
876{
877 int id;
878 int status;
879
880 if (adap->nr & ~MAX_ID_MASK)
881 return -EINVAL;
882
883retry:
884 if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0)
885 return -ENOMEM;
886
Jean Delvarecaada322008-01-27 18:14:49 +0100887 mutex_lock(&core_lock);
David Brownell6e13e642007-05-01 23:26:31 +0200888 /* "above" here means "above or equal to", sigh;
889 * we need the "equal to" result to force the result
890 */
891 status = idr_get_new_above(&i2c_adapter_idr, adap, adap->nr, &id);
892 if (status == 0 && id != adap->nr) {
893 status = -EBUSY;
894 idr_remove(&i2c_adapter_idr, id);
895 }
Jean Delvarecaada322008-01-27 18:14:49 +0100896 mutex_unlock(&core_lock);
David Brownell6e13e642007-05-01 23:26:31 +0200897 if (status == -EAGAIN)
898 goto retry;
899
900 if (status == 0)
901 status = i2c_register_adapter(adap);
902 return status;
903}
904EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter);
905
Jean Delvare69b00892009-12-06 17:06:27 +0100906static int i2c_do_del_adapter(struct i2c_driver *driver,
907 struct i2c_adapter *adapter)
Jean Delvare026526f2008-01-27 18:14:49 +0100908{
Jean Delvare4735c982008-07-14 22:38:36 +0200909 struct i2c_client *client, *_n;
Jean Delvare026526f2008-01-27 18:14:49 +0100910 int res;
911
Jean Delvareacec2112009-03-28 21:34:40 +0100912 /* Remove the devices we created ourselves as the result of hardware
913 * probing (using a driver's detect method) */
Jean Delvare4735c982008-07-14 22:38:36 +0200914 list_for_each_entry_safe(client, _n, &driver->clients, detected) {
915 if (client->adapter == adapter) {
916 dev_dbg(&adapter->dev, "Removing %s at 0x%x\n",
917 client->name, client->addr);
918 list_del(&client->detected);
919 i2c_unregister_device(client);
920 }
921 }
922
Jean Delvare026526f2008-01-27 18:14:49 +0100923 if (!driver->detach_adapter)
924 return 0;
925 res = driver->detach_adapter(adapter);
926 if (res)
927 dev_err(&adapter->dev, "detach_adapter failed (%d) "
928 "for driver [%s]\n", res, driver->driver.name);
929 return res;
930}
931
Jean Delvaree549c2b2009-06-19 16:58:19 +0200932static int __unregister_client(struct device *dev, void *dummy)
933{
934 struct i2c_client *client = i2c_verify_client(dev);
935 if (client)
936 i2c_unregister_device(client);
937 return 0;
938}
939
Jean Delvare69b00892009-12-06 17:06:27 +0100940static int __process_removed_adapter(struct device_driver *d, void *data)
941{
942 return i2c_do_del_adapter(to_i2c_driver(d), data);
943}
944
David Brownelld64f73b2007-07-12 14:12:28 +0200945/**
946 * i2c_del_adapter - unregister I2C adapter
947 * @adap: the adapter being unregistered
948 * Context: can sleep
949 *
950 * This unregisters an I2C adapter which was previously registered
951 * by @i2c_add_adapter or @i2c_add_numbered_adapter.
952 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953int i2c_del_adapter(struct i2c_adapter *adap)
954{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 int res = 0;
Jean Delvare35fc37f2009-06-19 16:58:19 +0200956 struct i2c_adapter *found;
Jean Delvarebbd2d9c2009-11-26 09:22:33 +0100957 struct i2c_client *client, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958
959 /* First make sure that this adapter was ever added */
Jean Delvare35fc37f2009-06-19 16:58:19 +0200960 mutex_lock(&core_lock);
961 found = idr_find(&i2c_adapter_idr, adap->nr);
962 mutex_unlock(&core_lock);
963 if (found != adap) {
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200964 pr_debug("i2c-core: attempting to delete unregistered "
965 "adapter [%s]\n", adap->name);
Jean Delvare35fc37f2009-06-19 16:58:19 +0200966 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 }
968
Jean Delvare026526f2008-01-27 18:14:49 +0100969 /* Tell drivers about this removal */
Jean Delvare35fc37f2009-06-19 16:58:19 +0200970 mutex_lock(&core_lock);
Jean Delvare026526f2008-01-27 18:14:49 +0100971 res = bus_for_each_drv(&i2c_bus_type, NULL, adap,
Jean Delvare69b00892009-12-06 17:06:27 +0100972 __process_removed_adapter);
Jean Delvare35fc37f2009-06-19 16:58:19 +0200973 mutex_unlock(&core_lock);
Jean Delvare026526f2008-01-27 18:14:49 +0100974 if (res)
Jean Delvare35fc37f2009-06-19 16:58:19 +0200975 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976
Jean Delvarebbd2d9c2009-11-26 09:22:33 +0100977 /* Remove devices instantiated from sysfs */
Jean Delvare6629dcf2010-05-04 11:09:28 +0200978 i2c_lock_adapter(adap);
979 list_for_each_entry_safe(client, next, &adap->userspace_clients,
980 detected) {
981 dev_dbg(&adap->dev, "Removing %s at 0x%x\n", client->name,
982 client->addr);
983 list_del(&client->detected);
984 i2c_unregister_device(client);
Jean Delvarebbd2d9c2009-11-26 09:22:33 +0100985 }
Jean Delvare6629dcf2010-05-04 11:09:28 +0200986 i2c_unlock_adapter(adap);
Jean Delvarebbd2d9c2009-11-26 09:22:33 +0100987
Jean Delvaree549c2b2009-06-19 16:58:19 +0200988 /* Detach any active clients. This can't fail, thus we do not
989 checking the returned value. */
990 res = device_for_each_child(&adap->dev, NULL, __unregister_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991
Jean Delvare2bb50952009-09-18 22:45:46 +0200992#ifdef CONFIG_I2C_COMPAT
993 class_compat_remove_link(i2c_adapter_compat_class, &adap->dev,
994 adap->dev.parent);
995#endif
996
Thadeu Lima de Souza Cascardoc5567522010-01-16 20:43:13 +0100997 /* device name is gone after device_unregister */
998 dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
999
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 /* clean up the sysfs representation */
1001 init_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 device_unregister(&adap->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003
1004 /* wait for sysfs to drop all references */
1005 wait_for_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006
David Brownell6e13e642007-05-01 23:26:31 +02001007 /* free bus id */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001008 mutex_lock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 idr_remove(&i2c_adapter_idr, adap->nr);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001010 mutex_unlock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011
Jean Delvarebd4bc3db2008-07-16 19:30:05 +02001012 /* Clear the device structure in case this adapter is ever going to be
1013 added again */
1014 memset(&adap->dev, 0, sizeof(adap->dev));
1015
Jean Delvare35fc37f2009-06-19 16:58:19 +02001016 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017}
David Brownellc0564602007-05-01 23:26:31 +02001018EXPORT_SYMBOL(i2c_del_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019
1020
David Brownell7b4fbc52007-05-01 23:26:30 +02001021/* ------------------------------------------------------------------------- */
1022
Jean Delvare69b00892009-12-06 17:06:27 +01001023static int __process_new_driver(struct device *dev, void *data)
Dave Young7f101a92008-07-14 22:38:19 +02001024{
Jean Delvare4f8cf822009-09-18 22:45:46 +02001025 if (dev->type != &i2c_adapter_type)
1026 return 0;
Jean Delvare69b00892009-12-06 17:06:27 +01001027 return i2c_do_add_adapter(data, to_i2c_adapter(dev));
Dave Young7f101a92008-07-14 22:38:19 +02001028}
1029
David Brownell7b4fbc52007-05-01 23:26:30 +02001030/*
1031 * An i2c_driver is used with one or more i2c_client (device) nodes to access
Jean Delvare729d6dd2009-06-19 16:58:18 +02001032 * i2c slave chips, on a bus instance associated with some i2c_adapter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 */
1034
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08001035int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036{
Jean Delvare7eebcb72006-02-05 23:28:21 +01001037 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038
David Brownell1d0b19c2008-10-14 17:30:05 +02001039 /* Can't register until after driver model init */
1040 if (unlikely(WARN_ON(!i2c_bus_type.p)))
1041 return -EAGAIN;
1042
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 /* add the driver to the list of i2c drivers in the driver core */
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08001044 driver->driver.owner = owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 driver->driver.bus = &i2c_bus_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046
Jean Delvare729d6dd2009-06-19 16:58:18 +02001047 /* When registration returns, the driver core
David Brownell6e13e642007-05-01 23:26:31 +02001048 * will have called probe() for all matching-but-unbound devices.
1049 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 res = driver_register(&driver->driver);
1051 if (res)
Jean Delvare7eebcb72006-02-05 23:28:21 +01001052 return res;
David Brownell438d6c22006-12-10 21:21:31 +01001053
Laurent Riffard35d8b2e2005-11-26 20:34:05 +01001054 pr_debug("i2c-core: driver [%s] registered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055
Jean Delvare4735c982008-07-14 22:38:36 +02001056 INIT_LIST_HEAD(&driver->clients);
1057 /* Walk the adapters that are already present */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001058 mutex_lock(&core_lock);
Jean Delvare69b00892009-12-06 17:06:27 +01001059 bus_for_each_dev(&i2c_bus_type, NULL, driver, __process_new_driver);
Jean Delvarecaada322008-01-27 18:14:49 +01001060 mutex_unlock(&core_lock);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001061
Jean Delvare7eebcb72006-02-05 23:28:21 +01001062 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063}
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08001064EXPORT_SYMBOL(i2c_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065
Jean Delvare69b00892009-12-06 17:06:27 +01001066static int __process_removed_driver(struct device *dev, void *data)
Dave Young7f101a92008-07-14 22:38:19 +02001067{
Jean Delvare4f8cf822009-09-18 22:45:46 +02001068 if (dev->type != &i2c_adapter_type)
1069 return 0;
Jean Delvare69b00892009-12-06 17:06:27 +01001070 return i2c_do_del_adapter(data, to_i2c_adapter(dev));
Dave Young7f101a92008-07-14 22:38:19 +02001071}
1072
David Brownella1d9e6e2007-05-01 23:26:30 +02001073/**
1074 * i2c_del_driver - unregister I2C driver
1075 * @driver: the driver being unregistered
David Brownelld64f73b2007-07-12 14:12:28 +02001076 * Context: can sleep
David Brownella1d9e6e2007-05-01 23:26:30 +02001077 */
Jean Delvareb3e82092007-05-01 23:26:32 +02001078void i2c_del_driver(struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079{
Jean Delvarecaada322008-01-27 18:14:49 +01001080 mutex_lock(&core_lock);
Jean Delvare69b00892009-12-06 17:06:27 +01001081 bus_for_each_dev(&i2c_bus_type, NULL, driver, __process_removed_driver);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001082 mutex_unlock(&core_lock);
David Brownella1d9e6e2007-05-01 23:26:30 +02001083
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 driver_unregister(&driver->driver);
Laurent Riffard35d8b2e2005-11-26 20:34:05 +01001085 pr_debug("i2c-core: driver [%s] unregistered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086}
David Brownellc0564602007-05-01 23:26:31 +02001087EXPORT_SYMBOL(i2c_del_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088
David Brownell7b4fbc52007-05-01 23:26:30 +02001089/* ------------------------------------------------------------------------- */
1090
Jean Delvaree48d3312008-01-27 18:14:48 +01001091/**
1092 * i2c_use_client - increments the reference count of the i2c client structure
1093 * @client: the client being referenced
1094 *
1095 * Each live reference to a client should be refcounted. The driver model does
1096 * that automatically as part of driver binding, so that most drivers don't
1097 * need to do this explicitly: they hold a reference until they're unbound
1098 * from the device.
1099 *
1100 * A pointer to the client with the incremented reference counter is returned.
1101 */
1102struct i2c_client *i2c_use_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103{
David Brownell6ea438e2008-07-14 22:38:24 +02001104 if (client && get_device(&client->dev))
1105 return client;
1106 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107}
David Brownellc0564602007-05-01 23:26:31 +02001108EXPORT_SYMBOL(i2c_use_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109
Jean Delvaree48d3312008-01-27 18:14:48 +01001110/**
1111 * i2c_release_client - release a use of the i2c client structure
1112 * @client: the client being no longer referenced
1113 *
1114 * Must be called when a user of a client is finished with it.
1115 */
1116void i2c_release_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117{
David Brownell6ea438e2008-07-14 22:38:24 +02001118 if (client)
1119 put_device(&client->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120}
David Brownellc0564602007-05-01 23:26:31 +02001121EXPORT_SYMBOL(i2c_release_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122
David Brownell9b766b82008-01-27 18:14:51 +01001123struct i2c_cmd_arg {
1124 unsigned cmd;
1125 void *arg;
1126};
1127
1128static int i2c_cmd(struct device *dev, void *_arg)
1129{
1130 struct i2c_client *client = i2c_verify_client(dev);
1131 struct i2c_cmd_arg *arg = _arg;
1132
1133 if (client && client->driver && client->driver->command)
1134 client->driver->command(client, arg->cmd, arg->arg);
1135 return 0;
1136}
1137
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
1139{
David Brownell9b766b82008-01-27 18:14:51 +01001140 struct i2c_cmd_arg cmd_arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141
David Brownell9b766b82008-01-27 18:14:51 +01001142 cmd_arg.cmd = cmd;
1143 cmd_arg.arg = arg;
1144 device_for_each_child(&adap->dev, &cmd_arg, i2c_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145}
David Brownellc0564602007-05-01 23:26:31 +02001146EXPORT_SYMBOL(i2c_clients_command);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147
1148static int __init i2c_init(void)
1149{
1150 int retval;
1151
1152 retval = bus_register(&i2c_bus_type);
1153 if (retval)
1154 return retval;
Jean Delvare2bb50952009-09-18 22:45:46 +02001155#ifdef CONFIG_I2C_COMPAT
1156 i2c_adapter_compat_class = class_compat_register("i2c-adapter");
1157 if (!i2c_adapter_compat_class) {
1158 retval = -ENOMEM;
1159 goto bus_err;
1160 }
1161#endif
David Brownelle9f13732008-01-27 18:14:52 +01001162 retval = i2c_add_driver(&dummy_driver);
1163 if (retval)
Jean Delvare2bb50952009-09-18 22:45:46 +02001164 goto class_err;
David Brownelle9f13732008-01-27 18:14:52 +01001165 return 0;
1166
Jean Delvare2bb50952009-09-18 22:45:46 +02001167class_err:
1168#ifdef CONFIG_I2C_COMPAT
1169 class_compat_unregister(i2c_adapter_compat_class);
David Brownelle9f13732008-01-27 18:14:52 +01001170bus_err:
Jean Delvare2bb50952009-09-18 22:45:46 +02001171#endif
David Brownelle9f13732008-01-27 18:14:52 +01001172 bus_unregister(&i2c_bus_type);
1173 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174}
1175
1176static void __exit i2c_exit(void)
1177{
David Brownelle9f13732008-01-27 18:14:52 +01001178 i2c_del_driver(&dummy_driver);
Jean Delvare2bb50952009-09-18 22:45:46 +02001179#ifdef CONFIG_I2C_COMPAT
1180 class_compat_unregister(i2c_adapter_compat_class);
1181#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 bus_unregister(&i2c_bus_type);
1183}
1184
David Brownella10f9e72008-10-14 17:30:06 +02001185/* We must initialize early, because some subsystems register i2c drivers
1186 * in subsys_initcall() code, but are linked (and initialized) before i2c.
1187 */
1188postcore_initcall(i2c_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189module_exit(i2c_exit);
1190
1191/* ----------------------------------------------------
1192 * the functional interface to the i2c busses.
1193 * ----------------------------------------------------
1194 */
1195
David Brownella1cdeda2008-07-14 22:38:24 +02001196/**
1197 * i2c_transfer - execute a single or combined I2C message
1198 * @adap: Handle to I2C bus
1199 * @msgs: One or more messages to execute before STOP is issued to
1200 * terminate the operation; each message begins with a START.
1201 * @num: Number of messages to be executed.
1202 *
1203 * Returns negative errno, else the number of messages executed.
1204 *
1205 * Note that there is no requirement that each message be sent to
1206 * the same slave address, although that is the most common model.
1207 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001208int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209{
Clifford Wolf66b650f2009-06-15 18:01:46 +02001210 unsigned long orig_jiffies;
1211 int ret, try;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212
David Brownella1cdeda2008-07-14 22:38:24 +02001213 /* REVISIT the fault reporting model here is weak:
1214 *
1215 * - When we get an error after receiving N bytes from a slave,
1216 * there is no way to report "N".
1217 *
1218 * - When we get a NAK after transmitting N bytes to a slave,
1219 * there is no way to report "N" ... or to let the master
1220 * continue executing the rest of this combined message, if
1221 * that's the appropriate response.
1222 *
1223 * - When for example "num" is two and we successfully complete
1224 * the first message but get an error part way through the
1225 * second, it's unclear whether that should be reported as
1226 * one (discarding status on the second message) or errno
1227 * (discarding status on the first one).
1228 */
1229
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 if (adap->algo->master_xfer) {
1231#ifdef DEBUG
1232 for (ret = 0; ret < num; ret++) {
1233 dev_dbg(&adap->dev, "master_xfer[%d] %c, addr=0x%02x, "
Jean Delvare209d27c2007-05-01 23:26:29 +02001234 "len=%d%s\n", ret, (msgs[ret].flags & I2C_M_RD)
1235 ? 'R' : 'W', msgs[ret].addr, msgs[ret].len,
1236 (msgs[ret].flags & I2C_M_RECV_LEN) ? "+" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 }
1238#endif
1239
Mike Rapoportcea443a82008-01-27 18:14:50 +01001240 if (in_atomic() || irqs_disabled()) {
Mika Kuoppala194684e2009-12-06 17:06:22 +01001241 ret = rt_mutex_trylock(&adap->bus_lock);
Mike Rapoportcea443a82008-01-27 18:14:50 +01001242 if (!ret)
1243 /* I2C activity is ongoing. */
1244 return -EAGAIN;
1245 } else {
Mika Kuoppala194684e2009-12-06 17:06:22 +01001246 rt_mutex_lock(&adap->bus_lock);
Mike Rapoportcea443a82008-01-27 18:14:50 +01001247 }
1248
Clifford Wolf66b650f2009-06-15 18:01:46 +02001249 /* Retry automatically on arbitration loss */
1250 orig_jiffies = jiffies;
1251 for (ret = 0, try = 0; try <= adap->retries; try++) {
1252 ret = adap->algo->master_xfer(adap, msgs, num);
1253 if (ret != -EAGAIN)
1254 break;
1255 if (time_after(jiffies, orig_jiffies + adap->timeout))
1256 break;
1257 }
Mika Kuoppala194684e2009-12-06 17:06:22 +01001258 rt_mutex_unlock(&adap->bus_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259
1260 return ret;
1261 } else {
1262 dev_dbg(&adap->dev, "I2C level transfers not supported\n");
David Brownell24a5bb72008-07-14 22:38:23 +02001263 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 }
1265}
David Brownellc0564602007-05-01 23:26:31 +02001266EXPORT_SYMBOL(i2c_transfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267
David Brownella1cdeda2008-07-14 22:38:24 +02001268/**
1269 * i2c_master_send - issue a single I2C message in master transmit mode
1270 * @client: Handle to slave device
1271 * @buf: Data that will be written to the slave
Zhangfei Gao0c43ea52010-03-02 12:23:49 +01001272 * @count: How many bytes to write, must be less than 64k since msg.len is u16
David Brownella1cdeda2008-07-14 22:38:24 +02001273 *
1274 * Returns negative errno, or else the number of bytes written.
1275 */
Farid Hammane7225acf2010-05-21 18:40:58 +02001276int i2c_master_send(struct i2c_client *client, const char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277{
1278 int ret;
Farid Hammane7225acf2010-05-21 18:40:58 +02001279 struct i2c_adapter *adap = client->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 struct i2c_msg msg;
1281
Jean Delvare815f55f2005-05-07 22:58:46 +02001282 msg.addr = client->addr;
1283 msg.flags = client->flags & I2C_M_TEN;
1284 msg.len = count;
1285 msg.buf = (char *)buf;
David Brownell438d6c22006-12-10 21:21:31 +01001286
Jean Delvare815f55f2005-05-07 22:58:46 +02001287 ret = i2c_transfer(adap, &msg, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288
Jean Delvare815f55f2005-05-07 22:58:46 +02001289 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
1290 transmitted, else error code. */
1291 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292}
David Brownellc0564602007-05-01 23:26:31 +02001293EXPORT_SYMBOL(i2c_master_send);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294
David Brownella1cdeda2008-07-14 22:38:24 +02001295/**
1296 * i2c_master_recv - issue a single I2C message in master receive mode
1297 * @client: Handle to slave device
1298 * @buf: Where to store data read from slave
Zhangfei Gao0c43ea52010-03-02 12:23:49 +01001299 * @count: How many bytes to read, must be less than 64k since msg.len is u16
David Brownella1cdeda2008-07-14 22:38:24 +02001300 *
1301 * Returns negative errno, or else the number of bytes read.
1302 */
Farid Hammane7225acf2010-05-21 18:40:58 +02001303int i2c_master_recv(struct i2c_client *client, char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304{
Farid Hammane7225acf2010-05-21 18:40:58 +02001305 struct i2c_adapter *adap = client->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 struct i2c_msg msg;
1307 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308
Jean Delvare815f55f2005-05-07 22:58:46 +02001309 msg.addr = client->addr;
1310 msg.flags = client->flags & I2C_M_TEN;
1311 msg.flags |= I2C_M_RD;
1312 msg.len = count;
1313 msg.buf = buf;
1314
1315 ret = i2c_transfer(adap, &msg, 1);
1316
1317 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
1318 transmitted, else error code. */
1319 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320}
David Brownellc0564602007-05-01 23:26:31 +02001321EXPORT_SYMBOL(i2c_master_recv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323/* ----------------------------------------------------
1324 * the i2c address scanning function
1325 * Will not work for 10-bit addresses!
1326 * ----------------------------------------------------
1327 */
Jean Delvare9fc6adf2005-07-31 21:20:43 +02001328
Jean Delvare63e4e802010-06-03 11:33:51 +02001329/*
1330 * Legacy default probe function, mostly relevant for SMBus. The default
1331 * probe method is a quick write, but it is known to corrupt the 24RF08
1332 * EEPROMs due to a state machine bug, and could also irreversibly
1333 * write-protect some EEPROMs, so for address ranges 0x30-0x37 and 0x50-0x5f,
1334 * we use a short byte read instead. Also, some bus drivers don't implement
1335 * quick write, so we fallback to a byte read in that case too.
1336 * On x86, there is another special case for FSC hardware monitoring chips,
1337 * which want regular byte reads (address 0x73.) Fortunately, these are the
1338 * only known chips using this I2C address on PC hardware.
1339 * Returns 1 if probe succeeded, 0 if not.
1340 */
1341static int i2c_default_probe(struct i2c_adapter *adap, unsigned short addr)
1342{
1343 int err;
1344 union i2c_smbus_data dummy;
1345
1346#ifdef CONFIG_X86
1347 if (addr == 0x73 && (adap->class & I2C_CLASS_HWMON)
1348 && i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE_DATA))
1349 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
1350 I2C_SMBUS_BYTE_DATA, &dummy);
1351 else
1352#endif
1353 if ((addr & ~0x07) == 0x30 || (addr & ~0x0f) == 0x50
1354 || !i2c_check_functionality(adap, I2C_FUNC_SMBUS_QUICK))
1355 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
1356 I2C_SMBUS_BYTE, &dummy);
1357 else
1358 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_WRITE, 0,
1359 I2C_SMBUS_QUICK, NULL);
1360
1361 return err >= 0;
1362}
1363
Jean Delvareccfbbd02009-12-06 17:06:25 +01001364static int i2c_detect_address(struct i2c_client *temp_client,
Jean Delvare4735c982008-07-14 22:38:36 +02001365 struct i2c_driver *driver)
1366{
1367 struct i2c_board_info info;
1368 struct i2c_adapter *adapter = temp_client->adapter;
1369 int addr = temp_client->addr;
1370 int err;
1371
1372 /* Make sure the address is valid */
Jean Delvare656b8762010-06-03 11:33:53 +02001373 err = i2c_check_addr_validity(addr);
1374 if (err) {
Jean Delvare4735c982008-07-14 22:38:36 +02001375 dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
1376 addr);
Jean Delvare656b8762010-06-03 11:33:53 +02001377 return err;
Jean Delvare4735c982008-07-14 22:38:36 +02001378 }
1379
1380 /* Skip if already in use */
Jean Delvare3b5f7942010-06-03 11:33:55 +02001381 if (i2c_check_addr_busy(adapter, addr))
Jean Delvare4735c982008-07-14 22:38:36 +02001382 return 0;
1383
Jean Delvareccfbbd02009-12-06 17:06:25 +01001384 /* Make sure there is something at this address */
Jean Delvare63e4e802010-06-03 11:33:51 +02001385 if (!i2c_default_probe(adapter, addr))
1386 return 0;
Jean Delvare4735c982008-07-14 22:38:36 +02001387
1388 /* Finally call the custom detection function */
1389 memset(&info, 0, sizeof(struct i2c_board_info));
1390 info.addr = addr;
Jean Delvare310ec792009-12-14 21:17:23 +01001391 err = driver->detect(temp_client, &info);
Jean Delvare4735c982008-07-14 22:38:36 +02001392 if (err) {
1393 /* -ENODEV is returned if the detection fails. We catch it
1394 here as this isn't an error. */
1395 return err == -ENODEV ? 0 : err;
1396 }
1397
1398 /* Consistency check */
1399 if (info.type[0] == '\0') {
1400 dev_err(&adapter->dev, "%s detection function provided "
1401 "no name for 0x%x\n", driver->driver.name,
1402 addr);
1403 } else {
1404 struct i2c_client *client;
1405
1406 /* Detection succeeded, instantiate the device */
1407 dev_dbg(&adapter->dev, "Creating %s at 0x%02x\n",
1408 info.type, info.addr);
1409 client = i2c_new_device(adapter, &info);
1410 if (client)
1411 list_add_tail(&client->detected, &driver->clients);
1412 else
1413 dev_err(&adapter->dev, "Failed creating %s at 0x%02x\n",
1414 info.type, info.addr);
1415 }
1416 return 0;
1417}
1418
1419static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver)
1420{
Jean Delvarec3813d62009-12-14 21:17:25 +01001421 const unsigned short *address_list;
Jean Delvare4735c982008-07-14 22:38:36 +02001422 struct i2c_client *temp_client;
1423 int i, err = 0;
1424 int adap_id = i2c_adapter_id(adapter);
1425
Jean Delvarec3813d62009-12-14 21:17:25 +01001426 address_list = driver->address_list;
1427 if (!driver->detect || !address_list)
Jean Delvare4735c982008-07-14 22:38:36 +02001428 return 0;
1429
1430 /* Set up a temporary client to help detect callback */
1431 temp_client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
1432 if (!temp_client)
1433 return -ENOMEM;
1434 temp_client->adapter = adapter;
1435
Jean Delvare4329cf82008-08-28 08:33:23 +02001436 /* Stop here if the classes do not match */
1437 if (!(adapter->class & driver->class))
1438 goto exit_free;
1439
Jean Delvare827900c2010-07-10 09:42:46 +02001440 /* Stop here if the bus doesn't support probing */
1441 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_READ_BYTE)) {
Jean Delvarec3813d62009-12-14 21:17:25 +01001442 if (address_list[0] == I2C_CLIENT_END)
Jean Delvare4735c982008-07-14 22:38:36 +02001443 goto exit_free;
1444
Jean Delvare827900c2010-07-10 09:42:46 +02001445 dev_warn(&adapter->dev, "Probing not supported\n");
Jean Delvare4735c982008-07-14 22:38:36 +02001446 err = -EOPNOTSUPP;
1447 goto exit_free;
1448 }
1449
Jean Delvarec3813d62009-12-14 21:17:25 +01001450 for (i = 0; address_list[i] != I2C_CLIENT_END; i += 1) {
Jean Delvare4735c982008-07-14 22:38:36 +02001451 dev_dbg(&adapter->dev, "found normal entry for adapter %d, "
Jean Delvarec3813d62009-12-14 21:17:25 +01001452 "addr 0x%02x\n", adap_id, address_list[i]);
1453 temp_client->addr = address_list[i];
Jean Delvareccfbbd02009-12-06 17:06:25 +01001454 err = i2c_detect_address(temp_client, driver);
Jean Delvare4735c982008-07-14 22:38:36 +02001455 if (err)
1456 goto exit_free;
1457 }
1458
1459 exit_free:
1460 kfree(temp_client);
1461 return err;
1462}
1463
Jean Delvare12b5053a2007-05-01 23:26:31 +02001464struct i2c_client *
1465i2c_new_probed_device(struct i2c_adapter *adap,
1466 struct i2c_board_info *info,
Jean Delvare9a942412010-08-11 18:20:56 +02001467 unsigned short const *addr_list,
1468 int (*probe)(struct i2c_adapter *, unsigned short addr))
Jean Delvare12b5053a2007-05-01 23:26:31 +02001469{
1470 int i;
1471
Jean Delvare9a942412010-08-11 18:20:56 +02001472 if (!probe) {
1473 /* Stop here if the bus doesn't support probing */
1474 if (!i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE)) {
1475 dev_err(&adap->dev, "Probing not supported\n");
1476 return NULL;
1477 }
1478 probe = i2c_default_probe;
Jean Delvare12b5053a2007-05-01 23:26:31 +02001479 }
1480
Jean Delvare12b5053a2007-05-01 23:26:31 +02001481 for (i = 0; addr_list[i] != I2C_CLIENT_END; i++) {
1482 /* Check address validity */
Jean Delvare656b8762010-06-03 11:33:53 +02001483 if (i2c_check_addr_validity(addr_list[i]) < 0) {
Jean Delvare12b5053a2007-05-01 23:26:31 +02001484 dev_warn(&adap->dev, "Invalid 7-bit address "
1485 "0x%02x\n", addr_list[i]);
1486 continue;
1487 }
1488
1489 /* Check address availability */
Jean Delvare3b5f7942010-06-03 11:33:55 +02001490 if (i2c_check_addr_busy(adap, addr_list[i])) {
Jean Delvare12b5053a2007-05-01 23:26:31 +02001491 dev_dbg(&adap->dev, "Address 0x%02x already in "
1492 "use, not probing\n", addr_list[i]);
1493 continue;
1494 }
1495
Jean Delvare63e4e802010-06-03 11:33:51 +02001496 /* Test address responsiveness */
Jean Delvare9a942412010-08-11 18:20:56 +02001497 if (probe(adap, addr_list[i]))
Jean Delvare63e4e802010-06-03 11:33:51 +02001498 break;
Jean Delvare12b5053a2007-05-01 23:26:31 +02001499 }
Jean Delvare12b5053a2007-05-01 23:26:31 +02001500
1501 if (addr_list[i] == I2C_CLIENT_END) {
1502 dev_dbg(&adap->dev, "Probing failed, no device found\n");
1503 return NULL;
1504 }
1505
1506 info->addr = addr_list[i];
1507 return i2c_new_device(adap, info);
1508}
1509EXPORT_SYMBOL_GPL(i2c_new_probed_device);
1510
Farid Hammane7225acf2010-05-21 18:40:58 +02001511struct i2c_adapter *i2c_get_adapter(int id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 struct i2c_adapter *adapter;
David Brownell438d6c22006-12-10 21:21:31 +01001514
Jean Delvarecaada322008-01-27 18:14:49 +01001515 mutex_lock(&core_lock);
Jack Stone1cf92b42009-06-15 18:01:46 +02001516 adapter = idr_find(&i2c_adapter_idr, id);
Mark M. Hoffmana0920e102005-06-28 00:21:30 -04001517 if (adapter && !try_module_get(adapter->owner))
1518 adapter = NULL;
1519
Jean Delvarecaada322008-01-27 18:14:49 +01001520 mutex_unlock(&core_lock);
Mark M. Hoffmana0920e102005-06-28 00:21:30 -04001521 return adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522}
David Brownellc0564602007-05-01 23:26:31 +02001523EXPORT_SYMBOL(i2c_get_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524
1525void i2c_put_adapter(struct i2c_adapter *adap)
1526{
1527 module_put(adap->owner);
1528}
David Brownellc0564602007-05-01 23:26:31 +02001529EXPORT_SYMBOL(i2c_put_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530
1531/* The SMBus parts */
1532
David Brownell438d6c22006-12-10 21:21:31 +01001533#define POLY (0x1070U << 3)
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001534static u8 crc8(u16 data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535{
1536 int i;
David Brownell438d6c22006-12-10 21:21:31 +01001537
Farid Hammane7225acf2010-05-21 18:40:58 +02001538 for (i = 0; i < 8; i++) {
David Brownell438d6c22006-12-10 21:21:31 +01001539 if (data & 0x8000)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 data = data ^ POLY;
1541 data = data << 1;
1542 }
1543 return (u8)(data >> 8);
1544}
1545
Jean Delvare421ef472005-10-26 21:28:55 +02001546/* Incremental CRC8 over count bytes in the array pointed to by p */
1547static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548{
1549 int i;
1550
Farid Hammane7225acf2010-05-21 18:40:58 +02001551 for (i = 0; i < count; i++)
Jean Delvare421ef472005-10-26 21:28:55 +02001552 crc = crc8((crc ^ p[i]) << 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 return crc;
1554}
1555
Jean Delvare421ef472005-10-26 21:28:55 +02001556/* Assume a 7-bit address, which is reasonable for SMBus */
1557static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558{
Jean Delvare421ef472005-10-26 21:28:55 +02001559 /* The address will be sent first */
1560 u8 addr = (msg->addr << 1) | !!(msg->flags & I2C_M_RD);
1561 pec = i2c_smbus_pec(pec, &addr, 1);
1562
1563 /* The data buffer follows */
1564 return i2c_smbus_pec(pec, msg->buf, msg->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565}
1566
Jean Delvare421ef472005-10-26 21:28:55 +02001567/* Used for write only transactions */
1568static inline void i2c_smbus_add_pec(struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569{
Jean Delvare421ef472005-10-26 21:28:55 +02001570 msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg);
1571 msg->len++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572}
1573
Jean Delvare421ef472005-10-26 21:28:55 +02001574/* Return <0 on CRC error
1575 If there was a write before this read (most cases) we need to take the
1576 partial CRC from the write part into account.
1577 Note that this function does modify the message (we need to decrease the
1578 message length to hide the CRC byte from the caller). */
1579static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580{
Jean Delvare421ef472005-10-26 21:28:55 +02001581 u8 rpec = msg->buf[--msg->len];
1582 cpec = i2c_smbus_msg_pec(cpec, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 if (rpec != cpec) {
1585 pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n",
1586 rpec, cpec);
David Brownell24a5bb72008-07-14 22:38:23 +02001587 return -EBADMSG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 }
David Brownell438d6c22006-12-10 21:21:31 +01001589 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590}
1591
David Brownella1cdeda2008-07-14 22:38:24 +02001592/**
1593 * i2c_smbus_read_byte - SMBus "receive byte" protocol
1594 * @client: Handle to slave device
1595 *
1596 * This executes the SMBus "receive byte" protocol, returning negative errno
1597 * else the byte received from the device.
1598 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599s32 i2c_smbus_read_byte(struct i2c_client *client)
1600{
1601 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02001602 int status;
1603
1604 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1605 I2C_SMBUS_READ, 0,
1606 I2C_SMBUS_BYTE, &data);
1607 return (status < 0) ? status : data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608}
David Brownellc0564602007-05-01 23:26:31 +02001609EXPORT_SYMBOL(i2c_smbus_read_byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610
David Brownella1cdeda2008-07-14 22:38:24 +02001611/**
1612 * i2c_smbus_write_byte - SMBus "send byte" protocol
1613 * @client: Handle to slave device
1614 * @value: Byte to be sent
1615 *
1616 * This executes the SMBus "send byte" protocol, returning negative errno
1617 * else zero on success.
1618 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619s32 i2c_smbus_write_byte(struct i2c_client *client, u8 value)
1620{
Farid Hammane7225acf2010-05-21 18:40:58 +02001621 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
Jean Delvare421ef472005-10-26 21:28:55 +02001622 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623}
David Brownellc0564602007-05-01 23:26:31 +02001624EXPORT_SYMBOL(i2c_smbus_write_byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625
David Brownella1cdeda2008-07-14 22:38:24 +02001626/**
1627 * i2c_smbus_read_byte_data - SMBus "read byte" protocol
1628 * @client: Handle to slave device
1629 * @command: Byte interpreted by slave
1630 *
1631 * This executes the SMBus "read byte" protocol, returning negative errno
1632 * else a data byte received from the device.
1633 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634s32 i2c_smbus_read_byte_data(struct i2c_client *client, u8 command)
1635{
1636 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02001637 int status;
1638
1639 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1640 I2C_SMBUS_READ, command,
1641 I2C_SMBUS_BYTE_DATA, &data);
1642 return (status < 0) ? status : data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643}
David Brownellc0564602007-05-01 23:26:31 +02001644EXPORT_SYMBOL(i2c_smbus_read_byte_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645
David Brownella1cdeda2008-07-14 22:38:24 +02001646/**
1647 * i2c_smbus_write_byte_data - SMBus "write byte" protocol
1648 * @client: Handle to slave device
1649 * @command: Byte interpreted by slave
1650 * @value: Byte being written
1651 *
1652 * This executes the SMBus "write byte" protocol, returning negative errno
1653 * else zero on success.
1654 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655s32 i2c_smbus_write_byte_data(struct i2c_client *client, u8 command, u8 value)
1656{
1657 union i2c_smbus_data data;
1658 data.byte = value;
Farid Hammane7225acf2010-05-21 18:40:58 +02001659 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1660 I2C_SMBUS_WRITE, command,
1661 I2C_SMBUS_BYTE_DATA, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662}
David Brownellc0564602007-05-01 23:26:31 +02001663EXPORT_SYMBOL(i2c_smbus_write_byte_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664
David Brownella1cdeda2008-07-14 22:38:24 +02001665/**
1666 * i2c_smbus_read_word_data - SMBus "read word" protocol
1667 * @client: Handle to slave device
1668 * @command: Byte interpreted by slave
1669 *
1670 * This executes the SMBus "read word" protocol, returning negative errno
1671 * else a 16-bit unsigned "word" received from the device.
1672 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673s32 i2c_smbus_read_word_data(struct i2c_client *client, u8 command)
1674{
1675 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02001676 int status;
1677
1678 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1679 I2C_SMBUS_READ, command,
1680 I2C_SMBUS_WORD_DATA, &data);
1681 return (status < 0) ? status : data.word;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682}
David Brownellc0564602007-05-01 23:26:31 +02001683EXPORT_SYMBOL(i2c_smbus_read_word_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684
David Brownella1cdeda2008-07-14 22:38:24 +02001685/**
1686 * i2c_smbus_write_word_data - SMBus "write word" protocol
1687 * @client: Handle to slave device
1688 * @command: Byte interpreted by slave
1689 * @value: 16-bit "word" being written
1690 *
1691 * This executes the SMBus "write word" protocol, returning negative errno
1692 * else zero on success.
1693 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694s32 i2c_smbus_write_word_data(struct i2c_client *client, u8 command, u16 value)
1695{
1696 union i2c_smbus_data data;
1697 data.word = value;
Farid Hammane7225acf2010-05-21 18:40:58 +02001698 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1699 I2C_SMBUS_WRITE, command,
1700 I2C_SMBUS_WORD_DATA, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701}
David Brownellc0564602007-05-01 23:26:31 +02001702EXPORT_SYMBOL(i2c_smbus_write_word_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703
David Brownella64ec072007-10-13 23:56:31 +02001704/**
Prakash Mortha596c88f2008-10-14 17:30:06 +02001705 * i2c_smbus_process_call - SMBus "process call" protocol
1706 * @client: Handle to slave device
1707 * @command: Byte interpreted by slave
1708 * @value: 16-bit "word" being written
1709 *
1710 * This executes the SMBus "process call" protocol, returning negative errno
1711 * else a 16-bit unsigned "word" received from the device.
1712 */
1713s32 i2c_smbus_process_call(struct i2c_client *client, u8 command, u16 value)
1714{
1715 union i2c_smbus_data data;
1716 int status;
1717 data.word = value;
1718
1719 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1720 I2C_SMBUS_WRITE, command,
1721 I2C_SMBUS_PROC_CALL, &data);
1722 return (status < 0) ? status : data.word;
1723}
1724EXPORT_SYMBOL(i2c_smbus_process_call);
1725
1726/**
David Brownella1cdeda2008-07-14 22:38:24 +02001727 * i2c_smbus_read_block_data - SMBus "block read" protocol
David Brownella64ec072007-10-13 23:56:31 +02001728 * @client: Handle to slave device
David Brownella1cdeda2008-07-14 22:38:24 +02001729 * @command: Byte interpreted by slave
David Brownella64ec072007-10-13 23:56:31 +02001730 * @values: Byte array into which data will be read; big enough to hold
1731 * the data returned by the slave. SMBus allows at most 32 bytes.
1732 *
David Brownella1cdeda2008-07-14 22:38:24 +02001733 * This executes the SMBus "block read" protocol, returning negative errno
1734 * else the number of data bytes in the slave's response.
David Brownella64ec072007-10-13 23:56:31 +02001735 *
1736 * Note that using this function requires that the client's adapter support
1737 * the I2C_FUNC_SMBUS_READ_BLOCK_DATA functionality. Not all adapter drivers
1738 * support this; its emulation through I2C messaging relies on a specific
1739 * mechanism (I2C_M_RECV_LEN) which may not be implemented.
1740 */
Jean Delvareb86a1bc2007-05-01 23:26:34 +02001741s32 i2c_smbus_read_block_data(struct i2c_client *client, u8 command,
1742 u8 *values)
1743{
1744 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02001745 int status;
Jean Delvareb86a1bc2007-05-01 23:26:34 +02001746
David Brownell24a5bb72008-07-14 22:38:23 +02001747 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1748 I2C_SMBUS_READ, command,
1749 I2C_SMBUS_BLOCK_DATA, &data);
1750 if (status)
1751 return status;
Jean Delvareb86a1bc2007-05-01 23:26:34 +02001752
1753 memcpy(values, &data.block[1], data.block[0]);
1754 return data.block[0];
1755}
1756EXPORT_SYMBOL(i2c_smbus_read_block_data);
1757
David Brownella1cdeda2008-07-14 22:38:24 +02001758/**
1759 * i2c_smbus_write_block_data - SMBus "block write" protocol
1760 * @client: Handle to slave device
1761 * @command: Byte interpreted by slave
1762 * @length: Size of data block; SMBus allows at most 32 bytes
1763 * @values: Byte array which will be written.
1764 *
1765 * This executes the SMBus "block write" protocol, returning negative errno
1766 * else zero on success.
1767 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768s32 i2c_smbus_write_block_data(struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +02001769 u8 length, const u8 *values)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770{
1771 union i2c_smbus_data data;
Jean Delvare76560322006-01-18 23:14:55 +01001772
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 if (length > I2C_SMBUS_BLOCK_MAX)
1774 length = I2C_SMBUS_BLOCK_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 data.block[0] = length;
Jean Delvare76560322006-01-18 23:14:55 +01001776 memcpy(&data.block[1], values, length);
Farid Hammane7225acf2010-05-21 18:40:58 +02001777 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1778 I2C_SMBUS_WRITE, command,
1779 I2C_SMBUS_BLOCK_DATA, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780}
David Brownellc0564602007-05-01 23:26:31 +02001781EXPORT_SYMBOL(i2c_smbus_write_block_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782
1783/* Returns the number of read bytes */
Jean Delvare4b2643d2007-07-12 14:12:29 +02001784s32 i2c_smbus_read_i2c_block_data(struct i2c_client *client, u8 command,
1785 u8 length, u8 *values)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786{
1787 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02001788 int status;
Jean Delvare76560322006-01-18 23:14:55 +01001789
Jean Delvare4b2643d2007-07-12 14:12:29 +02001790 if (length > I2C_SMBUS_BLOCK_MAX)
1791 length = I2C_SMBUS_BLOCK_MAX;
1792 data.block[0] = length;
David Brownell24a5bb72008-07-14 22:38:23 +02001793 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1794 I2C_SMBUS_READ, command,
1795 I2C_SMBUS_I2C_BLOCK_DATA, &data);
1796 if (status < 0)
1797 return status;
Jean Delvare76560322006-01-18 23:14:55 +01001798
1799 memcpy(values, &data.block[1], data.block[0]);
1800 return data.block[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801}
David Brownellc0564602007-05-01 23:26:31 +02001802EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803
Jean Delvare21bbd692006-01-09 15:19:18 +11001804s32 i2c_smbus_write_i2c_block_data(struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +02001805 u8 length, const u8 *values)
Jean Delvare21bbd692006-01-09 15:19:18 +11001806{
1807 union i2c_smbus_data data;
1808
1809 if (length > I2C_SMBUS_BLOCK_MAX)
1810 length = I2C_SMBUS_BLOCK_MAX;
1811 data.block[0] = length;
1812 memcpy(data.block + 1, values, length);
1813 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1814 I2C_SMBUS_WRITE, command,
1815 I2C_SMBUS_I2C_BLOCK_DATA, &data);
1816}
David Brownellc0564602007-05-01 23:26:31 +02001817EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data);
Jean Delvare21bbd692006-01-09 15:19:18 +11001818
David Brownell438d6c22006-12-10 21:21:31 +01001819/* Simulate a SMBus command using the i2c protocol
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 No checking of parameters is done! */
Farid Hammane7225acf2010-05-21 18:40:58 +02001821static s32 i2c_smbus_xfer_emulated(struct i2c_adapter *adapter, u16 addr,
1822 unsigned short flags,
1823 char read_write, u8 command, int size,
1824 union i2c_smbus_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825{
1826 /* So we need to generate a series of msgs. In the case of writing, we
1827 need to use only one message; when reading, we need two. We initialize
1828 most things with sane defaults, to keep the code below somewhat
1829 simpler. */
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02001830 unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3];
1831 unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2];
Farid Hammane7225acf2010-05-21 18:40:58 +02001832 int num = read_write == I2C_SMBUS_READ ? 2 : 1;
David Brownell438d6c22006-12-10 21:21:31 +01001833 struct i2c_msg msg[2] = { { addr, flags, 1, msgbuf0 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 { addr, flags | I2C_M_RD, 0, msgbuf1 }
1835 };
1836 int i;
Jean Delvare421ef472005-10-26 21:28:55 +02001837 u8 partial_pec = 0;
David Brownell24a5bb72008-07-14 22:38:23 +02001838 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839
1840 msgbuf0[0] = command;
Farid Hammane7225acf2010-05-21 18:40:58 +02001841 switch (size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 case I2C_SMBUS_QUICK:
1843 msg[0].len = 0;
1844 /* Special case: The read/write field is used as data */
Roel Kluinf29d2e02009-02-24 19:19:48 +01001845 msg[0].flags = flags | (read_write == I2C_SMBUS_READ ?
1846 I2C_M_RD : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 num = 1;
1848 break;
1849 case I2C_SMBUS_BYTE:
1850 if (read_write == I2C_SMBUS_READ) {
1851 /* Special case: only a read! */
1852 msg[0].flags = I2C_M_RD | flags;
1853 num = 1;
1854 }
1855 break;
1856 case I2C_SMBUS_BYTE_DATA:
1857 if (read_write == I2C_SMBUS_READ)
1858 msg[1].len = 1;
1859 else {
1860 msg[0].len = 2;
1861 msgbuf0[1] = data->byte;
1862 }
1863 break;
1864 case I2C_SMBUS_WORD_DATA:
1865 if (read_write == I2C_SMBUS_READ)
1866 msg[1].len = 2;
1867 else {
Farid Hammane7225acf2010-05-21 18:40:58 +02001868 msg[0].len = 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02001870 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 }
1872 break;
1873 case I2C_SMBUS_PROC_CALL:
1874 num = 2; /* Special case */
1875 read_write = I2C_SMBUS_READ;
1876 msg[0].len = 3;
1877 msg[1].len = 2;
1878 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02001879 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 break;
1881 case I2C_SMBUS_BLOCK_DATA:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 if (read_write == I2C_SMBUS_READ) {
Jean Delvare209d27c2007-05-01 23:26:29 +02001883 msg[1].flags |= I2C_M_RECV_LEN;
1884 msg[1].len = 1; /* block length will be added by
1885 the underlying bus driver */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 } else {
1887 msg[0].len = data->block[0] + 2;
1888 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
David Brownell24a5bb72008-07-14 22:38:23 +02001889 dev_err(&adapter->dev,
1890 "Invalid block write size %d\n",
1891 data->block[0]);
1892 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 }
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02001894 for (i = 1; i < msg[0].len; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 msgbuf0[i] = data->block[i-1];
1896 }
1897 break;
1898 case I2C_SMBUS_BLOCK_PROC_CALL:
Jean Delvare209d27c2007-05-01 23:26:29 +02001899 num = 2; /* Another special case */
1900 read_write = I2C_SMBUS_READ;
1901 if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
David Brownell24a5bb72008-07-14 22:38:23 +02001902 dev_err(&adapter->dev,
1903 "Invalid block write size %d\n",
Jean Delvare209d27c2007-05-01 23:26:29 +02001904 data->block[0]);
David Brownell24a5bb72008-07-14 22:38:23 +02001905 return -EINVAL;
Jean Delvare209d27c2007-05-01 23:26:29 +02001906 }
1907 msg[0].len = data->block[0] + 2;
1908 for (i = 1; i < msg[0].len; i++)
1909 msgbuf0[i] = data->block[i-1];
1910 msg[1].flags |= I2C_M_RECV_LEN;
1911 msg[1].len = 1; /* block length will be added by
1912 the underlying bus driver */
1913 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 case I2C_SMBUS_I2C_BLOCK_DATA:
1915 if (read_write == I2C_SMBUS_READ) {
Jean Delvare4b2643d2007-07-12 14:12:29 +02001916 msg[1].len = data->block[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 } else {
1918 msg[0].len = data->block[0] + 1;
Jean Delvare30dac742005-10-08 00:15:59 +02001919 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) {
David Brownell24a5bb72008-07-14 22:38:23 +02001920 dev_err(&adapter->dev,
1921 "Invalid block write size %d\n",
1922 data->block[0]);
1923 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 }
1925 for (i = 1; i <= data->block[0]; i++)
1926 msgbuf0[i] = data->block[i];
1927 }
1928 break;
1929 default:
David Brownell24a5bb72008-07-14 22:38:23 +02001930 dev_err(&adapter->dev, "Unsupported transaction %d\n", size);
1931 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 }
1933
Jean Delvare421ef472005-10-26 21:28:55 +02001934 i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK
1935 && size != I2C_SMBUS_I2C_BLOCK_DATA);
1936 if (i) {
1937 /* Compute PEC if first message is a write */
1938 if (!(msg[0].flags & I2C_M_RD)) {
David Brownell438d6c22006-12-10 21:21:31 +01001939 if (num == 1) /* Write only */
Jean Delvare421ef472005-10-26 21:28:55 +02001940 i2c_smbus_add_pec(&msg[0]);
1941 else /* Write followed by read */
1942 partial_pec = i2c_smbus_msg_pec(0, &msg[0]);
1943 }
1944 /* Ask for PEC if last message is a read */
1945 if (msg[num-1].flags & I2C_M_RD)
David Brownell438d6c22006-12-10 21:21:31 +01001946 msg[num-1].len++;
Jean Delvare421ef472005-10-26 21:28:55 +02001947 }
1948
David Brownell24a5bb72008-07-14 22:38:23 +02001949 status = i2c_transfer(adapter, msg, num);
1950 if (status < 0)
1951 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952
Jean Delvare421ef472005-10-26 21:28:55 +02001953 /* Check PEC if last message is a read */
1954 if (i && (msg[num-1].flags & I2C_M_RD)) {
David Brownell24a5bb72008-07-14 22:38:23 +02001955 status = i2c_smbus_check_pec(partial_pec, &msg[num-1]);
1956 if (status < 0)
1957 return status;
Jean Delvare421ef472005-10-26 21:28:55 +02001958 }
1959
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 if (read_write == I2C_SMBUS_READ)
Farid Hammane7225acf2010-05-21 18:40:58 +02001961 switch (size) {
1962 case I2C_SMBUS_BYTE:
1963 data->byte = msgbuf0[0];
1964 break;
1965 case I2C_SMBUS_BYTE_DATA:
1966 data->byte = msgbuf1[0];
1967 break;
1968 case I2C_SMBUS_WORD_DATA:
1969 case I2C_SMBUS_PROC_CALL:
1970 data->word = msgbuf1[0] | (msgbuf1[1] << 8);
1971 break;
1972 case I2C_SMBUS_I2C_BLOCK_DATA:
1973 for (i = 0; i < data->block[0]; i++)
1974 data->block[i+1] = msgbuf1[i];
1975 break;
1976 case I2C_SMBUS_BLOCK_DATA:
1977 case I2C_SMBUS_BLOCK_PROC_CALL:
1978 for (i = 0; i < msgbuf1[0] + 1; i++)
1979 data->block[i] = msgbuf1[i];
1980 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 }
1982 return 0;
1983}
1984
David Brownella1cdeda2008-07-14 22:38:24 +02001985/**
1986 * i2c_smbus_xfer - execute SMBus protocol operations
1987 * @adapter: Handle to I2C bus
1988 * @addr: Address of SMBus slave on that bus
1989 * @flags: I2C_CLIENT_* flags (usually zero or I2C_CLIENT_PEC)
1990 * @read_write: I2C_SMBUS_READ or I2C_SMBUS_WRITE
1991 * @command: Byte interpreted by slave, for protocols which use such bytes
1992 * @protocol: SMBus protocol operation to execute, such as I2C_SMBUS_PROC_CALL
1993 * @data: Data to be read or written
1994 *
1995 * This executes an SMBus protocol operation, and returns a negative
1996 * errno code else zero on success.
1997 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001998s32 i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr, unsigned short flags,
David Brownella1cdeda2008-07-14 22:38:24 +02001999 char read_write, u8 command, int protocol,
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01002000 union i2c_smbus_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001{
Clifford Wolf66b650f2009-06-15 18:01:46 +02002002 unsigned long orig_jiffies;
2003 int try;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 s32 res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005
2006 flags &= I2C_M_TEN | I2C_CLIENT_PEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007
2008 if (adapter->algo->smbus_xfer) {
Mika Kuoppala194684e2009-12-06 17:06:22 +01002009 rt_mutex_lock(&adapter->bus_lock);
Clifford Wolf66b650f2009-06-15 18:01:46 +02002010
2011 /* Retry automatically on arbitration loss */
2012 orig_jiffies = jiffies;
2013 for (res = 0, try = 0; try <= adapter->retries; try++) {
2014 res = adapter->algo->smbus_xfer(adapter, addr, flags,
2015 read_write, command,
2016 protocol, data);
2017 if (res != -EAGAIN)
2018 break;
2019 if (time_after(jiffies,
2020 orig_jiffies + adapter->timeout))
2021 break;
2022 }
Mika Kuoppala194684e2009-12-06 17:06:22 +01002023 rt_mutex_unlock(&adapter->bus_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 } else
Farid Hammane7225acf2010-05-21 18:40:58 +02002025 res = i2c_smbus_xfer_emulated(adapter, addr, flags, read_write,
David Brownella1cdeda2008-07-14 22:38:24 +02002026 command, protocol, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 return res;
2029}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030EXPORT_SYMBOL(i2c_smbus_xfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031
2032MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
2033MODULE_DESCRIPTION("I2C-Bus main module");
2034MODULE_LICENSE("GPL");