blob: ddc0f2e082d63473174ef308f60f5e5e6d888d5d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Driver for CS4231 sound chips found on Sparcs.
3 * Copyright (C) 2002 David S. Miller <davem@redhat.com>
4 *
5 * Based entirely upon drivers/sbus/audio/cs4231.c which is:
6 * Copyright (C) 1996, 1997, 1998, 1998 Derrick J Brashear (shadow@andrew.cmu.edu)
7 * and also sound/isa/cs423x/cs4231_lib.c which is:
8 * Copyright (c) by Jaroslav Kysela <perex@suse.cz>
9 */
10
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/module.h>
12#include <linux/kernel.h>
13#include <linux/slab.h>
14#include <linux/delay.h>
15#include <linux/init.h>
16#include <linux/interrupt.h>
17#include <linux/moduleparam.h>
18
19#include <sound/driver.h>
20#include <sound/core.h>
21#include <sound/pcm.h>
22#include <sound/info.h>
23#include <sound/control.h>
24#include <sound/timer.h>
25#include <sound/initval.h>
26#include <sound/pcm_params.h>
27
28#include <asm/io.h>
29#include <asm/irq.h>
30
31#ifdef CONFIG_SBUS
32#define SBUS_SUPPORT
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <asm/sbus.h>
34#endif
35
36#if defined(CONFIG_PCI) && defined(CONFIG_SPARC64)
37#define EBUS_SUPPORT
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <linux/pci.h>
39#include <asm/ebus.h>
40#endif
41
42static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
43static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
44static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP; /* Enable this card */
45
46module_param_array(index, int, NULL, 0444);
47MODULE_PARM_DESC(index, "Index value for Sun CS4231 soundcard.");
48module_param_array(id, charp, NULL, 0444);
49MODULE_PARM_DESC(id, "ID string for Sun CS4231 soundcard.");
50module_param_array(enable, bool, NULL, 0444);
51MODULE_PARM_DESC(enable, "Enable Sun CS4231 soundcard.");
52MODULE_AUTHOR("Jaroslav Kysela, Derrick J. Brashear and David S. Miller");
53MODULE_DESCRIPTION("Sun CS4231");
54MODULE_LICENSE("GPL");
55MODULE_SUPPORTED_DEVICE("{{Sun,CS4231}}");
56
Georg Chini5a820fa2005-11-07 14:08:25 -080057#ifdef SBUS_SUPPORT
Takashi Iwaibe9b7e82006-01-03 13:42:38 +010058struct sbus_dma_info {
Georg Chini5a820fa2005-11-07 14:08:25 -080059 spinlock_t lock;
60 int dir;
61 void __iomem *regs;
Takashi Iwaibe9b7e82006-01-03 13:42:38 +010062};
Georg Chini5a820fa2005-11-07 14:08:25 -080063#endif
64
David S. Miller4f3f2f62006-01-17 15:55:58 -080065struct snd_cs4231;
Takashi Iwaibe9b7e82006-01-03 13:42:38 +010066struct cs4231_dma_control {
Georg Chinib1282542005-11-07 14:09:19 -080067 void (*prepare)(struct cs4231_dma_control *dma_cont, int dir);
68 void (*enable)(struct cs4231_dma_control *dma_cont, int on);
69 int (*request)(struct cs4231_dma_control *dma_cont, dma_addr_t bus_addr, size_t len);
70 unsigned int (*address)(struct cs4231_dma_control *dma_cont);
David S. Miller4f3f2f62006-01-17 15:55:58 -080071 void (*preallocate)(struct snd_cs4231 *chip, struct snd_pcm *pcm);
Georg Chinib1282542005-11-07 14:09:19 -080072#ifdef EBUS_SUPPORT
73 struct ebus_dma_info ebus_info;
74#endif
75#ifdef SBUS_SUPPORT
76 struct sbus_dma_info sbus_info;
77#endif
Takashi Iwaibe9b7e82006-01-03 13:42:38 +010078};
Georg Chinib1282542005-11-07 14:09:19 -080079
80struct snd_cs4231 {
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 spinlock_t lock;
82 void __iomem *port;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
Takashi Iwaibe9b7e82006-01-03 13:42:38 +010084 struct cs4231_dma_control p_dma;
85 struct cs4231_dma_control c_dma;
Georg Chini5a820fa2005-11-07 14:08:25 -080086
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 u32 flags;
88#define CS4231_FLAG_EBUS 0x00000001
89#define CS4231_FLAG_PLAYBACK 0x00000002
90#define CS4231_FLAG_CAPTURE 0x00000004
91
Takashi Iwaibe9b7e82006-01-03 13:42:38 +010092 struct snd_card *card;
93 struct snd_pcm *pcm;
94 struct snd_pcm_substream *playback_substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 unsigned int p_periods_sent;
Takashi Iwaibe9b7e82006-01-03 13:42:38 +010096 struct snd_pcm_substream *capture_substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 unsigned int c_periods_sent;
Takashi Iwaibe9b7e82006-01-03 13:42:38 +010098 struct snd_timer *timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
100 unsigned short mode;
101#define CS4231_MODE_NONE 0x0000
102#define CS4231_MODE_PLAY 0x0001
103#define CS4231_MODE_RECORD 0x0002
104#define CS4231_MODE_TIMER 0x0004
105#define CS4231_MODE_OPEN (CS4231_MODE_PLAY|CS4231_MODE_RECORD|CS4231_MODE_TIMER)
106
107 unsigned char image[32]; /* registers image */
108 int mce_bit;
109 int calibrate_mute;
Ingo Molnar12aa7572006-01-16 16:36:05 +0100110 struct mutex mce_mutex;
111 struct mutex open_mutex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
113 union {
114#ifdef SBUS_SUPPORT
115 struct sbus_dev *sdev;
116#endif
117#ifdef EBUS_SUPPORT
118 struct pci_dev *pdev;
119#endif
120 } dev_u;
121 unsigned int irq[2];
122 unsigned int regs_size;
123 struct snd_cs4231 *next;
Georg Chinib1282542005-11-07 14:09:19 -0800124};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100126static struct snd_cs4231 *cs4231_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
128/* Eventually we can use sound/isa/cs423x/cs4231_lib.c directly, but for
129 * now.... -DaveM
130 */
131
132/* IO ports */
133
134#define CS4231P(chip, x) ((chip)->port + c_d_c_CS4231##x)
135
136/* XXX offsets are different than PC ISA chips... */
137#define c_d_c_CS4231REGSEL 0x0
138#define c_d_c_CS4231REG 0x4
139#define c_d_c_CS4231STATUS 0x8
140#define c_d_c_CS4231PIO 0xc
141
142/* codec registers */
143
144#define CS4231_LEFT_INPUT 0x00 /* left input control */
145#define CS4231_RIGHT_INPUT 0x01 /* right input control */
146#define CS4231_AUX1_LEFT_INPUT 0x02 /* left AUX1 input control */
147#define CS4231_AUX1_RIGHT_INPUT 0x03 /* right AUX1 input control */
148#define CS4231_AUX2_LEFT_INPUT 0x04 /* left AUX2 input control */
149#define CS4231_AUX2_RIGHT_INPUT 0x05 /* right AUX2 input control */
150#define CS4231_LEFT_OUTPUT 0x06 /* left output control register */
151#define CS4231_RIGHT_OUTPUT 0x07 /* right output control register */
152#define CS4231_PLAYBK_FORMAT 0x08 /* clock and data format - playback - bits 7-0 MCE */
153#define CS4231_IFACE_CTRL 0x09 /* interface control - bits 7-2 MCE */
154#define CS4231_PIN_CTRL 0x0a /* pin control */
155#define CS4231_TEST_INIT 0x0b /* test and initialization */
156#define CS4231_MISC_INFO 0x0c /* miscellaneaous information */
157#define CS4231_LOOPBACK 0x0d /* loopback control */
158#define CS4231_PLY_UPR_CNT 0x0e /* playback upper base count */
159#define CS4231_PLY_LWR_CNT 0x0f /* playback lower base count */
160#define CS4231_ALT_FEATURE_1 0x10 /* alternate #1 feature enable */
161#define CS4231_ALT_FEATURE_2 0x11 /* alternate #2 feature enable */
162#define CS4231_LEFT_LINE_IN 0x12 /* left line input control */
163#define CS4231_RIGHT_LINE_IN 0x13 /* right line input control */
164#define CS4231_TIMER_LOW 0x14 /* timer low byte */
165#define CS4231_TIMER_HIGH 0x15 /* timer high byte */
166#define CS4231_LEFT_MIC_INPUT 0x16 /* left MIC input control register (InterWave only) */
167#define CS4231_RIGHT_MIC_INPUT 0x17 /* right MIC input control register (InterWave only) */
168#define CS4236_EXT_REG 0x17 /* extended register access */
169#define CS4231_IRQ_STATUS 0x18 /* irq status register */
170#define CS4231_LINE_LEFT_OUTPUT 0x19 /* left line output control register (InterWave only) */
171#define CS4231_VERSION 0x19 /* CS4231(A) - version values */
172#define CS4231_MONO_CTRL 0x1a /* mono input/output control */
173#define CS4231_LINE_RIGHT_OUTPUT 0x1b /* right line output control register (InterWave only) */
174#define CS4235_LEFT_MASTER 0x1b /* left master output control */
175#define CS4231_REC_FORMAT 0x1c /* clock and data format - record - bits 7-0 MCE */
176#define CS4231_PLY_VAR_FREQ 0x1d /* playback variable frequency */
177#define CS4235_RIGHT_MASTER 0x1d /* right master output control */
178#define CS4231_REC_UPR_CNT 0x1e /* record upper count */
179#define CS4231_REC_LWR_CNT 0x1f /* record lower count */
180
181/* definitions for codec register select port - CODECP( REGSEL ) */
182
183#define CS4231_INIT 0x80 /* CODEC is initializing */
184#define CS4231_MCE 0x40 /* mode change enable */
185#define CS4231_TRD 0x20 /* transfer request disable */
186
187/* definitions for codec status register - CODECP( STATUS ) */
188
189#define CS4231_GLOBALIRQ 0x01 /* IRQ is active */
190
Christopher Zimmermanna1314302005-09-21 00:41:22 -0700191/* definitions for codec irq status - CS4231_IRQ_STATUS */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
193#define CS4231_PLAYBACK_IRQ 0x10
194#define CS4231_RECORD_IRQ 0x20
195#define CS4231_TIMER_IRQ 0x40
196#define CS4231_ALL_IRQS 0x70
197#define CS4231_REC_UNDERRUN 0x08
198#define CS4231_REC_OVERRUN 0x04
199#define CS4231_PLY_OVERRUN 0x02
200#define CS4231_PLY_UNDERRUN 0x01
201
202/* definitions for CS4231_LEFT_INPUT and CS4231_RIGHT_INPUT registers */
203
204#define CS4231_ENABLE_MIC_GAIN 0x20
205
206#define CS4231_MIXS_LINE 0x00
207#define CS4231_MIXS_AUX1 0x40
208#define CS4231_MIXS_MIC 0x80
209#define CS4231_MIXS_ALL 0xc0
210
211/* definitions for clock and data format register - CS4231_PLAYBK_FORMAT */
212
213#define CS4231_LINEAR_8 0x00 /* 8-bit unsigned data */
214#define CS4231_ALAW_8 0x60 /* 8-bit A-law companded */
215#define CS4231_ULAW_8 0x20 /* 8-bit U-law companded */
216#define CS4231_LINEAR_16 0x40 /* 16-bit twos complement data - little endian */
217#define CS4231_LINEAR_16_BIG 0xc0 /* 16-bit twos complement data - big endian */
218#define CS4231_ADPCM_16 0xa0 /* 16-bit ADPCM */
219#define CS4231_STEREO 0x10 /* stereo mode */
220/* bits 3-1 define frequency divisor */
221#define CS4231_XTAL1 0x00 /* 24.576 crystal */
222#define CS4231_XTAL2 0x01 /* 16.9344 crystal */
223
224/* definitions for interface control register - CS4231_IFACE_CTRL */
225
226#define CS4231_RECORD_PIO 0x80 /* record PIO enable */
227#define CS4231_PLAYBACK_PIO 0x40 /* playback PIO enable */
228#define CS4231_CALIB_MODE 0x18 /* calibration mode bits */
229#define CS4231_AUTOCALIB 0x08 /* auto calibrate */
230#define CS4231_SINGLE_DMA 0x04 /* use single DMA channel */
231#define CS4231_RECORD_ENABLE 0x02 /* record enable */
232#define CS4231_PLAYBACK_ENABLE 0x01 /* playback enable */
233
234/* definitions for pin control register - CS4231_PIN_CTRL */
235
236#define CS4231_IRQ_ENABLE 0x02 /* enable IRQ */
237#define CS4231_XCTL1 0x40 /* external control #1 */
238#define CS4231_XCTL0 0x80 /* external control #0 */
239
240/* definitions for test and init register - CS4231_TEST_INIT */
241
242#define CS4231_CALIB_IN_PROGRESS 0x20 /* auto calibrate in progress */
243#define CS4231_DMA_REQUEST 0x10 /* DMA request in progress */
244
245/* definitions for misc control register - CS4231_MISC_INFO */
246
247#define CS4231_MODE2 0x40 /* MODE 2 */
248#define CS4231_IW_MODE3 0x6c /* MODE 3 - InterWave enhanced mode */
249#define CS4231_4236_MODE3 0xe0 /* MODE 3 - CS4236+ enhanced mode */
250
251/* definitions for alternate feature 1 register - CS4231_ALT_FEATURE_1 */
252
253#define CS4231_DACZ 0x01 /* zero DAC when underrun */
254#define CS4231_TIMER_ENABLE 0x40 /* codec timer enable */
255#define CS4231_OLB 0x80 /* output level bit */
256
257/* SBUS DMA register defines. */
258
259#define APCCSR 0x10UL /* APC DMA CSR */
260#define APCCVA 0x20UL /* APC Capture DMA Address */
261#define APCCC 0x24UL /* APC Capture Count */
262#define APCCNVA 0x28UL /* APC Capture DMA Next Address */
263#define APCCNC 0x2cUL /* APC Capture Next Count */
264#define APCPVA 0x30UL /* APC Play DMA Address */
265#define APCPC 0x34UL /* APC Play Count */
266#define APCPNVA 0x38UL /* APC Play DMA Next Address */
267#define APCPNC 0x3cUL /* APC Play Next Count */
268
Georg Chini5a820fa2005-11-07 14:08:25 -0800269/* Defines for SBUS DMA-routines */
270
271#define APCVA 0x0UL /* APC DMA Address */
272#define APCC 0x4UL /* APC Count */
273#define APCNVA 0x8UL /* APC DMA Next Address */
274#define APCNC 0xcUL /* APC Next Count */
275#define APC_PLAY 0x30UL /* Play registers start at 0x30 */
276#define APC_RECORD 0x20UL /* Record registers start at 0x20 */
277
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278/* APCCSR bits */
279
280#define APC_INT_PENDING 0x800000 /* Interrupt Pending */
281#define APC_PLAY_INT 0x400000 /* Playback interrupt */
282#define APC_CAPT_INT 0x200000 /* Capture interrupt */
283#define APC_GENL_INT 0x100000 /* General interrupt */
284#define APC_XINT_ENA 0x80000 /* General ext int. enable */
285#define APC_XINT_PLAY 0x40000 /* Playback ext intr */
286#define APC_XINT_CAPT 0x20000 /* Capture ext intr */
287#define APC_XINT_GENL 0x10000 /* Error ext intr */
288#define APC_XINT_EMPT 0x8000 /* Pipe empty interrupt (0 write to pva) */
289#define APC_XINT_PEMP 0x4000 /* Play pipe empty (pva and pnva not set) */
290#define APC_XINT_PNVA 0x2000 /* Playback NVA dirty */
291#define APC_XINT_PENA 0x1000 /* play pipe empty Int enable */
292#define APC_XINT_COVF 0x800 /* Cap data dropped on floor */
293#define APC_XINT_CNVA 0x400 /* Capture NVA dirty */
294#define APC_XINT_CEMP 0x200 /* Capture pipe empty (cva and cnva not set) */
295#define APC_XINT_CENA 0x100 /* Cap. pipe empty int enable */
296#define APC_PPAUSE 0x80 /* Pause the play DMA */
297#define APC_CPAUSE 0x40 /* Pause the capture DMA */
298#define APC_CDC_RESET 0x20 /* CODEC RESET */
299#define APC_PDMA_READY 0x08 /* Play DMA Go */
300#define APC_CDMA_READY 0x04 /* Capture DMA Go */
301#define APC_CHIP_RESET 0x01 /* Reset the chip */
302
303/* EBUS DMA register offsets */
304
305#define EBDMA_CSR 0x00UL /* Control/Status */
306#define EBDMA_ADDR 0x04UL /* DMA Address */
307#define EBDMA_COUNT 0x08UL /* DMA Count */
308
309/*
310 * Some variables
311 */
312
313static unsigned char freq_bits[14] = {
314 /* 5510 */ 0x00 | CS4231_XTAL2,
315 /* 6620 */ 0x0E | CS4231_XTAL2,
316 /* 8000 */ 0x00 | CS4231_XTAL1,
317 /* 9600 */ 0x0E | CS4231_XTAL1,
318 /* 11025 */ 0x02 | CS4231_XTAL2,
319 /* 16000 */ 0x02 | CS4231_XTAL1,
320 /* 18900 */ 0x04 | CS4231_XTAL2,
321 /* 22050 */ 0x06 | CS4231_XTAL2,
322 /* 27042 */ 0x04 | CS4231_XTAL1,
323 /* 32000 */ 0x06 | CS4231_XTAL1,
324 /* 33075 */ 0x0C | CS4231_XTAL2,
325 /* 37800 */ 0x08 | CS4231_XTAL2,
326 /* 44100 */ 0x0A | CS4231_XTAL2,
327 /* 48000 */ 0x0C | CS4231_XTAL1
328};
329
330static unsigned int rates[14] = {
331 5510, 6620, 8000, 9600, 11025, 16000, 18900, 22050,
332 27042, 32000, 33075, 37800, 44100, 48000
333};
334
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100335static struct snd_pcm_hw_constraint_list hw_constraints_rates = {
Krzysztof Heltc6c2d572007-09-04 13:08:24 +0200336 .count = ARRAY_SIZE(rates),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 .list = rates,
338};
339
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100340static int snd_cs4231_xrate(struct snd_pcm_runtime *runtime)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341{
342 return snd_pcm_hw_constraint_list(runtime, 0,
343 SNDRV_PCM_HW_PARAM_RATE,
344 &hw_constraints_rates);
345}
346
347static unsigned char snd_cs4231_original_image[32] =
348{
349 0x00, /* 00/00 - lic */
350 0x00, /* 01/01 - ric */
351 0x9f, /* 02/02 - la1ic */
352 0x9f, /* 03/03 - ra1ic */
353 0x9f, /* 04/04 - la2ic */
354 0x9f, /* 05/05 - ra2ic */
355 0xbf, /* 06/06 - loc */
356 0xbf, /* 07/07 - roc */
357 0x20, /* 08/08 - pdfr */
358 CS4231_AUTOCALIB, /* 09/09 - ic */
359 0x00, /* 0a/10 - pc */
360 0x00, /* 0b/11 - ti */
361 CS4231_MODE2, /* 0c/12 - mi */
362 0x00, /* 0d/13 - lbc */
363 0x00, /* 0e/14 - pbru */
364 0x00, /* 0f/15 - pbrl */
365 0x80, /* 10/16 - afei */
366 0x01, /* 11/17 - afeii */
367 0x9f, /* 12/18 - llic */
368 0x9f, /* 13/19 - rlic */
369 0x00, /* 14/20 - tlb */
370 0x00, /* 15/21 - thb */
371 0x00, /* 16/22 - la3mic/reserved */
372 0x00, /* 17/23 - ra3mic/reserved */
373 0x00, /* 18/24 - afs */
374 0x00, /* 19/25 - lamoc/version */
375 0x00, /* 1a/26 - mioc */
376 0x00, /* 1b/27 - ramoc/reserved */
377 0x20, /* 1c/28 - cdfr */
378 0x00, /* 1d/29 - res4 */
379 0x00, /* 1e/30 - cbru */
380 0x00, /* 1f/31 - cbrl */
381};
382
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100383static u8 __cs4231_readb(struct snd_cs4231 *cp, void __iomem *reg_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384{
385#ifdef EBUS_SUPPORT
Krzysztof Heltc6c2d572007-09-04 13:08:24 +0200386 if (cp->flags & CS4231_FLAG_EBUS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 return readb(reg_addr);
Krzysztof Heltc6c2d572007-09-04 13:08:24 +0200388 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389#endif
390#ifdef SBUS_SUPPORT
391 return sbus_readb(reg_addr);
392#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393}
394
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100395static void __cs4231_writeb(struct snd_cs4231 *cp, u8 val, void __iomem *reg_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396{
397#ifdef EBUS_SUPPORT
Krzysztof Heltc6c2d572007-09-04 13:08:24 +0200398 if (cp->flags & CS4231_FLAG_EBUS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 return writeb(val, reg_addr);
Krzysztof Heltc6c2d572007-09-04 13:08:24 +0200400 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401#endif
402#ifdef SBUS_SUPPORT
403 return sbus_writeb(val, reg_addr);
404#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405}
406
407/*
408 * Basic I/O functions
409 */
410
Krzysztof Heltc6c2d572007-09-04 13:08:24 +0200411static void snd_cs4231_ready(struct snd_cs4231 *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412{
413 int timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
415 for (timeout = 250;
416 timeout > 0 && (__cs4231_readb(chip, CS4231P(chip, REGSEL)) & CS4231_INIT);
417 timeout--)
418 udelay(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419}
420
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100421static void snd_cs4231_dout(struct snd_cs4231 *chip, unsigned char reg, unsigned char value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422{
Krzysztof Heltc6c2d572007-09-04 13:08:24 +0200423 snd_cs4231_ready(chip);
Christopher Zimmermanna1314302005-09-21 00:41:22 -0700424#ifdef CONFIG_SND_DEBUG
425 if (__cs4231_readb(chip, CS4231P(chip, REGSEL)) & CS4231_INIT)
Krzysztof Heltc6c2d572007-09-04 13:08:24 +0200426 snd_printdd("out: auto calibration time out - reg = 0x%x, "
427 "value = 0x%x\n",
428 reg, value);
Christopher Zimmermanna1314302005-09-21 00:41:22 -0700429#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 __cs4231_writeb(chip, chip->mce_bit | reg, CS4231P(chip, REGSEL));
Krzysztof Heltc6c2d572007-09-04 13:08:24 +0200431 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 __cs4231_writeb(chip, value, CS4231P(chip, REG));
433 mb();
434}
435
Krzysztof Heltc6c2d572007-09-04 13:08:24 +0200436static inline void snd_cs4231_outm(struct snd_cs4231 *chip, unsigned char reg,
437 unsigned char mask, unsigned char value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438{
Krzysztof Heltc6c2d572007-09-04 13:08:24 +0200439 unsigned char tmp = (chip->image[reg] & mask) | value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
Krzysztof Heltc6c2d572007-09-04 13:08:24 +0200441 chip->image[reg] = tmp;
442 if (!chip->calibrate_mute)
443 snd_cs4231_dout(chip, reg, tmp);
444}
445
446static void snd_cs4231_out(struct snd_cs4231 *chip, unsigned char reg,
447 unsigned char value)
448{
449 snd_cs4231_dout(chip, reg, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 chip->image[reg] = value;
451 mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452}
453
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100454static unsigned char snd_cs4231_in(struct snd_cs4231 *chip, unsigned char reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455{
Krzysztof Heltc6c2d572007-09-04 13:08:24 +0200456 snd_cs4231_ready(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457#ifdef CONFIG_SND_DEBUG
458 if (__cs4231_readb(chip, CS4231P(chip, REGSEL)) & CS4231_INIT)
Krzysztof Heltc6c2d572007-09-04 13:08:24 +0200459 snd_printdd("in: auto calibration time out - reg = 0x%x\n",
460 reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461#endif
462 __cs4231_writeb(chip, chip->mce_bit | reg, CS4231P(chip, REGSEL));
463 mb();
Krzysztof Heltc6c2d572007-09-04 13:08:24 +0200464 return __cs4231_readb(chip, CS4231P(chip, REG));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465}
466
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467/*
468 * CS4231 detection / MCE routines
469 */
470
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100471static void snd_cs4231_busy_wait(struct snd_cs4231 *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472{
473 int timeout;
474
475 /* huh.. looks like this sequence is proper for CS4231A chip (GUS MAX) */
476 for (timeout = 5; timeout > 0; timeout--)
477 __cs4231_readb(chip, CS4231P(chip, REGSEL));
Christopher Zimmermanna1314302005-09-21 00:41:22 -0700478
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 /* end of cleanup sequence */
Christopher Zimmermanna1314302005-09-21 00:41:22 -0700480 for (timeout = 500;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 timeout > 0 && (__cs4231_readb(chip, CS4231P(chip, REGSEL)) & CS4231_INIT);
482 timeout--)
Krzysztof Heltc6c2d572007-09-04 13:08:24 +0200483 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484}
485
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100486static void snd_cs4231_mce_up(struct snd_cs4231 *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487{
488 unsigned long flags;
489 int timeout;
490
491 spin_lock_irqsave(&chip->lock, flags);
Krzysztof Heltc6c2d572007-09-04 13:08:24 +0200492 snd_cs4231_ready(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493#ifdef CONFIG_SND_DEBUG
494 if (__cs4231_readb(chip, CS4231P(chip, REGSEL)) & CS4231_INIT)
Christopher Zimmermanna1314302005-09-21 00:41:22 -0700495 snd_printdd("mce_up - auto calibration time out (0)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496#endif
497 chip->mce_bit |= CS4231_MCE;
498 timeout = __cs4231_readb(chip, CS4231P(chip, REGSEL));
499 if (timeout == 0x80)
Christopher Zimmermanna1314302005-09-21 00:41:22 -0700500 snd_printdd("mce_up [%p]: serious init problem - codec still busy\n", chip->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 if (!(timeout & CS4231_MCE))
502 __cs4231_writeb(chip, chip->mce_bit | (timeout & 0x1f), CS4231P(chip, REGSEL));
503 spin_unlock_irqrestore(&chip->lock, flags);
504}
505
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100506static void snd_cs4231_mce_down(struct snd_cs4231 *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507{
508 unsigned long flags;
509 int timeout;
510
511 spin_lock_irqsave(&chip->lock, flags);
512 snd_cs4231_busy_wait(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513#ifdef CONFIG_SND_DEBUG
514 if (__cs4231_readb(chip, CS4231P(chip, REGSEL)) & CS4231_INIT)
Christopher Zimmermanna1314302005-09-21 00:41:22 -0700515 snd_printdd("mce_down [%p] - auto calibration time out (0)\n", CS4231P(chip, REGSEL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516#endif
517 chip->mce_bit &= ~CS4231_MCE;
518 timeout = __cs4231_readb(chip, CS4231P(chip, REGSEL));
519 __cs4231_writeb(chip, chip->mce_bit | (timeout & 0x1f), CS4231P(chip, REGSEL));
520 if (timeout == 0x80)
Christopher Zimmermanna1314302005-09-21 00:41:22 -0700521 snd_printdd("mce_down [%p]: serious init problem - codec still busy\n", chip->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 if ((timeout & CS4231_MCE) == 0) {
523 spin_unlock_irqrestore(&chip->lock, flags);
524 return;
525 }
526 snd_cs4231_busy_wait(chip);
527
528 /* calibration process */
529
Krzysztof Heltc6c2d572007-09-04 13:08:24 +0200530 snd_cs4231_ready(chip);
531 snd_cs4231_ready(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 if ((snd_cs4231_in(chip, CS4231_TEST_INIT) & CS4231_CALIB_IN_PROGRESS) == 0) {
533 snd_printd("cs4231_mce_down - auto calibration time out (1)\n");
534 spin_unlock_irqrestore(&chip->lock, flags);
535 return;
536 }
Christopher Zimmermanna1314302005-09-21 00:41:22 -0700537
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 /* in 10ms increments, check condition, up to 250ms */
539 timeout = 25;
540 while (snd_cs4231_in(chip, CS4231_TEST_INIT) & CS4231_CALIB_IN_PROGRESS) {
541 spin_unlock_irqrestore(&chip->lock, flags);
542 if (--timeout < 0) {
543 snd_printk("mce_down - auto calibration time out (2)\n");
544 return;
545 }
546 msleep(10);
547 spin_lock_irqsave(&chip->lock, flags);
548 }
Christopher Zimmermanna1314302005-09-21 00:41:22 -0700549
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 /* in 10ms increments, check condition, up to 100ms */
551 timeout = 10;
552 while (__cs4231_readb(chip, CS4231P(chip, REGSEL)) & CS4231_INIT) {
553 spin_unlock_irqrestore(&chip->lock, flags);
554 if (--timeout < 0) {
555 snd_printk("mce_down - auto calibration time out (3)\n");
556 return;
557 }
558 msleep(10);
559 spin_lock_irqsave(&chip->lock, flags);
560 }
561 spin_unlock_irqrestore(&chip->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562}
563
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100564static void snd_cs4231_advance_dma(struct cs4231_dma_control *dma_cont,
565 struct snd_pcm_substream *substream,
566 unsigned int *periods_sent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567{
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100568 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
570 while (1) {
Christopher Zimmermanna1314302005-09-21 00:41:22 -0700571 unsigned int period_size = snd_pcm_lib_period_bytes(substream);
572 unsigned int offset = period_size * (*periods_sent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
Eric Sesterhenn817dd6e2006-03-24 18:49:12 +0100574 BUG_ON(period_size >= (1 << 24));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
Georg Chinib1282542005-11-07 14:09:19 -0800576 if (dma_cont->request(dma_cont, runtime->dma_addr + offset, period_size))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 (*periods_sent) = ((*periods_sent) + 1) % runtime->periods;
579 }
580}
Christopher Zimmermanna1314302005-09-21 00:41:22 -0700581
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100582static void cs4231_dma_trigger(struct snd_pcm_substream *substream,
583 unsigned int what, int on)
Christopher Zimmermanna1314302005-09-21 00:41:22 -0700584{
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100585 struct snd_cs4231 *chip = snd_pcm_substream_chip(substream);
586 struct cs4231_dma_control *dma_cont;
Christopher Zimmermanna1314302005-09-21 00:41:22 -0700587
Georg Chini5a820fa2005-11-07 14:08:25 -0800588 if (what & CS4231_PLAYBACK_ENABLE) {
Georg Chinib1282542005-11-07 14:09:19 -0800589 dma_cont = &chip->p_dma;
Christopher Zimmermanna1314302005-09-21 00:41:22 -0700590 if (on) {
Georg Chinib1282542005-11-07 14:09:19 -0800591 dma_cont->prepare(dma_cont, 0);
592 dma_cont->enable(dma_cont, 1);
593 snd_cs4231_advance_dma(dma_cont,
Georg Chini5a820fa2005-11-07 14:08:25 -0800594 chip->playback_substream,
595 &chip->p_periods_sent);
Christopher Zimmermanna1314302005-09-21 00:41:22 -0700596 } else {
Georg Chinib1282542005-11-07 14:09:19 -0800597 dma_cont->enable(dma_cont, 0);
Christopher Zimmermanna1314302005-09-21 00:41:22 -0700598 }
Georg Chini5a820fa2005-11-07 14:08:25 -0800599 }
600 if (what & CS4231_RECORD_ENABLE) {
Georg Chinib1282542005-11-07 14:09:19 -0800601 dma_cont = &chip->c_dma;
Christopher Zimmermanna1314302005-09-21 00:41:22 -0700602 if (on) {
Georg Chinib1282542005-11-07 14:09:19 -0800603 dma_cont->prepare(dma_cont, 1);
604 dma_cont->enable(dma_cont, 1);
605 snd_cs4231_advance_dma(dma_cont,
Georg Chini5a820fa2005-11-07 14:08:25 -0800606 chip->capture_substream,
607 &chip->c_periods_sent);
Christopher Zimmermanna1314302005-09-21 00:41:22 -0700608 } else {
Georg Chinib1282542005-11-07 14:09:19 -0800609 dma_cont->enable(dma_cont, 0);
Christopher Zimmermanna1314302005-09-21 00:41:22 -0700610 }
Christopher Zimmermanna1314302005-09-21 00:41:22 -0700611 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612}
613
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100614static int snd_cs4231_trigger(struct snd_pcm_substream *substream, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615{
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100616 struct snd_cs4231 *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 int result = 0;
618
619 switch (cmd) {
620 case SNDRV_PCM_TRIGGER_START:
621 case SNDRV_PCM_TRIGGER_STOP:
622 {
623 unsigned int what = 0;
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100624 struct snd_pcm_substream *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 unsigned long flags;
626
Takashi Iwaief991b92007-02-22 12:52:53 +0100627 snd_pcm_group_for_each_entry(s, substream) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 if (s == chip->playback_substream) {
629 what |= CS4231_PLAYBACK_ENABLE;
630 snd_pcm_trigger_done(s, substream);
631 } else if (s == chip->capture_substream) {
632 what |= CS4231_RECORD_ENABLE;
633 snd_pcm_trigger_done(s, substream);
634 }
635 }
636
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 spin_lock_irqsave(&chip->lock, flags);
638 if (cmd == SNDRV_PCM_TRIGGER_START) {
Christopher Zimmermanna1314302005-09-21 00:41:22 -0700639 cs4231_dma_trigger(substream, what, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 chip->image[CS4231_IFACE_CTRL] |= what;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 } else {
Christopher Zimmermanna1314302005-09-21 00:41:22 -0700642 cs4231_dma_trigger(substream, what, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 chip->image[CS4231_IFACE_CTRL] &= ~what;
644 }
645 snd_cs4231_out(chip, CS4231_IFACE_CTRL,
646 chip->image[CS4231_IFACE_CTRL]);
647 spin_unlock_irqrestore(&chip->lock, flags);
648 break;
649 }
650 default:
651 result = -EINVAL;
652 break;
653 }
Christopher Zimmermanna1314302005-09-21 00:41:22 -0700654
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 return result;
656}
657
658/*
659 * CODEC I/O
660 */
661
662static unsigned char snd_cs4231_get_rate(unsigned int rate)
663{
664 int i;
665
666 for (i = 0; i < 14; i++)
667 if (rate == rates[i])
668 return freq_bits[i];
669 // snd_BUG();
670 return freq_bits[13];
671}
672
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100673static unsigned char snd_cs4231_get_format(struct snd_cs4231 *chip, int format, int channels)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674{
675 unsigned char rformat;
676
677 rformat = CS4231_LINEAR_8;
678 switch (format) {
679 case SNDRV_PCM_FORMAT_MU_LAW: rformat = CS4231_ULAW_8; break;
680 case SNDRV_PCM_FORMAT_A_LAW: rformat = CS4231_ALAW_8; break;
681 case SNDRV_PCM_FORMAT_S16_LE: rformat = CS4231_LINEAR_16; break;
682 case SNDRV_PCM_FORMAT_S16_BE: rformat = CS4231_LINEAR_16_BIG; break;
683 case SNDRV_PCM_FORMAT_IMA_ADPCM: rformat = CS4231_ADPCM_16; break;
684 }
685 if (channels > 1)
686 rformat |= CS4231_STEREO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 return rformat;
688}
689
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100690static void snd_cs4231_calibrate_mute(struct snd_cs4231 *chip, int mute)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691{
692 unsigned long flags;
693
694 mute = mute ? 1 : 0;
695 spin_lock_irqsave(&chip->lock, flags);
696 if (chip->calibrate_mute == mute) {
697 spin_unlock_irqrestore(&chip->lock, flags);
698 return;
699 }
700 if (!mute) {
701 snd_cs4231_dout(chip, CS4231_LEFT_INPUT,
702 chip->image[CS4231_LEFT_INPUT]);
703 snd_cs4231_dout(chip, CS4231_RIGHT_INPUT,
704 chip->image[CS4231_RIGHT_INPUT]);
705 snd_cs4231_dout(chip, CS4231_LOOPBACK,
706 chip->image[CS4231_LOOPBACK]);
707 }
708 snd_cs4231_dout(chip, CS4231_AUX1_LEFT_INPUT,
709 mute ? 0x80 : chip->image[CS4231_AUX1_LEFT_INPUT]);
710 snd_cs4231_dout(chip, CS4231_AUX1_RIGHT_INPUT,
711 mute ? 0x80 : chip->image[CS4231_AUX1_RIGHT_INPUT]);
712 snd_cs4231_dout(chip, CS4231_AUX2_LEFT_INPUT,
713 mute ? 0x80 : chip->image[CS4231_AUX2_LEFT_INPUT]);
714 snd_cs4231_dout(chip, CS4231_AUX2_RIGHT_INPUT,
715 mute ? 0x80 : chip->image[CS4231_AUX2_RIGHT_INPUT]);
716 snd_cs4231_dout(chip, CS4231_LEFT_OUTPUT,
717 mute ? 0x80 : chip->image[CS4231_LEFT_OUTPUT]);
718 snd_cs4231_dout(chip, CS4231_RIGHT_OUTPUT,
719 mute ? 0x80 : chip->image[CS4231_RIGHT_OUTPUT]);
720 snd_cs4231_dout(chip, CS4231_LEFT_LINE_IN,
721 mute ? 0x80 : chip->image[CS4231_LEFT_LINE_IN]);
722 snd_cs4231_dout(chip, CS4231_RIGHT_LINE_IN,
723 mute ? 0x80 : chip->image[CS4231_RIGHT_LINE_IN]);
724 snd_cs4231_dout(chip, CS4231_MONO_CTRL,
725 mute ? 0xc0 : chip->image[CS4231_MONO_CTRL]);
726 chip->calibrate_mute = mute;
727 spin_unlock_irqrestore(&chip->lock, flags);
728}
729
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100730static void snd_cs4231_playback_format(struct snd_cs4231 *chip, struct snd_pcm_hw_params *params,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 unsigned char pdfr)
732{
733 unsigned long flags;
734
Ingo Molnar12aa7572006-01-16 16:36:05 +0100735 mutex_lock(&chip->mce_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 snd_cs4231_calibrate_mute(chip, 1);
737
738 snd_cs4231_mce_up(chip);
739
740 spin_lock_irqsave(&chip->lock, flags);
741 snd_cs4231_out(chip, CS4231_PLAYBK_FORMAT,
742 (chip->image[CS4231_IFACE_CTRL] & CS4231_RECORD_ENABLE) ?
743 (pdfr & 0xf0) | (chip->image[CS4231_REC_FORMAT] & 0x0f) :
744 pdfr);
745 spin_unlock_irqrestore(&chip->lock, flags);
746
747 snd_cs4231_mce_down(chip);
748
749 snd_cs4231_calibrate_mute(chip, 0);
Ingo Molnar12aa7572006-01-16 16:36:05 +0100750 mutex_unlock(&chip->mce_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751}
752
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100753static void snd_cs4231_capture_format(struct snd_cs4231 *chip, struct snd_pcm_hw_params *params,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 unsigned char cdfr)
755{
756 unsigned long flags;
757
Ingo Molnar12aa7572006-01-16 16:36:05 +0100758 mutex_lock(&chip->mce_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 snd_cs4231_calibrate_mute(chip, 1);
760
761 snd_cs4231_mce_up(chip);
762
763 spin_lock_irqsave(&chip->lock, flags);
764 if (!(chip->image[CS4231_IFACE_CTRL] & CS4231_PLAYBACK_ENABLE)) {
765 snd_cs4231_out(chip, CS4231_PLAYBK_FORMAT,
766 ((chip->image[CS4231_PLAYBK_FORMAT]) & 0xf0) |
767 (cdfr & 0x0f));
768 spin_unlock_irqrestore(&chip->lock, flags);
769 snd_cs4231_mce_down(chip);
770 snd_cs4231_mce_up(chip);
771 spin_lock_irqsave(&chip->lock, flags);
772 }
773 snd_cs4231_out(chip, CS4231_REC_FORMAT, cdfr);
774 spin_unlock_irqrestore(&chip->lock, flags);
775
776 snd_cs4231_mce_down(chip);
777
778 snd_cs4231_calibrate_mute(chip, 0);
Ingo Molnar12aa7572006-01-16 16:36:05 +0100779 mutex_unlock(&chip->mce_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780}
781
782/*
783 * Timer interface
784 */
785
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100786static unsigned long snd_cs4231_timer_resolution(struct snd_timer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787{
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100788 struct snd_cs4231 *chip = snd_timer_chip(timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
790 return chip->image[CS4231_PLAYBK_FORMAT] & 1 ? 9969 : 9920;
791}
792
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100793static int snd_cs4231_timer_start(struct snd_timer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794{
795 unsigned long flags;
796 unsigned int ticks;
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100797 struct snd_cs4231 *chip = snd_timer_chip(timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798
799 spin_lock_irqsave(&chip->lock, flags);
800 ticks = timer->sticks;
801 if ((chip->image[CS4231_ALT_FEATURE_1] & CS4231_TIMER_ENABLE) == 0 ||
802 (unsigned char)(ticks >> 8) != chip->image[CS4231_TIMER_HIGH] ||
803 (unsigned char)ticks != chip->image[CS4231_TIMER_LOW]) {
804 snd_cs4231_out(chip, CS4231_TIMER_HIGH,
805 chip->image[CS4231_TIMER_HIGH] =
806 (unsigned char) (ticks >> 8));
807 snd_cs4231_out(chip, CS4231_TIMER_LOW,
808 chip->image[CS4231_TIMER_LOW] =
809 (unsigned char) ticks);
810 snd_cs4231_out(chip, CS4231_ALT_FEATURE_1,
811 chip->image[CS4231_ALT_FEATURE_1] | CS4231_TIMER_ENABLE);
812 }
813 spin_unlock_irqrestore(&chip->lock, flags);
814
815 return 0;
816}
817
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100818static int snd_cs4231_timer_stop(struct snd_timer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819{
820 unsigned long flags;
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100821 struct snd_cs4231 *chip = snd_timer_chip(timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822
823 spin_lock_irqsave(&chip->lock, flags);
824 snd_cs4231_out(chip, CS4231_ALT_FEATURE_1,
825 chip->image[CS4231_ALT_FEATURE_1] &= ~CS4231_TIMER_ENABLE);
826 spin_unlock_irqrestore(&chip->lock, flags);
827
828 return 0;
829}
830
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100831static void __init snd_cs4231_init(struct snd_cs4231 *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832{
833 unsigned long flags;
834
835 snd_cs4231_mce_down(chip);
836
837#ifdef SNDRV_DEBUG_MCE
Christopher Zimmermanna1314302005-09-21 00:41:22 -0700838 snd_printdd("init: (1)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839#endif
840 snd_cs4231_mce_up(chip);
841 spin_lock_irqsave(&chip->lock, flags);
842 chip->image[CS4231_IFACE_CTRL] &= ~(CS4231_PLAYBACK_ENABLE | CS4231_PLAYBACK_PIO |
843 CS4231_RECORD_ENABLE | CS4231_RECORD_PIO |
844 CS4231_CALIB_MODE);
845 chip->image[CS4231_IFACE_CTRL] |= CS4231_AUTOCALIB;
846 snd_cs4231_out(chip, CS4231_IFACE_CTRL, chip->image[CS4231_IFACE_CTRL]);
847 spin_unlock_irqrestore(&chip->lock, flags);
848 snd_cs4231_mce_down(chip);
849
850#ifdef SNDRV_DEBUG_MCE
Christopher Zimmermanna1314302005-09-21 00:41:22 -0700851 snd_printdd("init: (2)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852#endif
853
854 snd_cs4231_mce_up(chip);
855 spin_lock_irqsave(&chip->lock, flags);
856 snd_cs4231_out(chip, CS4231_ALT_FEATURE_1, chip->image[CS4231_ALT_FEATURE_1]);
857 spin_unlock_irqrestore(&chip->lock, flags);
858 snd_cs4231_mce_down(chip);
859
860#ifdef SNDRV_DEBUG_MCE
Christopher Zimmermanna1314302005-09-21 00:41:22 -0700861 snd_printdd("init: (3) - afei = 0x%x\n", chip->image[CS4231_ALT_FEATURE_1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862#endif
863
864 spin_lock_irqsave(&chip->lock, flags);
865 snd_cs4231_out(chip, CS4231_ALT_FEATURE_2, chip->image[CS4231_ALT_FEATURE_2]);
866 spin_unlock_irqrestore(&chip->lock, flags);
867
868 snd_cs4231_mce_up(chip);
869 spin_lock_irqsave(&chip->lock, flags);
870 snd_cs4231_out(chip, CS4231_PLAYBK_FORMAT, chip->image[CS4231_PLAYBK_FORMAT]);
871 spin_unlock_irqrestore(&chip->lock, flags);
872 snd_cs4231_mce_down(chip);
873
874#ifdef SNDRV_DEBUG_MCE
Christopher Zimmermanna1314302005-09-21 00:41:22 -0700875 snd_printdd("init: (4)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876#endif
877
878 snd_cs4231_mce_up(chip);
879 spin_lock_irqsave(&chip->lock, flags);
880 snd_cs4231_out(chip, CS4231_REC_FORMAT, chip->image[CS4231_REC_FORMAT]);
881 spin_unlock_irqrestore(&chip->lock, flags);
882 snd_cs4231_mce_down(chip);
883
884#ifdef SNDRV_DEBUG_MCE
Christopher Zimmermanna1314302005-09-21 00:41:22 -0700885 snd_printdd("init: (5)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886#endif
887}
888
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100889static int snd_cs4231_open(struct snd_cs4231 *chip, unsigned int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890{
891 unsigned long flags;
892
Ingo Molnar12aa7572006-01-16 16:36:05 +0100893 mutex_lock(&chip->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 if ((chip->mode & mode)) {
Ingo Molnar12aa7572006-01-16 16:36:05 +0100895 mutex_unlock(&chip->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 return -EAGAIN;
897 }
898 if (chip->mode & CS4231_MODE_OPEN) {
899 chip->mode |= mode;
Ingo Molnar12aa7572006-01-16 16:36:05 +0100900 mutex_unlock(&chip->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 return 0;
902 }
903 /* ok. now enable and ack CODEC IRQ */
904 spin_lock_irqsave(&chip->lock, flags);
905 snd_cs4231_out(chip, CS4231_IRQ_STATUS, CS4231_PLAYBACK_IRQ |
906 CS4231_RECORD_IRQ |
907 CS4231_TIMER_IRQ);
908 snd_cs4231_out(chip, CS4231_IRQ_STATUS, 0);
909 __cs4231_writeb(chip, 0, CS4231P(chip, STATUS)); /* clear IRQ */
910 __cs4231_writeb(chip, 0, CS4231P(chip, STATUS)); /* clear IRQ */
911
912 snd_cs4231_out(chip, CS4231_IRQ_STATUS, CS4231_PLAYBACK_IRQ |
913 CS4231_RECORD_IRQ |
914 CS4231_TIMER_IRQ);
915 snd_cs4231_out(chip, CS4231_IRQ_STATUS, 0);
Christopher Zimmermanna1314302005-09-21 00:41:22 -0700916
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 spin_unlock_irqrestore(&chip->lock, flags);
918
919 chip->mode = mode;
Ingo Molnar12aa7572006-01-16 16:36:05 +0100920 mutex_unlock(&chip->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 return 0;
922}
923
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100924static void snd_cs4231_close(struct snd_cs4231 *chip, unsigned int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925{
926 unsigned long flags;
927
Ingo Molnar12aa7572006-01-16 16:36:05 +0100928 mutex_lock(&chip->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 chip->mode &= ~mode;
930 if (chip->mode & CS4231_MODE_OPEN) {
Ingo Molnar12aa7572006-01-16 16:36:05 +0100931 mutex_unlock(&chip->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 return;
933 }
934 snd_cs4231_calibrate_mute(chip, 1);
935
936 /* disable IRQ */
937 spin_lock_irqsave(&chip->lock, flags);
938 snd_cs4231_out(chip, CS4231_IRQ_STATUS, 0);
939 __cs4231_writeb(chip, 0, CS4231P(chip, STATUS)); /* clear IRQ */
940 __cs4231_writeb(chip, 0, CS4231P(chip, STATUS)); /* clear IRQ */
941
942 /* now disable record & playback */
943
944 if (chip->image[CS4231_IFACE_CTRL] &
945 (CS4231_PLAYBACK_ENABLE | CS4231_PLAYBACK_PIO |
946 CS4231_RECORD_ENABLE | CS4231_RECORD_PIO)) {
947 spin_unlock_irqrestore(&chip->lock, flags);
948 snd_cs4231_mce_up(chip);
949 spin_lock_irqsave(&chip->lock, flags);
950 chip->image[CS4231_IFACE_CTRL] &=
951 ~(CS4231_PLAYBACK_ENABLE | CS4231_PLAYBACK_PIO |
952 CS4231_RECORD_ENABLE | CS4231_RECORD_PIO);
953 snd_cs4231_out(chip, CS4231_IFACE_CTRL, chip->image[CS4231_IFACE_CTRL]);
954 spin_unlock_irqrestore(&chip->lock, flags);
955 snd_cs4231_mce_down(chip);
956 spin_lock_irqsave(&chip->lock, flags);
957 }
958
959 /* clear IRQ again */
960 snd_cs4231_out(chip, CS4231_IRQ_STATUS, 0);
961 __cs4231_writeb(chip, 0, CS4231P(chip, STATUS)); /* clear IRQ */
962 __cs4231_writeb(chip, 0, CS4231P(chip, STATUS)); /* clear IRQ */
963 spin_unlock_irqrestore(&chip->lock, flags);
964
965 snd_cs4231_calibrate_mute(chip, 0);
966
967 chip->mode = 0;
Ingo Molnar12aa7572006-01-16 16:36:05 +0100968 mutex_unlock(&chip->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969}
970
971/*
972 * timer open/close
973 */
974
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100975static int snd_cs4231_timer_open(struct snd_timer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976{
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100977 struct snd_cs4231 *chip = snd_timer_chip(timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 snd_cs4231_open(chip, CS4231_MODE_TIMER);
979 return 0;
980}
981
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100982static int snd_cs4231_timer_close(struct snd_timer * timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983{
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100984 struct snd_cs4231 *chip = snd_timer_chip(timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 snd_cs4231_close(chip, CS4231_MODE_TIMER);
986 return 0;
987}
988
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100989static struct snd_timer_hardware snd_cs4231_timer_table =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990{
991 .flags = SNDRV_TIMER_HW_AUTO,
992 .resolution = 9945,
993 .ticks = 65535,
994 .open = snd_cs4231_timer_open,
995 .close = snd_cs4231_timer_close,
996 .c_resolution = snd_cs4231_timer_resolution,
997 .start = snd_cs4231_timer_start,
998 .stop = snd_cs4231_timer_stop,
999};
1000
1001/*
1002 * ok.. exported functions..
1003 */
1004
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001005static int snd_cs4231_playback_hw_params(struct snd_pcm_substream *substream,
1006 struct snd_pcm_hw_params *hw_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007{
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001008 struct snd_cs4231 *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 unsigned char new_pdfr;
1010 int err;
1011
1012 if ((err = snd_pcm_lib_malloc_pages(substream,
1013 params_buffer_bytes(hw_params))) < 0)
1014 return err;
1015 new_pdfr = snd_cs4231_get_format(chip, params_format(hw_params),
1016 params_channels(hw_params)) |
1017 snd_cs4231_get_rate(params_rate(hw_params));
1018 snd_cs4231_playback_format(chip, hw_params, new_pdfr);
1019
1020 return 0;
1021}
1022
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001023static int snd_cs4231_playback_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024{
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001025 struct snd_cs4231 *chip = snd_pcm_substream_chip(substream);
1026 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 unsigned long flags;
1028
1029 spin_lock_irqsave(&chip->lock, flags);
Christopher Zimmermanna1314302005-09-21 00:41:22 -07001030
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 chip->image[CS4231_IFACE_CTRL] &= ~(CS4231_PLAYBACK_ENABLE |
1032 CS4231_PLAYBACK_PIO);
Christopher Zimmermanna1314302005-09-21 00:41:22 -07001033
Eric Sesterhenn817dd6e2006-03-24 18:49:12 +01001034 BUG_ON(runtime->period_size > 0xffff + 1);
Christopher Zimmermanna1314302005-09-21 00:41:22 -07001035
Christopher Zimmermanna1314302005-09-21 00:41:22 -07001036 chip->p_periods_sent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 spin_unlock_irqrestore(&chip->lock, flags);
1038
1039 return 0;
1040}
1041
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001042static int snd_cs4231_capture_hw_params(struct snd_pcm_substream *substream,
1043 struct snd_pcm_hw_params *hw_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044{
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001045 struct snd_cs4231 *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 unsigned char new_cdfr;
1047 int err;
1048
1049 if ((err = snd_pcm_lib_malloc_pages(substream,
1050 params_buffer_bytes(hw_params))) < 0)
1051 return err;
1052 new_cdfr = snd_cs4231_get_format(chip, params_format(hw_params),
1053 params_channels(hw_params)) |
1054 snd_cs4231_get_rate(params_rate(hw_params));
1055 snd_cs4231_capture_format(chip, hw_params, new_cdfr);
1056
1057 return 0;
1058}
1059
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001060static int snd_cs4231_capture_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061{
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001062 struct snd_cs4231 *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 unsigned long flags;
1064
1065 spin_lock_irqsave(&chip->lock, flags);
1066 chip->image[CS4231_IFACE_CTRL] &= ~(CS4231_RECORD_ENABLE |
1067 CS4231_RECORD_PIO);
1068
Christopher Zimmermanna1314302005-09-21 00:41:22 -07001069
Georg Chini5a820fa2005-11-07 14:08:25 -08001070 chip->c_periods_sent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 spin_unlock_irqrestore(&chip->lock, flags);
1072
1073 return 0;
1074}
1075
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001076static void snd_cs4231_overrange(struct snd_cs4231 *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077{
1078 unsigned long flags;
1079 unsigned char res;
1080
1081 spin_lock_irqsave(&chip->lock, flags);
1082 res = snd_cs4231_in(chip, CS4231_TEST_INIT);
1083 spin_unlock_irqrestore(&chip->lock, flags);
1084
1085 if (res & (0x08 | 0x02)) /* detect overrange only above 0dB; may be user selectable? */
1086 chip->capture_substream->runtime->overrange++;
1087}
1088
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001089static void snd_cs4231_play_callback(struct snd_cs4231 *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 if (chip->image[CS4231_IFACE_CTRL] & CS4231_PLAYBACK_ENABLE) {
1092 snd_pcm_period_elapsed(chip->playback_substream);
Georg Chinib1282542005-11-07 14:09:19 -08001093 snd_cs4231_advance_dma(&chip->p_dma, chip->playback_substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 &chip->p_periods_sent);
1095 }
1096}
1097
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001098static void snd_cs4231_capture_callback(struct snd_cs4231 *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 if (chip->image[CS4231_IFACE_CTRL] & CS4231_RECORD_ENABLE) {
1101 snd_pcm_period_elapsed(chip->capture_substream);
Georg Chinib1282542005-11-07 14:09:19 -08001102 snd_cs4231_advance_dma(&chip->c_dma, chip->capture_substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 &chip->c_periods_sent);
1104 }
1105}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001107static snd_pcm_uframes_t snd_cs4231_playback_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108{
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001109 struct snd_cs4231 *chip = snd_pcm_substream_chip(substream);
1110 struct cs4231_dma_control *dma_cont = &chip->p_dma;
Georg Chini5a820fa2005-11-07 14:08:25 -08001111 size_t ptr;
Georg Chini5a820fa2005-11-07 14:08:25 -08001112
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 if (!(chip->image[CS4231_IFACE_CTRL] & CS4231_PLAYBACK_ENABLE))
1114 return 0;
Georg Chinib1282542005-11-07 14:09:19 -08001115 ptr = dma_cont->address(dma_cont);
1116 if (ptr != 0)
1117 ptr -= substream->runtime->dma_addr;
1118
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 return bytes_to_frames(substream->runtime, ptr);
1120}
1121
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001122static snd_pcm_uframes_t snd_cs4231_capture_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123{
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001124 struct snd_cs4231 *chip = snd_pcm_substream_chip(substream);
1125 struct cs4231_dma_control *dma_cont = &chip->c_dma;
Georg Chini5a820fa2005-11-07 14:08:25 -08001126 size_t ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127
1128 if (!(chip->image[CS4231_IFACE_CTRL] & CS4231_RECORD_ENABLE))
1129 return 0;
Georg Chinib1282542005-11-07 14:09:19 -08001130 ptr = dma_cont->address(dma_cont);
1131 if (ptr != 0)
1132 ptr -= substream->runtime->dma_addr;
1133
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 return bytes_to_frames(substream->runtime, ptr);
1135}
1136
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001137static int __init snd_cs4231_probe(struct snd_cs4231 *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138{
1139 unsigned long flags;
1140 int i, id, vers;
1141 unsigned char *ptr;
1142
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 id = vers = 0;
1144 for (i = 0; i < 50; i++) {
1145 mb();
1146 if (__cs4231_readb(chip, CS4231P(chip, REGSEL)) & CS4231_INIT)
1147 udelay(2000);
1148 else {
1149 spin_lock_irqsave(&chip->lock, flags);
1150 snd_cs4231_out(chip, CS4231_MISC_INFO, CS4231_MODE2);
1151 id = snd_cs4231_in(chip, CS4231_MISC_INFO) & 0x0f;
1152 vers = snd_cs4231_in(chip, CS4231_VERSION);
1153 spin_unlock_irqrestore(&chip->lock, flags);
1154 if (id == 0x0a)
1155 break; /* this is valid value */
1156 }
1157 }
1158 snd_printdd("cs4231: port = %p, id = 0x%x\n", chip->port, id);
1159 if (id != 0x0a)
1160 return -ENODEV; /* no valid device found */
1161
1162 spin_lock_irqsave(&chip->lock, flags);
1163
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 __cs4231_readb(chip, CS4231P(chip, STATUS)); /* clear any pendings IRQ */
1165 __cs4231_writeb(chip, 0, CS4231P(chip, STATUS));
1166 mb();
1167
1168 spin_unlock_irqrestore(&chip->lock, flags);
1169
1170 chip->image[CS4231_MISC_INFO] = CS4231_MODE2;
1171 chip->image[CS4231_IFACE_CTRL] =
1172 chip->image[CS4231_IFACE_CTRL] & ~CS4231_SINGLE_DMA;
1173 chip->image[CS4231_ALT_FEATURE_1] = 0x80;
1174 chip->image[CS4231_ALT_FEATURE_2] = 0x01;
1175 if (vers & 0x20)
1176 chip->image[CS4231_ALT_FEATURE_2] |= 0x02;
1177
1178 ptr = (unsigned char *) &chip->image;
1179
1180 snd_cs4231_mce_down(chip);
1181
1182 spin_lock_irqsave(&chip->lock, flags);
1183
1184 for (i = 0; i < 32; i++) /* ok.. fill all CS4231 registers */
1185 snd_cs4231_out(chip, i, *ptr++);
1186
1187 spin_unlock_irqrestore(&chip->lock, flags);
1188
1189 snd_cs4231_mce_up(chip);
1190
1191 snd_cs4231_mce_down(chip);
1192
1193 mdelay(2);
1194
1195 return 0; /* all things are ok.. */
1196}
1197
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001198static struct snd_pcm_hardware snd_cs4231_playback =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199{
1200 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1201 SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_SYNC_START),
1202 .formats = (SNDRV_PCM_FMTBIT_MU_LAW | SNDRV_PCM_FMTBIT_A_LAW |
1203 SNDRV_PCM_FMTBIT_IMA_ADPCM |
1204 SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE |
1205 SNDRV_PCM_FMTBIT_S16_BE),
1206 .rates = SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_8000_48000,
1207 .rate_min = 5510,
1208 .rate_max = 48000,
1209 .channels_min = 1,
1210 .channels_max = 2,
1211 .buffer_bytes_max = (32*1024),
David S. Millerf9af1d92007-01-03 18:51:54 -08001212 .period_bytes_min = 64,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 .period_bytes_max = (32*1024),
1214 .periods_min = 1,
1215 .periods_max = 1024,
1216};
1217
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001218static struct snd_pcm_hardware snd_cs4231_capture =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219{
1220 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1221 SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_SYNC_START),
1222 .formats = (SNDRV_PCM_FMTBIT_MU_LAW | SNDRV_PCM_FMTBIT_A_LAW |
1223 SNDRV_PCM_FMTBIT_IMA_ADPCM |
1224 SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE |
1225 SNDRV_PCM_FMTBIT_S16_BE),
1226 .rates = SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_8000_48000,
1227 .rate_min = 5510,
1228 .rate_max = 48000,
1229 .channels_min = 1,
1230 .channels_max = 2,
1231 .buffer_bytes_max = (32*1024),
David S. Millerf9af1d92007-01-03 18:51:54 -08001232 .period_bytes_min = 64,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 .period_bytes_max = (32*1024),
1234 .periods_min = 1,
1235 .periods_max = 1024,
1236};
1237
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001238static int snd_cs4231_playback_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239{
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001240 struct snd_cs4231 *chip = snd_pcm_substream_chip(substream);
1241 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 int err;
1243
1244 runtime->hw = snd_cs4231_playback;
1245
1246 if ((err = snd_cs4231_open(chip, CS4231_MODE_PLAY)) < 0) {
1247 snd_free_pages(runtime->dma_area, runtime->dma_bytes);
1248 return err;
1249 }
1250 chip->playback_substream = substream;
1251 chip->p_periods_sent = 0;
1252 snd_pcm_set_sync(substream);
1253 snd_cs4231_xrate(runtime);
1254
1255 return 0;
1256}
1257
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001258static int snd_cs4231_capture_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259{
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001260 struct snd_cs4231 *chip = snd_pcm_substream_chip(substream);
1261 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 int err;
1263
1264 runtime->hw = snd_cs4231_capture;
1265
1266 if ((err = snd_cs4231_open(chip, CS4231_MODE_RECORD)) < 0) {
1267 snd_free_pages(runtime->dma_area, runtime->dma_bytes);
1268 return err;
1269 }
1270 chip->capture_substream = substream;
1271 chip->c_periods_sent = 0;
1272 snd_pcm_set_sync(substream);
1273 snd_cs4231_xrate(runtime);
1274
1275 return 0;
1276}
1277
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001278static int snd_cs4231_playback_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279{
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001280 struct snd_cs4231 *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 snd_cs4231_close(chip, CS4231_MODE_PLAY);
Georg Chinib1282542005-11-07 14:09:19 -08001283 chip->playback_substream = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284
1285 return 0;
1286}
1287
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001288static int snd_cs4231_capture_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289{
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001290 struct snd_cs4231 *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 snd_cs4231_close(chip, CS4231_MODE_RECORD);
Georg Chinib1282542005-11-07 14:09:19 -08001293 chip->capture_substream = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294
1295 return 0;
1296}
1297
1298/* XXX We can do some power-management, in particular on EBUS using
1299 * XXX the audio AUXIO register...
1300 */
1301
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001302static struct snd_pcm_ops snd_cs4231_playback_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 .open = snd_cs4231_playback_open,
1304 .close = snd_cs4231_playback_close,
1305 .ioctl = snd_pcm_lib_ioctl,
1306 .hw_params = snd_cs4231_playback_hw_params,
Krzysztof Heltc6c2d572007-09-04 13:08:24 +02001307 .hw_free = snd_pcm_lib_free_pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 .prepare = snd_cs4231_playback_prepare,
1309 .trigger = snd_cs4231_trigger,
1310 .pointer = snd_cs4231_playback_pointer,
1311};
1312
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001313static struct snd_pcm_ops snd_cs4231_capture_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 .open = snd_cs4231_capture_open,
1315 .close = snd_cs4231_capture_close,
1316 .ioctl = snd_pcm_lib_ioctl,
1317 .hw_params = snd_cs4231_capture_hw_params,
Krzysztof Heltc6c2d572007-09-04 13:08:24 +02001318 .hw_free = snd_pcm_lib_free_pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 .prepare = snd_cs4231_capture_prepare,
1320 .trigger = snd_cs4231_trigger,
1321 .pointer = snd_cs4231_capture_pointer,
1322};
1323
Krzysztof Heltc6c2d572007-09-04 13:08:24 +02001324static int __init snd_cs4231_pcm(struct snd_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325{
Krzysztof Heltc6c2d572007-09-04 13:08:24 +02001326 struct snd_cs4231 *chip = card->private_data;
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001327 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 int err;
1329
Krzysztof Heltc6c2d572007-09-04 13:08:24 +02001330 err = snd_pcm_new(card, "CS4231", 0, 1, 1, &pcm);
1331 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 return err;
1333
1334 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_cs4231_playback_ops);
1335 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_cs4231_capture_ops);
1336
1337 /* global setup */
1338 pcm->private_data = chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 pcm->info_flags = SNDRV_PCM_INFO_JOINT_DUPLEX;
1340 strcpy(pcm->name, "CS4231");
1341
Georg Chinib1282542005-11-07 14:09:19 -08001342 chip->p_dma.preallocate(chip, pcm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343
1344 chip->pcm = pcm;
1345
1346 return 0;
1347}
1348
Krzysztof Heltc6c2d572007-09-04 13:08:24 +02001349static int __init snd_cs4231_timer(struct snd_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350{
Krzysztof Heltc6c2d572007-09-04 13:08:24 +02001351 struct snd_cs4231 *chip = card->private_data;
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001352 struct snd_timer *timer;
1353 struct snd_timer_id tid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 int err;
1355
1356 /* Timer initialization */
1357 tid.dev_class = SNDRV_TIMER_CLASS_CARD;
1358 tid.dev_sclass = SNDRV_TIMER_SCLASS_NONE;
Krzysztof Heltc6c2d572007-09-04 13:08:24 +02001359 tid.card = card->number;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 tid.device = 0;
1361 tid.subdevice = 0;
Krzysztof Heltc6c2d572007-09-04 13:08:24 +02001362 err = snd_timer_new(card, "CS4231", &tid, &timer);
1363 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 return err;
1365 strcpy(timer->name, "CS4231");
1366 timer->private_data = chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 timer->hw = snd_cs4231_timer_table;
1368 chip->timer = timer;
1369
1370 return 0;
1371}
1372
1373/*
1374 * MIXER part
1375 */
1376
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001377static int snd_cs4231_info_mux(struct snd_kcontrol *kcontrol,
1378 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379{
1380 static char *texts[4] = {
1381 "Line", "CD", "Mic", "Mix"
1382 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1385 uinfo->count = 2;
1386 uinfo->value.enumerated.items = 4;
1387 if (uinfo->value.enumerated.item > 3)
1388 uinfo->value.enumerated.item = 3;
1389 strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
1390
1391 return 0;
1392}
1393
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001394static int snd_cs4231_get_mux(struct snd_kcontrol *kcontrol,
1395 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396{
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001397 struct snd_cs4231 *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 unsigned long flags;
1399
1400 spin_lock_irqsave(&chip->lock, flags);
1401 ucontrol->value.enumerated.item[0] =
1402 (chip->image[CS4231_LEFT_INPUT] & CS4231_MIXS_ALL) >> 6;
1403 ucontrol->value.enumerated.item[1] =
1404 (chip->image[CS4231_RIGHT_INPUT] & CS4231_MIXS_ALL) >> 6;
1405 spin_unlock_irqrestore(&chip->lock, flags);
1406
1407 return 0;
1408}
1409
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001410static int snd_cs4231_put_mux(struct snd_kcontrol *kcontrol,
1411 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412{
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001413 struct snd_cs4231 *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 unsigned long flags;
1415 unsigned short left, right;
1416 int change;
1417
1418 if (ucontrol->value.enumerated.item[0] > 3 ||
1419 ucontrol->value.enumerated.item[1] > 3)
1420 return -EINVAL;
1421 left = ucontrol->value.enumerated.item[0] << 6;
1422 right = ucontrol->value.enumerated.item[1] << 6;
1423
1424 spin_lock_irqsave(&chip->lock, flags);
1425
1426 left = (chip->image[CS4231_LEFT_INPUT] & ~CS4231_MIXS_ALL) | left;
1427 right = (chip->image[CS4231_RIGHT_INPUT] & ~CS4231_MIXS_ALL) | right;
1428 change = left != chip->image[CS4231_LEFT_INPUT] ||
1429 right != chip->image[CS4231_RIGHT_INPUT];
1430 snd_cs4231_out(chip, CS4231_LEFT_INPUT, left);
1431 snd_cs4231_out(chip, CS4231_RIGHT_INPUT, right);
1432
1433 spin_unlock_irqrestore(&chip->lock, flags);
1434
1435 return change;
1436}
1437
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001438static int snd_cs4231_info_single(struct snd_kcontrol *kcontrol,
1439 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440{
1441 int mask = (kcontrol->private_value >> 16) & 0xff;
1442
1443 uinfo->type = (mask == 1) ?
1444 SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
1445 uinfo->count = 1;
1446 uinfo->value.integer.min = 0;
1447 uinfo->value.integer.max = mask;
1448
1449 return 0;
1450}
1451
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001452static int snd_cs4231_get_single(struct snd_kcontrol *kcontrol,
1453 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454{
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001455 struct snd_cs4231 *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 unsigned long flags;
1457 int reg = kcontrol->private_value & 0xff;
1458 int shift = (kcontrol->private_value >> 8) & 0xff;
1459 int mask = (kcontrol->private_value >> 16) & 0xff;
1460 int invert = (kcontrol->private_value >> 24) & 0xff;
1461
1462 spin_lock_irqsave(&chip->lock, flags);
1463
1464 ucontrol->value.integer.value[0] = (chip->image[reg] >> shift) & mask;
1465
1466 spin_unlock_irqrestore(&chip->lock, flags);
1467
1468 if (invert)
1469 ucontrol->value.integer.value[0] =
1470 (mask - ucontrol->value.integer.value[0]);
1471
1472 return 0;
1473}
1474
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001475static int snd_cs4231_put_single(struct snd_kcontrol *kcontrol,
1476 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477{
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001478 struct snd_cs4231 *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 unsigned long flags;
1480 int reg = kcontrol->private_value & 0xff;
1481 int shift = (kcontrol->private_value >> 8) & 0xff;
1482 int mask = (kcontrol->private_value >> 16) & 0xff;
1483 int invert = (kcontrol->private_value >> 24) & 0xff;
1484 int change;
1485 unsigned short val;
1486
1487 val = (ucontrol->value.integer.value[0] & mask);
1488 if (invert)
1489 val = mask - val;
1490 val <<= shift;
1491
1492 spin_lock_irqsave(&chip->lock, flags);
1493
1494 val = (chip->image[reg] & ~(mask << shift)) | val;
1495 change = val != chip->image[reg];
1496 snd_cs4231_out(chip, reg, val);
1497
1498 spin_unlock_irqrestore(&chip->lock, flags);
1499
1500 return change;
1501}
1502
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001503static int snd_cs4231_info_double(struct snd_kcontrol *kcontrol,
1504 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505{
1506 int mask = (kcontrol->private_value >> 24) & 0xff;
1507
1508 uinfo->type = mask == 1 ?
1509 SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
1510 uinfo->count = 2;
1511 uinfo->value.integer.min = 0;
1512 uinfo->value.integer.max = mask;
1513
1514 return 0;
1515}
1516
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001517static int snd_cs4231_get_double(struct snd_kcontrol *kcontrol,
1518 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519{
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001520 struct snd_cs4231 *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 unsigned long flags;
1522 int left_reg = kcontrol->private_value & 0xff;
1523 int right_reg = (kcontrol->private_value >> 8) & 0xff;
1524 int shift_left = (kcontrol->private_value >> 16) & 0x07;
1525 int shift_right = (kcontrol->private_value >> 19) & 0x07;
1526 int mask = (kcontrol->private_value >> 24) & 0xff;
1527 int invert = (kcontrol->private_value >> 22) & 1;
1528
1529 spin_lock_irqsave(&chip->lock, flags);
1530
1531 ucontrol->value.integer.value[0] = (chip->image[left_reg] >> shift_left) & mask;
1532 ucontrol->value.integer.value[1] = (chip->image[right_reg] >> shift_right) & mask;
1533
1534 spin_unlock_irqrestore(&chip->lock, flags);
1535
1536 if (invert) {
1537 ucontrol->value.integer.value[0] =
1538 (mask - ucontrol->value.integer.value[0]);
1539 ucontrol->value.integer.value[1] =
1540 (mask - ucontrol->value.integer.value[1]);
1541 }
1542
1543 return 0;
1544}
1545
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001546static int snd_cs4231_put_double(struct snd_kcontrol *kcontrol,
1547 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548{
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001549 struct snd_cs4231 *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 unsigned long flags;
1551 int left_reg = kcontrol->private_value & 0xff;
1552 int right_reg = (kcontrol->private_value >> 8) & 0xff;
1553 int shift_left = (kcontrol->private_value >> 16) & 0x07;
1554 int shift_right = (kcontrol->private_value >> 19) & 0x07;
1555 int mask = (kcontrol->private_value >> 24) & 0xff;
1556 int invert = (kcontrol->private_value >> 22) & 1;
1557 int change;
1558 unsigned short val1, val2;
1559
1560 val1 = ucontrol->value.integer.value[0] & mask;
1561 val2 = ucontrol->value.integer.value[1] & mask;
1562 if (invert) {
1563 val1 = mask - val1;
1564 val2 = mask - val2;
1565 }
1566 val1 <<= shift_left;
1567 val2 <<= shift_right;
1568
1569 spin_lock_irqsave(&chip->lock, flags);
1570
1571 val1 = (chip->image[left_reg] & ~(mask << shift_left)) | val1;
1572 val2 = (chip->image[right_reg] & ~(mask << shift_right)) | val2;
1573 change = val1 != chip->image[left_reg] || val2 != chip->image[right_reg];
1574 snd_cs4231_out(chip, left_reg, val1);
1575 snd_cs4231_out(chip, right_reg, val2);
1576
1577 spin_unlock_irqrestore(&chip->lock, flags);
1578
1579 return change;
1580}
1581
1582#define CS4231_SINGLE(xname, xindex, reg, shift, mask, invert) \
1583{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
1584 .info = snd_cs4231_info_single, \
1585 .get = snd_cs4231_get_single, .put = snd_cs4231_put_single, \
1586 .private_value = reg | (shift << 8) | (mask << 16) | (invert << 24) }
1587
1588#define CS4231_DOUBLE(xname, xindex, left_reg, right_reg, shift_left, shift_right, mask, invert) \
1589{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
1590 .info = snd_cs4231_info_double, \
1591 .get = snd_cs4231_get_double, .put = snd_cs4231_put_double, \
1592 .private_value = left_reg | (right_reg << 8) | (shift_left << 16) | (shift_right << 19) | (mask << 24) | (invert << 22) }
1593
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001594static struct snd_kcontrol_new snd_cs4231_controls[] __initdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595CS4231_DOUBLE("PCM Playback Switch", 0, CS4231_LEFT_OUTPUT, CS4231_RIGHT_OUTPUT, 7, 7, 1, 1),
1596CS4231_DOUBLE("PCM Playback Volume", 0, CS4231_LEFT_OUTPUT, CS4231_RIGHT_OUTPUT, 0, 0, 63, 1),
1597CS4231_DOUBLE("Line Playback Switch", 0, CS4231_LEFT_LINE_IN, CS4231_RIGHT_LINE_IN, 7, 7, 1, 1),
1598CS4231_DOUBLE("Line Playback Volume", 0, CS4231_LEFT_LINE_IN, CS4231_RIGHT_LINE_IN, 0, 0, 31, 1),
1599CS4231_DOUBLE("Aux Playback Switch", 0, CS4231_AUX1_LEFT_INPUT, CS4231_AUX1_RIGHT_INPUT, 7, 7, 1, 1),
1600CS4231_DOUBLE("Aux Playback Volume", 0, CS4231_AUX1_LEFT_INPUT, CS4231_AUX1_RIGHT_INPUT, 0, 0, 31, 1),
1601CS4231_DOUBLE("Aux Playback Switch", 1, CS4231_AUX2_LEFT_INPUT, CS4231_AUX2_RIGHT_INPUT, 7, 7, 1, 1),
1602CS4231_DOUBLE("Aux Playback Volume", 1, CS4231_AUX2_LEFT_INPUT, CS4231_AUX2_RIGHT_INPUT, 0, 0, 31, 1),
1603CS4231_SINGLE("Mono Playback Switch", 0, CS4231_MONO_CTRL, 7, 1, 1),
1604CS4231_SINGLE("Mono Playback Volume", 0, CS4231_MONO_CTRL, 0, 15, 1),
1605CS4231_SINGLE("Mono Output Playback Switch", 0, CS4231_MONO_CTRL, 6, 1, 1),
1606CS4231_SINGLE("Mono Output Playback Bypass", 0, CS4231_MONO_CTRL, 5, 1, 0),
1607CS4231_DOUBLE("Capture Volume", 0, CS4231_LEFT_INPUT, CS4231_RIGHT_INPUT, 0, 0, 15, 0),
1608{
1609 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1610 .name = "Capture Source",
1611 .info = snd_cs4231_info_mux,
1612 .get = snd_cs4231_get_mux,
1613 .put = snd_cs4231_put_mux,
1614},
1615CS4231_DOUBLE("Mic Boost", 0, CS4231_LEFT_INPUT, CS4231_RIGHT_INPUT, 5, 5, 1, 0),
1616CS4231_SINGLE("Loopback Capture Switch", 0, CS4231_LOOPBACK, 0, 1, 0),
1617CS4231_SINGLE("Loopback Capture Volume", 0, CS4231_LOOPBACK, 2, 63, 1),
1618/* SPARC specific uses of XCTL{0,1} general purpose outputs. */
1619CS4231_SINGLE("Line Out Switch", 0, CS4231_PIN_CTRL, 6, 1, 1),
1620CS4231_SINGLE("Headphone Out Switch", 0, CS4231_PIN_CTRL, 7, 1, 1)
1621};
1622
Krzysztof Heltc6c2d572007-09-04 13:08:24 +02001623static int __init snd_cs4231_mixer(struct snd_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624{
Krzysztof Heltc6c2d572007-09-04 13:08:24 +02001625 struct snd_cs4231 *chip = card->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 int err, idx;
1627
1628 snd_assert(chip != NULL && chip->pcm != NULL, return -EINVAL);
1629
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 strcpy(card->mixername, chip->pcm->name);
1631
1632 for (idx = 0; idx < ARRAY_SIZE(snd_cs4231_controls); idx++) {
Krzysztof Heltc6c2d572007-09-04 13:08:24 +02001633 err = snd_ctl_add(card,
1634 snd_ctl_new1(&snd_cs4231_controls[idx], chip));
1635 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 return err;
1637 }
1638 return 0;
1639}
1640
1641static int dev;
1642
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001643static int __init cs4231_attach_begin(struct snd_card **rcard)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644{
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001645 struct snd_card *card;
Krzysztof Heltc6c2d572007-09-04 13:08:24 +02001646 struct snd_cs4231 *chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647
1648 *rcard = NULL;
1649
1650 if (dev >= SNDRV_CARDS)
1651 return -ENODEV;
1652
1653 if (!enable[dev]) {
1654 dev++;
1655 return -ENOENT;
1656 }
1657
Krzysztof Heltc6c2d572007-09-04 13:08:24 +02001658 card = snd_card_new(index[dev], id[dev], THIS_MODULE,
1659 sizeof(struct snd_cs4231));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 if (card == NULL)
1661 return -ENOMEM;
1662
1663 strcpy(card->driver, "CS4231");
1664 strcpy(card->shortname, "Sun CS4231");
1665
Krzysztof Heltc6c2d572007-09-04 13:08:24 +02001666 chip = card->private_data;
1667 chip->card = card;
1668
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 *rcard = card;
1670 return 0;
1671}
1672
Krzysztof Heltc6c2d572007-09-04 13:08:24 +02001673static int __init cs4231_attach_finish(struct snd_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674{
Krzysztof Heltc6c2d572007-09-04 13:08:24 +02001675 struct snd_cs4231 *chip = card->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 int err;
1677
Krzysztof Heltc6c2d572007-09-04 13:08:24 +02001678 err = snd_cs4231_pcm(card);
1679 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 goto out_err;
1681
Krzysztof Heltc6c2d572007-09-04 13:08:24 +02001682 err = snd_cs4231_mixer(card);
1683 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 goto out_err;
1685
Krzysztof Heltc6c2d572007-09-04 13:08:24 +02001686 err = snd_cs4231_timer(card);
1687 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 goto out_err;
1689
Krzysztof Heltc6c2d572007-09-04 13:08:24 +02001690 err = snd_card_register(card);
1691 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 goto out_err;
1693
1694 chip->next = cs4231_list;
1695 cs4231_list = chip;
1696
1697 dev++;
1698 return 0;
1699
1700out_err:
1701 snd_card_free(card);
1702 return err;
1703}
1704
1705#ifdef SBUS_SUPPORT
Georg Chinib1282542005-11-07 14:09:19 -08001706
David Howells7d12e782006-10-05 14:55:46 +01001707static irqreturn_t snd_cs4231_sbus_interrupt(int irq, void *dev_id)
Georg Chinib1282542005-11-07 14:09:19 -08001708{
1709 unsigned long flags;
1710 unsigned char status;
1711 u32 csr;
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001712 struct snd_cs4231 *chip = dev_id;
Georg Chinib1282542005-11-07 14:09:19 -08001713
1714 /*This is IRQ is not raised by the cs4231*/
1715 if (!(__cs4231_readb(chip, CS4231P(chip, STATUS)) & CS4231_GLOBALIRQ))
1716 return IRQ_NONE;
1717
1718 /* ACK the APC interrupt. */
1719 csr = sbus_readl(chip->port + APCCSR);
1720
1721 sbus_writel(csr, chip->port + APCCSR);
1722
1723 if ((csr & APC_PDMA_READY) &&
1724 (csr & APC_PLAY_INT) &&
1725 (csr & APC_XINT_PNVA) &&
1726 !(csr & APC_XINT_EMPT))
1727 snd_cs4231_play_callback(chip);
1728
1729 if ((csr & APC_CDMA_READY) &&
1730 (csr & APC_CAPT_INT) &&
1731 (csr & APC_XINT_CNVA) &&
1732 !(csr & APC_XINT_EMPT))
1733 snd_cs4231_capture_callback(chip);
1734
1735 status = snd_cs4231_in(chip, CS4231_IRQ_STATUS);
1736
1737 if (status & CS4231_TIMER_IRQ) {
1738 if (chip->timer)
1739 snd_timer_interrupt(chip->timer, chip->timer->sticks);
1740 }
1741
1742 if ((status & CS4231_RECORD_IRQ) && (csr & APC_CDMA_READY))
1743 snd_cs4231_overrange(chip);
1744
1745 /* ACK the CS4231 interrupt. */
1746 spin_lock_irqsave(&chip->lock, flags);
1747 snd_cs4231_outm(chip, CS4231_IRQ_STATUS, ~CS4231_ALL_IRQS | ~status, 0);
1748 spin_unlock_irqrestore(&chip->lock, flags);
1749
Georg Chinid35a1b92007-01-02 21:28:17 -08001750 return IRQ_HANDLED;
Georg Chinib1282542005-11-07 14:09:19 -08001751}
1752
1753/*
1754 * SBUS DMA routines
1755 */
1756
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001757static int sbus_dma_request(struct cs4231_dma_control *dma_cont, dma_addr_t bus_addr, size_t len)
Georg Chinib1282542005-11-07 14:09:19 -08001758{
1759 unsigned long flags;
1760 u32 test, csr;
1761 int err;
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001762 struct sbus_dma_info *base = &dma_cont->sbus_info;
Georg Chinib1282542005-11-07 14:09:19 -08001763
1764 if (len >= (1 << 24))
1765 return -EINVAL;
1766 spin_lock_irqsave(&base->lock, flags);
1767 csr = sbus_readl(base->regs + APCCSR);
1768 err = -EINVAL;
1769 test = APC_CDMA_READY;
1770 if ( base->dir == APC_PLAY )
1771 test = APC_PDMA_READY;
1772 if (!(csr & test))
1773 goto out;
1774 err = -EBUSY;
Georg Chinib1282542005-11-07 14:09:19 -08001775 test = APC_XINT_CNVA;
1776 if ( base->dir == APC_PLAY )
1777 test = APC_XINT_PNVA;
1778 if (!(csr & test))
1779 goto out;
1780 err = 0;
1781 sbus_writel(bus_addr, base->regs + base->dir + APCNVA);
1782 sbus_writel(len, base->regs + base->dir + APCNC);
1783out:
1784 spin_unlock_irqrestore(&base->lock, flags);
1785 return err;
1786}
1787
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001788static void sbus_dma_prepare(struct cs4231_dma_control *dma_cont, int d)
Georg Chinib1282542005-11-07 14:09:19 -08001789{
1790 unsigned long flags;
1791 u32 csr, test;
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001792 struct sbus_dma_info *base = &dma_cont->sbus_info;
Georg Chinib1282542005-11-07 14:09:19 -08001793
1794 spin_lock_irqsave(&base->lock, flags);
1795 csr = sbus_readl(base->regs + APCCSR);
1796 test = APC_GENL_INT | APC_PLAY_INT | APC_XINT_ENA |
1797 APC_XINT_PLAY | APC_XINT_PEMP | APC_XINT_GENL |
1798 APC_XINT_PENA;
1799 if ( base->dir == APC_RECORD )
1800 test = APC_GENL_INT | APC_CAPT_INT | APC_XINT_ENA |
1801 APC_XINT_CAPT | APC_XINT_CEMP | APC_XINT_GENL;
1802 csr |= test;
1803 sbus_writel(csr, base->regs + APCCSR);
1804 spin_unlock_irqrestore(&base->lock, flags);
1805}
1806
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001807static void sbus_dma_enable(struct cs4231_dma_control *dma_cont, int on)
Georg Chinib1282542005-11-07 14:09:19 -08001808{
1809 unsigned long flags;
1810 u32 csr, shift;
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001811 struct sbus_dma_info *base = &dma_cont->sbus_info;
Georg Chinib1282542005-11-07 14:09:19 -08001812
1813 spin_lock_irqsave(&base->lock, flags);
1814 if (!on) {
Georg Chinid35a1b92007-01-02 21:28:17 -08001815 sbus_writel(0, base->regs + base->dir + APCNC);
1816 sbus_writel(0, base->regs + base->dir + APCNVA);
Georg Chini3daadf32007-08-01 21:55:58 -07001817 if ( base->dir == APC_PLAY ) {
1818 sbus_writel(0, base->regs + base->dir + APCC);
1819 sbus_writel(0, base->regs + base->dir + APCVA);
1820 }
Georg Chinid35a1b92007-01-02 21:28:17 -08001821
Georg Chini3daadf32007-08-01 21:55:58 -07001822 udelay(1200);
Georg Chinib1282542005-11-07 14:09:19 -08001823 }
Georg Chinib1282542005-11-07 14:09:19 -08001824 csr = sbus_readl(base->regs + APCCSR);
1825 shift = 0;
1826 if ( base->dir == APC_PLAY )
1827 shift = 1;
1828 if (on)
1829 csr &= ~(APC_CPAUSE << shift);
1830 else
1831 csr |= (APC_CPAUSE << shift);
1832 sbus_writel(csr, base->regs + APCCSR);
1833 if (on)
1834 csr |= (APC_CDMA_READY << shift);
1835 else
1836 csr &= ~(APC_CDMA_READY << shift);
1837 sbus_writel(csr, base->regs + APCCSR);
1838
1839 spin_unlock_irqrestore(&base->lock, flags);
1840}
1841
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001842static unsigned int sbus_dma_addr(struct cs4231_dma_control *dma_cont)
Georg Chinib1282542005-11-07 14:09:19 -08001843{
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001844 struct sbus_dma_info *base = &dma_cont->sbus_info;
Georg Chinib1282542005-11-07 14:09:19 -08001845
1846 return sbus_readl(base->regs + base->dir + APCVA);
1847}
1848
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001849static void sbus_dma_preallocate(struct snd_cs4231 *chip, struct snd_pcm *pcm)
Georg Chinib1282542005-11-07 14:09:19 -08001850{
1851 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_SBUS,
1852 snd_dma_sbus_data(chip->dev_u.sdev),
1853 64*1024, 128*1024);
1854}
1855
1856/*
1857 * Init and exit routines
1858 */
1859
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001860static int snd_cs4231_sbus_free(struct snd_cs4231 *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861{
1862 if (chip->irq[0])
1863 free_irq(chip->irq[0], chip);
1864
1865 if (chip->port)
1866 sbus_iounmap(chip->port, chip->regs_size);
1867
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 kfree(chip);
1869
1870 return 0;
1871}
1872
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001873static int snd_cs4231_sbus_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874{
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001875 struct snd_cs4231 *cp = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876
1877 return snd_cs4231_sbus_free(cp);
1878}
1879
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001880static struct snd_device_ops snd_cs4231_sbus_dev_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 .dev_free = snd_cs4231_sbus_dev_free,
1882};
1883
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001884static int __init snd_cs4231_sbus_create(struct snd_card *card,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 struct sbus_dev *sdev,
Krzysztof Heltc6c2d572007-09-04 13:08:24 +02001886 int dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887{
Krzysztof Heltc6c2d572007-09-04 13:08:24 +02001888 struct snd_cs4231 *chip = card->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 int err;
1890
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 spin_lock_init(&chip->lock);
Georg Chinib1282542005-11-07 14:09:19 -08001892 spin_lock_init(&chip->c_dma.sbus_info.lock);
1893 spin_lock_init(&chip->p_dma.sbus_info.lock);
Ingo Molnar12aa7572006-01-16 16:36:05 +01001894 mutex_init(&chip->mce_mutex);
1895 mutex_init(&chip->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 chip->dev_u.sdev = sdev;
1897 chip->regs_size = sdev->reg_addrs[0].reg_size;
1898 memcpy(&chip->image, &snd_cs4231_original_image,
1899 sizeof(snd_cs4231_original_image));
1900
1901 chip->port = sbus_ioremap(&sdev->resource[0], 0,
1902 chip->regs_size, "cs4231");
1903 if (!chip->port) {
Christopher Zimmermanna1314302005-09-21 00:41:22 -07001904 snd_printdd("cs4231-%d: Unable to map chip registers.\n", dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 return -EIO;
1906 }
1907
Georg Chinib1282542005-11-07 14:09:19 -08001908 chip->c_dma.sbus_info.regs = chip->port;
1909 chip->p_dma.sbus_info.regs = chip->port;
1910 chip->c_dma.sbus_info.dir = APC_RECORD;
1911 chip->p_dma.sbus_info.dir = APC_PLAY;
1912
1913 chip->p_dma.prepare = sbus_dma_prepare;
1914 chip->p_dma.enable = sbus_dma_enable;
1915 chip->p_dma.request = sbus_dma_request;
1916 chip->p_dma.address = sbus_dma_addr;
Georg Chinib1282542005-11-07 14:09:19 -08001917 chip->p_dma.preallocate = sbus_dma_preallocate;
1918
1919 chip->c_dma.prepare = sbus_dma_prepare;
1920 chip->c_dma.enable = sbus_dma_enable;
1921 chip->c_dma.request = sbus_dma_request;
1922 chip->c_dma.address = sbus_dma_addr;
Georg Chinib1282542005-11-07 14:09:19 -08001923 chip->c_dma.preallocate = sbus_dma_preallocate;
Georg Chini5a820fa2005-11-07 14:08:25 -08001924
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 if (request_irq(sdev->irqs[0], snd_cs4231_sbus_interrupt,
Thomas Gleixner65ca68b2006-07-01 19:29:46 -07001926 IRQF_SHARED, "cs4231", chip)) {
David S. Millerc6387a42006-06-20 01:21:29 -07001927 snd_printdd("cs4231-%d: Unable to grab SBUS IRQ %d\n",
1928 dev, sdev->irqs[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 snd_cs4231_sbus_free(chip);
1930 return -EBUSY;
1931 }
1932 chip->irq[0] = sdev->irqs[0];
1933
1934 if (snd_cs4231_probe(chip) < 0) {
1935 snd_cs4231_sbus_free(chip);
1936 return -ENODEV;
1937 }
1938 snd_cs4231_init(chip);
1939
1940 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL,
1941 chip, &snd_cs4231_sbus_dev_ops)) < 0) {
1942 snd_cs4231_sbus_free(chip);
1943 return err;
1944 }
1945
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 return 0;
1947}
1948
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001949static int __init cs4231_sbus_attach(struct sbus_dev *sdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950{
1951 struct resource *rp = &sdev->resource[0];
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001952 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 int err;
1954
1955 err = cs4231_attach_begin(&card);
1956 if (err)
1957 return err;
1958
Andrew Morton5863aa62006-07-03 00:24:22 -07001959 sprintf(card->longname, "%s at 0x%02lx:0x%016Lx, irq %d",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 card->shortname,
1961 rp->flags & 0xffL,
Greg Kroah-Hartmanaa0a2dd2006-06-12 14:50:27 -07001962 (unsigned long long)rp->start,
David S. Millerc6387a42006-06-20 01:21:29 -07001963 sdev->irqs[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964
Krzysztof Heltc6c2d572007-09-04 13:08:24 +02001965 err = snd_cs4231_sbus_create(card, sdev, dev);
1966 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 snd_card_free(card);
1968 return err;
1969 }
1970
Krzysztof Heltc6c2d572007-09-04 13:08:24 +02001971 return cs4231_attach_finish(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972}
1973#endif
1974
1975#ifdef EBUS_SUPPORT
Georg Chinib1282542005-11-07 14:09:19 -08001976
1977static void snd_cs4231_ebus_play_callback(struct ebus_dma_info *p, int event, void *cookie)
1978{
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001979 struct snd_cs4231 *chip = cookie;
Georg Chinib1282542005-11-07 14:09:19 -08001980
1981 snd_cs4231_play_callback(chip);
1982}
1983
1984static void snd_cs4231_ebus_capture_callback(struct ebus_dma_info *p, int event, void *cookie)
1985{
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001986 struct snd_cs4231 *chip = cookie;
Georg Chinib1282542005-11-07 14:09:19 -08001987
1988 snd_cs4231_capture_callback(chip);
1989}
1990
1991/*
1992 * EBUS DMA wrappers
1993 */
1994
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001995static int _ebus_dma_request(struct cs4231_dma_control *dma_cont, dma_addr_t bus_addr, size_t len)
Georg Chinib1282542005-11-07 14:09:19 -08001996{
1997 return ebus_dma_request(&dma_cont->ebus_info, bus_addr, len);
1998}
1999
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01002000static void _ebus_dma_enable(struct cs4231_dma_control *dma_cont, int on)
Georg Chinib1282542005-11-07 14:09:19 -08002001{
2002 ebus_dma_enable(&dma_cont->ebus_info, on);
2003}
2004
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01002005static void _ebus_dma_prepare(struct cs4231_dma_control *dma_cont, int dir)
Georg Chinib1282542005-11-07 14:09:19 -08002006{
2007 ebus_dma_prepare(&dma_cont->ebus_info, dir);
2008}
2009
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01002010static unsigned int _ebus_dma_addr(struct cs4231_dma_control *dma_cont)
Georg Chinib1282542005-11-07 14:09:19 -08002011{
2012 return ebus_dma_addr(&dma_cont->ebus_info);
2013}
2014
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01002015static void _ebus_dma_preallocate(struct snd_cs4231 *chip, struct snd_pcm *pcm)
Georg Chinib1282542005-11-07 14:09:19 -08002016{
2017 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
2018 snd_dma_pci_data(chip->dev_u.pdev),
2019 64*1024, 128*1024);
2020}
2021
2022/*
2023 * Init and exit routines
2024 */
2025
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01002026static int snd_cs4231_ebus_free(struct snd_cs4231 *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027{
Georg Chinib1282542005-11-07 14:09:19 -08002028 if (chip->c_dma.ebus_info.regs) {
2029 ebus_dma_unregister(&chip->c_dma.ebus_info);
2030 iounmap(chip->c_dma.ebus_info.regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 }
Georg Chinib1282542005-11-07 14:09:19 -08002032 if (chip->p_dma.ebus_info.regs) {
2033 ebus_dma_unregister(&chip->p_dma.ebus_info);
2034 iounmap(chip->p_dma.ebus_info.regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 }
2036
2037 if (chip->port)
2038 iounmap(chip->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039
2040 kfree(chip);
2041
2042 return 0;
2043}
2044
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01002045static int snd_cs4231_ebus_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046{
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01002047 struct snd_cs4231 *cp = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048
2049 return snd_cs4231_ebus_free(cp);
2050}
2051
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01002052static struct snd_device_ops snd_cs4231_ebus_dev_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 .dev_free = snd_cs4231_ebus_dev_free,
2054};
2055
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01002056static int __init snd_cs4231_ebus_create(struct snd_card *card,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 struct linux_ebus_device *edev,
Krzysztof Heltc6c2d572007-09-04 13:08:24 +02002058 int dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059{
Krzysztof Heltc6c2d572007-09-04 13:08:24 +02002060 struct snd_cs4231 *chip = card->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 int err;
2062
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 spin_lock_init(&chip->lock);
Georg Chinib1282542005-11-07 14:09:19 -08002064 spin_lock_init(&chip->c_dma.ebus_info.lock);
2065 spin_lock_init(&chip->p_dma.ebus_info.lock);
Ingo Molnar12aa7572006-01-16 16:36:05 +01002066 mutex_init(&chip->mce_mutex);
2067 mutex_init(&chip->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 chip->flags |= CS4231_FLAG_EBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 chip->dev_u.pdev = edev->bus->self;
2070 memcpy(&chip->image, &snd_cs4231_original_image,
2071 sizeof(snd_cs4231_original_image));
Georg Chinib1282542005-11-07 14:09:19 -08002072 strcpy(chip->c_dma.ebus_info.name, "cs4231(capture)");
2073 chip->c_dma.ebus_info.flags = EBUS_DMA_FLAG_USE_EBDMA_HANDLER;
2074 chip->c_dma.ebus_info.callback = snd_cs4231_ebus_capture_callback;
2075 chip->c_dma.ebus_info.client_cookie = chip;
2076 chip->c_dma.ebus_info.irq = edev->irqs[0];
2077 strcpy(chip->p_dma.ebus_info.name, "cs4231(play)");
2078 chip->p_dma.ebus_info.flags = EBUS_DMA_FLAG_USE_EBDMA_HANDLER;
2079 chip->p_dma.ebus_info.callback = snd_cs4231_ebus_play_callback;
2080 chip->p_dma.ebus_info.client_cookie = chip;
2081 chip->p_dma.ebus_info.irq = edev->irqs[1];
2082
2083 chip->p_dma.prepare = _ebus_dma_prepare;
2084 chip->p_dma.enable = _ebus_dma_enable;
2085 chip->p_dma.request = _ebus_dma_request;
2086 chip->p_dma.address = _ebus_dma_addr;
Georg Chinib1282542005-11-07 14:09:19 -08002087 chip->p_dma.preallocate = _ebus_dma_preallocate;
2088
2089 chip->c_dma.prepare = _ebus_dma_prepare;
2090 chip->c_dma.enable = _ebus_dma_enable;
2091 chip->c_dma.request = _ebus_dma_request;
2092 chip->c_dma.address = _ebus_dma_addr;
Georg Chinib1282542005-11-07 14:09:19 -08002093 chip->c_dma.preallocate = _ebus_dma_preallocate;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094
2095 chip->port = ioremap(edev->resource[0].start, 0x10);
Georg Chinib1282542005-11-07 14:09:19 -08002096 chip->p_dma.ebus_info.regs = ioremap(edev->resource[1].start, 0x10);
2097 chip->c_dma.ebus_info.regs = ioremap(edev->resource[2].start, 0x10);
2098 if (!chip->port || !chip->p_dma.ebus_info.regs || !chip->c_dma.ebus_info.regs) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 snd_cs4231_ebus_free(chip);
Christopher Zimmermanna1314302005-09-21 00:41:22 -07002100 snd_printdd("cs4231-%d: Unable to map chip registers.\n", dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 return -EIO;
2102 }
2103
Georg Chinib1282542005-11-07 14:09:19 -08002104 if (ebus_dma_register(&chip->c_dma.ebus_info)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 snd_cs4231_ebus_free(chip);
Christopher Zimmermanna1314302005-09-21 00:41:22 -07002106 snd_printdd("cs4231-%d: Unable to register EBUS capture DMA\n", dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 return -EBUSY;
2108 }
Georg Chinib1282542005-11-07 14:09:19 -08002109 if (ebus_dma_irq_enable(&chip->c_dma.ebus_info, 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 snd_cs4231_ebus_free(chip);
Christopher Zimmermanna1314302005-09-21 00:41:22 -07002111 snd_printdd("cs4231-%d: Unable to enable EBUS capture IRQ\n", dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 return -EBUSY;
2113 }
2114
Georg Chinib1282542005-11-07 14:09:19 -08002115 if (ebus_dma_register(&chip->p_dma.ebus_info)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 snd_cs4231_ebus_free(chip);
Christopher Zimmermanna1314302005-09-21 00:41:22 -07002117 snd_printdd("cs4231-%d: Unable to register EBUS play DMA\n", dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 return -EBUSY;
2119 }
Georg Chinib1282542005-11-07 14:09:19 -08002120 if (ebus_dma_irq_enable(&chip->p_dma.ebus_info, 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 snd_cs4231_ebus_free(chip);
Christopher Zimmermanna1314302005-09-21 00:41:22 -07002122 snd_printdd("cs4231-%d: Unable to enable EBUS play IRQ\n", dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 return -EBUSY;
2124 }
2125
2126 if (snd_cs4231_probe(chip) < 0) {
2127 snd_cs4231_ebus_free(chip);
2128 return -ENODEV;
2129 }
2130 snd_cs4231_init(chip);
2131
2132 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL,
2133 chip, &snd_cs4231_ebus_dev_ops)) < 0) {
2134 snd_cs4231_ebus_free(chip);
2135 return err;
2136 }
2137
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 return 0;
2139}
2140
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01002141static int __init cs4231_ebus_attach(struct linux_ebus_device *edev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142{
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01002143 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 int err;
2145
2146 err = cs4231_attach_begin(&card);
2147 if (err)
2148 return err;
2149
David S. Millerc6387a42006-06-20 01:21:29 -07002150 sprintf(card->longname, "%s at 0x%lx, irq %d",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 card->shortname,
2152 edev->resource[0].start,
David S. Millerc6387a42006-06-20 01:21:29 -07002153 edev->irqs[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154
Krzysztof Heltc6c2d572007-09-04 13:08:24 +02002155 err = snd_cs4231_ebus_create(card, edev, dev);
2156 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 snd_card_free(card);
2158 return err;
2159 }
2160
Krzysztof Heltc6c2d572007-09-04 13:08:24 +02002161 return cs4231_attach_finish(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162}
2163#endif
2164
2165static int __init cs4231_init(void)
2166{
2167#ifdef SBUS_SUPPORT
2168 struct sbus_bus *sbus;
2169 struct sbus_dev *sdev;
2170#endif
2171#ifdef EBUS_SUPPORT
2172 struct linux_ebus *ebus;
2173 struct linux_ebus_device *edev;
2174#endif
2175 int found;
2176
2177 found = 0;
2178
2179#ifdef SBUS_SUPPORT
2180 for_all_sbusdev(sdev, sbus) {
2181 if (!strcmp(sdev->prom_name, "SUNW,CS4231")) {
2182 if (cs4231_sbus_attach(sdev) == 0)
2183 found++;
2184 }
2185 }
2186#endif
2187#ifdef EBUS_SUPPORT
2188 for_each_ebus(ebus) {
2189 for_each_ebusdev(edev, ebus) {
2190 int match = 0;
2191
David S. Miller690c8fd2006-06-22 19:12:03 -07002192 if (!strcmp(edev->prom_node->name, "SUNW,CS4231")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 match = 1;
David S. Miller690c8fd2006-06-22 19:12:03 -07002194 } else if (!strcmp(edev->prom_node->name, "audio")) {
Stephen Rothwell31985142007-03-29 00:50:57 -07002195 const char *compat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196
David S. Miller690c8fd2006-06-22 19:12:03 -07002197 compat = of_get_property(edev->prom_node,
2198 "compatible", NULL);
2199 if (compat && !strcmp(compat, "SUNW,CS4231"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 match = 1;
2201 }
2202
2203 if (match &&
2204 cs4231_ebus_attach(edev) == 0)
2205 found++;
2206 }
2207 }
2208#endif
2209
2210
2211 return (found > 0) ? 0 : -EIO;
2212}
2213
2214static void __exit cs4231_exit(void)
2215{
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01002216 struct snd_cs4231 *p = cs4231_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217
2218 while (p != NULL) {
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01002219 struct snd_cs4231 *next = p->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220
2221 snd_card_free(p->card);
2222
2223 p = next;
2224 }
2225
2226 cs4231_list = NULL;
2227}
2228
2229module_init(cs4231_init);
2230module_exit(cs4231_exit);