blob: 2f839f72c99c7929e0420b27e970e330b8d056cb [file] [log] [blame]
Kuninori Morimoto07539c12013-07-21 21:36:35 -07001/*
2 * Renesas R-Car SCU support
3 *
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
13struct rsnd_scu {
14 struct rsnd_scu_platform_info *info; /* rcar_snd.h */
15 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 Morimotoef749402013-12-19 19:28:51 -080019#define RSND_SCU_NAME_SIZE 16
Kuninori Morimoto374a52812013-07-28 18:59:12 -070020
21/*
22 * ADINR
23 */
24#define OTBL_24 (0 << 16)
25#define OTBL_22 (2 << 16)
26#define OTBL_20 (4 << 16)
27#define OTBL_18 (6 << 16)
28#define OTBL_16 (8 << 16)
29
Kuninori Morimoto39cf3c42014-01-23 18:40:47 -080030#define rsnd_scu_mode_flags(p) ((p)->info->flags)
31#define rsnd_scu_convert_rate(p) ((p)->info->convert_rate)
32#define rsnd_mod_to_scu(_mod) \
33 container_of((_mod), struct rsnd_scu, mod)
Kuninori Morimoto96c7c0d2014-01-23 18:40:54 -080034#define rsnd_scu_hpbif_is_enable(scu) \
35 (rsnd_scu_mode_flags(scu) & RSND_SCU_USE_HPBIF)
Kuninori Morimoto39cf3c42014-01-23 18:40:47 -080036
37#define for_each_rsnd_scu(pos, priv, i) \
38 for ((i) = 0; \
39 ((i) < rsnd_scu_nr(priv)) && \
40 ((pos) = (struct rsnd_scu *)(priv)->scu + i); \
41 i++)
42
43
Kuninori Morimotoef749402013-12-19 19:28:51 -080044/*
45 * image of SRC (Sampling Rate Converter)
46 *
47 * 96kHz <-> +-----+ 48kHz +-----+ 48kHz +-------+
48 * 48kHz <-> | SRC | <------> | SSI | <-----> | codec |
49 * 44.1kHz <-> +-----+ +-----+ +-------+
50 * ...
51 *
52 */
Kuninori Morimoto374a52812013-07-28 18:59:12 -070053
Kuninori Morimoto41c62212014-01-23 18:39:56 -080054/*
Kuninori Morimotoc926b742014-01-23 18:40:41 -080055 * scu.c is caring...
56 *
57 * Gen1
58 *
59 * [mem] -> [SRU] -> [SSI]
60 * |--------|
61 *
62 * Gen2
63 *
64 * [mem] -> [SCU] -> [SSIU] -> [SSI]
65 * |-----------------|
66 */
67
68/*
Kuninori Morimoto41c62212014-01-23 18:39:56 -080069 * How to use SRC bypass mode for debugging
70 *
71 * SRC has bypass mode, and it is useful for debugging.
72 * In Gen2 case,
73 * SRCm_MODE controls whether SRC is used or not
74 * SSI_MODE0 controls whether SSIU which receives SRC data
75 * is used or not.
76 * Both SRCm_MODE/SSI_MODE0 settings are needed if you use SRC,
77 * but SRC bypass mode needs SSI_MODE0 only.
78 *
79 * This driver request
80 * struct rsnd_scu_platform_info {
81 * u32 flags;
82 * u32 convert_rate;
83 * }
84 *
85 * rsnd_scu_hpbif_is_enable() will be true
86 * if flags had RSND_SCU_USE_HPBIF,
87 * and it controls whether SSIU is used or not.
88 *
89 * rsnd_scu_convert_rate() indicates
90 * above convert_rate, and it controls
91 * whether SRC is used or not.
92 *
93 * ex) doesn't use SRC
94 * struct rsnd_scu_platform_info info = {
95 * .flags = 0,
96 * .convert_rate = 0,
97 * };
98 *
99 * ex) uses SRC
100 * struct rsnd_scu_platform_info info = {
101 * .flags = RSND_SCU_USE_HPBIF,
102 * .convert_rate = 48000,
103 * };
104 *
105 * ex) uses SRC bypass mode
106 * struct rsnd_scu_platform_info info = {
107 * .flags = RSND_SCU_USE_HPBIF,
108 * .convert_rate = 0,
109 * };
110 *
111 */
112
Kuninori Morimoto7b5ce972014-01-23 18:39:32 -0800113static int rsnd_scu_ssi_mode_init(struct rsnd_mod *mod,
114 struct rsnd_dai *rdai,
115 struct rsnd_dai_stream *io)
116{
117 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
Kuninori Morimoto96c7c0d2014-01-23 18:40:54 -0800118 struct rsnd_scu *scu = rsnd_mod_to_scu(mod);
Kuninori Morimoto7b5ce972014-01-23 18:39:32 -0800119 int id = rsnd_mod_id(mod);
120
121 /*
122 * SSI_MODE0
123 */
124 rsnd_mod_bset(mod, SSI_MODE0, (1 << id),
Kuninori Morimoto96c7c0d2014-01-23 18:40:54 -0800125 rsnd_scu_hpbif_is_enable(scu) ? 0 : (1 << id));
Kuninori Morimoto7b5ce972014-01-23 18:39:32 -0800126
127 /*
128 * SSI_MODE1
129 */
130 if (rsnd_ssi_is_pin_sharing(rsnd_ssi_mod_get(priv, id))) {
131 int shift = -1;
132 switch (id) {
133 case 1:
134 shift = 0;
135 break;
136 case 2:
137 shift = 2;
138 break;
139 case 4:
140 shift = 16;
141 break;
142 }
143
144 if (shift >= 0)
145 rsnd_mod_bset(mod, SSI_MODE1,
146 0x3 << shift,
147 rsnd_dai_is_clk_master(rdai) ?
148 0x2 << shift : 0x1 << shift);
149 }
150
151 return 0;
152}
153
Kuninori Morimoto2582718c2013-12-19 19:27:37 -0800154/* Gen1 only */
Kuninori Morimoto47718dc2014-01-23 18:38:42 -0800155static int rsnd_src_set_route_if_gen1(
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700156 struct rsnd_mod *mod,
157 struct rsnd_dai *rdai,
158 struct rsnd_dai_stream *io)
159{
160 struct scu_route_config {
161 u32 mask;
162 int shift;
163 } routes[] = {
164 { 0xF, 0, }, /* 0 */
165 { 0xF, 4, }, /* 1 */
166 { 0xF, 8, }, /* 2 */
167 { 0x7, 12, }, /* 3 */
168 { 0x7, 16, }, /* 4 */
169 { 0x7, 20, }, /* 5 */
170 { 0x7, 24, }, /* 6 */
171 { 0x3, 28, }, /* 7 */
172 { 0x3, 30, }, /* 8 */
173 };
Kuninori Morimoto47718dc2014-01-23 18:38:42 -0800174 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
Kuninori Morimotoef749402013-12-19 19:28:51 -0800175 struct rsnd_scu *scu = rsnd_mod_to_scu(mod);
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700176 u32 mask;
177 u32 val;
178 int shift;
179 int id;
180
181 /*
182 * Gen1 only
183 */
184 if (!rsnd_is_gen1(priv))
185 return 0;
186
187 id = rsnd_mod_id(mod);
Dan Carpenterb5f3d7a2013-11-08 12:46:10 +0300188 if (id < 0 || id >= ARRAY_SIZE(routes))
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700189 return -EIO;
190
191 /*
192 * SRC_ROUTE_SELECT
193 */
194 val = rsnd_dai_is_play(rdai, io) ? 0x1 : 0x2;
195 val = val << routes[id].shift;
196 mask = routes[id].mask << routes[id].shift;
197
198 rsnd_mod_bset(mod, SRC_ROUTE_SEL, mask, val);
199
200 /*
201 * SRC_TIMING_SELECT
202 */
203 shift = (id % 4) * 8;
204 mask = 0x1F << shift;
Kuninori Morimotoef749402013-12-19 19:28:51 -0800205
206 /*
207 * ADG is used as source clock if SRC was used,
208 * then, SSI WS is used as destination clock.
209 * SSI WS is used as source clock if SRC is not used
210 * (when playback, source/destination become reverse when capture)
211 */
212 if (rsnd_scu_convert_rate(scu)) /* use ADG */
213 val = 0;
214 else if (8 == id) /* use SSI WS, but SRU8 is special */
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700215 val = id << shift;
Kuninori Morimotoef749402013-12-19 19:28:51 -0800216 else /* use SSI WS */
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700217 val = (id + 1) << shift;
218
219 switch (id / 4) {
220 case 0:
221 rsnd_mod_bset(mod, SRC_TMG_SEL0, mask, val);
222 break;
223 case 1:
224 rsnd_mod_bset(mod, SRC_TMG_SEL1, mask, val);
225 break;
226 case 2:
227 rsnd_mod_bset(mod, SRC_TMG_SEL2, mask, val);
228 break;
229 }
230
231 return 0;
232}
233
Kuninori Morimotoef749402013-12-19 19:28:51 -0800234unsigned int rsnd_scu_get_ssi_rate(struct rsnd_priv *priv,
235 struct rsnd_mod *ssi_mod,
236 struct snd_pcm_runtime *runtime)
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700237{
Kuninori Morimotoef749402013-12-19 19:28:51 -0800238 struct rsnd_scu *scu;
239 unsigned int rate;
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700240
Kuninori Morimotoef749402013-12-19 19:28:51 -0800241 /* this function is assuming SSI id = SCU id here */
242 scu = rsnd_mod_to_scu(rsnd_scu_mod_get(priv, rsnd_mod_id(ssi_mod)));
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700243
Kuninori Morimotoef749402013-12-19 19:28:51 -0800244 /*
245 * return convert rate if SRC is used,
246 * otherwise, return runtime->rate as usual
247 */
248 rate = rsnd_scu_convert_rate(scu);
249 if (!rate)
250 rate = runtime->rate;
251
252 return rate;
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700253}
254
Kuninori Morimotof80e1c92014-01-23 18:39:48 -0800255static int rsnd_scu_set_convert_rate(struct rsnd_mod *mod,
256 struct rsnd_dai *rdai,
257 struct rsnd_dai_stream *io)
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700258{
Kuninori Morimoto47718dc2014-01-23 18:38:42 -0800259 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700260 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
Kuninori Morimotoef749402013-12-19 19:28:51 -0800261 struct rsnd_scu *scu = rsnd_mod_to_scu(mod);
262 u32 convert_rate = rsnd_scu_convert_rate(scu);
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700263 u32 adinr = runtime->channels;
264
Kuninori Morimotoef749402013-12-19 19:28:51 -0800265 /* set/clear soft reset */
266 rsnd_mod_write(mod, SRC_SWRSR, 0);
267 rsnd_mod_write(mod, SRC_SWRSR, 1);
268
269 /* Initialize the operation of the SRC internal circuits */
270 rsnd_mod_write(mod, SRC_SRCIR, 1);
271
272 /* Set channel number and output bit length */
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700273 switch (runtime->sample_bits) {
274 case 16:
275 adinr |= OTBL_16;
276 break;
277 case 32:
278 adinr |= OTBL_24;
279 break;
280 default:
281 return -EIO;
282 }
Kuninori Morimoto690ef812013-12-19 19:27:03 -0800283 rsnd_mod_write(mod, SRC_ADINR, adinr);
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700284
Kuninori Morimotoef749402013-12-19 19:28:51 -0800285 if (convert_rate) {
286 u32 fsrate = 0x0400000 / convert_rate * runtime->rate;
287 int ret;
288
289 /* Enable the initial value of IFS */
290 rsnd_mod_write(mod, SRC_IFSCR, 1);
291
292 /* Set initial value of IFS */
293 rsnd_mod_write(mod, SRC_IFSVR, fsrate);
294
295 /* Select SRC mode (fixed value) */
296 rsnd_mod_write(mod, SRC_SRCCR, 0x00010110);
297
298 /* Set the restriction value of the FS ratio (98%) */
299 rsnd_mod_write(mod, SRC_MNFSR, fsrate / 100 * 98);
300
301 if (rsnd_is_gen1(priv)) {
302 /* no SRC_BFSSR settings, since SRC_SRCCR::BUFMD is 0 */
303 }
304
305 /* set convert clock */
306 ret = rsnd_adg_set_convert_clk(priv, mod,
307 runtime->rate,
308 convert_rate);
309 if (ret < 0)
310 return ret;
311 }
312
313 /* Cancel the initialization and operate the SRC function */
314 rsnd_mod_write(mod, SRC_SRCIR, 0);
315
316 /* use DMA transfer */
Kuninori Morimoto0290d2a2014-01-23 18:37:39 -0800317 rsnd_mod_write(mod, SRC_BUSIF_MODE, 1);
Kuninori Morimotoef749402013-12-19 19:28:51 -0800318
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700319 return 0;
320}
321
Kuninori Morimotoa204d902014-01-23 18:38:33 -0800322static int rsnd_scu_init(struct rsnd_mod *mod,
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700323 struct rsnd_dai *rdai,
324 struct rsnd_dai_stream *io)
325{
Kuninori Morimotoef749402013-12-19 19:28:51 -0800326 struct rsnd_scu *scu = rsnd_mod_to_scu(mod);
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700327 int ret;
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700328
Kuninori Morimotoef749402013-12-19 19:28:51 -0800329 clk_enable(scu->clk);
330
Kuninori Morimoto7b5ce972014-01-23 18:39:32 -0800331 ret = rsnd_scu_ssi_mode_init(mod, rdai, io);
332 if (ret < 0)
333 return ret;
334
Kuninori Morimoto47718dc2014-01-23 18:38:42 -0800335 ret = rsnd_src_set_route_if_gen1(mod, rdai, io);
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700336 if (ret < 0)
337 return ret;
338
Kuninori Morimotof80e1c92014-01-23 18:39:48 -0800339 ret = rsnd_scu_set_convert_rate(mod, rdai, io);
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700340 if (ret < 0)
341 return ret;
342
Kuninori Morimotoa204d902014-01-23 18:38:33 -0800343 return 0;
344}
345
346static int rsnd_scu_quit(struct rsnd_mod *mod,
347 struct rsnd_dai *rdai,
348 struct rsnd_dai_stream *io)
349{
350 struct rsnd_scu *scu = rsnd_mod_to_scu(mod);
351
352 clk_disable(scu->clk);
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700353
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700354 return 0;
355}
356
Kuninori Morimotoa204d902014-01-23 18:38:33 -0800357static int rsnd_scu_start(struct rsnd_mod *mod,
358 struct rsnd_dai *rdai,
359 struct rsnd_dai_stream *io)
360{
361 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
362 struct rsnd_scu *scu = rsnd_mod_to_scu(mod);
Kuninori Morimotoe7ce74e2014-01-23 18:38:50 -0800363 int id = rsnd_mod_id(mod);
Kuninori Morimotoa204d902014-01-23 18:38:33 -0800364
Kuninori Morimotoe7ce74e2014-01-23 18:38:50 -0800365 if (rsnd_is_gen1(priv))
366 rsnd_mod_bset(mod, SRC_ROUTE_CTRL, (1 << id), (1 << id));
367
368 if (rsnd_scu_convert_rate(scu))
369 rsnd_mod_write(mod, SRC_ROUTE_MODE0, 1);
370
371 return 0;
Kuninori Morimotoa204d902014-01-23 18:38:33 -0800372}
373
Kuninori Morimotoef749402013-12-19 19:28:51 -0800374static int rsnd_scu_stop(struct rsnd_mod *mod,
375 struct rsnd_dai *rdai,
376 struct rsnd_dai_stream *io)
377{
378 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
379 struct rsnd_scu *scu = rsnd_mod_to_scu(mod);
Kuninori Morimotoe7ce74e2014-01-23 18:38:50 -0800380 int id = rsnd_mod_id(mod);
Kuninori Morimotoef749402013-12-19 19:28:51 -0800381
Kuninori Morimotoe7ce74e2014-01-23 18:38:50 -0800382 if (rsnd_is_gen1(priv))
383 rsnd_mod_bset(mod, SRC_ROUTE_CTRL, (1 << id), 0);
384
385 if (rsnd_scu_convert_rate(scu))
386 rsnd_mod_write(mod, SRC_ROUTE_MODE0, 0);
Kuninori Morimotoef749402013-12-19 19:28:51 -0800387
Kuninori Morimotoef749402013-12-19 19:28:51 -0800388 return 0;
389}
390
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700391static struct rsnd_mod_ops rsnd_scu_ops = {
392 .name = "scu",
Kuninori Morimotoa204d902014-01-23 18:38:33 -0800393 .init = rsnd_scu_init,
394 .quit = rsnd_scu_quit,
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700395 .start = rsnd_scu_start,
Kuninori Morimotoef749402013-12-19 19:28:51 -0800396 .stop = rsnd_scu_stop,
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700397};
398
Kuninori Morimoto013f38f2014-01-23 18:38:26 -0800399static struct rsnd_mod_ops rsnd_scu_non_ops = {
400 .name = "scu (non)",
Kuninori Morimoto7b5ce972014-01-23 18:39:32 -0800401 .init = rsnd_scu_ssi_mode_init,
Kuninori Morimoto013f38f2014-01-23 18:38:26 -0800402};
403
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700404struct rsnd_mod *rsnd_scu_mod_get(struct rsnd_priv *priv, int id)
405{
Takashi Iwai8b147192013-11-05 18:40:05 +0100406 if (WARN_ON(id < 0 || id >= rsnd_scu_nr(priv)))
407 id = 0;
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700408
409 return &((struct rsnd_scu *)(priv->scu) + id)->mod;
410}
411
412int rsnd_scu_probe(struct platform_device *pdev,
413 struct rcar_snd_info *info,
414 struct rsnd_priv *priv)
415{
416 struct device *dev = rsnd_priv_to_dev(priv);
417 struct rsnd_scu *scu;
Kuninori Morimoto013f38f2014-01-23 18:38:26 -0800418 struct rsnd_mod_ops *ops;
Kuninori Morimotoef749402013-12-19 19:28:51 -0800419 struct clk *clk;
420 char name[RSND_SCU_NAME_SIZE];
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700421 int i, nr;
422
423 /*
424 * init SCU
425 */
426 nr = info->scu_info_nr;
427 scu = devm_kzalloc(dev, sizeof(*scu) * nr, GFP_KERNEL);
428 if (!scu) {
429 dev_err(dev, "SCU allocate failed\n");
430 return -ENOMEM;
431 }
432
433 priv->scu_nr = nr;
434 priv->scu = scu;
435
436 for_each_rsnd_scu(scu, priv, i) {
Kuninori Morimotoef749402013-12-19 19:28:51 -0800437 snprintf(name, RSND_SCU_NAME_SIZE, "scu.%d", i);
438
439 clk = devm_clk_get(dev, name);
440 if (IS_ERR(clk))
441 return PTR_ERR(clk);
442
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700443 scu->info = &info->scu_info[i];
Kuninori Morimotoef749402013-12-19 19:28:51 -0800444 scu->clk = clk;
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700445
Kuninori Morimoto013f38f2014-01-23 18:38:26 -0800446 ops = &rsnd_scu_non_ops;
Kuninori Morimoto96c7c0d2014-01-23 18:40:54 -0800447 if (rsnd_scu_hpbif_is_enable(scu))
Kuninori Morimoto013f38f2014-01-23 18:38:26 -0800448 ops = &rsnd_scu_ops;
449
450 rsnd_mod_init(priv, &scu->mod, ops, i);
451
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700452 dev_dbg(dev, "SCU%d probed\n", i);
453 }
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700454 dev_dbg(dev, "scu probed\n");
455
456 return 0;
457}
458
459void rsnd_scu_remove(struct platform_device *pdev,
460 struct rsnd_priv *priv)
461{
462}