Merge branch 'fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/evalenti/linux...
[linux/fpc-iii.git] / sound / soc / sh / rcar / ssiu.c
blob6f9b388ec5a8f0c2095a2a88e33d725244706b2e
1 /*
2 * Renesas R-Car SSIU support
4 * Copyright (c) 2015 Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
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"
12 #define SSIU_NAME "ssiu"
14 struct rsnd_ssiu {
15 struct rsnd_mod mod;
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++)
25 static int rsnd_ssiu_init(struct rsnd_mod *mod,
26 struct rsnd_dai_stream *io,
27 struct rsnd_priv *priv)
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;
37 * SSI_MODE0
39 rsnd_mod_bset(mod, SSI_MODE0, (1 << id), !use_busif << id);
42 * SSI_MODE1
44 mask1 = (1 << 4) | (1 << 20); /* mask sync bit */
45 mask2 = (1 << 4); /* mask sync bit */
46 val1 = val2 = 0;
47 if (rsnd_ssi_is_pin_sharing(io)) {
48 int shift = -1;
50 switch (id) {
51 case 1:
52 shift = 0;
53 break;
54 case 2:
55 shift = 2;
56 break;
57 case 4:
58 shift = 16;
59 break;
60 default:
61 return -EINVAL;
64 mask1 |= 0x3 << shift;
65 val1 = rsnd_rdai_is_clk_master(rdai) ?
66 0x2 << shift : 0x1 << shift;
68 } else if (multi_ssi_slaves) {
70 mask2 |= 0x00000007;
71 mask1 |= 0x0000000f;
73 switch (multi_ssi_slaves) {
74 case 0x0206: /* SSI0/1/2/9 */
75 val2 = (1 << 4) | /* SSI0129 sync */
76 (rsnd_rdai_is_clk_master(rdai) ? 0x2 : 0x1);
77 /* fall through */
78 case 0x0006: /* SSI0/1/2 */
79 val1 = rsnd_rdai_is_clk_master(rdai) ?
80 0xa : 0x5;
82 if (!val2) /* SSI012 sync */
83 val1 |= (1 << 4);
87 rsnd_mod_bset(mod, SSI_MODE1, mask1, val1);
88 rsnd_mod_bset(mod, SSI_MODE2, mask2, val2);
90 return 0;
93 static struct rsnd_mod_ops rsnd_ssiu_ops_gen1 = {
94 .name = SSIU_NAME,
95 .init = rsnd_ssiu_init,
98 static int rsnd_ssiu_init_gen2(struct rsnd_mod *mod,
99 struct rsnd_dai_stream *io,
100 struct rsnd_priv *priv)
102 int ret;
104 ret = rsnd_ssiu_init(mod, io, priv);
105 if (ret < 0)
106 return ret;
108 if (rsnd_runtime_is_ssi_tdm(io)) {
110 * TDM Extend Mode
111 * see
112 * rsnd_ssi_config_init()
114 rsnd_mod_write(mod, SSI_MODE, 0x1);
117 if (rsnd_ssi_use_busif(io)) {
118 rsnd_mod_write(mod, SSI_BUSIF_ADINR,
119 rsnd_get_adinr_bit(mod, io) |
120 (rsnd_io_is_play(io) ?
121 rsnd_runtime_channel_after_ctu(io) :
122 rsnd_runtime_channel_original(io)));
123 rsnd_mod_write(mod, SSI_BUSIF_MODE, 1);
124 rsnd_mod_write(mod, SSI_BUSIF_DALIGN,
125 rsnd_get_dalign(mod, io));
128 return 0;
131 static int rsnd_ssiu_start_gen2(struct rsnd_mod *mod,
132 struct rsnd_dai_stream *io,
133 struct rsnd_priv *priv)
135 if (!rsnd_ssi_use_busif(io))
136 return 0;
138 rsnd_mod_write(mod, SSI_CTRL, 0x1);
140 if (rsnd_ssi_multi_slaves_runtime(io))
141 rsnd_mod_write(mod, SSI_CONTROL, 0x1);
143 return 0;
146 static int rsnd_ssiu_stop_gen2(struct rsnd_mod *mod,
147 struct rsnd_dai_stream *io,
148 struct rsnd_priv *priv)
150 if (!rsnd_ssi_use_busif(io))
151 return 0;
153 rsnd_mod_write(mod, SSI_CTRL, 0);
155 if (rsnd_ssi_multi_slaves_runtime(io))
156 rsnd_mod_write(mod, SSI_CONTROL, 0);
158 return 0;
161 static struct rsnd_mod_ops rsnd_ssiu_ops_gen2 = {
162 .name = SSIU_NAME,
163 .init = rsnd_ssiu_init_gen2,
164 .start = rsnd_ssiu_start_gen2,
165 .stop = rsnd_ssiu_stop_gen2,
168 static struct rsnd_mod *rsnd_ssiu_mod_get(struct rsnd_priv *priv, int id)
170 if (WARN_ON(id < 0 || id >= rsnd_ssiu_nr(priv)))
171 id = 0;
173 return rsnd_mod_get((struct rsnd_ssiu *)(priv->ssiu) + id);
176 int rsnd_ssiu_attach(struct rsnd_dai_stream *io,
177 struct rsnd_mod *ssi_mod)
179 struct rsnd_priv *priv = rsnd_io_to_priv(io);
180 struct rsnd_mod *mod = rsnd_ssiu_mod_get(priv, rsnd_mod_id(ssi_mod));
182 rsnd_mod_confirm_ssi(ssi_mod);
184 return rsnd_dai_connect(mod, io, mod->type);
187 int rsnd_ssiu_probe(struct rsnd_priv *priv)
189 struct device *dev = rsnd_priv_to_dev(priv);
190 struct rsnd_ssiu *ssiu;
191 static struct rsnd_mod_ops *ops;
192 int i, nr, ret;
194 /* same number to SSI */
195 nr = priv->ssi_nr;
196 ssiu = devm_kzalloc(dev, sizeof(*ssiu) * nr, GFP_KERNEL);
197 if (!ssiu)
198 return -ENOMEM;
200 priv->ssiu = ssiu;
201 priv->ssiu_nr = nr;
203 if (rsnd_is_gen1(priv))
204 ops = &rsnd_ssiu_ops_gen1;
205 else
206 ops = &rsnd_ssiu_ops_gen2;
208 for_each_rsnd_ssiu(ssiu, priv, i) {
209 ret = rsnd_mod_init(priv, rsnd_mod_get(ssiu),
210 ops, NULL, rsnd_mod_get_status,
211 RSND_MOD_SSIU, i);
212 if (ret)
213 return ret;
216 return 0;
219 void rsnd_ssiu_remove(struct rsnd_priv *priv)
221 struct rsnd_ssiu *ssiu;
222 int i;
224 for_each_rsnd_ssiu(ssiu, priv, i) {
225 rsnd_mod_quit(rsnd_mod_get(ssiu));