i2c-core.c 54.8 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/* i2c-core.c - a device driver for the iic-bus interface		     */
/* ------------------------------------------------------------------------- */
/*   Copyright (C) 1995-99 Simon G. Vogl

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.		     */
/* ------------------------------------------------------------------------- */

20
/* With some changes from Kyösti Mälkki <kmalkki@cc.hut.fi>.
Linus Torvalds's avatar
Linus Torvalds committed
21
   All SMBus-related things are written by Frodo Looijaard <frodol@dds.nl>
22
23
   SMBus 2.0 support by Mark Studebaker <mdsxyz123@yahoo.com> and
   Jean Delvare <khali@linux-fr.org> */
Linus Torvalds's avatar
Linus Torvalds committed
24
25
26
27
28
29
30
31

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/errno.h>
#include <linux/slab.h>
#include <linux/i2c.h>
#include <linux/init.h>
#include <linux/idr.h>
32
#include <linux/platform_device.h>
33
#include <linux/mutex.h>
Jean Delvare's avatar
Jean Delvare committed
34
#include <linux/completion.h>
35
36
#include <linux/hardirq.h>
#include <linux/irqflags.h>
Linus Torvalds's avatar
Linus Torvalds committed
37
38
#include <asm/uaccess.h>

39
40
#include "i2c-core.h"

Linus Torvalds's avatar
Linus Torvalds committed
41

Jean Delvare's avatar
Jean Delvare committed
42
static DEFINE_MUTEX(core_lock);
Linus Torvalds's avatar
Linus Torvalds committed
43
44
static DEFINE_IDR(i2c_adapter_idr);

45
46
47
#define is_newstyle_driver(d) ((d)->probe || (d)->remove || (d)->detect)

static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver);
48
49
50

/* ------------------------------------------------------------------------- */

51
52
53
54
55
56
57
58
59
60
61
static const struct i2c_device_id *i2c_match_id(const struct i2c_device_id *id,
						const struct i2c_client *client)
{
	while (id->name[0]) {
		if (strcmp(client->name, id->name) == 0)
			return id;
		id++;
	}
	return NULL;
}

Linus Torvalds's avatar
Linus Torvalds committed
62
63
static int i2c_device_match(struct device *dev, struct device_driver *drv)
{
David Brownell's avatar
David Brownell committed
64
65
66
67
68
69
	struct i2c_client	*client = to_i2c_client(dev);
	struct i2c_driver	*driver = to_i2c_driver(drv);

	/* make legacy i2c drivers bypass driver model probing entirely;
	 * such drivers scan each i2c adapter/bus themselves.
	 */
David Brownell's avatar
David Brownell committed
70
	if (!is_newstyle_driver(driver))
David Brownell's avatar
David Brownell committed
71
72
		return 0;

73
74
75
76
	/* match on an id table if there is one */
	if (driver->id_table)
		return i2c_match_id(driver->id_table, client) != NULL;

77
	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
78
79
}

David Brownell's avatar
David Brownell committed
80
81
82
#ifdef	CONFIG_HOTPLUG

/* uevent helps with hotplug: modprobe -q $(MODALIAS) */
83
static int i2c_device_uevent(struct device *dev, struct kobj_uevent_env *env)
David Brownell's avatar
David Brownell committed
84
85
86
87
{
	struct i2c_client	*client = to_i2c_client(dev);

	/* by definition, legacy drivers can't hotplug */
88
	if (dev->driver)
David Brownell's avatar
David Brownell committed
89
90
		return 0;

91
92
93
	if (add_uevent_var(env, "MODALIAS=%s%s",
			   I2C_MODULE_PREFIX, client->name))
		return -ENOMEM;
David Brownell's avatar
David Brownell committed
94
95
96
97
98
99
100
101
	dev_dbg(dev, "uevent\n");
	return 0;
}

#else
#define i2c_device_uevent	NULL
#endif	/* CONFIG_HOTPLUG */

102
static int i2c_device_probe(struct device *dev)
Linus Torvalds's avatar
Linus Torvalds committed
103
{
David Brownell's avatar
David Brownell committed
104
105
	struct i2c_client	*client = to_i2c_client(dev);
	struct i2c_driver	*driver = to_i2c_driver(dev->driver);
106
	int status;
David Brownell's avatar
David Brownell committed
107

Jean Delvare's avatar
Jean Delvare committed
108
	if (!driver->probe || !driver->id_table)
David Brownell's avatar
David Brownell committed
109
110
		return -ENODEV;
	client->driver = driver;
111
112
113
	if (!device_can_wakeup(&client->dev))
		device_init_wakeup(&client->dev,
					client->flags & I2C_CLIENT_WAKE);
David Brownell's avatar
David Brownell committed
114
	dev_dbg(dev, "probe\n");
115

Jean Delvare's avatar
Jean Delvare committed
116
	status = driver->probe(client, i2c_match_id(driver->id_table, client));
117
118
119
	if (status)
		client->driver = NULL;
	return status;
120
}
Linus Torvalds's avatar
Linus Torvalds committed
121

122
123
static int i2c_device_remove(struct device *dev)
{
David Brownell's avatar
David Brownell committed
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
	struct i2c_client	*client = to_i2c_client(dev);
	struct i2c_driver	*driver;
	int			status;

	if (!dev->driver)
		return 0;

	driver = to_i2c_driver(dev->driver);
	if (driver->remove) {
		dev_dbg(dev, "remove\n");
		status = driver->remove(client);
	} else {
		dev->driver = NULL;
		status = 0;
	}
	if (status == 0)
		client->driver = NULL;
	return status;
Linus Torvalds's avatar
Linus Torvalds committed
142
143
}

144
static void i2c_device_shutdown(struct device *dev)
Linus Torvalds's avatar
Linus Torvalds committed
145
{
146
147
148
149
150
151
152
	struct i2c_driver *driver;

	if (!dev->driver)
		return;
	driver = to_i2c_driver(dev->driver);
	if (driver->shutdown)
		driver->shutdown(to_i2c_client(dev));
Linus Torvalds's avatar
Linus Torvalds committed
153
154
}

Zhenwen Xu's avatar
Zhenwen Xu committed
155
static int i2c_device_suspend(struct device *dev, pm_message_t mesg)
Linus Torvalds's avatar
Linus Torvalds committed
156
{
157
158
159
160
161
162
163
164
	struct i2c_driver *driver;

	if (!dev->driver)
		return 0;
	driver = to_i2c_driver(dev->driver);
	if (!driver->suspend)
		return 0;
	return driver->suspend(to_i2c_client(dev), mesg);
Linus Torvalds's avatar
Linus Torvalds committed
165
166
}

