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