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