Zhenwen Xu's avatar
Zhenwen Xu committed
167
static int i2c_device_resume(struct device *dev)
Linus Torvalds's avatar
Linus Torvalds committed
168
{
169
170
171
172
173
174
175
176
	struct i2c_driver *driver;

	if (!dev->driver)
		return 0;
	driver = to_i2c_driver(dev->driver);
	if (!driver->resume)
		return 0;
	return driver->resume(to_i2c_client(dev));
Linus Torvalds's avatar
Linus Torvalds committed
177
178
}

David Brownell's avatar
David Brownell committed
179
180
181
182
183
184
static void i2c_client_release(struct device *dev)
{
	struct i2c_client *client = to_i2c_client(dev);
	complete(&client->released);
}

185
186
187
188
189
static void i2c_client_dev_release(struct device *dev)
{
	kfree(to_i2c_client(dev));
}

Zhenwen Xu's avatar
Zhenwen Xu committed
190
191
static ssize_t
show_client_name(struct device *dev, struct device_attribute *attr, char *buf)
David Brownell's avatar
David Brownell committed
192
193
194
195
196
{
	struct i2c_client *client = to_i2c_client(dev);
	return sprintf(buf, "%s\n", client->name);
}

Zhenwen Xu's avatar
Zhenwen Xu committed
197
198
static ssize_t
show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
David Brownell's avatar
David Brownell committed
199
200
{
	struct i2c_client *client = to_i2c_client(dev);
201
	return sprintf(buf, "%s%s\n", I2C_MODULE_PREFIX, client->name);
David Brownell's avatar
David Brownell committed
202
203
204
205
206
207
208
209
210
}

static struct device_attribute i2c_dev_attrs[] = {
	__ATTR(name, S_IRUGO, show_client_name, NULL),
	/* modalias helps coldplug:  modprobe $(cat .../modalias) */
	__ATTR(modalias, S_IRUGO, show_modalias, NULL),
	{ },
};

211
struct bus_type i2c_bus_type = {
212
	.name		= "i2c",
David Brownell's avatar
David Brownell committed
213
	.dev_attrs	= i2c_dev_attrs,
214
	.match		= i2c_device_match,
David Brownell's avatar
David Brownell committed
215
	.uevent		= i2c_device_uevent,
216
217
218
219
220
	.probe		= i2c_device_probe,
	.remove		= i2c_device_remove,
	.shutdown	= i2c_device_shutdown,
	.suspend	= i2c_device_suspend,
	.resume		= i2c_device_resume,
221
};
222
EXPORT_SYMBOL_GPL(i2c_bus_type);
223

224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242

/**
 * i2c_verify_client - return parameter as i2c_client, or NULL
 * @dev: device, probably from some driver model iterator
 *
 * When traversing the driver model tree, perhaps using driver model
 * iterators like @device_for_each_child(), you can't assume very much
 * about the nodes you find.  Use this function to avoid oopses caused
 * by wrongly treating some non-I2C device as an i2c_client.
 */
struct i2c_client *i2c_verify_client(struct device *dev)
{
	return (dev->bus == &i2c_bus_type)
			? to_i2c_client(dev)
			: NULL;
}
EXPORT_SYMBOL(i2c_verify_client);


243
244
245
246
/**
 * i2c_new_device - instantiate an i2c device for use with a new style driver
 * @adap: the adapter managing the device
 * @info: describes one I2C device; bus_num is ignored
David Brownell's avatar
David Brownell committed
247
 * Context: can sleep
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
 *
 * Create a device to work with a new style i2c driver, where binding is
 * handled through driver model probe()/remove() methods.  This call is not
 * appropriate for use by mainboad initialization logic, which usually runs
 * during an arch_initcall() long before any i2c_adapter could exist.
 *
 * This returns the new i2c client, which may be saved for later use with
 * i2c_unregister_device(); or NULL to indicate an error.
 */
struct i2c_client *
i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
{
	struct i2c_client	*client;
	int			status;

	client = kzalloc(sizeof *client, GFP_KERNEL);
	if (!client)
		return NULL;

	client->adapter = adap;

	client->dev.platform_data = info->platform_data;
270

271
272
273
	if (info->archdata)
		client->dev.archdata = *info->archdata;

274
	client->flags = info->flags;
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
	client->addr = info->addr;
	client->irq = info->irq;

	strlcpy(client->name, info->type, sizeof(client->name));

	/* a new style driver may be bound to this device when we
	 * return from this function, or any later moment (e.g. maybe
	 * hotplugging will load the driver module).  and the device
	 * refcount model is the standard driver model one.
	 */
	status = i2c_attach_client(client);
	if (status < 0) {
		kfree(client);
		client = NULL;
	}
	return client;
}
EXPORT_SYMBOL_GPL(i2c_new_device);


/**
 * i2c_unregister_device - reverse effect of i2c_new_device()
 * @client: value returned from i2c_new_device()
David Brownell's avatar
David Brownell committed
298
 * Context: can sleep
299
300
 */
void i2c_unregister_device(struct i2c_client *client)
David Brownell's avatar
David Brownell committed
301
302
303
304
305
306
307
308
309
310
311
{
	struct i2c_adapter	*adapter = client->adapter;
	struct i2c_driver	*driver = client->driver;

	if (driver && !is_newstyle_driver(driver)) {
		dev_err(&client->dev, "can't unregister devices "
			"with legacy drivers\n");
		WARN_ON(1);
		return;
	}

312
313
314
315
316
317
318
319
	if (adapter->client_unregister) {
		if (adapter->client_unregister(client)) {
			dev_warn(&client->dev,
				 "client_unregister [%s] failed\n",
				 client->name);
		}
	}

David Brownell's avatar
David Brownell committed
320
321
322
323
324
325
	mutex_lock(&adapter->clist_lock);
	list_del(&client->list);
	mutex_unlock(&adapter->clist_lock);

	device_unregister(&client->dev);
}
326
EXPORT_SYMBOL_GPL(i2c_unregister_device);
David Brownell's avatar
David Brownell committed
327
328


329
330
331
332
333
static const struct i2c_device_id dummy_id[] = {
	{ "dummy", 0 },
	{ },
};

334
335
336
337
338
339
340
static int dummy_probe(struct i2c_client *client,
		       const struct i2c_device_id *id)
{
	return 0;
}

static int dummy_remove(struct i2c_client *client)
341
342
343
344
345
346
{
	return 0;
}

static struct i2c_driver dummy_driver = {
	.driver.name	= "dummy",
347
348
	.probe		= dummy_probe,
	.remove		= dummy_remove,
349
	.id_table	= dummy_id,
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
};

