spi: delete non-required instances of include <linux/init.h>
[cascardo/linux.git] / drivers / spi / spi-sh-msiof.c
1 /*
2  * SuperH MSIOF SPI Master Interface
3  *
4  * Copyright (c) 2009 Magnus Damm
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 version 2 as
8  * published by the Free Software Foundation.
9  *
10  */
11
12 #include <linux/bitmap.h>
13 #include <linux/clk.h>
14 #include <linux/completion.h>
15 #include <linux/delay.h>
16 #include <linux/err.h>
17 #include <linux/gpio.h>
18 #include <linux/interrupt.h>
19 #include <linux/io.h>
20 #include <linux/kernel.h>
21 #include <linux/module.h>
22 #include <linux/of.h>
23 #include <linux/platform_device.h>
24 #include <linux/pm_runtime.h>
25
26 #include <linux/spi/sh_msiof.h>
27 #include <linux/spi/spi.h>
28 #include <linux/spi/spi_bitbang.h>
29
30 #include <asm/unaligned.h>
31
32 struct sh_msiof_spi_priv {
33         struct spi_bitbang bitbang; /* must be first for spi_bitbang.c */
34         void __iomem *mapbase;
35         struct clk *clk;
36         struct platform_device *pdev;
37         struct sh_msiof_spi_info *info;
38         struct completion done;
39         unsigned long flags;
40         int tx_fifo_size;
41         int rx_fifo_size;
42 };
43
44 #define TMDR1   0x00
45 #define TMDR2   0x04
46 #define TMDR3   0x08
47 #define RMDR1   0x10
48 #define RMDR2   0x14
49 #define RMDR3   0x18
50 #define TSCR    0x20
51 #define RSCR    0x22
52 #define CTR     0x28
53 #define FCTR    0x30
54 #define STR     0x40
55 #define IER     0x44
56 #define TDR1    0x48
57 #define TDR2    0x4c
58 #define TFDR    0x50
59 #define RDR1    0x58
60 #define RDR2    0x5c
61 #define RFDR    0x60
62
63 #define CTR_TSCKE (1 << 15)
64 #define CTR_TFSE  (1 << 14)
65 #define CTR_TXE   (1 << 9)
66 #define CTR_RXE   (1 << 8)
67
68 #define STR_TEOF  (1 << 23)
69 #define STR_REOF  (1 << 7)
70
71 static u32 sh_msiof_read(struct sh_msiof_spi_priv *p, int reg_offs)
72 {
73         switch (reg_offs) {
74         case TSCR:
75         case RSCR:
76                 return ioread16(p->mapbase + reg_offs);
77         default:
78                 return ioread32(p->mapbase + reg_offs);
79         }
80 }
81
82 static void sh_msiof_write(struct sh_msiof_spi_priv *p, int reg_offs,
83                            u32 value)
84 {
85         switch (reg_offs) {
86         case TSCR:
87         case RSCR:
88                 iowrite16(value, p->mapbase + reg_offs);
89                 break;
90         default:
91                 iowrite32(value, p->mapbase + reg_offs);
92                 break;
93         }
94 }
95
96 static int sh_msiof_modify_ctr_wait(struct sh_msiof_spi_priv *p,
97                                     u32 clr, u32 set)
98 {
99         u32 mask = clr | set;
100         u32 data;
101         int k;
102
103         data = sh_msiof_read(p, CTR);
104         data &= ~clr;
105         data |= set;
106         sh_msiof_write(p, CTR, data);
107
108         for (k = 100; k > 0; k--) {
109                 if ((sh_msiof_read(p, CTR) & mask) == set)
110                         break;
111
112                 udelay(10);
113         }
114
115         return k > 0 ? 0 : -ETIMEDOUT;
116 }
117
118 static irqreturn_t sh_msiof_spi_irq(int irq, void *data)
119 {
120         struct sh_msiof_spi_priv *p = data;
121
122         /* just disable the interrupt and wake up */
123         sh_msiof_write(p, IER, 0);
124         complete(&p->done);
125
126         return IRQ_HANDLED;
127 }
128
129 static struct {
130         unsigned short div;
131         unsigned short scr;
132 } const sh_msiof_spi_clk_table[] = {
133         { 1, 0x0007 },
134         { 2, 0x0000 },
135         { 4, 0x0001 },
136         { 8, 0x0002 },
137         { 16, 0x0003 },
138         { 32, 0x0004 },
139         { 64, 0x1f00 },
140         { 128, 0x1f01 },
141         { 256, 0x1f02 },
142         { 512, 0x1f03 },
143         { 1024, 0x1f04 },
144 };
145
146 static void sh_msiof_spi_set_clk_regs(struct sh_msiof_spi_priv *p,
147                                       unsigned long parent_rate,
148                                       unsigned long spi_hz)
149 {
150         unsigned long div = 1024;
151         size_t k;
152
153         if (!WARN_ON(!spi_hz || !parent_rate))
154                 div = DIV_ROUND_UP(parent_rate, spi_hz);
155
156         /* TODO: make more fine grained */
157
158         for (k = 0; k < ARRAY_SIZE(sh_msiof_spi_clk_table); k++) {
159                 if (sh_msiof_spi_clk_table[k].div >= div)
160                         break;
161         }
162
163         k = min_t(int, k, ARRAY_SIZE(sh_msiof_spi_clk_table) - 1);
164
165         sh_msiof_write(p, TSCR, sh_msiof_spi_clk_table[k].scr);
166         sh_msiof_write(p, RSCR, sh_msiof_spi_clk_table[k].scr);
167 }
168
169 static void sh_msiof_spi_set_pin_regs(struct sh_msiof_spi_priv *p,
170                                       u32 cpol, u32 cpha,
171                                       u32 tx_hi_z, u32 lsb_first, u32 cs_high)
172 {
173         u32 tmp;
174         int edge;
175
176         /*
177          * CPOL CPHA     TSCKIZ RSCKIZ TEDG REDG
178          *    0    0         10     10    1    1
179          *    0    1         10     10    0    0
180          *    1    0         11     11    0    0
181          *    1    1         11     11    1    1
182          */
183         sh_msiof_write(p, FCTR, 0);
184
185         tmp = 0;
186         tmp |= !cs_high << 25;
187         tmp |= lsb_first << 24;
188         sh_msiof_write(p, TMDR1, 0xe0000005 | tmp);
189         sh_msiof_write(p, RMDR1, 0x20000005 | tmp);
190
191         tmp = 0xa0000000;
192         tmp |= cpol << 30; /* TSCKIZ */
193         tmp |= cpol << 28; /* RSCKIZ */
194
195         edge = cpol ^ !cpha;
196
197         tmp |= edge << 27; /* TEDG */
198         tmp |= edge << 26; /* REDG */
199         tmp |= (tx_hi_z ? 2 : 0) << 22; /* TXDIZ */
200         sh_msiof_write(p, CTR, tmp);
201 }
202
203 static void sh_msiof_spi_set_mode_regs(struct sh_msiof_spi_priv *p,
204                                        const void *tx_buf, void *rx_buf,
205                                        u32 bits, u32 words)
206 {
207         u32 dr2 = ((bits - 1) << 24) | ((words - 1) << 16);
208
209         if (tx_buf)
210                 sh_msiof_write(p, TMDR2, dr2);
211         else
212                 sh_msiof_write(p, TMDR2, dr2 | 1);
213
214         if (rx_buf)
215                 sh_msiof_write(p, RMDR2, dr2);
216
217         sh_msiof_write(p, IER, STR_TEOF | STR_REOF);
218 }
219
220 static void sh_msiof_reset_str(struct sh_msiof_spi_priv *p)
221 {
222         sh_msiof_write(p, STR, sh_msiof_read(p, STR));
223 }
224
225 static void sh_msiof_spi_write_fifo_8(struct sh_msiof_spi_priv *p,
226                                       const void *tx_buf, int words, int fs)
227 {
228         const u8 *buf_8 = tx_buf;
229         int k;
230
231         for (k = 0; k < words; k++)
232                 sh_msiof_write(p, TFDR, buf_8[k] << fs);
233 }
234
235 static void sh_msiof_spi_write_fifo_16(struct sh_msiof_spi_priv *p,
236                                        const void *tx_buf, int words, int fs)
237 {
238         const u16 *buf_16 = tx_buf;
239         int k;
240
241         for (k = 0; k < words; k++)
242                 sh_msiof_write(p, TFDR, buf_16[k] << fs);
243 }
244
245 static void sh_msiof_spi_write_fifo_16u(struct sh_msiof_spi_priv *p,
246                                         const void *tx_buf, int words, int fs)
247 {
248         const u16 *buf_16 = tx_buf;
249         int k;
250
251         for (k = 0; k < words; k++)
252                 sh_msiof_write(p, TFDR, get_unaligned(&buf_16[k]) << fs);
253 }
254
255 static void sh_msiof_spi_write_fifo_32(struct sh_msiof_spi_priv *p,
256                                        const void *tx_buf, int words, int fs)
257 {
258         const u32 *buf_32 = tx_buf;
259         int k;
260
261         for (k = 0; k < words; k++)
262                 sh_msiof_write(p, TFDR, buf_32[k] << fs);
263 }
264
265 static void sh_msiof_spi_write_fifo_32u(struct sh_msiof_spi_priv *p,
266                                         const void *tx_buf, int words, int fs)
267 {
268         const u32 *buf_32 = tx_buf;
269         int k;
270
271         for (k = 0; k < words; k++)
272                 sh_msiof_write(p, TFDR, get_unaligned(&buf_32[k]) << fs);
273 }
274
275 static void sh_msiof_spi_write_fifo_s32(struct sh_msiof_spi_priv *p,
276                                         const void *tx_buf, int words, int fs)
277 {
278         const u32 *buf_32 = tx_buf;
279         int k;
280
281         for (k = 0; k < words; k++)
282                 sh_msiof_write(p, TFDR, swab32(buf_32[k] << fs));
283 }
284
285 static void sh_msiof_spi_write_fifo_s32u(struct sh_msiof_spi_priv *p,
286                                          const void *tx_buf, int words, int fs)
287 {
288         const u32 *buf_32 = tx_buf;
289         int k;
290
291         for (k = 0; k < words; k++)
292                 sh_msiof_write(p, TFDR, swab32(get_unaligned(&buf_32[k]) << fs));
293 }
294
295 static void sh_msiof_spi_read_fifo_8(struct sh_msiof_spi_priv *p,
296                                      void *rx_buf, int words, int fs)
297 {
298         u8 *buf_8 = rx_buf;
299         int k;
300
301         for (k = 0; k < words; k++)
302                 buf_8[k] = sh_msiof_read(p, RFDR) >> fs;
303 }
304
305 static void sh_msiof_spi_read_fifo_16(struct sh_msiof_spi_priv *p,
306                                       void *rx_buf, int words, int fs)
307 {
308         u16 *buf_16 = rx_buf;
309         int k;
310
311         for (k = 0; k < words; k++)
312                 buf_16[k] = sh_msiof_read(p, RFDR) >> fs;
313 }
314
315 static void sh_msiof_spi_read_fifo_16u(struct sh_msiof_spi_priv *p,
316                                        void *rx_buf, int words, int fs)
317 {
318         u16 *buf_16 = rx_buf;
319         int k;
320
321         for (k = 0; k < words; k++)
322                 put_unaligned(sh_msiof_read(p, RFDR) >> fs, &buf_16[k]);
323 }
324
325 static void sh_msiof_spi_read_fifo_32(struct sh_msiof_spi_priv *p,
326                                       void *rx_buf, int words, int fs)
327 {
328         u32 *buf_32 = rx_buf;
329         int k;
330
331         for (k = 0; k < words; k++)
332                 buf_32[k] = sh_msiof_read(p, RFDR) >> fs;
333 }
334
335 static void sh_msiof_spi_read_fifo_32u(struct sh_msiof_spi_priv *p,
336                                        void *rx_buf, int words, int fs)
337 {
338         u32 *buf_32 = rx_buf;
339         int k;
340
341         for (k = 0; k < words; k++)
342                 put_unaligned(sh_msiof_read(p, RFDR) >> fs, &buf_32[k]);
343 }
344
345 static void sh_msiof_spi_read_fifo_s32(struct sh_msiof_spi_priv *p,
346                                        void *rx_buf, int words, int fs)
347 {
348         u32 *buf_32 = rx_buf;
349         int k;
350
351         for (k = 0; k < words; k++)
352                 buf_32[k] = swab32(sh_msiof_read(p, RFDR) >> fs);
353 }
354
355 static void sh_msiof_spi_read_fifo_s32u(struct sh_msiof_spi_priv *p,
356                                        void *rx_buf, int words, int fs)
357 {
358         u32 *buf_32 = rx_buf;
359         int k;
360
361         for (k = 0; k < words; k++)
362                 put_unaligned(swab32(sh_msiof_read(p, RFDR) >> fs), &buf_32[k]);
363 }
364
365 static int sh_msiof_spi_bits(struct spi_device *spi, struct spi_transfer *t)
366 {
367         int bits;
368
369         bits = t ? t->bits_per_word : 0;
370         if (!bits)
371                 bits = spi->bits_per_word;
372         return bits;
373 }
374
375 static unsigned long sh_msiof_spi_hz(struct spi_device *spi,
376                                      struct spi_transfer *t)
377 {
378         unsigned long hz;
379
380         hz = t ? t->speed_hz : 0;
381         if (!hz)
382                 hz = spi->max_speed_hz;
383         return hz;
384 }
385
386 static int sh_msiof_spi_setup_transfer(struct spi_device *spi,
387                                        struct spi_transfer *t)
388 {
389         int bits;
390
391         /* noting to check hz values against since parent clock is disabled */
392
393         bits = sh_msiof_spi_bits(spi, t);
394         if (bits < 8)
395                 return -EINVAL;
396         if (bits > 32)
397                 return -EINVAL;
398
399         return spi_bitbang_setup_transfer(spi, t);
400 }
401
402 static void sh_msiof_spi_chipselect(struct spi_device *spi, int is_on)
403 {
404         struct sh_msiof_spi_priv *p = spi_master_get_devdata(spi->master);
405         int value;
406
407         /* chip select is active low unless SPI_CS_HIGH is set */
408         if (spi->mode & SPI_CS_HIGH)
409                 value = (is_on == BITBANG_CS_ACTIVE) ? 1 : 0;
410         else
411                 value = (is_on == BITBANG_CS_ACTIVE) ? 0 : 1;
412
413         if (is_on == BITBANG_CS_ACTIVE) {
414                 if (!test_and_set_bit(0, &p->flags)) {
415                         pm_runtime_get_sync(&p->pdev->dev);
416                         clk_enable(p->clk);
417                 }
418
419                 /* Configure pins before asserting CS */
420                 sh_msiof_spi_set_pin_regs(p, !!(spi->mode & SPI_CPOL),
421                                           !!(spi->mode & SPI_CPHA),
422                                           !!(spi->mode & SPI_3WIRE),
423                                           !!(spi->mode & SPI_LSB_FIRST),
424                                           !!(spi->mode & SPI_CS_HIGH));
425         }
426
427         /* use spi->controller data for CS (same strategy as spi_gpio) */
428         gpio_set_value((uintptr_t)spi->controller_data, value);
429
430         if (is_on == BITBANG_CS_INACTIVE) {
431                 if (test_and_clear_bit(0, &p->flags)) {
432                         clk_disable(p->clk);
433                         pm_runtime_put(&p->pdev->dev);
434                 }
435         }
436 }
437
438 static int sh_msiof_spi_txrx_once(struct sh_msiof_spi_priv *p,
439                                   void (*tx_fifo)(struct sh_msiof_spi_priv *,
440                                                   const void *, int, int),
441                                   void (*rx_fifo)(struct sh_msiof_spi_priv *,
442                                                   void *, int, int),
443                                   const void *tx_buf, void *rx_buf,
444                                   int words, int bits)
445 {
446         int fifo_shift;
447         int ret;
448
449         /* limit maximum word transfer to rx/tx fifo size */
450         if (tx_buf)
451                 words = min_t(int, words, p->tx_fifo_size);
452         if (rx_buf)
453                 words = min_t(int, words, p->rx_fifo_size);
454
455         /* the fifo contents need shifting */
456         fifo_shift = 32 - bits;
457
458         /* setup msiof transfer mode registers */
459         sh_msiof_spi_set_mode_regs(p, tx_buf, rx_buf, bits, words);
460
461         /* write tx fifo */
462         if (tx_buf)
463                 tx_fifo(p, tx_buf, words, fifo_shift);
464
465         /* setup clock and rx/tx signals */
466         ret = sh_msiof_modify_ctr_wait(p, 0, CTR_TSCKE);
467         if (rx_buf)
468                 ret = ret ? ret : sh_msiof_modify_ctr_wait(p, 0, CTR_RXE);
469         ret = ret ? ret : sh_msiof_modify_ctr_wait(p, 0, CTR_TXE);
470
471         /* start by setting frame bit */
472         reinit_completion(&p->done);
473         ret = ret ? ret : sh_msiof_modify_ctr_wait(p, 0, CTR_TFSE);
474         if (ret) {
475                 dev_err(&p->pdev->dev, "failed to start hardware\n");
476                 goto err;
477         }
478
479         /* wait for tx fifo to be emptied / rx fifo to be filled */
480         wait_for_completion(&p->done);
481
482         /* read rx fifo */
483         if (rx_buf)
484                 rx_fifo(p, rx_buf, words, fifo_shift);
485
486         /* clear status bits */
487         sh_msiof_reset_str(p);
488
489         /* shut down frame, tx/tx and clock signals */
490         ret = sh_msiof_modify_ctr_wait(p, CTR_TFSE, 0);
491         ret = ret ? ret : sh_msiof_modify_ctr_wait(p, CTR_TXE, 0);
492         if (rx_buf)
493                 ret = ret ? ret : sh_msiof_modify_ctr_wait(p, CTR_RXE, 0);
494         ret = ret ? ret : sh_msiof_modify_ctr_wait(p, CTR_TSCKE, 0);
495         if (ret) {
496                 dev_err(&p->pdev->dev, "failed to shut down hardware\n");
497                 goto err;
498         }
499
500         return words;
501
502  err:
503         sh_msiof_write(p, IER, 0);
504         return ret;
505 }
506
507 static int sh_msiof_spi_txrx(struct spi_device *spi, struct spi_transfer *t)
508 {
509         struct sh_msiof_spi_priv *p = spi_master_get_devdata(spi->master);
510         void (*tx_fifo)(struct sh_msiof_spi_priv *, const void *, int, int);
511         void (*rx_fifo)(struct sh_msiof_spi_priv *, void *, int, int);
512         int bits;
513         int bytes_per_word;
514         int bytes_done;
515         int words;
516         int n;
517         bool swab;
518
519         bits = sh_msiof_spi_bits(spi, t);
520
521         if (bits <= 8 && t->len > 15 && !(t->len & 3)) {
522                 bits = 32;
523                 swab = true;
524         } else {
525                 swab = false;
526         }
527
528         /* setup bytes per word and fifo read/write functions */
529         if (bits <= 8) {
530                 bytes_per_word = 1;
531                 tx_fifo = sh_msiof_spi_write_fifo_8;
532                 rx_fifo = sh_msiof_spi_read_fifo_8;
533         } else if (bits <= 16) {
534                 bytes_per_word = 2;
535                 if ((unsigned long)t->tx_buf & 0x01)
536                         tx_fifo = sh_msiof_spi_write_fifo_16u;
537                 else
538                         tx_fifo = sh_msiof_spi_write_fifo_16;
539
540                 if ((unsigned long)t->rx_buf & 0x01)
541                         rx_fifo = sh_msiof_spi_read_fifo_16u;
542                 else
543                         rx_fifo = sh_msiof_spi_read_fifo_16;
544         } else if (swab) {
545                 bytes_per_word = 4;
546                 if ((unsigned long)t->tx_buf & 0x03)
547                         tx_fifo = sh_msiof_spi_write_fifo_s32u;
548                 else
549                         tx_fifo = sh_msiof_spi_write_fifo_s32;
550
551                 if ((unsigned long)t->rx_buf & 0x03)
552                         rx_fifo = sh_msiof_spi_read_fifo_s32u;
553                 else
554                         rx_fifo = sh_msiof_spi_read_fifo_s32;
555         } else {
556                 bytes_per_word = 4;
557                 if ((unsigned long)t->tx_buf & 0x03)
558                         tx_fifo = sh_msiof_spi_write_fifo_32u;
559                 else
560                         tx_fifo = sh_msiof_spi_write_fifo_32;
561
562                 if ((unsigned long)t->rx_buf & 0x03)
563                         rx_fifo = sh_msiof_spi_read_fifo_32u;
564                 else
565                         rx_fifo = sh_msiof_spi_read_fifo_32;
566         }
567
568         /* setup clocks (clock already enabled in chipselect()) */
569         sh_msiof_spi_set_clk_regs(p, clk_get_rate(p->clk),
570                                   sh_msiof_spi_hz(spi, t));
571
572         /* transfer in fifo sized chunks */
573         words = t->len / bytes_per_word;
574         bytes_done = 0;
575
576         while (bytes_done < t->len) {
577                 void *rx_buf = t->rx_buf ? t->rx_buf + bytes_done : NULL;
578                 const void *tx_buf = t->tx_buf ? t->tx_buf + bytes_done : NULL;
579                 n = sh_msiof_spi_txrx_once(p, tx_fifo, rx_fifo,
580                                            tx_buf,
581                                            rx_buf,
582                                            words, bits);
583                 if (n < 0)
584                         break;
585
586                 bytes_done += n * bytes_per_word;
587                 words -= n;
588         }
589
590         return bytes_done;
591 }
592
593 static u32 sh_msiof_spi_txrx_word(struct spi_device *spi, unsigned nsecs,
594                                   u32 word, u8 bits)
595 {
596         BUG(); /* unused but needed by bitbang code */
597         return 0;
598 }
599
600 #ifdef CONFIG_OF
601 static struct sh_msiof_spi_info *sh_msiof_spi_parse_dt(struct device *dev)
602 {
603         struct sh_msiof_spi_info *info;
604         struct device_node *np = dev->of_node;
605         u32 num_cs = 0;
606
607         info = devm_kzalloc(dev, sizeof(struct sh_msiof_spi_info), GFP_KERNEL);
608         if (!info) {
609                 dev_err(dev, "failed to allocate setup data\n");
610                 return NULL;
611         }
612
613         /* Parse the MSIOF properties */
614         of_property_read_u32(np, "num-cs", &num_cs);
615         of_property_read_u32(np, "renesas,tx-fifo-size",
616                                         &info->tx_fifo_override);
617         of_property_read_u32(np, "renesas,rx-fifo-size",
618                                         &info->rx_fifo_override);
619
620         info->num_chipselect = num_cs;
621
622         return info;
623 }
624 #else
625 static struct sh_msiof_spi_info *sh_msiof_spi_parse_dt(struct device *dev)
626 {
627         return NULL;
628 }
629 #endif
630
631 static int sh_msiof_spi_probe(struct platform_device *pdev)
632 {
633         struct resource *r;
634         struct spi_master *master;
635         struct sh_msiof_spi_priv *p;
636         int i;
637         int ret;
638
639         master = spi_alloc_master(&pdev->dev, sizeof(struct sh_msiof_spi_priv));
640         if (master == NULL) {
641                 dev_err(&pdev->dev, "failed to allocate spi master\n");
642                 return -ENOMEM;
643         }
644
645         p = spi_master_get_devdata(master);
646
647         platform_set_drvdata(pdev, p);
648         if (pdev->dev.of_node)
649                 p->info = sh_msiof_spi_parse_dt(&pdev->dev);
650         else
651                 p->info = dev_get_platdata(&pdev->dev);
652
653         if (!p->info) {
654                 dev_err(&pdev->dev, "failed to obtain device info\n");
655                 ret = -ENXIO;
656                 goto err1;
657         }
658
659         init_completion(&p->done);
660
661         p->clk = devm_clk_get(&pdev->dev, NULL);
662         if (IS_ERR(p->clk)) {
663                 dev_err(&pdev->dev, "cannot get clock\n");
664                 ret = PTR_ERR(p->clk);
665                 goto err1;
666         }
667
668         i = platform_get_irq(pdev, 0);
669         if (i < 0) {
670                 dev_err(&pdev->dev, "cannot get platform IRQ\n");
671                 ret = -ENOENT;
672                 goto err1;
673         }
674
675         r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
676         p->mapbase = devm_ioremap_resource(&pdev->dev, r);
677         if (IS_ERR(p->mapbase)) {
678                 ret = PTR_ERR(p->mapbase);
679                 goto err1;
680         }
681
682         ret = devm_request_irq(&pdev->dev, i, sh_msiof_spi_irq, 0,
683                                dev_name(&pdev->dev), p);
684         if (ret) {
685                 dev_err(&pdev->dev, "unable to request irq\n");
686                 goto err1;
687         }
688
689         ret = clk_prepare(p->clk);
690         if (ret < 0) {
691                 dev_err(&pdev->dev, "unable to prepare clock\n");
692                 goto err1;
693         }
694
695         p->pdev = pdev;
696         pm_runtime_enable(&pdev->dev);
697
698         /* The standard version of MSIOF use 64 word FIFOs */
699         p->tx_fifo_size = 64;
700         p->rx_fifo_size = 64;
701
702         /* Platform data may override FIFO sizes */
703         if (p->info->tx_fifo_override)
704                 p->tx_fifo_size = p->info->tx_fifo_override;
705         if (p->info->rx_fifo_override)
706                 p->rx_fifo_size = p->info->rx_fifo_override;
707
708         /* init master and bitbang code */
709         master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
710         master->mode_bits |= SPI_LSB_FIRST | SPI_3WIRE;
711         master->flags = 0;
712         master->bus_num = pdev->id;
713         master->num_chipselect = p->info->num_chipselect;
714         master->setup = spi_bitbang_setup;
715         master->cleanup = spi_bitbang_cleanup;
716
717         p->bitbang.master = master;
718         p->bitbang.chipselect = sh_msiof_spi_chipselect;
719         p->bitbang.setup_transfer = sh_msiof_spi_setup_transfer;
720         p->bitbang.txrx_bufs = sh_msiof_spi_txrx;
721         p->bitbang.txrx_word[SPI_MODE_0] = sh_msiof_spi_txrx_word;
722         p->bitbang.txrx_word[SPI_MODE_1] = sh_msiof_spi_txrx_word;
723         p->bitbang.txrx_word[SPI_MODE_2] = sh_msiof_spi_txrx_word;
724         p->bitbang.txrx_word[SPI_MODE_3] = sh_msiof_spi_txrx_word;
725
726         ret = spi_bitbang_start(&p->bitbang);
727         if (ret == 0)
728                 return 0;
729
730         pm_runtime_disable(&pdev->dev);
731         clk_unprepare(p->clk);
732  err1:
733         spi_master_put(master);
734         return ret;
735 }
736
737 static int sh_msiof_spi_remove(struct platform_device *pdev)
738 {
739         struct sh_msiof_spi_priv *p = platform_get_drvdata(pdev);
740         int ret;
741
742         ret = spi_bitbang_stop(&p->bitbang);
743         if (!ret) {
744                 pm_runtime_disable(&pdev->dev);
745                 clk_unprepare(p->clk);
746                 spi_master_put(p->bitbang.master);
747         }
748         return ret;
749 }
750
751 #ifdef CONFIG_OF
752 static const struct of_device_id sh_msiof_match[] = {
753         { .compatible = "renesas,sh-msiof", },
754         { .compatible = "renesas,sh-mobile-msiof", },
755         {},
756 };
757 MODULE_DEVICE_TABLE(of, sh_msiof_match);
758 #endif
759
760 static struct platform_driver sh_msiof_spi_drv = {
761         .probe          = sh_msiof_spi_probe,
762         .remove         = sh_msiof_spi_remove,
763         .driver         = {
764                 .name           = "spi_sh_msiof",
765                 .owner          = THIS_MODULE,
766                 .of_match_table = of_match_ptr(sh_msiof_match),
767         },
768 };
769 module_platform_driver(sh_msiof_spi_drv);
770
771 MODULE_DESCRIPTION("SuperH MSIOF SPI Master Interface Driver");
772 MODULE_AUTHOR("Magnus Damm");
773 MODULE_LICENSE("GPL v2");
774 MODULE_ALIAS("platform:spi_sh_msiof");