spi: enable spi_board_info to be registered after spi_master

Currently spi_register_board_info() has to be called before its related
spi_master be registered, otherwise these board info will be just ignored.

This patch will remove this order limit, it adds a global spi master list
like the existing global board info listr. Whenever a board info or a
spi_master is registered, the spi master list or board info list
will be scanned, and a new spi device will be created if there is a
master-board info match.

Signed-off-by: Feng Tang <feng.tang@intel.com>
Signed-off-by: Grant Likely <grant.likely@secretlab.ca>

authored by Feng Tang and committed by Grant Likely 2b9603a0 d4429f60

+50 -41
+47 -41
drivers/spi/spi.c
··· 29 29 #include <linux/spi/spi.h> 30 30 #include <linux/of_spi.h> 31 31 32 - 33 - /* SPI bustype and spi_master class are registered after board init code 34 - * provides the SPI device tables, ensuring that both are present by the 35 - * time controller driver registration causes spi_devices to "enumerate". 36 - */ 37 32 static void spidev_release(struct device *dev) 38 33 { 39 34 struct spi_device *spi = to_spi_device(dev); ··· 197 202 198 203 struct boardinfo { 199 204 struct list_head list; 200 - unsigned n_board_info; 201 - struct spi_board_info board_info[0]; 205 + struct spi_board_info board_info; 202 206 }; 203 207 204 208 static LIST_HEAD(board_list); 209 + static LIST_HEAD(spi_master_list); 210 + 211 + /* 212 + * Used to protect add/del opertion for board_info list and 213 + * spi_master list, and their matching process 214 + */ 205 215 static DEFINE_MUTEX(board_lock); 206 216 207 217 /** ··· 371 371 } 372 372 EXPORT_SYMBOL_GPL(spi_new_device); 373 373 374 + static void spi_match_master_to_boardinfo(struct spi_master *master, 375 + struct spi_board_info *bi) 376 + { 377 + struct spi_device *dev; 378 + 379 + if (master->bus_num != bi->bus_num) 380 + return; 381 + 382 + dev = spi_new_device(master, bi); 383 + if (!dev) 384 + dev_err(master->dev.parent, "can't create new device for %s\n", 385 + bi->modalias); 386 + } 387 + 374 388 /** 375 389 * spi_register_board_info - register SPI devices for a given board 376 390 * @info: array of chip descriptors ··· 407 393 int __init 408 394 spi_register_board_info(struct spi_board_info const *info, unsigned n) 409 395 { 410 - struct boardinfo *bi; 396 + struct boardinfo *bi; 397 + int i; 411 398 412 - bi = kmalloc(sizeof(*bi) + n * sizeof *info, GFP_KERNEL); 399 + bi = kzalloc(n * sizeof(*bi), GFP_KERNEL); 413 400 if (!bi) 414 401 return -ENOMEM; 415 - bi->n_board_info = n; 416 - memcpy(bi->board_info, info, n * sizeof *info); 417 402 418 - mutex_lock(&board_lock); 419 - list_add_tail(&bi->list, &board_list); 420 - mutex_unlock(&board_lock); 421 - return 0; 422 - } 403 + for (i = 0; i < n; i++, bi++, info++) { 404 + struct spi_master *master; 423 405 424 - /* FIXME someone should add support for a __setup("spi", ...) that 425 - * creates board info from kernel command lines 426 - */ 427 - 428 - static void scan_boardinfo(struct spi_master *master) 429 - { 430 - struct boardinfo *bi; 431 - 432 - mutex_lock(&board_lock); 433 - list_for_each_entry(bi, &board_list, list) { 434 - struct spi_board_info *chip = bi->board_info; 435 - unsigned n; 436 - 437 - for (n = bi->n_board_info; n > 0; n--, chip++) { 438 - if (chip->bus_num != master->bus_num) 439 - continue; 440 - /* NOTE: this relies on spi_new_device to 441 - * issue diagnostics when given bogus inputs 442 - */ 443 - (void) spi_new_device(master, chip); 444 - } 406 + memcpy(&bi->board_info, info, sizeof(*info)); 407 + mutex_lock(&board_lock); 408 + list_add_tail(&bi->list, &board_list); 409 + list_for_each_entry(master, &spi_master_list, list) 410 + spi_match_master_to_boardinfo(master, &bi->board_info); 411 + mutex_unlock(&board_lock); 445 412 } 446 - mutex_unlock(&board_lock); 413 + 414 + return 0; 447 415 } 448 416 449 417 /*-------------------------------------------------------------------------*/ ··· 508 512 { 509 513 static atomic_t dyn_bus_id = ATOMIC_INIT((1<<15) - 1); 510 514 struct device *dev = master->dev.parent; 515 + struct boardinfo *bi; 511 516 int status = -ENODEV; 512 517 int dynamic = 0; 513 518 ··· 544 547 dev_dbg(dev, "registered master %s%s\n", dev_name(&master->dev), 545 548 dynamic ? " (dynamic)" : ""); 546 549 547 - /* populate children from any spi device tables */ 548 - scan_boardinfo(master); 550 + mutex_lock(&board_lock); 551 + list_add_tail(&master->list, &spi_master_list); 552 + list_for_each_entry(bi, &board_list, list) 553 + spi_match_master_to_boardinfo(master, &bi->board_info); 554 + mutex_unlock(&board_lock); 555 + 549 556 status = 0; 550 557 551 558 /* Register devices from the device tree */ ··· 580 579 { 581 580 int dummy; 582 581 583 - dummy = device_for_each_child(&master->dev, NULL, __unregister); 582 + mutex_lock(&board_lock); 583 + list_del(&master->list); 584 + mutex_unlock(&board_lock); 585 + 586 + dummy = device_for_each_child(master->dev.parent, &master->dev, 587 + __unregister); 584 588 device_unregister(&master->dev); 585 589 } 586 590 EXPORT_SYMBOL_GPL(spi_unregister_master);
+3
include/linux/spi/spi.h
··· 204 204 /** 205 205 * struct spi_master - interface to SPI master controller 206 206 * @dev: device interface to this driver 207 + * @list: link with the global spi_master list 207 208 * @bus_num: board-specific (and often SOC-specific) identifier for a 208 209 * given SPI controller. 209 210 * @num_chipselect: chipselects are used to distinguish individual ··· 238 237 */ 239 238 struct spi_master { 240 239 struct device dev; 240 + 241 + struct list_head list; 241 242 242 243 /* other than negative (== assign one dynamically), bus_num is fully 243 244 * board-specific. usually that simplifies to being SOC-specific.