/**
 * i2c_new_dummy - return a new i2c device bound to a dummy driver
 * @adapter: the adapter managing the device
 * @address: seven bit address to be used
 * Context: can sleep
 *
 * This returns an I2C client bound to the "dummy" driver, intended for use
 * with devices that consume multiple addresses.  Examples of such chips
 * include various EEPROMS (like 24c04 and 24c08 models).
 *
 * These dummy devices have two main uses.  First, most I2C and SMBus calls
 * except i2c_transfer() need a client handle; the dummy will be that handle.
 * And second, this prevents the specified address from being bound to a
 * different driver.
 *
 * This returns the new i2c client, which should be saved for later use with
 * i2c_unregister_device(); or NULL to indicate an error.
 */
Zhenwen Xu's avatar
Zhenwen Xu committed
370
struct i2c_client *i2c_new_dummy(struct i2c_adapter *adapter, u16 address)
371
372
{
	struct i2c_board_info info = {
373
		I2C_BOARD_INFO("dummy", address),
374
375
376
377
378
379
	};

	return i2c_new_device(adapter, &info);
}
EXPORT_SYMBOL_GPL(i2c_new_dummy);

380
381
/* ------------------------------------------------------------------------- */

David Brownell's avatar
David Brownell committed
382
383
/* I2C bus adapters -- one roots each I2C or SMBUS segment */

Adrian Bunk's avatar
Adrian Bunk committed
384
static void i2c_adapter_dev_release(struct device *dev)
Linus Torvalds's avatar
Linus Torvalds committed
385
{
386
	struct i2c_adapter *adap = to_i2c_adapter(dev);
Linus Torvalds's avatar
Linus Torvalds committed
387
388
389
	complete(&adap->dev_released);
}

David Brownell's avatar
David Brownell committed
390
391
392
static ssize_t
show_adapter_name(struct device *dev, struct device_attribute *attr, char *buf)
{
393
	struct i2c_adapter *adap = to_i2c_adapter(dev);
David Brownell's avatar
David Brownell committed
394
395
	return sprintf(buf, "%s\n", adap->name);
}
396

David Brownell's avatar
David Brownell committed
397
398
399
400
static struct device_attribute i2c_adapter_attrs[] = {
	__ATTR(name, S_IRUGO, show_adapter_name, NULL),
	{ },
};
401

Adrian Bunk's avatar
Adrian Bunk committed
402
static struct class i2c_adapter_class = {
403
404
	.owner			= THIS_MODULE,
	.name			= "i2c-adapter",
David Brownell's avatar
David Brownell committed
405
	.dev_attrs		= i2c_adapter_attrs,
Linus Torvalds's avatar
Linus Torvalds committed
406
407
};

408
409
410
411
412
413
414
415
416
static void i2c_scan_static_board_info(struct i2c_adapter *adapter)
{
	struct i2c_devinfo	*devinfo;

	mutex_lock(&__i2c_board_lock);
	list_for_each_entry(devinfo, &__i2c_board_list, list) {
		if (devinfo->busnum == adapter->nr
				&& !i2c_new_device(adapter,
						&devinfo->board_info))
Zhenwen Xu's avatar
Zhenwen Xu committed
417
418
			dev_err(&adapter->dev,
				"Can't create device at 0x%02x\n",
419
420
421
422
423
				devinfo->board_info.addr);
	}
	mutex_unlock(&__i2c_board_lock);
}

424
425
426
427
428
static int i2c_do_add_adapter(struct device_driver *d, void *data)
{
	struct i2c_driver *driver = to_i2c_driver(d);
	struct i2c_adapter *adap = data;

429
430
431
432
	/* Detect supported devices on that bus, and instantiate them */
	i2c_detect(adap, driver);

	/* Let legacy drivers scan this bus for matching devices */
433
434
435
436
437
438
439
	if (driver->attach_adapter) {
		/* We ignore the return code; if it fails, too bad */
		driver->attach_adapter(adap);
	}
	return 0;
}

440
static int i2c_register_adapter(struct i2c_adapter *adap)
Linus Torvalds's avatar
Linus Torvalds committed
441
{
442
	int res = 0, dummy;
Linus Torvalds's avatar
Linus Torvalds committed
443

444
445
446
447
	/* Can't register until after driver model init */
	if (unlikely(WARN_ON(!i2c_bus_type.p)))
		return -EAGAIN;

448
449
	mutex_init(&adap->bus_lock);
	mutex_init(&adap->clist_lock);
Linus Torvalds's avatar
Linus Torvalds committed
450
451
	INIT_LIST_HEAD(&adap->clients);

Jean Delvare's avatar
Jean Delvare committed
452
	mutex_lock(&core_lock);
453

Linus Torvalds's avatar
Linus Torvalds committed
454
455
456
457
	/* Add the adapter to the driver core.
	 * If the parent pointer is not set up,
	 * we add this adapter to the host bus.
	 */
458
	if (adap->dev.parent == NULL) {
Linus Torvalds's avatar
Linus Torvalds committed
459
		adap->dev.parent = &platform_bus;
460
461
		pr_debug("I2C adapter driver [%s] forgot to specify "
			 "physical device\n", adap->name);
462
	}
463
464
465
466
467

	/* Set default timeout to 1 second if not already set */
	if (adap->timeout == 0)
		adap->timeout = HZ;

468
	dev_set_name(&adap->dev, "i2c-%d", adap->nr);
Linus Torvalds's avatar
Linus Torvalds committed
469
	adap->dev.release = &i2c_adapter_dev_release;
Jean Delvare's avatar
Jean Delvare committed
470
	adap->dev.class = &i2c_adapter_class;
471
472
473
	res = device_register(&adap->dev);
	if (res)
		goto out_list;
Linus Torvalds's avatar
Linus Torvalds committed
474

475
476
	dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);

477
478
479
480
	/* create pre-declared device nodes for new-style drivers */
	if (adap->nr < __i2c_first_dynamic_bus_num)
		i2c_scan_static_board_info(adap);

481
	/* Notify drivers */
482
483
	dummy = bus_for_each_drv(&i2c_bus_type, NULL, adap,
				 i2c_do_add_adapter);
Linus Torvalds's avatar
Linus Torvalds committed
484
485

out_unlock:
Jean Delvare's avatar
Jean Delvare committed
486
	mutex_unlock(&core_lock);
Linus Torvalds's avatar
Linus Torvalds committed
487
	return res;
488
489
490
491

out_list:
	idr_remove(&i2c_adapter_idr, adap->nr);
	goto out_unlock;
Linus Torvalds's avatar
Linus Torvalds committed
492
493
}

494
495
496
/**
 * i2c_add_adapter - declare i2c adapter, use dynamic bus number
 * @adapter: the adapter to add
David Brownell's avatar
David Brownell committed
497
 * Context: can sleep
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
 *
 * This routine is used to declare an I2C adapter when its bus number
 * doesn't matter.  Examples: for I2C adapters dynamically added by
 * USB links or PCI plugin cards.
 *
 * When this returns zero, a new bus number was allocated and stored
 * in adap->nr, and the specified adapter became available for clients.
 * Otherwise, a negative errno value is returned.
 */
