fdt_dai.c revision 1.1 1 /* $NetBSD: fdt_dai.c,v 1.1 2018/05/09 23:59:05 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: fdt_dai.c,v 1.1 2018/05/09 23:59:05 jmcneill Exp $");
31
32 #include <sys/param.h>
33 #include <sys/bus.h>
34 #include <sys/kmem.h>
35
36 #include <libfdt.h>
37 #include <dev/fdt/fdtvar.h>
38
39 #include <dev/audio_dai.h>
40
41 struct fdtbus_dai_controller {
42 device_t dc_dev;
43 int dc_phandle;
44 const struct fdtbus_dai_controller_func *dc_funcs;
45
46 struct fdtbus_dai_controller *dc_next;
47 };
48
49 static struct fdtbus_dai_controller *fdtbus_dc = NULL;
50
51 int
52 fdtbus_register_dai_controller(device_t dev, int phandle,
53 const struct fdtbus_dai_controller_func *funcs)
54 {
55 struct fdtbus_dai_controller *dc;
56
57 dc = kmem_alloc(sizeof(*dc), KM_SLEEP);
58 dc->dc_dev = dev;
59 dc->dc_phandle = phandle;
60 dc->dc_funcs = funcs;
61
62 dc->dc_next = fdtbus_dc;
63 fdtbus_dc = dc;
64
65 return 0;
66 }
67
68 static struct fdtbus_dai_controller *
69 fdtbus_get_dai_controller(int phandle)
70 {
71 struct fdtbus_dai_controller *dc;
72
73 for (dc = fdtbus_dc; dc; dc = dc->dc_next) {
74 if (dc->dc_phandle == phandle) {
75 return dc;
76 }
77 }
78
79 return NULL;
80 }
81
82 audio_dai_tag_t
83 fdtbus_dai_acquire(int phandle, const char *prop)
84 {
85 return fdtbus_dai_acquire_index(phandle, prop, 0);
86 }
87
88 audio_dai_tag_t
89 fdtbus_dai_acquire_index(int phandle, const char *prop, int index)
90 {
91 struct fdtbus_dai_controller *dc;
92 const uint32_t *dais, *p;
93 u_int n, dai_cells;
94 int len, resid;
95
96 dais = fdtbus_get_prop(phandle, prop, &len);
97 if (dais == NULL)
98 return NULL;
99
100 p = dais;
101 for (n = 0, resid = len; resid > 0; n++) {
102 const int dc_phandle =
103 fdtbus_get_phandle_from_native(be32toh(p[0]));
104 if (of_getprop_uint32(dc_phandle, "#sound-dai-cells", &dai_cells))
105 dai_cells = 0;
106 if (n == index) {
107 dc = fdtbus_get_dai_controller(dc_phandle);
108 if (dc == NULL)
109 return NULL;
110 return dc->dc_funcs->get_tag(dc->dc_dev,
111 &p[0], (dai_cells + 1) * 4);
112 }
113 resid -= (dai_cells + 1) * 4;
114 p += dai_cells + 1;
115 }
116
117 return NULL;
118 }
119