ASoC: rsnd: SSI_MODE0/1 settings goes to scu.c
[deliverable/linux.git] / sound / soc / sh / rcar / scu.c
CommitLineData
07539c1d
KM
1/*
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;
ef749400 16 struct clk *clk;
07539c1d
KM
17};
18
374a5281 19#define rsnd_scu_mode_flags(p) ((p)->info->flags)
ef749400
KM
20#define rsnd_scu_convert_rate(p) ((p)->info->convert_rate)
21
22#define RSND_SCU_NAME_SIZE 16
374a5281
KM
23
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
ef749400
KM
33/*
34 * image of SRC (Sampling Rate Converter)
35 *
36 * 96kHz <-> +-----+ 48kHz +-----+ 48kHz +-------+
37 * 48kHz <-> | SRC | <------> | SSI | <-----> | codec |
38 * 44.1kHz <-> +-----+ +-----+ +-------+
39 * ...
40 *
41 */
374a5281 42
07539c1d
KM
43#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
7b5ce975
KM
52static int rsnd_scu_ssi_mode_init(struct rsnd_mod *mod,
53 struct rsnd_dai *rdai,
54 struct rsnd_dai_stream *io)
55{
56 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
57 int id = rsnd_mod_id(mod);
58
59 /*
60 * SSI_MODE0
61 */
62 rsnd_mod_bset(mod, SSI_MODE0, (1 << id),
63 rsnd_scu_hpbif_is_enable(mod) ? 0 : (1 << id));
64
65 /*
66 * SSI_MODE1
67 */
68 if (rsnd_ssi_is_pin_sharing(rsnd_ssi_mod_get(priv, id))) {
69 int shift = -1;
70 switch (id) {
71 case 1:
72 shift = 0;
73 break;
74 case 2:
75 shift = 2;
76 break;
77 case 4:
78 shift = 16;
79 break;
80 }
81
82 if (shift >= 0)
83 rsnd_mod_bset(mod, SSI_MODE1,
84 0x3 << shift,
85 rsnd_dai_is_clk_master(rdai) ?
86 0x2 << shift : 0x1 << shift);
87 }
88
89 return 0;
90}
91
2582718c 92/* Gen1 only */
47718dc7 93static int rsnd_src_set_route_if_gen1(
374a5281
KM
94 struct rsnd_mod *mod,
95 struct rsnd_dai *rdai,
96 struct rsnd_dai_stream *io)
97{
98 struct scu_route_config {
99 u32 mask;
100 int shift;
101 } routes[] = {
102 { 0xF, 0, }, /* 0 */
103 { 0xF, 4, }, /* 1 */
104 { 0xF, 8, }, /* 2 */
105 { 0x7, 12, }, /* 3 */
106 { 0x7, 16, }, /* 4 */
107 { 0x7, 20, }, /* 5 */
108 { 0x7, 24, }, /* 6 */
109 { 0x3, 28, }, /* 7 */
110 { 0x3, 30, }, /* 8 */
111 };
47718dc7 112 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
ef749400 113 struct rsnd_scu *scu = rsnd_mod_to_scu(mod);
374a5281
KM
114 u32 mask;
115 u32 val;
116 int shift;
117 int id;
118
119 /*
120 * Gen1 only
121 */
122 if (!rsnd_is_gen1(priv))
123 return 0;
124
125 id = rsnd_mod_id(mod);
b5f3d7af 126 if (id < 0 || id >= ARRAY_SIZE(routes))
374a5281
KM
127 return -EIO;
128
129 /*
130 * SRC_ROUTE_SELECT
131 */
132 val = rsnd_dai_is_play(rdai, io) ? 0x1 : 0x2;
133 val = val << routes[id].shift;
134 mask = routes[id].mask << routes[id].shift;
135
136 rsnd_mod_bset(mod, SRC_ROUTE_SEL, mask, val);
137
138 /*
139 * SRC_TIMING_SELECT
140 */
141 shift = (id % 4) * 8;
142 mask = 0x1F << shift;
ef749400
KM
143
144 /*
145 * ADG is used as source clock if SRC was used,
146 * then, SSI WS is used as destination clock.
147 * SSI WS is used as source clock if SRC is not used
148 * (when playback, source/destination become reverse when capture)
149 */
150 if (rsnd_scu_convert_rate(scu)) /* use ADG */
151 val = 0;
152 else if (8 == id) /* use SSI WS, but SRU8 is special */
374a5281 153 val = id << shift;
ef749400 154 else /* use SSI WS */
374a5281
KM
155 val = (id + 1) << shift;
156
157 switch (id / 4) {
158 case 0:
159 rsnd_mod_bset(mod, SRC_TMG_SEL0, mask, val);
160 break;
161 case 1:
162 rsnd_mod_bset(mod, SRC_TMG_SEL1, mask, val);
163 break;
164 case 2:
165 rsnd_mod_bset(mod, SRC_TMG_SEL2, mask, val);
166 break;
167 }
168
169 return 0;
170}
171
ef749400
KM
172unsigned int rsnd_scu_get_ssi_rate(struct rsnd_priv *priv,
173 struct rsnd_mod *ssi_mod,
174 struct snd_pcm_runtime *runtime)
374a5281 175{
ef749400
KM
176 struct rsnd_scu *scu;
177 unsigned int rate;
374a5281 178
ef749400
KM
179 /* this function is assuming SSI id = SCU id here */
180 scu = rsnd_mod_to_scu(rsnd_scu_mod_get(priv, rsnd_mod_id(ssi_mod)));
374a5281 181
ef749400
KM
182 /*
183 * return convert rate if SRC is used,
184 * otherwise, return runtime->rate as usual
185 */
186 rate = rsnd_scu_convert_rate(scu);
187 if (!rate)
188 rate = runtime->rate;
189
190 return rate;
374a5281
KM
191}
192
47718dc7 193static int rsnd_scu_convert_rate_ctrl(
374a5281
KM
194 struct rsnd_mod *mod,
195 struct rsnd_dai *rdai,
196 struct rsnd_dai_stream *io)
197{
47718dc7 198 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
374a5281 199 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
ef749400
KM
200 struct rsnd_scu *scu = rsnd_mod_to_scu(mod);
201 u32 convert_rate = rsnd_scu_convert_rate(scu);
374a5281
KM
202 u32 adinr = runtime->channels;
203
ef749400
KM
204 /* set/clear soft reset */
205 rsnd_mod_write(mod, SRC_SWRSR, 0);
206 rsnd_mod_write(mod, SRC_SWRSR, 1);
207
208 /* Initialize the operation of the SRC internal circuits */
209 rsnd_mod_write(mod, SRC_SRCIR, 1);
210
211 /* Set channel number and output bit length */
374a5281
KM
212 switch (runtime->sample_bits) {
213 case 16:
214 adinr |= OTBL_16;
215 break;
216 case 32:
217 adinr |= OTBL_24;
218 break;
219 default:
220 return -EIO;
221 }
690ef81e 222 rsnd_mod_write(mod, SRC_ADINR, adinr);
374a5281 223
ef749400
KM
224 if (convert_rate) {
225 u32 fsrate = 0x0400000 / convert_rate * runtime->rate;
226 int ret;
227
228 /* Enable the initial value of IFS */
229 rsnd_mod_write(mod, SRC_IFSCR, 1);
230
231 /* Set initial value of IFS */
232 rsnd_mod_write(mod, SRC_IFSVR, fsrate);
233
234 /* Select SRC mode (fixed value) */
235 rsnd_mod_write(mod, SRC_SRCCR, 0x00010110);
236
237 /* Set the restriction value of the FS ratio (98%) */
238 rsnd_mod_write(mod, SRC_MNFSR, fsrate / 100 * 98);
239
240 if (rsnd_is_gen1(priv)) {
241 /* no SRC_BFSSR settings, since SRC_SRCCR::BUFMD is 0 */
242 }
374a5281 243
ef749400
KM
244 /* set convert clock */
245 ret = rsnd_adg_set_convert_clk(priv, mod,
246 runtime->rate,
247 convert_rate);
248 if (ret < 0)
249 return ret;
250 }
251
252 /* Cancel the initialization and operate the SRC function */
253 rsnd_mod_write(mod, SRC_SRCIR, 0);
254
255 /* use DMA transfer */
0290d2a4 256 rsnd_mod_write(mod, SRC_BUSIF_MODE, 1);
ef749400 257
374a5281
KM
258 return 0;
259}
260
cdcfcac9
KM
261bool 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
a204d90c 269static int rsnd_scu_init(struct rsnd_mod *mod,
07539c1d
KM
270 struct rsnd_dai *rdai,
271 struct rsnd_dai_stream *io)
272{
ef749400 273 struct rsnd_scu *scu = rsnd_mod_to_scu(mod);
374a5281
KM
274 int ret;
275
ef749400
KM
276 clk_enable(scu->clk);
277
7b5ce975
KM
278 ret = rsnd_scu_ssi_mode_init(mod, rdai, io);
279 if (ret < 0)
280 return ret;
281
47718dc7 282 ret = rsnd_src_set_route_if_gen1(mod, rdai, io);
374a5281
KM
283 if (ret < 0)
284 return ret;
285
47718dc7 286 ret = rsnd_scu_convert_rate_ctrl(mod, rdai, io);
374a5281
KM
287 if (ret < 0)
288 return ret;
07539c1d 289
a204d90c
KM
290 return 0;
291}
292
293static int rsnd_scu_quit(struct rsnd_mod *mod,
294 struct rsnd_dai *rdai,
295 struct rsnd_dai_stream *io)
296{
297 struct rsnd_scu *scu = rsnd_mod_to_scu(mod);
298
299 clk_disable(scu->clk);
374a5281 300
07539c1d
KM
301 return 0;
302}
303
a204d90c
KM
304static int rsnd_scu_start(struct rsnd_mod *mod,
305 struct rsnd_dai *rdai,
306 struct rsnd_dai_stream *io)
307{
308 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
309 struct rsnd_scu *scu = rsnd_mod_to_scu(mod);
e7ce74ea 310 int id = rsnd_mod_id(mod);
a204d90c 311
e7ce74ea
KM
312 if (rsnd_is_gen1(priv))
313 rsnd_mod_bset(mod, SRC_ROUTE_CTRL, (1 << id), (1 << id));
314
315 if (rsnd_scu_convert_rate(scu))
316 rsnd_mod_write(mod, SRC_ROUTE_MODE0, 1);
317
318 return 0;
a204d90c
KM
319}
320
ef749400
KM
321static int rsnd_scu_stop(struct rsnd_mod *mod,
322 struct rsnd_dai *rdai,
323 struct rsnd_dai_stream *io)
324{
325 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
326 struct rsnd_scu *scu = rsnd_mod_to_scu(mod);
e7ce74ea
KM
327 int id = rsnd_mod_id(mod);
328
329 if (rsnd_is_gen1(priv))
330 rsnd_mod_bset(mod, SRC_ROUTE_CTRL, (1 << id), 0);
ef749400 331
e7ce74ea
KM
332 if (rsnd_scu_convert_rate(scu))
333 rsnd_mod_write(mod, SRC_ROUTE_MODE0, 0);
ef749400 334
ef749400
KM
335 return 0;
336}
337
07539c1d
KM
338static struct rsnd_mod_ops rsnd_scu_ops = {
339 .name = "scu",
a204d90c
KM
340 .init = rsnd_scu_init,
341 .quit = rsnd_scu_quit,
07539c1d 342 .start = rsnd_scu_start,
ef749400 343 .stop = rsnd_scu_stop,
07539c1d
KM
344};
345
013f38fe
KM
346static struct rsnd_mod_ops rsnd_scu_non_ops = {
347 .name = "scu (non)",
7b5ce975 348 .init = rsnd_scu_ssi_mode_init,
013f38fe
KM
349};
350
07539c1d
KM
351struct rsnd_mod *rsnd_scu_mod_get(struct rsnd_priv *priv, int id)
352{
8b14719b
TI
353 if (WARN_ON(id < 0 || id >= rsnd_scu_nr(priv)))
354 id = 0;
07539c1d
KM
355
356 return &((struct rsnd_scu *)(priv->scu) + id)->mod;
357}
358
359int rsnd_scu_probe(struct platform_device *pdev,
360 struct rcar_snd_info *info,
361 struct rsnd_priv *priv)
362{
363 struct device *dev = rsnd_priv_to_dev(priv);
364 struct rsnd_scu *scu;
013f38fe 365 struct rsnd_mod_ops *ops;
ef749400
KM
366 struct clk *clk;
367 char name[RSND_SCU_NAME_SIZE];
07539c1d
KM
368 int i, nr;
369
370 /*
371 * init SCU
372 */
373 nr = info->scu_info_nr;
374 scu = devm_kzalloc(dev, sizeof(*scu) * nr, GFP_KERNEL);
375 if (!scu) {
376 dev_err(dev, "SCU allocate failed\n");
377 return -ENOMEM;
378 }
379
380 priv->scu_nr = nr;
381 priv->scu = scu;
382
383 for_each_rsnd_scu(scu, priv, i) {
ef749400
KM
384 snprintf(name, RSND_SCU_NAME_SIZE, "scu.%d", i);
385
386 clk = devm_clk_get(dev, name);
387 if (IS_ERR(clk))
388 return PTR_ERR(clk);
389
07539c1d 390 scu->info = &info->scu_info[i];
ef749400 391 scu->clk = clk;
07539c1d 392
013f38fe
KM
393 ops = &rsnd_scu_non_ops;
394 if (rsnd_scu_hpbif_is_enable(&scu->mod))
395 ops = &rsnd_scu_ops;
396
397 rsnd_mod_init(priv, &scu->mod, ops, i);
398
374a5281
KM
399 dev_dbg(dev, "SCU%d probed\n", i);
400 }
07539c1d
KM
401 dev_dbg(dev, "scu probed\n");
402
403 return 0;
404}
405
406void rsnd_scu_remove(struct platform_device *pdev,
407 struct rsnd_priv *priv)
408{
409}
This page took 0.260626 seconds and 5 git commands to generate.