int i2c_add_adapter(struct i2c_adapter *adapter)
{
	int	id, res = 0;

retry:
	if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0)
		return -ENOMEM;

Jean Delvare's avatar
Jean Delvare committed
515
	mutex_lock(&core_lock);
516
517
518
	/* "above" here means "above or equal to", sigh */
	res = idr_get_new_above(&i2c_adapter_idr, adapter,
				__i2c_first_dynamic_bus_num, &id);
Jean Delvare's avatar
Jean Delvare committed
519
	mutex_unlock(&core_lock);
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534

	if (res < 0) {
		if (res == -EAGAIN)
			goto retry;
		return res;
	}

	adapter->nr = id;
	return i2c_register_adapter(adapter);
}
EXPORT_SYMBOL(i2c_add_adapter);

/**
 * i2c_add_numbered_adapter - declare i2c adapter, use static bus number
 * @adap: the adapter to register (with adap->nr initialized)
David Brownell's avatar
David Brownell committed
535
 * Context: can sleep
536
537
 *
 * This routine is used to declare an I2C adapter when its bus number
Randy Dunlap's avatar
Randy Dunlap committed
538
539
 * matters.  For example, use it for I2C adapters from system-on-chip CPUs,
 * or otherwise built in to the system's mainboard, and where i2c_board_info
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
 * is used to properly configure I2C devices.
 *
 * If no devices have pre-been declared for this bus, then be sure to
 * register the adapter before any dynamically allocated ones.  Otherwise
 * the required bus ID may not be available.
 *
 * When this returns zero, the specified adapter became available for
 * clients using the bus number provided in adap->nr.  Also, the table
 * of I2C devices pre-declared using i2c_register_board_info() is scanned,
 * and the appropriate driver model device nodes are created.  Otherwise, a
 * negative errno value is returned.
 */
int i2c_add_numbered_adapter(struct i2c_adapter *adap)
{
	int	id;
	int	status;

	if (adap->nr & ~MAX_ID_MASK)
		return -EINVAL;

retry:
	if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0)
		return -ENOMEM;

Jean Delvare's avatar
Jean Delvare committed
564
	mutex_lock(&core_lock);
565
566
567
568
569
570
571
572
	/* "above" here means "above or equal to", sigh;
	 * we need the "equal to" result to force the result
	 */
	status = idr_get_new_above(&i2c_adapter_idr, adap, adap->nr, &id);
	if (status == 0 && id != adap->nr) {
		status = -EBUSY;
		idr_remove(&i2c_adapter_idr, id);
	}
Jean Delvare's avatar
Jean Delvare committed
573
	mutex_unlock(&core_lock);
574
575
576
577
578
579
580
581
582
	if (status == -EAGAIN)
		goto retry;

	if (status == 0)
		status = i2c_register_adapter(adap);
	return status;
}
EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter);

583
584
585
586
static int i2c_do_del_adapter(struct device_driver *d, void *data)
{
	struct i2c_driver *driver = to_i2c_driver(d);
	struct i2c_adapter *adapter = data;
587
	struct i2c_client *client, *_n;
588
589
	int res;

590
591
	/* Remove the devices we created ourselves as the result of hardware
	 * probing (using a driver's detect method) */
592
593
594
595
596
597
598
599
600
	list_for_each_entry_safe(client, _n, &driver->clients, detected) {
		if (client->adapter == adapter) {
			dev_dbg(&adapter->dev, "Removing %s at 0x%x\n",
				client->name, client->addr);
			list_del(&client->detected);
			i2c_unregister_device(client);
		}
	}

601
602
603
604
605
606
607
608
609
	if (!driver->detach_adapter)
		return 0;
	res = driver->detach_adapter(adapter);
	if (res)
		dev_err(&adapter->dev, "detach_adapter failed (%d) "
			"for driver [%s]\n", res, driver->driver.name);
	return res;
}

David Brownell's avatar
David Brownell committed
610
611
612
613
614
615
616
617
/**
 * i2c_del_adapter - unregister I2C adapter
 * @adap: the adapter being unregistered
 * Context: can sleep
 *
 * This unregisters an I2C adapter which was previously registered
 * by @i2c_add_adapter or @i2c_add_numbered_adapter.
 */
Linus Torvalds's avatar
Linus Torvalds committed
618
619
int i2c_del_adapter(struct i2c_adapter *adap)
{
620
	struct i2c_client *client, *_n;
Linus Torvalds's avatar
Linus Torvalds committed
621
622
	int res = 0;

Jean Delvare's avatar
Jean Delvare committed
623
	mutex_lock(&core_lock);
Linus Torvalds's avatar
Linus Torvalds committed
624
625

	/* First make sure that this adapter was ever added */
626
	if (idr_find(&i2c_adapter_idr, adap->nr) != adap) {
627
628
		pr_debug("i2c-core: attempting to delete unregistered "
			 "adapter [%s]\n", adap->name);
Linus Torvalds's avatar
Linus Torvalds committed
629
630
631
632
		res = -EINVAL;
		goto out_unlock;
	}

633
634
635
636
637
	/* Tell drivers about this removal */
	res = bus_for_each_drv(&i2c_bus_type, NULL, adap,
			       i2c_do_del_adapter);
	if (res)
		goto out_unlock;
Linus Torvalds's avatar
Linus Torvalds committed
638
639

	/* detach any active clients. This must be done first, because
640
	 * it can fail; in which case we give up. */
641
	list_for_each_entry_safe_reverse(client, _n, &adap->clients, list) {
David Brownell's avatar
David Brownell committed
642
643
644
645
646
647
648
649
650
		struct i2c_driver	*driver;

		driver = client->driver;

		/* new style, follow standard driver model */
		if (!driver || is_newstyle_driver(driver)) {
			i2c_unregister_device(client);
			continue;
		}
Linus Torvalds's avatar
Linus Torvalds committed
651

David Brownell's avatar
David Brownell committed
652
653
		/* legacy drivers create and remove clients themselves */
		if ((res = driver->detach_client(client))) {
654
655
			dev_err(&adap->dev, "detach_client failed for client "
				"[%s] at address 0x%02x\n", client->name,
Linus Torvalds's avatar
Linus Torvalds committed
656
657
658
659
660
661
662
663
664
665
666
667
				client->addr);
			goto out_unlock;
		}
	}

	/* clean up the sysfs representation */
	init_completion(&adap->dev_released);
	device_unregister(&adap->dev);

	/* wait for sysfs to drop all references */
	wait_for_completion(&adap->dev_released);

668
	/* free bus id */
Linus Torvalds's avatar
Linus Torvalds committed
669
670
	idr_remove(&i2c_adapter_idr, adap->nr);

671
	dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
Linus Torvalds's avatar
Linus Torvalds committed
672

673
674
675
676
	/* Clear the device structure in case this adapter is ever going to be
	   added again */
	memset(&adap->dev, 0, sizeof(adap->dev));

Linus Torvalds's avatar
Linus Torvalds committed
677
 out_unlock:
Jean Delvare's avatar
Jean Delvare committed
678
	mutex_unlock(&core_lock);
Linus Torvalds's avatar
Linus Torvalds committed
679
680
	return res;
}
681
EXPORT_SYMBOL(i2c_del_adapter);
Linus Torvalds's avatar
Linus Torvalds committed
682
683


