rk_i2s.c revision 1.4 1 1.4 mrg /* $NetBSD: rk_i2s.c,v 1.4 2020/12/31 06:44:11 mrg Exp $ */
2 1.1 jmcneill
3 1.1 jmcneill /*-
4 1.1 jmcneill * Copyright (c) 2019 Jared McNeill <jmcneill (at) invisible.ca>
5 1.1 jmcneill * All rights reserved.
6 1.1 jmcneill *
7 1.1 jmcneill * Redistribution and use in source and binary forms, with or without
8 1.1 jmcneill * modification, are permitted provided that the following conditions
9 1.1 jmcneill * are met:
10 1.1 jmcneill * 1. Redistributions of source code must retain the above copyright
11 1.1 jmcneill * notice, this list of conditions and the following disclaimer.
12 1.1 jmcneill * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 jmcneill * notice, this list of conditions and the following disclaimer in the
14 1.1 jmcneill * documentation and/or other materials provided with the distribution.
15 1.1 jmcneill *
16 1.1 jmcneill * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 1.1 jmcneill * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 1.1 jmcneill * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 1.1 jmcneill * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 1.1 jmcneill * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 1.1 jmcneill * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 1.1 jmcneill * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 1.1 jmcneill * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 1.1 jmcneill * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 1.1 jmcneill * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 1.1 jmcneill * SUCH DAMAGE.
27 1.1 jmcneill */
28 1.1 jmcneill
29 1.1 jmcneill #include <sys/cdefs.h>
30 1.4 mrg __KERNEL_RCSID(0, "$NetBSD: rk_i2s.c,v 1.4 2020/12/31 06:44:11 mrg Exp $");
31 1.1 jmcneill
32 1.1 jmcneill #include <sys/param.h>
33 1.1 jmcneill #include <sys/bus.h>
34 1.1 jmcneill #include <sys/cpu.h>
35 1.1 jmcneill #include <sys/device.h>
36 1.1 jmcneill #include <sys/kmem.h>
37 1.1 jmcneill
38 1.1 jmcneill #include <sys/audioio.h>
39 1.1 jmcneill #include <dev/audio/audio_if.h>
40 1.1 jmcneill #include <dev/audio/linear.h>
41 1.1 jmcneill
42 1.1 jmcneill #include <dev/fdt/fdtvar.h>
43 1.1 jmcneill #include <dev/fdt/syscon.h>
44 1.1 jmcneill
45 1.1 jmcneill #define RK_I2S_FIFO_DEPTH 32
46 1.1 jmcneill #define RK_I2S_SAMPLE_RATE 48000
47 1.1 jmcneill
48 1.1 jmcneill #define I2S_TXCR 0x00
49 1.1 jmcneill #define TXCR_RCNT __BITS(22,17)
50 1.1 jmcneill #define TXCR_TCSR __BITS(16,15)
51 1.1 jmcneill #define TXCR_HWT __BIT(14)
52 1.1 jmcneill #define TXCR_SJM __BIT(12)
53 1.1 jmcneill #define TXCR_FBM __BIT(11)
54 1.1 jmcneill #define TXCR_IBM __BITS(10,9)
55 1.1 jmcneill #define TXCR_PBM __BITS(8,7)
56 1.1 jmcneill #define TXCR_TFS __BIT(5)
57 1.1 jmcneill #define TXCR_VDW __BITS(4,0)
58 1.1 jmcneill #define I2S_RXCR 0x04
59 1.1 jmcneill #define RXCR_RCSR __BITS(16,15)
60 1.1 jmcneill #define RXCR_HWT __BIT(14)
61 1.1 jmcneill #define RXCR_SJM __BIT(12)
62 1.1 jmcneill #define RXCR_FBM __BIT(11)
63 1.1 jmcneill #define RXCR_IBM __BITS(10,9)
64 1.1 jmcneill #define RXCR_PBM __BITS(8,7)
65 1.1 jmcneill #define RXCR_TFS __BIT(5)
66 1.1 jmcneill #define RXCR_VDW __BITS(4,0)
67 1.1 jmcneill #define I2S_CKR 0x08
68 1.1 jmcneill #define CKR_TRCM __BITS(29,28)
69 1.1 jmcneill #define CKR_MSS __BIT(27)
70 1.1 jmcneill #define CKR_CKP __BIT(26)
71 1.1 jmcneill #define CKR_RLP __BIT(25)
72 1.1 jmcneill #define CKR_TLP __BIT(24)
73 1.1 jmcneill #define CKR_MDIV __BITS(23,16)
74 1.1 jmcneill #define CKR_RSD __BITS(15,8)
75 1.1 jmcneill #define CKR_TSD __BITS(7,0)
76 1.1 jmcneill #define I2S_TXFIFOLR 0x0c
77 1.1 jmcneill #define TXFIFOLR_TFL(n) __BITS((n) * 6 + 5, (n) * 6)
78 1.1 jmcneill #define I2S_DMACR 0x10
79 1.1 jmcneill #define DMACR_RDE __BIT(24)
80 1.1 jmcneill #define DMACR_RDL __BITS(20,16)
81 1.1 jmcneill #define DMACR_TDE __BIT(8)
82 1.1 jmcneill #define DMACR_TDL __BITS(4,0)
83 1.1 jmcneill #define I2S_INTCR 0x14
84 1.1 jmcneill #define INTCR_RFT __BITS(24,20)
85 1.1 jmcneill #define INTCR_RXOIC __BIT(18)
86 1.1 jmcneill #define INTCR_RXOIE __BIT(17)
87 1.1 jmcneill #define INTCR_RXFIE __BIT(16)
88 1.1 jmcneill #define INTCR_TFT __BITS(8,4)
89 1.1 jmcneill #define INTCR_TXUIC __BIT(2)
90 1.1 jmcneill #define INTCR_TXUIE __BIT(1)
91 1.1 jmcneill #define INTCR_TXEIE __BIT(0)
92 1.1 jmcneill #define I2S_INTSR 0x18
93 1.1 jmcneill #define INTSR_RXOI __BIT(17)
94 1.1 jmcneill #define INTSR_RXFI __BIT(16)
95 1.1 jmcneill #define INTSR_TXUI __BIT(1)
96 1.1 jmcneill #define INTSR_TXEI __BIT(0)
97 1.1 jmcneill #define I2S_XFER 0x1c
98 1.1 jmcneill #define XFER_RXS __BIT(1)
99 1.1 jmcneill #define XFER_TXS __BIT(0)
100 1.1 jmcneill #define I2S_CLR 0x20
101 1.1 jmcneill #define CLR_RXC __BIT(1)
102 1.1 jmcneill #define CLR_TXC __BIT(0)
103 1.1 jmcneill #define I2S_TXDR 0x24
104 1.1 jmcneill #define I2S_RXDR 0x28
105 1.1 jmcneill #define I2S_RXFIFOLR 0x2c
106 1.1 jmcneill #define RXFIFOLR_RFL(n) __BITS((n) * 6 + 5, (n) * 6)
107 1.1 jmcneill
108 1.1 jmcneill struct rk_i2s_config {
109 1.1 jmcneill bus_size_t oe_reg;
110 1.1 jmcneill u_int oe_mask;
111 1.1 jmcneill u_int oe_val;
112 1.1 jmcneill };
113 1.1 jmcneill
114 1.1 jmcneill static const struct rk_i2s_config rk3399_i2s_config = {
115 1.1 jmcneill .oe_reg = 0x0e220,
116 1.1 jmcneill .oe_mask = __BITS(13,11),
117 1.1 jmcneill .oe_val = 0x7,
118 1.1 jmcneill };
119 1.1 jmcneill
120 1.1 jmcneill static const struct of_compat_data compat_data[] = {
121 1.1 jmcneill { "rockchip,rk3399-i2s", (uintptr_t)&rk3399_i2s_config },
122 1.1 jmcneill { NULL }
123 1.1 jmcneill };
124 1.1 jmcneill
125 1.1 jmcneill struct rk_i2s_softc;
126 1.1 jmcneill
127 1.1 jmcneill struct rk_i2s_chan {
128 1.1 jmcneill uint32_t *ch_start;
129 1.1 jmcneill uint32_t *ch_end;
130 1.1 jmcneill uint32_t *ch_cur;
131 1.1 jmcneill
132 1.1 jmcneill int ch_blksize;
133 1.1 jmcneill int ch_resid;
134 1.1 jmcneill
135 1.1 jmcneill void (*ch_intr)(void *);
136 1.1 jmcneill void *ch_intrarg;
137 1.1 jmcneill };
138 1.1 jmcneill
139 1.1 jmcneill struct rk_i2s_softc {
140 1.1 jmcneill device_t sc_dev;
141 1.1 jmcneill bus_space_tag_t sc_bst;
142 1.1 jmcneill bus_space_handle_t sc_bsh;
143 1.1 jmcneill int sc_phandle;
144 1.1 jmcneill struct clk *sc_clk;
145 1.1 jmcneill struct syscon *sc_grf;
146 1.1 jmcneill const struct rk_i2s_config *sc_conf;
147 1.1 jmcneill
148 1.1 jmcneill kmutex_t sc_lock;
149 1.1 jmcneill kmutex_t sc_intr_lock;
150 1.1 jmcneill
151 1.1 jmcneill struct audio_format sc_format;
152 1.1 jmcneill
153 1.1 jmcneill struct rk_i2s_chan sc_pchan;
154 1.1 jmcneill struct rk_i2s_chan sc_rchan;
155 1.1 jmcneill
156 1.1 jmcneill u_int sc_active;
157 1.1 jmcneill
158 1.1 jmcneill struct audio_dai_device sc_dai;
159 1.1 jmcneill };
160 1.1 jmcneill
161 1.1 jmcneill #define RD4(sc, reg) \
162 1.1 jmcneill bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
163 1.1 jmcneill #define WR4(sc, reg, val) \
164 1.1 jmcneill bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
165 1.1 jmcneill
166 1.1 jmcneill static int
167 1.1 jmcneill rk_i2s_query_format(void *priv, audio_format_query_t *afp)
168 1.1 jmcneill {
169 1.1 jmcneill struct rk_i2s_softc * const sc = priv;
170 1.1 jmcneill
171 1.1 jmcneill return audio_query_format(&sc->sc_format, 1, afp);
172 1.1 jmcneill }
173 1.1 jmcneill
174 1.1 jmcneill static int
175 1.1 jmcneill rk_i2s_set_format(void *priv, int setmode,
176 1.1 jmcneill const audio_params_t *play, const audio_params_t *rec,
177 1.1 jmcneill audio_filter_reg_t *pfil, audio_filter_reg_t *rfil)
178 1.1 jmcneill {
179 1.1 jmcneill struct rk_i2s_softc * const sc = priv;
180 1.1 jmcneill uint32_t ckr, txcr, rxcr;
181 1.1 jmcneill
182 1.1 jmcneill ckr = RD4(sc, I2S_CKR);
183 1.1 jmcneill if ((ckr & CKR_MSS) == 0) {
184 1.1 jmcneill const u_int mclk_rate = clk_get_rate(sc->sc_clk);
185 1.1 jmcneill const u_int bclk_rate = 2 * 32 * RK_I2S_SAMPLE_RATE;
186 1.1 jmcneill const u_int bclk_div = mclk_rate / bclk_rate;
187 1.1 jmcneill const u_int lrck_div = bclk_rate / RK_I2S_SAMPLE_RATE;
188 1.1 jmcneill
189 1.1 jmcneill ckr &= ~CKR_MDIV;
190 1.1 jmcneill ckr |= __SHIFTIN(bclk_div - 1, CKR_MDIV);
191 1.1 jmcneill ckr &= ~CKR_TSD;
192 1.1 jmcneill ckr |= __SHIFTIN(lrck_div - 1, CKR_TSD);
193 1.1 jmcneill ckr &= ~CKR_RSD;
194 1.1 jmcneill ckr |= __SHIFTIN(lrck_div - 1, CKR_RSD);
195 1.1 jmcneill }
196 1.1 jmcneill
197 1.1 jmcneill ckr &= ~CKR_TRCM;
198 1.1 jmcneill ckr |= __SHIFTIN(0, CKR_TRCM);
199 1.1 jmcneill WR4(sc, I2S_CKR, ckr);
200 1.1 jmcneill
201 1.1 jmcneill if (play && (setmode & AUMODE_PLAY) != 0) {
202 1.1 jmcneill if (play->channels & 1)
203 1.1 jmcneill return EINVAL;
204 1.1 jmcneill txcr = RD4(sc, I2S_TXCR);
205 1.1 jmcneill txcr &= ~TXCR_VDW;
206 1.1 jmcneill txcr |= __SHIFTIN(play->validbits - 1, TXCR_VDW);
207 1.1 jmcneill txcr &= ~TXCR_TCSR;
208 1.1 jmcneill txcr |= __SHIFTIN(play->channels / 2 - 1, TXCR_TCSR);
209 1.1 jmcneill WR4(sc, I2S_TXCR, txcr);
210 1.1 jmcneill }
211 1.1 jmcneill
212 1.1 jmcneill if (rec && (setmode & AUMODE_RECORD) != 0) {
213 1.1 jmcneill if (rec->channels & 1)
214 1.1 jmcneill return EINVAL;
215 1.1 jmcneill rxcr = RD4(sc, I2S_RXCR);
216 1.1 jmcneill rxcr &= ~RXCR_VDW;
217 1.1 jmcneill rxcr |= __SHIFTIN(rec->validbits - 1, RXCR_VDW);
218 1.1 jmcneill rxcr &= ~RXCR_RCSR;
219 1.1 jmcneill rxcr |= __SHIFTIN(rec->channels / 2 - 1, RXCR_RCSR);
220 1.1 jmcneill WR4(sc, I2S_RXCR, rxcr);
221 1.1 jmcneill }
222 1.1 jmcneill
223 1.1 jmcneill return 0;
224 1.1 jmcneill }
225 1.1 jmcneill
226 1.1 jmcneill static int
227 1.1 jmcneill rk_i2s_get_props(void *priv)
228 1.1 jmcneill {
229 1.1 jmcneill
230 1.1 jmcneill return AUDIO_PROP_PLAYBACK | AUDIO_PROP_CAPTURE |
231 1.1 jmcneill AUDIO_PROP_FULLDUPLEX;
232 1.1 jmcneill }
233 1.1 jmcneill
234 1.1 jmcneill static void *
235 1.1 jmcneill rk_i2s_allocm(void *priv, int dir, size_t size)
236 1.1 jmcneill {
237 1.1 jmcneill return kmem_zalloc(size, KM_SLEEP);
238 1.1 jmcneill }
239 1.1 jmcneill
240 1.1 jmcneill static void
241 1.1 jmcneill rk_i2s_freem(void *priv, void *addr, size_t size)
242 1.1 jmcneill {
243 1.1 jmcneill kmem_free(addr, size);
244 1.1 jmcneill }
245 1.1 jmcneill
246 1.1 jmcneill static int
247 1.1 jmcneill rk_i2s_trigger_output(void *priv, void *start, void *end, int blksize,
248 1.1 jmcneill void (*intr)(void *), void *intrarg, const audio_params_t *params)
249 1.1 jmcneill {
250 1.1 jmcneill struct rk_i2s_softc * const sc = priv;
251 1.1 jmcneill struct rk_i2s_chan *ch = &sc->sc_pchan;
252 1.1 jmcneill uint32_t val;
253 1.1 jmcneill
254 1.1 jmcneill if (sc->sc_active == 0) {
255 1.1 jmcneill val = RD4(sc, I2S_XFER);
256 1.1 jmcneill val |= (XFER_TXS | XFER_RXS);
257 1.1 jmcneill WR4(sc, I2S_XFER, val);
258 1.1 jmcneill }
259 1.1 jmcneill
260 1.1 jmcneill sc->sc_active |= XFER_TXS;
261 1.1 jmcneill
262 1.1 jmcneill val = RD4(sc, I2S_INTCR);
263 1.1 jmcneill val |= INTCR_TXEIE;
264 1.1 jmcneill val &= ~INTCR_TFT;
265 1.1 jmcneill val |= __SHIFTIN(RK_I2S_FIFO_DEPTH / 2, INTCR_TFT);
266 1.1 jmcneill WR4(sc, I2S_INTCR, val);
267 1.1 jmcneill
268 1.1 jmcneill ch->ch_intr = intr;
269 1.1 jmcneill ch->ch_intrarg = intrarg;
270 1.1 jmcneill ch->ch_start = ch->ch_cur = start;
271 1.1 jmcneill ch->ch_end = end;
272 1.1 jmcneill ch->ch_blksize = blksize;
273 1.1 jmcneill ch->ch_resid = blksize;
274 1.1 jmcneill
275 1.1 jmcneill return 0;
276 1.1 jmcneill }
277 1.1 jmcneill
278 1.1 jmcneill static int
279 1.1 jmcneill rk_i2s_trigger_input(void *priv, void *start, void *end, int blksize,
280 1.1 jmcneill void (*intr)(void *), void *intrarg, const audio_params_t *params)
281 1.1 jmcneill {
282 1.1 jmcneill return EIO;
283 1.1 jmcneill }
284 1.1 jmcneill
285 1.1 jmcneill static int
286 1.1 jmcneill rk_i2s_halt_output(void *priv)
287 1.1 jmcneill {
288 1.1 jmcneill struct rk_i2s_softc * const sc = priv;
289 1.1 jmcneill struct rk_i2s_chan *ch = &sc->sc_pchan;
290 1.1 jmcneill uint32_t val;
291 1.1 jmcneill
292 1.1 jmcneill sc->sc_active &= ~XFER_TXS;
293 1.1 jmcneill if (sc->sc_active == 0) {
294 1.1 jmcneill val = RD4(sc, I2S_XFER);
295 1.1 jmcneill val &= ~(XFER_TXS|XFER_RXS);
296 1.1 jmcneill WR4(sc, I2S_XFER, val);
297 1.1 jmcneill }
298 1.1 jmcneill
299 1.1 jmcneill val = RD4(sc, I2S_INTCR);
300 1.1 jmcneill val &= ~INTCR_TXEIE;
301 1.1 jmcneill WR4(sc, I2S_INTCR, val);
302 1.1 jmcneill
303 1.1 jmcneill val = RD4(sc, I2S_CLR);
304 1.1 jmcneill val |= CLR_TXC;
305 1.1 jmcneill WR4(sc, I2S_CLR, val);
306 1.1 jmcneill
307 1.1 jmcneill while ((RD4(sc, I2S_CLR) & CLR_TXC) != 0)
308 1.1 jmcneill delay(1);
309 1.1 jmcneill
310 1.1 jmcneill ch->ch_intr = NULL;
311 1.1 jmcneill ch->ch_intrarg = NULL;
312 1.1 jmcneill
313 1.1 jmcneill return 0;
314 1.1 jmcneill }
315 1.1 jmcneill
316 1.1 jmcneill static int
317 1.1 jmcneill rk_i2s_halt_input(void *priv)
318 1.1 jmcneill {
319 1.1 jmcneill struct rk_i2s_softc * const sc = priv;
320 1.1 jmcneill struct rk_i2s_chan *ch = &sc->sc_rchan;
321 1.1 jmcneill uint32_t val;
322 1.1 jmcneill
323 1.1 jmcneill sc->sc_active &= ~XFER_RXS;
324 1.1 jmcneill if (sc->sc_active == 0) {
325 1.1 jmcneill val = RD4(sc, I2S_XFER);
326 1.1 jmcneill val &= ~(XFER_TXS|XFER_RXS);
327 1.1 jmcneill WR4(sc, I2S_XFER, val);
328 1.1 jmcneill }
329 1.1 jmcneill
330 1.1 jmcneill val = RD4(sc, I2S_INTCR);
331 1.1 jmcneill val &= ~INTCR_RXFIE;
332 1.1 jmcneill WR4(sc, I2S_INTCR, val);
333 1.1 jmcneill
334 1.1 jmcneill ch->ch_intr = NULL;
335 1.1 jmcneill ch->ch_intrarg = NULL;
336 1.1 jmcneill
337 1.1 jmcneill return 0;
338 1.1 jmcneill }
339 1.1 jmcneill
340 1.1 jmcneill static void
341 1.1 jmcneill rk_i2s_get_locks(void *priv, kmutex_t **intr, kmutex_t **thread)
342 1.1 jmcneill {
343 1.1 jmcneill struct rk_i2s_softc * const sc = priv;
344 1.1 jmcneill
345 1.1 jmcneill *intr = &sc->sc_intr_lock;
346 1.1 jmcneill *thread = &sc->sc_lock;
347 1.1 jmcneill }
348 1.1 jmcneill
349 1.1 jmcneill static const struct audio_hw_if rk_i2s_hw_if = {
350 1.1 jmcneill .query_format = rk_i2s_query_format,
351 1.1 jmcneill .set_format = rk_i2s_set_format,
352 1.1 jmcneill .get_props = rk_i2s_get_props,
353 1.1 jmcneill .allocm = rk_i2s_allocm,
354 1.1 jmcneill .freem = rk_i2s_freem,
355 1.1 jmcneill .trigger_output = rk_i2s_trigger_output,
356 1.1 jmcneill .trigger_input = rk_i2s_trigger_input,
357 1.1 jmcneill .halt_output = rk_i2s_halt_output,
358 1.1 jmcneill .halt_input = rk_i2s_halt_input,
359 1.1 jmcneill .get_locks = rk_i2s_get_locks,
360 1.1 jmcneill };
361 1.1 jmcneill
362 1.1 jmcneill static int
363 1.1 jmcneill rk_i2s_intr(void *priv)
364 1.1 jmcneill {
365 1.1 jmcneill struct rk_i2s_softc * const sc = priv;
366 1.1 jmcneill struct rk_i2s_chan * const pch = &sc->sc_pchan;
367 1.1 jmcneill #if notyet
368 1.1 jmcneill struct rk_i2s_chan * const rch = &sc->sc_rchan;
369 1.1 jmcneill #endif
370 1.1 jmcneill uint32_t sr, val;
371 1.1 jmcneill int fifolr;
372 1.1 jmcneill
373 1.1 jmcneill mutex_enter(&sc->sc_intr_lock);
374 1.1 jmcneill
375 1.1 jmcneill sr = RD4(sc, I2S_INTSR);
376 1.1 jmcneill
377 1.1 jmcneill if ((sr & INTSR_RXFI) != 0) {
378 1.1 jmcneill #if notyet
379 1.1 jmcneill val = RD4(sc, I2S_RXFIFOLR);
380 1.1 jmcneill fifolr = __SHIFTOUT(val, RXFIFOLR_RFL(0));
381 1.1 jmcneill while (fifolr > 0) {
382 1.1 jmcneill *rch->ch_data = RD4(sc, I2S_RXDR);
383 1.1 jmcneill rch->ch_data++;
384 1.1 jmcneill rch->ch_resid -= 4;
385 1.1 jmcneill if (rch->ch_resid == 0)
386 1.1 jmcneill rch->ch_intr(rch->ch_intrarg);
387 1.1 jmcneill --fifolr;
388 1.1 jmcneill }
389 1.1 jmcneill #endif
390 1.1 jmcneill }
391 1.1 jmcneill
392 1.1 jmcneill if ((sr & INTSR_TXEI) != 0) {
393 1.1 jmcneill val = RD4(sc, I2S_TXFIFOLR);
394 1.1 jmcneill fifolr = __SHIFTOUT(val, TXFIFOLR_TFL(0));
395 1.1 jmcneill fifolr = uimin(fifolr, RK_I2S_FIFO_DEPTH);
396 1.1 jmcneill while (fifolr < RK_I2S_FIFO_DEPTH - 1) {
397 1.1 jmcneill WR4(sc, I2S_TXDR, *pch->ch_cur);
398 1.1 jmcneill pch->ch_cur++;
399 1.1 jmcneill if (pch->ch_cur == pch->ch_end)
400 1.1 jmcneill pch->ch_cur = pch->ch_start;
401 1.1 jmcneill pch->ch_resid -= 4;
402 1.1 jmcneill if (pch->ch_resid == 0) {
403 1.1 jmcneill pch->ch_intr(pch->ch_intrarg);
404 1.1 jmcneill pch->ch_resid = pch->ch_blksize;
405 1.1 jmcneill }
406 1.1 jmcneill ++fifolr;
407 1.1 jmcneill }
408 1.1 jmcneill }
409 1.1 jmcneill
410 1.1 jmcneill mutex_exit(&sc->sc_intr_lock);
411 1.1 jmcneill
412 1.1 jmcneill return 0;
413 1.1 jmcneill }
414 1.1 jmcneill
415 1.1 jmcneill static int
416 1.1 jmcneill rk_i2s_dai_set_sysclk(audio_dai_tag_t dai, u_int rate, int dir)
417 1.1 jmcneill {
418 1.1 jmcneill struct rk_i2s_softc * const sc = audio_dai_private(dai);
419 1.1 jmcneill int error;
420 1.1 jmcneill
421 1.1 jmcneill error = clk_set_rate(sc->sc_clk, rate);
422 1.1 jmcneill if (error != 0) {
423 1.1 jmcneill device_printf(sc->sc_dev, "failed to set sysclk to %u Hz: %d\n",
424 1.1 jmcneill rate, error);
425 1.1 jmcneill return error;
426 1.1 jmcneill }
427 1.1 jmcneill
428 1.1 jmcneill return 0;
429 1.1 jmcneill }
430 1.1 jmcneill
431 1.1 jmcneill static int
432 1.1 jmcneill rk_i2s_dai_set_format(audio_dai_tag_t dai, u_int format)
433 1.1 jmcneill {
434 1.1 jmcneill struct rk_i2s_softc * const sc = audio_dai_private(dai);
435 1.1 jmcneill uint32_t txcr, rxcr, ckr;
436 1.1 jmcneill
437 1.1 jmcneill const u_int fmt = __SHIFTOUT(format, AUDIO_DAI_FORMAT_MASK);
438 1.1 jmcneill const u_int pol = __SHIFTOUT(format, AUDIO_DAI_POLARITY_MASK);
439 1.1 jmcneill const u_int clk = __SHIFTOUT(format, AUDIO_DAI_CLOCK_MASK);
440 1.1 jmcneill
441 1.1 jmcneill txcr = RD4(sc, I2S_TXCR);
442 1.1 jmcneill rxcr = RD4(sc, I2S_RXCR);
443 1.1 jmcneill ckr = RD4(sc, I2S_CKR);
444 1.1 jmcneill
445 1.1 jmcneill txcr &= ~(TXCR_IBM|TXCR_PBM|TXCR_TFS);
446 1.1 jmcneill rxcr &= ~(RXCR_IBM|RXCR_PBM|RXCR_TFS);
447 1.1 jmcneill switch (fmt) {
448 1.1 jmcneill case AUDIO_DAI_FORMAT_I2S:
449 1.1 jmcneill txcr |= __SHIFTIN(0, TXCR_IBM);
450 1.1 jmcneill rxcr |= __SHIFTIN(0, RXCR_IBM);
451 1.1 jmcneill break;
452 1.1 jmcneill case AUDIO_DAI_FORMAT_LJ:
453 1.1 jmcneill txcr |= __SHIFTIN(1, TXCR_IBM);
454 1.1 jmcneill rxcr |= __SHIFTIN(1, RXCR_IBM);
455 1.1 jmcneill break;
456 1.1 jmcneill case AUDIO_DAI_FORMAT_RJ:
457 1.1 jmcneill txcr |= __SHIFTIN(2, TXCR_IBM);
458 1.1 jmcneill rxcr |= __SHIFTIN(2, RXCR_IBM);
459 1.1 jmcneill break;
460 1.1 jmcneill case AUDIO_DAI_FORMAT_DSPA:
461 1.1 jmcneill txcr |= __SHIFTIN(0, TXCR_PBM);
462 1.1 jmcneill txcr |= TXCR_TFS;
463 1.1 jmcneill rxcr |= __SHIFTIN(0, RXCR_PBM);
464 1.1 jmcneill txcr |= RXCR_TFS;
465 1.1 jmcneill break;
466 1.1 jmcneill case AUDIO_DAI_FORMAT_DSPB:
467 1.1 jmcneill txcr |= __SHIFTIN(1, TXCR_PBM);
468 1.1 jmcneill txcr |= TXCR_TFS;
469 1.1 jmcneill rxcr |= __SHIFTIN(1, RXCR_PBM);
470 1.1 jmcneill txcr |= RXCR_TFS;
471 1.1 jmcneill break;
472 1.1 jmcneill default:
473 1.1 jmcneill return EINVAL;
474 1.1 jmcneill }
475 1.1 jmcneill
476 1.1 jmcneill WR4(sc, I2S_TXCR, txcr);
477 1.1 jmcneill WR4(sc, I2S_RXCR, rxcr);
478 1.1 jmcneill
479 1.1 jmcneill switch (pol) {
480 1.1 jmcneill case AUDIO_DAI_POLARITY_IB_NF:
481 1.1 jmcneill ckr |= CKR_CKP;
482 1.1 jmcneill break;
483 1.1 jmcneill case AUDIO_DAI_POLARITY_NB_NF:
484 1.1 jmcneill ckr &= ~CKR_CKP;
485 1.1 jmcneill break;
486 1.1 jmcneill default:
487 1.1 jmcneill return EINVAL;
488 1.1 jmcneill }
489 1.1 jmcneill
490 1.1 jmcneill switch (clk) {
491 1.1 jmcneill case AUDIO_DAI_CLOCK_CBM_CFM:
492 1.1 jmcneill ckr |= CKR_MSS; /* sclk input */
493 1.1 jmcneill break;
494 1.1 jmcneill case AUDIO_DAI_CLOCK_CBS_CFS:
495 1.1 jmcneill ckr &= ~CKR_MSS; /* sclk output */
496 1.1 jmcneill break;
497 1.1 jmcneill default:
498 1.1 jmcneill return EINVAL;
499 1.1 jmcneill }
500 1.1 jmcneill
501 1.1 jmcneill WR4(sc, I2S_CKR, ckr);
502 1.1 jmcneill
503 1.1 jmcneill return 0;
504 1.1 jmcneill }
505 1.1 jmcneill
506 1.1 jmcneill static audio_dai_tag_t
507 1.1 jmcneill rk_i2s_dai_get_tag(device_t dev, const void *data, size_t len)
508 1.1 jmcneill {
509 1.1 jmcneill struct rk_i2s_softc * const sc = device_private(dev);
510 1.1 jmcneill
511 1.1 jmcneill if (len != 4)
512 1.1 jmcneill return NULL;
513 1.1 jmcneill
514 1.1 jmcneill return &sc->sc_dai;
515 1.1 jmcneill }
516 1.1 jmcneill
517 1.1 jmcneill static struct fdtbus_dai_controller_func rk_i2s_dai_funcs = {
518 1.1 jmcneill .get_tag = rk_i2s_dai_get_tag
519 1.1 jmcneill };
520 1.1 jmcneill
521 1.1 jmcneill static int
522 1.1 jmcneill rk_i2s_clock_init(struct rk_i2s_softc *sc)
523 1.1 jmcneill {
524 1.1 jmcneill const int phandle = sc->sc_phandle;
525 1.1 jmcneill int error;
526 1.1 jmcneill
527 1.1 jmcneill sc->sc_clk = fdtbus_clock_get(phandle, "i2s_clk");
528 1.1 jmcneill if (sc->sc_clk == NULL) {
529 1.1 jmcneill aprint_error(": couldn't find i2s_clk clock\n");
530 1.1 jmcneill return ENXIO;
531 1.1 jmcneill }
532 1.1 jmcneill error = clk_enable(sc->sc_clk);
533 1.1 jmcneill if (error != 0) {
534 1.1 jmcneill aprint_error(": couldn't enable i2s_clk clock: %d\n", error);
535 1.1 jmcneill return error;
536 1.1 jmcneill }
537 1.1 jmcneill
538 1.1 jmcneill /* Enable bus clock */
539 1.4 mrg error = fdtbus_clock_enable(phandle, "i2s_hclk", true);
540 1.4 mrg if (error != 0) {
541 1.1 jmcneill aprint_error(": couldn't enable i2s_hclk clock: %d\n", error);
542 1.1 jmcneill return error;
543 1.1 jmcneill }
544 1.1 jmcneill
545 1.1 jmcneill return 0;
546 1.1 jmcneill }
547 1.1 jmcneill
548 1.1 jmcneill static int
549 1.1 jmcneill rk_i2s_match(device_t parent, cfdata_t cf, void *aux)
550 1.1 jmcneill {
551 1.1 jmcneill struct fdt_attach_args * const faa = aux;
552 1.1 jmcneill
553 1.1 jmcneill return of_match_compat_data(faa->faa_phandle, compat_data);
554 1.1 jmcneill }
555 1.1 jmcneill
556 1.1 jmcneill static void
557 1.1 jmcneill rk_i2s_attach(device_t parent, device_t self, void *aux)
558 1.1 jmcneill {
559 1.1 jmcneill struct rk_i2s_softc * const sc = device_private(self);
560 1.1 jmcneill struct fdt_attach_args * const faa = aux;
561 1.1 jmcneill const int phandle = faa->faa_phandle;
562 1.1 jmcneill char intrstr[128];
563 1.1 jmcneill bus_addr_t addr;
564 1.1 jmcneill bus_size_t size;
565 1.1 jmcneill uint32_t val;
566 1.1 jmcneill
567 1.1 jmcneill if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
568 1.1 jmcneill aprint_error(": couldn't get registers\n");
569 1.1 jmcneill return;
570 1.1 jmcneill }
571 1.1 jmcneill if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) {
572 1.1 jmcneill aprint_error(": couldn't decode interrupt\n");
573 1.1 jmcneill return;
574 1.1 jmcneill }
575 1.1 jmcneill
576 1.1 jmcneill sc->sc_dev = self;
577 1.1 jmcneill sc->sc_phandle = phandle;
578 1.1 jmcneill sc->sc_bst = faa->faa_bst;
579 1.1 jmcneill if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) {
580 1.1 jmcneill aprint_error(": couldn't map registers\n");
581 1.1 jmcneill return;
582 1.1 jmcneill }
583 1.1 jmcneill mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE);
584 1.1 jmcneill mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_SCHED);
585 1.1 jmcneill
586 1.1 jmcneill sc->sc_conf = (void *)of_search_compatible(phandle, compat_data)->data;
587 1.1 jmcneill sc->sc_grf = fdtbus_syscon_acquire(phandle, "rockchip,grf");
588 1.1 jmcneill if (sc->sc_grf != NULL && sc->sc_conf->oe_mask != 0) {
589 1.1 jmcneill syscon_lock(sc->sc_grf);
590 1.1 jmcneill val = __SHIFTIN(sc->sc_conf->oe_val, sc->sc_conf->oe_mask);
591 1.1 jmcneill val |= (sc->sc_conf->oe_mask << 16);
592 1.1 jmcneill syscon_write_4(sc->sc_grf, sc->sc_conf->oe_reg, val);
593 1.1 jmcneill syscon_unlock(sc->sc_grf);
594 1.1 jmcneill }
595 1.1 jmcneill
596 1.1 jmcneill if (rk_i2s_clock_init(sc) != 0)
597 1.1 jmcneill return;
598 1.1 jmcneill
599 1.1 jmcneill aprint_naive("\n");
600 1.1 jmcneill aprint_normal(": I2S/PCM controller\n");
601 1.1 jmcneill
602 1.1 jmcneill if (fdtbus_intr_establish(phandle, 0, IPL_AUDIO, FDT_INTR_MPSAFE, rk_i2s_intr, sc) == NULL) {
603 1.1 jmcneill aprint_error_dev(self, "couldn't establish interrupt on %s\n", intrstr);
604 1.1 jmcneill return;
605 1.1 jmcneill }
606 1.1 jmcneill aprint_normal_dev(self, "interrupting on %s\n", intrstr);
607 1.1 jmcneill
608 1.1 jmcneill sc->sc_format.mode = AUMODE_PLAY|AUMODE_RECORD;
609 1.1 jmcneill sc->sc_format.encoding = AUDIO_ENCODING_SLINEAR_LE;
610 1.1 jmcneill sc->sc_format.validbits = 16;
611 1.1 jmcneill sc->sc_format.precision = 16;
612 1.1 jmcneill sc->sc_format.channels = 2;
613 1.1 jmcneill sc->sc_format.channel_mask = AUFMT_STEREO;
614 1.1 jmcneill sc->sc_format.frequency_type = 1;
615 1.1 jmcneill sc->sc_format.frequency[0] = RK_I2S_SAMPLE_RATE;
616 1.1 jmcneill
617 1.1 jmcneill sc->sc_dai.dai_set_sysclk = rk_i2s_dai_set_sysclk;
618 1.1 jmcneill sc->sc_dai.dai_set_format = rk_i2s_dai_set_format;
619 1.1 jmcneill sc->sc_dai.dai_hw_if = &rk_i2s_hw_if;
620 1.1 jmcneill sc->sc_dai.dai_dev = self;
621 1.1 jmcneill sc->sc_dai.dai_priv = sc;
622 1.1 jmcneill fdtbus_register_dai_controller(self, phandle, &rk_i2s_dai_funcs);
623 1.1 jmcneill }
624 1.1 jmcneill
625 1.1 jmcneill CFATTACH_DECL_NEW(rk_i2s, sizeof(struct rk_i2s_softc),
626 1.1 jmcneill rk_i2s_match, rk_i2s_attach, NULL, NULL);
627