1 // SPDX-License-Identifier: GPL-2.0-or-later 2 // SPI init/core code 3 // 4 // Copyright (C) 2005 David Brownell 5 // Copyright (C) 2008 Secret Lab Technologies Ltd. 6 7 #include <linux/kernel.h> 8 #include <linux/device.h> 9 #include <linux/init.h> 10 #include <linux/cache.h> 11 #include <linux/dma-mapping.h> 12 #include <linux/dmaengine.h> 13 #include <linux/mutex.h> 14 #include <linux/of_device.h> 15 #include <linux/of_irq.h> 16 #include <linux/clk/clk-conf.h> 17 #include <linux/slab.h> 18 #include <linux/mod_devicetable.h> 19 #include <linux/spi/spi.h> 20 #include <linux/spi/spi-mem.h> 21 #include <linux/of_gpio.h> 22 #include <linux/gpio/consumer.h> 23 #include <linux/pm_runtime.h> 24 #include <linux/pm_domain.h> 25 #include <linux/property.h> 26 #include <linux/export.h> 27 #include <linux/sched/rt.h> 28 #include <uapi/linux/sched/types.h> 29 #include <linux/delay.h> 30 #include <linux/kthread.h> 31 #include <linux/ioport.h> 32 #include <linux/acpi.h> 33 #include <linux/highmem.h> 34 #include <linux/idr.h> 35 #include <linux/platform_data/x86/apple.h> 36 37 #define CREATE_TRACE_POINTS 38 #include <trace/events/spi.h> 39 EXPORT_TRACEPOINT_SYMBOL(spi_transfer_start); 40 EXPORT_TRACEPOINT_SYMBOL(spi_transfer_stop); 41 42 #include "internals.h" 43 44 static DEFINE_IDR(spi_master_idr); 45 46 static void spidev_release(struct device *dev) 47 { 48 struct spi_device *spi = to_spi_device(dev); 49 50 /* spi controllers may cleanup for released devices */ 51 if (spi->controller->cleanup) 52 spi->controller->cleanup(spi); 53 54 spi_controller_put(spi->controller); 55 kfree(spi->driver_override); 56 kfree(spi); 57 } 58 59 static ssize_t 60 modalias_show(struct device *dev, struct device_attribute *a, char *buf) 61 { 62 const struct spi_device *spi = to_spi_device(dev); 63 int len; 64 65 len = acpi_device_modalias(dev, buf, PAGE_SIZE - 1); 66 if (len != -ENODEV) 67 return len; 68 69 return sprintf(buf, "%s%s\n", SPI_MODULE_PREFIX, spi->modalias); 70 } 71 static DEVICE_ATTR_RO(modalias); 72 73 static ssize_t driver_override_store(struct device *dev, 74 struct device_attribute *a, 75 const char *buf, size_t count) 76 { 77 struct spi_device *spi = to_spi_device(dev); 78 const char *end = memchr(buf, '\n', count); 79 const size_t len = end ? end - buf : count; 80 const char *driver_override, *old; 81 82 /* We need to keep extra room for a newline when displaying value */ 83 if (len >= (PAGE_SIZE - 1)) 84 return -EINVAL; 85 86 driver_override = kstrndup(buf, len, GFP_KERNEL); 87 if (!driver_override) 88 return -ENOMEM; 89 90 device_lock(dev); 91 old = spi->driver_override; 92 if (len) { 93 spi->driver_override = driver_override; 94 } else { 95 /* Empty string, disable driver override */ 96 spi->driver_override = NULL; 97 kfree(driver_override); 98 } 99 device_unlock(dev); 100 kfree(old); 101 102 return count; 103 } 104 105 static ssize_t driver_override_show(struct device *dev, 106 struct device_attribute *a, char *buf) 107 { 108 const struct spi_device *spi = to_spi_device(dev); 109 ssize_t len; 110 111 device_lock(dev); 112 len = snprintf(buf, PAGE_SIZE, "%s\n", spi->driver_override ? : ""); 113 device_unlock(dev); 114 return len; 115 } 116 static DEVICE_ATTR_RW(driver_override); 117 118 #define SPI_STATISTICS_ATTRS(field, file) \ 119 static ssize_t spi_controller_##field##_show(struct device *dev, \ 120 struct device_attribute *attr, \ 121 char *buf) \ 122 { \ 123 struct spi_controller *ctlr = container_of(dev, \ 124 struct spi_controller, dev); \ 125 return spi_statistics_##field##_show(&ctlr->statistics, buf); \ 126 } \ 127 static struct device_attribute dev_attr_spi_controller_##field = { \ 128 .attr = { .name = file, .mode = 0444 }, \ 129 .show = spi_controller_##field##_show, \ 130 }; \ 131 static ssize_t spi_device_##field##_show(struct device *dev, \ 132 struct device_attribute *attr, \ 133 char *buf) \ 134 { \ 135 struct spi_device *spi = to_spi_device(dev); \ 136 return spi_statistics_##field##_show(&spi->statistics, buf); \ 137 } \ 138 static struct device_attribute dev_attr_spi_device_##field = { \ 139 .attr = { .name = file, .mode = 0444 }, \ 140 .show = spi_device_##field##_show, \ 141 } 142 143 #define SPI_STATISTICS_SHOW_NAME(name, file, field, format_string) \ 144 static ssize_t spi_statistics_##name##_show(struct spi_statistics *stat, \ 145 char *buf) \ 146 { \ 147 unsigned long flags; \ 148 ssize_t len; \ 149 spin_lock_irqsave(&stat->lock, flags); \ 150 len = sprintf(buf, format_string, stat->field); \ 151 spin_unlock_irqrestore(&stat->lock, flags); \ 152 return len; \ 153 } \ 154 SPI_STATISTICS_ATTRS(name, file) 155 156 #define SPI_STATISTICS_SHOW(field, format_string) \ 157 SPI_STATISTICS_SHOW_NAME(field, __stringify(field), \ 158 field, format_string) 159 160 SPI_STATISTICS_SHOW(messages, "%lu"); 161 SPI_STATISTICS_SHOW(transfers, "%lu"); 162 SPI_STATISTICS_SHOW(errors, "%lu"); 163 SPI_STATISTICS_SHOW(timedout, "%lu"); 164 165 SPI_STATISTICS_SHOW(spi_sync, "%lu"); 166 SPI_STATISTICS_SHOW(spi_sync_immediate, "%lu"); 167 SPI_STATISTICS_SHOW(spi_async, "%lu"); 168 169 SPI_STATISTICS_SHOW(bytes, "%llu"); 170 SPI_STATISTICS_SHOW(bytes_rx, "%llu"); 171 SPI_STATISTICS_SHOW(bytes_tx, "%llu"); 172 173 #define SPI_STATISTICS_TRANSFER_BYTES_HISTO(index, number) \ 174 SPI_STATISTICS_SHOW_NAME(transfer_bytes_histo##index, \ 175 "transfer_bytes_histo_" number, \ 176 transfer_bytes_histo[index], "%lu") 177 SPI_STATISTICS_TRANSFER_BYTES_HISTO(0, "0-1"); 178 SPI_STATISTICS_TRANSFER_BYTES_HISTO(1, "2-3"); 179 SPI_STATISTICS_TRANSFER_BYTES_HISTO(2, "4-7"); 180 SPI_STATISTICS_TRANSFER_BYTES_HISTO(3, "8-15"); 181 SPI_STATISTICS_TRANSFER_BYTES_HISTO(4, "16-31"); 182 SPI_STATISTICS_TRANSFER_BYTES_HISTO(5, "32-63"); 183 SPI_STATISTICS_TRANSFER_BYTES_HISTO(6, "64-127"); 184 SPI_STATISTICS_TRANSFER_BYTES_HISTO(7, "128-255"); 185 SPI_STATISTICS_TRANSFER_BYTES_HISTO(8, "256-511"); 186 SPI_STATISTICS_TRANSFER_BYTES_HISTO(9, "512-1023"); 187 SPI_STATISTICS_TRANSFER_BYTES_HISTO(10, "1024-2047"); 188 SPI_STATISTICS_TRANSFER_BYTES_HISTO(11, "2048-4095"); 189 SPI_STATISTICS_TRANSFER_BYTES_HISTO(12, "4096-8191"); 190 SPI_STATISTICS_TRANSFER_BYTES_HISTO(13, "8192-16383"); 191 SPI_STATISTICS_TRANSFER_BYTES_HISTO(14, "16384-32767"); 192 SPI_STATISTICS_TRANSFER_BYTES_HISTO(15, "32768-65535"); 193 SPI_STATISTICS_TRANSFER_BYTES_HISTO(16, "65536+"); 194 195 SPI_STATISTICS_SHOW(transfers_split_maxsize, "%lu"); 196 197 static struct attribute *spi_dev_attrs[] = { 198 &dev_attr_modalias.attr, 199 &dev_attr_driver_override.attr, 200 NULL, 201 }; 202 203 static const struct attribute_group spi_dev_group = { 204 .attrs = spi_dev_attrs, 205 }; 206 207 static struct attribute *spi_device_statistics_attrs[] = { 208 &dev_attr_spi_device_messages.attr, 209 &dev_attr_spi_device_transfers.attr, 210 &dev_attr_spi_device_errors.attr, 211 &dev_attr_spi_device_timedout.attr, 212 &dev_attr_spi_device_spi_sync.attr, 213 &dev_attr_spi_device_spi_sync_immediate.attr, 214 &dev_attr_spi_device_spi_async.attr, 215 &dev_attr_spi_device_bytes.attr, 216 &dev_attr_spi_device_bytes_rx.attr, 217 &dev_attr_spi_device_bytes_tx.attr, 218 &dev_attr_spi_device_transfer_bytes_histo0.attr, 219 &dev_attr_spi_device_transfer_bytes_histo1.attr, 220 &dev_attr_spi_device_transfer_bytes_histo2.attr, 221 &dev_attr_spi_device_transfer_bytes_histo3.attr, 222 &dev_attr_spi_device_transfer_bytes_histo4.attr, 223 &dev_attr_spi_device_transfer_bytes_histo5.attr, 224 &dev_attr_spi_device_transfer_bytes_histo6.attr, 225 &dev_attr_spi_device_transfer_bytes_histo7.attr, 226 &dev_attr_spi_device_transfer_bytes_histo8.attr, 227 &dev_attr_spi_device_transfer_bytes_histo9.attr, 228 &dev_attr_spi_device_transfer_bytes_histo10.attr, 229 &dev_attr_spi_device_transfer_bytes_histo11.attr, 230 &dev_attr_spi_device_transfer_bytes_histo12.attr, 231 &dev_attr_spi_device_transfer_bytes_histo13.attr, 232 &dev_attr_spi_device_transfer_bytes_histo14.attr, 233 &dev_attr_spi_device_transfer_bytes_histo15.attr, 234 &dev_attr_spi_device_transfer_bytes_histo16.attr, 235 &dev_attr_spi_device_transfers_split_maxsize.attr, 236 NULL, 237 }; 238 239 static const struct attribute_group spi_device_statistics_group = { 240 .name = "statistics", 241 .attrs = spi_device_statistics_attrs, 242 }; 243 244 static const struct attribute_group *spi_dev_groups[] = { 245 &spi_dev_group, 246 &spi_device_statistics_group, 247 NULL, 248 }; 249 250 static struct attribute *spi_controller_statistics_attrs[] = { 251 &dev_attr_spi_controller_messages.attr, 252 &dev_attr_spi_controller_transfers.attr, 253 &dev_attr_spi_controller_errors.attr, 254 &dev_attr_spi_controller_timedout.attr, 255 &dev_attr_spi_controller_spi_sync.attr, 256 &dev_attr_spi_controller_spi_sync_immediate.attr, 257 &dev_attr_spi_controller_spi_async.attr, 258 &dev_attr_spi_controller_bytes.attr, 259 &dev_attr_spi_controller_bytes_rx.attr, 260 &dev_attr_spi_controller_bytes_tx.attr, 261 &dev_attr_spi_controller_transfer_bytes_histo0.attr, 262 &dev_attr_spi_controller_transfer_bytes_histo1.attr, 263 &dev_attr_spi_controller_transfer_bytes_histo2.attr, 264 &dev_attr_spi_controller_transfer_bytes_histo3.attr, 265 &dev_attr_spi_controller_transfer_bytes_histo4.attr, 266 &dev_attr_spi_controller_transfer_bytes_histo5.attr, 267 &dev_attr_spi_controller_transfer_bytes_histo6.attr, 268 &dev_attr_spi_controller_transfer_bytes_histo7.attr, 269 &dev_attr_spi_controller_transfer_bytes_histo8.attr, 270 &dev_attr_spi_controller_transfer_bytes_histo9.attr, 271 &dev_attr_spi_controller_transfer_bytes_histo10.attr, 272 &dev_attr_spi_controller_transfer_bytes_histo11.attr, 273 &dev_attr_spi_controller_transfer_bytes_histo12.attr, 274 &dev_attr_spi_controller_transfer_bytes_histo13.attr, 275 &dev_attr_spi_controller_transfer_bytes_histo14.attr, 276 &dev_attr_spi_controller_transfer_bytes_histo15.attr, 277 &dev_attr_spi_controller_transfer_bytes_histo16.attr, 278 &dev_attr_spi_controller_transfers_split_maxsize.attr, 279 NULL, 280 }; 281 282 static const struct attribute_group spi_controller_statistics_group = { 283 .name = "statistics", 284 .attrs = spi_controller_statistics_attrs, 285 }; 286 287 static const struct attribute_group *spi_master_groups[] = { 288 &spi_controller_statistics_group, 289 NULL, 290 }; 291 292 void spi_statistics_add_transfer_stats(struct spi_statistics *stats, 293 struct spi_transfer *xfer, 294 struct spi_controller *ctlr) 295 { 296 unsigned long flags; 297 int l2len = min(fls(xfer->len), SPI_STATISTICS_HISTO_SIZE) - 1; 298 299 if (l2len < 0) 300 l2len = 0; 301 302 spin_lock_irqsave(&stats->lock, flags); 303 304 stats->transfers++; 305 stats->transfer_bytes_histo[l2len]++; 306 307 stats->bytes += xfer->len; 308 if ((xfer->tx_buf) && 309 (xfer->tx_buf != ctlr->dummy_tx)) 310 stats->bytes_tx += xfer->len; 311 if ((xfer->rx_buf) && 312 (xfer->rx_buf != ctlr->dummy_rx)) 313 stats->bytes_rx += xfer->len; 314 315 spin_unlock_irqrestore(&stats->lock, flags); 316 } 317 EXPORT_SYMBOL_GPL(spi_statistics_add_transfer_stats); 318 319 /* modalias support makes "modprobe $MODALIAS" new-style hotplug work, 320 * and the sysfs version makes coldplug work too. 321 */ 322 323 static const struct spi_device_id *spi_match_id(const struct spi_device_id *id, 324 const struct spi_device *sdev) 325 { 326 while (id->name[0]) { 327 if (!strcmp(sdev->modalias, id->name)) 328 return id; 329 id++; 330 } 331 return NULL; 332 } 333 334 const struct spi_device_id *spi_get_device_id(const struct spi_device *sdev) 335 { 336 const struct spi_driver *sdrv = to_spi_driver(sdev->dev.driver); 337 338 return spi_match_id(sdrv->id_table, sdev); 339 } 340 EXPORT_SYMBOL_GPL(spi_get_device_id); 341 342 static int spi_match_device(struct device *dev, struct device_driver *drv) 343 { 344 const struct spi_device *spi = to_spi_device(dev); 345 const struct spi_driver *sdrv = to_spi_driver(drv); 346 347 /* Check override first, and if set, only use the named driver */ 348 if (spi->driver_override) 349 return strcmp(spi->driver_override, drv->name) == 0; 350 351 /* Attempt an OF style match */ 352 if (of_driver_match_device(dev, drv)) 353 return 1; 354 355 /* Then try ACPI */ 356 if (acpi_driver_match_device(dev, drv)) 357 return 1; 358 359 if (sdrv->id_table) 360 return !!spi_match_id(sdrv->id_table, spi); 361 362 return strcmp(spi->modalias, drv->name) == 0; 363 } 364 365 static int spi_uevent(struct device *dev, struct kobj_uevent_env *env) 366 { 367 const struct spi_device *spi = to_spi_device(dev); 368 int rc; 369 370 rc = acpi_device_uevent_modalias(dev, env); 371 if (rc != -ENODEV) 372 return rc; 373 374 return add_uevent_var(env, "MODALIAS=%s%s", SPI_MODULE_PREFIX, spi->modalias); 375 } 376 377 static int spi_probe(struct device *dev) 378 { 379 const struct spi_driver *sdrv = to_spi_driver(dev->driver); 380 struct spi_device *spi = to_spi_device(dev); 381 int ret; 382 383 ret = of_clk_set_defaults(dev->of_node, false); 384 if (ret) 385 return ret; 386 387 if (dev->of_node) { 388 spi->irq = of_irq_get(dev->of_node, 0); 389 if (spi->irq == -EPROBE_DEFER) 390 return -EPROBE_DEFER; 391 if (spi->irq < 0) 392 spi->irq = 0; 393 } 394 395 ret = dev_pm_domain_attach(dev, true); 396 if (ret) 397 return ret; 398 399 if (sdrv->probe) { 400 ret = sdrv->probe(spi); 401 if (ret) 402 dev_pm_domain_detach(dev, true); 403 } 404 405 return ret; 406 } 407 408 static int spi_remove(struct device *dev) 409 { 410 const struct spi_driver *sdrv = to_spi_driver(dev->driver); 411 412 if (sdrv->remove) { 413 int ret; 414 415 ret = sdrv->remove(to_spi_device(dev)); 416 if (ret) 417 dev_warn(dev, 418 "Failed to unbind driver (%pe), ignoring\n", 419 ERR_PTR(ret)); 420 } 421 422 dev_pm_domain_detach(dev, true); 423 424 return 0; 425 } 426 427 static void spi_shutdown(struct device *dev) 428 { 429 if (dev->driver) { 430 const struct spi_driver *sdrv = to_spi_driver(dev->driver); 431 432 if (sdrv->shutdown) 433 sdrv->shutdown(to_spi_device(dev)); 434 } 435 } 436 437 struct bus_type spi_bus_type = { 438 .name = "spi", 439 .dev_groups = spi_dev_groups, 440 .match = spi_match_device, 441 .uevent = spi_uevent, 442 .probe = spi_probe, 443 .remove = spi_remove, 444 .shutdown = spi_shutdown, 445 }; 446 EXPORT_SYMBOL_GPL(spi_bus_type); 447 448 /** 449 * __spi_register_driver - register a SPI driver 450 * @owner: owner module of the driver to register 451 * @sdrv: the driver to register 452 * Context: can sleep 453 * 454 * Return: zero on success, else a negative error code. 455 */ 456 int __spi_register_driver(struct module *owner, struct spi_driver *sdrv) 457 { 458 sdrv->driver.owner = owner; 459 sdrv->driver.bus = &spi_bus_type; 460 return driver_register(&sdrv->driver); 461 } 462 EXPORT_SYMBOL_GPL(__spi_register_driver); 463 464 /*-------------------------------------------------------------------------*/ 465 466 /* SPI devices should normally not be created by SPI device drivers; that 467 * would make them board-specific. Similarly with SPI controller drivers. 468 * Device registration normally goes into like arch/.../mach.../board-YYY.c 469 * with other readonly (flashable) information about mainboard devices. 470 */ 471 472 struct boardinfo { 473 struct list_head list; 474 struct spi_board_info board_info; 475 }; 476 477 static LIST_HEAD(board_list); 478 static LIST_HEAD(spi_controller_list); 479 480 /* 481 * Used to protect add/del operation for board_info list and 482 * spi_controller list, and their matching process 483 * also used to protect object of type struct idr 484 */ 485 static DEFINE_MUTEX(board_lock); 486 487 /* 488 * Prevents addition of devices with same chip select and 489 * addition of devices below an unregistering controller. 490 */ 491 static DEFINE_MUTEX(spi_add_lock); 492 493 /** 494 * spi_alloc_device - Allocate a new SPI device 495 * @ctlr: Controller to which device is connected 496 * Context: can sleep 497 * 498 * Allows a driver to allocate and initialize a spi_device without 499 * registering it immediately. This allows a driver to directly 500 * fill the spi_device with device parameters before calling 501 * spi_add_device() on it. 502 * 503 * Caller is responsible to call spi_add_device() on the returned 504 * spi_device structure to add it to the SPI controller. If the caller 505 * needs to discard the spi_device without adding it, then it should 506 * call spi_dev_put() on it. 507 * 508 * Return: a pointer to the new device, or NULL. 509 */ 510 struct spi_device *spi_alloc_device(struct spi_controller *ctlr) 511 { 512 struct spi_device *spi; 513 514 if (!spi_controller_get(ctlr)) 515 return NULL; 516 517 spi = kzalloc(sizeof(*spi), GFP_KERNEL); 518 if (!spi) { 519 spi_controller_put(ctlr); 520 return NULL; 521 } 522 523 spi->master = spi->controller = ctlr; 524 spi->dev.parent = &ctlr->dev; 525 spi->dev.bus = &spi_bus_type; 526 spi->dev.release = spidev_release; 527 spi->cs_gpio = -ENOENT; 528 spi->mode = ctlr->buswidth_override_bits; 529 530 spin_lock_init(&spi->statistics.lock); 531 532 device_initialize(&spi->dev); 533 return spi; 534 } 535 EXPORT_SYMBOL_GPL(spi_alloc_device); 536 537 static void spi_dev_set_name(struct spi_device *spi) 538 { 539 struct acpi_device *adev = ACPI_COMPANION(&spi->dev); 540 541 if (adev) { 542 dev_set_name(&spi->dev, "spi-%s", acpi_dev_name(adev)); 543 return; 544 } 545 546 dev_set_name(&spi->dev, "%s.%u", dev_name(&spi->controller->dev), 547 spi->chip_select); 548 } 549 550 static int spi_dev_check(struct device *dev, void *data) 551 { 552 struct spi_device *spi = to_spi_device(dev); 553 struct spi_device *new_spi = data; 554 555 if (spi->controller == new_spi->controller && 556 spi->chip_select == new_spi->chip_select) 557 return -EBUSY; 558 return 0; 559 } 560 561 /** 562 * spi_add_device - Add spi_device allocated with spi_alloc_device 563 * @spi: spi_device to register 564 * 565 * Companion function to spi_alloc_device. Devices allocated with 566 * spi_alloc_device can be added onto the spi bus with this function. 567 * 568 * Return: 0 on success; negative errno on failure 569 */ 570 int spi_add_device(struct spi_device *spi) 571 { 572 struct spi_controller *ctlr = spi->controller; 573 struct device *dev = ctlr->dev.parent; 574 int status; 575 576 /* Chipselects are numbered 0..max; validate. */ 577 if (spi->chip_select >= ctlr->num_chipselect) { 578 dev_err(dev, "cs%d >= max %d\n", spi->chip_select, 579 ctlr->num_chipselect); 580 return -EINVAL; 581 } 582 583 /* Set the bus ID string */ 584 spi_dev_set_name(spi); 585 586 /* We need to make sure there's no other device with this 587 * chipselect **BEFORE** we call setup(), else we'll trash 588 * its configuration. Lock against concurrent add() calls. 589 */ 590 mutex_lock(&spi_add_lock); 591 592 status = bus_for_each_dev(&spi_bus_type, NULL, spi, spi_dev_check); 593 if (status) { 594 dev_err(dev, "chipselect %d already in use\n", 595 spi->chip_select); 596 goto done; 597 } 598 599 /* Controller may unregister concurrently */ 600 if (IS_ENABLED(CONFIG_SPI_DYNAMIC) && 601 !device_is_registered(&ctlr->dev)) { 602 status = -ENODEV; 603 goto done; 604 } 605 606 /* Descriptors take precedence */ 607 if (ctlr->cs_gpiods) 608 spi->cs_gpiod = ctlr->cs_gpiods[spi->chip_select]; 609 else if (ctlr->cs_gpios) 610 spi->cs_gpio = ctlr->cs_gpios[spi->chip_select]; 611 612 /* Drivers may modify this initial i/o setup, but will 613 * normally rely on the device being setup. Devices 614 * using SPI_CS_HIGH can't coexist well otherwise... 615 */ 616 status = spi_setup(spi); 617 if (status < 0) { 618 dev_err(dev, "can't setup %s, status %d\n", 619 dev_name(&spi->dev), status); 620 goto done; 621 } 622 623 /* Device may be bound to an active driver when this returns */ 624 status = device_add(&spi->dev); 625 if (status < 0) 626 dev_err(dev, "can't add %s, status %d\n", 627 dev_name(&spi->dev), status); 628 else 629 dev_dbg(dev, "registered child %s\n", dev_name(&spi->dev)); 630 631 done: 632 mutex_unlock(&spi_add_lock); 633 return status; 634 } 635 EXPORT_SYMBOL_GPL(spi_add_device); 636 637 /** 638 * spi_new_device - instantiate one new SPI device 639 * @ctlr: Controller to which device is connected 640 * @chip: Describes the SPI device 641 * Context: can sleep 642 * 643 * On typical mainboards, this is purely internal; and it's not needed 644 * after board init creates the hard-wired devices. Some development 645 * platforms may not be able to use spi_register_board_info though, and 646 * this is exported so that for example a USB or parport based adapter 647 * driver could add devices (which it would learn about out-of-band). 648 * 649 * Return: the new device, or NULL. 650 */ 651 struct spi_device *spi_new_device(struct spi_controller *ctlr, 652 struct spi_board_info *chip) 653 { 654 struct spi_device *proxy; 655 int status; 656 657 /* NOTE: caller did any chip->bus_num checks necessary. 658 * 659 * Also, unless we change the return value convention to use 660 * error-or-pointer (not NULL-or-pointer), troubleshootability 661 * suggests syslogged diagnostics are best here (ugh). 662 */ 663 664 proxy = spi_alloc_device(ctlr); 665 if (!proxy) 666 return NULL; 667 668 WARN_ON(strlen(chip->modalias) >= sizeof(proxy->modalias)); 669 670 proxy->chip_select = chip->chip_select; 671 proxy->max_speed_hz = chip->max_speed_hz; 672 proxy->mode = chip->mode; 673 proxy->irq = chip->irq; 674 strlcpy(proxy->modalias, chip->modalias, sizeof(proxy->modalias)); 675 proxy->dev.platform_data = (void *) chip->platform_data; 676 proxy->controller_data = chip->controller_data; 677 proxy->controller_state = NULL; 678 679 if (chip->properties) { 680 status = device_add_properties(&proxy->dev, chip->properties); 681 if (status) { 682 dev_err(&ctlr->dev, 683 "failed to add properties to '%s': %d\n", 684 chip->modalias, status); 685 goto err_dev_put; 686 } 687 } 688 689 status = spi_add_device(proxy); 690 if (status < 0) 691 goto err_remove_props; 692 693 return proxy; 694 695 err_remove_props: 696 if (chip->properties) 697 device_remove_properties(&proxy->dev); 698 err_dev_put: 699 spi_dev_put(proxy); 700 return NULL; 701 } 702 EXPORT_SYMBOL_GPL(spi_new_device); 703 704 /** 705 * spi_unregister_device - unregister a single SPI device 706 * @spi: spi_device to unregister 707 * 708 * Start making the passed SPI device vanish. Normally this would be handled 709 * by spi_unregister_controller(). 710 */ 711 void spi_unregister_device(struct spi_device *spi) 712 { 713 if (!spi) 714 return; 715 716 if (spi->dev.of_node) { 717 of_node_clear_flag(spi->dev.of_node, OF_POPULATED); 718 of_node_put(spi->dev.of_node); 719 } 720 if (ACPI_COMPANION(&spi->dev)) 721 acpi_device_clear_enumerated(ACPI_COMPANION(&spi->dev)); 722 device_unregister(&spi->dev); 723 } 724 EXPORT_SYMBOL_GPL(spi_unregister_device); 725 726 static void spi_match_controller_to_boardinfo(struct spi_controller *ctlr, 727 struct spi_board_info *bi) 728 { 729 struct spi_device *dev; 730 731 if (ctlr->bus_num != bi->bus_num) 732 return; 733 734 dev = spi_new_device(ctlr, bi); 735 if (!dev) 736 dev_err(ctlr->dev.parent, "can't create new device for %s\n", 737 bi->modalias); 738 } 739 740 /** 741 * spi_register_board_info - register SPI devices for a given board 742 * @info: array of chip descriptors 743 * @n: how many descriptors are provided 744 * Context: can sleep 745 * 746 * Board-specific early init code calls this (probably during arch_initcall) 747 * with segments of the SPI device table. Any device nodes are created later, 748 * after the relevant parent SPI controller (bus_num) is defined. We keep 749 * this table of devices forever, so that reloading a controller driver will 750 * not make Linux forget about these hard-wired devices. 751 * 752 * Other code can also call this, e.g. a particular add-on board might provide 753 * SPI devices through its expansion connector, so code initializing that board 754 * would naturally declare its SPI devices. 755 * 756 * The board info passed can safely be __initdata ... but be careful of 757 * any embedded pointers (platform_data, etc), they're copied as-is. 758 * Device properties are deep-copied though. 759 * 760 * Return: zero on success, else a negative error code. 761 */ 762 int spi_register_board_info(struct spi_board_info const *info, unsigned n) 763 { 764 struct boardinfo *bi; 765 int i; 766 767 if (!n) 768 return 0; 769 770 bi = kcalloc(n, sizeof(*bi), GFP_KERNEL); 771 if (!bi) 772 return -ENOMEM; 773 774 for (i = 0; i < n; i++, bi++, info++) { 775 struct spi_controller *ctlr; 776 777 memcpy(&bi->board_info, info, sizeof(*info)); 778 if (info->properties) { 779 bi->board_info.properties = 780 property_entries_dup(info->properties); 781 if (IS_ERR(bi->board_info.properties)) 782 return PTR_ERR(bi->board_info.properties); 783 } 784 785 mutex_lock(&board_lock); 786 list_add_tail(&bi->list, &board_list); 787 list_for_each_entry(ctlr, &spi_controller_list, list) 788 spi_match_controller_to_boardinfo(ctlr, 789 &bi->board_info); 790 mutex_unlock(&board_lock); 791 } 792 793 return 0; 794 } 795 796 /*-------------------------------------------------------------------------*/ 797 798 static void spi_set_cs(struct spi_device *spi, bool enable) 799 { 800 bool enable1 = enable; 801 802 /* 803 * Avoid calling into the driver (or doing delays) if the chip select 804 * isn't actually changing from the last time this was called. 805 */ 806 if ((spi->controller->last_cs_enable == enable) && 807 (spi->controller->last_cs_mode_high == (spi->mode & SPI_CS_HIGH))) 808 return; 809 810 spi->controller->last_cs_enable = enable; 811 spi->controller->last_cs_mode_high = spi->mode & SPI_CS_HIGH; 812 813 if (!spi->controller->set_cs_timing) { 814 if (enable1) 815 spi_delay_exec(&spi->controller->cs_setup, NULL); 816 else 817 spi_delay_exec(&spi->controller->cs_hold, NULL); 818 } 819 820 if (spi->mode & SPI_CS_HIGH) 821 enable = !enable; 822 823 if (spi->cs_gpiod || gpio_is_valid(spi->cs_gpio)) { 824 if (!(spi->mode & SPI_NO_CS)) { 825 if (spi->cs_gpiod) 826 /* polarity handled by gpiolib */ 827 gpiod_set_value_cansleep(spi->cs_gpiod, 828 enable1); 829 else 830 /* 831 * invert the enable line, as active low is 832 * default for SPI. 833 */ 834 gpio_set_value_cansleep(spi->cs_gpio, !enable); 835 } 836 /* Some SPI masters need both GPIO CS & slave_select */ 837 if ((spi->controller->flags & SPI_MASTER_GPIO_SS) && 838 spi->controller->set_cs) 839 spi->controller->set_cs(spi, !enable); 840 } else if (spi->controller->set_cs) { 841 spi->controller->set_cs(spi, !enable); 842 } 843 844 if (!spi->controller->set_cs_timing) { 845 if (!enable1) 846 spi_delay_exec(&spi->controller->cs_inactive, NULL); 847 } 848 } 849 850 #ifdef CONFIG_HAS_DMA 851 int spi_map_buf(struct spi_controller *ctlr, struct device *dev, 852 struct sg_table *sgt, void *buf, size_t len, 853 enum dma_data_direction dir) 854 { 855 const bool vmalloced_buf = is_vmalloc_addr(buf); 856 unsigned int max_seg_size = dma_get_max_seg_size(dev); 857 #ifdef CONFIG_HIGHMEM 858 const bool kmap_buf = ((unsigned long)buf >= PKMAP_BASE && 859 (unsigned long)buf < (PKMAP_BASE + 860 (LAST_PKMAP * PAGE_SIZE))); 861 #else 862 const bool kmap_buf = false; 863 #endif 864 int desc_len; 865 int sgs; 866 struct page *vm_page; 867 struct scatterlist *sg; 868 void *sg_buf; 869 size_t min; 870 int i, ret; 871 872 if (vmalloced_buf || kmap_buf) { 873 desc_len = min_t(int, max_seg_size, PAGE_SIZE); 874 sgs = DIV_ROUND_UP(len + offset_in_page(buf), desc_len); 875 } else if (virt_addr_valid(buf)) { 876 desc_len = min_t(int, max_seg_size, ctlr->max_dma_len); 877 sgs = DIV_ROUND_UP(len, desc_len); 878 } else { 879 return -EINVAL; 880 } 881 882 ret = sg_alloc_table(sgt, sgs, GFP_KERNEL); 883 if (ret != 0) 884 return ret; 885 886 sg = &sgt->sgl[0]; 887 for (i = 0; i < sgs; i++) { 888 889 if (vmalloced_buf || kmap_buf) { 890 /* 891 * Next scatterlist entry size is the minimum between 892 * the desc_len and the remaining buffer length that 893 * fits in a page. 894 */ 895 min = min_t(size_t, desc_len, 896 min_t(size_t, len, 897 PAGE_SIZE - offset_in_page(buf))); 898 if (vmalloced_buf) 899 vm_page = vmalloc_to_page(buf); 900 else 901 vm_page = kmap_to_page(buf); 902 if (!vm_page) { 903 sg_free_table(sgt); 904 return -ENOMEM; 905 } 906 sg_set_page(sg, vm_page, 907 min, offset_in_page(buf)); 908 } else { 909 min = min_t(size_t, len, desc_len); 910 sg_buf = buf; 911 sg_set_buf(sg, sg_buf, min); 912 } 913 914 buf += min; 915 len -= min; 916 sg = sg_next(sg); 917 } 918 919 ret = dma_map_sg(dev, sgt->sgl, sgt->nents, dir); 920 if (!ret) 921 ret = -ENOMEM; 922 if (ret < 0) { 923 sg_free_table(sgt); 924 return ret; 925 } 926 927 sgt->nents = ret; 928 929 return 0; 930 } 931 932 void spi_unmap_buf(struct spi_controller *ctlr, struct device *dev, 933 struct sg_table *sgt, enum dma_data_direction dir) 934 { 935 if (sgt->orig_nents) { 936 dma_unmap_sg(dev, sgt->sgl, sgt->orig_nents, dir); 937 sg_free_table(sgt); 938 } 939 } 940 941 static int __spi_map_msg(struct spi_controller *ctlr, struct spi_message *msg) 942 { 943 struct device *tx_dev, *rx_dev; 944 struct spi_transfer *xfer; 945 int ret; 946 947 if (!ctlr->can_dma) 948 return 0; 949 950 if (ctlr->dma_tx) 951 tx_dev = ctlr->dma_tx->device->dev; 952 else 953 tx_dev = ctlr->dev.parent; 954 955 if (ctlr->dma_rx) 956 rx_dev = ctlr->dma_rx->device->dev; 957 else 958 rx_dev = ctlr->dev.parent; 959 960 list_for_each_entry(xfer, &msg->transfers, transfer_list) { 961 if (!ctlr->can_dma(ctlr, msg->spi, xfer)) 962 continue; 963 964 if (xfer->tx_buf != NULL) { 965 ret = spi_map_buf(ctlr, tx_dev, &xfer->tx_sg, 966 (void *)xfer->tx_buf, xfer->len, 967 DMA_TO_DEVICE); 968 if (ret != 0) 969 return ret; 970 } 971 972 if (xfer->rx_buf != NULL) { 973 ret = spi_map_buf(ctlr, rx_dev, &xfer->rx_sg, 974 xfer->rx_buf, xfer->len, 975 DMA_FROM_DEVICE); 976 if (ret != 0) { 977 spi_unmap_buf(ctlr, tx_dev, &xfer->tx_sg, 978 DMA_TO_DEVICE); 979 return ret; 980 } 981 } 982 } 983 984 ctlr->cur_msg_mapped = true; 985 986 return 0; 987 } 988 989 static int __spi_unmap_msg(struct spi_controller *ctlr, struct spi_message *msg) 990 { 991 struct spi_transfer *xfer; 992 struct device *tx_dev, *rx_dev; 993 994 if (!ctlr->cur_msg_mapped || !ctlr->can_dma) 995 return 0; 996 997 if (ctlr->dma_tx) 998 tx_dev = ctlr->dma_tx->device->dev; 999 else 1000 tx_dev = ctlr->dev.parent; 1001 1002 if (ctlr->dma_rx) 1003 rx_dev = ctlr->dma_rx->device->dev; 1004 else 1005 rx_dev = ctlr->dev.parent; 1006 1007 list_for_each_entry(xfer, &msg->transfers, transfer_list) { 1008 if (!ctlr->can_dma(ctlr, msg->spi, xfer)) 1009 continue; 1010 1011 spi_unmap_buf(ctlr, rx_dev, &xfer->rx_sg, DMA_FROM_DEVICE); 1012 spi_unmap_buf(ctlr, tx_dev, &xfer->tx_sg, DMA_TO_DEVICE); 1013 } 1014 1015 ctlr->cur_msg_mapped = false; 1016 1017 return 0; 1018 } 1019 #else /* !CONFIG_HAS_DMA */ 1020 static inline int __spi_map_msg(struct spi_controller *ctlr, 1021 struct spi_message *msg) 1022 { 1023 return 0; 1024 } 1025 1026 static inline int __spi_unmap_msg(struct spi_controller *ctlr, 1027 struct spi_message *msg) 1028 { 1029 return 0; 1030 } 1031 #endif /* !CONFIG_HAS_DMA */ 1032 1033 static inline int spi_unmap_msg(struct spi_controller *ctlr, 1034 struct spi_message *msg) 1035 { 1036 struct spi_transfer *xfer; 1037 1038 list_for_each_entry(xfer, &msg->transfers, transfer_list) { 1039 /* 1040 * Restore the original value of tx_buf or rx_buf if they are 1041 * NULL. 1042 */ 1043 if (xfer->tx_buf == ctlr->dummy_tx) 1044 xfer->tx_buf = NULL; 1045 if (xfer->rx_buf == ctlr->dummy_rx) 1046 xfer->rx_buf = NULL; 1047 } 1048 1049 return __spi_unmap_msg(ctlr, msg); 1050 } 1051 1052 static int spi_map_msg(struct spi_controller *ctlr, struct spi_message *msg) 1053 { 1054 struct spi_transfer *xfer; 1055 void *tmp; 1056 unsigned int max_tx, max_rx; 1057 1058 if ((ctlr->flags & (SPI_CONTROLLER_MUST_RX | SPI_CONTROLLER_MUST_TX)) 1059 && !(msg->spi->mode & SPI_3WIRE)) { 1060 max_tx = 0; 1061 max_rx = 0; 1062 1063 list_for_each_entry(xfer, &msg->transfers, transfer_list) { 1064 if ((ctlr->flags & SPI_CONTROLLER_MUST_TX) && 1065 !xfer->tx_buf) 1066 max_tx = max(xfer->len, max_tx); 1067 if ((ctlr->flags & SPI_CONTROLLER_MUST_RX) && 1068 !xfer->rx_buf) 1069 max_rx = max(xfer->len, max_rx); 1070 } 1071 1072 if (max_tx) { 1073 tmp = krealloc(ctlr->dummy_tx, max_tx, 1074 GFP_KERNEL | GFP_DMA); 1075 if (!tmp) 1076 return -ENOMEM; 1077 ctlr->dummy_tx = tmp; 1078 memset(tmp, 0, max_tx); 1079 } 1080 1081 if (max_rx) { 1082 tmp = krealloc(ctlr->dummy_rx, max_rx, 1083 GFP_KERNEL | GFP_DMA); 1084 if (!tmp) 1085 return -ENOMEM; 1086 ctlr->dummy_rx = tmp; 1087 } 1088 1089 if (max_tx || max_rx) { 1090 list_for_each_entry(xfer, &msg->transfers, 1091 transfer_list) { 1092 if (!xfer->len) 1093 continue; 1094 if (!xfer->tx_buf) 1095 xfer->tx_buf = ctlr->dummy_tx; 1096 if (!xfer->rx_buf) 1097 xfer->rx_buf = ctlr->dummy_rx; 1098 } 1099 } 1100 } 1101 1102 return __spi_map_msg(ctlr, msg); 1103 } 1104 1105 static int spi_transfer_wait(struct spi_controller *ctlr, 1106 struct spi_message *msg, 1107 struct spi_transfer *xfer) 1108 { 1109 struct spi_statistics *statm = &ctlr->statistics; 1110 struct spi_statistics *stats = &msg->spi->statistics; 1111 u32 speed_hz = xfer->speed_hz; 1112 unsigned long long ms; 1113 1114 if (spi_controller_is_slave(ctlr)) { 1115 if (wait_for_completion_interruptible(&ctlr->xfer_completion)) { 1116 dev_dbg(&msg->spi->dev, "SPI transfer interrupted\n"); 1117 return -EINTR; 1118 } 1119 } else { 1120 if (!speed_hz) 1121 speed_hz = 100000; 1122 1123 ms = 8LL * 1000LL * xfer->len; 1124 do_div(ms, speed_hz); 1125 ms += ms + 200; /* some tolerance */ 1126 1127 if (ms > UINT_MAX) 1128 ms = UINT_MAX; 1129 1130 ms = wait_for_completion_timeout(&ctlr->xfer_completion, 1131 msecs_to_jiffies(ms)); 1132 1133 if (ms == 0) { 1134 SPI_STATISTICS_INCREMENT_FIELD(statm, timedout); 1135 SPI_STATISTICS_INCREMENT_FIELD(stats, timedout); 1136 dev_err(&msg->spi->dev, 1137 "SPI transfer timed out\n"); 1138 return -ETIMEDOUT; 1139 } 1140 } 1141 1142 return 0; 1143 } 1144 1145 static void _spi_transfer_delay_ns(u32 ns) 1146 { 1147 if (!ns) 1148 return; 1149 if (ns <= 1000) { 1150 ndelay(ns); 1151 } else { 1152 u32 us = DIV_ROUND_UP(ns, 1000); 1153 1154 if (us <= 10) 1155 udelay(us); 1156 else 1157 usleep_range(us, us + DIV_ROUND_UP(us, 10)); 1158 } 1159 } 1160 1161 int spi_delay_to_ns(struct spi_delay *_delay, struct spi_transfer *xfer) 1162 { 1163 u32 delay = _delay->value; 1164 u32 unit = _delay->unit; 1165 u32 hz; 1166 1167 if (!delay) 1168 return 0; 1169 1170 switch (unit) { 1171 case SPI_DELAY_UNIT_USECS: 1172 delay *= 1000; 1173 break; 1174 case SPI_DELAY_UNIT_NSECS: /* nothing to do here */ 1175 break; 1176 case SPI_DELAY_UNIT_SCK: 1177 /* clock cycles need to be obtained from spi_transfer */ 1178 if (!xfer) 1179 return -EINVAL; 1180 /* if there is no effective speed know, then approximate 1181 * by underestimating with half the requested hz 1182 */ 1183 hz = xfer->effective_speed_hz ?: xfer->speed_hz / 2; 1184 if (!hz) 1185 return -EINVAL; 1186 delay *= DIV_ROUND_UP(1000000000, hz); 1187 break; 1188 default: 1189 return -EINVAL; 1190 } 1191 1192 return delay; 1193 } 1194 EXPORT_SYMBOL_GPL(spi_delay_to_ns); 1195 1196 int spi_delay_exec(struct spi_delay *_delay, struct spi_transfer *xfer) 1197 { 1198 int delay; 1199 1200 might_sleep(); 1201 1202 if (!_delay) 1203 return -EINVAL; 1204 1205 delay = spi_delay_to_ns(_delay, xfer); 1206 if (delay < 0) 1207 return delay; 1208 1209 _spi_transfer_delay_ns(delay); 1210 1211 return 0; 1212 } 1213 EXPORT_SYMBOL_GPL(spi_delay_exec); 1214 1215 static void _spi_transfer_cs_change_delay(struct spi_message *msg, 1216 struct spi_transfer *xfer) 1217 { 1218 u32 delay = xfer->cs_change_delay.value; 1219 u32 unit = xfer->cs_change_delay.unit; 1220 int ret; 1221 1222 /* return early on "fast" mode - for everything but USECS */ 1223 if (!delay) { 1224 if (unit == SPI_DELAY_UNIT_USECS) 1225 _spi_transfer_delay_ns(10000); 1226 return; 1227 } 1228 1229 ret = spi_delay_exec(&xfer->cs_change_delay, xfer); 1230 if (ret) { 1231 dev_err_once(&msg->spi->dev, 1232 "Use of unsupported delay unit %i, using default of 10us\n", 1233 unit); 1234 _spi_transfer_delay_ns(10000); 1235 } 1236 } 1237 1238 /* 1239 * spi_transfer_one_message - Default implementation of transfer_one_message() 1240 * 1241 * This is a standard implementation of transfer_one_message() for 1242 * drivers which implement a transfer_one() operation. It provides 1243 * standard handling of delays and chip select management. 1244 */ 1245 static int spi_transfer_one_message(struct spi_controller *ctlr, 1246 struct spi_message *msg) 1247 { 1248 struct spi_transfer *xfer; 1249 bool keep_cs = false; 1250 int ret = 0; 1251 struct spi_statistics *statm = &ctlr->statistics; 1252 struct spi_statistics *stats = &msg->spi->statistics; 1253 1254 spi_set_cs(msg->spi, true); 1255 1256 SPI_STATISTICS_INCREMENT_FIELD(statm, messages); 1257 SPI_STATISTICS_INCREMENT_FIELD(stats, messages); 1258 1259 list_for_each_entry(xfer, &msg->transfers, transfer_list) { 1260 trace_spi_transfer_start(msg, xfer); 1261 1262 spi_statistics_add_transfer_stats(statm, xfer, ctlr); 1263 spi_statistics_add_transfer_stats(stats, xfer, ctlr); 1264 1265 if (!ctlr->ptp_sts_supported) { 1266 xfer->ptp_sts_word_pre = 0; 1267 ptp_read_system_prets(xfer->ptp_sts); 1268 } 1269 1270 if (xfer->tx_buf || xfer->rx_buf) { 1271 reinit_completion(&ctlr->xfer_completion); 1272 1273 fallback_pio: 1274 ret = ctlr->transfer_one(ctlr, msg->spi, xfer); 1275 if (ret < 0) { 1276 if (ctlr->cur_msg_mapped && 1277 (xfer->error & SPI_TRANS_FAIL_NO_START)) { 1278 __spi_unmap_msg(ctlr, msg); 1279 ctlr->fallback = true; 1280 xfer->error &= ~SPI_TRANS_FAIL_NO_START; 1281 goto fallback_pio; 1282 } 1283 1284 SPI_STATISTICS_INCREMENT_FIELD(statm, 1285 errors); 1286 SPI_STATISTICS_INCREMENT_FIELD(stats, 1287 errors); 1288 dev_err(&msg->spi->dev, 1289 "SPI transfer failed: %d\n", ret); 1290 goto out; 1291 } 1292 1293 if (ret > 0) { 1294 ret = spi_transfer_wait(ctlr, msg, xfer); 1295 if (ret < 0) 1296 msg->status = ret; 1297 } 1298 } else { 1299 if (xfer->len) 1300 dev_err(&msg->spi->dev, 1301 "Bufferless transfer has length %u\n", 1302 xfer->len); 1303 } 1304 1305 if (!ctlr->ptp_sts_supported) { 1306 ptp_read_system_postts(xfer->ptp_sts); 1307 xfer->ptp_sts_word_post = xfer->len; 1308 } 1309 1310 trace_spi_transfer_stop(msg, xfer); 1311 1312 if (msg->status != -EINPROGRESS) 1313 goto out; 1314 1315 spi_transfer_delay_exec(xfer); 1316 1317 if (xfer->cs_change) { 1318 if (list_is_last(&xfer->transfer_list, 1319 &msg->transfers)) { 1320 keep_cs = true; 1321 } else { 1322 spi_set_cs(msg->spi, false); 1323 _spi_transfer_cs_change_delay(msg, xfer); 1324 spi_set_cs(msg->spi, true); 1325 } 1326 } 1327 1328 msg->actual_length += xfer->len; 1329 } 1330 1331 out: 1332 if (ret != 0 || !keep_cs) 1333 spi_set_cs(msg->spi, false); 1334 1335 if (msg->status == -EINPROGRESS) 1336 msg->status = ret; 1337 1338 if (msg->status && ctlr->handle_err) 1339 ctlr->handle_err(ctlr, msg); 1340 1341 spi_finalize_current_message(ctlr); 1342 1343 return ret; 1344 } 1345 1346 /** 1347 * spi_finalize_current_transfer - report completion of a transfer 1348 * @ctlr: the controller reporting completion 1349 * 1350 * Called by SPI drivers using the core transfer_one_message() 1351 * implementation to notify it that the current interrupt driven 1352 * transfer has finished and the next one may be scheduled. 1353 */ 1354 void spi_finalize_current_transfer(struct spi_controller *ctlr) 1355 { 1356 complete(&ctlr->xfer_completion); 1357 } 1358 EXPORT_SYMBOL_GPL(spi_finalize_current_transfer); 1359 1360 static void spi_idle_runtime_pm(struct spi_controller *ctlr) 1361 { 1362 if (ctlr->auto_runtime_pm) { 1363 pm_runtime_mark_last_busy(ctlr->dev.parent); 1364 pm_runtime_put_autosuspend(ctlr->dev.parent); 1365 } 1366 } 1367 1368 /** 1369 * __spi_pump_messages - function which processes spi message queue 1370 * @ctlr: controller to process queue for 1371 * @in_kthread: true if we are in the context of the message pump thread 1372 * 1373 * This function checks if there is any spi message in the queue that 1374 * needs processing and if so call out to the driver to initialize hardware 1375 * and transfer each message. 1376 * 1377 * Note that it is called both from the kthread itself and also from 1378 * inside spi_sync(); the queue extraction handling at the top of the 1379 * function should deal with this safely. 1380 */ 1381 static void __spi_pump_messages(struct spi_controller *ctlr, bool in_kthread) 1382 { 1383 struct spi_transfer *xfer; 1384 struct spi_message *msg; 1385 bool was_busy = false; 1386 unsigned long flags; 1387 int ret; 1388 1389 /* Lock queue */ 1390 spin_lock_irqsave(&ctlr->queue_lock, flags); 1391 1392 /* Make sure we are not already running a message */ 1393 if (ctlr->cur_msg) { 1394 spin_unlock_irqrestore(&ctlr->queue_lock, flags); 1395 return; 1396 } 1397 1398 /* If another context is idling the device then defer */ 1399 if (ctlr->idling) { 1400 kthread_queue_work(ctlr->kworker, &ctlr->pump_messages); 1401 spin_unlock_irqrestore(&ctlr->queue_lock, flags); 1402 return; 1403 } 1404 1405 /* Check if the queue is idle */ 1406 if (list_empty(&ctlr->queue) || !ctlr->running) { 1407 if (!ctlr->busy) { 1408 spin_unlock_irqrestore(&ctlr->queue_lock, flags); 1409 return; 1410 } 1411 1412 /* Defer any non-atomic teardown to the thread */ 1413 if (!in_kthread) { 1414 if (!ctlr->dummy_rx && !ctlr->dummy_tx && 1415 !ctlr->unprepare_transfer_hardware) { 1416 spi_idle_runtime_pm(ctlr); 1417 ctlr->busy = false; 1418 trace_spi_controller_idle(ctlr); 1419 } else { 1420 kthread_queue_work(ctlr->kworker, 1421 &ctlr->pump_messages); 1422 } 1423 spin_unlock_irqrestore(&ctlr->queue_lock, flags); 1424 return; 1425 } 1426 1427 ctlr->busy = false; 1428 ctlr->idling = true; 1429 spin_unlock_irqrestore(&ctlr->queue_lock, flags); 1430 1431 kfree(ctlr->dummy_rx); 1432 ctlr->dummy_rx = NULL; 1433 kfree(ctlr->dummy_tx); 1434 ctlr->dummy_tx = NULL; 1435 if (ctlr->unprepare_transfer_hardware && 1436 ctlr->unprepare_transfer_hardware(ctlr)) 1437 dev_err(&ctlr->dev, 1438 "failed to unprepare transfer hardware\n"); 1439 spi_idle_runtime_pm(ctlr); 1440 trace_spi_controller_idle(ctlr); 1441 1442 spin_lock_irqsave(&ctlr->queue_lock, flags); 1443 ctlr->idling = false; 1444 spin_unlock_irqrestore(&ctlr->queue_lock, flags); 1445 return; 1446 } 1447 1448 /* Extract head of queue */ 1449 msg = list_first_entry(&ctlr->queue, struct spi_message, queue); 1450 ctlr->cur_msg = msg; 1451 1452 list_del_init(&msg->queue); 1453 if (ctlr->busy) 1454 was_busy = true; 1455 else 1456 ctlr->busy = true; 1457 spin_unlock_irqrestore(&ctlr->queue_lock, flags); 1458 1459 mutex_lock(&ctlr->io_mutex); 1460 1461 if (!was_busy && ctlr->auto_runtime_pm) { 1462 ret = pm_runtime_get_sync(ctlr->dev.parent); 1463 if (ret < 0) { 1464 pm_runtime_put_noidle(ctlr->dev.parent); 1465 dev_err(&ctlr->dev, "Failed to power device: %d\n", 1466 ret); 1467 mutex_unlock(&ctlr->io_mutex); 1468 return; 1469 } 1470 } 1471 1472 if (!was_busy) 1473 trace_spi_controller_busy(ctlr); 1474 1475 if (!was_busy && ctlr->prepare_transfer_hardware) { 1476 ret = ctlr->prepare_transfer_hardware(ctlr); 1477 if (ret) { 1478 dev_err(&ctlr->dev, 1479 "failed to prepare transfer hardware: %d\n", 1480 ret); 1481 1482 if (ctlr->auto_runtime_pm) 1483 pm_runtime_put(ctlr->dev.parent); 1484 1485 msg->status = ret; 1486 spi_finalize_current_message(ctlr); 1487 1488 mutex_unlock(&ctlr->io_mutex); 1489 return; 1490 } 1491 } 1492 1493 trace_spi_message_start(msg); 1494 1495 if (ctlr->prepare_message) { 1496 ret = ctlr->prepare_message(ctlr, msg); 1497 if (ret) { 1498 dev_err(&ctlr->dev, "failed to prepare message: %d\n", 1499 ret); 1500 msg->status = ret; 1501 spi_finalize_current_message(ctlr); 1502 goto out; 1503 } 1504 ctlr->cur_msg_prepared = true; 1505 } 1506 1507 ret = spi_map_msg(ctlr, msg); 1508 if (ret) { 1509 msg->status = ret; 1510 spi_finalize_current_message(ctlr); 1511 goto out; 1512 } 1513 1514 if (!ctlr->ptp_sts_supported && !ctlr->transfer_one) { 1515 list_for_each_entry(xfer, &msg->transfers, transfer_list) { 1516 xfer->ptp_sts_word_pre = 0; 1517 ptp_read_system_prets(xfer->ptp_sts); 1518 } 1519 } 1520 1521 ret = ctlr->transfer_one_message(ctlr, msg); 1522 if (ret) { 1523 dev_err(&ctlr->dev, 1524 "failed to transfer one message from queue\n"); 1525 goto out; 1526 } 1527 1528 out: 1529 mutex_unlock(&ctlr->io_mutex); 1530 1531 /* Prod the scheduler in case transfer_one() was busy waiting */ 1532 if (!ret) 1533 cond_resched(); 1534 } 1535 1536 /** 1537 * spi_pump_messages - kthread work function which processes spi message queue 1538 * @work: pointer to kthread work struct contained in the controller struct 1539 */ 1540 static void spi_pump_messages(struct kthread_work *work) 1541 { 1542 struct spi_controller *ctlr = 1543 container_of(work, struct spi_controller, pump_messages); 1544 1545 __spi_pump_messages(ctlr, true); 1546 } 1547 1548 /** 1549 * spi_take_timestamp_pre - helper for drivers to collect the beginning of the 1550 * TX timestamp for the requested byte from the SPI 1551 * transfer. The frequency with which this function 1552 * must be called (once per word, once for the whole 1553 * transfer, once per batch of words etc) is arbitrary 1554 * as long as the @tx buffer offset is greater than or 1555 * equal to the requested byte at the time of the 1556 * call. The timestamp is only taken once, at the 1557 * first such call. It is assumed that the driver 1558 * advances its @tx buffer pointer monotonically. 1559 * @ctlr: Pointer to the spi_controller structure of the driver 1560 * @xfer: Pointer to the transfer being timestamped 1561 * @progress: How many words (not bytes) have been transferred so far 1562 * @irqs_off: If true, will disable IRQs and preemption for the duration of the 1563 * transfer, for less jitter in time measurement. Only compatible 1564 * with PIO drivers. If true, must follow up with 1565 * spi_take_timestamp_post or otherwise system will crash. 1566 * WARNING: for fully predictable results, the CPU frequency must 1567 * also be under control (governor). 1568 */ 1569 void spi_take_timestamp_pre(struct spi_controller *ctlr, 1570 struct spi_transfer *xfer, 1571 size_t progress, bool irqs_off) 1572 { 1573 if (!xfer->ptp_sts) 1574 return; 1575 1576 if (xfer->timestamped) 1577 return; 1578 1579 if (progress > xfer->ptp_sts_word_pre) 1580 return; 1581 1582 /* Capture the resolution of the timestamp */ 1583 xfer->ptp_sts_word_pre = progress; 1584 1585 if (irqs_off) { 1586 local_irq_save(ctlr->irq_flags); 1587 preempt_disable(); 1588 } 1589 1590 ptp_read_system_prets(xfer->ptp_sts); 1591 } 1592 EXPORT_SYMBOL_GPL(spi_take_timestamp_pre); 1593 1594 /** 1595 * spi_take_timestamp_post - helper for drivers to collect the end of the 1596 * TX timestamp for the requested byte from the SPI 1597 * transfer. Can be called with an arbitrary 1598 * frequency: only the first call where @tx exceeds 1599 * or is equal to the requested word will be 1600 * timestamped. 1601 * @ctlr: Pointer to the spi_controller structure of the driver 1602 * @xfer: Pointer to the transfer being timestamped 1603 * @progress: How many words (not bytes) have been transferred so far 1604 * @irqs_off: If true, will re-enable IRQs and preemption for the local CPU. 1605 */ 1606 void spi_take_timestamp_post(struct spi_controller *ctlr, 1607 struct spi_transfer *xfer, 1608 size_t progress, bool irqs_off) 1609 { 1610 if (!xfer->ptp_sts) 1611 return; 1612 1613 if (xfer->timestamped) 1614 return; 1615 1616 if (progress < xfer->ptp_sts_word_post) 1617 return; 1618 1619 ptp_read_system_postts(xfer->ptp_sts); 1620 1621 if (irqs_off) { 1622 local_irq_restore(ctlr->irq_flags); 1623 preempt_enable(); 1624 } 1625 1626 /* Capture the resolution of the timestamp */ 1627 xfer->ptp_sts_word_post = progress; 1628 1629 xfer->timestamped = true; 1630 } 1631 EXPORT_SYMBOL_GPL(spi_take_timestamp_post); 1632 1633 /** 1634 * spi_set_thread_rt - set the controller to pump at realtime priority 1635 * @ctlr: controller to boost priority of 1636 * 1637 * This can be called because the controller requested realtime priority 1638 * (by setting the ->rt value before calling spi_register_controller()) or 1639 * because a device on the bus said that its transfers needed realtime 1640 * priority. 1641 * 1642 * NOTE: at the moment if any device on a bus says it needs realtime then 1643 * the thread will be at realtime priority for all transfers on that 1644 * controller. If this eventually becomes a problem we may see if we can 1645 * find a way to boost the priority only temporarily during relevant 1646 * transfers. 1647 */ 1648 static void spi_set_thread_rt(struct spi_controller *ctlr) 1649 { 1650 dev_info(&ctlr->dev, 1651 "will run message pump with realtime priority\n"); 1652 sched_set_fifo(ctlr->kworker->task); 1653 } 1654 1655 static int spi_init_queue(struct spi_controller *ctlr) 1656 { 1657 ctlr->running = false; 1658 ctlr->busy = false; 1659 1660 ctlr->kworker = kthread_create_worker(0, dev_name(&ctlr->dev)); 1661 if (IS_ERR(ctlr->kworker)) { 1662 dev_err(&ctlr->dev, "failed to create message pump kworker\n"); 1663 return PTR_ERR(ctlr->kworker); 1664 } 1665 1666 kthread_init_work(&ctlr->pump_messages, spi_pump_messages); 1667 1668 /* 1669 * Controller config will indicate if this controller should run the 1670 * message pump with high (realtime) priority to reduce the transfer 1671 * latency on the bus by minimising the delay between a transfer 1672 * request and the scheduling of the message pump thread. Without this 1673 * setting the message pump thread will remain at default priority. 1674 */ 1675 if (ctlr->rt) 1676 spi_set_thread_rt(ctlr); 1677 1678 return 0; 1679 } 1680 1681 /** 1682 * spi_get_next_queued_message() - called by driver to check for queued 1683 * messages 1684 * @ctlr: the controller to check for queued messages 1685 * 1686 * If there are more messages in the queue, the next message is returned from 1687 * this call. 1688 * 1689 * Return: the next message in the queue, else NULL if the queue is empty. 1690 */ 1691 struct spi_message *spi_get_next_queued_message(struct spi_controller *ctlr) 1692 { 1693 struct spi_message *next; 1694 unsigned long flags; 1695 1696 /* get a pointer to the next message, if any */ 1697 spin_lock_irqsave(&ctlr->queue_lock, flags); 1698 next = list_first_entry_or_null(&ctlr->queue, struct spi_message, 1699 queue); 1700 spin_unlock_irqrestore(&ctlr->queue_lock, flags); 1701 1702 return next; 1703 } 1704 EXPORT_SYMBOL_GPL(spi_get_next_queued_message); 1705 1706 /** 1707 * spi_finalize_current_message() - the current message is complete 1708 * @ctlr: the controller to return the message to 1709 * 1710 * Called by the driver to notify the core that the message in the front of the 1711 * queue is complete and can be removed from the queue. 1712 */ 1713 void spi_finalize_current_message(struct spi_controller *ctlr) 1714 { 1715 struct spi_transfer *xfer; 1716 struct spi_message *mesg; 1717 unsigned long flags; 1718 int ret; 1719 1720 spin_lock_irqsave(&ctlr->queue_lock, flags); 1721 mesg = ctlr->cur_msg; 1722 spin_unlock_irqrestore(&ctlr->queue_lock, flags); 1723 1724 if (!ctlr->ptp_sts_supported && !ctlr->transfer_one) { 1725 list_for_each_entry(xfer, &mesg->transfers, transfer_list) { 1726 ptp_read_system_postts(xfer->ptp_sts); 1727 xfer->ptp_sts_word_post = xfer->len; 1728 } 1729 } 1730 1731 if (unlikely(ctlr->ptp_sts_supported)) 1732 list_for_each_entry(xfer, &mesg->transfers, transfer_list) 1733 WARN_ON_ONCE(xfer->ptp_sts && !xfer->timestamped); 1734 1735 spi_unmap_msg(ctlr, mesg); 1736 1737 /* In the prepare_messages callback the spi bus has the opportunity to 1738 * split a transfer to smaller chunks. 1739 * Release splited transfers here since spi_map_msg is done on the 1740 * splited transfers. 1741 */ 1742 spi_res_release(ctlr, mesg); 1743 1744 if (ctlr->cur_msg_prepared && ctlr->unprepare_message) { 1745 ret = ctlr->unprepare_message(ctlr, mesg); 1746 if (ret) { 1747 dev_err(&ctlr->dev, "failed to unprepare message: %d\n", 1748 ret); 1749 } 1750 } 1751 1752 spin_lock_irqsave(&ctlr->queue_lock, flags); 1753 ctlr->cur_msg = NULL; 1754 ctlr->cur_msg_prepared = false; 1755 ctlr->fallback = false; 1756 kthread_queue_work(ctlr->kworker, &ctlr->pump_messages); 1757 spin_unlock_irqrestore(&ctlr->queue_lock, flags); 1758 1759 trace_spi_message_done(mesg); 1760 1761 mesg->state = NULL; 1762 if (mesg->complete) 1763 mesg->complete(mesg->context); 1764 } 1765 EXPORT_SYMBOL_GPL(spi_finalize_current_message); 1766 1767 static int spi_start_queue(struct spi_controller *ctlr) 1768 { 1769 unsigned long flags; 1770 1771 spin_lock_irqsave(&ctlr->queue_lock, flags); 1772 1773 if (ctlr->running || ctlr->busy) { 1774 spin_unlock_irqrestore(&ctlr->queue_lock, flags); 1775 return -EBUSY; 1776 } 1777 1778 ctlr->running = true; 1779 ctlr->cur_msg = NULL; 1780 spin_unlock_irqrestore(&ctlr->queue_lock, flags); 1781 1782 kthread_queue_work(ctlr->kworker, &ctlr->pump_messages); 1783 1784 return 0; 1785 } 1786 1787 static int spi_stop_queue(struct spi_controller *ctlr) 1788 { 1789 unsigned long flags; 1790 unsigned limit = 500; 1791 int ret = 0; 1792 1793 spin_lock_irqsave(&ctlr->queue_lock, flags); 1794 1795 /* 1796 * This is a bit lame, but is optimized for the common execution path. 1797 * A wait_queue on the ctlr->busy could be used, but then the common 1798 * execution path (pump_messages) would be required to call wake_up or 1799 * friends on every SPI message. Do this instead. 1800 */ 1801 while ((!list_empty(&ctlr->queue) || ctlr->busy) && limit--) { 1802 spin_unlock_irqrestore(&ctlr->queue_lock, flags); 1803 usleep_range(10000, 11000); 1804 spin_lock_irqsave(&ctlr->queue_lock, flags); 1805 } 1806 1807 if (!list_empty(&ctlr->queue) || ctlr->busy) 1808 ret = -EBUSY; 1809 else 1810 ctlr->running = false; 1811 1812 spin_unlock_irqrestore(&ctlr->queue_lock, flags); 1813 1814 if (ret) { 1815 dev_warn(&ctlr->dev, "could not stop message queue\n"); 1816 return ret; 1817 } 1818 return ret; 1819 } 1820 1821 static int spi_destroy_queue(struct spi_controller *ctlr) 1822 { 1823 int ret; 1824 1825 ret = spi_stop_queue(ctlr); 1826 1827 /* 1828 * kthread_flush_worker will block until all work is done. 1829 * If the reason that stop_queue timed out is that the work will never 1830 * finish, then it does no good to call flush/stop thread, so 1831 * return anyway. 1832 */ 1833 if (ret) { 1834 dev_err(&ctlr->dev, "problem destroying queue\n"); 1835 return ret; 1836 } 1837 1838 kthread_destroy_worker(ctlr->kworker); 1839 1840 return 0; 1841 } 1842 1843 static int __spi_queued_transfer(struct spi_device *spi, 1844 struct spi_message *msg, 1845 bool need_pump) 1846 { 1847 struct spi_controller *ctlr = spi->controller; 1848 unsigned long flags; 1849 1850 spin_lock_irqsave(&ctlr->queue_lock, flags); 1851 1852 if (!ctlr->running) { 1853 spin_unlock_irqrestore(&ctlr->queue_lock, flags); 1854 return -ESHUTDOWN; 1855 } 1856 msg->actual_length = 0; 1857 msg->status = -EINPROGRESS; 1858 1859 list_add_tail(&msg->queue, &ctlr->queue); 1860 if (!ctlr->busy && need_pump) 1861 kthread_queue_work(ctlr->kworker, &ctlr->pump_messages); 1862 1863 spin_unlock_irqrestore(&ctlr->queue_lock, flags); 1864 return 0; 1865 } 1866 1867 /** 1868 * spi_queued_transfer - transfer function for queued transfers 1869 * @spi: spi device which is requesting transfer 1870 * @msg: spi message which is to handled is queued to driver queue 1871 * 1872 * Return: zero on success, else a negative error code. 1873 */ 1874 static int spi_queued_transfer(struct spi_device *spi, struct spi_message *msg) 1875 { 1876 return __spi_queued_transfer(spi, msg, true); 1877 } 1878 1879 static int spi_controller_initialize_queue(struct spi_controller *ctlr) 1880 { 1881 int ret; 1882 1883 ctlr->transfer = spi_queued_transfer; 1884 if (!ctlr->transfer_one_message) 1885 ctlr->transfer_one_message = spi_transfer_one_message; 1886 1887 /* Initialize and start queue */ 1888 ret = spi_init_queue(ctlr); 1889 if (ret) { 1890 dev_err(&ctlr->dev, "problem initializing queue\n"); 1891 goto err_init_queue; 1892 } 1893 ctlr->queued = true; 1894 ret = spi_start_queue(ctlr); 1895 if (ret) { 1896 dev_err(&ctlr->dev, "problem starting queue\n"); 1897 goto err_start_queue; 1898 } 1899 1900 return 0; 1901 1902 err_start_queue: 1903 spi_destroy_queue(ctlr); 1904 err_init_queue: 1905 return ret; 1906 } 1907 1908 /** 1909 * spi_flush_queue - Send all pending messages in the queue from the callers' 1910 * context 1911 * @ctlr: controller to process queue for 1912 * 1913 * This should be used when one wants to ensure all pending messages have been 1914 * sent before doing something. Is used by the spi-mem code to make sure SPI 1915 * memory operations do not preempt regular SPI transfers that have been queued 1916 * before the spi-mem operation. 1917 */ 1918 void spi_flush_queue(struct spi_controller *ctlr) 1919 { 1920 if (ctlr->transfer == spi_queued_transfer) 1921 __spi_pump_messages(ctlr, false); 1922 } 1923 1924 /*-------------------------------------------------------------------------*/ 1925 1926 #if defined(CONFIG_OF) 1927 static int of_spi_parse_dt(struct spi_controller *ctlr, struct spi_device *spi, 1928 struct device_node *nc) 1929 { 1930 u32 value; 1931 int rc; 1932 1933 /* Mode (clock phase/polarity/etc.) */ 1934 if (of_property_read_bool(nc, "spi-cpha")) 1935 spi->mode |= SPI_CPHA; 1936 if (of_property_read_bool(nc, "spi-cpol")) 1937 spi->mode |= SPI_CPOL; 1938 if (of_property_read_bool(nc, "spi-3wire")) 1939 spi->mode |= SPI_3WIRE; 1940 if (of_property_read_bool(nc, "spi-lsb-first")) 1941 spi->mode |= SPI_LSB_FIRST; 1942 if (of_property_read_bool(nc, "spi-cs-high")) 1943 spi->mode |= SPI_CS_HIGH; 1944 1945 /* Device DUAL/QUAD mode */ 1946 if (!of_property_read_u32(nc, "spi-tx-bus-width", &value)) { 1947 switch (value) { 1948 case 1: 1949 break; 1950 case 2: 1951 spi->mode |= SPI_TX_DUAL; 1952 break; 1953 case 4: 1954 spi->mode |= SPI_TX_QUAD; 1955 break; 1956 case 8: 1957 spi->mode |= SPI_TX_OCTAL; 1958 break; 1959 default: 1960 dev_warn(&ctlr->dev, 1961 "spi-tx-bus-width %d not supported\n", 1962 value); 1963 break; 1964 } 1965 } 1966 1967 if (!of_property_read_u32(nc, "spi-rx-bus-width", &value)) { 1968 switch (value) { 1969 case 1: 1970 break; 1971 case 2: 1972 spi->mode |= SPI_RX_DUAL; 1973 break; 1974 case 4: 1975 spi->mode |= SPI_RX_QUAD; 1976 break; 1977 case 8: 1978 spi->mode |= SPI_RX_OCTAL; 1979 break; 1980 default: 1981 dev_warn(&ctlr->dev, 1982 "spi-rx-bus-width %d not supported\n", 1983 value); 1984 break; 1985 } 1986 } 1987 1988 if (spi_controller_is_slave(ctlr)) { 1989 if (!of_node_name_eq(nc, "slave")) { 1990 dev_err(&ctlr->dev, "%pOF is not called 'slave'\n", 1991 nc); 1992 return -EINVAL; 1993 } 1994 return 0; 1995 } 1996 1997 /* Device address */ 1998 rc = of_property_read_u32(nc, "reg", &value); 1999 if (rc) { 2000 dev_err(&ctlr->dev, "%pOF has no valid 'reg' property (%d)\n", 2001 nc, rc); 2002 return rc; 2003 } 2004 spi->chip_select = value; 2005 2006 /* Device speed */ 2007 if (!of_property_read_u32(nc, "spi-max-frequency", &value)) 2008 spi->max_speed_hz = value; 2009 2010 return 0; 2011 } 2012 2013 static struct spi_device * 2014 of_register_spi_device(struct spi_controller *ctlr, struct device_node *nc) 2015 { 2016 struct spi_device *spi; 2017 int rc; 2018 2019 /* Alloc an spi_device */ 2020 spi = spi_alloc_device(ctlr); 2021 if (!spi) { 2022 dev_err(&ctlr->dev, "spi_device alloc error for %pOF\n", nc); 2023 rc = -ENOMEM; 2024 goto err_out; 2025 } 2026 2027 /* Select device driver */ 2028 rc = of_modalias_node(nc, spi->modalias, 2029 sizeof(spi->modalias)); 2030 if (rc < 0) { 2031 dev_err(&ctlr->dev, "cannot find modalias for %pOF\n", nc); 2032 goto err_out; 2033 } 2034 2035 rc = of_spi_parse_dt(ctlr, spi, nc); 2036 if (rc) 2037 goto err_out; 2038 2039 /* Store a pointer to the node in the device structure */ 2040 of_node_get(nc); 2041 spi->dev.of_node = nc; 2042 2043 /* Register the new device */ 2044 rc = spi_add_device(spi); 2045 if (rc) { 2046 dev_err(&ctlr->dev, "spi_device register error %pOF\n", nc); 2047 goto err_of_node_put; 2048 } 2049 2050 return spi; 2051 2052 err_of_node_put: 2053 of_node_put(nc); 2054 err_out: 2055 spi_dev_put(spi); 2056 return ERR_PTR(rc); 2057 } 2058 2059 /** 2060 * of_register_spi_devices() - Register child devices onto the SPI bus 2061 * @ctlr: Pointer to spi_controller device 2062 * 2063 * Registers an spi_device for each child node of controller node which 2064 * represents a valid SPI slave. 2065 */ 2066 static void of_register_spi_devices(struct spi_controller *ctlr) 2067 { 2068 struct spi_device *spi; 2069 struct device_node *nc; 2070 2071 if (!ctlr->dev.of_node) 2072 return; 2073 2074 for_each_available_child_of_node(ctlr->dev.of_node, nc) { 2075 if (of_node_test_and_set_flag(nc, OF_POPULATED)) 2076 continue; 2077 spi = of_register_spi_device(ctlr, nc); 2078 if (IS_ERR(spi)) { 2079 dev_warn(&ctlr->dev, 2080 "Failed to create SPI device for %pOF\n", nc); 2081 of_node_clear_flag(nc, OF_POPULATED); 2082 } 2083 } 2084 } 2085 #else 2086 static void of_register_spi_devices(struct spi_controller *ctlr) { } 2087 #endif 2088 2089 #ifdef CONFIG_ACPI 2090 struct acpi_spi_lookup { 2091 struct spi_controller *ctlr; 2092 u32 max_speed_hz; 2093 u32 mode; 2094 int irq; 2095 u8 bits_per_word; 2096 u8 chip_select; 2097 }; 2098 2099 static void acpi_spi_parse_apple_properties(struct acpi_device *dev, 2100 struct acpi_spi_lookup *lookup) 2101 { 2102 const union acpi_object *obj; 2103 2104 if (!x86_apple_machine) 2105 return; 2106 2107 if (!acpi_dev_get_property(dev, "spiSclkPeriod", ACPI_TYPE_BUFFER, &obj) 2108 && obj->buffer.length >= 4) 2109 lookup->max_speed_hz = NSEC_PER_SEC / *(u32 *)obj->buffer.pointer; 2110 2111 if (!acpi_dev_get_property(dev, "spiWordSize", ACPI_TYPE_BUFFER, &obj) 2112 && obj->buffer.length == 8) 2113 lookup->bits_per_word = *(u64 *)obj->buffer.pointer; 2114 2115 if (!acpi_dev_get_property(dev, "spiBitOrder", ACPI_TYPE_BUFFER, &obj) 2116 && obj->buffer.length == 8 && !*(u64 *)obj->buffer.pointer) 2117 lookup->mode |= SPI_LSB_FIRST; 2118 2119 if (!acpi_dev_get_property(dev, "spiSPO", ACPI_TYPE_BUFFER, &obj) 2120 && obj->buffer.length == 8 && *(u64 *)obj->buffer.pointer) 2121 lookup->mode |= SPI_CPOL; 2122 2123 if (!acpi_dev_get_property(dev, "spiSPH", ACPI_TYPE_BUFFER, &obj) 2124 && obj->buffer.length == 8 && *(u64 *)obj->buffer.pointer) 2125 lookup->mode |= SPI_CPHA; 2126 } 2127 2128 static int acpi_spi_add_resource(struct acpi_resource *ares, void *data) 2129 { 2130 struct acpi_spi_lookup *lookup = data; 2131 struct spi_controller *ctlr = lookup->ctlr; 2132 2133 if (ares->type == ACPI_RESOURCE_TYPE_SERIAL_BUS) { 2134 struct acpi_resource_spi_serialbus *sb; 2135 acpi_handle parent_handle; 2136 acpi_status status; 2137 2138 sb = &ares->data.spi_serial_bus; 2139 if (sb->type == ACPI_RESOURCE_SERIAL_TYPE_SPI) { 2140 2141 status = acpi_get_handle(NULL, 2142 sb->resource_source.string_ptr, 2143 &parent_handle); 2144 2145 if (ACPI_FAILURE(status) || 2146 ACPI_HANDLE(ctlr->dev.parent) != parent_handle) 2147 return -ENODEV; 2148 2149 /* 2150 * ACPI DeviceSelection numbering is handled by the 2151 * host controller driver in Windows and can vary 2152 * from driver to driver. In Linux we always expect 2153 * 0 .. max - 1 so we need to ask the driver to 2154 * translate between the two schemes. 2155 */ 2156 if (ctlr->fw_translate_cs) { 2157 int cs = ctlr->fw_translate_cs(ctlr, 2158 sb->device_selection); 2159 if (cs < 0) 2160 return cs; 2161 lookup->chip_select = cs; 2162 } else { 2163 lookup->chip_select = sb->device_selection; 2164 } 2165 2166 lookup->max_speed_hz = sb->connection_speed; 2167 lookup->bits_per_word = sb->data_bit_length; 2168 2169 if (sb->clock_phase == ACPI_SPI_SECOND_PHASE) 2170 lookup->mode |= SPI_CPHA; 2171 if (sb->clock_polarity == ACPI_SPI_START_HIGH) 2172 lookup->mode |= SPI_CPOL; 2173 if (sb->device_polarity == ACPI_SPI_ACTIVE_HIGH) 2174 lookup->mode |= SPI_CS_HIGH; 2175 } 2176 } else if (lookup->irq < 0) { 2177 struct resource r; 2178 2179 if (acpi_dev_resource_interrupt(ares, 0, &r)) 2180 lookup->irq = r.start; 2181 } 2182 2183 /* Always tell the ACPI core to skip this resource */ 2184 return 1; 2185 } 2186 2187 static acpi_status acpi_register_spi_device(struct spi_controller *ctlr, 2188 struct acpi_device *adev) 2189 { 2190 acpi_handle parent_handle = NULL; 2191 struct list_head resource_list; 2192 struct acpi_spi_lookup lookup = {}; 2193 struct spi_device *spi; 2194 int ret; 2195 2196 if (acpi_bus_get_status(adev) || !adev->status.present || 2197 acpi_device_enumerated(adev)) 2198 return AE_OK; 2199 2200 lookup.ctlr = ctlr; 2201 lookup.irq = -1; 2202 2203 INIT_LIST_HEAD(&resource_list); 2204 ret = acpi_dev_get_resources(adev, &resource_list, 2205 acpi_spi_add_resource, &lookup); 2206 acpi_dev_free_resource_list(&resource_list); 2207 2208 if (ret < 0) 2209 /* found SPI in _CRS but it points to another controller */ 2210 return AE_OK; 2211 2212 if (!lookup.max_speed_hz && 2213 ACPI_SUCCESS(acpi_get_parent(adev->handle, &parent_handle)) && 2214 ACPI_HANDLE(ctlr->dev.parent) == parent_handle) { 2215 /* Apple does not use _CRS but nested devices for SPI slaves */ 2216 acpi_spi_parse_apple_properties(adev, &lookup); 2217 } 2218 2219 if (!lookup.max_speed_hz) 2220 return AE_OK; 2221 2222 spi = spi_alloc_device(ctlr); 2223 if (!spi) { 2224 dev_err(&ctlr->dev, "failed to allocate SPI device for %s\n", 2225 dev_name(&adev->dev)); 2226 return AE_NO_MEMORY; 2227 } 2228 2229 2230 ACPI_COMPANION_SET(&spi->dev, adev); 2231 spi->max_speed_hz = lookup.max_speed_hz; 2232 spi->mode |= lookup.mode; 2233 spi->irq = lookup.irq; 2234 spi->bits_per_word = lookup.bits_per_word; 2235 spi->chip_select = lookup.chip_select; 2236 2237 acpi_set_modalias(adev, acpi_device_hid(adev), spi->modalias, 2238 sizeof(spi->modalias)); 2239 2240 if (spi->irq < 0) 2241 spi->irq = acpi_dev_gpio_irq_get(adev, 0); 2242 2243 acpi_device_set_enumerated(adev); 2244 2245 adev->power.flags.ignore_parent = true; 2246 if (spi_add_device(spi)) { 2247 adev->power.flags.ignore_parent = false; 2248 dev_err(&ctlr->dev, "failed to add SPI device %s from ACPI\n", 2249 dev_name(&adev->dev)); 2250 spi_dev_put(spi); 2251 } 2252 2253 return AE_OK; 2254 } 2255 2256 static acpi_status acpi_spi_add_device(acpi_handle handle, u32 level, 2257 void *data, void **return_value) 2258 { 2259 struct spi_controller *ctlr = data; 2260 struct acpi_device *adev; 2261 2262 if (acpi_bus_get_device(handle, &adev)) 2263 return AE_OK; 2264 2265 return acpi_register_spi_device(ctlr, adev); 2266 } 2267 2268 #define SPI_ACPI_ENUMERATE_MAX_DEPTH 32 2269 2270 static void acpi_register_spi_devices(struct spi_controller *ctlr) 2271 { 2272 acpi_status status; 2273 acpi_handle handle; 2274 2275 handle = ACPI_HANDLE(ctlr->dev.parent); 2276 if (!handle) 2277 return; 2278 2279 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT, 2280 SPI_ACPI_ENUMERATE_MAX_DEPTH, 2281 acpi_spi_add_device, NULL, ctlr, NULL); 2282 if (ACPI_FAILURE(status)) 2283 dev_warn(&ctlr->dev, "failed to enumerate SPI slaves\n"); 2284 } 2285 #else 2286 static inline void acpi_register_spi_devices(struct spi_controller *ctlr) {} 2287 #endif /* CONFIG_ACPI */ 2288 2289 static void spi_controller_release(struct device *dev) 2290 { 2291 struct spi_controller *ctlr; 2292 2293 ctlr = container_of(dev, struct spi_controller, dev); 2294 kfree(ctlr); 2295 } 2296 2297 static struct class spi_master_class = { 2298 .name = "spi_master", 2299 .owner = THIS_MODULE, 2300 .dev_release = spi_controller_release, 2301 .dev_groups = spi_master_groups, 2302 }; 2303 2304 #ifdef CONFIG_SPI_SLAVE 2305 /** 2306 * spi_slave_abort - abort the ongoing transfer request on an SPI slave 2307 * controller 2308 * @spi: device used for the current transfer 2309 */ 2310 int spi_slave_abort(struct spi_device *spi) 2311 { 2312 struct spi_controller *ctlr = spi->controller; 2313 2314 if (spi_controller_is_slave(ctlr) && ctlr->slave_abort) 2315 return ctlr->slave_abort(ctlr); 2316 2317 return -ENOTSUPP; 2318 } 2319 EXPORT_SYMBOL_GPL(spi_slave_abort); 2320 2321 static int match_true(struct device *dev, void *data) 2322 { 2323 return 1; 2324 } 2325 2326 static ssize_t slave_show(struct device *dev, struct device_attribute *attr, 2327 char *buf) 2328 { 2329 struct spi_controller *ctlr = container_of(dev, struct spi_controller, 2330 dev); 2331 struct device *child; 2332 2333 child = device_find_child(&ctlr->dev, NULL, match_true); 2334 return sprintf(buf, "%s\n", 2335 child ? to_spi_device(child)->modalias : NULL); 2336 } 2337 2338 static ssize_t slave_store(struct device *dev, struct device_attribute *attr, 2339 const char *buf, size_t count) 2340 { 2341 struct spi_controller *ctlr = container_of(dev, struct spi_controller, 2342 dev); 2343 struct spi_device *spi; 2344 struct device *child; 2345 char name[32]; 2346 int rc; 2347 2348 rc = sscanf(buf, "%31s", name); 2349 if (rc != 1 || !name[0]) 2350 return -EINVAL; 2351 2352 child = device_find_child(&ctlr->dev, NULL, match_true); 2353 if (child) { 2354 /* Remove registered slave */ 2355 device_unregister(child); 2356 put_device(child); 2357 } 2358 2359 if (strcmp(name, "(null)")) { 2360 /* Register new slave */ 2361 spi = spi_alloc_device(ctlr); 2362 if (!spi) 2363 return -ENOMEM; 2364 2365 strlcpy(spi->modalias, name, sizeof(spi->modalias)); 2366 2367 rc = spi_add_device(spi); 2368 if (rc) { 2369 spi_dev_put(spi); 2370 return rc; 2371 } 2372 } 2373 2374 return count; 2375 } 2376 2377 static DEVICE_ATTR_RW(slave); 2378 2379 static struct attribute *spi_slave_attrs[] = { 2380 &dev_attr_slave.attr, 2381 NULL, 2382 }; 2383 2384 static const struct attribute_group spi_slave_group = { 2385 .attrs = spi_slave_attrs, 2386 }; 2387 2388 static const struct attribute_group *spi_slave_groups[] = { 2389 &spi_controller_statistics_group, 2390 &spi_slave_group, 2391 NULL, 2392 }; 2393 2394 static struct class spi_slave_class = { 2395 .name = "spi_slave", 2396 .owner = THIS_MODULE, 2397 .dev_release = spi_controller_release, 2398 .dev_groups = spi_slave_groups, 2399 }; 2400 #else 2401 extern struct class spi_slave_class; /* dummy */ 2402 #endif 2403 2404 /** 2405 * __spi_alloc_controller - allocate an SPI master or slave controller 2406 * @dev: the controller, possibly using the platform_bus 2407 * @size: how much zeroed driver-private data to allocate; the pointer to this 2408 * memory is in the driver_data field of the returned device, accessible 2409 * with spi_controller_get_devdata(); the memory is cacheline aligned; 2410 * drivers granting DMA access to portions of their private data need to 2411 * round up @size using ALIGN(size, dma_get_cache_alignment()). 2412 * @slave: flag indicating whether to allocate an SPI master (false) or SPI 2413 * slave (true) controller 2414 * Context: can sleep 2415 * 2416 * This call is used only by SPI controller drivers, which are the 2417 * only ones directly touching chip registers. It's how they allocate 2418 * an spi_controller structure, prior to calling spi_register_controller(). 2419 * 2420 * This must be called from context that can sleep. 2421 * 2422 * The caller is responsible for assigning the bus number and initializing the 2423 * controller's methods before calling spi_register_controller(); and (after 2424 * errors adding the device) calling spi_controller_put() to prevent a memory 2425 * leak. 2426 * 2427 * Return: the SPI controller structure on success, else NULL. 2428 */ 2429 struct spi_controller *__spi_alloc_controller(struct device *dev, 2430 unsigned int size, bool slave) 2431 { 2432 struct spi_controller *ctlr; 2433 size_t ctlr_size = ALIGN(sizeof(*ctlr), dma_get_cache_alignment()); 2434 2435 if (!dev) 2436 return NULL; 2437 2438 ctlr = kzalloc(size + ctlr_size, GFP_KERNEL); 2439 if (!ctlr) 2440 return NULL; 2441 2442 device_initialize(&ctlr->dev); 2443 ctlr->bus_num = -1; 2444 ctlr->num_chipselect = 1; 2445 ctlr->slave = slave; 2446 if (IS_ENABLED(CONFIG_SPI_SLAVE) && slave) 2447 ctlr->dev.class = &spi_slave_class; 2448 else 2449 ctlr->dev.class = &spi_master_class; 2450 ctlr->dev.parent = dev; 2451 pm_suspend_ignore_children(&ctlr->dev, true); 2452 spi_controller_set_devdata(ctlr, (void *)ctlr + ctlr_size); 2453 2454 return ctlr; 2455 } 2456 EXPORT_SYMBOL_GPL(__spi_alloc_controller); 2457 2458 static void devm_spi_release_controller(struct device *dev, void *ctlr) 2459 { 2460 spi_controller_put(*(struct spi_controller **)ctlr); 2461 } 2462 2463 /** 2464 * __devm_spi_alloc_controller - resource-managed __spi_alloc_controller() 2465 * @dev: physical device of SPI controller 2466 * @size: how much zeroed driver-private data to allocate 2467 * @slave: whether to allocate an SPI master (false) or SPI slave (true) 2468 * Context: can sleep 2469 * 2470 * Allocate an SPI controller and automatically release a reference on it 2471 * when @dev is unbound from its driver. Drivers are thus relieved from 2472 * having to call spi_controller_put(). 2473 * 2474 * The arguments to this function are identical to __spi_alloc_controller(). 2475 * 2476 * Return: the SPI controller structure on success, else NULL. 2477 */ 2478 struct spi_controller *__devm_spi_alloc_controller(struct device *dev, 2479 unsigned int size, 2480 bool slave) 2481 { 2482 struct spi_controller **ptr, *ctlr; 2483 2484 ptr = devres_alloc(devm_spi_release_controller, sizeof(*ptr), 2485 GFP_KERNEL); 2486 if (!ptr) 2487 return NULL; 2488 2489 ctlr = __spi_alloc_controller(dev, size, slave); 2490 if (ctlr) { 2491 *ptr = ctlr; 2492 devres_add(dev, ptr); 2493 } else { 2494 devres_free(ptr); 2495 } 2496 2497 return ctlr; 2498 } 2499 EXPORT_SYMBOL_GPL(__devm_spi_alloc_controller); 2500 2501 #ifdef CONFIG_OF 2502 static int of_spi_get_gpio_numbers(struct spi_controller *ctlr) 2503 { 2504 int nb, i, *cs; 2505 struct device_node *np = ctlr->dev.of_node; 2506 2507 if (!np) 2508 return 0; 2509 2510 nb = of_gpio_named_count(np, "cs-gpios"); 2511 ctlr->num_chipselect = max_t(int, nb, ctlr->num_chipselect); 2512 2513 /* Return error only for an incorrectly formed cs-gpios property */ 2514 if (nb == 0 || nb == -ENOENT) 2515 return 0; 2516 else if (nb < 0) 2517 return nb; 2518 2519 cs = devm_kcalloc(&ctlr->dev, ctlr->num_chipselect, sizeof(int), 2520 GFP_KERNEL); 2521 ctlr->cs_gpios = cs; 2522 2523 if (!ctlr->cs_gpios) 2524 return -ENOMEM; 2525 2526 for (i = 0; i < ctlr->num_chipselect; i++) 2527 cs[i] = -ENOENT; 2528 2529 for (i = 0; i < nb; i++) 2530 cs[i] = of_get_named_gpio(np, "cs-gpios", i); 2531 2532 return 0; 2533 } 2534 #else 2535 static int of_spi_get_gpio_numbers(struct spi_controller *ctlr) 2536 { 2537 return 0; 2538 } 2539 #endif 2540 2541 /** 2542 * spi_get_gpio_descs() - grab chip select GPIOs for the master 2543 * @ctlr: The SPI master to grab GPIO descriptors for 2544 */ 2545 static int spi_get_gpio_descs(struct spi_controller *ctlr) 2546 { 2547 int nb, i; 2548 struct gpio_desc **cs; 2549 struct device *dev = &ctlr->dev; 2550 unsigned long native_cs_mask = 0; 2551 unsigned int num_cs_gpios = 0; 2552 2553 nb = gpiod_count(dev, "cs"); 2554 ctlr->num_chipselect = max_t(int, nb, ctlr->num_chipselect); 2555 2556 /* No GPIOs at all is fine, else return the error */ 2557 if (nb == 0 || nb == -ENOENT) 2558 return 0; 2559 else if (nb < 0) 2560 return nb; 2561 2562 cs = devm_kcalloc(dev, ctlr->num_chipselect, sizeof(*cs), 2563 GFP_KERNEL); 2564 if (!cs) 2565 return -ENOMEM; 2566 ctlr->cs_gpiods = cs; 2567 2568 for (i = 0; i < nb; i++) { 2569 /* 2570 * Most chipselects are active low, the inverted 2571 * semantics are handled by special quirks in gpiolib, 2572 * so initializing them GPIOD_OUT_LOW here means 2573 * "unasserted", in most cases this will drive the physical 2574 * line high. 2575 */ 2576 cs[i] = devm_gpiod_get_index_optional(dev, "cs", i, 2577 GPIOD_OUT_LOW); 2578 if (IS_ERR(cs[i])) 2579 return PTR_ERR(cs[i]); 2580 2581 if (cs[i]) { 2582 /* 2583 * If we find a CS GPIO, name it after the device and 2584 * chip select line. 2585 */ 2586 char *gpioname; 2587 2588 gpioname = devm_kasprintf(dev, GFP_KERNEL, "%s CS%d", 2589 dev_name(dev), i); 2590 if (!gpioname) 2591 return -ENOMEM; 2592 gpiod_set_consumer_name(cs[i], gpioname); 2593 num_cs_gpios++; 2594 continue; 2595 } 2596 2597 if (ctlr->max_native_cs && i >= ctlr->max_native_cs) { 2598 dev_err(dev, "Invalid native chip select %d\n", i); 2599 return -EINVAL; 2600 } 2601 native_cs_mask |= BIT(i); 2602 } 2603 2604 ctlr->unused_native_cs = ffz(native_cs_mask); 2605 if (num_cs_gpios && ctlr->max_native_cs && 2606 ctlr->unused_native_cs >= ctlr->max_native_cs) { 2607 dev_err(dev, "No unused native chip select available\n"); 2608 return -EINVAL; 2609 } 2610 2611 return 0; 2612 } 2613 2614 static int spi_controller_check_ops(struct spi_controller *ctlr) 2615 { 2616 /* 2617 * The controller may implement only the high-level SPI-memory like 2618 * operations if it does not support regular SPI transfers, and this is 2619 * valid use case. 2620 * If ->mem_ops is NULL, we request that at least one of the 2621 * ->transfer_xxx() method be implemented. 2622 */ 2623 if (ctlr->mem_ops) { 2624 if (!ctlr->mem_ops->exec_op) 2625 return -EINVAL; 2626 } else if (!ctlr->transfer && !ctlr->transfer_one && 2627 !ctlr->transfer_one_message) { 2628 return -EINVAL; 2629 } 2630 2631 return 0; 2632 } 2633 2634 /** 2635 * spi_register_controller - register SPI master or slave controller 2636 * @ctlr: initialized master, originally from spi_alloc_master() or 2637 * spi_alloc_slave() 2638 * Context: can sleep 2639 * 2640 * SPI controllers connect to their drivers using some non-SPI bus, 2641 * such as the platform bus. The final stage of probe() in that code 2642 * includes calling spi_register_controller() to hook up to this SPI bus glue. 2643 * 2644 * SPI controllers use board specific (often SOC specific) bus numbers, 2645 * and board-specific addressing for SPI devices combines those numbers 2646 * with chip select numbers. Since SPI does not directly support dynamic 2647 * device identification, boards need configuration tables telling which 2648 * chip is at which address. 2649 * 2650 * This must be called from context that can sleep. It returns zero on 2651 * success, else a negative error code (dropping the controller's refcount). 2652 * After a successful return, the caller is responsible for calling 2653 * spi_unregister_controller(). 2654 * 2655 * Return: zero on success, else a negative error code. 2656 */ 2657 int spi_register_controller(struct spi_controller *ctlr) 2658 { 2659 struct device *dev = ctlr->dev.parent; 2660 struct boardinfo *bi; 2661 int status; 2662 int id, first_dynamic; 2663 2664 if (!dev) 2665 return -ENODEV; 2666 2667 /* 2668 * Make sure all necessary hooks are implemented before registering 2669 * the SPI controller. 2670 */ 2671 status = spi_controller_check_ops(ctlr); 2672 if (status) 2673 return status; 2674 2675 if (ctlr->bus_num >= 0) { 2676 /* devices with a fixed bus num must check-in with the num */ 2677 mutex_lock(&board_lock); 2678 id = idr_alloc(&spi_master_idr, ctlr, ctlr->bus_num, 2679 ctlr->bus_num + 1, GFP_KERNEL); 2680 mutex_unlock(&board_lock); 2681 if (WARN(id < 0, "couldn't get idr")) 2682 return id == -ENOSPC ? -EBUSY : id; 2683 ctlr->bus_num = id; 2684 } else if (ctlr->dev.of_node) { 2685 /* allocate dynamic bus number using Linux idr */ 2686 id = of_alias_get_id(ctlr->dev.of_node, "spi"); 2687 if (id >= 0) { 2688 ctlr->bus_num = id; 2689 mutex_lock(&board_lock); 2690 id = idr_alloc(&spi_master_idr, ctlr, ctlr->bus_num, 2691 ctlr->bus_num + 1, GFP_KERNEL); 2692 mutex_unlock(&board_lock); 2693 if (WARN(id < 0, "couldn't get idr")) 2694 return id == -ENOSPC ? -EBUSY : id; 2695 } 2696 } 2697 if (ctlr->bus_num < 0) { 2698 first_dynamic = of_alias_get_highest_id("spi"); 2699 if (first_dynamic < 0) 2700 first_dynamic = 0; 2701 else 2702 first_dynamic++; 2703 2704 mutex_lock(&board_lock); 2705 id = idr_alloc(&spi_master_idr, ctlr, first_dynamic, 2706 0, GFP_KERNEL); 2707 mutex_unlock(&board_lock); 2708 if (WARN(id < 0, "couldn't get idr")) 2709 return id; 2710 ctlr->bus_num = id; 2711 } 2712 INIT_LIST_HEAD(&ctlr->queue); 2713 spin_lock_init(&ctlr->queue_lock); 2714 spin_lock_init(&ctlr->bus_lock_spinlock); 2715 mutex_init(&ctlr->bus_lock_mutex); 2716 mutex_init(&ctlr->io_mutex); 2717 ctlr->bus_lock_flag = 0; 2718 init_completion(&ctlr->xfer_completion); 2719 if (!ctlr->max_dma_len) 2720 ctlr->max_dma_len = INT_MAX; 2721 2722 /* register the device, then userspace will see it. 2723 * registration fails if the bus ID is in use. 2724 */ 2725 dev_set_name(&ctlr->dev, "spi%u", ctlr->bus_num); 2726 2727 if (!spi_controller_is_slave(ctlr)) { 2728 if (ctlr->use_gpio_descriptors) { 2729 status = spi_get_gpio_descs(ctlr); 2730 if (status) 2731 goto free_bus_id; 2732 /* 2733 * A controller using GPIO descriptors always 2734 * supports SPI_CS_HIGH if need be. 2735 */ 2736 ctlr->mode_bits |= SPI_CS_HIGH; 2737 } else { 2738 /* Legacy code path for GPIOs from DT */ 2739 status = of_spi_get_gpio_numbers(ctlr); 2740 if (status) 2741 goto free_bus_id; 2742 } 2743 } 2744 2745 /* 2746 * Even if it's just one always-selected device, there must 2747 * be at least one chipselect. 2748 */ 2749 if (!ctlr->num_chipselect) { 2750 status = -EINVAL; 2751 goto free_bus_id; 2752 } 2753 2754 status = device_add(&ctlr->dev); 2755 if (status < 0) 2756 goto free_bus_id; 2757 dev_dbg(dev, "registered %s %s\n", 2758 spi_controller_is_slave(ctlr) ? "slave" : "master", 2759 dev_name(&ctlr->dev)); 2760 2761 /* 2762 * If we're using a queued driver, start the queue. Note that we don't 2763 * need the queueing logic if the driver is only supporting high-level 2764 * memory operations. 2765 */ 2766 if (ctlr->transfer) { 2767 dev_info(dev, "controller is unqueued, this is deprecated\n"); 2768 } else if (ctlr->transfer_one || ctlr->transfer_one_message) { 2769 status = spi_controller_initialize_queue(ctlr); 2770 if (status) { 2771 device_del(&ctlr->dev); 2772 goto free_bus_id; 2773 } 2774 } 2775 /* add statistics */ 2776 spin_lock_init(&ctlr->statistics.lock); 2777 2778 mutex_lock(&board_lock); 2779 list_add_tail(&ctlr->list, &spi_controller_list); 2780 list_for_each_entry(bi, &board_list, list) 2781 spi_match_controller_to_boardinfo(ctlr, &bi->board_info); 2782 mutex_unlock(&board_lock); 2783 2784 /* Register devices from the device tree and ACPI */ 2785 of_register_spi_devices(ctlr); 2786 acpi_register_spi_devices(ctlr); 2787 return status; 2788 2789 free_bus_id: 2790 mutex_lock(&board_lock); 2791 idr_remove(&spi_master_idr, ctlr->bus_num); 2792 mutex_unlock(&board_lock); 2793 return status; 2794 } 2795 EXPORT_SYMBOL_GPL(spi_register_controller); 2796 2797 static void devm_spi_unregister(struct device *dev, void *res) 2798 { 2799 spi_unregister_controller(*(struct spi_controller **)res); 2800 } 2801 2802 /** 2803 * devm_spi_register_controller - register managed SPI master or slave 2804 * controller 2805 * @dev: device managing SPI controller 2806 * @ctlr: initialized controller, originally from spi_alloc_master() or 2807 * spi_alloc_slave() 2808 * Context: can sleep 2809 * 2810 * Register a SPI device as with spi_register_controller() which will 2811 * automatically be unregistered and freed. 2812 * 2813 * Return: zero on success, else a negative error code. 2814 */ 2815 int devm_spi_register_controller(struct device *dev, 2816 struct spi_controller *ctlr) 2817 { 2818 struct spi_controller **ptr; 2819 int ret; 2820 2821 ptr = devres_alloc(devm_spi_unregister, sizeof(*ptr), GFP_KERNEL); 2822 if (!ptr) 2823 return -ENOMEM; 2824 2825 ret = spi_register_controller(ctlr); 2826 if (!ret) { 2827 *ptr = ctlr; 2828 devres_add(dev, ptr); 2829 } else { 2830 devres_free(ptr); 2831 } 2832 2833 return ret; 2834 } 2835 EXPORT_SYMBOL_GPL(devm_spi_register_controller); 2836 2837 static int devm_spi_match_controller(struct device *dev, void *res, void *ctlr) 2838 { 2839 return *(struct spi_controller **)res == ctlr; 2840 } 2841 2842 static int __unregister(struct device *dev, void *null) 2843 { 2844 spi_unregister_device(to_spi_device(dev)); 2845 return 0; 2846 } 2847 2848 /** 2849 * spi_unregister_controller - unregister SPI master or slave controller 2850 * @ctlr: the controller being unregistered 2851 * Context: can sleep 2852 * 2853 * This call is used only by SPI controller drivers, which are the 2854 * only ones directly touching chip registers. 2855 * 2856 * This must be called from context that can sleep. 2857 * 2858 * Note that this function also drops a reference to the controller. 2859 */ 2860 void spi_unregister_controller(struct spi_controller *ctlr) 2861 { 2862 struct spi_controller *found; 2863 int id = ctlr->bus_num; 2864 2865 /* Prevent addition of new devices, unregister existing ones */ 2866 if (IS_ENABLED(CONFIG_SPI_DYNAMIC)) 2867 mutex_lock(&spi_add_lock); 2868 2869 device_for_each_child(&ctlr->dev, NULL, __unregister); 2870 2871 /* First make sure that this controller was ever added */ 2872 mutex_lock(&board_lock); 2873 found = idr_find(&spi_master_idr, id); 2874 mutex_unlock(&board_lock); 2875 if (ctlr->queued) { 2876 if (spi_destroy_queue(ctlr)) 2877 dev_err(&ctlr->dev, "queue remove failed\n"); 2878 } 2879 mutex_lock(&board_lock); 2880 list_del(&ctlr->list); 2881 mutex_unlock(&board_lock); 2882 2883 device_del(&ctlr->dev); 2884 2885 /* Release the last reference on the controller if its driver 2886 * has not yet been converted to devm_spi_alloc_master/slave(). 2887 */ 2888 if (!devres_find(ctlr->dev.parent, devm_spi_release_controller, 2889 devm_spi_match_controller, ctlr)) 2890 put_device(&ctlr->dev); 2891 2892 /* free bus id */ 2893 mutex_lock(&board_lock); 2894 if (found == ctlr) 2895 idr_remove(&spi_master_idr, id); 2896 mutex_unlock(&board_lock); 2897 2898 if (IS_ENABLED(CONFIG_SPI_DYNAMIC)) 2899 mutex_unlock(&spi_add_lock); 2900 } 2901 EXPORT_SYMBOL_GPL(spi_unregister_controller); 2902 2903 int spi_controller_suspend(struct spi_controller *ctlr) 2904 { 2905 int ret; 2906 2907 /* Basically no-ops for non-queued controllers */ 2908 if (!ctlr->queued) 2909 return 0; 2910 2911 ret = spi_stop_queue(ctlr); 2912 if (ret) 2913 dev_err(&ctlr->dev, "queue stop failed\n"); 2914 2915 return ret; 2916 } 2917 EXPORT_SYMBOL_GPL(spi_controller_suspend); 2918 2919 int spi_controller_resume(struct spi_controller *ctlr) 2920 { 2921 int ret; 2922 2923 if (!ctlr->queued) 2924 return 0; 2925 2926 ret = spi_start_queue(ctlr); 2927 if (ret) 2928 dev_err(&ctlr->dev, "queue restart failed\n"); 2929 2930 return ret; 2931 } 2932 EXPORT_SYMBOL_GPL(spi_controller_resume); 2933 2934 static int __spi_controller_match(struct device *dev, const void *data) 2935 { 2936 struct spi_controller *ctlr; 2937 const u16 *bus_num = data; 2938 2939 ctlr = container_of(dev, struct spi_controller, dev); 2940 return ctlr->bus_num == *bus_num; 2941 } 2942 2943 /** 2944 * spi_busnum_to_master - look up master associated with bus_num 2945 * @bus_num: the master's bus number 2946 * Context: can sleep 2947 * 2948 * This call may be used with devices that are registered after 2949 * arch init time. It returns a refcounted pointer to the relevant 2950 * spi_controller (which the caller must release), or NULL if there is 2951 * no such master registered. 2952 * 2953 * Return: the SPI master structure on success, else NULL. 2954 */ 2955 struct spi_controller *spi_busnum_to_master(u16 bus_num) 2956 { 2957 struct device *dev; 2958 struct spi_controller *ctlr = NULL; 2959 2960 dev = class_find_device(&spi_master_class, NULL, &bus_num, 2961 __spi_controller_match); 2962 if (dev) 2963 ctlr = container_of(dev, struct spi_controller, dev); 2964 /* reference got in class_find_device */ 2965 return ctlr; 2966 } 2967 EXPORT_SYMBOL_GPL(spi_busnum_to_master); 2968 2969 /*-------------------------------------------------------------------------*/ 2970 2971 /* Core methods for SPI resource management */ 2972 2973 /** 2974 * spi_res_alloc - allocate a spi resource that is life-cycle managed 2975 * during the processing of a spi_message while using 2976 * spi_transfer_one 2977 * @spi: the spi device for which we allocate memory 2978 * @release: the release code to execute for this resource 2979 * @size: size to alloc and return 2980 * @gfp: GFP allocation flags 2981 * 2982 * Return: the pointer to the allocated data 2983 * 2984 * This may get enhanced in the future to allocate from a memory pool 2985 * of the @spi_device or @spi_controller to avoid repeated allocations. 2986 */ 2987 void *spi_res_alloc(struct spi_device *spi, 2988 spi_res_release_t release, 2989 size_t size, gfp_t gfp) 2990 { 2991 struct spi_res *sres; 2992 2993 sres = kzalloc(sizeof(*sres) + size, gfp); 2994 if (!sres) 2995 return NULL; 2996 2997 INIT_LIST_HEAD(&sres->entry); 2998 sres->release = release; 2999 3000 return sres->data; 3001 } 3002 EXPORT_SYMBOL_GPL(spi_res_alloc); 3003 3004 /** 3005 * spi_res_free - free an spi resource 3006 * @res: pointer to the custom data of a resource 3007 * 3008 */ 3009 void spi_res_free(void *res) 3010 { 3011 struct spi_res *sres = container_of(res, struct spi_res, data); 3012 3013 if (!res) 3014 return; 3015 3016 WARN_ON(!list_empty(&sres->entry)); 3017 kfree(sres); 3018 } 3019 EXPORT_SYMBOL_GPL(spi_res_free); 3020 3021 /** 3022 * spi_res_add - add a spi_res to the spi_message 3023 * @message: the spi message 3024 * @res: the spi_resource 3025 */ 3026 void spi_res_add(struct spi_message *message, void *res) 3027 { 3028 struct spi_res *sres = container_of(res, struct spi_res, data); 3029 3030 WARN_ON(!list_empty(&sres->entry)); 3031 list_add_tail(&sres->entry, &message->resources); 3032 } 3033 EXPORT_SYMBOL_GPL(spi_res_add); 3034 3035 /** 3036 * spi_res_release - release all spi resources for this message 3037 * @ctlr: the @spi_controller 3038 * @message: the @spi_message 3039 */ 3040 void spi_res_release(struct spi_controller *ctlr, struct spi_message *message) 3041 { 3042 struct spi_res *res, *tmp; 3043 3044 list_for_each_entry_safe_reverse(res, tmp, &message->resources, entry) { 3045 if (res->release) 3046 res->release(ctlr, message, res->data); 3047 3048 list_del(&res->entry); 3049 3050 kfree(res); 3051 } 3052 } 3053 EXPORT_SYMBOL_GPL(spi_res_release); 3054 3055 /*-------------------------------------------------------------------------*/ 3056 3057 /* Core methods for spi_message alterations */ 3058 3059 static void __spi_replace_transfers_release(struct spi_controller *ctlr, 3060 struct spi_message *msg, 3061 void *res) 3062 { 3063 struct spi_replaced_transfers *rxfer = res; 3064 size_t i; 3065 3066 /* call extra callback if requested */ 3067 if (rxfer->release) 3068 rxfer->release(ctlr, msg, res); 3069 3070 /* insert replaced transfers back into the message */ 3071 list_splice(&rxfer->replaced_transfers, rxfer->replaced_after); 3072 3073 /* remove the formerly inserted entries */ 3074 for (i = 0; i < rxfer->inserted; i++) 3075 list_del(&rxfer->inserted_transfers[i].transfer_list); 3076 } 3077 3078 /** 3079 * spi_replace_transfers - replace transfers with several transfers 3080 * and register change with spi_message.resources 3081 * @msg: the spi_message we work upon 3082 * @xfer_first: the first spi_transfer we want to replace 3083 * @remove: number of transfers to remove 3084 * @insert: the number of transfers we want to insert instead 3085 * @release: extra release code necessary in some circumstances 3086 * @extradatasize: extra data to allocate (with alignment guarantees 3087 * of struct @spi_transfer) 3088 * @gfp: gfp flags 3089 * 3090 * Returns: pointer to @spi_replaced_transfers, 3091 * PTR_ERR(...) in case of errors. 3092 */ 3093 struct spi_replaced_transfers *spi_replace_transfers( 3094 struct spi_message *msg, 3095 struct spi_transfer *xfer_first, 3096 size_t remove, 3097 size_t insert, 3098 spi_replaced_release_t release, 3099 size_t extradatasize, 3100 gfp_t gfp) 3101 { 3102 struct spi_replaced_transfers *rxfer; 3103 struct spi_transfer *xfer; 3104 size_t i; 3105 3106 /* allocate the structure using spi_res */ 3107 rxfer = spi_res_alloc(msg->spi, __spi_replace_transfers_release, 3108 struct_size(rxfer, inserted_transfers, insert) 3109 + extradatasize, 3110 gfp); 3111 if (!rxfer) 3112 return ERR_PTR(-ENOMEM); 3113 3114 /* the release code to invoke before running the generic release */ 3115 rxfer->release = release; 3116 3117 /* assign extradata */ 3118 if (extradatasize) 3119 rxfer->extradata = 3120 &rxfer->inserted_transfers[insert]; 3121 3122 /* init the replaced_transfers list */ 3123 INIT_LIST_HEAD(&rxfer->replaced_transfers); 3124 3125 /* assign the list_entry after which we should reinsert 3126 * the @replaced_transfers - it may be spi_message.messages! 3127 */ 3128 rxfer->replaced_after = xfer_first->transfer_list.prev; 3129 3130 /* remove the requested number of transfers */ 3131 for (i = 0; i < remove; i++) { 3132 /* if the entry after replaced_after it is msg->transfers 3133 * then we have been requested to remove more transfers 3134 * than are in the list 3135 */ 3136 if (rxfer->replaced_after->next == &msg->transfers) { 3137 dev_err(&msg->spi->dev, 3138 "requested to remove more spi_transfers than are available\n"); 3139 /* insert replaced transfers back into the message */ 3140 list_splice(&rxfer->replaced_transfers, 3141 rxfer->replaced_after); 3142 3143 /* free the spi_replace_transfer structure */ 3144 spi_res_free(rxfer); 3145 3146 /* and return with an error */ 3147 return ERR_PTR(-EINVAL); 3148 } 3149 3150 /* remove the entry after replaced_after from list of 3151 * transfers and add it to list of replaced_transfers 3152 */ 3153 list_move_tail(rxfer->replaced_after->next, 3154 &rxfer->replaced_transfers); 3155 } 3156 3157 /* create copy of the given xfer with identical settings 3158 * based on the first transfer to get removed 3159 */ 3160 for (i = 0; i < insert; i++) { 3161 /* we need to run in reverse order */ 3162 xfer = &rxfer->inserted_transfers[insert - 1 - i]; 3163 3164 /* copy all spi_transfer data */ 3165 memcpy(xfer, xfer_first, sizeof(*xfer)); 3166 3167 /* add to list */ 3168 list_add(&xfer->transfer_list, rxfer->replaced_after); 3169 3170 /* clear cs_change and delay for all but the last */ 3171 if (i) { 3172 xfer->cs_change = false; 3173 xfer->delay_usecs = 0; 3174 xfer->delay.value = 0; 3175 } 3176 } 3177 3178 /* set up inserted */ 3179 rxfer->inserted = insert; 3180 3181 /* and register it with spi_res/spi_message */ 3182 spi_res_add(msg, rxfer); 3183 3184 return rxfer; 3185 } 3186 EXPORT_SYMBOL_GPL(spi_replace_transfers); 3187 3188 static int __spi_split_transfer_maxsize(struct spi_controller *ctlr, 3189 struct spi_message *msg, 3190 struct spi_transfer **xferp, 3191 size_t maxsize, 3192 gfp_t gfp) 3193 { 3194 struct spi_transfer *xfer = *xferp, *xfers; 3195 struct spi_replaced_transfers *srt; 3196 size_t offset; 3197 size_t count, i; 3198 3199 /* calculate how many we have to replace */ 3200 count = DIV_ROUND_UP(xfer->len, maxsize); 3201 3202 /* create replacement */ 3203 srt = spi_replace_transfers(msg, xfer, 1, count, NULL, 0, gfp); 3204 if (IS_ERR(srt)) 3205 return PTR_ERR(srt); 3206 xfers = srt->inserted_transfers; 3207 3208 /* now handle each of those newly inserted spi_transfers 3209 * note that the replacements spi_transfers all are preset 3210 * to the same values as *xferp, so tx_buf, rx_buf and len 3211 * are all identical (as well as most others) 3212 * so we just have to fix up len and the pointers. 3213 * 3214 * this also includes support for the depreciated 3215 * spi_message.is_dma_mapped interface 3216 */ 3217 3218 /* the first transfer just needs the length modified, so we 3219 * run it outside the loop 3220 */ 3221 xfers[0].len = min_t(size_t, maxsize, xfer[0].len); 3222 3223 /* all the others need rx_buf/tx_buf also set */ 3224 for (i = 1, offset = maxsize; i < count; offset += maxsize, i++) { 3225 /* update rx_buf, tx_buf and dma */ 3226 if (xfers[i].rx_buf) 3227 xfers[i].rx_buf += offset; 3228 if (xfers[i].rx_dma) 3229 xfers[i].rx_dma += offset; 3230 if (xfers[i].tx_buf) 3231 xfers[i].tx_buf += offset; 3232 if (xfers[i].tx_dma) 3233 xfers[i].tx_dma += offset; 3234 3235 /* update length */ 3236 xfers[i].len = min(maxsize, xfers[i].len - offset); 3237 } 3238 3239 /* we set up xferp to the last entry we have inserted, 3240 * so that we skip those already split transfers 3241 */ 3242 *xferp = &xfers[count - 1]; 3243 3244 /* increment statistics counters */ 3245 SPI_STATISTICS_INCREMENT_FIELD(&ctlr->statistics, 3246 transfers_split_maxsize); 3247 SPI_STATISTICS_INCREMENT_FIELD(&msg->spi->statistics, 3248 transfers_split_maxsize); 3249 3250 return 0; 3251 } 3252 3253 /** 3254 * spi_split_transfers_maxsize - split spi transfers into multiple transfers 3255 * when an individual transfer exceeds a 3256 * certain size 3257 * @ctlr: the @spi_controller for this transfer 3258 * @msg: the @spi_message to transform 3259 * @maxsize: the maximum when to apply this 3260 * @gfp: GFP allocation flags 3261 * 3262 * Return: status of transformation 3263 */ 3264 int spi_split_transfers_maxsize(struct spi_controller *ctlr, 3265 struct spi_message *msg, 3266 size_t maxsize, 3267 gfp_t gfp) 3268 { 3269 struct spi_transfer *xfer; 3270 int ret; 3271 3272 /* iterate over the transfer_list, 3273 * but note that xfer is advanced to the last transfer inserted 3274 * to avoid checking sizes again unnecessarily (also xfer does 3275 * potentiall belong to a different list by the time the 3276 * replacement has happened 3277 */ 3278 list_for_each_entry(xfer, &msg->transfers, transfer_list) { 3279 if (xfer->len > maxsize) { 3280 ret = __spi_split_transfer_maxsize(ctlr, msg, &xfer, 3281 maxsize, gfp); 3282 if (ret) 3283 return ret; 3284 } 3285 } 3286 3287 return 0; 3288 } 3289 EXPORT_SYMBOL_GPL(spi_split_transfers_maxsize); 3290 3291 /*-------------------------------------------------------------------------*/ 3292 3293 /* Core methods for SPI controller protocol drivers. Some of the 3294 * other core methods are currently defined as inline functions. 3295 */ 3296 3297 static int __spi_validate_bits_per_word(struct spi_controller *ctlr, 3298 u8 bits_per_word) 3299 { 3300 if (ctlr->bits_per_word_mask) { 3301 /* Only 32 bits fit in the mask */ 3302 if (bits_per_word > 32) 3303 return -EINVAL; 3304 if (!(ctlr->bits_per_word_mask & SPI_BPW_MASK(bits_per_word))) 3305 return -EINVAL; 3306 } 3307 3308 return 0; 3309 } 3310 3311 /** 3312 * spi_setup - setup SPI mode and clock rate 3313 * @spi: the device whose settings are being modified 3314 * Context: can sleep, and no requests are queued to the device 3315 * 3316 * SPI protocol drivers may need to update the transfer mode if the 3317 * device doesn't work with its default. They may likewise need 3318 * to update clock rates or word sizes from initial values. This function 3319 * changes those settings, and must be called from a context that can sleep. 3320 * Except for SPI_CS_HIGH, which takes effect immediately, the changes take 3321 * effect the next time the device is selected and data is transferred to 3322 * or from it. When this function returns, the spi device is deselected. 3323 * 3324 * Note that this call will fail if the protocol driver specifies an option 3325 * that the underlying controller or its driver does not support. For 3326 * example, not all hardware supports wire transfers using nine bit words, 3327 * LSB-first wire encoding, or active-high chipselects. 3328 * 3329 * Return: zero on success, else a negative error code. 3330 */ 3331 int spi_setup(struct spi_device *spi) 3332 { 3333 unsigned bad_bits, ugly_bits; 3334 int status; 3335 3336 /* check mode to prevent that DUAL and QUAD set at the same time 3337 */ 3338 if (((spi->mode & SPI_TX_DUAL) && (spi->mode & SPI_TX_QUAD)) || 3339 ((spi->mode & SPI_RX_DUAL) && (spi->mode & SPI_RX_QUAD))) { 3340 dev_err(&spi->dev, 3341 "setup: can not select dual and quad at the same time\n"); 3342 return -EINVAL; 3343 } 3344 /* if it is SPI_3WIRE mode, DUAL and QUAD should be forbidden 3345 */ 3346 if ((spi->mode & SPI_3WIRE) && (spi->mode & 3347 (SPI_TX_DUAL | SPI_TX_QUAD | SPI_TX_OCTAL | 3348 SPI_RX_DUAL | SPI_RX_QUAD | SPI_RX_OCTAL))) 3349 return -EINVAL; 3350 /* help drivers fail *cleanly* when they need options 3351 * that aren't supported with their current controller 3352 * SPI_CS_WORD has a fallback software implementation, 3353 * so it is ignored here. 3354 */ 3355 bad_bits = spi->mode & ~(spi->controller->mode_bits | SPI_CS_WORD); 3356 /* nothing prevents from working with active-high CS in case if it 3357 * is driven by GPIO. 3358 */ 3359 if (gpio_is_valid(spi->cs_gpio)) 3360 bad_bits &= ~SPI_CS_HIGH; 3361 ugly_bits = bad_bits & 3362 (SPI_TX_DUAL | SPI_TX_QUAD | SPI_TX_OCTAL | 3363 SPI_RX_DUAL | SPI_RX_QUAD | SPI_RX_OCTAL); 3364 if (ugly_bits) { 3365 dev_warn(&spi->dev, 3366 "setup: ignoring unsupported mode bits %x\n", 3367 ugly_bits); 3368 spi->mode &= ~ugly_bits; 3369 bad_bits &= ~ugly_bits; 3370 } 3371 if (bad_bits) { 3372 dev_err(&spi->dev, "setup: unsupported mode bits %x\n", 3373 bad_bits); 3374 return -EINVAL; 3375 } 3376 3377 if (!spi->bits_per_word) 3378 spi->bits_per_word = 8; 3379 3380 status = __spi_validate_bits_per_word(spi->controller, 3381 spi->bits_per_word); 3382 if (status) 3383 return status; 3384 3385 if (spi->controller->max_speed_hz && 3386 (!spi->max_speed_hz || 3387 spi->max_speed_hz > spi->controller->max_speed_hz)) 3388 spi->max_speed_hz = spi->controller->max_speed_hz; 3389 3390 mutex_lock(&spi->controller->io_mutex); 3391 3392 if (spi->controller->setup) 3393 status = spi->controller->setup(spi); 3394 3395 if (spi->controller->auto_runtime_pm && spi->controller->set_cs) { 3396 status = pm_runtime_get_sync(spi->controller->dev.parent); 3397 if (status < 0) { 3398 mutex_unlock(&spi->controller->io_mutex); 3399 pm_runtime_put_noidle(spi->controller->dev.parent); 3400 dev_err(&spi->controller->dev, "Failed to power device: %d\n", 3401 status); 3402 return status; 3403 } 3404 3405 /* 3406 * We do not want to return positive value from pm_runtime_get, 3407 * there are many instances of devices calling spi_setup() and 3408 * checking for a non-zero return value instead of a negative 3409 * return value. 3410 */ 3411 status = 0; 3412 3413 spi_set_cs(spi, false); 3414 pm_runtime_mark_last_busy(spi->controller->dev.parent); 3415 pm_runtime_put_autosuspend(spi->controller->dev.parent); 3416 } else { 3417 spi_set_cs(spi, false); 3418 } 3419 3420 mutex_unlock(&spi->controller->io_mutex); 3421 3422 if (spi->rt && !spi->controller->rt) { 3423 spi->controller->rt = true; 3424 spi_set_thread_rt(spi->controller); 3425 } 3426 3427 dev_dbg(&spi->dev, "setup mode %d, %s%s%s%s%u bits/w, %u Hz max --> %d\n", 3428 (int) (spi->mode & (SPI_CPOL | SPI_CPHA)), 3429 (spi->mode & SPI_CS_HIGH) ? "cs_high, " : "", 3430 (spi->mode & SPI_LSB_FIRST) ? "lsb, " : "", 3431 (spi->mode & SPI_3WIRE) ? "3wire, " : "", 3432 (spi->mode & SPI_LOOP) ? "loopback, " : "", 3433 spi->bits_per_word, spi->max_speed_hz, 3434 status); 3435 3436 return status; 3437 } 3438 EXPORT_SYMBOL_GPL(spi_setup); 3439 3440 /** 3441 * spi_set_cs_timing - configure CS setup, hold, and inactive delays 3442 * @spi: the device that requires specific CS timing configuration 3443 * @setup: CS setup time specified via @spi_delay 3444 * @hold: CS hold time specified via @spi_delay 3445 * @inactive: CS inactive delay between transfers specified via @spi_delay 3446 * 3447 * Return: zero on success, else a negative error code. 3448 */ 3449 int spi_set_cs_timing(struct spi_device *spi, struct spi_delay *setup, 3450 struct spi_delay *hold, struct spi_delay *inactive) 3451 { 3452 size_t len; 3453 3454 if (spi->controller->set_cs_timing) 3455 return spi->controller->set_cs_timing(spi, setup, hold, 3456 inactive); 3457 3458 if ((setup && setup->unit == SPI_DELAY_UNIT_SCK) || 3459 (hold && hold->unit == SPI_DELAY_UNIT_SCK) || 3460 (inactive && inactive->unit == SPI_DELAY_UNIT_SCK)) { 3461 dev_err(&spi->dev, 3462 "Clock-cycle delays for CS not supported in SW mode\n"); 3463 return -ENOTSUPP; 3464 } 3465 3466 len = sizeof(struct spi_delay); 3467 3468 /* copy delays to controller */ 3469 if (setup) 3470 memcpy(&spi->controller->cs_setup, setup, len); 3471 else 3472 memset(&spi->controller->cs_setup, 0, len); 3473 3474 if (hold) 3475 memcpy(&spi->controller->cs_hold, hold, len); 3476 else 3477 memset(&spi->controller->cs_hold, 0, len); 3478 3479 if (inactive) 3480 memcpy(&spi->controller->cs_inactive, inactive, len); 3481 else 3482 memset(&spi->controller->cs_inactive, 0, len); 3483 3484 return 0; 3485 } 3486 EXPORT_SYMBOL_GPL(spi_set_cs_timing); 3487 3488 static int _spi_xfer_word_delay_update(struct spi_transfer *xfer, 3489 struct spi_device *spi) 3490 { 3491 int delay1, delay2; 3492 3493 delay1 = spi_delay_to_ns(&xfer->word_delay, xfer); 3494 if (delay1 < 0) 3495 return delay1; 3496 3497 delay2 = spi_delay_to_ns(&spi->word_delay, xfer); 3498 if (delay2 < 0) 3499 return delay2; 3500 3501 if (delay1 < delay2) 3502 memcpy(&xfer->word_delay, &spi->word_delay, 3503 sizeof(xfer->word_delay)); 3504 3505 return 0; 3506 } 3507 3508 static int __spi_validate(struct spi_device *spi, struct spi_message *message) 3509 { 3510 struct spi_controller *ctlr = spi->controller; 3511 struct spi_transfer *xfer; 3512 int w_size; 3513 3514 if (list_empty(&message->transfers)) 3515 return -EINVAL; 3516 3517 /* If an SPI controller does not support toggling the CS line on each 3518 * transfer (indicated by the SPI_CS_WORD flag) or we are using a GPIO 3519 * for the CS line, we can emulate the CS-per-word hardware function by 3520 * splitting transfers into one-word transfers and ensuring that 3521 * cs_change is set for each transfer. 3522 */ 3523 if ((spi->mode & SPI_CS_WORD) && (!(ctlr->mode_bits & SPI_CS_WORD) || 3524 spi->cs_gpiod || 3525 gpio_is_valid(spi->cs_gpio))) { 3526 size_t maxsize; 3527 int ret; 3528 3529 maxsize = (spi->bits_per_word + 7) / 8; 3530 3531 /* spi_split_transfers_maxsize() requires message->spi */ 3532 message->spi = spi; 3533 3534 ret = spi_split_transfers_maxsize(ctlr, message, maxsize, 3535 GFP_KERNEL); 3536 if (ret) 3537 return ret; 3538 3539 list_for_each_entry(xfer, &message->transfers, transfer_list) { 3540 /* don't change cs_change on the last entry in the list */ 3541 if (list_is_last(&xfer->transfer_list, &message->transfers)) 3542 break; 3543 xfer->cs_change = 1; 3544 } 3545 } 3546 3547 /* Half-duplex links include original MicroWire, and ones with 3548 * only one data pin like SPI_3WIRE (switches direction) or where 3549 * either MOSI or MISO is missing. They can also be caused by 3550 * software limitations. 3551 */ 3552 if ((ctlr->flags & SPI_CONTROLLER_HALF_DUPLEX) || 3553 (spi->mode & SPI_3WIRE)) { 3554 unsigned flags = ctlr->flags; 3555 3556 list_for_each_entry(xfer, &message->transfers, transfer_list) { 3557 if (xfer->rx_buf && xfer->tx_buf) 3558 return -EINVAL; 3559 if ((flags & SPI_CONTROLLER_NO_TX) && xfer->tx_buf) 3560 return -EINVAL; 3561 if ((flags & SPI_CONTROLLER_NO_RX) && xfer->rx_buf) 3562 return -EINVAL; 3563 } 3564 } 3565 3566 /** 3567 * Set transfer bits_per_word and max speed as spi device default if 3568 * it is not set for this transfer. 3569 * Set transfer tx_nbits and rx_nbits as single transfer default 3570 * (SPI_NBITS_SINGLE) if it is not set for this transfer. 3571 * Ensure transfer word_delay is at least as long as that required by 3572 * device itself. 3573 */ 3574 message->frame_length = 0; 3575 list_for_each_entry(xfer, &message->transfers, transfer_list) { 3576 xfer->effective_speed_hz = 0; 3577 message->frame_length += xfer->len; 3578 if (!xfer->bits_per_word) 3579 xfer->bits_per_word = spi->bits_per_word; 3580 3581 if (!xfer->speed_hz) 3582 xfer->speed_hz = spi->max_speed_hz; 3583 3584 if (ctlr->max_speed_hz && xfer->speed_hz > ctlr->max_speed_hz) 3585 xfer->speed_hz = ctlr->max_speed_hz; 3586 3587 if (__spi_validate_bits_per_word(ctlr, xfer->bits_per_word)) 3588 return -EINVAL; 3589 3590 /* 3591 * SPI transfer length should be multiple of SPI word size 3592 * where SPI word size should be power-of-two multiple 3593 */ 3594 if (xfer->bits_per_word <= 8) 3595 w_size = 1; 3596 else if (xfer->bits_per_word <= 16) 3597 w_size = 2; 3598 else 3599 w_size = 4; 3600 3601 /* No partial transfers accepted */ 3602 if (xfer->len % w_size) 3603 return -EINVAL; 3604 3605 if (xfer->speed_hz && ctlr->min_speed_hz && 3606 xfer->speed_hz < ctlr->min_speed_hz) 3607 return -EINVAL; 3608 3609 if (xfer->tx_buf && !xfer->tx_nbits) 3610 xfer->tx_nbits = SPI_NBITS_SINGLE; 3611 if (xfer->rx_buf && !xfer->rx_nbits) 3612 xfer->rx_nbits = SPI_NBITS_SINGLE; 3613 /* check transfer tx/rx_nbits: 3614 * 1. check the value matches one of single, dual and quad 3615 * 2. check tx/rx_nbits match the mode in spi_device 3616 */ 3617 if (xfer->tx_buf) { 3618 if (xfer->tx_nbits != SPI_NBITS_SINGLE && 3619 xfer->tx_nbits != SPI_NBITS_DUAL && 3620 xfer->tx_nbits != SPI_NBITS_QUAD) 3621 return -EINVAL; 3622 if ((xfer->tx_nbits == SPI_NBITS_DUAL) && 3623 !(spi->mode & (SPI_TX_DUAL | SPI_TX_QUAD))) 3624 return -EINVAL; 3625 if ((xfer->tx_nbits == SPI_NBITS_QUAD) && 3626 !(spi->mode & SPI_TX_QUAD)) 3627 return -EINVAL; 3628 } 3629 /* check transfer rx_nbits */ 3630 if (xfer->rx_buf) { 3631 if (xfer->rx_nbits != SPI_NBITS_SINGLE && 3632 xfer->rx_nbits != SPI_NBITS_DUAL && 3633 xfer->rx_nbits != SPI_NBITS_QUAD) 3634 return -EINVAL; 3635 if ((xfer->rx_nbits == SPI_NBITS_DUAL) && 3636 !(spi->mode & (SPI_RX_DUAL | SPI_RX_QUAD))) 3637 return -EINVAL; 3638 if ((xfer->rx_nbits == SPI_NBITS_QUAD) && 3639 !(spi->mode & SPI_RX_QUAD)) 3640 return -EINVAL; 3641 } 3642 3643 if (_spi_xfer_word_delay_update(xfer, spi)) 3644 return -EINVAL; 3645 } 3646 3647 message->status = -EINPROGRESS; 3648 3649 return 0; 3650 } 3651 3652 static int __spi_async(struct spi_device *spi, struct spi_message *message) 3653 { 3654 struct spi_controller *ctlr = spi->controller; 3655 struct spi_transfer *xfer; 3656 3657 /* 3658 * Some controllers do not support doing regular SPI transfers. Return 3659 * ENOTSUPP when this is the case. 3660 */ 3661 if (!ctlr->transfer) 3662 return -ENOTSUPP; 3663 3664 message->spi = spi; 3665 3666 SPI_STATISTICS_INCREMENT_FIELD(&ctlr->statistics, spi_async); 3667 SPI_STATISTICS_INCREMENT_FIELD(&spi->statistics, spi_async); 3668 3669 trace_spi_message_submit(message); 3670 3671 if (!ctlr->ptp_sts_supported) { 3672 list_for_each_entry(xfer, &message->transfers, transfer_list) { 3673 xfer->ptp_sts_word_pre = 0; 3674 ptp_read_system_prets(xfer->ptp_sts); 3675 } 3676 } 3677 3678 return ctlr->transfer(spi, message); 3679 } 3680 3681 /** 3682 * spi_async - asynchronous SPI transfer 3683 * @spi: device with which data will be exchanged 3684 * @message: describes the data transfers, including completion callback 3685 * Context: any (irqs may be blocked, etc) 3686 * 3687 * This call may be used in_irq and other contexts which can't sleep, 3688 * as well as from task contexts which can sleep. 3689 * 3690 * The completion callback is invoked in a context which can't sleep. 3691 * Before that invocation, the value of message->status is undefined. 3692 * When the callback is issued, message->status holds either zero (to 3693 * indicate complete success) or a negative error code. After that 3694 * callback returns, the driver which issued the transfer request may 3695 * deallocate the associated memory; it's no longer in use by any SPI 3696 * core or controller driver code. 3697 * 3698 * Note that although all messages to a spi_device are handled in 3699 * FIFO order, messages may go to different devices in other orders. 3700 * Some device might be higher priority, or have various "hard" access 3701 * time requirements, for example. 3702 * 3703 * On detection of any fault during the transfer, processing of 3704 * the entire message is aborted, and the device is deselected. 3705 * Until returning from the associated message completion callback, 3706 * no other spi_message queued to that device will be processed. 3707 * (This rule applies equally to all the synchronous transfer calls, 3708 * which are wrappers around this core asynchronous primitive.) 3709 * 3710 * Return: zero on success, else a negative error code. 3711 */ 3712 int spi_async(struct spi_device *spi, struct spi_message *message) 3713 { 3714 struct spi_controller *ctlr = spi->controller; 3715 int ret; 3716 unsigned long flags; 3717 3718 ret = __spi_validate(spi, message); 3719 if (ret != 0) 3720 return ret; 3721 3722 spin_lock_irqsave(&ctlr->bus_lock_spinlock, flags); 3723 3724 if (ctlr->bus_lock_flag) 3725 ret = -EBUSY; 3726 else 3727 ret = __spi_async(spi, message); 3728 3729 spin_unlock_irqrestore(&ctlr->bus_lock_spinlock, flags); 3730 3731 return ret; 3732 } 3733 EXPORT_SYMBOL_GPL(spi_async); 3734 3735 /** 3736 * spi_async_locked - version of spi_async with exclusive bus usage 3737 * @spi: device with which data will be exchanged 3738 * @message: describes the data transfers, including completion callback 3739 * Context: any (irqs may be blocked, etc) 3740 * 3741 * This call may be used in_irq and other contexts which can't sleep, 3742 * as well as from task contexts which can sleep. 3743 * 3744 * The completion callback is invoked in a context which can't sleep. 3745 * Before that invocation, the value of message->status is undefined. 3746 * When the callback is issued, message->status holds either zero (to 3747 * indicate complete success) or a negative error code. After that 3748 * callback returns, the driver which issued the transfer request may 3749 * deallocate the associated memory; it's no longer in use by any SPI 3750 * core or controller driver code. 3751 * 3752 * Note that although all messages to a spi_device are handled in 3753 * FIFO order, messages may go to different devices in other orders. 3754 * Some device might be higher priority, or have various "hard" access 3755 * time requirements, for example. 3756 * 3757 * On detection of any fault during the transfer, processing of 3758 * the entire message is aborted, and the device is deselected. 3759 * Until returning from the associated message completion callback, 3760 * no other spi_message queued to that device will be processed. 3761 * (This rule applies equally to all the synchronous transfer calls, 3762 * which are wrappers around this core asynchronous primitive.) 3763 * 3764 * Return: zero on success, else a negative error code. 3765 */ 3766 int spi_async_locked(struct spi_device *spi, struct spi_message *message) 3767 { 3768 struct spi_controller *ctlr = spi->controller; 3769 int ret; 3770 unsigned long flags; 3771 3772 ret = __spi_validate(spi, message); 3773 if (ret != 0) 3774 return ret; 3775 3776 spin_lock_irqsave(&ctlr->bus_lock_spinlock, flags); 3777 3778 ret = __spi_async(spi, message); 3779 3780 spin_unlock_irqrestore(&ctlr->bus_lock_spinlock, flags); 3781 3782 return ret; 3783 3784 } 3785 EXPORT_SYMBOL_GPL(spi_async_locked); 3786 3787 /*-------------------------------------------------------------------------*/ 3788 3789 /* Utility methods for SPI protocol drivers, layered on 3790 * top of the core. Some other utility methods are defined as 3791 * inline functions. 3792 */ 3793 3794 static void spi_complete(void *arg) 3795 { 3796 complete(arg); 3797 } 3798 3799 static int __spi_sync(struct spi_device *spi, struct spi_message *message) 3800 { 3801 DECLARE_COMPLETION_ONSTACK(done); 3802 int status; 3803 struct spi_controller *ctlr = spi->controller; 3804 unsigned long flags; 3805 3806 status = __spi_validate(spi, message); 3807 if (status != 0) 3808 return status; 3809 3810 message->complete = spi_complete; 3811 message->context = &done; 3812 message->spi = spi; 3813 3814 SPI_STATISTICS_INCREMENT_FIELD(&ctlr->statistics, spi_sync); 3815 SPI_STATISTICS_INCREMENT_FIELD(&spi->statistics, spi_sync); 3816 3817 /* If we're not using the legacy transfer method then we will 3818 * try to transfer in the calling context so special case. 3819 * This code would be less tricky if we could remove the 3820 * support for driver implemented message queues. 3821 */ 3822 if (ctlr->transfer == spi_queued_transfer) { 3823 spin_lock_irqsave(&ctlr->bus_lock_spinlock, flags); 3824 3825 trace_spi_message_submit(message); 3826 3827 status = __spi_queued_transfer(spi, message, false); 3828 3829 spin_unlock_irqrestore(&ctlr->bus_lock_spinlock, flags); 3830 } else { 3831 status = spi_async_locked(spi, message); 3832 } 3833 3834 if (status == 0) { 3835 /* Push out the messages in the calling context if we 3836 * can. 3837 */ 3838 if (ctlr->transfer == spi_queued_transfer) { 3839 SPI_STATISTICS_INCREMENT_FIELD(&ctlr->statistics, 3840 spi_sync_immediate); 3841 SPI_STATISTICS_INCREMENT_FIELD(&spi->statistics, 3842 spi_sync_immediate); 3843 __spi_pump_messages(ctlr, false); 3844 } 3845 3846 wait_for_completion(&done); 3847 status = message->status; 3848 } 3849 message->context = NULL; 3850 return status; 3851 } 3852 3853 /** 3854 * spi_sync - blocking/synchronous SPI data transfers 3855 * @spi: device with which data will be exchanged 3856 * @message: describes the data transfers 3857 * Context: can sleep 3858 * 3859 * This call may only be used from a context that may sleep. The sleep 3860 * is non-interruptible, and has no timeout. Low-overhead controller 3861 * drivers may DMA directly into and out of the message buffers. 3862 * 3863 * Note that the SPI device's chip select is active during the message, 3864 * and then is normally disabled between messages. Drivers for some 3865 * frequently-used devices may want to minimize costs of selecting a chip, 3866 * by leaving it selected in anticipation that the next message will go 3867 * to the same chip. (That may increase power usage.) 3868 * 3869 * Also, the caller is guaranteeing that the memory associated with the 3870 * message will not be freed before this call returns. 3871 * 3872 * Return: zero on success, else a negative error code. 3873 */ 3874 int spi_sync(struct spi_device *spi, struct spi_message *message) 3875 { 3876 int ret; 3877 3878 mutex_lock(&spi->controller->bus_lock_mutex); 3879 ret = __spi_sync(spi, message); 3880 mutex_unlock(&spi->controller->bus_lock_mutex); 3881 3882 return ret; 3883 } 3884 EXPORT_SYMBOL_GPL(spi_sync); 3885 3886 /** 3887 * spi_sync_locked - version of spi_sync with exclusive bus usage 3888 * @spi: device with which data will be exchanged 3889 * @message: describes the data transfers 3890 * Context: can sleep 3891 * 3892 * This call may only be used from a context that may sleep. The sleep 3893 * is non-interruptible, and has no timeout. Low-overhead controller 3894 * drivers may DMA directly into and out of the message buffers. 3895 * 3896 * This call should be used by drivers that require exclusive access to the 3897 * SPI bus. It has to be preceded by a spi_bus_lock call. The SPI bus must 3898 * be released by a spi_bus_unlock call when the exclusive access is over. 3899 * 3900 * Return: zero on success, else a negative error code. 3901 */ 3902 int spi_sync_locked(struct spi_device *spi, struct spi_message *message) 3903 { 3904 return __spi_sync(spi, message); 3905 } 3906 EXPORT_SYMBOL_GPL(spi_sync_locked); 3907 3908 /** 3909 * spi_bus_lock - obtain a lock for exclusive SPI bus usage 3910 * @ctlr: SPI bus master that should be locked for exclusive bus access 3911 * Context: can sleep 3912 * 3913 * This call may only be used from a context that may sleep. The sleep 3914 * is non-interruptible, and has no timeout. 3915 * 3916 * This call should be used by drivers that require exclusive access to the 3917 * SPI bus. The SPI bus must be released by a spi_bus_unlock call when the 3918 * exclusive access is over. Data transfer must be done by spi_sync_locked 3919 * and spi_async_locked calls when the SPI bus lock is held. 3920 * 3921 * Return: always zero. 3922 */ 3923 int spi_bus_lock(struct spi_controller *ctlr) 3924 { 3925 unsigned long flags; 3926 3927 mutex_lock(&ctlr->bus_lock_mutex); 3928 3929 spin_lock_irqsave(&ctlr->bus_lock_spinlock, flags); 3930 ctlr->bus_lock_flag = 1; 3931 spin_unlock_irqrestore(&ctlr->bus_lock_spinlock, flags); 3932 3933 /* mutex remains locked until spi_bus_unlock is called */ 3934 3935 return 0; 3936 } 3937 EXPORT_SYMBOL_GPL(spi_bus_lock); 3938 3939 /** 3940 * spi_bus_unlock - release the lock for exclusive SPI bus usage 3941 * @ctlr: SPI bus master that was locked for exclusive bus access 3942 * Context: can sleep 3943 * 3944 * This call may only be used from a context that may sleep. The sleep 3945 * is non-interruptible, and has no timeout. 3946 * 3947 * This call releases an SPI bus lock previously obtained by an spi_bus_lock 3948 * call. 3949 * 3950 * Return: always zero. 3951 */ 3952 int spi_bus_unlock(struct spi_controller *ctlr) 3953 { 3954 ctlr->bus_lock_flag = 0; 3955 3956 mutex_unlock(&ctlr->bus_lock_mutex); 3957 3958 return 0; 3959 } 3960 EXPORT_SYMBOL_GPL(spi_bus_unlock); 3961 3962 /* portable code must never pass more than 32 bytes */ 3963 #define SPI_BUFSIZ max(32, SMP_CACHE_BYTES) 3964 3965 static u8 *buf; 3966 3967 /** 3968 * spi_write_then_read - SPI synchronous write followed by read 3969 * @spi: device with which data will be exchanged 3970 * @txbuf: data to be written (need not be dma-safe) 3971 * @n_tx: size of txbuf, in bytes 3972 * @rxbuf: buffer into which data will be read (need not be dma-safe) 3973 * @n_rx: size of rxbuf, in bytes 3974 * Context: can sleep 3975 * 3976 * This performs a half duplex MicroWire style transaction with the 3977 * device, sending txbuf and then reading rxbuf. The return value 3978 * is zero for success, else a negative errno status code. 3979 * This call may only be used from a context that may sleep. 3980 * 3981 * Parameters to this routine are always copied using a small buffer. 3982 * Performance-sensitive or bulk transfer code should instead use 3983 * spi_{async,sync}() calls with dma-safe buffers. 3984 * 3985 * Return: zero on success, else a negative error code. 3986 */ 3987 int spi_write_then_read(struct spi_device *spi, 3988 const void *txbuf, unsigned n_tx, 3989 void *rxbuf, unsigned n_rx) 3990 { 3991 static DEFINE_MUTEX(lock); 3992 3993 int status; 3994 struct spi_message message; 3995 struct spi_transfer x[2]; 3996 u8 *local_buf; 3997 3998 /* Use preallocated DMA-safe buffer if we can. We can't avoid 3999 * copying here, (as a pure convenience thing), but we can 4000 * keep heap costs out of the hot path unless someone else is 4001 * using the pre-allocated buffer or the transfer is too large. 4002 */ 4003 if ((n_tx + n_rx) > SPI_BUFSIZ || !mutex_trylock(&lock)) { 4004 local_buf = kmalloc(max((unsigned)SPI_BUFSIZ, n_tx + n_rx), 4005 GFP_KERNEL | GFP_DMA); 4006 if (!local_buf) 4007 return -ENOMEM; 4008 } else { 4009 local_buf = buf; 4010 } 4011 4012 spi_message_init(&message); 4013 memset(x, 0, sizeof(x)); 4014 if (n_tx) { 4015 x[0].len = n_tx; 4016 spi_message_add_tail(&x[0], &message); 4017 } 4018 if (n_rx) { 4019 x[1].len = n_rx; 4020 spi_message_add_tail(&x[1], &message); 4021 } 4022 4023 memcpy(local_buf, txbuf, n_tx); 4024 x[0].tx_buf = local_buf; 4025 x[1].rx_buf = local_buf + n_tx; 4026 4027 /* do the i/o */ 4028 status = spi_sync(spi, &message); 4029 if (status == 0) 4030 memcpy(rxbuf, x[1].rx_buf, n_rx); 4031 4032 if (x[0].tx_buf == buf) 4033 mutex_unlock(&lock); 4034 else 4035 kfree(local_buf); 4036 4037 return status; 4038 } 4039 EXPORT_SYMBOL_GPL(spi_write_then_read); 4040 4041 /*-------------------------------------------------------------------------*/ 4042 4043 #if IS_ENABLED(CONFIG_OF) 4044 /* must call put_device() when done with returned spi_device device */ 4045 struct spi_device *of_find_spi_device_by_node(struct device_node *node) 4046 { 4047 struct device *dev = bus_find_device_by_of_node(&spi_bus_type, node); 4048 4049 return dev ? to_spi_device(dev) : NULL; 4050 } 4051 EXPORT_SYMBOL_GPL(of_find_spi_device_by_node); 4052 #endif /* IS_ENABLED(CONFIG_OF) */ 4053 4054 #if IS_ENABLED(CONFIG_OF_DYNAMIC) 4055 /* the spi controllers are not using spi_bus, so we find it with another way */ 4056 static struct spi_controller *of_find_spi_controller_by_node(struct device_node *node) 4057 { 4058 struct device *dev; 4059 4060 dev = class_find_device_by_of_node(&spi_master_class, node); 4061 if (!dev && IS_ENABLED(CONFIG_SPI_SLAVE)) 4062 dev = class_find_device_by_of_node(&spi_slave_class, node); 4063 if (!dev) 4064 return NULL; 4065 4066 /* reference got in class_find_device */ 4067 return container_of(dev, struct spi_controller, dev); 4068 } 4069 4070 static int of_spi_notify(struct notifier_block *nb, unsigned long action, 4071 void *arg) 4072 { 4073 struct of_reconfig_data *rd = arg; 4074 struct spi_controller *ctlr; 4075 struct spi_device *spi; 4076 4077 switch (of_reconfig_get_state_change(action, arg)) { 4078 case OF_RECONFIG_CHANGE_ADD: 4079 ctlr = of_find_spi_controller_by_node(rd->dn->parent); 4080 if (ctlr == NULL) 4081 return NOTIFY_OK; /* not for us */ 4082 4083 if (of_node_test_and_set_flag(rd->dn, OF_POPULATED)) { 4084 put_device(&ctlr->dev); 4085 return NOTIFY_OK; 4086 } 4087 4088 spi = of_register_spi_device(ctlr, rd->dn); 4089 put_device(&ctlr->dev); 4090 4091 if (IS_ERR(spi)) { 4092 pr_err("%s: failed to create for '%pOF'\n", 4093 __func__, rd->dn); 4094 of_node_clear_flag(rd->dn, OF_POPULATED); 4095 return notifier_from_errno(PTR_ERR(spi)); 4096 } 4097 break; 4098 4099 case OF_RECONFIG_CHANGE_REMOVE: 4100 /* already depopulated? */ 4101 if (!of_node_check_flag(rd->dn, OF_POPULATED)) 4102 return NOTIFY_OK; 4103 4104 /* find our device by node */ 4105 spi = of_find_spi_device_by_node(rd->dn); 4106 if (spi == NULL) 4107 return NOTIFY_OK; /* no? not meant for us */ 4108 4109 /* unregister takes one ref away */ 4110 spi_unregister_device(spi); 4111 4112 /* and put the reference of the find */ 4113 put_device(&spi->dev); 4114 break; 4115 } 4116 4117 return NOTIFY_OK; 4118 } 4119 4120 static struct notifier_block spi_of_notifier = { 4121 .notifier_call = of_spi_notify, 4122 }; 4123 #else /* IS_ENABLED(CONFIG_OF_DYNAMIC) */ 4124 extern struct notifier_block spi_of_notifier; 4125 #endif /* IS_ENABLED(CONFIG_OF_DYNAMIC) */ 4126 4127 #if IS_ENABLED(CONFIG_ACPI) 4128 static int spi_acpi_controller_match(struct device *dev, const void *data) 4129 { 4130 return ACPI_COMPANION(dev->parent) == data; 4131 } 4132 4133 static struct spi_controller *acpi_spi_find_controller_by_adev(struct acpi_device *adev) 4134 { 4135 struct device *dev; 4136 4137 dev = class_find_device(&spi_master_class, NULL, adev, 4138 spi_acpi_controller_match); 4139 if (!dev && IS_ENABLED(CONFIG_SPI_SLAVE)) 4140 dev = class_find_device(&spi_slave_class, NULL, adev, 4141 spi_acpi_controller_match); 4142 if (!dev) 4143 return NULL; 4144 4145 return container_of(dev, struct spi_controller, dev); 4146 } 4147 4148 static struct spi_device *acpi_spi_find_device_by_adev(struct acpi_device *adev) 4149 { 4150 struct device *dev; 4151 4152 dev = bus_find_device_by_acpi_dev(&spi_bus_type, adev); 4153 return to_spi_device(dev); 4154 } 4155 4156 static int acpi_spi_notify(struct notifier_block *nb, unsigned long value, 4157 void *arg) 4158 { 4159 struct acpi_device *adev = arg; 4160 struct spi_controller *ctlr; 4161 struct spi_device *spi; 4162 4163 switch (value) { 4164 case ACPI_RECONFIG_DEVICE_ADD: 4165 ctlr = acpi_spi_find_controller_by_adev(adev->parent); 4166 if (!ctlr) 4167 break; 4168 4169 acpi_register_spi_device(ctlr, adev); 4170 put_device(&ctlr->dev); 4171 break; 4172 case ACPI_RECONFIG_DEVICE_REMOVE: 4173 if (!acpi_device_enumerated(adev)) 4174 break; 4175 4176 spi = acpi_spi_find_device_by_adev(adev); 4177 if (!spi) 4178 break; 4179 4180 spi_unregister_device(spi); 4181 put_device(&spi->dev); 4182 break; 4183 } 4184 4185 return NOTIFY_OK; 4186 } 4187 4188 static struct notifier_block spi_acpi_notifier = { 4189 .notifier_call = acpi_spi_notify, 4190 }; 4191 #else 4192 extern struct notifier_block spi_acpi_notifier; 4193 #endif 4194 4195 static int __init spi_init(void) 4196 { 4197 int status; 4198 4199 buf = kmalloc(SPI_BUFSIZ, GFP_KERNEL); 4200 if (!buf) { 4201 status = -ENOMEM; 4202 goto err0; 4203 } 4204 4205 status = bus_register(&spi_bus_type); 4206 if (status < 0) 4207 goto err1; 4208 4209 status = class_register(&spi_master_class); 4210 if (status < 0) 4211 goto err2; 4212 4213 if (IS_ENABLED(CONFIG_SPI_SLAVE)) { 4214 status = class_register(&spi_slave_class); 4215 if (status < 0) 4216 goto err3; 4217 } 4218 4219 if (IS_ENABLED(CONFIG_OF_DYNAMIC)) 4220 WARN_ON(of_reconfig_notifier_register(&spi_of_notifier)); 4221 if (IS_ENABLED(CONFIG_ACPI)) 4222 WARN_ON(acpi_reconfig_notifier_register(&spi_acpi_notifier)); 4223 4224 return 0; 4225 4226 err3: 4227 class_unregister(&spi_master_class); 4228 err2: 4229 bus_unregister(&spi_bus_type); 4230 err1: 4231 kfree(buf); 4232 buf = NULL; 4233 err0: 4234 return status; 4235 } 4236 4237 /* board_info is normally registered in arch_initcall(), 4238 * but even essential drivers wait till later 4239 * 4240 * REVISIT only boardinfo really needs static linking. the rest (device and 4241 * driver registration) _could_ be dynamically linked (modular) ... costs 4242 * include needing to have boardinfo data structures be much more public. 4243 */ 4244 postcore_initcall(spi_init); 4245