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