blob: 47bdba9fc58228878289f0f5b3b2245785812c6b [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 Morimoto4b30eeb2017-03-03 04:25:09 +000067 if (id == 8) {
68 /*
69 * SSI8 pin is sharing with SSI7, nothing to do.
70 */
71 } else if (rsnd_ssi_is_pin_sharing(io)) {
Kuninori Morimotoc7f69ab2015-10-26 08:43:41 +000072 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;
Kuninori Morimotob4c83b12015-12-17 03:00:10 +000084 default:
85 return -EINVAL;
Kuninori Morimotoc7f69ab2015-10-26 08:43:41 +000086 }
87
Kuninori Morimotob4c83b12015-12-17 03:00:10 +000088 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 */
Dan Carpenter8012c982016-01-06 12:38:41 +0300100 (rsnd_rdai_is_clk_master(rdai) ? 0x2 : 0x1);
Kuninori Morimotob4c83b12015-12-17 03:00:10 +0000101 /* 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 }
Kuninori Morimotoc7f69ab2015-10-26 08:43:41 +0000109 }
110
Kuninori Morimotob4c83b12015-12-17 03:00:10 +0000111 rsnd_mod_bset(mod, SSI_MODE1, mask1, val1);
112 rsnd_mod_bset(mod, SSI_MODE2, mask2, val2);
113
Kuninori Morimotoc7f69ab2015-10-26 08:43:41 +0000114 return 0;
115}
116
117static struct rsnd_mod_ops rsnd_ssiu_ops_gen1 = {
118 .name = SSIU_NAME,
119 .init = rsnd_ssiu_init,
120};
121
122static int rsnd_ssiu_init_gen2(struct rsnd_mod *mod,
123 struct rsnd_dai_stream *io,
124 struct rsnd_priv *priv)
125{
Kuninori Morimoto7fa72cc2017-05-18 01:28:22 +0000126 int hdmi = rsnd_ssi_hdmi_port(io);
Kuninori Morimotoc7f69ab2015-10-26 08:43:41 +0000127 int ret;
Jiada Wanga91d7fb2017-11-28 16:05:13 +0900128 u32 mode = 0;
Kuninori Morimotoc7f69ab2015-10-26 08:43:41 +0000129
130 ret = rsnd_ssiu_init(mod, io, priv);
131 if (ret < 0)
132 return ret;
133
Kuninori Morimotoeed76bb2016-02-25 05:54:58 +0000134 if (rsnd_runtime_is_ssi_tdm(io)) {
Kuninori Morimoto186fadc2015-11-30 08:54:03 +0000135 /*
136 * TDM Extend Mode
137 * see
138 * rsnd_ssi_config_init()
139 */
Jiada Wanga91d7fb2017-11-28 16:05:13 +0900140 mode = 0x1;
Kuninori Morimoto186fadc2015-11-30 08:54:03 +0000141 }
142
Jiada Wanga91d7fb2017-11-28 16:05:13 +0900143 rsnd_mod_write(mod, SSI_MODE, mode);
144
Kuninori Morimotoc7f69ab2015-10-26 08:43:41 +0000145 if (rsnd_ssi_use_busif(io)) {
Kuninori Morimotoc7f69ab2015-10-26 08:43:41 +0000146 rsnd_mod_write(mod, SSI_BUSIF_ADINR,
Kuninori Morimotoc45f7262015-11-30 08:49:33 +0000147 rsnd_get_adinr_bit(mod, io) |
Kuninori Morimotoeed76bb2016-02-25 05:54:58 +0000148 (rsnd_io_is_play(io) ?
149 rsnd_runtime_channel_after_ctu(io) :
150 rsnd_runtime_channel_original(io)));
Kuninori Morimoto90431eb2017-05-16 01:51:41 +0000151 rsnd_mod_write(mod, SSI_BUSIF_MODE,
152 rsnd_get_busif_shift(io, mod) | 1);
Kuninori Morimoto135bb7d2016-02-25 05:52:13 +0000153 rsnd_mod_write(mod, SSI_BUSIF_DALIGN,
154 rsnd_get_dalign(mod, io));
Kuninori Morimotoc7f69ab2015-10-26 08:43:41 +0000155 }
156
Kuninori Morimoto7fa72cc2017-05-18 01:28:22 +0000157 if (hdmi) {
158 enum rsnd_mod_type rsnd_ssi_array[] = {
159 RSND_MOD_SSIM1,
160 RSND_MOD_SSIM2,
161 RSND_MOD_SSIM3,
162 };
163 struct rsnd_mod *ssi_mod = rsnd_io_to_mod_ssi(io);
164 struct rsnd_mod *pos;
165 u32 val;
166 int i, shift;
167
168 i = rsnd_mod_id(ssi_mod);
169
170 /* output all same SSI as default */
171 val = i << 16 |
172 i << 20 |
173 i << 24 |
174 i << 28 |
175 i;
176
177 for_each_rsnd_mod_array(i, pos, io, rsnd_ssi_array) {
178 shift = (i * 4) + 16;
179 val = (val & ~(0xF << shift)) |
180 rsnd_mod_id(pos) << shift;
181 }
182
183 switch (hdmi) {
184 case RSND_SSI_HDMI_PORT0:
185 rsnd_mod_write(mod, HDMI0_SEL, val);
186 break;
187 case RSND_SSI_HDMI_PORT1:
188 rsnd_mod_write(mod, HDMI1_SEL, val);
189 break;
190 }
191 }
192
Kuninori Morimotoc7f69ab2015-10-26 08:43:41 +0000193 return 0;
194}
195
196static int rsnd_ssiu_start_gen2(struct rsnd_mod *mod,
197 struct rsnd_dai_stream *io,
198 struct rsnd_priv *priv)
199{
Kuninori Morimotob4c83b12015-12-17 03:00:10 +0000200 if (!rsnd_ssi_use_busif(io))
201 return 0;
202
203 rsnd_mod_write(mod, SSI_CTRL, 0x1);
204
Kuninori Morimoto4f5c6342016-02-18 08:18:54 +0000205 if (rsnd_ssi_multi_slaves_runtime(io))
Kuninori Morimotob4c83b12015-12-17 03:00:10 +0000206 rsnd_mod_write(mod, SSI_CONTROL, 0x1);
Kuninori Morimotoc7f69ab2015-10-26 08:43:41 +0000207
208 return 0;
209}
210
211static int rsnd_ssiu_stop_gen2(struct rsnd_mod *mod,
212 struct rsnd_dai_stream *io,
213 struct rsnd_priv *priv)
214{
Kuninori Morimotob4c83b12015-12-17 03:00:10 +0000215 if (!rsnd_ssi_use_busif(io))
216 return 0;
217
218 rsnd_mod_write(mod, SSI_CTRL, 0);
219
Kuninori Morimoto4f5c6342016-02-18 08:18:54 +0000220 if (rsnd_ssi_multi_slaves_runtime(io))
Kuninori Morimotob4c83b12015-12-17 03:00:10 +0000221 rsnd_mod_write(mod, SSI_CONTROL, 0);
Kuninori Morimotoc7f69ab2015-10-26 08:43:41 +0000222
223 return 0;
224}
225
226static struct rsnd_mod_ops rsnd_ssiu_ops_gen2 = {
227 .name = SSIU_NAME,
228 .init = rsnd_ssiu_init_gen2,
229 .start = rsnd_ssiu_start_gen2,
230 .stop = rsnd_ssiu_stop_gen2,
231};
232
233static struct rsnd_mod *rsnd_ssiu_mod_get(struct rsnd_priv *priv, int id)
234{
235 if (WARN_ON(id < 0 || id >= rsnd_ssiu_nr(priv)))
236 id = 0;
237
238 return rsnd_mod_get((struct rsnd_ssiu *)(priv->ssiu) + id);
239}
240
241int rsnd_ssiu_attach(struct rsnd_dai_stream *io,
242 struct rsnd_mod *ssi_mod)
243{
244 struct rsnd_priv *priv = rsnd_io_to_priv(io);
245 struct rsnd_mod *mod = rsnd_ssiu_mod_get(priv, rsnd_mod_id(ssi_mod));
246
247 rsnd_mod_confirm_ssi(ssi_mod);
248
249 return rsnd_dai_connect(mod, io, mod->type);
250}
251
Kuninori Morimoto2ea6b072015-11-10 05:14:12 +0000252int rsnd_ssiu_probe(struct rsnd_priv *priv)
Kuninori Morimotoc7f69ab2015-10-26 08:43:41 +0000253{
254 struct device *dev = rsnd_priv_to_dev(priv);
255 struct rsnd_ssiu *ssiu;
Gustavo A. R. Silva32e1b602017-07-17 10:30:20 -0500256 struct rsnd_mod_ops *ops;
Kuninori Morimotoc7f69ab2015-10-26 08:43:41 +0000257 int i, nr, ret;
258
259 /* same number to SSI */
260 nr = priv->ssi_nr;
Kees Cooka86854d2018-06-12 14:07:58 -0700261 ssiu = devm_kcalloc(dev, nr, sizeof(*ssiu), GFP_KERNEL);
Kuninori Morimotoc7f69ab2015-10-26 08:43:41 +0000262 if (!ssiu)
263 return -ENOMEM;
264
265 priv->ssiu = ssiu;
266 priv->ssiu_nr = nr;
267
268 if (rsnd_is_gen1(priv))
269 ops = &rsnd_ssiu_ops_gen1;
270 else
271 ops = &rsnd_ssiu_ops_gen2;
272
273 for_each_rsnd_ssiu(ssiu, priv, i) {
274 ret = rsnd_mod_init(priv, rsnd_mod_get(ssiu),
Kuninori Morimoto5ba17b422016-01-21 01:58:07 +0000275 ops, NULL, rsnd_mod_get_status,
276 RSND_MOD_SSIU, i);
Kuninori Morimotoc7f69ab2015-10-26 08:43:41 +0000277 if (ret)
278 return ret;
279 }
280
281 return 0;
282}
283
Kuninori Morimoto2ea6b072015-11-10 05:14:12 +0000284void rsnd_ssiu_remove(struct rsnd_priv *priv)
Kuninori Morimotoc7f69ab2015-10-26 08:43:41 +0000285{
286 struct rsnd_ssiu *ssiu;
287 int i;
288
289 for_each_rsnd_ssiu(ssiu, priv, i) {
290 rsnd_mod_quit(rsnd_mod_get(ssiu));
291 }
292}