David Brownell's avatar
David Brownell committed
684
685
/* ------------------------------------------------------------------------- */

Dave Young's avatar
Dave Young committed
686
687
688
689
690
static int __attach_adapter(struct device *dev, void *data)
{
	struct i2c_adapter *adapter = to_i2c_adapter(dev);
	struct i2c_driver *driver = data;

691
692
693
694
695
	i2c_detect(adapter, driver);

	/* Legacy drivers scan i2c busses directly */
	if (driver->attach_adapter)
		driver->attach_adapter(adapter);
Dave Young's avatar
Dave Young committed
696
697
698
699

	return 0;
}

David Brownell's avatar
David Brownell committed
700
701
702
703
704
705
706
/*
 * An i2c_driver is used with one or more i2c_client (device) nodes to access
 * i2c slave chips, on a bus instance associated with some i2c_adapter.  There
 * are two models for binding the driver to its device:  "new style" drivers
 * follow the standard Linux driver model and just respond to probe() calls
 * issued if the driver core sees they match(); "legacy" drivers create device
 * nodes themselves.
Linus Torvalds's avatar
Linus Torvalds committed
707
708
 */

709
int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
Linus Torvalds's avatar
Linus Torvalds committed
710
{
711
	int res;
Linus Torvalds's avatar
Linus Torvalds committed
712

713
714
715
716
	/* Can't register until after driver model init */
	if (unlikely(WARN_ON(!i2c_bus_type.p)))
		return -EAGAIN;

David Brownell's avatar
David Brownell committed
717
	/* new style driver methods can't mix with legacy ones */
David Brownell's avatar
David Brownell committed
718
	if (is_newstyle_driver(driver)) {
719
		if (driver->detach_adapter || driver->detach_client) {
David Brownell's avatar
David Brownell committed
720
721
722
723
724
725
726
			printk(KERN_WARNING
					"i2c-core: driver [%s] is confused\n",
					driver->driver.name);
			return -EINVAL;
		}
	}

Linus Torvalds's avatar
Linus Torvalds committed
727
	/* add the driver to the list of i2c drivers in the driver core */
728
	driver->driver.owner = owner;
Linus Torvalds's avatar
Linus Torvalds committed
729
730
	driver->driver.bus = &i2c_bus_type;

731
732
733
	/* for new style drivers, when registration returns the driver core
	 * will have called probe() for all matching-but-unbound devices.
	 */
Linus Torvalds's avatar
Linus Torvalds committed
734
735
	res = driver_register(&driver->driver);
	if (res)
736
		return res;
David Brownell's avatar
David Brownell committed
737

Jean Delvare's avatar
Jean Delvare committed
738
	mutex_lock(&core_lock);
739

740
	pr_debug("i2c-core: driver [%s] registered\n", driver->driver.name);
Linus Torvalds's avatar
Linus Torvalds committed
741

742
743
	INIT_LIST_HEAD(&driver->clients);
	/* Walk the adapters that are already present */
744
745
	class_for_each_device(&i2c_adapter_class, NULL, driver,
			      __attach_adapter);
746

Dave Young's avatar
Dave Young committed
747
748
749
750
751
752
753
754
755
	mutex_unlock(&core_lock);
	return 0;
}
EXPORT_SYMBOL(i2c_register_driver);

static int __detach_adapter(struct device *dev, void *data)
{
	struct i2c_adapter *adapter = to_i2c_adapter(dev);
	struct i2c_driver *driver = data;
756
757
	struct i2c_client *client, *_n;

758
759
	/* Remove the devices we created ourselves as the result of hardware
	 * probing (using a driver's detect method) */
760
761
762
763
764
765
766
767
768
	list_for_each_entry_safe(client, _n, &driver->clients, detected) {
		dev_dbg(&adapter->dev, "Removing %s at 0x%x\n",
			client->name, client->addr);
		list_del(&client->detected);
		i2c_unregister_device(client);
	}

	if (is_newstyle_driver(driver))
		return 0;
Dave Young's avatar
Dave Young committed
769
770
771
772
773
774
775
776
777
778
779

	/* Have a look at each adapter, if clients of this driver are still
	 * attached. If so, detach them to be able to kill the driver
	 * afterwards.
	 */
	if (driver->detach_adapter) {
		if (driver->detach_adapter(adapter))
			dev_err(&adapter->dev,
				"detach_adapter failed for driver [%s]\n",
				driver->driver.name);
	} else {
780
		struct i2c_client *client, *_n;
Dave Young's avatar
Dave Young committed
781

782
		list_for_each_entry_safe(client, _n, &adapter->clients, list) {
Dave Young's avatar
Dave Young committed
783
784
785
786
787
788
789
790
791
			if (client->driver != driver)
				continue;
			dev_dbg(&adapter->dev,
				"detaching client [%s] at 0x%02x\n",
				client->name, client->addr);
			if (driver->detach_client(client))
				dev_err(&adapter->dev, "detach_client "
					"failed for client [%s] at 0x%02x\n",
					client->name, client->addr);
Linus Torvalds's avatar
Linus Torvalds committed
792
793
794
		}
	}

795
	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
796
797
}

David Brownell's avatar
David Brownell committed
798
799
800
/**
 * i2c_del_driver - unregister I2C driver
 * @driver: the driver being unregistered
David Brownell's avatar
David Brownell committed
801
 * Context: can sleep
David Brownell's avatar
David Brownell committed
802
 */
803
void i2c_del_driver(struct i2c_driver *driver)
Linus Torvalds's avatar
Linus Torvalds committed
804
{
Jean Delvare's avatar
Jean Delvare committed
805
	mutex_lock(&core_lock);
Linus Torvalds's avatar
Linus Torvalds committed
806

807
808
	class_for_each_device(&i2c_adapter_class, NULL, driver,
			      __detach_adapter);
Linus Torvalds's avatar
Linus Torvalds committed
809
810

	driver_unregister(&driver->driver);
811
	pr_debug("i2c-core: driver [%s] unregistered\n", driver->driver.name);
Linus Torvalds's avatar
Linus Torvalds committed
812

Jean Delvare's avatar
Jean Delvare committed
813
	mutex_unlock(&core_lock);
Linus Torvalds's avatar
Linus Torvalds committed
814
}
815
EXPORT_SYMBOL(i2c_del_driver);
Linus Torvalds's avatar
Linus Torvalds committed
816

