blob: 1c4797be72ee774a54b6e3dd7ec8b0e476ee2381 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Driver for CS4231 sound chips found on Sparcs.
3 * Copyright (C) 2002 David S. Miller <davem@redhat.com>
4 *
5 * Based entirely upon drivers/sbus/audio/cs4231.c which is:
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>
20
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <sound/core.h>
23#include <sound/pcm.h>
24#include <sound/info.h>
25#include <sound/control.h>
26#include <sound/timer.h>
27#include <sound/initval.h>
28#include <sound/pcm_params.h>
29
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#ifdef CONFIG_SBUS
31#define SBUS_SUPPORT
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <asm/sbus.h>
33#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>
39#endif
40
41static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
42static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
Krzysztof Helt9e9abb42007-09-10 23:06:55 +020043/* Enable this card */
44static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
46module_param_array(index, int, NULL, 0444);
47MODULE_PARM_DESC(index, "Index value for Sun CS4231 soundcard.");
48module_param_array(id, charp, NULL, 0444);
49MODULE_PARM_DESC(id, "ID string for Sun CS4231 soundcard.");
50module_param_array(enable, bool, NULL, 0444);
51MODULE_PARM_DESC(enable, "Enable Sun CS4231 soundcard.");
52MODULE_AUTHOR("Jaroslav Kysela, Derrick J. Brashear and David S. Miller");
53MODULE_DESCRIPTION("Sun CS4231");
54MODULE_LICENSE("GPL");
55MODULE_SUPPORTED_DEVICE("{{Sun,CS4231}}");
56
Georg Chini5a820fa2005-11-07 14:08:25 -080057#ifdef SBUS_SUPPORT
Takashi Iwaibe9b7e82006-01-03 13:42:38 +010058struct sbus_dma_info {
Krzysztof Helt9e9abb42007-09-10 23:06:55 +020059 spinlock_t lock; /* DMA access lock */
60 int dir;
61 void __iomem *regs;
Takashi Iwaibe9b7e82006-01-03 13:42:38 +010062};
Georg Chini5a820fa2005-11-07 14:08:25 -080063#endif
64
David S. Miller4f3f2f62006-01-17 15:55:58 -080065struct snd_cs4231;
Takashi Iwaibe9b7e82006-01-03 13:42:38 +010066struct cs4231_dma_control {
Krzysztof Helt9e9abb42007-09-10 23:06:55 +020067 void (*prepare)(struct cs4231_dma_control *dma_cont,
68 int dir);
69 void (*enable)(struct cs4231_dma_control *dma_cont, int on);
70 int (*request)(struct cs4231_dma_control *dma_cont,
71 dma_addr_t bus_addr, size_t len);
72 unsigned int (*address)(struct cs4231_dma_control *dma_cont);
73 void (*preallocate)(struct snd_cs4231 *chip,
74 struct snd_pcm *pcm);
Georg Chinib1282542005-11-07 14:09:19 -080075#ifdef EBUS_SUPPORT
76 struct ebus_dma_info ebus_info;
77#endif
78#ifdef SBUS_SUPPORT
79 struct sbus_dma_info sbus_info;
80#endif
Takashi Iwaibe9b7e82006-01-03 13:42:38 +010081};
Georg Chinib1282542005-11-07 14:09:19 -080082
83struct snd_cs4231 {
Krzysztof Helt9e9abb42007-09-10 23:06:55 +020084 spinlock_t lock; /* registers access lock */
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 void __iomem *port;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
Takashi Iwaibe9b7e82006-01-03 13:42:38 +010087 struct cs4231_dma_control p_dma;
88 struct cs4231_dma_control c_dma;
Georg Chini5a820fa2005-11-07 14:08:25 -080089
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 u32 flags;
91#define CS4231_FLAG_EBUS 0x00000001
92#define CS4231_FLAG_PLAYBACK 0x00000002
93#define CS4231_FLAG_CAPTURE 0x00000004
94
Takashi Iwaibe9b7e82006-01-03 13:42:38 +010095 struct snd_card *card;
96 struct snd_pcm *pcm;
97 struct snd_pcm_substream *playback_substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 unsigned int p_periods_sent;
Takashi Iwaibe9b7e82006-01-03 13:42:38 +010099 struct snd_pcm_substream *capture_substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 unsigned int c_periods_sent;
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100101 struct snd_timer *timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
103 unsigned short mode;
104#define CS4231_MODE_NONE 0x0000
105#define CS4231_MODE_PLAY 0x0001
106#define CS4231_MODE_RECORD 0x0002
107#define CS4231_MODE_TIMER 0x0004
Krzysztof Helt9e9abb42007-09-10 23:06:55 +0200108#define CS4231_MODE_OPEN (CS4231_MODE_PLAY | CS4231_MODE_RECORD | \
109 CS4231_MODE_TIMER)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
111 unsigned char image[32]; /* registers image */
112 int mce_bit;
113 int calibrate_mute;
Krzysztof Helt9e9abb42007-09-10 23:06:55 +0200114 struct mutex mce_mutex; /* mutex for mce register */
115 struct mutex open_mutex; /* mutex for ALSA open/close */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
117 union {
118#ifdef SBUS_SUPPORT
119 struct sbus_dev *sdev;
120#endif
121#ifdef EBUS_SUPPORT
122 struct pci_dev *pdev;
123#endif
124 } dev_u;
125 unsigned int irq[2];
126 unsigned int regs_size;
127 struct snd_cs4231 *next;
Georg Chinib1282542005-11-07 14:09:19 -0800128};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100130static struct snd_cs4231 *cs4231_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
132/* Eventually we can use sound/isa/cs423x/cs4231_lib.c directly, but for
133 * now.... -DaveM
134 */
135
136/* IO ports */
Krzysztof Helt7e52f3d2007-09-05 15:08:23 +0200137#include <sound/cs4231-regs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
139/* XXX offsets are different than PC ISA chips... */
Krzysztof Helt7e52f3d2007-09-05 15:08:23 +0200140#define CS4231U(chip, x) ((chip)->port + ((c_d_c_CS4231##x) << 2))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
142/* SBUS DMA register defines. */
143
144#define APCCSR 0x10UL /* APC DMA CSR */
145#define APCCVA 0x20UL /* APC Capture DMA Address */
146#define APCCC 0x24UL /* APC Capture Count */
147#define APCCNVA 0x28UL /* APC Capture DMA Next Address */
148#define APCCNC 0x2cUL /* APC Capture Next Count */
149#define APCPVA 0x30UL /* APC Play DMA Address */
150#define APCPC 0x34UL /* APC Play Count */
151#define APCPNVA 0x38UL /* APC Play DMA Next Address */
152#define APCPNC 0x3cUL /* APC Play Next Count */
153
Georg Chini5a820fa2005-11-07 14:08:25 -0800154/* Defines for SBUS DMA-routines */
155
156#define APCVA 0x0UL /* APC DMA Address */
157#define APCC 0x4UL /* APC Count */
158#define APCNVA 0x8UL /* APC DMA Next Address */
159#define APCNC 0xcUL /* APC Next Count */
160#define APC_PLAY 0x30UL /* Play registers start at 0x30 */
161#define APC_RECORD 0x20UL /* Record registers start at 0x20 */
162
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163/* APCCSR bits */
164
165#define APC_INT_PENDING 0x800000 /* Interrupt Pending */
166#define APC_PLAY_INT 0x400000 /* Playback interrupt */
167#define APC_CAPT_INT 0x200000 /* Capture interrupt */
168#define APC_GENL_INT 0x100000 /* General interrupt */
169#define APC_XINT_ENA 0x80000 /* General ext int. enable */
170#define APC_XINT_PLAY 0x40000 /* Playback ext intr */
171#define APC_XINT_CAPT 0x20000 /* Capture ext intr */
172#define APC_XINT_GENL 0x10000 /* Error ext intr */
173#define APC_XINT_EMPT 0x8000 /* Pipe empty interrupt (0 write to pva) */
174#define APC_XINT_PEMP 0x4000 /* Play pipe empty (pva and pnva not set) */
175#define APC_XINT_PNVA 0x2000 /* Playback NVA dirty */
176#define APC_XINT_PENA 0x1000 /* play pipe empty Int enable */
177#define APC_XINT_COVF 0x800 /* Cap data dropped on floor */
178#define APC_XINT_CNVA 0x400 /* Capture NVA dirty */
179#define APC_XINT_CEMP 0x200 /* Capture pipe empty (cva and cnva not set) */
180#define APC_XINT_CENA 0x100 /* Cap. pipe empty int enable */
181#define APC_PPAUSE 0x80 /* Pause the play DMA */
182#define APC_CPAUSE 0x40 /* Pause the capture DMA */
183#define APC_CDC_RESET 0x20 /* CODEC RESET */
184#define APC_PDMA_READY 0x08 /* Play DMA Go */
185#define APC_CDMA_READY 0x04 /* Capture DMA Go */
186#define APC_CHIP_RESET 0x01 /* Reset the chip */
187
188/* EBUS DMA register offsets */
189
190#define EBDMA_CSR 0x00UL /* Control/Status */
191#define EBDMA_ADDR 0x04UL /* DMA Address */
192#define EBDMA_COUNT 0x08UL /* DMA Count */
193
194/*
195 * Some variables
196 */
197
198static unsigned char freq_bits[14] = {
199 /* 5510 */ 0x00 | CS4231_XTAL2,
200 /* 6620 */ 0x0E | CS4231_XTAL2,
201 /* 8000 */ 0x00 | CS4231_XTAL1,
202 /* 9600 */ 0x0E | CS4231_XTAL1,
203 /* 11025 */ 0x02 | CS4231_XTAL2,
204 /* 16000 */ 0x02 | CS4231_XTAL1,
205 /* 18900 */ 0x04 | CS4231_XTAL2,
206 /* 22050 */ 0x06 | CS4231_XTAL2,
207 /* 27042 */ 0x04 | CS4231_XTAL1,
208 /* 32000 */ 0x06 | CS4231_XTAL1,
209 /* 33075 */ 0x0C | CS4231_XTAL2,
210 /* 37800 */ 0x08 | CS4231_XTAL2,
211 /* 44100 */ 0x0A | CS4231_XTAL2,
212 /* 48000 */ 0x0C | CS4231_XTAL1
213};
214
215static unsigned int rates[14] = {
216 5510, 6620, 8000, 9600, 11025, 16000, 18900, 22050,
217 27042, 32000, 33075, 37800, 44100, 48000
218};
219
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100220static struct snd_pcm_hw_constraint_list hw_constraints_rates = {
Krzysztof Heltc6c2d572007-09-04 13:08:24 +0200221 .count = ARRAY_SIZE(rates),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 .list = rates,
223};
224
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100225static int snd_cs4231_xrate(struct snd_pcm_runtime *runtime)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226{
227 return snd_pcm_hw_constraint_list(runtime, 0,
228 SNDRV_PCM_HW_PARAM_RATE,
229 &hw_constraints_rates);
230}
231
232static unsigned char snd_cs4231_original_image[32] =
233{
234 0x00, /* 00/00 - lic */
235 0x00, /* 01/01 - ric */
236 0x9f, /* 02/02 - la1ic */
237 0x9f, /* 03/03 - ra1ic */
238 0x9f, /* 04/04 - la2ic */
239 0x9f, /* 05/05 - ra2ic */
240 0xbf, /* 06/06 - loc */
241 0xbf, /* 07/07 - roc */
242 0x20, /* 08/08 - pdfr */
243 CS4231_AUTOCALIB, /* 09/09 - ic */
244 0x00, /* 0a/10 - pc */
245 0x00, /* 0b/11 - ti */
246 CS4231_MODE2, /* 0c/12 - mi */
247 0x00, /* 0d/13 - lbc */
248 0x00, /* 0e/14 - pbru */
249 0x00, /* 0f/15 - pbrl */
250 0x80, /* 10/16 - afei */
251 0x01, /* 11/17 - afeii */
252 0x9f, /* 12/18 - llic */
253 0x9f, /* 13/19 - rlic */
254 0x00, /* 14/20 - tlb */
255 0x00, /* 15/21 - thb */
256 0x00, /* 16/22 - la3mic/reserved */
257 0x00, /* 17/23 - ra3mic/reserved */
258 0x00, /* 18/24 - afs */
259 0x00, /* 19/25 - lamoc/version */
260 0x00, /* 1a/26 - mioc */
261 0x00, /* 1b/27 - ramoc/reserved */
262 0x20, /* 1c/28 - cdfr */
263 0x00, /* 1d/29 - res4 */
264 0x00, /* 1e/30 - cbru */
265 0x00, /* 1f/31 - cbrl */
266};
267
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100268static u8 __cs4231_readb(struct snd_cs4231 *cp, void __iomem *reg_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269{
270#ifdef EBUS_SUPPORT
Krzysztof Heltc6c2d572007-09-04 13:08:24 +0200271 if (cp->flags & CS4231_FLAG_EBUS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 return readb(reg_addr);
Krzysztof Heltc6c2d572007-09-04 13:08:24 +0200273 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274#endif
275#ifdef SBUS_SUPPORT
276 return sbus_readb(reg_addr);
277#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278}
279
Krzysztof Helt9e9abb42007-09-10 23:06:55 +0200280static void __cs4231_writeb(struct snd_cs4231 *cp, u8 val,
281 void __iomem *reg_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282{
283#ifdef EBUS_SUPPORT
Krzysztof Heltc6c2d572007-09-04 13:08:24 +0200284 if (cp->flags & CS4231_FLAG_EBUS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 return writeb(val, reg_addr);
Krzysztof Heltc6c2d572007-09-04 13:08:24 +0200286 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287#endif
288#ifdef SBUS_SUPPORT
289 return sbus_writeb(val, reg_addr);
290#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291}
292
293/*
294 * Basic I/O functions
295 */
296
Krzysztof Heltc6c2d572007-09-04 13:08:24 +0200297static void snd_cs4231_ready(struct snd_cs4231 *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298{
299 int timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
Krzysztof Helt7e52f3d2007-09-05 15:08:23 +0200301 for (timeout = 250; timeout > 0; timeout--) {
302 int val = __cs4231_readb(chip, CS4231U(chip, REGSEL));
303 if ((val & CS4231_INIT) == 0)
304 break;
305 udelay(100);
306 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307}
308
Krzysztof Helt9e9abb42007-09-10 23:06:55 +0200309static void snd_cs4231_dout(struct snd_cs4231 *chip, unsigned char reg,
310 unsigned char value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311{
Krzysztof Heltc6c2d572007-09-04 13:08:24 +0200312 snd_cs4231_ready(chip);
Christopher Zimmermanna1314302005-09-21 00:41:22 -0700313#ifdef CONFIG_SND_DEBUG
Krzysztof Helt7e52f3d2007-09-05 15:08:23 +0200314 if (__cs4231_readb(chip, CS4231U(chip, REGSEL)) & CS4231_INIT)
Krzysztof Heltc6c2d572007-09-04 13:08:24 +0200315 snd_printdd("out: auto calibration time out - reg = 0x%x, "
316 "value = 0x%x\n",
317 reg, value);
Christopher Zimmermanna1314302005-09-21 00:41:22 -0700318#endif
Krzysztof Helt7e52f3d2007-09-05 15:08:23 +0200319 __cs4231_writeb(chip, chip->mce_bit | reg, CS4231U(chip, REGSEL));
Krzysztof Heltc6c2d572007-09-04 13:08:24 +0200320 wmb();
Krzysztof Helt7e52f3d2007-09-05 15:08:23 +0200321 __cs4231_writeb(chip, value, CS4231U(chip, REG));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 mb();
323}
324
Krzysztof Heltc6c2d572007-09-04 13:08:24 +0200325static inline void snd_cs4231_outm(struct snd_cs4231 *chip, unsigned char reg,
326 unsigned char mask, unsigned char value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327{
Krzysztof Heltc6c2d572007-09-04 13:08:24 +0200328 unsigned char tmp = (chip->image[reg] & mask) | value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
Krzysztof Heltc6c2d572007-09-04 13:08:24 +0200330 chip->image[reg] = tmp;
331 if (!chip->calibrate_mute)
332 snd_cs4231_dout(chip, reg, tmp);
333}
334
335static void snd_cs4231_out(struct snd_cs4231 *chip, unsigned char reg,
336 unsigned char value)
337{
338 snd_cs4231_dout(chip, reg, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 chip->image[reg] = value;
340 mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341}
342
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100343static unsigned char snd_cs4231_in(struct snd_cs4231 *chip, unsigned char reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344{
Krzysztof Heltc6c2d572007-09-04 13:08:24 +0200345 snd_cs4231_ready(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346#ifdef CONFIG_SND_DEBUG
Krzysztof Helt7e52f3d2007-09-05 15:08:23 +0200347 if (__cs4231_readb(chip, CS4231U(chip, REGSEL)) & CS4231_INIT)
Krzysztof Heltc6c2d572007-09-04 13:08:24 +0200348 snd_printdd("in: auto calibration time out - reg = 0x%x\n",
349 reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350#endif
Krzysztof Helt7e52f3d2007-09-05 15:08:23 +0200351 __cs4231_writeb(chip, chip->mce_bit | reg, CS4231U(chip, REGSEL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 mb();
Krzysztof Helt7e52f3d2007-09-05 15:08:23 +0200353 return __cs4231_readb(chip, CS4231U(chip, REG));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354}
355
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356/*
357 * CS4231 detection / MCE routines
358 */
359
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100360static void snd_cs4231_busy_wait(struct snd_cs4231 *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361{
362 int timeout;
363
Krzysztof Helt9e9abb42007-09-10 23:06:55 +0200364 /* looks like this sequence is proper for CS4231A chip (GUS MAX) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 for (timeout = 5; timeout > 0; timeout--)
Krzysztof Helt7e52f3d2007-09-05 15:08:23 +0200366 __cs4231_readb(chip, CS4231U(chip, REGSEL));
Christopher Zimmermanna1314302005-09-21 00:41:22 -0700367
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 /* end of cleanup sequence */
Krzysztof Helt7e52f3d2007-09-05 15:08:23 +0200369 for (timeout = 500; timeout > 0; timeout--) {
370 int val = __cs4231_readb(chip, CS4231U(chip, REGSEL));
371 if ((val & CS4231_INIT) == 0)
372 break;
Krzysztof Heltc6c2d572007-09-04 13:08:24 +0200373 msleep(1);
Krzysztof Helt7e52f3d2007-09-05 15:08:23 +0200374 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375}
376
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100377static void snd_cs4231_mce_up(struct snd_cs4231 *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378{
379 unsigned long flags;
380 int timeout;
381
382 spin_lock_irqsave(&chip->lock, flags);
Krzysztof Heltc6c2d572007-09-04 13:08:24 +0200383 snd_cs4231_ready(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384#ifdef CONFIG_SND_DEBUG
Krzysztof Helt7e52f3d2007-09-05 15:08:23 +0200385 if (__cs4231_readb(chip, CS4231U(chip, REGSEL)) & CS4231_INIT)
Christopher Zimmermanna1314302005-09-21 00:41:22 -0700386 snd_printdd("mce_up - auto calibration time out (0)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387#endif
388 chip->mce_bit |= CS4231_MCE;
Krzysztof Helt7e52f3d2007-09-05 15:08:23 +0200389 timeout = __cs4231_readb(chip, CS4231U(chip, REGSEL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 if (timeout == 0x80)
Krzysztof Helt9e9abb42007-09-10 23:06:55 +0200391 snd_printdd("mce_up [%p]: serious init problem - "
392 "codec still busy\n",
393 chip->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 if (!(timeout & CS4231_MCE))
Krzysztof Helt7e52f3d2007-09-05 15:08:23 +0200395 __cs4231_writeb(chip, chip->mce_bit | (timeout & 0x1f),
396 CS4231U(chip, REGSEL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 spin_unlock_irqrestore(&chip->lock, flags);
398}
399
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100400static void snd_cs4231_mce_down(struct snd_cs4231 *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401{
Krzysztof Helt9823adf2007-10-16 14:54:58 +0200402 unsigned long flags, timeout;
403 int reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 snd_cs4231_busy_wait(chip);
Krzysztof Helt9823adf2007-10-16 14:54:58 +0200406 spin_lock_irqsave(&chip->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407#ifdef CONFIG_SND_DEBUG
Krzysztof Helt7e52f3d2007-09-05 15:08:23 +0200408 if (__cs4231_readb(chip, CS4231U(chip, REGSEL)) & CS4231_INIT)
409 snd_printdd("mce_down [%p] - auto calibration time out (0)\n",
410 CS4231U(chip, REGSEL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411#endif
412 chip->mce_bit &= ~CS4231_MCE;
Krzysztof Helt9823adf2007-10-16 14:54:58 +0200413 reg = __cs4231_readb(chip, CS4231U(chip, REGSEL));
414 __cs4231_writeb(chip, chip->mce_bit | (reg & 0x1f),
Krzysztof Helt7e52f3d2007-09-05 15:08:23 +0200415 CS4231U(chip, REGSEL));
Krzysztof Helt9823adf2007-10-16 14:54:58 +0200416 if (reg == 0x80)
417 snd_printdd("mce_down [%p]: serious init problem "
418 "- codec still busy\n", chip->port);
419 if ((reg & CS4231_MCE) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 spin_unlock_irqrestore(&chip->lock, flags);
421 return;
422 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
Krzysztof Helt56f91582007-09-11 00:55:46 +0200424 /*
Krzysztof Helt9823adf2007-10-16 14:54:58 +0200425 * Wait for auto-calibration (AC) process to finish, i.e. ACI to go low.
Krzysztof Helt56f91582007-09-11 00:55:46 +0200426 */
Krzysztof Helt9823adf2007-10-16 14:54:58 +0200427 timeout = jiffies + msecs_to_jiffies(250);
428 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 spin_unlock_irqrestore(&chip->lock, flags);
Takashi Iwaib875d652007-09-11 10:12:14 +0200430 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 spin_lock_irqsave(&chip->lock, flags);
Krzysztof Helt9823adf2007-10-16 14:54:58 +0200432 reg = snd_cs4231_in(chip, CS4231_TEST_INIT);
433 reg &= CS4231_CALIB_IN_PROGRESS;
434 } while (reg && time_before(jiffies, timeout));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 spin_unlock_irqrestore(&chip->lock, flags);
Krzysztof Helt9823adf2007-10-16 14:54:58 +0200436
437 if (reg)
438 snd_printk(KERN_ERR
439 "mce_down - auto calibration time out (2)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440}
441
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100442static void snd_cs4231_advance_dma(struct cs4231_dma_control *dma_cont,
443 struct snd_pcm_substream *substream,
444 unsigned int *periods_sent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445{
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100446 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
448 while (1) {
Christopher Zimmermanna1314302005-09-21 00:41:22 -0700449 unsigned int period_size = snd_pcm_lib_period_bytes(substream);
450 unsigned int offset = period_size * (*periods_sent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
Eric Sesterhenn817dd6e2006-03-24 18:49:12 +0100452 BUG_ON(period_size >= (1 << 24));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
Krzysztof Helt9e9abb42007-09-10 23:06:55 +0200454 if (dma_cont->request(dma_cont,
455 runtime->dma_addr + offset, period_size))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 (*periods_sent) = ((*periods_sent) + 1) % runtime->periods;
458 }
459}
Christopher Zimmermanna1314302005-09-21 00:41:22 -0700460
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100461static void cs4231_dma_trigger(struct snd_pcm_substream *substream,
462 unsigned int what, int on)
Christopher Zimmermanna1314302005-09-21 00:41:22 -0700463{
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100464 struct snd_cs4231 *chip = snd_pcm_substream_chip(substream);
465 struct cs4231_dma_control *dma_cont;
Christopher Zimmermanna1314302005-09-21 00:41:22 -0700466
Georg Chini5a820fa2005-11-07 14:08:25 -0800467 if (what & CS4231_PLAYBACK_ENABLE) {
Georg Chinib1282542005-11-07 14:09:19 -0800468 dma_cont = &chip->p_dma;
Christopher Zimmermanna1314302005-09-21 00:41:22 -0700469 if (on) {
Georg Chinib1282542005-11-07 14:09:19 -0800470 dma_cont->prepare(dma_cont, 0);
471 dma_cont->enable(dma_cont, 1);
472 snd_cs4231_advance_dma(dma_cont,
Georg Chini5a820fa2005-11-07 14:08:25 -0800473 chip->playback_substream,
474 &chip->p_periods_sent);
Christopher Zimmermanna1314302005-09-21 00:41:22 -0700475 } else {
Georg Chinib1282542005-11-07 14:09:19 -0800476 dma_cont->enable(dma_cont, 0);
Christopher Zimmermanna1314302005-09-21 00:41:22 -0700477 }
Georg Chini5a820fa2005-11-07 14:08:25 -0800478 }
479 if (what & CS4231_RECORD_ENABLE) {
Georg Chinib1282542005-11-07 14:09:19 -0800480 dma_cont = &chip->c_dma;
Christopher Zimmermanna1314302005-09-21 00:41:22 -0700481 if (on) {
Georg Chinib1282542005-11-07 14:09:19 -0800482 dma_cont->prepare(dma_cont, 1);
483 dma_cont->enable(dma_cont, 1);
484 snd_cs4231_advance_dma(dma_cont,
Georg Chini5a820fa2005-11-07 14:08:25 -0800485 chip->capture_substream,
486 &chip->c_periods_sent);
Christopher Zimmermanna1314302005-09-21 00:41:22 -0700487 } else {
Georg Chinib1282542005-11-07 14:09:19 -0800488 dma_cont->enable(dma_cont, 0);
Christopher Zimmermanna1314302005-09-21 00:41:22 -0700489 }
Christopher Zimmermanna1314302005-09-21 00:41:22 -0700490 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491}
492
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100493static int snd_cs4231_trigger(struct snd_pcm_substream *substream, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494{
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100495 struct snd_cs4231 *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 int result = 0;
497
498 switch (cmd) {
499 case SNDRV_PCM_TRIGGER_START:
500 case SNDRV_PCM_TRIGGER_STOP:
501 {
502 unsigned int what = 0;
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100503 struct snd_pcm_substream *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 unsigned long flags;
505
Takashi Iwaief991b92007-02-22 12:52:53 +0100506 snd_pcm_group_for_each_entry(s, substream) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 if (s == chip->playback_substream) {
508 what |= CS4231_PLAYBACK_ENABLE;
509 snd_pcm_trigger_done(s, substream);
510 } else if (s == chip->capture_substream) {
511 what |= CS4231_RECORD_ENABLE;
512 snd_pcm_trigger_done(s, substream);
513 }
514 }
515
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 spin_lock_irqsave(&chip->lock, flags);
517 if (cmd == SNDRV_PCM_TRIGGER_START) {
Christopher Zimmermanna1314302005-09-21 00:41:22 -0700518 cs4231_dma_trigger(substream, what, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 chip->image[CS4231_IFACE_CTRL] |= what;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 } else {
Christopher Zimmermanna1314302005-09-21 00:41:22 -0700521 cs4231_dma_trigger(substream, what, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 chip->image[CS4231_IFACE_CTRL] &= ~what;
523 }
524 snd_cs4231_out(chip, CS4231_IFACE_CTRL,
525 chip->image[CS4231_IFACE_CTRL]);
526 spin_unlock_irqrestore(&chip->lock, flags);
527 break;
528 }
529 default:
530 result = -EINVAL;
531 break;
532 }
Christopher Zimmermanna1314302005-09-21 00:41:22 -0700533
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 return result;
535}
536
537/*
538 * CODEC I/O
539 */
540
541static unsigned char snd_cs4231_get_rate(unsigned int rate)
542{
543 int i;
544
545 for (i = 0; i < 14; i++)
546 if (rate == rates[i])
547 return freq_bits[i];
Krzysztof Helt9e9abb42007-09-10 23:06:55 +0200548
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 return freq_bits[13];
550}
551
Krzysztof Helt9e9abb42007-09-10 23:06:55 +0200552static unsigned char snd_cs4231_get_format(struct snd_cs4231 *chip, int format,
553 int channels)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554{
555 unsigned char rformat;
556
557 rformat = CS4231_LINEAR_8;
558 switch (format) {
Krzysztof Helt9e9abb42007-09-10 23:06:55 +0200559 case SNDRV_PCM_FORMAT_MU_LAW:
560 rformat = CS4231_ULAW_8;
561 break;
562 case SNDRV_PCM_FORMAT_A_LAW:
563 rformat = CS4231_ALAW_8;
564 break;
565 case SNDRV_PCM_FORMAT_S16_LE:
566 rformat = CS4231_LINEAR_16;
567 break;
568 case SNDRV_PCM_FORMAT_S16_BE:
569 rformat = CS4231_LINEAR_16_BIG;
570 break;
571 case SNDRV_PCM_FORMAT_IMA_ADPCM:
572 rformat = CS4231_ADPCM_16;
573 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 }
575 if (channels > 1)
576 rformat |= CS4231_STEREO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 return rformat;
578}
579
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100580static void snd_cs4231_calibrate_mute(struct snd_cs4231 *chip, int mute)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581{
582 unsigned long flags;
583
584 mute = mute ? 1 : 0;
585 spin_lock_irqsave(&chip->lock, flags);
586 if (chip->calibrate_mute == mute) {
587 spin_unlock_irqrestore(&chip->lock, flags);
588 return;
589 }
590 if (!mute) {
591 snd_cs4231_dout(chip, CS4231_LEFT_INPUT,
592 chip->image[CS4231_LEFT_INPUT]);
593 snd_cs4231_dout(chip, CS4231_RIGHT_INPUT,
594 chip->image[CS4231_RIGHT_INPUT]);
595 snd_cs4231_dout(chip, CS4231_LOOPBACK,
596 chip->image[CS4231_LOOPBACK]);
597 }
598 snd_cs4231_dout(chip, CS4231_AUX1_LEFT_INPUT,
599 mute ? 0x80 : chip->image[CS4231_AUX1_LEFT_INPUT]);
600 snd_cs4231_dout(chip, CS4231_AUX1_RIGHT_INPUT,
601 mute ? 0x80 : chip->image[CS4231_AUX1_RIGHT_INPUT]);
602 snd_cs4231_dout(chip, CS4231_AUX2_LEFT_INPUT,
603 mute ? 0x80 : chip->image[CS4231_AUX2_LEFT_INPUT]);
604 snd_cs4231_dout(chip, CS4231_AUX2_RIGHT_INPUT,
605 mute ? 0x80 : chip->image[CS4231_AUX2_RIGHT_INPUT]);
606 snd_cs4231_dout(chip, CS4231_LEFT_OUTPUT,
607 mute ? 0x80 : chip->image[CS4231_LEFT_OUTPUT]);
608 snd_cs4231_dout(chip, CS4231_RIGHT_OUTPUT,
609 mute ? 0x80 : chip->image[CS4231_RIGHT_OUTPUT]);
610 snd_cs4231_dout(chip, CS4231_LEFT_LINE_IN,
611 mute ? 0x80 : chip->image[CS4231_LEFT_LINE_IN]);
612 snd_cs4231_dout(chip, CS4231_RIGHT_LINE_IN,
613 mute ? 0x80 : chip->image[CS4231_RIGHT_LINE_IN]);
614 snd_cs4231_dout(chip, CS4231_MONO_CTRL,
615 mute ? 0xc0 : chip->image[CS4231_MONO_CTRL]);
616 chip->calibrate_mute = mute;
617 spin_unlock_irqrestore(&chip->lock, flags);
618}
619
Krzysztof Helt9e9abb42007-09-10 23:06:55 +0200620static void snd_cs4231_playback_format(struct snd_cs4231 *chip,
621 struct snd_pcm_hw_params *params,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 unsigned char pdfr)
623{
624 unsigned long flags;
625
Ingo Molnar12aa7572006-01-16 16:36:05 +0100626 mutex_lock(&chip->mce_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 snd_cs4231_calibrate_mute(chip, 1);
628
629 snd_cs4231_mce_up(chip);
630
631 spin_lock_irqsave(&chip->lock, flags);
632 snd_cs4231_out(chip, CS4231_PLAYBK_FORMAT,
633 (chip->image[CS4231_IFACE_CTRL] & CS4231_RECORD_ENABLE) ?
634 (pdfr & 0xf0) | (chip->image[CS4231_REC_FORMAT] & 0x0f) :
635 pdfr);
636 spin_unlock_irqrestore(&chip->lock, flags);
637
638 snd_cs4231_mce_down(chip);
639
640 snd_cs4231_calibrate_mute(chip, 0);
Ingo Molnar12aa7572006-01-16 16:36:05 +0100641 mutex_unlock(&chip->mce_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642}
643
Krzysztof Helt9e9abb42007-09-10 23:06:55 +0200644static void snd_cs4231_capture_format(struct snd_cs4231 *chip,
645 struct snd_pcm_hw_params *params,
646 unsigned char cdfr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647{
648 unsigned long flags;
649
Ingo Molnar12aa7572006-01-16 16:36:05 +0100650 mutex_lock(&chip->mce_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 snd_cs4231_calibrate_mute(chip, 1);
652
653 snd_cs4231_mce_up(chip);
654
655 spin_lock_irqsave(&chip->lock, flags);
656 if (!(chip->image[CS4231_IFACE_CTRL] & CS4231_PLAYBACK_ENABLE)) {
657 snd_cs4231_out(chip, CS4231_PLAYBK_FORMAT,
658 ((chip->image[CS4231_PLAYBK_FORMAT]) & 0xf0) |
659 (cdfr & 0x0f));
660 spin_unlock_irqrestore(&chip->lock, flags);
661 snd_cs4231_mce_down(chip);
662 snd_cs4231_mce_up(chip);
663 spin_lock_irqsave(&chip->lock, flags);
664 }
665 snd_cs4231_out(chip, CS4231_REC_FORMAT, cdfr);
666 spin_unlock_irqrestore(&chip->lock, flags);
667
668 snd_cs4231_mce_down(chip);
669
670 snd_cs4231_calibrate_mute(chip, 0);
Ingo Molnar12aa7572006-01-16 16:36:05 +0100671 mutex_unlock(&chip->mce_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672}
673
674/*
675 * Timer interface
676 */
677
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100678static unsigned long snd_cs4231_timer_resolution(struct snd_timer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679{
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100680 struct snd_cs4231 *chip = snd_timer_chip(timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
682 return chip->image[CS4231_PLAYBK_FORMAT] & 1 ? 9969 : 9920;
683}
684
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100685static int snd_cs4231_timer_start(struct snd_timer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686{
687 unsigned long flags;
688 unsigned int ticks;
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100689 struct snd_cs4231 *chip = snd_timer_chip(timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
691 spin_lock_irqsave(&chip->lock, flags);
692 ticks = timer->sticks;
693 if ((chip->image[CS4231_ALT_FEATURE_1] & CS4231_TIMER_ENABLE) == 0 ||
694 (unsigned char)(ticks >> 8) != chip->image[CS4231_TIMER_HIGH] ||
695 (unsigned char)ticks != chip->image[CS4231_TIMER_LOW]) {
696 snd_cs4231_out(chip, CS4231_TIMER_HIGH,
697 chip->image[CS4231_TIMER_HIGH] =
698 (unsigned char) (ticks >> 8));
699 snd_cs4231_out(chip, CS4231_TIMER_LOW,
700 chip->image[CS4231_TIMER_LOW] =
701 (unsigned char) ticks);
702 snd_cs4231_out(chip, CS4231_ALT_FEATURE_1,
Krzysztof Helt9e9abb42007-09-10 23:06:55 +0200703 chip->image[CS4231_ALT_FEATURE_1] |
704 CS4231_TIMER_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 }
706 spin_unlock_irqrestore(&chip->lock, flags);
707
708 return 0;
709}
710
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100711static int snd_cs4231_timer_stop(struct snd_timer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712{
713 unsigned long flags;
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100714 struct snd_cs4231 *chip = snd_timer_chip(timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
716 spin_lock_irqsave(&chip->lock, flags);
Krzysztof Helt9e9abb42007-09-10 23:06:55 +0200717 chip->image[CS4231_ALT_FEATURE_1] &= ~CS4231_TIMER_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 snd_cs4231_out(chip, CS4231_ALT_FEATURE_1,
Krzysztof Helt9e9abb42007-09-10 23:06:55 +0200719 chip->image[CS4231_ALT_FEATURE_1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 spin_unlock_irqrestore(&chip->lock, flags);
721
722 return 0;
723}
724
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100725static void __init snd_cs4231_init(struct snd_cs4231 *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726{
727 unsigned long flags;
728
729 snd_cs4231_mce_down(chip);
730
731#ifdef SNDRV_DEBUG_MCE
Christopher Zimmermanna1314302005-09-21 00:41:22 -0700732 snd_printdd("init: (1)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733#endif
734 snd_cs4231_mce_up(chip);
735 spin_lock_irqsave(&chip->lock, flags);
Krzysztof Helt9e9abb42007-09-10 23:06:55 +0200736 chip->image[CS4231_IFACE_CTRL] &= ~(CS4231_PLAYBACK_ENABLE |
737 CS4231_PLAYBACK_PIO |
738 CS4231_RECORD_ENABLE |
739 CS4231_RECORD_PIO |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 CS4231_CALIB_MODE);
741 chip->image[CS4231_IFACE_CTRL] |= CS4231_AUTOCALIB;
742 snd_cs4231_out(chip, CS4231_IFACE_CTRL, chip->image[CS4231_IFACE_CTRL]);
743 spin_unlock_irqrestore(&chip->lock, flags);
744 snd_cs4231_mce_down(chip);
745
746#ifdef SNDRV_DEBUG_MCE
Christopher Zimmermanna1314302005-09-21 00:41:22 -0700747 snd_printdd("init: (2)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748#endif
749
750 snd_cs4231_mce_up(chip);
751 spin_lock_irqsave(&chip->lock, flags);
Krzysztof Helt9e9abb42007-09-10 23:06:55 +0200752 snd_cs4231_out(chip, CS4231_ALT_FEATURE_1,
753 chip->image[CS4231_ALT_FEATURE_1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 spin_unlock_irqrestore(&chip->lock, flags);
755 snd_cs4231_mce_down(chip);
756
757#ifdef SNDRV_DEBUG_MCE
Krzysztof Helt9e9abb42007-09-10 23:06:55 +0200758 snd_printdd("init: (3) - afei = 0x%x\n",
759 chip->image[CS4231_ALT_FEATURE_1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760#endif
761
762 spin_lock_irqsave(&chip->lock, flags);
Krzysztof Helt9e9abb42007-09-10 23:06:55 +0200763 snd_cs4231_out(chip, CS4231_ALT_FEATURE_2,
764 chip->image[CS4231_ALT_FEATURE_2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 spin_unlock_irqrestore(&chip->lock, flags);
766
767 snd_cs4231_mce_up(chip);
768 spin_lock_irqsave(&chip->lock, flags);
Krzysztof Helt9e9abb42007-09-10 23:06:55 +0200769 snd_cs4231_out(chip, CS4231_PLAYBK_FORMAT,
770 chip->image[CS4231_PLAYBK_FORMAT]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 spin_unlock_irqrestore(&chip->lock, flags);
772 snd_cs4231_mce_down(chip);
773
774#ifdef SNDRV_DEBUG_MCE
Christopher Zimmermanna1314302005-09-21 00:41:22 -0700775 snd_printdd("init: (4)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776#endif
777
778 snd_cs4231_mce_up(chip);
779 spin_lock_irqsave(&chip->lock, flags);
780 snd_cs4231_out(chip, CS4231_REC_FORMAT, chip->image[CS4231_REC_FORMAT]);
781 spin_unlock_irqrestore(&chip->lock, flags);
782 snd_cs4231_mce_down(chip);
783
784#ifdef SNDRV_DEBUG_MCE
Christopher Zimmermanna1314302005-09-21 00:41:22 -0700785 snd_printdd("init: (5)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786#endif
787}
788
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100789static int snd_cs4231_open(struct snd_cs4231 *chip, unsigned int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790{
791 unsigned long flags;
792
Ingo Molnar12aa7572006-01-16 16:36:05 +0100793 mutex_lock(&chip->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 if ((chip->mode & mode)) {
Ingo Molnar12aa7572006-01-16 16:36:05 +0100795 mutex_unlock(&chip->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 return -EAGAIN;
797 }
798 if (chip->mode & CS4231_MODE_OPEN) {
799 chip->mode |= mode;
Ingo Molnar12aa7572006-01-16 16:36:05 +0100800 mutex_unlock(&chip->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 return 0;
802 }
803 /* ok. now enable and ack CODEC IRQ */
804 spin_lock_irqsave(&chip->lock, flags);
805 snd_cs4231_out(chip, CS4231_IRQ_STATUS, CS4231_PLAYBACK_IRQ |
806 CS4231_RECORD_IRQ |
807 CS4231_TIMER_IRQ);
808 snd_cs4231_out(chip, CS4231_IRQ_STATUS, 0);
Krzysztof Helt7e52f3d2007-09-05 15:08:23 +0200809 __cs4231_writeb(chip, 0, CS4231U(chip, STATUS)); /* clear IRQ */
810 __cs4231_writeb(chip, 0, CS4231U(chip, STATUS)); /* clear IRQ */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
812 snd_cs4231_out(chip, CS4231_IRQ_STATUS, CS4231_PLAYBACK_IRQ |
813 CS4231_RECORD_IRQ |
814 CS4231_TIMER_IRQ);
815 snd_cs4231_out(chip, CS4231_IRQ_STATUS, 0);
Christopher Zimmermanna1314302005-09-21 00:41:22 -0700816
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 spin_unlock_irqrestore(&chip->lock, flags);
818
819 chip->mode = mode;
Ingo Molnar12aa7572006-01-16 16:36:05 +0100820 mutex_unlock(&chip->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 return 0;
822}
823
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100824static void snd_cs4231_close(struct snd_cs4231 *chip, unsigned int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825{
826 unsigned long flags;
827
Ingo Molnar12aa7572006-01-16 16:36:05 +0100828 mutex_lock(&chip->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 chip->mode &= ~mode;
830 if (chip->mode & CS4231_MODE_OPEN) {
Ingo Molnar12aa7572006-01-16 16:36:05 +0100831 mutex_unlock(&chip->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 return;
833 }
834 snd_cs4231_calibrate_mute(chip, 1);
835
836 /* disable IRQ */
837 spin_lock_irqsave(&chip->lock, flags);
838 snd_cs4231_out(chip, CS4231_IRQ_STATUS, 0);
Krzysztof Helt7e52f3d2007-09-05 15:08:23 +0200839 __cs4231_writeb(chip, 0, CS4231U(chip, STATUS)); /* clear IRQ */
840 __cs4231_writeb(chip, 0, CS4231U(chip, STATUS)); /* clear IRQ */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841
842 /* now disable record & playback */
843
844 if (chip->image[CS4231_IFACE_CTRL] &
845 (CS4231_PLAYBACK_ENABLE | CS4231_PLAYBACK_PIO |
846 CS4231_RECORD_ENABLE | CS4231_RECORD_PIO)) {
847 spin_unlock_irqrestore(&chip->lock, flags);
848 snd_cs4231_mce_up(chip);
849 spin_lock_irqsave(&chip->lock, flags);
850 chip->image[CS4231_IFACE_CTRL] &=
851 ~(CS4231_PLAYBACK_ENABLE | CS4231_PLAYBACK_PIO |
852 CS4231_RECORD_ENABLE | CS4231_RECORD_PIO);
Krzysztof Helt9e9abb42007-09-10 23:06:55 +0200853 snd_cs4231_out(chip, CS4231_IFACE_CTRL,
854 chip->image[CS4231_IFACE_CTRL]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 spin_unlock_irqrestore(&chip->lock, flags);
856 snd_cs4231_mce_down(chip);
857 spin_lock_irqsave(&chip->lock, flags);
858 }
859
860 /* clear IRQ again */
861 snd_cs4231_out(chip, CS4231_IRQ_STATUS, 0);
Krzysztof Helt7e52f3d2007-09-05 15:08:23 +0200862 __cs4231_writeb(chip, 0, CS4231U(chip, STATUS)); /* clear IRQ */
863 __cs4231_writeb(chip, 0, CS4231U(chip, STATUS)); /* clear IRQ */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 spin_unlock_irqrestore(&chip->lock, flags);
865
866 snd_cs4231_calibrate_mute(chip, 0);
867
868 chip->mode = 0;
Ingo Molnar12aa7572006-01-16 16:36:05 +0100869 mutex_unlock(&chip->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870}
871
872/*
873 * timer open/close
874 */
875
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100876static int snd_cs4231_timer_open(struct snd_timer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877{
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100878 struct snd_cs4231 *chip = snd_timer_chip(timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 snd_cs4231_open(chip, CS4231_MODE_TIMER);
880 return 0;
881}
882
Krzysztof Helt9e9abb42007-09-10 23:06:55 +0200883static int snd_cs4231_timer_close(struct snd_timer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884{
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100885 struct snd_cs4231 *chip = snd_timer_chip(timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 snd_cs4231_close(chip, CS4231_MODE_TIMER);
887 return 0;
888}
889
Krzysztof Helt9e9abb42007-09-10 23:06:55 +0200890static struct snd_timer_hardware snd_cs4231_timer_table = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 .flags = SNDRV_TIMER_HW_AUTO,
892 .resolution = 9945,
893 .ticks = 65535,
894 .open = snd_cs4231_timer_open,
895 .close = snd_cs4231_timer_close,
896 .c_resolution = snd_cs4231_timer_resolution,
897 .start = snd_cs4231_timer_start,
898 .stop = snd_cs4231_timer_stop,
899};
900
901/*
902 * ok.. exported functions..
903 */
904
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100905static int snd_cs4231_playback_hw_params(struct snd_pcm_substream *substream,
906 struct snd_pcm_hw_params *hw_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907{
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100908 struct snd_cs4231 *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 unsigned char new_pdfr;
910 int err;
911
Krzysztof Helt9e9abb42007-09-10 23:06:55 +0200912 err = snd_pcm_lib_malloc_pages(substream,
913 params_buffer_bytes(hw_params));
914 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 return err;
916 new_pdfr = snd_cs4231_get_format(chip, params_format(hw_params),
917 params_channels(hw_params)) |
918 snd_cs4231_get_rate(params_rate(hw_params));
919 snd_cs4231_playback_format(chip, hw_params, new_pdfr);
920
921 return 0;
922}
923
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100924static int snd_cs4231_playback_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925{
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100926 struct snd_cs4231 *chip = snd_pcm_substream_chip(substream);
927 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 unsigned long flags;
929
930 spin_lock_irqsave(&chip->lock, flags);
Christopher Zimmermanna1314302005-09-21 00:41:22 -0700931
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 chip->image[CS4231_IFACE_CTRL] &= ~(CS4231_PLAYBACK_ENABLE |
933 CS4231_PLAYBACK_PIO);
Christopher Zimmermanna1314302005-09-21 00:41:22 -0700934
Eric Sesterhenn817dd6e2006-03-24 18:49:12 +0100935 BUG_ON(runtime->period_size > 0xffff + 1);
Christopher Zimmermanna1314302005-09-21 00:41:22 -0700936
Christopher Zimmermanna1314302005-09-21 00:41:22 -0700937 chip->p_periods_sent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 spin_unlock_irqrestore(&chip->lock, flags);
939
940 return 0;
941}
942
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100943static int snd_cs4231_capture_hw_params(struct snd_pcm_substream *substream,
944 struct snd_pcm_hw_params *hw_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945{
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100946 struct snd_cs4231 *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 unsigned char new_cdfr;
948 int err;
949
Krzysztof Helt9e9abb42007-09-10 23:06:55 +0200950 err = snd_pcm_lib_malloc_pages(substream,
951 params_buffer_bytes(hw_params));
952 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 return err;
954 new_cdfr = snd_cs4231_get_format(chip, params_format(hw_params),
955 params_channels(hw_params)) |
956 snd_cs4231_get_rate(params_rate(hw_params));
957 snd_cs4231_capture_format(chip, hw_params, new_cdfr);
958
959 return 0;
960}
961
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100962static int snd_cs4231_capture_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963{
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100964 struct snd_cs4231 *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 unsigned long flags;
966
967 spin_lock_irqsave(&chip->lock, flags);
968 chip->image[CS4231_IFACE_CTRL] &= ~(CS4231_RECORD_ENABLE |
969 CS4231_RECORD_PIO);
970
Christopher Zimmermanna1314302005-09-21 00:41:22 -0700971
Georg Chini5a820fa2005-11-07 14:08:25 -0800972 chip->c_periods_sent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 spin_unlock_irqrestore(&chip->lock, flags);
974
975 return 0;
976}
977
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100978static void snd_cs4231_overrange(struct snd_cs4231 *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979{
980 unsigned long flags;
981 unsigned char res;
982
983 spin_lock_irqsave(&chip->lock, flags);
984 res = snd_cs4231_in(chip, CS4231_TEST_INIT);
985 spin_unlock_irqrestore(&chip->lock, flags);
986
Krzysztof Helt9e9abb42007-09-10 23:06:55 +0200987 /* detect overrange only above 0dB; may be user selectable? */
988 if (res & (0x08 | 0x02))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 chip->capture_substream->runtime->overrange++;
990}
991
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100992static void snd_cs4231_play_callback(struct snd_cs4231 *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 if (chip->image[CS4231_IFACE_CTRL] & CS4231_PLAYBACK_ENABLE) {
995 snd_pcm_period_elapsed(chip->playback_substream);
Georg Chinib1282542005-11-07 14:09:19 -0800996 snd_cs4231_advance_dma(&chip->p_dma, chip->playback_substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 &chip->p_periods_sent);
998 }
999}
1000
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001001static void snd_cs4231_capture_callback(struct snd_cs4231 *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 if (chip->image[CS4231_IFACE_CTRL] & CS4231_RECORD_ENABLE) {
1004 snd_pcm_period_elapsed(chip->capture_substream);
Georg Chinib1282542005-11-07 14:09:19 -08001005 snd_cs4231_advance_dma(&chip->c_dma, chip->capture_substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 &chip->c_periods_sent);
1007 }
1008}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001010static snd_pcm_uframes_t snd_cs4231_playback_pointer(
1011 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012{
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001013 struct snd_cs4231 *chip = snd_pcm_substream_chip(substream);
1014 struct cs4231_dma_control *dma_cont = &chip->p_dma;
Georg Chini5a820fa2005-11-07 14:08:25 -08001015 size_t ptr;
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001016
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 if (!(chip->image[CS4231_IFACE_CTRL] & CS4231_PLAYBACK_ENABLE))
1018 return 0;
Georg Chinib1282542005-11-07 14:09:19 -08001019 ptr = dma_cont->address(dma_cont);
1020 if (ptr != 0)
1021 ptr -= substream->runtime->dma_addr;
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001022
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 return bytes_to_frames(substream->runtime, ptr);
1024}
1025
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001026static snd_pcm_uframes_t snd_cs4231_capture_pointer(
1027 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028{
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001029 struct snd_cs4231 *chip = snd_pcm_substream_chip(substream);
1030 struct cs4231_dma_control *dma_cont = &chip->c_dma;
Georg Chini5a820fa2005-11-07 14:08:25 -08001031 size_t ptr;
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001032
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 if (!(chip->image[CS4231_IFACE_CTRL] & CS4231_RECORD_ENABLE))
1034 return 0;
Georg Chinib1282542005-11-07 14:09:19 -08001035 ptr = dma_cont->address(dma_cont);
1036 if (ptr != 0)
1037 ptr -= substream->runtime->dma_addr;
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001038
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 return bytes_to_frames(substream->runtime, ptr);
1040}
1041
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001042static int __init snd_cs4231_probe(struct snd_cs4231 *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043{
1044 unsigned long flags;
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001045 int i;
1046 int id = 0;
1047 int vers = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 unsigned char *ptr;
1049
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 for (i = 0; i < 50; i++) {
1051 mb();
Krzysztof Helt7e52f3d2007-09-05 15:08:23 +02001052 if (__cs4231_readb(chip, CS4231U(chip, REGSEL)) & CS4231_INIT)
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001053 msleep(2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 else {
1055 spin_lock_irqsave(&chip->lock, flags);
1056 snd_cs4231_out(chip, CS4231_MISC_INFO, CS4231_MODE2);
1057 id = snd_cs4231_in(chip, CS4231_MISC_INFO) & 0x0f;
1058 vers = snd_cs4231_in(chip, CS4231_VERSION);
1059 spin_unlock_irqrestore(&chip->lock, flags);
1060 if (id == 0x0a)
1061 break; /* this is valid value */
1062 }
1063 }
1064 snd_printdd("cs4231: port = %p, id = 0x%x\n", chip->port, id);
1065 if (id != 0x0a)
1066 return -ENODEV; /* no valid device found */
1067
1068 spin_lock_irqsave(&chip->lock, flags);
1069
Krzysztof Helt7e52f3d2007-09-05 15:08:23 +02001070 /* clear any pendings IRQ */
1071 __cs4231_readb(chip, CS4231U(chip, STATUS));
1072 __cs4231_writeb(chip, 0, CS4231U(chip, STATUS));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 mb();
1074
1075 spin_unlock_irqrestore(&chip->lock, flags);
1076
1077 chip->image[CS4231_MISC_INFO] = CS4231_MODE2;
1078 chip->image[CS4231_IFACE_CTRL] =
1079 chip->image[CS4231_IFACE_CTRL] & ~CS4231_SINGLE_DMA;
1080 chip->image[CS4231_ALT_FEATURE_1] = 0x80;
1081 chip->image[CS4231_ALT_FEATURE_2] = 0x01;
1082 if (vers & 0x20)
1083 chip->image[CS4231_ALT_FEATURE_2] |= 0x02;
1084
1085 ptr = (unsigned char *) &chip->image;
1086
1087 snd_cs4231_mce_down(chip);
1088
1089 spin_lock_irqsave(&chip->lock, flags);
1090
1091 for (i = 0; i < 32; i++) /* ok.. fill all CS4231 registers */
1092 snd_cs4231_out(chip, i, *ptr++);
1093
1094 spin_unlock_irqrestore(&chip->lock, flags);
1095
1096 snd_cs4231_mce_up(chip);
1097
1098 snd_cs4231_mce_down(chip);
1099
1100 mdelay(2);
1101
1102 return 0; /* all things are ok.. */
1103}
1104
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001105static struct snd_pcm_hardware snd_cs4231_playback = {
1106 .info = SNDRV_PCM_INFO_MMAP |
1107 SNDRV_PCM_INFO_INTERLEAVED |
1108 SNDRV_PCM_INFO_MMAP_VALID |
1109 SNDRV_PCM_INFO_SYNC_START,
1110 .formats = SNDRV_PCM_FMTBIT_MU_LAW |
1111 SNDRV_PCM_FMTBIT_A_LAW |
1112 SNDRV_PCM_FMTBIT_IMA_ADPCM |
1113 SNDRV_PCM_FMTBIT_U8 |
1114 SNDRV_PCM_FMTBIT_S16_LE |
1115 SNDRV_PCM_FMTBIT_S16_BE,
1116 .rates = SNDRV_PCM_RATE_KNOT |
1117 SNDRV_PCM_RATE_8000_48000,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 .rate_min = 5510,
1119 .rate_max = 48000,
1120 .channels_min = 1,
1121 .channels_max = 2,
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001122 .buffer_bytes_max = 32 * 1024,
David S. Millerf9af1d92007-01-03 18:51:54 -08001123 .period_bytes_min = 64,
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001124 .period_bytes_max = 32 * 1024,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 .periods_min = 1,
1126 .periods_max = 1024,
1127};
1128
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001129static struct snd_pcm_hardware snd_cs4231_capture = {
1130 .info = SNDRV_PCM_INFO_MMAP |
1131 SNDRV_PCM_INFO_INTERLEAVED |
1132 SNDRV_PCM_INFO_MMAP_VALID |
1133 SNDRV_PCM_INFO_SYNC_START,
1134 .formats = SNDRV_PCM_FMTBIT_MU_LAW |
1135 SNDRV_PCM_FMTBIT_A_LAW |
1136 SNDRV_PCM_FMTBIT_IMA_ADPCM |
1137 SNDRV_PCM_FMTBIT_U8 |
1138 SNDRV_PCM_FMTBIT_S16_LE |
1139 SNDRV_PCM_FMTBIT_S16_BE,
1140 .rates = SNDRV_PCM_RATE_KNOT |
1141 SNDRV_PCM_RATE_8000_48000,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 .rate_min = 5510,
1143 .rate_max = 48000,
1144 .channels_min = 1,
1145 .channels_max = 2,
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001146 .buffer_bytes_max = 32 * 1024,
David S. Millerf9af1d92007-01-03 18:51:54 -08001147 .period_bytes_min = 64,
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001148 .period_bytes_max = 32 * 1024,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 .periods_min = 1,
1150 .periods_max = 1024,
1151};
1152
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001153static int snd_cs4231_playback_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154{
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001155 struct snd_cs4231 *chip = snd_pcm_substream_chip(substream);
1156 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 int err;
1158
1159 runtime->hw = snd_cs4231_playback;
1160
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001161 err = snd_cs4231_open(chip, CS4231_MODE_PLAY);
1162 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 snd_free_pages(runtime->dma_area, runtime->dma_bytes);
1164 return err;
1165 }
1166 chip->playback_substream = substream;
1167 chip->p_periods_sent = 0;
1168 snd_pcm_set_sync(substream);
1169 snd_cs4231_xrate(runtime);
1170
1171 return 0;
1172}
1173
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001174static int snd_cs4231_capture_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175{
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001176 struct snd_cs4231 *chip = snd_pcm_substream_chip(substream);
1177 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 int err;
1179
1180 runtime->hw = snd_cs4231_capture;
1181
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001182 err = snd_cs4231_open(chip, CS4231_MODE_RECORD);
1183 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 snd_free_pages(runtime->dma_area, runtime->dma_bytes);
1185 return err;
1186 }
1187 chip->capture_substream = substream;
1188 chip->c_periods_sent = 0;
1189 snd_pcm_set_sync(substream);
1190 snd_cs4231_xrate(runtime);
1191
1192 return 0;
1193}
1194
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001195static int snd_cs4231_playback_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196{
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001197 struct snd_cs4231 *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 snd_cs4231_close(chip, CS4231_MODE_PLAY);
Georg Chinib1282542005-11-07 14:09:19 -08001200 chip->playback_substream = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201
1202 return 0;
1203}
1204
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001205static int snd_cs4231_capture_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206{
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001207 struct snd_cs4231 *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 snd_cs4231_close(chip, CS4231_MODE_RECORD);
Georg Chinib1282542005-11-07 14:09:19 -08001210 chip->capture_substream = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211
1212 return 0;
1213}
1214
1215/* XXX We can do some power-management, in particular on EBUS using
1216 * XXX the audio AUXIO register...
1217 */
1218
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001219static struct snd_pcm_ops snd_cs4231_playback_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 .open = snd_cs4231_playback_open,
1221 .close = snd_cs4231_playback_close,
1222 .ioctl = snd_pcm_lib_ioctl,
1223 .hw_params = snd_cs4231_playback_hw_params,
Krzysztof Heltc6c2d572007-09-04 13:08:24 +02001224 .hw_free = snd_pcm_lib_free_pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 .prepare = snd_cs4231_playback_prepare,
1226 .trigger = snd_cs4231_trigger,
1227 .pointer = snd_cs4231_playback_pointer,
1228};
1229
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001230static struct snd_pcm_ops snd_cs4231_capture_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 .open = snd_cs4231_capture_open,
1232 .close = snd_cs4231_capture_close,
1233 .ioctl = snd_pcm_lib_ioctl,
1234 .hw_params = snd_cs4231_capture_hw_params,
Krzysztof Heltc6c2d572007-09-04 13:08:24 +02001235 .hw_free = snd_pcm_lib_free_pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 .prepare = snd_cs4231_capture_prepare,
1237 .trigger = snd_cs4231_trigger,
1238 .pointer = snd_cs4231_capture_pointer,
1239};
1240
Krzysztof Heltc6c2d572007-09-04 13:08:24 +02001241static int __init snd_cs4231_pcm(struct snd_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242{
Krzysztof Heltc6c2d572007-09-04 13:08:24 +02001243 struct snd_cs4231 *chip = card->private_data;
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001244 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 int err;
1246
Krzysztof Heltc6c2d572007-09-04 13:08:24 +02001247 err = snd_pcm_new(card, "CS4231", 0, 1, 1, &pcm);
1248 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 return err;
1250
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001251 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
1252 &snd_cs4231_playback_ops);
1253 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
1254 &snd_cs4231_capture_ops);
1255
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 /* global setup */
1257 pcm->private_data = chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 pcm->info_flags = SNDRV_PCM_INFO_JOINT_DUPLEX;
1259 strcpy(pcm->name, "CS4231");
1260
Georg Chinib1282542005-11-07 14:09:19 -08001261 chip->p_dma.preallocate(chip, pcm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262
1263 chip->pcm = pcm;
1264
1265 return 0;
1266}
1267
Krzysztof Heltc6c2d572007-09-04 13:08:24 +02001268static int __init snd_cs4231_timer(struct snd_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269{
Krzysztof Heltc6c2d572007-09-04 13:08:24 +02001270 struct snd_cs4231 *chip = card->private_data;
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001271 struct snd_timer *timer;
1272 struct snd_timer_id tid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 int err;
1274
1275 /* Timer initialization */
1276 tid.dev_class = SNDRV_TIMER_CLASS_CARD;
1277 tid.dev_sclass = SNDRV_TIMER_SCLASS_NONE;
Krzysztof Heltc6c2d572007-09-04 13:08:24 +02001278 tid.card = card->number;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 tid.device = 0;
1280 tid.subdevice = 0;
Krzysztof Heltc6c2d572007-09-04 13:08:24 +02001281 err = snd_timer_new(card, "CS4231", &tid, &timer);
1282 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 return err;
1284 strcpy(timer->name, "CS4231");
1285 timer->private_data = chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 timer->hw = snd_cs4231_timer_table;
1287 chip->timer = timer;
1288
1289 return 0;
1290}
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001291
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292/*
1293 * MIXER part
1294 */
1295
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001296static int snd_cs4231_info_mux(struct snd_kcontrol *kcontrol,
1297 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298{
1299 static char *texts[4] = {
1300 "Line", "CD", "Mic", "Mix"
1301 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1304 uinfo->count = 2;
1305 uinfo->value.enumerated.items = 4;
1306 if (uinfo->value.enumerated.item > 3)
1307 uinfo->value.enumerated.item = 3;
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001308 strcpy(uinfo->value.enumerated.name,
1309 texts[uinfo->value.enumerated.item]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310
1311 return 0;
1312}
1313
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001314static int snd_cs4231_get_mux(struct snd_kcontrol *kcontrol,
1315 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316{
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001317 struct snd_cs4231 *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 unsigned long flags;
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001319
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 spin_lock_irqsave(&chip->lock, flags);
1321 ucontrol->value.enumerated.item[0] =
1322 (chip->image[CS4231_LEFT_INPUT] & CS4231_MIXS_ALL) >> 6;
1323 ucontrol->value.enumerated.item[1] =
1324 (chip->image[CS4231_RIGHT_INPUT] & CS4231_MIXS_ALL) >> 6;
1325 spin_unlock_irqrestore(&chip->lock, flags);
1326
1327 return 0;
1328}
1329
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001330static int snd_cs4231_put_mux(struct snd_kcontrol *kcontrol,
1331 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332{
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001333 struct snd_cs4231 *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 unsigned long flags;
1335 unsigned short left, right;
1336 int change;
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001337
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 if (ucontrol->value.enumerated.item[0] > 3 ||
1339 ucontrol->value.enumerated.item[1] > 3)
1340 return -EINVAL;
1341 left = ucontrol->value.enumerated.item[0] << 6;
1342 right = ucontrol->value.enumerated.item[1] << 6;
1343
1344 spin_lock_irqsave(&chip->lock, flags);
1345
1346 left = (chip->image[CS4231_LEFT_INPUT] & ~CS4231_MIXS_ALL) | left;
1347 right = (chip->image[CS4231_RIGHT_INPUT] & ~CS4231_MIXS_ALL) | right;
1348 change = left != chip->image[CS4231_LEFT_INPUT] ||
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001349 right != chip->image[CS4231_RIGHT_INPUT];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 snd_cs4231_out(chip, CS4231_LEFT_INPUT, left);
1351 snd_cs4231_out(chip, CS4231_RIGHT_INPUT, right);
1352
1353 spin_unlock_irqrestore(&chip->lock, flags);
1354
1355 return change;
1356}
1357
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001358static int snd_cs4231_info_single(struct snd_kcontrol *kcontrol,
1359 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360{
1361 int mask = (kcontrol->private_value >> 16) & 0xff;
1362
1363 uinfo->type = (mask == 1) ?
1364 SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
1365 uinfo->count = 1;
1366 uinfo->value.integer.min = 0;
1367 uinfo->value.integer.max = mask;
1368
1369 return 0;
1370}
1371
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001372static int snd_cs4231_get_single(struct snd_kcontrol *kcontrol,
1373 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374{
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001375 struct snd_cs4231 *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 unsigned long flags;
1377 int reg = kcontrol->private_value & 0xff;
1378 int shift = (kcontrol->private_value >> 8) & 0xff;
1379 int mask = (kcontrol->private_value >> 16) & 0xff;
1380 int invert = (kcontrol->private_value >> 24) & 0xff;
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001381
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 spin_lock_irqsave(&chip->lock, flags);
1383
1384 ucontrol->value.integer.value[0] = (chip->image[reg] >> shift) & mask;
1385
1386 spin_unlock_irqrestore(&chip->lock, flags);
1387
1388 if (invert)
1389 ucontrol->value.integer.value[0] =
1390 (mask - ucontrol->value.integer.value[0]);
1391
1392 return 0;
1393}
1394
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001395static int snd_cs4231_put_single(struct snd_kcontrol *kcontrol,
1396 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397{
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001398 struct snd_cs4231 *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 unsigned long flags;
1400 int reg = kcontrol->private_value & 0xff;
1401 int shift = (kcontrol->private_value >> 8) & 0xff;
1402 int mask = (kcontrol->private_value >> 16) & 0xff;
1403 int invert = (kcontrol->private_value >> 24) & 0xff;
1404 int change;
1405 unsigned short val;
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001406
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 val = (ucontrol->value.integer.value[0] & mask);
1408 if (invert)
1409 val = mask - val;
1410 val <<= shift;
1411
1412 spin_lock_irqsave(&chip->lock, flags);
1413
1414 val = (chip->image[reg] & ~(mask << shift)) | val;
1415 change = val != chip->image[reg];
1416 snd_cs4231_out(chip, reg, val);
1417
1418 spin_unlock_irqrestore(&chip->lock, flags);
1419
1420 return change;
1421}
1422
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001423static int snd_cs4231_info_double(struct snd_kcontrol *kcontrol,
1424 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425{
1426 int mask = (kcontrol->private_value >> 24) & 0xff;
1427
1428 uinfo->type = mask == 1 ?
1429 SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
1430 uinfo->count = 2;
1431 uinfo->value.integer.min = 0;
1432 uinfo->value.integer.max = mask;
1433
1434 return 0;
1435}
1436
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001437static int snd_cs4231_get_double(struct snd_kcontrol *kcontrol,
1438 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439{
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001440 struct snd_cs4231 *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 unsigned long flags;
1442 int left_reg = kcontrol->private_value & 0xff;
1443 int right_reg = (kcontrol->private_value >> 8) & 0xff;
1444 int shift_left = (kcontrol->private_value >> 16) & 0x07;
1445 int shift_right = (kcontrol->private_value >> 19) & 0x07;
1446 int mask = (kcontrol->private_value >> 24) & 0xff;
1447 int invert = (kcontrol->private_value >> 22) & 1;
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001448
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 spin_lock_irqsave(&chip->lock, flags);
1450
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001451 ucontrol->value.integer.value[0] =
1452 (chip->image[left_reg] >> shift_left) & mask;
1453 ucontrol->value.integer.value[1] =
1454 (chip->image[right_reg] >> shift_right) & mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455
1456 spin_unlock_irqrestore(&chip->lock, flags);
1457
1458 if (invert) {
1459 ucontrol->value.integer.value[0] =
1460 (mask - ucontrol->value.integer.value[0]);
1461 ucontrol->value.integer.value[1] =
1462 (mask - ucontrol->value.integer.value[1]);
1463 }
1464
1465 return 0;
1466}
1467
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001468static int snd_cs4231_put_double(struct snd_kcontrol *kcontrol,
1469 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470{
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001471 struct snd_cs4231 *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 unsigned long flags;
1473 int left_reg = kcontrol->private_value & 0xff;
1474 int right_reg = (kcontrol->private_value >> 8) & 0xff;
1475 int shift_left = (kcontrol->private_value >> 16) & 0x07;
1476 int shift_right = (kcontrol->private_value >> 19) & 0x07;
1477 int mask = (kcontrol->private_value >> 24) & 0xff;
1478 int invert = (kcontrol->private_value >> 22) & 1;
1479 int change;
1480 unsigned short val1, val2;
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001481
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 val1 = ucontrol->value.integer.value[0] & mask;
1483 val2 = ucontrol->value.integer.value[1] & mask;
1484 if (invert) {
1485 val1 = mask - val1;
1486 val2 = mask - val2;
1487 }
1488 val1 <<= shift_left;
1489 val2 <<= shift_right;
1490
1491 spin_lock_irqsave(&chip->lock, flags);
1492
1493 val1 = (chip->image[left_reg] & ~(mask << shift_left)) | val1;
1494 val2 = (chip->image[right_reg] & ~(mask << shift_right)) | val2;
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001495 change = val1 != chip->image[left_reg];
1496 change |= val2 != chip->image[right_reg];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 snd_cs4231_out(chip, left_reg, val1);
1498 snd_cs4231_out(chip, right_reg, val2);
1499
1500 spin_unlock_irqrestore(&chip->lock, flags);
1501
1502 return change;
1503}
1504
1505#define CS4231_SINGLE(xname, xindex, reg, shift, mask, invert) \
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001506{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = (xname), .index = (xindex), \
1507 .info = snd_cs4231_info_single, \
1508 .get = snd_cs4231_get_single, .put = snd_cs4231_put_single, \
1509 .private_value = (reg) | ((shift) << 8) | ((mask) << 16) | ((invert) << 24) }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001511#define CS4231_DOUBLE(xname, xindex, left_reg, right_reg, shift_left, \
1512 shift_right, mask, invert) \
1513{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = (xname), .index = (xindex), \
1514 .info = snd_cs4231_info_double, \
1515 .get = snd_cs4231_get_double, .put = snd_cs4231_put_double, \
1516 .private_value = (left_reg) | ((right_reg) << 8) | ((shift_left) << 16) | \
1517 ((shift_right) << 19) | ((mask) << 24) | ((invert) << 22) }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001519static struct snd_kcontrol_new snd_cs4231_controls[] __initdata = {
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001520CS4231_DOUBLE("PCM Playback Switch", 0, CS4231_LEFT_OUTPUT,
1521 CS4231_RIGHT_OUTPUT, 7, 7, 1, 1),
1522CS4231_DOUBLE("PCM Playback Volume", 0, CS4231_LEFT_OUTPUT,
1523 CS4231_RIGHT_OUTPUT, 0, 0, 63, 1),
1524CS4231_DOUBLE("Line Playback Switch", 0, CS4231_LEFT_LINE_IN,
1525 CS4231_RIGHT_LINE_IN, 7, 7, 1, 1),
1526CS4231_DOUBLE("Line Playback Volume", 0, CS4231_LEFT_LINE_IN,
1527 CS4231_RIGHT_LINE_IN, 0, 0, 31, 1),
1528CS4231_DOUBLE("Aux Playback Switch", 0, CS4231_AUX1_LEFT_INPUT,
1529 CS4231_AUX1_RIGHT_INPUT, 7, 7, 1, 1),
1530CS4231_DOUBLE("Aux Playback Volume", 0, CS4231_AUX1_LEFT_INPUT,
1531 CS4231_AUX1_RIGHT_INPUT, 0, 0, 31, 1),
1532CS4231_DOUBLE("Aux Playback Switch", 1, CS4231_AUX2_LEFT_INPUT,
1533 CS4231_AUX2_RIGHT_INPUT, 7, 7, 1, 1),
1534CS4231_DOUBLE("Aux Playback Volume", 1, CS4231_AUX2_LEFT_INPUT,
1535 CS4231_AUX2_RIGHT_INPUT, 0, 0, 31, 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536CS4231_SINGLE("Mono Playback Switch", 0, CS4231_MONO_CTRL, 7, 1, 1),
1537CS4231_SINGLE("Mono Playback Volume", 0, CS4231_MONO_CTRL, 0, 15, 1),
1538CS4231_SINGLE("Mono Output Playback Switch", 0, CS4231_MONO_CTRL, 6, 1, 1),
1539CS4231_SINGLE("Mono Output Playback Bypass", 0, CS4231_MONO_CTRL, 5, 1, 0),
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001540CS4231_DOUBLE("Capture Volume", 0, CS4231_LEFT_INPUT, CS4231_RIGHT_INPUT, 0, 0,
1541 15, 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542{
1543 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1544 .name = "Capture Source",
1545 .info = snd_cs4231_info_mux,
1546 .get = snd_cs4231_get_mux,
1547 .put = snd_cs4231_put_mux,
1548},
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001549CS4231_DOUBLE("Mic Boost", 0, CS4231_LEFT_INPUT, CS4231_RIGHT_INPUT, 5, 5,
1550 1, 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551CS4231_SINGLE("Loopback Capture Switch", 0, CS4231_LOOPBACK, 0, 1, 0),
1552CS4231_SINGLE("Loopback Capture Volume", 0, CS4231_LOOPBACK, 2, 63, 1),
1553/* SPARC specific uses of XCTL{0,1} general purpose outputs. */
1554CS4231_SINGLE("Line Out Switch", 0, CS4231_PIN_CTRL, 6, 1, 1),
1555CS4231_SINGLE("Headphone Out Switch", 0, CS4231_PIN_CTRL, 7, 1, 1)
1556};
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001557
Krzysztof Heltc6c2d572007-09-04 13:08:24 +02001558static int __init snd_cs4231_mixer(struct snd_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559{
Krzysztof Heltc6c2d572007-09-04 13:08:24 +02001560 struct snd_cs4231 *chip = card->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 int err, idx;
1562
1563 snd_assert(chip != NULL && chip->pcm != NULL, return -EINVAL);
1564
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 strcpy(card->mixername, chip->pcm->name);
1566
1567 for (idx = 0; idx < ARRAY_SIZE(snd_cs4231_controls); idx++) {
Krzysztof Heltc6c2d572007-09-04 13:08:24 +02001568 err = snd_ctl_add(card,
1569 snd_ctl_new1(&snd_cs4231_controls[idx], chip));
1570 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 return err;
1572 }
1573 return 0;
1574}
1575
1576static int dev;
1577
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001578static int __init cs4231_attach_begin(struct snd_card **rcard)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579{
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001580 struct snd_card *card;
Krzysztof Heltc6c2d572007-09-04 13:08:24 +02001581 struct snd_cs4231 *chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582
1583 *rcard = NULL;
1584
1585 if (dev >= SNDRV_CARDS)
1586 return -ENODEV;
1587
1588 if (!enable[dev]) {
1589 dev++;
1590 return -ENOENT;
1591 }
1592
Krzysztof Heltc6c2d572007-09-04 13:08:24 +02001593 card = snd_card_new(index[dev], id[dev], THIS_MODULE,
1594 sizeof(struct snd_cs4231));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 if (card == NULL)
1596 return -ENOMEM;
1597
1598 strcpy(card->driver, "CS4231");
1599 strcpy(card->shortname, "Sun CS4231");
1600
Krzysztof Heltc6c2d572007-09-04 13:08:24 +02001601 chip = card->private_data;
1602 chip->card = card;
1603
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 *rcard = card;
1605 return 0;
1606}
1607
Krzysztof Heltc6c2d572007-09-04 13:08:24 +02001608static int __init cs4231_attach_finish(struct snd_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609{
Krzysztof Heltc6c2d572007-09-04 13:08:24 +02001610 struct snd_cs4231 *chip = card->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 int err;
1612
Krzysztof Heltc6c2d572007-09-04 13:08:24 +02001613 err = snd_cs4231_pcm(card);
1614 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 goto out_err;
1616
Krzysztof Heltc6c2d572007-09-04 13:08:24 +02001617 err = snd_cs4231_mixer(card);
1618 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 goto out_err;
1620
Krzysztof Heltc6c2d572007-09-04 13:08:24 +02001621 err = snd_cs4231_timer(card);
1622 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 goto out_err;
1624
Krzysztof Heltc6c2d572007-09-04 13:08:24 +02001625 err = snd_card_register(card);
1626 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 goto out_err;
1628
1629 chip->next = cs4231_list;
1630 cs4231_list = chip;
1631
1632 dev++;
1633 return 0;
1634
1635out_err:
1636 snd_card_free(card);
1637 return err;
1638}
1639
1640#ifdef SBUS_SUPPORT
Georg Chinib1282542005-11-07 14:09:19 -08001641
David Howells7d12e782006-10-05 14:55:46 +01001642static irqreturn_t snd_cs4231_sbus_interrupt(int irq, void *dev_id)
Georg Chinib1282542005-11-07 14:09:19 -08001643{
1644 unsigned long flags;
1645 unsigned char status;
1646 u32 csr;
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001647 struct snd_cs4231 *chip = dev_id;
Georg Chinib1282542005-11-07 14:09:19 -08001648
1649 /*This is IRQ is not raised by the cs4231*/
Krzysztof Helt7e52f3d2007-09-05 15:08:23 +02001650 if (!(__cs4231_readb(chip, CS4231U(chip, STATUS)) & CS4231_GLOBALIRQ))
Georg Chinib1282542005-11-07 14:09:19 -08001651 return IRQ_NONE;
1652
1653 /* ACK the APC interrupt. */
1654 csr = sbus_readl(chip->port + APCCSR);
1655
1656 sbus_writel(csr, chip->port + APCCSR);
1657
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001658 if ((csr & APC_PDMA_READY) &&
1659 (csr & APC_PLAY_INT) &&
Georg Chinib1282542005-11-07 14:09:19 -08001660 (csr & APC_XINT_PNVA) &&
1661 !(csr & APC_XINT_EMPT))
1662 snd_cs4231_play_callback(chip);
1663
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001664 if ((csr & APC_CDMA_READY) &&
1665 (csr & APC_CAPT_INT) &&
Georg Chinib1282542005-11-07 14:09:19 -08001666 (csr & APC_XINT_CNVA) &&
1667 !(csr & APC_XINT_EMPT))
1668 snd_cs4231_capture_callback(chip);
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001669
Georg Chinib1282542005-11-07 14:09:19 -08001670 status = snd_cs4231_in(chip, CS4231_IRQ_STATUS);
1671
1672 if (status & CS4231_TIMER_IRQ) {
1673 if (chip->timer)
1674 snd_timer_interrupt(chip->timer, chip->timer->sticks);
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001675 }
Georg Chinib1282542005-11-07 14:09:19 -08001676
1677 if ((status & CS4231_RECORD_IRQ) && (csr & APC_CDMA_READY))
1678 snd_cs4231_overrange(chip);
1679
1680 /* ACK the CS4231 interrupt. */
1681 spin_lock_irqsave(&chip->lock, flags);
1682 snd_cs4231_outm(chip, CS4231_IRQ_STATUS, ~CS4231_ALL_IRQS | ~status, 0);
1683 spin_unlock_irqrestore(&chip->lock, flags);
1684
Georg Chinid35a1b92007-01-02 21:28:17 -08001685 return IRQ_HANDLED;
Georg Chinib1282542005-11-07 14:09:19 -08001686}
1687
1688/*
1689 * SBUS DMA routines
1690 */
1691
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001692static int sbus_dma_request(struct cs4231_dma_control *dma_cont,
1693 dma_addr_t bus_addr, size_t len)
Georg Chinib1282542005-11-07 14:09:19 -08001694{
1695 unsigned long flags;
1696 u32 test, csr;
1697 int err;
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001698 struct sbus_dma_info *base = &dma_cont->sbus_info;
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001699
Georg Chinib1282542005-11-07 14:09:19 -08001700 if (len >= (1 << 24))
1701 return -EINVAL;
1702 spin_lock_irqsave(&base->lock, flags);
1703 csr = sbus_readl(base->regs + APCCSR);
1704 err = -EINVAL;
1705 test = APC_CDMA_READY;
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001706 if (base->dir == APC_PLAY)
Georg Chinib1282542005-11-07 14:09:19 -08001707 test = APC_PDMA_READY;
1708 if (!(csr & test))
1709 goto out;
1710 err = -EBUSY;
Georg Chinib1282542005-11-07 14:09:19 -08001711 test = APC_XINT_CNVA;
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001712 if (base->dir == APC_PLAY)
Georg Chinib1282542005-11-07 14:09:19 -08001713 test = APC_XINT_PNVA;
1714 if (!(csr & test))
1715 goto out;
1716 err = 0;
1717 sbus_writel(bus_addr, base->regs + base->dir + APCNVA);
1718 sbus_writel(len, base->regs + base->dir + APCNC);
1719out:
1720 spin_unlock_irqrestore(&base->lock, flags);
1721 return err;
1722}
1723
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001724static void sbus_dma_prepare(struct cs4231_dma_control *dma_cont, int d)
Georg Chinib1282542005-11-07 14:09:19 -08001725{
1726 unsigned long flags;
1727 u32 csr, test;
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001728 struct sbus_dma_info *base = &dma_cont->sbus_info;
Georg Chinib1282542005-11-07 14:09:19 -08001729
1730 spin_lock_irqsave(&base->lock, flags);
1731 csr = sbus_readl(base->regs + APCCSR);
1732 test = APC_GENL_INT | APC_PLAY_INT | APC_XINT_ENA |
1733 APC_XINT_PLAY | APC_XINT_PEMP | APC_XINT_GENL |
1734 APC_XINT_PENA;
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001735 if (base->dir == APC_RECORD)
Georg Chinib1282542005-11-07 14:09:19 -08001736 test = APC_GENL_INT | APC_CAPT_INT | APC_XINT_ENA |
1737 APC_XINT_CAPT | APC_XINT_CEMP | APC_XINT_GENL;
1738 csr |= test;
1739 sbus_writel(csr, base->regs + APCCSR);
1740 spin_unlock_irqrestore(&base->lock, flags);
1741}
1742
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001743static void sbus_dma_enable(struct cs4231_dma_control *dma_cont, int on)
Georg Chinib1282542005-11-07 14:09:19 -08001744{
1745 unsigned long flags;
1746 u32 csr, shift;
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001747 struct sbus_dma_info *base = &dma_cont->sbus_info;
Georg Chinib1282542005-11-07 14:09:19 -08001748
1749 spin_lock_irqsave(&base->lock, flags);
1750 if (!on) {
Georg Chinid35a1b92007-01-02 21:28:17 -08001751 sbus_writel(0, base->regs + base->dir + APCNC);
1752 sbus_writel(0, base->regs + base->dir + APCNVA);
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001753 if (base->dir == APC_PLAY) {
Georg Chini3daadf32007-08-01 21:55:58 -07001754 sbus_writel(0, base->regs + base->dir + APCC);
1755 sbus_writel(0, base->regs + base->dir + APCVA);
1756 }
Georg Chinid35a1b92007-01-02 21:28:17 -08001757
Georg Chini3daadf32007-08-01 21:55:58 -07001758 udelay(1200);
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001759 }
Georg Chinib1282542005-11-07 14:09:19 -08001760 csr = sbus_readl(base->regs + APCCSR);
1761 shift = 0;
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001762 if (base->dir == APC_PLAY)
Georg Chinib1282542005-11-07 14:09:19 -08001763 shift = 1;
1764 if (on)
1765 csr &= ~(APC_CPAUSE << shift);
1766 else
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001767 csr |= (APC_CPAUSE << shift);
Georg Chinib1282542005-11-07 14:09:19 -08001768 sbus_writel(csr, base->regs + APCCSR);
1769 if (on)
1770 csr |= (APC_CDMA_READY << shift);
1771 else
1772 csr &= ~(APC_CDMA_READY << shift);
1773 sbus_writel(csr, base->regs + APCCSR);
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001774
Georg Chinib1282542005-11-07 14:09:19 -08001775 spin_unlock_irqrestore(&base->lock, flags);
1776}
1777
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001778static unsigned int sbus_dma_addr(struct cs4231_dma_control *dma_cont)
Georg Chinib1282542005-11-07 14:09:19 -08001779{
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001780 struct sbus_dma_info *base = &dma_cont->sbus_info;
Georg Chinib1282542005-11-07 14:09:19 -08001781
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001782 return sbus_readl(base->regs + base->dir + APCVA);
Georg Chinib1282542005-11-07 14:09:19 -08001783}
1784
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001785static void sbus_dma_preallocate(struct snd_cs4231 *chip, struct snd_pcm *pcm)
Georg Chinib1282542005-11-07 14:09:19 -08001786{
1787 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_SBUS,
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001788 snd_dma_sbus_data(chip->dev_u.sdev),
1789 64 * 1024, 128 * 1024);
Georg Chinib1282542005-11-07 14:09:19 -08001790}
1791
1792/*
1793 * Init and exit routines
1794 */
1795
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001796static int snd_cs4231_sbus_free(struct snd_cs4231 *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797{
1798 if (chip->irq[0])
1799 free_irq(chip->irq[0], chip);
1800
1801 if (chip->port)
1802 sbus_iounmap(chip->port, chip->regs_size);
1803
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 return 0;
1805}
1806
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001807static int snd_cs4231_sbus_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808{
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001809 struct snd_cs4231 *cp = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810
1811 return snd_cs4231_sbus_free(cp);
1812}
1813
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001814static struct snd_device_ops snd_cs4231_sbus_dev_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 .dev_free = snd_cs4231_sbus_dev_free,
1816};
1817
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001818static int __init snd_cs4231_sbus_create(struct snd_card *card,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 struct sbus_dev *sdev,
Krzysztof Heltc6c2d572007-09-04 13:08:24 +02001820 int dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821{
Krzysztof Heltc6c2d572007-09-04 13:08:24 +02001822 struct snd_cs4231 *chip = card->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 int err;
1824
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 spin_lock_init(&chip->lock);
Georg Chinib1282542005-11-07 14:09:19 -08001826 spin_lock_init(&chip->c_dma.sbus_info.lock);
1827 spin_lock_init(&chip->p_dma.sbus_info.lock);
Ingo Molnar12aa7572006-01-16 16:36:05 +01001828 mutex_init(&chip->mce_mutex);
1829 mutex_init(&chip->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 chip->dev_u.sdev = sdev;
1831 chip->regs_size = sdev->reg_addrs[0].reg_size;
1832 memcpy(&chip->image, &snd_cs4231_original_image,
1833 sizeof(snd_cs4231_original_image));
1834
1835 chip->port = sbus_ioremap(&sdev->resource[0], 0,
1836 chip->regs_size, "cs4231");
1837 if (!chip->port) {
Christopher Zimmermanna1314302005-09-21 00:41:22 -07001838 snd_printdd("cs4231-%d: Unable to map chip registers.\n", dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 return -EIO;
1840 }
1841
Georg Chinib1282542005-11-07 14:09:19 -08001842 chip->c_dma.sbus_info.regs = chip->port;
1843 chip->p_dma.sbus_info.regs = chip->port;
1844 chip->c_dma.sbus_info.dir = APC_RECORD;
1845 chip->p_dma.sbus_info.dir = APC_PLAY;
1846
1847 chip->p_dma.prepare = sbus_dma_prepare;
1848 chip->p_dma.enable = sbus_dma_enable;
1849 chip->p_dma.request = sbus_dma_request;
1850 chip->p_dma.address = sbus_dma_addr;
Georg Chinib1282542005-11-07 14:09:19 -08001851 chip->p_dma.preallocate = sbus_dma_preallocate;
1852
1853 chip->c_dma.prepare = sbus_dma_prepare;
1854 chip->c_dma.enable = sbus_dma_enable;
1855 chip->c_dma.request = sbus_dma_request;
1856 chip->c_dma.address = sbus_dma_addr;
Georg Chinib1282542005-11-07 14:09:19 -08001857 chip->c_dma.preallocate = sbus_dma_preallocate;
Georg Chini5a820fa2005-11-07 14:08:25 -08001858
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 if (request_irq(sdev->irqs[0], snd_cs4231_sbus_interrupt,
Thomas Gleixner65ca68b2006-07-01 19:29:46 -07001860 IRQF_SHARED, "cs4231", chip)) {
David S. Millerc6387a42006-06-20 01:21:29 -07001861 snd_printdd("cs4231-%d: Unable to grab SBUS IRQ %d\n",
1862 dev, sdev->irqs[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 snd_cs4231_sbus_free(chip);
1864 return -EBUSY;
1865 }
1866 chip->irq[0] = sdev->irqs[0];
1867
1868 if (snd_cs4231_probe(chip) < 0) {
1869 snd_cs4231_sbus_free(chip);
1870 return -ENODEV;
1871 }
1872 snd_cs4231_init(chip);
1873
1874 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL,
1875 chip, &snd_cs4231_sbus_dev_ops)) < 0) {
1876 snd_cs4231_sbus_free(chip);
1877 return err;
1878 }
1879
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 return 0;
1881}
1882
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001883static int __init cs4231_sbus_attach(struct sbus_dev *sdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884{
1885 struct resource *rp = &sdev->resource[0];
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001886 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 int err;
1888
1889 err = cs4231_attach_begin(&card);
1890 if (err)
1891 return err;
1892
Andrew Morton5863aa62006-07-03 00:24:22 -07001893 sprintf(card->longname, "%s at 0x%02lx:0x%016Lx, irq %d",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 card->shortname,
1895 rp->flags & 0xffL,
Greg Kroah-Hartmanaa0a2dd2006-06-12 14:50:27 -07001896 (unsigned long long)rp->start,
David S. Millerc6387a42006-06-20 01:21:29 -07001897 sdev->irqs[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898
Krzysztof Heltc6c2d572007-09-04 13:08:24 +02001899 err = snd_cs4231_sbus_create(card, sdev, dev);
1900 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 snd_card_free(card);
1902 return err;
1903 }
1904
Krzysztof Heltc6c2d572007-09-04 13:08:24 +02001905 return cs4231_attach_finish(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906}
1907#endif
1908
1909#ifdef EBUS_SUPPORT
Georg Chinib1282542005-11-07 14:09:19 -08001910
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001911static void snd_cs4231_ebus_play_callback(struct ebus_dma_info *p, int event,
1912 void *cookie)
Georg Chinib1282542005-11-07 14:09:19 -08001913{
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001914 struct snd_cs4231 *chip = cookie;
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001915
Georg Chinib1282542005-11-07 14:09:19 -08001916 snd_cs4231_play_callback(chip);
1917}
1918
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001919static void snd_cs4231_ebus_capture_callback(struct ebus_dma_info *p,
1920 int event, void *cookie)
Georg Chinib1282542005-11-07 14:09:19 -08001921{
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001922 struct snd_cs4231 *chip = cookie;
Georg Chinib1282542005-11-07 14:09:19 -08001923
1924 snd_cs4231_capture_callback(chip);
1925}
1926
1927/*
1928 * EBUS DMA wrappers
1929 */
1930
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001931static int _ebus_dma_request(struct cs4231_dma_control *dma_cont,
1932 dma_addr_t bus_addr, size_t len)
Georg Chinib1282542005-11-07 14:09:19 -08001933{
1934 return ebus_dma_request(&dma_cont->ebus_info, bus_addr, len);
1935}
1936
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001937static void _ebus_dma_enable(struct cs4231_dma_control *dma_cont, int on)
Georg Chinib1282542005-11-07 14:09:19 -08001938{
1939 ebus_dma_enable(&dma_cont->ebus_info, on);
1940}
1941
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001942static void _ebus_dma_prepare(struct cs4231_dma_control *dma_cont, int dir)
Georg Chinib1282542005-11-07 14:09:19 -08001943{
1944 ebus_dma_prepare(&dma_cont->ebus_info, dir);
1945}
1946
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001947static unsigned int _ebus_dma_addr(struct cs4231_dma_control *dma_cont)
Georg Chinib1282542005-11-07 14:09:19 -08001948{
1949 return ebus_dma_addr(&dma_cont->ebus_info);
1950}
1951
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001952static void _ebus_dma_preallocate(struct snd_cs4231 *chip, struct snd_pcm *pcm)
Georg Chinib1282542005-11-07 14:09:19 -08001953{
1954 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1955 snd_dma_pci_data(chip->dev_u.pdev),
1956 64*1024, 128*1024);
1957}
1958
1959/*
1960 * Init and exit routines
1961 */
1962
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001963static int snd_cs4231_ebus_free(struct snd_cs4231 *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964{
Georg Chinib1282542005-11-07 14:09:19 -08001965 if (chip->c_dma.ebus_info.regs) {
1966 ebus_dma_unregister(&chip->c_dma.ebus_info);
1967 iounmap(chip->c_dma.ebus_info.regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 }
Georg Chinib1282542005-11-07 14:09:19 -08001969 if (chip->p_dma.ebus_info.regs) {
1970 ebus_dma_unregister(&chip->p_dma.ebus_info);
1971 iounmap(chip->p_dma.ebus_info.regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 }
1973
1974 if (chip->port)
1975 iounmap(chip->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 return 0;
1978}
1979
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001980static int snd_cs4231_ebus_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981{
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001982 struct snd_cs4231 *cp = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983
1984 return snd_cs4231_ebus_free(cp);
1985}
1986
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001987static struct snd_device_ops snd_cs4231_ebus_dev_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 .dev_free = snd_cs4231_ebus_dev_free,
1989};
1990
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001991static int __init snd_cs4231_ebus_create(struct snd_card *card,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 struct linux_ebus_device *edev,
Krzysztof Heltc6c2d572007-09-04 13:08:24 +02001993 int dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994{
Krzysztof Heltc6c2d572007-09-04 13:08:24 +02001995 struct snd_cs4231 *chip = card->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 int err;
1997
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 spin_lock_init(&chip->lock);
Georg Chinib1282542005-11-07 14:09:19 -08001999 spin_lock_init(&chip->c_dma.ebus_info.lock);
2000 spin_lock_init(&chip->p_dma.ebus_info.lock);
Ingo Molnar12aa7572006-01-16 16:36:05 +01002001 mutex_init(&chip->mce_mutex);
2002 mutex_init(&chip->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 chip->flags |= CS4231_FLAG_EBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 chip->dev_u.pdev = edev->bus->self;
2005 memcpy(&chip->image, &snd_cs4231_original_image,
2006 sizeof(snd_cs4231_original_image));
Georg Chinib1282542005-11-07 14:09:19 -08002007 strcpy(chip->c_dma.ebus_info.name, "cs4231(capture)");
2008 chip->c_dma.ebus_info.flags = EBUS_DMA_FLAG_USE_EBDMA_HANDLER;
2009 chip->c_dma.ebus_info.callback = snd_cs4231_ebus_capture_callback;
2010 chip->c_dma.ebus_info.client_cookie = chip;
2011 chip->c_dma.ebus_info.irq = edev->irqs[0];
2012 strcpy(chip->p_dma.ebus_info.name, "cs4231(play)");
2013 chip->p_dma.ebus_info.flags = EBUS_DMA_FLAG_USE_EBDMA_HANDLER;
2014 chip->p_dma.ebus_info.callback = snd_cs4231_ebus_play_callback;
2015 chip->p_dma.ebus_info.client_cookie = chip;
2016 chip->p_dma.ebus_info.irq = edev->irqs[1];
2017
2018 chip->p_dma.prepare = _ebus_dma_prepare;
2019 chip->p_dma.enable = _ebus_dma_enable;
2020 chip->p_dma.request = _ebus_dma_request;
2021 chip->p_dma.address = _ebus_dma_addr;
Georg Chinib1282542005-11-07 14:09:19 -08002022 chip->p_dma.preallocate = _ebus_dma_preallocate;
2023
2024 chip->c_dma.prepare = _ebus_dma_prepare;
2025 chip->c_dma.enable = _ebus_dma_enable;
2026 chip->c_dma.request = _ebus_dma_request;
2027 chip->c_dma.address = _ebus_dma_addr;
Georg Chinib1282542005-11-07 14:09:19 -08002028 chip->c_dma.preallocate = _ebus_dma_preallocate;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029
2030 chip->port = ioremap(edev->resource[0].start, 0x10);
Georg Chinib1282542005-11-07 14:09:19 -08002031 chip->p_dma.ebus_info.regs = ioremap(edev->resource[1].start, 0x10);
2032 chip->c_dma.ebus_info.regs = ioremap(edev->resource[2].start, 0x10);
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02002033 if (!chip->port || !chip->p_dma.ebus_info.regs ||
2034 !chip->c_dma.ebus_info.regs) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 snd_cs4231_ebus_free(chip);
Christopher Zimmermanna1314302005-09-21 00:41:22 -07002036 snd_printdd("cs4231-%d: Unable to map chip registers.\n", dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 return -EIO;
2038 }
2039
Georg Chinib1282542005-11-07 14:09:19 -08002040 if (ebus_dma_register(&chip->c_dma.ebus_info)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 snd_cs4231_ebus_free(chip);
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02002042 snd_printdd("cs4231-%d: Unable to register EBUS capture DMA\n",
2043 dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 return -EBUSY;
2045 }
Georg Chinib1282542005-11-07 14:09:19 -08002046 if (ebus_dma_irq_enable(&chip->c_dma.ebus_info, 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 snd_cs4231_ebus_free(chip);
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02002048 snd_printdd("cs4231-%d: Unable to enable EBUS capture IRQ\n",
2049 dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 return -EBUSY;
2051 }
2052
Georg Chinib1282542005-11-07 14:09:19 -08002053 if (ebus_dma_register(&chip->p_dma.ebus_info)) {
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 register EBUS play DMA\n",
2056 dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 return -EBUSY;
2058 }
Georg Chinib1282542005-11-07 14:09:19 -08002059 if (ebus_dma_irq_enable(&chip->p_dma.ebus_info, 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 snd_cs4231_ebus_free(chip);
Christopher Zimmermanna1314302005-09-21 00:41:22 -07002061 snd_printdd("cs4231-%d: Unable to enable EBUS play IRQ\n", dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 return -EBUSY;
2063 }
2064
2065 if (snd_cs4231_probe(chip) < 0) {
2066 snd_cs4231_ebus_free(chip);
2067 return -ENODEV;
2068 }
2069 snd_cs4231_init(chip);
2070
2071 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL,
2072 chip, &snd_cs4231_ebus_dev_ops)) < 0) {
2073 snd_cs4231_ebus_free(chip);
2074 return err;
2075 }
2076
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 return 0;
2078}
2079
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01002080static int __init cs4231_ebus_attach(struct linux_ebus_device *edev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081{
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01002082 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 int err;
2084
2085 err = cs4231_attach_begin(&card);
2086 if (err)
2087 return err;
2088
David S. Millerc6387a42006-06-20 01:21:29 -07002089 sprintf(card->longname, "%s at 0x%lx, irq %d",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 card->shortname,
2091 edev->resource[0].start,
David S. Millerc6387a42006-06-20 01:21:29 -07002092 edev->irqs[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093
Krzysztof Heltc6c2d572007-09-04 13:08:24 +02002094 err = snd_cs4231_ebus_create(card, edev, dev);
2095 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 snd_card_free(card);
2097 return err;
2098 }
2099
Krzysztof Heltc6c2d572007-09-04 13:08:24 +02002100 return cs4231_attach_finish(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101}
2102#endif
2103
2104static int __init cs4231_init(void)
2105{
2106#ifdef SBUS_SUPPORT
2107 struct sbus_bus *sbus;
2108 struct sbus_dev *sdev;
2109#endif
2110#ifdef EBUS_SUPPORT
2111 struct linux_ebus *ebus;
2112 struct linux_ebus_device *edev;
2113#endif
2114 int found;
2115
2116 found = 0;
2117
2118#ifdef SBUS_SUPPORT
2119 for_all_sbusdev(sdev, sbus) {
2120 if (!strcmp(sdev->prom_name, "SUNW,CS4231")) {
2121 if (cs4231_sbus_attach(sdev) == 0)
2122 found++;
2123 }
2124 }
2125#endif
2126#ifdef EBUS_SUPPORT
2127 for_each_ebus(ebus) {
2128 for_each_ebusdev(edev, ebus) {
2129 int match = 0;
2130
David S. Miller690c8fd2006-06-22 19:12:03 -07002131 if (!strcmp(edev->prom_node->name, "SUNW,CS4231")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 match = 1;
David S. Miller690c8fd2006-06-22 19:12:03 -07002133 } else if (!strcmp(edev->prom_node->name, "audio")) {
Stephen Rothwell31985142007-03-29 00:50:57 -07002134 const char *compat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135
David S. Miller690c8fd2006-06-22 19:12:03 -07002136 compat = of_get_property(edev->prom_node,
2137 "compatible", NULL);
2138 if (compat && !strcmp(compat, "SUNW,CS4231"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 match = 1;
2140 }
2141
2142 if (match &&
2143 cs4231_ebus_attach(edev) == 0)
2144 found++;
2145 }
2146 }
2147#endif
2148
2149
2150 return (found > 0) ? 0 : -EIO;
2151}
2152
2153static void __exit cs4231_exit(void)
2154{
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01002155 struct snd_cs4231 *p = cs4231_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156
2157 while (p != NULL) {
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01002158 struct snd_cs4231 *next = p->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159
2160 snd_card_free(p->card);
2161
2162 p = next;
2163 }
2164
2165 cs4231_list = NULL;
2166}
2167
2168module_init(cs4231_init);
2169module_exit(cs4231_exit);