2 * spi.c - SPI init/core code
4 * Copyright (C) 2005 David Brownell
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.
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.
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.
21 #include <linux/autoconf.h>
22 #include <linux/kernel.h>
23 #include <linux/device.h>
24 #include <linux/init.h>
25 #include <linux/cache.h>
26 #include <linux/spi/spi.h>
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.
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)
37 static void spidev_release(struct device
*dev
)
39 const struct spi_device
*spi
= to_spi_device(dev
);
41 /* spi masters may cleanup for released devices */
42 if (spi
->master
->cleanup
)
43 spi
->master
->cleanup(spi
);
45 class_device_put(&spi
->master
->cdev
);
50 modalias_show(struct device
*dev
, struct device_attribute
*a
, char *buf
)
52 const struct spi_device
*spi
= to_spi_device(dev
);
54 return snprintf(buf
, BUS_ID_SIZE
+ 1, "%s\n", spi
->modalias
);
57 static struct device_attribute spi_dev_attrs
[] = {
62 /* modalias support makes "modprobe $MODALIAS" new-style hotplug work,
63 * and the sysfs version makes coldplug work too.
66 static int spi_match_device(struct device
*dev
, struct device_driver
*drv
)
68 const struct spi_device
*spi
= to_spi_device(dev
);
70 return strncmp(spi
->modalias
, drv
->name
, BUS_ID_SIZE
) == 0;
73 static int spi_uevent(struct device
*dev
, char **envp
, int num_envp
,
74 char *buffer
, int buffer_size
)
76 const struct spi_device
*spi
= to_spi_device(dev
);
79 snprintf(buffer
, buffer_size
, "MODALIAS=%s", spi
->modalias
);
86 /* Suspend/resume in "struct device_driver" don't really need that
87 * strange third parameter, so we just make it a constant and expect
88 * SPI drivers to ignore it just like most platform drivers do.
90 * NOTE: the suspend() method for an spi_master controller driver
91 * should verify that all its child devices are marked as suspended;
92 * suspend requests delivered through sysfs power/state files don't
93 * enforce such constraints.
95 static int spi_suspend(struct device
*dev
, pm_message_t message
)
99 if (!dev
->driver
|| !dev
->driver
->suspend
)
102 /* suspend will stop irqs and dma; no more i/o */
103 value
= dev
->driver
->suspend(dev
, message
);
105 dev
->power
.power_state
= message
;
109 static int spi_resume(struct device
*dev
)
113 if (!dev
->driver
|| !dev
->driver
->resume
)
116 /* resume may restart the i/o queue */
117 value
= dev
->driver
->resume(dev
);
119 dev
->power
.power_state
= PMSG_ON
;
124 #define spi_suspend NULL
125 #define spi_resume NULL
128 struct bus_type spi_bus_type
= {
130 .dev_attrs
= spi_dev_attrs
,
131 .match
= spi_match_device
,
132 .uevent
= spi_uevent
,
133 .suspend
= spi_suspend
,
134 .resume
= spi_resume
,
136 EXPORT_SYMBOL_GPL(spi_bus_type
);
138 /*-------------------------------------------------------------------------*/
140 /* SPI devices should normally not be created by SPI device drivers; that
141 * would make them board-specific. Similarly with SPI master drivers.
142 * Device registration normally goes into like arch/.../mach.../board-YYY.c
143 * with other readonly (flashable) information about mainboard devices.
147 struct list_head list
;
148 unsigned n_board_info
;
149 struct spi_board_info board_info
[0];
152 static LIST_HEAD(board_list
);
153 static DECLARE_MUTEX(board_lock
);
156 /* On typical mainboards, this is purely internal; and it's not needed
157 * after board init creates the hard-wired devices. Some development
158 * platforms may not be able to use spi_register_board_info though, and
159 * this is exported so that for example a USB or parport based adapter
160 * driver could add devices (which it would learn about out-of-band).
162 struct spi_device
*__init_or_module
163 spi_new_device(struct spi_master
*master
, struct spi_board_info
*chip
)
165 struct spi_device
*proxy
;
166 struct device
*dev
= master
->cdev
.dev
;
169 /* NOTE: caller did any chip->bus_num checks necessary */
171 if (!class_device_get(&master
->cdev
))
174 proxy
= kzalloc(sizeof *proxy
, GFP_KERNEL
);
176 dev_err(dev
, "can't alloc dev for cs%d\n",
180 proxy
->master
= master
;
181 proxy
->chip_select
= chip
->chip_select
;
182 proxy
->max_speed_hz
= chip
->max_speed_hz
;
183 proxy
->irq
= chip
->irq
;
184 proxy
->modalias
= chip
->modalias
;
186 snprintf(proxy
->dev
.bus_id
, sizeof proxy
->dev
.bus_id
,
187 "%s.%u", master
->cdev
.class_id
,
189 proxy
->dev
.parent
= dev
;
190 proxy
->dev
.bus
= &spi_bus_type
;
191 proxy
->dev
.platform_data
= (void *) chip
->platform_data
;
192 proxy
->controller_data
= chip
->controller_data
;
193 proxy
->controller_state
= NULL
;
194 proxy
->dev
.release
= spidev_release
;
196 /* drivers may modify this default i/o setup */
197 status
= master
->setup(proxy
);
199 dev_dbg(dev
, "can't %s %s, status %d\n",
200 "setup", proxy
->dev
.bus_id
, status
);
204 /* driver core catches callers that misbehave by defining
205 * devices that already exist.
207 status
= device_register(&proxy
->dev
);
209 dev_dbg(dev
, "can't %s %s, status %d\n",
210 "add", proxy
->dev
.bus_id
, status
);
212 class_device_put(&master
->cdev
);
216 dev_dbg(dev
, "registered child %s\n", proxy
->dev
.bus_id
);
219 EXPORT_SYMBOL_GPL(spi_new_device
);
222 * Board-specific early init code calls this (probably during arch_initcall)
223 * with segments of the SPI device table. Any device nodes are created later,
224 * after the relevant parent SPI controller (bus_num) is defined. We keep
225 * this table of devices forever, so that reloading a controller driver will
226 * not make Linux forget about these hard-wired devices.
228 * Other code can also call this, e.g. a particular add-on board might provide
229 * SPI devices through its expansion connector, so code initializing that board
230 * would naturally declare its SPI devices.
232 * The board info passed can safely be __initdata ... but be careful of
233 * any embedded pointers (platform_data, etc), they're copied as-is.
236 spi_register_board_info(struct spi_board_info
const *info
, unsigned n
)
238 struct boardinfo
*bi
;
240 bi
= kmalloc (sizeof (*bi
) + n
* sizeof (*info
), GFP_KERNEL
);
243 bi
->n_board_info
= n
;
244 memcpy(bi
->board_info
, info
, n
* sizeof (*info
));
247 list_add_tail(&bi
->list
, &board_list
);
251 EXPORT_SYMBOL_GPL(spi_register_board_info
);
253 /* FIXME someone should add support for a __setup("spi", ...) that
254 * creates board info from kernel command lines
257 static void __init_or_module
258 scan_boardinfo(struct spi_master
*master
)
260 struct boardinfo
*bi
;
261 struct device
*dev
= master
->cdev
.dev
;
264 list_for_each_entry(bi
, &board_list
, list
) {
265 struct spi_board_info
*chip
= bi
->board_info
;
268 for (n
= bi
->n_board_info
; n
> 0; n
--, chip
++) {
269 if (chip
->bus_num
!= master
->bus_num
)
271 /* some controllers only have one chip, so they
272 * might not use chipselects. otherwise, the
273 * chipselects are numbered 0..max.
275 if (chip
->chip_select
>= master
->num_chipselect
276 && master
->num_chipselect
) {
277 dev_dbg(dev
, "cs%d > max %d\n",
279 master
->num_chipselect
);
282 (void) spi_new_device(master
, chip
);
288 /*-------------------------------------------------------------------------*/
290 static void spi_master_release(struct class_device
*cdev
)
292 struct spi_master
*master
;
294 master
= container_of(cdev
, struct spi_master
, cdev
);
295 put_device(master
->cdev
.dev
);
296 master
->cdev
.dev
= NULL
;
300 static struct class spi_master_class
= {
301 .name
= "spi_master",
302 .owner
= THIS_MODULE
,
303 .release
= spi_master_release
,
308 * spi_alloc_master - allocate SPI master controller
309 * @dev: the controller, possibly using the platform_bus
310 * @size: how much driver-private data to preallocate; a pointer to this
311 * memory in the class_data field of the returned class_device
313 * This call is used only by SPI master controller drivers, which are the
314 * only ones directly touching chip registers. It's how they allocate
315 * an spi_master structure, prior to calling spi_add_master().
317 * This must be called from context that can sleep. It returns the SPI
318 * master structure on success, else NULL.
320 * The caller is responsible for assigning the bus number and initializing
321 * the master's methods before calling spi_add_master(), or else (on error)
322 * calling class_device_put() to prevent a memory leak.
324 struct spi_master
* __init_or_module
325 spi_alloc_master(struct device
*dev
, unsigned size
)
327 struct spi_master
*master
;
329 master
= kzalloc(size
+ sizeof *master
, SLAB_KERNEL
);
333 master
->cdev
.class = &spi_master_class
;
334 master
->cdev
.dev
= get_device(dev
);
335 class_set_devdata(&master
->cdev
, &master
[1]);
339 EXPORT_SYMBOL_GPL(spi_alloc_master
);
342 * spi_register_master - register SPI master controller
343 * @master: initialized master, originally from spi_alloc_master()
345 * SPI master controllers connect to their drivers using some non-SPI bus,
346 * such as the platform bus. The final stage of probe() in that code
347 * includes calling spi_register_master() to hook up to this SPI bus glue.
349 * SPI controllers use board specific (often SOC specific) bus numbers,
350 * and board-specific addressing for SPI devices combines those numbers
351 * with chip select numbers. Since SPI does not directly support dynamic
352 * device identification, boards need configuration tables telling which
353 * chip is at which address.
355 * This must be called from context that can sleep. It returns zero on
356 * success, else a negative error code (dropping the master's refcount).
359 spi_register_master(struct spi_master
*master
)
361 static atomic_t dyn_bus_id
= ATOMIC_INIT(0);
362 struct device
*dev
= master
->cdev
.dev
;
363 int status
= -ENODEV
;
366 /* convention: dynamically assigned bus IDs count down from the max */
367 if (master
->bus_num
== 0) {
368 master
->bus_num
= atomic_dec_return(&dyn_bus_id
);
372 /* register the device, then userspace will see it.
373 * registration fails if the bus ID is in use.
375 snprintf(master
->cdev
.class_id
, sizeof master
->cdev
.class_id
,
376 "spi%u", master
->bus_num
);
377 status
= class_device_register(&master
->cdev
);
379 class_device_put(&master
->cdev
);
382 dev_dbg(dev
, "registered master %s%s\n", master
->cdev
.class_id
,
383 dynamic
? " (dynamic)" : "");
385 /* populate children from any spi device tables */
386 scan_boardinfo(master
);
391 EXPORT_SYMBOL_GPL(spi_register_master
);
394 static int __unregister(struct device
*dev
, void *unused
)
396 /* note: before about 2.6.14-rc1 this would corrupt memory: */
397 device_unregister(dev
);
402 * spi_unregister_master - unregister SPI master controller
403 * @master: the master being unregistered
405 * This call is used only by SPI master controller drivers, which are the
406 * only ones directly touching chip registers.
408 * This must be called from context that can sleep.
410 void spi_unregister_master(struct spi_master
*master
)
412 class_device_unregister(&master
->cdev
);
413 (void) device_for_each_child(master
->cdev
.dev
, NULL
, __unregister
);
415 EXPORT_SYMBOL_GPL(spi_unregister_master
);
418 * spi_busnum_to_master - look up master associated with bus_num
419 * @bus_num: the master's bus number
421 * This call may be used with devices that are registered after
422 * arch init time. It returns a refcounted pointer to the relevant
423 * spi_master (which the caller must release), or NULL if there is
424 * no such master registered.
426 struct spi_master
*spi_busnum_to_master(u16 bus_num
)
432 snprintf(name
, sizeof name
, "spi%u", bus_num
);
433 bus
= kset_find_obj(&spi_master_class
.subsys
.kset
, name
);
435 return container_of(bus
, struct spi_master
, cdev
.kobj
);
439 EXPORT_SYMBOL_GPL(spi_busnum_to_master
);
442 /*-------------------------------------------------------------------------*/
445 * spi_sync - blocking/synchronous SPI data transfers
446 * @spi: device with which data will be exchanged
447 * @message: describes the data transfers
449 * This call may only be used from a context that may sleep. The sleep
450 * is non-interruptible, and has no timeout. Low-overhead controller
451 * drivers may DMA directly into and out of the message buffers.
453 * Note that the SPI device's chip select is active during the message,
454 * and then is normally disabled between messages. Drivers for some
455 * frequently-used devices may want to minimize costs of selecting a chip,
456 * by leaving it selected in anticipation that the next message will go
457 * to the same chip. (That may increase power usage.)
459 * The return value is a negative error code if the message could not be
460 * submitted, else zero. When the value is zero, then message->status is
461 * also defined: it's the completion code for the transfer, either zero
462 * or a negative error code from the controller driver.
464 int spi_sync(struct spi_device
*spi
, struct spi_message
*message
)
466 DECLARE_COMPLETION(done
);
469 message
->complete
= (void (*)(void *)) complete
;
470 message
->context
= &done
;
471 status
= spi_async(spi
, message
);
473 wait_for_completion(&done
);
474 message
->context
= NULL
;
477 EXPORT_SYMBOL_GPL(spi_sync
);
479 #define SPI_BUFSIZ (SMP_CACHE_BYTES)
484 * spi_write_then_read - SPI synchronous write followed by read
485 * @spi: device with which data will be exchanged
486 * @txbuf: data to be written (need not be dma-safe)
487 * @n_tx: size of txbuf, in bytes
488 * @rxbuf: buffer into which data will be read
489 * @n_rx: size of rxbuf, in bytes (need not be dma-safe)
491 * This performs a half duplex MicroWire style transaction with the
492 * device, sending txbuf and then reading rxbuf. The return value
493 * is zero for success, else a negative errno status code.
495 * Parameters to this routine are always copied using a small buffer,
496 * large transfers should use use spi_{async,sync}() calls with
499 int spi_write_then_read(struct spi_device
*spi
,
500 const u8
*txbuf
, unsigned n_tx
,
501 u8
*rxbuf
, unsigned n_rx
)
503 static DECLARE_MUTEX(lock
);
506 struct spi_message message
;
507 struct spi_transfer x
[2];
510 /* Use preallocated DMA-safe buffer. We can't avoid copying here,
511 * (as a pure convenience thing), but we can keep heap costs
512 * out of the hot path ...
514 if ((n_tx
+ n_rx
) > SPI_BUFSIZ
)
517 /* ... unless someone else is using the pre-allocated buffer */
518 if (down_trylock(&lock
)) {
519 local_buf
= kmalloc(SPI_BUFSIZ
, GFP_KERNEL
);
525 memset(x
, 0, sizeof x
);
527 memcpy(local_buf
, txbuf
, n_tx
);
528 x
[0].tx_buf
= local_buf
;
531 x
[1].rx_buf
= local_buf
+ n_tx
;
535 message
.transfers
= x
;
536 message
.n_transfer
= ARRAY_SIZE(x
);
537 status
= spi_sync(spi
, &message
);
539 memcpy(rxbuf
, x
[1].rx_buf
, n_rx
);
540 status
= message
.status
;
543 if (x
[0].tx_buf
== buf
)
550 EXPORT_SYMBOL_GPL(spi_write_then_read
);
552 /*-------------------------------------------------------------------------*/
554 static int __init
spi_init(void)
556 buf
= kmalloc(SPI_BUFSIZ
, SLAB_KERNEL
);
560 bus_register(&spi_bus_type
);
561 class_register(&spi_master_class
);
564 /* board_info is normally registered in arch_initcall(),
565 * but even essential drivers wait till later
567 subsys_initcall(spi_init
);