blob: 1adc85825f4eb0966b56af6afcb3c3f4f76a9a03 [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 Morimoto374a52812013-07-28 18:59:12 -070019#define rsnd_scu_mode_flags(p) ((p)->info->flags)
Kuninori Morimotoef749402013-12-19 19:28:51 -080020#define rsnd_scu_convert_rate(p) ((p)->info->convert_rate)
21
22#define RSND_SCU_NAME_SIZE 16
Kuninori Morimoto374a52812013-07-28 18:59:12 -070023
24/*
25 * ADINR
26 */
27#define OTBL_24 (0 << 16)
28#define OTBL_22 (2 << 16)
29#define OTBL_20 (4 << 16)
30#define OTBL_18 (6 << 16)
31#define OTBL_16 (8 << 16)
32
Kuninori Morimotoef749402013-12-19 19:28:51 -080033/*
34 * image of SRC (Sampling Rate Converter)
35 *
36 * 96kHz <-> +-----+ 48kHz +-----+ 48kHz +-------+
37 * 48kHz <-> | SRC | <------> | SSI | <-----> | codec |
38 * 44.1kHz <-> +-----+ +-----+ +-------+
39 * ...
40 *
41 */
Kuninori Morimoto374a52812013-07-28 18:59:12 -070042
Kuninori Morimoto41c62212014-01-23 18:39:56 -080043/*
Kuninori Morimotoc926b742014-01-23 18:40:41 -080044 * scu.c is caring...
45 *
46 * Gen1
47 *
48 * [mem] -> [SRU] -> [SSI]
49 * |--------|
50 *
51 * Gen2
52 *
53 * [mem] -> [SCU] -> [SSIU] -> [SSI]
54 * |-----------------|
55 */
56
57/*
Kuninori Morimoto41c62212014-01-23 18:39:56 -080058 * How to use SRC bypass mode for debugging
59 *
60 * SRC has bypass mode, and it is useful for debugging.
61 * In Gen2 case,
62 * SRCm_MODE controls whether SRC is used or not
63 * SSI_MODE0 controls whether SSIU which receives SRC data
64 * is used or not.
65 * Both SRCm_MODE/SSI_MODE0 settings are needed if you use SRC,
66 * but SRC bypass mode needs SSI_MODE0 only.
67 *
68 * This driver request
69 * struct rsnd_scu_platform_info {
70 * u32 flags;
71 * u32 convert_rate;
72 * }
73 *
74 * rsnd_scu_hpbif_is_enable() will be true
75 * if flags had RSND_SCU_USE_HPBIF,
76 * and it controls whether SSIU is used or not.
77 *
78 * rsnd_scu_convert_rate() indicates
79 * above convert_rate, and it controls
80 * whether SRC is used or not.
81 *
82 * ex) doesn't use SRC
83 * struct rsnd_scu_platform_info info = {
84 * .flags = 0,
85 * .convert_rate = 0,
86 * };
87 *
88 * ex) uses SRC
89 * struct rsnd_scu_platform_info info = {
90 * .flags = RSND_SCU_USE_HPBIF,
91 * .convert_rate = 48000,
92 * };
93 *
94 * ex) uses SRC bypass mode
95 * struct rsnd_scu_platform_info info = {
96 * .flags = RSND_SCU_USE_HPBIF,
97 * .convert_rate = 0,
98 * };
99 *
100 */
101
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700102#define rsnd_mod_to_scu(_mod) \
103 container_of((_mod), struct rsnd_scu, mod)
104
105#define for_each_rsnd_scu(pos, priv, i) \
106 for ((i) = 0; \
107 ((i) < rsnd_scu_nr(priv)) && \
108 ((pos) = (struct rsnd_scu *)(priv)->scu + i); \
109 i++)
110
Kuninori Morimoto7b5ce972014-01-23 18:39:32 -0800111static int rsnd_scu_ssi_mode_init(struct rsnd_mod *mod,
112 struct rsnd_dai *rdai,
113 struct rsnd_dai_stream *io)
114{
115 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
116 int id = rsnd_mod_id(mod);
117
118 /*
119 * SSI_MODE0
120 */
121 rsnd_mod_bset(mod, SSI_MODE0, (1 << id),
122 rsnd_scu_hpbif_is_enable(mod) ? 0 : (1 << id));
123
124 /*
125 * SSI_MODE1
126 */
127 if (rsnd_ssi_is_pin_sharing(rsnd_ssi_mod_get(priv, id))) {
128 int shift = -1;
129 switch (id) {
130 case 1:
131 shift = 0;
132 break;
133 case 2:
134 shift = 2;
135 break;
136 case 4:
137 shift = 16;
138 break;
139 }
140
141 if (shift >= 0)
142 rsnd_mod_bset(mod, SSI_MODE1,
143 0x3 << shift,
144 rsnd_dai_is_clk_master(rdai) ?
145 0x2 << shift : 0x1 << shift);
146 }
147
148 return 0;
149}
150
Kuninori Morimoto2582718c2013-12-19 19:27:37 -0800151/* Gen1 only */
Kuninori Morimoto47718dc2014-01-23 18:38:42 -0800152static int rsnd_src_set_route_if_gen1(
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700153 struct rsnd_mod *mod,
154 struct rsnd_dai *rdai,
155 struct rsnd_dai_stream *io)
156{
157 struct scu_route_config {
158 u32 mask;
159 int shift;
160 } routes[] = {
161 { 0xF, 0, }, /* 0 */
162 { 0xF, 4, }, /* 1 */
163 { 0xF, 8, }, /* 2 */
164 { 0x7, 12, }, /* 3 */
165 { 0x7, 16, }, /* 4 */
166 { 0x7, 20, }, /* 5 */
167 { 0x7, 24, }, /* 6 */
168 { 0x3, 28, }, /* 7 */
169 { 0x3, 30, }, /* 8 */
170 };
Kuninori Morimoto47718dc2014-01-23 18:38:42 -0800171 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
Kuninori Morimotoef749402013-12-19 19:28:51 -0800172 struct rsnd_scu *scu = rsnd_mod_to_scu(mod);
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700173 u32 mask;
174 u32 val;
175 int shift;
176 int id;
177
178 /*
179 * Gen1 only
180 */
181 if (!rsnd_is_gen1(priv))
182 return 0;
183
184 id = rsnd_mod_id(mod);
Dan Carpenterb5f3d7a2013-11-08 12:46:10 +0300185 if (id < 0 || id >= ARRAY_SIZE(routes))
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700186 return -EIO;
187
188 /*
189 * SRC_ROUTE_SELECT
190 */
191 val = rsnd_dai_is_play(rdai, io) ? 0x1 : 0x2;
192 val = val << routes[id].shift;
193 mask = routes[id].mask << routes[id].shift;
194
195 rsnd_mod_bset(mod, SRC_ROUTE_SEL, mask, val);
196
197 /*
198 * SRC_TIMING_SELECT
199 */
200 shift = (id % 4) * 8;
201 mask = 0x1F << shift;
Kuninori Morimotoef749402013-12-19 19:28:51 -0800202
203 /*
204 * ADG is used as source clock if SRC was used,
205 * then, SSI WS is used as destination clock.
206 * SSI WS is used as source clock if SRC is not used
207 * (when playback, source/destination become reverse when capture)
208 */
209 if (rsnd_scu_convert_rate(scu)) /* use ADG */
210 val = 0;
211 else if (8 == id) /* use SSI WS, but SRU8 is special */
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700212 val = id << shift;
Kuninori Morimotoef749402013-12-19 19:28:51 -0800213 else /* use SSI WS */
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700214 val = (id + 1) << shift;
215
216 switch (id / 4) {
217 case 0:
218 rsnd_mod_bset(mod, SRC_TMG_SEL0, mask, val);
219 break;
220 case 1:
221 rsnd_mod_bset(mod, SRC_TMG_SEL1, mask, val);
222 break;
223 case 2:
224 rsnd_mod_bset(mod, SRC_TMG_SEL2, mask, val);
225 break;
226 }
227
228 return 0;
229}
230
Kuninori Morimotoef749402013-12-19 19:28:51 -0800231unsigned int rsnd_scu_get_ssi_rate(struct rsnd_priv *priv,
232 struct rsnd_mod *ssi_mod,
233 struct snd_pcm_runtime *runtime)
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700234{
Kuninori Morimotoef749402013-12-19 19:28:51 -0800235 struct rsnd_scu *scu;
236 unsigned int rate;
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700237
Kuninori Morimotoef749402013-12-19 19:28:51 -0800238 /* this function is assuming SSI id = SCU id here */
239 scu = rsnd_mod_to_scu(rsnd_scu_mod_get(priv, rsnd_mod_id(ssi_mod)));
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700240
Kuninori Morimotoef749402013-12-19 19:28:51 -0800241 /*
242 * return convert rate if SRC is used,
243 * otherwise, return runtime->rate as usual
244 */
245 rate = rsnd_scu_convert_rate(scu);
246 if (!rate)
247 rate = runtime->rate;
248
249 return rate;
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700250}
251
Kuninori Morimotof80e1c92014-01-23 18:39:48 -0800252static int rsnd_scu_set_convert_rate(struct rsnd_mod *mod,
253 struct rsnd_dai *rdai,
254 struct rsnd_dai_stream *io)
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700255{
Kuninori Morimoto47718dc2014-01-23 18:38:42 -0800256 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700257 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
Kuninori Morimotoef749402013-12-19 19:28:51 -0800258 struct rsnd_scu *scu = rsnd_mod_to_scu(mod);
259 u32 convert_rate = rsnd_scu_convert_rate(scu);
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700260 u32 adinr = runtime->channels;
261
Kuninori Morimotoef749402013-12-19 19:28:51 -0800262 /* set/clear soft reset */
263 rsnd_mod_write(mod, SRC_SWRSR, 0);
264 rsnd_mod_write(mod, SRC_SWRSR, 1);
265
266 /* Initialize the operation of the SRC internal circuits */
267 rsnd_mod_write(mod, SRC_SRCIR, 1);
268
269 /* Set channel number and output bit length */
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700270 switch (runtime->sample_bits) {
271 case 16:
272 adinr |= OTBL_16;
273 break;
274 case 32:
275 adinr |= OTBL_24;
276 break;
277 default:
278 return -EIO;
279 }
Kuninori Morimoto690ef812013-12-19 19:27:03 -0800280 rsnd_mod_write(mod, SRC_ADINR, adinr);
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700281
Kuninori Morimotoef749402013-12-19 19:28:51 -0800282 if (convert_rate) {
283 u32 fsrate = 0x0400000 / convert_rate * runtime->rate;
284 int ret;
285
286 /* Enable the initial value of IFS */
287 rsnd_mod_write(mod, SRC_IFSCR, 1);
288
289 /* Set initial value of IFS */
290 rsnd_mod_write(mod, SRC_IFSVR, fsrate);
291
292 /* Select SRC mode (fixed value) */
293 rsnd_mod_write(mod, SRC_SRCCR, 0x00010110);
294
295 /* Set the restriction value of the FS ratio (98%) */
296 rsnd_mod_write(mod, SRC_MNFSR, fsrate / 100 * 98);
297
298 if (rsnd_is_gen1(priv)) {
299 /* no SRC_BFSSR settings, since SRC_SRCCR::BUFMD is 0 */
300 }
301
302 /* set convert clock */
303 ret = rsnd_adg_set_convert_clk(priv, mod,
304 runtime->rate,
305 convert_rate);
306 if (ret < 0)
307 return ret;
308 }
309
310 /* Cancel the initialization and operate the SRC function */
311 rsnd_mod_write(mod, SRC_SRCIR, 0);
312
313 /* use DMA transfer */
Kuninori Morimoto0290d2a2014-01-23 18:37:39 -0800314 rsnd_mod_write(mod, SRC_BUSIF_MODE, 1);
Kuninori Morimotoef749402013-12-19 19:28:51 -0800315
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700316 return 0;
317}
318
Kuninori Morimotocdcfcac2013-10-17 22:50:59 -0700319bool rsnd_scu_hpbif_is_enable(struct rsnd_mod *mod)
320{
321 struct rsnd_scu *scu = rsnd_mod_to_scu(mod);
322 u32 flags = rsnd_scu_mode_flags(scu);
323
324 return !!(flags & RSND_SCU_USE_HPBIF);
325}
326
Kuninori Morimotoa204d902014-01-23 18:38:33 -0800327static int rsnd_scu_init(struct rsnd_mod *mod,
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700328 struct rsnd_dai *rdai,
329 struct rsnd_dai_stream *io)
330{
Kuninori Morimotoef749402013-12-19 19:28:51 -0800331 struct rsnd_scu *scu = rsnd_mod_to_scu(mod);
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700332 int ret;
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700333
Kuninori Morimotoef749402013-12-19 19:28:51 -0800334 clk_enable(scu->clk);
335
Kuninori Morimoto7b5ce972014-01-23 18:39:32 -0800336 ret = rsnd_scu_ssi_mode_init(mod, rdai, io);
337 if (ret < 0)
338 return ret;
339
Kuninori Morimoto47718dc2014-01-23 18:38:42 -0800340 ret = rsnd_src_set_route_if_gen1(mod, rdai, io);
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700341 if (ret < 0)
342 return ret;
343
Kuninori Morimotof80e1c92014-01-23 18:39:48 -0800344 ret = rsnd_scu_set_convert_rate(mod, rdai, io);
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700345 if (ret < 0)
346 return ret;
347
Kuninori Morimotoa204d902014-01-23 18:38:33 -0800348 return 0;
349}
350
351static int rsnd_scu_quit(struct rsnd_mod *mod,
352 struct rsnd_dai *rdai,
353 struct rsnd_dai_stream *io)
354{
355 struct rsnd_scu *scu = rsnd_mod_to_scu(mod);
356
357 clk_disable(scu->clk);
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700358
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700359 return 0;
360}
361
Kuninori Morimotoa204d902014-01-23 18:38:33 -0800362static int rsnd_scu_start(struct rsnd_mod *mod,
363 struct rsnd_dai *rdai,
364 struct rsnd_dai_stream *io)
365{
366 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
367 struct rsnd_scu *scu = rsnd_mod_to_scu(mod);
Kuninori Morimotoe7ce74e2014-01-23 18:38:50 -0800368 int id = rsnd_mod_id(mod);
Kuninori Morimotoa204d902014-01-23 18:38:33 -0800369
Kuninori Morimotoe7ce74e2014-01-23 18:38:50 -0800370 if (rsnd_is_gen1(priv))
371 rsnd_mod_bset(mod, SRC_ROUTE_CTRL, (1 << id), (1 << id));
372
373 if (rsnd_scu_convert_rate(scu))
374 rsnd_mod_write(mod, SRC_ROUTE_MODE0, 1);
375
376 return 0;
Kuninori Morimotoa204d902014-01-23 18:38:33 -0800377}
378
Kuninori Morimotoef749402013-12-19 19:28:51 -0800379static int rsnd_scu_stop(struct rsnd_mod *mod,
380 struct rsnd_dai *rdai,
381 struct rsnd_dai_stream *io)
382{
383 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
384 struct rsnd_scu *scu = rsnd_mod_to_scu(mod);
Kuninori Morimotoe7ce74e2014-01-23 18:38:50 -0800385 int id = rsnd_mod_id(mod);
Kuninori Morimotoef749402013-12-19 19:28:51 -0800386
Kuninori Morimotoe7ce74e2014-01-23 18:38:50 -0800387 if (rsnd_is_gen1(priv))
388 rsnd_mod_bset(mod, SRC_ROUTE_CTRL, (1 << id), 0);
389
390 if (rsnd_scu_convert_rate(scu))
391 rsnd_mod_write(mod, SRC_ROUTE_MODE0, 0);
Kuninori Morimotoef749402013-12-19 19:28:51 -0800392
Kuninori Morimotoef749402013-12-19 19:28:51 -0800393 return 0;
394}
395
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700396static struct rsnd_mod_ops rsnd_scu_ops = {
397 .name = "scu",
Kuninori Morimotoa204d902014-01-23 18:38:33 -0800398 .init = rsnd_scu_init,
399 .quit = rsnd_scu_quit,
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700400 .start = rsnd_scu_start,
Kuninori Morimotoef749402013-12-19 19:28:51 -0800401 .stop = rsnd_scu_stop,
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700402};
403
Kuninori Morimoto013f38f2014-01-23 18:38:26 -0800404static struct rsnd_mod_ops rsnd_scu_non_ops = {
405 .name = "scu (non)",
Kuninori Morimoto7b5ce972014-01-23 18:39:32 -0800406 .init = rsnd_scu_ssi_mode_init,
Kuninori Morimoto013f38f2014-01-23 18:38:26 -0800407};
408
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700409struct rsnd_mod *rsnd_scu_mod_get(struct rsnd_priv *priv, int id)
410{
Takashi Iwai8b147192013-11-05 18:40:05 +0100411 if (WARN_ON(id < 0 || id >= rsnd_scu_nr(priv)))
412 id = 0;
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700413
414 return &((struct rsnd_scu *)(priv->scu) + id)->mod;
415}
416
417int rsnd_scu_probe(struct platform_device *pdev,
418 struct rcar_snd_info *info,
419 struct rsnd_priv *priv)
420{
421 struct device *dev = rsnd_priv_to_dev(priv);
422 struct rsnd_scu *scu;
Kuninori Morimoto013f38f2014-01-23 18:38:26 -0800423 struct rsnd_mod_ops *ops;
Kuninori Morimotoef749402013-12-19 19:28:51 -0800424 struct clk *clk;
425 char name[RSND_SCU_NAME_SIZE];
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700426 int i, nr;
427
428 /*
429 * init SCU
430 */
431 nr = info->scu_info_nr;
432 scu = devm_kzalloc(dev, sizeof(*scu) * nr, GFP_KERNEL);
433 if (!scu) {
434 dev_err(dev, "SCU allocate failed\n");
435 return -ENOMEM;
436 }
437
438 priv->scu_nr = nr;
439 priv->scu = scu;
440
441 for_each_rsnd_scu(scu, priv, i) {
Kuninori Morimotoef749402013-12-19 19:28:51 -0800442 snprintf(name, RSND_SCU_NAME_SIZE, "scu.%d", i);
443
444 clk = devm_clk_get(dev, name);
445 if (IS_ERR(clk))
446 return PTR_ERR(clk);
447
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700448 scu->info = &info->scu_info[i];
Kuninori Morimotoef749402013-12-19 19:28:51 -0800449 scu->clk = clk;
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700450
Kuninori Morimoto013f38f2014-01-23 18:38:26 -0800451 ops = &rsnd_scu_non_ops;
452 if (rsnd_scu_hpbif_is_enable(&scu->mod))
453 ops = &rsnd_scu_ops;
454
455 rsnd_mod_init(priv, &scu->mod, ops, i);
456
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700457 dev_dbg(dev, "SCU%d probed\n", i);
458 }
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700459 dev_dbg(dev, "scu probed\n");
460
461 return 0;
462}
463
464void rsnd_scu_remove(struct platform_device *pdev,
465 struct rsnd_priv *priv)
466{
467}