[PATCH] spi: add spi_driver to SPI framework

This is a refresh of the "Simple SPI Framework" found in 2.6.15-rc3-mm1
which makes the following changes:

* There's now a "struct spi_driver". This increase the footprint
of the core a bit, since it now includes code to do what the driver
core was previously handling directly. Documentation and comments
were updated to match.

* spi_alloc_master() now does class_device_initialize(), so it can
at least be refcounted before spi_register_master(). To match,
spi_register_master() switched over to class_device_add().

* States explicitly that after transfer errors, spi_devices will be
deselected. We want fault recovery procedures to work the same
for all controller drivers.

* Minor tweaks: controller_data no longer points to readonly data;
prevent some potential cast-from-null bugs with container_of calls;
clarifies some existing kerneldoc,

And a few small cleanups.

Signed-off-by: David Brownell <dbrownell@users.sourceforge.net>
Signed-off-by: Andrew Morton <akpm@osdl.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>

authored by David Brownell and committed by Greg Kroah-Hartman b885244e 1d6432fe

+171 -76
+32 -20
Documentation/spi/spi-summary
··· 1 1 Overview of Linux kernel SPI support 2 2 ==================================== 3 3 4 - 22-Nov-2005 4 + 02-Dec-2005 5 5 6 6 What is SPI? 7 7 ------------ 8 - The "Serial Peripheral Interface" (SPI) is a four-wire point-to-point 9 - serial link used to connect microcontrollers to sensors and memory. 8 + The "Serial Peripheral Interface" (SPI) is a synchronous four wire serial 9 + link used to connect microcontrollers to sensors, memory, and peripherals. 10 10 11 11 The three signal wires hold a clock (SCLK, often on the order of 10 MHz), 12 12 and parallel data lines with "Master Out, Slave In" (MOSI) or "Master In, 13 13 Slave Out" (MISO) signals. (Other names are also used.) There are four 14 14 clocking modes through which data is exchanged; mode-0 and mode-3 are most 15 - commonly used. 15 + commonly used. Each clock cycle shifts data out and data in; the clock 16 + doesn't cycle except when there is data to shift. 16 17 17 18 SPI masters may use a "chip select" line to activate a given SPI slave 18 19 device, so those three signal wires may be connected to several chips ··· 80 79 main source code, and you should certainly read that. This is just 81 80 an overview, so you get the big picture before the details. 82 81 82 + SPI requests always go into I/O queues. Requests for a given SPI device 83 + are always executed in FIFO order, and complete asynchronously through 84 + completion callbacks. There are also some simple synchronous wrappers 85 + for those calls, including ones for common transaction types like writing 86 + a command and then reading its response. 87 + 83 88 There are two types of SPI driver, here called: 84 89 85 90 Controller drivers ... these are often built in to System-On-Chip 86 91 processors, and often support both Master and Slave roles. 87 92 These drivers touch hardware registers and may use DMA. 93 + Or they can be PIO bitbangers, needing just GPIO pins. 88 94 89 95 Protocol drivers ... these pass messages through the controller 90 96 driver to communicate with a Slave or Master device on the ··· 123 115 /sys/class/spi_master/spiB ... class device for the controller 124 116 managing bus "B". All the spiB.* devices share the same 125 117 physical SPI bus segment, with SCLK, MOSI, and MISO. 126 - 127 - The basic I/O primitive submits an asynchronous message to an I/O queue 128 - maintained by the controller driver. A completion callback is issued 129 - asynchronously when the data transfer(s) in that message completes. 130 - There are also some simple synchronous wrappers for those calls. 131 118 132 119 133 120 How does board-specific init code declare SPI devices? ··· 266 263 access through aio_read(), aio_write(), and ioctl() calls and using the 267 264 standard userspace sysfs mechanisms to bind to a given SPI device. 268 265 269 - SPI protocol drivers are normal device drivers, with no more wrapper 270 - than needed by platform devices: 266 + SPI protocol drivers somewhat resemble platform device drivers: 271 267 272 - static struct device_driver CHIP_driver = { 273 - .name = "CHIP", 274 - .bus = &spi_bus_type, 268 + static struct spi_driver CHIP_driver = { 269 + .driver = { 270 + .name = "CHIP", 271 + .bus = &spi_bus_type, 272 + .owner = THIS_MODULE, 273 + }, 274 + 275 275 .probe = CHIP_probe, 276 - .remove = __exit_p(CHIP_remove), 276 + .remove = __devexit_p(CHIP_remove), 277 277 .suspend = CHIP_suspend, 278 278 .resume = CHIP_resume, 279 279 }; 280 280 281 - The SPI core will autmatically attempt to bind this driver to any SPI 281 + The driver core will autmatically attempt to bind this driver to any SPI 282 282 device whose board_info gave a modalias of "CHIP". Your probe() code 283 283 might look like this unless you're creating a class_device: 284 284 285 - static int __init CHIP_probe(struct device *dev) 285 + static int __devinit CHIP_probe(struct spi_device *spi) 286 286 { 287 - struct spi_device *spi = to_spi_device(dev); 288 287 struct CHIP *chip; 289 - struct CHIP_platform_data *pdata = dev->platform_data; 288 + struct CHIP_platform_data *pdata; 289 + 290 + /* assuming the driver requires board-specific data: */ 291 + pdata = &spi->dev.platform_data; 292 + if (!pdata) 293 + return -ENODEV; 290 294 291 295 /* get memory for driver's per-chip state */ 292 296 chip = kzalloc(sizeof *chip, GFP_KERNEL); 293 297 if (!chip) 294 298 return -ENOMEM; 295 - dev_set_drvdata(dev, chip); 299 + dev_set_drvdata(&spi->dev, chip); 296 300 297 301 ... etc 298 302 return 0; ··· 338 328 - The basic I/O primitive is spi_async(). Async requests may be 339 329 issued in any context (irq handler, task, etc) and completion 340 330 is reported using a callback provided with the message. 331 + After any detected error, the chip is deselected and processing 332 + of that spi_message is aborted. 341 333 342 334 - There are also synchronous wrappers like spi_sync(), and wrappers 343 335 like spi_read(), spi_write(), and spi_write_then_read(). These
+87 -33
drivers/spi/spi.c
··· 26 26 #include <linux/spi/spi.h> 27 27 28 28 29 - /* SPI bustype and spi_master class are registered during early boot, 30 - * usually before board init code provides the SPI device tables, and 31 - * are available later when driver init code needs them. 32 - * 33 - * Drivers for SPI devices started out like those for platform bus 34 - * devices. But both have changed in 2.6.15; maybe this should get 35 - * an "spi_driver" structure at some point (not currently needed) 29 + /* SPI bustype and spi_master class are registered after board init code 30 + * provides the SPI device tables, ensuring that both are present by the 31 + * time controller driver registration causes spi_devices to "enumerate". 36 32 */ 37 33 static void spidev_release(struct device *dev) 38 34 { ··· 79 83 80 84 #ifdef CONFIG_PM 81 85 82 - /* Suspend/resume in "struct device_driver" don't really need that 83 - * strange third parameter, so we just make it a constant and expect 84 - * SPI drivers to ignore it just like most platform drivers do. 85 - * 86 + /* 86 87 * NOTE: the suspend() method for an spi_master controller driver 87 88 * should verify that all its child devices are marked as suspended; 88 89 * suspend requests delivered through sysfs power/state files don't ··· 87 94 */ 88 95 static int spi_suspend(struct device *dev, pm_message_t message) 89 96 { 90 - int value; 97 + int value; 98 + struct spi_driver *drv = to_spi_driver(dev->driver); 91 99 92 - if (!dev->driver || !dev->driver->suspend) 100 + if (!drv || !drv->suspend) 93 101 return 0; 94 102 95 103 /* suspend will stop irqs and dma; no more i/o */ 96 - value = dev->driver->suspend(dev, message); 104 + value = drv->suspend(to_spi_device(dev), message); 97 105 if (value == 0) 98 106 dev->power.power_state = message; 99 107 return value; ··· 102 108 103 109 static int spi_resume(struct device *dev) 104 110 { 105 - int value; 111 + int value; 112 + struct spi_driver *drv = to_spi_driver(dev->driver); 106 113 107 - if (!dev->driver || !dev->driver->resume) 114 + if (!drv || !drv->resume) 108 115 return 0; 109 116 110 117 /* resume may restart the i/o queue */ 111 - value = dev->driver->resume(dev); 118 + value = drv->resume(to_spi_device(dev)); 112 119 if (value == 0) 113 120 dev->power.power_state = PMSG_ON; 114 121 return value; ··· 129 134 .resume = spi_resume, 130 135 }; 131 136 EXPORT_SYMBOL_GPL(spi_bus_type); 137 + 138 + 139 + static int spi_drv_probe(struct device *dev) 140 + { 141 + const struct spi_driver *sdrv = to_spi_driver(dev->driver); 142 + 143 + return sdrv->probe(to_spi_device(dev)); 144 + } 145 + 146 + static int spi_drv_remove(struct device *dev) 147 + { 148 + const struct spi_driver *sdrv = to_spi_driver(dev->driver); 149 + 150 + return sdrv->remove(to_spi_device(dev)); 151 + } 152 + 153 + static void spi_drv_shutdown(struct device *dev) 154 + { 155 + const struct spi_driver *sdrv = to_spi_driver(dev->driver); 156 + 157 + sdrv->shutdown(to_spi_device(dev)); 158 + } 159 + 160 + int spi_register_driver(struct spi_driver *sdrv) 161 + { 162 + sdrv->driver.bus = &spi_bus_type; 163 + if (sdrv->probe) 164 + sdrv->driver.probe = spi_drv_probe; 165 + if (sdrv->remove) 166 + sdrv->driver.remove = spi_drv_remove; 167 + if (sdrv->shutdown) 168 + sdrv->driver.shutdown = spi_drv_shutdown; 169 + return driver_register(&sdrv->driver); 170 + } 171 + EXPORT_SYMBOL_GPL(spi_register_driver); 132 172 133 173 /*-------------------------------------------------------------------------*/ 134 174 ··· 238 208 if (status < 0) { 239 209 dev_dbg(dev, "can't %s %s, status %d\n", 240 210 "add", proxy->dev.bus_id, status); 241 - fail: 242 - class_device_put(&master->cdev); 243 - kfree(proxy); 244 - return NULL; 211 + goto fail; 245 212 } 246 213 dev_dbg(dev, "registered child %s\n", proxy->dev.bus_id); 247 214 return proxy; 215 + 216 + fail: 217 + class_device_put(&master->cdev); 218 + kfree(proxy); 219 + return NULL; 248 220 } 249 221 EXPORT_SYMBOL_GPL(spi_new_device); 250 222 ··· 269 237 { 270 238 struct boardinfo *bi; 271 239 272 - bi = kmalloc (sizeof (*bi) + n * sizeof (*info), GFP_KERNEL); 240 + bi = kmalloc(sizeof(*bi) + n * sizeof *info, GFP_KERNEL); 273 241 if (!bi) 274 242 return -ENOMEM; 275 243 bi->n_board_info = n; 276 - memcpy(bi->board_info, info, n * sizeof (*info)); 244 + memcpy(bi->board_info, info, n * sizeof *info); 277 245 278 246 down(&board_lock); 279 247 list_add_tail(&bi->list, &board_list); ··· 362 330 if (!master) 363 331 return NULL; 364 332 333 + class_device_initialize(&master->cdev); 365 334 master->cdev.class = &spi_master_class; 366 335 master->cdev.dev = get_device(dev); 367 336 class_set_devdata(&master->cdev, &master[1]); ··· 399 366 /* convention: dynamically assigned bus IDs count down from the max */ 400 367 if (master->bus_num == 0) { 401 368 master->bus_num = atomic_dec_return(&dyn_bus_id); 402 - dynamic = 0; 369 + dynamic = 1; 403 370 } 404 371 405 372 /* register the device, then userspace will see it. ··· 407 374 */ 408 375 snprintf(master->cdev.class_id, sizeof master->cdev.class_id, 409 376 "spi%u", master->bus_num); 410 - status = class_device_register(&master->cdev); 411 - if (status < 0) { 412 - class_device_put(&master->cdev); 377 + status = class_device_add(&master->cdev); 378 + if (status < 0) 413 379 goto done; 414 - } 415 380 dev_dbg(dev, "registered master %s%s\n", master->cdev.class_id, 416 381 dynamic ? " (dynamic)" : ""); 417 382 ··· 522 491 * This performs a half duplex MicroWire style transaction with the 523 492 * device, sending txbuf and then reading rxbuf. The return value 524 493 * is zero for success, else a negative errno status code. 494 + * This call may only be used from a context that may sleep. 525 495 * 526 496 * Parameters to this routine are always copied using a small buffer, 527 497 * large transfers should use use spi_{async,sync}() calls with ··· 585 553 586 554 static int __init spi_init(void) 587 555 { 588 - buf = kmalloc(SPI_BUFSIZ, SLAB_KERNEL); 589 - if (!buf) 590 - return -ENOMEM; 556 + int status; 591 557 592 - bus_register(&spi_bus_type); 593 - class_register(&spi_master_class); 558 + buf = kmalloc(SPI_BUFSIZ, SLAB_KERNEL); 559 + if (!buf) { 560 + status = -ENOMEM; 561 + goto err0; 562 + } 563 + 564 + status = bus_register(&spi_bus_type); 565 + if (status < 0) 566 + goto err1; 567 + 568 + status = class_register(&spi_master_class); 569 + if (status < 0) 570 + goto err2; 594 571 return 0; 572 + 573 + err2: 574 + bus_unregister(&spi_bus_type); 575 + err1: 576 + kfree(buf); 577 + buf = NULL; 578 + err0: 579 + return status; 595 580 } 581 + 596 582 /* board_info is normally registered in arch_initcall(), 597 583 * but even essential drivers wait till later 584 + * 585 + * REVISIT only boardinfo really needs static linking. the rest (device and 586 + * driver registration) _could_ be dynamically linked (modular) ... costs 587 + * include needing to have boardinfo data structures be much more public. 598 588 */ 599 589 subsys_initcall(spi_init); 600 590
+52 -23
include/linux/spi/spi.h
··· 20 20 #define __LINUX_SPI_H 21 21 22 22 /* 23 - * INTERFACES between SPI master drivers and infrastructure 23 + * INTERFACES between SPI master-side drivers and SPI infrastructure. 24 24 * (There's no SPI slave support for Linux yet...) 25 - * 26 - * A "struct device_driver" for an spi_device uses "spi_bus_type" and 27 - * needs no special API wrappers (much like platform_bus). These drivers 28 - * are bound to devices based on their names (much like platform_bus), 29 - * and are available in dev->driver. 30 25 */ 31 26 extern struct bus_type spi_bus_type; 32 27 ··· 41 46 * @irq: Negative, or the number passed to request_irq() to receive 42 47 * interrupts from this device. 43 48 * @controller_state: Controller's runtime state 44 - * @controller_data: Static board-specific definitions for controller, such 45 - * as FIFO initialization parameters; from board_info.controller_data 49 + * @controller_data: Board-specific definitions for controller, such as 50 + * FIFO initialization parameters; from board_info.controller_data 46 51 * 47 52 * An spi_device is used to interchange data between an SPI slave 48 53 * (usually a discrete chip) and CPU memory. ··· 58 63 u32 max_speed_hz; 59 64 u8 chip_select; 60 65 u8 mode; 61 - #define SPI_CPHA 0x01 /* clock phase */ 62 - #define SPI_CPOL 0x02 /* clock polarity */ 66 + #define SPI_CPHA 0x01 /* clock phase */ 67 + #define SPI_CPOL 0x02 /* clock polarity */ 63 68 #define SPI_MODE_0 (0|0) 64 - #define SPI_MODE_1 (0|SPI_CPHA) 69 + #define SPI_MODE_1 (0|SPI_CPHA) /* (original MicroWire) */ 65 70 #define SPI_MODE_2 (SPI_CPOL|0) 66 71 #define SPI_MODE_3 (SPI_CPOL|SPI_CPHA) 67 - #define SPI_CS_HIGH 0x04 /* chipselect active high? */ 72 + #define SPI_CS_HIGH 0x04 /* chipselect active high? */ 68 73 u8 bits_per_word; 69 74 int irq; 70 75 void *controller_state; 71 - const void *controller_data; 76 + void *controller_data; 72 77 const char *modalias; 73 78 74 79 // likely need more hooks for more protocol options affecting how 75 - // the controller talks to its chips, like: 80 + // the controller talks to each chip, like: 76 81 // - bit order (default is wordwise msb-first) 77 82 // - memory packing (12 bit samples into low bits, others zeroed) 78 83 // - priority 84 + // - drop chipselect after each word 79 85 // - chipselect delays 80 86 // - ... 81 87 }; 82 88 83 89 static inline struct spi_device *to_spi_device(struct device *dev) 84 90 { 85 - return container_of(dev, struct spi_device, dev); 91 + return dev ? container_of(dev, struct spi_device, dev) : NULL; 86 92 } 87 93 88 94 /* most drivers won't need to care about device refcounting */ ··· 113 117 struct spi_message; 114 118 115 119 120 + 121 + struct spi_driver { 122 + int (*probe)(struct spi_device *spi); 123 + int (*remove)(struct spi_device *spi); 124 + void (*shutdown)(struct spi_device *spi); 125 + int (*suspend)(struct spi_device *spi, pm_message_t mesg); 126 + int (*resume)(struct spi_device *spi); 127 + struct device_driver driver; 128 + }; 129 + 130 + static inline struct spi_driver *to_spi_driver(struct device_driver *drv) 131 + { 132 + return drv ? container_of(drv, struct spi_driver, driver) : NULL; 133 + } 134 + 135 + extern int spi_register_driver(struct spi_driver *sdrv); 136 + 137 + static inline void spi_unregister_driver(struct spi_driver *sdrv) 138 + { 139 + if (!sdrv) 140 + return; 141 + driver_unregister(&sdrv->driver); 142 + } 143 + 144 + 145 + 116 146 /** 117 147 * struct spi_master - interface to SPI master controller 118 148 * @cdev: class interface to this driver 119 149 * @bus_num: board-specific (and often SOC-specific) identifier for a 120 150 * given SPI controller. 121 - * @num_chipselects: chipselects are used to distinguish individual 151 + * @num_chipselect: chipselects are used to distinguish individual 122 152 * SPI slaves, and are numbered from zero to num_chipselects. 123 153 * each slave has a chipselect signal, but it's common that not 124 154 * every chipselect is connected to a slave. ··· 297 275 * addresses for each transfer buffer 298 276 * @complete: called to report transaction completions 299 277 * @context: the argument to complete() when it's called 300 - * @actual_length: how many bytes were transferd 278 + * @actual_length: the total number of bytes that were transferred in all 279 + * successful segments 301 280 * @status: zero for success, else negative errno 302 281 * @queue: for use by whichever driver currently owns the message 303 282 * @state: for use by whichever driver currently owns the message ··· 318 295 * 319 296 * Some controller drivers (message-at-a-time queue processing) 320 297 * could provide that as their default scheduling algorithm. But 321 - * others (with multi-message pipelines) would need a flag to 298 + * others (with multi-message pipelines) could need a flag to 322 299 * tell them about such special cases. 323 300 */ 324 301 ··· 369 346 * FIFO order, messages may go to different devices in other orders. 370 347 * Some device might be higher priority, or have various "hard" access 371 348 * time requirements, for example. 349 + * 350 + * On detection of any fault during the transfer, processing of 351 + * the entire message is aborted, and the device is deselected. 352 + * Until returning from the associated message completion callback, 353 + * no other spi_message queued to that device will be processed. 354 + * (This rule applies equally to all the synchronous transfer calls, 355 + * which are wrappers around this core asynchronous primitive.) 372 356 */ 373 357 static inline int 374 358 spi_async(struct spi_device *spi, struct spi_message *message) ··· 514 484 * "modalias" is normally the driver name. 515 485 * 516 486 * platform_data goes to spi_device.dev.platform_data, 517 - * controller_data goes to spi_device.platform_data, 487 + * controller_data goes to spi_device.controller_data, 518 488 * irq is copied too 519 489 */ 520 490 char modalias[KOBJ_NAME_LEN]; 521 491 const void *platform_data; 522 - const void *controller_data; 492 + void *controller_data; 523 493 int irq; 524 494 525 495 /* slower signaling on noisy or low voltage boards */ ··· 555 525 556 526 557 527 /* If you're hotplugging an adapter with devices (parport, usb, etc) 558 - * use spi_new_device() to describe each device. You can also call 559 - * spi_unregister_device() to get start making that device vanish, 560 - * but normally that would be handled by spi_unregister_master(). 528 + * use spi_new_device() to describe each device. You would then call 529 + * spi_unregister_device() to start making that device vanish. 561 530 */ 562 531 extern struct spi_device * 563 532 spi_new_device(struct spi_master *, struct spi_board_info *);