blob: 035eda2de239d731a30d2efcf8e6e0ae9eb0b636 [file] [log] [blame]
Kuninori Morimoto07539c12013-07-21 21:36:35 -07001/*
Kuninori Morimotoba9c9492014-03-03 20:51:21 -08002 * Renesas R-Car SRC support
Kuninori Morimoto07539c12013-07-21 21:36:35 -07003 *
4 * Copyright (C) 2013 Renesas Solutions Corp.
5 * Kuninori Morimoto <kuninori.morimoto.gx@renesas.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 version 2 as
9 * published by the Free Software Foundation.
10 */
11#include "rsnd.h"
12
Kuninori Morimotoba9c9492014-03-03 20:51:21 -080013struct rsnd_src {
14 struct rsnd_src_platform_info *info; /* rcar_snd.h */
Kuninori Morimoto07539c12013-07-21 21:36:35 -070015 struct rsnd_mod mod;
Kuninori Morimotoef749402013-12-19 19:28:51 -080016 struct clk *clk;
Kuninori Morimoto07539c12013-07-21 21:36:35 -070017};
18
Kuninori Morimotoba9c9492014-03-03 20:51:21 -080019#define RSND_SRC_NAME_SIZE 16
Kuninori Morimoto374a52812013-07-28 18:59:12 -070020
Kuninori Morimotoba9c9492014-03-03 20:51:21 -080021#define rsnd_src_convert_rate(p) ((p)->info->convert_rate)
22#define rsnd_mod_to_src(_mod) \
23 container_of((_mod), struct rsnd_src, mod)
Kuninori Morimotoba9c9492014-03-03 20:51:21 -080024#define rsnd_src_dma_available(src) \
25 rsnd_dma_available(rsnd_mod_to_dma(&(src)->mod))
Kuninori Morimoto39cf3c42014-01-23 18:40:47 -080026
Kuninori Morimotoba9c9492014-03-03 20:51:21 -080027#define for_each_rsnd_src(pos, priv, i) \
Kuninori Morimoto39cf3c42014-01-23 18:40:47 -080028 for ((i) = 0; \
Kuninori Morimotoba9c9492014-03-03 20:51:21 -080029 ((i) < rsnd_src_nr(priv)) && \
30 ((pos) = (struct rsnd_src *)(priv)->src + i); \
Kuninori Morimoto39cf3c42014-01-23 18:40:47 -080031 i++)
32
33
Kuninori Morimotoef749402013-12-19 19:28:51 -080034/*
35 * image of SRC (Sampling Rate Converter)
36 *
37 * 96kHz <-> +-----+ 48kHz +-----+ 48kHz +-------+
38 * 48kHz <-> | SRC | <------> | SSI | <-----> | codec |
39 * 44.1kHz <-> +-----+ +-----+ +-------+
40 * ...
41 *
42 */
Kuninori Morimoto374a52812013-07-28 18:59:12 -070043
Kuninori Morimoto41c62212014-01-23 18:39:56 -080044/*
Kuninori Morimotoba9c9492014-03-03 20:51:21 -080045 * src.c is caring...
Kuninori Morimotoc926b742014-01-23 18:40:41 -080046 *
47 * Gen1
48 *
49 * [mem] -> [SRU] -> [SSI]
50 * |--------|
51 *
52 * Gen2
53 *
Kuninori Morimotoba9c9492014-03-03 20:51:21 -080054 * [mem] -> [SRC] -> [SSIU] -> [SSI]
Kuninori Morimotoc926b742014-01-23 18:40:41 -080055 * |-----------------|
56 */
57
58/*
Kuninori Morimoto41c62212014-01-23 18:39:56 -080059 * How to use SRC bypass mode for debugging
60 *
61 * SRC has bypass mode, and it is useful for debugging.
62 * In Gen2 case,
63 * SRCm_MODE controls whether SRC is used or not
64 * SSI_MODE0 controls whether SSIU which receives SRC data
65 * is used or not.
66 * Both SRCm_MODE/SSI_MODE0 settings are needed if you use SRC,
67 * but SRC bypass mode needs SSI_MODE0 only.
68 *
69 * This driver request
Kuninori Morimotoba9c9492014-03-03 20:51:21 -080070 * struct rsnd_src_platform_info {
Kuninori Morimoto41c62212014-01-23 18:39:56 -080071 * u32 convert_rate;
Kuninori Morimoto29e69fd2014-05-08 01:59:26 -070072 * int dma_id;
Kuninori Morimoto41c62212014-01-23 18:39:56 -080073 * }
74 *
Kuninori Morimotoba9c9492014-03-03 20:51:21 -080075 * rsnd_src_convert_rate() indicates
Kuninori Morimoto41c62212014-01-23 18:39:56 -080076 * above convert_rate, and it controls
77 * whether SRC is used or not.
78 *
79 * ex) doesn't use SRC
Kuninori Morimoto29e69fd2014-05-08 01:59:26 -070080 * static struct rsnd_dai_platform_info rsnd_dai = {
81 * .playback = { .ssi = &rsnd_ssi[0], },
Kuninori Morimoto41c62212014-01-23 18:39:56 -080082 * };
83 *
84 * ex) uses SRC
Kuninori Morimoto29e69fd2014-05-08 01:59:26 -070085 * static struct rsnd_src_platform_info rsnd_src[] = {
86 * RSND_SCU(48000, 0),
87 * ...
88 * };
89 * static struct rsnd_dai_platform_info rsnd_dai = {
90 * .playback = { .ssi = &rsnd_ssi[0], .src = &rsnd_src[0] },
Kuninori Morimoto41c62212014-01-23 18:39:56 -080091 * };
92 *
93 * ex) uses SRC bypass mode
Kuninori Morimoto29e69fd2014-05-08 01:59:26 -070094 * static struct rsnd_src_platform_info rsnd_src[] = {
95 * RSND_SCU(0, 0),
96 * ...
97 * };
98 * static struct rsnd_dai_platform_info rsnd_dai = {
99 * .playback = { .ssi = &rsnd_ssi[0], .src = &rsnd_src[0] },
Kuninori Morimoto41c62212014-01-23 18:39:56 -0800100 * };
101 *
102 */
103
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800104/*
105 * Gen1/Gen2 common functions
106 */
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800107int rsnd_src_ssi_mode_init(struct rsnd_mod *ssi_mod,
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700108 struct rsnd_dai *rdai)
Kuninori Morimoto7b5ce972014-01-23 18:39:32 -0800109{
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700110 struct rsnd_dai_stream *io = rsnd_mod_to_io(ssi_mod);
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800111 struct rsnd_mod *src_mod = rsnd_io_to_mod_src(io);
Kuninori Morimoto374e5422014-03-02 23:43:03 -0800112 int ssi_id = rsnd_mod_id(ssi_mod);
Kuninori Morimoto7b5ce972014-01-23 18:39:32 -0800113
114 /*
115 * SSI_MODE0
116 */
Kuninori Morimoto221bf522014-03-03 20:50:24 -0800117 rsnd_mod_bset(ssi_mod, SSI_MODE0, (1 << ssi_id),
Kuninori Morimoto29e69fd2014-05-08 01:59:26 -0700118 src_mod ? 0 : (1 << ssi_id));
Kuninori Morimoto7b5ce972014-01-23 18:39:32 -0800119
120 /*
121 * SSI_MODE1
122 */
Kuninori Morimoto374e5422014-03-02 23:43:03 -0800123 if (rsnd_ssi_is_pin_sharing(ssi_mod)) {
Kuninori Morimoto7b5ce972014-01-23 18:39:32 -0800124 int shift = -1;
Kuninori Morimoto374e5422014-03-02 23:43:03 -0800125 switch (ssi_id) {
Kuninori Morimoto7b5ce972014-01-23 18:39:32 -0800126 case 1:
127 shift = 0;
128 break;
129 case 2:
130 shift = 2;
131 break;
132 case 4:
133 shift = 16;
134 break;
135 }
136
137 if (shift >= 0)
Kuninori Morimoto221bf522014-03-03 20:50:24 -0800138 rsnd_mod_bset(ssi_mod, SSI_MODE1,
Kuninori Morimoto7b5ce972014-01-23 18:39:32 -0800139 0x3 << shift,
140 rsnd_dai_is_clk_master(rdai) ?
141 0x2 << shift : 0x1 << shift);
142 }
143
144 return 0;
145}
146
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800147int rsnd_src_enable_ssi_irq(struct rsnd_mod *ssi_mod,
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700148 struct rsnd_dai *rdai)
Kuninori Morimotob8cc41e2014-03-03 20:50:08 -0800149{
150 struct rsnd_priv *priv = rsnd_mod_to_priv(ssi_mod);
151
152 /* enable PIO interrupt if Gen2 */
153 if (rsnd_is_gen2(priv))
154 rsnd_mod_write(ssi_mod, INT_ENABLE, 0x0f000000);
155
156 return 0;
157}
158
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800159unsigned int rsnd_src_get_ssi_rate(struct rsnd_priv *priv,
Kuninori Morimoto374e5422014-03-02 23:43:03 -0800160 struct rsnd_dai_stream *io,
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800161 struct snd_pcm_runtime *runtime)
162{
Kuninori Morimotob1eac432014-03-23 21:23:42 -0700163 struct rsnd_mod *src_mod = rsnd_io_to_mod_src(io);
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800164 struct rsnd_src *src;
Kuninori Morimotob1eac432014-03-23 21:23:42 -0700165 unsigned int rate = 0;
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800166
Kuninori Morimotob1eac432014-03-23 21:23:42 -0700167 if (src_mod) {
168 src = rsnd_mod_to_src(src_mod);
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800169
Kuninori Morimotob1eac432014-03-23 21:23:42 -0700170 /*
171 * return convert rate if SRC is used,
172 * otherwise, return runtime->rate as usual
173 */
174 rate = rsnd_src_convert_rate(src);
175 }
176
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800177 if (!rate)
178 rate = runtime->rate;
179
180 return rate;
181}
182
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800183static int rsnd_src_set_convert_rate(struct rsnd_mod *mod,
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700184 struct rsnd_dai *rdai)
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800185{
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700186 struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800187 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800188 struct rsnd_src *src = rsnd_mod_to_src(mod);
189 u32 convert_rate = rsnd_src_convert_rate(src);
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800190 u32 fsrate = 0;
191
192 if (convert_rate)
193 fsrate = 0x0400000 / convert_rate * runtime->rate;
194
195 /* set/clear soft reset */
196 rsnd_mod_write(mod, SRC_SWRSR, 0);
197 rsnd_mod_write(mod, SRC_SWRSR, 1);
198
199 /*
200 * Initialize the operation of the SRC internal circuits
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800201 * see rsnd_src_start()
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800202 */
203 rsnd_mod_write(mod, SRC_SRCIR, 1);
204
205 /* Set channel number and output bit length */
Kuninori Morimotod7bdbc52014-05-08 17:44:14 -0700206 rsnd_mod_write(mod, SRC_ADINR, rsnd_get_adinr(mod));
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800207
208 /* Enable the initial value of IFS */
209 if (fsrate) {
210 rsnd_mod_write(mod, SRC_IFSCR, 1);
211
212 /* Set initial value of IFS */
213 rsnd_mod_write(mod, SRC_IFSVR, fsrate);
214 }
215
216 /* use DMA transfer */
217 rsnd_mod_write(mod, SRC_BUSIF_MODE, 1);
218
219 return 0;
220}
221
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800222static int rsnd_src_init(struct rsnd_mod *mod,
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700223 struct rsnd_dai *rdai)
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800224{
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800225 struct rsnd_src *src = rsnd_mod_to_src(mod);
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800226
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800227 clk_enable(src->clk);
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800228
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800229 return 0;
230}
231
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800232static int rsnd_src_quit(struct rsnd_mod *mod,
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700233 struct rsnd_dai *rdai)
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800234{
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800235 struct rsnd_src *src = rsnd_mod_to_src(mod);
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800236
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800237 clk_disable(src->clk);
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800238
239 return 0;
240}
241
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800242static int rsnd_src_start(struct rsnd_mod *mod,
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700243 struct rsnd_dai *rdai)
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800244{
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800245 struct rsnd_src *src = rsnd_mod_to_src(mod);
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800246
247 /*
248 * Cancel the initialization and operate the SRC function
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800249 * see rsnd_src_set_convert_rate()
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800250 */
251 rsnd_mod_write(mod, SRC_SRCIR, 0);
252
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800253 if (rsnd_src_convert_rate(src))
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800254 rsnd_mod_write(mod, SRC_ROUTE_MODE0, 1);
255
256 return 0;
257}
258
259
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800260static int rsnd_src_stop(struct rsnd_mod *mod,
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700261 struct rsnd_dai *rdai)
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800262{
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800263 struct rsnd_src *src = rsnd_mod_to_src(mod);
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800264
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800265 if (rsnd_src_convert_rate(src))
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800266 rsnd_mod_write(mod, SRC_ROUTE_MODE0, 0);
267
268 return 0;
269}
270
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800271static struct rsnd_mod_ops rsnd_src_non_ops = {
272 .name = "src (non)",
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800273};
274
275/*
276 * Gen1 functions
277 */
278static int rsnd_src_set_route_gen1(struct rsnd_mod *mod,
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700279 struct rsnd_dai *rdai)
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700280{
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700281 struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800282 struct src_route_config {
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700283 u32 mask;
284 int shift;
285 } routes[] = {
286 { 0xF, 0, }, /* 0 */
287 { 0xF, 4, }, /* 1 */
288 { 0xF, 8, }, /* 2 */
289 { 0x7, 12, }, /* 3 */
290 { 0x7, 16, }, /* 4 */
291 { 0x7, 20, }, /* 5 */
292 { 0x7, 24, }, /* 6 */
293 { 0x3, 28, }, /* 7 */
294 { 0x3, 30, }, /* 8 */
295 };
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700296 u32 mask;
297 u32 val;
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700298 int id;
299
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700300 id = rsnd_mod_id(mod);
Dan Carpenterb5f3d7a2013-11-08 12:46:10 +0300301 if (id < 0 || id >= ARRAY_SIZE(routes))
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700302 return -EIO;
303
304 /*
305 * SRC_ROUTE_SELECT
306 */
307 val = rsnd_dai_is_play(rdai, io) ? 0x1 : 0x2;
308 val = val << routes[id].shift;
309 mask = routes[id].mask << routes[id].shift;
310
311 rsnd_mod_bset(mod, SRC_ROUTE_SEL, mask, val);
312
Kuninori Morimoto28dc4b62014-01-23 18:41:10 -0800313 return 0;
314}
315
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800316static int rsnd_src_set_convert_timing_gen1(struct rsnd_mod *mod,
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700317 struct rsnd_dai *rdai)
Kuninori Morimoto28dc4b62014-01-23 18:41:10 -0800318{
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700319 struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
Kuninori Morimoto28dc4b62014-01-23 18:41:10 -0800320 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800321 struct rsnd_src *src = rsnd_mod_to_src(mod);
Kuninori Morimoto28dc4b62014-01-23 18:41:10 -0800322 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800323 u32 convert_rate = rsnd_src_convert_rate(src);
Kuninori Morimoto28dc4b62014-01-23 18:41:10 -0800324 u32 mask;
325 u32 val;
326 int shift;
327 int id = rsnd_mod_id(mod);
328 int ret;
329
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700330 /*
331 * SRC_TIMING_SELECT
332 */
333 shift = (id % 4) * 8;
334 mask = 0x1F << shift;
Kuninori Morimotoef749402013-12-19 19:28:51 -0800335
336 /*
337 * ADG is used as source clock if SRC was used,
338 * then, SSI WS is used as destination clock.
339 * SSI WS is used as source clock if SRC is not used
340 * (when playback, source/destination become reverse when capture)
341 */
Kuninori Morimoto28dc4b62014-01-23 18:41:10 -0800342 ret = 0;
343 if (convert_rate) {
344 /* use ADG */
Kuninori Morimotoef749402013-12-19 19:28:51 -0800345 val = 0;
Kuninori Morimoto28dc4b62014-01-23 18:41:10 -0800346 ret = rsnd_adg_set_convert_clk_gen1(priv, mod,
347 runtime->rate,
348 convert_rate);
349 } else if (8 == id) {
350 /* use SSI WS, but SRU8 is special */
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700351 val = id << shift;
Kuninori Morimoto28dc4b62014-01-23 18:41:10 -0800352 } else {
353 /* use SSI WS */
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700354 val = (id + 1) << shift;
Kuninori Morimoto28dc4b62014-01-23 18:41:10 -0800355 }
356
357 if (ret < 0)
358 return ret;
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700359
360 switch (id / 4) {
361 case 0:
362 rsnd_mod_bset(mod, SRC_TMG_SEL0, mask, val);
363 break;
364 case 1:
365 rsnd_mod_bset(mod, SRC_TMG_SEL1, mask, val);
366 break;
367 case 2:
368 rsnd_mod_bset(mod, SRC_TMG_SEL2, mask, val);
369 break;
370 }
371
372 return 0;
373}
374
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800375static int rsnd_src_set_convert_rate_gen1(struct rsnd_mod *mod,
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700376 struct rsnd_dai *rdai)
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700377{
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800378 int ret;
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700379
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700380 ret = rsnd_src_set_convert_rate(mod, rdai);
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800381 if (ret < 0)
382 return ret;
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700383
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800384 /* Select SRC mode (fixed value) */
385 rsnd_mod_write(mod, SRC_SRCCR, 0x00010110);
Kuninori Morimotoef749402013-12-19 19:28:51 -0800386
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800387 /* Set the restriction value of the FS ratio (98%) */
388 rsnd_mod_write(mod, SRC_MNFSR,
389 rsnd_mod_read(mod, SRC_IFSVR) / 100 * 98);
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700390
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800391 /* no SRC_BFSSR settings, since SRC_SRCCR::BUFMD is 0 */
Kuninori Morimotoef749402013-12-19 19:28:51 -0800392
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700393 return 0;
394}
395
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800396static int rsnd_src_init_gen1(struct rsnd_mod *mod,
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700397 struct rsnd_dai *rdai)
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700398{
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700399 int ret;
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700400
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700401 ret = rsnd_src_init(mod, rdai);
Kuninori Morimoto7b5ce972014-01-23 18:39:32 -0800402 if (ret < 0)
403 return ret;
404
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700405 ret = rsnd_src_set_route_gen1(mod, rdai);
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700406 if (ret < 0)
407 return ret;
408
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700409 ret = rsnd_src_set_convert_rate_gen1(mod, rdai);
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700410 if (ret < 0)
411 return ret;
412
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700413 ret = rsnd_src_set_convert_timing_gen1(mod, rdai);
Kuninori Morimoto28dc4b62014-01-23 18:41:10 -0800414 if (ret < 0)
415 return ret;
416
Kuninori Morimotoa204d902014-01-23 18:38:33 -0800417 return 0;
418}
419
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800420static int rsnd_src_start_gen1(struct rsnd_mod *mod,
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700421 struct rsnd_dai *rdai)
Kuninori Morimotoa204d902014-01-23 18:38:33 -0800422{
Kuninori Morimotoe7ce74e2014-01-23 18:38:50 -0800423 int id = rsnd_mod_id(mod);
Kuninori Morimotoa204d902014-01-23 18:38:33 -0800424
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800425 rsnd_mod_bset(mod, SRC_ROUTE_CTRL, (1 << id), (1 << id));
Kuninori Morimotoe7ce74e2014-01-23 18:38:50 -0800426
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700427 return rsnd_src_start(mod, rdai);
Kuninori Morimotoa204d902014-01-23 18:38:33 -0800428}
429
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800430static int rsnd_src_stop_gen1(struct rsnd_mod *mod,
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700431 struct rsnd_dai *rdai)
Kuninori Morimotoef749402013-12-19 19:28:51 -0800432{
Kuninori Morimotoe7ce74e2014-01-23 18:38:50 -0800433 int id = rsnd_mod_id(mod);
Kuninori Morimotoef749402013-12-19 19:28:51 -0800434
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800435 rsnd_mod_bset(mod, SRC_ROUTE_CTRL, (1 << id), 0);
Kuninori Morimotoe7ce74e2014-01-23 18:38:50 -0800436
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700437 return rsnd_src_stop(mod, rdai);
Kuninori Morimotoef749402013-12-19 19:28:51 -0800438}
439
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800440static struct rsnd_mod_ops rsnd_src_gen1_ops = {
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800441 .name = "sru (gen1)",
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800442 .init = rsnd_src_init_gen1,
443 .quit = rsnd_src_quit,
444 .start = rsnd_src_start_gen1,
445 .stop = rsnd_src_stop_gen1,
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700446};
447
Kuninori Morimoto1b7b08ef2014-01-23 18:41:36 -0800448/*
449 * Gen2 functions
450 */
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800451static int rsnd_src_set_convert_rate_gen2(struct rsnd_mod *mod,
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700452 struct rsnd_dai *rdai)
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800453{
454 int ret;
455
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700456 ret = rsnd_src_set_convert_rate(mod, rdai);
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800457 if (ret < 0)
458 return ret;
459
Kuninori Morimotod7bdbc52014-05-08 17:44:14 -0700460 rsnd_mod_write(mod, SSI_BUSIF_ADINR, rsnd_get_adinr(mod));
461 rsnd_mod_write(mod, SSI_BUSIF_MODE, 1);
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800462
463 rsnd_mod_write(mod, SRC_SRCCR, 0x00011110);
464
465 rsnd_mod_write(mod, SRC_BSDSR, 0x01800000);
466 rsnd_mod_write(mod, SRC_BSISR, 0x00100060);
467
468 return 0;
469}
470
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800471static int rsnd_src_set_convert_timing_gen2(struct rsnd_mod *mod,
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700472 struct rsnd_dai *rdai)
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800473{
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700474 struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800475 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800476 struct rsnd_src *src = rsnd_mod_to_src(mod);
477 u32 convert_rate = rsnd_src_convert_rate(src);
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800478 int ret;
479
480 if (convert_rate)
481 ret = rsnd_adg_set_convert_clk_gen2(mod, rdai, io,
482 runtime->rate,
483 convert_rate);
484 else
485 ret = rsnd_adg_set_convert_timing_gen2(mod, rdai, io);
486
487 return ret;
488}
489
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800490static int rsnd_src_probe_gen2(struct rsnd_mod *mod,
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700491 struct rsnd_dai *rdai)
Kuninori Morimoto76c6fb52014-03-03 20:50:41 -0800492{
493 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800494 struct rsnd_src *src = rsnd_mod_to_src(mod);
Kuninori Morimoto76c6fb52014-03-03 20:50:41 -0800495 struct device *dev = rsnd_priv_to_dev(priv);
496 int ret;
Kuninori Morimoto76c6fb52014-03-03 20:50:41 -0800497
498 ret = rsnd_dma_init(priv,
499 rsnd_mod_to_dma(mod),
Kuninori Morimoto29e69fd2014-05-08 01:59:26 -0700500 rsnd_info_is_playback(priv, src),
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800501 src->info->dma_id);
Kuninori Morimoto76c6fb52014-03-03 20:50:41 -0800502 if (ret < 0)
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800503 dev_err(dev, "SRC DMA failed\n");
Kuninori Morimoto76c6fb52014-03-03 20:50:41 -0800504
505 return ret;
506}
507
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800508static int rsnd_src_remove_gen2(struct rsnd_mod *mod,
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700509 struct rsnd_dai *rdai)
Kuninori Morimoto76c6fb52014-03-03 20:50:41 -0800510{
511 rsnd_dma_quit(rsnd_mod_to_priv(mod), rsnd_mod_to_dma(mod));
512
513 return 0;
514}
515
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800516static int rsnd_src_init_gen2(struct rsnd_mod *mod,
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700517 struct rsnd_dai *rdai)
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800518{
519 int ret;
520
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700521 ret = rsnd_src_init(mod, rdai);
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800522 if (ret < 0)
523 return ret;
524
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700525 ret = rsnd_src_set_convert_rate_gen2(mod, rdai);
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800526 if (ret < 0)
527 return ret;
528
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700529 ret = rsnd_src_set_convert_timing_gen2(mod, rdai);
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800530 if (ret < 0)
531 return ret;
532
533 return 0;
534}
535
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800536static int rsnd_src_start_gen2(struct rsnd_mod *mod,
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700537 struct rsnd_dai *rdai)
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800538{
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800539 struct rsnd_src *src = rsnd_mod_to_src(mod);
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800540
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800541 rsnd_dma_start(rsnd_mod_to_dma(&src->mod));
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800542
543 rsnd_mod_write(mod, SSI_CTRL, 0x1);
544 rsnd_mod_write(mod, SRC_CTRL, 0x11);
545
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700546 return rsnd_src_start(mod, rdai);
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800547}
548
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800549static int rsnd_src_stop_gen2(struct rsnd_mod *mod,
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700550 struct rsnd_dai *rdai)
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800551{
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800552 struct rsnd_src *src = rsnd_mod_to_src(mod);
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800553
554 rsnd_mod_write(mod, SSI_CTRL, 0);
555 rsnd_mod_write(mod, SRC_CTRL, 0);
556
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800557 rsnd_dma_stop(rsnd_mod_to_dma(&src->mod));
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800558
Kuninori Morimotob42fccf2014-05-08 17:44:29 -0700559 return rsnd_src_stop(mod, rdai);
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800560}
561
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800562static struct rsnd_mod_ops rsnd_src_gen2_ops = {
563 .name = "src (gen2)",
564 .probe = rsnd_src_probe_gen2,
565 .remove = rsnd_src_remove_gen2,
566 .init = rsnd_src_init_gen2,
567 .quit = rsnd_src_quit,
568 .start = rsnd_src_start_gen2,
569 .stop = rsnd_src_stop_gen2,
Kuninori Morimoto629509c2014-01-23 18:42:00 -0800570};
571
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800572struct rsnd_mod *rsnd_src_mod_get(struct rsnd_priv *priv, int id)
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700573{
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800574 if (WARN_ON(id < 0 || id >= rsnd_src_nr(priv)))
Takashi Iwai8b147192013-11-05 18:40:05 +0100575 id = 0;
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700576
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800577 return &((struct rsnd_src *)(priv->src) + id)->mod;
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700578}
579
Kuninori Morimoto90e8e502014-03-17 19:29:55 -0700580static void rsnd_of_parse_src(struct platform_device *pdev,
581 const struct rsnd_of_data *of_data,
582 struct rsnd_priv *priv)
583{
584 struct device_node *src_node;
585 struct rcar_snd_info *info = rsnd_priv_to_info(priv);
586 struct rsnd_src_platform_info *src_info;
587 struct device *dev = &pdev->dev;
588 int nr;
589
590 if (!of_data)
591 return;
592
593 src_node = of_get_child_by_name(dev->of_node, "rcar_sound,src");
594 if (!src_node)
595 return;
596
597 nr = of_get_child_count(src_node);
598 if (!nr)
599 return;
600
601 src_info = devm_kzalloc(dev,
602 sizeof(struct rsnd_src_platform_info) * nr,
603 GFP_KERNEL);
604 if (!src_info) {
605 dev_err(dev, "src info allocation error\n");
606 return;
607 }
608
609 info->src_info = src_info;
610 info->src_info_nr = nr;
611}
612
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800613int rsnd_src_probe(struct platform_device *pdev,
Kuninori Morimoto90e8e502014-03-17 19:29:55 -0700614 const struct rsnd_of_data *of_data,
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700615 struct rsnd_priv *priv)
616{
Kuninori Morimoto5da39cf2014-02-24 22:15:00 -0800617 struct rcar_snd_info *info = rsnd_priv_to_info(priv);
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700618 struct device *dev = rsnd_priv_to_dev(priv);
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800619 struct rsnd_src *src;
Kuninori Morimoto013f38f2014-01-23 18:38:26 -0800620 struct rsnd_mod_ops *ops;
Kuninori Morimotoef749402013-12-19 19:28:51 -0800621 struct clk *clk;
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800622 char name[RSND_SRC_NAME_SIZE];
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700623 int i, nr;
624
Kuninori Morimoto90e8e502014-03-17 19:29:55 -0700625 rsnd_of_parse_src(pdev, of_data, priv);
626
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700627 /*
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800628 * init SRC
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700629 */
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800630 nr = info->src_info_nr;
Kuninori Morimoto389933d2014-03-03 20:50:00 -0800631 if (!nr)
632 return 0;
633
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800634 src = devm_kzalloc(dev, sizeof(*src) * nr, GFP_KERNEL);
635 if (!src) {
636 dev_err(dev, "SRC allocate failed\n");
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700637 return -ENOMEM;
638 }
639
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800640 priv->src_nr = nr;
641 priv->src = src;
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700642
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800643 for_each_rsnd_src(src, priv, i) {
644 snprintf(name, RSND_SRC_NAME_SIZE, "src.%d", i);
Kuninori Morimotoef749402013-12-19 19:28:51 -0800645
646 clk = devm_clk_get(dev, name);
647 if (IS_ERR(clk))
648 return PTR_ERR(clk);
649
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800650 src->info = &info->src_info[i];
651 src->clk = clk;
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700652
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800653 ops = &rsnd_src_non_ops;
Kuninori Morimoto29e69fd2014-05-08 01:59:26 -0700654 if (rsnd_is_gen1(priv))
655 ops = &rsnd_src_gen1_ops;
656 if (rsnd_is_gen2(priv))
657 ops = &rsnd_src_gen2_ops;
Kuninori Morimoto013f38f2014-01-23 18:38:26 -0800658
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800659 rsnd_mod_init(priv, &src->mod, ops, RSND_MOD_SRC, i);
Kuninori Morimoto013f38f2014-01-23 18:38:26 -0800660
Kuninori Morimotoba9c9492014-03-03 20:51:21 -0800661 dev_dbg(dev, "SRC%d probed\n", i);
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700662 }
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700663
664 return 0;
665}