aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/spi
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/spi')
-rw-r--r--drivers/spi/Kconfig76
-rw-r--r--drivers/spi/Makefile23
-rw-r--r--drivers/spi/spi.c568
3 files changed, 667 insertions, 0 deletions
diff --git a/drivers/spi/Kconfig b/drivers/spi/Kconfig
new file mode 100644
index 000000000000..d3105104a297
--- /dev/null
+++ b/drivers/spi/Kconfig
@@ -0,0 +1,76 @@
1#
2# SPI driver configuration
3#
4# NOTE: the reason this doesn't show SPI slave support is mostly that
5# nobody's needed a slave side API yet. The master-role API is not
6# fully appropriate there, so it'd need some thought to do well.
7#
8menu "SPI support"
9
10config SPI
11 bool "SPI support"
12 help
13 The "Serial Peripheral Interface" is a low level synchronous
14 protocol. Chips that support SPI can have data transfer rates
15 up to several tens of Mbit/sec. Chips are addressed with a
16 controller and a chipselect. Most SPI slaves don't support
17 dynamic device discovery; some are even write-only or read-only.
18
19 SPI is widely used by microcontollers to talk with sensors,
20 eeprom and flash memory, codecs and various other controller
21 chips, analog to digital (and d-to-a) converters, and more.
22 MMC and SD cards can be accessed using SPI protocol; and for
23 DataFlash cards used in MMC sockets, SPI must always be used.
24
25 SPI is one of a family of similar protocols using a four wire
26 interface (select, clock, data in, data out) including Microwire
27 (half duplex), SSP, SSI, and PSP. This driver framework should
28 work with most such devices and controllers.
29
30config SPI_DEBUG
31 boolean "Debug support for SPI drivers"
32 depends on SPI && DEBUG_KERNEL
33 help
34 Say "yes" to enable debug messaging (like dev_dbg and pr_debug),
35 sysfs, and debugfs support in SPI controller and protocol drivers.
36
37#
38# MASTER side ... talking to discrete SPI slave chips including microcontrollers
39#
40
41config SPI_MASTER
42# boolean "SPI Master Support"
43 boolean
44 default SPI
45 help
46 If your system has an master-capable SPI controller (which
47 provides the clock and chipselect), you can enable that
48 controller and the protocol drivers for the SPI slave chips
49 that are connected.
50
51comment "SPI Master Controller Drivers"
52 depends on SPI_MASTER
53
54
55#
56# Add new SPI master controllers in alphabetical order above this line
57#
58
59
60#
61# There are lots of SPI device types, with sensors and memory
62# being probably the most widely used ones.
63#
64comment "SPI Protocol Masters"
65 depends on SPI_MASTER
66
67
68#
69# Add new SPI protocol masters in alphabetical order above this line
70#
71
72
73# (slave support would go here)
74
75endmenu # "SPI support"
76
diff --git a/drivers/spi/Makefile b/drivers/spi/Makefile
new file mode 100644
index 000000000000..afd2321753b3
--- /dev/null
+++ b/drivers/spi/Makefile
@@ -0,0 +1,23 @@
1#
2# Makefile for kernel SPI drivers.
3#
4
5ifeq ($(CONFIG_SPI_DEBUG),y)
6EXTRA_CFLAGS += -DDEBUG
7endif
8
9# small core, mostly translating board-specific
10# config declarations into driver model code
11obj-$(CONFIG_SPI_MASTER) += spi.o
12
13# SPI master controller drivers (bus)
14# ... add above this line ...
15
16# SPI protocol drivers (device/link on bus)
17# ... add above this line ...
18
19# SPI slave controller drivers (upstream link)
20# ... add above this line ...
21
22# SPI slave drivers (protocol for that link)
23# ... add above this line ...
diff --git a/drivers/spi/spi.c b/drivers/spi/spi.c
new file mode 100644
index 000000000000..7cd356b17644
--- /dev/null
+++ b/drivers/spi/spi.c
@@ -0,0 +1,568 @@
1/*
2 * spi.c - 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/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>
27
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)
36 */
37static void spidev_release(struct device *dev)
38{
39 const struct spi_device *spi = to_spi_device(dev);
40
41 /* spi masters may cleanup for released devices */
42 if (spi->master->cleanup)
43 spi->master->cleanup(spi);
44
45 class_device_put(&spi->master->cdev);
46 kfree(dev);
47}
48
49static ssize_t
50modalias_show(struct device *dev, struct device_attribute *a, char *buf)
51{
52 const struct spi_device *spi = to_spi_device(dev);
53
54 return snprintf(buf, BUS_ID_SIZE + 1, "%s\n", spi->modalias);
55}
56
57static struct device_attribute spi_dev_attrs[] = {
58 __ATTR_RO(modalias),
59 __ATTR_NULL,
60};
61
62/* modalias support makes "modprobe $MODALIAS" new-style hotplug work,
63 * and the sysfs version makes coldplug work too.
64 */
65
66static int spi_match_device(struct device *dev, struct device_driver *drv)
67{
68 const struct spi_device *spi = to_spi_device(dev);
69
70 return strncmp(spi->modalias, drv->name, BUS_ID_SIZE) == 0;
71}
72
73static int spi_uevent(struct device *dev, char **envp, int num_envp,
74 char *buffer, int buffer_size)
75{
76 const struct spi_device *spi = to_spi_device(dev);
77
78 envp[0] = buffer;
79 snprintf(buffer, buffer_size, "MODALIAS=%s", spi->modalias);
80 envp[1] = NULL;
81 return 0;
82}
83
84#ifdef CONFIG_PM
85
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.
89 *
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.
94 */
95static int spi_suspend(struct device *dev, pm_message_t message)
96{
97 int value;
98
99 if (!dev->driver || !dev->driver->suspend)
100 return 0;
101
102 /* suspend will stop irqs and dma; no more i/o */
103 value = dev->driver->suspend(dev, message);
104 if (value == 0)
105 dev->power.power_state = message;
106 return value;
107}
108
109static int spi_resume(struct device *dev)
110{
111 int value;
112
113 if (!dev->driver || !dev->driver->resume)
114 return 0;
115
116 /* resume may restart the i/o queue */
117 value = dev->driver->resume(dev);
118 if (value == 0)
119 dev->power.power_state = PMSG_ON;
120 return value;
121}
122
123#else
124#define spi_suspend NULL
125#define spi_resume NULL
126#endif
127
128struct bus_type spi_bus_type = {
129 .name = "spi",
130 .dev_attrs = spi_dev_attrs,
131 .match = spi_match_device,
132 .uevent = spi_uevent,
133 .suspend = spi_suspend,
134 .resume = spi_resume,
135};
136EXPORT_SYMBOL_GPL(spi_bus_type);
137
138/*-------------------------------------------------------------------------*/
139
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.
144 */
145
146struct boardinfo {
147 struct list_head list;
148 unsigned n_board_info;
149 struct spi_board_info board_info[0];
150};
151
152static LIST_HEAD(board_list);
153static DECLARE_MUTEX(board_lock);
154
155
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).
161 */
162struct spi_device *__init_or_module
163spi_new_device(struct spi_master *master, struct spi_board_info *chip)
164{
165 struct spi_device *proxy;
166 struct device *dev = master->cdev.dev;
167 int status;
168
169 /* NOTE: caller did any chip->bus_num checks necessary */
170
171 if (!class_device_get(&master->cdev))
172 return NULL;
173
174 proxy = kzalloc(sizeof *proxy, GFP_KERNEL);
175 if (!proxy) {
176 dev_err(dev, "can't alloc dev for cs%d\n",
177 chip->chip_select);
178 goto fail;
179 }
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;
185
186 snprintf(proxy->dev.bus_id, sizeof proxy->dev.bus_id,
187 "%s.%u", master->cdev.class_id,
188 chip->chip_select);
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;
195
196 /* drivers may modify this default i/o setup */
197 status = master->setup(proxy);
198 if (status < 0) {
199 dev_dbg(dev, "can't %s %s, status %d\n",
200 "setup", proxy->dev.bus_id, status);
201 goto fail;
202 }
203
204 /* driver core catches callers that misbehave by defining
205 * devices that already exist.
206 */
207 status = device_register(&proxy->dev);
208 if (status < 0) {
209 dev_dbg(dev, "can't %s %s, status %d\n",
210 "add", proxy->dev.bus_id, status);
211fail:
212 class_device_put(&master->cdev);
213 kfree(proxy);
214 return NULL;
215 }
216 dev_dbg(dev, "registered child %s\n", proxy->dev.bus_id);
217 return proxy;
218}
219EXPORT_SYMBOL_GPL(spi_new_device);
220
221/*
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.
227 *
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.
231 *
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.
234 */
235int __init
236spi_register_board_info(struct spi_board_info const *info, unsigned n)
237{
238 struct boardinfo *bi;
239
240 bi = kmalloc (sizeof (*bi) + n * sizeof (*info), GFP_KERNEL);
241 if (!bi)
242 return -ENOMEM;
243 bi->n_board_info = n;
244 memcpy(bi->board_info, info, n * sizeof (*info));
245
246 down(&board_lock);
247 list_add_tail(&bi->list, &board_list);
248 up(&board_lock);
249 return 0;
250}
251EXPORT_SYMBOL_GPL(spi_register_board_info);
252
253/* FIXME someone should add support for a __setup("spi", ...) that
254 * creates board info from kernel command lines
255 */
256
257static void __init_or_module
258scan_boardinfo(struct spi_master *master)
259{
260 struct boardinfo *bi;
261 struct device *dev = master->cdev.dev;
262
263 down(&board_lock);
264 list_for_each_entry(bi, &board_list, list) {
265 struct spi_board_info *chip = bi->board_info;
266 unsigned n;
267
268 for (n = bi->n_board_info; n > 0; n--, chip++) {
269 if (chip->bus_num != master->bus_num)
270 continue;
271 /* some controllers only have one chip, so they
272 * might not use chipselects. otherwise, the
273 * chipselects are numbered 0..max.
274 */
275 if (chip->chip_select >= master->num_chipselect
276 && master->num_chipselect) {
277 dev_dbg(dev, "cs%d > max %d\n",
278 chip->chip_select,
279 master->num_chipselect);
280 continue;
281 }
282 (void) spi_new_device(master, chip);
283 }
284 }
285 up(&board_lock);
286}
287
288/*-------------------------------------------------------------------------*/
289
290static void spi_master_release(struct class_device *cdev)
291{
292 struct spi_master *master;
293
294 master = container_of(cdev, struct spi_master, cdev);
295 put_device(master->cdev.dev);
296 master->cdev.dev = NULL;
297 kfree(master);
298}
299
300static struct class spi_master_class = {
301 .name = "spi_master",
302 .owner = THIS_MODULE,
303 .release = spi_master_release,
304};
305
306
307/**
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
312 *
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().
316 *
317 * This must be called from context that can sleep. It returns the SPI
318 * master structure on success, else NULL.
319 *
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.
323 */
324struct spi_master * __init_or_module
325spi_alloc_master(struct device *dev, unsigned size)
326{
327 struct spi_master *master;
328
329 master = kzalloc(size + sizeof *master, SLAB_KERNEL);
330 if (!master)
331 return NULL;
332
333 master->cdev.class = &spi_master_class;
334 master->cdev.dev = get_device(dev);
335 class_set_devdata(&master->cdev, &master[1]);
336
337 return master;
338}
339EXPORT_SYMBOL_GPL(spi_alloc_master);
340
341/**
342 * spi_register_master - register SPI master controller
343 * @master: initialized master, originally from spi_alloc_master()
344 *
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.
348 *
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.
354 *
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).
357 */
358int __init_or_module
359spi_register_master(struct spi_master *master)
360{
361 static atomic_t dyn_bus_id = ATOMIC_INIT(0);
362 struct device *dev = master->cdev.dev;
363 int status = -ENODEV;
364 int dynamic = 0;
365
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);
369 dynamic = 0;
370 }
371
372 /* register the device, then userspace will see it.
373 * registration fails if the bus ID is in use.
374 */
375 snprintf(master->cdev.class_id, sizeof master->cdev.class_id,
376 "spi%u", master->bus_num);
377 status = class_device_register(&master->cdev);
378 if (status < 0) {
379 class_device_put(&master->cdev);
380 goto done;
381 }
382 dev_dbg(dev, "registered master %s%s\n", master->cdev.class_id,
383 dynamic ? " (dynamic)" : "");
384
385 /* populate children from any spi device tables */
386 scan_boardinfo(master);
387 status = 0;
388done:
389 return status;
390}
391EXPORT_SYMBOL_GPL(spi_register_master);
392
393
394static int __unregister(struct device *dev, void *unused)
395{
396 /* note: before about 2.6.14-rc1 this would corrupt memory: */
397 device_unregister(dev);
398 return 0;
399}
400
401/**
402 * spi_unregister_master - unregister SPI master controller
403 * @master: the master being unregistered
404 *
405 * This call is used only by SPI master controller drivers, which are the
406 * only ones directly touching chip registers.
407 *
408 * This must be called from context that can sleep.
409 */
410void spi_unregister_master(struct spi_master *master)
411{
412 class_device_unregister(&master->cdev);
413 (void) device_for_each_child(master->cdev.dev, NULL, __unregister);
414}
415EXPORT_SYMBOL_GPL(spi_unregister_master);
416
417/**
418 * spi_busnum_to_master - look up master associated with bus_num
419 * @bus_num: the master's bus number
420 *
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.
425 */
426struct spi_master *spi_busnum_to_master(u16 bus_num)
427{
428 if (bus_num) {
429 char name[8];
430 struct kobject *bus;
431
432 snprintf(name, sizeof name, "spi%u", bus_num);
433 bus = kset_find_obj(&spi_master_class.subsys.kset, name);
434 if (bus)
435 return container_of(bus, struct spi_master, cdev.kobj);
436 }
437 return NULL;
438}
439EXPORT_SYMBOL_GPL(spi_busnum_to_master);
440
441
442/*-------------------------------------------------------------------------*/
443
444/**
445 * spi_sync - blocking/synchronous SPI data transfers
446 * @spi: device with which data will be exchanged
447 * @message: describes the data transfers
448 *
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.
452 *
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.)
458 *
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.
463 */
464int spi_sync(struct spi_device *spi, struct spi_message *message)
465{
466 DECLARE_COMPLETION(done);
467 int status;
468
469 message->complete = (void (*)(void *)) complete;
470 message->context = &done;
471 status = spi_async(spi, message);
472 if (status == 0)
473 wait_for_completion(&done);
474 message->context = NULL;
475 return status;
476}
477EXPORT_SYMBOL_GPL(spi_sync);
478
479#define SPI_BUFSIZ (SMP_CACHE_BYTES)
480
481static u8 *buf;
482
483/**
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)
490 *
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.
494 *
495 * Parameters to this routine are always copied using a small buffer,
496 * large transfers should use use spi_{async,sync}() calls with
497 * dma-safe buffers.
498 */
499int spi_write_then_read(struct spi_device *spi,
500 const u8 *txbuf, unsigned n_tx,
501 u8 *rxbuf, unsigned n_rx)
502{
503 static DECLARE_MUTEX(lock);
504
505 int status;
506 struct spi_message message;
507 struct spi_transfer x[2];
508 u8 *local_buf;
509
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 ...
513 */
514 if ((n_tx + n_rx) > SPI_BUFSIZ)
515 return -EINVAL;
516
517 /* ... unless someone else is using the pre-allocated buffer */
518 if (down_trylock(&lock)) {
519 local_buf = kmalloc(SPI_BUFSIZ, GFP_KERNEL);
520 if (!local_buf)
521 return -ENOMEM;
522 } else
523 local_buf = buf;
524
525 memset(x, 0, sizeof x);
526
527 memcpy(local_buf, txbuf, n_tx);
528 x[0].tx_buf = local_buf;
529 x[0].len = n_tx;
530
531 x[1].rx_buf = local_buf + n_tx;
532 x[1].len = n_rx;
533
534 /* do the i/o */
535 message.transfers = x;
536 message.n_transfer = ARRAY_SIZE(x);
537 status = spi_sync(spi, &message);
538 if (status == 0) {
539 memcpy(rxbuf, x[1].rx_buf, n_rx);
540 status = message.status;
541 }
542
543 if (x[0].tx_buf == buf)
544 up(&lock);
545 else
546 kfree(local_buf);
547
548 return status;
549}
550EXPORT_SYMBOL_GPL(spi_write_then_read);
551
552/*-------------------------------------------------------------------------*/
553
554static int __init spi_init(void)
555{
556 buf = kmalloc(SPI_BUFSIZ, SLAB_KERNEL);
557 if (!buf)
558 return -ENOMEM;
559
560 bus_register(&spi_bus_type);
561 class_register(&spi_master_class);
562 return 0;
563}
564/* board_info is normally registered in arch_initcall(),
565 * but even essential drivers wait till later
566 */
567subsys_initcall(spi_init);
568