blob: 5d2dbbbf9d98c31f896a83c769035d899bb38387 [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 Morimotocdcfcac2013-10-17 22:50:59 -0700221bool rsnd_scu_hpbif_is_enable(struct rsnd_mod *mod)
222{
223 struct rsnd_scu *scu = rsnd_mod_to_scu(mod);
224 u32 flags = rsnd_scu_mode_flags(scu);
225
226 return !!(flags & RSND_SCU_USE_HPBIF);
227}
228
Kuninori Morimotoa204d902014-01-23 18:38:33 -0800229static int rsnd_scu_init(struct rsnd_mod *mod,
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700230 struct rsnd_dai *rdai,
231 struct rsnd_dai_stream *io)
232{
Kuninori Morimotoef749402013-12-19 19:28:51 -0800233 struct rsnd_scu *scu = rsnd_mod_to_scu(mod);
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700234 int ret;
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700235
Kuninori Morimotoef749402013-12-19 19:28:51 -0800236 clk_enable(scu->clk);
237
Kuninori Morimoto47718dc2014-01-23 18:38:42 -0800238 ret = rsnd_src_set_route_if_gen1(mod, rdai, io);
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700239 if (ret < 0)
240 return ret;
241
Kuninori Morimoto47718dc2014-01-23 18:38:42 -0800242 ret = rsnd_scu_convert_rate_ctrl(mod, rdai, io);
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700243 if (ret < 0)
244 return ret;
245
Kuninori Morimotoa204d902014-01-23 18:38:33 -0800246 return 0;
247}
248
249static int rsnd_scu_quit(struct rsnd_mod *mod,
250 struct rsnd_dai *rdai,
251 struct rsnd_dai_stream *io)
252{
253 struct rsnd_scu *scu = rsnd_mod_to_scu(mod);
254
255 clk_disable(scu->clk);
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700256
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700257 return 0;
258}
259
Kuninori Morimotoa204d902014-01-23 18:38:33 -0800260static int rsnd_scu_start(struct rsnd_mod *mod,
261 struct rsnd_dai *rdai,
262 struct rsnd_dai_stream *io)
263{
264 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
265 struct rsnd_scu *scu = rsnd_mod_to_scu(mod);
Kuninori Morimotoe7ce74e2014-01-23 18:38:50 -0800266 int id = rsnd_mod_id(mod);
Kuninori Morimotoa204d902014-01-23 18:38:33 -0800267
Kuninori Morimotoe7ce74e2014-01-23 18:38:50 -0800268 if (rsnd_is_gen1(priv))
269 rsnd_mod_bset(mod, SRC_ROUTE_CTRL, (1 << id), (1 << id));
270
271 if (rsnd_scu_convert_rate(scu))
272 rsnd_mod_write(mod, SRC_ROUTE_MODE0, 1);
273
274 return 0;
Kuninori Morimotoa204d902014-01-23 18:38:33 -0800275}
276
Kuninori Morimotoef749402013-12-19 19:28:51 -0800277static int rsnd_scu_stop(struct rsnd_mod *mod,
278 struct rsnd_dai *rdai,
279 struct rsnd_dai_stream *io)
280{
281 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
282 struct rsnd_scu *scu = rsnd_mod_to_scu(mod);
Kuninori Morimotoe7ce74e2014-01-23 18:38:50 -0800283 int id = rsnd_mod_id(mod);
Kuninori Morimotoef749402013-12-19 19:28:51 -0800284
Kuninori Morimotoe7ce74e2014-01-23 18:38:50 -0800285 if (rsnd_is_gen1(priv))
286 rsnd_mod_bset(mod, SRC_ROUTE_CTRL, (1 << id), 0);
287
288 if (rsnd_scu_convert_rate(scu))
289 rsnd_mod_write(mod, SRC_ROUTE_MODE0, 0);
Kuninori Morimotoef749402013-12-19 19:28:51 -0800290
Kuninori Morimotoef749402013-12-19 19:28:51 -0800291 return 0;
292}
293
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700294static struct rsnd_mod_ops rsnd_scu_ops = {
295 .name = "scu",
Kuninori Morimotoa204d902014-01-23 18:38:33 -0800296 .init = rsnd_scu_init,
297 .quit = rsnd_scu_quit,
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700298 .start = rsnd_scu_start,
Kuninori Morimotoef749402013-12-19 19:28:51 -0800299 .stop = rsnd_scu_stop,
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700300};
301
Kuninori Morimoto013f38f2014-01-23 18:38:26 -0800302static struct rsnd_mod_ops rsnd_scu_non_ops = {
303 .name = "scu (non)",
304};
305
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700306struct rsnd_mod *rsnd_scu_mod_get(struct rsnd_priv *priv, int id)
307{
Takashi Iwai8b147192013-11-05 18:40:05 +0100308 if (WARN_ON(id < 0 || id >= rsnd_scu_nr(priv)))
309 id = 0;
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700310
311 return &((struct rsnd_scu *)(priv->scu) + id)->mod;
312}
313
314int rsnd_scu_probe(struct platform_device *pdev,
315 struct rcar_snd_info *info,
316 struct rsnd_priv *priv)
317{
318 struct device *dev = rsnd_priv_to_dev(priv);
319 struct rsnd_scu *scu;
Kuninori Morimoto013f38f2014-01-23 18:38:26 -0800320 struct rsnd_mod_ops *ops;
Kuninori Morimotoef749402013-12-19 19:28:51 -0800321 struct clk *clk;
322 char name[RSND_SCU_NAME_SIZE];
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700323 int i, nr;
324
325 /*
326 * init SCU
327 */
328 nr = info->scu_info_nr;
329 scu = devm_kzalloc(dev, sizeof(*scu) * nr, GFP_KERNEL);
330 if (!scu) {
331 dev_err(dev, "SCU allocate failed\n");
332 return -ENOMEM;
333 }
334
335 priv->scu_nr = nr;
336 priv->scu = scu;
337
338 for_each_rsnd_scu(scu, priv, i) {
Kuninori Morimotoef749402013-12-19 19:28:51 -0800339 snprintf(name, RSND_SCU_NAME_SIZE, "scu.%d", i);
340
341 clk = devm_clk_get(dev, name);
342 if (IS_ERR(clk))
343 return PTR_ERR(clk);
344
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700345 scu->info = &info->scu_info[i];
Kuninori Morimotoef749402013-12-19 19:28:51 -0800346 scu->clk = clk;
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700347
Kuninori Morimoto013f38f2014-01-23 18:38:26 -0800348 ops = &rsnd_scu_non_ops;
349 if (rsnd_scu_hpbif_is_enable(&scu->mod))
350 ops = &rsnd_scu_ops;
351
352 rsnd_mod_init(priv, &scu->mod, ops, i);
353
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700354 dev_dbg(dev, "SCU%d probed\n", i);
355 }
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700356 dev_dbg(dev, "scu probed\n");
357
358 return 0;
359}
360
361void rsnd_scu_remove(struct platform_device *pdev,
362 struct rsnd_priv *priv)
363{
364}