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