blob: ab5f1d21731e290b7d6cb3e50de33c538dd13888 [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 */
53static int rsnd_src_set_route_if_gen1(struct rsnd_priv *priv,
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 Morimotoef749402013-12-19 19:28:51 -080072 struct rsnd_scu *scu = rsnd_mod_to_scu(mod);
Kuninori Morimoto374a52812013-07-28 18:59:12 -070073 u32 mask;
74 u32 val;
75 int shift;
76 int id;
77
78 /*
79 * Gen1 only
80 */
81 if (!rsnd_is_gen1(priv))
82 return 0;
83
84 id = rsnd_mod_id(mod);
Dan Carpenterb5f3d7a2013-11-08 12:46:10 +030085 if (id < 0 || id >= ARRAY_SIZE(routes))
Kuninori Morimoto374a52812013-07-28 18:59:12 -070086 return -EIO;
87
88 /*
89 * SRC_ROUTE_SELECT
90 */
91 val = rsnd_dai_is_play(rdai, io) ? 0x1 : 0x2;
92 val = val << routes[id].shift;
93 mask = routes[id].mask << routes[id].shift;
94
95 rsnd_mod_bset(mod, SRC_ROUTE_SEL, mask, val);
96
97 /*
98 * SRC_TIMING_SELECT
99 */
100 shift = (id % 4) * 8;
101 mask = 0x1F << shift;
Kuninori Morimotoef749402013-12-19 19:28:51 -0800102
103 /*
104 * ADG is used as source clock if SRC was used,
105 * then, SSI WS is used as destination clock.
106 * SSI WS is used as source clock if SRC is not used
107 * (when playback, source/destination become reverse when capture)
108 */
109 if (rsnd_scu_convert_rate(scu)) /* use ADG */
110 val = 0;
111 else if (8 == id) /* use SSI WS, but SRU8 is special */
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700112 val = id << shift;
Kuninori Morimotoef749402013-12-19 19:28:51 -0800113 else /* use SSI WS */
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700114 val = (id + 1) << shift;
115
116 switch (id / 4) {
117 case 0:
118 rsnd_mod_bset(mod, SRC_TMG_SEL0, mask, val);
119 break;
120 case 1:
121 rsnd_mod_bset(mod, SRC_TMG_SEL1, mask, val);
122 break;
123 case 2:
124 rsnd_mod_bset(mod, SRC_TMG_SEL2, mask, val);
125 break;
126 }
127
128 return 0;
129}
130
Kuninori Morimotoef749402013-12-19 19:28:51 -0800131unsigned int rsnd_scu_get_ssi_rate(struct rsnd_priv *priv,
132 struct rsnd_mod *ssi_mod,
133 struct snd_pcm_runtime *runtime)
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700134{
Kuninori Morimotoef749402013-12-19 19:28:51 -0800135 struct rsnd_scu *scu;
136 unsigned int rate;
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700137
Kuninori Morimotoef749402013-12-19 19:28:51 -0800138 /* this function is assuming SSI id = SCU id here */
139 scu = rsnd_mod_to_scu(rsnd_scu_mod_get(priv, rsnd_mod_id(ssi_mod)));
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700140
Kuninori Morimotoef749402013-12-19 19:28:51 -0800141 /*
142 * return convert rate if SRC is used,
143 * otherwise, return runtime->rate as usual
144 */
145 rate = rsnd_scu_convert_rate(scu);
146 if (!rate)
147 rate = runtime->rate;
148
149 return rate;
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700150}
151
Kuninori Morimotoef749402013-12-19 19:28:51 -0800152static int rsnd_scu_convert_rate_ctrl(struct rsnd_priv *priv,
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 snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
Kuninori Morimotoef749402013-12-19 19:28:51 -0800158 struct rsnd_scu *scu = rsnd_mod_to_scu(mod);
159 u32 convert_rate = rsnd_scu_convert_rate(scu);
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700160 u32 adinr = runtime->channels;
161
Kuninori Morimotoef749402013-12-19 19:28:51 -0800162 /* set/clear soft reset */
163 rsnd_mod_write(mod, SRC_SWRSR, 0);
164 rsnd_mod_write(mod, SRC_SWRSR, 1);
165
166 /* Initialize the operation of the SRC internal circuits */
167 rsnd_mod_write(mod, SRC_SRCIR, 1);
168
169 /* Set channel number and output bit length */
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700170 switch (runtime->sample_bits) {
171 case 16:
172 adinr |= OTBL_16;
173 break;
174 case 32:
175 adinr |= OTBL_24;
176 break;
177 default:
178 return -EIO;
179 }
Kuninori Morimoto690ef812013-12-19 19:27:03 -0800180 rsnd_mod_write(mod, SRC_ADINR, adinr);
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700181
Kuninori Morimotoef749402013-12-19 19:28:51 -0800182 if (convert_rate) {
183 u32 fsrate = 0x0400000 / convert_rate * runtime->rate;
184 int ret;
185
186 /* Enable the initial value of IFS */
187 rsnd_mod_write(mod, SRC_IFSCR, 1);
188
189 /* Set initial value of IFS */
190 rsnd_mod_write(mod, SRC_IFSVR, fsrate);
191
192 /* Select SRC mode (fixed value) */
193 rsnd_mod_write(mod, SRC_SRCCR, 0x00010110);
194
195 /* Set the restriction value of the FS ratio (98%) */
196 rsnd_mod_write(mod, SRC_MNFSR, fsrate / 100 * 98);
197
198 if (rsnd_is_gen1(priv)) {
199 /* no SRC_BFSSR settings, since SRC_SRCCR::BUFMD is 0 */
200 }
201
202 /* set convert clock */
203 ret = rsnd_adg_set_convert_clk(priv, mod,
204 runtime->rate,
205 convert_rate);
206 if (ret < 0)
207 return ret;
208 }
209
210 /* Cancel the initialization and operate the SRC function */
211 rsnd_mod_write(mod, SRC_SRCIR, 0);
212
213 /* use DMA transfer */
Kuninori Morimoto0290d2a2014-01-23 18:37:39 -0800214 rsnd_mod_write(mod, SRC_BUSIF_MODE, 1);
Kuninori Morimotoef749402013-12-19 19:28:51 -0800215
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700216 return 0;
217}
218
Kuninori Morimotoaf8a4782013-12-19 19:28:04 -0800219static int rsnd_scu_transfer_start(struct rsnd_priv *priv,
220 struct rsnd_mod *mod,
221 struct rsnd_dai *rdai,
222 struct rsnd_dai_stream *io)
223{
Kuninori Morimotoef749402013-12-19 19:28:51 -0800224 struct rsnd_scu *scu = rsnd_mod_to_scu(mod);
Kuninori Morimotoaf8a4782013-12-19 19:28:04 -0800225 int id = rsnd_mod_id(mod);
226 u32 val;
227
228 if (rsnd_is_gen1(priv)) {
229 val = (1 << id);
230 rsnd_mod_bset(mod, SRC_ROUTE_CTRL, val, val);
231 }
232
Kuninori Morimotoef749402013-12-19 19:28:51 -0800233 if (rsnd_scu_convert_rate(scu))
234 rsnd_mod_write(mod, SRC_ROUTE_MODE0, 1);
235
236 return 0;
237}
238
239static int rsnd_scu_transfer_stop(struct rsnd_priv *priv,
240 struct rsnd_mod *mod,
241 struct rsnd_dai *rdai,
242 struct rsnd_dai_stream *io)
243{
244 struct rsnd_scu *scu = rsnd_mod_to_scu(mod);
245 int id = rsnd_mod_id(mod);
246 u32 mask;
247
248 if (rsnd_is_gen1(priv)) {
249 mask = (1 << id);
250 rsnd_mod_bset(mod, SRC_ROUTE_CTRL, mask, 0);
251 }
252
253 if (rsnd_scu_convert_rate(scu))
254 rsnd_mod_write(mod, SRC_ROUTE_MODE0, 0);
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700255
256 return 0;
257}
258
Kuninori Morimotocdcfcac2013-10-17 22:50:59 -0700259bool rsnd_scu_hpbif_is_enable(struct rsnd_mod *mod)
260{
261 struct rsnd_scu *scu = rsnd_mod_to_scu(mod);
262 u32 flags = rsnd_scu_mode_flags(scu);
263
264 return !!(flags & RSND_SCU_USE_HPBIF);
265}
266
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700267static int rsnd_scu_start(struct rsnd_mod *mod,
268 struct rsnd_dai *rdai,
269 struct rsnd_dai_stream *io)
270{
271 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
Kuninori Morimotoef749402013-12-19 19:28:51 -0800272 struct rsnd_scu *scu = rsnd_mod_to_scu(mod);
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700273 int ret;
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700274
Kuninori Morimotoef749402013-12-19 19:28:51 -0800275 clk_enable(scu->clk);
276
Kuninori Morimoto2582718c2013-12-19 19:27:37 -0800277 ret = rsnd_src_set_route_if_gen1(priv, mod, rdai, io);
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700278 if (ret < 0)
279 return ret;
280
Kuninori Morimotoef749402013-12-19 19:28:51 -0800281 ret = rsnd_scu_convert_rate_ctrl(priv, mod, rdai, io);
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700282 if (ret < 0)
283 return ret;
284
Kuninori Morimotoaf8a4782013-12-19 19:28:04 -0800285 ret = rsnd_scu_transfer_start(priv, mod, rdai, io);
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700286 if (ret < 0)
287 return ret;
288
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700289 return 0;
290}
291
Kuninori Morimotoef749402013-12-19 19:28:51 -0800292static int rsnd_scu_stop(struct rsnd_mod *mod,
293 struct rsnd_dai *rdai,
294 struct rsnd_dai_stream *io)
295{
296 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
297 struct rsnd_scu *scu = rsnd_mod_to_scu(mod);
298
Kuninori Morimotoef749402013-12-19 19:28:51 -0800299 rsnd_scu_transfer_stop(priv, mod, rdai, io);
300
301 clk_disable(scu->clk);
302
303 return 0;
304}
305
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700306static struct rsnd_mod_ops rsnd_scu_ops = {
307 .name = "scu",
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700308 .start = rsnd_scu_start,
Kuninori Morimotoef749402013-12-19 19:28:51 -0800309 .stop = rsnd_scu_stop,
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700310};
311
Kuninori Morimoto013f38f2014-01-23 18:38:26 -0800312static struct rsnd_mod_ops rsnd_scu_non_ops = {
313 .name = "scu (non)",
314};
315
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700316struct rsnd_mod *rsnd_scu_mod_get(struct rsnd_priv *priv, int id)
317{
Takashi Iwai8b147192013-11-05 18:40:05 +0100318 if (WARN_ON(id < 0 || id >= rsnd_scu_nr(priv)))
319 id = 0;
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700320
321 return &((struct rsnd_scu *)(priv->scu) + id)->mod;
322}
323
324int rsnd_scu_probe(struct platform_device *pdev,
325 struct rcar_snd_info *info,
326 struct rsnd_priv *priv)
327{
328 struct device *dev = rsnd_priv_to_dev(priv);
329 struct rsnd_scu *scu;
Kuninori Morimoto013f38f2014-01-23 18:38:26 -0800330 struct rsnd_mod_ops *ops;
Kuninori Morimotoef749402013-12-19 19:28:51 -0800331 struct clk *clk;
332 char name[RSND_SCU_NAME_SIZE];
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700333 int i, nr;
334
335 /*
336 * init SCU
337 */
338 nr = info->scu_info_nr;
339 scu = devm_kzalloc(dev, sizeof(*scu) * nr, GFP_KERNEL);
340 if (!scu) {
341 dev_err(dev, "SCU allocate failed\n");
342 return -ENOMEM;
343 }
344
345 priv->scu_nr = nr;
346 priv->scu = scu;
347
348 for_each_rsnd_scu(scu, priv, i) {
Kuninori Morimotoef749402013-12-19 19:28:51 -0800349 snprintf(name, RSND_SCU_NAME_SIZE, "scu.%d", i);
350
351 clk = devm_clk_get(dev, name);
352 if (IS_ERR(clk))
353 return PTR_ERR(clk);
354
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700355 scu->info = &info->scu_info[i];
Kuninori Morimotoef749402013-12-19 19:28:51 -0800356 scu->clk = clk;
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700357
Kuninori Morimoto013f38f2014-01-23 18:38:26 -0800358 ops = &rsnd_scu_non_ops;
359 if (rsnd_scu_hpbif_is_enable(&scu->mod))
360 ops = &rsnd_scu_ops;
361
362 rsnd_mod_init(priv, &scu->mod, ops, i);
363
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700364 dev_dbg(dev, "SCU%d probed\n", i);
365 }
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700366 dev_dbg(dev, "scu probed\n");
367
368 return 0;
369}
370
371void rsnd_scu_remove(struct platform_device *pdev,
372 struct rsnd_priv *priv)
373{
374}