aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Brownell <david-b@pacbell.net>2007-05-01 17:26:31 -0400
committerJean Delvare <khali@hyperion.delvare>2007-05-01 17:26:31 -0400
commit6e13e641841833cc2aa5baefe89bb04bc388801b (patch)
tree600a1a87e2ae15afef01487428ee6c994c54877e
parent9c1600eda42e52796f49b36cf15b9debcfd09bea (diff)
i2c: Add i2c_add_numbered_adapter()
This adds a call, i2c_add_numbered_adapter(), registering an I2C adapter with a specific bus number and then creating I2C device nodes for any pre-declared devices on that bus. It builds on previous patches adding I2C probe() and remove() support, and that pre-declaration of devices. This completes the core support for "new style" I2C device drivers. Those follow the standard driver model for binding devices to drivers (using probe and remove methods) rather than a legacy model (where the driver tries to autoconfigure each bus, and registers devices itself). Signed-off-by: David Brownell <dbrownell@users.sourceforge.net> Signed-off-by: Jean Delvare <khali@linux-fr.org>
-rw-r--r--drivers/i2c/i2c-core.c126
-rw-r--r--include/linux/i2c.h1
2 files changed, 101 insertions, 26 deletions
diff --git a/drivers/i2c/i2c-core.c b/drivers/i2c/i2c-core.c
index a7dcdc69b9e7..c50229bd9f85 100644
--- a/drivers/i2c/i2c-core.c
+++ b/drivers/i2c/i2c-core.c
@@ -320,38 +320,19 @@ static void i2c_scan_static_board_info(struct i2c_adapter *adapter)
320 mutex_unlock(&__i2c_board_lock); 320 mutex_unlock(&__i2c_board_lock);
321} 321}
322 322
323 323static int i2c_register_adapter(struct i2c_adapter *adap)
324/* -----
325 * i2c_add_adapter is called from within the algorithm layer,
326 * when a new hw adapter registers. A new device is register to be
327 * available for clients.
328 */
329int i2c_add_adapter(struct i2c_adapter *adap)
330{ 324{
331 int id, res = 0; 325 int res = 0;
332 struct list_head *item; 326 struct list_head *item;
333 struct i2c_driver *driver; 327 struct i2c_driver *driver;
334 328
335 mutex_lock(&core_lists);
336
337 if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0) {
338 res = -ENOMEM;
339 goto out_unlock;
340 }
341
342 res = idr_get_new(&i2c_adapter_idr, adap, &id);
343 if (res < 0) {
344 if (res == -EAGAIN)
345 res = -ENOMEM;
346 goto out_unlock;
347 }
348
349 adap->nr = id & MAX_ID_MASK;
350 mutex_init(&adap->bus_lock); 329 mutex_init(&adap->bus_lock);
351 mutex_init(&adap->clist_lock); 330 mutex_init(&adap->clist_lock);
352 list_add_tail(&adap->list,&adapters);
353 INIT_LIST_HEAD(&adap->clients); 331 INIT_LIST_HEAD(&adap->clients);
354 332
333 mutex_lock(&core_lists);
334 list_add_tail(&adap->list, &adapters);
335
355 /* Add the adapter to the driver core. 336 /* Add the adapter to the driver core.
356 * If the parent pointer is not set up, 337 * If the parent pointer is not set up,
357 * we add this adapter to the host bus. 338 * we add this adapter to the host bus.
@@ -370,6 +351,10 @@ int i2c_add_adapter(struct i2c_adapter *adap)
370 351
371 dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name); 352 dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
372 353
354 /* create pre-declared device nodes for new-style drivers */
355 if (adap->nr < __i2c_first_dynamic_bus_num)
356 i2c_scan_static_board_info(adap);
357
373 /* let legacy drivers scan this bus for matching devices */ 358 /* let legacy drivers scan this bus for matching devices */
374 list_for_each(item,&drivers) { 359 list_for_each(item,&drivers) {
375 driver = list_entry(item, struct i2c_driver, list); 360 driver = list_entry(item, struct i2c_driver, list);
@@ -388,6 +373,93 @@ out_list:
388 goto out_unlock; 373 goto out_unlock;
389} 374}
390 375
376/**
377 * i2c_add_adapter - declare i2c adapter, use dynamic bus number
378 * @adapter: the adapter to add
379 *
380 * This routine is used to declare an I2C adapter when its bus number
381 * doesn't matter. Examples: for I2C adapters dynamically added by
382 * USB links or PCI plugin cards.
383 *
384 * When this returns zero, a new bus number was allocated and stored
385 * in adap->nr, and the specified adapter became available for clients.
386 * Otherwise, a negative errno value is returned.
387 */
388int i2c_add_adapter(struct i2c_adapter *adapter)
389{
390 int id, res = 0;
391
392retry:
393 if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0)
394 return -ENOMEM;
395
396 mutex_lock(&core_lists);
397 /* "above" here means "above or equal to", sigh */
398 res = idr_get_new_above(&i2c_adapter_idr, adapter,
399 __i2c_first_dynamic_bus_num, &id);
400 mutex_unlock(&core_lists);
401
402 if (res < 0) {
403 if (res == -EAGAIN)
404 goto retry;
405 return res;
406 }
407
408 adapter->nr = id;
409 return i2c_register_adapter(adapter);
410}
411EXPORT_SYMBOL(i2c_add_adapter);
412
413/**
414 * i2c_add_numbered_adapter - declare i2c adapter, use static bus number
415 * @adap: the adapter to register (with adap->nr initialized)
416 *
417 * This routine is used to declare an I2C adapter when its bus number
418 * matters. Example: for I2C adapters from system-on-chip CPUs, or
419 * otherwise built in to the system's mainboard, and where i2c_board_info
420 * is used to properly configure I2C devices.
421 *
422 * If no devices have pre-been declared for this bus, then be sure to
423 * register the adapter before any dynamically allocated ones. Otherwise
424 * the required bus ID may not be available.
425 *
426 * When this returns zero, the specified adapter became available for
427 * clients using the bus number provided in adap->nr. Also, the table
428 * of I2C devices pre-declared using i2c_register_board_info() is scanned,
429 * and the appropriate driver model device nodes are created. Otherwise, a
430 * negative errno value is returned.
431 */
432int i2c_add_numbered_adapter(struct i2c_adapter *adap)
433{
434 int id;
435 int status;
436
437 if (adap->nr & ~MAX_ID_MASK)
438 return -EINVAL;
439
440retry:
441 if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0)
442 return -ENOMEM;
443
444 mutex_lock(&core_lists);
445 /* "above" here means "above or equal to", sigh;
446 * we need the "equal to" result to force the result
447 */
448 status = idr_get_new_above(&i2c_adapter_idr, adap, adap->nr, &id);
449 if (status == 0 && id != adap->nr) {
450 status = -EBUSY;
451 idr_remove(&i2c_adapter_idr, id);
452 }
453 mutex_unlock(&core_lists);
454 if (status == -EAGAIN)
455 goto retry;
456
457 if (status == 0)
458 status = i2c_register_adapter(adap);
459 return status;
460}
461EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter);
462
391int i2c_del_adapter(struct i2c_adapter *adap) 463int i2c_del_adapter(struct i2c_adapter *adap)
392{ 464{
393 struct list_head *item, *_n; 465 struct list_head *item, *_n;
@@ -452,7 +524,7 @@ int i2c_del_adapter(struct i2c_adapter *adap)
452 /* wait for sysfs to drop all references */ 524 /* wait for sysfs to drop all references */
453 wait_for_completion(&adap->dev_released); 525 wait_for_completion(&adap->dev_released);
454 526
455 /* free dynamically allocated bus id */ 527 /* free bus id */
456 idr_remove(&i2c_adapter_idr, adap->nr); 528 idr_remove(&i2c_adapter_idr, adap->nr);
457 529
458 dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name); 530 dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
@@ -493,6 +565,9 @@ int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
493 driver->driver.owner = owner; 565 driver->driver.owner = owner;
494 driver->driver.bus = &i2c_bus_type; 566 driver->driver.bus = &i2c_bus_type;
495 567
568 /* for new style drivers, when registration returns the driver core
569 * will have called probe() for all matching-but-unbound devices.
570 */
496 res = driver_register(&driver->driver); 571 res = driver_register(&driver->driver);
497 if (res) 572 if (res)
498 return res; 573 return res;
@@ -1377,7 +1452,6 @@ EXPORT_SYMBOL_GPL(i2c_adapter_dev_release);
1377EXPORT_SYMBOL_GPL(i2c_adapter_class); 1452EXPORT_SYMBOL_GPL(i2c_adapter_class);
1378EXPORT_SYMBOL_GPL(i2c_bus_type); 1453EXPORT_SYMBOL_GPL(i2c_bus_type);
1379 1454
1380EXPORT_SYMBOL(i2c_add_adapter);
1381EXPORT_SYMBOL(i2c_del_adapter); 1455EXPORT_SYMBOL(i2c_del_adapter);
1382EXPORT_SYMBOL(i2c_del_driver); 1456EXPORT_SYMBOL(i2c_del_driver);
1383EXPORT_SYMBOL(i2c_attach_client); 1457EXPORT_SYMBOL(i2c_attach_client);
diff --git a/include/linux/i2c.h b/include/linux/i2c.h
index 382a43bf3ad5..36d6814a6df4 100644
--- a/include/linux/i2c.h
+++ b/include/linux/i2c.h
@@ -363,6 +363,7 @@ struct i2c_client_address_data {
363 */ 363 */
364extern int i2c_add_adapter(struct i2c_adapter *); 364extern int i2c_add_adapter(struct i2c_adapter *);
365extern int i2c_del_adapter(struct i2c_adapter *); 365extern int i2c_del_adapter(struct i2c_adapter *);
366extern int i2c_add_numbered_adapter(struct i2c_adapter *);
366 367
367extern int i2c_register_driver(struct module *, struct i2c_driver *); 368extern int i2c_register_driver(struct module *, struct i2c_driver *);
368extern int i2c_del_driver(struct i2c_driver *); 369extern int i2c_del_driver(struct i2c_driver *);