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