a9605a0163f2853610d82d014c452ad78fdec06a
[muen/linux.git] / sound / soc / sh / rcar / ssiu.c
1 // SPDX-License-Identifier: GPL-2.0
2 //
3 // Renesas R-Car SSIU support
4 //
5 // Copyright (c) 2015 Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
6
7 #include "rsnd.h"
8
9 #define SSIU_NAME "ssiu"
10
11 struct rsnd_ssiu {
12         struct rsnd_mod mod;
13 };
14
15 #define rsnd_ssiu_nr(priv) ((priv)->ssiu_nr)
16 #define for_each_rsnd_ssiu(pos, priv, i)                                \
17         for (i = 0;                                                     \
18              (i < rsnd_ssiu_nr(priv)) &&                                \
19                      ((pos) = ((struct rsnd_ssiu *)(priv)->ssiu + i));  \
20              i++)
21
22 static int rsnd_ssiu_init(struct rsnd_mod *mod,
23                           struct rsnd_dai_stream *io,
24                           struct rsnd_priv *priv)
25 {
26         struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
27         u32 multi_ssi_slaves = rsnd_ssi_multi_slaves_runtime(io);
28         int use_busif = rsnd_ssi_use_busif(io);
29         int id = rsnd_mod_id(mod);
30         u32 mask1, val1;
31         u32 mask2, val2;
32
33         /* clear status */
34         switch (id) {
35         case 0:
36         case 1:
37         case 2:
38         case 3:
39         case 4:
40                 rsnd_mod_write(mod, SSI_SYS_STATUS0, 0xf << (id * 4));
41                 rsnd_mod_write(mod, SSI_SYS_STATUS2, 0xf << (id * 4));
42                 rsnd_mod_write(mod, SSI_SYS_STATUS4, 0xf << (id * 4));
43                 rsnd_mod_write(mod, SSI_SYS_STATUS6, 0xf << (id * 4));
44                 break;
45         case 9:
46                 rsnd_mod_write(mod, SSI_SYS_STATUS1, 0xf << 4);
47                 rsnd_mod_write(mod, SSI_SYS_STATUS3, 0xf << 4);
48                 rsnd_mod_write(mod, SSI_SYS_STATUS5, 0xf << 4);
49                 rsnd_mod_write(mod, SSI_SYS_STATUS7, 0xf << 4);
50                 break;
51         }
52
53         /*
54          * SSI_MODE0
55          */
56         rsnd_mod_bset(mod, SSI_MODE0, (1 << id), !use_busif << id);
57
58         /*
59          * SSI_MODE1
60          */
61         mask1 = (1 << 4) | (1 << 20);   /* mask sync bit */
62         mask2 = (1 << 4);               /* mask sync bit */
63         val1  = val2  = 0;
64         if (id == 8) {
65                 /*
66                  * SSI8 pin is sharing with SSI7, nothing to do.
67                  */
68         } else if (rsnd_ssi_is_pin_sharing(io)) {
69                 int shift = -1;
70
71                 switch (id) {
72                 case 1:
73                         shift = 0;
74                         break;
75                 case 2:
76                         shift = 2;
77                         break;
78                 case 4:
79                         shift = 16;
80                         break;
81                 default:
82                         return -EINVAL;
83                 }
84
85                 mask1 |= 0x3 << shift;
86                 val1 = rsnd_rdai_is_clk_master(rdai) ?
87                         0x2 << shift : 0x1 << shift;
88
89         } else if (multi_ssi_slaves) {
90
91                 mask2 |= 0x00000007;
92                 mask1 |= 0x0000000f;
93
94                 switch (multi_ssi_slaves) {
95                 case 0x0206: /* SSI0/1/2/9 */
96                         val2 = (1 << 4) | /* SSI0129 sync */
97                                 (rsnd_rdai_is_clk_master(rdai) ? 0x2 : 0x1);
98                         /* fall through */
99                 case 0x0006: /* SSI0/1/2 */
100                         val1 = rsnd_rdai_is_clk_master(rdai) ?
101                                 0xa : 0x5;
102
103                         if (!val2)  /* SSI012 sync */
104                                 val1 |= (1 << 4);
105                 }
106         }
107
108         rsnd_mod_bset(mod, SSI_MODE1, mask1, val1);
109         rsnd_mod_bset(mod, SSI_MODE2, mask2, val2);
110
111         return 0;
112 }
113
114 static struct rsnd_mod_ops rsnd_ssiu_ops_gen1 = {
115         .name   = SSIU_NAME,
116         .init   = rsnd_ssiu_init,
117 };
118
119 static int rsnd_ssiu_init_gen2(struct rsnd_mod *mod,
120                                struct rsnd_dai_stream *io,
121                                struct rsnd_priv *priv)
122 {
123         int hdmi = rsnd_ssi_hdmi_port(io);
124         int ret;
125         u32 mode = 0;
126
127         ret = rsnd_ssiu_init(mod, io, priv);
128         if (ret < 0)
129                 return ret;
130
131         if (rsnd_runtime_is_ssi_tdm(io)) {
132                 /*
133                  * TDM Extend Mode
134                  * see
135                  *      rsnd_ssi_config_init()
136                  */
137                 mode = 0x1;
138         }
139
140         rsnd_mod_write(mod, SSI_MODE, mode);
141
142         if (rsnd_ssi_use_busif(io)) {
143                 int id = rsnd_mod_id(mod);
144                 int busif = rsnd_ssi_get_busif(io);
145
146                 /*
147                  * FIXME
148                  *
149                  * We can't support SSI9-4/5/6/7, because its address is
150                  * out of calculation rule
151                  */
152                 if ((id == 9) && (busif >= 4)) {
153                         struct device *dev = rsnd_priv_to_dev(priv);
154
155                         dev_err(dev, "This driver doesn't support SSI%d-%d, so far",
156                                 id, busif);
157                 }
158
159 #define RSND_WRITE_BUSIF(i)                                             \
160                 rsnd_mod_write(mod, SSI_BUSIF##i##_ADINR,               \
161                                rsnd_get_adinr_bit(mod, io) |            \
162                                (rsnd_io_is_play(io) ?                   \
163                                 rsnd_runtime_channel_after_ctu(io) :    \
164                                 rsnd_runtime_channel_original(io)));    \
165                 rsnd_mod_write(mod, SSI_BUSIF##i##_MODE,                \
166                                rsnd_get_busif_shift(io, mod) | 1);      \
167                 rsnd_mod_write(mod, SSI_BUSIF##i##_DALIGN,              \
168                                rsnd_get_dalign(mod, io))
169
170                 switch (busif) {
171                 case 0:
172                         RSND_WRITE_BUSIF(0);
173                         break;
174                 case 1:
175                         RSND_WRITE_BUSIF(1);
176                         break;
177                 case 2:
178                         RSND_WRITE_BUSIF(2);
179                         break;
180                 case 3:
181                         RSND_WRITE_BUSIF(3);
182                         break;
183                 case 4:
184                         RSND_WRITE_BUSIF(4);
185                         break;
186                 case 5:
187                         RSND_WRITE_BUSIF(5);
188                         break;
189                 case 6:
190                         RSND_WRITE_BUSIF(6);
191                         break;
192                 case 7:
193                         RSND_WRITE_BUSIF(7);
194                         break;
195                 }
196         }
197
198         if (hdmi) {
199                 enum rsnd_mod_type rsnd_ssi_array[] = {
200                         RSND_MOD_SSIM1,
201                         RSND_MOD_SSIM2,
202                         RSND_MOD_SSIM3,
203                 };
204                 struct rsnd_mod *ssi_mod = rsnd_io_to_mod_ssi(io);
205                 struct rsnd_mod *pos;
206                 u32 val;
207                 int i, shift;
208
209                 i = rsnd_mod_id(ssi_mod);
210
211                 /* output all same SSI as default */
212                 val =   i << 16 |
213                         i << 20 |
214                         i << 24 |
215                         i << 28 |
216                         i;
217
218                 for_each_rsnd_mod_array(i, pos, io, rsnd_ssi_array) {
219                         shift   = (i * 4) + 16;
220                         val     = (val & ~(0xF << shift)) |
221                                 rsnd_mod_id(pos) << shift;
222                 }
223
224                 switch (hdmi) {
225                 case RSND_SSI_HDMI_PORT0:
226                         rsnd_mod_write(mod, HDMI0_SEL, val);
227                         break;
228                 case RSND_SSI_HDMI_PORT1:
229                         rsnd_mod_write(mod, HDMI1_SEL, val);
230                         break;
231                 }
232         }
233
234         return 0;
235 }
236
237 static int rsnd_ssiu_start_gen2(struct rsnd_mod *mod,
238                                 struct rsnd_dai_stream *io,
239                                 struct rsnd_priv *priv)
240 {
241         int busif = rsnd_ssi_get_busif(io);
242
243         if (!rsnd_ssi_use_busif(io))
244                 return 0;
245
246         rsnd_mod_bset(mod, SSI_CTRL, 1 << (busif * 4), 1 << (busif * 4));
247
248         if (rsnd_ssi_multi_slaves_runtime(io))
249                 rsnd_mod_write(mod, SSI_CONTROL, 0x1);
250
251         return 0;
252 }
253
254 static int rsnd_ssiu_stop_gen2(struct rsnd_mod *mod,
255                                struct rsnd_dai_stream *io,
256                                struct rsnd_priv *priv)
257 {
258         int busif = rsnd_ssi_get_busif(io);
259
260         if (!rsnd_ssi_use_busif(io))
261                 return 0;
262
263         rsnd_mod_bset(mod, SSI_CTRL, 1 << (busif * 4), 0);
264
265         if (rsnd_ssi_multi_slaves_runtime(io))
266                 rsnd_mod_write(mod, SSI_CONTROL, 0);
267
268         return 0;
269 }
270
271 static struct rsnd_mod_ops rsnd_ssiu_ops_gen2 = {
272         .name   = SSIU_NAME,
273         .init   = rsnd_ssiu_init_gen2,
274         .start  = rsnd_ssiu_start_gen2,
275         .stop   = rsnd_ssiu_stop_gen2,
276 };
277
278 static struct rsnd_mod *rsnd_ssiu_mod_get(struct rsnd_priv *priv, int id)
279 {
280         if (WARN_ON(id < 0 || id >= rsnd_ssiu_nr(priv)))
281                 id = 0;
282
283         return rsnd_mod_get((struct rsnd_ssiu *)(priv->ssiu) + id);
284 }
285
286 int rsnd_ssiu_attach(struct rsnd_dai_stream *io,
287                      struct rsnd_mod *ssi_mod)
288 {
289         struct rsnd_priv *priv = rsnd_io_to_priv(io);
290         struct rsnd_mod *mod = rsnd_ssiu_mod_get(priv, rsnd_mod_id(ssi_mod));
291
292         rsnd_mod_confirm_ssi(ssi_mod);
293
294         return rsnd_dai_connect(mod, io, mod->type);
295 }
296
297 int rsnd_ssiu_probe(struct rsnd_priv *priv)
298 {
299         struct device *dev = rsnd_priv_to_dev(priv);
300         struct rsnd_ssiu *ssiu;
301         struct rsnd_mod_ops *ops;
302         int i, nr, ret;
303
304         /* same number to SSI */
305         nr      = priv->ssi_nr;
306         ssiu    = devm_kcalloc(dev, nr, sizeof(*ssiu), GFP_KERNEL);
307         if (!ssiu)
308                 return -ENOMEM;
309
310         priv->ssiu      = ssiu;
311         priv->ssiu_nr   = nr;
312
313         if (rsnd_is_gen1(priv))
314                 ops = &rsnd_ssiu_ops_gen1;
315         else
316                 ops = &rsnd_ssiu_ops_gen2;
317
318         for_each_rsnd_ssiu(ssiu, priv, i) {
319                 ret = rsnd_mod_init(priv, rsnd_mod_get(ssiu),
320                                     ops, NULL, rsnd_mod_get_status,
321                                     RSND_MOD_SSIU, i);
322                 if (ret)
323                         return ret;
324         }
325
326         return 0;
327 }
328
329 void rsnd_ssiu_remove(struct rsnd_priv *priv)
330 {
331         struct rsnd_ssiu *ssiu;
332         int i;
333
334         for_each_rsnd_ssiu(ssiu, priv, i) {
335                 rsnd_mod_quit(rsnd_mod_get(ssiu));
336         }
337 }