blob: 4e817c8a18c0bbe899374028ae56ef0754042d59 [file] [log] [blame]
Kuninori Morimotoc7f69ab2015-10-26 08:43:41 +00001/*
2 * Renesas R-Car SSIU support
3 *
4 * Copyright (c) 2015 Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 */
10#include "rsnd.h"
11
12#define SSIU_NAME "ssiu"
13
14struct rsnd_ssiu {
15 struct rsnd_mod mod;
16};
17
18#define rsnd_ssiu_nr(priv) ((priv)->ssiu_nr)
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
25static 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);
Kuninori Morimoto4f5c6342016-02-18 08:18:54 +000030 u32 multi_ssi_slaves = rsnd_ssi_multi_slaves_runtime(io);
Kuninori Morimotoc7f69ab2015-10-26 08:43:41 +000031 int use_busif = rsnd_ssi_use_busif(io);
32 int id = rsnd_mod_id(mod);
Kuninori Morimotob4c83b12015-12-17 03:00:10 +000033 u32 mask1, val1;
34 u32 mask2, val2;
Kuninori Morimotoc7f69ab2015-10-26 08:43:41 +000035
Kuninori Morimoto814efe32016-10-26 04:29:21 +000036 /* 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
Kuninori Morimotoc7f69ab2015-10-26 08:43:41 +000056 /*
57 * SSI_MODE0
58 */
59 rsnd_mod_bset(mod, SSI_MODE0, (1 << id), !use_busif << id);
60
61 /*
62 * SSI_MODE1
63 */
Kuninori Morimotob4c83b12015-12-17 03:00:10 +000064 mask1 = (1 << 4) | (1 << 20); /* mask sync bit */
65 mask2 = (1 << 4); /* mask sync bit */
66 val1 = val2 = 0;
Kuninori Morimotoc7f69ab2015-10-26 08:43:41 +000067 if (rsnd_ssi_is_pin_sharing(io)) {
68 int shift = -1;
69
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;
Kuninori Morimotob4c83b12015-12-17 03:00:10 +000080 default:
81 return -EINVAL;
Kuninori Morimotoc7f69ab2015-10-26 08:43:41 +000082 }
83
Kuninori Morimotob4c83b12015-12-17 03:00:10 +000084 mask1 |= 0x3 << shift;
85 val1 = rsnd_rdai_is_clk_master(rdai) ?
86 0x2 << shift : 0x1 << shift;
87
88 } else if (multi_ssi_slaves) {
89
90 mask2 |= 0x00000007;
91 mask1 |= 0x0000000f;
92
93 switch (multi_ssi_slaves) {
94 case 0x0206: /* SSI0/1/2/9 */
95 val2 = (1 << 4) | /* SSI0129 sync */
Dan Carpenter8012c982016-01-06 12:38:41 +030096 (rsnd_rdai_is_clk_master(rdai) ? 0x2 : 0x1);
Kuninori Morimotob4c83b12015-12-17 03:00:10 +000097 /* fall through */
98 case 0x0006: /* SSI0/1/2 */
99 val1 = rsnd_rdai_is_clk_master(rdai) ?
100 0xa : 0x5;
101
102 if (!val2) /* SSI012 sync */
103 val1 |= (1 << 4);
104 }
Kuninori Morimotoc7f69ab2015-10-26 08:43:41 +0000105 }
106
Kuninori Morimotob4c83b12015-12-17 03:00:10 +0000107 rsnd_mod_bset(mod, SSI_MODE1, mask1, val1);
108 rsnd_mod_bset(mod, SSI_MODE2, mask2, val2);
109
Kuninori Morimotoc7f69ab2015-10-26 08:43:41 +0000110 return 0;
111}
112
113static struct rsnd_mod_ops rsnd_ssiu_ops_gen1 = {
114 .name = SSIU_NAME,
115 .init = rsnd_ssiu_init,
116};
117
118static int rsnd_ssiu_init_gen2(struct rsnd_mod *mod,
119 struct rsnd_dai_stream *io,
120 struct rsnd_priv *priv)
121{
122 int ret;
123
124 ret = rsnd_ssiu_init(mod, io, priv);
125 if (ret < 0)
126 return ret;
127
Kuninori Morimotoeed76bb2016-02-25 05:54:58 +0000128 if (rsnd_runtime_is_ssi_tdm(io)) {
Kuninori Morimoto186fadc2015-11-30 08:54:03 +0000129 /*
130 * TDM Extend Mode
131 * see
132 * rsnd_ssi_config_init()
133 */
134 rsnd_mod_write(mod, SSI_MODE, 0x1);
135 }
136
Kuninori Morimotoc7f69ab2015-10-26 08:43:41 +0000137 if (rsnd_ssi_use_busif(io)) {
Kuninori Morimotoc7f69ab2015-10-26 08:43:41 +0000138 rsnd_mod_write(mod, SSI_BUSIF_ADINR,
Kuninori Morimotoc45f7262015-11-30 08:49:33 +0000139 rsnd_get_adinr_bit(mod, io) |
Kuninori Morimotoeed76bb2016-02-25 05:54:58 +0000140 (rsnd_io_is_play(io) ?
141 rsnd_runtime_channel_after_ctu(io) :
142 rsnd_runtime_channel_original(io)));
Kuninori Morimotoc7f69ab2015-10-26 08:43:41 +0000143 rsnd_mod_write(mod, SSI_BUSIF_MODE, 1);
Kuninori Morimoto135bb7d2016-02-25 05:52:13 +0000144 rsnd_mod_write(mod, SSI_BUSIF_DALIGN,
145 rsnd_get_dalign(mod, io));
Kuninori Morimotoc7f69ab2015-10-26 08:43:41 +0000146 }
147
148 return 0;
149}
150
151static int rsnd_ssiu_start_gen2(struct rsnd_mod *mod,
152 struct rsnd_dai_stream *io,
153 struct rsnd_priv *priv)
154{
Kuninori Morimotob4c83b12015-12-17 03:00:10 +0000155 if (!rsnd_ssi_use_busif(io))
156 return 0;
157
158 rsnd_mod_write(mod, SSI_CTRL, 0x1);
159
Kuninori Morimoto4f5c6342016-02-18 08:18:54 +0000160 if (rsnd_ssi_multi_slaves_runtime(io))
Kuninori Morimotob4c83b12015-12-17 03:00:10 +0000161 rsnd_mod_write(mod, SSI_CONTROL, 0x1);
Kuninori Morimotoc7f69ab2015-10-26 08:43:41 +0000162
163 return 0;
164}
165
166static int rsnd_ssiu_stop_gen2(struct rsnd_mod *mod,
167 struct rsnd_dai_stream *io,
168 struct rsnd_priv *priv)
169{
Kuninori Morimotob4c83b12015-12-17 03:00:10 +0000170 if (!rsnd_ssi_use_busif(io))
171 return 0;
172
173 rsnd_mod_write(mod, SSI_CTRL, 0);
174
Kuninori Morimoto4f5c6342016-02-18 08:18:54 +0000175 if (rsnd_ssi_multi_slaves_runtime(io))
Kuninori Morimotob4c83b12015-12-17 03:00:10 +0000176 rsnd_mod_write(mod, SSI_CONTROL, 0);
Kuninori Morimotoc7f69ab2015-10-26 08:43:41 +0000177
178 return 0;
179}
180
181static struct rsnd_mod_ops rsnd_ssiu_ops_gen2 = {
182 .name = SSIU_NAME,
183 .init = rsnd_ssiu_init_gen2,
184 .start = rsnd_ssiu_start_gen2,
185 .stop = rsnd_ssiu_stop_gen2,
186};
187
188static struct rsnd_mod *rsnd_ssiu_mod_get(struct rsnd_priv *priv, int id)
189{
190 if (WARN_ON(id < 0 || id >= rsnd_ssiu_nr(priv)))
191 id = 0;
192
193 return rsnd_mod_get((struct rsnd_ssiu *)(priv->ssiu) + id);
194}
195
196int rsnd_ssiu_attach(struct rsnd_dai_stream *io,
197 struct rsnd_mod *ssi_mod)
198{
199 struct rsnd_priv *priv = rsnd_io_to_priv(io);
200 struct rsnd_mod *mod = rsnd_ssiu_mod_get(priv, rsnd_mod_id(ssi_mod));
201
202 rsnd_mod_confirm_ssi(ssi_mod);
203
204 return rsnd_dai_connect(mod, io, mod->type);
205}
206
Kuninori Morimoto2ea6b072015-11-10 05:14:12 +0000207int rsnd_ssiu_probe(struct rsnd_priv *priv)
Kuninori Morimotoc7f69ab2015-10-26 08:43:41 +0000208{
209 struct device *dev = rsnd_priv_to_dev(priv);
210 struct rsnd_ssiu *ssiu;
211 static struct rsnd_mod_ops *ops;
212 int i, nr, ret;
213
214 /* same number to SSI */
215 nr = priv->ssi_nr;
216 ssiu = devm_kzalloc(dev, sizeof(*ssiu) * nr, GFP_KERNEL);
217 if (!ssiu)
218 return -ENOMEM;
219
220 priv->ssiu = ssiu;
221 priv->ssiu_nr = nr;
222
223 if (rsnd_is_gen1(priv))
224 ops = &rsnd_ssiu_ops_gen1;
225 else
226 ops = &rsnd_ssiu_ops_gen2;
227
228 for_each_rsnd_ssiu(ssiu, priv, i) {
229 ret = rsnd_mod_init(priv, rsnd_mod_get(ssiu),
Kuninori Morimoto5ba17b422016-01-21 01:58:07 +0000230 ops, NULL, rsnd_mod_get_status,
231 RSND_MOD_SSIU, i);
Kuninori Morimotoc7f69ab2015-10-26 08:43:41 +0000232 if (ret)
233 return ret;
234 }
235
236 return 0;
237}
238
Kuninori Morimoto2ea6b072015-11-10 05:14:12 +0000239void rsnd_ssiu_remove(struct rsnd_priv *priv)
Kuninori Morimotoc7f69ab2015-10-26 08:43:41 +0000240{
241 struct rsnd_ssiu *ssiu;
242 int i;
243
244 for_each_rsnd_ssiu(ssiu, priv, i) {
245 rsnd_mod_quit(rsnd_mod_get(ssiu));
246 }
247}