1 /* $NetBSD: uda1341.c,v 1.13 2009/03/14 21:04:09 dsl Exp $ */
4 * Copyright (c) 2001 The NetBSD Foundation, Inc. All rights reserved.
6 * This code is derived from software contributed to The NetBSD Foundation
7 * by Ichiro FUKUHARA (ichiro@ichiro.org).
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
31 #include <sys/cdefs.h>
32 __KERNEL_RCSID(0, "$NetBSD: uda1341.c,v 1.13 2009/03/14 21:04:09 dsl Exp $");
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/types.h>
39 #include <sys/device.h>
40 #include <sys/kernel.h>
41 #include <sys/kthread.h>
42 #include <sys/malloc.h>
44 #include <machine/bus.h>
46 #include <hpcarm/dev/ipaq_saipvar.h>
47 #include <hpcarm/dev/ipaq_gpioreg.h>
48 #include <hpcarm/dev/uda1341.h>
50 #include <arm/sa11x0/sa11x0_gpioreg.h>
51 #include <arm/sa11x0/sa11x0_sspreg.h>
53 struct uda1341_softc
{
55 bus_space_tag_t sc_iot
;
56 bus_space_handle_t sc_ioh
;
57 struct ipaq_softc
*sc_parent
;
60 static int uda1341_match(device_t
, cfdata_t
, void *);
61 static void uda1341_attach(device_t
, device_t
, void *);
62 static int uda1341_print(void *, const char *);
63 static int uda1341_search(device_t
, cfdata_t
, const int *, void *);
65 static void uda1341_output_high(struct uda1341_softc
*);
66 static void uda1341_output_low(struct uda1341_softc
*);
67 static void uda1341_L3_init(struct uda1341_softc
*);
68 static void uda1341_init(struct uda1341_softc
*);
69 static void uda1341_reset(struct uda1341_softc
*);
70 static void uda1341_reginit(struct uda1341_softc
*);
73 static int L3_getbit(struct uda1341_softc
*);
75 static void L3_sendbit(struct uda1341_softc
*, int);
77 static uint8_t L3_getbyte(struct uda1341_softc
*, int);
79 static void L3_sendbyte(struct uda1341_softc
*, uint8_t, int);
81 static int L3_read(struct uda1341_softc
*, uint8_t, uint8_t *, int);
83 static int L3_write(struct uda1341_softc
*, uint8_t, uint8_t *, int);
85 CFATTACH_DECL_NEW(uda
, sizeof(struct uda1341_softc
),
86 uda1341_match
, uda1341_attach
, NULL
, NULL
);
89 * Philips L3 bus support.
90 * GPIO lines are used for clock, data and mode pins.
92 #define L3_DATA GPIO_H3600_L3_DATA
93 #define L3_MODE GPIO_H3600_L3_MODE
94 #define L3_CLK GPIO_H3600_L3_CLK
97 uint8_t data0
; /* direct addressing register */
101 uint8_t data0
; /* extended addressing register 1 */
102 uint8_t data1
; /* extended addressing register 2 */
103 } EXTEND_REG
= {0, 0};
106 * register space access macros
108 #define GPIO_WRITE(sc, reg, val) \
109 bus_space_write_4(sc->sc_iot, sc->sc_parent->sc_gpioh, reg, val)
110 #define GPIO_READ(sc, reg) \
111 bus_space_read_4(sc->sc_iot, sc->sc_parent->sc_gpioh, reg)
112 #define EGPIO_WRITE(sc) \
113 bus_space_write_2(sc->sc_iot, sc->sc_parent->sc_egpioh, \
114 0, sc->sc_parent->ipaq_egpio)
115 #define SSP_WRITE(sc, reg, val) \
116 bus_space_write_4(sc->sc_iot, sc->sc_parent->sc_ssph, reg, val)
119 uda1341_match(device_t parent
, cfdata_t cf
, void *aux
)
125 uda1341_attach(device_t parent
, device_t self
, void *aux
)
127 struct uda1341_softc
*sc
= device_private(self
);
128 struct ipaq_softc
*psc
= device_private(parent
);
131 aprint_normal_dev(self
, "UDA1341 CODEC\n");
134 sc
->sc_iot
= psc
->sc_iot
;
135 sc
->sc_ioh
= psc
->sc_ioh
;
147 * Attach each devices
150 config_search_ia(uda1341_search
, self
, "udaif", NULL
);
154 uda1341_search(device_t parent
, cfdata_t cf
, const int *ldesc
, void *aux
)
156 if (config_match(parent
, cf
, NULL
) > 0)
157 config_attach(parent
, cf
, NULL
, uda1341_print
);
163 uda1341_print(void *aux
, const char *name
)
169 uda1341_output_high(struct uda1341_softc
*sc
)
173 GPIO_WRITE(sc
, SAGPIO_PSR
, (L3_DATA
| L3_MODE
| L3_CLK
));
174 cr
= GPIO_READ(sc
, SAGPIO_PDR
) | (L3_DATA
| L3_MODE
| L3_CLK
);
175 GPIO_WRITE(sc
, SAGPIO_PDR
, cr
);
179 uda1341_output_low(struct uda1341_softc
*sc
)
183 cr
= GPIO_READ(sc
, SAGPIO_PDR
);
184 cr
&= ~(L3_DATA
| L3_MODE
| L3_CLK
);
185 GPIO_WRITE(sc
, SAGPIO_PDR
, cr
);
189 uda1341_L3_init(struct uda1341_softc
*sc
)
193 cr
= GPIO_READ(sc
, SAGPIO_AFR
);
194 cr
&= ~(L3_DATA
| L3_MODE
| L3_CLK
);
195 GPIO_WRITE(sc
, SAGPIO_AFR
, cr
);
197 uda1341_output_low(sc
);
201 uda1341_init(struct uda1341_softc
*sc
)
205 /* GPIO initialize */
206 cr
= GPIO_READ(sc
, SAGPIO_AFR
);
207 cr
&= ~(GPIO_ALT_SSP_TXD
| GPIO_ALT_SSP_RXD
| GPIO_ALT_SSP_SCLK
|
209 cr
|= GPIO_ALT_SSP_CLK
;
210 GPIO_WRITE(sc
, SAGPIO_AFR
, cr
);
212 cr
= GPIO_READ(sc
, SAGPIO_PDR
);
213 cr
&= ~GPIO_ALT_SSP_CLK
;
214 GPIO_WRITE(sc
, SAGPIO_PDR
, cr
);
216 /* SSP initialize & enable */
217 SSP_WRITE(sc
, SASSP_CR1
, CR1_ECS
);
218 cr
= 0xF | (CR0_FRF_MASK
& (1<<4)) | (CR0_SCR_MASK
& (3<<8)) | CR0_SSE
;
219 SSP_WRITE(sc
, SASSP_CR0
, cr
);
221 /* Enable the audio power */
222 sc
->sc_parent
->ipaq_egpio
|=
223 (EGPIO_H3600_AUD_PWRON
| EGPIO_H3600_AUD_ON
);
224 sc
->sc_parent
->ipaq_egpio
&=
225 ~(EGPIO_H3600_CODEC_RESET
| EGPIO_H3600_QMUTE
);
228 /* external clock configured for 44100 samples/sec */
229 cr
= GPIO_READ(sc
, SAGPIO_PDR
);
230 cr
|= (GPIO_H3600_CLK_SET0
| GPIO_H3600_CLK_SET1
);
231 GPIO_WRITE(sc
, SAGPIO_PDR
, cr
);
232 GPIO_WRITE(sc
, SAGPIO_PSR
, GPIO_H3600_CLK_SET0
);
233 GPIO_WRITE(sc
, SAGPIO_PCR
, GPIO_H3600_CLK_SET1
);
235 /* wait for power on */
237 sc
->sc_parent
->ipaq_egpio
|= EGPIO_H3600_CODEC_RESET
;
240 /* Wait for the UDA1341 to wake up */
245 uda1341_reset(struct uda1341_softc
*sc
)
249 command
= (L3_ADDRESS_COM
<< 2) | L3_ADDRESS_STATUS
;
250 DIRECT_REG
.data0
= STATUS0_RST
| STATUS0_SC_256
| STATUS0_IF_LSB16
;
251 L3_write(sc
, command
, (uint8_t *) &DIRECT_REG
, 1);
253 sc
->sc_parent
->ipaq_egpio
&= ~EGPIO_H3600_CODEC_RESET
;
255 sc
->sc_parent
->ipaq_egpio
|= EGPIO_H3600_CODEC_RESET
;
258 DIRECT_REG
.data0
&= ~STATUS0_RST
;
259 L3_write(sc
, command
, (uint8_t *) &DIRECT_REG
, 1);
263 uda1341_reginit(struct uda1341_softc
*sc
)
268 command
= (L3_ADDRESS_COM
<< 2) | L3_ADDRESS_STATUS
;
269 DIRECT_REG
.data0
= STATUS0_SC_256
| STATUS0_IF_LSB16
;
270 L3_write(sc
, command
, (uint8_t *) &DIRECT_REG
, 1);
273 DIRECT_REG
.data0
= STATUS1_OGS
| STATUS1_IGS
| (1<<7);
274 L3_write(sc
, command
, (uint8_t *) &DIRECT_REG
, 1);
277 command
= (L3_ADDRESS_COM
<< 2) | L3_ADDRESS_DATA0
;
278 DIRECT_REG
.data0
= DATA0_VC(100) | DATA0_COMMON
;
279 L3_write(sc
, command
, (uint8_t *) &DIRECT_REG
, 1);
282 DIRECT_REG
.data0
= DATA1_BB(0) | DATA1_TR(0) | DATA1_COMMON
;
283 L3_write(sc
, command
, (uint8_t *) &DIRECT_REG
, 1);
286 DIRECT_REG
.data0
= DATA2_PP
| DATA2_COMMON
;
287 L3_write(sc
, command
, (uint8_t *) &DIRECT_REG
, 1);
289 /* Extended DATA 0 */
290 EXTEND_REG
.data0
= EXT_ADDR_COMMON
| EXT_ADDR_E0
;
291 EXTEND_REG
.data1
= EXT_DATA_COMMN
| 0x4 ;
292 L3_write(sc
, command
, (uint8_t *) &EXTEND_REG
, 2);
294 /* Extended DATA 1 */
295 EXTEND_REG
.data0
= EXT_ADDR_COMMON
| EXT_ADDR_E1
;
296 EXTEND_REG
.data1
= EXT_DATA_COMMN
| 0x4 ;
297 L3_write(sc
, command
, (uint8_t *) &EXTEND_REG
, 2);
299 /* Extended DATA 2 */
300 EXTEND_REG
.data0
= EXT_ADDR_COMMON
| EXT_ADDR_E2
;
301 EXTEND_REG
.data1
= EXT_DATA_COMMN
| DATA_E2_MS(30);
302 L3_write(sc
, command
, (uint8_t *) &EXTEND_REG
, 2);
304 /* Extended DATA 3 */
305 EXTEND_REG
.data0
= EXT_ADDR_COMMON
| EXT_ADDR_E3
;
306 EXTEND_REG
.data1
= EXT_DATA_COMMN
| DATA_E3_IG_L(0);
307 L3_write(sc
, command
, (uint8_t *) &EXTEND_REG
, 2);
309 /* Extended DATA 4 */
310 EXTEND_REG
.data0
= EXT_ADDR_COMMON
| EXT_ADDR_E4
;
311 EXTEND_REG
.data1
= EXT_DATA_COMMN
| DATA_E4_IG_H(0);
312 L3_write(sc
, command
, (uint8_t *) &EXTEND_REG
, 2);
314 /* Extended DATA 5 */
315 EXTEND_REG
.data0
= EXT_ADDR_COMMON
| EXT_ADDR_E5
;
316 EXTEND_REG
.data1
= EXT_DATA_COMMN
;
317 L3_write(sc
, command
, (uint8_t *) &EXTEND_REG
, 2);
322 L3_getbit(struct uda1341_softc
*sc
)
326 GPIO_WRITE(sc
, SAGPIO_PCR
, L3_CLK
); /* Clock down */
329 cr
= GPIO_READ(sc
, SAGPIO_PLR
);
330 data
= (cr
& L3_DATA
) ? 1 : 0;
332 GPIO_WRITE(sc
, SAGPIO_PSR
, L3_CLK
); /* Clock up */
340 L3_sendbit(struct uda1341_softc
*sc
, int bit
)
342 GPIO_WRITE(sc
, SAGPIO_PCR
, L3_CLK
); /* Clock down */
345 GPIO_WRITE(sc
, SAGPIO_PSR
, L3_DATA
);
347 GPIO_WRITE(sc
, SAGPIO_PCR
, L3_DATA
);
350 GPIO_WRITE(sc
, SAGPIO_PSR
, L3_CLK
); /* Clock up */
356 L3_getbyte(struct uda1341_softc
*sc
, int mode
)
362 case 0: /* Address mode */
363 case 1: /* First data byte */
365 default: /* second data byte via halt-Time */
366 GPIO_WRITE(sc
, SAGPIO_PCR
, L3_CLK
); /* Clock down */
368 GPIO_WRITE(sc
, SAGPIO_PSR
, L3_CLK
); /* Clock up */
372 delay(L3_MODE_SETUP
);
374 for (i
= 0; i
< 8; i
++)
375 data
|= (L3_getbit(sc
) << i
);
384 L3_sendbyte(struct uda1341_softc
*sc
, uint8_t data
, int mode
)
389 case 0: /* Address mode */
390 GPIO_WRITE(sc
, SAGPIO_PCR
, L3_CLK
); /* Clock down */
392 case 1: /* First data byte */
394 default: /* second data byte via halt-Time */
395 GPIO_WRITE(sc
, SAGPIO_PCR
, L3_CLK
); /* Clock down */
397 GPIO_WRITE(sc
, SAGPIO_PSR
, L3_CLK
); /* Clock up */
401 delay(L3_MODE_SETUP
);
403 for (i
= 0; i
< 8; i
++)
404 L3_sendbit(sc
, data
>> i
);
406 if (mode
== 0) /* Address mode */
407 GPIO_WRITE(sc
, SAGPIO_PSR
, L3_CLK
); /* Clock up */
414 L3_read(struct uda1341_softc
*sc
, uint8_t addr
, uint8_t *data
, int len
)
419 uda1341_output_high(sc
);
420 L3_sendbyte(sc
, addr
, mode
++);
422 cr
= GPIO_READ(sc
, SAGPIO_PDR
);
424 GPIO_WRITE(sc
, SAGPIO_PDR
, cr
);
427 *data
++ = L3_getbyte(sc
, mode
++);
428 uda1341_output_low(sc
);
435 L3_write(struct uda1341_softc
*sc
, uint8_t addr
, uint8_t *data
, int len
)
439 uda1341_output_high(sc
);
440 L3_sendbyte(sc
, addr
, mode
++);
442 L3_sendbyte(sc
, *data
++, mode
++);
443 uda1341_output_low(sc
);