1 /* $NetBSD: zaudio.c,v 1.23 2019/06/08 08:02:37 isaki 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.23 2019/06/08 08:02:37 isaki 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/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 const struct audio_format zaudio_formats[] = {
86 {
87 .mode = AUMODE_PLAY | AUMODE_RECORD,
88 .encoding = AUDIO_ENCODING_SLINEAR_LE,
89 .validbits = 16,
90 .precision = 16,
91 .channels = 2,
92 .channel_mask = AUFMT_STEREO,
93 .frequency_type = 6,
94 .frequency = { 8000, 11025, 16000, 22050, 44100, 48000 },
95 }
96 };
97 #define ZAUDIO_NFORMATS __arraycount(zaudio_formats)
98
99 static int
zaudio_match(device_t parent,cfdata_t cf,void * aux)100 zaudio_match(device_t parent, cfdata_t cf, void *aux)
101 {
102 struct i2c_attach_args *ia = aux;
103
104 if (ZAURUS_ISC1000 || ZAURUS_ISC3000) {
105 #if defined(CPU_XSCALE_PXA270)
106 return wm8750_match(parent, cf, ia);
107 #endif
108 } else if (ZAURUS_ISC860) {
109 #if defined(CPU_XSCALE_PXA250)
110 return wm8731_match(parent, cf, ia);
111 #endif
112 }
113 return 0;
114 }
115
116 static void
zaudio_attach(device_t parent,device_t self,void * aux)117 zaudio_attach(device_t parent, device_t self, void *aux)
118 {
119 struct zaudio_softc *sc = device_private(self);
120 struct i2c_attach_args *ia = aux;
121
122 sc->sc_dev = self;
123 sc->sc_i2c = ia->ia_tag;
124
125 mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE);
126 mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_SCHED);
127 callout_init(&sc->sc_to, 0);
128
129 sc->sc_i2s.sc_iot = &pxa2x0_bs_tag;
130 sc->sc_i2s.sc_dmat = &pxa2x0_bus_dma_tag;
131 sc->sc_i2s.sc_size = PXA2X0_I2S_SIZE;
132 sc->sc_i2s.sc_intr_lock = &sc->sc_intr_lock;
133 if (pxa2x0_i2s_attach_sub(&sc->sc_i2s)) {
134 aprint_error_dev(self, "unable to attach I2S\n");
135 return;
136 }
137
138 if (ZAURUS_ISC1000 || ZAURUS_ISC3000) {
139 #if defined(CPU_XSCALE_PXA270)
140 wm8750_attach(parent, self, ia);
141 #endif
142 } else if (ZAURUS_ISC860) {
143 #if defined(CPU_XSCALE_PXA250)
144 wm8731_attach(parent, self, ia);
145 #endif
146 }
147
148 return;
149 }
150
151 /*
152 * audio operation functions.
153 */
154 int
zaudio_open(void * hdl,int flags)155 zaudio_open(void *hdl, int flags)
156 {
157 struct zaudio_softc *sc = hdl;
158
159 /* Power on the I2S bus and codec */
160 pxa2x0_i2s_open(&sc->sc_i2s);
161
162 return 0;
163 }
164
165 void
zaudio_close(void * hdl)166 zaudio_close(void *hdl)
167 {
168 struct zaudio_softc *sc = hdl;
169
170 /* Power off the I2S bus and codec */
171 pxa2x0_i2s_close(&sc->sc_i2s);
172 }
173
174 int
zaudio_query_format(void * hdl,audio_format_query_t * afp)175 zaudio_query_format(void *hdl, audio_format_query_t *afp)
176 {
177
178 return audio_query_format(zaudio_formats, ZAUDIO_NFORMATS, afp);
179 }
180
181 int
zaudio_set_format(void * hdl,int setmode,const audio_params_t * play,const audio_params_t * rec,audio_filter_reg_t * pfil,audio_filter_reg_t * rfil)182 zaudio_set_format(void *hdl, int setmode,
183 const audio_params_t *play, const audio_params_t *rec,
184 audio_filter_reg_t *pfil, audio_filter_reg_t *rfil)
185 {
186 struct zaudio_softc *sc = hdl;
187
188 /* *play and *rec are the identical because !AUDIO_PROP_INDEPENDENT. */
189
190 if (setmode == AUMODE_RECORD)
191 pxa2x0_i2s_setspeed(&sc->sc_i2s, rec->sample_rate);
192 else
193 pxa2x0_i2s_setspeed(&sc->sc_i2s, play->sample_rate);
194
195 return 0;
196 }
197
198 int
zaudio_round_blocksize(void * hdl,int bs,int mode,const audio_params_t * param)199 zaudio_round_blocksize(void *hdl, int bs, int mode, const audio_params_t *param)
200 {
201 struct zaudio_softc *sc = hdl;
202
203 return pxa2x0_i2s_round_blocksize(&sc->sc_i2s, bs, mode, param);
204 }
205
206 void *
zaudio_allocm(void * hdl,int direction,size_t size)207 zaudio_allocm(void *hdl, int direction, size_t size)
208 {
209 struct zaudio_softc *sc = hdl;
210
211 return pxa2x0_i2s_allocm(&sc->sc_i2s, direction, size);
212 }
213
214 void
zaudio_freem(void * hdl,void * ptr,size_t size)215 zaudio_freem(void *hdl, void *ptr, size_t size)
216 {
217 struct zaudio_softc *sc = hdl;
218
219 return pxa2x0_i2s_freem(&sc->sc_i2s, ptr, size);
220 }
221
222 size_t
zaudio_round_buffersize(void * hdl,int direction,size_t bufsize)223 zaudio_round_buffersize(void *hdl, int direction, size_t bufsize)
224 {
225 struct zaudio_softc *sc = hdl;
226
227 return pxa2x0_i2s_round_buffersize(&sc->sc_i2s, direction, bufsize);
228 }
229
230 int
zaudio_get_props(void * hdl)231 zaudio_get_props(void *hdl)
232 {
233
234 return AUDIO_PROP_PLAYBACK | AUDIO_PROP_CAPTURE;
235 }
236
237 void
zaudio_get_locks(void * hdl,kmutex_t ** intr,kmutex_t ** thread)238 zaudio_get_locks(void *hdl, kmutex_t **intr, kmutex_t **thread)
239 {
240 struct zaudio_softc *sc = hdl;
241
242 *intr = &sc->sc_intr_lock;
243 *thread = &sc->sc_lock;
244 }
245