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