blob: e2ffcc415057022c5b64ade7e910d605c6cf28d9 [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 Morimoto374a52812013-07-28 18:59:12 -0700175 u32 mask;
176 u32 val;
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700177 int id;
178
179 /*
180 * Gen1 only
181 */
182 if (!rsnd_is_gen1(priv))
183 return 0;
184
185 id = rsnd_mod_id(mod);
Dan Carpenterb5f3d7a2013-11-08 12:46:10 +0300186 if (id < 0 || id >= ARRAY_SIZE(routes))
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700187 return -EIO;
188
189 /*
190 * SRC_ROUTE_SELECT
191 */
192 val = rsnd_dai_is_play(rdai, io) ? 0x1 : 0x2;
193 val = val << routes[id].shift;
194 mask = routes[id].mask << routes[id].shift;
195
196 rsnd_mod_bset(mod, SRC_ROUTE_SEL, mask, val);
197
Kuninori Morimoto28dc4b62014-01-23 18:41:10 -0800198 return 0;
199}
200
201static int rsnd_scu_set_convert_timing_gen1(struct rsnd_mod *mod,
202 struct rsnd_dai *rdai,
203 struct rsnd_dai_stream *io)
204{
205 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
206 struct rsnd_scu *scu = rsnd_mod_to_scu(mod);
207 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
208 u32 convert_rate = rsnd_scu_convert_rate(scu);
209 u32 mask;
210 u32 val;
211 int shift;
212 int id = rsnd_mod_id(mod);
213 int ret;
214
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700215 /*
216 * SRC_TIMING_SELECT
217 */
218 shift = (id % 4) * 8;
219 mask = 0x1F << shift;
Kuninori Morimotoef749402013-12-19 19:28:51 -0800220
221 /*
222 * ADG is used as source clock if SRC was used,
223 * then, SSI WS is used as destination clock.
224 * SSI WS is used as source clock if SRC is not used
225 * (when playback, source/destination become reverse when capture)
226 */
Kuninori Morimoto28dc4b62014-01-23 18:41:10 -0800227 ret = 0;
228 if (convert_rate) {
229 /* use ADG */
Kuninori Morimotoef749402013-12-19 19:28:51 -0800230 val = 0;
Kuninori Morimoto28dc4b62014-01-23 18:41:10 -0800231 ret = rsnd_adg_set_convert_clk_gen1(priv, mod,
232 runtime->rate,
233 convert_rate);
234 } else if (8 == id) {
235 /* use SSI WS, but SRU8 is special */
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700236 val = id << shift;
Kuninori Morimoto28dc4b62014-01-23 18:41:10 -0800237 } else {
238 /* use SSI WS */
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700239 val = (id + 1) << shift;
Kuninori Morimoto28dc4b62014-01-23 18:41:10 -0800240 }
241
242 if (ret < 0)
243 return ret;
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700244
245 switch (id / 4) {
246 case 0:
247 rsnd_mod_bset(mod, SRC_TMG_SEL0, mask, val);
248 break;
249 case 1:
250 rsnd_mod_bset(mod, SRC_TMG_SEL1, mask, val);
251 break;
252 case 2:
253 rsnd_mod_bset(mod, SRC_TMG_SEL2, mask, val);
254 break;
255 }
256
257 return 0;
258}
259
Kuninori Morimotoef749402013-12-19 19:28:51 -0800260unsigned int rsnd_scu_get_ssi_rate(struct rsnd_priv *priv,
261 struct rsnd_mod *ssi_mod,
262 struct snd_pcm_runtime *runtime)
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700263{
Kuninori Morimotoef749402013-12-19 19:28:51 -0800264 struct rsnd_scu *scu;
265 unsigned int rate;
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700266
Kuninori Morimotoef749402013-12-19 19:28:51 -0800267 /* this function is assuming SSI id = SCU id here */
268 scu = rsnd_mod_to_scu(rsnd_scu_mod_get(priv, rsnd_mod_id(ssi_mod)));
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700269
Kuninori Morimotoef749402013-12-19 19:28:51 -0800270 /*
271 * return convert rate if SRC is used,
272 * otherwise, return runtime->rate as usual
273 */
274 rate = rsnd_scu_convert_rate(scu);
275 if (!rate)
276 rate = runtime->rate;
277
278 return rate;
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700279}
280
Kuninori Morimotof80e1c92014-01-23 18:39:48 -0800281static int rsnd_scu_set_convert_rate(struct rsnd_mod *mod,
282 struct rsnd_dai *rdai,
283 struct rsnd_dai_stream *io)
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700284{
Kuninori Morimoto47718dc2014-01-23 18:38:42 -0800285 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700286 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
Kuninori Morimotoef749402013-12-19 19:28:51 -0800287 struct rsnd_scu *scu = rsnd_mod_to_scu(mod);
288 u32 convert_rate = rsnd_scu_convert_rate(scu);
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700289 u32 adinr = runtime->channels;
290
Kuninori Morimotoef749402013-12-19 19:28:51 -0800291 /* set/clear soft reset */
292 rsnd_mod_write(mod, SRC_SWRSR, 0);
293 rsnd_mod_write(mod, SRC_SWRSR, 1);
294
295 /* Initialize the operation of the SRC internal circuits */
296 rsnd_mod_write(mod, SRC_SRCIR, 1);
297
298 /* Set channel number and output bit length */
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700299 switch (runtime->sample_bits) {
300 case 16:
301 adinr |= OTBL_16;
302 break;
303 case 32:
304 adinr |= OTBL_24;
305 break;
306 default:
307 return -EIO;
308 }
Kuninori Morimoto690ef812013-12-19 19:27:03 -0800309 rsnd_mod_write(mod, SRC_ADINR, adinr);
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700310
Kuninori Morimotoef749402013-12-19 19:28:51 -0800311 if (convert_rate) {
312 u32 fsrate = 0x0400000 / convert_rate * runtime->rate;
Kuninori Morimotoef749402013-12-19 19:28:51 -0800313
314 /* Enable the initial value of IFS */
315 rsnd_mod_write(mod, SRC_IFSCR, 1);
316
317 /* Set initial value of IFS */
318 rsnd_mod_write(mod, SRC_IFSVR, fsrate);
319
320 /* Select SRC mode (fixed value) */
321 rsnd_mod_write(mod, SRC_SRCCR, 0x00010110);
322
323 /* Set the restriction value of the FS ratio (98%) */
324 rsnd_mod_write(mod, SRC_MNFSR, fsrate / 100 * 98);
325
326 if (rsnd_is_gen1(priv)) {
327 /* no SRC_BFSSR settings, since SRC_SRCCR::BUFMD is 0 */
328 }
Kuninori Morimotoef749402013-12-19 19:28:51 -0800329 }
330
331 /* Cancel the initialization and operate the SRC function */
332 rsnd_mod_write(mod, SRC_SRCIR, 0);
333
334 /* use DMA transfer */
Kuninori Morimoto0290d2a2014-01-23 18:37:39 -0800335 rsnd_mod_write(mod, SRC_BUSIF_MODE, 1);
Kuninori Morimotoef749402013-12-19 19:28:51 -0800336
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700337 return 0;
338}
339
Kuninori Morimotoa204d902014-01-23 18:38:33 -0800340static int rsnd_scu_init(struct rsnd_mod *mod,
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700341 struct rsnd_dai *rdai,
342 struct rsnd_dai_stream *io)
343{
Kuninori Morimotoef749402013-12-19 19:28:51 -0800344 struct rsnd_scu *scu = rsnd_mod_to_scu(mod);
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700345 int ret;
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700346
Kuninori Morimotoef749402013-12-19 19:28:51 -0800347 clk_enable(scu->clk);
348
Kuninori Morimoto7b5ce972014-01-23 18:39:32 -0800349 ret = rsnd_scu_ssi_mode_init(mod, rdai, io);
350 if (ret < 0)
351 return ret;
352
Kuninori Morimoto47718dc2014-01-23 18:38:42 -0800353 ret = rsnd_src_set_route_if_gen1(mod, rdai, io);
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700354 if (ret < 0)
355 return ret;
356
Kuninori Morimotof80e1c92014-01-23 18:39:48 -0800357 ret = rsnd_scu_set_convert_rate(mod, rdai, io);
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700358 if (ret < 0)
359 return ret;
360
Kuninori Morimoto28dc4b62014-01-23 18:41:10 -0800361 ret = rsnd_scu_set_convert_timing_gen1(mod, rdai, io);
362 if (ret < 0)
363 return ret;
364
Kuninori Morimotoa204d902014-01-23 18:38:33 -0800365 return 0;
366}
367
368static int rsnd_scu_quit(struct rsnd_mod *mod,
369 struct rsnd_dai *rdai,
370 struct rsnd_dai_stream *io)
371{
372 struct rsnd_scu *scu = rsnd_mod_to_scu(mod);
373
374 clk_disable(scu->clk);
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700375
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700376 return 0;
377}
378
Kuninori Morimotoa204d902014-01-23 18:38:33 -0800379static int rsnd_scu_start(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 Morimotoa204d902014-01-23 18:38:33 -0800386
Kuninori Morimotoe7ce74e2014-01-23 18:38:50 -0800387 if (rsnd_is_gen1(priv))
388 rsnd_mod_bset(mod, SRC_ROUTE_CTRL, (1 << id), (1 << id));
389
390 if (rsnd_scu_convert_rate(scu))
391 rsnd_mod_write(mod, SRC_ROUTE_MODE0, 1);
392
393 return 0;
Kuninori Morimotoa204d902014-01-23 18:38:33 -0800394}
395
Kuninori Morimotoef749402013-12-19 19:28:51 -0800396static int rsnd_scu_stop(struct rsnd_mod *mod,
397 struct rsnd_dai *rdai,
398 struct rsnd_dai_stream *io)
399{
400 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
401 struct rsnd_scu *scu = rsnd_mod_to_scu(mod);
Kuninori Morimotoe7ce74e2014-01-23 18:38:50 -0800402 int id = rsnd_mod_id(mod);
Kuninori Morimotoef749402013-12-19 19:28:51 -0800403
Kuninori Morimotoe7ce74e2014-01-23 18:38:50 -0800404 if (rsnd_is_gen1(priv))
405 rsnd_mod_bset(mod, SRC_ROUTE_CTRL, (1 << id), 0);
406
407 if (rsnd_scu_convert_rate(scu))
408 rsnd_mod_write(mod, SRC_ROUTE_MODE0, 0);
Kuninori Morimotoef749402013-12-19 19:28:51 -0800409
Kuninori Morimotoef749402013-12-19 19:28:51 -0800410 return 0;
411}
412
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700413static struct rsnd_mod_ops rsnd_scu_ops = {
414 .name = "scu",
Kuninori Morimotoa204d902014-01-23 18:38:33 -0800415 .init = rsnd_scu_init,
416 .quit = rsnd_scu_quit,
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700417 .start = rsnd_scu_start,
Kuninori Morimotoef749402013-12-19 19:28:51 -0800418 .stop = rsnd_scu_stop,
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700419};
420
Kuninori Morimoto013f38f2014-01-23 18:38:26 -0800421static struct rsnd_mod_ops rsnd_scu_non_ops = {
422 .name = "scu (non)",
Kuninori Morimoto7b5ce972014-01-23 18:39:32 -0800423 .init = rsnd_scu_ssi_mode_init,
Kuninori Morimoto013f38f2014-01-23 18:38:26 -0800424};
425
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700426struct rsnd_mod *rsnd_scu_mod_get(struct rsnd_priv *priv, int id)
427{
Takashi Iwai8b147192013-11-05 18:40:05 +0100428 if (WARN_ON(id < 0 || id >= rsnd_scu_nr(priv)))
429 id = 0;
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700430
431 return &((struct rsnd_scu *)(priv->scu) + id)->mod;
432}
433
434int rsnd_scu_probe(struct platform_device *pdev,
435 struct rcar_snd_info *info,
436 struct rsnd_priv *priv)
437{
438 struct device *dev = rsnd_priv_to_dev(priv);
439 struct rsnd_scu *scu;
Kuninori Morimoto013f38f2014-01-23 18:38:26 -0800440 struct rsnd_mod_ops *ops;
Kuninori Morimotoef749402013-12-19 19:28:51 -0800441 struct clk *clk;
442 char name[RSND_SCU_NAME_SIZE];
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700443 int i, nr;
444
445 /*
446 * init SCU
447 */
448 nr = info->scu_info_nr;
449 scu = devm_kzalloc(dev, sizeof(*scu) * nr, GFP_KERNEL);
450 if (!scu) {
451 dev_err(dev, "SCU allocate failed\n");
452 return -ENOMEM;
453 }
454
455 priv->scu_nr = nr;
456 priv->scu = scu;
457
458 for_each_rsnd_scu(scu, priv, i) {
Kuninori Morimotoef749402013-12-19 19:28:51 -0800459 snprintf(name, RSND_SCU_NAME_SIZE, "scu.%d", i);
460
461 clk = devm_clk_get(dev, name);
462 if (IS_ERR(clk))
463 return PTR_ERR(clk);
464
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700465 scu->info = &info->scu_info[i];
Kuninori Morimotoef749402013-12-19 19:28:51 -0800466 scu->clk = clk;
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700467
Kuninori Morimoto013f38f2014-01-23 18:38:26 -0800468 ops = &rsnd_scu_non_ops;
Kuninori Morimoto96c7c0d2014-01-23 18:40:54 -0800469 if (rsnd_scu_hpbif_is_enable(scu))
Kuninori Morimoto013f38f2014-01-23 18:38:26 -0800470 ops = &rsnd_scu_ops;
471
472 rsnd_mod_init(priv, &scu->mod, ops, i);
473
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700474 dev_dbg(dev, "SCU%d probed\n", i);
475 }
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700476 dev_dbg(dev, "scu probed\n");
477
478 return 0;
479}
480
481void rsnd_scu_remove(struct platform_device *pdev,
482 struct rsnd_priv *priv)
483{
484}