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