Home | History | Annotate | Line # | Download | only in dev
      1  1.23     isaki /*	$NetBSD: zaudio.c,v 1.23 2019/06/08 08:02:37 isaki Exp $	*/
      2   1.1      ober /*	$OpenBSD: zaurus_audio.c,v 1.8 2005/08/18 13:23:02 robert Exp $	*/
      3   1.1      ober 
      4   1.1      ober /*
      5   1.1      ober  * Copyright (c) 2005 Christopher Pascoe <pascoe (at) openbsd.org>
      6   1.1      ober  *
      7   1.1      ober  * Permission to use, copy, modify, and distribute this software for any
      8   1.1      ober  * purpose with or without fee is hereby granted, provided that the above
      9   1.1      ober  * copyright notice and this permission notice appear in all copies.
     10   1.1      ober  *
     11   1.1      ober  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
     12   1.1      ober  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     13   1.1      ober  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
     14   1.1      ober  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     15   1.1      ober  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
     16   1.1      ober  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
     17   1.1      ober  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     18   1.1      ober  */
     19   1.1      ober 
     20  1.10    nonaka /*-
     21  1.17    nonaka  * Copyright (C) 2009 NONAKA Kimihiro <nonaka (at) netbsd.org>
     22  1.10    nonaka  * All rights reserved.
     23  1.10    nonaka  *
     24  1.10    nonaka  * Redistribution and use in source and binary forms, with or without
     25  1.10    nonaka  * modification, are permitted provided that the following conditions
     26  1.10    nonaka  * are met:
     27  1.10    nonaka  * 1. Redistributions of source code must retain the above copyright
     28  1.10    nonaka  *    notice, this list of conditions and the following disclaimer.
     29  1.10    nonaka  * 2. Redistributions in binary form must reproduce the above copyright
     30  1.10    nonaka  *    notice, this list of conditions and the following disclaimer in the
     31  1.10    nonaka  *    documentation and/or other materials provided with the distribution.
     32  1.10    nonaka  *
     33  1.17    nonaka  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     34  1.17    nonaka  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     35  1.17    nonaka  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     36  1.17    nonaka  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     37  1.17    nonaka  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     38  1.17    nonaka  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     39  1.17    nonaka  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     40  1.17    nonaka  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     41  1.17    nonaka  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     42  1.17    nonaka  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     43  1.10    nonaka  */
     44  1.10    nonaka 
     45   1.1      ober /*
     46   1.1      ober  * TODO:
     47   1.1      ober  *	- powerhooks (currently only works until first suspend)
     48   1.1      ober  */
     49   1.1      ober 
     50  1.21    nonaka #include "opt_cputypes.h"
     51  1.13    nonaka 
     52   1.1      ober #include <sys/cdefs.h>
     53  1.23     isaki __KERNEL_RCSID(0, "$NetBSD: zaudio.c,v 1.23 2019/06/08 08:02:37 isaki Exp $");
     54   1.1      ober 
     55   1.1      ober #include <sys/param.h>
     56   1.1      ober #include <sys/systm.h>
     57  1.21    nonaka #include <sys/audioio.h>
     58   1.1      ober #include <sys/callout.h>
     59   1.1      ober #include <sys/device.h>
     60  1.13    nonaka #include <sys/mutex.h>
     61  1.13    nonaka 
     62  1.22     isaki #include <dev/audio/audio_if.h>
     63   1.1      ober 
     64  1.13    nonaka #include <dev/i2c/i2cvar.h>
     65   1.1      ober 
     66   1.1      ober #include <arm/xscale/pxa2x0reg.h>
     67   1.1      ober #include <arm/xscale/pxa2x0var.h>
     68   1.1      ober #include <arm/xscale/pxa2x0_i2s.h>
     69   1.1      ober 
     70  1.13    nonaka #include <zaurus/zaurus/zaurus_var.h>
     71  1.21    nonaka #include <zaurus/dev/zaudiovar.h>
     72  1.21    nonaka #if defined(CPU_XSCALE_PXA270)
     73  1.21    nonaka #include <zaurus/dev/wm8750var.h>
     74  1.21    nonaka #endif
     75  1.21    nonaka #if defined(CPU_XSCALE_PXA250)
     76  1.21    nonaka #include <zaurus/dev/wm8731var.h>
     77  1.21    nonaka #endif
     78  1.13    nonaka 
     79  1.13    nonaka static int	zaudio_match(device_t, cfdata_t, void *);
     80  1.13    nonaka static void	zaudio_attach(device_t, device_t, void *);
     81  1.13    nonaka 
     82   1.6    nonaka CFATTACH_DECL_NEW(zaudio, sizeof(struct zaudio_softc),
     83   1.7    nonaka     zaudio_match, zaudio_attach, NULL, NULL);
     84   1.1      ober 
     85  1.22     isaki static const struct audio_format zaudio_formats[] = {
     86  1.22     isaki 	{
     87  1.22     isaki 		.mode		= AUMODE_PLAY | AUMODE_RECORD,
     88  1.22     isaki 		.encoding	= AUDIO_ENCODING_SLINEAR_LE,
     89  1.22     isaki 		.validbits	= 16,
     90  1.22     isaki 		.precision	= 16,
     91  1.22     isaki 		.channels	= 2,
     92  1.22     isaki 		.channel_mask	= AUFMT_STEREO,
     93  1.22     isaki 		.frequency_type	= 6,
     94  1.22     isaki 		.frequency	= { 8000, 11025, 16000, 22050, 44100, 48000 },
     95  1.22     isaki 	}
     96  1.22     isaki };
     97  1.22     isaki #define ZAUDIO_NFORMATS	__arraycount(zaudio_formats)
     98  1.22     isaki 
     99   1.1      ober static int
    100   1.6    nonaka zaudio_match(device_t parent, cfdata_t cf, void *aux)
    101   1.1      ober {
    102  1.13    nonaka 	struct i2c_attach_args *ia = aux;
    103   1.1      ober 
    104  1.21    nonaka 	if (ZAURUS_ISC1000 || ZAURUS_ISC3000) {
    105  1.21    nonaka #if defined(CPU_XSCALE_PXA270)
    106  1.21    nonaka 		return wm8750_match(parent, cf, ia);
    107  1.21    nonaka #endif
    108  1.21    nonaka 	} else if (ZAURUS_ISC860) {
    109  1.21    nonaka #if defined(CPU_XSCALE_PXA250)
    110  1.21    nonaka 		return wm8731_match(parent, cf, ia);
    111  1.21    nonaka #endif
    112  1.13    nonaka 	}
    113  1.13    nonaka 	return 0;
    114   1.1      ober }
    115   1.1      ober 
    116   1.1      ober static void
    117   1.6    nonaka zaudio_attach(device_t parent, device_t self, void *aux)
    118   1.1      ober {
    119   1.6    nonaka 	struct zaudio_softc *sc = device_private(self);
    120  1.13    nonaka 	struct i2c_attach_args *ia = aux;
    121   1.1      ober 
    122  1.21    nonaka 	sc->sc_dev = self;
    123  1.13    nonaka 	sc->sc_i2c = ia->ia_tag;
    124  1.21    nonaka 
    125  1.16  jmcneill 	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE);
    126  1.16  jmcneill 	mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_SCHED);
    127  1.21    nonaka 	callout_init(&sc->sc_to, 0);
    128   1.6    nonaka 
    129  1.13    nonaka 	sc->sc_i2s.sc_iot = &pxa2x0_bs_tag;
    130  1.13    nonaka 	sc->sc_i2s.sc_dmat = &pxa2x0_bus_dma_tag;
    131   1.1      ober 	sc->sc_i2s.sc_size = PXA2X0_I2S_SIZE;
    132  1.16  jmcneill 	sc->sc_i2s.sc_intr_lock = &sc->sc_intr_lock;
    133   1.1      ober 	if (pxa2x0_i2s_attach_sub(&sc->sc_i2s)) {
    134  1.13    nonaka 		aprint_error_dev(self, "unable to attach I2S\n");
    135  1.21    nonaka 		return;
    136   1.1      ober 	}
    137   1.1      ober 
    138  1.21    nonaka 	if (ZAURUS_ISC1000 || ZAURUS_ISC3000) {
    139  1.21    nonaka #if defined(CPU_XSCALE_PXA270)
    140  1.21    nonaka 		wm8750_attach(parent, self, ia);
    141  1.21    nonaka #endif
    142  1.21    nonaka 	} else if (ZAURUS_ISC860) {
    143  1.21    nonaka #if defined(CPU_XSCALE_PXA250)
    144  1.21    nonaka 		wm8731_attach(parent, self, ia);
    145  1.13    nonaka #endif
    146  1.13    nonaka 	}
    147   1.2     peter 
    148   1.1      ober 	return;
    149   1.1      ober }
    150   1.1      ober 
    151   1.2     peter /*
    152   1.2     peter  * audio operation functions.
    153   1.2     peter  */
    154  1.21    nonaka int
    155   1.1      ober zaudio_open(void *hdl, int flags)
    156   1.1      ober {
    157   1.1      ober 	struct zaudio_softc *sc = hdl;
    158   1.1      ober 
    159   1.1      ober 	/* Power on the I2S bus and codec */
    160   1.1      ober 	pxa2x0_i2s_open(&sc->sc_i2s);
    161   1.1      ober 
    162   1.1      ober 	return 0;
    163   1.1      ober }
    164   1.1      ober 
    165  1.21    nonaka void
    166   1.1      ober zaudio_close(void *hdl)
    167   1.1      ober {
    168   1.1      ober 	struct zaudio_softc *sc = hdl;
    169   1.1      ober 
    170   1.1      ober 	/* Power off the I2S bus and codec */
    171   1.1      ober 	pxa2x0_i2s_close(&sc->sc_i2s);
    172   1.1      ober }
    173   1.1      ober 
    174  1.21    nonaka int
    175  1.22     isaki zaudio_query_format(void *hdl, audio_format_query_t *afp)
    176  1.22     isaki {
    177  1.22     isaki 
    178  1.22     isaki 	return audio_query_format(zaudio_formats, ZAUDIO_NFORMATS, afp);
    179  1.22     isaki }
    180  1.22     isaki 
    181  1.22     isaki int
    182  1.22     isaki zaudio_set_format(void *hdl, int setmode,
    183  1.22     isaki     const audio_params_t *play, const audio_params_t *rec,
    184  1.22     isaki     audio_filter_reg_t *pfil, audio_filter_reg_t *rfil)
    185  1.22     isaki {
    186  1.22     isaki 	struct zaudio_softc *sc = hdl;
    187  1.22     isaki 
    188  1.22     isaki 	/* *play and *rec are the identical because !AUDIO_PROP_INDEPENDENT. */
    189  1.22     isaki 
    190  1.22     isaki 	if (setmode == AUMODE_RECORD)
    191  1.22     isaki 		pxa2x0_i2s_setspeed(&sc->sc_i2s, rec->sample_rate);
    192  1.22     isaki 	else
    193  1.22     isaki 		pxa2x0_i2s_setspeed(&sc->sc_i2s, play->sample_rate);
    194  1.22     isaki 
    195  1.22     isaki 	return 0;
    196  1.22     isaki }
    197  1.22     isaki 
    198  1.22     isaki int
    199   1.2     peter zaudio_round_blocksize(void *hdl, int bs, int mode, const audio_params_t *param)
    200   1.2     peter {
    201   1.2     peter 	struct zaudio_softc *sc = hdl;
    202   1.1      ober 
    203   1.2     peter 	return pxa2x0_i2s_round_blocksize(&sc->sc_i2s, bs, mode, param);
    204   1.1      ober }
    205   1.1      ober 
    206  1.21    nonaka void *
    207  1.16  jmcneill zaudio_allocm(void *hdl, int direction, size_t size)
    208   1.2     peter {
    209   1.2     peter 	struct zaudio_softc *sc = hdl;
    210   1.2     peter 
    211  1.16  jmcneill 	return pxa2x0_i2s_allocm(&sc->sc_i2s, direction, size);
    212   1.2     peter }
    213   1.2     peter 
    214  1.21    nonaka void
    215  1.16  jmcneill zaudio_freem(void *hdl, void *ptr, size_t size)
    216   1.2     peter {
    217   1.2     peter 	struct zaudio_softc *sc = hdl;
    218   1.2     peter 
    219  1.16  jmcneill 	return pxa2x0_i2s_freem(&sc->sc_i2s, ptr, size);
    220   1.2     peter }
    221   1.2     peter 
    222  1.21    nonaka size_t
    223   1.2     peter zaudio_round_buffersize(void *hdl, int direction, size_t bufsize)
    224   1.2     peter {
    225   1.2     peter 	struct zaudio_softc *sc = hdl;
    226   1.2     peter 
    227   1.2     peter 	return pxa2x0_i2s_round_buffersize(&sc->sc_i2s, direction, bufsize);
    228   1.2     peter }
    229   1.2     peter 
    230  1.21    nonaka int
    231   1.1      ober zaudio_get_props(void *hdl)
    232   1.1      ober {
    233   1.2     peter 
    234  1.23     isaki 	return AUDIO_PROP_PLAYBACK | AUDIO_PROP_CAPTURE;
    235   1.1      ober }
    236   1.1      ober 
    237  1.21    nonaka void
    238  1.16  jmcneill zaudio_get_locks(void *hdl, kmutex_t **intr, kmutex_t **thread)
    239  1.16  jmcneill {
    240  1.16  jmcneill 	struct zaudio_softc *sc = hdl;
    241  1.16  jmcneill 
    242  1.16  jmcneill 	*intr = &sc->sc_intr_lock;
    243  1.16  jmcneill 	*thread = &sc->sc_lock;
    244  1.16  jmcneill }
    245