blob: 110577c523179c3974a2ebe45ef577ea651aa5ce [file] [log] [blame]
Kuninori Morimoto1536a962013-07-21 21:35:52 -07001/*
2 * Renesas R-Car SRU/SCU/SSIU/SSI support
3 *
4 * Copyright (C) 2013 Renesas Solutions Corp.
5 * Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
6 *
7 * Based on fsi.c
8 * Kuninori Morimoto <morimoto.kuninori@renesas.com>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
13 */
14
15/*
16 * Renesas R-Car sound device structure
17 *
18 * Gen1
19 *
20 * SRU : Sound Routing Unit
21 * - SRC : Sampling Rate Converter
22 * - CMD
23 * - CTU : Channel Count Conversion Unit
24 * - MIX : Mixer
25 * - DVC : Digital Volume and Mute Function
26 * - SSI : Serial Sound Interface
27 *
28 * Gen2
29 *
30 * SCU : Sampling Rate Converter Unit
31 * - SRC : Sampling Rate Converter
32 * - CMD
33 * - CTU : Channel Count Conversion Unit
34 * - MIX : Mixer
35 * - DVC : Digital Volume and Mute Function
36 * SSIU : Serial Sound Interface Unit
37 * - SSI : Serial Sound Interface
38 */
39
40/*
41 * driver data Image
42 *
43 * rsnd_priv
44 * |
45 * | ** this depends on Gen1/Gen2
46 * |
47 * +- gen
48 * |
49 * | ** these depend on data path
50 * | ** gen and platform data control it
51 * |
52 * +- rdai[0]
53 * | | sru ssiu ssi
54 * | +- playback -> [mod] -> [mod] -> [mod] -> ...
55 * | |
56 * | | sru ssiu ssi
57 * | +- capture -> [mod] -> [mod] -> [mod] -> ...
58 * |
59 * +- rdai[1]
60 * | | sru ssiu ssi
61 * | +- playback -> [mod] -> [mod] -> [mod] -> ...
62 * | |
63 * | | sru ssiu ssi
64 * | +- capture -> [mod] -> [mod] -> [mod] -> ...
65 * ...
66 * |
67 * | ** these control ssi
68 * |
69 * +- ssi
70 * | |
71 * | +- ssi[0]
72 * | +- ssi[1]
73 * | +- ssi[2]
74 * | ...
75 * |
Kuninori Morimotoba9c9492014-03-03 20:51:21 -080076 * | ** these control src
Kuninori Morimoto1536a962013-07-21 21:35:52 -070077 * |
Kuninori Morimotoba9c9492014-03-03 20:51:21 -080078 * +- src
Kuninori Morimoto1536a962013-07-21 21:35:52 -070079 * |
Kuninori Morimotoba9c9492014-03-03 20:51:21 -080080 * +- src[0]
81 * +- src[1]
82 * +- src[2]
Kuninori Morimoto1536a962013-07-21 21:35:52 -070083 * ...
84 *
85 *
86 * for_each_rsnd_dai(xx, priv, xx)
87 * rdai[0] => rdai[1] => rdai[2] => ...
88 *
89 * for_each_rsnd_mod(xx, rdai, xx)
90 * [mod] => [mod] => [mod] => ...
91 *
92 * rsnd_dai_call(xxx, fn )
93 * [mod]->fn() -> [mod]->fn() -> [mod]->fn()...
94 *
95 */
96#include <linux/pm_runtime.h>
Kuninori Morimoto9ade09d2013-10-29 00:52:19 -070097#include <linux/shdma-base.h>
Kuninori Morimoto1536a962013-07-21 21:35:52 -070098#include "rsnd.h"
99
100#define RSND_RATES SNDRV_PCM_RATE_8000_96000
101#define RSND_FMTS (SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S16_LE)
102
Kuninori Morimoto90e8e502014-03-17 19:29:55 -0700103static struct rsnd_of_data rsnd_of_data_gen1 = {
104 .flags = RSND_GEN1,
105};
106
107static struct rsnd_of_data rsnd_of_data_gen2 = {
108 .flags = RSND_GEN2,
109};
110
111static struct of_device_id rsnd_of_match[] = {
112 { .compatible = "renesas,rcar_sound-gen1", .data = &rsnd_of_data_gen1 },
113 { .compatible = "renesas,rcar_sound-gen2", .data = &rsnd_of_data_gen2 },
114 {},
115};
116MODULE_DEVICE_TABLE(of, rsnd_of_match);
117
Kuninori Morimoto1536a962013-07-21 21:35:52 -0700118/*
119 * rsnd_platform functions
120 */
121#define rsnd_platform_call(priv, dai, func, param...) \
Kuninori Morimoto740ad6c2013-10-11 00:06:34 -0700122 (!(priv->info->func) ? 0 : \
Kuninori Morimoto1536a962013-07-21 21:35:52 -0700123 priv->info->func(param))
124
Kuninori Morimoto389933d2014-03-03 20:50:00 -0800125#define rsnd_is_enable_path(io, name) \
126 ((io)->info ? (io)->info->name : NULL)
127#define rsnd_info_id(priv, io, name) \
128 ((io)->info->name - priv->info->name##_info)
129
Kuninori Morimoto33377442013-07-21 21:36:21 -0700130/*
Kuninori Morimotocdaa3cd2013-07-21 21:36:03 -0700131 * rsnd_mod functions
132 */
133char *rsnd_mod_name(struct rsnd_mod *mod)
134{
135 if (!mod || !mod->ops)
136 return "unknown";
137
138 return mod->ops->name;
139}
140
Kuninori Morimotod9288d02014-06-22 17:56:23 -0700141char *rsnd_mod_dma_name(struct rsnd_mod *mod)
142{
143 if (!mod || !mod->ops)
144 return "unknown";
145
146 if (!mod->ops->dma_name)
147 return mod->ops->name;
148
149 return mod->ops->dma_name(mod);
150}
151
Kuninori Morimoto1b13d1182015-01-15 08:08:34 +0000152void rsnd_mod_init(struct rsnd_mod *mod,
Kuninori Morimotocdaa3cd2013-07-21 21:36:03 -0700153 struct rsnd_mod_ops *ops,
Kuninori Morimoto85642952015-01-15 08:03:22 +0000154 struct clk *clk,
Kuninori Morimotoa1260212014-03-02 23:42:55 -0800155 enum rsnd_mod_type type,
Kuninori Morimotocdaa3cd2013-07-21 21:36:03 -0700156 int id)
157{
Kuninori Morimotocdaa3cd2013-07-21 21:36:03 -0700158 mod->id = id;
159 mod->ops = ops;
Kuninori Morimotoa1260212014-03-02 23:42:55 -0800160 mod->type = type;
Kuninori Morimoto85642952015-01-15 08:03:22 +0000161 mod->clk = clk;
Kuninori Morimotocdaa3cd2013-07-21 21:36:03 -0700162}
163
164/*
Kuninori Morimoto0a4d94c2013-07-28 18:58:50 -0700165 * rsnd_dma functions
166 */
Kuninori Morimoto0a4d94c2013-07-28 18:58:50 -0700167void rsnd_dma_stop(struct rsnd_dma *dma)
168{
Kuninori Morimoto0a4d94c2013-07-28 18:58:50 -0700169 dmaengine_terminate_all(dma->chan);
170}
171
172static void rsnd_dma_complete(void *data)
173{
174 struct rsnd_dma *dma = (struct rsnd_dma *)data;
Kuninori Morimoto4686a0a2014-01-23 18:41:44 -0800175 struct rsnd_mod *mod = rsnd_dma_to_mod(dma);
Kuninori Morimoto4686a0a2014-01-23 18:41:44 -0800176 struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
Kuninori Morimoto0a4d94c2013-07-28 18:58:50 -0700177
Kuninori Morimoto4686a0a2014-01-23 18:41:44 -0800178 /*
179 * Renesas sound Gen1 needs 1 DMAC,
180 * Gen2 needs 2 DMAC.
181 * In Gen2 case, it are Audio-DMAC, and Audio-DMAC-peri-peri.
182 * But, Audio-DMAC-peri-peri doesn't have interrupt,
183 * and this driver is assuming that here.
184 *
185 * If Audio-DMAC-peri-peri has interrpt,
186 * rsnd_dai_pointer_update() will be called twice,
187 * ant it will breaks io->byte_pos
188 */
Kuninori Morimoto836b31f2014-04-23 14:59:12 +0900189
190 rsnd_dai_pointer_update(io, io->byte_per_period);
Kuninori Morimoto0a4d94c2013-07-28 18:58:50 -0700191}
192
Kuninori Morimotoccd01552014-06-22 17:56:41 -0700193void rsnd_dma_start(struct rsnd_dma *dma)
Kuninori Morimoto0a4d94c2013-07-28 18:58:50 -0700194{
Kuninori Morimoto4686a0a2014-01-23 18:41:44 -0800195 struct rsnd_mod *mod = rsnd_dma_to_mod(dma);
196 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
197 struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
Kuninori Morimotoccd01552014-06-22 17:56:41 -0700198 struct snd_pcm_substream *substream = io->substream;
Kuninori Morimoto0a4d94c2013-07-28 18:58:50 -0700199 struct device *dev = rsnd_priv_to_dev(priv);
200 struct dma_async_tx_descriptor *desc;
Kuninori Morimoto0a4d94c2013-07-28 18:58:50 -0700201
Kuninori Morimotoccd01552014-06-22 17:56:41 -0700202 desc = dmaengine_prep_dma_cyclic(dma->chan,
Kuninori Morimoto34037102014-06-22 17:59:02 -0700203 (dma->addr) ? dma->addr :
Kuninori Morimotoccd01552014-06-22 17:56:41 -0700204 substream->runtime->dma_addr,
205 snd_pcm_lib_buffer_bytes(substream),
206 snd_pcm_lib_period_bytes(substream),
207 dma->dir,
208 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
Kuninori Morimoto0a4d94c2013-07-28 18:58:50 -0700209
Kuninori Morimotoccd01552014-06-22 17:56:41 -0700210 if (!desc) {
211 dev_err(dev, "dmaengine_prep_slave_sg() fail\n");
212 return;
Kuninori Morimoto0a4d94c2013-07-28 18:58:50 -0700213 }
Kuninori Morimoto0a4d94c2013-07-28 18:58:50 -0700214
Kuninori Morimotoccd01552014-06-22 17:56:41 -0700215 desc->callback = rsnd_dma_complete;
216 desc->callback_param = dma;
Kuninori Morimotof5cab3b2014-01-23 18:40:27 -0800217
Kuninori Morimotoccd01552014-06-22 17:56:41 -0700218 if (dmaengine_submit(desc) < 0) {
219 dev_err(dev, "dmaengine_submit() fail\n");
220 return;
221 }
222
223 dma_async_issue_pending(dma->chan);
Kuninori Morimotof5cab3b2014-01-23 18:40:27 -0800224}
225
Kuninori Morimoto0a4d94c2013-07-28 18:58:50 -0700226int rsnd_dma_available(struct rsnd_dma *dma)
227{
228 return !!dma->chan;
229}
230
Kuninori Morimoto199e76882014-05-22 23:25:49 -0700231#define DMA_NAME_SIZE 16
232#define MOD_MAX 4 /* MEM/SSI/SRC/DVC */
233static int _rsnd_dma_of_name(char *dma_name, struct rsnd_mod *mod)
234{
235 if (mod)
236 return snprintf(dma_name, DMA_NAME_SIZE / 2, "%s%d",
Kuninori Morimotod9288d02014-06-22 17:56:23 -0700237 rsnd_mod_dma_name(mod), rsnd_mod_id(mod));
Kuninori Morimoto199e76882014-05-22 23:25:49 -0700238 else
239 return snprintf(dma_name, DMA_NAME_SIZE / 2, "mem");
240
241}
242
Kuninori Morimoto37523032014-06-22 17:58:26 -0700243static void rsnd_dma_of_name(struct rsnd_mod *mod_from,
244 struct rsnd_mod *mod_to,
245 char *dma_name)
246{
247 int index = 0;
248
249 index = _rsnd_dma_of_name(dma_name + index, mod_from);
250 *(dma_name + index++) = '_';
251 index = _rsnd_dma_of_name(dma_name + index, mod_to);
252}
253
254static void rsnd_dma_of_path(struct rsnd_dma *dma,
255 int is_play,
256 struct rsnd_mod **mod_from,
257 struct rsnd_mod **mod_to)
Kuninori Morimoto199e76882014-05-22 23:25:49 -0700258{
259 struct rsnd_mod *this = rsnd_dma_to_mod(dma);
260 struct rsnd_dai_stream *io = rsnd_mod_to_io(this);
261 struct rsnd_mod *ssi = rsnd_io_to_mod_ssi(io);
262 struct rsnd_mod *src = rsnd_io_to_mod_src(io);
263 struct rsnd_mod *dvc = rsnd_io_to_mod_dvc(io);
264 struct rsnd_mod *mod[MOD_MAX];
Kuninori Morimoto199e76882014-05-22 23:25:49 -0700265 int i, index;
266
267
268 for (i = 0; i < MOD_MAX; i++)
269 mod[i] = NULL;
270
271 /*
272 * in play case...
273 *
274 * src -> dst
275 *
276 * mem -> SSI
277 * mem -> SRC -> SSI
278 * mem -> SRC -> DVC -> SSI
279 */
280 mod[0] = NULL; /* for "mem" */
281 index = 1;
282 for (i = 1; i < MOD_MAX; i++) {
283 if (!src) {
284 mod[i] = ssi;
Kuninori Morimoto199e76882014-05-22 23:25:49 -0700285 } else if (!dvc) {
286 mod[i] = src;
287 src = NULL;
288 } else {
Kuninori Morimoto34cb6122014-06-22 17:59:28 -0700289 if ((!is_play) && (this == src))
290 this = dvc;
291
292 mod[i] = (is_play) ? src : dvc;
293 i++;
294 mod[i] = (is_play) ? dvc : src;
295 src = NULL;
Kuninori Morimoto199e76882014-05-22 23:25:49 -0700296 dvc = NULL;
297 }
298
299 if (mod[i] == this)
300 index = i;
Kuninori Morimotoc08c3b02014-06-18 17:55:09 +0900301
302 if (mod[i] == ssi)
303 break;
Kuninori Morimoto199e76882014-05-22 23:25:49 -0700304 }
305
306 if (is_play) {
Kuninori Morimoto37523032014-06-22 17:58:26 -0700307 *mod_from = mod[index - 1];
308 *mod_to = mod[index];
Kuninori Morimoto199e76882014-05-22 23:25:49 -0700309 } else {
Kuninori Morimoto37523032014-06-22 17:58:26 -0700310 *mod_from = mod[index];
311 *mod_to = mod[index - 1];
Kuninori Morimoto199e76882014-05-22 23:25:49 -0700312 }
Kuninori Morimoto199e76882014-05-22 23:25:49 -0700313}
314
Kuninori Morimoto0a4d94c2013-07-28 18:58:50 -0700315int rsnd_dma_init(struct rsnd_priv *priv, struct rsnd_dma *dma,
Kuninori Morimoto4686a0a2014-01-23 18:41:44 -0800316 int is_play, int id)
Kuninori Morimoto0a4d94c2013-07-28 18:58:50 -0700317{
318 struct device *dev = rsnd_priv_to_dev(priv);
Kuninori Morimoto9ade09d2013-10-29 00:52:19 -0700319 struct dma_slave_config cfg;
Kuninori Morimoto37523032014-06-22 17:58:26 -0700320 struct rsnd_mod *mod_from;
321 struct rsnd_mod *mod_to;
Kuninori Morimoto199e76882014-05-22 23:25:49 -0700322 char dma_name[DMA_NAME_SIZE];
Kuninori Morimoto0a4d94c2013-07-28 18:58:50 -0700323 dma_cap_mask_t mask;
Kuninori Morimoto9ade09d2013-10-29 00:52:19 -0700324 int ret;
Kuninori Morimoto0a4d94c2013-07-28 18:58:50 -0700325
326 if (dma->chan) {
327 dev_err(dev, "it already has dma channel\n");
328 return -EIO;
329 }
330
331 dma_cap_zero(mask);
332 dma_cap_set(DMA_SLAVE, mask);
333
Kuninori Morimoto37523032014-06-22 17:58:26 -0700334 rsnd_dma_of_path(dma, is_play, &mod_from, &mod_to);
335 rsnd_dma_of_name(mod_from, mod_to, dma_name);
Kuninori Morimoto199e76882014-05-22 23:25:49 -0700336
Kuninori Morimoto37523032014-06-22 17:58:26 -0700337 cfg.slave_id = id;
338 cfg.direction = is_play ? DMA_MEM_TO_DEV : DMA_DEV_TO_MEM;
339 cfg.src_addr = rsnd_gen_dma_addr(priv, mod_from, is_play, 1);
340 cfg.dst_addr = rsnd_gen_dma_addr(priv, mod_to, is_play, 0);
Kuninori Morimoto2bf865b2014-07-16 23:18:44 -0700341 cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
342 cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
Kuninori Morimoto37523032014-06-22 17:58:26 -0700343
344 dev_dbg(dev, "dma : %s %pad -> %pad\n",
345 dma_name, &cfg.src_addr, &cfg.dst_addr);
Kuninori Morimoto199e76882014-05-22 23:25:49 -0700346
Kuninori Morimoto9ade09d2013-10-29 00:52:19 -0700347 dma->chan = dma_request_slave_channel_compat(mask, shdma_chan_filter,
348 (void *)id, dev,
Kuninori Morimoto199e76882014-05-22 23:25:49 -0700349 dma_name);
Kuninori Morimoto0a4d94c2013-07-28 18:58:50 -0700350 if (!dma->chan) {
351 dev_err(dev, "can't get dma channel\n");
Kuninori Morimotod3a76822014-11-09 20:00:58 -0800352 goto rsnd_dma_channel_err;
Kuninori Morimoto0a4d94c2013-07-28 18:58:50 -0700353 }
354
Kuninori Morimoto9ade09d2013-10-29 00:52:19 -0700355 ret = dmaengine_slave_config(dma->chan, &cfg);
356 if (ret < 0)
357 goto rsnd_dma_init_err;
358
Kuninori Morimoto34037102014-06-22 17:59:02 -0700359 dma->addr = is_play ? cfg.src_addr : cfg.dst_addr;
Lars-Peter Clausencd7bcc62014-06-19 09:40:30 +0200360 dma->dir = is_play ? DMA_MEM_TO_DEV : DMA_DEV_TO_MEM;
Kuninori Morimoto0a4d94c2013-07-28 18:58:50 -0700361
362 return 0;
Kuninori Morimoto9ade09d2013-10-29 00:52:19 -0700363
364rsnd_dma_init_err:
365 rsnd_dma_quit(priv, dma);
Kuninori Morimotod3a76822014-11-09 20:00:58 -0800366rsnd_dma_channel_err:
Kuninori Morimoto9ade09d2013-10-29 00:52:19 -0700367
Kuninori Morimotod3a76822014-11-09 20:00:58 -0800368 /*
369 * DMA failed. try to PIO mode
370 * see
Kuninori Morimoto97463e192014-11-27 08:02:43 +0000371 * rsnd_ssi_fallback()
Kuninori Morimotod3a76822014-11-09 20:00:58 -0800372 * rsnd_rdai_continuance_probe()
373 */
374 return -EAGAIN;
Kuninori Morimoto0a4d94c2013-07-28 18:58:50 -0700375}
376
377void rsnd_dma_quit(struct rsnd_priv *priv,
378 struct rsnd_dma *dma)
379{
380 if (dma->chan)
381 dma_release_channel(dma->chan);
382
383 dma->chan = NULL;
384}
385
386/*
Kuninori Morimotod7bdbc52014-05-08 17:44:14 -0700387 * settting function
388 */
389u32 rsnd_get_adinr(struct rsnd_mod *mod)
390{
391 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
392 struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
393 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
394 struct device *dev = rsnd_priv_to_dev(priv);
395 u32 adinr = runtime->channels;
396
397 switch (runtime->sample_bits) {
398 case 16:
399 adinr |= (8 << 16);
400 break;
401 case 32:
402 adinr |= (0 << 16);
403 break;
404 default:
405 dev_warn(dev, "not supported sample bits\n");
406 return 0;
407 }
408
409 return adinr;
410}
411
412/*
Kuninori Morimoto1536a962013-07-21 21:35:52 -0700413 * rsnd_dai functions
414 */
Kuninori Morimoto690602f2015-01-15 08:07:47 +0000415#define __rsnd_mod_call(mod, func, param...) \
Kuninori Morimotod870a91e2014-02-24 22:14:41 -0800416({ \
417 struct rsnd_priv *priv = rsnd_mod_to_priv(mod); \
418 struct device *dev = rsnd_priv_to_dev(priv); \
Kuninori Morimoto417f9642014-11-27 08:02:55 +0000419 u32 mask = 1 << __rsnd_mod_shift_##func; \
420 u32 call = __rsnd_mod_call_##func << __rsnd_mod_shift_##func; \
421 int ret = 0; \
422 if ((mod->status & mask) == call) { \
423 dev_dbg(dev, "%s[%d] %s\n", \
424 rsnd_mod_name(mod), rsnd_mod_id(mod), #func); \
Kuninori Morimoto690602f2015-01-15 08:07:47 +0000425 ret = (mod)->ops->func(mod, param); \
Kuninori Morimoto417f9642014-11-27 08:02:55 +0000426 mod->status = (mod->status & ~mask) | (~call & mask); \
427 } \
428 ret; \
Kuninori Morimotocdaa3cd2013-07-21 21:36:03 -0700429})
430
Kuninori Morimoto690602f2015-01-15 08:07:47 +0000431#define rsnd_mod_call(mod, func, param...) \
Kuninori Morimotod870a91e2014-02-24 22:14:41 -0800432 (!(mod) ? -ENODEV : \
433 !((mod)->ops->func) ? 0 : \
Kuninori Morimoto690602f2015-01-15 08:07:47 +0000434 __rsnd_mod_call(mod, func, param))
Kuninori Morimotod870a91e2014-02-24 22:14:41 -0800435
Kuninori Morimoto690602f2015-01-15 08:07:47 +0000436#define rsnd_dai_call(fn, io, param...) \
Kuninori Morimotod870a91e2014-02-24 22:14:41 -0800437({ \
Kuninori Morimotoa1260212014-03-02 23:42:55 -0800438 struct rsnd_mod *mod; \
439 int ret = 0, i; \
440 for (i = 0; i < RSND_MOD_MAX; i++) { \
441 mod = (io)->mod[i]; \
442 if (!mod) \
443 continue; \
Kuninori Morimoto690602f2015-01-15 08:07:47 +0000444 ret = rsnd_mod_call(mod, fn, param); \
Kuninori Morimotod870a91e2014-02-24 22:14:41 -0800445 if (ret < 0) \
446 break; \
447 } \
448 ret; \
Kuninori Morimotocdaa3cd2013-07-21 21:36:03 -0700449})
450
Kuninori Morimotoa1260212014-03-02 23:42:55 -0800451static int rsnd_dai_connect(struct rsnd_mod *mod,
Kuninori Morimoto9bfed6c2014-03-02 23:42:47 -0800452 struct rsnd_dai_stream *io)
Kuninori Morimotocdaa3cd2013-07-21 21:36:03 -0700453{
Kuninori Morimoto60207792013-11-10 17:00:42 -0800454 if (!mod)
Kuninori Morimotocdaa3cd2013-07-21 21:36:03 -0700455 return -EIO;
Kuninori Morimotocdaa3cd2013-07-21 21:36:03 -0700456
Kuninori Morimotoa1260212014-03-02 23:42:55 -0800457 if (io->mod[mod->type]) {
Kuninori Morimoto60207792013-11-10 17:00:42 -0800458 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
459 struct device *dev = rsnd_priv_to_dev(priv);
460
Kuninori Morimotocdaa3cd2013-07-21 21:36:03 -0700461 dev_err(dev, "%s%d is not empty\n",
462 rsnd_mod_name(mod),
463 rsnd_mod_id(mod));
464 return -EIO;
465 }
466
Kuninori Morimotoa1260212014-03-02 23:42:55 -0800467 io->mod[mod->type] = mod;
Kuninori Morimoto4686a0a2014-01-23 18:41:44 -0800468 mod->io = io;
Kuninori Morimotocdaa3cd2013-07-21 21:36:03 -0700469
470 return 0;
471}
472
Kuninori Morimotod3a76822014-11-09 20:00:58 -0800473static void rsnd_dai_disconnect(struct rsnd_mod *mod,
474 struct rsnd_dai_stream *io)
475{
476 mod->io = NULL;
477 io->mod[mod->type] = NULL;
478}
479
Kuninori Morimoto710d0882015-01-15 08:03:38 +0000480struct rsnd_dai *rsnd_rdai_get(struct rsnd_priv *priv, int id)
Kuninori Morimoto1536a962013-07-21 21:35:52 -0700481{
Kuninori Morimotoecba9e72014-02-24 22:15:18 -0800482 if ((id < 0) || (id >= rsnd_rdai_nr(priv)))
Kuninori Morimoto2192f812013-10-11 00:07:48 -0700483 return NULL;
484
Kuninori Morimoto1536a962013-07-21 21:35:52 -0700485 return priv->rdai + id;
486}
487
488static struct rsnd_dai *rsnd_dai_to_rdai(struct snd_soc_dai *dai)
489{
490 struct rsnd_priv *priv = snd_soc_dai_get_drvdata(dai);
491
Kuninori Morimoto710d0882015-01-15 08:03:38 +0000492 return rsnd_rdai_get(priv, dai->id);
Kuninori Morimoto1536a962013-07-21 21:35:52 -0700493}
494
Kuninori Morimoto1536a962013-07-21 21:35:52 -0700495/*
496 * rsnd_soc_dai functions
497 */
498int rsnd_dai_pointer_offset(struct rsnd_dai_stream *io, int additional)
499{
500 struct snd_pcm_substream *substream = io->substream;
501 struct snd_pcm_runtime *runtime = substream->runtime;
502 int pos = io->byte_pos + additional;
503
504 pos %= (runtime->periods * io->byte_per_period);
505
506 return pos;
507}
508
509void rsnd_dai_pointer_update(struct rsnd_dai_stream *io, int byte)
510{
511 io->byte_pos += byte;
512
513 if (io->byte_pos >= io->next_period_byte) {
514 struct snd_pcm_substream *substream = io->substream;
515 struct snd_pcm_runtime *runtime = substream->runtime;
516
517 io->period_pos++;
518 io->next_period_byte += io->byte_per_period;
519
520 if (io->period_pos >= runtime->periods) {
521 io->byte_pos = 0;
522 io->period_pos = 0;
523 io->next_period_byte = io->byte_per_period;
524 }
525
526 snd_pcm_period_elapsed(substream);
527 }
528}
529
530static int rsnd_dai_stream_init(struct rsnd_dai_stream *io,
531 struct snd_pcm_substream *substream)
532{
533 struct snd_pcm_runtime *runtime = substream->runtime;
534
Kuninori Morimoto1536a962013-07-21 21:35:52 -0700535 io->substream = substream;
536 io->byte_pos = 0;
537 io->period_pos = 0;
538 io->byte_per_period = runtime->period_size *
539 runtime->channels *
540 samples_to_bytes(runtime, 1);
541 io->next_period_byte = io->byte_per_period;
542
543 return 0;
544}
545
546static
547struct snd_soc_dai *rsnd_substream_to_dai(struct snd_pcm_substream *substream)
548{
549 struct snd_soc_pcm_runtime *rtd = substream->private_data;
550
551 return rtd->cpu_dai;
552}
553
554static
555struct rsnd_dai_stream *rsnd_rdai_to_io(struct rsnd_dai *rdai,
556 struct snd_pcm_substream *substream)
557{
558 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
559 return &rdai->playback;
560 else
561 return &rdai->capture;
562}
563
564static int rsnd_soc_dai_trigger(struct snd_pcm_substream *substream, int cmd,
565 struct snd_soc_dai *dai)
566{
567 struct rsnd_priv *priv = snd_soc_dai_get_drvdata(dai);
568 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
569 struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
Kuninori Morimoto29e69fd2014-05-08 01:59:26 -0700570 int ssi_id = rsnd_mod_id(rsnd_io_to_mod_ssi(io));
Kuninori Morimoto1536a962013-07-21 21:35:52 -0700571 int ret;
572 unsigned long flags;
573
574 rsnd_lock(priv, flags);
575
576 switch (cmd) {
577 case SNDRV_PCM_TRIGGER_START:
578 ret = rsnd_dai_stream_init(io, substream);
579 if (ret < 0)
580 goto dai_trigger_end;
581
582 ret = rsnd_platform_call(priv, dai, start, ssi_id);
583 if (ret < 0)
584 goto dai_trigger_end;
585
Kuninori Morimoto690602f2015-01-15 08:07:47 +0000586 ret = rsnd_dai_call(init, io, priv);
Kuninori Morimotocdaa3cd2013-07-21 21:36:03 -0700587 if (ret < 0)
588 goto dai_trigger_end;
589
Kuninori Morimoto690602f2015-01-15 08:07:47 +0000590 ret = rsnd_dai_call(start, io, priv);
Kuninori Morimotocdaa3cd2013-07-21 21:36:03 -0700591 if (ret < 0)
592 goto dai_trigger_end;
Kuninori Morimoto1536a962013-07-21 21:35:52 -0700593 break;
594 case SNDRV_PCM_TRIGGER_STOP:
Kuninori Morimoto690602f2015-01-15 08:07:47 +0000595 ret = rsnd_dai_call(stop, io, priv);
Kuninori Morimotocdaa3cd2013-07-21 21:36:03 -0700596 if (ret < 0)
597 goto dai_trigger_end;
598
Kuninori Morimoto690602f2015-01-15 08:07:47 +0000599 ret = rsnd_dai_call(quit, io, priv);
Kuninori Morimotocdaa3cd2013-07-21 21:36:03 -0700600 if (ret < 0)
601 goto dai_trigger_end;
602
Kuninori Morimoto33377442013-07-21 21:36:21 -0700603 ret = rsnd_platform_call(priv, dai, stop, ssi_id);
604 if (ret < 0)
605 goto dai_trigger_end;
Kuninori Morimoto1536a962013-07-21 21:35:52 -0700606 break;
607 default:
608 ret = -EINVAL;
609 }
610
611dai_trigger_end:
612 rsnd_unlock(priv, flags);
613
614 return ret;
615}
616
617static int rsnd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
618{
619 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
620
621 /* set master/slave audio interface */
622 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
623 case SND_SOC_DAIFMT_CBM_CFM:
Kuninori Morimotoe1508282014-03-13 17:56:43 -0700624 rdai->clk_master = 0;
Kuninori Morimoto1536a962013-07-21 21:35:52 -0700625 break;
626 case SND_SOC_DAIFMT_CBS_CFS:
Kuninori Morimotoe1508282014-03-13 17:56:43 -0700627 rdai->clk_master = 1; /* codec is slave, cpu is master */
Kuninori Morimoto1536a962013-07-21 21:35:52 -0700628 break;
629 default:
630 return -EINVAL;
631 }
632
Kuninori Morimoto1536a962013-07-21 21:35:52 -0700633 /* set format */
634 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
635 case SND_SOC_DAIFMT_I2S:
636 rdai->sys_delay = 0;
637 rdai->data_alignment = 0;
Kuninori Morimoto1a7889c2014-07-31 18:08:18 -0700638 rdai->frm_clk_inv = 0;
Kuninori Morimoto1536a962013-07-21 21:35:52 -0700639 break;
640 case SND_SOC_DAIFMT_LEFT_J:
641 rdai->sys_delay = 1;
642 rdai->data_alignment = 0;
Kuninori Morimoto1a7889c2014-07-31 18:08:18 -0700643 rdai->frm_clk_inv = 1;
Kuninori Morimoto1536a962013-07-21 21:35:52 -0700644 break;
645 case SND_SOC_DAIFMT_RIGHT_J:
646 rdai->sys_delay = 1;
647 rdai->data_alignment = 1;
Kuninori Morimoto1a7889c2014-07-31 18:08:18 -0700648 rdai->frm_clk_inv = 1;
649 break;
650 }
651
652 /* set clock inversion */
653 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
654 case SND_SOC_DAIFMT_NB_IF:
655 rdai->bit_clk_inv = rdai->bit_clk_inv;
656 rdai->frm_clk_inv = !rdai->frm_clk_inv;
657 break;
658 case SND_SOC_DAIFMT_IB_NF:
659 rdai->bit_clk_inv = !rdai->bit_clk_inv;
660 rdai->frm_clk_inv = rdai->frm_clk_inv;
661 break;
662 case SND_SOC_DAIFMT_IB_IF:
663 rdai->bit_clk_inv = !rdai->bit_clk_inv;
664 rdai->frm_clk_inv = !rdai->frm_clk_inv;
665 break;
666 case SND_SOC_DAIFMT_NB_NF:
667 default:
Kuninori Morimoto1536a962013-07-21 21:35:52 -0700668 break;
669 }
670
671 return 0;
672}
673
674static const struct snd_soc_dai_ops rsnd_soc_dai_ops = {
675 .trigger = rsnd_soc_dai_trigger,
676 .set_fmt = rsnd_soc_dai_set_fmt,
677};
678
Kuninori Morimoto739f9502014-05-08 17:43:26 -0700679#define rsnd_path_parse(priv, io, type) \
680({ \
681 struct rsnd_mod *mod; \
682 int ret = 0; \
683 int id = -1; \
684 \
685 if (rsnd_is_enable_path(io, type)) { \
686 id = rsnd_info_id(priv, io, type); \
687 if (id >= 0) { \
688 mod = rsnd_##type##_mod_get(priv, id); \
689 ret = rsnd_dai_connect(mod, io); \
690 } \
691 } \
692 ret; \
693})
694
Kuninori Morimotod3a76822014-11-09 20:00:58 -0800695#define rsnd_path_break(priv, io, type) \
696{ \
697 struct rsnd_mod *mod; \
698 int id = -1; \
699 \
700 if (rsnd_is_enable_path(io, type)) { \
701 id = rsnd_info_id(priv, io, type); \
702 if (id >= 0) { \
703 mod = rsnd_##type##_mod_get(priv, id); \
704 rsnd_dai_disconnect(mod, io); \
705 } \
706 } \
707}
708
Kuninori Morimoto9bfed6c2014-03-02 23:42:47 -0800709static int rsnd_path_init(struct rsnd_priv *priv,
710 struct rsnd_dai *rdai,
711 struct rsnd_dai_stream *io)
712{
Kuninori Morimoto9bfed6c2014-03-02 23:42:47 -0800713 int ret;
Kuninori Morimoto9bfed6c2014-03-02 23:42:47 -0800714
715 /*
716 * Gen1 is created by SRU/SSI, and this SRU is base module of
717 * Gen2's SCU/SSIU/SSI. (Gen2 SCU/SSIU came from SRU)
718 *
719 * Easy image is..
720 * Gen1 SRU = Gen2 SCU + SSIU + etc
721 *
722 * Gen2 SCU path is very flexible, but, Gen1 SRU (SCU parts) is
723 * using fixed path.
Kuninori Morimoto9bfed6c2014-03-02 23:42:47 -0800724 */
Kuninori Morimoto9bfed6c2014-03-02 23:42:47 -0800725
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800726 /* SRC */
Kuninori Morimoto739f9502014-05-08 17:43:26 -0700727 ret = rsnd_path_parse(priv, io, src);
728 if (ret < 0)
729 return ret;
Kuninori Morimoto9bfed6c2014-03-02 23:42:47 -0800730
731 /* SSI */
Kuninori Morimoto739f9502014-05-08 17:43:26 -0700732 ret = rsnd_path_parse(priv, io, ssi);
733 if (ret < 0)
734 return ret;
Kuninori Morimoto9bfed6c2014-03-02 23:42:47 -0800735
Kuninori Morimotobff58ea2014-05-08 17:44:49 -0700736 /* DVC */
737 ret = rsnd_path_parse(priv, io, dvc);
738 if (ret < 0)
739 return ret;
Kuninori Morimoto9bfed6c2014-03-02 23:42:47 -0800740
741 return ret;
742}
743
Kuninori Morimoto90e8e502014-03-17 19:29:55 -0700744static void rsnd_of_parse_dai(struct platform_device *pdev,
745 const struct rsnd_of_data *of_data,
746 struct rsnd_priv *priv)
747{
748 struct device_node *dai_node, *dai_np;
749 struct device_node *ssi_node, *ssi_np;
750 struct device_node *src_node, *src_np;
Kuninori Morimoto34cb6122014-06-22 17:59:28 -0700751 struct device_node *dvc_node, *dvc_np;
Kuninori Morimoto90e8e502014-03-17 19:29:55 -0700752 struct device_node *playback, *capture;
753 struct rsnd_dai_platform_info *dai_info;
754 struct rcar_snd_info *info = rsnd_priv_to_info(priv);
755 struct device *dev = &pdev->dev;
756 int nr, i;
Kuninori Morimoto34cb6122014-06-22 17:59:28 -0700757 int dai_i, ssi_i, src_i, dvc_i;
Kuninori Morimoto90e8e502014-03-17 19:29:55 -0700758
759 if (!of_data)
760 return;
761
762 dai_node = of_get_child_by_name(dev->of_node, "rcar_sound,dai");
763 if (!dai_node)
764 return;
765
766 nr = of_get_child_count(dai_node);
767 if (!nr)
768 return;
769
770 dai_info = devm_kzalloc(dev,
771 sizeof(struct rsnd_dai_platform_info) * nr,
772 GFP_KERNEL);
773 if (!dai_info) {
774 dev_err(dev, "dai info allocation error\n");
775 return;
776 }
777
778 info->dai_info_nr = nr;
779 info->dai_info = dai_info;
780
781 ssi_node = of_get_child_by_name(dev->of_node, "rcar_sound,ssi");
782 src_node = of_get_child_by_name(dev->of_node, "rcar_sound,src");
Kuninori Morimoto34cb6122014-06-22 17:59:28 -0700783 dvc_node = of_get_child_by_name(dev->of_node, "rcar_sound,dvc");
Kuninori Morimoto90e8e502014-03-17 19:29:55 -0700784
785#define mod_parse(name) \
786if (name##_node) { \
787 struct rsnd_##name##_platform_info *name##_info; \
788 \
789 name##_i = 0; \
790 for_each_child_of_node(name##_node, name##_np) { \
791 name##_info = info->name##_info + name##_i; \
792 \
793 if (name##_np == playback) \
794 dai_info->playback.name = name##_info; \
795 if (name##_np == capture) \
796 dai_info->capture.name = name##_info; \
797 \
798 name##_i++; \
799 } \
800}
801
802 /*
803 * parse all dai
804 */
805 dai_i = 0;
806 for_each_child_of_node(dai_node, dai_np) {
807 dai_info = info->dai_info + dai_i;
808
809 for (i = 0;; i++) {
810
811 playback = of_parse_phandle(dai_np, "playback", i);
812 capture = of_parse_phandle(dai_np, "capture", i);
813
814 if (!playback && !capture)
815 break;
816
817 mod_parse(ssi);
818 mod_parse(src);
Kuninori Morimoto34cb6122014-06-22 17:59:28 -0700819 mod_parse(dvc);
Kuninori Morimoto90e8e502014-03-17 19:29:55 -0700820
Julia Lawalla493b6a2014-08-08 12:07:49 +0200821 of_node_put(playback);
822 of_node_put(capture);
Kuninori Morimoto90e8e502014-03-17 19:29:55 -0700823 }
824
825 dai_i++;
826 }
827}
828
Kuninori Morimoto1536a962013-07-21 21:35:52 -0700829static int rsnd_dai_probe(struct platform_device *pdev,
Kuninori Morimoto90e8e502014-03-17 19:29:55 -0700830 const struct rsnd_of_data *of_data,
Kuninori Morimoto1536a962013-07-21 21:35:52 -0700831 struct rsnd_priv *priv)
832{
833 struct snd_soc_dai_driver *drv;
Kuninori Morimoto78f13d02014-03-03 20:49:50 -0800834 struct rcar_snd_info *info = rsnd_priv_to_info(priv);
Kuninori Morimoto1536a962013-07-21 21:35:52 -0700835 struct rsnd_dai *rdai;
Kuninori Morimoto29e69fd2014-05-08 01:59:26 -0700836 struct rsnd_ssi_platform_info *pmod, *cmod;
Kuninori Morimoto1536a962013-07-21 21:35:52 -0700837 struct device *dev = rsnd_priv_to_dev(priv);
Kuninori Morimoto90e8e502014-03-17 19:29:55 -0700838 int dai_nr;
Kuninori Morimoto4b4dab82013-07-28 18:58:29 -0700839 int i;
840
Kuninori Morimoto90e8e502014-03-17 19:29:55 -0700841 rsnd_of_parse_dai(pdev, of_data, priv);
842
Kuninori Morimoto90e8e502014-03-17 19:29:55 -0700843 dai_nr = info->dai_info_nr;
Kuninori Morimoto78f13d02014-03-03 20:49:50 -0800844 if (!dai_nr) {
Kuninori Morimoto4b4dab82013-07-28 18:58:29 -0700845 dev_err(dev, "no dai\n");
846 return -EIO;
847 }
Kuninori Morimoto1536a962013-07-21 21:35:52 -0700848
849 drv = devm_kzalloc(dev, sizeof(*drv) * dai_nr, GFP_KERNEL);
850 rdai = devm_kzalloc(dev, sizeof(*rdai) * dai_nr, GFP_KERNEL);
851 if (!drv || !rdai) {
852 dev_err(dev, "dai allocate failed\n");
853 return -ENOMEM;
854 }
855
Kuninori Morimotoecba9e72014-02-24 22:15:18 -0800856 priv->rdai_nr = dai_nr;
Kuninori Morimoto49848072014-02-24 22:14:33 -0800857 priv->daidrv = drv;
858 priv->rdai = rdai;
859
Kuninori Morimoto1536a962013-07-21 21:35:52 -0700860 for (i = 0; i < dai_nr; i++) {
Kuninori Morimoto1536a962013-07-21 21:35:52 -0700861
Kuninori Morimoto7c57d762015-01-15 08:05:59 +0000862 pmod = info->dai_info[i].playback.ssi;
863 cmod = info->dai_info[i].capture.ssi;
Kuninori Morimoto1536a962013-07-21 21:35:52 -0700864
865 /*
866 * init rsnd_dai
867 */
Kuninori Morimoto1536a962013-07-21 21:35:52 -0700868 snprintf(rdai[i].name, RSND_DAI_NAME_SIZE, "rsnd-dai.%d", i);
Kuninori Morimoto1b13d1182015-01-15 08:08:34 +0000869 rdai[i].priv = priv;
Kuninori Morimoto1536a962013-07-21 21:35:52 -0700870
871 /*
872 * init snd_soc_dai_driver
873 */
874 drv[i].name = rdai[i].name;
875 drv[i].ops = &rsnd_soc_dai_ops;
Kuninori Morimoto4b4dab82013-07-28 18:58:29 -0700876 if (pmod) {
Kuninori Morimoto1536a962013-07-21 21:35:52 -0700877 drv[i].playback.rates = RSND_RATES;
878 drv[i].playback.formats = RSND_FMTS;
879 drv[i].playback.channels_min = 2;
880 drv[i].playback.channels_max = 2;
Kuninori Morimoto389933d2014-03-03 20:50:00 -0800881
Kuninori Morimoto29e69fd2014-05-08 01:59:26 -0700882 rdai[i].playback.info = &info->dai_info[i].playback;
Kuninori Morimoto54cb5562015-01-15 08:06:24 +0000883 rdai[i].playback.rdai = rdai + i;
Kuninori Morimoto9bfed6c2014-03-02 23:42:47 -0800884 rsnd_path_init(priv, &rdai[i], &rdai[i].playback);
Kuninori Morimoto1536a962013-07-21 21:35:52 -0700885 }
Kuninori Morimoto4b4dab82013-07-28 18:58:29 -0700886 if (cmod) {
Kuninori Morimoto1536a962013-07-21 21:35:52 -0700887 drv[i].capture.rates = RSND_RATES;
888 drv[i].capture.formats = RSND_FMTS;
889 drv[i].capture.channels_min = 2;
890 drv[i].capture.channels_max = 2;
Kuninori Morimoto389933d2014-03-03 20:50:00 -0800891
Kuninori Morimoto29e69fd2014-05-08 01:59:26 -0700892 rdai[i].capture.info = &info->dai_info[i].capture;
Kuninori Morimoto54cb5562015-01-15 08:06:24 +0000893 rdai[i].capture.rdai = rdai + i;
Kuninori Morimoto9bfed6c2014-03-02 23:42:47 -0800894 rsnd_path_init(priv, &rdai[i], &rdai[i].capture);
Kuninori Morimoto1536a962013-07-21 21:35:52 -0700895 }
896
Kuninori Morimoto4b4dab82013-07-28 18:58:29 -0700897 dev_dbg(dev, "%s (%s/%s)\n", rdai[i].name,
898 pmod ? "play" : " -- ",
899 cmod ? "capture" : " -- ");
Kuninori Morimoto1536a962013-07-21 21:35:52 -0700900 }
901
Kuninori Morimoto1536a962013-07-21 21:35:52 -0700902 return 0;
903}
904
Kuninori Morimoto1536a962013-07-21 21:35:52 -0700905/*
906 * pcm ops
907 */
908static struct snd_pcm_hardware rsnd_pcm_hardware = {
909 .info = SNDRV_PCM_INFO_INTERLEAVED |
910 SNDRV_PCM_INFO_MMAP |
Kuninori Morimoto706c6622014-10-28 21:02:03 -0700911 SNDRV_PCM_INFO_MMAP_VALID,
Kuninori Morimoto1536a962013-07-21 21:35:52 -0700912 .buffer_bytes_max = 64 * 1024,
913 .period_bytes_min = 32,
914 .period_bytes_max = 8192,
915 .periods_min = 1,
916 .periods_max = 32,
917 .fifo_size = 256,
918};
919
920static int rsnd_pcm_open(struct snd_pcm_substream *substream)
921{
922 struct snd_pcm_runtime *runtime = substream->runtime;
923 int ret = 0;
924
925 snd_soc_set_runtime_hwparams(substream, &rsnd_pcm_hardware);
926
927 ret = snd_pcm_hw_constraint_integer(runtime,
928 SNDRV_PCM_HW_PARAM_PERIODS);
929
930 return ret;
931}
932
933static int rsnd_hw_params(struct snd_pcm_substream *substream,
934 struct snd_pcm_hw_params *hw_params)
935{
936 return snd_pcm_lib_malloc_pages(substream,
937 params_buffer_bytes(hw_params));
938}
939
940static snd_pcm_uframes_t rsnd_pointer(struct snd_pcm_substream *substream)
941{
942 struct snd_pcm_runtime *runtime = substream->runtime;
943 struct snd_soc_dai *dai = rsnd_substream_to_dai(substream);
944 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
945 struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
946
947 return bytes_to_frames(runtime, io->byte_pos);
948}
949
950static struct snd_pcm_ops rsnd_pcm_ops = {
951 .open = rsnd_pcm_open,
952 .ioctl = snd_pcm_lib_ioctl,
953 .hw_params = rsnd_hw_params,
954 .hw_free = snd_pcm_lib_free_pages,
955 .pointer = rsnd_pointer,
956};
957
958/*
Kuninori Morimoto170a2492014-11-27 08:06:14 +0000959 * snd_kcontrol
960 */
961#define kcontrol_to_cfg(kctrl) ((struct rsnd_kctrl_cfg *)kctrl->private_value)
962static int rsnd_kctrl_info(struct snd_kcontrol *kctrl,
963 struct snd_ctl_elem_info *uinfo)
964{
965 struct rsnd_kctrl_cfg *cfg = kcontrol_to_cfg(kctrl);
966
967 if (cfg->texts) {
968 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
969 uinfo->count = cfg->size;
970 uinfo->value.enumerated.items = cfg->max;
971 if (uinfo->value.enumerated.item >= cfg->max)
972 uinfo->value.enumerated.item = cfg->max - 1;
973 strlcpy(uinfo->value.enumerated.name,
974 cfg->texts[uinfo->value.enumerated.item],
975 sizeof(uinfo->value.enumerated.name));
976 } else {
977 uinfo->count = cfg->size;
978 uinfo->value.integer.min = 0;
979 uinfo->value.integer.max = cfg->max;
980 uinfo->type = (cfg->max == 1) ?
981 SNDRV_CTL_ELEM_TYPE_BOOLEAN :
982 SNDRV_CTL_ELEM_TYPE_INTEGER;
983 }
984
985 return 0;
986}
987
988static int rsnd_kctrl_get(struct snd_kcontrol *kctrl,
989 struct snd_ctl_elem_value *uc)
990{
991 struct rsnd_kctrl_cfg *cfg = kcontrol_to_cfg(kctrl);
992 int i;
993
994 for (i = 0; i < cfg->size; i++)
995 if (cfg->texts)
996 uc->value.enumerated.item[i] = cfg->val[i];
997 else
998 uc->value.integer.value[i] = cfg->val[i];
999
1000 return 0;
1001}
1002
1003static int rsnd_kctrl_put(struct snd_kcontrol *kctrl,
1004 struct snd_ctl_elem_value *uc)
1005{
1006 struct rsnd_mod *mod = snd_kcontrol_chip(kctrl);
1007 struct rsnd_kctrl_cfg *cfg = kcontrol_to_cfg(kctrl);
1008 int i, change = 0;
1009
1010 for (i = 0; i < cfg->size; i++) {
1011 if (cfg->texts) {
1012 change |= (uc->value.enumerated.item[i] != cfg->val[i]);
1013 cfg->val[i] = uc->value.enumerated.item[i];
1014 } else {
1015 change |= (uc->value.integer.value[i] != cfg->val[i]);
1016 cfg->val[i] = uc->value.integer.value[i];
1017 }
1018 }
1019
1020 if (change)
1021 cfg->update(mod);
1022
1023 return change;
1024}
1025
1026static int __rsnd_kctrl_new(struct rsnd_mod *mod,
Kuninori Morimoto170a2492014-11-27 08:06:14 +00001027 struct snd_soc_pcm_runtime *rtd,
1028 const unsigned char *name,
1029 struct rsnd_kctrl_cfg *cfg,
1030 void (*update)(struct rsnd_mod *mod))
1031{
1032 struct snd_card *card = rtd->card->snd_card;
1033 struct snd_kcontrol *kctrl;
1034 struct snd_kcontrol_new knew = {
1035 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1036 .name = name,
1037 .info = rsnd_kctrl_info,
1038 .get = rsnd_kctrl_get,
1039 .put = rsnd_kctrl_put,
1040 .private_value = (unsigned long)cfg,
1041 };
1042 int ret;
1043
1044 kctrl = snd_ctl_new1(&knew, mod);
1045 if (!kctrl)
1046 return -ENOMEM;
1047
1048 ret = snd_ctl_add(card, kctrl);
Kuninori Morimotod1f83d62015-02-02 04:53:53 +00001049 if (ret < 0) {
1050 snd_ctl_free_one(kctrl);
Kuninori Morimoto170a2492014-11-27 08:06:14 +00001051 return ret;
Kuninori Morimotod1f83d62015-02-02 04:53:53 +00001052 }
Kuninori Morimoto170a2492014-11-27 08:06:14 +00001053
1054 cfg->update = update;
Kuninori Morimotod1f83d62015-02-02 04:53:53 +00001055 cfg->card = card;
1056 cfg->kctrl = kctrl;
Kuninori Morimoto170a2492014-11-27 08:06:14 +00001057
1058 return 0;
1059}
1060
Kuninori Morimotod1f83d62015-02-02 04:53:53 +00001061void _rsnd_kctrl_remove(struct rsnd_kctrl_cfg *cfg)
1062{
1063 snd_ctl_remove(cfg->card, cfg->kctrl);
1064}
1065
Kuninori Morimoto170a2492014-11-27 08:06:14 +00001066int rsnd_kctrl_new_m(struct rsnd_mod *mod,
Kuninori Morimoto170a2492014-11-27 08:06:14 +00001067 struct snd_soc_pcm_runtime *rtd,
1068 const unsigned char *name,
1069 void (*update)(struct rsnd_mod *mod),
1070 struct rsnd_kctrl_cfg_m *_cfg,
1071 u32 max)
1072{
1073 _cfg->cfg.max = max;
1074 _cfg->cfg.size = RSND_DVC_CHANNELS;
1075 _cfg->cfg.val = _cfg->val;
Kuninori Morimotof708d942015-01-15 08:07:19 +00001076 return __rsnd_kctrl_new(mod, rtd, name, &_cfg->cfg, update);
Kuninori Morimoto170a2492014-11-27 08:06:14 +00001077}
1078
1079int rsnd_kctrl_new_s(struct rsnd_mod *mod,
Kuninori Morimoto170a2492014-11-27 08:06:14 +00001080 struct snd_soc_pcm_runtime *rtd,
1081 const unsigned char *name,
1082 void (*update)(struct rsnd_mod *mod),
1083 struct rsnd_kctrl_cfg_s *_cfg,
1084 u32 max)
1085{
1086 _cfg->cfg.max = max;
1087 _cfg->cfg.size = 1;
1088 _cfg->cfg.val = &_cfg->val;
Kuninori Morimotof708d942015-01-15 08:07:19 +00001089 return __rsnd_kctrl_new(mod, rtd, name, &_cfg->cfg, update);
Kuninori Morimoto170a2492014-11-27 08:06:14 +00001090}
1091
1092int rsnd_kctrl_new_e(struct rsnd_mod *mod,
Kuninori Morimoto170a2492014-11-27 08:06:14 +00001093 struct snd_soc_pcm_runtime *rtd,
1094 const unsigned char *name,
1095 struct rsnd_kctrl_cfg_s *_cfg,
1096 void (*update)(struct rsnd_mod *mod),
1097 const char * const *texts,
1098 u32 max)
1099{
1100 _cfg->cfg.max = max;
1101 _cfg->cfg.size = 1;
1102 _cfg->cfg.val = &_cfg->val;
1103 _cfg->cfg.texts = texts;
Kuninori Morimotof708d942015-01-15 08:07:19 +00001104 return __rsnd_kctrl_new(mod, rtd, name, &_cfg->cfg, update);
Kuninori Morimoto170a2492014-11-27 08:06:14 +00001105}
1106
1107/*
Kuninori Morimoto1536a962013-07-21 21:35:52 -07001108 * snd_soc_platform
1109 */
1110
1111#define PREALLOC_BUFFER (32 * 1024)
1112#define PREALLOC_BUFFER_MAX (32 * 1024)
1113
1114static int rsnd_pcm_new(struct snd_soc_pcm_runtime *rtd)
1115{
Kuninori Morimoto7c63f3c2014-07-30 23:53:03 -07001116 struct snd_soc_dai *dai = rtd->cpu_dai;
1117 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
1118 int ret;
Kuninori Morimotobff58ea2014-05-08 17:44:49 -07001119
Kuninori Morimoto690602f2015-01-15 08:07:47 +00001120 ret = rsnd_dai_call(pcm_new, &rdai->playback, rtd);
Kuninori Morimoto7c63f3c2014-07-30 23:53:03 -07001121 if (ret)
1122 return ret;
Kuninori Morimotobff58ea2014-05-08 17:44:49 -07001123
Kuninori Morimoto690602f2015-01-15 08:07:47 +00001124 ret = rsnd_dai_call(pcm_new, &rdai->capture, rtd);
Kuninori Morimoto7c63f3c2014-07-30 23:53:03 -07001125 if (ret)
1126 return ret;
Kuninori Morimotobff58ea2014-05-08 17:44:49 -07001127
Kuninori Morimoto1536a962013-07-21 21:35:52 -07001128 return snd_pcm_lib_preallocate_pages_for_all(
1129 rtd->pcm,
1130 SNDRV_DMA_TYPE_DEV,
1131 rtd->card->snd_card->dev,
1132 PREALLOC_BUFFER, PREALLOC_BUFFER_MAX);
1133}
1134
Kuninori Morimoto1536a962013-07-21 21:35:52 -07001135static struct snd_soc_platform_driver rsnd_soc_platform = {
1136 .ops = &rsnd_pcm_ops,
1137 .pcm_new = rsnd_pcm_new,
Kuninori Morimoto1536a962013-07-21 21:35:52 -07001138};
1139
1140static const struct snd_soc_component_driver rsnd_soc_component = {
1141 .name = "rsnd",
1142};
1143
Kuninori Morimotod3a76822014-11-09 20:00:58 -08001144static int rsnd_rdai_continuance_probe(struct rsnd_priv *priv,
Kuninori Morimotof708d942015-01-15 08:07:19 +00001145 struct rsnd_dai_stream *io)
Kuninori Morimotod3a76822014-11-09 20:00:58 -08001146{
Kuninori Morimotod3a76822014-11-09 20:00:58 -08001147 int ret;
1148
Kuninori Morimoto690602f2015-01-15 08:07:47 +00001149 ret = rsnd_dai_call(probe, io, priv);
Kuninori Morimotod3a76822014-11-09 20:00:58 -08001150 if (ret == -EAGAIN) {
1151 /*
1152 * Fallback to PIO mode
1153 */
1154
1155 /*
1156 * call "remove" for SSI/SRC/DVC
1157 * SSI will be switch to PIO mode if it was DMA mode
1158 * see
1159 * rsnd_dma_init()
Kuninori Morimoto97463e192014-11-27 08:02:43 +00001160 * rsnd_ssi_fallback()
Kuninori Morimotod3a76822014-11-09 20:00:58 -08001161 */
Kuninori Morimoto690602f2015-01-15 08:07:47 +00001162 rsnd_dai_call(remove, io, priv);
Kuninori Morimotod3a76822014-11-09 20:00:58 -08001163
1164 /*
1165 * remove SRC/DVC from DAI,
1166 */
1167 rsnd_path_break(priv, io, src);
1168 rsnd_path_break(priv, io, dvc);
1169
1170 /*
Kuninori Morimoto97463e192014-11-27 08:02:43 +00001171 * fallback
1172 */
Kuninori Morimoto690602f2015-01-15 08:07:47 +00001173 rsnd_dai_call(fallback, io, priv);
Kuninori Morimoto97463e192014-11-27 08:02:43 +00001174
1175 /*
Kuninori Morimotod3a76822014-11-09 20:00:58 -08001176 * retry to "probe".
1177 * DAI has SSI which is PIO mode only now.
1178 */
Kuninori Morimoto690602f2015-01-15 08:07:47 +00001179 ret = rsnd_dai_call(probe, io, priv);
Kuninori Morimotod3a76822014-11-09 20:00:58 -08001180 }
1181
1182 return ret;
1183}
1184
Kuninori Morimoto1536a962013-07-21 21:35:52 -07001185/*
1186 * rsnd probe
1187 */
1188static int rsnd_probe(struct platform_device *pdev)
1189{
1190 struct rcar_snd_info *info;
1191 struct rsnd_priv *priv;
1192 struct device *dev = &pdev->dev;
Kuninori Morimoto7681f6a2014-03-03 20:50:33 -08001193 struct rsnd_dai *rdai;
Kuninori Morimoto90e8e502014-03-17 19:29:55 -07001194 const struct of_device_id *of_id = of_match_device(rsnd_of_match, dev);
1195 const struct rsnd_of_data *of_data;
Kuninori Morimotod1ac970f2014-03-02 23:43:18 -08001196 int (*probe_func[])(struct platform_device *pdev,
Kuninori Morimoto90e8e502014-03-17 19:29:55 -07001197 const struct rsnd_of_data *of_data,
Kuninori Morimotod1ac970f2014-03-02 23:43:18 -08001198 struct rsnd_priv *priv) = {
1199 rsnd_gen_probe,
1200 rsnd_ssi_probe,
Kuninori Morimotoba9c9492014-03-03 20:51:21 -08001201 rsnd_src_probe,
Kuninori Morimotobff58ea2014-05-08 17:44:49 -07001202 rsnd_dvc_probe,
Kuninori Morimotod1ac970f2014-03-02 23:43:18 -08001203 rsnd_adg_probe,
1204 rsnd_dai_probe,
1205 };
1206 int ret, i;
Kuninori Morimoto1536a962013-07-21 21:35:52 -07001207
Kuninori Morimoto90e8e502014-03-17 19:29:55 -07001208 info = NULL;
1209 of_data = NULL;
1210 if (of_id) {
1211 info = devm_kzalloc(&pdev->dev,
1212 sizeof(struct rcar_snd_info), GFP_KERNEL);
1213 of_data = of_id->data;
1214 } else {
1215 info = pdev->dev.platform_data;
1216 }
1217
Kuninori Morimoto1536a962013-07-21 21:35:52 -07001218 if (!info) {
1219 dev_err(dev, "driver needs R-Car sound information\n");
1220 return -ENODEV;
1221 }
1222
1223 /*
1224 * init priv data
1225 */
1226 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
1227 if (!priv) {
1228 dev_err(dev, "priv allocate failed\n");
1229 return -ENODEV;
1230 }
1231
Kuninori Morimoto9f464f82014-05-22 23:25:30 -07001232 priv->pdev = pdev;
Kuninori Morimoto1536a962013-07-21 21:35:52 -07001233 priv->info = info;
1234 spin_lock_init(&priv->lock);
1235
1236 /*
1237 * init each module
1238 */
Kuninori Morimotod1ac970f2014-03-02 23:43:18 -08001239 for (i = 0; i < ARRAY_SIZE(probe_func); i++) {
Kuninori Morimoto90e8e502014-03-17 19:29:55 -07001240 ret = probe_func[i](pdev, of_data, priv);
Kuninori Morimotod1ac970f2014-03-02 23:43:18 -08001241 if (ret)
1242 return ret;
1243 }
Kuninori Morimoto33377442013-07-21 21:36:21 -07001244
Kuninori Morimoto7681f6a2014-03-03 20:50:33 -08001245 for_each_rsnd_dai(rdai, priv, i) {
Kuninori Morimotof708d942015-01-15 08:07:19 +00001246 ret = rsnd_rdai_continuance_probe(priv, &rdai->playback);
Kuninori Morimoto7681f6a2014-03-03 20:50:33 -08001247 if (ret)
Kuninori Morimotod62a3dc2014-07-24 01:51:31 -07001248 goto exit_snd_probe;
Kuninori Morimoto07539c12013-07-21 21:36:35 -07001249
Kuninori Morimotof708d942015-01-15 08:07:19 +00001250 ret = rsnd_rdai_continuance_probe(priv, &rdai->capture);
Kuninori Morimoto7681f6a2014-03-03 20:50:33 -08001251 if (ret)
Kuninori Morimotod62a3dc2014-07-24 01:51:31 -07001252 goto exit_snd_probe;
Kuninori Morimoto7681f6a2014-03-03 20:50:33 -08001253 }
Kuninori Morimoto4b4dab82013-07-28 18:58:29 -07001254
Kuninori Morimoto0b1f6ec2015-02-09 08:05:22 +00001255 dev_set_drvdata(dev, priv);
1256
Kuninori Morimoto1536a962013-07-21 21:35:52 -07001257 /*
1258 * asoc register
1259 */
1260 ret = snd_soc_register_platform(dev, &rsnd_soc_platform);
1261 if (ret < 0) {
1262 dev_err(dev, "cannot snd soc register\n");
1263 return ret;
1264 }
1265
1266 ret = snd_soc_register_component(dev, &rsnd_soc_component,
Kuninori Morimotoecba9e72014-02-24 22:15:18 -08001267 priv->daidrv, rsnd_rdai_nr(priv));
Kuninori Morimoto1536a962013-07-21 21:35:52 -07001268 if (ret < 0) {
1269 dev_err(dev, "cannot snd dai register\n");
1270 goto exit_snd_soc;
1271 }
1272
Kuninori Morimoto1536a962013-07-21 21:35:52 -07001273 pm_runtime_enable(dev);
1274
1275 dev_info(dev, "probed\n");
1276 return ret;
1277
1278exit_snd_soc:
1279 snd_soc_unregister_platform(dev);
Kuninori Morimotod62a3dc2014-07-24 01:51:31 -07001280exit_snd_probe:
1281 for_each_rsnd_dai(rdai, priv, i) {
Kuninori Morimoto690602f2015-01-15 08:07:47 +00001282 rsnd_dai_call(remove, &rdai->playback, priv);
1283 rsnd_dai_call(remove, &rdai->capture, priv);
Kuninori Morimotod62a3dc2014-07-24 01:51:31 -07001284 }
Kuninori Morimoto1536a962013-07-21 21:35:52 -07001285
1286 return ret;
1287}
1288
1289static int rsnd_remove(struct platform_device *pdev)
1290{
1291 struct rsnd_priv *priv = dev_get_drvdata(&pdev->dev);
Kuninori Morimoto7681f6a2014-03-03 20:50:33 -08001292 struct rsnd_dai *rdai;
Kuninori Morimotod62a3dc2014-07-24 01:51:31 -07001293 int ret = 0, i;
Kuninori Morimoto1536a962013-07-21 21:35:52 -07001294
1295 pm_runtime_disable(&pdev->dev);
1296
Kuninori Morimoto7681f6a2014-03-03 20:50:33 -08001297 for_each_rsnd_dai(rdai, priv, i) {
Kuninori Morimoto690602f2015-01-15 08:07:47 +00001298 ret |= rsnd_dai_call(remove, &rdai->playback, priv);
1299 ret |= rsnd_dai_call(remove, &rdai->capture, priv);
Kuninori Morimoto7681f6a2014-03-03 20:50:33 -08001300 }
Kuninori Morimoto1536a962013-07-21 21:35:52 -07001301
Kuninori Morimotod7c42ff2015-02-02 04:54:07 +00001302 snd_soc_unregister_component(&pdev->dev);
1303 snd_soc_unregister_platform(&pdev->dev);
1304
Kuninori Morimotod62a3dc2014-07-24 01:51:31 -07001305 return ret;
Kuninori Morimoto1536a962013-07-21 21:35:52 -07001306}
1307
1308static struct platform_driver rsnd_driver = {
1309 .driver = {
1310 .name = "rcar_sound",
Kuninori Morimoto90e8e502014-03-17 19:29:55 -07001311 .of_match_table = rsnd_of_match,
Kuninori Morimoto1536a962013-07-21 21:35:52 -07001312 },
1313 .probe = rsnd_probe,
1314 .remove = rsnd_remove,
1315};
1316module_platform_driver(rsnd_driver);
1317
1318MODULE_LICENSE("GPL");
1319MODULE_DESCRIPTION("Renesas R-Car audio driver");
1320MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");
1321MODULE_ALIAS("platform:rcar-pcm-audio");