1*233c5d85Sjmcneill /* $NetBSD: fdt_dai.c,v 1.3 2018/06/30 20:34:43 jmcneill Exp $ */
2a0c36fc0Sjmcneill
3a0c36fc0Sjmcneill /*-
4a0c36fc0Sjmcneill * Copyright (c) 2018 Jared McNeill <jmcneill@invisible.ca>
5a0c36fc0Sjmcneill * All rights reserved.
6a0c36fc0Sjmcneill *
7a0c36fc0Sjmcneill * Redistribution and use in source and binary forms, with or without
8a0c36fc0Sjmcneill * modification, are permitted provided that the following conditions
9a0c36fc0Sjmcneill * are met:
10a0c36fc0Sjmcneill * 1. Redistributions of source code must retain the above copyright
11a0c36fc0Sjmcneill * notice, this list of conditions and the following disclaimer.
12a0c36fc0Sjmcneill * 2. Redistributions in binary form must reproduce the above copyright
13a0c36fc0Sjmcneill * notice, this list of conditions and the following disclaimer in the
14a0c36fc0Sjmcneill * documentation and/or other materials provided with the distribution.
15a0c36fc0Sjmcneill *
16a0c36fc0Sjmcneill * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17a0c36fc0Sjmcneill * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18a0c36fc0Sjmcneill * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19a0c36fc0Sjmcneill * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20a0c36fc0Sjmcneill * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21a0c36fc0Sjmcneill * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22a0c36fc0Sjmcneill * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23a0c36fc0Sjmcneill * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24a0c36fc0Sjmcneill * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25a0c36fc0Sjmcneill * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26a0c36fc0Sjmcneill * SUCH DAMAGE.
27a0c36fc0Sjmcneill */
28a0c36fc0Sjmcneill
29a0c36fc0Sjmcneill #include <sys/cdefs.h>
30*233c5d85Sjmcneill __KERNEL_RCSID(0, "$NetBSD: fdt_dai.c,v 1.3 2018/06/30 20:34:43 jmcneill Exp $");
31a0c36fc0Sjmcneill
32a0c36fc0Sjmcneill #include <sys/param.h>
33a0c36fc0Sjmcneill #include <sys/bus.h>
34a0c36fc0Sjmcneill #include <sys/kmem.h>
35*233c5d85Sjmcneill #include <sys/queue.h>
36a0c36fc0Sjmcneill
37a0c36fc0Sjmcneill #include <libfdt.h>
38a0c36fc0Sjmcneill #include <dev/fdt/fdtvar.h>
39a0c36fc0Sjmcneill
40a0c36fc0Sjmcneill struct fdtbus_dai_controller {
41a0c36fc0Sjmcneill device_t dc_dev;
42a0c36fc0Sjmcneill int dc_phandle;
43a0c36fc0Sjmcneill const struct fdtbus_dai_controller_func *dc_funcs;
44a0c36fc0Sjmcneill
45*233c5d85Sjmcneill LIST_ENTRY(fdtbus_dai_controller) dc_next;
46a0c36fc0Sjmcneill };
47a0c36fc0Sjmcneill
48*233c5d85Sjmcneill static LIST_HEAD(, fdtbus_dai_controller) fdtbus_dai_controllers =
49*233c5d85Sjmcneill LIST_HEAD_INITIALIZER(fdtbus_dai_controllers);
50a0c36fc0Sjmcneill
51a0c36fc0Sjmcneill int
fdtbus_register_dai_controller(device_t dev,int phandle,const struct fdtbus_dai_controller_func * funcs)52a0c36fc0Sjmcneill fdtbus_register_dai_controller(device_t dev, int phandle,
53a0c36fc0Sjmcneill const struct fdtbus_dai_controller_func *funcs)
54a0c36fc0Sjmcneill {
55a0c36fc0Sjmcneill struct fdtbus_dai_controller *dc;
56a0c36fc0Sjmcneill
57a0c36fc0Sjmcneill dc = kmem_alloc(sizeof(*dc), KM_SLEEP);
58a0c36fc0Sjmcneill dc->dc_dev = dev;
59a0c36fc0Sjmcneill dc->dc_phandle = phandle;
60a0c36fc0Sjmcneill dc->dc_funcs = funcs;
61a0c36fc0Sjmcneill
62*233c5d85Sjmcneill LIST_INSERT_HEAD(&fdtbus_dai_controllers, dc, dc_next);
63a0c36fc0Sjmcneill
64a0c36fc0Sjmcneill return 0;
65a0c36fc0Sjmcneill }
66a0c36fc0Sjmcneill
67a0c36fc0Sjmcneill static struct fdtbus_dai_controller *
fdtbus_get_dai_controller(int phandle)68a0c36fc0Sjmcneill fdtbus_get_dai_controller(int phandle)
69a0c36fc0Sjmcneill {
70a0c36fc0Sjmcneill struct fdtbus_dai_controller *dc;
71a0c36fc0Sjmcneill
72*233c5d85Sjmcneill LIST_FOREACH(dc, &fdtbus_dai_controllers, dc_next) {
73*233c5d85Sjmcneill if (dc->dc_phandle == phandle)
74a0c36fc0Sjmcneill return dc;
75a0c36fc0Sjmcneill }
76a0c36fc0Sjmcneill
77a0c36fc0Sjmcneill return NULL;
78a0c36fc0Sjmcneill }
79a0c36fc0Sjmcneill
80a0c36fc0Sjmcneill audio_dai_tag_t
fdtbus_dai_acquire(int phandle,const char * prop)81a0c36fc0Sjmcneill fdtbus_dai_acquire(int phandle, const char *prop)
82a0c36fc0Sjmcneill {
83a0c36fc0Sjmcneill return fdtbus_dai_acquire_index(phandle, prop, 0);
84a0c36fc0Sjmcneill }
85a0c36fc0Sjmcneill
86a0c36fc0Sjmcneill audio_dai_tag_t
fdtbus_dai_acquire_index(int phandle,const char * prop,int index)87a0c36fc0Sjmcneill fdtbus_dai_acquire_index(int phandle, const char *prop, int index)
88a0c36fc0Sjmcneill {
89a0c36fc0Sjmcneill struct fdtbus_dai_controller *dc;
90a0c36fc0Sjmcneill const uint32_t *dais, *p;
91a0c36fc0Sjmcneill u_int n, dai_cells;
92a0c36fc0Sjmcneill int len, resid;
93a0c36fc0Sjmcneill
94a0c36fc0Sjmcneill dais = fdtbus_get_prop(phandle, prop, &len);
95a0c36fc0Sjmcneill if (dais == NULL)
96a0c36fc0Sjmcneill return NULL;
97a0c36fc0Sjmcneill
98a0c36fc0Sjmcneill p = dais;
99a0c36fc0Sjmcneill for (n = 0, resid = len; resid > 0; n++) {
100a0c36fc0Sjmcneill const int dc_phandle =
101a0c36fc0Sjmcneill fdtbus_get_phandle_from_native(be32toh(p[0]));
102a0c36fc0Sjmcneill if (of_getprop_uint32(dc_phandle, "#sound-dai-cells", &dai_cells))
103a0c36fc0Sjmcneill dai_cells = 0;
104a0c36fc0Sjmcneill if (n == index) {
105a0c36fc0Sjmcneill dc = fdtbus_get_dai_controller(dc_phandle);
106a0c36fc0Sjmcneill if (dc == NULL)
107a0c36fc0Sjmcneill return NULL;
108a0c36fc0Sjmcneill return dc->dc_funcs->get_tag(dc->dc_dev,
109a0c36fc0Sjmcneill &p[0], (dai_cells + 1) * 4);
110a0c36fc0Sjmcneill }
111a0c36fc0Sjmcneill resid -= (dai_cells + 1) * 4;
112a0c36fc0Sjmcneill p += dai_cells + 1;
113a0c36fc0Sjmcneill }
114a0c36fc0Sjmcneill
115a0c36fc0Sjmcneill return NULL;
116a0c36fc0Sjmcneill }
117