blob: 29837e326bc52fe5ea3995436ab998fc3ae4815d [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;
16};
17
Kuninori Morimoto374a52812013-07-28 18:59:12 -070018#define rsnd_scu_mode_flags(p) ((p)->info->flags)
19
20/*
21 * ADINR
22 */
23#define OTBL_24 (0 << 16)
24#define OTBL_22 (2 << 16)
25#define OTBL_20 (4 << 16)
26#define OTBL_18 (6 << 16)
27#define OTBL_16 (8 << 16)
28
29
Kuninori Morimoto07539c12013-07-21 21:36:35 -070030#define rsnd_mod_to_scu(_mod) \
31 container_of((_mod), struct rsnd_scu, mod)
32
33#define for_each_rsnd_scu(pos, priv, i) \
34 for ((i) = 0; \
35 ((i) < rsnd_scu_nr(priv)) && \
36 ((pos) = (struct rsnd_scu *)(priv)->scu + i); \
37 i++)
38
Kuninori Morimoto374a52812013-07-28 18:59:12 -070039static int rsnd_scu_set_route(struct rsnd_priv *priv,
40 struct rsnd_mod *mod,
41 struct rsnd_dai *rdai,
42 struct rsnd_dai_stream *io)
43{
44 struct scu_route_config {
45 u32 mask;
46 int shift;
47 } routes[] = {
48 { 0xF, 0, }, /* 0 */
49 { 0xF, 4, }, /* 1 */
50 { 0xF, 8, }, /* 2 */
51 { 0x7, 12, }, /* 3 */
52 { 0x7, 16, }, /* 4 */
53 { 0x7, 20, }, /* 5 */
54 { 0x7, 24, }, /* 6 */
55 { 0x3, 28, }, /* 7 */
56 { 0x3, 30, }, /* 8 */
57 };
58
59 u32 mask;
60 u32 val;
61 int shift;
62 int id;
63
64 /*
65 * Gen1 only
66 */
67 if (!rsnd_is_gen1(priv))
68 return 0;
69
70 id = rsnd_mod_id(mod);
71 if (id < 0 || id > ARRAY_SIZE(routes))
72 return -EIO;
73
74 /*
75 * SRC_ROUTE_SELECT
76 */
77 val = rsnd_dai_is_play(rdai, io) ? 0x1 : 0x2;
78 val = val << routes[id].shift;
79 mask = routes[id].mask << routes[id].shift;
80
81 rsnd_mod_bset(mod, SRC_ROUTE_SEL, mask, val);
82
83 /*
84 * SRC_TIMING_SELECT
85 */
86 shift = (id % 4) * 8;
87 mask = 0x1F << shift;
88 if (8 == id) /* SRU8 is very special */
89 val = id << shift;
90 else
91 val = (id + 1) << shift;
92
93 switch (id / 4) {
94 case 0:
95 rsnd_mod_bset(mod, SRC_TMG_SEL0, mask, val);
96 break;
97 case 1:
98 rsnd_mod_bset(mod, SRC_TMG_SEL1, mask, val);
99 break;
100 case 2:
101 rsnd_mod_bset(mod, SRC_TMG_SEL2, mask, val);
102 break;
103 }
104
105 return 0;
106}
107
108static int rsnd_scu_set_mode(struct rsnd_priv *priv,
109 struct rsnd_mod *mod,
110 struct rsnd_dai *rdai,
111 struct rsnd_dai_stream *io)
112{
113 int id = rsnd_mod_id(mod);
114 u32 val;
115
116 if (rsnd_is_gen1(priv)) {
117 val = (1 << id);
118 rsnd_mod_bset(mod, SRC_CTRL, val, val);
119 }
120
121 return 0;
122}
123
124static int rsnd_scu_set_hpbif(struct rsnd_priv *priv,
125 struct rsnd_mod *mod,
126 struct rsnd_dai *rdai,
127 struct rsnd_dai_stream *io)
128{
129 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
130 u32 adinr = runtime->channels;
131
132 switch (runtime->sample_bits) {
133 case 16:
134 adinr |= OTBL_16;
135 break;
136 case 32:
137 adinr |= OTBL_24;
138 break;
139 default:
140 return -EIO;
141 }
142
143 rsnd_mod_write(mod, BUSIF_MODE, 1);
144 rsnd_mod_write(mod, BUSIF_ADINR, adinr);
145
146 return 0;
147}
148
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700149static int rsnd_scu_init(struct rsnd_mod *mod,
150 struct rsnd_dai *rdai,
151 struct rsnd_dai_stream *io)
152{
153 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
154 struct device *dev = rsnd_priv_to_dev(priv);
155
156 dev_dbg(dev, "%s.%d init\n", rsnd_mod_name(mod), rsnd_mod_id(mod));
157
158 return 0;
159}
160
161static int rsnd_scu_quit(struct rsnd_mod *mod,
162 struct rsnd_dai *rdai,
163 struct rsnd_dai_stream *io)
164{
165 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
166 struct device *dev = rsnd_priv_to_dev(priv);
167
168 dev_dbg(dev, "%s.%d quit\n", rsnd_mod_name(mod), rsnd_mod_id(mod));
169
170 return 0;
171}
172
173static int rsnd_scu_start(struct rsnd_mod *mod,
174 struct rsnd_dai *rdai,
175 struct rsnd_dai_stream *io)
176{
177 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700178 struct rsnd_scu *scu = rsnd_mod_to_scu(mod);
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700179 struct device *dev = rsnd_priv_to_dev(priv);
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700180 u32 flags = rsnd_scu_mode_flags(scu);
181 int ret;
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700182
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700183 /*
184 * SCU will be used if it has RSND_SCU_USB_HPBIF flags
185 */
186 if (!(flags & RSND_SCU_USB_HPBIF)) {
187 /* it use PIO transter */
188 dev_dbg(dev, "%s%d is not used\n",
189 rsnd_mod_name(mod), rsnd_mod_id(mod));
190
191 return 0;
192 }
193
194 /* it use DMA transter */
195 ret = rsnd_scu_set_route(priv, mod, rdai, io);
196 if (ret < 0)
197 return ret;
198
199 ret = rsnd_scu_set_mode(priv, mod, rdai, io);
200 if (ret < 0)
201 return ret;
202
203 ret = rsnd_scu_set_hpbif(priv, mod, rdai, io);
204 if (ret < 0)
205 return ret;
206
207 dev_dbg(dev, "%s%d start\n", rsnd_mod_name(mod), rsnd_mod_id(mod));
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700208
209 return 0;
210}
211
212static int rsnd_scu_stop(struct rsnd_mod *mod,
213 struct rsnd_dai *rdai,
214 struct rsnd_dai_stream *io)
215{
216 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
217 struct device *dev = rsnd_priv_to_dev(priv);
218
219 dev_dbg(dev, "%s.%d stop\n", rsnd_mod_name(mod), rsnd_mod_id(mod));
220
221 return 0;
222}
223
224static struct rsnd_mod_ops rsnd_scu_ops = {
225 .name = "scu",
226 .init = rsnd_scu_init,
227 .quit = rsnd_scu_quit,
228 .start = rsnd_scu_start,
229 .stop = rsnd_scu_stop,
230};
231
232struct rsnd_mod *rsnd_scu_mod_get(struct rsnd_priv *priv, int id)
233{
234 BUG_ON(id < 0 || id >= rsnd_scu_nr(priv));
235
236 return &((struct rsnd_scu *)(priv->scu) + id)->mod;
237}
238
239int rsnd_scu_probe(struct platform_device *pdev,
240 struct rcar_snd_info *info,
241 struct rsnd_priv *priv)
242{
243 struct device *dev = rsnd_priv_to_dev(priv);
244 struct rsnd_scu *scu;
245 int i, nr;
246
247 /*
248 * init SCU
249 */
250 nr = info->scu_info_nr;
251 scu = devm_kzalloc(dev, sizeof(*scu) * nr, GFP_KERNEL);
252 if (!scu) {
253 dev_err(dev, "SCU allocate failed\n");
254 return -ENOMEM;
255 }
256
257 priv->scu_nr = nr;
258 priv->scu = scu;
259
260 for_each_rsnd_scu(scu, priv, i) {
261 rsnd_mod_init(priv, &scu->mod,
262 &rsnd_scu_ops, i);
263 scu->info = &info->scu_info[i];
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700264
Kuninori Morimoto374a52812013-07-28 18:59:12 -0700265 dev_dbg(dev, "SCU%d probed\n", i);
266 }
Kuninori Morimoto07539c12013-07-21 21:36:35 -0700267 dev_dbg(dev, "scu probed\n");
268
269 return 0;
270}
271
272void rsnd_scu_remove(struct platform_device *pdev,
273 struct rsnd_priv *priv)
274{
275}