Merge branch 'x86-fixes-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git...
[sfrench/cifs-2.6.git] / sound / soc / sh / siu_dai.c
1 /*
2  * siu_dai.c - ALSA SoC driver for Renesas SH7343, SH7722 SIU peripheral.
3  *
4  * Copyright (C) 2009-2010 Guennadi Liakhovetski <g.liakhovetski@gmx.de>
5  * Copyright (C) 2006 Carlos Munoz <carlos@kenati.com>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  */
21
22 #include <linux/delay.h>
23 #include <linux/firmware.h>
24 #include <linux/pm_runtime.h>
25 #include <linux/slab.h>
26
27 #include <asm/clock.h>
28 #include <asm/siu.h>
29
30 #include <sound/control.h>
31 #include <sound/soc-dai.h>
32
33 #include "siu.h"
34
35 /* Board specifics */
36 #if defined(CONFIG_CPU_SUBTYPE_SH7722)
37 # define SIU_MAX_VOLUME         0x1000
38 #else
39 # define SIU_MAX_VOLUME         0x7fff
40 #endif
41
42 #define PRAM_SIZE       0x2000
43 #define XRAM_SIZE       0x800
44 #define YRAM_SIZE       0x800
45
46 #define XRAM_OFFSET     0x4000
47 #define YRAM_OFFSET     0x6000
48 #define REG_OFFSET      0xc000
49
50 #define PLAYBACK_ENABLED        1
51 #define CAPTURE_ENABLED         2
52
53 #define VOLUME_CAPTURE          0
54 #define VOLUME_PLAYBACK         1
55 #define DFLT_VOLUME_LEVEL       0x08000800
56
57 /*
58  * SPDIF is only available on port A and on some SIU implementations it is only
59  * available for input. Due to the lack of hardware to test it, SPDIF is left
60  * disabled in this driver version
61  */
62 struct format_flag {
63         u32     i2s;
64         u32     pcm;
65         u32     spdif;
66         u32     mask;
67 };
68
69 struct port_flag {
70         struct format_flag      playback;
71         struct format_flag      capture;
72 };
73
74 static struct port_flag siu_flags[SIU_PORT_NUM] = {
75         [SIU_PORT_A] = {
76                 .playback = {
77                         .i2s    = 0x50000000,
78                         .pcm    = 0x40000000,
79                         .spdif  = 0x80000000,   /* not on all SIU versions */
80                         .mask   = 0xd0000000,
81                 },
82                 .capture = {
83                         .i2s    = 0x05000000,
84                         .pcm    = 0x04000000,
85                         .spdif  = 0x08000000,
86                         .mask   = 0x0d000000,
87                 },
88         },
89         [SIU_PORT_B] = {
90                 .playback = {
91                         .i2s    = 0x00500000,
92                         .pcm    = 0x00400000,
93                         .spdif  = 0,            /* impossible - turn off */
94                         .mask   = 0x00500000,
95                 },
96                 .capture = {
97                         .i2s    = 0x00050000,
98                         .pcm    = 0x00040000,
99                         .spdif  = 0,            /* impossible - turn off */
100                         .mask   = 0x00050000,
101                 },
102         },
103 };
104
105 static void siu_dai_start(struct siu_port *port_info)
106 {
107         struct siu_info *info = siu_i2s_dai.private_data;
108         u32 __iomem *base = info->reg;
109
110         dev_dbg(port_info->pcm->card->dev, "%s\n", __func__);
111
112         /* Turn on SIU clock */
113         pm_runtime_get_sync(siu_i2s_dai.dev);
114
115         /* Issue software reset to siu */
116         siu_write32(base + SIU_SRCTL, 0);
117
118         /* Wait for the reset to take effect */
119         udelay(1);
120
121         port_info->stfifo = 0;
122         port_info->trdat = 0;
123
124         /* portA, portB, SIU operate */
125         siu_write32(base + SIU_SRCTL, 0x301);
126
127         /* portA=256fs, portB=256fs */
128         siu_write32(base + SIU_CKCTL, 0x40400000);
129
130         /* portA's BRG does not divide SIUCKA */
131         siu_write32(base + SIU_BRGASEL, 0);
132         siu_write32(base + SIU_BRRA, 0);
133
134         /* portB's BRG divides SIUCKB by half */
135         siu_write32(base + SIU_BRGBSEL, 1);
136         siu_write32(base + SIU_BRRB, 0);
137
138         siu_write32(base + SIU_IFCTL, 0x44440000);
139
140         /* portA: 32 bit/fs, master; portB: 32 bit/fs, master */
141         siu_write32(base + SIU_SFORM, 0x0c0c0000);
142
143         /*
144          * Volume levels: looks like the DSP firmware implements volume controls
145          * differently from what's described in the datasheet
146          */
147         siu_write32(base + SIU_SBDVCA, port_info->playback.volume);
148         siu_write32(base + SIU_SBDVCB, port_info->capture.volume);
149 }
150
151 static void siu_dai_stop(void)
152 {
153         struct siu_info *info = siu_i2s_dai.private_data;
154         u32 __iomem *base = info->reg;
155
156         /* SIU software reset */
157         siu_write32(base + SIU_SRCTL, 0);
158
159         /* Turn off SIU clock */
160         pm_runtime_put_sync(siu_i2s_dai.dev);
161 }
162
163 static void siu_dai_spbAselect(struct siu_port *port_info)
164 {
165         struct siu_info *info = siu_i2s_dai.private_data;
166         struct siu_firmware *fw = &info->fw;
167         u32 *ydef = fw->yram0;
168         u32 idx;
169
170         /* path A use */
171         if (!info->port_id)
172                 idx = 1;                /* portA */
173         else
174                 idx = 2;                /* portB */
175
176         ydef[0] = (fw->spbpar[idx].ab1a << 16) |
177                 (fw->spbpar[idx].ab0a << 8) |
178                 (fw->spbpar[idx].dir << 7) | 3;
179         ydef[1] = fw->yram0[1]; /* 0x03000300 */
180         ydef[2] = (16 / 2) << 24;
181         ydef[3] = fw->yram0[3]; /* 0 */
182         ydef[4] = fw->yram0[4]; /* 0 */
183         ydef[7] = fw->spbpar[idx].event;
184         port_info->stfifo |= fw->spbpar[idx].stfifo;
185         port_info->trdat |= fw->spbpar[idx].trdat;
186 }
187
188 static void siu_dai_spbBselect(struct siu_port *port_info)
189 {
190         struct siu_info *info = siu_i2s_dai.private_data;
191         struct siu_firmware *fw = &info->fw;
192         u32 *ydef = fw->yram0;
193         u32 idx;
194
195         /* path B use */
196         if (!info->port_id)
197                 idx = 7;                /* portA */
198         else
199                 idx = 8;                /* portB */
200
201         ydef[5] = (fw->spbpar[idx].ab1a << 16) |
202                 (fw->spbpar[idx].ab0a << 8) | 1;
203         ydef[6] = fw->spbpar[idx].event;
204         port_info->stfifo |= fw->spbpar[idx].stfifo;
205         port_info->trdat |= fw->spbpar[idx].trdat;
206 }
207
208 static void siu_dai_open(struct siu_stream *siu_stream)
209 {
210         struct siu_info *info = siu_i2s_dai.private_data;
211         u32 __iomem *base = info->reg;
212         u32 srctl, ifctl;
213
214         srctl = siu_read32(base + SIU_SRCTL);
215         ifctl = siu_read32(base + SIU_IFCTL);
216
217         switch (info->port_id) {
218         case SIU_PORT_A:
219                 /* portA operates */
220                 srctl |= 0x200;
221                 ifctl &= ~0xc2;
222                 break;
223         case SIU_PORT_B:
224                 /* portB operates */
225                 srctl |= 0x100;
226                 ifctl &= ~0x31;
227                 break;
228         }
229
230         siu_write32(base + SIU_SRCTL, srctl);
231         /* Unmute and configure portA */
232         siu_write32(base + SIU_IFCTL, ifctl);
233 }
234
235 /*
236  * At the moment only fixed Left-upper, Left-lower, Right-upper, Right-lower
237  * packing is supported
238  */
239 static void siu_dai_pcmdatapack(struct siu_stream *siu_stream)
240 {
241         struct siu_info *info = siu_i2s_dai.private_data;
242         u32 __iomem *base = info->reg;
243         u32 dpak;
244
245         dpak = siu_read32(base + SIU_DPAK);
246
247         switch (info->port_id) {
248         case SIU_PORT_A:
249                 dpak &= ~0xc0000000;
250                 break;
251         case SIU_PORT_B:
252                 dpak &= ~0x00c00000;
253                 break;
254         }
255
256         siu_write32(base + SIU_DPAK, dpak);
257 }
258
259 static int siu_dai_spbstart(struct siu_port *port_info)
260 {
261         struct siu_info *info = siu_i2s_dai.private_data;
262         u32 __iomem *base = info->reg;
263         struct siu_firmware *fw = &info->fw;
264         u32 *ydef = fw->yram0;
265         int cnt;
266         u32 __iomem *add;
267         u32 *ptr;
268
269         /* Load SPB Program in PRAM */
270         ptr = fw->pram0;
271         add = info->pram;
272         for (cnt = 0; cnt < PRAM0_SIZE; cnt++, add++, ptr++)
273                 siu_write32(add, *ptr);
274
275         ptr = fw->pram1;
276         add = info->pram + (0x0100 / sizeof(u32));
277         for (cnt = 0; cnt < PRAM1_SIZE; cnt++, add++, ptr++)
278                 siu_write32(add, *ptr);
279
280         /* XRAM initialization */
281         add = info->xram;
282         for (cnt = 0; cnt < XRAM0_SIZE + XRAM1_SIZE + XRAM2_SIZE; cnt++, add++)
283                 siu_write32(add, 0);
284
285         /* YRAM variable area initialization */
286         add = info->yram;
287         for (cnt = 0; cnt < YRAM_DEF_SIZE; cnt++, add++)
288                 siu_write32(add, ydef[cnt]);
289
290         /* YRAM FIR coefficient area initialization */
291         add = info->yram + (0x0200 / sizeof(u32));
292         for (cnt = 0; cnt < YRAM_FIR_SIZE; cnt++, add++)
293                 siu_write32(add, fw->yram_fir_coeff[cnt]);
294
295         /* YRAM IIR coefficient area initialization */
296         add = info->yram + (0x0600 / sizeof(u32));
297         for (cnt = 0; cnt < YRAM_IIR_SIZE; cnt++, add++)
298                 siu_write32(add, 0);
299
300         siu_write32(base + SIU_TRDAT, port_info->trdat);
301         port_info->trdat = 0x0;
302
303
304         /* SPB start condition: software */
305         siu_write32(base + SIU_SBACTIV, 0);
306         /* Start SPB */
307         siu_write32(base + SIU_SBCTL, 0xc0000000);
308         /* Wait for program to halt */
309         cnt = 0x10000;
310         while (--cnt && siu_read32(base + SIU_SBCTL) != 0x80000000)
311                 cpu_relax();
312
313         if (!cnt)
314                 return -EBUSY;
315
316         /* SPB program start address setting */
317         siu_write32(base + SIU_SBPSET, 0x00400000);
318         /* SPB hardware start(FIFOCTL source) */
319         siu_write32(base + SIU_SBACTIV, 0xc0000000);
320
321         return 0;
322 }
323
324 static void siu_dai_spbstop(struct siu_port *port_info)
325 {
326         struct siu_info *info = siu_i2s_dai.private_data;
327         u32 __iomem *base = info->reg;
328
329         siu_write32(base + SIU_SBACTIV, 0);
330         /* SPB stop */
331         siu_write32(base + SIU_SBCTL, 0);
332
333         port_info->stfifo = 0;
334 }
335
336 /*              API functions           */
337
338 /* Playback and capture hardware properties are identical */
339 static struct snd_pcm_hardware siu_dai_pcm_hw = {
340         .info                   = SNDRV_PCM_INFO_INTERLEAVED,
341         .formats                = SNDRV_PCM_FMTBIT_S16,
342         .rates                  = SNDRV_PCM_RATE_8000_48000,
343         .rate_min               = 8000,
344         .rate_max               = 48000,
345         .channels_min           = 2,
346         .channels_max           = 2,
347         .buffer_bytes_max       = SIU_BUFFER_BYTES_MAX,
348         .period_bytes_min       = SIU_PERIOD_BYTES_MIN,
349         .period_bytes_max       = SIU_PERIOD_BYTES_MAX,
350         .periods_min            = SIU_PERIODS_MIN,
351         .periods_max            = SIU_PERIODS_MAX,
352 };
353
354 static int siu_dai_info_volume(struct snd_kcontrol *kctrl,
355                                struct snd_ctl_elem_info *uinfo)
356 {
357         struct siu_port *port_info = snd_kcontrol_chip(kctrl);
358
359         dev_dbg(port_info->pcm->card->dev, "%s\n", __func__);
360
361         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
362         uinfo->count = 2;
363         uinfo->value.integer.min = 0;
364         uinfo->value.integer.max = SIU_MAX_VOLUME;
365
366         return 0;
367 }
368
369 static int siu_dai_get_volume(struct snd_kcontrol *kctrl,
370                               struct snd_ctl_elem_value *ucontrol)
371 {
372         struct siu_port *port_info = snd_kcontrol_chip(kctrl);
373         struct device *dev = port_info->pcm->card->dev;
374         u32 vol;
375
376         dev_dbg(dev, "%s\n", __func__);
377
378         switch (kctrl->private_value) {
379         case VOLUME_PLAYBACK:
380                 /* Playback is always on port 0 */
381                 vol = port_info->playback.volume;
382                 ucontrol->value.integer.value[0] = vol & 0xffff;
383                 ucontrol->value.integer.value[1] = vol >> 16 & 0xffff;
384                 break;
385         case VOLUME_CAPTURE:
386                 /* Capture is always on port 1 */
387                 vol = port_info->capture.volume;
388                 ucontrol->value.integer.value[0] = vol & 0xffff;
389                 ucontrol->value.integer.value[1] = vol >> 16 & 0xffff;
390                 break;
391         default:
392                 dev_err(dev, "%s() invalid private_value=%ld\n",
393                         __func__, kctrl->private_value);
394                 return -EINVAL;
395         }
396
397         return 0;
398 }
399
400 static int siu_dai_put_volume(struct snd_kcontrol *kctrl,
401                               struct snd_ctl_elem_value *ucontrol)
402 {
403         struct siu_port *port_info = snd_kcontrol_chip(kctrl);
404         struct device *dev = port_info->pcm->card->dev;
405         struct siu_info *info = siu_i2s_dai.private_data;
406         u32 __iomem *base = info->reg;
407         u32 new_vol;
408         u32 cur_vol;
409
410         dev_dbg(dev, "%s\n", __func__);
411
412         if (ucontrol->value.integer.value[0] < 0 ||
413             ucontrol->value.integer.value[0] > SIU_MAX_VOLUME ||
414             ucontrol->value.integer.value[1] < 0 ||
415             ucontrol->value.integer.value[1] > SIU_MAX_VOLUME)
416                 return -EINVAL;
417
418         new_vol = ucontrol->value.integer.value[0] |
419                 ucontrol->value.integer.value[1] << 16;
420
421         /* See comment above - DSP firmware implementation */
422         switch (kctrl->private_value) {
423         case VOLUME_PLAYBACK:
424                 /* Playback is always on port 0 */
425                 cur_vol = port_info->playback.volume;
426                 siu_write32(base + SIU_SBDVCA, new_vol);
427                 port_info->playback.volume = new_vol;
428                 break;
429         case VOLUME_CAPTURE:
430                 /* Capture is always on port 1 */
431                 cur_vol = port_info->capture.volume;
432                 siu_write32(base + SIU_SBDVCB, new_vol);
433                 port_info->capture.volume = new_vol;
434                 break;
435         default:
436                 dev_err(dev, "%s() invalid private_value=%ld\n",
437                         __func__, kctrl->private_value);
438                 return -EINVAL;
439         }
440
441         if (cur_vol != new_vol)
442                 return 1;
443
444         return 0;
445 }
446
447 static struct snd_kcontrol_new playback_controls = {
448         .iface          = SNDRV_CTL_ELEM_IFACE_MIXER,
449         .name           = "PCM Playback Volume",
450         .index          = 0,
451         .info           = siu_dai_info_volume,
452         .get            = siu_dai_get_volume,
453         .put            = siu_dai_put_volume,
454         .private_value  = VOLUME_PLAYBACK,
455 };
456
457 static struct snd_kcontrol_new capture_controls = {
458         .iface          = SNDRV_CTL_ELEM_IFACE_MIXER,
459         .name           = "PCM Capture Volume",
460         .index          = 0,
461         .info           = siu_dai_info_volume,
462         .get            = siu_dai_get_volume,
463         .put            = siu_dai_put_volume,
464         .private_value  = VOLUME_CAPTURE,
465 };
466
467 int siu_init_port(int port, struct siu_port **port_info, struct snd_card *card)
468 {
469         struct device *dev = card->dev;
470         struct snd_kcontrol *kctrl;
471         int ret;
472
473         *port_info = kzalloc(sizeof(**port_info), GFP_KERNEL);
474         if (!*port_info)
475                 return -ENOMEM;
476
477         dev_dbg(dev, "%s: port #%d@%p\n", __func__, port, *port_info);
478
479         (*port_info)->playback.volume = DFLT_VOLUME_LEVEL;
480         (*port_info)->capture.volume = DFLT_VOLUME_LEVEL;
481
482         /*
483          * Add mixer support. The SPB is used to change the volume. Both
484          * ports use the same SPB. Therefore, we only register one
485          * control instance since it will be used by both channels.
486          * In error case we continue without controls.
487          */
488         kctrl = snd_ctl_new1(&playback_controls, *port_info);
489         ret = snd_ctl_add(card, kctrl);
490         if (ret < 0)
491                 dev_err(dev,
492                         "failed to add playback controls %p port=%d err=%d\n",
493                         kctrl, port, ret);
494
495         kctrl = snd_ctl_new1(&capture_controls, *port_info);
496         ret = snd_ctl_add(card, kctrl);
497         if (ret < 0)
498                 dev_err(dev,
499                         "failed to add capture controls %p port=%d err=%d\n",
500                         kctrl, port, ret);
501
502         return 0;
503 }
504
505 void siu_free_port(struct siu_port *port_info)
506 {
507         kfree(port_info);
508 }
509
510 static int siu_dai_startup(struct snd_pcm_substream *substream,
511                            struct snd_soc_dai *dai)
512 {
513         struct siu_info *info = siu_i2s_dai.private_data;
514         struct snd_pcm_runtime *rt = substream->runtime;
515         struct siu_port *port_info = siu_port_info(substream);
516         int ret;
517
518         dev_dbg(substream->pcm->card->dev, "%s: port=%d@%p\n", __func__,
519                 info->port_id, port_info);
520
521         snd_soc_set_runtime_hwparams(substream, &siu_dai_pcm_hw);
522
523         ret = snd_pcm_hw_constraint_integer(rt, SNDRV_PCM_HW_PARAM_PERIODS);
524         if (unlikely(ret < 0))
525                 return ret;
526
527         siu_dai_start(port_info);
528
529         return 0;
530 }
531
532 static void siu_dai_shutdown(struct snd_pcm_substream *substream,
533                              struct snd_soc_dai *dai)
534 {
535         struct siu_info *info = siu_i2s_dai.private_data;
536         struct siu_port *port_info = siu_port_info(substream);
537
538         dev_dbg(substream->pcm->card->dev, "%s: port=%d@%p\n", __func__,
539                 info->port_id, port_info);
540
541         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
542                 port_info->play_cap &= ~PLAYBACK_ENABLED;
543         else
544                 port_info->play_cap &= ~CAPTURE_ENABLED;
545
546         /* Stop the siu if the other stream is not using it */
547         if (!port_info->play_cap) {
548                 /* during stmread or stmwrite ? */
549                 BUG_ON(port_info->playback.rw_flg || port_info->capture.rw_flg);
550                 siu_dai_spbstop(port_info);
551                 siu_dai_stop();
552         }
553 }
554
555 /* PCM part of siu_dai_playback_prepare() / siu_dai_capture_prepare() */
556 static int siu_dai_prepare(struct snd_pcm_substream *substream,
557                            struct snd_soc_dai *dai)
558 {
559         struct siu_info *info = siu_i2s_dai.private_data;
560         struct snd_pcm_runtime *rt = substream->runtime;
561         struct siu_port *port_info = siu_port_info(substream);
562         struct siu_stream *siu_stream;
563         int self, ret;
564
565         dev_dbg(substream->pcm->card->dev,
566                 "%s: port %d, active streams %lx, %d channels\n",
567                 __func__, info->port_id, port_info->play_cap, rt->channels);
568
569         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
570                 self = PLAYBACK_ENABLED;
571                 siu_stream = &port_info->playback;
572         } else {
573                 self = CAPTURE_ENABLED;
574                 siu_stream = &port_info->capture;
575         }
576
577         /* Set up the siu if not already done */
578         if (!port_info->play_cap) {
579                 siu_stream->rw_flg = 0; /* stream-data transfer flag */
580
581                 siu_dai_spbAselect(port_info);
582                 siu_dai_spbBselect(port_info);
583
584                 siu_dai_open(siu_stream);
585
586                 siu_dai_pcmdatapack(siu_stream);
587
588                 ret = siu_dai_spbstart(port_info);
589                 if (ret < 0)
590                         goto fail;
591         } else {
592                 ret = 0;
593         }
594
595         port_info->play_cap |= self;
596
597 fail:
598         return ret;
599 }
600
601 /*
602  * SIU can set bus format to I2S / PCM / SPDIF independently for playback and
603  * capture, however, the current API sets the bus format globally for a DAI.
604  */
605 static int siu_dai_set_fmt(struct snd_soc_dai *dai,
606                            unsigned int fmt)
607 {
608         struct siu_info *info = siu_i2s_dai.private_data;
609         u32 __iomem *base = info->reg;
610         u32 ifctl;
611
612         dev_dbg(dai->dev, "%s: fmt 0x%x on port %d\n",
613                 __func__, fmt, info->port_id);
614
615         if (info->port_id < 0)
616                 return -ENODEV;
617
618         /* Here select between I2S / PCM / SPDIF */
619         switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
620         case SND_SOC_DAIFMT_I2S:
621                 ifctl = siu_flags[info->port_id].playback.i2s |
622                         siu_flags[info->port_id].capture.i2s;
623                 break;
624         case SND_SOC_DAIFMT_LEFT_J:
625                 ifctl = siu_flags[info->port_id].playback.pcm |
626                         siu_flags[info->port_id].capture.pcm;
627                 break;
628         /* SPDIF disabled - see comment at the top */
629         default:
630                 return -EINVAL;
631         }
632
633         ifctl |= ~(siu_flags[info->port_id].playback.mask |
634                    siu_flags[info->port_id].capture.mask) &
635                 siu_read32(base + SIU_IFCTL);
636         siu_write32(base + SIU_IFCTL, ifctl);
637
638         return 0;
639 }
640
641 static int siu_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
642                               unsigned int freq, int dir)
643 {
644         struct clk *siu_clk, *parent_clk;
645         char *siu_name, *parent_name;
646         int ret;
647
648         if (dir != SND_SOC_CLOCK_IN)
649                 return -EINVAL;
650
651         dev_dbg(dai->dev, "%s: using clock %d\n", __func__, clk_id);
652
653         switch (clk_id) {
654         case SIU_CLKA_PLL:
655                 siu_name = "siua_clk";
656                 parent_name = "pll_clk";
657                 break;
658         case SIU_CLKA_EXT:
659                 siu_name = "siua_clk";
660                 parent_name = "siumcka_clk";
661                 break;
662         case SIU_CLKB_PLL:
663                 siu_name = "siub_clk";
664                 parent_name = "pll_clk";
665                 break;
666         case SIU_CLKB_EXT:
667                 siu_name = "siub_clk";
668                 parent_name = "siumckb_clk";
669                 break;
670         default:
671                 return -EINVAL;
672         }
673
674         siu_clk = clk_get(siu_i2s_dai.dev, siu_name);
675         if (IS_ERR(siu_clk))
676                 return PTR_ERR(siu_clk);
677
678         parent_clk = clk_get(siu_i2s_dai.dev, parent_name);
679         if (!IS_ERR(parent_clk)) {
680                 ret = clk_set_parent(siu_clk, parent_clk);
681                 if (!ret)
682                         clk_set_rate(siu_clk, freq);
683                 clk_put(parent_clk);
684         }
685
686         clk_put(siu_clk);
687
688         return 0;
689 }
690
691 static struct snd_soc_dai_ops siu_dai_ops = {
692         .startup        = siu_dai_startup,
693         .shutdown       = siu_dai_shutdown,
694         .prepare        = siu_dai_prepare,
695         .set_sysclk     = siu_dai_set_sysclk,
696         .set_fmt        = siu_dai_set_fmt,
697 };
698
699 struct snd_soc_dai siu_i2s_dai = {
700         .name = "sh-siu",
701         .id = 0,
702         .playback = {
703                 .channels_min = 2,
704                 .channels_max = 2,
705                 .formats = SNDRV_PCM_FMTBIT_S16,
706                 .rates = SNDRV_PCM_RATE_8000_48000,
707         },
708         .capture = {
709                 .channels_min = 2,
710                 .channels_max = 2,
711                 .formats = SNDRV_PCM_FMTBIT_S16,
712                 .rates = SNDRV_PCM_RATE_8000_48000,
713          },
714         .ops = &siu_dai_ops,
715 };
716 EXPORT_SYMBOL_GPL(siu_i2s_dai);
717
718 static int __devinit siu_probe(struct platform_device *pdev)
719 {
720         const struct firmware *fw_entry;
721         struct resource *res, *region;
722         struct siu_info *info;
723         int ret;
724
725         info = kmalloc(sizeof(*info), GFP_KERNEL);
726         if (!info)
727                 return -ENOMEM;
728
729         ret = request_firmware(&fw_entry, "siu_spb.bin", &pdev->dev);
730         if (ret)
731                 goto ereqfw;
732
733         /*
734          * Loaded firmware is "const" - read only, but we have to modify it in
735          * snd_siu_sh7343_spbAselect() and snd_siu_sh7343_spbBselect()
736          */
737         memcpy(&info->fw, fw_entry->data, fw_entry->size);
738
739         release_firmware(fw_entry);
740
741         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
742         if (!res) {
743                 ret = -ENODEV;
744                 goto egetres;
745         }
746
747         region = request_mem_region(res->start, resource_size(res),
748                                     pdev->name);
749         if (!region) {
750                 dev_err(&pdev->dev, "SIU region already claimed\n");
751                 ret = -EBUSY;
752                 goto ereqmemreg;
753         }
754
755         ret = -ENOMEM;
756         info->pram = ioremap(res->start, PRAM_SIZE);
757         if (!info->pram)
758                 goto emappram;
759         info->xram = ioremap(res->start + XRAM_OFFSET, XRAM_SIZE);
760         if (!info->xram)
761                 goto emapxram;
762         info->yram = ioremap(res->start + YRAM_OFFSET, YRAM_SIZE);
763         if (!info->yram)
764                 goto emapyram;
765         info->reg = ioremap(res->start + REG_OFFSET, resource_size(res) -
766                             REG_OFFSET);
767         if (!info->reg)
768                 goto emapreg;
769
770         siu_i2s_dai.dev = &pdev->dev;
771         siu_i2s_dai.private_data = info;
772
773         ret = snd_soc_register_dais(&siu_i2s_dai, 1);
774         if (ret < 0)
775                 goto edaiinit;
776
777         ret = snd_soc_register_platform(&siu_platform);
778         if (ret < 0)
779                 goto esocregp;
780
781         pm_runtime_enable(&pdev->dev);
782
783         return ret;
784
785 esocregp:
786         snd_soc_unregister_dais(&siu_i2s_dai, 1);
787 edaiinit:
788         iounmap(info->reg);
789 emapreg:
790         iounmap(info->yram);
791 emapyram:
792         iounmap(info->xram);
793 emapxram:
794         iounmap(info->pram);
795 emappram:
796         release_mem_region(res->start, resource_size(res));
797 ereqmemreg:
798 egetres:
799 ereqfw:
800         kfree(info);
801
802         return ret;
803 }
804
805 static int __devexit siu_remove(struct platform_device *pdev)
806 {
807         struct siu_info *info = siu_i2s_dai.private_data;
808         struct resource *res;
809
810         pm_runtime_disable(&pdev->dev);
811
812         snd_soc_unregister_platform(&siu_platform);
813         snd_soc_unregister_dais(&siu_i2s_dai, 1);
814
815         iounmap(info->reg);
816         iounmap(info->yram);
817         iounmap(info->xram);
818         iounmap(info->pram);
819         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
820         if (res)
821                 release_mem_region(res->start, resource_size(res));
822         kfree(info);
823
824         return 0;
825 }
826
827 static struct platform_driver siu_driver = {
828         .driver         = {
829                 .name   = "sh_siu",
830         },
831         .probe          = siu_probe,
832         .remove         = __devexit_p(siu_remove),
833 };
834
835 static int __init siu_init(void)
836 {
837         return platform_driver_register(&siu_driver);
838 }
839
840 static void __exit siu_exit(void)
841 {
842         platform_driver_unregister(&siu_driver);
843 }
844
845 module_init(siu_init)
846 module_exit(siu_exit)
847
848 MODULE_AUTHOR("Carlos Munoz <carlos@kenati.com>");
849 MODULE_DESCRIPTION("ALSA SoC SH7722 SIU driver");
850 MODULE_LICENSE("GPL");