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