David Brownell's avatar
David Brownell committed
817
818
/* ------------------------------------------------------------------------- */

819
static int __i2c_check_addr(struct device *dev, void *addrp)
Linus Torvalds's avatar
Linus Torvalds committed
820
{
821
822
	struct i2c_client	*client = i2c_verify_client(dev);
	int			addr = *(int *)addrp;
Linus Torvalds's avatar
Linus Torvalds committed
823

824
825
	if (client && client->addr == addr)
		return -EBUSY;
Linus Torvalds's avatar
Linus Torvalds committed
826
827
828
	return 0;
}

Jean Delvare's avatar
Jean Delvare committed
829
static int i2c_check_addr(struct i2c_adapter *adapter, int addr)
Linus Torvalds's avatar
Linus Torvalds committed
830
{
831
	return device_for_each_child(&adapter->dev, &addr, __i2c_check_addr);
Linus Torvalds's avatar
Linus Torvalds committed
832
833
834
835
836
}

int i2c_attach_client(struct i2c_client *client)
{
	struct i2c_adapter *adapter = client->adapter;
837
838
839
840
841
842
	int res;

	/* Check for address business */
	res = i2c_check_addr(adapter, client->addr);
	if (res)
		return res;
Linus Torvalds's avatar
Linus Torvalds committed
843
844
845

	client->dev.parent = &client->adapter->dev;
	client->dev.bus = &i2c_bus_type;
846
847
848
849

	if (client->driver)
		client->dev.driver = &client->driver->driver;

850
	if (client->driver && !is_newstyle_driver(client->driver)) {
851
		client->dev.release = i2c_client_release;
852
		dev_set_uevent_suppress(&client->dev, 1);
853
	} else
854
		client->dev.release = i2c_client_dev_release;
David Brownell's avatar
David Brownell committed
855

856
857
	dev_set_name(&client->dev, "%d-%04x", i2c_adapter_id(adapter),
		     client->addr);
858
859
	res = device_register(&client->dev);
	if (res)
860
861
862
863
		goto out_err;

	mutex_lock(&adapter->clist_lock);
	list_add_tail(&client->list, &adapter->clients);
864
	mutex_unlock(&adapter->clist_lock);
865

866
	dev_dbg(&adapter->dev, "client [%s] registered with bus id %s\n",
867
		client->name, dev_name(&client->dev));
868

869
870
871
872
873
874
875
876
877
	if (adapter->client_register)  {
		if (adapter->client_register(client)) {
			dev_dbg(&adapter->dev, "client_register "
				"failed for client [%s] at 0x%02x\n",
				client->name, client->addr);
		}
	}

	return 0;
878

879
out_err:
880
881
	dev_err(&adapter->dev, "Failed to attach i2c client %s at 0x%02x "
		"(%d)\n", client->name, client->addr, res);
882
	return res;
Linus Torvalds's avatar
Linus Torvalds committed
883
}
884
EXPORT_SYMBOL(i2c_attach_client);
Linus Torvalds's avatar
Linus Torvalds committed
885
886
887
888
889

int i2c_detach_client(struct i2c_client *client)
{
	struct i2c_adapter *adapter = client->adapter;
	int res = 0;
David Brownell's avatar
David Brownell committed
890

Linus Torvalds's avatar
Linus Torvalds committed
891
892
893
894
	if (adapter->client_unregister)  {
		res = adapter->client_unregister(client);
		if (res) {
			dev_err(&client->dev,
895
896
				"client_unregister [%s] failed, "
				"client not detached\n", client->name);
Linus Torvalds's avatar
Linus Torvalds committed
897
898
899
900
			goto out;
		}
	}

901
	mutex_lock(&adapter->clist_lock);
Linus Torvalds's avatar
Linus Torvalds committed
902
	list_del(&client->list);
903
904
	mutex_unlock(&adapter->clist_lock);

Linus Torvalds's avatar
Linus Torvalds committed
905
906
907
908
909
910
911
	init_completion(&client->released);
	device_unregister(&client->dev);
	wait_for_completion(&client->released);

 out:
	return res;
}
912
EXPORT_SYMBOL(i2c_detach_client);
Linus Torvalds's avatar
Linus Torvalds committed
913

914
915
916
917
918
919
920
921
922
923
924
925
/**
 * i2c_use_client - increments the reference count of the i2c client structure
 * @client: the client being referenced
 *
 * Each live reference to a client should be refcounted. The driver model does
 * that automatically as part of driver binding, so that most drivers don't
 * need to do this explicitly: they hold a reference until they're unbound
 * from the device.
 *
 * A pointer to the client with the incremented reference counter is returned.
 */
struct i2c_client *i2c_use_client(struct i2c_client *client)
Linus Torvalds's avatar
Linus Torvalds committed
926
{
927
928
929
	if (client && get_device(&client->dev))
		return client;
	return NULL;
Linus Torvalds's avatar
Linus Torvalds committed
930
}
931
EXPORT_SYMBOL(i2c_use_client);
Linus Torvalds's avatar
Linus Torvalds committed
932

933
934
935
936
937
938
939
/**
 * i2c_release_client - release a use of the i2c client structure
 * @client: the client being no longer referenced
 *
 * Must be called when a user of a client is finished with it.
 */
void i2c_release_client(struct i2c_client *client)
Linus Torvalds's avatar
Linus Torvalds committed
940
{
941
942
	if (client)
		put_device(&client->dev);
Linus Torvalds's avatar
Linus Torvalds committed
943
}
944
EXPORT_SYMBOL(i2c_release_client);
Linus Torvalds's avatar
Linus Torvalds committed
945

946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
struct i2c_cmd_arg {
	unsigned	cmd;
	void		*arg;
};

static int i2c_cmd(struct device *dev, void *_arg)
{
	struct i2c_client	*client = i2c_verify_client(dev);
	struct i2c_cmd_arg	*arg = _arg;

	if (client && client->driver && client->driver->command)
		client->driver->command(client, arg->cmd, arg->arg);
	return 0;
}

Linus Torvalds's avatar
Linus Torvalds committed
961
962
void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
{
963
	struct i2c_cmd_arg	cmd_arg;
Linus Torvalds's avatar
Linus Torvalds committed
964

965
966
967
	cmd_arg.cmd = cmd;
	cmd_arg.arg = arg;
	device_for_each_child(&adap->dev, &cmd_arg, i2c_cmd);
Linus Torvalds's avatar
Linus Torvalds committed
968
}
969
EXPORT_SYMBOL(i2c_clients_command);
Linus Torvalds's avatar
Linus Torvalds committed
970
971
972
973
974
975
976
977

