blob: ece539b758d17751ba8c67a76f49bc40be59a259 [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 Morimoto07539c12013-07-21 21:36:35 -070043#define rsnd_mod_to_scu(_mod) \
44 container_of((_mod), struct rsnd_scu, mod)
45
46#define for_each_rsnd_scu(pos, priv, i) \
47 for ((i) = 0; \
48 ((i) < rsnd_scu_nr(priv)) && \
49 ((pos) = (struct rsnd_scu *)(priv)->scu + i); \
50 i++)
51
Kuninori Morimoto2582718c2013-12-19 19:27:37 -080052/* Gen1 only */
Kuninori Morimoto47718dc2014-01-23 18:38:42 -080053static int rsnd_src_set_route_if_gen1(
Kuninori Morimoto374a52812013-07-28 18:59:12 -070054 struct rsnd_mod *mod,
55 struct rsnd_dai *rdai,
56 struct rsnd_dai_stream *io)
57{
58 struct scu_route_config {
59 u32 mask;
60 int shift;
61 } routes[] = {
62 { 0xF, 0, }, /* 0 */
63 { 0xF, 4, }, /* 1 */
64 { 0xF, 8, }, /* 2 */
65 { 0x7, 12, }, /* 3 */
66 { 0x7, 16, }, /* 4 */
67 { 0x7, 20, }, /* 5 */
68 { 0x7, 24, }, /* 6 */
69 { 0x3, 28, }, /* 7 */
70 { 0x3, 30, }, /* 8 */
71 };
Kuninori Morimoto47718dc2014-01-23 18:38:42 -080072 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
Kuninori Morimotoef749402013-12-19 19:28:51 -080073 struct rsnd_scu *scu = rsnd_mod_to_scu(mod);
Kuninori Morimoto374a52812013-07-28 18:59:12 -070074 u32 mask;
75 u32 val;
76 int shift;
77 int id;
78
79 /*
80 * Gen1 only
81 */
82 if (!rsnd_is_gen1(priv))
83 return 0;
84
85 id = rsnd_mod_id(mod);
Dan Carpenterb5f3d7a2013-11-08 12:46:10 +030086 if (id < 0 || id >= ARRAY_SIZE(routes))
Kuninori Morimoto374a52812013-07-28 18:59:12 -070087 return -EIO;
88
89 /*
90 * SRC_ROUTE_SELECT
91 */
92 val = rsnd_dai_is_play(rdai, io) ? 0x1 : 0x2;
93 val = val << routes[id].shift;
94 mask = routes[id].mask << routes[id].shift;
95
96 rsnd_mod_bset(mod, SRC_ROUTE_SEL, mask, val);
97
98 /*
99 * SRC_TIMING_SELECT
100 */
101 shift = (id % 4) * 8;
102 mask = 0x1F << shift;
Kuninori Morimotoef749402013-12-19 19:28:51 -0800103
104 /*
105 * ADG is used as source clock if SRC was used,
106 * then, SSI WS is used as destination clock.
107 * SSI WS is used as source clock if SRC is not used
108 * (when playback, source/destination become reverse when capture)
109 */
110 if (rsnd_scu_convert_rate(scu)) /* use ADG */
111 val = 0;
112 else if (8 == id) /* use SSI WS, but SRU8 is special */
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700113 val = id << shift;
Kuninori Morimotoef749402013-12-19 19:28:51 -0800114 else /* use SSI WS */
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700115 val = (id + 1) << shift;
116
117 switch (id / 4) {
118 case 0:
119 rsnd_mod_bset(mod, SRC_TMG_SEL0, mask, val);
120 break;
121 case 1:
122 rsnd_mod_bset(mod, SRC_TMG_SEL1, mask, val);
123 break;
124 case 2:
125 rsnd_mod_bset(mod, SRC_TMG_SEL2, mask, val);
126 break;
127 }
128
129 return 0;
130}
131
Kuninori Morimotoef749402013-12-19 19:28:51 -0800132unsigned int rsnd_scu_get_ssi_rate(struct rsnd_priv *priv,
133 struct rsnd_mod *ssi_mod,
134 struct snd_pcm_runtime *runtime)
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700135{
Kuninori Morimotoef749402013-12-19 19:28:51 -0800136 struct rsnd_scu *scu;
137 unsigned int rate;
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700138
Kuninori Morimotoef749402013-12-19 19:28:51 -0800139 /* this function is assuming SSI id = SCU id here */
140 scu = rsnd_mod_to_scu(rsnd_scu_mod_get(priv, rsnd_mod_id(ssi_mod)));
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700141
Kuninori Morimotoef749402013-12-19 19:28:51 -0800142 /*
143 * return convert rate if SRC is used,
144 * otherwise, return runtime->rate as usual
145 */
146 rate = rsnd_scu_convert_rate(scu);
147 if (!rate)
148 rate = runtime->rate;
149
150 return rate;
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700151}
152
Kuninori Morimoto47718dc2014-01-23 18:38:42 -0800153static int rsnd_scu_convert_rate_ctrl(
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700154 struct rsnd_mod *mod,
155 struct rsnd_dai *rdai,
156 struct rsnd_dai_stream *io)
157{
Kuninori Morimoto47718dc2014-01-23 18:38:42 -0800158 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700159 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
Kuninori Morimotoef749402013-12-19 19:28:51 -0800160 struct rsnd_scu *scu = rsnd_mod_to_scu(mod);
161 u32 convert_rate = rsnd_scu_convert_rate(scu);
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700162 u32 adinr = runtime->channels;
163
Kuninori Morimotoef749402013-12-19 19:28:51 -0800164 /* set/clear soft reset */
165 rsnd_mod_write(mod, SRC_SWRSR, 0);
166 rsnd_mod_write(mod, SRC_SWRSR, 1);
167
168 /* Initialize the operation of the SRC internal circuits */
169 rsnd_mod_write(mod, SRC_SRCIR, 1);
170
171 /* Set channel number and output bit length */
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700172 switch (runtime->sample_bits) {
173 case 16:
174 adinr |= OTBL_16;
175 break;
176 case 32:
177 adinr |= OTBL_24;
178 break;
179 default:
180 return -EIO;
181 }
Kuninori Morimoto690ef812013-12-19 19:27:03 -0800182 rsnd_mod_write(mod, SRC_ADINR, adinr);
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700183
Kuninori Morimotoef749402013-12-19 19:28:51 -0800184 if (convert_rate) {
185 u32 fsrate = 0x0400000 / convert_rate * runtime->rate;
186 int ret;
187
188 /* Enable the initial value of IFS */
189 rsnd_mod_write(mod, SRC_IFSCR, 1);
190
191 /* Set initial value of IFS */
192 rsnd_mod_write(mod, SRC_IFSVR, fsrate);
193
194 /* Select SRC mode (fixed value) */
195 rsnd_mod_write(mod, SRC_SRCCR, 0x00010110);
196
197 /* Set the restriction value of the FS ratio (98%) */
198 rsnd_mod_write(mod, SRC_MNFSR, fsrate / 100 * 98);
199
200 if (rsnd_is_gen1(priv)) {
201 /* no SRC_BFSSR settings, since SRC_SRCCR::BUFMD is 0 */
202 }
203
204 /* set convert clock */
205 ret = rsnd_adg_set_convert_clk(priv, mod,
206 runtime->rate,
207 convert_rate);
208 if (ret < 0)
209 return ret;
210 }
211
212 /* Cancel the initialization and operate the SRC function */
213 rsnd_mod_write(mod, SRC_SRCIR, 0);
214
215 /* use DMA transfer */
Kuninori Morimoto0290d2a2014-01-23 18:37:39 -0800216 rsnd_mod_write(mod, SRC_BUSIF_MODE, 1);
Kuninori Morimotoef749402013-12-19 19:28:51 -0800217
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700218 return 0;
219}
220
Kuninori Morimotoaf8a4782013-12-19 19:28:04 -0800221static int rsnd_scu_transfer_start(struct rsnd_priv *priv,
222 struct rsnd_mod *mod,
223 struct rsnd_dai *rdai,
224 struct rsnd_dai_stream *io)
225{
Kuninori Morimotoef749402013-12-19 19:28:51 -0800226 struct rsnd_scu *scu = rsnd_mod_to_scu(mod);
Kuninori Morimotoaf8a4782013-12-19 19:28:04 -0800227 int id = rsnd_mod_id(mod);
228 u32 val;
229
230 if (rsnd_is_gen1(priv)) {
231 val = (1 << id);
232 rsnd_mod_bset(mod, SRC_ROUTE_CTRL, val, val);
233 }
234
Kuninori Morimotoef749402013-12-19 19:28:51 -0800235 if (rsnd_scu_convert_rate(scu))
236 rsnd_mod_write(mod, SRC_ROUTE_MODE0, 1);
237
238 return 0;
239}
240
241static int rsnd_scu_transfer_stop(struct rsnd_priv *priv,
242 struct rsnd_mod *mod,
243 struct rsnd_dai *rdai,
244 struct rsnd_dai_stream *io)
245{
246 struct rsnd_scu *scu = rsnd_mod_to_scu(mod);
247 int id = rsnd_mod_id(mod);
248 u32 mask;
249
250 if (rsnd_is_gen1(priv)) {
251 mask = (1 << id);
252 rsnd_mod_bset(mod, SRC_ROUTE_CTRL, mask, 0);
253 }
254
255 if (rsnd_scu_convert_rate(scu))
256 rsnd_mod_write(mod, SRC_ROUTE_MODE0, 0);
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700257
258 return 0;
259}
260
Kuninori Morimotocdcfcac2013-10-17 22:50:59 -0700261bool rsnd_scu_hpbif_is_enable(struct rsnd_mod *mod)
262{
263 struct rsnd_scu *scu = rsnd_mod_to_scu(mod);
264 u32 flags = rsnd_scu_mode_flags(scu);
265
266 return !!(flags & RSND_SCU_USE_HPBIF);
267}
268
Kuninori Morimotoa204d902014-01-23 18:38:33 -0800269static int rsnd_scu_init(struct rsnd_mod *mod,
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700270 struct rsnd_dai *rdai,
271 struct rsnd_dai_stream *io)
272{
Kuninori Morimotoef749402013-12-19 19:28:51 -0800273 struct rsnd_scu *scu = rsnd_mod_to_scu(mod);
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700274 int ret;
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700275
Kuninori Morimotoef749402013-12-19 19:28:51 -0800276 clk_enable(scu->clk);
277
Kuninori Morimoto47718dc2014-01-23 18:38:42 -0800278 ret = rsnd_src_set_route_if_gen1(mod, rdai, io);
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700279 if (ret < 0)
280 return ret;
281
Kuninori Morimoto47718dc2014-01-23 18:38:42 -0800282 ret = rsnd_scu_convert_rate_ctrl(mod, rdai, io);
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700283 if (ret < 0)
284 return ret;
285
Kuninori Morimotoa204d902014-01-23 18:38:33 -0800286 return 0;
287}
288
289static int rsnd_scu_quit(struct rsnd_mod *mod,
290 struct rsnd_dai *rdai,
291 struct rsnd_dai_stream *io)
292{
293 struct rsnd_scu *scu = rsnd_mod_to_scu(mod);
294
295 clk_disable(scu->clk);
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700296
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700297 return 0;
298}
299
Kuninori Morimotoa204d902014-01-23 18:38:33 -0800300static int rsnd_scu_start(struct rsnd_mod *mod,
301 struct rsnd_dai *rdai,
302 struct rsnd_dai_stream *io)
303{
304 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
305 struct rsnd_scu *scu = rsnd_mod_to_scu(mod);
306
307 return rsnd_scu_transfer_start(priv, mod, rdai, io);
308}
309
Kuninori Morimotoef749402013-12-19 19:28:51 -0800310static int rsnd_scu_stop(struct rsnd_mod *mod,
311 struct rsnd_dai *rdai,
312 struct rsnd_dai_stream *io)
313{
314 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
315 struct rsnd_scu *scu = rsnd_mod_to_scu(mod);
316
Kuninori Morimotoef749402013-12-19 19:28:51 -0800317 rsnd_scu_transfer_stop(priv, mod, rdai, io);
318
Kuninori Morimotoef749402013-12-19 19:28:51 -0800319 return 0;
320}
321
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700322static struct rsnd_mod_ops rsnd_scu_ops = {
323 .name = "scu",
Kuninori Morimotoa204d902014-01-23 18:38:33 -0800324 .init = rsnd_scu_init,
325 .quit = rsnd_scu_quit,
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700326 .start = rsnd_scu_start,
Kuninori Morimotoef749402013-12-19 19:28:51 -0800327 .stop = rsnd_scu_stop,
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700328};
329
Kuninori Morimoto013f38f2014-01-23 18:38:26 -0800330static struct rsnd_mod_ops rsnd_scu_non_ops = {
331 .name = "scu (non)",
332};
333
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700334struct rsnd_mod *rsnd_scu_mod_get(struct rsnd_priv *priv, int id)
335{
Takashi Iwai8b147192013-11-05 18:40:05 +0100336 if (WARN_ON(id < 0 || id >= rsnd_scu_nr(priv)))
337 id = 0;
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700338
339 return &((struct rsnd_scu *)(priv->scu) + id)->mod;
340}
341
342int rsnd_scu_probe(struct platform_device *pdev,
343 struct rcar_snd_info *info,
344 struct rsnd_priv *priv)
345{
346 struct device *dev = rsnd_priv_to_dev(priv);
347 struct rsnd_scu *scu;
Kuninori Morimoto013f38f2014-01-23 18:38:26 -0800348 struct rsnd_mod_ops *ops;
Kuninori Morimotoef749402013-12-19 19:28:51 -0800349 struct clk *clk;
350 char name[RSND_SCU_NAME_SIZE];
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700351 int i, nr;
352
353 /*
354 * init SCU
355 */
356 nr = info->scu_info_nr;
357 scu = devm_kzalloc(dev, sizeof(*scu) * nr, GFP_KERNEL);
358 if (!scu) {
359 dev_err(dev, "SCU allocate failed\n");
360 return -ENOMEM;
361 }
362
363 priv->scu_nr = nr;
364 priv->scu = scu;
365
366 for_each_rsnd_scu(scu, priv, i) {
Kuninori Morimotoef749402013-12-19 19:28:51 -0800367 snprintf(name, RSND_SCU_NAME_SIZE, "scu.%d", i);
368
369 clk = devm_clk_get(dev, name);
370 if (IS_ERR(clk))
371 return PTR_ERR(clk);
372
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700373 scu->info = &info->scu_info[i];
Kuninori Morimotoef749402013-12-19 19:28:51 -0800374 scu->clk = clk;
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700375
Kuninori Morimoto013f38f2014-01-23 18:38:26 -0800376 ops = &rsnd_scu_non_ops;
377 if (rsnd_scu_hpbif_is_enable(&scu->mod))
378 ops = &rsnd_scu_ops;
379
380 rsnd_mod_init(priv, &scu->mod, ops, i);
381
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700382 dev_dbg(dev, "SCU%d probed\n", i);
383 }
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700384 dev_dbg(dev, "scu probed\n");
385
386 return 0;
387}
388
389void rsnd_scu_remove(struct platform_device *pdev,
390 struct rsnd_priv *priv)
391{
392}