ausoc.c revision 1.2 1 /* $NetBSD: ausoc.c,v 1.2 2018/05/11 22:49:19 jmcneill Exp $ */
2
3 /*-
4 * Copyright (c) 2018 Jared McNeill <jmcneill (at) invisible.ca>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: ausoc.c,v 1.2 2018/05/11 22:49:19 jmcneill Exp $");
31
32 #include <sys/param.h>
33 #include <sys/bus.h>
34 #include <sys/cpu.h>
35 #include <sys/device.h>
36 #include <sys/kmem.h>
37 #include <sys/gpio.h>
38
39 #include <sys/audioio.h>
40 #include <dev/audio_if.h>
41 #include <dev/audio_dai.h>
42
43 #include <dev/fdt/fdtvar.h>
44
45 static const char *compatible[] = { "simple-audio-card", NULL };
46
47 #define AUSOC_MIXER_DAI(link) \
48 ((link)->link_naux > 0 ? (link)->link_aux[0] : (link)->link_codec)
49
50 struct ausoc_link {
51 const char *link_name;
52
53 audio_dai_tag_t link_cpu;
54 audio_dai_tag_t link_codec;
55 audio_dai_tag_t *link_aux;
56 u_int link_naux;
57
58 u_int link_mclk_fs;
59
60 kmutex_t link_lock;
61 kmutex_t link_intr_lock;
62 };
63
64 struct ausoc_softc {
65 device_t sc_dev;
66 int sc_phandle;
67 const char *sc_name;
68
69 struct ausoc_link *sc_link;
70 u_int sc_nlink;
71 };
72
73 static void
74 ausoc_close(void *priv)
75 {
76 struct ausoc_link * const link = priv;
77 u_int aux;
78
79 for (aux = 0; aux < link->link_naux; aux++)
80 audio_dai_close(link->link_aux[aux]);
81 audio_dai_close(link->link_codec);
82 audio_dai_close(link->link_cpu);
83 }
84
85 static int
86 ausoc_open(void *priv, int flags)
87 {
88 struct ausoc_link * const link = priv;
89 u_int aux;
90 int error;
91
92 error = audio_dai_open(link->link_cpu, flags);
93 if (error)
94 goto failed;
95
96 error = audio_dai_open(link->link_codec, flags);
97 if (error)
98 goto failed;
99
100 for (aux = 0; aux < link->link_naux; aux++) {
101 error = audio_dai_open(link->link_aux[aux], flags);
102 if (error)
103 goto failed;
104 }
105
106 return 0;
107
108 failed:
109 ausoc_close(priv);
110 return error;
111 }
112
113 static int
114 ausoc_drain(void *priv)
115 {
116 struct ausoc_link * const link = priv;
117
118 return audio_dai_drain(link->link_cpu);
119 }
120
121 static int
122 ausoc_query_encoding(void *priv, struct audio_encoding *ae)
123 {
124 struct ausoc_link * const link = priv;
125
126 return audio_dai_query_encoding(link->link_cpu, ae);
127 }
128
129 static int
130 ausoc_set_params(void *priv, int setmode, int usemode,
131 audio_params_t *play, audio_params_t *rec,
132 stream_filter_list_t *pfil, stream_filter_list_t *rfil)
133 {
134 struct ausoc_link * const link = priv;
135 int error;
136
137 error = audio_dai_set_params(link->link_cpu, setmode,
138 usemode, play, rec, pfil, rfil);
139 if (error)
140 return error;
141
142 return audio_dai_set_params(link->link_codec, setmode,
143 usemode, play, rec, pfil, rfil);
144 }
145
146 static int
147 ausoc_set_port(void *priv, mixer_ctrl_t *mc)
148 {
149 struct ausoc_link * const link = priv;
150
151 return audio_dai_set_port(AUSOC_MIXER_DAI(link), mc);
152 }
153
154 static int
155 ausoc_get_port(void *priv, mixer_ctrl_t *mc)
156 {
157 struct ausoc_link * const link = priv;
158
159 return audio_dai_get_port(AUSOC_MIXER_DAI(link), mc);
160 }
161
162 static int
163 ausoc_query_devinfo(void *priv, mixer_devinfo_t *di)
164 {
165 struct ausoc_link * const link = priv;
166
167 return audio_dai_query_devinfo(AUSOC_MIXER_DAI(link), di);
168 }
169
170 static void *
171 ausoc_allocm(void *priv, int dir, size_t size)
172 {
173 struct ausoc_link * const link = priv;
174
175 return audio_dai_allocm(link->link_cpu, dir, size);
176 }
177
178 static void
179 ausoc_freem(void *priv, void *addr, size_t size)
180 {
181 struct ausoc_link * const link = priv;
182
183 return audio_dai_freem(link->link_cpu, addr, size);
184 }
185
186 static paddr_t
187 ausoc_mappage(void *priv, void *addr, off_t off, int prot)
188 {
189 struct ausoc_link * const link = priv;
190
191 return audio_dai_mappage(link->link_cpu, addr, off, prot);
192 }
193
194 static int
195 ausoc_getdev(void *priv, struct audio_device *adev)
196 {
197 struct ausoc_link * const link = priv;
198
199 /* Defaults */
200 snprintf(adev->name, sizeof(adev->name), "%s", link->link_name);
201 snprintf(adev->version, sizeof(adev->version), "");
202 snprintf(adev->config, sizeof(adev->config), "ausoc");
203
204 /* Codec can override */
205 (void)audio_dai_getdev(link->link_codec, adev);
206
207 return 0;
208 }
209
210 static int
211 ausoc_get_props(void *priv)
212 {
213 struct ausoc_link * const link = priv;
214
215 return audio_dai_get_props(link->link_cpu);
216 }
217
218 static int
219 ausoc_round_blocksize(void *priv, int bs, int mode,
220 const audio_params_t *params)
221 {
222 struct ausoc_link * const link = priv;
223
224 return audio_dai_round_blocksize(link->link_cpu, bs, mode, params);
225 }
226
227 static size_t
228 ausoc_round_buffersize(void *priv, int dir, size_t bufsize)
229 {
230 struct ausoc_link * const link = priv;
231
232 return audio_dai_round_buffersize(link->link_cpu, dir, bufsize);
233 }
234
235 static int
236 ausoc_halt_output(void *priv)
237 {
238 struct ausoc_link * const link = priv;
239 u_int n;
240
241 for (n = 0; n < link->link_naux; n++)
242 audio_dai_halt(link->link_aux[n], AUMODE_PLAY);
243
244 audio_dai_halt(link->link_codec, AUMODE_PLAY);
245
246 return audio_dai_halt(link->link_cpu, AUMODE_PLAY);
247 }
248
249 static int
250 ausoc_halt_input(void *priv)
251 {
252 struct ausoc_link * const link = priv;
253 u_int n;
254
255 for (n = 0; n < link->link_naux; n++)
256 audio_dai_halt(link->link_aux[n], AUMODE_RECORD);
257
258 audio_dai_halt(link->link_codec, AUMODE_RECORD);
259
260 return audio_dai_halt(link->link_cpu, AUMODE_RECORD);
261 }
262
263 static int
264 ausoc_trigger_output(void *priv, void *start, void *end, int blksize,
265 void (*intr)(void *), void *intrarg, const audio_params_t *params)
266 {
267 struct ausoc_link * const link = priv;
268 u_int n, rate;
269 int error;
270
271 if (link->link_mclk_fs) {
272 rate = params->sample_rate * link->link_mclk_fs;
273 error = audio_dai_set_sysclk(link->link_codec, rate,
274 AUDIO_DAI_CLOCK_IN);
275 if (error)
276 goto failed;
277 error = audio_dai_set_sysclk(link->link_cpu, rate,
278 AUDIO_DAI_CLOCK_OUT);
279 if (error)
280 goto failed;
281 }
282
283 for (n = 0; n < link->link_naux; n++) {
284 error = audio_dai_trigger(link->link_aux[n], start, end,
285 blksize, intr, intrarg, params, AUMODE_PLAY);
286 if (error)
287 goto failed;
288 }
289 error = audio_dai_trigger(link->link_codec, start, end, blksize,
290 intr, intrarg, params, AUMODE_PLAY);
291 if (error)
292 goto failed;
293
294 return audio_dai_trigger(link->link_cpu, start, end, blksize,
295 intr, intrarg, params, AUMODE_PLAY);
296
297 failed:
298 ausoc_halt_output(priv);
299 return error;
300 }
301
302 static int
303 ausoc_trigger_input(void *priv, void *start, void *end, int blksize,
304 void (*intr)(void *), void *intrarg, const audio_params_t *params)
305 {
306 struct ausoc_link * const link = priv;
307 u_int n, rate;
308 int error;
309
310 if (link->link_mclk_fs) {
311 rate = params->sample_rate * link->link_mclk_fs;
312 error = audio_dai_set_sysclk(link->link_codec, rate,
313 AUDIO_DAI_CLOCK_IN);
314 if (error)
315 goto failed;
316 error = audio_dai_set_sysclk(link->link_cpu, rate,
317 AUDIO_DAI_CLOCK_OUT);
318 if (error)
319 goto failed;
320 }
321
322 for (n = 0; n < link->link_naux; n++) {
323 error = audio_dai_trigger(link->link_aux[n], start, end,
324 blksize, intr, intrarg, params, AUMODE_RECORD);
325 if (error)
326 goto failed;
327 }
328 error = audio_dai_trigger(link->link_codec, start, end, blksize,
329 intr, intrarg, params, AUMODE_RECORD);
330 if (error)
331 goto failed;
332
333 return audio_dai_trigger(link->link_cpu, start, end, blksize,
334 intr, intrarg, params, AUMODE_RECORD);
335
336 failed:
337 ausoc_halt_input(priv);
338 return error;
339 }
340
341 static void
342 ausoc_get_locks(void *priv, kmutex_t **intr, kmutex_t **thread)
343 {
344 struct ausoc_link * const link = priv;
345
346 return audio_dai_get_locks(link->link_cpu, intr, thread);
347 }
348
349 static const struct audio_hw_if ausoc_hw_if = {
350 .open = ausoc_open,
351 .close = ausoc_close,
352 .drain = ausoc_drain,
353 .query_encoding = ausoc_query_encoding,
354 .set_params = ausoc_set_params,
355 .allocm = ausoc_allocm,
356 .freem = ausoc_freem,
357 .mappage = ausoc_mappage,
358 .getdev = ausoc_getdev,
359 .set_port = ausoc_set_port,
360 .get_port = ausoc_get_port,
361 .query_devinfo = ausoc_query_devinfo,
362 .get_props = ausoc_get_props,
363 .round_blocksize = ausoc_round_blocksize,
364 .round_buffersize = ausoc_round_buffersize,
365 .trigger_output = ausoc_trigger_output,
366 .trigger_input = ausoc_trigger_input,
367 .halt_output = ausoc_halt_output,
368 .halt_input = ausoc_halt_input,
369 .get_locks = ausoc_get_locks,
370 };
371
372 static int
373 ausoc_match(device_t parent, cfdata_t cf, void *aux)
374 {
375 struct fdt_attach_args * const faa = aux;
376
377 return of_match_compatible(faa->faa_phandle, compatible);
378 }
379
380 static struct {
381 const char *name;
382 u_int fmt;
383 } ausoc_dai_formats[] = {
384 { "i2s", AUDIO_DAI_FORMAT_I2S },
385 { "right_j", AUDIO_DAI_FORMAT_RJ },
386 { "left_j", AUDIO_DAI_FORMAT_LJ },
387 { "dsp_a", AUDIO_DAI_FORMAT_DSPA },
388 { "dsp_b", AUDIO_DAI_FORMAT_DSPB },
389 { "ac97", AUDIO_DAI_FORMAT_AC97 },
390 { "pdm", AUDIO_DAI_FORMAT_PDM },
391 };
392
393 static int
394 ausoc_link_format(struct ausoc_softc *sc, struct ausoc_link *link, int phandle,
395 int dai_phandle, bool single_link, u_int *format)
396 {
397 const char *format_prop = single_link ?
398 "simple-audio-card,format" : "format";
399 const char *frame_master_prop = single_link ?
400 "simple-audio-card,frame-master" : "frame-master";
401 const char *bitclock_master_prop = single_link ?
402 "simple-audio-card,bitclock-master" : "bitclock-master";
403 const char *bitclock_inversion_prop = single_link ?
404 "simple-audio-card,bitclock-inversion" : "bitclock-inversion";
405 const char *frame_inversion_prop = single_link ?
406 "simple-audio-card,frame-inversion" : "frame-inversion";
407
408 u_int fmt, pol, clk;
409 const char *s;
410 u_int n;
411
412 s = fdtbus_get_string(phandle, format_prop);
413 if (s) {
414 for (n = 0; n < __arraycount(ausoc_dai_formats); n++) {
415 if (strcmp(s, ausoc_dai_formats[n].name) == 0) {
416 fmt = ausoc_dai_formats[n].fmt;
417 break;
418 }
419 }
420 if (n == __arraycount(ausoc_dai_formats))
421 return EINVAL;
422 } else {
423 fmt = AUDIO_DAI_FORMAT_I2S;
424 }
425
426 const bool frame_master =
427 dai_phandle == fdtbus_get_phandle(phandle, frame_master_prop);
428 const bool bitclock_master =
429 dai_phandle == fdtbus_get_phandle(phandle, bitclock_master_prop);
430 if (frame_master) {
431 clk = bitclock_master ?
432 AUDIO_DAI_CLOCK_CBM_CFM : AUDIO_DAI_CLOCK_CBS_CFM;
433 } else {
434 clk = bitclock_master ?
435 AUDIO_DAI_CLOCK_CBM_CFS : AUDIO_DAI_CLOCK_CBS_CFS;
436 }
437
438 const bool bitclock_inversion = of_hasprop(phandle, bitclock_inversion_prop);
439 const bool frame_inversion = of_hasprop(phandle, frame_inversion_prop);
440 if (bitclock_inversion) {
441 pol = frame_inversion ?
442 AUDIO_DAI_POLARITY_IB_IF : AUDIO_DAI_POLARITY_IB_NF;
443 } else {
444 pol = frame_inversion ?
445 AUDIO_DAI_POLARITY_NB_IF : AUDIO_DAI_POLARITY_NB_NF;
446 }
447
448 *format = __SHIFTIN(fmt, AUDIO_DAI_FORMAT_MASK) |
449 __SHIFTIN(pol, AUDIO_DAI_POLARITY_MASK) |
450 __SHIFTIN(clk, AUDIO_DAI_CLOCK_MASK);
451
452 return 0;
453 }
454
455 static void
456 ausoc_attach_link(struct ausoc_softc *sc, struct ausoc_link *link,
457 int card_phandle, int link_phandle)
458 {
459 const bool single_link = card_phandle == link_phandle;
460 const char *cpu_prop = single_link ?
461 "simple-audio-card,cpu" : "cpu";
462 const char *codec_prop = single_link ?
463 "simple-audio-card,codec" : "codec";
464 const char *mclk_fs_prop = single_link ?
465 "simple-audio-card,mclk-fs" : "mclk-fs";
466 const char *node_name = fdtbus_get_string(link_phandle, "name");
467 u_int n, format;
468
469 const int cpu_phandle = of_find_firstchild_byname(link_phandle, cpu_prop);
470 if (cpu_phandle <= 0) {
471 aprint_error_dev(sc->sc_dev, "missing %s prop on %s node\n",
472 cpu_prop, node_name);
473 return;
474 }
475
476 link->link_cpu = fdtbus_dai_acquire(cpu_phandle, "sound-dai");
477 if (!link->link_cpu) {
478 aprint_error_dev(sc->sc_dev,
479 "couldn't acquire cpu dai on %s node\n", node_name);
480 return;
481 }
482
483 const int codec_phandle = of_find_firstchild_byname(link_phandle, codec_prop);
484 if (codec_phandle <= 0) {
485 aprint_error_dev(sc->sc_dev, "missing %s prop on %s node\n",
486 codec_prop, node_name);
487 return;
488 }
489
490 link->link_codec = fdtbus_dai_acquire(codec_phandle, "sound-dai");
491 if (!link->link_codec) {
492 aprint_error_dev(sc->sc_dev,
493 "couldn't acquire codec dai on %s node\n", node_name);
494 return;
495 }
496
497 for (;;) {
498 if (fdtbus_dai_acquire_index(card_phandle,
499 "simple-audio-card,aux-devs", link->link_naux) == NULL)
500 break;
501 link->link_naux++;
502 }
503 if (link->link_naux) {
504 link->link_aux = kmem_zalloc(sizeof(audio_dai_tag_t) * link->link_naux, KM_SLEEP);
505 for (n = 0; n < link->link_naux; n++) {
506 link->link_aux[n] = fdtbus_dai_acquire_index(card_phandle,
507 "simple-audio-card,aux-devs", n);
508 KASSERT(link->link_aux[n] != NULL);
509
510 /* Attach aux devices to codec */
511 audio_dai_add_device(link->link_codec, link->link_aux[n]);
512 }
513 }
514
515 of_getprop_uint32(link_phandle, mclk_fs_prop, &link->link_mclk_fs);
516 if (ausoc_link_format(sc, link, link_phandle, codec_phandle, single_link, &format) != 0) {
517 aprint_error_dev(sc->sc_dev, "couldn't parse format properties\n");
518 return;
519 }
520 if (audio_dai_set_format(link->link_cpu, format) != 0) {
521 aprint_error_dev(sc->sc_dev, "couldn't set cpu format\n");
522 return;
523 }
524 if (audio_dai_set_format(link->link_codec, format) != 0) {
525 aprint_error_dev(sc->sc_dev, "couldn't set codec format\n");
526 return;
527 }
528
529 aprint_normal_dev(sc->sc_dev, "codec: %s, cpu: %s",
530 device_xname(audio_dai_device(link->link_codec)),
531 device_xname(audio_dai_device(link->link_cpu)));
532 for (n = 0; n < link->link_naux; n++) {
533 if (n == 0)
534 aprint_normal(", aux:");
535 aprint_normal(" %s",
536 device_xname(audio_dai_device(link->link_aux[n])));
537 }
538 aprint_normal("\n");
539
540 audio_attach_mi(&ausoc_hw_if, link, sc->sc_dev);
541 }
542
543 static void
544 ausoc_attach_cb(device_t self)
545 {
546 struct ausoc_softc * const sc = device_private(self);
547 const int phandle = sc->sc_phandle;
548 const char *name;
549 int child, n;
550 size_t len;
551
552 /*
553 * If the root node defines a cpu and codec, there is only one link. For
554 * cards with multiple links, there will be simple-audio-card,dai-link
555 * child nodes for each one.
556 */
557 if (of_find_firstchild_byname(phandle, "simple-audio-card,cpu") > 0 &&
558 of_find_firstchild_byname(phandle, "simple-audio-card,codec") > 0) {
559 sc->sc_nlink = 1;
560 sc->sc_link = kmem_zalloc(sizeof(*sc->sc_link), KM_SLEEP);
561 sc->sc_link[0].link_name = sc->sc_name;
562 ausoc_attach_link(sc, &sc->sc_link[0], phandle, phandle);
563 } else {
564 for (child = OF_child(phandle); child; child = OF_peer(child)) {
565 name = fdtbus_get_string(child, "name");
566 len = strlen("simple-audio-card,dai-link");
567 if (strncmp(name, "simple-audio-card,dai-link", len) != 0)
568 continue;
569 sc->sc_nlink++;
570 }
571 if (sc->sc_nlink == 0)
572 return;
573 sc->sc_link = kmem_zalloc(sizeof(*sc->sc_link) * sc->sc_nlink,
574 KM_SLEEP);
575 for (child = OF_child(phandle), n = 0; child; child = OF_peer(child)) {
576 name = fdtbus_get_string(child, "name");
577 len = strlen("simple-audio-card,dai-link");
578 if (strncmp(name, "simple-audio-card,dai-link", len) != 0)
579 continue;
580 sc->sc_link[n].link_name = sc->sc_name;
581 ausoc_attach_link(sc, &sc->sc_link[n], phandle, child);
582 n++;
583 }
584 }
585 }
586
587 static void
588 ausoc_attach(device_t parent, device_t self, void *aux)
589 {
590 struct ausoc_softc * const sc = device_private(self);
591 struct fdt_attach_args * const faa = aux;
592 const int phandle = faa->faa_phandle;
593
594 sc->sc_dev = self;
595 sc->sc_phandle = phandle;
596 sc->sc_name = fdtbus_get_string(phandle, "simple-audio-card,name");
597 if (!sc->sc_name)
598 sc->sc_name = "SoC Audio";
599
600 aprint_naive("\n");
601 aprint_normal(": %s\n", sc->sc_name);
602
603 /*
604 * Defer attachment until all other drivers are ready.
605 */
606 config_defer(self, ausoc_attach_cb);
607 }
608
609 CFATTACH_DECL_NEW(ausoc, sizeof(struct ausoc_softc),
610 ausoc_match, ausoc_attach, NULL, NULL);
611