static int __init i2c_init(void)
{
	int retval;

	retval = bus_register(&i2c_bus_type);
	if (retval)
		return retval;
978
979
980
981
982
983
984
985
986
987
988
989
990
	retval = class_register(&i2c_adapter_class);
	if (retval)
		goto bus_err;
	retval = i2c_add_driver(&dummy_driver);
	if (retval)
		goto class_err;
	return 0;

class_err:
	class_unregister(&i2c_adapter_class);
bus_err:
	bus_unregister(&i2c_bus_type);
	return retval;
Linus Torvalds's avatar
Linus Torvalds committed
991
992
993
994
}

static void __exit i2c_exit(void)
{
995
	i2c_del_driver(&dummy_driver);
Linus Torvalds's avatar
Linus Torvalds committed
996
997
998
999
	class_unregister(&i2c_adapter_class);
	bus_unregister(&i2c_bus_type);
}

1000
1001
1002
1003
/* We must initialize early, because some subsystems register i2c drivers
 * in subsys_initcall() code, but are linked (and initialized) before i2c.
 */
postcore_initcall(i2c_init);
Linus Torvalds's avatar
Linus Torvalds committed
1004
1005
1006
1007
1008
1009
1010
module_exit(i2c_exit);

/* ----------------------------------------------------
 * the functional interface to the i2c busses.
 * ----------------------------------------------------
 */

1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
/**
 * i2c_transfer - execute a single or combined I2C message
 * @adap: Handle to I2C bus
 * @msgs: One or more messages to execute before STOP is issued to
 *	terminate the operation; each message begins with a START.
 * @num: Number of messages to be executed.
 *
 * Returns negative errno, else the number of messages executed.
 *
 * Note that there is no requirement that each message be sent to
 * the same slave address, although that is the most common model.
 */
Zhenwen Xu's avatar
Zhenwen Xu committed
1023
int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
Linus Torvalds's avatar
Linus Torvalds committed
1024
1025
1026
{
	int ret;

1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
	/* REVISIT the fault reporting model here is weak:
	 *
	 *  - When we get an error after receiving N bytes from a slave,
	 *    there is no way to report "N".
	 *
	 *  - When we get a NAK after transmitting N bytes to a slave,
	 *    there is no way to report "N" ... or to let the master
	 *    continue executing the rest of this combined message, if
	 *    that's the appropriate response.
	 *
	 *  - When for example "num" is two and we successfully complete
	 *    the first message but get an error part way through the
	 *    second, it's unclear whether that should be reported as
	 *    one (discarding status on the second message) or errno
	 *    (discarding status on the first one).
	 */

Linus Torvalds's avatar
Linus Torvalds committed
1044
1045
1046
1047
	if (adap->algo->master_xfer) {
#ifdef DEBUG
		for (ret = 0; ret < num; ret++) {
			dev_dbg(&adap->dev, "master_xfer[%d] %c, addr=0x%02x, "
1048
1049
1050
				"len=%d%s\n", ret, (msgs[ret].flags & I2C_M_RD)
				? 'R' : 'W', msgs[ret].addr, msgs[ret].len,
				(msgs[ret].flags & I2C_M_RECV_LEN) ? "+" : "");
Linus Torvalds's avatar
Linus Torvalds committed
1051
1052
1053
		}
#endif

1054
1055
1056
1057
1058
1059
1060
1061
1062
		if (in_atomic() || irqs_disabled()) {
			ret = mutex_trylock(&adap->bus_lock);
			if (!ret)
				/* I2C activity is ongoing. */
				return -EAGAIN;
		} else {
			mutex_lock_nested(&adap->bus_lock, adap->level);
		}

Linus Torvalds's avatar
Linus Torvalds committed
1063
		ret = adap->algo->master_xfer(adap,msgs,num);
1064
		mutex_unlock(&adap->bus_lock);
Linus Torvalds's avatar
Linus Torvalds committed
1065
1066
1067
1068

		return ret;
	} else {
		dev_dbg(&adap->dev, "I2C level transfers not supported\n");
1069
		return -EOPNOTSUPP;
Linus Torvalds's avatar
Linus Torvalds committed
1070
1071
	}
}
1072
EXPORT_SYMBOL(i2c_transfer);
Linus Torvalds's avatar
Linus Torvalds committed
1073

1074
1075
1076
1077
1078
1079
1080
1081
/**
 * i2c_master_send - issue a single I2C message in master transmit mode
 * @client: Handle to slave device
 * @buf: Data that will be written to the slave
 * @count: How many bytes to write
 *
 * Returns negative errno, or else the number of bytes written.
 */
Linus Torvalds's avatar
Linus Torvalds committed
1082
1083
1084
1085
1086
1087
int i2c_master_send(struct i2c_client *client,const char *buf ,int count)
{
	int ret;
	struct i2c_adapter *adap=client->adapter;
	struct i2c_msg msg;

1088
1089
1090
1091
	msg.addr = client->addr;
	msg.flags = client->flags & I2C_M_TEN;
	msg.len = count;
	msg.buf = (char *)buf;
David Brownell's avatar
David Brownell committed
1092

1093
	ret = i2c_transfer(adap, &msg, 1);
Linus Torvalds's avatar
Linus Torvalds committed
1094

1095
1096
1097
	/* If everything went ok (i.e. 1 msg transmitted), return #bytes
	   transmitted, else error code. */
	return (ret == 1) ? count : ret;
Linus Torvalds's avatar
Linus Torvalds committed
1098
}
1099
EXPORT_SYMBOL(i2c_master_send);
Linus Torvalds's avatar
Linus Torvalds committed
1100

1101
1102
1103
1104
1105
1106
1107
1108
/**
 * i2c_master_recv - issue a single I2C message in master receive mode
 * @client: Handle to slave device
 * @buf: Where to store data read from slave
 * @count: How many bytes to read
 *
 * Returns negative errno, or else the number of bytes read.
 */
Linus Torvalds's avatar
Linus Torvalds committed
1109
1110
1111
1112
1113
int i2c_master_recv(struct i2c_client *client, char *buf ,int count)
{
	struct i2c_adapter *adap=client->adapter;
	struct i2c_msg msg;
	int ret;
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125

	msg.addr = client->addr;
	msg.flags = client->flags & I2C_M_TEN;
	msg.flags |= I2C_M_RD;
	msg.len = count;
	msg.buf = buf;

	ret = i2c_transfer(adap, &msg, 1);

	/* If everything went ok (i.e. 1 msg transmitted), return #bytes
	   transmitted, else error code. */
	return (ret == 1) ? count : ret;
Linus Torvalds's avatar
Linus Torvalds committed
1126
}
1127
EXPORT_SYMBOL(i2c_master_recv);
Linus Torvalds's avatar
Linus Torvalds committed
1128
1129
1130
1131
1132
1133

