brcm2708: update to v3.18
[openwrt.git] / target / linux / brcm2708 / patches-3.18 / 0019-Add-Chris-Boot-s-i2c-and-spi-drivers.patch
1 From 66ea263246ca6dd9f63dce8fb22157fa83693300 Mon Sep 17 00:00:00 2001
2 From: popcornmix <popcornmix@gmail.com>
3 Date: Mon, 4 Nov 2013 18:56:10 +0000
4 Subject: [PATCH 019/114] Add Chris Boot's i2c and spi drivers.
5
6 i2c-bcm2708: fixed baudrate
7
8 Fixed issue where the wrong CDIV value was set for baudrates below 3815 Hz (for 250MHz bus clock).
9 In that case the computed CDIV value was more than 0xffff. However the CDIV register width is only 16 bits.
10 This resulted in incorrect setting of CDIV and higher baudrate than intended.
11 Example: 3500Hz -> CDIV=0x11704 -> CDIV(16bit)=0x1704 -> 42430Hz
12 After correction: 3500Hz -> CDIV=0x11704 -> CDIV(16bit)=0xffff -> 3815Hz
13 The correct baudrate is shown in the log after the cdiv > 0xffff correction.
14 ---
15  arch/arm/configs/bcmrpi_defconfig |   7 +
16  arch/arm/mach-bcm2708/Kconfig     |   7 +
17  arch/arm/mach-bcm2708/bcm2708.c   | 104 ++++++-
18  drivers/i2c/busses/Kconfig        |  19 ++
19  drivers/i2c/busses/Makefile       |   1 +
20  drivers/i2c/busses/i2c-bcm2708.c  | 420 +++++++++++++++++++++++++
21  drivers/spi/Kconfig               |   8 +
22  drivers/spi/Makefile              |   1 +
23  drivers/spi/spi-bcm2708.c         | 626 ++++++++++++++++++++++++++++++++++++++
24  9 files changed, 1191 insertions(+), 2 deletions(-)
25  create mode 100644 drivers/i2c/busses/i2c-bcm2708.c
26  create mode 100644 drivers/spi/spi-bcm2708.c
27
28 diff --git a/arch/arm/configs/bcmrpi_defconfig b/arch/arm/configs/bcmrpi_defconfig
29 index 63cb6a4..6d2eae1 100644
30 --- a/arch/arm/configs/bcmrpi_defconfig
31 +++ b/arch/arm/configs/bcmrpi_defconfig
32 @@ -195,6 +195,13 @@ CONFIG_SERIAL_AMBA_PL011=y
33  CONFIG_SERIAL_AMBA_PL011_CONSOLE=y
34  # CONFIG_HW_RANDOM is not set
35  CONFIG_RAW_DRIVER=y
36 +CONFIG_I2C=y
37 +CONFIG_I2C_CHARDEV=m
38 +CONFIG_I2C_BCM2708=m
39 +CONFIG_SPI=y
40 +CONFIG_SPI_BCM2708=m
41 +CONFIG_SPI_SPIDEV=m
42 +CONFIG_GPIO_SYSFS=y
43  # CONFIG_HWMON is not set
44  CONFIG_WATCHDOG=y
45  CONFIG_FB=y
46 diff --git a/arch/arm/mach-bcm2708/Kconfig b/arch/arm/mach-bcm2708/Kconfig
47 index 9355841..e151ed4 100644
48 --- a/arch/arm/mach-bcm2708/Kconfig
49 +++ b/arch/arm/mach-bcm2708/Kconfig
50 @@ -31,4 +31,11 @@ config BCM2708_NOL2CACHE
51          help
52            Do not allow ARM to use GPU's L2 cache. Requires disable_l2cache in config.txt.
53  
54 +config BCM2708_SPIDEV
55 +       bool "Bind spidev to SPI0 master"
56 +       depends on MACH_BCM2708
57 +       depends on SPI
58 +       default y
59 +       help
60 +         Binds spidev driver to the SPI0 master
61  endmenu
62 diff --git a/arch/arm/mach-bcm2708/bcm2708.c b/arch/arm/mach-bcm2708/bcm2708.c
63 index af57d11..82f56fb 100644
64 --- a/arch/arm/mach-bcm2708/bcm2708.c
65 +++ b/arch/arm/mach-bcm2708/bcm2708.c
66 @@ -31,6 +31,7 @@
67  #include <linux/cnt32_to_63.h>
68  #include <linux/io.h>
69  #include <linux/module.h>
70 +#include <linux/spi/spi.h>
71  
72  #include <linux/version.h>
73  #include <linux/clkdev.h>
74 @@ -205,7 +206,6 @@ static struct clk osc_clk = {
75  
76  /* warning - the USB needs a clock > 34MHz */
77  
78 -#ifdef CONFIG_MMC_BCM2708
79  static struct clk sdhost_clk = {
80  #ifdef CONFIG_ARCH_BCM2708_CHIPIT
81         .rate = 4000000,        /* 4MHz */
82 @@ -213,7 +213,6 @@ static struct clk sdhost_clk = {
83         .rate = 250000000,      /* 250MHz */
84  #endif
85  };
86 -#endif
87  
88  static struct clk_lookup lookups[] = {
89         {                       /* UART0 */
90 @@ -223,6 +222,15 @@ static struct clk_lookup lookups[] = {
91         {                       /* USB */
92          .dev_id = "bcm2708_usb",
93          .clk = &osc_clk,
94 +        }, {   /* SPI */
95 +                .dev_id = "bcm2708_spi.0",
96 +                .clk = &sdhost_clk,
97 +        }, {   /* BSC0 */
98 +                .dev_id = "bcm2708_i2c.0",
99 +                .clk = &sdhost_clk,
100 +        }, {   /* BSC1 */
101 +                .dev_id = "bcm2708_i2c.1",
102 +                .clk = &sdhost_clk,
103          }
104  };
105  
106 @@ -455,6 +463,89 @@ static struct platform_device bcm2708_alsa_devices[] = {
107                },
108  };
109  
110 +static struct resource bcm2708_spi_resources[] = {
111 +       {
112 +               .start = SPI0_BASE,
113 +               .end = SPI0_BASE + SZ_256 - 1,
114 +               .flags = IORESOURCE_MEM,
115 +       }, {
116 +               .start = IRQ_SPI,
117 +               .end = IRQ_SPI,
118 +               .flags = IORESOURCE_IRQ,
119 +       }
120 +};
121 +
122 +
123 +static u64 bcm2708_spi_dmamask = DMA_BIT_MASK(DMA_MASK_BITS_COMMON);
124 +static struct platform_device bcm2708_spi_device = {
125 +       .name = "bcm2708_spi",
126 +       .id = 0,
127 +       .num_resources = ARRAY_SIZE(bcm2708_spi_resources),
128 +       .resource = bcm2708_spi_resources,
129 +       .dev = {
130 +               .dma_mask = &bcm2708_spi_dmamask,
131 +               .coherent_dma_mask = DMA_BIT_MASK(DMA_MASK_BITS_COMMON)},
132 +};
133 +
134 +#ifdef CONFIG_BCM2708_SPIDEV
135 +static struct spi_board_info bcm2708_spi_devices[] = {
136 +#ifdef CONFIG_SPI_SPIDEV
137 +       {
138 +               .modalias = "spidev",
139 +               .max_speed_hz = 500000,
140 +               .bus_num = 0,
141 +               .chip_select = 0,
142 +               .mode = SPI_MODE_0,
143 +       }, {
144 +               .modalias = "spidev",
145 +               .max_speed_hz = 500000,
146 +               .bus_num = 0,
147 +               .chip_select = 1,
148 +               .mode = SPI_MODE_0,
149 +       }
150 +#endif
151 +};
152 +#endif
153 +
154 +static struct resource bcm2708_bsc0_resources[] = {
155 +       {
156 +               .start = BSC0_BASE,
157 +               .end = BSC0_BASE + SZ_256 - 1,
158 +               .flags = IORESOURCE_MEM,
159 +       }, {
160 +               .start = INTERRUPT_I2C,
161 +               .end = INTERRUPT_I2C,
162 +               .flags = IORESOURCE_IRQ,
163 +       }
164 +};
165 +
166 +static struct platform_device bcm2708_bsc0_device = {
167 +       .name = "bcm2708_i2c",
168 +       .id = 0,
169 +       .num_resources = ARRAY_SIZE(bcm2708_bsc0_resources),
170 +       .resource = bcm2708_bsc0_resources,
171 +};
172 +
173 +
174 +static struct resource bcm2708_bsc1_resources[] = {
175 +       {
176 +               .start = BSC1_BASE,
177 +               .end = BSC1_BASE + SZ_256 - 1,
178 +               .flags = IORESOURCE_MEM,
179 +       }, {
180 +               .start = INTERRUPT_I2C,
181 +               .end = INTERRUPT_I2C,
182 +               .flags = IORESOURCE_IRQ,
183 +       }
184 +};
185 +
186 +static struct platform_device bcm2708_bsc1_device = {
187 +       .name = "bcm2708_i2c",
188 +       .id = 1,
189 +       .num_resources = ARRAY_SIZE(bcm2708_bsc1_resources),
190 +       .resource = bcm2708_bsc1_resources,
191 +};
192 +
193  static struct platform_device bcm2835_hwmon_device = {
194         .name = "bcm2835_hwmon",
195  };
196 @@ -571,6 +662,10 @@ void __init bcm2708_init(void)
197         for (i = 0; i < ARRAY_SIZE(bcm2708_alsa_devices); i++)
198                 bcm_register_device(&bcm2708_alsa_devices[i]);
199  
200 +       bcm_register_device(&bcm2708_spi_device);
201 +       bcm_register_device(&bcm2708_bsc0_device);
202 +       bcm_register_device(&bcm2708_bsc1_device);
203 +
204         bcm_register_device(&bcm2835_hwmon_device);
205         bcm_register_device(&bcm2835_thermal_device);
206  
207 @@ -580,6 +675,11 @@ void __init bcm2708_init(void)
208         }
209         system_rev = boardrev;
210         system_serial_low = serial;
211 +
212 +#ifdef CONFIG_BCM2708_SPIDEV
213 +       spi_register_board_info(bcm2708_spi_devices,
214 +                       ARRAY_SIZE(bcm2708_spi_devices));
215 +#endif
216  }
217  
218  static void timer_set_mode(enum clock_event_mode mode,
219 diff --git a/drivers/i2c/busses/Kconfig b/drivers/i2c/busses/Kconfig
220 index 917c358..3d3db41 100644
221 --- a/drivers/i2c/busses/Kconfig
222 +++ b/drivers/i2c/busses/Kconfig
223 @@ -371,6 +371,25 @@ config I2C_BCM2835
224           This support is also available as a module.  If so, the module
225           will be called i2c-bcm2835.
226  
227 +config I2C_BCM2708
228 +       tristate "BCM2708 BSC"
229 +       depends on MACH_BCM2708
230 +       help
231 +         Enabling this option will add BSC (Broadcom Serial Controller)
232 +         support for the BCM2708. BSC is a Broadcom proprietary bus compatible
233 +         with I2C/TWI/SMBus.
234 +
235 +config I2C_BCM2708_BAUDRATE
236 +       prompt "BCM2708 I2C baudrate"
237 +       depends on I2C_BCM2708
238 +       int
239 +       default 100000
240 +       help
241 +         Set the I2C baudrate. This will alter the default value. A
242 +         different baudrate can be set by using a module parameter as well. If
243 +         no parameter is provided when loading, this is the value that will be
244 +         used.
245 +
246  config I2C_BCM_KONA
247         tristate "BCM Kona I2C adapter"
248         depends on ARCH_BCM_MOBILE
249 diff --git a/drivers/i2c/busses/Makefile b/drivers/i2c/busses/Makefile
250 index 78d56c5..abe642f 100644
251 --- a/drivers/i2c/busses/Makefile
252 +++ b/drivers/i2c/busses/Makefile
253 @@ -33,6 +33,7 @@ obj-$(CONFIG_I2C_AT91)                += i2c-at91.o
254  obj-$(CONFIG_I2C_AU1550)       += i2c-au1550.o
255  obj-$(CONFIG_I2C_AXXIA)                += i2c-axxia.o
256  obj-$(CONFIG_I2C_BCM2835)      += i2c-bcm2835.o
257 +obj-$(CONFIG_I2C_BCM2708)      += i2c-bcm2708.o
258  obj-$(CONFIG_I2C_BLACKFIN_TWI) += i2c-bfin-twi.o
259  obj-$(CONFIG_I2C_CADENCE)      += i2c-cadence.o
260  obj-$(CONFIG_I2C_CBUS_GPIO)    += i2c-cbus-gpio.o
261 diff --git a/drivers/i2c/busses/i2c-bcm2708.c b/drivers/i2c/busses/i2c-bcm2708.c
262 new file mode 100644
263 index 0000000..09203c0
264 --- /dev/null
265 +++ b/drivers/i2c/busses/i2c-bcm2708.c
266 @@ -0,0 +1,420 @@
267 +/*
268 + * Driver for Broadcom BCM2708 BSC Controllers
269 + *
270 + * Copyright (C) 2012 Chris Boot & Frank Buss
271 + *
272 + * This driver is inspired by:
273 + * i2c-ocores.c, by Peter Korsgaard <jacmet@sunsite.dk>
274 + *
275 + * This program is free software; you can redistribute it and/or modify
276 + * it under the terms of the GNU General Public License as published by
277 + * the Free Software Foundation; either version 2 of the License, or
278 + * (at your option) any later version.
279 + *
280 + * This program is distributed in the hope that it will be useful,
281 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
282 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
283 + * GNU General Public License for more details.
284 + *
285 + * You should have received a copy of the GNU General Public License
286 + * along with this program; if not, write to the Free Software
287 + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
288 + */
289 +
290 +#include <linux/kernel.h>
291 +#include <linux/module.h>
292 +#include <linux/spinlock.h>
293 +#include <linux/clk.h>
294 +#include <linux/err.h>
295 +#include <linux/platform_device.h>
296 +#include <linux/io.h>
297 +#include <linux/slab.h>
298 +#include <linux/i2c.h>
299 +#include <linux/interrupt.h>
300 +#include <linux/sched.h>
301 +#include <linux/wait.h>
302 +
303 +/* BSC register offsets */
304 +#define BSC_C                  0x00
305 +#define BSC_S                  0x04
306 +#define BSC_DLEN               0x08
307 +#define BSC_A                  0x0c
308 +#define BSC_FIFO               0x10
309 +#define BSC_DIV                        0x14
310 +#define BSC_DEL                        0x18
311 +#define BSC_CLKT               0x1c
312 +
313 +/* Bitfields in BSC_C */
314 +#define BSC_C_I2CEN            0x00008000
315 +#define BSC_C_INTR             0x00000400
316 +#define BSC_C_INTT             0x00000200
317 +#define BSC_C_INTD             0x00000100
318 +#define BSC_C_ST               0x00000080
319 +#define BSC_C_CLEAR_1          0x00000020
320 +#define BSC_C_CLEAR_2          0x00000010
321 +#define BSC_C_READ             0x00000001
322 +
323 +/* Bitfields in BSC_S */
324 +#define BSC_S_CLKT             0x00000200
325 +#define BSC_S_ERR              0x00000100
326 +#define BSC_S_RXF              0x00000080
327 +#define BSC_S_TXE              0x00000040
328 +#define BSC_S_RXD              0x00000020
329 +#define BSC_S_TXD              0x00000010
330 +#define BSC_S_RXR              0x00000008
331 +#define BSC_S_TXW              0x00000004
332 +#define BSC_S_DONE             0x00000002
333 +#define BSC_S_TA               0x00000001
334 +
335 +#define I2C_TIMEOUT_MS 150
336 +
337 +#define DRV_NAME       "bcm2708_i2c"
338 +
339 +static unsigned int baudrate = CONFIG_I2C_BCM2708_BAUDRATE;
340 +module_param(baudrate, uint, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
341 +MODULE_PARM_DESC(baudrate, "The I2C baudrate");
342 +
343 +
344 +struct bcm2708_i2c {
345 +       struct i2c_adapter adapter;
346 +
347 +       spinlock_t lock;
348 +       void __iomem *base;
349 +       int irq;
350 +       struct clk *clk;
351 +
352 +       struct completion done;
353 +
354 +       struct i2c_msg *msg;
355 +       int pos;
356 +       int nmsgs;
357 +       bool error;
358 +};
359 +
360 +/*
361 + * This function sets the ALT mode on the I2C pins so that we can use them with
362 + * the BSC hardware.
363 + *
364 + * FIXME: This is a hack. Use pinmux / pinctrl.
365 + */
366 +static void bcm2708_i2c_init_pinmode(int id)
367 +{
368 +#define INP_GPIO(g) *(gpio+((g)/10)) &= ~(7<<(((g)%10)*3))
369 +#define SET_GPIO_ALT(g,a) *(gpio+(((g)/10))) |= (((a)<=3?(a)+4:(a)==4?3:2)<<(((g)%10)*3))
370 +
371 +       int pin;
372 +       u32 *gpio = ioremap(GPIO_BASE, SZ_16K);
373 +
374 +        BUG_ON(id != 0 && id != 1);
375 +       /* BSC0 is on GPIO 0 & 1, BSC1 is on GPIO 2 & 3 */
376 +       for (pin = id*2+0; pin <= id*2+1; pin++) {
377 +printk("bcm2708_i2c_init_pinmode(%d,%d)\n", id, pin);
378 +               INP_GPIO(pin);          /* set mode to GPIO input first */
379 +               SET_GPIO_ALT(pin, 0);   /* set mode to ALT 0 */
380 +       }
381 +
382 +       iounmap(gpio);
383 +
384 +#undef INP_GPIO
385 +#undef SET_GPIO_ALT
386 +}
387 +
388 +static inline u32 bcm2708_rd(struct bcm2708_i2c *bi, unsigned reg)
389 +{
390 +       return readl(bi->base + reg);
391 +}
392 +
393 +static inline void bcm2708_wr(struct bcm2708_i2c *bi, unsigned reg, u32 val)
394 +{
395 +       writel(val, bi->base + reg);
396 +}
397 +
398 +static inline void bcm2708_bsc_reset(struct bcm2708_i2c *bi)
399 +{
400 +       bcm2708_wr(bi, BSC_C, 0);
401 +       bcm2708_wr(bi, BSC_S, BSC_S_CLKT | BSC_S_ERR | BSC_S_DONE);
402 +}
403 +
404 +static inline void bcm2708_bsc_fifo_drain(struct bcm2708_i2c *bi)
405 +{
406 +       while ((bcm2708_rd(bi, BSC_S) & BSC_S_RXD) && (bi->pos < bi->msg->len))
407 +               bi->msg->buf[bi->pos++] = bcm2708_rd(bi, BSC_FIFO);
408 +}
409 +
410 +static inline void bcm2708_bsc_fifo_fill(struct bcm2708_i2c *bi)
411 +{
412 +       while ((bcm2708_rd(bi, BSC_S) & BSC_S_TXD) && (bi->pos < bi->msg->len))
413 +               bcm2708_wr(bi, BSC_FIFO, bi->msg->buf[bi->pos++]);
414 +}
415 +
416 +static inline void bcm2708_bsc_setup(struct bcm2708_i2c *bi)
417 +{
418 +       unsigned long bus_hz;
419 +       u32 cdiv;
420 +       u32 c = BSC_C_I2CEN | BSC_C_INTD | BSC_C_ST | BSC_C_CLEAR_1;
421 +
422 +       bus_hz = clk_get_rate(bi->clk);
423 +       cdiv = bus_hz / baudrate;
424 +       if (cdiv > 0xffff)
425 +               cdiv = 0xffff;
426 +
427 +       if (bi->msg->flags & I2C_M_RD)
428 +               c |= BSC_C_INTR | BSC_C_READ;
429 +       else
430 +               c |= BSC_C_INTT;
431 +
432 +       bcm2708_wr(bi, BSC_DIV, cdiv);
433 +       bcm2708_wr(bi, BSC_A, bi->msg->addr);
434 +       bcm2708_wr(bi, BSC_DLEN, bi->msg->len);
435 +       bcm2708_wr(bi, BSC_C, c);
436 +}
437 +
438 +static irqreturn_t bcm2708_i2c_interrupt(int irq, void *dev_id)
439 +{
440 +       struct bcm2708_i2c *bi = dev_id;
441 +       bool handled = true;
442 +       u32 s;
443 +
444 +       spin_lock(&bi->lock);
445 +
446 +       /* we may see camera interrupts on the "other" I2C channel
447 +           Just return if we've not sent anything */
448 +        if (!bi->nmsgs || !bi->msg )
449 +               goto early_exit;
450 +
451 +       s = bcm2708_rd(bi, BSC_S);
452 +
453 +       if (s & (BSC_S_CLKT | BSC_S_ERR)) {
454 +               bcm2708_bsc_reset(bi);
455 +               bi->error = true;
456 +
457 +               /* wake up our bh */
458 +               complete(&bi->done);
459 +       } else if (s & BSC_S_DONE) {
460 +               bi->nmsgs--;
461 +
462 +               if (bi->msg->flags & I2C_M_RD)
463 +                       bcm2708_bsc_fifo_drain(bi);
464 +
465 +               bcm2708_bsc_reset(bi);
466 +
467 +               if (bi->nmsgs) {
468 +                       /* advance to next message */
469 +                       bi->msg++;
470 +                       bi->pos = 0;
471 +                       bcm2708_bsc_setup(bi);
472 +               } else {
473 +                       /* wake up our bh */
474 +                       complete(&bi->done);
475 +               }
476 +       } else if (s & BSC_S_TXW) {
477 +               bcm2708_bsc_fifo_fill(bi);
478 +       } else if (s & BSC_S_RXR) {
479 +               bcm2708_bsc_fifo_drain(bi);
480 +       } else {
481 +               handled = false;
482 +       }
483 +
484 +early_exit:
485 +       spin_unlock(&bi->lock);
486 +
487 +       return handled ? IRQ_HANDLED : IRQ_NONE;
488 +}
489 +
490 +static int bcm2708_i2c_master_xfer(struct i2c_adapter *adap,
491 +       struct i2c_msg *msgs, int num)
492 +{
493 +       struct bcm2708_i2c *bi = adap->algo_data;
494 +       unsigned long flags;
495 +       int ret;
496 +
497 +       spin_lock_irqsave(&bi->lock, flags);
498 +
499 +       reinit_completion(&bi->done);
500 +       bi->msg = msgs;
501 +       bi->pos = 0;
502 +       bi->nmsgs = num;
503 +       bi->error = false;
504 +
505 +       bcm2708_bsc_setup(bi);
506 +
507 +       /* unlockig _after_ the setup to avoid races with the interrupt routine */
508 +       spin_unlock_irqrestore(&bi->lock, flags);
509 +
510 +       ret = wait_for_completion_timeout(&bi->done,
511 +                       msecs_to_jiffies(I2C_TIMEOUT_MS));
512 +       if (ret == 0) {
513 +               dev_err(&adap->dev, "transfer timed out\n");
514 +               spin_lock_irqsave(&bi->lock, flags);
515 +               bcm2708_bsc_reset(bi);
516 +               spin_unlock_irqrestore(&bi->lock, flags);
517 +               return -ETIMEDOUT;
518 +       }
519 +
520 +       return bi->error ? -EIO : num;
521 +}
522 +
523 +static u32 bcm2708_i2c_functionality(struct i2c_adapter *adap)
524 +{
525 +       return I2C_FUNC_I2C | /*I2C_FUNC_10BIT_ADDR |*/ I2C_FUNC_SMBUS_EMUL;
526 +}
527 +
528 +static struct i2c_algorithm bcm2708_i2c_algorithm = {
529 +       .master_xfer = bcm2708_i2c_master_xfer,
530 +       .functionality = bcm2708_i2c_functionality,
531 +};
532 +
533 +static int bcm2708_i2c_probe(struct platform_device *pdev)
534 +{
535 +       struct resource *regs;
536 +       int irq, err = -ENOMEM;
537 +       struct clk *clk;
538 +       struct bcm2708_i2c *bi;
539 +       struct i2c_adapter *adap;
540 +       unsigned long bus_hz;
541 +       u32 cdiv;
542 +
543 +       regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
544 +       if (!regs) {
545 +               dev_err(&pdev->dev, "could not get IO memory\n");
546 +               return -ENXIO;
547 +       }
548 +
549 +       irq = platform_get_irq(pdev, 0);
550 +       if (irq < 0) {
551 +               dev_err(&pdev->dev, "could not get IRQ\n");
552 +               return irq;
553 +       }
554 +
555 +       clk = clk_get(&pdev->dev, NULL);
556 +       if (IS_ERR(clk)) {
557 +               dev_err(&pdev->dev, "could not find clk: %ld\n", PTR_ERR(clk));
558 +               return PTR_ERR(clk);
559 +       }
560 +
561 +       bcm2708_i2c_init_pinmode(pdev->id);
562 +
563 +       bi = kzalloc(sizeof(*bi), GFP_KERNEL);
564 +       if (!bi)
565 +               goto out_clk_put;
566 +
567 +       platform_set_drvdata(pdev, bi);
568 +
569 +       adap = &bi->adapter;
570 +       adap->class = I2C_CLASS_HWMON | I2C_CLASS_DDC;
571 +       adap->algo = &bcm2708_i2c_algorithm;
572 +       adap->algo_data = bi;
573 +       adap->dev.parent = &pdev->dev;
574 +       adap->nr = pdev->id;
575 +       strlcpy(adap->name, dev_name(&pdev->dev), sizeof(adap->name));
576 +
577 +       switch (pdev->id) {
578 +       case 0:
579 +               adap->class = I2C_CLASS_HWMON;
580 +               break;
581 +       case 1:
582 +               adap->class = I2C_CLASS_DDC;
583 +               break;
584 +       default:
585 +               dev_err(&pdev->dev, "can only bind to BSC 0 or 1\n");
586 +               err = -ENXIO;
587 +               goto out_free_bi;
588 +       }
589 +
590 +       spin_lock_init(&bi->lock);
591 +       init_completion(&bi->done);
592 +
593 +       bi->base = ioremap(regs->start, resource_size(regs));
594 +       if (!bi->base) {
595 +               dev_err(&pdev->dev, "could not remap memory\n");
596 +               goto out_free_bi;
597 +       }
598 +
599 +       bi->irq = irq;
600 +       bi->clk = clk;
601 +
602 +       err = request_irq(irq, bcm2708_i2c_interrupt, IRQF_SHARED,
603 +                       dev_name(&pdev->dev), bi);
604 +       if (err) {
605 +               dev_err(&pdev->dev, "could not request IRQ: %d\n", err);
606 +               goto out_iounmap;
607 +       }
608 +
609 +       bcm2708_bsc_reset(bi);
610 +
611 +       err = i2c_add_numbered_adapter(adap);
612 +       if (err < 0) {
613 +               dev_err(&pdev->dev, "could not add I2C adapter: %d\n", err);
614 +               goto out_free_irq;
615 +       }
616 +
617 +       bus_hz = clk_get_rate(bi->clk);
618 +       cdiv = bus_hz / baudrate;
619 +       if (cdiv > 0xffff) {
620 +               cdiv = 0xffff;
621 +               baudrate = bus_hz / cdiv;
622 +       }
623 +
624 +       dev_info(&pdev->dev, "BSC%d Controller at 0x%08lx (irq %d) (baudrate %d)\n",
625 +               pdev->id, (unsigned long)regs->start, irq, baudrate);
626 +
627 +       return 0;
628 +
629 +out_free_irq:
630 +       free_irq(bi->irq, bi);
631 +out_iounmap:
632 +       iounmap(bi->base);
633 +out_free_bi:
634 +       kfree(bi);
635 +out_clk_put:
636 +       clk_put(clk);
637 +       return err;
638 +}
639 +
640 +static int bcm2708_i2c_remove(struct platform_device *pdev)
641 +{
642 +       struct bcm2708_i2c *bi = platform_get_drvdata(pdev);
643 +
644 +       platform_set_drvdata(pdev, NULL);
645 +
646 +       i2c_del_adapter(&bi->adapter);
647 +       free_irq(bi->irq, bi);
648 +       iounmap(bi->base);
649 +       clk_disable(bi->clk);
650 +       clk_put(bi->clk);
651 +       kfree(bi);
652 +
653 +       return 0;
654 +}
655 +
656 +static struct platform_driver bcm2708_i2c_driver = {
657 +       .driver         = {
658 +               .name   = DRV_NAME,
659 +               .owner  = THIS_MODULE,
660 +       },
661 +       .probe          = bcm2708_i2c_probe,
662 +       .remove         = bcm2708_i2c_remove,
663 +};
664 +
665 +// module_platform_driver(bcm2708_i2c_driver);
666 +
667 +
668 +static int __init bcm2708_i2c_init(void)
669 +{
670 +       return platform_driver_register(&bcm2708_i2c_driver);
671 +}
672 +
673 +static void __exit bcm2708_i2c_exit(void)
674 +{
675 +       platform_driver_unregister(&bcm2708_i2c_driver);
676 +}
677 +
678 +module_init(bcm2708_i2c_init);
679 +module_exit(bcm2708_i2c_exit);
680 +
681 +
682 +
683 +MODULE_DESCRIPTION("BSC controller driver for Broadcom BCM2708");
684 +MODULE_AUTHOR("Chris Boot <bootc@bootc.net>");
685 +MODULE_LICENSE("GPL v2");
686 +MODULE_ALIAS("platform:" DRV_NAME);
687 diff --git a/drivers/spi/Kconfig b/drivers/spi/Kconfig
688 index 84e7c9e..71b4741 100644
689 --- a/drivers/spi/Kconfig
690 +++ b/drivers/spi/Kconfig
691 @@ -86,6 +86,14 @@ config SPI_BCM2835
692           is for the regular SPI controller. Slave mode operation is not also
693           not supported.
694  
695 +config SPI_BCM2708
696 +       tristate "BCM2708 SPI controller driver (SPI0)"
697 +       depends on MACH_BCM2708
698 +       help
699 +         This selects a driver for the Broadcom BCM2708 SPI master (SPI0). This
700 +         driver is not compatible with the "Universal SPI Master" or the SPI slave
701 +         device.
702 +
703  config SPI_BFIN5XX
704         tristate "SPI controller driver for ADI Blackfin5xx"
705         depends on BLACKFIN && !BF60x
706 diff --git a/drivers/spi/Makefile b/drivers/spi/Makefile
707 index 78f24ca..fdd29d3 100644
708 --- a/drivers/spi/Makefile
709 +++ b/drivers/spi/Makefile
710 @@ -20,6 +20,7 @@ obj-$(CONFIG_SPI_BCM63XX)             += spi-bcm63xx.o
711  obj-$(CONFIG_SPI_BCM63XX_HSSPI)                += spi-bcm63xx-hsspi.o
712  obj-$(CONFIG_SPI_BFIN5XX)              += spi-bfin5xx.o
713  obj-$(CONFIG_SPI_ADI_V3)                += spi-adi-v3.o
714 +obj-$(CONFIG_SPI_BCM2708)              += spi-bcm2708.o
715  obj-$(CONFIG_SPI_BFIN_SPORT)           += spi-bfin-sport.o
716  obj-$(CONFIG_SPI_BITBANG)              += spi-bitbang.o
717  obj-$(CONFIG_SPI_BUTTERFLY)            += spi-butterfly.o
718 diff --git a/drivers/spi/spi-bcm2708.c b/drivers/spi/spi-bcm2708.c
719 new file mode 100644
720 index 0000000..b04a57d
721 --- /dev/null
722 +++ b/drivers/spi/spi-bcm2708.c
723 @@ -0,0 +1,626 @@
724 +/*
725 + * Driver for Broadcom BCM2708 SPI Controllers
726 + *
727 + * Copyright (C) 2012 Chris Boot
728 + *
729 + * This driver is inspired by:
730 + * spi-ath79.c, Copyright (C) 2009-2011 Gabor Juhos <juhosg@openwrt.org>
731 + * spi-atmel.c, Copyright (C) 2006 Atmel Corporation
732 + *
733 + * This program is free software; you can redistribute it and/or modify
734 + * it under the terms of the GNU General Public License as published by
735 + * the Free Software Foundation; either version 2 of the License, or
736 + * (at your option) any later version.
737 + *
738 + * This program is distributed in the hope that it will be useful,
739 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
740 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
741 + * GNU General Public License for more details.
742 + *
743 + * You should have received a copy of the GNU General Public License
744 + * along with this program; if not, write to the Free Software
745 + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
746 + */
747 +
748 +#include <linux/kernel.h>
749 +#include <linux/module.h>
750 +#include <linux/spinlock.h>
751 +#include <linux/clk.h>
752 +#include <linux/err.h>
753 +#include <linux/platform_device.h>
754 +#include <linux/io.h>
755 +#include <linux/spi/spi.h>
756 +#include <linux/interrupt.h>
757 +#include <linux/delay.h>
758 +#include <linux/log2.h>
759 +#include <linux/sched.h>
760 +#include <linux/wait.h>
761 +
762 +/* SPI register offsets */
763 +#define SPI_CS                 0x00
764 +#define SPI_FIFO               0x04
765 +#define SPI_CLK                        0x08
766 +#define SPI_DLEN               0x0c
767 +#define SPI_LTOH               0x10
768 +#define SPI_DC                 0x14
769 +
770 +/* Bitfields in CS */
771 +#define SPI_CS_LEN_LONG                0x02000000
772 +#define SPI_CS_DMA_LEN         0x01000000
773 +#define SPI_CS_CSPOL2          0x00800000
774 +#define SPI_CS_CSPOL1          0x00400000
775 +#define SPI_CS_CSPOL0          0x00200000
776 +#define SPI_CS_RXF             0x00100000
777 +#define SPI_CS_RXR             0x00080000
778 +#define SPI_CS_TXD             0x00040000
779 +#define SPI_CS_RXD             0x00020000
780 +#define SPI_CS_DONE            0x00010000
781 +#define SPI_CS_LEN             0x00002000
782 +#define SPI_CS_REN             0x00001000
783 +#define SPI_CS_ADCS            0x00000800
784 +#define SPI_CS_INTR            0x00000400
785 +#define SPI_CS_INTD            0x00000200
786 +#define SPI_CS_DMAEN           0x00000100
787 +#define SPI_CS_TA              0x00000080
788 +#define SPI_CS_CSPOL           0x00000040
789 +#define SPI_CS_CLEAR_RX                0x00000020
790 +#define SPI_CS_CLEAR_TX                0x00000010
791 +#define SPI_CS_CPOL            0x00000008
792 +#define SPI_CS_CPHA            0x00000004
793 +#define SPI_CS_CS_10           0x00000002
794 +#define SPI_CS_CS_01           0x00000001
795 +
796 +#define SPI_TIMEOUT_MS 150
797 +
798 +#define DRV_NAME       "bcm2708_spi"
799 +
800 +struct bcm2708_spi {
801 +       spinlock_t lock;
802 +       void __iomem *base;
803 +       int irq;
804 +       struct clk *clk;
805 +       bool stopping;
806 +
807 +       struct list_head queue;
808 +       struct workqueue_struct *workq;
809 +       struct work_struct work;
810 +       struct completion done;
811 +
812 +       const u8 *tx_buf;
813 +       u8 *rx_buf;
814 +       int len;
815 +};
816 +
817 +struct bcm2708_spi_state {
818 +       u32 cs;
819 +       u16 cdiv;
820 +};
821 +
822 +/*
823 + * This function sets the ALT mode on the SPI pins so that we can use them with
824 + * the SPI hardware.
825 + *
826 + * FIXME: This is a hack. Use pinmux / pinctrl.
827 + */
828 +static void bcm2708_init_pinmode(void)
829 +{
830 +#define INP_GPIO(g) *(gpio+((g)/10)) &= ~(7<<(((g)%10)*3))
831 +#define SET_GPIO_ALT(g,a) *(gpio+(((g)/10))) |= (((a)<=3?(a)+4:(a)==4?3:2)<<(((g)%10)*3))
832 +
833 +       int pin;
834 +       u32 *gpio = ioremap(GPIO_BASE, SZ_16K);
835 +
836 +       /* SPI is on GPIO 7..11 */
837 +       for (pin = 7; pin <= 11; pin++) {
838 +               INP_GPIO(pin);          /* set mode to GPIO input first */
839 +               SET_GPIO_ALT(pin, 0);   /* set mode to ALT 0 */
840 +       }
841 +
842 +       iounmap(gpio);
843 +
844 +#undef INP_GPIO
845 +#undef SET_GPIO_ALT
846 +}
847 +
848 +static inline u32 bcm2708_rd(struct bcm2708_spi *bs, unsigned reg)
849 +{
850 +       return readl(bs->base + reg);
851 +}
852 +
853 +static inline void bcm2708_wr(struct bcm2708_spi *bs, unsigned reg, u32 val)
854 +{
855 +       writel(val, bs->base + reg);
856 +}
857 +
858 +static inline void bcm2708_rd_fifo(struct bcm2708_spi *bs, int len)
859 +{
860 +       u8 byte;
861 +
862 +       while (len--) {
863 +               byte = bcm2708_rd(bs, SPI_FIFO);
864 +               if (bs->rx_buf)
865 +                       *bs->rx_buf++ = byte;
866 +       }
867 +}
868 +
869 +static inline void bcm2708_wr_fifo(struct bcm2708_spi *bs, int len)
870 +{
871 +       u8 byte;
872 +       u16 val;
873 +
874 +       if (len > bs->len)
875 +               len = bs->len;
876 +
877 +       if (unlikely(bcm2708_rd(bs, SPI_CS) & SPI_CS_LEN)) {
878 +               /* LoSSI mode */
879 +               if (unlikely(len % 2)) {
880 +                       printk(KERN_ERR"bcm2708_wr_fifo: length must be even, skipping.\n");
881 +                       bs->len = 0;
882 +                       return;
883 +               }
884 +               while (len) {
885 +                       if (bs->tx_buf) {
886 +                               val = *(const u16 *)bs->tx_buf;
887 +                               bs->tx_buf += 2;
888 +                       } else
889 +                               val = 0;
890 +                       bcm2708_wr(bs, SPI_FIFO, val);
891 +                       bs->len -= 2;
892 +                       len -= 2;
893 +               }
894 +               return;
895 +       }
896 +
897 +       while (len--) {
898 +               byte = bs->tx_buf ? *bs->tx_buf++ : 0;
899 +               bcm2708_wr(bs, SPI_FIFO, byte);
900 +               bs->len--;
901 +       }
902 +}
903 +
904 +static irqreturn_t bcm2708_spi_interrupt(int irq, void *dev_id)
905 +{
906 +       struct spi_master *master = dev_id;
907 +       struct bcm2708_spi *bs = spi_master_get_devdata(master);
908 +       u32 cs;
909 +
910 +       spin_lock(&bs->lock);
911 +
912 +       cs = bcm2708_rd(bs, SPI_CS);
913 +
914 +       if (cs & SPI_CS_DONE) {
915 +               if (bs->len) { /* first interrupt in a transfer */
916 +                       /* fill the TX fifo with up to 16 bytes */
917 +                       bcm2708_wr_fifo(bs, 16);
918 +               } else { /* transfer complete */
919 +                       /* disable interrupts */
920 +                       cs &= ~(SPI_CS_INTR | SPI_CS_INTD);
921 +                       bcm2708_wr(bs, SPI_CS, cs);
922 +
923 +                       /* drain RX FIFO */
924 +                       while (cs & SPI_CS_RXD) {
925 +                               bcm2708_rd_fifo(bs, 1);
926 +                               cs = bcm2708_rd(bs, SPI_CS);
927 +                       }
928 +
929 +                       /* wake up our bh */
930 +                       complete(&bs->done);
931 +               }
932 +       } else if (cs & SPI_CS_RXR) {
933 +               /* read 12 bytes of data */
934 +               bcm2708_rd_fifo(bs, 12);
935 +
936 +               /* write up to 12 bytes */
937 +               bcm2708_wr_fifo(bs, 12);
938 +       }
939 +
940 +       spin_unlock(&bs->lock);
941 +
942 +       return IRQ_HANDLED;
943 +}
944 +
945 +static int bcm2708_setup_state(struct spi_master *master,
946 +               struct device *dev, struct bcm2708_spi_state *state,
947 +               u32 hz, u8 csel, u8 mode, u8 bpw)
948 +{
949 +       struct bcm2708_spi *bs = spi_master_get_devdata(master);
950 +       int cdiv;
951 +       unsigned long bus_hz;
952 +       u32 cs = 0;
953 +
954 +       bus_hz = clk_get_rate(bs->clk);
955 +
956 +       if (hz >= bus_hz) {
957 +               cdiv = 2; /* bus_hz / 2 is as fast as we can go */
958 +       } else if (hz) {
959 +               cdiv = DIV_ROUND_UP(bus_hz, hz);
960 +
961 +               /* CDIV must be a power of 2, so round up */
962 +               cdiv = roundup_pow_of_two(cdiv);
963 +
964 +               if (cdiv > 65536) {
965 +                       dev_dbg(dev,
966 +                               "setup: %d Hz too slow, cdiv %u; min %ld Hz\n",
967 +                               hz, cdiv, bus_hz / 65536);
968 +                       return -EINVAL;
969 +               } else if (cdiv == 65536) {
970 +                       cdiv = 0;
971 +               } else if (cdiv == 1) {
972 +                       cdiv = 2; /* 1 gets rounded down to 0; == 65536 */
973 +               }
974 +       } else {
975 +               cdiv = 0;
976 +       }
977 +
978 +       switch (bpw) {
979 +       case 8:
980 +               break;
981 +       case 9:
982 +               /* Reading in LoSSI mode is a special case. See 'BCM2835 ARM Peripherals' datasheet */
983 +               cs |= SPI_CS_LEN;
984 +               break;
985 +       default:
986 +               dev_dbg(dev, "setup: invalid bits_per_word %u (must be 8 or 9)\n",
987 +                       bpw);
988 +               return -EINVAL;
989 +       }
990 +
991 +       if (mode & SPI_CPOL)
992 +               cs |= SPI_CS_CPOL;
993 +       if (mode & SPI_CPHA)
994 +               cs |= SPI_CS_CPHA;
995 +
996 +       if (!(mode & SPI_NO_CS)) {
997 +               if (mode & SPI_CS_HIGH) {
998 +                       cs |= SPI_CS_CSPOL;
999 +                       cs |= SPI_CS_CSPOL0 << csel;
1000 +               }
1001 +
1002 +               cs |= csel;
1003 +       } else {
1004 +               cs |= SPI_CS_CS_10 | SPI_CS_CS_01;
1005 +       }
1006 +
1007 +       if (state) {
1008 +               state->cs = cs;
1009 +               state->cdiv = cdiv;
1010 +               dev_dbg(dev, "setup: want %d Hz; "
1011 +                       "bus_hz=%lu / cdiv=%u == %lu Hz; "
1012 +                       "mode %u: cs 0x%08X\n",
1013 +                       hz, bus_hz, cdiv, bus_hz/cdiv, mode, cs);
1014 +       }
1015 +
1016 +       return 0;
1017 +}
1018 +
1019 +static int bcm2708_process_transfer(struct bcm2708_spi *bs,
1020 +               struct spi_message *msg, struct spi_transfer *xfer)
1021 +{
1022 +       struct spi_device *spi = msg->spi;
1023 +       struct bcm2708_spi_state state, *stp;
1024 +       int ret;
1025 +       u32 cs;
1026 +
1027 +       if (bs->stopping)
1028 +               return -ESHUTDOWN;
1029 +
1030 +       if (xfer->bits_per_word || xfer->speed_hz) {
1031 +               ret = bcm2708_setup_state(spi->master, &spi->dev, &state,
1032 +                       xfer->speed_hz ? xfer->speed_hz : spi->max_speed_hz,
1033 +                       spi->chip_select, spi->mode,
1034 +                       xfer->bits_per_word ? xfer->bits_per_word :
1035 +                               spi->bits_per_word);
1036 +               if (ret)
1037 +                       return ret;
1038 +
1039 +               stp = &state;
1040 +       } else {
1041 +               stp = spi->controller_state;
1042 +       }
1043 +
1044 +       reinit_completion(&bs->done);
1045 +       bs->tx_buf = xfer->tx_buf;
1046 +       bs->rx_buf = xfer->rx_buf;
1047 +       bs->len = xfer->len;
1048 +
1049 +       cs = stp->cs | SPI_CS_INTR | SPI_CS_INTD | SPI_CS_TA;
1050 +
1051 +       bcm2708_wr(bs, SPI_CLK, stp->cdiv);
1052 +       bcm2708_wr(bs, SPI_CS, cs);
1053 +
1054 +       ret = wait_for_completion_timeout(&bs->done,
1055 +                       msecs_to_jiffies(SPI_TIMEOUT_MS));
1056 +       if (ret == 0) {
1057 +               dev_err(&spi->dev, "transfer timed out\n");
1058 +               return -ETIMEDOUT;
1059 +       }
1060 +
1061 +       if (xfer->delay_usecs)
1062 +               udelay(xfer->delay_usecs);
1063 +
1064 +       if (list_is_last(&xfer->transfer_list, &msg->transfers) ||
1065 +                       xfer->cs_change) {
1066 +               /* clear TA and interrupt flags */
1067 +               bcm2708_wr(bs, SPI_CS, stp->cs);
1068 +       }
1069 +
1070 +       msg->actual_length += (xfer->len - bs->len);
1071 +
1072 +       return 0;
1073 +}
1074 +
1075 +static void bcm2708_work(struct work_struct *work)
1076 +{
1077 +       struct bcm2708_spi *bs = container_of(work, struct bcm2708_spi, work);
1078 +       unsigned long flags;
1079 +       struct spi_message *msg;
1080 +       struct spi_transfer *xfer;
1081 +       int status = 0;
1082 +
1083 +       spin_lock_irqsave(&bs->lock, flags);
1084 +       while (!list_empty(&bs->queue)) {
1085 +               msg = list_first_entry(&bs->queue, struct spi_message, queue);
1086 +               list_del_init(&msg->queue);
1087 +               spin_unlock_irqrestore(&bs->lock, flags);
1088 +
1089 +               list_for_each_entry(xfer, &msg->transfers, transfer_list) {
1090 +                       status = bcm2708_process_transfer(bs, msg, xfer);
1091 +                       if (status)
1092 +                               break;
1093 +               }
1094 +
1095 +               msg->status = status;
1096 +               msg->complete(msg->context);
1097 +
1098 +               spin_lock_irqsave(&bs->lock, flags);
1099 +       }
1100 +       spin_unlock_irqrestore(&bs->lock, flags);
1101 +}
1102 +
1103 +static int bcm2708_spi_setup(struct spi_device *spi)
1104 +{
1105 +       struct bcm2708_spi *bs = spi_master_get_devdata(spi->master);
1106 +       struct bcm2708_spi_state *state;
1107 +       int ret;
1108 +
1109 +       if (bs->stopping)
1110 +               return -ESHUTDOWN;
1111 +
1112 +       if (!(spi->mode & SPI_NO_CS) &&
1113 +                       (spi->chip_select > spi->master->num_chipselect)) {
1114 +               dev_dbg(&spi->dev,
1115 +                       "setup: invalid chipselect %u (%u defined)\n",
1116 +                       spi->chip_select, spi->master->num_chipselect);
1117 +               return -EINVAL;
1118 +       }
1119 +
1120 +       state = spi->controller_state;
1121 +       if (!state) {
1122 +               state = kzalloc(sizeof(*state), GFP_KERNEL);
1123 +               if (!state)
1124 +                       return -ENOMEM;
1125 +
1126 +               spi->controller_state = state;
1127 +       }
1128 +
1129 +       ret = bcm2708_setup_state(spi->master, &spi->dev, state,
1130 +               spi->max_speed_hz, spi->chip_select, spi->mode,
1131 +               spi->bits_per_word);
1132 +       if (ret < 0) {
1133 +               kfree(state);
1134 +               spi->controller_state = NULL;
1135 +                return ret;
1136 +       }
1137 +
1138 +       dev_dbg(&spi->dev,
1139 +               "setup: cd %d: %d Hz, bpw %u, mode 0x%x -> CS=%08x CDIV=%04x\n",
1140 +               spi->chip_select, spi->max_speed_hz, spi->bits_per_word,
1141 +               spi->mode, state->cs, state->cdiv);
1142 +
1143 +       return 0;
1144 +}
1145 +
1146 +static int bcm2708_spi_transfer(struct spi_device *spi, struct spi_message *msg)
1147 +{
1148 +       struct bcm2708_spi *bs = spi_master_get_devdata(spi->master);
1149 +       struct spi_transfer *xfer;
1150 +       int ret;
1151 +       unsigned long flags;
1152 +
1153 +       if (unlikely(list_empty(&msg->transfers)))
1154 +               return -EINVAL;
1155 +
1156 +       if (bs->stopping)
1157 +               return -ESHUTDOWN;
1158 +
1159 +       list_for_each_entry(xfer, &msg->transfers, transfer_list) {
1160 +               if (!(xfer->tx_buf || xfer->rx_buf) && xfer->len) {
1161 +                       dev_dbg(&spi->dev, "missing rx or tx buf\n");
1162 +                       return -EINVAL;
1163 +               }
1164 +
1165 +               if (!xfer->bits_per_word || xfer->speed_hz)
1166 +                       continue;
1167 +
1168 +               ret = bcm2708_setup_state(spi->master, &spi->dev, NULL,
1169 +                       xfer->speed_hz ? xfer->speed_hz : spi->max_speed_hz,
1170 +                       spi->chip_select, spi->mode,
1171 +                       xfer->bits_per_word ? xfer->bits_per_word :
1172 +                               spi->bits_per_word);
1173 +               if (ret)
1174 +                       return ret;
1175 +       }
1176 +
1177 +       msg->status = -EINPROGRESS;
1178 +       msg->actual_length = 0;
1179 +
1180 +       spin_lock_irqsave(&bs->lock, flags);
1181 +       list_add_tail(&msg->queue, &bs->queue);
1182 +       queue_work(bs->workq, &bs->work);
1183 +       spin_unlock_irqrestore(&bs->lock, flags);
1184 +
1185 +       return 0;
1186 +}
1187 +
1188 +static void bcm2708_spi_cleanup(struct spi_device *spi)
1189 +{
1190 +       if (spi->controller_state) {
1191 +               kfree(spi->controller_state);
1192 +               spi->controller_state = NULL;
1193 +       }
1194 +}
1195 +
1196 +static int bcm2708_spi_probe(struct platform_device *pdev)
1197 +{
1198 +       struct resource *regs;
1199 +       int irq, err = -ENOMEM;
1200 +       struct clk *clk;
1201 +       struct spi_master *master;
1202 +       struct bcm2708_spi *bs;
1203 +
1204 +       regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1205 +       if (!regs) {
1206 +               dev_err(&pdev->dev, "could not get IO memory\n");
1207 +               return -ENXIO;
1208 +       }
1209 +
1210 +       irq = platform_get_irq(pdev, 0);
1211 +       if (irq < 0) {
1212 +               dev_err(&pdev->dev, "could not get IRQ\n");
1213 +               return irq;
1214 +       }
1215 +
1216 +       clk = clk_get(&pdev->dev, NULL);
1217 +       if (IS_ERR(clk)) {
1218 +               dev_err(&pdev->dev, "could not find clk: %ld\n", PTR_ERR(clk));
1219 +               return PTR_ERR(clk);
1220 +       }
1221 +
1222 +       bcm2708_init_pinmode();
1223 +
1224 +       master = spi_alloc_master(&pdev->dev, sizeof(*bs));
1225 +       if (!master) {
1226 +               dev_err(&pdev->dev, "spi_alloc_master() failed\n");
1227 +               goto out_clk_put;
1228 +       }
1229 +
1230 +       /* the spi->mode bits understood by this driver: */
1231 +       master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH | SPI_NO_CS;
1232 +
1233 +       master->bus_num = pdev->id;
1234 +       master->num_chipselect = 3;
1235 +       master->setup = bcm2708_spi_setup;
1236 +       master->transfer = bcm2708_spi_transfer;
1237 +       master->cleanup = bcm2708_spi_cleanup;
1238 +       platform_set_drvdata(pdev, master);
1239 +
1240 +       bs = spi_master_get_devdata(master);
1241 +
1242 +       spin_lock_init(&bs->lock);
1243 +       INIT_LIST_HEAD(&bs->queue);
1244 +       init_completion(&bs->done);
1245 +       INIT_WORK(&bs->work, bcm2708_work);
1246 +
1247 +       bs->base = ioremap(regs->start, resource_size(regs));
1248 +       if (!bs->base) {
1249 +               dev_err(&pdev->dev, "could not remap memory\n");
1250 +               goto out_master_put;
1251 +       }
1252 +
1253 +       bs->workq = create_singlethread_workqueue(dev_name(&pdev->dev));
1254 +       if (!bs->workq) {
1255 +               dev_err(&pdev->dev, "could not create workqueue\n");
1256 +               goto out_iounmap;
1257 +       }
1258 +
1259 +       bs->irq = irq;
1260 +       bs->clk = clk;
1261 +       bs->stopping = false;
1262 +
1263 +       err = request_irq(irq, bcm2708_spi_interrupt, 0, dev_name(&pdev->dev),
1264 +                       master);
1265 +       if (err) {
1266 +               dev_err(&pdev->dev, "could not request IRQ: %d\n", err);
1267 +               goto out_workqueue;
1268 +       }
1269 +
1270 +       /* initialise the hardware */
1271 +       clk_enable(clk);
1272 +       bcm2708_wr(bs, SPI_CS, SPI_CS_REN | SPI_CS_CLEAR_RX | SPI_CS_CLEAR_TX);
1273 +
1274 +       err = spi_register_master(master);
1275 +       if (err) {
1276 +               dev_err(&pdev->dev, "could not register SPI master: %d\n", err);
1277 +               goto out_free_irq;
1278 +       }
1279 +
1280 +       dev_info(&pdev->dev, "SPI Controller at 0x%08lx (irq %d)\n",
1281 +               (unsigned long)regs->start, irq);
1282 +
1283 +       return 0;
1284 +
1285 +out_free_irq:
1286 +       free_irq(bs->irq, master);
1287 +out_workqueue:
1288 +       destroy_workqueue(bs->workq);
1289 +out_iounmap:
1290 +       iounmap(bs->base);
1291 +out_master_put:
1292 +       spi_master_put(master);
1293 +out_clk_put:
1294 +       clk_put(clk);
1295 +       return err;
1296 +}
1297 +
1298 +static int bcm2708_spi_remove(struct platform_device *pdev)
1299 +{
1300 +       struct spi_master *master = platform_get_drvdata(pdev);
1301 +       struct bcm2708_spi *bs = spi_master_get_devdata(master);
1302 +
1303 +       /* reset the hardware and block queue progress */
1304 +       spin_lock_irq(&bs->lock);
1305 +       bs->stopping = true;
1306 +       bcm2708_wr(bs, SPI_CS, SPI_CS_CLEAR_RX | SPI_CS_CLEAR_TX);
1307 +       spin_unlock_irq(&bs->lock);
1308 +
1309 +       flush_work(&bs->work);
1310 +
1311 +       clk_disable(bs->clk);
1312 +       clk_put(bs->clk);
1313 +       free_irq(bs->irq, master);
1314 +       iounmap(bs->base);
1315 +
1316 +       spi_unregister_master(master);
1317 +
1318 +       return 0;
1319 +}
1320 +
1321 +static struct platform_driver bcm2708_spi_driver = {
1322 +       .driver         = {
1323 +               .name   = DRV_NAME,
1324 +               .owner  = THIS_MODULE,
1325 +       },
1326 +       .probe          = bcm2708_spi_probe,
1327 +       .remove         = bcm2708_spi_remove,
1328 +};
1329 +
1330 +
1331 +static int __init bcm2708_spi_init(void)
1332 +{
1333 +       return platform_driver_probe(&bcm2708_spi_driver, bcm2708_spi_probe);
1334 +}
1335 +module_init(bcm2708_spi_init);
1336 +
1337 +static void __exit bcm2708_spi_exit(void)
1338 +{
1339 +       platform_driver_unregister(&bcm2708_spi_driver);
1340 +}
1341 +module_exit(bcm2708_spi_exit);
1342 +
1343 +
1344 +//module_platform_driver(bcm2708_spi_driver);
1345 +
1346 +MODULE_DESCRIPTION("SPI controller driver for Broadcom BCM2708");
1347 +MODULE_AUTHOR("Chris Boot <bootc@bootc.net>");
1348 +MODULE_LICENSE("GPL v2");
1349 +MODULE_ALIAS("platform:" DRV_NAME);
1350 -- 
1351 1.8.3.2
1352