lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1 | /* |
| 2 | * SPI init/core code |
| 3 | * |
| 4 | * Copyright (C) 2005 David Brownell |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 19 | */ |
| 20 | |
| 21 | #include <linux/kernel.h> |
| 22 | #include <linux/device.h> |
| 23 | #include <linux/init.h> |
| 24 | #include <linux/cache.h> |
| 25 | #include <linux/mutex.h> |
| 26 | #include <linux/of_device.h> |
| 27 | #include <linux/slab.h> |
| 28 | #include <linux/mod_devicetable.h> |
| 29 | #include <linux/spi/spi.h> |
| 30 | #include <linux/of_spi.h> |
| 31 | #include <linux/pm_runtime.h> |
| 32 | #include <linux/export.h> |
| 33 | #include <linux/sched.h> |
| 34 | #include <linux/delay.h> |
| 35 | #include <linux/kthread.h> |
| 36 | |
| 37 | static void spidev_release(struct device *dev) |
| 38 | { |
| 39 | struct spi_device *spi = to_spi_device(dev); |
| 40 | |
| 41 | if (!dev) |
| 42 | return; |
| 43 | |
| 44 | /* spi masters may cleanup for released devices */ |
| 45 | if (spi->master->cleanup) |
| 46 | spi->master->cleanup(spi); |
| 47 | |
| 48 | spi_master_put(spi->master); |
| 49 | kfree(spi); |
| 50 | } |
| 51 | |
| 52 | static ssize_t |
| 53 | modalias_show(struct device *dev, struct device_attribute *a, char *buf) |
| 54 | { |
| 55 | const struct spi_device *spi = to_spi_device(dev); |
| 56 | |
| 57 | return sprintf(buf, "%s\n", spi->modalias); |
| 58 | } |
| 59 | |
| 60 | static struct device_attribute spi_dev_attrs[] = { |
| 61 | __ATTR_RO(modalias), |
| 62 | __ATTR_NULL, |
| 63 | }; |
| 64 | |
| 65 | /* modalias support makes "modprobe $MODALIAS" new-style hotplug work, |
| 66 | * and the sysfs version makes coldplug work too. |
| 67 | */ |
| 68 | |
| 69 | static const struct spi_device_id *spi_match_id(const struct spi_device_id *id, |
| 70 | const struct spi_device *sdev) |
| 71 | { |
| 72 | while (id->name[0]) { |
| 73 | if (!strcmp(sdev->modalias, id->name)) |
| 74 | return id; |
| 75 | id++; |
| 76 | } |
| 77 | return NULL; |
| 78 | } |
| 79 | |
| 80 | const struct spi_device_id *spi_get_device_id(const struct spi_device *sdev) |
| 81 | { |
| 82 | const struct spi_driver *sdrv = to_spi_driver(sdev->dev.driver); |
| 83 | |
| 84 | #ifdef CONFIG_KLOCWORK |
| 85 | if (!sdrv) |
| 86 | return NULL; |
| 87 | #endif |
| 88 | |
| 89 | return spi_match_id(sdrv->id_table, sdev); |
| 90 | } |
| 91 | EXPORT_SYMBOL_GPL(spi_get_device_id); |
| 92 | |
| 93 | static int spi_match_device(struct device *dev, struct device_driver *drv) |
| 94 | { |
| 95 | const struct spi_device *spi = to_spi_device(dev); |
| 96 | const struct spi_driver *sdrv = to_spi_driver(drv); |
| 97 | |
| 98 | /* Attempt an OF style match */ |
| 99 | if (of_driver_match_device(dev, drv)) |
| 100 | return 1; |
| 101 | |
| 102 | #ifdef CONFIG_KLOCWORK |
| 103 | if (!dev || !drv) |
| 104 | return -ENODEV; |
| 105 | #endif |
| 106 | |
| 107 | if (sdrv->id_table) |
| 108 | return !!spi_match_id(sdrv->id_table, spi); |
| 109 | |
| 110 | return strcmp(spi->modalias, drv->name) == 0; |
| 111 | } |
| 112 | |
| 113 | static int spi_uevent(struct device *dev, struct kobj_uevent_env *env) |
| 114 | { |
| 115 | const struct spi_device *spi = to_spi_device(dev); |
| 116 | |
| 117 | add_uevent_var(env, "MODALIAS=%s%s", SPI_MODULE_PREFIX, spi->modalias); |
| 118 | return 0; |
| 119 | } |
| 120 | |
| 121 | #ifdef CONFIG_PM_SLEEP |
| 122 | static int spi_legacy_suspend(struct device *dev, pm_message_t message) |
| 123 | { |
| 124 | int value = 0; |
| 125 | struct spi_driver *drv = to_spi_driver(dev->driver); |
| 126 | |
| 127 | /* suspend will stop irqs and dma; no more i/o */ |
| 128 | if (drv) { |
| 129 | if (drv->suspend) |
| 130 | value = drv->suspend(to_spi_device(dev), message); |
| 131 | else |
| 132 | dev_dbg(dev, "... can't suspend\n"); |
| 133 | } |
| 134 | return value; |
| 135 | } |
| 136 | |
| 137 | static int spi_legacy_resume(struct device *dev) |
| 138 | { |
| 139 | int value = 0; |
| 140 | struct spi_driver *drv = to_spi_driver(dev->driver); |
| 141 | |
| 142 | /* resume may restart the i/o queue */ |
| 143 | if (drv) { |
| 144 | if (drv->resume) |
| 145 | value = drv->resume(to_spi_device(dev)); |
| 146 | else |
| 147 | dev_dbg(dev, "... can't resume\n"); |
| 148 | } |
| 149 | return value; |
| 150 | } |
| 151 | |
| 152 | static int spi_pm_suspend(struct device *dev) |
| 153 | { |
| 154 | const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL; |
| 155 | |
| 156 | if (pm) |
| 157 | return pm_generic_suspend(dev); |
| 158 | else |
| 159 | return spi_legacy_suspend(dev, PMSG_SUSPEND); |
| 160 | } |
| 161 | |
| 162 | static int spi_pm_resume(struct device *dev) |
| 163 | { |
| 164 | const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL; |
| 165 | |
| 166 | if (pm) |
| 167 | return pm_generic_resume(dev); |
| 168 | else |
| 169 | return spi_legacy_resume(dev); |
| 170 | } |
| 171 | |
| 172 | static int spi_pm_freeze(struct device *dev) |
| 173 | { |
| 174 | const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL; |
| 175 | |
| 176 | if (pm) |
| 177 | return pm_generic_freeze(dev); |
| 178 | else |
| 179 | return spi_legacy_suspend(dev, PMSG_FREEZE); |
| 180 | } |
| 181 | |
| 182 | static int spi_pm_thaw(struct device *dev) |
| 183 | { |
| 184 | const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL; |
| 185 | |
| 186 | if (pm) |
| 187 | return pm_generic_thaw(dev); |
| 188 | else |
| 189 | return spi_legacy_resume(dev); |
| 190 | } |
| 191 | |
| 192 | static int spi_pm_poweroff(struct device *dev) |
| 193 | { |
| 194 | const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL; |
| 195 | |
| 196 | if (pm) |
| 197 | return pm_generic_poweroff(dev); |
| 198 | else |
| 199 | return spi_legacy_suspend(dev, PMSG_HIBERNATE); |
| 200 | } |
| 201 | |
| 202 | static int spi_pm_restore(struct device *dev) |
| 203 | { |
| 204 | const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL; |
| 205 | |
| 206 | if (pm) |
| 207 | return pm_generic_restore(dev); |
| 208 | else |
| 209 | return spi_legacy_resume(dev); |
| 210 | } |
| 211 | #else |
| 212 | #define spi_pm_suspend NULL |
| 213 | #define spi_pm_resume NULL |
| 214 | #define spi_pm_freeze NULL |
| 215 | #define spi_pm_thaw NULL |
| 216 | #define spi_pm_poweroff NULL |
| 217 | #define spi_pm_restore NULL |
| 218 | #endif |
| 219 | |
| 220 | static const struct dev_pm_ops spi_pm = { |
| 221 | .suspend = spi_pm_suspend, |
| 222 | .resume = spi_pm_resume, |
| 223 | .freeze = spi_pm_freeze, |
| 224 | .thaw = spi_pm_thaw, |
| 225 | .poweroff = spi_pm_poweroff, |
| 226 | .restore = spi_pm_restore, |
| 227 | SET_RUNTIME_PM_OPS( |
| 228 | pm_generic_runtime_suspend, |
| 229 | pm_generic_runtime_resume, |
| 230 | pm_generic_runtime_idle |
| 231 | ) |
| 232 | }; |
| 233 | |
| 234 | struct bus_type spi_bus_type = { |
| 235 | .name = "spi", |
| 236 | .dev_attrs = spi_dev_attrs, |
| 237 | .match = spi_match_device, |
| 238 | .uevent = spi_uevent, |
| 239 | .pm = &spi_pm, |
| 240 | }; |
| 241 | EXPORT_SYMBOL_GPL(spi_bus_type); |
| 242 | |
| 243 | |
| 244 | static int spi_drv_probe(struct device *dev) |
| 245 | { |
| 246 | const struct spi_driver *sdrv = to_spi_driver(dev->driver); |
| 247 | |
| 248 | #ifdef CONFIG_KLOCWORK |
| 249 | if (!sdrv) |
| 250 | return -ENODEV; |
| 251 | #endif |
| 252 | |
| 253 | return sdrv->probe(to_spi_device(dev)); |
| 254 | } |
| 255 | |
| 256 | static int spi_drv_remove(struct device *dev) |
| 257 | { |
| 258 | const struct spi_driver *sdrv = to_spi_driver(dev->driver); |
| 259 | |
| 260 | #ifdef CONFIG_KLOCWORK |
| 261 | if (!sdrv) |
| 262 | return -ENODEV; |
| 263 | #endif |
| 264 | |
| 265 | return sdrv->remove(to_spi_device(dev)); |
| 266 | } |
| 267 | |
| 268 | static void spi_drv_shutdown(struct device *dev) |
| 269 | { |
| 270 | const struct spi_driver *sdrv = to_spi_driver(dev->driver); |
| 271 | |
| 272 | #ifdef CONFIG_KLOCWORK |
| 273 | if (!sdrv) |
| 274 | return; |
| 275 | #endif |
| 276 | |
| 277 | sdrv->shutdown(to_spi_device(dev)); |
| 278 | } |
| 279 | |
| 280 | /** |
| 281 | * spi_register_driver - register a SPI driver |
| 282 | * @sdrv: the driver to register |
| 283 | * Context: can sleep |
| 284 | */ |
| 285 | int spi_register_driver(struct spi_driver *sdrv) |
| 286 | { |
| 287 | sdrv->driver.bus = &spi_bus_type; |
| 288 | if (sdrv->probe) |
| 289 | sdrv->driver.probe = spi_drv_probe; |
| 290 | if (sdrv->remove) |
| 291 | sdrv->driver.remove = spi_drv_remove; |
| 292 | if (sdrv->shutdown) |
| 293 | sdrv->driver.shutdown = spi_drv_shutdown; |
| 294 | return driver_register(&sdrv->driver); |
| 295 | } |
| 296 | EXPORT_SYMBOL_GPL(spi_register_driver); |
| 297 | |
| 298 | /*-------------------------------------------------------------------------*/ |
| 299 | |
| 300 | /* SPI devices should normally not be created by SPI device drivers; that |
| 301 | * would make them board-specific. Similarly with SPI master drivers. |
| 302 | * Device registration normally goes into like arch/.../mach.../board-YYY.c |
| 303 | * with other readonly (flashable) information about mainboard devices. |
| 304 | */ |
| 305 | |
| 306 | struct boardinfo { |
| 307 | struct list_head list; |
| 308 | struct spi_board_info board_info; |
| 309 | }; |
| 310 | |
| 311 | static LIST_HEAD(board_list); |
| 312 | static LIST_HEAD(spi_master_list); |
| 313 | |
| 314 | /* |
| 315 | * Used to protect add/del opertion for board_info list and |
| 316 | * spi_master list, and their matching process |
| 317 | */ |
| 318 | static DEFINE_MUTEX(board_lock); |
| 319 | |
| 320 | /** |
| 321 | * spi_alloc_device - Allocate a new SPI device |
| 322 | * @master: Controller to which device is connected |
| 323 | * Context: can sleep |
| 324 | * |
| 325 | * Allows a driver to allocate and initialize a spi_device without |
| 326 | * registering it immediately. This allows a driver to directly |
| 327 | * fill the spi_device with device parameters before calling |
| 328 | * spi_add_device() on it. |
| 329 | * |
| 330 | * Caller is responsible to call spi_add_device() on the returned |
| 331 | * spi_device structure to add it to the SPI master. If the caller |
| 332 | * needs to discard the spi_device without adding it, then it should |
| 333 | * call spi_dev_put() on it. |
| 334 | * |
| 335 | * Returns a pointer to the new device, or NULL. |
| 336 | */ |
| 337 | struct spi_device *spi_alloc_device(struct spi_master *master) |
| 338 | { |
| 339 | struct spi_device *spi; |
| 340 | struct device *dev = master->dev.parent; |
| 341 | |
| 342 | if (!spi_master_get(master)) |
| 343 | return NULL; |
| 344 | |
| 345 | spi = kzalloc(sizeof *spi, GFP_KERNEL); |
| 346 | if (!spi) { |
| 347 | dev_err(dev, "cannot alloc spi_device\n"); |
| 348 | spi_master_put(master); |
| 349 | return NULL; |
| 350 | } |
| 351 | |
| 352 | spi->master = master; |
| 353 | spi->dev.parent = &master->dev; |
| 354 | spi->dev.bus = &spi_bus_type; |
| 355 | spi->dev.release = spidev_release; |
| 356 | device_initialize(&spi->dev); |
| 357 | return spi; |
| 358 | } |
| 359 | EXPORT_SYMBOL_GPL(spi_alloc_device); |
| 360 | |
| 361 | /** |
| 362 | * spi_add_device - Add spi_device allocated with spi_alloc_device |
| 363 | * @spi: spi_device to register |
| 364 | * |
| 365 | * Companion function to spi_alloc_device. Devices allocated with |
| 366 | * spi_alloc_device can be added onto the spi bus with this function. |
| 367 | * |
| 368 | * Returns 0 on success; negative errno on failure |
| 369 | */ |
| 370 | int spi_add_device(struct spi_device *spi) |
| 371 | { |
| 372 | static DEFINE_MUTEX(spi_add_lock); |
| 373 | struct device *dev = spi->master->dev.parent; |
| 374 | struct device *d; |
| 375 | int status; |
| 376 | |
| 377 | /* Chipselects are numbered 0..max; validate. */ |
| 378 | if (spi->chip_select >= spi->master->num_chipselect) { |
| 379 | dev_err(dev, "cs%d >= max %d\n", |
| 380 | spi->chip_select, |
| 381 | spi->master->num_chipselect); |
| 382 | return -EINVAL; |
| 383 | } |
| 384 | |
| 385 | /* Set the bus ID string */ |
| 386 | dev_set_name(&spi->dev, "%s.%u", dev_name(&spi->master->dev), |
| 387 | spi->chip_select); |
| 388 | |
| 389 | |
| 390 | /* We need to make sure there's no other device with this |
| 391 | * chipselect **BEFORE** we call setup(), else we'll trash |
| 392 | * its configuration. Lock against concurrent add() calls. |
| 393 | */ |
| 394 | mutex_lock(&spi_add_lock); |
| 395 | |
| 396 | d = bus_find_device_by_name(&spi_bus_type, NULL, dev_name(&spi->dev)); |
| 397 | if (d != NULL) { |
| 398 | dev_err(dev, "chipselect %d already in use\n", |
| 399 | spi->chip_select); |
| 400 | put_device(d); |
| 401 | status = -EBUSY; |
| 402 | goto done; |
| 403 | } |
| 404 | |
| 405 | /* Drivers may modify this initial i/o setup, but will |
| 406 | * normally rely on the device being setup. Devices |
| 407 | * using SPI_CS_HIGH can't coexist well otherwise... |
| 408 | */ |
| 409 | status = spi_setup(spi); |
| 410 | if (status < 0) { |
| 411 | dev_err(dev, "can't setup %s, status %d\n", |
| 412 | dev_name(&spi->dev), status); |
| 413 | goto done; |
| 414 | } |
| 415 | |
| 416 | /* Device may be bound to an active driver when this returns */ |
| 417 | status = device_add(&spi->dev); |
| 418 | if (status < 0) |
| 419 | dev_err(dev, "can't add %s, status %d\n", |
| 420 | dev_name(&spi->dev), status); |
| 421 | else |
| 422 | dev_dbg(dev, "registered child %s\n", dev_name(&spi->dev)); |
| 423 | |
| 424 | done: |
| 425 | mutex_unlock(&spi_add_lock); |
| 426 | return status; |
| 427 | } |
| 428 | EXPORT_SYMBOL_GPL(spi_add_device); |
| 429 | |
| 430 | /** |
| 431 | * spi_new_device - instantiate one new SPI device |
| 432 | * @master: Controller to which device is connected |
| 433 | * @chip: Describes the SPI device |
| 434 | * Context: can sleep |
| 435 | * |
| 436 | * On typical mainboards, this is purely internal; and it's not needed |
| 437 | * after board init creates the hard-wired devices. Some development |
| 438 | * platforms may not be able to use spi_register_board_info though, and |
| 439 | * this is exported so that for example a USB or parport based adapter |
| 440 | * driver could add devices (which it would learn about out-of-band). |
| 441 | * |
| 442 | * Returns the new device, or NULL. |
| 443 | */ |
| 444 | struct spi_device *spi_new_device(struct spi_master *master, |
| 445 | struct spi_board_info *chip) |
| 446 | { |
| 447 | struct spi_device *proxy; |
| 448 | int status; |
| 449 | |
| 450 | /* NOTE: caller did any chip->bus_num checks necessary. |
| 451 | * |
| 452 | * Also, unless we change the return value convention to use |
| 453 | * error-or-pointer (not NULL-or-pointer), troubleshootability |
| 454 | * suggests syslogged diagnostics are best here (ugh). |
| 455 | */ |
| 456 | |
| 457 | proxy = spi_alloc_device(master); |
| 458 | if (!proxy) |
| 459 | return NULL; |
| 460 | |
| 461 | WARN_ON(strlen(chip->modalias) >= sizeof(proxy->modalias)); |
| 462 | |
| 463 | proxy->chip_select = chip->chip_select; |
| 464 | proxy->max_speed_hz = chip->max_speed_hz; |
| 465 | proxy->mode = chip->mode; |
| 466 | proxy->irq = chip->irq; |
| 467 | strlcpy(proxy->modalias, chip->modalias, sizeof(proxy->modalias)); |
| 468 | proxy->dev.platform_data = (void *) chip->platform_data; |
| 469 | proxy->controller_data = chip->controller_data; |
| 470 | proxy->controller_state = NULL; |
| 471 | |
| 472 | status = spi_add_device(proxy); |
| 473 | if (status < 0) { |
| 474 | spi_dev_put(proxy); |
| 475 | return NULL; |
| 476 | } |
| 477 | |
| 478 | return proxy; |
| 479 | } |
| 480 | EXPORT_SYMBOL_GPL(spi_new_device); |
| 481 | |
| 482 | static void spi_match_master_to_boardinfo(struct spi_master *master, |
| 483 | struct spi_board_info *bi) |
| 484 | { |
| 485 | struct spi_device *dev; |
| 486 | |
| 487 | if (master->bus_num != bi->bus_num) |
| 488 | return; |
| 489 | |
| 490 | dev = spi_new_device(master, bi); |
| 491 | if (!dev) |
| 492 | dev_err(master->dev.parent, "can't create new device for %s\n", |
| 493 | bi->modalias); |
| 494 | } |
| 495 | |
| 496 | /** |
| 497 | * spi_register_board_info - register SPI devices for a given board |
| 498 | * @info: array of chip descriptors |
| 499 | * @n: how many descriptors are provided |
| 500 | * Context: can sleep |
| 501 | * |
| 502 | * Board-specific early init code calls this (probably during arch_initcall) |
| 503 | * with segments of the SPI device table. Any device nodes are created later, |
| 504 | * after the relevant parent SPI controller (bus_num) is defined. We keep |
| 505 | * this table of devices forever, so that reloading a controller driver will |
| 506 | * not make Linux forget about these hard-wired devices. |
| 507 | * |
| 508 | * Other code can also call this, e.g. a particular add-on board might provide |
| 509 | * SPI devices through its expansion connector, so code initializing that board |
| 510 | * would naturally declare its SPI devices. |
| 511 | * |
| 512 | * The board info passed can safely be __initdata ... but be careful of |
| 513 | * any embedded pointers (platform_data, etc), they're copied as-is. |
| 514 | */ |
| 515 | int __devinit |
| 516 | spi_register_board_info(struct spi_board_info const *info, unsigned n) |
| 517 | { |
| 518 | struct boardinfo *bi; |
| 519 | int i; |
| 520 | |
| 521 | if (!n) |
| 522 | return 0; |
| 523 | |
| 524 | bi = kzalloc(n * sizeof(*bi), GFP_KERNEL); |
| 525 | if (!bi) |
| 526 | return -ENOMEM; |
| 527 | |
| 528 | for (i = 0; i < n; i++, bi++, info++) { |
| 529 | struct spi_master *master; |
| 530 | |
| 531 | memcpy(&bi->board_info, info, sizeof(*info)); |
| 532 | mutex_lock(&board_lock); |
| 533 | list_add_tail(&bi->list, &board_list); |
| 534 | list_for_each_entry(master, &spi_master_list, list) |
| 535 | spi_match_master_to_boardinfo(master, &bi->board_info); |
| 536 | mutex_unlock(&board_lock); |
| 537 | } |
| 538 | |
| 539 | return 0; |
| 540 | } |
| 541 | EXPORT_SYMBOL_GPL(spi_register_board_info); |
| 542 | /*-------------------------------------------------------------------------*/ |
| 543 | |
| 544 | /** |
| 545 | * spi_pump_messages - kthread work function which processes spi message queue |
| 546 | * @work: pointer to kthread work struct contained in the master struct |
| 547 | * |
| 548 | * This function checks if there is any spi message in the queue that |
| 549 | * needs processing and if so call out to the driver to initialize hardware |
| 550 | * and transfer each message. |
| 551 | * |
| 552 | */ |
| 553 | static void spi_pump_messages(struct kthread_work *work) |
| 554 | { |
| 555 | struct spi_master *master = |
| 556 | container_of(work, struct spi_master, pump_messages); |
| 557 | unsigned long flags; |
| 558 | bool was_busy = false; |
| 559 | int ret; |
| 560 | |
| 561 | /* Lock queue and check for queue work */ |
| 562 | spin_lock_irqsave(&master->queue_lock, flags); |
| 563 | if (list_empty(&master->queue) || !master->running) { |
| 564 | if (master->busy) { |
| 565 | ret = master->unprepare_transfer_hardware(master); |
| 566 | if (ret) { |
| 567 | spin_unlock_irqrestore(&master->queue_lock, flags); |
| 568 | dev_err(&master->dev, |
| 569 | "failed to unprepare transfer hardware\n"); |
| 570 | return; |
| 571 | } |
| 572 | } |
| 573 | master->busy = false; |
| 574 | spin_unlock_irqrestore(&master->queue_lock, flags); |
| 575 | return; |
| 576 | } |
| 577 | |
| 578 | /* Make sure we are not already running a message */ |
| 579 | if (master->cur_msg) { |
| 580 | spin_unlock_irqrestore(&master->queue_lock, flags); |
| 581 | return; |
| 582 | } |
| 583 | /* Extract head of queue */ |
| 584 | master->cur_msg = |
| 585 | list_entry(master->queue.next, struct spi_message, queue); |
| 586 | |
| 587 | list_del_init(&master->cur_msg->queue); |
| 588 | if (master->busy) |
| 589 | was_busy = true; |
| 590 | else |
| 591 | master->busy = true; |
| 592 | spin_unlock_irqrestore(&master->queue_lock, flags); |
| 593 | |
| 594 | if (!was_busy) { |
| 595 | ret = master->prepare_transfer_hardware(master); |
| 596 | if (ret) { |
| 597 | dev_err(&master->dev, |
| 598 | "failed to prepare transfer hardware\n"); |
| 599 | return; |
| 600 | } |
| 601 | } |
| 602 | |
| 603 | ret = master->transfer_one_message(master, master->cur_msg); |
| 604 | if (ret) { |
| 605 | dev_err(&master->dev, |
| 606 | "failed to transfer one message from queue\n"); |
| 607 | return; |
| 608 | } |
| 609 | } |
| 610 | |
| 611 | static int spi_init_queue(struct spi_master *master) |
| 612 | { |
| 613 | struct sched_param param = { .sched_priority = MAX_RT_PRIO - 1 }; |
| 614 | |
| 615 | INIT_LIST_HEAD(&master->queue); |
| 616 | spin_lock_init(&master->queue_lock); |
| 617 | |
| 618 | master->running = false; |
| 619 | master->busy = false; |
| 620 | |
| 621 | init_kthread_worker(&master->kworker); |
| 622 | master->kworker_task = kthread_run(kthread_worker_fn, |
| 623 | &master->kworker, |
| 624 | dev_name(&master->dev)); |
| 625 | if (IS_ERR(master->kworker_task)) { |
| 626 | dev_err(&master->dev, "failed to create message pump task\n"); |
| 627 | return -ENOMEM; |
| 628 | } |
| 629 | init_kthread_work(&master->pump_messages, spi_pump_messages); |
| 630 | |
| 631 | /* |
| 632 | * Master config will indicate if this controller should run the |
| 633 | * message pump with high (realtime) priority to reduce the transfer |
| 634 | * latency on the bus by minimising the delay between a transfer |
| 635 | * request and the scheduling of the message pump thread. Without this |
| 636 | * setting the message pump thread will remain at default priority. |
| 637 | */ |
| 638 | if (master->rt) { |
| 639 | dev_info(&master->dev, |
| 640 | "will run message pump with realtime priority\n"); |
| 641 | sched_setscheduler(master->kworker_task, SCHED_FIFO, ¶m); |
| 642 | } |
| 643 | |
| 644 | return 0; |
| 645 | } |
| 646 | |
| 647 | /** |
| 648 | * spi_get_next_queued_message() - called by driver to check for queued |
| 649 | * messages |
| 650 | * @master: the master to check for queued messages |
| 651 | * |
| 652 | * If there are more messages in the queue, the next message is returned from |
| 653 | * this call. |
| 654 | */ |
| 655 | struct spi_message *spi_get_next_queued_message(struct spi_master *master) |
| 656 | { |
| 657 | struct spi_message *next; |
| 658 | unsigned long flags; |
| 659 | |
| 660 | /* get a pointer to the next message, if any */ |
| 661 | spin_lock_irqsave(&master->queue_lock, flags); |
| 662 | if (list_empty(&master->queue)) |
| 663 | next = NULL; |
| 664 | else |
| 665 | next = list_entry(master->queue.next, |
| 666 | struct spi_message, queue); |
| 667 | spin_unlock_irqrestore(&master->queue_lock, flags); |
| 668 | |
| 669 | return next; |
| 670 | } |
| 671 | EXPORT_SYMBOL_GPL(spi_get_next_queued_message); |
| 672 | |
| 673 | /** |
| 674 | * spi_finalize_current_message() - the current message is complete |
| 675 | * @master: the master to return the message to |
| 676 | * |
| 677 | * Called by the driver to notify the core that the message in the front of the |
| 678 | * queue is complete and can be removed from the queue. |
| 679 | */ |
| 680 | void spi_finalize_current_message(struct spi_master *master) |
| 681 | { |
| 682 | struct spi_message *mesg; |
| 683 | unsigned long flags; |
| 684 | |
| 685 | spin_lock_irqsave(&master->queue_lock, flags); |
| 686 | mesg = master->cur_msg; |
| 687 | master->cur_msg = NULL; |
| 688 | |
| 689 | queue_kthread_work(&master->kworker, &master->pump_messages); |
| 690 | spin_unlock_irqrestore(&master->queue_lock, flags); |
| 691 | |
| 692 | mesg->state = NULL; |
| 693 | if (mesg->complete) |
| 694 | mesg->complete(mesg->context); |
| 695 | } |
| 696 | EXPORT_SYMBOL_GPL(spi_finalize_current_message); |
| 697 | |
| 698 | static int spi_start_queue(struct spi_master *master) |
| 699 | { |
| 700 | unsigned long flags; |
| 701 | |
| 702 | spin_lock_irqsave(&master->queue_lock, flags); |
| 703 | |
| 704 | if (master->running || master->busy) { |
| 705 | spin_unlock_irqrestore(&master->queue_lock, flags); |
| 706 | return -EBUSY; |
| 707 | } |
| 708 | |
| 709 | master->running = true; |
| 710 | master->cur_msg = NULL; |
| 711 | spin_unlock_irqrestore(&master->queue_lock, flags); |
| 712 | |
| 713 | queue_kthread_work(&master->kworker, &master->pump_messages); |
| 714 | |
| 715 | return 0; |
| 716 | } |
| 717 | |
| 718 | static int spi_stop_queue(struct spi_master *master) |
| 719 | { |
| 720 | unsigned long flags; |
| 721 | unsigned limit = 500; |
| 722 | int ret = 0; |
| 723 | |
| 724 | spin_lock_irqsave(&master->queue_lock, flags); |
| 725 | |
| 726 | /* |
| 727 | * This is a bit lame, but is optimized for the common execution path. |
| 728 | * A wait_queue on the master->busy could be used, but then the common |
| 729 | * execution path (pump_messages) would be required to call wake_up or |
| 730 | * friends on every SPI message. Do this instead. |
| 731 | */ |
| 732 | while ((!list_empty(&master->queue) || master->busy) && limit--) { |
| 733 | spin_unlock_irqrestore(&master->queue_lock, flags); |
| 734 | msleep(10); |
| 735 | spin_lock_irqsave(&master->queue_lock, flags); |
| 736 | } |
| 737 | |
| 738 | if (!list_empty(&master->queue) || master->busy) |
| 739 | ret = -EBUSY; |
| 740 | else |
| 741 | master->running = false; |
| 742 | |
| 743 | spin_unlock_irqrestore(&master->queue_lock, flags); |
| 744 | |
| 745 | if (ret) { |
| 746 | dev_warn(&master->dev, |
| 747 | "could not stop message queue\n"); |
| 748 | return ret; |
| 749 | } |
| 750 | return ret; |
| 751 | } |
| 752 | |
| 753 | static int spi_destroy_queue(struct spi_master *master) |
| 754 | { |
| 755 | int ret; |
| 756 | |
| 757 | ret = spi_stop_queue(master); |
| 758 | |
| 759 | /* |
| 760 | * flush_kthread_worker will block until all work is done. |
| 761 | * If the reason that stop_queue timed out is that the work will never |
| 762 | * finish, then it does no good to call flush/stop thread, so |
| 763 | * return anyway. |
| 764 | */ |
| 765 | if (ret) { |
| 766 | dev_err(&master->dev, "problem destroying queue\n"); |
| 767 | return ret; |
| 768 | } |
| 769 | |
| 770 | flush_kthread_worker(&master->kworker); |
| 771 | kthread_stop(master->kworker_task); |
| 772 | |
| 773 | return 0; |
| 774 | } |
| 775 | |
| 776 | /** |
| 777 | * spi_queued_transfer - transfer function for queued transfers |
| 778 | * @spi: spi device which is requesting transfer |
| 779 | * @msg: spi message which is to handled is queued to driver queue |
| 780 | */ |
| 781 | static int spi_queued_transfer(struct spi_device *spi, struct spi_message *msg) |
| 782 | { |
| 783 | struct spi_master *master = spi->master; |
| 784 | unsigned long flags; |
| 785 | |
| 786 | spin_lock_irqsave(&master->queue_lock, flags); |
| 787 | |
| 788 | if (!master->running) { |
| 789 | spin_unlock_irqrestore(&master->queue_lock, flags); |
| 790 | return -ESHUTDOWN; |
| 791 | } |
| 792 | msg->actual_length = 0; |
| 793 | msg->status = -EINPROGRESS; |
| 794 | |
| 795 | list_add_tail(&msg->queue, &master->queue); |
| 796 | if (master->running && !master->busy) |
| 797 | queue_kthread_work(&master->kworker, &master->pump_messages); |
| 798 | |
| 799 | spin_unlock_irqrestore(&master->queue_lock, flags); |
| 800 | return 0; |
| 801 | } |
| 802 | |
| 803 | static int spi_master_initialize_queue(struct spi_master *master) |
| 804 | { |
| 805 | int ret; |
| 806 | |
| 807 | master->queued = true; |
| 808 | master->transfer = spi_queued_transfer; |
| 809 | |
| 810 | /* Initialize and start queue */ |
| 811 | ret = spi_init_queue(master); |
| 812 | if (ret) { |
| 813 | dev_err(&master->dev, "problem initializing queue\n"); |
| 814 | goto err_init_queue; |
| 815 | } |
| 816 | ret = spi_start_queue(master); |
| 817 | if (ret) { |
| 818 | dev_err(&master->dev, "problem starting queue\n"); |
| 819 | goto err_start_queue; |
| 820 | } |
| 821 | |
| 822 | return 0; |
| 823 | |
| 824 | err_start_queue: |
| 825 | err_init_queue: |
| 826 | spi_destroy_queue(master); |
| 827 | return ret; |
| 828 | } |
| 829 | |
| 830 | /*-------------------------------------------------------------------------*/ |
| 831 | |
| 832 | static void spi_master_release(struct device *dev) |
| 833 | { |
| 834 | struct spi_master *master; |
| 835 | |
| 836 | master = container_of(dev, struct spi_master, dev); |
| 837 | kfree(master); |
| 838 | } |
| 839 | |
| 840 | static struct class spi_master_class = { |
| 841 | .name = "spi_master", |
| 842 | .owner = THIS_MODULE, |
| 843 | .dev_release = spi_master_release, |
| 844 | }; |
| 845 | |
| 846 | |
| 847 | |
| 848 | /** |
| 849 | * spi_alloc_master - allocate SPI master controller |
| 850 | * @dev: the controller, possibly using the platform_bus |
| 851 | * @size: how much zeroed driver-private data to allocate; the pointer to this |
| 852 | * memory is in the driver_data field of the returned device, |
| 853 | * accessible with spi_master_get_devdata(). |
| 854 | * Context: can sleep |
| 855 | * |
| 856 | * This call is used only by SPI master controller drivers, which are the |
| 857 | * only ones directly touching chip registers. It's how they allocate |
| 858 | * an spi_master structure, prior to calling spi_register_master(). |
| 859 | * |
| 860 | * This must be called from context that can sleep. It returns the SPI |
| 861 | * master structure on success, else NULL. |
| 862 | * |
| 863 | * The caller is responsible for assigning the bus number and initializing |
| 864 | * the master's methods before calling spi_register_master(); and (after errors |
| 865 | * adding the device) calling spi_master_put() and kfree() to prevent a memory |
| 866 | * leak. |
| 867 | */ |
| 868 | struct spi_master *spi_alloc_master(struct device *dev, unsigned size) |
| 869 | { |
| 870 | struct spi_master *master; |
| 871 | |
| 872 | if (!dev) |
| 873 | return NULL; |
| 874 | |
| 875 | master = kzalloc(size + sizeof *master, GFP_KERNEL); |
| 876 | if (!master) |
| 877 | return NULL; |
| 878 | |
| 879 | device_initialize(&master->dev); |
| 880 | master->dev.class = &spi_master_class; |
| 881 | master->dev.parent = get_device(dev); |
| 882 | spi_master_set_devdata(master, &master[1]); |
| 883 | |
| 884 | return master; |
| 885 | } |
| 886 | EXPORT_SYMBOL_GPL(spi_alloc_master); |
| 887 | |
| 888 | /** |
| 889 | * spi_register_master - register SPI master controller |
| 890 | * @master: initialized master, originally from spi_alloc_master() |
| 891 | * Context: can sleep |
| 892 | * |
| 893 | * SPI master controllers connect to their drivers using some non-SPI bus, |
| 894 | * such as the platform bus. The final stage of probe() in that code |
| 895 | * includes calling spi_register_master() to hook up to this SPI bus glue. |
| 896 | * |
| 897 | * SPI controllers use board specific (often SOC specific) bus numbers, |
| 898 | * and board-specific addressing for SPI devices combines those numbers |
| 899 | * with chip select numbers. Since SPI does not directly support dynamic |
| 900 | * device identification, boards need configuration tables telling which |
| 901 | * chip is at which address. |
| 902 | * |
| 903 | * This must be called from context that can sleep. It returns zero on |
| 904 | * success, else a negative error code (dropping the master's refcount). |
| 905 | * After a successful return, the caller is responsible for calling |
| 906 | * spi_unregister_master(). |
| 907 | */ |
| 908 | int spi_register_master(struct spi_master *master) |
| 909 | { |
| 910 | static atomic_t dyn_bus_id = ATOMIC_INIT((1<<15) - 1); |
| 911 | struct device *dev = master->dev.parent; |
| 912 | struct boardinfo *bi; |
| 913 | int status = -ENODEV; |
| 914 | int dynamic = 0; |
| 915 | |
| 916 | if (!dev) |
| 917 | return -ENODEV; |
| 918 | |
| 919 | /* even if it's just one always-selected device, there must |
| 920 | * be at least one chipselect |
| 921 | */ |
| 922 | if (master->num_chipselect == 0) |
| 923 | return -EINVAL; |
| 924 | |
| 925 | /* convention: dynamically assigned bus IDs count down from the max */ |
| 926 | if (master->bus_num < 0) { |
| 927 | /* FIXME switch to an IDR based scheme, something like |
| 928 | * I2C now uses, so we can't run out of "dynamic" IDs |
| 929 | */ |
| 930 | master->bus_num = atomic_dec_return(&dyn_bus_id); |
| 931 | dynamic = 1; |
| 932 | } |
| 933 | |
| 934 | spin_lock_init(&master->bus_lock_spinlock); |
| 935 | mutex_init(&master->bus_lock_mutex); |
| 936 | master->bus_lock_flag = 0; |
| 937 | |
| 938 | /* register the device, then userspace will see it. |
| 939 | * registration fails if the bus ID is in use. |
| 940 | */ |
| 941 | dev_set_name(&master->dev, "spi%u", master->bus_num); |
| 942 | status = device_add(&master->dev); |
| 943 | if (status < 0) |
| 944 | goto done; |
| 945 | dev_dbg(dev, "registered master %s%s\n", dev_name(&master->dev), |
| 946 | dynamic ? " (dynamic)" : ""); |
| 947 | |
| 948 | /* If we're using a queued driver, start the queue */ |
| 949 | if (master->transfer) |
| 950 | dev_info(dev, "master is unqueued, this is deprecated\n"); |
| 951 | else { |
| 952 | status = spi_master_initialize_queue(master); |
| 953 | if (status) { |
| 954 | device_unregister(&master->dev); |
| 955 | goto done; |
| 956 | } |
| 957 | } |
| 958 | |
| 959 | mutex_lock(&board_lock); |
| 960 | list_add_tail(&master->list, &spi_master_list); |
| 961 | list_for_each_entry(bi, &board_list, list) |
| 962 | spi_match_master_to_boardinfo(master, &bi->board_info); |
| 963 | mutex_unlock(&board_lock); |
| 964 | |
| 965 | /* Register devices from the device tree */ |
| 966 | of_register_spi_devices(master); |
| 967 | done: |
| 968 | return status; |
| 969 | } |
| 970 | EXPORT_SYMBOL_GPL(spi_register_master); |
| 971 | |
| 972 | static int __unregister(struct device *dev, void *null) |
| 973 | { |
| 974 | spi_unregister_device(to_spi_device(dev)); |
| 975 | return 0; |
| 976 | } |
| 977 | |
| 978 | /** |
| 979 | * spi_unregister_master - unregister SPI master controller |
| 980 | * @master: the master being unregistered |
| 981 | * Context: can sleep |
| 982 | * |
| 983 | * This call is used only by SPI master controller drivers, which are the |
| 984 | * only ones directly touching chip registers. |
| 985 | * |
| 986 | * This must be called from context that can sleep. |
| 987 | */ |
| 988 | void spi_unregister_master(struct spi_master *master) |
| 989 | { |
| 990 | int dummy; |
| 991 | |
| 992 | if (master->queued) { |
| 993 | if (spi_destroy_queue(master)) |
| 994 | dev_err(&master->dev, "queue remove failed\n"); |
| 995 | } |
| 996 | |
| 997 | mutex_lock(&board_lock); |
| 998 | list_del(&master->list); |
| 999 | mutex_unlock(&board_lock); |
| 1000 | |
| 1001 | dummy = device_for_each_child(&master->dev, NULL, __unregister); |
| 1002 | device_unregister(&master->dev); |
| 1003 | } |
| 1004 | EXPORT_SYMBOL_GPL(spi_unregister_master); |
| 1005 | |
| 1006 | int spi_master_suspend(struct spi_master *master) |
| 1007 | { |
| 1008 | int ret; |
| 1009 | |
| 1010 | /* Basically no-ops for non-queued masters */ |
| 1011 | if (!master->queued) |
| 1012 | return 0; |
| 1013 | |
| 1014 | ret = spi_stop_queue(master); |
| 1015 | if (ret) |
| 1016 | dev_err(&master->dev, "queue stop failed\n"); |
| 1017 | |
| 1018 | return ret; |
| 1019 | } |
| 1020 | EXPORT_SYMBOL_GPL(spi_master_suspend); |
| 1021 | |
| 1022 | int spi_master_resume(struct spi_master *master) |
| 1023 | { |
| 1024 | int ret; |
| 1025 | |
| 1026 | if (!master->queued) |
| 1027 | return 0; |
| 1028 | |
| 1029 | ret = spi_start_queue(master); |
| 1030 | if (ret) |
| 1031 | dev_err(&master->dev, "queue restart failed\n"); |
| 1032 | |
| 1033 | return ret; |
| 1034 | } |
| 1035 | EXPORT_SYMBOL_GPL(spi_master_resume); |
| 1036 | |
| 1037 | static int __spi_master_match(struct device *dev, void *data) |
| 1038 | { |
| 1039 | struct spi_master *m; |
| 1040 | u16 *bus_num = data; |
| 1041 | |
| 1042 | m = container_of(dev, struct spi_master, dev); |
| 1043 | return m->bus_num == *bus_num; |
| 1044 | } |
| 1045 | |
| 1046 | /** |
| 1047 | * spi_busnum_to_master - look up master associated with bus_num |
| 1048 | * @bus_num: the master's bus number |
| 1049 | * Context: can sleep |
| 1050 | * |
| 1051 | * This call may be used with devices that are registered after |
| 1052 | * arch init time. It returns a refcounted pointer to the relevant |
| 1053 | * spi_master (which the caller must release), or NULL if there is |
| 1054 | * no such master registered. |
| 1055 | */ |
| 1056 | struct spi_master *spi_busnum_to_master(u16 bus_num) |
| 1057 | { |
| 1058 | struct device *dev; |
| 1059 | struct spi_master *master = NULL; |
| 1060 | |
| 1061 | dev = class_find_device(&spi_master_class, NULL, &bus_num, |
| 1062 | __spi_master_match); |
| 1063 | if (dev) |
| 1064 | master = container_of(dev, struct spi_master, dev); |
| 1065 | /* reference got in class_find_device */ |
| 1066 | return master; |
| 1067 | } |
| 1068 | EXPORT_SYMBOL_GPL(spi_busnum_to_master); |
| 1069 | |
| 1070 | |
| 1071 | /*-------------------------------------------------------------------------*/ |
| 1072 | |
| 1073 | /* Core methods for SPI master protocol drivers. Some of the |
| 1074 | * other core methods are currently defined as inline functions. |
| 1075 | */ |
| 1076 | |
| 1077 | /** |
| 1078 | * spi_setup - setup SPI mode and clock rate |
| 1079 | * @spi: the device whose settings are being modified |
| 1080 | * Context: can sleep, and no requests are queued to the device |
| 1081 | * |
| 1082 | * SPI protocol drivers may need to update the transfer mode if the |
| 1083 | * device doesn't work with its default. They may likewise need |
| 1084 | * to update clock rates or word sizes from initial values. This function |
| 1085 | * changes those settings, and must be called from a context that can sleep. |
| 1086 | * Except for SPI_CS_HIGH, which takes effect immediately, the changes take |
| 1087 | * effect the next time the device is selected and data is transferred to |
| 1088 | * or from it. When this function returns, the spi device is deselected. |
| 1089 | * |
| 1090 | * Note that this call will fail if the protocol driver specifies an option |
| 1091 | * that the underlying controller or its driver does not support. For |
| 1092 | * example, not all hardware supports wire transfers using nine bit words, |
| 1093 | * LSB-first wire encoding, or active-high chipselects. |
| 1094 | */ |
| 1095 | int spi_setup(struct spi_device *spi) |
| 1096 | { |
| 1097 | unsigned bad_bits; |
| 1098 | int status; |
| 1099 | |
| 1100 | /* help drivers fail *cleanly* when they need options |
| 1101 | * that aren't supported with their current master |
| 1102 | */ |
| 1103 | bad_bits = spi->mode & ~spi->master->mode_bits; |
| 1104 | if (bad_bits) { |
| 1105 | dev_err(&spi->dev, "setup: unsupported mode bits %x\n", |
| 1106 | bad_bits); |
| 1107 | return -EINVAL; |
| 1108 | } |
| 1109 | |
| 1110 | if (!spi->bits_per_word) |
| 1111 | spi->bits_per_word = 8; |
| 1112 | |
| 1113 | status = spi->master->setup(spi); |
| 1114 | |
| 1115 | dev_dbg(&spi->dev, "setup mode %d, %s%s%s%s" |
| 1116 | "%u bits/w, %u Hz max --> %d\n", |
| 1117 | (int) (spi->mode & (SPI_CPOL | SPI_CPHA)), |
| 1118 | (spi->mode & SPI_CS_HIGH) ? "cs_high, " : "", |
| 1119 | (spi->mode & SPI_LSB_FIRST) ? "lsb, " : "", |
| 1120 | (spi->mode & SPI_3WIRE) ? "3wire, " : "", |
| 1121 | (spi->mode & SPI_LOOP) ? "loopback, " : "", |
| 1122 | spi->bits_per_word, spi->max_speed_hz, |
| 1123 | status); |
| 1124 | |
| 1125 | return status; |
| 1126 | } |
| 1127 | EXPORT_SYMBOL_GPL(spi_setup); |
| 1128 | |
| 1129 | static int __spi_async(struct spi_device *spi, struct spi_message *message) |
| 1130 | { |
| 1131 | struct spi_master *master = spi->master; |
| 1132 | |
| 1133 | /* Half-duplex links include original MicroWire, and ones with |
| 1134 | * only one data pin like SPI_3WIRE (switches direction) or where |
| 1135 | * either MOSI or MISO is missing. They can also be caused by |
| 1136 | * software limitations. |
| 1137 | */ |
| 1138 | if ((master->flags & SPI_MASTER_HALF_DUPLEX) |
| 1139 | || (spi->mode & SPI_3WIRE)) { |
| 1140 | struct spi_transfer *xfer; |
| 1141 | unsigned flags = master->flags; |
| 1142 | |
| 1143 | list_for_each_entry(xfer, &message->transfers, transfer_list) { |
| 1144 | if (xfer->rx_buf && xfer->tx_buf) |
| 1145 | return -EINVAL; |
| 1146 | if ((flags & SPI_MASTER_NO_TX) && xfer->tx_buf) |
| 1147 | return -EINVAL; |
| 1148 | if ((flags & SPI_MASTER_NO_RX) && xfer->rx_buf) |
| 1149 | return -EINVAL; |
| 1150 | } |
| 1151 | } |
| 1152 | |
| 1153 | message->spi = spi; |
| 1154 | message->status = -EINPROGRESS; |
| 1155 | return master->transfer(spi, message); |
| 1156 | } |
| 1157 | |
| 1158 | /** |
| 1159 | * spi_async - asynchronous SPI transfer |
| 1160 | * @spi: device with which data will be exchanged |
| 1161 | * @message: describes the data transfers, including completion callback |
| 1162 | * Context: any (irqs may be blocked, etc) |
| 1163 | * |
| 1164 | * This call may be used in_irq and other contexts which can't sleep, |
| 1165 | * as well as from task contexts which can sleep. |
| 1166 | * |
| 1167 | * The completion callback is invoked in a context which can't sleep. |
| 1168 | * Before that invocation, the value of message->status is undefined. |
| 1169 | * When the callback is issued, message->status holds either zero (to |
| 1170 | * indicate complete success) or a negative error code. After that |
| 1171 | * callback returns, the driver which issued the transfer request may |
| 1172 | * deallocate the associated memory; it's no longer in use by any SPI |
| 1173 | * core or controller driver code. |
| 1174 | * |
| 1175 | * Note that although all messages to a spi_device are handled in |
| 1176 | * FIFO order, messages may go to different devices in other orders. |
| 1177 | * Some device might be higher priority, or have various "hard" access |
| 1178 | * time requirements, for example. |
| 1179 | * |
| 1180 | * On detection of any fault during the transfer, processing of |
| 1181 | * the entire message is aborted, and the device is deselected. |
| 1182 | * Until returning from the associated message completion callback, |
| 1183 | * no other spi_message queued to that device will be processed. |
| 1184 | * (This rule applies equally to all the synchronous transfer calls, |
| 1185 | * which are wrappers around this core asynchronous primitive.) |
| 1186 | */ |
| 1187 | int spi_async(struct spi_device *spi, struct spi_message *message) |
| 1188 | { |
| 1189 | struct spi_master *master = spi->master; |
| 1190 | int ret; |
| 1191 | unsigned long flags; |
| 1192 | |
| 1193 | spin_lock_irqsave(&master->bus_lock_spinlock, flags); |
| 1194 | |
| 1195 | if (master->bus_lock_flag) |
| 1196 | ret = -EBUSY; |
| 1197 | else |
| 1198 | ret = __spi_async(spi, message); |
| 1199 | |
| 1200 | spin_unlock_irqrestore(&master->bus_lock_spinlock, flags); |
| 1201 | |
| 1202 | return ret; |
| 1203 | } |
| 1204 | EXPORT_SYMBOL_GPL(spi_async); |
| 1205 | |
| 1206 | /** |
| 1207 | * spi_async_locked - version of spi_async with exclusive bus usage |
| 1208 | * @spi: device with which data will be exchanged |
| 1209 | * @message: describes the data transfers, including completion callback |
| 1210 | * Context: any (irqs may be blocked, etc) |
| 1211 | * |
| 1212 | * This call may be used in_irq and other contexts which can't sleep, |
| 1213 | * as well as from task contexts which can sleep. |
| 1214 | * |
| 1215 | * The completion callback is invoked in a context which can't sleep. |
| 1216 | * Before that invocation, the value of message->status is undefined. |
| 1217 | * When the callback is issued, message->status holds either zero (to |
| 1218 | * indicate complete success) or a negative error code. After that |
| 1219 | * callback returns, the driver which issued the transfer request may |
| 1220 | * deallocate the associated memory; it's no longer in use by any SPI |
| 1221 | * core or controller driver code. |
| 1222 | * |
| 1223 | * Note that although all messages to a spi_device are handled in |
| 1224 | * FIFO order, messages may go to different devices in other orders. |
| 1225 | * Some device might be higher priority, or have various "hard" access |
| 1226 | * time requirements, for example. |
| 1227 | * |
| 1228 | * On detection of any fault during the transfer, processing of |
| 1229 | * the entire message is aborted, and the device is deselected. |
| 1230 | * Until returning from the associated message completion callback, |
| 1231 | * no other spi_message queued to that device will be processed. |
| 1232 | * (This rule applies equally to all the synchronous transfer calls, |
| 1233 | * which are wrappers around this core asynchronous primitive.) |
| 1234 | */ |
| 1235 | int spi_async_locked(struct spi_device *spi, struct spi_message *message) |
| 1236 | { |
| 1237 | struct spi_master *master = spi->master; |
| 1238 | int ret; |
| 1239 | unsigned long flags; |
| 1240 | |
| 1241 | spin_lock_irqsave(&master->bus_lock_spinlock, flags); |
| 1242 | |
| 1243 | ret = __spi_async(spi, message); |
| 1244 | |
| 1245 | spin_unlock_irqrestore(&master->bus_lock_spinlock, flags); |
| 1246 | |
| 1247 | return ret; |
| 1248 | |
| 1249 | } |
| 1250 | EXPORT_SYMBOL_GPL(spi_async_locked); |
| 1251 | |
| 1252 | |
| 1253 | /*-------------------------------------------------------------------------*/ |
| 1254 | |
| 1255 | /* Utility methods for SPI master protocol drivers, layered on |
| 1256 | * top of the core. Some other utility methods are defined as |
| 1257 | * inline functions. |
| 1258 | */ |
| 1259 | |
| 1260 | static void spi_complete(void *arg) |
| 1261 | { |
| 1262 | complete(arg); |
| 1263 | } |
| 1264 | |
| 1265 | static int __spi_sync(struct spi_device *spi, struct spi_message *message, |
| 1266 | int bus_locked) |
| 1267 | { |
| 1268 | DECLARE_COMPLETION_ONSTACK(done); |
| 1269 | int status; |
| 1270 | struct spi_master *master = spi->master; |
| 1271 | |
| 1272 | message->complete = spi_complete; |
| 1273 | message->context = &done; |
| 1274 | |
| 1275 | if (!bus_locked) |
| 1276 | mutex_lock(&master->bus_lock_mutex); |
| 1277 | |
| 1278 | status = spi_async_locked(spi, message); |
| 1279 | |
| 1280 | if (!bus_locked) |
| 1281 | mutex_unlock(&master->bus_lock_mutex); |
| 1282 | |
| 1283 | if (status == 0) { |
| 1284 | wait_for_completion(&done); |
| 1285 | status = message->status; |
| 1286 | } |
| 1287 | message->context = NULL; |
| 1288 | return status; |
| 1289 | } |
| 1290 | |
| 1291 | /** |
| 1292 | * spi_sync - blocking/synchronous SPI data transfers |
| 1293 | * @spi: device with which data will be exchanged |
| 1294 | * @message: describes the data transfers |
| 1295 | * Context: can sleep |
| 1296 | * |
| 1297 | * This call may only be used from a context that may sleep. The sleep |
| 1298 | * is non-interruptible, and has no timeout. Low-overhead controller |
| 1299 | * drivers may DMA directly into and out of the message buffers. |
| 1300 | * |
| 1301 | * Note that the SPI device's chip select is active during the message, |
| 1302 | * and then is normally disabled between messages. Drivers for some |
| 1303 | * frequently-used devices may want to minimize costs of selecting a chip, |
| 1304 | * by leaving it selected in anticipation that the next message will go |
| 1305 | * to the same chip. (That may increase power usage.) |
| 1306 | * |
| 1307 | * Also, the caller is guaranteeing that the memory associated with the |
| 1308 | * message will not be freed before this call returns. |
| 1309 | * |
| 1310 | * It returns zero on success, else a negative error code. |
| 1311 | */ |
| 1312 | int spi_sync(struct spi_device *spi, struct spi_message *message) |
| 1313 | { |
| 1314 | return __spi_sync(spi, message, 0); |
| 1315 | } |
| 1316 | EXPORT_SYMBOL_GPL(spi_sync); |
| 1317 | |
| 1318 | /** |
| 1319 | * spi_sync_locked - version of spi_sync with exclusive bus usage |
| 1320 | * @spi: device with which data will be exchanged |
| 1321 | * @message: describes the data transfers |
| 1322 | * Context: can sleep |
| 1323 | * |
| 1324 | * This call may only be used from a context that may sleep. The sleep |
| 1325 | * is non-interruptible, and has no timeout. Low-overhead controller |
| 1326 | * drivers may DMA directly into and out of the message buffers. |
| 1327 | * |
| 1328 | * This call should be used by drivers that require exclusive access to the |
| 1329 | * SPI bus. It has to be preceded by a spi_bus_lock call. The SPI bus must |
| 1330 | * be released by a spi_bus_unlock call when the exclusive access is over. |
| 1331 | * |
| 1332 | * It returns zero on success, else a negative error code. |
| 1333 | */ |
| 1334 | int spi_sync_locked(struct spi_device *spi, struct spi_message *message) |
| 1335 | { |
| 1336 | return __spi_sync(spi, message, 1); |
| 1337 | } |
| 1338 | EXPORT_SYMBOL_GPL(spi_sync_locked); |
| 1339 | |
| 1340 | /** |
| 1341 | * spi_bus_lock - obtain a lock for exclusive SPI bus usage |
| 1342 | * @master: SPI bus master that should be locked for exclusive bus access |
| 1343 | * Context: can sleep |
| 1344 | * |
| 1345 | * This call may only be used from a context that may sleep. The sleep |
| 1346 | * is non-interruptible, and has no timeout. |
| 1347 | * |
| 1348 | * This call should be used by drivers that require exclusive access to the |
| 1349 | * SPI bus. The SPI bus must be released by a spi_bus_unlock call when the |
| 1350 | * exclusive access is over. Data transfer must be done by spi_sync_locked |
| 1351 | * and spi_async_locked calls when the SPI bus lock is held. |
| 1352 | * |
| 1353 | * It returns zero on success, else a negative error code. |
| 1354 | */ |
| 1355 | int spi_bus_lock(struct spi_master *master) |
| 1356 | { |
| 1357 | unsigned long flags; |
| 1358 | |
| 1359 | mutex_lock(&master->bus_lock_mutex); |
| 1360 | |
| 1361 | spin_lock_irqsave(&master->bus_lock_spinlock, flags); |
| 1362 | master->bus_lock_flag = 1; |
| 1363 | spin_unlock_irqrestore(&master->bus_lock_spinlock, flags); |
| 1364 | |
| 1365 | /* mutex remains locked until spi_bus_unlock is called */ |
| 1366 | |
| 1367 | return 0; |
| 1368 | } |
| 1369 | EXPORT_SYMBOL_GPL(spi_bus_lock); |
| 1370 | |
| 1371 | /** |
| 1372 | * spi_bus_unlock - release the lock for exclusive SPI bus usage |
| 1373 | * @master: SPI bus master that was locked for exclusive bus access |
| 1374 | * Context: can sleep |
| 1375 | * |
| 1376 | * This call may only be used from a context that may sleep. The sleep |
| 1377 | * is non-interruptible, and has no timeout. |
| 1378 | * |
| 1379 | * This call releases an SPI bus lock previously obtained by an spi_bus_lock |
| 1380 | * call. |
| 1381 | * |
| 1382 | * It returns zero on success, else a negative error code. |
| 1383 | */ |
| 1384 | int spi_bus_unlock(struct spi_master *master) |
| 1385 | { |
| 1386 | master->bus_lock_flag = 0; |
| 1387 | |
| 1388 | mutex_unlock(&master->bus_lock_mutex); |
| 1389 | |
| 1390 | return 0; |
| 1391 | } |
| 1392 | EXPORT_SYMBOL_GPL(spi_bus_unlock); |
| 1393 | |
| 1394 | /* portable code must never pass more than 32 bytes */ |
| 1395 | #define SPI_BUFSIZ max(32,SMP_CACHE_BYTES) |
| 1396 | |
| 1397 | static u8 *buf; |
| 1398 | |
| 1399 | /** |
| 1400 | * spi_write_then_read - SPI synchronous write followed by read |
| 1401 | * @spi: device with which data will be exchanged |
| 1402 | * @txbuf: data to be written (need not be dma-safe) |
| 1403 | * @n_tx: size of txbuf, in bytes |
| 1404 | * @rxbuf: buffer into which data will be read (need not be dma-safe) |
| 1405 | * @n_rx: size of rxbuf, in bytes |
| 1406 | * Context: can sleep |
| 1407 | * |
| 1408 | * This performs a half duplex MicroWire style transaction with the |
| 1409 | * device, sending txbuf and then reading rxbuf. The return value |
| 1410 | * is zero for success, else a negative errno status code. |
| 1411 | * This call may only be used from a context that may sleep. |
| 1412 | * |
| 1413 | * Parameters to this routine are always copied using a small buffer; |
| 1414 | * portable code should never use this for more than 32 bytes. |
| 1415 | * Performance-sensitive or bulk transfer code should instead use |
| 1416 | * spi_{async,sync}() calls with dma-safe buffers. |
| 1417 | */ |
| 1418 | int spi_write_then_read(struct spi_device *spi, |
| 1419 | const void *txbuf, unsigned n_tx, |
| 1420 | void *rxbuf, unsigned n_rx) |
| 1421 | { |
| 1422 | static DEFINE_MUTEX(lock); |
| 1423 | |
| 1424 | int status; |
| 1425 | struct spi_message message; |
| 1426 | struct spi_transfer x[2]; |
| 1427 | u8 *local_buf; |
| 1428 | |
| 1429 | /* Use preallocated DMA-safe buffer. We can't avoid copying here, |
| 1430 | * (as a pure convenience thing), but we can keep heap costs |
| 1431 | * out of the hot path ... |
| 1432 | */ |
| 1433 | if ((n_tx + n_rx) > SPI_BUFSIZ) { |
| 1434 | dev_err(&spi->dev, "total len %d > SPI_BUFSIZ %d.", (n_tx + n_rx), SPI_BUFSIZ); |
| 1435 | return -EINVAL; |
| 1436 | } |
| 1437 | |
| 1438 | spi_message_init(&message); |
| 1439 | memset(x, 0, sizeof x); |
| 1440 | if (n_tx) { |
| 1441 | x[0].len = n_tx; |
| 1442 | spi_message_add_tail(&x[0], &message); |
| 1443 | } |
| 1444 | if (n_rx) { |
| 1445 | x[1].len = n_rx; |
| 1446 | spi_message_add_tail(&x[1], &message); |
| 1447 | } |
| 1448 | |
| 1449 | /* ... unless someone else is using the pre-allocated buffer */ |
| 1450 | if (!mutex_trylock(&lock)) { |
| 1451 | local_buf = kmalloc(SPI_BUFSIZ, GFP_KERNEL); |
| 1452 | if (!local_buf) |
| 1453 | return -ENOMEM; |
| 1454 | } else |
| 1455 | local_buf = buf; |
| 1456 | |
| 1457 | memcpy(local_buf, txbuf, n_tx); |
| 1458 | x[0].tx_buf = local_buf; |
| 1459 | x[1].rx_buf = local_buf + n_tx; |
| 1460 | |
| 1461 | /* do the i/o */ |
| 1462 | status = spi_sync(spi, &message); |
| 1463 | if (status == 0) |
| 1464 | memcpy(rxbuf, x[1].rx_buf, n_rx); |
| 1465 | |
| 1466 | if (x[0].tx_buf == buf) |
| 1467 | mutex_unlock(&lock); |
| 1468 | else |
| 1469 | kfree(local_buf); |
| 1470 | |
| 1471 | return status; |
| 1472 | } |
| 1473 | EXPORT_SYMBOL_GPL(spi_write_then_read); |
| 1474 | |
| 1475 | /*-------------------------------------------------------------------------*/ |
| 1476 | |
| 1477 | static int __init spi_init(void) |
| 1478 | { |
| 1479 | int status; |
| 1480 | |
| 1481 | buf = kmalloc(SPI_BUFSIZ, GFP_KERNEL); |
| 1482 | if (!buf) { |
| 1483 | status = -ENOMEM; |
| 1484 | goto err0; |
| 1485 | } |
| 1486 | |
| 1487 | status = bus_register(&spi_bus_type); |
| 1488 | if (status < 0) |
| 1489 | goto err1; |
| 1490 | |
| 1491 | status = class_register(&spi_master_class); |
| 1492 | if (status < 0) |
| 1493 | goto err2; |
| 1494 | return 0; |
| 1495 | |
| 1496 | err2: |
| 1497 | bus_unregister(&spi_bus_type); |
| 1498 | err1: |
| 1499 | kfree(buf); |
| 1500 | buf = NULL; |
| 1501 | err0: |
| 1502 | return status; |
| 1503 | } |
| 1504 | |
| 1505 | /* board_info is normally registered in arch_initcall(), |
| 1506 | * but even essential drivers wait till later |
| 1507 | * |
| 1508 | * REVISIT only boardinfo really needs static linking. the rest (device and |
| 1509 | * driver registration) _could_ be dynamically linked (modular) ... costs |
| 1510 | * include needing to have boardinfo data structures be much more public. |
| 1511 | */ |
| 1512 | postcore_initcall(spi_init); |
| 1513 | |