blob: 4651ff5135137f728cd9dce90ee8c4980f4b1feb [file] [log] [blame]
Takashi Iwai1bd9deb2005-06-30 18:26:20 +02001/*
2 * Driver for DBRI sound chip found on Sparcs.
Martin Habets43388292005-09-10 15:39:00 +02003 * Copyright (C) 2004, 2005 Martin Habets (mhabets@users.sourceforge.net)
Takashi Iwai1bd9deb2005-06-30 18:26:20 +02004 *
5 * Based entirely upon drivers/sbus/audio/dbri.c which is:
6 * Copyright (C) 1997 Rudolf Koenig (rfkoenig@immd4.informatik.uni-erlangen.de)
7 * Copyright (C) 1998, 1999 Brent Baccala (baccala@freesoft.org)
8 *
9 * This is the lowlevel driver for the DBRI & MMCODEC duo used for ISDN & AUDIO
10 * on Sun SPARCstation 10, 20, LX and Voyager models.
11 *
12 * - DBRI: AT&T T5900FX Dual Basic Rates ISDN Interface. It is a 32 channel
13 * data time multiplexer with ISDN support (aka T7259)
14 * Interfaces: SBus,ISDN NT & TE, CHI, 4 bits parallel.
15 * CHI: (spelled ki) Concentration Highway Interface (AT&T or Intel bus ?).
16 * Documentation:
17 * - "STP 4000SBus Dual Basic Rate ISDN (DBRI) Tranceiver" from
18 * Sparc Technology Business (courtesy of Sun Support)
19 * - Data sheet of the T7903, a newer but very similar ISA bus equivalent
20 * available from the Lucent (formarly AT&T microelectronics) home
21 * page.
22 * - http://www.freesoft.org/Linux/DBRI/
23 * - MMCODEC: Crystal Semiconductor CS4215 16 bit Multimedia Audio Codec
24 * Interfaces: CHI, Audio In & Out, 2 bits parallel
25 * Documentation: from the Crystal Semiconductor home page.
26 *
27 * The DBRI is a 32 pipe machine, each pipe can transfer some bits between
28 * memory and a serial device (long pipes, nr 0-15) or between two serial
29 * devices (short pipes, nr 16-31), or simply send a fixed data to a serial
30 * device (short pipes).
31 * A timeslot defines the bit-offset and nr of bits read from a serial device.
32 * The timeslots are linked to 6 circular lists, one for each direction for
33 * each serial device (NT,TE,CHI). A timeslot is associated to 1 or 2 pipes
34 * (the second one is a monitor/tee pipe, valid only for serial input).
35 *
36 * The mmcodec is connected via the CHI bus and needs the data & some
37 * parameters (volume, balance, output selection) timemultiplexed in 8 byte
38 * chunks. It also has a control mode, which serves for audio format setting.
39 *
40 * Looking at the CS4215 data sheet it is easy to set up 2 or 4 codecs on
41 * the same CHI bus, so I thought perhaps it is possible to use the onboard
42 * & the speakerbox codec simultanously, giving 2 (not very independent :-)
43 * audio devices. But the SUN HW group decided against it, at least on my
44 * LX the speakerbox connector has at least 1 pin missing and 1 wrongly
45 * connected.
Martin Habets43388292005-09-10 15:39:00 +020046 *
47 * I've tried to stick to the following function naming conventions:
48 * snd_* ALSA stuff
Adrian Bunkd254c8f2006-06-30 18:29:51 +020049 * cs4215_* CS4215 codec specific stuff
Martin Habets43388292005-09-10 15:39:00 +020050 * dbri_* DBRI high-level stuff
51 * other DBRI low-level stuff
Takashi Iwai1bd9deb2005-06-30 18:26:20 +020052 */
53
54#include <sound/driver.h>
55#include <linux/interrupt.h>
56#include <linux/delay.h>
57
58#include <sound/core.h>
59#include <sound/pcm.h>
60#include <sound/pcm_params.h>
61#include <sound/info.h>
62#include <sound/control.h>
63#include <sound/initval.h>
64
65#include <asm/irq.h>
66#include <asm/io.h>
67#include <asm/sbus.h>
68#include <asm/atomic.h>
69
70MODULE_AUTHOR("Rudolf Koenig, Brent Baccala and Martin Habets");
71MODULE_DESCRIPTION("Sun DBRI");
72MODULE_LICENSE("GPL");
73MODULE_SUPPORTED_DEVICE("{{Sun,DBRI}}");
74
75static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
76static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
77static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP; /* Enable this card */
78
79module_param_array(index, int, NULL, 0444);
80MODULE_PARM_DESC(index, "Index value for Sun DBRI soundcard.");
81module_param_array(id, charp, NULL, 0444);
82MODULE_PARM_DESC(id, "ID string for Sun DBRI soundcard.");
83module_param_array(enable, bool, NULL, 0444);
84MODULE_PARM_DESC(enable, "Enable Sun DBRI soundcard.");
85
86#define DBRI_DEBUG
87
88#define D_INT (1<<0)
89#define D_GEN (1<<1)
90#define D_CMD (1<<2)
91#define D_MM (1<<3)
92#define D_USR (1<<4)
93#define D_DESC (1<<5)
94
Takashi Iwai6581f4e2006-05-17 17:14:51 +020095static int dbri_debug;
Martin Habets43388292005-09-10 15:39:00 +020096module_param(dbri_debug, int, 0644);
Takashi Iwai1bd9deb2005-06-30 18:26:20 +020097MODULE_PARM_DESC(dbri_debug, "Debug value for Sun DBRI soundcard.");
98
99#ifdef DBRI_DEBUG
100static char *cmds[] = {
101 "WAIT", "PAUSE", "JUMP", "IIQ", "REX", "SDP", "CDP", "DTS",
102 "SSP", "CHI", "NT", "TE", "CDEC", "TEST", "CDM", "RESRV"
103};
104
105#define dprintk(a, x...) if(dbri_debug & a) printk(KERN_DEBUG x)
106
Takashi Iwai1bd9deb2005-06-30 18:26:20 +0200107#else
108#define dprintk(a, x...)
109
Takashi Iwai1bd9deb2005-06-30 18:26:20 +0200110#endif /* DBRI_DEBUG */
111
Krzysztof Helt42fe7642006-08-16 12:53:34 +0200112#define DBRI_CMD(cmd, intr, value) ((cmd << 28) | \
113 (intr << 27) | \
114 value)
115
Takashi Iwai1bd9deb2005-06-30 18:26:20 +0200116/***************************************************************************
117 CS4215 specific definitions and structures
118****************************************************************************/
119
120struct cs4215 {
121 __u8 data[4]; /* Data mode: Time slots 5-8 */
122 __u8 ctrl[4]; /* Ctrl mode: Time slots 1-4 */
123 __u8 onboard;
124 __u8 offset; /* Bit offset from frame sync to time slot 1 */
125 volatile __u32 status;
126 volatile __u32 version;
127 __u8 precision; /* In bits, either 8 or 16 */
128 __u8 channels; /* 1 or 2 */
129};
130
131/*
132 * Control mode first
133 */
134
135/* Time Slot 1, Status register */
136#define CS4215_CLB (1<<2) /* Control Latch Bit */
137#define CS4215_OLB (1<<3) /* 1: line: 2.0V, speaker 4V */
138 /* 0: line: 2.8V, speaker 8V */
139#define CS4215_MLB (1<<4) /* 1: Microphone: 20dB gain disabled */
140#define CS4215_RSRVD_1 (1<<5)
141
142/* Time Slot 2, Data Format Register */
143#define CS4215_DFR_LINEAR16 0
144#define CS4215_DFR_ULAW 1
145#define CS4215_DFR_ALAW 2
146#define CS4215_DFR_LINEAR8 3
147#define CS4215_DFR_STEREO (1<<2)
148static struct {
149 unsigned short freq;
150 unsigned char xtal;
151 unsigned char csval;
152} CS4215_FREQ[] = {
153 { 8000, (1 << 4), (0 << 3) },
154 { 16000, (1 << 4), (1 << 3) },
155 { 27429, (1 << 4), (2 << 3) }, /* Actually 24428.57 */
156 { 32000, (1 << 4), (3 << 3) },
157 /* { NA, (1 << 4), (4 << 3) }, */
158 /* { NA, (1 << 4), (5 << 3) }, */
159 { 48000, (1 << 4), (6 << 3) },
160 { 9600, (1 << 4), (7 << 3) },
161 { 5513, (2 << 4), (0 << 3) }, /* Actually 5512.5 */
162 { 11025, (2 << 4), (1 << 3) },
163 { 18900, (2 << 4), (2 << 3) },
164 { 22050, (2 << 4), (3 << 3) },
165 { 37800, (2 << 4), (4 << 3) },
166 { 44100, (2 << 4), (5 << 3) },
167 { 33075, (2 << 4), (6 << 3) },
168 { 6615, (2 << 4), (7 << 3) },
169 { 0, 0, 0}
170};
171
172#define CS4215_HPF (1<<7) /* High Pass Filter, 1: Enabled */
173
174#define CS4215_12_MASK 0xfcbf /* Mask off reserved bits in slot 1 & 2 */
175
176/* Time Slot 3, Serial Port Control register */
177#define CS4215_XEN (1<<0) /* 0: Enable serial output */
178#define CS4215_XCLK (1<<1) /* 1: Master mode: Generate SCLK */
179#define CS4215_BSEL_64 (0<<2) /* Bitrate: 64 bits per frame */
180#define CS4215_BSEL_128 (1<<2)
181#define CS4215_BSEL_256 (2<<2)
182#define CS4215_MCK_MAST (0<<4) /* Master clock */
183#define CS4215_MCK_XTL1 (1<<4) /* 24.576 MHz clock source */
184#define CS4215_MCK_XTL2 (2<<4) /* 16.9344 MHz clock source */
185#define CS4215_MCK_CLK1 (3<<4) /* Clockin, 256 x Fs */
186#define CS4215_MCK_CLK2 (4<<4) /* Clockin, see DFR */
187
188/* Time Slot 4, Test Register */
189#define CS4215_DAD (1<<0) /* 0:Digital-Dig loop, 1:Dig-Analog-Dig loop */
190#define CS4215_ENL (1<<1) /* Enable Loopback Testing */
191
192/* Time Slot 5, Parallel Port Register */
193/* Read only here and the same as the in data mode */
194
195/* Time Slot 6, Reserved */
196
197/* Time Slot 7, Version Register */
198#define CS4215_VERSION_MASK 0xf /* Known versions 0/C, 1/D, 2/E */
199
200/* Time Slot 8, Reserved */
201
202/*
203 * Data mode
204 */
205/* Time Slot 1-2: Left Channel Data, 2-3: Right Channel Data */
206
207/* Time Slot 5, Output Setting */
208#define CS4215_LO(v) v /* Left Output Attenuation 0x3f: -94.5 dB */
209#define CS4215_LE (1<<6) /* Line Out Enable */
210#define CS4215_HE (1<<7) /* Headphone Enable */
211
212/* Time Slot 6, Output Setting */
213#define CS4215_RO(v) v /* Right Output Attenuation 0x3f: -94.5 dB */
214#define CS4215_SE (1<<6) /* Speaker Enable */
215#define CS4215_ADI (1<<7) /* A/D Data Invalid: Busy in calibration */
216
217/* Time Slot 7, Input Setting */
218#define CS4215_LG(v) v /* Left Gain Setting 0xf: 22.5 dB */
219#define CS4215_IS (1<<4) /* Input Select: 1=Microphone, 0=Line */
220#define CS4215_OVR (1<<5) /* 1: Overrange condition occurred */
221#define CS4215_PIO0 (1<<6) /* Parallel I/O 0 */
222#define CS4215_PIO1 (1<<7)
223
224/* Time Slot 8, Input Setting */
225#define CS4215_RG(v) v /* Right Gain Setting 0xf: 22.5 dB */
226#define CS4215_MA(v) (v<<4) /* Monitor Path Attenuation 0xf: mute */
227
228/***************************************************************************
229 DBRI specific definitions and structures
230****************************************************************************/
231
232/* DBRI main registers */
233#define REG0 0x00UL /* Status and Control */
234#define REG1 0x04UL /* Mode and Interrupt */
235#define REG2 0x08UL /* Parallel IO */
236#define REG3 0x0cUL /* Test */
237#define REG8 0x20UL /* Command Queue Pointer */
238#define REG9 0x24UL /* Interrupt Queue Pointer */
239
240#define DBRI_NO_CMDS 64
Takashi Iwai1bd9deb2005-06-30 18:26:20 +0200241#define DBRI_INT_BLK 64
242#define DBRI_NO_DESCS 64
243#define DBRI_NO_PIPES 32
244
245#define DBRI_MM_ONB 1
246#define DBRI_MM_SB 2
247
248#define DBRI_REC 0
249#define DBRI_PLAY 1
250#define DBRI_NO_STREAMS 2
251
252/* One transmit/receive descriptor */
253struct dbri_mem {
254 volatile __u32 word1;
255 volatile __u32 ba; /* Transmit/Receive Buffer Address */
256 volatile __u32 nda; /* Next Descriptor Address */
257 volatile __u32 word4;
258};
259
260/* This structure is in a DMA region where it can accessed by both
261 * the CPU and the DBRI
262 */
263struct dbri_dma {
264 volatile s32 cmd[DBRI_NO_CMDS]; /* Place for commands */
Krzysztof Helt6fb98282006-08-16 12:54:29 +0200265 volatile s32 intr[DBRI_INT_BLK]; /* Interrupt field */
Takashi Iwai1bd9deb2005-06-30 18:26:20 +0200266 struct dbri_mem desc[DBRI_NO_DESCS]; /* Xmit/receive descriptors */
267};
268
269#define dbri_dma_off(member, elem) \
270 ((u32)(unsigned long) \
271 (&(((struct dbri_dma *)0)->member[elem])))
272
273enum in_or_out { PIPEinput, PIPEoutput };
274
275struct dbri_pipe {
276 u32 sdp; /* SDP command word */
277 enum in_or_out direction;
278 int nextpipe; /* Next pipe in linked list */
279 int prevpipe;
280 int cycle; /* Offset of timeslot (bits) */
281 int length; /* Length of timeslot (bits) */
282 int first_desc; /* Index of first descriptor */
283 int desc; /* Index of active descriptor */
284 volatile __u32 *recv_fixed_ptr; /* Ptr to receive fixed data */
285};
286
287struct dbri_desc {
288 int inuse; /* Boolean flag */
289 int next; /* Index of next desc, or -1 */
290 unsigned int len;
291};
292
293/* Per stream (playback or record) information */
Takashi Iwai475675d2005-11-17 15:11:51 +0100294struct dbri_streaminfo {
295 struct snd_pcm_substream *substream;
Takashi Iwai1bd9deb2005-06-30 18:26:20 +0200296 u32 dvma_buffer; /* Device view of Alsa DMA buffer */
297 int left; /* # of bytes left in DMA buffer */
298 int size; /* Size of DMA buffer */
299 size_t offset; /* offset in user buffer */
300 int pipe; /* Data pipe used */
301 int left_gain; /* mixer elements */
302 int right_gain;
303 int balance;
Takashi Iwai475675d2005-11-17 15:11:51 +0100304};
Takashi Iwai1bd9deb2005-06-30 18:26:20 +0200305
306/* This structure holds the information for both chips (DBRI & CS4215) */
Takashi Iwai475675d2005-11-17 15:11:51 +0100307struct snd_dbri {
308 struct snd_card *card; /* ALSA card */
309 struct snd_pcm *pcm;
Takashi Iwai1bd9deb2005-06-30 18:26:20 +0200310
311 int regs_size, irq; /* Needed for unload */
312 struct sbus_dev *sdev; /* SBUS device info */
313 spinlock_t lock;
314
315 volatile struct dbri_dma *dma; /* Pointer to our DMA block */
316 u32 dma_dvma; /* DBRI visible DMA address */
317
318 void __iomem *regs; /* dbri HW regs */
319 int dbri_version; /* 'e' and up is OK */
320 int dbri_irqp; /* intr queue pointer */
Martin Habets43388292005-09-10 15:39:00 +0200321 int wait_send; /* sequence of command buffers send */
322 int wait_ackd; /* sequence of command buffers acknowledged */
Takashi Iwai1bd9deb2005-06-30 18:26:20 +0200323
324 struct dbri_pipe pipes[DBRI_NO_PIPES]; /* DBRI's 32 data pipes */
325 struct dbri_desc descs[DBRI_NO_DESCS];
326
327 int chi_in_pipe;
328 int chi_out_pipe;
329 int chi_bpf;
330
331 struct cs4215 mm; /* mmcodec special info */
332 /* per stream (playback/record) info */
333 struct dbri_streaminfo stream_info[DBRI_NO_STREAMS];
334
335 struct snd_dbri *next;
Takashi Iwai475675d2005-11-17 15:11:51 +0100336};
Takashi Iwai1bd9deb2005-06-30 18:26:20 +0200337
Takashi Iwai1bd9deb2005-06-30 18:26:20 +0200338#define DBRI_MAX_VOLUME 63 /* Output volume */
339#define DBRI_MAX_GAIN 15 /* Input gain */
340#define DBRI_RIGHT_BALANCE 255
341#define DBRI_MID_BALANCE (DBRI_RIGHT_BALANCE >> 1)
342
343/* DBRI Reg0 - Status Control Register - defines. (Page 17) */
344#define D_P (1<<15) /* Program command & queue pointer valid */
345#define D_G (1<<14) /* Allow 4-Word SBus Burst */
346#define D_S (1<<13) /* Allow 16-Word SBus Burst */
347#define D_E (1<<12) /* Allow 8-Word SBus Burst */
348#define D_X (1<<7) /* Sanity Timer Disable */
349#define D_T (1<<6) /* Permit activation of the TE interface */
350#define D_N (1<<5) /* Permit activation of the NT interface */
351#define D_C (1<<4) /* Permit activation of the CHI interface */
352#define D_F (1<<3) /* Force Sanity Timer Time-Out */
353#define D_D (1<<2) /* Disable Master Mode */
354#define D_H (1<<1) /* Halt for Analysis */
355#define D_R (1<<0) /* Soft Reset */
356
357/* DBRI Reg1 - Mode and Interrupt Register - defines. (Page 18) */
358#define D_LITTLE_END (1<<8) /* Byte Order */
359#define D_BIG_END (0<<8) /* Byte Order */
360#define D_MRR (1<<4) /* Multiple Error Ack on SBus (readonly) */
361#define D_MLE (1<<3) /* Multiple Late Error on SBus (readonly) */
362#define D_LBG (1<<2) /* Lost Bus Grant on SBus (readonly) */
363#define D_MBE (1<<1) /* Burst Error on SBus (readonly) */
364#define D_IR (1<<0) /* Interrupt Indicator (readonly) */
365
366/* DBRI Reg2 - Parallel IO Register - defines. (Page 18) */
367#define D_ENPIO3 (1<<7) /* Enable Pin 3 */
368#define D_ENPIO2 (1<<6) /* Enable Pin 2 */
369#define D_ENPIO1 (1<<5) /* Enable Pin 1 */
370#define D_ENPIO0 (1<<4) /* Enable Pin 0 */
371#define D_ENPIO (0xf0) /* Enable all the pins */
372#define D_PIO3 (1<<3) /* Pin 3: 1: Data mode, 0: Ctrl mode */
373#define D_PIO2 (1<<2) /* Pin 2: 1: Onboard PDN */
374#define D_PIO1 (1<<1) /* Pin 1: 0: Reset */
375#define D_PIO0 (1<<0) /* Pin 0: 1: Speakerbox PDN */
376
377/* DBRI Commands (Page 20) */
378#define D_WAIT 0x0 /* Stop execution */
379#define D_PAUSE 0x1 /* Flush long pipes */
380#define D_JUMP 0x2 /* New command queue */
381#define D_IIQ 0x3 /* Initialize Interrupt Queue */
382#define D_REX 0x4 /* Report command execution via interrupt */
383#define D_SDP 0x5 /* Setup Data Pipe */
384#define D_CDP 0x6 /* Continue Data Pipe (reread NULL Pointer) */
385#define D_DTS 0x7 /* Define Time Slot */
386#define D_SSP 0x8 /* Set short Data Pipe */
387#define D_CHI 0x9 /* Set CHI Global Mode */
388#define D_NT 0xa /* NT Command */
389#define D_TE 0xb /* TE Command */
390#define D_CDEC 0xc /* Codec setup */
391#define D_TEST 0xd /* No comment */
392#define D_CDM 0xe /* CHI Data mode command */
393
394/* Special bits for some commands */
395#define D_PIPE(v) ((v)<<0) /* Pipe Nr: 0-15 long, 16-21 short */
396
397/* Setup Data Pipe */
398/* IRM */
399#define D_SDP_2SAME (1<<18) /* Report 2nd time in a row value rcvd */
400#define D_SDP_CHANGE (2<<18) /* Report any changes */
401#define D_SDP_EVERY (3<<18) /* Report any changes */
402#define D_SDP_EOL (1<<17) /* EOL interrupt enable */
403#define D_SDP_IDLE (1<<16) /* HDLC idle interrupt enable */
404
405/* Pipe data MODE */
406#define D_SDP_MEM (0<<13) /* To/from memory */
407#define D_SDP_HDLC (2<<13)
408#define D_SDP_HDLC_D (3<<13) /* D Channel (prio control) */
409#define D_SDP_SER (4<<13) /* Serial to serial */
410#define D_SDP_FIXED (6<<13) /* Short only */
411#define D_SDP_MODE(v) ((v)&(7<<13))
412
413#define D_SDP_TO_SER (1<<12) /* Direction */
414#define D_SDP_FROM_SER (0<<12) /* Direction */
415#define D_SDP_MSB (1<<11) /* Bit order within Byte */
416#define D_SDP_LSB (0<<11) /* Bit order within Byte */
417#define D_SDP_P (1<<10) /* Pointer Valid */
418#define D_SDP_A (1<<8) /* Abort */
419#define D_SDP_C (1<<7) /* Clear */
420
421/* Define Time Slot */
422#define D_DTS_VI (1<<17) /* Valid Input Time-Slot Descriptor */
423#define D_DTS_VO (1<<16) /* Valid Output Time-Slot Descriptor */
424#define D_DTS_INS (1<<15) /* Insert Time Slot */
425#define D_DTS_DEL (0<<15) /* Delete Time Slot */
426#define D_DTS_PRVIN(v) ((v)<<10) /* Previous In Pipe */
427#define D_DTS_PRVOUT(v) ((v)<<5) /* Previous Out Pipe */
428
429/* Time Slot defines */
430#define D_TS_LEN(v) ((v)<<24) /* Number of bits in this time slot */
431#define D_TS_CYCLE(v) ((v)<<14) /* Bit Count at start of TS */
432#define D_TS_DI (1<<13) /* Data Invert */
433#define D_TS_1CHANNEL (0<<10) /* Single Channel / Normal mode */
434#define D_TS_MONITOR (2<<10) /* Monitor pipe */
435#define D_TS_NONCONTIG (3<<10) /* Non contiguous mode */
436#define D_TS_ANCHOR (7<<10) /* Starting short pipes */
437#define D_TS_MON(v) ((v)<<5) /* Monitor Pipe */
438#define D_TS_NEXT(v) ((v)<<0) /* Pipe Nr: 0-15 long, 16-21 short */
439
440/* Concentration Highway Interface Modes */
441#define D_CHI_CHICM(v) ((v)<<16) /* Clock mode */
442#define D_CHI_IR (1<<15) /* Immediate Interrupt Report */
443#define D_CHI_EN (1<<14) /* CHIL Interrupt enabled */
444#define D_CHI_OD (1<<13) /* Open Drain Enable */
445#define D_CHI_FE (1<<12) /* Sample CHIFS on Rising Frame Edge */
446#define D_CHI_FD (1<<11) /* Frame Drive */
447#define D_CHI_BPF(v) ((v)<<0) /* Bits per Frame */
448
449/* NT: These are here for completeness */
450#define D_NT_FBIT (1<<17) /* Frame Bit */
451#define D_NT_NBF (1<<16) /* Number of bad frames to loose framing */
452#define D_NT_IRM_IMM (1<<15) /* Interrupt Report & Mask: Immediate */
453#define D_NT_IRM_EN (1<<14) /* Interrupt Report & Mask: Enable */
454#define D_NT_ISNT (1<<13) /* Configfure interface as NT */
455#define D_NT_FT (1<<12) /* Fixed Timing */
456#define D_NT_EZ (1<<11) /* Echo Channel is Zeros */
457#define D_NT_IFA (1<<10) /* Inhibit Final Activation */
458#define D_NT_ACT (1<<9) /* Activate Interface */
459#define D_NT_MFE (1<<8) /* Multiframe Enable */
460#define D_NT_RLB(v) ((v)<<5) /* Remote Loopback */
461#define D_NT_LLB(v) ((v)<<2) /* Local Loopback */
462#define D_NT_FACT (1<<1) /* Force Activation */
463#define D_NT_ABV (1<<0) /* Activate Bipolar Violation */
464
465/* Codec Setup */
466#define D_CDEC_CK(v) ((v)<<24) /* Clock Select */
467#define D_CDEC_FED(v) ((v)<<12) /* FSCOD Falling Edge Delay */
468#define D_CDEC_RED(v) ((v)<<0) /* FSCOD Rising Edge Delay */
469
470/* Test */
471#define D_TEST_RAM(v) ((v)<<16) /* RAM Pointer */
472#define D_TEST_SIZE(v) ((v)<<11) /* */
473#define D_TEST_ROMONOFF 0x5 /* Toggle ROM opcode monitor on/off */
474#define D_TEST_PROC 0x6 /* MicroProcessor test */
475#define D_TEST_SER 0x7 /* Serial-Controller test */
476#define D_TEST_RAMREAD 0x8 /* Copy from Ram to system memory */
477#define D_TEST_RAMWRITE 0x9 /* Copy into Ram from system memory */
478#define D_TEST_RAMBIST 0xa /* RAM Built-In Self Test */
479#define D_TEST_MCBIST 0xb /* Microcontroller Built-In Self Test */
480#define D_TEST_DUMP 0xe /* ROM Dump */
481
482/* CHI Data Mode */
483#define D_CDM_THI (1<<8) /* Transmit Data on CHIDR Pin */
484#define D_CDM_RHI (1<<7) /* Receive Data on CHIDX Pin */
485#define D_CDM_RCE (1<<6) /* Receive on Rising Edge of CHICK */
486#define D_CDM_XCE (1<<2) /* Transmit Data on Rising Edge of CHICK */
487#define D_CDM_XEN (1<<1) /* Transmit Highway Enable */
488#define D_CDM_REN (1<<0) /* Receive Highway Enable */
489
490/* The Interrupts */
491#define D_INTR_BRDY 1 /* Buffer Ready for processing */
492#define D_INTR_MINT 2 /* Marked Interrupt in RD/TD */
493#define D_INTR_IBEG 3 /* Flag to idle transition detected (HDLC) */
494#define D_INTR_IEND 4 /* Idle to flag transition detected (HDLC) */
495#define D_INTR_EOL 5 /* End of List */
496#define D_INTR_CMDI 6 /* Command has bean read */
497#define D_INTR_XCMP 8 /* Transmission of frame complete */
498#define D_INTR_SBRI 9 /* BRI status change info */
499#define D_INTR_FXDT 10 /* Fixed data change */
500#define D_INTR_CHIL 11 /* CHI lost frame sync (channel 36 only) */
501#define D_INTR_COLL 11 /* Unrecoverable D-Channel collision */
502#define D_INTR_DBYT 12 /* Dropped by frame slip */
503#define D_INTR_RBYT 13 /* Repeated by frame slip */
504#define D_INTR_LINT 14 /* Lost Interrupt */
505#define D_INTR_UNDR 15 /* DMA underrun */
506
507#define D_INTR_TE 32
508#define D_INTR_NT 34
509#define D_INTR_CHI 36
510#define D_INTR_CMD 38
511
512#define D_INTR_GETCHAN(v) (((v)>>24) & 0x3f)
513#define D_INTR_GETCODE(v) (((v)>>20) & 0xf)
514#define D_INTR_GETCMD(v) (((v)>>16) & 0xf)
515#define D_INTR_GETVAL(v) ((v) & 0xffff)
516#define D_INTR_GETRVAL(v) ((v) & 0xfffff)
517
518#define D_P_0 0 /* TE receive anchor */
519#define D_P_1 1 /* TE transmit anchor */
520#define D_P_2 2 /* NT transmit anchor */
521#define D_P_3 3 /* NT receive anchor */
522#define D_P_4 4 /* CHI send data */
523#define D_P_5 5 /* CHI receive data */
524#define D_P_6 6 /* */
525#define D_P_7 7 /* */
526#define D_P_8 8 /* */
527#define D_P_9 9 /* */
528#define D_P_10 10 /* */
529#define D_P_11 11 /* */
530#define D_P_12 12 /* */
531#define D_P_13 13 /* */
532#define D_P_14 14 /* */
533#define D_P_15 15 /* */
534#define D_P_16 16 /* CHI anchor pipe */
535#define D_P_17 17 /* CHI send */
536#define D_P_18 18 /* CHI receive */
537#define D_P_19 19 /* CHI receive */
538#define D_P_20 20 /* CHI receive */
539#define D_P_21 21 /* */
540#define D_P_22 22 /* */
541#define D_P_23 23 /* */
542#define D_P_24 24 /* */
543#define D_P_25 25 /* */
544#define D_P_26 26 /* */
545#define D_P_27 27 /* */
546#define D_P_28 28 /* */
547#define D_P_29 29 /* */
548#define D_P_30 30 /* */
549#define D_P_31 31 /* */
550
551/* Transmit descriptor defines */
552#define DBRI_TD_F (1<<31) /* End of Frame */
553#define DBRI_TD_D (1<<30) /* Do not append CRC */
554#define DBRI_TD_CNT(v) ((v)<<16) /* Number of valid bytes in the buffer */
555#define DBRI_TD_B (1<<15) /* Final interrupt */
556#define DBRI_TD_M (1<<14) /* Marker interrupt */
557#define DBRI_TD_I (1<<13) /* Transmit Idle Characters */
558#define DBRI_TD_FCNT(v) (v) /* Flag Count */
559#define DBRI_TD_UNR (1<<3) /* Underrun: transmitter is out of data */
560#define DBRI_TD_ABT (1<<2) /* Abort: frame aborted */
561#define DBRI_TD_TBC (1<<0) /* Transmit buffer Complete */
562#define DBRI_TD_STATUS(v) ((v)&0xff) /* Transmit status */
563 /* Maximum buffer size per TD: almost 8Kb */
564#define DBRI_TD_MAXCNT ((1 << 13) - 1)
565
566/* Receive descriptor defines */
567#define DBRI_RD_F (1<<31) /* End of Frame */
568#define DBRI_RD_C (1<<30) /* Completed buffer */
569#define DBRI_RD_B (1<<15) /* Final interrupt */
570#define DBRI_RD_M (1<<14) /* Marker interrupt */
571#define DBRI_RD_BCNT(v) (v) /* Buffer size */
572#define DBRI_RD_CRC (1<<7) /* 0: CRC is correct */
573#define DBRI_RD_BBC (1<<6) /* 1: Bad Byte received */
574#define DBRI_RD_ABT (1<<5) /* Abort: frame aborted */
575#define DBRI_RD_OVRN (1<<3) /* Overrun: data lost */
576#define DBRI_RD_STATUS(v) ((v)&0xff) /* Receive status */
577#define DBRI_RD_CNT(v) (((v)>>16)&0x1fff) /* Valid bytes in the buffer */
578
579/* stream_info[] access */
580/* Translate the ALSA direction into the array index */
581#define DBRI_STREAMNO(substream) \
582 (substream->stream == \
583 SNDRV_PCM_STREAM_PLAYBACK? DBRI_PLAY: DBRI_REC)
584
585/* Return a pointer to dbri_streaminfo */
586#define DBRI_STREAM(dbri, substream) &dbri->stream_info[DBRI_STREAMNO(substream)]
587
Takashi Iwai6581f4e2006-05-17 17:14:51 +0200588static struct snd_dbri *dbri_list; /* All DBRI devices */
Takashi Iwai1bd9deb2005-06-30 18:26:20 +0200589
590/*
591 * Short data pipes transmit LSB first. The CS4215 receives MSB first. Grrr.
592 * So we have to reverse the bits. Note: not all bit lengths are supported
593 */
594static __u32 reverse_bytes(__u32 b, int len)
595{
596 switch (len) {
597 case 32:
598 b = ((b & 0xffff0000) >> 16) | ((b & 0x0000ffff) << 16);
599 case 16:
600 b = ((b & 0xff00ff00) >> 8) | ((b & 0x00ff00ff) << 8);
601 case 8:
602 b = ((b & 0xf0f0f0f0) >> 4) | ((b & 0x0f0f0f0f) << 4);
603 case 4:
604 b = ((b & 0xcccccccc) >> 2) | ((b & 0x33333333) << 2);
605 case 2:
606 b = ((b & 0xaaaaaaaa) >> 1) | ((b & 0x55555555) << 1);
607 case 1:
608 case 0:
609 break;
610 default:
611 printk(KERN_ERR "DBRI reverse_bytes: unsupported length\n");
612 };
613
614 return b;
615}
616
617/*
618****************************************************************************
619************** DBRI initialization and command synchronization *************
620****************************************************************************
621
622Commands are sent to the DBRI by building a list of them in memory,
623then writing the address of the first list item to DBRI register 8.
Martin Habets43388292005-09-10 15:39:00 +0200624The list is terminated with a WAIT command, which generates a
625CPU interrupt to signal completion.
Takashi Iwai1bd9deb2005-06-30 18:26:20 +0200626
627Since the DBRI can run in parallel with the CPU, several means of
Martin Habets43388292005-09-10 15:39:00 +0200628synchronization present themselves. The method implemented here is close
629to the original scheme (Rudolf's), and uses 2 counters (wait_send and
630wait_ackd) to synchronize the command buffer between the CPU and the DBRI.
Takashi Iwai1bd9deb2005-06-30 18:26:20 +0200631
632A more sophisticated scheme might involve a circular command buffer
633or an array of command buffers. A routine could fill one with
634commands and link it onto a list. When a interrupt signaled
635completion of the current command buffer, look on the list for
636the next one.
637
Takashi Iwai1bd9deb2005-06-30 18:26:20 +0200638Every time a routine wants to write commands to the DBRI, it must
639first call dbri_cmdlock() and get an initial pointer into dbri->dma->cmd
Martin Habets43388292005-09-10 15:39:00 +0200640in return. dbri_cmdlock() will block if the previous commands have not
641been completed yet. After this the commands can be written to the buffer,
642and dbri_cmdsend() is called with the final pointer value to send them
643to the DBRI.
Takashi Iwai1bd9deb2005-06-30 18:26:20 +0200644
645*/
646
Takashi Iwai475675d2005-11-17 15:11:51 +0100647static void dbri_process_interrupt_buffer(struct snd_dbri * dbri);
Martin Habets43388292005-09-10 15:39:00 +0200648
Takashi Iwai475675d2005-11-17 15:11:51 +0100649enum dbri_lock { NoGetLock, GetLock };
Martin Habets43388292005-09-10 15:39:00 +0200650#define MAXLOOPS 10
Takashi Iwai1bd9deb2005-06-30 18:26:20 +0200651
Takashi Iwai475675d2005-11-17 15:11:51 +0100652static volatile s32 *dbri_cmdlock(struct snd_dbri * dbri, enum dbri_lock get)
Takashi Iwai1bd9deb2005-06-30 18:26:20 +0200653{
Martin Habets43388292005-09-10 15:39:00 +0200654 int maxloops = MAXLOOPS;
655
Takashi Iwai1bd9deb2005-06-30 18:26:20 +0200656#ifndef SMP
657 if ((get == GetLock) && spin_is_locked(&dbri->lock)) {
658 printk(KERN_ERR "DBRI: cmdlock called while in spinlock.");
659 }
660#endif
661
Martin Habets43388292005-09-10 15:39:00 +0200662 /* Delay if previous commands are still being processed */
663 while ((--maxloops) > 0 && (dbri->wait_send != dbri->wait_ackd)) {
664 msleep_interruptible(1);
665 /* If dbri_cmdlock() got called from inside the
666 * interrupt handler, this will do the processing.
667 */
668 dbri_process_interrupt_buffer(dbri);
669 }
670 if (maxloops == 0) {
671 printk(KERN_ERR "DBRI: Chip never completed command buffer %d\n",
672 dbri->wait_send);
673 } else {
674 dprintk(D_CMD, "Chip completed command buffer (%d)\n",
675 MAXLOOPS - maxloops - 1);
676 }
677
Takashi Iwai1bd9deb2005-06-30 18:26:20 +0200678 /*if (get == GetLock) spin_lock(&dbri->lock); */
679 return &dbri->dma->cmd[0];
680}
681
Takashi Iwai475675d2005-11-17 15:11:51 +0100682static void dbri_cmdsend(struct snd_dbri * dbri, volatile s32 * cmd)
Takashi Iwai1bd9deb2005-06-30 18:26:20 +0200683{
Takashi Iwai1bd9deb2005-06-30 18:26:20 +0200684 volatile s32 *ptr;
685
686 for (ptr = &dbri->dma->cmd[0]; ptr < cmd; ptr++) {
687 dprintk(D_CMD, "cmd: %lx:%08x\n", (unsigned long)ptr, *ptr);
688 }
689
690 if ((cmd - &dbri->dma->cmd[0]) >= DBRI_NO_CMDS - 1) {
Martin Habets43388292005-09-10 15:39:00 +0200691 printk(KERN_ERR "DBRI: Command buffer overflow! (bug in driver)\n");
Takashi Iwai1bd9deb2005-06-30 18:26:20 +0200692 /* Ignore the last part. */
693 cmd = &dbri->dma->cmd[DBRI_NO_CMDS - 3];
694 }
695
Martin Habets43388292005-09-10 15:39:00 +0200696 dbri->wait_send++;
697 dbri->wait_send &= 0xffff; /* restrict it to a 16 bit counter. */
Takashi Iwai1bd9deb2005-06-30 18:26:20 +0200698 *(cmd++) = DBRI_CMD(D_PAUSE, 0, 0);
Martin Habets43388292005-09-10 15:39:00 +0200699 *(cmd++) = DBRI_CMD(D_WAIT, 1, dbri->wait_send);
700
701 /* Set command pointer and signal it is valid. */
Takashi Iwai1bd9deb2005-06-30 18:26:20 +0200702 sbus_writel(dbri->dma_dvma, dbri->regs + REG8);
Takashi Iwai1bd9deb2005-06-30 18:26:20 +0200703
704 /*spin_unlock(&dbri->lock); */
705}
706
707/* Lock must be held when calling this */
Takashi Iwai475675d2005-11-17 15:11:51 +0100708static void dbri_reset(struct snd_dbri * dbri)
Takashi Iwai1bd9deb2005-06-30 18:26:20 +0200709{
710 int i;
711
712 dprintk(D_GEN, "reset 0:%x 2:%x 8:%x 9:%x\n",
713 sbus_readl(dbri->regs + REG0),
714 sbus_readl(dbri->regs + REG2),
715 sbus_readl(dbri->regs + REG8), sbus_readl(dbri->regs + REG9));
716
717 sbus_writel(D_R, dbri->regs + REG0); /* Soft Reset */
718 for (i = 0; (sbus_readl(dbri->regs + REG0) & D_R) && i < 64; i++)
719 udelay(10);
720}
721
722/* Lock must not be held before calling this */
Takashi Iwai475675d2005-11-17 15:11:51 +0100723static void dbri_initialize(struct snd_dbri * dbri)
Takashi Iwai1bd9deb2005-06-30 18:26:20 +0200724{
725 volatile s32 *cmd;
726 u32 dma_addr, tmp;
727 unsigned long flags;
728 int n;
729
730 spin_lock_irqsave(&dbri->lock, flags);
731
732 dbri_reset(dbri);
733
734 cmd = dbri_cmdlock(dbri, NoGetLock);
735 dprintk(D_GEN, "init: cmd: %p, int: %p\n",
736 &dbri->dma->cmd[0], &dbri->dma->intr[0]);
737
Takashi Iwai1bd9deb2005-06-30 18:26:20 +0200738 /* Initialize pipes */
739 for (n = 0; n < DBRI_NO_PIPES; n++)
740 dbri->pipes[n].desc = dbri->pipes[n].first_desc = -1;
741
Martin Habets43388292005-09-10 15:39:00 +0200742 /* A brute approach - DBRI falls back to working burst size by itself
743 * On SS20 D_S does not work, so do not try so high. */
Takashi Iwai1bd9deb2005-06-30 18:26:20 +0200744 tmp = sbus_readl(dbri->regs + REG0);
Martin Habets43388292005-09-10 15:39:00 +0200745 tmp |= D_G | D_E;
746 tmp &= ~D_S;
Takashi Iwai1bd9deb2005-06-30 18:26:20 +0200747 sbus_writel(tmp, dbri->regs + REG0);
748
749 /*
Krzysztof Helt6fb98282006-08-16 12:54:29 +0200750 * Initialize the interrupt ringbuffer.
Takashi Iwai1bd9deb2005-06-30 18:26:20 +0200751 */
752 dma_addr = dbri->dma_dvma + dbri_dma_off(intr, 0);
Krzysztof Helt6fb98282006-08-16 12:54:29 +0200753 dbri->dma->intr[0] = dma_addr;
754 dbri->dbri_irqp = 1;
755 /*
756 * Set up the interrupt queue
757 */
Takashi Iwai1bd9deb2005-06-30 18:26:20 +0200758 *(cmd++) = DBRI_CMD(D_IIQ, 0, 0);
759 *(cmd++) = dma_addr;
760
761 dbri_cmdsend(dbri, cmd);
762 spin_unlock_irqrestore(&dbri->lock, flags);
763}
764
765/*
766****************************************************************************
767************************** DBRI data pipe management ***********************
768****************************************************************************
769
770While DBRI control functions use the command and interrupt buffers, the
771main data path takes the form of data pipes, which can be short (command
772and interrupt driven), or long (attached to DMA buffers). These functions
773provide a rudimentary means of setting up and managing the DBRI's pipes,
774but the calling functions have to make sure they respect the pipes' linked
775list ordering, among other things. The transmit and receive functions
776here interface closely with the transmit and receive interrupt code.
777
778*/
Takashi Iwai475675d2005-11-17 15:11:51 +0100779static int pipe_active(struct snd_dbri * dbri, int pipe)
Takashi Iwai1bd9deb2005-06-30 18:26:20 +0200780{
781 return ((pipe >= 0) && (dbri->pipes[pipe].desc != -1));
782}
783
784/* reset_pipe(dbri, pipe)
785 *
786 * Called on an in-use pipe to clear anything being transmitted or received
787 * Lock must be held before calling this.
788 */
Takashi Iwai475675d2005-11-17 15:11:51 +0100789static void reset_pipe(struct snd_dbri * dbri, int pipe)
Takashi Iwai1bd9deb2005-06-30 18:26:20 +0200790{
791 int sdp;
792 int desc;
793 volatile int *cmd;
794
795 if (pipe < 0 || pipe > 31) {
Martin Habets43388292005-09-10 15:39:00 +0200796 printk(KERN_ERR "DBRI: reset_pipe called with illegal pipe number\n");
Takashi Iwai1bd9deb2005-06-30 18:26:20 +0200797 return;
798 }
799
800 sdp = dbri->pipes[pipe].sdp;
801 if (sdp == 0) {
Martin Habets43388292005-09-10 15:39:00 +0200802 printk(KERN_ERR "DBRI: reset_pipe called on uninitialized pipe\n");
Takashi Iwai1bd9deb2005-06-30 18:26:20 +0200803 return;
804 }
805
806 cmd = dbri_cmdlock(dbri, NoGetLock);
807 *(cmd++) = DBRI_CMD(D_SDP, 0, sdp | D_SDP_C | D_SDP_P);
808 *(cmd++) = 0;
809 dbri_cmdsend(dbri, cmd);
810
811 desc = dbri->pipes[pipe].first_desc;
812 while (desc != -1) {
813 dbri->descs[desc].inuse = 0;
814 desc = dbri->descs[desc].next;
815 }
816
817 dbri->pipes[pipe].desc = -1;
818 dbri->pipes[pipe].first_desc = -1;
819}
820
821/* FIXME: direction as an argument? */
Takashi Iwai475675d2005-11-17 15:11:51 +0100822static void setup_pipe(struct snd_dbri * dbri, int pipe, int sdp)
Takashi Iwai1bd9deb2005-06-30 18:26:20 +0200823{
824 if (pipe < 0 || pipe > 31) {
Martin Habets43388292005-09-10 15:39:00 +0200825 printk(KERN_ERR "DBRI: setup_pipe called with illegal pipe number\n");
Takashi Iwai1bd9deb2005-06-30 18:26:20 +0200826 return;
827 }
828
829 if ((sdp & 0xf800) != sdp) {
Martin Habets43388292005-09-10 15:39:00 +0200830 printk(KERN_ERR "DBRI: setup_pipe called with strange SDP value\n");
Takashi Iwai1bd9deb2005-06-30 18:26:20 +0200831 /* sdp &= 0xf800; */
832 }
833
834 /* If this is a fixed receive pipe, arrange for an interrupt
835 * every time its data changes
836 */
837 if (D_SDP_MODE(sdp) == D_SDP_FIXED && !(sdp & D_SDP_TO_SER))
838 sdp |= D_SDP_CHANGE;
839
840 sdp |= D_PIPE(pipe);
841 dbri->pipes[pipe].sdp = sdp;
842 dbri->pipes[pipe].desc = -1;
843 dbri->pipes[pipe].first_desc = -1;
844 if (sdp & D_SDP_TO_SER)
845 dbri->pipes[pipe].direction = PIPEoutput;
846 else
847 dbri->pipes[pipe].direction = PIPEinput;
848
849 reset_pipe(dbri, pipe);
850}
851
852/* FIXME: direction not needed */
Takashi Iwai475675d2005-11-17 15:11:51 +0100853static void link_time_slot(struct snd_dbri * dbri, int pipe,
Takashi Iwai1bd9deb2005-06-30 18:26:20 +0200854 enum in_or_out direction, int basepipe,
855 int length, int cycle)
856{
857 volatile s32 *cmd;
858 int val;
859 int prevpipe;
860 int nextpipe;
861
862 if (pipe < 0 || pipe > 31 || basepipe < 0 || basepipe > 31) {
Martin Habets43388292005-09-10 15:39:00 +0200863 printk(KERN_ERR
864 "DBRI: link_time_slot called with illegal pipe number\n");
Takashi Iwai1bd9deb2005-06-30 18:26:20 +0200865 return;
866 }
867
868 if (dbri->pipes[pipe].sdp == 0 || dbri->pipes[basepipe].sdp == 0) {
Martin Habets43388292005-09-10 15:39:00 +0200869 printk(KERN_ERR "DBRI: link_time_slot called on uninitialized pipe\n");
Takashi Iwai1bd9deb2005-06-30 18:26:20 +0200870 return;
871 }
872
873 /* Deal with CHI special case:
874 * "If transmission on edges 0 or 1 is desired, then cycle n
875 * (where n = # of bit times per frame...) must be used."
876 * - DBRI data sheet, page 11
877 */
878 if (basepipe == 16 && direction == PIPEoutput && cycle == 0)
879 cycle = dbri->chi_bpf;
880
881 if (basepipe == pipe) {
882 prevpipe = pipe;
883 nextpipe = pipe;
884 } else {
885 /* We're not initializing a new linked list (basepipe != pipe),
886 * so run through the linked list and find where this pipe
887 * should be sloted in, based on its cycle. CHI confuses
888 * things a bit, since it has a single anchor for both its
889 * transmit and receive lists.
890 */
891 if (basepipe == 16) {
892 if (direction == PIPEinput) {
893 prevpipe = dbri->chi_in_pipe;
894 } else {
895 prevpipe = dbri->chi_out_pipe;
896 }
897 } else {
898 prevpipe = basepipe;
899 }
900
901 nextpipe = dbri->pipes[prevpipe].nextpipe;
902
903 while (dbri->pipes[nextpipe].cycle < cycle
904 && dbri->pipes[nextpipe].nextpipe != basepipe) {
905 prevpipe = nextpipe;
906 nextpipe = dbri->pipes[nextpipe].nextpipe;
907 }
908 }
909
910 if (prevpipe == 16) {
911 if (direction == PIPEinput) {
912 dbri->chi_in_pipe = pipe;
913 } else {
914 dbri->chi_out_pipe = pipe;
915 }
916 } else {
917 dbri->pipes[prevpipe].nextpipe = pipe;
918 }
919
920 dbri->pipes[pipe].nextpipe = nextpipe;
921 dbri->pipes[pipe].cycle = cycle;
922 dbri->pipes[pipe].length = length;
923
924 cmd = dbri_cmdlock(dbri, NoGetLock);
925
926 if (direction == PIPEinput) {
927 val = D_DTS_VI | D_DTS_INS | D_DTS_PRVIN(prevpipe) | pipe;
928 *(cmd++) = DBRI_CMD(D_DTS, 0, val);
929 *(cmd++) =
930 D_TS_LEN(length) | D_TS_CYCLE(cycle) | D_TS_NEXT(nextpipe);
931 *(cmd++) = 0;
932 } else {
933 val = D_DTS_VO | D_DTS_INS | D_DTS_PRVOUT(prevpipe) | pipe;
934 *(cmd++) = DBRI_CMD(D_DTS, 0, val);
935 *(cmd++) = 0;
936 *(cmd++) =
937 D_TS_LEN(length) | D_TS_CYCLE(cycle) | D_TS_NEXT(nextpipe);
938 }
939
940 dbri_cmdsend(dbri, cmd);
941}
942
Takashi Iwai475675d2005-11-17 15:11:51 +0100943static void unlink_time_slot(struct snd_dbri * dbri, int pipe,
Takashi Iwai1bd9deb2005-06-30 18:26:20 +0200944 enum in_or_out direction, int prevpipe,
945 int nextpipe)
946{
947 volatile s32 *cmd;
948 int val;
949
950 if (pipe < 0 || pipe > 31 || prevpipe < 0 || prevpipe > 31) {
Martin Habets43388292005-09-10 15:39:00 +0200951 printk(KERN_ERR
952 "DBRI: unlink_time_slot called with illegal pipe number\n");
Takashi Iwai1bd9deb2005-06-30 18:26:20 +0200953 return;
954 }
955
956 cmd = dbri_cmdlock(dbri, NoGetLock);
957
958 if (direction == PIPEinput) {
959 val = D_DTS_VI | D_DTS_DEL | D_DTS_PRVIN(prevpipe) | pipe;
960 *(cmd++) = DBRI_CMD(D_DTS, 0, val);
961 *(cmd++) = D_TS_NEXT(nextpipe);
962 *(cmd++) = 0;
963 } else {
964 val = D_DTS_VO | D_DTS_DEL | D_DTS_PRVOUT(prevpipe) | pipe;
965 *(cmd++) = DBRI_CMD(D_DTS, 0, val);
966 *(cmd++) = 0;
967 *(cmd++) = D_TS_NEXT(nextpipe);
968 }
969
970 dbri_cmdsend(dbri, cmd);
971}
972
973/* xmit_fixed() / recv_fixed()
974 *
975 * Transmit/receive data on a "fixed" pipe - i.e, one whose contents are not
976 * expected to change much, and which we don't need to buffer.
977 * The DBRI only interrupts us when the data changes (receive pipes),
978 * or only changes the data when this function is called (transmit pipes).
979 * Only short pipes (numbers 16-31) can be used in fixed data mode.
980 *
981 * These function operate on a 32-bit field, no matter how large
982 * the actual time slot is. The interrupt handler takes care of bit
983 * ordering and alignment. An 8-bit time slot will always end up
984 * in the low-order 8 bits, filled either MSB-first or LSB-first,
985 * depending on the settings passed to setup_pipe()
986 */
Takashi Iwai475675d2005-11-17 15:11:51 +0100987static void xmit_fixed(struct snd_dbri * dbri, int pipe, unsigned int data)
Takashi Iwai1bd9deb2005-06-30 18:26:20 +0200988{
989 volatile s32 *cmd;
990
991 if (pipe < 16 || pipe > 31) {
Martin Habets43388292005-09-10 15:39:00 +0200992 printk(KERN_ERR "DBRI: xmit_fixed: Illegal pipe number\n");
Takashi Iwai1bd9deb2005-06-30 18:26:20 +0200993 return;
994 }
995
996 if (D_SDP_MODE(dbri->pipes[pipe].sdp) == 0) {
Martin Habets43388292005-09-10 15:39:00 +0200997 printk(KERN_ERR "DBRI: xmit_fixed: Uninitialized pipe %d\n", pipe);
Takashi Iwai1bd9deb2005-06-30 18:26:20 +0200998 return;
999 }
1000
1001 if (D_SDP_MODE(dbri->pipes[pipe].sdp) != D_SDP_FIXED) {
Martin Habets43388292005-09-10 15:39:00 +02001002 printk(KERN_ERR "DBRI: xmit_fixed: Non-fixed pipe %d\n", pipe);
Takashi Iwai1bd9deb2005-06-30 18:26:20 +02001003 return;
1004 }
1005
1006 if (!(dbri->pipes[pipe].sdp & D_SDP_TO_SER)) {
Martin Habets43388292005-09-10 15:39:00 +02001007 printk(KERN_ERR "DBRI: xmit_fixed: Called on receive pipe %d\n", pipe);
Takashi Iwai1bd9deb2005-06-30 18:26:20 +02001008 return;
1009 }
1010
1011 /* DBRI short pipes always transmit LSB first */
1012
1013 if (dbri->pipes[pipe].sdp & D_SDP_MSB)
1014 data = reverse_bytes(data, dbri->pipes[pipe].length);
1015
1016 cmd = dbri_cmdlock(dbri, GetLock);
1017
1018 *(cmd++) = DBRI_CMD(D_SSP, 0, pipe);
1019 *(cmd++) = data;
1020
1021 dbri_cmdsend(dbri, cmd);
1022}
1023
Takashi Iwai475675d2005-11-17 15:11:51 +01001024static void recv_fixed(struct snd_dbri * dbri, int pipe, volatile __u32 * ptr)
Takashi Iwai1bd9deb2005-06-30 18:26:20 +02001025{
1026 if (pipe < 16 || pipe > 31) {
Martin Habets43388292005-09-10 15:39:00 +02001027 printk(KERN_ERR "DBRI: recv_fixed called with illegal pipe number\n");
Takashi Iwai1bd9deb2005-06-30 18:26:20 +02001028 return;
1029 }
1030
1031 if (D_SDP_MODE(dbri->pipes[pipe].sdp) != D_SDP_FIXED) {
Martin Habets43388292005-09-10 15:39:00 +02001032 printk(KERN_ERR "DBRI: recv_fixed called on non-fixed pipe %d\n", pipe);
Takashi Iwai1bd9deb2005-06-30 18:26:20 +02001033 return;
1034 }
1035
1036 if (dbri->pipes[pipe].sdp & D_SDP_TO_SER) {
Martin Habets43388292005-09-10 15:39:00 +02001037 printk(KERN_ERR "DBRI: recv_fixed called on transmit pipe %d\n", pipe);
Takashi Iwai1bd9deb2005-06-30 18:26:20 +02001038 return;
1039 }
1040
1041 dbri->pipes[pipe].recv_fixed_ptr = ptr;
1042}
1043
1044/* setup_descs()
1045 *
1046 * Setup transmit/receive data on a "long" pipe - i.e, one associated
1047 * with a DMA buffer.
1048 *
1049 * Only pipe numbers 0-15 can be used in this mode.
1050 *
1051 * This function takes a stream number pointing to a data buffer,
1052 * and work by building chains of descriptors which identify the
1053 * data buffers. Buffers too large for a single descriptor will
1054 * be spread across multiple descriptors.
1055 */
Takashi Iwai475675d2005-11-17 15:11:51 +01001056static int setup_descs(struct snd_dbri * dbri, int streamno, unsigned int period)
Takashi Iwai1bd9deb2005-06-30 18:26:20 +02001057{
Takashi Iwai475675d2005-11-17 15:11:51 +01001058 struct dbri_streaminfo *info = &dbri->stream_info[streamno];
Takashi Iwai1bd9deb2005-06-30 18:26:20 +02001059 __u32 dvma_buffer;
1060 int desc = 0;
1061 int len;
1062 int first_desc = -1;
1063 int last_desc = -1;
1064
1065 if (info->pipe < 0 || info->pipe > 15) {
Martin Habets43388292005-09-10 15:39:00 +02001066 printk(KERN_ERR "DBRI: setup_descs: Illegal pipe number\n");
Takashi Iwai1bd9deb2005-06-30 18:26:20 +02001067 return -2;
1068 }
1069
1070 if (dbri->pipes[info->pipe].sdp == 0) {
Martin Habets43388292005-09-10 15:39:00 +02001071 printk(KERN_ERR "DBRI: setup_descs: Uninitialized pipe %d\n",
Takashi Iwai1bd9deb2005-06-30 18:26:20 +02001072 info->pipe);
1073 return -2;
1074 }
1075
1076 dvma_buffer = info->dvma_buffer;
1077 len = info->size;
1078
1079 if (streamno == DBRI_PLAY) {
1080 if (!(dbri->pipes[info->pipe].sdp & D_SDP_TO_SER)) {
Martin Habets43388292005-09-10 15:39:00 +02001081 printk(KERN_ERR "DBRI: setup_descs: Called on receive pipe %d\n",
Takashi Iwai1bd9deb2005-06-30 18:26:20 +02001082 info->pipe);
1083 return -2;
1084 }
1085 } else {
1086 if (dbri->pipes[info->pipe].sdp & D_SDP_TO_SER) {
Martin Habets43388292005-09-10 15:39:00 +02001087 printk(KERN_ERR
1088 "DBRI: setup_descs: Called on transmit pipe %d\n",
Takashi Iwai1bd9deb2005-06-30 18:26:20 +02001089 info->pipe);
1090 return -2;
1091 }
1092 /* Should be able to queue multiple buffers to receive on a pipe */
1093 if (pipe_active(dbri, info->pipe)) {
Martin Habets43388292005-09-10 15:39:00 +02001094 printk(KERN_ERR "DBRI: recv_on_pipe: Called on active pipe %d\n",
Takashi Iwai1bd9deb2005-06-30 18:26:20 +02001095 info->pipe);
1096 return -2;
1097 }
1098
1099 /* Make sure buffer size is multiple of four */
1100 len &= ~3;
1101 }
1102
1103 while (len > 0) {
1104 int mylen;
1105
1106 for (; desc < DBRI_NO_DESCS; desc++) {
1107 if (!dbri->descs[desc].inuse)
1108 break;
1109 }
1110 if (desc == DBRI_NO_DESCS) {
Martin Habets43388292005-09-10 15:39:00 +02001111 printk(KERN_ERR "DBRI: setup_descs: No descriptors\n");
Takashi Iwai1bd9deb2005-06-30 18:26:20 +02001112 return -1;
1113 }
1114
1115 if (len > DBRI_TD_MAXCNT) {
1116 mylen = DBRI_TD_MAXCNT; /* 8KB - 1 */
1117 } else {
1118 mylen = len;
1119 }
1120 if (mylen > period) {
1121 mylen = period;
1122 }
1123
1124 dbri->descs[desc].inuse = 1;
1125 dbri->descs[desc].next = -1;
1126 dbri->dma->desc[desc].ba = dvma_buffer;
1127 dbri->dma->desc[desc].nda = 0;
1128
1129 if (streamno == DBRI_PLAY) {
1130 dbri->descs[desc].len = mylen;
1131 dbri->dma->desc[desc].word1 = DBRI_TD_CNT(mylen);
1132 dbri->dma->desc[desc].word4 = 0;
1133 if (first_desc != -1)
1134 dbri->dma->desc[desc].word1 |= DBRI_TD_M;
1135 } else {
1136 dbri->descs[desc].len = 0;
1137 dbri->dma->desc[desc].word1 = 0;
1138 dbri->dma->desc[desc].word4 =
1139 DBRI_RD_B | DBRI_RD_BCNT(mylen);
1140 }
1141
1142 if (first_desc == -1) {
1143 first_desc = desc;
1144 } else {
1145 dbri->descs[last_desc].next = desc;
1146 dbri->dma->desc[last_desc].nda =
1147 dbri->dma_dvma + dbri_dma_off(desc, desc);
1148 }
1149
1150 last_desc = desc;
1151 dvma_buffer += mylen;
1152 len -= mylen;
1153 }
1154
1155 if (first_desc == -1 || last_desc == -1) {
Martin Habets43388292005-09-10 15:39:00 +02001156 printk(KERN_ERR "DBRI: setup_descs: Not enough descriptors available\n");
Takashi Iwai1bd9deb2005-06-30 18:26:20 +02001157 return -1;
1158 }
1159
1160 dbri->dma->desc[last_desc].word1 &= ~DBRI_TD_M;
1161 if (streamno == DBRI_PLAY) {
1162 dbri->dma->desc[last_desc].word1 |=
1163 DBRI_TD_I | DBRI_TD_F | DBRI_TD_B;
1164 }
1165 dbri->pipes[info->pipe].first_desc = first_desc;
1166 dbri->pipes[info->pipe].desc = first_desc;
1167
1168 for (desc = first_desc; desc != -1; desc = dbri->descs[desc].next) {
1169 dprintk(D_DESC, "DESC %d: %08x %08x %08x %08x\n",
1170 desc,
1171 dbri->dma->desc[desc].word1,
1172 dbri->dma->desc[desc].ba,
1173 dbri->dma->desc[desc].nda, dbri->dma->desc[desc].word4);
1174 }
1175 return 0;
1176}
1177
1178/*
1179****************************************************************************
1180************************** DBRI - CHI interface ****************************
1181****************************************************************************
1182
1183The CHI is a four-wire (clock, frame sync, data in, data out) time-division
1184multiplexed serial interface which the DBRI can operate in either master
1185(give clock/frame sync) or slave (take clock/frame sync) mode.
1186
1187*/
1188
1189enum master_or_slave { CHImaster, CHIslave };
1190
Takashi Iwai475675d2005-11-17 15:11:51 +01001191static void reset_chi(struct snd_dbri * dbri, enum master_or_slave master_or_slave,
Takashi Iwai1bd9deb2005-06-30 18:26:20 +02001192 int bits_per_frame)
1193{
1194 volatile s32 *cmd;
1195 int val;
1196 static int chi_initialized = 0; /* FIXME: mutex? */
1197
1198 if (!chi_initialized) {
1199
1200 cmd = dbri_cmdlock(dbri, GetLock);
1201
1202 /* Set CHI Anchor: Pipe 16 */
1203
1204 val = D_DTS_VI | D_DTS_INS | D_DTS_PRVIN(16) | D_PIPE(16);
1205 *(cmd++) = DBRI_CMD(D_DTS, 0, val);
1206 *(cmd++) = D_TS_ANCHOR | D_TS_NEXT(16);
1207 *(cmd++) = 0;
1208
1209 val = D_DTS_VO | D_DTS_INS | D_DTS_PRVOUT(16) | D_PIPE(16);
1210 *(cmd++) = DBRI_CMD(D_DTS, 0, val);
1211 *(cmd++) = 0;
1212 *(cmd++) = D_TS_ANCHOR | D_TS_NEXT(16);
1213
1214 dbri->pipes[16].sdp = 1;
1215 dbri->pipes[16].nextpipe = 16;
1216 dbri->chi_in_pipe = 16;
1217 dbri->chi_out_pipe = 16;
1218
1219#if 0
1220 chi_initialized++;
1221#endif
1222 } else {
1223 int pipe;
1224
1225 for (pipe = dbri->chi_in_pipe;
1226 pipe != 16; pipe = dbri->pipes[pipe].nextpipe) {
1227 unlink_time_slot(dbri, pipe, PIPEinput,
1228 16, dbri->pipes[pipe].nextpipe);
1229 }
1230 for (pipe = dbri->chi_out_pipe;
1231 pipe != 16; pipe = dbri->pipes[pipe].nextpipe) {
1232 unlink_time_slot(dbri, pipe, PIPEoutput,
1233 16, dbri->pipes[pipe].nextpipe);
1234 }
1235
1236 dbri->chi_in_pipe = 16;
1237 dbri->chi_out_pipe = 16;
1238
1239 cmd = dbri_cmdlock(dbri, GetLock);
1240 }
1241
1242 if (master_or_slave == CHIslave) {
1243 /* Setup DBRI for CHI Slave - receive clock, frame sync (FS)
1244 *
1245 * CHICM = 0 (slave mode, 8 kHz frame rate)
1246 * IR = give immediate CHI status interrupt
1247 * EN = give CHI status interrupt upon change
1248 */
1249 *(cmd++) = DBRI_CMD(D_CHI, 0, D_CHI_CHICM(0));
1250 } else {
1251 /* Setup DBRI for CHI Master - generate clock, FS
1252 *
1253 * BPF = bits per 8 kHz frame
1254 * 12.288 MHz / CHICM_divisor = clock rate
1255 * FD = 1 - drive CHIFS on rising edge of CHICK
1256 */
1257 int clockrate = bits_per_frame * 8;
1258 int divisor = 12288 / clockrate;
1259
1260 if (divisor > 255 || divisor * clockrate != 12288)
Martin Habets43388292005-09-10 15:39:00 +02001261 printk(KERN_ERR "DBRI: illegal bits_per_frame in setup_chi\n");
Takashi Iwai1bd9deb2005-06-30 18:26:20 +02001262
1263 *(cmd++) = DBRI_CMD(D_CHI, 0, D_CHI_CHICM(divisor) | D_CHI_FD
1264 | D_CHI_BPF(bits_per_frame));
1265 }
1266
1267 dbri->chi_bpf = bits_per_frame;
1268
1269 /* CHI Data Mode
1270 *
1271 * RCE = 0 - receive on falling edge of CHICK
1272 * XCE = 1 - transmit on rising edge of CHICK
1273 * XEN = 1 - enable transmitter
1274 * REN = 1 - enable receiver
1275 */
1276
1277 *(cmd++) = DBRI_CMD(D_PAUSE, 0, 0);
1278 *(cmd++) = DBRI_CMD(D_CDM, 0, D_CDM_XCE | D_CDM_XEN | D_CDM_REN);
1279
1280 dbri_cmdsend(dbri, cmd);
1281}
1282
1283/*
1284****************************************************************************
1285*********************** CS4215 audio codec management **********************
1286****************************************************************************
1287
1288In the standard SPARC audio configuration, the CS4215 codec is attached
1289to the DBRI via the CHI interface and few of the DBRI's PIO pins.
1290
1291*/
Takashi Iwai475675d2005-11-17 15:11:51 +01001292static void cs4215_setup_pipes(struct snd_dbri * dbri)
Takashi Iwai1bd9deb2005-06-30 18:26:20 +02001293{
1294 /*
1295 * Data mode:
1296 * Pipe 4: Send timeslots 1-4 (audio data)
1297 * Pipe 20: Send timeslots 5-8 (part of ctrl data)
1298 * Pipe 6: Receive timeslots 1-4 (audio data)
1299 * Pipe 21: Receive timeslots 6-7. We can only receive 20 bits via
1300 * interrupt, and the rest of the data (slot 5 and 8) is
1301 * not relevant for us (only for doublechecking).
1302 *
1303 * Control mode:
1304 * Pipe 17: Send timeslots 1-4 (slots 5-8 are readonly)
1305 * Pipe 18: Receive timeslot 1 (clb).
1306 * Pipe 19: Receive timeslot 7 (version).
1307 */
1308
1309 setup_pipe(dbri, 4, D_SDP_MEM | D_SDP_TO_SER | D_SDP_MSB);
1310 setup_pipe(dbri, 20, D_SDP_FIXED | D_SDP_TO_SER | D_SDP_MSB);
1311 setup_pipe(dbri, 6, D_SDP_MEM | D_SDP_FROM_SER | D_SDP_MSB);
1312 setup_pipe(dbri, 21, D_SDP_FIXED | D_SDP_FROM_SER | D_SDP_MSB);
1313
1314 setup_pipe(dbri, 17, D_SDP_FIXED | D_SDP_TO_SER | D_SDP_MSB);
1315 setup_pipe(dbri, 18, D_SDP_FIXED | D_SDP_FROM_SER | D_SDP_MSB);
1316 setup_pipe(dbri, 19, D_SDP_FIXED | D_SDP_FROM_SER | D_SDP_MSB);
1317}
1318
1319static int cs4215_init_data(struct cs4215 *mm)
1320{
1321 /*
1322 * No action, memory resetting only.
1323 *
1324 * Data Time Slot 5-8
1325 * Speaker,Line and Headphone enable. Gain set to the half.
1326 * Input is mike.
1327 */
1328 mm->data[0] = CS4215_LO(0x20) | CS4215_HE | CS4215_LE;
1329 mm->data[1] = CS4215_RO(0x20) | CS4215_SE;
1330 mm->data[2] = CS4215_LG(0x8) | CS4215_IS | CS4215_PIO0 | CS4215_PIO1;
1331 mm->data[3] = CS4215_RG(0x8) | CS4215_MA(0xf);
1332
1333 /*
1334 * Control Time Slot 1-4
1335 * 0: Default I/O voltage scale
1336 * 1: 8 bit ulaw, 8kHz, mono, high pass filter disabled
1337 * 2: Serial enable, CHI master, 128 bits per frame, clock 1
1338 * 3: Tests disabled
1339 */
1340 mm->ctrl[0] = CS4215_RSRVD_1 | CS4215_MLB;
1341 mm->ctrl[1] = CS4215_DFR_ULAW | CS4215_FREQ[0].csval;
1342 mm->ctrl[2] = CS4215_XCLK | CS4215_BSEL_128 | CS4215_FREQ[0].xtal;
1343 mm->ctrl[3] = 0;
1344
1345 mm->status = 0;
1346 mm->version = 0xff;
1347 mm->precision = 8; /* For ULAW */
1348 mm->channels = 2;
1349
1350 return 0;
1351}
1352
Takashi Iwai475675d2005-11-17 15:11:51 +01001353static void cs4215_setdata(struct snd_dbri * dbri, int muted)
Takashi Iwai1bd9deb2005-06-30 18:26:20 +02001354{
1355 if (muted) {
1356 dbri->mm.data[0] |= 63;
1357 dbri->mm.data[1] |= 63;
1358 dbri->mm.data[2] &= ~15;
1359 dbri->mm.data[3] &= ~15;
1360 } else {
1361 /* Start by setting the playback attenuation. */
Takashi Iwai475675d2005-11-17 15:11:51 +01001362 struct dbri_streaminfo *info = &dbri->stream_info[DBRI_PLAY];
Takashi Iwai1bd9deb2005-06-30 18:26:20 +02001363 int left_gain = info->left_gain % 64;
1364 int right_gain = info->right_gain % 64;
1365
1366 if (info->balance < DBRI_MID_BALANCE) {
1367 right_gain *= info->balance;
1368 right_gain /= DBRI_MID_BALANCE;
1369 } else {
1370 left_gain *= DBRI_RIGHT_BALANCE - info->balance;
1371 left_gain /= DBRI_MID_BALANCE;
1372 }
1373
1374 dbri->mm.data[0] &= ~0x3f; /* Reset the volume bits */
1375 dbri->mm.data[1] &= ~0x3f;
1376 dbri->mm.data[0] |= (DBRI_MAX_VOLUME - left_gain);
1377 dbri->mm.data[1] |= (DBRI_MAX_VOLUME - right_gain);
1378
1379 /* Now set the recording gain. */
1380 info = &dbri->stream_info[DBRI_REC];
1381 left_gain = info->left_gain % 16;
1382 right_gain = info->right_gain % 16;
1383 dbri->mm.data[2] |= CS4215_LG(left_gain);
1384 dbri->mm.data[3] |= CS4215_RG(right_gain);
1385 }
1386
1387 xmit_fixed(dbri, 20, *(int *)dbri->mm.data);
1388}
1389
1390/*
1391 * Set the CS4215 to data mode.
1392 */
Takashi Iwai475675d2005-11-17 15:11:51 +01001393static void cs4215_open(struct snd_dbri * dbri)
Takashi Iwai1bd9deb2005-06-30 18:26:20 +02001394{
1395 int data_width;
1396 u32 tmp;
1397
1398 dprintk(D_MM, "cs4215_open: %d channels, %d bits\n",
1399 dbri->mm.channels, dbri->mm.precision);
1400
1401 /* Temporarily mute outputs, and wait 1/8000 sec (125 us)
1402 * to make sure this takes. This avoids clicking noises.
1403 */
1404
1405 cs4215_setdata(dbri, 1);
1406 udelay(125);
1407
1408 /*
1409 * Data mode:
1410 * Pipe 4: Send timeslots 1-4 (audio data)
1411 * Pipe 20: Send timeslots 5-8 (part of ctrl data)
1412 * Pipe 6: Receive timeslots 1-4 (audio data)
1413 * Pipe 21: Receive timeslots 6-7. We can only receive 20 bits via
1414 * interrupt, and the rest of the data (slot 5 and 8) is
1415 * not relevant for us (only for doublechecking).
1416 *
1417 * Just like in control mode, the time slots are all offset by eight
1418 * bits. The CS4215, it seems, observes TSIN (the delayed signal)
1419 * even if it's the CHI master. Don't ask me...
1420 */
1421 tmp = sbus_readl(dbri->regs + REG0);
1422 tmp &= ~(D_C); /* Disable CHI */
1423 sbus_writel(tmp, dbri->regs + REG0);
1424
1425 /* Switch CS4215 to data mode - set PIO3 to 1 */
1426 sbus_writel(D_ENPIO | D_PIO1 | D_PIO3 |
1427 (dbri->mm.onboard ? D_PIO0 : D_PIO2), dbri->regs + REG2);
1428
1429 reset_chi(dbri, CHIslave, 128);
1430
1431 /* Note: this next doesn't work for 8-bit stereo, because the two
1432 * channels would be on timeslots 1 and 3, with 2 and 4 idle.
1433 * (See CS4215 datasheet Fig 15)
1434 *
1435 * DBRI non-contiguous mode would be required to make this work.
1436 */
1437 data_width = dbri->mm.channels * dbri->mm.precision;
1438
1439 link_time_slot(dbri, 20, PIPEoutput, 16, 32, dbri->mm.offset + 32);
1440 link_time_slot(dbri, 4, PIPEoutput, 16, data_width, dbri->mm.offset);
1441 link_time_slot(dbri, 6, PIPEinput, 16, data_width, dbri->mm.offset);
1442 link_time_slot(dbri, 21, PIPEinput, 16, 16, dbri->mm.offset + 40);
1443
1444 /* FIXME: enable CHI after _setdata? */
1445 tmp = sbus_readl(dbri->regs + REG0);
1446 tmp |= D_C; /* Enable CHI */
1447 sbus_writel(tmp, dbri->regs + REG0);
1448
1449 cs4215_setdata(dbri, 0);
1450}
1451
1452/*
1453 * Send the control information (i.e. audio format)
1454 */
Takashi Iwai475675d2005-11-17 15:11:51 +01001455static int cs4215_setctrl(struct snd_dbri * dbri)
Takashi Iwai1bd9deb2005-06-30 18:26:20 +02001456{
1457 int i, val;
1458 u32 tmp;
1459
1460 /* FIXME - let the CPU do something useful during these delays */
1461
1462 /* Temporarily mute outputs, and wait 1/8000 sec (125 us)
1463 * to make sure this takes. This avoids clicking noises.
1464 */
Takashi Iwai1bd9deb2005-06-30 18:26:20 +02001465 cs4215_setdata(dbri, 1);
1466 udelay(125);
1467
1468 /*
1469 * Enable Control mode: Set DBRI's PIO3 (4215's D/~C) to 0, then wait
1470 * 12 cycles <= 12/(5512.5*64) sec = 34.01 usec
1471 */
1472 val = D_ENPIO | D_PIO1 | (dbri->mm.onboard ? D_PIO0 : D_PIO2);
1473 sbus_writel(val, dbri->regs + REG2);
1474 dprintk(D_MM, "cs4215_setctrl: reg2=0x%x\n", val);
1475 udelay(34);
1476
1477 /* In Control mode, the CS4215 is a slave device, so the DBRI must
1478 * operate as CHI master, supplying clocking and frame synchronization.
1479 *
1480 * In Data mode, however, the CS4215 must be CHI master to insure
1481 * that its data stream is synchronous with its codec.
1482 *
1483 * The upshot of all this? We start by putting the DBRI into master
1484 * mode, program the CS4215 in Control mode, then switch the CS4215
1485 * into Data mode and put the DBRI into slave mode. Various timing
1486 * requirements must be observed along the way.
1487 *
1488 * Oh, and one more thing, on a SPARCStation 20 (and maybe
1489 * others?), the addressing of the CS4215's time slots is
1490 * offset by eight bits, so we add eight to all the "cycle"
1491 * values in the Define Time Slot (DTS) commands. This is
1492 * done in hardware by a TI 248 that delays the DBRI->4215
1493 * frame sync signal by eight clock cycles. Anybody know why?
1494 */
1495 tmp = sbus_readl(dbri->regs + REG0);
1496 tmp &= ~D_C; /* Disable CHI */
1497 sbus_writel(tmp, dbri->regs + REG0);
1498
1499 reset_chi(dbri, CHImaster, 128);
1500
1501 /*
1502 * Control mode:
1503 * Pipe 17: Send timeslots 1-4 (slots 5-8 are readonly)
1504 * Pipe 18: Receive timeslot 1 (clb).
1505 * Pipe 19: Receive timeslot 7 (version).
1506 */
1507
1508 link_time_slot(dbri, 17, PIPEoutput, 16, 32, dbri->mm.offset);
1509 link_time_slot(dbri, 18, PIPEinput, 16, 8, dbri->mm.offset);
1510 link_time_slot(dbri, 19, PIPEinput, 16, 8, dbri->mm.offset + 48);
1511
1512 /* Wait for the chip to echo back CLB (Control Latch Bit) as zero */
1513 dbri->mm.ctrl[0] &= ~CS4215_CLB;
1514 xmit_fixed(dbri, 17, *(int *)dbri->mm.ctrl);
1515
1516 tmp = sbus_readl(dbri->regs + REG0);
1517 tmp |= D_C; /* Enable CHI */
1518 sbus_writel(tmp, dbri->regs + REG0);
1519
Martin Habets43388292005-09-10 15:39:00 +02001520 for (i = 10; ((dbri->mm.status & 0xe4) != 0x20); --i) {
1521 msleep_interruptible(1);
Takashi Iwai1bd9deb2005-06-30 18:26:20 +02001522 }
1523 if (i == 0) {
1524 dprintk(D_MM, "CS4215 didn't respond to CLB (0x%02x)\n",
1525 dbri->mm.status);
1526 return -1;
1527 }
1528
1529 /* Disable changes to our copy of the version number, as we are about
1530 * to leave control mode.
1531 */
1532 recv_fixed(dbri, 19, NULL);
1533
1534 /* Terminate CS4215 control mode - data sheet says
1535 * "Set CLB=1 and send two more frames of valid control info"
1536 */
1537 dbri->mm.ctrl[0] |= CS4215_CLB;
1538 xmit_fixed(dbri, 17, *(int *)dbri->mm.ctrl);
1539
1540 /* Two frames of control info @ 8kHz frame rate = 250 us delay */
1541 udelay(250);
1542
1543 cs4215_setdata(dbri, 0);
1544
1545 return 0;
1546}
1547
1548/*
1549 * Setup the codec with the sampling rate, audio format and number of
1550 * channels.
1551 * As part of the process we resend the settings for the data
1552 * timeslots as well.
1553 */
Takashi Iwai475675d2005-11-17 15:11:51 +01001554static int cs4215_prepare(struct snd_dbri * dbri, unsigned int rate,
Takashi Iwai1bd9deb2005-06-30 18:26:20 +02001555 snd_pcm_format_t format, unsigned int channels)
1556{
1557 int freq_idx;
1558 int ret = 0;
1559
1560 /* Lookup index for this rate */
1561 for (freq_idx = 0; CS4215_FREQ[freq_idx].freq != 0; freq_idx++) {
1562 if (CS4215_FREQ[freq_idx].freq == rate)
1563 break;
1564 }
1565 if (CS4215_FREQ[freq_idx].freq != rate) {
1566 printk(KERN_WARNING "DBRI: Unsupported rate %d Hz\n", rate);
1567 return -1;
1568 }
1569
1570 switch (format) {
1571 case SNDRV_PCM_FORMAT_MU_LAW:
1572 dbri->mm.ctrl[1] = CS4215_DFR_ULAW;
1573 dbri->mm.precision = 8;
1574 break;
1575 case SNDRV_PCM_FORMAT_A_LAW:
1576 dbri->mm.ctrl[1] = CS4215_DFR_ALAW;
1577 dbri->mm.precision = 8;
1578 break;
1579 case SNDRV_PCM_FORMAT_U8:
1580 dbri->mm.ctrl[1] = CS4215_DFR_LINEAR8;
1581 dbri->mm.precision = 8;
1582 break;
1583 case SNDRV_PCM_FORMAT_S16_BE:
1584 dbri->mm.ctrl[1] = CS4215_DFR_LINEAR16;
1585 dbri->mm.precision = 16;
1586 break;
1587 default:
1588 printk(KERN_WARNING "DBRI: Unsupported format %d\n", format);
1589 return -1;
1590 }
1591
1592 /* Add rate parameters */
1593 dbri->mm.ctrl[1] |= CS4215_FREQ[freq_idx].csval;
1594 dbri->mm.ctrl[2] = CS4215_XCLK |
1595 CS4215_BSEL_128 | CS4215_FREQ[freq_idx].xtal;
1596
1597 dbri->mm.channels = channels;
1598 /* Stereo bit: 8 bit stereo not working yet. */
1599 if ((channels > 1) && (dbri->mm.precision == 16))
1600 dbri->mm.ctrl[1] |= CS4215_DFR_STEREO;
1601
1602 ret = cs4215_setctrl(dbri);
1603 if (ret == 0)
1604 cs4215_open(dbri); /* set codec to data mode */
1605
1606 return ret;
1607}
1608
1609/*
1610 *
1611 */
Takashi Iwai475675d2005-11-17 15:11:51 +01001612static int cs4215_init(struct snd_dbri * dbri)
Takashi Iwai1bd9deb2005-06-30 18:26:20 +02001613{
1614 u32 reg2 = sbus_readl(dbri->regs + REG2);
1615 dprintk(D_MM, "cs4215_init: reg2=0x%x\n", reg2);
1616
1617 /* Look for the cs4215 chips */
1618 if (reg2 & D_PIO2) {
1619 dprintk(D_MM, "Onboard CS4215 detected\n");
1620 dbri->mm.onboard = 1;
1621 }
1622 if (reg2 & D_PIO0) {
1623 dprintk(D_MM, "Speakerbox detected\n");
1624 dbri->mm.onboard = 0;
1625
1626 if (reg2 & D_PIO2) {
1627 printk(KERN_INFO "DBRI: Using speakerbox / "
1628 "ignoring onboard mmcodec.\n");
1629 sbus_writel(D_ENPIO2, dbri->regs + REG2);
1630 }
1631 }
1632
1633 if (!(reg2 & (D_PIO0 | D_PIO2))) {
1634 printk(KERN_ERR "DBRI: no mmcodec found.\n");
1635 return -EIO;
1636 }
1637
1638 cs4215_setup_pipes(dbri);
1639
1640 cs4215_init_data(&dbri->mm);
1641
1642 /* Enable capture of the status & version timeslots. */
1643 recv_fixed(dbri, 18, &dbri->mm.status);
1644 recv_fixed(dbri, 19, &dbri->mm.version);
1645
1646 dbri->mm.offset = dbri->mm.onboard ? 0 : 8;
1647 if (cs4215_setctrl(dbri) == -1 || dbri->mm.version == 0xff) {
1648 dprintk(D_MM, "CS4215 failed probe at offset %d\n",
1649 dbri->mm.offset);
1650 return -EIO;
1651 }
1652 dprintk(D_MM, "Found CS4215 at offset %d\n", dbri->mm.offset);
1653
1654 return 0;
1655}
1656
1657/*
1658****************************************************************************
1659*************************** DBRI interrupt handler *************************
1660****************************************************************************
1661
1662The DBRI communicates with the CPU mainly via a circular interrupt
1663buffer. When an interrupt is signaled, the CPU walks through the
1664buffer and calls dbri_process_one_interrupt() for each interrupt word.
1665Complicated interrupts are handled by dedicated functions (which
1666appear first in this file). Any pending interrupts can be serviced by
1667calling dbri_process_interrupt_buffer(), which works even if the CPU's
Martin Habets43388292005-09-10 15:39:00 +02001668interrupts are disabled. This function is used by dbri_cmdlock()
1669to make sure we're synced up with the chip before each command sequence,
Takashi Iwai1bd9deb2005-06-30 18:26:20 +02001670even if we're running cli'ed.
1671
1672*/
1673
1674/* xmit_descs()
1675 *
1676 * Transmit the current TD's for recording/playing, if needed.
1677 * For playback, ALSA has filled the DMA memory with new data (we hope).
1678 */
1679static void xmit_descs(unsigned long data)
1680{
Takashi Iwai475675d2005-11-17 15:11:51 +01001681 struct snd_dbri *dbri = (struct snd_dbri *) data;
1682 struct dbri_streaminfo *info;
Takashi Iwai1bd9deb2005-06-30 18:26:20 +02001683 volatile s32 *cmd;
1684 unsigned long flags;
1685 int first_td;
1686
1687 if (dbri == NULL)
1688 return; /* Disabled */
1689
1690 /* First check the recording stream for buffer overflow */
1691 info = &dbri->stream_info[DBRI_REC];
1692 spin_lock_irqsave(&dbri->lock, flags);
1693
1694 if ((info->left >= info->size) && (info->pipe >= 0)) {
1695 first_td = dbri->pipes[info->pipe].first_desc;
1696
1697 dprintk(D_DESC, "xmit_descs rec @ TD %d\n", first_td);
1698
1699 /* Stream could be closed by the time we run. */
1700 if (first_td < 0) {
1701 goto play;
1702 }
1703
1704 cmd = dbri_cmdlock(dbri, NoGetLock);
1705 *(cmd++) = DBRI_CMD(D_SDP, 0,
1706 dbri->pipes[info->pipe].sdp
1707 | D_SDP_P | D_SDP_EVERY | D_SDP_C);
1708 *(cmd++) = dbri->dma_dvma + dbri_dma_off(desc, first_td);
1709 dbri_cmdsend(dbri, cmd);
1710
1711 /* Reset our admin of the pipe & bytes read. */
1712 dbri->pipes[info->pipe].desc = first_td;
1713 info->left = 0;
1714 }
1715
1716play:
1717 spin_unlock_irqrestore(&dbri->lock, flags);
1718
1719 /* Now check the playback stream for buffer underflow */
1720 info = &dbri->stream_info[DBRI_PLAY];
1721 spin_lock_irqsave(&dbri->lock, flags);
1722
1723 if ((info->left <= 0) && (info->pipe >= 0)) {
1724 first_td = dbri->pipes[info->pipe].first_desc;
1725
1726 dprintk(D_DESC, "xmit_descs play @ TD %d\n", first_td);
1727
1728 /* Stream could be closed by the time we run. */
1729 if (first_td < 0) {
1730 spin_unlock_irqrestore(&dbri->lock, flags);
1731 return;
1732 }
1733
1734 cmd = dbri_cmdlock(dbri, NoGetLock);
1735 *(cmd++) = DBRI_CMD(D_SDP, 0,
1736 dbri->pipes[info->pipe].sdp
1737 | D_SDP_P | D_SDP_EVERY | D_SDP_C);
1738 *(cmd++) = dbri->dma_dvma + dbri_dma_off(desc, first_td);
1739 dbri_cmdsend(dbri, cmd);
1740
1741 /* Reset our admin of the pipe & bytes written. */
1742 dbri->pipes[info->pipe].desc = first_td;
1743 info->left = info->size;
1744 }
1745 spin_unlock_irqrestore(&dbri->lock, flags);
1746}
1747
Takashi Iwai063859c2005-10-14 17:17:02 +02001748static DECLARE_TASKLET(xmit_descs_task, xmit_descs, 0);
Takashi Iwai1bd9deb2005-06-30 18:26:20 +02001749
1750/* transmission_complete_intr()
1751 *
1752 * Called by main interrupt handler when DBRI signals transmission complete
1753 * on a pipe (interrupt triggered by the B bit in a transmit descriptor).
1754 *
Martin Habets43388292005-09-10 15:39:00 +02001755 * Walks through the pipe's list of transmit buffer descriptors and marks
1756 * them as available. Stops when the first descriptor is found without
Takashi Iwai1bd9deb2005-06-30 18:26:20 +02001757 * TBC (Transmit Buffer Complete) set, or we've run through them all.
Martin Habets43388292005-09-10 15:39:00 +02001758 *
1759 * The DMA buffers are not released, but re-used. Since the transmit buffer
1760 * descriptors are not clobbered, they can be re-submitted as is. This is
1761 * done by the xmit_descs() tasklet above since that could take longer.
Takashi Iwai1bd9deb2005-06-30 18:26:20 +02001762 */
1763
Takashi Iwai475675d2005-11-17 15:11:51 +01001764static void transmission_complete_intr(struct snd_dbri * dbri, int pipe)
Takashi Iwai1bd9deb2005-06-30 18:26:20 +02001765{
Takashi Iwai475675d2005-11-17 15:11:51 +01001766 struct dbri_streaminfo *info;
Takashi Iwai1bd9deb2005-06-30 18:26:20 +02001767 int td;
1768 int status;
1769
1770 info = &dbri->stream_info[DBRI_PLAY];
1771
1772 td = dbri->pipes[pipe].desc;
1773 while (td >= 0) {
1774 if (td >= DBRI_NO_DESCS) {
1775 printk(KERN_ERR "DBRI: invalid td on pipe %d\n", pipe);
1776 return;
1777 }
1778
1779 status = DBRI_TD_STATUS(dbri->dma->desc[td].word4);
1780 if (!(status & DBRI_TD_TBC)) {
1781 break;
1782 }
1783
1784 dprintk(D_INT, "TD %d, status 0x%02x\n", td, status);
1785
1786 dbri->dma->desc[td].word4 = 0; /* Reset it for next time. */
1787 info->offset += dbri->descs[td].len;
1788 info->left -= dbri->descs[td].len;
1789
1790 /* On the last TD, transmit them all again. */
1791 if (dbri->descs[td].next == -1) {
1792 if (info->left > 0) {
1793 printk(KERN_WARNING
1794 "%d bytes left after last transfer.\n",
1795 info->left);
1796 info->left = 0;
1797 }
1798 tasklet_schedule(&xmit_descs_task);
1799 }
1800
1801 td = dbri->descs[td].next;
1802 dbri->pipes[pipe].desc = td;
1803 }
1804
1805 /* Notify ALSA */
1806 if (spin_is_locked(&dbri->lock)) {
1807 spin_unlock(&dbri->lock);
1808 snd_pcm_period_elapsed(info->substream);
1809 spin_lock(&dbri->lock);
1810 } else
1811 snd_pcm_period_elapsed(info->substream);
1812}
1813
Takashi Iwai475675d2005-11-17 15:11:51 +01001814static void reception_complete_intr(struct snd_dbri * dbri, int pipe)
Takashi Iwai1bd9deb2005-06-30 18:26:20 +02001815{
Takashi Iwai475675d2005-11-17 15:11:51 +01001816 struct dbri_streaminfo *info;
Takashi Iwai1bd9deb2005-06-30 18:26:20 +02001817 int rd = dbri->pipes[pipe].desc;
1818 s32 status;
1819
1820 if (rd < 0 || rd >= DBRI_NO_DESCS) {
1821 printk(KERN_ERR "DBRI: invalid rd on pipe %d\n", pipe);
1822 return;
1823 }
1824
1825 dbri->descs[rd].inuse = 0;
1826 dbri->pipes[pipe].desc = dbri->descs[rd].next;
1827 status = dbri->dma->desc[rd].word1;
1828 dbri->dma->desc[rd].word1 = 0; /* Reset it for next time. */
1829
1830 info = &dbri->stream_info[DBRI_REC];
1831 info->offset += DBRI_RD_CNT(status);
1832 info->left += DBRI_RD_CNT(status);
1833
1834 /* FIXME: Check status */
1835
1836 dprintk(D_INT, "Recv RD %d, status 0x%02x, len %d\n",
1837 rd, DBRI_RD_STATUS(status), DBRI_RD_CNT(status));
1838
1839 /* On the last TD, transmit them all again. */
1840 if (dbri->descs[rd].next == -1) {
1841 if (info->left > info->size) {
1842 printk(KERN_WARNING
1843 "%d bytes recorded in %d size buffer.\n",
1844 info->left, info->size);
1845 }
1846 tasklet_schedule(&xmit_descs_task);
1847 }
1848
1849 /* Notify ALSA */
1850 if (spin_is_locked(&dbri->lock)) {
1851 spin_unlock(&dbri->lock);
1852 snd_pcm_period_elapsed(info->substream);
1853 spin_lock(&dbri->lock);
1854 } else
1855 snd_pcm_period_elapsed(info->substream);
1856}
1857
Takashi Iwai475675d2005-11-17 15:11:51 +01001858static void dbri_process_one_interrupt(struct snd_dbri * dbri, int x)
Takashi Iwai1bd9deb2005-06-30 18:26:20 +02001859{
1860 int val = D_INTR_GETVAL(x);
1861 int channel = D_INTR_GETCHAN(x);
1862 int command = D_INTR_GETCMD(x);
1863 int code = D_INTR_GETCODE(x);
1864#ifdef DBRI_DEBUG
1865 int rval = D_INTR_GETRVAL(x);
1866#endif
1867
1868 if (channel == D_INTR_CMD) {
1869 dprintk(D_CMD, "INTR: Command: %-5s Value:%d\n",
1870 cmds[command], val);
1871 } else {
1872 dprintk(D_INT, "INTR: Chan:%d Code:%d Val:%#x\n",
1873 channel, code, rval);
1874 }
1875
1876 if (channel == D_INTR_CMD && command == D_WAIT) {
Martin Habets43388292005-09-10 15:39:00 +02001877 dbri->wait_ackd = val;
1878 if (dbri->wait_send != val) {
1879 printk(KERN_ERR "Processing wait command %d when %d was send.\n",
1880 val, dbri->wait_send);
1881 }
Takashi Iwai1bd9deb2005-06-30 18:26:20 +02001882 return;
1883 }
1884
1885 switch (code) {
1886 case D_INTR_BRDY:
1887 reception_complete_intr(dbri, channel);
1888 break;
1889 case D_INTR_XCMP:
1890 case D_INTR_MINT:
1891 transmission_complete_intr(dbri, channel);
1892 break;
1893 case D_INTR_UNDR:
1894 /* UNDR - Transmission underrun
1895 * resend SDP command with clear pipe bit (C) set
1896 */
1897 {
1898 volatile s32 *cmd;
1899
1900 int pipe = channel;
1901 int td = dbri->pipes[pipe].desc;
1902
1903 dbri->dma->desc[td].word4 = 0;
1904 cmd = dbri_cmdlock(dbri, NoGetLock);
1905 *(cmd++) = DBRI_CMD(D_SDP, 0,
1906 dbri->pipes[pipe].sdp
1907 | D_SDP_P | D_SDP_C | D_SDP_2SAME);
1908 *(cmd++) = dbri->dma_dvma + dbri_dma_off(desc, td);
1909 dbri_cmdsend(dbri, cmd);
1910 }
1911 break;
1912 case D_INTR_FXDT:
1913 /* FXDT - Fixed data change */
1914 if (dbri->pipes[channel].sdp & D_SDP_MSB)
1915 val = reverse_bytes(val, dbri->pipes[channel].length);
1916
1917 if (dbri->pipes[channel].recv_fixed_ptr)
1918 *(dbri->pipes[channel].recv_fixed_ptr) = val;
1919 break;
1920 default:
1921 if (channel != D_INTR_CMD)
1922 printk(KERN_WARNING
1923 "DBRI: Ignored Interrupt: %d (0x%x)\n", code, x);
1924 }
1925}
1926
1927/* dbri_process_interrupt_buffer advances through the DBRI's interrupt
1928 * buffer until it finds a zero word (indicating nothing more to do
1929 * right now). Non-zero words require processing and are handed off
1930 * to dbri_process_one_interrupt AFTER advancing the pointer. This
1931 * order is important since we might recurse back into this function
1932 * and need to make sure the pointer has been advanced first.
1933 */
Takashi Iwai475675d2005-11-17 15:11:51 +01001934static void dbri_process_interrupt_buffer(struct snd_dbri * dbri)
Takashi Iwai1bd9deb2005-06-30 18:26:20 +02001935{
1936 s32 x;
1937
1938 while ((x = dbri->dma->intr[dbri->dbri_irqp]) != 0) {
1939 dbri->dma->intr[dbri->dbri_irqp] = 0;
1940 dbri->dbri_irqp++;
Krzysztof Helt6fb98282006-08-16 12:54:29 +02001941 if (dbri->dbri_irqp == DBRI_INT_BLK)
Takashi Iwai1bd9deb2005-06-30 18:26:20 +02001942 dbri->dbri_irqp = 1;
Takashi Iwai1bd9deb2005-06-30 18:26:20 +02001943
1944 dbri_process_one_interrupt(dbri, x);
1945 }
1946}
1947
1948static irqreturn_t snd_dbri_interrupt(int irq, void *dev_id,
1949 struct pt_regs *regs)
1950{
Takashi Iwai475675d2005-11-17 15:11:51 +01001951 struct snd_dbri *dbri = dev_id;
Takashi Iwai1bd9deb2005-06-30 18:26:20 +02001952 static int errcnt = 0;
1953 int x;
1954
1955 if (dbri == NULL)
1956 return IRQ_NONE;
1957 spin_lock(&dbri->lock);
1958
1959 /*
1960 * Read it, so the interrupt goes away.
1961 */
1962 x = sbus_readl(dbri->regs + REG1);
1963
1964 if (x & (D_MRR | D_MLE | D_LBG | D_MBE)) {
1965 u32 tmp;
1966
1967 if (x & D_MRR)
1968 printk(KERN_ERR
1969 "DBRI: Multiple Error Ack on SBus reg1=0x%x\n",
1970 x);
1971 if (x & D_MLE)
1972 printk(KERN_ERR
1973 "DBRI: Multiple Late Error on SBus reg1=0x%x\n",
1974 x);
1975 if (x & D_LBG)
1976 printk(KERN_ERR
1977 "DBRI: Lost Bus Grant on SBus reg1=0x%x\n", x);
1978 if (x & D_MBE)
1979 printk(KERN_ERR
1980 "DBRI: Burst Error on SBus reg1=0x%x\n", x);
1981
1982 /* Some of these SBus errors cause the chip's SBus circuitry
1983 * to be disabled, so just re-enable and try to keep going.
1984 *
1985 * The only one I've seen is MRR, which will be triggered
1986 * if you let a transmit pipe underrun, then try to CDP it.
1987 *
Martin Habets43388292005-09-10 15:39:00 +02001988 * If these things persist, we reset the chip.
Takashi Iwai1bd9deb2005-06-30 18:26:20 +02001989 */
1990 if ((++errcnt) % 10 == 0) {
1991 dprintk(D_INT, "Interrupt errors exceeded.\n");
1992 dbri_reset(dbri);
1993 } else {
1994 tmp = sbus_readl(dbri->regs + REG0);
1995 tmp &= ~(D_D);
1996 sbus_writel(tmp, dbri->regs + REG0);
1997 }
1998 }
1999
2000 dbri_process_interrupt_buffer(dbri);
2001
2002 /* FIXME: Write 0 into regs to ACK interrupt */
2003
2004 spin_unlock(&dbri->lock);
2005
2006 return IRQ_HANDLED;
2007}
2008
2009/****************************************************************************
2010 PCM Interface
2011****************************************************************************/
Takashi Iwai475675d2005-11-17 15:11:51 +01002012static struct snd_pcm_hardware snd_dbri_pcm_hw = {
Takashi Iwai1bd9deb2005-06-30 18:26:20 +02002013 .info = (SNDRV_PCM_INFO_MMAP |
2014 SNDRV_PCM_INFO_INTERLEAVED |
2015 SNDRV_PCM_INFO_BLOCK_TRANSFER |
2016 SNDRV_PCM_INFO_MMAP_VALID),
2017 .formats = SNDRV_PCM_FMTBIT_MU_LAW |
2018 SNDRV_PCM_FMTBIT_A_LAW |
2019 SNDRV_PCM_FMTBIT_U8 |
2020 SNDRV_PCM_FMTBIT_S16_BE,
2021 .rates = SNDRV_PCM_RATE_8000_48000,
2022 .rate_min = 8000,
2023 .rate_max = 48000,
2024 .channels_min = 1,
2025 .channels_max = 2,
2026 .buffer_bytes_max = (64 * 1024),
2027 .period_bytes_min = 1,
2028 .period_bytes_max = DBRI_TD_MAXCNT,
2029 .periods_min = 1,
2030 .periods_max = 1024,
2031};
2032
Takashi Iwai475675d2005-11-17 15:11:51 +01002033static int snd_dbri_open(struct snd_pcm_substream *substream)
Takashi Iwai1bd9deb2005-06-30 18:26:20 +02002034{
Takashi Iwai475675d2005-11-17 15:11:51 +01002035 struct snd_dbri *dbri = snd_pcm_substream_chip(substream);
2036 struct snd_pcm_runtime *runtime = substream->runtime;
2037 struct dbri_streaminfo *info = DBRI_STREAM(dbri, substream);
Takashi Iwai1bd9deb2005-06-30 18:26:20 +02002038 unsigned long flags;
2039
2040 dprintk(D_USR, "open audio output.\n");
2041 runtime->hw = snd_dbri_pcm_hw;
2042
2043 spin_lock_irqsave(&dbri->lock, flags);
2044 info->substream = substream;
2045 info->left = 0;
2046 info->offset = 0;
2047 info->dvma_buffer = 0;
2048 info->pipe = -1;
2049 spin_unlock_irqrestore(&dbri->lock, flags);
2050
2051 cs4215_open(dbri);
2052
2053 return 0;
2054}
2055
Takashi Iwai475675d2005-11-17 15:11:51 +01002056static int snd_dbri_close(struct snd_pcm_substream *substream)
Takashi Iwai1bd9deb2005-06-30 18:26:20 +02002057{
Takashi Iwai475675d2005-11-17 15:11:51 +01002058 struct snd_dbri *dbri = snd_pcm_substream_chip(substream);
2059 struct dbri_streaminfo *info = DBRI_STREAM(dbri, substream);
Takashi Iwai1bd9deb2005-06-30 18:26:20 +02002060
2061 dprintk(D_USR, "close audio output.\n");
2062 info->substream = NULL;
2063 info->left = 0;
2064 info->offset = 0;
2065
2066 return 0;
2067}
2068
Takashi Iwai475675d2005-11-17 15:11:51 +01002069static int snd_dbri_hw_params(struct snd_pcm_substream *substream,
2070 struct snd_pcm_hw_params *hw_params)
Takashi Iwai1bd9deb2005-06-30 18:26:20 +02002071{
Takashi Iwai475675d2005-11-17 15:11:51 +01002072 struct snd_pcm_runtime *runtime = substream->runtime;
2073 struct snd_dbri *dbri = snd_pcm_substream_chip(substream);
2074 struct dbri_streaminfo *info = DBRI_STREAM(dbri, substream);
Takashi Iwai1bd9deb2005-06-30 18:26:20 +02002075 int direction;
2076 int ret;
2077
2078 /* set sampling rate, audio format and number of channels */
2079 ret = cs4215_prepare(dbri, params_rate(hw_params),
2080 params_format(hw_params),
2081 params_channels(hw_params));
2082 if (ret != 0)
2083 return ret;
2084
2085 if ((ret = snd_pcm_lib_malloc_pages(substream,
2086 params_buffer_bytes(hw_params))) < 0) {
Martin Habets43388292005-09-10 15:39:00 +02002087 printk(KERN_ERR "malloc_pages failed with %d\n", ret);
Takashi Iwai1bd9deb2005-06-30 18:26:20 +02002088 return ret;
2089 }
2090
2091 /* hw_params can get called multiple times. Only map the DMA once.
2092 */
2093 if (info->dvma_buffer == 0) {
2094 if (DBRI_STREAMNO(substream) == DBRI_PLAY)
2095 direction = SBUS_DMA_TODEVICE;
2096 else
2097 direction = SBUS_DMA_FROMDEVICE;
2098
2099 info->dvma_buffer = sbus_map_single(dbri->sdev,
2100 runtime->dma_area,
2101 params_buffer_bytes(hw_params),
2102 direction);
2103 }
2104
2105 direction = params_buffer_bytes(hw_params);
2106 dprintk(D_USR, "hw_params: %d bytes, dvma=%x\n",
2107 direction, info->dvma_buffer);
2108 return 0;
2109}
2110
Takashi Iwai475675d2005-11-17 15:11:51 +01002111static int snd_dbri_hw_free(struct snd_pcm_substream *substream)
Takashi Iwai1bd9deb2005-06-30 18:26:20 +02002112{
Takashi Iwai475675d2005-11-17 15:11:51 +01002113 struct snd_dbri *dbri = snd_pcm_substream_chip(substream);
2114 struct dbri_streaminfo *info = DBRI_STREAM(dbri, substream);
Takashi Iwai1bd9deb2005-06-30 18:26:20 +02002115 int direction;
2116 dprintk(D_USR, "hw_free.\n");
2117
2118 /* hw_free can get called multiple times. Only unmap the DMA once.
2119 */
2120 if (info->dvma_buffer) {
2121 if (DBRI_STREAMNO(substream) == DBRI_PLAY)
2122 direction = SBUS_DMA_TODEVICE;
2123 else
2124 direction = SBUS_DMA_FROMDEVICE;
2125
2126 sbus_unmap_single(dbri->sdev, info->dvma_buffer,
2127 substream->runtime->buffer_size, direction);
2128 info->dvma_buffer = 0;
2129 }
2130 info->pipe = -1;
2131
2132 return snd_pcm_lib_free_pages(substream);
2133}
2134
Takashi Iwai475675d2005-11-17 15:11:51 +01002135static int snd_dbri_prepare(struct snd_pcm_substream *substream)
Takashi Iwai1bd9deb2005-06-30 18:26:20 +02002136{
Takashi Iwai475675d2005-11-17 15:11:51 +01002137 struct snd_dbri *dbri = snd_pcm_substream_chip(substream);
2138 struct dbri_streaminfo *info = DBRI_STREAM(dbri, substream);
2139 struct snd_pcm_runtime *runtime = substream->runtime;
Takashi Iwai1bd9deb2005-06-30 18:26:20 +02002140 int ret;
2141
2142 info->size = snd_pcm_lib_buffer_bytes(substream);
2143 if (DBRI_STREAMNO(substream) == DBRI_PLAY)
2144 info->pipe = 4; /* Send pipe */
2145 else {
2146 info->pipe = 6; /* Receive pipe */
2147 info->left = info->size; /* To trigger submittal */
2148 }
2149
2150 spin_lock_irq(&dbri->lock);
2151
2152 /* Setup the all the transmit/receive desciptors to cover the
2153 * whole DMA buffer.
2154 */
2155 ret = setup_descs(dbri, DBRI_STREAMNO(substream),
2156 snd_pcm_lib_period_bytes(substream));
2157
2158 runtime->stop_threshold = DBRI_TD_MAXCNT / runtime->channels;
2159
2160 spin_unlock_irq(&dbri->lock);
2161
2162 dprintk(D_USR, "prepare audio output. %d bytes\n", info->size);
2163 return ret;
2164}
2165
Takashi Iwai475675d2005-11-17 15:11:51 +01002166static int snd_dbri_trigger(struct snd_pcm_substream *substream, int cmd)
Takashi Iwai1bd9deb2005-06-30 18:26:20 +02002167{
Takashi Iwai475675d2005-11-17 15:11:51 +01002168 struct snd_dbri *dbri = snd_pcm_substream_chip(substream);
2169 struct dbri_streaminfo *info = DBRI_STREAM(dbri, substream);
Takashi Iwai1bd9deb2005-06-30 18:26:20 +02002170 int ret = 0;
2171
2172 switch (cmd) {
2173 case SNDRV_PCM_TRIGGER_START:
2174 dprintk(D_USR, "start audio, period is %d bytes\n",
2175 (int)snd_pcm_lib_period_bytes(substream));
2176 /* Enable & schedule the tasklet that re-submits the TDs. */
2177 xmit_descs_task.data = (unsigned long)dbri;
2178 tasklet_schedule(&xmit_descs_task);
2179 break;
2180 case SNDRV_PCM_TRIGGER_STOP:
2181 dprintk(D_USR, "stop audio.\n");
2182 /* Make the tasklet bail out immediately. */
2183 xmit_descs_task.data = 0;
2184 reset_pipe(dbri, info->pipe);
2185 break;
2186 default:
2187 ret = -EINVAL;
2188 }
2189
2190 return ret;
2191}
2192
Takashi Iwai475675d2005-11-17 15:11:51 +01002193static snd_pcm_uframes_t snd_dbri_pointer(struct snd_pcm_substream *substream)
Takashi Iwai1bd9deb2005-06-30 18:26:20 +02002194{
Takashi Iwai475675d2005-11-17 15:11:51 +01002195 struct snd_dbri *dbri = snd_pcm_substream_chip(substream);
2196 struct dbri_streaminfo *info = DBRI_STREAM(dbri, substream);
Takashi Iwai1bd9deb2005-06-30 18:26:20 +02002197 snd_pcm_uframes_t ret;
2198
2199 ret = bytes_to_frames(substream->runtime, info->offset)
2200 % substream->runtime->buffer_size;
2201 dprintk(D_USR, "I/O pointer: %ld frames, %d bytes left.\n",
2202 ret, info->left);
2203 return ret;
2204}
2205
Takashi Iwai475675d2005-11-17 15:11:51 +01002206static struct snd_pcm_ops snd_dbri_ops = {
Takashi Iwai1bd9deb2005-06-30 18:26:20 +02002207 .open = snd_dbri_open,
2208 .close = snd_dbri_close,
2209 .ioctl = snd_pcm_lib_ioctl,
2210 .hw_params = snd_dbri_hw_params,
2211 .hw_free = snd_dbri_hw_free,
2212 .prepare = snd_dbri_prepare,
2213 .trigger = snd_dbri_trigger,
2214 .pointer = snd_dbri_pointer,
2215};
2216
Takashi Iwai475675d2005-11-17 15:11:51 +01002217static int __devinit snd_dbri_pcm(struct snd_dbri * dbri)
Takashi Iwai1bd9deb2005-06-30 18:26:20 +02002218{
Takashi Iwai475675d2005-11-17 15:11:51 +01002219 struct snd_pcm *pcm;
Takashi Iwai1bd9deb2005-06-30 18:26:20 +02002220 int err;
2221
2222 if ((err = snd_pcm_new(dbri->card,
2223 /* ID */ "sun_dbri",
2224 /* device */ 0,
2225 /* playback count */ 1,
2226 /* capture count */ 1, &pcm)) < 0)
2227 return err;
2228 snd_assert(pcm != NULL, return -EINVAL);
2229
2230 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_dbri_ops);
2231 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_dbri_ops);
2232
2233 pcm->private_data = dbri;
2234 pcm->info_flags = 0;
2235 strcpy(pcm->name, dbri->card->shortname);
2236 dbri->pcm = pcm;
2237
2238 if ((err = snd_pcm_lib_preallocate_pages_for_all(pcm,
2239 SNDRV_DMA_TYPE_CONTINUOUS,
2240 snd_dma_continuous_data(GFP_KERNEL),
2241 64 * 1024, 64 * 1024)) < 0) {
2242 return err;
2243 }
2244
2245 return 0;
2246}
2247
2248/*****************************************************************************
2249 Mixer interface
2250*****************************************************************************/
2251
Takashi Iwai475675d2005-11-17 15:11:51 +01002252static int snd_cs4215_info_volume(struct snd_kcontrol *kcontrol,
2253 struct snd_ctl_elem_info *uinfo)
Takashi Iwai1bd9deb2005-06-30 18:26:20 +02002254{
2255 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2256 uinfo->count = 2;
2257 uinfo->value.integer.min = 0;
2258 if (kcontrol->private_value == DBRI_PLAY) {
2259 uinfo->value.integer.max = DBRI_MAX_VOLUME;
2260 } else {
2261 uinfo->value.integer.max = DBRI_MAX_GAIN;
2262 }
2263 return 0;
2264}
2265
Takashi Iwai475675d2005-11-17 15:11:51 +01002266static int snd_cs4215_get_volume(struct snd_kcontrol *kcontrol,
2267 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai1bd9deb2005-06-30 18:26:20 +02002268{
Takashi Iwai475675d2005-11-17 15:11:51 +01002269 struct snd_dbri *dbri = snd_kcontrol_chip(kcontrol);
2270 struct dbri_streaminfo *info;
Takashi Iwai1bd9deb2005-06-30 18:26:20 +02002271 snd_assert(dbri != NULL, return -EINVAL);
2272 info = &dbri->stream_info[kcontrol->private_value];
2273 snd_assert(info != NULL, return -EINVAL);
2274
2275 ucontrol->value.integer.value[0] = info->left_gain;
2276 ucontrol->value.integer.value[1] = info->right_gain;
2277 return 0;
2278}
2279
Takashi Iwai475675d2005-11-17 15:11:51 +01002280static int snd_cs4215_put_volume(struct snd_kcontrol *kcontrol,
2281 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai1bd9deb2005-06-30 18:26:20 +02002282{
Takashi Iwai475675d2005-11-17 15:11:51 +01002283 struct snd_dbri *dbri = snd_kcontrol_chip(kcontrol);
2284 struct dbri_streaminfo *info = &dbri->stream_info[kcontrol->private_value];
Takashi Iwai1bd9deb2005-06-30 18:26:20 +02002285 unsigned long flags;
2286 int changed = 0;
2287
2288 if (info->left_gain != ucontrol->value.integer.value[0]) {
2289 info->left_gain = ucontrol->value.integer.value[0];
2290 changed = 1;
2291 }
2292 if (info->right_gain != ucontrol->value.integer.value[1]) {
2293 info->right_gain = ucontrol->value.integer.value[1];
2294 changed = 1;
2295 }
2296 if (changed == 1) {
2297 /* First mute outputs, and wait 1/8000 sec (125 us)
2298 * to make sure this takes. This avoids clicking noises.
2299 */
2300 spin_lock_irqsave(&dbri->lock, flags);
2301
2302 cs4215_setdata(dbri, 1);
2303 udelay(125);
2304 cs4215_setdata(dbri, 0);
2305
2306 spin_unlock_irqrestore(&dbri->lock, flags);
2307 }
2308 return changed;
2309}
2310
Takashi Iwai475675d2005-11-17 15:11:51 +01002311static int snd_cs4215_info_single(struct snd_kcontrol *kcontrol,
2312 struct snd_ctl_elem_info *uinfo)
Takashi Iwai1bd9deb2005-06-30 18:26:20 +02002313{
2314 int mask = (kcontrol->private_value >> 16) & 0xff;
2315
2316 uinfo->type = (mask == 1) ?
2317 SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
2318 uinfo->count = 1;
2319 uinfo->value.integer.min = 0;
2320 uinfo->value.integer.max = mask;
2321 return 0;
2322}
2323
Takashi Iwai475675d2005-11-17 15:11:51 +01002324static int snd_cs4215_get_single(struct snd_kcontrol *kcontrol,
2325 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai1bd9deb2005-06-30 18:26:20 +02002326{
Takashi Iwai475675d2005-11-17 15:11:51 +01002327 struct snd_dbri *dbri = snd_kcontrol_chip(kcontrol);
Takashi Iwai1bd9deb2005-06-30 18:26:20 +02002328 int elem = kcontrol->private_value & 0xff;
2329 int shift = (kcontrol->private_value >> 8) & 0xff;
2330 int mask = (kcontrol->private_value >> 16) & 0xff;
2331 int invert = (kcontrol->private_value >> 24) & 1;
2332 snd_assert(dbri != NULL, return -EINVAL);
2333
2334 if (elem < 4) {
2335 ucontrol->value.integer.value[0] =
2336 (dbri->mm.data[elem] >> shift) & mask;
2337 } else {
2338 ucontrol->value.integer.value[0] =
2339 (dbri->mm.ctrl[elem - 4] >> shift) & mask;
2340 }
2341
2342 if (invert == 1) {
2343 ucontrol->value.integer.value[0] =
2344 mask - ucontrol->value.integer.value[0];
2345 }
2346 return 0;
2347}
2348
Takashi Iwai475675d2005-11-17 15:11:51 +01002349static int snd_cs4215_put_single(struct snd_kcontrol *kcontrol,
2350 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai1bd9deb2005-06-30 18:26:20 +02002351{
Takashi Iwai475675d2005-11-17 15:11:51 +01002352 struct snd_dbri *dbri = snd_kcontrol_chip(kcontrol);
Takashi Iwai1bd9deb2005-06-30 18:26:20 +02002353 unsigned long flags;
2354 int elem = kcontrol->private_value & 0xff;
2355 int shift = (kcontrol->private_value >> 8) & 0xff;
2356 int mask = (kcontrol->private_value >> 16) & 0xff;
2357 int invert = (kcontrol->private_value >> 24) & 1;
2358 int changed = 0;
2359 unsigned short val;
2360 snd_assert(dbri != NULL, return -EINVAL);
2361
2362 val = (ucontrol->value.integer.value[0] & mask);
2363 if (invert == 1)
2364 val = mask - val;
2365 val <<= shift;
2366
2367 if (elem < 4) {
2368 dbri->mm.data[elem] = (dbri->mm.data[elem] &
2369 ~(mask << shift)) | val;
2370 changed = (val != dbri->mm.data[elem]);
2371 } else {
2372 dbri->mm.ctrl[elem - 4] = (dbri->mm.ctrl[elem - 4] &
2373 ~(mask << shift)) | val;
2374 changed = (val != dbri->mm.ctrl[elem - 4]);
2375 }
2376
2377 dprintk(D_GEN, "put_single: mask=0x%x, changed=%d, "
2378 "mixer-value=%ld, mm-value=0x%x\n",
2379 mask, changed, ucontrol->value.integer.value[0],
2380 dbri->mm.data[elem & 3]);
2381
2382 if (changed) {
2383 /* First mute outputs, and wait 1/8000 sec (125 us)
2384 * to make sure this takes. This avoids clicking noises.
2385 */
2386 spin_lock_irqsave(&dbri->lock, flags);
2387
2388 cs4215_setdata(dbri, 1);
2389 udelay(125);
2390 cs4215_setdata(dbri, 0);
2391
2392 spin_unlock_irqrestore(&dbri->lock, flags);
2393 }
2394 return changed;
2395}
2396
2397/* Entries 0-3 map to the 4 data timeslots, entries 4-7 map to the 4 control
2398 timeslots. Shift is the bit offset in the timeslot, mask defines the
2399 number of bits. invert is a boolean for use with attenuation.
2400 */
2401#define CS4215_SINGLE(xname, entry, shift, mask, invert) \
2402{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
2403 .info = snd_cs4215_info_single, \
2404 .get = snd_cs4215_get_single, .put = snd_cs4215_put_single, \
2405 .private_value = entry | (shift << 8) | (mask << 16) | (invert << 24) },
2406
Takashi Iwai475675d2005-11-17 15:11:51 +01002407static struct snd_kcontrol_new dbri_controls[] __devinitdata = {
Takashi Iwai1bd9deb2005-06-30 18:26:20 +02002408 {
2409 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2410 .name = "Playback Volume",
2411 .info = snd_cs4215_info_volume,
2412 .get = snd_cs4215_get_volume,
2413 .put = snd_cs4215_put_volume,
2414 .private_value = DBRI_PLAY,
2415 },
2416 CS4215_SINGLE("Headphone switch", 0, 7, 1, 0)
2417 CS4215_SINGLE("Line out switch", 0, 6, 1, 0)
2418 CS4215_SINGLE("Speaker switch", 1, 6, 1, 0)
2419 {
2420 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2421 .name = "Capture Volume",
2422 .info = snd_cs4215_info_volume,
2423 .get = snd_cs4215_get_volume,
2424 .put = snd_cs4215_put_volume,
2425 .private_value = DBRI_REC,
2426 },
2427 /* FIXME: mic/line switch */
2428 CS4215_SINGLE("Line in switch", 2, 4, 1, 0)
2429 CS4215_SINGLE("High Pass Filter switch", 5, 7, 1, 0)
2430 CS4215_SINGLE("Monitor Volume", 3, 4, 0xf, 1)
2431 CS4215_SINGLE("Mic boost", 4, 4, 1, 1)
2432};
2433
Takashi Iwai475675d2005-11-17 15:11:51 +01002434#define NUM_CS4215_CONTROLS (sizeof(dbri_controls)/sizeof(struct snd_kcontrol_new))
Takashi Iwai1bd9deb2005-06-30 18:26:20 +02002435
Takashi Iwai475675d2005-11-17 15:11:51 +01002436static int __init snd_dbri_mixer(struct snd_dbri * dbri)
Takashi Iwai1bd9deb2005-06-30 18:26:20 +02002437{
Takashi Iwai475675d2005-11-17 15:11:51 +01002438 struct snd_card *card;
Takashi Iwai1bd9deb2005-06-30 18:26:20 +02002439 int idx, err;
2440
2441 snd_assert(dbri != NULL && dbri->card != NULL, return -EINVAL);
2442
2443 card = dbri->card;
2444 strcpy(card->mixername, card->shortname);
2445
2446 for (idx = 0; idx < NUM_CS4215_CONTROLS; idx++) {
2447 if ((err = snd_ctl_add(card,
Martin Habets43388292005-09-10 15:39:00 +02002448 snd_ctl_new1(&dbri_controls[idx], dbri))) < 0)
Takashi Iwai1bd9deb2005-06-30 18:26:20 +02002449 return err;
2450 }
2451
2452 for (idx = DBRI_REC; idx < DBRI_NO_STREAMS; idx++) {
2453 dbri->stream_info[idx].left_gain = 0;
2454 dbri->stream_info[idx].right_gain = 0;
2455 dbri->stream_info[idx].balance = DBRI_MID_BALANCE;
2456 }
2457
2458 return 0;
2459}
2460
2461/****************************************************************************
2462 /proc interface
2463****************************************************************************/
Takashi Iwai475675d2005-11-17 15:11:51 +01002464static void dbri_regs_read(struct snd_info_entry * entry, struct snd_info_buffer *buffer)
Takashi Iwai1bd9deb2005-06-30 18:26:20 +02002465{
Takashi Iwai475675d2005-11-17 15:11:51 +01002466 struct snd_dbri *dbri = entry->private_data;
Takashi Iwai1bd9deb2005-06-30 18:26:20 +02002467
2468 snd_iprintf(buffer, "REG0: 0x%x\n", sbus_readl(dbri->regs + REG0));
2469 snd_iprintf(buffer, "REG2: 0x%x\n", sbus_readl(dbri->regs + REG2));
2470 snd_iprintf(buffer, "REG8: 0x%x\n", sbus_readl(dbri->regs + REG8));
2471 snd_iprintf(buffer, "REG9: 0x%x\n", sbus_readl(dbri->regs + REG9));
2472}
2473
2474#ifdef DBRI_DEBUG
Takashi Iwai475675d2005-11-17 15:11:51 +01002475static void dbri_debug_read(struct snd_info_entry * entry,
2476 struct snd_info_buffer *buffer)
Takashi Iwai1bd9deb2005-06-30 18:26:20 +02002477{
Takashi Iwai475675d2005-11-17 15:11:51 +01002478 struct snd_dbri *dbri = entry->private_data;
Takashi Iwai1bd9deb2005-06-30 18:26:20 +02002479 int pipe;
2480 snd_iprintf(buffer, "debug=%d\n", dbri_debug);
2481
Takashi Iwai1bd9deb2005-06-30 18:26:20 +02002482 for (pipe = 0; pipe < 32; pipe++) {
2483 if (pipe_active(dbri, pipe)) {
2484 struct dbri_pipe *pptr = &dbri->pipes[pipe];
2485 snd_iprintf(buffer,
2486 "Pipe %d: %s SDP=0x%x desc=%d, "
2487 "len=%d @ %d prev: %d next %d\n",
2488 pipe,
2489 (pptr->direction ==
2490 PIPEinput ? "input" : "output"), pptr->sdp,
2491 pptr->desc, pptr->length, pptr->cycle,
2492 pptr->prevpipe, pptr->nextpipe);
2493 }
2494 }
2495}
Takashi Iwai1bd9deb2005-06-30 18:26:20 +02002496#endif
2497
Takashi Iwai475675d2005-11-17 15:11:51 +01002498void snd_dbri_proc(struct snd_dbri * dbri)
Takashi Iwai1bd9deb2005-06-30 18:26:20 +02002499{
Takashi Iwai475675d2005-11-17 15:11:51 +01002500 struct snd_info_entry *entry;
Takashi Iwai1bd9deb2005-06-30 18:26:20 +02002501
Takashi Iwai8cb7b632005-12-01 10:48:37 +01002502 if (! snd_card_proc_new(dbri->card, "regs", &entry))
Takashi Iwaibf850202006-04-28 15:13:41 +02002503 snd_info_set_text_ops(entry, dbri, dbri_regs_read);
Takashi Iwai1bd9deb2005-06-30 18:26:20 +02002504
2505#ifdef DBRI_DEBUG
Takashi Iwai8cb7b632005-12-01 10:48:37 +01002506 if (! snd_card_proc_new(dbri->card, "debug", &entry)) {
Takashi Iwaibf850202006-04-28 15:13:41 +02002507 snd_info_set_text_ops(entry, dbri, dbri_debug_read);
Takashi Iwai8cb7b632005-12-01 10:48:37 +01002508 entry->mode = S_IFREG | S_IRUGO; /* Readable only. */
2509 }
Takashi Iwai1bd9deb2005-06-30 18:26:20 +02002510#endif
2511}
2512
2513/*
2514****************************************************************************
2515**************************** Initialization ********************************
2516****************************************************************************
2517*/
Takashi Iwai475675d2005-11-17 15:11:51 +01002518static void snd_dbri_free(struct snd_dbri * dbri);
Takashi Iwai1bd9deb2005-06-30 18:26:20 +02002519
Takashi Iwai475675d2005-11-17 15:11:51 +01002520static int __init snd_dbri_create(struct snd_card *card,
Takashi Iwai1bd9deb2005-06-30 18:26:20 +02002521 struct sbus_dev *sdev,
2522 struct linux_prom_irqs *irq, int dev)
2523{
Takashi Iwai475675d2005-11-17 15:11:51 +01002524 struct snd_dbri *dbri = card->private_data;
Takashi Iwai1bd9deb2005-06-30 18:26:20 +02002525 int err;
2526
2527 spin_lock_init(&dbri->lock);
2528 dbri->card = card;
2529 dbri->sdev = sdev;
2530 dbri->irq = irq->pri;
2531 dbri->dbri_version = sdev->prom_name[9];
2532
2533 dbri->dma = sbus_alloc_consistent(sdev, sizeof(struct dbri_dma),
2534 &dbri->dma_dvma);
2535 memset((void *)dbri->dma, 0, sizeof(struct dbri_dma));
2536
2537 dprintk(D_GEN, "DMA Cmd Block 0x%p (0x%08x)\n",
2538 dbri->dma, dbri->dma_dvma);
2539
2540 /* Map the registers into memory. */
2541 dbri->regs_size = sdev->reg_addrs[0].reg_size;
2542 dbri->regs = sbus_ioremap(&sdev->resource[0], 0,
2543 dbri->regs_size, "DBRI Registers");
2544 if (!dbri->regs) {
2545 printk(KERN_ERR "DBRI: could not allocate registers\n");
2546 sbus_free_consistent(sdev, sizeof(struct dbri_dma),
2547 (void *)dbri->dma, dbri->dma_dvma);
2548 return -EIO;
2549 }
2550
Thomas Gleixner65ca68b2006-07-01 19:29:46 -07002551 err = request_irq(dbri->irq, snd_dbri_interrupt, IRQF_SHARED,
Takashi Iwai1bd9deb2005-06-30 18:26:20 +02002552 "DBRI audio", dbri);
2553 if (err) {
2554 printk(KERN_ERR "DBRI: Can't get irq %d\n", dbri->irq);
2555 sbus_iounmap(dbri->regs, dbri->regs_size);
2556 sbus_free_consistent(sdev, sizeof(struct dbri_dma),
2557 (void *)dbri->dma, dbri->dma_dvma);
2558 return err;
2559 }
2560
2561 /* Do low level initialization of the DBRI and CS4215 chips */
2562 dbri_initialize(dbri);
2563 err = cs4215_init(dbri);
2564 if (err) {
2565 snd_dbri_free(dbri);
2566 return err;
2567 }
2568
2569 dbri->next = dbri_list;
2570 dbri_list = dbri;
2571
2572 return 0;
2573}
2574
Takashi Iwai475675d2005-11-17 15:11:51 +01002575static void snd_dbri_free(struct snd_dbri * dbri)
Takashi Iwai1bd9deb2005-06-30 18:26:20 +02002576{
2577 dprintk(D_GEN, "snd_dbri_free\n");
2578 dbri_reset(dbri);
2579
2580 if (dbri->irq)
2581 free_irq(dbri->irq, dbri);
2582
2583 if (dbri->regs)
2584 sbus_iounmap(dbri->regs, dbri->regs_size);
2585
2586 if (dbri->dma)
2587 sbus_free_consistent(dbri->sdev, sizeof(struct dbri_dma),
2588 (void *)dbri->dma, dbri->dma_dvma);
2589}
2590
2591static int __init dbri_attach(int prom_node, struct sbus_dev *sdev)
2592{
Takashi Iwai475675d2005-11-17 15:11:51 +01002593 struct snd_dbri *dbri;
Takashi Iwai1bd9deb2005-06-30 18:26:20 +02002594 struct linux_prom_irqs irq;
2595 struct resource *rp;
Takashi Iwai475675d2005-11-17 15:11:51 +01002596 struct snd_card *card;
Takashi Iwai1bd9deb2005-06-30 18:26:20 +02002597 static int dev = 0;
2598 int err;
2599
2600 if (sdev->prom_name[9] < 'e') {
2601 printk(KERN_ERR "DBRI: unsupported chip version %c found.\n",
2602 sdev->prom_name[9]);
2603 return -EIO;
2604 }
2605
2606 if (dev >= SNDRV_CARDS)
2607 return -ENODEV;
2608 if (!enable[dev]) {
2609 dev++;
2610 return -ENOENT;
2611 }
2612
Martin Habets43388292005-09-10 15:39:00 +02002613 err = prom_getproperty(prom_node, "intr", (char *)&irq, sizeof(irq));
2614 if (err < 0) {
2615 printk(KERN_ERR "DBRI-%d: Firmware node lacks IRQ property.\n", dev);
2616 return -ENODEV;
2617 }
Takashi Iwai1bd9deb2005-06-30 18:26:20 +02002618
2619 card = snd_card_new(index[dev], id[dev], THIS_MODULE,
Takashi Iwai475675d2005-11-17 15:11:51 +01002620 sizeof(struct snd_dbri));
Takashi Iwai1bd9deb2005-06-30 18:26:20 +02002621 if (card == NULL)
2622 return -ENOMEM;
2623
2624 strcpy(card->driver, "DBRI");
2625 strcpy(card->shortname, "Sun DBRI");
2626 rp = &sdev->resource[0];
Andrew Morton5863aa62006-07-03 00:24:22 -07002627 sprintf(card->longname, "%s at 0x%02lx:0x%016Lx, irq %d",
Takashi Iwai1bd9deb2005-06-30 18:26:20 +02002628 card->shortname,
Greg Kroah-Hartmanaa0a2dd2006-06-12 14:50:27 -07002629 rp->flags & 0xffL, (unsigned long long)rp->start, irq.pri);
Takashi Iwai1bd9deb2005-06-30 18:26:20 +02002630
2631 if ((err = snd_dbri_create(card, sdev, &irq, dev)) < 0) {
2632 snd_card_free(card);
2633 return err;
2634 }
2635
Takashi Iwai475675d2005-11-17 15:11:51 +01002636 dbri = card->private_data;
Takashi Iwai16dab542005-09-05 17:17:58 +02002637 if ((err = snd_dbri_pcm(dbri)) < 0)
2638 goto _err;
Takashi Iwai1bd9deb2005-06-30 18:26:20 +02002639
Takashi Iwai16dab542005-09-05 17:17:58 +02002640 if ((err = snd_dbri_mixer(dbri)) < 0)
2641 goto _err;
Takashi Iwai1bd9deb2005-06-30 18:26:20 +02002642
2643 /* /proc file handling */
2644 snd_dbri_proc(dbri);
2645
Takashi Iwai16dab542005-09-05 17:17:58 +02002646 if ((err = snd_card_register(card)) < 0)
2647 goto _err;
Takashi Iwai1bd9deb2005-06-30 18:26:20 +02002648
2649 printk(KERN_INFO "audio%d at %p (irq %d) is DBRI(%c)+CS4215(%d)\n",
2650 dev, dbri->regs,
2651 dbri->irq, dbri->dbri_version, dbri->mm.version);
2652 dev++;
2653
2654 return 0;
Takashi Iwai16dab542005-09-05 17:17:58 +02002655
2656 _err:
2657 snd_dbri_free(dbri);
2658 snd_card_free(card);
2659 return err;
Takashi Iwai1bd9deb2005-06-30 18:26:20 +02002660}
2661
2662/* Probe for the dbri chip and then attach the driver. */
2663static int __init dbri_init(void)
2664{
2665 struct sbus_bus *sbus;
2666 struct sbus_dev *sdev;
2667 int found = 0;
2668
2669 /* Probe each SBUS for the DBRI chip(s). */
2670 for_all_sbusdev(sdev, sbus) {
2671 /*
2672 * The version is coded in the last character
2673 */
2674 if (!strncmp(sdev->prom_name, "SUNW,DBRI", 9)) {
2675 dprintk(D_GEN, "DBRI: Found %s in SBUS slot %d\n",
2676 sdev->prom_name, sdev->slot);
2677
2678 if (dbri_attach(sdev->prom_node, sdev) == 0)
2679 found++;
2680 }
2681 }
2682
2683 return (found > 0) ? 0 : -EIO;
2684}
2685
2686static void __exit dbri_exit(void)
2687{
Takashi Iwai475675d2005-11-17 15:11:51 +01002688 struct snd_dbri *this = dbri_list;
Takashi Iwai1bd9deb2005-06-30 18:26:20 +02002689
2690 while (this != NULL) {
Takashi Iwai475675d2005-11-17 15:11:51 +01002691 struct snd_dbri *next = this->next;
2692 struct snd_card *card = this->card;
Takashi Iwai1bd9deb2005-06-30 18:26:20 +02002693
2694 snd_dbri_free(this);
2695 snd_card_free(card);
2696 this = next;
2697 }
2698 dbri_list = NULL;
2699}
2700
2701module_init(dbri_init);
2702module_exit(dbri_exit);