xref: /openbmc/linux/sound/soc/sh/rcar/ssiu.c (revision bb0eb050)
1 /*
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 
14 struct 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 
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 	int ret;
127 
128 	ret = rsnd_ssiu_init(mod, io, priv);
129 	if (ret < 0)
130 		return ret;
131 
132 	if (rsnd_runtime_is_ssi_tdm(io)) {
133 		/*
134 		 * TDM Extend Mode
135 		 * see
136 		 *	rsnd_ssi_config_init()
137 		 */
138 		rsnd_mod_write(mod, SSI_MODE, 0x1);
139 	}
140 
141 	if (rsnd_ssi_use_busif(io)) {
142 		rsnd_mod_write(mod, SSI_BUSIF_ADINR,
143 			       rsnd_get_adinr_bit(mod, io) |
144 			       (rsnd_io_is_play(io) ?
145 				rsnd_runtime_channel_after_ctu(io) :
146 				rsnd_runtime_channel_original(io)));
147 		rsnd_mod_write(mod, SSI_BUSIF_MODE,  1);
148 		rsnd_mod_write(mod, SSI_BUSIF_DALIGN,
149 			       rsnd_get_dalign(mod, io));
150 	}
151 
152 	return 0;
153 }
154 
155 static int rsnd_ssiu_start_gen2(struct rsnd_mod *mod,
156 				struct rsnd_dai_stream *io,
157 				struct rsnd_priv *priv)
158 {
159 	if (!rsnd_ssi_use_busif(io))
160 		return 0;
161 
162 	rsnd_mod_write(mod, SSI_CTRL, 0x1);
163 
164 	if (rsnd_ssi_multi_slaves_runtime(io))
165 		rsnd_mod_write(mod, SSI_CONTROL, 0x1);
166 
167 	return 0;
168 }
169 
170 static int rsnd_ssiu_stop_gen2(struct rsnd_mod *mod,
171 			       struct rsnd_dai_stream *io,
172 			       struct rsnd_priv *priv)
173 {
174 	if (!rsnd_ssi_use_busif(io))
175 		return 0;
176 
177 	rsnd_mod_write(mod, SSI_CTRL, 0);
178 
179 	if (rsnd_ssi_multi_slaves_runtime(io))
180 		rsnd_mod_write(mod, SSI_CONTROL, 0);
181 
182 	return 0;
183 }
184 
185 static struct rsnd_mod_ops rsnd_ssiu_ops_gen2 = {
186 	.name	= SSIU_NAME,
187 	.init	= rsnd_ssiu_init_gen2,
188 	.start	= rsnd_ssiu_start_gen2,
189 	.stop	= rsnd_ssiu_stop_gen2,
190 };
191 
192 static struct rsnd_mod *rsnd_ssiu_mod_get(struct rsnd_priv *priv, int id)
193 {
194 	if (WARN_ON(id < 0 || id >= rsnd_ssiu_nr(priv)))
195 		id = 0;
196 
197 	return rsnd_mod_get((struct rsnd_ssiu *)(priv->ssiu) + id);
198 }
199 
200 int rsnd_ssiu_attach(struct rsnd_dai_stream *io,
201 		     struct rsnd_mod *ssi_mod)
202 {
203 	struct rsnd_priv *priv = rsnd_io_to_priv(io);
204 	struct rsnd_mod *mod = rsnd_ssiu_mod_get(priv, rsnd_mod_id(ssi_mod));
205 
206 	rsnd_mod_confirm_ssi(ssi_mod);
207 
208 	return rsnd_dai_connect(mod, io, mod->type);
209 }
210 
211 int rsnd_ssiu_probe(struct rsnd_priv *priv)
212 {
213 	struct device *dev = rsnd_priv_to_dev(priv);
214 	struct rsnd_ssiu *ssiu;
215 	static struct rsnd_mod_ops *ops;
216 	int i, nr, ret;
217 
218 	/* same number to SSI */
219 	nr	= priv->ssi_nr;
220 	ssiu	= devm_kzalloc(dev, sizeof(*ssiu) * nr, GFP_KERNEL);
221 	if (!ssiu)
222 		return -ENOMEM;
223 
224 	priv->ssiu	= ssiu;
225 	priv->ssiu_nr	= nr;
226 
227 	if (rsnd_is_gen1(priv))
228 		ops = &rsnd_ssiu_ops_gen1;
229 	else
230 		ops = &rsnd_ssiu_ops_gen2;
231 
232 	for_each_rsnd_ssiu(ssiu, priv, i) {
233 		ret = rsnd_mod_init(priv, rsnd_mod_get(ssiu),
234 				    ops, NULL, rsnd_mod_get_status,
235 				    RSND_MOD_SSIU, i);
236 		if (ret)
237 			return ret;
238 	}
239 
240 	return 0;
241 }
242 
243 void rsnd_ssiu_remove(struct rsnd_priv *priv)
244 {
245 	struct rsnd_ssiu *ssiu;
246 	int i;
247 
248 	for_each_rsnd_ssiu(ssiu, priv, i) {
249 		rsnd_mod_quit(rsnd_mod_get(ssiu));
250 	}
251 }
252