/* ----------------------------------------------------
 * the i2c address scanning function
 * Will not work for 10-bit addresses!
 * ----------------------------------------------------
 */
Jean Delvare's avatar
Jean Delvare committed
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
static int i2c_probe_address(struct i2c_adapter *adapter, int addr, int kind,
			     int (*found_proc) (struct i2c_adapter *, int, int))
{
	int err;

	/* Make sure the address is valid */
	if (addr < 0x03 || addr > 0x77) {
		dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
			 addr);
		return -EINVAL;
1144
1145
	}

Jean Delvare's avatar
Jean Delvare committed
1146
1147
1148
1149
1150
	/* Skip if already in use */
	if (i2c_check_addr(adapter, addr))
		return 0;

	/* Make sure there is something at this address, unless forced */
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
	if (kind < 0) {
		if (i2c_smbus_xfer(adapter, addr, 0, 0, 0,
				   I2C_SMBUS_QUICK, NULL) < 0)
			return 0;

		/* prevent 24RF08 corruption */
		if ((addr & ~0x0f) == 0x50)
			i2c_smbus_xfer(adapter, addr, 0, 0, 0,
				       I2C_SMBUS_QUICK, NULL);
	}
Jean Delvare's avatar
Jean Delvare committed
1161
1162
1163
1164
1165
1166

	/* Finally call the custom detection function */
	err = found_proc(adapter, addr, kind);
	/* -ENODEV can be returned if there is a chip at the given address
	   but it isn't supported by this chip driver. We catch it here as
	   this isn't an error. */
1167
1168
1169
1170
1171
1172
1173
	if (err == -ENODEV)
		err = 0;

	if (err)
		dev_warn(&adapter->dev, "Client creation failed at 0x%x (%d)\n",
			 addr, err);
	return err;
1174
1175
}

Linus Torvalds's avatar
Linus Torvalds committed
1176
int i2c_probe(struct i2c_adapter *adapter,
1177
	      const struct i2c_client_address_data *address_data,
Linus Torvalds's avatar
Linus Torvalds committed
1178
1179
	      int (*found_proc) (struct i2c_adapter *, int, int))
{
Jean Delvare's avatar
Jean Delvare committed
1180
	int i, err;
Linus Torvalds's avatar
Linus Torvalds committed
1181
1182
	int adap_id = i2c_adapter_id(adapter);

Jean Delvare's avatar
Jean Delvare committed
1183
1184
1185
	/* Force entries are done first, and are not affected by ignore
	   entries */
	if (address_data->forces) {
1186
		const unsigned short * const *forces = address_data->forces;
Jean Delvare's avatar
Jean Delvare committed
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
		int kind;

		for (kind = 0; forces[kind]; kind++) {
			for (i = 0; forces[kind][i] != I2C_CLIENT_END;
			     i += 2) {
				if (forces[kind][i] == adap_id
				 || forces[kind][i] == ANY_I2C_BUS) {
					dev_dbg(&adapter->dev, "found force "
						"parameter for adapter %d, "
						"addr 0x%02x, kind %d\n",
						adap_id, forces[kind][i + 1],
						kind);
					err = i2c_probe_address(adapter,
						forces[kind][i + 1],
						kind, found_proc);
					if (err)
						return err;
				}
Linus Torvalds's avatar
Linus Torvalds committed
1205
1206
			}
		}
Jean Delvare's avatar
Jean Delvare committed
1207
	}
Linus Torvalds's avatar
Linus Torvalds committed
1208

1209
1210
1211
1212
	/* Stop here if we can't use SMBUS_QUICK */
	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_QUICK)) {
		if (address_data->probe[0] == I2C_CLIENT_END
		 && address_data->normal_i2c[0] == I2C_CLIENT_END)
David Brownell's avatar
David Brownell committed
1213
			return 0;
1214

1215
1216
		dev_dbg(&adapter->dev, "SMBus Quick command not supported, "
			"can't probe for chips\n");
1217
		return -EOPNOTSUPP;
1218
1219
	}

Jean Delvare's avatar
Jean Delvare committed
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
	/* Probe entries are done second, and are not affected by ignore
	   entries either */
	for (i = 0; address_data->probe[i] != I2C_CLIENT_END; i += 2) {
		if (address_data->probe[i] == adap_id
		 || address_data->probe[i] == ANY_I2C_BUS) {
			dev_dbg(&adapter->dev, "found probe parameter for "
				"adapter %d, addr 0x%02x\n", adap_id,
				address_data->probe[i + 1]);
			err = i2c_probe_address(adapter,
						address_data->probe[i + 1],
						-1, found_proc);
			if (err)
				return err;
Linus Torvalds's avatar
Linus Torvalds committed
1233
		}
Jean Delvare's avatar
Jean Delvare committed
1234
	}
Linus Torvalds's avatar
Linus Torvalds committed
1235

Jean Delvare's avatar
Jean Delvare committed
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
	/* Normal entries are done last, unless shadowed by an ignore entry */
	for (i = 0; address_data->normal_i2c[i] != I2C_CLIENT_END; i += 1) {
		int j, ignore;

		ignore = 0;
		for (j = 0; address_data->ignore[j] != I2C_CLIENT_END;
		     j += 2) {
			if ((address_data->ignore[j] == adap_id ||
			     address_data->ignore[j] == ANY_I2C_BUS)
			 && address_data->ignore[j + 1]
			    == address_data->normal_i2c[i]) {
				dev_dbg(&adapter->dev, "found ignore "
					"parameter for adapter %d, "
					"addr 0x%02x\n", adap_id,
					address_data->ignore[j + 1]);
1251
1252
				ignore = 1;
				break;
Linus Torvalds's avatar
Linus Torvalds committed
1253
1254
			}
		}
Jean Delvare's avatar
Jean Delvare committed
1255
		if (ignore)
Linus Torvalds's avatar
Linus Torvalds committed
1256
1257
			continue;

Jean Delvare's avatar
Jean Delvare committed
1258
1259
1260
1261
1262
1263
1264
		dev_dbg(&adapter->dev, "found normal entry for adapter %d, "
			"addr 0x%02x\n", adap_id,
			address_data->normal_i2c[i]);
		err = i2c_probe_address(adapter, address_data->normal_i2c[i],
					-1, found_proc);
		if (err)
			return err;
Linus Torvalds's avatar
Linus Torvalds committed
1265
	}
Jean Delvare's avatar
Jean Delvare committed
1266

Linus Torvalds's avatar
Linus Torvalds committed
1267
1268
	return 0;
}
1269
EXPORT_SYMBOL(i2c_probe);
Linus Torvalds's avatar
Linus Torvalds committed
1270

1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292