blob: e6dba4f7169ca7d3fe1a331e162b75e249a55078 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Driver for CS4231 sound chips found on Sparcs.
David S. Millerae251032008-08-27 18:24:01 -07003 * Copyright (C) 2002, 2008 David S. Miller <davem@davemloft.net>
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
5 * Based entirely upon drivers/sbus/audio/cs4231.c which is:
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02006 * Copyright (C) 1996, 1997, 1998 Derrick J Brashear (shadow@andrew.cmu.edu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * and also sound/isa/cs423x/cs4231_lib.c which is:
Jaroslav Kyselac1017a42007-10-15 09:50:19 +02008 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 */
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>
Krzysztof Helt9e9abb42007-09-10 23:06:55 +020018#include <linux/irq.h>
19#include <linux/io.h>
David S. Millerae251032008-08-27 18:24:01 -070020#include <linux/of.h>
21#include <linux/of_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <sound/core.h>
24#include <sound/pcm.h>
25#include <sound/info.h>
26#include <sound/control.h>
27#include <sound/timer.h>
28#include <sound/initval.h>
29#include <sound/pcm_params.h>
30
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#ifdef CONFIG_SBUS
32#define SBUS_SUPPORT
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#endif
34
35#if defined(CONFIG_PCI) && defined(CONFIG_SPARC64)
36#define EBUS_SUPPORT
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/pci.h>
38#include <asm/ebus.h>
David S. Milleraae7fb82008-08-29 23:10:21 -070039#include <asm/ebus_dma.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#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 */
Krzysztof Helt9e9abb42007-09-10 23:06:55 +020044/* Enable this card */
45static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
47module_param_array(index, int, NULL, 0444);
48MODULE_PARM_DESC(index, "Index value for Sun CS4231 soundcard.");
49module_param_array(id, charp, NULL, 0444);
50MODULE_PARM_DESC(id, "ID string for Sun CS4231 soundcard.");
51module_param_array(enable, bool, NULL, 0444);
52MODULE_PARM_DESC(enable, "Enable Sun CS4231 soundcard.");
53MODULE_AUTHOR("Jaroslav Kysela, Derrick J. Brashear and David S. Miller");
54MODULE_DESCRIPTION("Sun CS4231");
55MODULE_LICENSE("GPL");
56MODULE_SUPPORTED_DEVICE("{{Sun,CS4231}}");
57
Georg Chini5a820fa2005-11-07 14:08:25 -080058#ifdef SBUS_SUPPORT
Takashi Iwaibe9b7e82006-01-03 13:42:38 +010059struct sbus_dma_info {
Krzysztof Helt9e9abb42007-09-10 23:06:55 +020060 spinlock_t lock; /* DMA access lock */
61 int dir;
62 void __iomem *regs;
Takashi Iwaibe9b7e82006-01-03 13:42:38 +010063};
Georg Chini5a820fa2005-11-07 14:08:25 -080064#endif
65
David S. Miller4f3f2f62006-01-17 15:55:58 -080066struct snd_cs4231;
Takashi Iwaibe9b7e82006-01-03 13:42:38 +010067struct cs4231_dma_control {
Krzysztof Helt9e9abb42007-09-10 23:06:55 +020068 void (*prepare)(struct cs4231_dma_control *dma_cont,
69 int dir);
70 void (*enable)(struct cs4231_dma_control *dma_cont, int on);
71 int (*request)(struct cs4231_dma_control *dma_cont,
72 dma_addr_t bus_addr, size_t len);
73 unsigned int (*address)(struct cs4231_dma_control *dma_cont);
74 void (*preallocate)(struct snd_cs4231 *chip,
75 struct snd_pcm *pcm);
Georg Chinib1282542005-11-07 14:09:19 -080076#ifdef EBUS_SUPPORT
77 struct ebus_dma_info ebus_info;
78#endif
79#ifdef SBUS_SUPPORT
80 struct sbus_dma_info sbus_info;
81#endif
Takashi Iwaibe9b7e82006-01-03 13:42:38 +010082};
Georg Chinib1282542005-11-07 14:09:19 -080083
84struct snd_cs4231 {
Krzysztof Helt9e9abb42007-09-10 23:06:55 +020085 spinlock_t lock; /* registers access lock */
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 void __iomem *port;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
Takashi Iwaibe9b7e82006-01-03 13:42:38 +010088 struct cs4231_dma_control p_dma;
89 struct cs4231_dma_control c_dma;
Georg Chini5a820fa2005-11-07 14:08:25 -080090
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 u32 flags;
92#define CS4231_FLAG_EBUS 0x00000001
93#define CS4231_FLAG_PLAYBACK 0x00000002
94#define CS4231_FLAG_CAPTURE 0x00000004
95
Takashi Iwaibe9b7e82006-01-03 13:42:38 +010096 struct snd_card *card;
97 struct snd_pcm *pcm;
98 struct snd_pcm_substream *playback_substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 unsigned int p_periods_sent;
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100100 struct snd_pcm_substream *capture_substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 unsigned int c_periods_sent;
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100102 struct snd_timer *timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
104 unsigned short mode;
105#define CS4231_MODE_NONE 0x0000
106#define CS4231_MODE_PLAY 0x0001
107#define CS4231_MODE_RECORD 0x0002
108#define CS4231_MODE_TIMER 0x0004
Krzysztof Helt9e9abb42007-09-10 23:06:55 +0200109#define CS4231_MODE_OPEN (CS4231_MODE_PLAY | CS4231_MODE_RECORD | \
110 CS4231_MODE_TIMER)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
112 unsigned char image[32]; /* registers image */
113 int mce_bit;
114 int calibrate_mute;
Krzysztof Helt9e9abb42007-09-10 23:06:55 +0200115 struct mutex mce_mutex; /* mutex for mce register */
116 struct mutex open_mutex; /* mutex for ALSA open/close */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
118 union {
119#ifdef SBUS_SUPPORT
David S. Millerae251032008-08-27 18:24:01 -0700120 struct of_device *op;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121#endif
122#ifdef EBUS_SUPPORT
123 struct pci_dev *pdev;
124#endif
125 } dev_u;
126 unsigned int irq[2];
127 unsigned int regs_size;
128 struct snd_cs4231 *next;
Georg Chinib1282542005-11-07 14:09:19 -0800129};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100131static struct snd_cs4231 *cs4231_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
133/* Eventually we can use sound/isa/cs423x/cs4231_lib.c directly, but for
134 * now.... -DaveM
135 */
136
137/* IO ports */
Krzysztof Helt7e52f3d2007-09-05 15:08:23 +0200138#include <sound/cs4231-regs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
140/* XXX offsets are different than PC ISA chips... */
Krzysztof Helt7e52f3d2007-09-05 15:08:23 +0200141#define CS4231U(chip, x) ((chip)->port + ((c_d_c_CS4231##x) << 2))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
143/* SBUS DMA register defines. */
144
145#define APCCSR 0x10UL /* APC DMA CSR */
146#define APCCVA 0x20UL /* APC Capture DMA Address */
147#define APCCC 0x24UL /* APC Capture Count */
148#define APCCNVA 0x28UL /* APC Capture DMA Next Address */
149#define APCCNC 0x2cUL /* APC Capture Next Count */
150#define APCPVA 0x30UL /* APC Play DMA Address */
151#define APCPC 0x34UL /* APC Play Count */
152#define APCPNVA 0x38UL /* APC Play DMA Next Address */
153#define APCPNC 0x3cUL /* APC Play Next Count */
154
Georg Chini5a820fa2005-11-07 14:08:25 -0800155/* Defines for SBUS DMA-routines */
156
157#define APCVA 0x0UL /* APC DMA Address */
158#define APCC 0x4UL /* APC Count */
159#define APCNVA 0x8UL /* APC DMA Next Address */
160#define APCNC 0xcUL /* APC Next Count */
161#define APC_PLAY 0x30UL /* Play registers start at 0x30 */
162#define APC_RECORD 0x20UL /* Record registers start at 0x20 */
163
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164/* APCCSR bits */
165
166#define APC_INT_PENDING 0x800000 /* Interrupt Pending */
167#define APC_PLAY_INT 0x400000 /* Playback interrupt */
168#define APC_CAPT_INT 0x200000 /* Capture interrupt */
169#define APC_GENL_INT 0x100000 /* General interrupt */
170#define APC_XINT_ENA 0x80000 /* General ext int. enable */
171#define APC_XINT_PLAY 0x40000 /* Playback ext intr */
172#define APC_XINT_CAPT 0x20000 /* Capture ext intr */
173#define APC_XINT_GENL 0x10000 /* Error ext intr */
174#define APC_XINT_EMPT 0x8000 /* Pipe empty interrupt (0 write to pva) */
175#define APC_XINT_PEMP 0x4000 /* Play pipe empty (pva and pnva not set) */
176#define APC_XINT_PNVA 0x2000 /* Playback NVA dirty */
177#define APC_XINT_PENA 0x1000 /* play pipe empty Int enable */
178#define APC_XINT_COVF 0x800 /* Cap data dropped on floor */
179#define APC_XINT_CNVA 0x400 /* Capture NVA dirty */
180#define APC_XINT_CEMP 0x200 /* Capture pipe empty (cva and cnva not set) */
181#define APC_XINT_CENA 0x100 /* Cap. pipe empty int enable */
182#define APC_PPAUSE 0x80 /* Pause the play DMA */
183#define APC_CPAUSE 0x40 /* Pause the capture DMA */
184#define APC_CDC_RESET 0x20 /* CODEC RESET */
185#define APC_PDMA_READY 0x08 /* Play DMA Go */
186#define APC_CDMA_READY 0x04 /* Capture DMA Go */
187#define APC_CHIP_RESET 0x01 /* Reset the chip */
188
189/* EBUS DMA register offsets */
190
191#define EBDMA_CSR 0x00UL /* Control/Status */
192#define EBDMA_ADDR 0x04UL /* DMA Address */
193#define EBDMA_COUNT 0x08UL /* DMA Count */
194
195/*
196 * Some variables
197 */
198
199static unsigned char freq_bits[14] = {
200 /* 5510 */ 0x00 | CS4231_XTAL2,
201 /* 6620 */ 0x0E | CS4231_XTAL2,
202 /* 8000 */ 0x00 | CS4231_XTAL1,
203 /* 9600 */ 0x0E | CS4231_XTAL1,
204 /* 11025 */ 0x02 | CS4231_XTAL2,
205 /* 16000 */ 0x02 | CS4231_XTAL1,
206 /* 18900 */ 0x04 | CS4231_XTAL2,
207 /* 22050 */ 0x06 | CS4231_XTAL2,
208 /* 27042 */ 0x04 | CS4231_XTAL1,
209 /* 32000 */ 0x06 | CS4231_XTAL1,
210 /* 33075 */ 0x0C | CS4231_XTAL2,
211 /* 37800 */ 0x08 | CS4231_XTAL2,
212 /* 44100 */ 0x0A | CS4231_XTAL2,
213 /* 48000 */ 0x0C | CS4231_XTAL1
214};
215
216static unsigned int rates[14] = {
217 5510, 6620, 8000, 9600, 11025, 16000, 18900, 22050,
218 27042, 32000, 33075, 37800, 44100, 48000
219};
220
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100221static struct snd_pcm_hw_constraint_list hw_constraints_rates = {
Krzysztof Heltc6c2d572007-09-04 13:08:24 +0200222 .count = ARRAY_SIZE(rates),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 .list = rates,
224};
225
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100226static int snd_cs4231_xrate(struct snd_pcm_runtime *runtime)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227{
228 return snd_pcm_hw_constraint_list(runtime, 0,
229 SNDRV_PCM_HW_PARAM_RATE,
230 &hw_constraints_rates);
231}
232
233static unsigned char snd_cs4231_original_image[32] =
234{
235 0x00, /* 00/00 - lic */
236 0x00, /* 01/01 - ric */
237 0x9f, /* 02/02 - la1ic */
238 0x9f, /* 03/03 - ra1ic */
239 0x9f, /* 04/04 - la2ic */
240 0x9f, /* 05/05 - ra2ic */
241 0xbf, /* 06/06 - loc */
242 0xbf, /* 07/07 - roc */
243 0x20, /* 08/08 - pdfr */
244 CS4231_AUTOCALIB, /* 09/09 - ic */
245 0x00, /* 0a/10 - pc */
246 0x00, /* 0b/11 - ti */
247 CS4231_MODE2, /* 0c/12 - mi */
248 0x00, /* 0d/13 - lbc */
249 0x00, /* 0e/14 - pbru */
250 0x00, /* 0f/15 - pbrl */
251 0x80, /* 10/16 - afei */
252 0x01, /* 11/17 - afeii */
253 0x9f, /* 12/18 - llic */
254 0x9f, /* 13/19 - rlic */
255 0x00, /* 14/20 - tlb */
256 0x00, /* 15/21 - thb */
257 0x00, /* 16/22 - la3mic/reserved */
258 0x00, /* 17/23 - ra3mic/reserved */
259 0x00, /* 18/24 - afs */
260 0x00, /* 19/25 - lamoc/version */
261 0x00, /* 1a/26 - mioc */
262 0x00, /* 1b/27 - ramoc/reserved */
263 0x20, /* 1c/28 - cdfr */
264 0x00, /* 1d/29 - res4 */
265 0x00, /* 1e/30 - cbru */
266 0x00, /* 1f/31 - cbrl */
267};
268
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100269static u8 __cs4231_readb(struct snd_cs4231 *cp, void __iomem *reg_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270{
271#ifdef EBUS_SUPPORT
Krzysztof Heltc6c2d572007-09-04 13:08:24 +0200272 if (cp->flags & CS4231_FLAG_EBUS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 return readb(reg_addr);
Krzysztof Heltc6c2d572007-09-04 13:08:24 +0200274 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275#endif
276#ifdef SBUS_SUPPORT
277 return sbus_readb(reg_addr);
278#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279}
280
Krzysztof Helt9e9abb42007-09-10 23:06:55 +0200281static void __cs4231_writeb(struct snd_cs4231 *cp, u8 val,
282 void __iomem *reg_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283{
284#ifdef EBUS_SUPPORT
Krzysztof Heltc6c2d572007-09-04 13:08:24 +0200285 if (cp->flags & CS4231_FLAG_EBUS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 return writeb(val, reg_addr);
Krzysztof Heltc6c2d572007-09-04 13:08:24 +0200287 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288#endif
289#ifdef SBUS_SUPPORT
290 return sbus_writeb(val, reg_addr);
291#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292}
293
294/*
295 * Basic I/O functions
296 */
297
Krzysztof Heltc6c2d572007-09-04 13:08:24 +0200298static void snd_cs4231_ready(struct snd_cs4231 *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299{
300 int timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
Krzysztof Helt7e52f3d2007-09-05 15:08:23 +0200302 for (timeout = 250; timeout > 0; timeout--) {
303 int val = __cs4231_readb(chip, CS4231U(chip, REGSEL));
304 if ((val & CS4231_INIT) == 0)
305 break;
306 udelay(100);
307 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308}
309
Krzysztof Helt9e9abb42007-09-10 23:06:55 +0200310static void snd_cs4231_dout(struct snd_cs4231 *chip, unsigned char reg,
311 unsigned char value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312{
Krzysztof Heltc6c2d572007-09-04 13:08:24 +0200313 snd_cs4231_ready(chip);
Christopher Zimmermanna1314302005-09-21 00:41:22 -0700314#ifdef CONFIG_SND_DEBUG
Krzysztof Helt7e52f3d2007-09-05 15:08:23 +0200315 if (__cs4231_readb(chip, CS4231U(chip, REGSEL)) & CS4231_INIT)
Krzysztof Heltc6c2d572007-09-04 13:08:24 +0200316 snd_printdd("out: auto calibration time out - reg = 0x%x, "
317 "value = 0x%x\n",
318 reg, value);
Christopher Zimmermanna1314302005-09-21 00:41:22 -0700319#endif
Krzysztof Helt7e52f3d2007-09-05 15:08:23 +0200320 __cs4231_writeb(chip, chip->mce_bit | reg, CS4231U(chip, REGSEL));
Krzysztof Heltc6c2d572007-09-04 13:08:24 +0200321 wmb();
Krzysztof Helt7e52f3d2007-09-05 15:08:23 +0200322 __cs4231_writeb(chip, value, CS4231U(chip, REG));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 mb();
324}
325
Krzysztof Heltc6c2d572007-09-04 13:08:24 +0200326static inline void snd_cs4231_outm(struct snd_cs4231 *chip, unsigned char reg,
327 unsigned char mask, unsigned char value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328{
Krzysztof Heltc6c2d572007-09-04 13:08:24 +0200329 unsigned char tmp = (chip->image[reg] & mask) | value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
Krzysztof Heltc6c2d572007-09-04 13:08:24 +0200331 chip->image[reg] = tmp;
332 if (!chip->calibrate_mute)
333 snd_cs4231_dout(chip, reg, tmp);
334}
335
336static void snd_cs4231_out(struct snd_cs4231 *chip, unsigned char reg,
337 unsigned char value)
338{
339 snd_cs4231_dout(chip, reg, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 chip->image[reg] = value;
341 mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342}
343
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100344static unsigned char snd_cs4231_in(struct snd_cs4231 *chip, unsigned char reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345{
Krzysztof Heltc6c2d572007-09-04 13:08:24 +0200346 snd_cs4231_ready(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347#ifdef CONFIG_SND_DEBUG
Krzysztof Helt7e52f3d2007-09-05 15:08:23 +0200348 if (__cs4231_readb(chip, CS4231U(chip, REGSEL)) & CS4231_INIT)
Krzysztof Heltc6c2d572007-09-04 13:08:24 +0200349 snd_printdd("in: auto calibration time out - reg = 0x%x\n",
350 reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351#endif
Krzysztof Helt7e52f3d2007-09-05 15:08:23 +0200352 __cs4231_writeb(chip, chip->mce_bit | reg, CS4231U(chip, REGSEL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 mb();
Krzysztof Helt7e52f3d2007-09-05 15:08:23 +0200354 return __cs4231_readb(chip, CS4231U(chip, REG));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355}
356
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357/*
358 * CS4231 detection / MCE routines
359 */
360
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100361static void snd_cs4231_busy_wait(struct snd_cs4231 *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362{
363 int timeout;
364
Krzysztof Helt9e9abb42007-09-10 23:06:55 +0200365 /* looks like this sequence is proper for CS4231A chip (GUS MAX) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 for (timeout = 5; timeout > 0; timeout--)
Krzysztof Helt7e52f3d2007-09-05 15:08:23 +0200367 __cs4231_readb(chip, CS4231U(chip, REGSEL));
Christopher Zimmermanna1314302005-09-21 00:41:22 -0700368
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 /* end of cleanup sequence */
Krzysztof Helt7e52f3d2007-09-05 15:08:23 +0200370 for (timeout = 500; timeout > 0; timeout--) {
371 int val = __cs4231_readb(chip, CS4231U(chip, REGSEL));
372 if ((val & CS4231_INIT) == 0)
373 break;
Krzysztof Heltc6c2d572007-09-04 13:08:24 +0200374 msleep(1);
Krzysztof Helt7e52f3d2007-09-05 15:08:23 +0200375 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376}
377
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100378static void snd_cs4231_mce_up(struct snd_cs4231 *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379{
380 unsigned long flags;
381 int timeout;
382
383 spin_lock_irqsave(&chip->lock, flags);
Krzysztof Heltc6c2d572007-09-04 13:08:24 +0200384 snd_cs4231_ready(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385#ifdef CONFIG_SND_DEBUG
Krzysztof Helt7e52f3d2007-09-05 15:08:23 +0200386 if (__cs4231_readb(chip, CS4231U(chip, REGSEL)) & CS4231_INIT)
Christopher Zimmermanna1314302005-09-21 00:41:22 -0700387 snd_printdd("mce_up - auto calibration time out (0)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388#endif
389 chip->mce_bit |= CS4231_MCE;
Krzysztof Helt7e52f3d2007-09-05 15:08:23 +0200390 timeout = __cs4231_readb(chip, CS4231U(chip, REGSEL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 if (timeout == 0x80)
Krzysztof Helt9e9abb42007-09-10 23:06:55 +0200392 snd_printdd("mce_up [%p]: serious init problem - "
393 "codec still busy\n",
394 chip->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 if (!(timeout & CS4231_MCE))
Krzysztof Helt7e52f3d2007-09-05 15:08:23 +0200396 __cs4231_writeb(chip, chip->mce_bit | (timeout & 0x1f),
397 CS4231U(chip, REGSEL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 spin_unlock_irqrestore(&chip->lock, flags);
399}
400
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100401static void snd_cs4231_mce_down(struct snd_cs4231 *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402{
Krzysztof Helt9823adf2007-10-16 14:54:58 +0200403 unsigned long flags, timeout;
404 int reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 snd_cs4231_busy_wait(chip);
Krzysztof Helt9823adf2007-10-16 14:54:58 +0200407 spin_lock_irqsave(&chip->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408#ifdef CONFIG_SND_DEBUG
Krzysztof Helt7e52f3d2007-09-05 15:08:23 +0200409 if (__cs4231_readb(chip, CS4231U(chip, REGSEL)) & CS4231_INIT)
410 snd_printdd("mce_down [%p] - auto calibration time out (0)\n",
411 CS4231U(chip, REGSEL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412#endif
413 chip->mce_bit &= ~CS4231_MCE;
Krzysztof Helt9823adf2007-10-16 14:54:58 +0200414 reg = __cs4231_readb(chip, CS4231U(chip, REGSEL));
415 __cs4231_writeb(chip, chip->mce_bit | (reg & 0x1f),
Krzysztof Helt7e52f3d2007-09-05 15:08:23 +0200416 CS4231U(chip, REGSEL));
Krzysztof Helt9823adf2007-10-16 14:54:58 +0200417 if (reg == 0x80)
418 snd_printdd("mce_down [%p]: serious init problem "
419 "- codec still busy\n", chip->port);
420 if ((reg & CS4231_MCE) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 spin_unlock_irqrestore(&chip->lock, flags);
422 return;
423 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
Krzysztof Helt56f91582007-09-11 00:55:46 +0200425 /*
Krzysztof Helt9823adf2007-10-16 14:54:58 +0200426 * Wait for auto-calibration (AC) process to finish, i.e. ACI to go low.
Krzysztof Helt56f91582007-09-11 00:55:46 +0200427 */
Krzysztof Helt9823adf2007-10-16 14:54:58 +0200428 timeout = jiffies + msecs_to_jiffies(250);
429 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 spin_unlock_irqrestore(&chip->lock, flags);
Takashi Iwaib875d652007-09-11 10:12:14 +0200431 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 spin_lock_irqsave(&chip->lock, flags);
Krzysztof Helt9823adf2007-10-16 14:54:58 +0200433 reg = snd_cs4231_in(chip, CS4231_TEST_INIT);
434 reg &= CS4231_CALIB_IN_PROGRESS;
435 } while (reg && time_before(jiffies, timeout));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 spin_unlock_irqrestore(&chip->lock, flags);
Krzysztof Helt9823adf2007-10-16 14:54:58 +0200437
438 if (reg)
439 snd_printk(KERN_ERR
440 "mce_down - auto calibration time out (2)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441}
442
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100443static void snd_cs4231_advance_dma(struct cs4231_dma_control *dma_cont,
444 struct snd_pcm_substream *substream,
445 unsigned int *periods_sent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446{
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100447 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
449 while (1) {
Christopher Zimmermanna1314302005-09-21 00:41:22 -0700450 unsigned int period_size = snd_pcm_lib_period_bytes(substream);
451 unsigned int offset = period_size * (*periods_sent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
Eric Sesterhenn817dd6e2006-03-24 18:49:12 +0100453 BUG_ON(period_size >= (1 << 24));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
Krzysztof Helt9e9abb42007-09-10 23:06:55 +0200455 if (dma_cont->request(dma_cont,
456 runtime->dma_addr + offset, period_size))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 (*periods_sent) = ((*periods_sent) + 1) % runtime->periods;
459 }
460}
Christopher Zimmermanna1314302005-09-21 00:41:22 -0700461
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100462static void cs4231_dma_trigger(struct snd_pcm_substream *substream,
463 unsigned int what, int on)
Christopher Zimmermanna1314302005-09-21 00:41:22 -0700464{
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100465 struct snd_cs4231 *chip = snd_pcm_substream_chip(substream);
466 struct cs4231_dma_control *dma_cont;
Christopher Zimmermanna1314302005-09-21 00:41:22 -0700467
Georg Chini5a820fa2005-11-07 14:08:25 -0800468 if (what & CS4231_PLAYBACK_ENABLE) {
Georg Chinib1282542005-11-07 14:09:19 -0800469 dma_cont = &chip->p_dma;
Christopher Zimmermanna1314302005-09-21 00:41:22 -0700470 if (on) {
Georg Chinib1282542005-11-07 14:09:19 -0800471 dma_cont->prepare(dma_cont, 0);
472 dma_cont->enable(dma_cont, 1);
473 snd_cs4231_advance_dma(dma_cont,
Georg Chini5a820fa2005-11-07 14:08:25 -0800474 chip->playback_substream,
475 &chip->p_periods_sent);
Christopher Zimmermanna1314302005-09-21 00:41:22 -0700476 } else {
Georg Chinib1282542005-11-07 14:09:19 -0800477 dma_cont->enable(dma_cont, 0);
Christopher Zimmermanna1314302005-09-21 00:41:22 -0700478 }
Georg Chini5a820fa2005-11-07 14:08:25 -0800479 }
480 if (what & CS4231_RECORD_ENABLE) {
Georg Chinib1282542005-11-07 14:09:19 -0800481 dma_cont = &chip->c_dma;
Christopher Zimmermanna1314302005-09-21 00:41:22 -0700482 if (on) {
Georg Chinib1282542005-11-07 14:09:19 -0800483 dma_cont->prepare(dma_cont, 1);
484 dma_cont->enable(dma_cont, 1);
485 snd_cs4231_advance_dma(dma_cont,
Georg Chini5a820fa2005-11-07 14:08:25 -0800486 chip->capture_substream,
487 &chip->c_periods_sent);
Christopher Zimmermanna1314302005-09-21 00:41:22 -0700488 } else {
Georg Chinib1282542005-11-07 14:09:19 -0800489 dma_cont->enable(dma_cont, 0);
Christopher Zimmermanna1314302005-09-21 00:41:22 -0700490 }
Christopher Zimmermanna1314302005-09-21 00:41:22 -0700491 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492}
493
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100494static int snd_cs4231_trigger(struct snd_pcm_substream *substream, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495{
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100496 struct snd_cs4231 *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 int result = 0;
498
499 switch (cmd) {
500 case SNDRV_PCM_TRIGGER_START:
501 case SNDRV_PCM_TRIGGER_STOP:
502 {
503 unsigned int what = 0;
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100504 struct snd_pcm_substream *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 unsigned long flags;
506
Takashi Iwaief991b92007-02-22 12:52:53 +0100507 snd_pcm_group_for_each_entry(s, substream) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 if (s == chip->playback_substream) {
509 what |= CS4231_PLAYBACK_ENABLE;
510 snd_pcm_trigger_done(s, substream);
511 } else if (s == chip->capture_substream) {
512 what |= CS4231_RECORD_ENABLE;
513 snd_pcm_trigger_done(s, substream);
514 }
515 }
516
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 spin_lock_irqsave(&chip->lock, flags);
518 if (cmd == SNDRV_PCM_TRIGGER_START) {
Christopher Zimmermanna1314302005-09-21 00:41:22 -0700519 cs4231_dma_trigger(substream, what, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 chip->image[CS4231_IFACE_CTRL] |= what;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 } else {
Christopher Zimmermanna1314302005-09-21 00:41:22 -0700522 cs4231_dma_trigger(substream, what, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 chip->image[CS4231_IFACE_CTRL] &= ~what;
524 }
525 snd_cs4231_out(chip, CS4231_IFACE_CTRL,
526 chip->image[CS4231_IFACE_CTRL]);
527 spin_unlock_irqrestore(&chip->lock, flags);
528 break;
529 }
530 default:
531 result = -EINVAL;
532 break;
533 }
Christopher Zimmermanna1314302005-09-21 00:41:22 -0700534
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 return result;
536}
537
538/*
539 * CODEC I/O
540 */
541
542static unsigned char snd_cs4231_get_rate(unsigned int rate)
543{
544 int i;
545
546 for (i = 0; i < 14; i++)
547 if (rate == rates[i])
548 return freq_bits[i];
Krzysztof Helt9e9abb42007-09-10 23:06:55 +0200549
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 return freq_bits[13];
551}
552
Krzysztof Helt9e9abb42007-09-10 23:06:55 +0200553static unsigned char snd_cs4231_get_format(struct snd_cs4231 *chip, int format,
554 int channels)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555{
556 unsigned char rformat;
557
558 rformat = CS4231_LINEAR_8;
559 switch (format) {
Krzysztof Helt9e9abb42007-09-10 23:06:55 +0200560 case SNDRV_PCM_FORMAT_MU_LAW:
561 rformat = CS4231_ULAW_8;
562 break;
563 case SNDRV_PCM_FORMAT_A_LAW:
564 rformat = CS4231_ALAW_8;
565 break;
566 case SNDRV_PCM_FORMAT_S16_LE:
567 rformat = CS4231_LINEAR_16;
568 break;
569 case SNDRV_PCM_FORMAT_S16_BE:
570 rformat = CS4231_LINEAR_16_BIG;
571 break;
572 case SNDRV_PCM_FORMAT_IMA_ADPCM:
573 rformat = CS4231_ADPCM_16;
574 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 }
576 if (channels > 1)
577 rformat |= CS4231_STEREO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 return rformat;
579}
580
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100581static void snd_cs4231_calibrate_mute(struct snd_cs4231 *chip, int mute)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582{
583 unsigned long flags;
584
585 mute = mute ? 1 : 0;
586 spin_lock_irqsave(&chip->lock, flags);
587 if (chip->calibrate_mute == mute) {
588 spin_unlock_irqrestore(&chip->lock, flags);
589 return;
590 }
591 if (!mute) {
592 snd_cs4231_dout(chip, CS4231_LEFT_INPUT,
593 chip->image[CS4231_LEFT_INPUT]);
594 snd_cs4231_dout(chip, CS4231_RIGHT_INPUT,
595 chip->image[CS4231_RIGHT_INPUT]);
596 snd_cs4231_dout(chip, CS4231_LOOPBACK,
597 chip->image[CS4231_LOOPBACK]);
598 }
599 snd_cs4231_dout(chip, CS4231_AUX1_LEFT_INPUT,
600 mute ? 0x80 : chip->image[CS4231_AUX1_LEFT_INPUT]);
601 snd_cs4231_dout(chip, CS4231_AUX1_RIGHT_INPUT,
602 mute ? 0x80 : chip->image[CS4231_AUX1_RIGHT_INPUT]);
603 snd_cs4231_dout(chip, CS4231_AUX2_LEFT_INPUT,
604 mute ? 0x80 : chip->image[CS4231_AUX2_LEFT_INPUT]);
605 snd_cs4231_dout(chip, CS4231_AUX2_RIGHT_INPUT,
606 mute ? 0x80 : chip->image[CS4231_AUX2_RIGHT_INPUT]);
607 snd_cs4231_dout(chip, CS4231_LEFT_OUTPUT,
608 mute ? 0x80 : chip->image[CS4231_LEFT_OUTPUT]);
609 snd_cs4231_dout(chip, CS4231_RIGHT_OUTPUT,
610 mute ? 0x80 : chip->image[CS4231_RIGHT_OUTPUT]);
611 snd_cs4231_dout(chip, CS4231_LEFT_LINE_IN,
612 mute ? 0x80 : chip->image[CS4231_LEFT_LINE_IN]);
613 snd_cs4231_dout(chip, CS4231_RIGHT_LINE_IN,
614 mute ? 0x80 : chip->image[CS4231_RIGHT_LINE_IN]);
615 snd_cs4231_dout(chip, CS4231_MONO_CTRL,
616 mute ? 0xc0 : chip->image[CS4231_MONO_CTRL]);
617 chip->calibrate_mute = mute;
618 spin_unlock_irqrestore(&chip->lock, flags);
619}
620
Krzysztof Helt9e9abb42007-09-10 23:06:55 +0200621static void snd_cs4231_playback_format(struct snd_cs4231 *chip,
622 struct snd_pcm_hw_params *params,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 unsigned char pdfr)
624{
625 unsigned long flags;
626
Ingo Molnar12aa7572006-01-16 16:36:05 +0100627 mutex_lock(&chip->mce_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 snd_cs4231_calibrate_mute(chip, 1);
629
630 snd_cs4231_mce_up(chip);
631
632 spin_lock_irqsave(&chip->lock, flags);
633 snd_cs4231_out(chip, CS4231_PLAYBK_FORMAT,
634 (chip->image[CS4231_IFACE_CTRL] & CS4231_RECORD_ENABLE) ?
635 (pdfr & 0xf0) | (chip->image[CS4231_REC_FORMAT] & 0x0f) :
636 pdfr);
637 spin_unlock_irqrestore(&chip->lock, flags);
638
639 snd_cs4231_mce_down(chip);
640
641 snd_cs4231_calibrate_mute(chip, 0);
Ingo Molnar12aa7572006-01-16 16:36:05 +0100642 mutex_unlock(&chip->mce_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643}
644
Krzysztof Helt9e9abb42007-09-10 23:06:55 +0200645static void snd_cs4231_capture_format(struct snd_cs4231 *chip,
646 struct snd_pcm_hw_params *params,
647 unsigned char cdfr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648{
649 unsigned long flags;
650
Ingo Molnar12aa7572006-01-16 16:36:05 +0100651 mutex_lock(&chip->mce_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 snd_cs4231_calibrate_mute(chip, 1);
653
654 snd_cs4231_mce_up(chip);
655
656 spin_lock_irqsave(&chip->lock, flags);
657 if (!(chip->image[CS4231_IFACE_CTRL] & CS4231_PLAYBACK_ENABLE)) {
658 snd_cs4231_out(chip, CS4231_PLAYBK_FORMAT,
659 ((chip->image[CS4231_PLAYBK_FORMAT]) & 0xf0) |
660 (cdfr & 0x0f));
661 spin_unlock_irqrestore(&chip->lock, flags);
662 snd_cs4231_mce_down(chip);
663 snd_cs4231_mce_up(chip);
664 spin_lock_irqsave(&chip->lock, flags);
665 }
666 snd_cs4231_out(chip, CS4231_REC_FORMAT, cdfr);
667 spin_unlock_irqrestore(&chip->lock, flags);
668
669 snd_cs4231_mce_down(chip);
670
671 snd_cs4231_calibrate_mute(chip, 0);
Ingo Molnar12aa7572006-01-16 16:36:05 +0100672 mutex_unlock(&chip->mce_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673}
674
675/*
676 * Timer interface
677 */
678
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100679static unsigned long snd_cs4231_timer_resolution(struct snd_timer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680{
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100681 struct snd_cs4231 *chip = snd_timer_chip(timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
683 return chip->image[CS4231_PLAYBK_FORMAT] & 1 ? 9969 : 9920;
684}
685
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100686static int snd_cs4231_timer_start(struct snd_timer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687{
688 unsigned long flags;
689 unsigned int ticks;
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100690 struct snd_cs4231 *chip = snd_timer_chip(timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
692 spin_lock_irqsave(&chip->lock, flags);
693 ticks = timer->sticks;
694 if ((chip->image[CS4231_ALT_FEATURE_1] & CS4231_TIMER_ENABLE) == 0 ||
695 (unsigned char)(ticks >> 8) != chip->image[CS4231_TIMER_HIGH] ||
696 (unsigned char)ticks != chip->image[CS4231_TIMER_LOW]) {
697 snd_cs4231_out(chip, CS4231_TIMER_HIGH,
698 chip->image[CS4231_TIMER_HIGH] =
699 (unsigned char) (ticks >> 8));
700 snd_cs4231_out(chip, CS4231_TIMER_LOW,
701 chip->image[CS4231_TIMER_LOW] =
702 (unsigned char) ticks);
703 snd_cs4231_out(chip, CS4231_ALT_FEATURE_1,
Krzysztof Helt9e9abb42007-09-10 23:06:55 +0200704 chip->image[CS4231_ALT_FEATURE_1] |
705 CS4231_TIMER_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 }
707 spin_unlock_irqrestore(&chip->lock, flags);
708
709 return 0;
710}
711
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100712static int snd_cs4231_timer_stop(struct snd_timer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713{
714 unsigned long flags;
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100715 struct snd_cs4231 *chip = snd_timer_chip(timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
717 spin_lock_irqsave(&chip->lock, flags);
Krzysztof Helt9e9abb42007-09-10 23:06:55 +0200718 chip->image[CS4231_ALT_FEATURE_1] &= ~CS4231_TIMER_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 snd_cs4231_out(chip, CS4231_ALT_FEATURE_1,
Krzysztof Helt9e9abb42007-09-10 23:06:55 +0200720 chip->image[CS4231_ALT_FEATURE_1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 spin_unlock_irqrestore(&chip->lock, flags);
722
723 return 0;
724}
725
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100726static void __init snd_cs4231_init(struct snd_cs4231 *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727{
728 unsigned long flags;
729
730 snd_cs4231_mce_down(chip);
731
732#ifdef SNDRV_DEBUG_MCE
Christopher Zimmermanna1314302005-09-21 00:41:22 -0700733 snd_printdd("init: (1)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734#endif
735 snd_cs4231_mce_up(chip);
736 spin_lock_irqsave(&chip->lock, flags);
Krzysztof Helt9e9abb42007-09-10 23:06:55 +0200737 chip->image[CS4231_IFACE_CTRL] &= ~(CS4231_PLAYBACK_ENABLE |
738 CS4231_PLAYBACK_PIO |
739 CS4231_RECORD_ENABLE |
740 CS4231_RECORD_PIO |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 CS4231_CALIB_MODE);
742 chip->image[CS4231_IFACE_CTRL] |= CS4231_AUTOCALIB;
743 snd_cs4231_out(chip, CS4231_IFACE_CTRL, chip->image[CS4231_IFACE_CTRL]);
744 spin_unlock_irqrestore(&chip->lock, flags);
745 snd_cs4231_mce_down(chip);
746
747#ifdef SNDRV_DEBUG_MCE
Christopher Zimmermanna1314302005-09-21 00:41:22 -0700748 snd_printdd("init: (2)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749#endif
750
751 snd_cs4231_mce_up(chip);
752 spin_lock_irqsave(&chip->lock, flags);
Krzysztof Helt9e9abb42007-09-10 23:06:55 +0200753 snd_cs4231_out(chip, CS4231_ALT_FEATURE_1,
754 chip->image[CS4231_ALT_FEATURE_1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 spin_unlock_irqrestore(&chip->lock, flags);
756 snd_cs4231_mce_down(chip);
757
758#ifdef SNDRV_DEBUG_MCE
Krzysztof Helt9e9abb42007-09-10 23:06:55 +0200759 snd_printdd("init: (3) - afei = 0x%x\n",
760 chip->image[CS4231_ALT_FEATURE_1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761#endif
762
763 spin_lock_irqsave(&chip->lock, flags);
Krzysztof Helt9e9abb42007-09-10 23:06:55 +0200764 snd_cs4231_out(chip, CS4231_ALT_FEATURE_2,
765 chip->image[CS4231_ALT_FEATURE_2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 spin_unlock_irqrestore(&chip->lock, flags);
767
768 snd_cs4231_mce_up(chip);
769 spin_lock_irqsave(&chip->lock, flags);
Krzysztof Helt9e9abb42007-09-10 23:06:55 +0200770 snd_cs4231_out(chip, CS4231_PLAYBK_FORMAT,
771 chip->image[CS4231_PLAYBK_FORMAT]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 spin_unlock_irqrestore(&chip->lock, flags);
773 snd_cs4231_mce_down(chip);
774
775#ifdef SNDRV_DEBUG_MCE
Christopher Zimmermanna1314302005-09-21 00:41:22 -0700776 snd_printdd("init: (4)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777#endif
778
779 snd_cs4231_mce_up(chip);
780 spin_lock_irqsave(&chip->lock, flags);
781 snd_cs4231_out(chip, CS4231_REC_FORMAT, chip->image[CS4231_REC_FORMAT]);
782 spin_unlock_irqrestore(&chip->lock, flags);
783 snd_cs4231_mce_down(chip);
784
785#ifdef SNDRV_DEBUG_MCE
Christopher Zimmermanna1314302005-09-21 00:41:22 -0700786 snd_printdd("init: (5)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787#endif
788}
789
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100790static int snd_cs4231_open(struct snd_cs4231 *chip, unsigned int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791{
792 unsigned long flags;
793
Ingo Molnar12aa7572006-01-16 16:36:05 +0100794 mutex_lock(&chip->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 if ((chip->mode & mode)) {
Ingo Molnar12aa7572006-01-16 16:36:05 +0100796 mutex_unlock(&chip->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 return -EAGAIN;
798 }
799 if (chip->mode & CS4231_MODE_OPEN) {
800 chip->mode |= mode;
Ingo Molnar12aa7572006-01-16 16:36:05 +0100801 mutex_unlock(&chip->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 return 0;
803 }
804 /* ok. now enable and ack CODEC IRQ */
805 spin_lock_irqsave(&chip->lock, flags);
806 snd_cs4231_out(chip, CS4231_IRQ_STATUS, CS4231_PLAYBACK_IRQ |
807 CS4231_RECORD_IRQ |
808 CS4231_TIMER_IRQ);
809 snd_cs4231_out(chip, CS4231_IRQ_STATUS, 0);
Krzysztof Helt7e52f3d2007-09-05 15:08:23 +0200810 __cs4231_writeb(chip, 0, CS4231U(chip, STATUS)); /* clear IRQ */
811 __cs4231_writeb(chip, 0, CS4231U(chip, STATUS)); /* clear IRQ */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812
813 snd_cs4231_out(chip, CS4231_IRQ_STATUS, CS4231_PLAYBACK_IRQ |
814 CS4231_RECORD_IRQ |
815 CS4231_TIMER_IRQ);
816 snd_cs4231_out(chip, CS4231_IRQ_STATUS, 0);
Christopher Zimmermanna1314302005-09-21 00:41:22 -0700817
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 spin_unlock_irqrestore(&chip->lock, flags);
819
820 chip->mode = mode;
Ingo Molnar12aa7572006-01-16 16:36:05 +0100821 mutex_unlock(&chip->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 return 0;
823}
824
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100825static void snd_cs4231_close(struct snd_cs4231 *chip, unsigned int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826{
827 unsigned long flags;
828
Ingo Molnar12aa7572006-01-16 16:36:05 +0100829 mutex_lock(&chip->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 chip->mode &= ~mode;
831 if (chip->mode & CS4231_MODE_OPEN) {
Ingo Molnar12aa7572006-01-16 16:36:05 +0100832 mutex_unlock(&chip->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 return;
834 }
835 snd_cs4231_calibrate_mute(chip, 1);
836
837 /* disable IRQ */
838 spin_lock_irqsave(&chip->lock, flags);
839 snd_cs4231_out(chip, CS4231_IRQ_STATUS, 0);
Krzysztof Helt7e52f3d2007-09-05 15:08:23 +0200840 __cs4231_writeb(chip, 0, CS4231U(chip, STATUS)); /* clear IRQ */
841 __cs4231_writeb(chip, 0, CS4231U(chip, STATUS)); /* clear IRQ */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842
843 /* now disable record & playback */
844
845 if (chip->image[CS4231_IFACE_CTRL] &
846 (CS4231_PLAYBACK_ENABLE | CS4231_PLAYBACK_PIO |
847 CS4231_RECORD_ENABLE | CS4231_RECORD_PIO)) {
848 spin_unlock_irqrestore(&chip->lock, flags);
849 snd_cs4231_mce_up(chip);
850 spin_lock_irqsave(&chip->lock, flags);
851 chip->image[CS4231_IFACE_CTRL] &=
852 ~(CS4231_PLAYBACK_ENABLE | CS4231_PLAYBACK_PIO |
853 CS4231_RECORD_ENABLE | CS4231_RECORD_PIO);
Krzysztof Helt9e9abb42007-09-10 23:06:55 +0200854 snd_cs4231_out(chip, CS4231_IFACE_CTRL,
855 chip->image[CS4231_IFACE_CTRL]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 spin_unlock_irqrestore(&chip->lock, flags);
857 snd_cs4231_mce_down(chip);
858 spin_lock_irqsave(&chip->lock, flags);
859 }
860
861 /* clear IRQ again */
862 snd_cs4231_out(chip, CS4231_IRQ_STATUS, 0);
Krzysztof Helt7e52f3d2007-09-05 15:08:23 +0200863 __cs4231_writeb(chip, 0, CS4231U(chip, STATUS)); /* clear IRQ */
864 __cs4231_writeb(chip, 0, CS4231U(chip, STATUS)); /* clear IRQ */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 spin_unlock_irqrestore(&chip->lock, flags);
866
867 snd_cs4231_calibrate_mute(chip, 0);
868
869 chip->mode = 0;
Ingo Molnar12aa7572006-01-16 16:36:05 +0100870 mutex_unlock(&chip->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871}
872
873/*
874 * timer open/close
875 */
876
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100877static int snd_cs4231_timer_open(struct snd_timer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878{
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100879 struct snd_cs4231 *chip = snd_timer_chip(timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 snd_cs4231_open(chip, CS4231_MODE_TIMER);
881 return 0;
882}
883
Krzysztof Helt9e9abb42007-09-10 23:06:55 +0200884static int snd_cs4231_timer_close(struct snd_timer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885{
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100886 struct snd_cs4231 *chip = snd_timer_chip(timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 snd_cs4231_close(chip, CS4231_MODE_TIMER);
888 return 0;
889}
890
Krzysztof Helt9e9abb42007-09-10 23:06:55 +0200891static struct snd_timer_hardware snd_cs4231_timer_table = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 .flags = SNDRV_TIMER_HW_AUTO,
893 .resolution = 9945,
894 .ticks = 65535,
895 .open = snd_cs4231_timer_open,
896 .close = snd_cs4231_timer_close,
897 .c_resolution = snd_cs4231_timer_resolution,
898 .start = snd_cs4231_timer_start,
899 .stop = snd_cs4231_timer_stop,
900};
901
902/*
903 * ok.. exported functions..
904 */
905
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100906static int snd_cs4231_playback_hw_params(struct snd_pcm_substream *substream,
907 struct snd_pcm_hw_params *hw_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908{
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100909 struct snd_cs4231 *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 unsigned char new_pdfr;
911 int err;
912
Krzysztof Helt9e9abb42007-09-10 23:06:55 +0200913 err = snd_pcm_lib_malloc_pages(substream,
914 params_buffer_bytes(hw_params));
915 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 return err;
917 new_pdfr = snd_cs4231_get_format(chip, params_format(hw_params),
918 params_channels(hw_params)) |
919 snd_cs4231_get_rate(params_rate(hw_params));
920 snd_cs4231_playback_format(chip, hw_params, new_pdfr);
921
922 return 0;
923}
924
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100925static int snd_cs4231_playback_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926{
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100927 struct snd_cs4231 *chip = snd_pcm_substream_chip(substream);
928 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 unsigned long flags;
930
931 spin_lock_irqsave(&chip->lock, flags);
Christopher Zimmermanna1314302005-09-21 00:41:22 -0700932
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 chip->image[CS4231_IFACE_CTRL] &= ~(CS4231_PLAYBACK_ENABLE |
934 CS4231_PLAYBACK_PIO);
Christopher Zimmermanna1314302005-09-21 00:41:22 -0700935
Eric Sesterhenn817dd6e2006-03-24 18:49:12 +0100936 BUG_ON(runtime->period_size > 0xffff + 1);
Christopher Zimmermanna1314302005-09-21 00:41:22 -0700937
Christopher Zimmermanna1314302005-09-21 00:41:22 -0700938 chip->p_periods_sent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 spin_unlock_irqrestore(&chip->lock, flags);
940
941 return 0;
942}
943
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100944static int snd_cs4231_capture_hw_params(struct snd_pcm_substream *substream,
945 struct snd_pcm_hw_params *hw_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946{
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100947 struct snd_cs4231 *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 unsigned char new_cdfr;
949 int err;
950
Krzysztof Helt9e9abb42007-09-10 23:06:55 +0200951 err = snd_pcm_lib_malloc_pages(substream,
952 params_buffer_bytes(hw_params));
953 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 return err;
955 new_cdfr = snd_cs4231_get_format(chip, params_format(hw_params),
956 params_channels(hw_params)) |
957 snd_cs4231_get_rate(params_rate(hw_params));
958 snd_cs4231_capture_format(chip, hw_params, new_cdfr);
959
960 return 0;
961}
962
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100963static int snd_cs4231_capture_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964{
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100965 struct snd_cs4231 *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 unsigned long flags;
967
968 spin_lock_irqsave(&chip->lock, flags);
969 chip->image[CS4231_IFACE_CTRL] &= ~(CS4231_RECORD_ENABLE |
970 CS4231_RECORD_PIO);
971
Christopher Zimmermanna1314302005-09-21 00:41:22 -0700972
Georg Chini5a820fa2005-11-07 14:08:25 -0800973 chip->c_periods_sent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 spin_unlock_irqrestore(&chip->lock, flags);
975
976 return 0;
977}
978
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100979static void snd_cs4231_overrange(struct snd_cs4231 *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980{
981 unsigned long flags;
982 unsigned char res;
983
984 spin_lock_irqsave(&chip->lock, flags);
985 res = snd_cs4231_in(chip, CS4231_TEST_INIT);
986 spin_unlock_irqrestore(&chip->lock, flags);
987
Krzysztof Helt9e9abb42007-09-10 23:06:55 +0200988 /* detect overrange only above 0dB; may be user selectable? */
989 if (res & (0x08 | 0x02))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 chip->capture_substream->runtime->overrange++;
991}
992
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100993static void snd_cs4231_play_callback(struct snd_cs4231 *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 if (chip->image[CS4231_IFACE_CTRL] & CS4231_PLAYBACK_ENABLE) {
996 snd_pcm_period_elapsed(chip->playback_substream);
Georg Chinib1282542005-11-07 14:09:19 -0800997 snd_cs4231_advance_dma(&chip->p_dma, chip->playback_substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 &chip->p_periods_sent);
999 }
1000}
1001
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001002static void snd_cs4231_capture_callback(struct snd_cs4231 *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 if (chip->image[CS4231_IFACE_CTRL] & CS4231_RECORD_ENABLE) {
1005 snd_pcm_period_elapsed(chip->capture_substream);
Georg Chinib1282542005-11-07 14:09:19 -08001006 snd_cs4231_advance_dma(&chip->c_dma, chip->capture_substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 &chip->c_periods_sent);
1008 }
1009}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001011static snd_pcm_uframes_t snd_cs4231_playback_pointer(
1012 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013{
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001014 struct snd_cs4231 *chip = snd_pcm_substream_chip(substream);
1015 struct cs4231_dma_control *dma_cont = &chip->p_dma;
Georg Chini5a820fa2005-11-07 14:08:25 -08001016 size_t ptr;
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001017
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 if (!(chip->image[CS4231_IFACE_CTRL] & CS4231_PLAYBACK_ENABLE))
1019 return 0;
Georg Chinib1282542005-11-07 14:09:19 -08001020 ptr = dma_cont->address(dma_cont);
1021 if (ptr != 0)
1022 ptr -= substream->runtime->dma_addr;
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001023
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 return bytes_to_frames(substream->runtime, ptr);
1025}
1026
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001027static snd_pcm_uframes_t snd_cs4231_capture_pointer(
1028 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029{
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001030 struct snd_cs4231 *chip = snd_pcm_substream_chip(substream);
1031 struct cs4231_dma_control *dma_cont = &chip->c_dma;
Georg Chini5a820fa2005-11-07 14:08:25 -08001032 size_t ptr;
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001033
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 if (!(chip->image[CS4231_IFACE_CTRL] & CS4231_RECORD_ENABLE))
1035 return 0;
Georg Chinib1282542005-11-07 14:09:19 -08001036 ptr = dma_cont->address(dma_cont);
1037 if (ptr != 0)
1038 ptr -= substream->runtime->dma_addr;
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001039
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 return bytes_to_frames(substream->runtime, ptr);
1041}
1042
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001043static int __init snd_cs4231_probe(struct snd_cs4231 *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044{
1045 unsigned long flags;
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001046 int i;
1047 int id = 0;
1048 int vers = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 unsigned char *ptr;
1050
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 for (i = 0; i < 50; i++) {
1052 mb();
Krzysztof Helt7e52f3d2007-09-05 15:08:23 +02001053 if (__cs4231_readb(chip, CS4231U(chip, REGSEL)) & CS4231_INIT)
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001054 msleep(2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 else {
1056 spin_lock_irqsave(&chip->lock, flags);
1057 snd_cs4231_out(chip, CS4231_MISC_INFO, CS4231_MODE2);
1058 id = snd_cs4231_in(chip, CS4231_MISC_INFO) & 0x0f;
1059 vers = snd_cs4231_in(chip, CS4231_VERSION);
1060 spin_unlock_irqrestore(&chip->lock, flags);
1061 if (id == 0x0a)
1062 break; /* this is valid value */
1063 }
1064 }
1065 snd_printdd("cs4231: port = %p, id = 0x%x\n", chip->port, id);
1066 if (id != 0x0a)
1067 return -ENODEV; /* no valid device found */
1068
1069 spin_lock_irqsave(&chip->lock, flags);
1070
Krzysztof Helt7e52f3d2007-09-05 15:08:23 +02001071 /* clear any pendings IRQ */
1072 __cs4231_readb(chip, CS4231U(chip, STATUS));
1073 __cs4231_writeb(chip, 0, CS4231U(chip, STATUS));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 mb();
1075
1076 spin_unlock_irqrestore(&chip->lock, flags);
1077
1078 chip->image[CS4231_MISC_INFO] = CS4231_MODE2;
1079 chip->image[CS4231_IFACE_CTRL] =
1080 chip->image[CS4231_IFACE_CTRL] & ~CS4231_SINGLE_DMA;
1081 chip->image[CS4231_ALT_FEATURE_1] = 0x80;
1082 chip->image[CS4231_ALT_FEATURE_2] = 0x01;
1083 if (vers & 0x20)
1084 chip->image[CS4231_ALT_FEATURE_2] |= 0x02;
1085
1086 ptr = (unsigned char *) &chip->image;
1087
1088 snd_cs4231_mce_down(chip);
1089
1090 spin_lock_irqsave(&chip->lock, flags);
1091
1092 for (i = 0; i < 32; i++) /* ok.. fill all CS4231 registers */
1093 snd_cs4231_out(chip, i, *ptr++);
1094
1095 spin_unlock_irqrestore(&chip->lock, flags);
1096
1097 snd_cs4231_mce_up(chip);
1098
1099 snd_cs4231_mce_down(chip);
1100
1101 mdelay(2);
1102
1103 return 0; /* all things are ok.. */
1104}
1105
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001106static struct snd_pcm_hardware snd_cs4231_playback = {
1107 .info = SNDRV_PCM_INFO_MMAP |
1108 SNDRV_PCM_INFO_INTERLEAVED |
1109 SNDRV_PCM_INFO_MMAP_VALID |
1110 SNDRV_PCM_INFO_SYNC_START,
1111 .formats = SNDRV_PCM_FMTBIT_MU_LAW |
1112 SNDRV_PCM_FMTBIT_A_LAW |
1113 SNDRV_PCM_FMTBIT_IMA_ADPCM |
1114 SNDRV_PCM_FMTBIT_U8 |
1115 SNDRV_PCM_FMTBIT_S16_LE |
1116 SNDRV_PCM_FMTBIT_S16_BE,
1117 .rates = SNDRV_PCM_RATE_KNOT |
1118 SNDRV_PCM_RATE_8000_48000,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 .rate_min = 5510,
1120 .rate_max = 48000,
1121 .channels_min = 1,
1122 .channels_max = 2,
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001123 .buffer_bytes_max = 32 * 1024,
David S. Millerf9af1d92007-01-03 18:51:54 -08001124 .period_bytes_min = 64,
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001125 .period_bytes_max = 32 * 1024,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 .periods_min = 1,
1127 .periods_max = 1024,
1128};
1129
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001130static struct snd_pcm_hardware snd_cs4231_capture = {
1131 .info = SNDRV_PCM_INFO_MMAP |
1132 SNDRV_PCM_INFO_INTERLEAVED |
1133 SNDRV_PCM_INFO_MMAP_VALID |
1134 SNDRV_PCM_INFO_SYNC_START,
1135 .formats = SNDRV_PCM_FMTBIT_MU_LAW |
1136 SNDRV_PCM_FMTBIT_A_LAW |
1137 SNDRV_PCM_FMTBIT_IMA_ADPCM |
1138 SNDRV_PCM_FMTBIT_U8 |
1139 SNDRV_PCM_FMTBIT_S16_LE |
1140 SNDRV_PCM_FMTBIT_S16_BE,
1141 .rates = SNDRV_PCM_RATE_KNOT |
1142 SNDRV_PCM_RATE_8000_48000,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 .rate_min = 5510,
1144 .rate_max = 48000,
1145 .channels_min = 1,
1146 .channels_max = 2,
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001147 .buffer_bytes_max = 32 * 1024,
David S. Millerf9af1d92007-01-03 18:51:54 -08001148 .period_bytes_min = 64,
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001149 .period_bytes_max = 32 * 1024,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 .periods_min = 1,
1151 .periods_max = 1024,
1152};
1153
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001154static int snd_cs4231_playback_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155{
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001156 struct snd_cs4231 *chip = snd_pcm_substream_chip(substream);
1157 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 int err;
1159
1160 runtime->hw = snd_cs4231_playback;
1161
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001162 err = snd_cs4231_open(chip, CS4231_MODE_PLAY);
1163 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 snd_free_pages(runtime->dma_area, runtime->dma_bytes);
1165 return err;
1166 }
1167 chip->playback_substream = substream;
1168 chip->p_periods_sent = 0;
1169 snd_pcm_set_sync(substream);
1170 snd_cs4231_xrate(runtime);
1171
1172 return 0;
1173}
1174
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001175static int snd_cs4231_capture_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176{
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001177 struct snd_cs4231 *chip = snd_pcm_substream_chip(substream);
1178 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 int err;
1180
1181 runtime->hw = snd_cs4231_capture;
1182
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001183 err = snd_cs4231_open(chip, CS4231_MODE_RECORD);
1184 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 snd_free_pages(runtime->dma_area, runtime->dma_bytes);
1186 return err;
1187 }
1188 chip->capture_substream = substream;
1189 chip->c_periods_sent = 0;
1190 snd_pcm_set_sync(substream);
1191 snd_cs4231_xrate(runtime);
1192
1193 return 0;
1194}
1195
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001196static int snd_cs4231_playback_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197{
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001198 struct snd_cs4231 *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 snd_cs4231_close(chip, CS4231_MODE_PLAY);
Georg Chinib1282542005-11-07 14:09:19 -08001201 chip->playback_substream = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202
1203 return 0;
1204}
1205
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001206static int snd_cs4231_capture_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207{
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001208 struct snd_cs4231 *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 snd_cs4231_close(chip, CS4231_MODE_RECORD);
Georg Chinib1282542005-11-07 14:09:19 -08001211 chip->capture_substream = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212
1213 return 0;
1214}
1215
1216/* XXX We can do some power-management, in particular on EBUS using
1217 * XXX the audio AUXIO register...
1218 */
1219
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001220static struct snd_pcm_ops snd_cs4231_playback_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 .open = snd_cs4231_playback_open,
1222 .close = snd_cs4231_playback_close,
1223 .ioctl = snd_pcm_lib_ioctl,
1224 .hw_params = snd_cs4231_playback_hw_params,
Krzysztof Heltc6c2d572007-09-04 13:08:24 +02001225 .hw_free = snd_pcm_lib_free_pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 .prepare = snd_cs4231_playback_prepare,
1227 .trigger = snd_cs4231_trigger,
1228 .pointer = snd_cs4231_playback_pointer,
1229};
1230
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001231static struct snd_pcm_ops snd_cs4231_capture_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 .open = snd_cs4231_capture_open,
1233 .close = snd_cs4231_capture_close,
1234 .ioctl = snd_pcm_lib_ioctl,
1235 .hw_params = snd_cs4231_capture_hw_params,
Krzysztof Heltc6c2d572007-09-04 13:08:24 +02001236 .hw_free = snd_pcm_lib_free_pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 .prepare = snd_cs4231_capture_prepare,
1238 .trigger = snd_cs4231_trigger,
1239 .pointer = snd_cs4231_capture_pointer,
1240};
1241
Krzysztof Heltc6c2d572007-09-04 13:08:24 +02001242static int __init snd_cs4231_pcm(struct snd_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243{
Krzysztof Heltc6c2d572007-09-04 13:08:24 +02001244 struct snd_cs4231 *chip = card->private_data;
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001245 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 int err;
1247
Krzysztof Heltc6c2d572007-09-04 13:08:24 +02001248 err = snd_pcm_new(card, "CS4231", 0, 1, 1, &pcm);
1249 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 return err;
1251
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001252 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
1253 &snd_cs4231_playback_ops);
1254 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
1255 &snd_cs4231_capture_ops);
1256
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 /* global setup */
1258 pcm->private_data = chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 pcm->info_flags = SNDRV_PCM_INFO_JOINT_DUPLEX;
1260 strcpy(pcm->name, "CS4231");
1261
Georg Chinib1282542005-11-07 14:09:19 -08001262 chip->p_dma.preallocate(chip, pcm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263
1264 chip->pcm = pcm;
1265
1266 return 0;
1267}
1268
Krzysztof Heltc6c2d572007-09-04 13:08:24 +02001269static int __init snd_cs4231_timer(struct snd_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270{
Krzysztof Heltc6c2d572007-09-04 13:08:24 +02001271 struct snd_cs4231 *chip = card->private_data;
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001272 struct snd_timer *timer;
1273 struct snd_timer_id tid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 int err;
1275
1276 /* Timer initialization */
1277 tid.dev_class = SNDRV_TIMER_CLASS_CARD;
1278 tid.dev_sclass = SNDRV_TIMER_SCLASS_NONE;
Krzysztof Heltc6c2d572007-09-04 13:08:24 +02001279 tid.card = card->number;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 tid.device = 0;
1281 tid.subdevice = 0;
Krzysztof Heltc6c2d572007-09-04 13:08:24 +02001282 err = snd_timer_new(card, "CS4231", &tid, &timer);
1283 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 return err;
1285 strcpy(timer->name, "CS4231");
1286 timer->private_data = chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 timer->hw = snd_cs4231_timer_table;
1288 chip->timer = timer;
1289
1290 return 0;
1291}
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001292
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293/*
1294 * MIXER part
1295 */
1296
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001297static int snd_cs4231_info_mux(struct snd_kcontrol *kcontrol,
1298 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299{
1300 static char *texts[4] = {
1301 "Line", "CD", "Mic", "Mix"
1302 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1305 uinfo->count = 2;
1306 uinfo->value.enumerated.items = 4;
1307 if (uinfo->value.enumerated.item > 3)
1308 uinfo->value.enumerated.item = 3;
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001309 strcpy(uinfo->value.enumerated.name,
1310 texts[uinfo->value.enumerated.item]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311
1312 return 0;
1313}
1314
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001315static int snd_cs4231_get_mux(struct snd_kcontrol *kcontrol,
1316 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317{
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001318 struct snd_cs4231 *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 unsigned long flags;
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001320
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 spin_lock_irqsave(&chip->lock, flags);
1322 ucontrol->value.enumerated.item[0] =
1323 (chip->image[CS4231_LEFT_INPUT] & CS4231_MIXS_ALL) >> 6;
1324 ucontrol->value.enumerated.item[1] =
1325 (chip->image[CS4231_RIGHT_INPUT] & CS4231_MIXS_ALL) >> 6;
1326 spin_unlock_irqrestore(&chip->lock, flags);
1327
1328 return 0;
1329}
1330
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001331static int snd_cs4231_put_mux(struct snd_kcontrol *kcontrol,
1332 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333{
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001334 struct snd_cs4231 *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 unsigned long flags;
1336 unsigned short left, right;
1337 int change;
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001338
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 if (ucontrol->value.enumerated.item[0] > 3 ||
1340 ucontrol->value.enumerated.item[1] > 3)
1341 return -EINVAL;
1342 left = ucontrol->value.enumerated.item[0] << 6;
1343 right = ucontrol->value.enumerated.item[1] << 6;
1344
1345 spin_lock_irqsave(&chip->lock, flags);
1346
1347 left = (chip->image[CS4231_LEFT_INPUT] & ~CS4231_MIXS_ALL) | left;
1348 right = (chip->image[CS4231_RIGHT_INPUT] & ~CS4231_MIXS_ALL) | right;
1349 change = left != chip->image[CS4231_LEFT_INPUT] ||
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001350 right != chip->image[CS4231_RIGHT_INPUT];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 snd_cs4231_out(chip, CS4231_LEFT_INPUT, left);
1352 snd_cs4231_out(chip, CS4231_RIGHT_INPUT, right);
1353
1354 spin_unlock_irqrestore(&chip->lock, flags);
1355
1356 return change;
1357}
1358
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001359static int snd_cs4231_info_single(struct snd_kcontrol *kcontrol,
1360 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361{
1362 int mask = (kcontrol->private_value >> 16) & 0xff;
1363
1364 uinfo->type = (mask == 1) ?
1365 SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
1366 uinfo->count = 1;
1367 uinfo->value.integer.min = 0;
1368 uinfo->value.integer.max = mask;
1369
1370 return 0;
1371}
1372
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001373static int snd_cs4231_get_single(struct snd_kcontrol *kcontrol,
1374 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375{
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001376 struct snd_cs4231 *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 unsigned long flags;
1378 int reg = kcontrol->private_value & 0xff;
1379 int shift = (kcontrol->private_value >> 8) & 0xff;
1380 int mask = (kcontrol->private_value >> 16) & 0xff;
1381 int invert = (kcontrol->private_value >> 24) & 0xff;
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001382
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 spin_lock_irqsave(&chip->lock, flags);
1384
1385 ucontrol->value.integer.value[0] = (chip->image[reg] >> shift) & mask;
1386
1387 spin_unlock_irqrestore(&chip->lock, flags);
1388
1389 if (invert)
1390 ucontrol->value.integer.value[0] =
1391 (mask - ucontrol->value.integer.value[0]);
1392
1393 return 0;
1394}
1395
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001396static int snd_cs4231_put_single(struct snd_kcontrol *kcontrol,
1397 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398{
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001399 struct snd_cs4231 *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 unsigned long flags;
1401 int reg = kcontrol->private_value & 0xff;
1402 int shift = (kcontrol->private_value >> 8) & 0xff;
1403 int mask = (kcontrol->private_value >> 16) & 0xff;
1404 int invert = (kcontrol->private_value >> 24) & 0xff;
1405 int change;
1406 unsigned short val;
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001407
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 val = (ucontrol->value.integer.value[0] & mask);
1409 if (invert)
1410 val = mask - val;
1411 val <<= shift;
1412
1413 spin_lock_irqsave(&chip->lock, flags);
1414
1415 val = (chip->image[reg] & ~(mask << shift)) | val;
1416 change = val != chip->image[reg];
1417 snd_cs4231_out(chip, reg, val);
1418
1419 spin_unlock_irqrestore(&chip->lock, flags);
1420
1421 return change;
1422}
1423
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001424static int snd_cs4231_info_double(struct snd_kcontrol *kcontrol,
1425 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426{
1427 int mask = (kcontrol->private_value >> 24) & 0xff;
1428
1429 uinfo->type = mask == 1 ?
1430 SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
1431 uinfo->count = 2;
1432 uinfo->value.integer.min = 0;
1433 uinfo->value.integer.max = mask;
1434
1435 return 0;
1436}
1437
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001438static int snd_cs4231_get_double(struct snd_kcontrol *kcontrol,
1439 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440{
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001441 struct snd_cs4231 *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 unsigned long flags;
1443 int left_reg = kcontrol->private_value & 0xff;
1444 int right_reg = (kcontrol->private_value >> 8) & 0xff;
1445 int shift_left = (kcontrol->private_value >> 16) & 0x07;
1446 int shift_right = (kcontrol->private_value >> 19) & 0x07;
1447 int mask = (kcontrol->private_value >> 24) & 0xff;
1448 int invert = (kcontrol->private_value >> 22) & 1;
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001449
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 spin_lock_irqsave(&chip->lock, flags);
1451
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001452 ucontrol->value.integer.value[0] =
1453 (chip->image[left_reg] >> shift_left) & mask;
1454 ucontrol->value.integer.value[1] =
1455 (chip->image[right_reg] >> shift_right) & mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456
1457 spin_unlock_irqrestore(&chip->lock, flags);
1458
1459 if (invert) {
1460 ucontrol->value.integer.value[0] =
1461 (mask - ucontrol->value.integer.value[0]);
1462 ucontrol->value.integer.value[1] =
1463 (mask - ucontrol->value.integer.value[1]);
1464 }
1465
1466 return 0;
1467}
1468
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001469static int snd_cs4231_put_double(struct snd_kcontrol *kcontrol,
1470 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471{
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001472 struct snd_cs4231 *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 unsigned long flags;
1474 int left_reg = kcontrol->private_value & 0xff;
1475 int right_reg = (kcontrol->private_value >> 8) & 0xff;
1476 int shift_left = (kcontrol->private_value >> 16) & 0x07;
1477 int shift_right = (kcontrol->private_value >> 19) & 0x07;
1478 int mask = (kcontrol->private_value >> 24) & 0xff;
1479 int invert = (kcontrol->private_value >> 22) & 1;
1480 int change;
1481 unsigned short val1, val2;
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001482
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 val1 = ucontrol->value.integer.value[0] & mask;
1484 val2 = ucontrol->value.integer.value[1] & mask;
1485 if (invert) {
1486 val1 = mask - val1;
1487 val2 = mask - val2;
1488 }
1489 val1 <<= shift_left;
1490 val2 <<= shift_right;
1491
1492 spin_lock_irqsave(&chip->lock, flags);
1493
1494 val1 = (chip->image[left_reg] & ~(mask << shift_left)) | val1;
1495 val2 = (chip->image[right_reg] & ~(mask << shift_right)) | val2;
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001496 change = val1 != chip->image[left_reg];
1497 change |= val2 != chip->image[right_reg];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 snd_cs4231_out(chip, left_reg, val1);
1499 snd_cs4231_out(chip, right_reg, val2);
1500
1501 spin_unlock_irqrestore(&chip->lock, flags);
1502
1503 return change;
1504}
1505
1506#define CS4231_SINGLE(xname, xindex, reg, shift, mask, invert) \
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001507{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = (xname), .index = (xindex), \
1508 .info = snd_cs4231_info_single, \
1509 .get = snd_cs4231_get_single, .put = snd_cs4231_put_single, \
1510 .private_value = (reg) | ((shift) << 8) | ((mask) << 16) | ((invert) << 24) }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001512#define CS4231_DOUBLE(xname, xindex, left_reg, right_reg, shift_left, \
1513 shift_right, mask, invert) \
1514{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = (xname), .index = (xindex), \
1515 .info = snd_cs4231_info_double, \
1516 .get = snd_cs4231_get_double, .put = snd_cs4231_put_double, \
1517 .private_value = (left_reg) | ((right_reg) << 8) | ((shift_left) << 16) | \
1518 ((shift_right) << 19) | ((mask) << 24) | ((invert) << 22) }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001520static struct snd_kcontrol_new snd_cs4231_controls[] __initdata = {
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001521CS4231_DOUBLE("PCM Playback Switch", 0, CS4231_LEFT_OUTPUT,
1522 CS4231_RIGHT_OUTPUT, 7, 7, 1, 1),
1523CS4231_DOUBLE("PCM Playback Volume", 0, CS4231_LEFT_OUTPUT,
1524 CS4231_RIGHT_OUTPUT, 0, 0, 63, 1),
1525CS4231_DOUBLE("Line Playback Switch", 0, CS4231_LEFT_LINE_IN,
1526 CS4231_RIGHT_LINE_IN, 7, 7, 1, 1),
1527CS4231_DOUBLE("Line Playback Volume", 0, CS4231_LEFT_LINE_IN,
1528 CS4231_RIGHT_LINE_IN, 0, 0, 31, 1),
1529CS4231_DOUBLE("Aux Playback Switch", 0, CS4231_AUX1_LEFT_INPUT,
1530 CS4231_AUX1_RIGHT_INPUT, 7, 7, 1, 1),
1531CS4231_DOUBLE("Aux Playback Volume", 0, CS4231_AUX1_LEFT_INPUT,
1532 CS4231_AUX1_RIGHT_INPUT, 0, 0, 31, 1),
1533CS4231_DOUBLE("Aux Playback Switch", 1, CS4231_AUX2_LEFT_INPUT,
1534 CS4231_AUX2_RIGHT_INPUT, 7, 7, 1, 1),
1535CS4231_DOUBLE("Aux Playback Volume", 1, CS4231_AUX2_LEFT_INPUT,
1536 CS4231_AUX2_RIGHT_INPUT, 0, 0, 31, 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537CS4231_SINGLE("Mono Playback Switch", 0, CS4231_MONO_CTRL, 7, 1, 1),
1538CS4231_SINGLE("Mono Playback Volume", 0, CS4231_MONO_CTRL, 0, 15, 1),
1539CS4231_SINGLE("Mono Output Playback Switch", 0, CS4231_MONO_CTRL, 6, 1, 1),
1540CS4231_SINGLE("Mono Output Playback Bypass", 0, CS4231_MONO_CTRL, 5, 1, 0),
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001541CS4231_DOUBLE("Capture Volume", 0, CS4231_LEFT_INPUT, CS4231_RIGHT_INPUT, 0, 0,
1542 15, 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543{
1544 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1545 .name = "Capture Source",
1546 .info = snd_cs4231_info_mux,
1547 .get = snd_cs4231_get_mux,
1548 .put = snd_cs4231_put_mux,
1549},
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001550CS4231_DOUBLE("Mic Boost", 0, CS4231_LEFT_INPUT, CS4231_RIGHT_INPUT, 5, 5,
1551 1, 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552CS4231_SINGLE("Loopback Capture Switch", 0, CS4231_LOOPBACK, 0, 1, 0),
1553CS4231_SINGLE("Loopback Capture Volume", 0, CS4231_LOOPBACK, 2, 63, 1),
1554/* SPARC specific uses of XCTL{0,1} general purpose outputs. */
1555CS4231_SINGLE("Line Out Switch", 0, CS4231_PIN_CTRL, 6, 1, 1),
1556CS4231_SINGLE("Headphone Out Switch", 0, CS4231_PIN_CTRL, 7, 1, 1)
1557};
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001558
Krzysztof Heltc6c2d572007-09-04 13:08:24 +02001559static int __init snd_cs4231_mixer(struct snd_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560{
Krzysztof Heltc6c2d572007-09-04 13:08:24 +02001561 struct snd_cs4231 *chip = card->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 int err, idx;
1563
1564 snd_assert(chip != NULL && chip->pcm != NULL, return -EINVAL);
1565
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 strcpy(card->mixername, chip->pcm->name);
1567
1568 for (idx = 0; idx < ARRAY_SIZE(snd_cs4231_controls); idx++) {
Krzysztof Heltc6c2d572007-09-04 13:08:24 +02001569 err = snd_ctl_add(card,
1570 snd_ctl_new1(&snd_cs4231_controls[idx], chip));
1571 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 return err;
1573 }
1574 return 0;
1575}
1576
1577static int dev;
1578
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001579static int __init cs4231_attach_begin(struct snd_card **rcard)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580{
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001581 struct snd_card *card;
Krzysztof Heltc6c2d572007-09-04 13:08:24 +02001582 struct snd_cs4231 *chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583
1584 *rcard = NULL;
1585
1586 if (dev >= SNDRV_CARDS)
1587 return -ENODEV;
1588
1589 if (!enable[dev]) {
1590 dev++;
1591 return -ENOENT;
1592 }
1593
Krzysztof Heltc6c2d572007-09-04 13:08:24 +02001594 card = snd_card_new(index[dev], id[dev], THIS_MODULE,
1595 sizeof(struct snd_cs4231));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 if (card == NULL)
1597 return -ENOMEM;
1598
1599 strcpy(card->driver, "CS4231");
1600 strcpy(card->shortname, "Sun CS4231");
1601
Krzysztof Heltc6c2d572007-09-04 13:08:24 +02001602 chip = card->private_data;
1603 chip->card = card;
1604
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 *rcard = card;
1606 return 0;
1607}
1608
Krzysztof Heltc6c2d572007-09-04 13:08:24 +02001609static int __init cs4231_attach_finish(struct snd_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610{
Krzysztof Heltc6c2d572007-09-04 13:08:24 +02001611 struct snd_cs4231 *chip = card->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 int err;
1613
Krzysztof Heltc6c2d572007-09-04 13:08:24 +02001614 err = snd_cs4231_pcm(card);
1615 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 goto out_err;
1617
Krzysztof Heltc6c2d572007-09-04 13:08:24 +02001618 err = snd_cs4231_mixer(card);
1619 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 goto out_err;
1621
Krzysztof Heltc6c2d572007-09-04 13:08:24 +02001622 err = snd_cs4231_timer(card);
1623 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 goto out_err;
1625
Krzysztof Heltc6c2d572007-09-04 13:08:24 +02001626 err = snd_card_register(card);
1627 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 goto out_err;
1629
1630 chip->next = cs4231_list;
1631 cs4231_list = chip;
1632
1633 dev++;
1634 return 0;
1635
1636out_err:
1637 snd_card_free(card);
1638 return err;
1639}
1640
1641#ifdef SBUS_SUPPORT
Georg Chinib1282542005-11-07 14:09:19 -08001642
David Howells7d12e782006-10-05 14:55:46 +01001643static irqreturn_t snd_cs4231_sbus_interrupt(int irq, void *dev_id)
Georg Chinib1282542005-11-07 14:09:19 -08001644{
1645 unsigned long flags;
1646 unsigned char status;
1647 u32 csr;
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001648 struct snd_cs4231 *chip = dev_id;
Georg Chinib1282542005-11-07 14:09:19 -08001649
1650 /*This is IRQ is not raised by the cs4231*/
Krzysztof Helt7e52f3d2007-09-05 15:08:23 +02001651 if (!(__cs4231_readb(chip, CS4231U(chip, STATUS)) & CS4231_GLOBALIRQ))
Georg Chinib1282542005-11-07 14:09:19 -08001652 return IRQ_NONE;
1653
1654 /* ACK the APC interrupt. */
1655 csr = sbus_readl(chip->port + APCCSR);
1656
1657 sbus_writel(csr, chip->port + APCCSR);
1658
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001659 if ((csr & APC_PDMA_READY) &&
1660 (csr & APC_PLAY_INT) &&
Georg Chinib1282542005-11-07 14:09:19 -08001661 (csr & APC_XINT_PNVA) &&
1662 !(csr & APC_XINT_EMPT))
1663 snd_cs4231_play_callback(chip);
1664
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001665 if ((csr & APC_CDMA_READY) &&
1666 (csr & APC_CAPT_INT) &&
Georg Chinib1282542005-11-07 14:09:19 -08001667 (csr & APC_XINT_CNVA) &&
1668 !(csr & APC_XINT_EMPT))
1669 snd_cs4231_capture_callback(chip);
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001670
Georg Chinib1282542005-11-07 14:09:19 -08001671 status = snd_cs4231_in(chip, CS4231_IRQ_STATUS);
1672
1673 if (status & CS4231_TIMER_IRQ) {
1674 if (chip->timer)
1675 snd_timer_interrupt(chip->timer, chip->timer->sticks);
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001676 }
Georg Chinib1282542005-11-07 14:09:19 -08001677
1678 if ((status & CS4231_RECORD_IRQ) && (csr & APC_CDMA_READY))
1679 snd_cs4231_overrange(chip);
1680
1681 /* ACK the CS4231 interrupt. */
1682 spin_lock_irqsave(&chip->lock, flags);
1683 snd_cs4231_outm(chip, CS4231_IRQ_STATUS, ~CS4231_ALL_IRQS | ~status, 0);
1684 spin_unlock_irqrestore(&chip->lock, flags);
1685
Georg Chinid35a1b92007-01-02 21:28:17 -08001686 return IRQ_HANDLED;
Georg Chinib1282542005-11-07 14:09:19 -08001687}
1688
1689/*
1690 * SBUS DMA routines
1691 */
1692
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001693static int sbus_dma_request(struct cs4231_dma_control *dma_cont,
1694 dma_addr_t bus_addr, size_t len)
Georg Chinib1282542005-11-07 14:09:19 -08001695{
1696 unsigned long flags;
1697 u32 test, csr;
1698 int err;
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001699 struct sbus_dma_info *base = &dma_cont->sbus_info;
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001700
Georg Chinib1282542005-11-07 14:09:19 -08001701 if (len >= (1 << 24))
1702 return -EINVAL;
1703 spin_lock_irqsave(&base->lock, flags);
1704 csr = sbus_readl(base->regs + APCCSR);
1705 err = -EINVAL;
1706 test = APC_CDMA_READY;
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001707 if (base->dir == APC_PLAY)
Georg Chinib1282542005-11-07 14:09:19 -08001708 test = APC_PDMA_READY;
1709 if (!(csr & test))
1710 goto out;
1711 err = -EBUSY;
Georg Chinib1282542005-11-07 14:09:19 -08001712 test = APC_XINT_CNVA;
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001713 if (base->dir == APC_PLAY)
Georg Chinib1282542005-11-07 14:09:19 -08001714 test = APC_XINT_PNVA;
1715 if (!(csr & test))
1716 goto out;
1717 err = 0;
1718 sbus_writel(bus_addr, base->regs + base->dir + APCNVA);
1719 sbus_writel(len, base->regs + base->dir + APCNC);
1720out:
1721 spin_unlock_irqrestore(&base->lock, flags);
1722 return err;
1723}
1724
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001725static void sbus_dma_prepare(struct cs4231_dma_control *dma_cont, int d)
Georg Chinib1282542005-11-07 14:09:19 -08001726{
1727 unsigned long flags;
1728 u32 csr, test;
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001729 struct sbus_dma_info *base = &dma_cont->sbus_info;
Georg Chinib1282542005-11-07 14:09:19 -08001730
1731 spin_lock_irqsave(&base->lock, flags);
1732 csr = sbus_readl(base->regs + APCCSR);
1733 test = APC_GENL_INT | APC_PLAY_INT | APC_XINT_ENA |
1734 APC_XINT_PLAY | APC_XINT_PEMP | APC_XINT_GENL |
1735 APC_XINT_PENA;
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001736 if (base->dir == APC_RECORD)
Georg Chinib1282542005-11-07 14:09:19 -08001737 test = APC_GENL_INT | APC_CAPT_INT | APC_XINT_ENA |
1738 APC_XINT_CAPT | APC_XINT_CEMP | APC_XINT_GENL;
1739 csr |= test;
1740 sbus_writel(csr, base->regs + APCCSR);
1741 spin_unlock_irqrestore(&base->lock, flags);
1742}
1743
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001744static void sbus_dma_enable(struct cs4231_dma_control *dma_cont, int on)
Georg Chinib1282542005-11-07 14:09:19 -08001745{
1746 unsigned long flags;
1747 u32 csr, shift;
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001748 struct sbus_dma_info *base = &dma_cont->sbus_info;
Georg Chinib1282542005-11-07 14:09:19 -08001749
1750 spin_lock_irqsave(&base->lock, flags);
1751 if (!on) {
Georg Chinid35a1b92007-01-02 21:28:17 -08001752 sbus_writel(0, base->regs + base->dir + APCNC);
1753 sbus_writel(0, base->regs + base->dir + APCNVA);
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001754 if (base->dir == APC_PLAY) {
Georg Chini3daadf32007-08-01 21:55:58 -07001755 sbus_writel(0, base->regs + base->dir + APCC);
1756 sbus_writel(0, base->regs + base->dir + APCVA);
1757 }
Georg Chinid35a1b92007-01-02 21:28:17 -08001758
Georg Chini3daadf32007-08-01 21:55:58 -07001759 udelay(1200);
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001760 }
Georg Chinib1282542005-11-07 14:09:19 -08001761 csr = sbus_readl(base->regs + APCCSR);
1762 shift = 0;
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001763 if (base->dir == APC_PLAY)
Georg Chinib1282542005-11-07 14:09:19 -08001764 shift = 1;
1765 if (on)
1766 csr &= ~(APC_CPAUSE << shift);
1767 else
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001768 csr |= (APC_CPAUSE << shift);
Georg Chinib1282542005-11-07 14:09:19 -08001769 sbus_writel(csr, base->regs + APCCSR);
1770 if (on)
1771 csr |= (APC_CDMA_READY << shift);
1772 else
1773 csr &= ~(APC_CDMA_READY << shift);
1774 sbus_writel(csr, base->regs + APCCSR);
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001775
Georg Chinib1282542005-11-07 14:09:19 -08001776 spin_unlock_irqrestore(&base->lock, flags);
1777}
1778
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001779static unsigned int sbus_dma_addr(struct cs4231_dma_control *dma_cont)
Georg Chinib1282542005-11-07 14:09:19 -08001780{
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001781 struct sbus_dma_info *base = &dma_cont->sbus_info;
Georg Chinib1282542005-11-07 14:09:19 -08001782
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001783 return sbus_readl(base->regs + base->dir + APCVA);
Georg Chinib1282542005-11-07 14:09:19 -08001784}
1785
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001786static void sbus_dma_preallocate(struct snd_cs4231 *chip, struct snd_pcm *pcm)
Georg Chinib1282542005-11-07 14:09:19 -08001787{
David S. Miller12b1c032008-08-27 00:31:36 -07001788 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
David S. Millerae251032008-08-27 18:24:01 -07001789 &chip->dev_u.op->dev,
David S. Miller12b1c032008-08-27 00:31:36 -07001790 64 * 1024, 128 * 1024);
Georg Chinib1282542005-11-07 14:09:19 -08001791}
1792
1793/*
1794 * Init and exit routines
1795 */
1796
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001797static int snd_cs4231_sbus_free(struct snd_cs4231 *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798{
David S. Millerae251032008-08-27 18:24:01 -07001799 struct of_device *op = chip->dev_u.op;
1800
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 if (chip->irq[0])
1802 free_irq(chip->irq[0], chip);
1803
1804 if (chip->port)
David S. Millerae251032008-08-27 18:24:01 -07001805 of_iounmap(&op->resource[0], chip->port, chip->regs_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 return 0;
1808}
1809
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001810static int snd_cs4231_sbus_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811{
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001812 struct snd_cs4231 *cp = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813
1814 return snd_cs4231_sbus_free(cp);
1815}
1816
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001817static struct snd_device_ops snd_cs4231_sbus_dev_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 .dev_free = snd_cs4231_sbus_dev_free,
1819};
1820
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001821static int __init snd_cs4231_sbus_create(struct snd_card *card,
David S. Millerae251032008-08-27 18:24:01 -07001822 struct of_device *op,
Krzysztof Heltc6c2d572007-09-04 13:08:24 +02001823 int dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824{
Krzysztof Heltc6c2d572007-09-04 13:08:24 +02001825 struct snd_cs4231 *chip = card->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 int err;
1827
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 spin_lock_init(&chip->lock);
Georg Chinib1282542005-11-07 14:09:19 -08001829 spin_lock_init(&chip->c_dma.sbus_info.lock);
1830 spin_lock_init(&chip->p_dma.sbus_info.lock);
Ingo Molnar12aa7572006-01-16 16:36:05 +01001831 mutex_init(&chip->mce_mutex);
1832 mutex_init(&chip->open_mutex);
David S. Millerae251032008-08-27 18:24:01 -07001833 chip->dev_u.op = op;
1834 chip->regs_size = resource_size(&op->resource[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 memcpy(&chip->image, &snd_cs4231_original_image,
1836 sizeof(snd_cs4231_original_image));
1837
David S. Millerae251032008-08-27 18:24:01 -07001838 chip->port = of_ioremap(&op->resource[0], 0,
1839 chip->regs_size, "cs4231");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 if (!chip->port) {
Christopher Zimmermanna1314302005-09-21 00:41:22 -07001841 snd_printdd("cs4231-%d: Unable to map chip registers.\n", dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 return -EIO;
1843 }
1844
Georg Chinib1282542005-11-07 14:09:19 -08001845 chip->c_dma.sbus_info.regs = chip->port;
1846 chip->p_dma.sbus_info.regs = chip->port;
1847 chip->c_dma.sbus_info.dir = APC_RECORD;
1848 chip->p_dma.sbus_info.dir = APC_PLAY;
1849
1850 chip->p_dma.prepare = sbus_dma_prepare;
1851 chip->p_dma.enable = sbus_dma_enable;
1852 chip->p_dma.request = sbus_dma_request;
1853 chip->p_dma.address = sbus_dma_addr;
Georg Chinib1282542005-11-07 14:09:19 -08001854 chip->p_dma.preallocate = sbus_dma_preallocate;
1855
1856 chip->c_dma.prepare = sbus_dma_prepare;
1857 chip->c_dma.enable = sbus_dma_enable;
1858 chip->c_dma.request = sbus_dma_request;
1859 chip->c_dma.address = sbus_dma_addr;
Georg Chinib1282542005-11-07 14:09:19 -08001860 chip->c_dma.preallocate = sbus_dma_preallocate;
Georg Chini5a820fa2005-11-07 14:08:25 -08001861
David S. Millerae251032008-08-27 18:24:01 -07001862 if (request_irq(op->irqs[0], snd_cs4231_sbus_interrupt,
Thomas Gleixner65ca68b2006-07-01 19:29:46 -07001863 IRQF_SHARED, "cs4231", chip)) {
David S. Millerc6387a42006-06-20 01:21:29 -07001864 snd_printdd("cs4231-%d: Unable to grab SBUS IRQ %d\n",
David S. Millerae251032008-08-27 18:24:01 -07001865 dev, op->irqs[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 snd_cs4231_sbus_free(chip);
1867 return -EBUSY;
1868 }
David S. Millerae251032008-08-27 18:24:01 -07001869 chip->irq[0] = op->irqs[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870
1871 if (snd_cs4231_probe(chip) < 0) {
1872 snd_cs4231_sbus_free(chip);
1873 return -ENODEV;
1874 }
1875 snd_cs4231_init(chip);
1876
1877 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL,
1878 chip, &snd_cs4231_sbus_dev_ops)) < 0) {
1879 snd_cs4231_sbus_free(chip);
1880 return err;
1881 }
1882
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 return 0;
1884}
1885
David S. Millerae251032008-08-27 18:24:01 -07001886static int __devinit cs4231_probe(struct of_device *op, const struct of_device_id *match)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887{
David S. Millerae251032008-08-27 18:24:01 -07001888 struct resource *rp = &op->resource[0];
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001889 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 int err;
1891
David S. Millerae251032008-08-27 18:24:01 -07001892 if (strcmp(op->node->parent->name, "sbus") &&
1893 strcmp(op->node->parent->name, "sbi"))
1894 return -ENODEV;
1895
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 err = cs4231_attach_begin(&card);
1897 if (err)
1898 return err;
1899
Andrew Morton5863aa62006-07-03 00:24:22 -07001900 sprintf(card->longname, "%s at 0x%02lx:0x%016Lx, irq %d",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 card->shortname,
1902 rp->flags & 0xffL,
Greg Kroah-Hartmanaa0a2dd2006-06-12 14:50:27 -07001903 (unsigned long long)rp->start,
David S. Millerae251032008-08-27 18:24:01 -07001904 op->irqs[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905
David S. Millerae251032008-08-27 18:24:01 -07001906 err = snd_cs4231_sbus_create(card, op, dev);
Krzysztof Heltc6c2d572007-09-04 13:08:24 +02001907 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 snd_card_free(card);
1909 return err;
1910 }
1911
Krzysztof Heltc6c2d572007-09-04 13:08:24 +02001912 return cs4231_attach_finish(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913}
1914#endif
1915
1916#ifdef EBUS_SUPPORT
Georg Chinib1282542005-11-07 14:09:19 -08001917
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001918static void snd_cs4231_ebus_play_callback(struct ebus_dma_info *p, int event,
1919 void *cookie)
Georg Chinib1282542005-11-07 14:09:19 -08001920{
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001921 struct snd_cs4231 *chip = cookie;
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001922
Georg Chinib1282542005-11-07 14:09:19 -08001923 snd_cs4231_play_callback(chip);
1924}
1925
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001926static void snd_cs4231_ebus_capture_callback(struct ebus_dma_info *p,
1927 int event, void *cookie)
Georg Chinib1282542005-11-07 14:09:19 -08001928{
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001929 struct snd_cs4231 *chip = cookie;
Georg Chinib1282542005-11-07 14:09:19 -08001930
1931 snd_cs4231_capture_callback(chip);
1932}
1933
1934/*
1935 * EBUS DMA wrappers
1936 */
1937
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001938static int _ebus_dma_request(struct cs4231_dma_control *dma_cont,
1939 dma_addr_t bus_addr, size_t len)
Georg Chinib1282542005-11-07 14:09:19 -08001940{
1941 return ebus_dma_request(&dma_cont->ebus_info, bus_addr, len);
1942}
1943
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001944static void _ebus_dma_enable(struct cs4231_dma_control *dma_cont, int on)
Georg Chinib1282542005-11-07 14:09:19 -08001945{
1946 ebus_dma_enable(&dma_cont->ebus_info, on);
1947}
1948
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001949static void _ebus_dma_prepare(struct cs4231_dma_control *dma_cont, int dir)
Georg Chinib1282542005-11-07 14:09:19 -08001950{
1951 ebus_dma_prepare(&dma_cont->ebus_info, dir);
1952}
1953
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001954static unsigned int _ebus_dma_addr(struct cs4231_dma_control *dma_cont)
Georg Chinib1282542005-11-07 14:09:19 -08001955{
1956 return ebus_dma_addr(&dma_cont->ebus_info);
1957}
1958
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001959static void _ebus_dma_preallocate(struct snd_cs4231 *chip, struct snd_pcm *pcm)
Georg Chinib1282542005-11-07 14:09:19 -08001960{
1961 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1962 snd_dma_pci_data(chip->dev_u.pdev),
1963 64*1024, 128*1024);
1964}
1965
1966/*
1967 * Init and exit routines
1968 */
1969
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001970static int snd_cs4231_ebus_free(struct snd_cs4231 *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971{
Georg Chinib1282542005-11-07 14:09:19 -08001972 if (chip->c_dma.ebus_info.regs) {
1973 ebus_dma_unregister(&chip->c_dma.ebus_info);
1974 iounmap(chip->c_dma.ebus_info.regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 }
Georg Chinib1282542005-11-07 14:09:19 -08001976 if (chip->p_dma.ebus_info.regs) {
1977 ebus_dma_unregister(&chip->p_dma.ebus_info);
1978 iounmap(chip->p_dma.ebus_info.regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 }
1980
1981 if (chip->port)
1982 iounmap(chip->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 return 0;
1985}
1986
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001987static int snd_cs4231_ebus_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988{
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001989 struct snd_cs4231 *cp = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990
1991 return snd_cs4231_ebus_free(cp);
1992}
1993
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001994static struct snd_device_ops snd_cs4231_ebus_dev_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 .dev_free = snd_cs4231_ebus_dev_free,
1996};
1997
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001998static int __init snd_cs4231_ebus_create(struct snd_card *card,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 struct linux_ebus_device *edev,
Krzysztof Heltc6c2d572007-09-04 13:08:24 +02002000 int dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001{
Krzysztof Heltc6c2d572007-09-04 13:08:24 +02002002 struct snd_cs4231 *chip = card->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 int err;
2004
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 spin_lock_init(&chip->lock);
Georg Chinib1282542005-11-07 14:09:19 -08002006 spin_lock_init(&chip->c_dma.ebus_info.lock);
2007 spin_lock_init(&chip->p_dma.ebus_info.lock);
Ingo Molnar12aa7572006-01-16 16:36:05 +01002008 mutex_init(&chip->mce_mutex);
2009 mutex_init(&chip->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 chip->flags |= CS4231_FLAG_EBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 chip->dev_u.pdev = edev->bus->self;
2012 memcpy(&chip->image, &snd_cs4231_original_image,
2013 sizeof(snd_cs4231_original_image));
Georg Chinib1282542005-11-07 14:09:19 -08002014 strcpy(chip->c_dma.ebus_info.name, "cs4231(capture)");
2015 chip->c_dma.ebus_info.flags = EBUS_DMA_FLAG_USE_EBDMA_HANDLER;
2016 chip->c_dma.ebus_info.callback = snd_cs4231_ebus_capture_callback;
2017 chip->c_dma.ebus_info.client_cookie = chip;
2018 chip->c_dma.ebus_info.irq = edev->irqs[0];
2019 strcpy(chip->p_dma.ebus_info.name, "cs4231(play)");
2020 chip->p_dma.ebus_info.flags = EBUS_DMA_FLAG_USE_EBDMA_HANDLER;
2021 chip->p_dma.ebus_info.callback = snd_cs4231_ebus_play_callback;
2022 chip->p_dma.ebus_info.client_cookie = chip;
2023 chip->p_dma.ebus_info.irq = edev->irqs[1];
2024
2025 chip->p_dma.prepare = _ebus_dma_prepare;
2026 chip->p_dma.enable = _ebus_dma_enable;
2027 chip->p_dma.request = _ebus_dma_request;
2028 chip->p_dma.address = _ebus_dma_addr;
Georg Chinib1282542005-11-07 14:09:19 -08002029 chip->p_dma.preallocate = _ebus_dma_preallocate;
2030
2031 chip->c_dma.prepare = _ebus_dma_prepare;
2032 chip->c_dma.enable = _ebus_dma_enable;
2033 chip->c_dma.request = _ebus_dma_request;
2034 chip->c_dma.address = _ebus_dma_addr;
Georg Chinib1282542005-11-07 14:09:19 -08002035 chip->c_dma.preallocate = _ebus_dma_preallocate;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036
2037 chip->port = ioremap(edev->resource[0].start, 0x10);
Georg Chinib1282542005-11-07 14:09:19 -08002038 chip->p_dma.ebus_info.regs = ioremap(edev->resource[1].start, 0x10);
2039 chip->c_dma.ebus_info.regs = ioremap(edev->resource[2].start, 0x10);
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02002040 if (!chip->port || !chip->p_dma.ebus_info.regs ||
2041 !chip->c_dma.ebus_info.regs) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 snd_cs4231_ebus_free(chip);
Christopher Zimmermanna1314302005-09-21 00:41:22 -07002043 snd_printdd("cs4231-%d: Unable to map chip registers.\n", dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 return -EIO;
2045 }
2046
Georg Chinib1282542005-11-07 14:09:19 -08002047 if (ebus_dma_register(&chip->c_dma.ebus_info)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 snd_cs4231_ebus_free(chip);
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02002049 snd_printdd("cs4231-%d: Unable to register EBUS capture DMA\n",
2050 dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 return -EBUSY;
2052 }
Georg Chinib1282542005-11-07 14:09:19 -08002053 if (ebus_dma_irq_enable(&chip->c_dma.ebus_info, 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 snd_cs4231_ebus_free(chip);
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02002055 snd_printdd("cs4231-%d: Unable to enable EBUS capture IRQ\n",
2056 dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 return -EBUSY;
2058 }
2059
Georg Chinib1282542005-11-07 14:09:19 -08002060 if (ebus_dma_register(&chip->p_dma.ebus_info)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 snd_cs4231_ebus_free(chip);
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02002062 snd_printdd("cs4231-%d: Unable to register EBUS play DMA\n",
2063 dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 return -EBUSY;
2065 }
Georg Chinib1282542005-11-07 14:09:19 -08002066 if (ebus_dma_irq_enable(&chip->p_dma.ebus_info, 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 snd_cs4231_ebus_free(chip);
Christopher Zimmermanna1314302005-09-21 00:41:22 -07002068 snd_printdd("cs4231-%d: Unable to enable EBUS play IRQ\n", dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 return -EBUSY;
2070 }
2071
2072 if (snd_cs4231_probe(chip) < 0) {
2073 snd_cs4231_ebus_free(chip);
2074 return -ENODEV;
2075 }
2076 snd_cs4231_init(chip);
2077
2078 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL,
2079 chip, &snd_cs4231_ebus_dev_ops)) < 0) {
2080 snd_cs4231_ebus_free(chip);
2081 return err;
2082 }
2083
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 return 0;
2085}
2086
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01002087static int __init cs4231_ebus_attach(struct linux_ebus_device *edev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088{
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01002089 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 int err;
2091
2092 err = cs4231_attach_begin(&card);
2093 if (err)
2094 return err;
2095
David S. Millerc6387a42006-06-20 01:21:29 -07002096 sprintf(card->longname, "%s at 0x%lx, irq %d",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 card->shortname,
2098 edev->resource[0].start,
David S. Millerc6387a42006-06-20 01:21:29 -07002099 edev->irqs[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100
Krzysztof Heltc6c2d572007-09-04 13:08:24 +02002101 err = snd_cs4231_ebus_create(card, edev, dev);
2102 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 snd_card_free(card);
2104 return err;
2105 }
2106
Krzysztof Heltc6c2d572007-09-04 13:08:24 +02002107 return cs4231_attach_finish(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108}
2109#endif
2110
David S. Millerae251032008-08-27 18:24:01 -07002111#ifdef SBUS_SUPPORT
2112static struct of_device_id cs4231_match[] = {
2113 {
2114 .name = "SUNW,CS4231",
2115 },
2116 {},
2117};
2118
2119MODULE_DEVICE_TABLE(of, cs4231_match);
2120
2121static struct of_platform_driver cs4231_driver = {
2122 .name = "audio",
2123 .match_table = cs4231_match,
2124 .probe = cs4231_probe,
2125};
2126#endif
2127
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128static int __init cs4231_init(void)
2129{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130#ifdef EBUS_SUPPORT
2131 struct linux_ebus *ebus;
2132 struct linux_ebus_device *edev;
2133#endif
2134 int found;
2135
2136 found = 0;
2137
2138#ifdef SBUS_SUPPORT
David S. Millerae251032008-08-27 18:24:01 -07002139 {
2140 int err = of_register_driver(&cs4231_driver, &of_bus_type);
2141 if (err)
2142 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 }
2144#endif
2145#ifdef EBUS_SUPPORT
2146 for_each_ebus(ebus) {
2147 for_each_ebusdev(edev, ebus) {
2148 int match = 0;
2149
David S. Miller690c8fd2006-06-22 19:12:03 -07002150 if (!strcmp(edev->prom_node->name, "SUNW,CS4231")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 match = 1;
David S. Miller690c8fd2006-06-22 19:12:03 -07002152 } else if (!strcmp(edev->prom_node->name, "audio")) {
Stephen Rothwell31985142007-03-29 00:50:57 -07002153 const char *compat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154
David S. Miller690c8fd2006-06-22 19:12:03 -07002155 compat = of_get_property(edev->prom_node,
2156 "compatible", NULL);
2157 if (compat && !strcmp(compat, "SUNW,CS4231"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 match = 1;
2159 }
2160
2161 if (match &&
2162 cs4231_ebus_attach(edev) == 0)
2163 found++;
2164 }
2165 }
2166#endif
2167
2168
David S. Millerae251032008-08-27 18:24:01 -07002169 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170}
2171
2172static void __exit cs4231_exit(void)
2173{
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01002174 struct snd_cs4231 *p = cs4231_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175
David S. Millerae251032008-08-27 18:24:01 -07002176#ifdef SBUS_SUPPORT
2177 of_unregister_driver(&cs4231_driver);
2178#endif
2179
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 while (p != NULL) {
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01002181 struct snd_cs4231 *next = p->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182
2183 snd_card_free(p->card);
2184
2185 p = next;
2186 }
2187
2188 cs4231_list = NULL;
2189}
2190
2191module_init(cs4231_init);
2192module_exit(cs4231_exit);