blob: 8d13d933087dee455186d568e2ba4364988225ba [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Driver for CS4231 sound chips found on Sparcs.
David S. Millerae251032008-08-27 18:24:01 -07003 * Copyright (C) 2002, 2008 David S. Miller <davem@davemloft.net>
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
5 * Based entirely upon drivers/sbus/audio/cs4231.c which is:
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02006 * Copyright (C) 1996, 1997, 1998 Derrick J Brashear (shadow@andrew.cmu.edu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * and also sound/isa/cs423x/cs4231_lib.c which is:
Jaroslav Kyselac1017a42007-10-15 09:50:19 +02008 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 */
10
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/module.h>
12#include <linux/kernel.h>
13#include <linux/slab.h>
14#include <linux/delay.h>
15#include <linux/init.h>
16#include <linux/interrupt.h>
17#include <linux/moduleparam.h>
Krzysztof Helt9e9abb42007-09-10 23:06:55 +020018#include <linux/irq.h>
19#include <linux/io.h>
David S. Millerae251032008-08-27 18:24:01 -070020#include <linux/of.h>
21#include <linux/of_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <sound/core.h>
24#include <sound/pcm.h>
25#include <sound/info.h>
26#include <sound/control.h>
27#include <sound/timer.h>
28#include <sound/initval.h>
29#include <sound/pcm_params.h>
30
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#ifdef CONFIG_SBUS
32#define SBUS_SUPPORT
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#endif
34
35#if defined(CONFIG_PCI) && defined(CONFIG_SPARC64)
36#define EBUS_SUPPORT
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/pci.h>
David S. Milleraae7fb82008-08-29 23:10:21 -070038#include <asm/ebus_dma.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#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);
Georg Chinib1282542005-11-07 14:09:19 -080073#ifdef EBUS_SUPPORT
74 struct ebus_dma_info ebus_info;
75#endif
76#ifdef SBUS_SUPPORT
77 struct sbus_dma_info sbus_info;
78#endif
Takashi Iwaibe9b7e82006-01-03 13:42:38 +010079};
Georg Chinib1282542005-11-07 14:09:19 -080080
81struct snd_cs4231 {
Krzysztof Helt9e9abb42007-09-10 23:06:55 +020082 spinlock_t lock; /* registers access lock */
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 void __iomem *port;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
Takashi Iwaibe9b7e82006-01-03 13:42:38 +010085 struct cs4231_dma_control p_dma;
86 struct cs4231_dma_control c_dma;
Georg Chini5a820fa2005-11-07 14:08:25 -080087
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 u32 flags;
89#define CS4231_FLAG_EBUS 0x00000001
90#define CS4231_FLAG_PLAYBACK 0x00000002
91#define CS4231_FLAG_CAPTURE 0x00000004
92
Takashi Iwaibe9b7e82006-01-03 13:42:38 +010093 struct snd_card *card;
94 struct snd_pcm *pcm;
95 struct snd_pcm_substream *playback_substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 unsigned int p_periods_sent;
Takashi Iwaibe9b7e82006-01-03 13:42:38 +010097 struct snd_pcm_substream *capture_substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 unsigned int c_periods_sent;
Takashi Iwaibe9b7e82006-01-03 13:42:38 +010099 struct snd_timer *timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
101 unsigned short mode;
102#define CS4231_MODE_NONE 0x0000
103#define CS4231_MODE_PLAY 0x0001
104#define CS4231_MODE_RECORD 0x0002
105#define CS4231_MODE_TIMER 0x0004
Krzysztof Helt9e9abb42007-09-10 23:06:55 +0200106#define CS4231_MODE_OPEN (CS4231_MODE_PLAY | CS4231_MODE_RECORD | \
107 CS4231_MODE_TIMER)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
109 unsigned char image[32]; /* registers image */
110 int mce_bit;
111 int calibrate_mute;
Krzysztof Helt9e9abb42007-09-10 23:06:55 +0200112 struct mutex mce_mutex; /* mutex for mce register */
113 struct mutex open_mutex; /* mutex for ALSA open/close */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
David S. Millerafc88ad2008-08-30 00:13:55 -0700115 struct of_device *op;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 unsigned int irq[2];
117 unsigned int regs_size;
118 struct snd_cs4231 *next;
Georg Chinib1282542005-11-07 14:09:19 -0800119};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121/* Eventually we can use sound/isa/cs423x/cs4231_lib.c directly, but for
122 * now.... -DaveM
123 */
124
125/* IO ports */
Krzysztof Helt7e52f3d2007-09-05 15:08:23 +0200126#include <sound/cs4231-regs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
128/* XXX offsets are different than PC ISA chips... */
Krzysztof Helt7e52f3d2007-09-05 15:08:23 +0200129#define CS4231U(chip, x) ((chip)->port + ((c_d_c_CS4231##x) << 2))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
131/* SBUS DMA register defines. */
132
133#define APCCSR 0x10UL /* APC DMA CSR */
134#define APCCVA 0x20UL /* APC Capture DMA Address */
135#define APCCC 0x24UL /* APC Capture Count */
136#define APCCNVA 0x28UL /* APC Capture DMA Next Address */
137#define APCCNC 0x2cUL /* APC Capture Next Count */
138#define APCPVA 0x30UL /* APC Play DMA Address */
139#define APCPC 0x34UL /* APC Play Count */
140#define APCPNVA 0x38UL /* APC Play DMA Next Address */
141#define APCPNC 0x3cUL /* APC Play Next Count */
142
Georg Chini5a820fa2005-11-07 14:08:25 -0800143/* Defines for SBUS DMA-routines */
144
145#define APCVA 0x0UL /* APC DMA Address */
146#define APCC 0x4UL /* APC Count */
147#define APCNVA 0x8UL /* APC DMA Next Address */
148#define APCNC 0xcUL /* APC Next Count */
149#define APC_PLAY 0x30UL /* Play registers start at 0x30 */
150#define APC_RECORD 0x20UL /* Record registers start at 0x20 */
151
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152/* APCCSR bits */
153
154#define APC_INT_PENDING 0x800000 /* Interrupt Pending */
155#define APC_PLAY_INT 0x400000 /* Playback interrupt */
156#define APC_CAPT_INT 0x200000 /* Capture interrupt */
157#define APC_GENL_INT 0x100000 /* General interrupt */
158#define APC_XINT_ENA 0x80000 /* General ext int. enable */
159#define APC_XINT_PLAY 0x40000 /* Playback ext intr */
160#define APC_XINT_CAPT 0x20000 /* Capture ext intr */
161#define APC_XINT_GENL 0x10000 /* Error ext intr */
162#define APC_XINT_EMPT 0x8000 /* Pipe empty interrupt (0 write to pva) */
163#define APC_XINT_PEMP 0x4000 /* Play pipe empty (pva and pnva not set) */
164#define APC_XINT_PNVA 0x2000 /* Playback NVA dirty */
165#define APC_XINT_PENA 0x1000 /* play pipe empty Int enable */
166#define APC_XINT_COVF 0x800 /* Cap data dropped on floor */
167#define APC_XINT_CNVA 0x400 /* Capture NVA dirty */
168#define APC_XINT_CEMP 0x200 /* Capture pipe empty (cva and cnva not set) */
169#define APC_XINT_CENA 0x100 /* Cap. pipe empty int enable */
170#define APC_PPAUSE 0x80 /* Pause the play DMA */
171#define APC_CPAUSE 0x40 /* Pause the capture DMA */
172#define APC_CDC_RESET 0x20 /* CODEC RESET */
173#define APC_PDMA_READY 0x08 /* Play DMA Go */
174#define APC_CDMA_READY 0x04 /* Capture DMA Go */
175#define APC_CHIP_RESET 0x01 /* Reset the chip */
176
177/* EBUS DMA register offsets */
178
179#define EBDMA_CSR 0x00UL /* Control/Status */
180#define EBDMA_ADDR 0x04UL /* DMA Address */
181#define EBDMA_COUNT 0x08UL /* DMA Count */
182
183/*
184 * Some variables
185 */
186
187static unsigned char freq_bits[14] = {
188 /* 5510 */ 0x00 | CS4231_XTAL2,
189 /* 6620 */ 0x0E | CS4231_XTAL2,
190 /* 8000 */ 0x00 | CS4231_XTAL1,
191 /* 9600 */ 0x0E | CS4231_XTAL1,
192 /* 11025 */ 0x02 | CS4231_XTAL2,
193 /* 16000 */ 0x02 | CS4231_XTAL1,
194 /* 18900 */ 0x04 | CS4231_XTAL2,
195 /* 22050 */ 0x06 | CS4231_XTAL2,
196 /* 27042 */ 0x04 | CS4231_XTAL1,
197 /* 32000 */ 0x06 | CS4231_XTAL1,
198 /* 33075 */ 0x0C | CS4231_XTAL2,
199 /* 37800 */ 0x08 | CS4231_XTAL2,
200 /* 44100 */ 0x0A | CS4231_XTAL2,
201 /* 48000 */ 0x0C | CS4231_XTAL1
202};
203
204static unsigned int rates[14] = {
205 5510, 6620, 8000, 9600, 11025, 16000, 18900, 22050,
206 27042, 32000, 33075, 37800, 44100, 48000
207};
208
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100209static struct snd_pcm_hw_constraint_list hw_constraints_rates = {
Krzysztof Heltc6c2d572007-09-04 13:08:24 +0200210 .count = ARRAY_SIZE(rates),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 .list = rates,
212};
213
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100214static int snd_cs4231_xrate(struct snd_pcm_runtime *runtime)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215{
216 return snd_pcm_hw_constraint_list(runtime, 0,
217 SNDRV_PCM_HW_PARAM_RATE,
218 &hw_constraints_rates);
219}
220
221static unsigned char snd_cs4231_original_image[32] =
222{
223 0x00, /* 00/00 - lic */
224 0x00, /* 01/01 - ric */
225 0x9f, /* 02/02 - la1ic */
226 0x9f, /* 03/03 - ra1ic */
227 0x9f, /* 04/04 - la2ic */
228 0x9f, /* 05/05 - ra2ic */
229 0xbf, /* 06/06 - loc */
230 0xbf, /* 07/07 - roc */
231 0x20, /* 08/08 - pdfr */
232 CS4231_AUTOCALIB, /* 09/09 - ic */
233 0x00, /* 0a/10 - pc */
234 0x00, /* 0b/11 - ti */
235 CS4231_MODE2, /* 0c/12 - mi */
236 0x00, /* 0d/13 - lbc */
237 0x00, /* 0e/14 - pbru */
238 0x00, /* 0f/15 - pbrl */
239 0x80, /* 10/16 - afei */
240 0x01, /* 11/17 - afeii */
241 0x9f, /* 12/18 - llic */
242 0x9f, /* 13/19 - rlic */
243 0x00, /* 14/20 - tlb */
244 0x00, /* 15/21 - thb */
245 0x00, /* 16/22 - la3mic/reserved */
246 0x00, /* 17/23 - ra3mic/reserved */
247 0x00, /* 18/24 - afs */
248 0x00, /* 19/25 - lamoc/version */
249 0x00, /* 1a/26 - mioc */
250 0x00, /* 1b/27 - ramoc/reserved */
251 0x20, /* 1c/28 - cdfr */
252 0x00, /* 1d/29 - res4 */
253 0x00, /* 1e/30 - cbru */
254 0x00, /* 1f/31 - cbrl */
255};
256
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100257static u8 __cs4231_readb(struct snd_cs4231 *cp, void __iomem *reg_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258{
Krzysztof Heltc6c2d572007-09-04 13:08:24 +0200259 if (cp->flags & CS4231_FLAG_EBUS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 return readb(reg_addr);
Krzysztof Heltc6c2d572007-09-04 13:08:24 +0200261 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 return sbus_readb(reg_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263}
264
Krzysztof Helt9e9abb42007-09-10 23:06:55 +0200265static void __cs4231_writeb(struct snd_cs4231 *cp, u8 val,
266 void __iomem *reg_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267{
Krzysztof Heltc6c2d572007-09-04 13:08:24 +0200268 if (cp->flags & CS4231_FLAG_EBUS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 return writeb(val, reg_addr);
Krzysztof Heltc6c2d572007-09-04 13:08:24 +0200270 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 return sbus_writeb(val, reg_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272}
273
274/*
275 * Basic I/O functions
276 */
277
Krzysztof Heltc6c2d572007-09-04 13:08:24 +0200278static void snd_cs4231_ready(struct snd_cs4231 *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279{
280 int timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
Krzysztof Helt7e52f3d2007-09-05 15:08:23 +0200282 for (timeout = 250; timeout > 0; timeout--) {
283 int val = __cs4231_readb(chip, CS4231U(chip, REGSEL));
284 if ((val & CS4231_INIT) == 0)
285 break;
286 udelay(100);
287 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288}
289
Krzysztof Helt9e9abb42007-09-10 23:06:55 +0200290static void snd_cs4231_dout(struct snd_cs4231 *chip, unsigned char reg,
291 unsigned char value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292{
Krzysztof Heltc6c2d572007-09-04 13:08:24 +0200293 snd_cs4231_ready(chip);
Christopher Zimmermanna1314302005-09-21 00:41:22 -0700294#ifdef CONFIG_SND_DEBUG
Krzysztof Helt7e52f3d2007-09-05 15:08:23 +0200295 if (__cs4231_readb(chip, CS4231U(chip, REGSEL)) & CS4231_INIT)
Krzysztof Heltc6c2d572007-09-04 13:08:24 +0200296 snd_printdd("out: auto calibration time out - reg = 0x%x, "
297 "value = 0x%x\n",
298 reg, value);
Christopher Zimmermanna1314302005-09-21 00:41:22 -0700299#endif
Krzysztof Helt7e52f3d2007-09-05 15:08:23 +0200300 __cs4231_writeb(chip, chip->mce_bit | reg, CS4231U(chip, REGSEL));
Krzysztof Heltc6c2d572007-09-04 13:08:24 +0200301 wmb();
Krzysztof Helt7e52f3d2007-09-05 15:08:23 +0200302 __cs4231_writeb(chip, value, CS4231U(chip, REG));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 mb();
304}
305
Krzysztof Heltc6c2d572007-09-04 13:08:24 +0200306static inline void snd_cs4231_outm(struct snd_cs4231 *chip, unsigned char reg,
307 unsigned char mask, unsigned char value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308{
Krzysztof Heltc6c2d572007-09-04 13:08:24 +0200309 unsigned char tmp = (chip->image[reg] & mask) | value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
Krzysztof Heltc6c2d572007-09-04 13:08:24 +0200311 chip->image[reg] = tmp;
312 if (!chip->calibrate_mute)
313 snd_cs4231_dout(chip, reg, tmp);
314}
315
316static void snd_cs4231_out(struct snd_cs4231 *chip, unsigned char reg,
317 unsigned char value)
318{
319 snd_cs4231_dout(chip, reg, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 chip->image[reg] = value;
321 mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322}
323
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100324static unsigned char snd_cs4231_in(struct snd_cs4231 *chip, unsigned char reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325{
Krzysztof Heltc6c2d572007-09-04 13:08:24 +0200326 snd_cs4231_ready(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327#ifdef CONFIG_SND_DEBUG
Krzysztof Helt7e52f3d2007-09-05 15:08:23 +0200328 if (__cs4231_readb(chip, CS4231U(chip, REGSEL)) & CS4231_INIT)
Krzysztof Heltc6c2d572007-09-04 13:08:24 +0200329 snd_printdd("in: auto calibration time out - reg = 0x%x\n",
330 reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331#endif
Krzysztof Helt7e52f3d2007-09-05 15:08:23 +0200332 __cs4231_writeb(chip, chip->mce_bit | reg, CS4231U(chip, REGSEL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 mb();
Krzysztof Helt7e52f3d2007-09-05 15:08:23 +0200334 return __cs4231_readb(chip, CS4231U(chip, REG));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335}
336
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337/*
338 * CS4231 detection / MCE routines
339 */
340
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100341static void snd_cs4231_busy_wait(struct snd_cs4231 *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342{
343 int timeout;
344
Krzysztof Helt9e9abb42007-09-10 23:06:55 +0200345 /* looks like this sequence is proper for CS4231A chip (GUS MAX) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 for (timeout = 5; timeout > 0; timeout--)
Krzysztof Helt7e52f3d2007-09-05 15:08:23 +0200347 __cs4231_readb(chip, CS4231U(chip, REGSEL));
Christopher Zimmermanna1314302005-09-21 00:41:22 -0700348
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 /* end of cleanup sequence */
Krzysztof Helt7e52f3d2007-09-05 15:08:23 +0200350 for (timeout = 500; timeout > 0; timeout--) {
351 int val = __cs4231_readb(chip, CS4231U(chip, REGSEL));
352 if ((val & CS4231_INIT) == 0)
353 break;
Krzysztof Heltc6c2d572007-09-04 13:08:24 +0200354 msleep(1);
Krzysztof Helt7e52f3d2007-09-05 15:08:23 +0200355 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356}
357
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100358static void snd_cs4231_mce_up(struct snd_cs4231 *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359{
360 unsigned long flags;
361 int timeout;
362
363 spin_lock_irqsave(&chip->lock, flags);
Krzysztof Heltc6c2d572007-09-04 13:08:24 +0200364 snd_cs4231_ready(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365#ifdef CONFIG_SND_DEBUG
Krzysztof Helt7e52f3d2007-09-05 15:08:23 +0200366 if (__cs4231_readb(chip, CS4231U(chip, REGSEL)) & CS4231_INIT)
Christopher Zimmermanna1314302005-09-21 00:41:22 -0700367 snd_printdd("mce_up - auto calibration time out (0)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368#endif
369 chip->mce_bit |= CS4231_MCE;
Krzysztof Helt7e52f3d2007-09-05 15:08:23 +0200370 timeout = __cs4231_readb(chip, CS4231U(chip, REGSEL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 if (timeout == 0x80)
Krzysztof Helt9e9abb42007-09-10 23:06:55 +0200372 snd_printdd("mce_up [%p]: serious init problem - "
373 "codec still busy\n",
374 chip->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 if (!(timeout & CS4231_MCE))
Krzysztof Helt7e52f3d2007-09-05 15:08:23 +0200376 __cs4231_writeb(chip, chip->mce_bit | (timeout & 0x1f),
377 CS4231U(chip, REGSEL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 spin_unlock_irqrestore(&chip->lock, flags);
379}
380
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100381static void snd_cs4231_mce_down(struct snd_cs4231 *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382{
Krzysztof Helt9823adf2007-10-16 14:54:58 +0200383 unsigned long flags, timeout;
384 int reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 snd_cs4231_busy_wait(chip);
Krzysztof Helt9823adf2007-10-16 14:54:58 +0200387 spin_lock_irqsave(&chip->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388#ifdef CONFIG_SND_DEBUG
Krzysztof Helt7e52f3d2007-09-05 15:08:23 +0200389 if (__cs4231_readb(chip, CS4231U(chip, REGSEL)) & CS4231_INIT)
390 snd_printdd("mce_down [%p] - auto calibration time out (0)\n",
391 CS4231U(chip, REGSEL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392#endif
393 chip->mce_bit &= ~CS4231_MCE;
Krzysztof Helt9823adf2007-10-16 14:54:58 +0200394 reg = __cs4231_readb(chip, CS4231U(chip, REGSEL));
395 __cs4231_writeb(chip, chip->mce_bit | (reg & 0x1f),
Krzysztof Helt7e52f3d2007-09-05 15:08:23 +0200396 CS4231U(chip, REGSEL));
Krzysztof Helt9823adf2007-10-16 14:54:58 +0200397 if (reg == 0x80)
398 snd_printdd("mce_down [%p]: serious init problem "
399 "- codec still busy\n", chip->port);
400 if ((reg & CS4231_MCE) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 spin_unlock_irqrestore(&chip->lock, flags);
402 return;
403 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
Krzysztof Helt56f91582007-09-11 00:55:46 +0200405 /*
Krzysztof Helt9823adf2007-10-16 14:54:58 +0200406 * Wait for auto-calibration (AC) process to finish, i.e. ACI to go low.
Krzysztof Helt56f91582007-09-11 00:55:46 +0200407 */
Krzysztof Helt9823adf2007-10-16 14:54:58 +0200408 timeout = jiffies + msecs_to_jiffies(250);
409 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 spin_unlock_irqrestore(&chip->lock, flags);
Takashi Iwaib875d652007-09-11 10:12:14 +0200411 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 spin_lock_irqsave(&chip->lock, flags);
Krzysztof Helt9823adf2007-10-16 14:54:58 +0200413 reg = snd_cs4231_in(chip, CS4231_TEST_INIT);
414 reg &= CS4231_CALIB_IN_PROGRESS;
415 } while (reg && time_before(jiffies, timeout));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 spin_unlock_irqrestore(&chip->lock, flags);
Krzysztof Helt9823adf2007-10-16 14:54:58 +0200417
418 if (reg)
419 snd_printk(KERN_ERR
420 "mce_down - auto calibration time out (2)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421}
422
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100423static void snd_cs4231_advance_dma(struct cs4231_dma_control *dma_cont,
424 struct snd_pcm_substream *substream,
425 unsigned int *periods_sent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426{
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100427 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
429 while (1) {
Christopher Zimmermanna1314302005-09-21 00:41:22 -0700430 unsigned int period_size = snd_pcm_lib_period_bytes(substream);
431 unsigned int offset = period_size * (*periods_sent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
Eric Sesterhenn817dd6e2006-03-24 18:49:12 +0100433 BUG_ON(period_size >= (1 << 24));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
Krzysztof Helt9e9abb42007-09-10 23:06:55 +0200435 if (dma_cont->request(dma_cont,
436 runtime->dma_addr + offset, period_size))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 (*periods_sent) = ((*periods_sent) + 1) % runtime->periods;
439 }
440}
Christopher Zimmermanna1314302005-09-21 00:41:22 -0700441
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100442static void cs4231_dma_trigger(struct snd_pcm_substream *substream,
443 unsigned int what, int on)
Christopher Zimmermanna1314302005-09-21 00:41:22 -0700444{
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100445 struct snd_cs4231 *chip = snd_pcm_substream_chip(substream);
446 struct cs4231_dma_control *dma_cont;
Christopher Zimmermanna1314302005-09-21 00:41:22 -0700447
Georg Chini5a820fa2005-11-07 14:08:25 -0800448 if (what & CS4231_PLAYBACK_ENABLE) {
Georg Chinib1282542005-11-07 14:09:19 -0800449 dma_cont = &chip->p_dma;
Christopher Zimmermanna1314302005-09-21 00:41:22 -0700450 if (on) {
Georg Chinib1282542005-11-07 14:09:19 -0800451 dma_cont->prepare(dma_cont, 0);
452 dma_cont->enable(dma_cont, 1);
453 snd_cs4231_advance_dma(dma_cont,
Georg Chini5a820fa2005-11-07 14:08:25 -0800454 chip->playback_substream,
455 &chip->p_periods_sent);
Christopher Zimmermanna1314302005-09-21 00:41:22 -0700456 } else {
Georg Chinib1282542005-11-07 14:09:19 -0800457 dma_cont->enable(dma_cont, 0);
Christopher Zimmermanna1314302005-09-21 00:41:22 -0700458 }
Georg Chini5a820fa2005-11-07 14:08:25 -0800459 }
460 if (what & CS4231_RECORD_ENABLE) {
Georg Chinib1282542005-11-07 14:09:19 -0800461 dma_cont = &chip->c_dma;
Christopher Zimmermanna1314302005-09-21 00:41:22 -0700462 if (on) {
Georg Chinib1282542005-11-07 14:09:19 -0800463 dma_cont->prepare(dma_cont, 1);
464 dma_cont->enable(dma_cont, 1);
465 snd_cs4231_advance_dma(dma_cont,
Georg Chini5a820fa2005-11-07 14:08:25 -0800466 chip->capture_substream,
467 &chip->c_periods_sent);
Christopher Zimmermanna1314302005-09-21 00:41:22 -0700468 } else {
Georg Chinib1282542005-11-07 14:09:19 -0800469 dma_cont->enable(dma_cont, 0);
Christopher Zimmermanna1314302005-09-21 00:41:22 -0700470 }
Christopher Zimmermanna1314302005-09-21 00:41:22 -0700471 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472}
473
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100474static int snd_cs4231_trigger(struct snd_pcm_substream *substream, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475{
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100476 struct snd_cs4231 *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 int result = 0;
478
479 switch (cmd) {
480 case SNDRV_PCM_TRIGGER_START:
481 case SNDRV_PCM_TRIGGER_STOP:
482 {
483 unsigned int what = 0;
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100484 struct snd_pcm_substream *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 unsigned long flags;
486
Takashi Iwaief991b92007-02-22 12:52:53 +0100487 snd_pcm_group_for_each_entry(s, substream) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 if (s == chip->playback_substream) {
489 what |= CS4231_PLAYBACK_ENABLE;
490 snd_pcm_trigger_done(s, substream);
491 } else if (s == chip->capture_substream) {
492 what |= CS4231_RECORD_ENABLE;
493 snd_pcm_trigger_done(s, substream);
494 }
495 }
496
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 spin_lock_irqsave(&chip->lock, flags);
498 if (cmd == SNDRV_PCM_TRIGGER_START) {
Christopher Zimmermanna1314302005-09-21 00:41:22 -0700499 cs4231_dma_trigger(substream, what, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 chip->image[CS4231_IFACE_CTRL] |= what;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 } else {
Christopher Zimmermanna1314302005-09-21 00:41:22 -0700502 cs4231_dma_trigger(substream, what, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 chip->image[CS4231_IFACE_CTRL] &= ~what;
504 }
505 snd_cs4231_out(chip, CS4231_IFACE_CTRL,
506 chip->image[CS4231_IFACE_CTRL]);
507 spin_unlock_irqrestore(&chip->lock, flags);
508 break;
509 }
510 default:
511 result = -EINVAL;
512 break;
513 }
Christopher Zimmermanna1314302005-09-21 00:41:22 -0700514
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 return result;
516}
517
518/*
519 * CODEC I/O
520 */
521
522static unsigned char snd_cs4231_get_rate(unsigned int rate)
523{
524 int i;
525
526 for (i = 0; i < 14; i++)
527 if (rate == rates[i])
528 return freq_bits[i];
Krzysztof Helt9e9abb42007-09-10 23:06:55 +0200529
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 return freq_bits[13];
531}
532
Krzysztof Helt9e9abb42007-09-10 23:06:55 +0200533static unsigned char snd_cs4231_get_format(struct snd_cs4231 *chip, int format,
534 int channels)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535{
536 unsigned char rformat;
537
538 rformat = CS4231_LINEAR_8;
539 switch (format) {
Krzysztof Helt9e9abb42007-09-10 23:06:55 +0200540 case SNDRV_PCM_FORMAT_MU_LAW:
541 rformat = CS4231_ULAW_8;
542 break;
543 case SNDRV_PCM_FORMAT_A_LAW:
544 rformat = CS4231_ALAW_8;
545 break;
546 case SNDRV_PCM_FORMAT_S16_LE:
547 rformat = CS4231_LINEAR_16;
548 break;
549 case SNDRV_PCM_FORMAT_S16_BE:
550 rformat = CS4231_LINEAR_16_BIG;
551 break;
552 case SNDRV_PCM_FORMAT_IMA_ADPCM:
553 rformat = CS4231_ADPCM_16;
554 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 }
556 if (channels > 1)
557 rformat |= CS4231_STEREO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 return rformat;
559}
560
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100561static void snd_cs4231_calibrate_mute(struct snd_cs4231 *chip, int mute)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562{
563 unsigned long flags;
564
565 mute = mute ? 1 : 0;
566 spin_lock_irqsave(&chip->lock, flags);
567 if (chip->calibrate_mute == mute) {
568 spin_unlock_irqrestore(&chip->lock, flags);
569 return;
570 }
571 if (!mute) {
572 snd_cs4231_dout(chip, CS4231_LEFT_INPUT,
573 chip->image[CS4231_LEFT_INPUT]);
574 snd_cs4231_dout(chip, CS4231_RIGHT_INPUT,
575 chip->image[CS4231_RIGHT_INPUT]);
576 snd_cs4231_dout(chip, CS4231_LOOPBACK,
577 chip->image[CS4231_LOOPBACK]);
578 }
579 snd_cs4231_dout(chip, CS4231_AUX1_LEFT_INPUT,
580 mute ? 0x80 : chip->image[CS4231_AUX1_LEFT_INPUT]);
581 snd_cs4231_dout(chip, CS4231_AUX1_RIGHT_INPUT,
582 mute ? 0x80 : chip->image[CS4231_AUX1_RIGHT_INPUT]);
583 snd_cs4231_dout(chip, CS4231_AUX2_LEFT_INPUT,
584 mute ? 0x80 : chip->image[CS4231_AUX2_LEFT_INPUT]);
585 snd_cs4231_dout(chip, CS4231_AUX2_RIGHT_INPUT,
586 mute ? 0x80 : chip->image[CS4231_AUX2_RIGHT_INPUT]);
587 snd_cs4231_dout(chip, CS4231_LEFT_OUTPUT,
588 mute ? 0x80 : chip->image[CS4231_LEFT_OUTPUT]);
589 snd_cs4231_dout(chip, CS4231_RIGHT_OUTPUT,
590 mute ? 0x80 : chip->image[CS4231_RIGHT_OUTPUT]);
591 snd_cs4231_dout(chip, CS4231_LEFT_LINE_IN,
592 mute ? 0x80 : chip->image[CS4231_LEFT_LINE_IN]);
593 snd_cs4231_dout(chip, CS4231_RIGHT_LINE_IN,
594 mute ? 0x80 : chip->image[CS4231_RIGHT_LINE_IN]);
595 snd_cs4231_dout(chip, CS4231_MONO_CTRL,
596 mute ? 0xc0 : chip->image[CS4231_MONO_CTRL]);
597 chip->calibrate_mute = mute;
598 spin_unlock_irqrestore(&chip->lock, flags);
599}
600
Krzysztof Helt9e9abb42007-09-10 23:06:55 +0200601static void snd_cs4231_playback_format(struct snd_cs4231 *chip,
602 struct snd_pcm_hw_params *params,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 unsigned char pdfr)
604{
605 unsigned long flags;
606
Ingo Molnar12aa7572006-01-16 16:36:05 +0100607 mutex_lock(&chip->mce_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 snd_cs4231_calibrate_mute(chip, 1);
609
610 snd_cs4231_mce_up(chip);
611
612 spin_lock_irqsave(&chip->lock, flags);
613 snd_cs4231_out(chip, CS4231_PLAYBK_FORMAT,
614 (chip->image[CS4231_IFACE_CTRL] & CS4231_RECORD_ENABLE) ?
615 (pdfr & 0xf0) | (chip->image[CS4231_REC_FORMAT] & 0x0f) :
616 pdfr);
617 spin_unlock_irqrestore(&chip->lock, flags);
618
619 snd_cs4231_mce_down(chip);
620
621 snd_cs4231_calibrate_mute(chip, 0);
Ingo Molnar12aa7572006-01-16 16:36:05 +0100622 mutex_unlock(&chip->mce_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623}
624
Krzysztof Helt9e9abb42007-09-10 23:06:55 +0200625static void snd_cs4231_capture_format(struct snd_cs4231 *chip,
626 struct snd_pcm_hw_params *params,
627 unsigned char cdfr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628{
629 unsigned long flags;
630
Ingo Molnar12aa7572006-01-16 16:36:05 +0100631 mutex_lock(&chip->mce_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 snd_cs4231_calibrate_mute(chip, 1);
633
634 snd_cs4231_mce_up(chip);
635
636 spin_lock_irqsave(&chip->lock, flags);
637 if (!(chip->image[CS4231_IFACE_CTRL] & CS4231_PLAYBACK_ENABLE)) {
638 snd_cs4231_out(chip, CS4231_PLAYBK_FORMAT,
639 ((chip->image[CS4231_PLAYBK_FORMAT]) & 0xf0) |
640 (cdfr & 0x0f));
641 spin_unlock_irqrestore(&chip->lock, flags);
642 snd_cs4231_mce_down(chip);
643 snd_cs4231_mce_up(chip);
644 spin_lock_irqsave(&chip->lock, flags);
645 }
646 snd_cs4231_out(chip, CS4231_REC_FORMAT, cdfr);
647 spin_unlock_irqrestore(&chip->lock, flags);
648
649 snd_cs4231_mce_down(chip);
650
651 snd_cs4231_calibrate_mute(chip, 0);
Ingo Molnar12aa7572006-01-16 16:36:05 +0100652 mutex_unlock(&chip->mce_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653}
654
655/*
656 * Timer interface
657 */
658
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100659static unsigned long snd_cs4231_timer_resolution(struct snd_timer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660{
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100661 struct snd_cs4231 *chip = snd_timer_chip(timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662
663 return chip->image[CS4231_PLAYBK_FORMAT] & 1 ? 9969 : 9920;
664}
665
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100666static int snd_cs4231_timer_start(struct snd_timer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667{
668 unsigned long flags;
669 unsigned int ticks;
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100670 struct snd_cs4231 *chip = snd_timer_chip(timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671
672 spin_lock_irqsave(&chip->lock, flags);
673 ticks = timer->sticks;
674 if ((chip->image[CS4231_ALT_FEATURE_1] & CS4231_TIMER_ENABLE) == 0 ||
675 (unsigned char)(ticks >> 8) != chip->image[CS4231_TIMER_HIGH] ||
676 (unsigned char)ticks != chip->image[CS4231_TIMER_LOW]) {
677 snd_cs4231_out(chip, CS4231_TIMER_HIGH,
678 chip->image[CS4231_TIMER_HIGH] =
679 (unsigned char) (ticks >> 8));
680 snd_cs4231_out(chip, CS4231_TIMER_LOW,
681 chip->image[CS4231_TIMER_LOW] =
682 (unsigned char) ticks);
683 snd_cs4231_out(chip, CS4231_ALT_FEATURE_1,
Krzysztof Helt9e9abb42007-09-10 23:06:55 +0200684 chip->image[CS4231_ALT_FEATURE_1] |
685 CS4231_TIMER_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 }
687 spin_unlock_irqrestore(&chip->lock, flags);
688
689 return 0;
690}
691
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100692static int snd_cs4231_timer_stop(struct snd_timer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693{
694 unsigned long flags;
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100695 struct snd_cs4231 *chip = snd_timer_chip(timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
697 spin_lock_irqsave(&chip->lock, flags);
Krzysztof Helt9e9abb42007-09-10 23:06:55 +0200698 chip->image[CS4231_ALT_FEATURE_1] &= ~CS4231_TIMER_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 snd_cs4231_out(chip, CS4231_ALT_FEATURE_1,
Krzysztof Helt9e9abb42007-09-10 23:06:55 +0200700 chip->image[CS4231_ALT_FEATURE_1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 spin_unlock_irqrestore(&chip->lock, flags);
702
703 return 0;
704}
705
David S. Miller78162382009-04-07 00:45:51 -0700706static void __devinit snd_cs4231_init(struct snd_cs4231 *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707{
708 unsigned long flags;
709
710 snd_cs4231_mce_down(chip);
711
712#ifdef SNDRV_DEBUG_MCE
Christopher Zimmermanna1314302005-09-21 00:41:22 -0700713 snd_printdd("init: (1)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714#endif
715 snd_cs4231_mce_up(chip);
716 spin_lock_irqsave(&chip->lock, flags);
Krzysztof Helt9e9abb42007-09-10 23:06:55 +0200717 chip->image[CS4231_IFACE_CTRL] &= ~(CS4231_PLAYBACK_ENABLE |
718 CS4231_PLAYBACK_PIO |
719 CS4231_RECORD_ENABLE |
720 CS4231_RECORD_PIO |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 CS4231_CALIB_MODE);
722 chip->image[CS4231_IFACE_CTRL] |= CS4231_AUTOCALIB;
723 snd_cs4231_out(chip, CS4231_IFACE_CTRL, chip->image[CS4231_IFACE_CTRL]);
724 spin_unlock_irqrestore(&chip->lock, flags);
725 snd_cs4231_mce_down(chip);
726
727#ifdef SNDRV_DEBUG_MCE
Christopher Zimmermanna1314302005-09-21 00:41:22 -0700728 snd_printdd("init: (2)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729#endif
730
731 snd_cs4231_mce_up(chip);
732 spin_lock_irqsave(&chip->lock, flags);
Krzysztof Helt9e9abb42007-09-10 23:06:55 +0200733 snd_cs4231_out(chip, CS4231_ALT_FEATURE_1,
734 chip->image[CS4231_ALT_FEATURE_1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 spin_unlock_irqrestore(&chip->lock, flags);
736 snd_cs4231_mce_down(chip);
737
738#ifdef SNDRV_DEBUG_MCE
Krzysztof Helt9e9abb42007-09-10 23:06:55 +0200739 snd_printdd("init: (3) - afei = 0x%x\n",
740 chip->image[CS4231_ALT_FEATURE_1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741#endif
742
743 spin_lock_irqsave(&chip->lock, flags);
Krzysztof Helt9e9abb42007-09-10 23:06:55 +0200744 snd_cs4231_out(chip, CS4231_ALT_FEATURE_2,
745 chip->image[CS4231_ALT_FEATURE_2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 spin_unlock_irqrestore(&chip->lock, flags);
747
748 snd_cs4231_mce_up(chip);
749 spin_lock_irqsave(&chip->lock, flags);
Krzysztof Helt9e9abb42007-09-10 23:06:55 +0200750 snd_cs4231_out(chip, CS4231_PLAYBK_FORMAT,
751 chip->image[CS4231_PLAYBK_FORMAT]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 spin_unlock_irqrestore(&chip->lock, flags);
753 snd_cs4231_mce_down(chip);
754
755#ifdef SNDRV_DEBUG_MCE
Christopher Zimmermanna1314302005-09-21 00:41:22 -0700756 snd_printdd("init: (4)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757#endif
758
759 snd_cs4231_mce_up(chip);
760 spin_lock_irqsave(&chip->lock, flags);
761 snd_cs4231_out(chip, CS4231_REC_FORMAT, chip->image[CS4231_REC_FORMAT]);
762 spin_unlock_irqrestore(&chip->lock, flags);
763 snd_cs4231_mce_down(chip);
764
765#ifdef SNDRV_DEBUG_MCE
Christopher Zimmermanna1314302005-09-21 00:41:22 -0700766 snd_printdd("init: (5)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767#endif
768}
769
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100770static int snd_cs4231_open(struct snd_cs4231 *chip, unsigned int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771{
772 unsigned long flags;
773
Ingo Molnar12aa7572006-01-16 16:36:05 +0100774 mutex_lock(&chip->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 if ((chip->mode & mode)) {
Ingo Molnar12aa7572006-01-16 16:36:05 +0100776 mutex_unlock(&chip->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 return -EAGAIN;
778 }
779 if (chip->mode & CS4231_MODE_OPEN) {
780 chip->mode |= mode;
Ingo Molnar12aa7572006-01-16 16:36:05 +0100781 mutex_unlock(&chip->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 return 0;
783 }
784 /* ok. now enable and ack CODEC IRQ */
785 spin_lock_irqsave(&chip->lock, flags);
786 snd_cs4231_out(chip, CS4231_IRQ_STATUS, CS4231_PLAYBACK_IRQ |
787 CS4231_RECORD_IRQ |
788 CS4231_TIMER_IRQ);
789 snd_cs4231_out(chip, CS4231_IRQ_STATUS, 0);
Krzysztof Helt7e52f3d2007-09-05 15:08:23 +0200790 __cs4231_writeb(chip, 0, CS4231U(chip, STATUS)); /* clear IRQ */
791 __cs4231_writeb(chip, 0, CS4231U(chip, STATUS)); /* clear IRQ */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
793 snd_cs4231_out(chip, CS4231_IRQ_STATUS, CS4231_PLAYBACK_IRQ |
794 CS4231_RECORD_IRQ |
795 CS4231_TIMER_IRQ);
796 snd_cs4231_out(chip, CS4231_IRQ_STATUS, 0);
Christopher Zimmermanna1314302005-09-21 00:41:22 -0700797
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 spin_unlock_irqrestore(&chip->lock, flags);
799
800 chip->mode = mode;
Ingo Molnar12aa7572006-01-16 16:36:05 +0100801 mutex_unlock(&chip->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 return 0;
803}
804
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100805static void snd_cs4231_close(struct snd_cs4231 *chip, unsigned int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806{
807 unsigned long flags;
808
Ingo Molnar12aa7572006-01-16 16:36:05 +0100809 mutex_lock(&chip->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 chip->mode &= ~mode;
811 if (chip->mode & CS4231_MODE_OPEN) {
Ingo Molnar12aa7572006-01-16 16:36:05 +0100812 mutex_unlock(&chip->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 return;
814 }
815 snd_cs4231_calibrate_mute(chip, 1);
816
817 /* disable IRQ */
818 spin_lock_irqsave(&chip->lock, flags);
819 snd_cs4231_out(chip, CS4231_IRQ_STATUS, 0);
Krzysztof Helt7e52f3d2007-09-05 15:08:23 +0200820 __cs4231_writeb(chip, 0, CS4231U(chip, STATUS)); /* clear IRQ */
821 __cs4231_writeb(chip, 0, CS4231U(chip, STATUS)); /* clear IRQ */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822
823 /* now disable record & playback */
824
825 if (chip->image[CS4231_IFACE_CTRL] &
826 (CS4231_PLAYBACK_ENABLE | CS4231_PLAYBACK_PIO |
827 CS4231_RECORD_ENABLE | CS4231_RECORD_PIO)) {
828 spin_unlock_irqrestore(&chip->lock, flags);
829 snd_cs4231_mce_up(chip);
830 spin_lock_irqsave(&chip->lock, flags);
831 chip->image[CS4231_IFACE_CTRL] &=
832 ~(CS4231_PLAYBACK_ENABLE | CS4231_PLAYBACK_PIO |
833 CS4231_RECORD_ENABLE | CS4231_RECORD_PIO);
Krzysztof Helt9e9abb42007-09-10 23:06:55 +0200834 snd_cs4231_out(chip, CS4231_IFACE_CTRL,
835 chip->image[CS4231_IFACE_CTRL]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 spin_unlock_irqrestore(&chip->lock, flags);
837 snd_cs4231_mce_down(chip);
838 spin_lock_irqsave(&chip->lock, flags);
839 }
840
841 /* clear IRQ again */
842 snd_cs4231_out(chip, CS4231_IRQ_STATUS, 0);
Krzysztof Helt7e52f3d2007-09-05 15:08:23 +0200843 __cs4231_writeb(chip, 0, CS4231U(chip, STATUS)); /* clear IRQ */
844 __cs4231_writeb(chip, 0, CS4231U(chip, STATUS)); /* clear IRQ */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 spin_unlock_irqrestore(&chip->lock, flags);
846
847 snd_cs4231_calibrate_mute(chip, 0);
848
849 chip->mode = 0;
Ingo Molnar12aa7572006-01-16 16:36:05 +0100850 mutex_unlock(&chip->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851}
852
853/*
854 * timer open/close
855 */
856
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100857static int snd_cs4231_timer_open(struct snd_timer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858{
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100859 struct snd_cs4231 *chip = snd_timer_chip(timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 snd_cs4231_open(chip, CS4231_MODE_TIMER);
861 return 0;
862}
863
Krzysztof Helt9e9abb42007-09-10 23:06:55 +0200864static int snd_cs4231_timer_close(struct snd_timer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865{
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100866 struct snd_cs4231 *chip = snd_timer_chip(timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 snd_cs4231_close(chip, CS4231_MODE_TIMER);
868 return 0;
869}
870
Krzysztof Helt9e9abb42007-09-10 23:06:55 +0200871static struct snd_timer_hardware snd_cs4231_timer_table = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 .flags = SNDRV_TIMER_HW_AUTO,
873 .resolution = 9945,
874 .ticks = 65535,
875 .open = snd_cs4231_timer_open,
876 .close = snd_cs4231_timer_close,
877 .c_resolution = snd_cs4231_timer_resolution,
878 .start = snd_cs4231_timer_start,
879 .stop = snd_cs4231_timer_stop,
880};
881
882/*
883 * ok.. exported functions..
884 */
885
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100886static int snd_cs4231_playback_hw_params(struct snd_pcm_substream *substream,
887 struct snd_pcm_hw_params *hw_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888{
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100889 struct snd_cs4231 *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 unsigned char new_pdfr;
891 int err;
892
Krzysztof Helt9e9abb42007-09-10 23:06:55 +0200893 err = snd_pcm_lib_malloc_pages(substream,
894 params_buffer_bytes(hw_params));
895 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 return err;
897 new_pdfr = snd_cs4231_get_format(chip, params_format(hw_params),
898 params_channels(hw_params)) |
899 snd_cs4231_get_rate(params_rate(hw_params));
900 snd_cs4231_playback_format(chip, hw_params, new_pdfr);
901
902 return 0;
903}
904
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100905static int snd_cs4231_playback_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906{
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100907 struct snd_cs4231 *chip = snd_pcm_substream_chip(substream);
908 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 unsigned long flags;
910
911 spin_lock_irqsave(&chip->lock, flags);
Christopher Zimmermanna1314302005-09-21 00:41:22 -0700912
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 chip->image[CS4231_IFACE_CTRL] &= ~(CS4231_PLAYBACK_ENABLE |
914 CS4231_PLAYBACK_PIO);
Christopher Zimmermanna1314302005-09-21 00:41:22 -0700915
Eric Sesterhenn817dd6e2006-03-24 18:49:12 +0100916 BUG_ON(runtime->period_size > 0xffff + 1);
Christopher Zimmermanna1314302005-09-21 00:41:22 -0700917
Christopher Zimmermanna1314302005-09-21 00:41:22 -0700918 chip->p_periods_sent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 spin_unlock_irqrestore(&chip->lock, flags);
920
921 return 0;
922}
923
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100924static int snd_cs4231_capture_hw_params(struct snd_pcm_substream *substream,
925 struct snd_pcm_hw_params *hw_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926{
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100927 struct snd_cs4231 *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 unsigned char new_cdfr;
929 int err;
930
Krzysztof Helt9e9abb42007-09-10 23:06:55 +0200931 err = snd_pcm_lib_malloc_pages(substream,
932 params_buffer_bytes(hw_params));
933 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 return err;
935 new_cdfr = snd_cs4231_get_format(chip, params_format(hw_params),
936 params_channels(hw_params)) |
937 snd_cs4231_get_rate(params_rate(hw_params));
938 snd_cs4231_capture_format(chip, hw_params, new_cdfr);
939
940 return 0;
941}
942
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100943static int snd_cs4231_capture_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944{
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100945 struct snd_cs4231 *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 unsigned long flags;
947
948 spin_lock_irqsave(&chip->lock, flags);
949 chip->image[CS4231_IFACE_CTRL] &= ~(CS4231_RECORD_ENABLE |
950 CS4231_RECORD_PIO);
951
Christopher Zimmermanna1314302005-09-21 00:41:22 -0700952
Georg Chini5a820fa2005-11-07 14:08:25 -0800953 chip->c_periods_sent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 spin_unlock_irqrestore(&chip->lock, flags);
955
956 return 0;
957}
958
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100959static void snd_cs4231_overrange(struct snd_cs4231 *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960{
961 unsigned long flags;
962 unsigned char res;
963
964 spin_lock_irqsave(&chip->lock, flags);
965 res = snd_cs4231_in(chip, CS4231_TEST_INIT);
966 spin_unlock_irqrestore(&chip->lock, flags);
967
Krzysztof Helt9e9abb42007-09-10 23:06:55 +0200968 /* detect overrange only above 0dB; may be user selectable? */
969 if (res & (0x08 | 0x02))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 chip->capture_substream->runtime->overrange++;
971}
972
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100973static void snd_cs4231_play_callback(struct snd_cs4231 *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 if (chip->image[CS4231_IFACE_CTRL] & CS4231_PLAYBACK_ENABLE) {
976 snd_pcm_period_elapsed(chip->playback_substream);
Georg Chinib1282542005-11-07 14:09:19 -0800977 snd_cs4231_advance_dma(&chip->p_dma, chip->playback_substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 &chip->p_periods_sent);
979 }
980}
981
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100982static void snd_cs4231_capture_callback(struct snd_cs4231 *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 if (chip->image[CS4231_IFACE_CTRL] & CS4231_RECORD_ENABLE) {
985 snd_pcm_period_elapsed(chip->capture_substream);
Georg Chinib1282542005-11-07 14:09:19 -0800986 snd_cs4231_advance_dma(&chip->c_dma, chip->capture_substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 &chip->c_periods_sent);
988 }
989}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990
Krzysztof Helt9e9abb42007-09-10 23:06:55 +0200991static snd_pcm_uframes_t snd_cs4231_playback_pointer(
992 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993{
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100994 struct snd_cs4231 *chip = snd_pcm_substream_chip(substream);
995 struct cs4231_dma_control *dma_cont = &chip->p_dma;
Georg Chini5a820fa2005-11-07 14:08:25 -0800996 size_t ptr;
Krzysztof Helt9e9abb42007-09-10 23:06:55 +0200997
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 if (!(chip->image[CS4231_IFACE_CTRL] & CS4231_PLAYBACK_ENABLE))
999 return 0;
Georg Chinib1282542005-11-07 14:09:19 -08001000 ptr = dma_cont->address(dma_cont);
1001 if (ptr != 0)
1002 ptr -= substream->runtime->dma_addr;
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001003
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 return bytes_to_frames(substream->runtime, ptr);
1005}
1006
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001007static snd_pcm_uframes_t snd_cs4231_capture_pointer(
1008 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009{
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001010 struct snd_cs4231 *chip = snd_pcm_substream_chip(substream);
1011 struct cs4231_dma_control *dma_cont = &chip->c_dma;
Georg Chini5a820fa2005-11-07 14:08:25 -08001012 size_t ptr;
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001013
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 if (!(chip->image[CS4231_IFACE_CTRL] & CS4231_RECORD_ENABLE))
1015 return 0;
Georg Chinib1282542005-11-07 14:09:19 -08001016 ptr = dma_cont->address(dma_cont);
1017 if (ptr != 0)
1018 ptr -= substream->runtime->dma_addr;
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001019
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 return bytes_to_frames(substream->runtime, ptr);
1021}
1022
David S. Miller78162382009-04-07 00:45:51 -07001023static int __devinit snd_cs4231_probe(struct snd_cs4231 *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024{
1025 unsigned long flags;
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001026 int i;
1027 int id = 0;
1028 int vers = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 unsigned char *ptr;
1030
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 for (i = 0; i < 50; i++) {
1032 mb();
Krzysztof Helt7e52f3d2007-09-05 15:08:23 +02001033 if (__cs4231_readb(chip, CS4231U(chip, REGSEL)) & CS4231_INIT)
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001034 msleep(2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 else {
1036 spin_lock_irqsave(&chip->lock, flags);
1037 snd_cs4231_out(chip, CS4231_MISC_INFO, CS4231_MODE2);
1038 id = snd_cs4231_in(chip, CS4231_MISC_INFO) & 0x0f;
1039 vers = snd_cs4231_in(chip, CS4231_VERSION);
1040 spin_unlock_irqrestore(&chip->lock, flags);
1041 if (id == 0x0a)
1042 break; /* this is valid value */
1043 }
1044 }
1045 snd_printdd("cs4231: port = %p, id = 0x%x\n", chip->port, id);
1046 if (id != 0x0a)
1047 return -ENODEV; /* no valid device found */
1048
1049 spin_lock_irqsave(&chip->lock, flags);
1050
Krzysztof Helt7e52f3d2007-09-05 15:08:23 +02001051 /* clear any pendings IRQ */
1052 __cs4231_readb(chip, CS4231U(chip, STATUS));
1053 __cs4231_writeb(chip, 0, CS4231U(chip, STATUS));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 mb();
1055
1056 spin_unlock_irqrestore(&chip->lock, flags);
1057
1058 chip->image[CS4231_MISC_INFO] = CS4231_MODE2;
1059 chip->image[CS4231_IFACE_CTRL] =
1060 chip->image[CS4231_IFACE_CTRL] & ~CS4231_SINGLE_DMA;
1061 chip->image[CS4231_ALT_FEATURE_1] = 0x80;
1062 chip->image[CS4231_ALT_FEATURE_2] = 0x01;
1063 if (vers & 0x20)
1064 chip->image[CS4231_ALT_FEATURE_2] |= 0x02;
1065
1066 ptr = (unsigned char *) &chip->image;
1067
1068 snd_cs4231_mce_down(chip);
1069
1070 spin_lock_irqsave(&chip->lock, flags);
1071
1072 for (i = 0; i < 32; i++) /* ok.. fill all CS4231 registers */
1073 snd_cs4231_out(chip, i, *ptr++);
1074
1075 spin_unlock_irqrestore(&chip->lock, flags);
1076
1077 snd_cs4231_mce_up(chip);
1078
1079 snd_cs4231_mce_down(chip);
1080
1081 mdelay(2);
1082
1083 return 0; /* all things are ok.. */
1084}
1085
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001086static struct snd_pcm_hardware snd_cs4231_playback = {
1087 .info = SNDRV_PCM_INFO_MMAP |
1088 SNDRV_PCM_INFO_INTERLEAVED |
1089 SNDRV_PCM_INFO_MMAP_VALID |
1090 SNDRV_PCM_INFO_SYNC_START,
1091 .formats = SNDRV_PCM_FMTBIT_MU_LAW |
1092 SNDRV_PCM_FMTBIT_A_LAW |
1093 SNDRV_PCM_FMTBIT_IMA_ADPCM |
1094 SNDRV_PCM_FMTBIT_U8 |
1095 SNDRV_PCM_FMTBIT_S16_LE |
1096 SNDRV_PCM_FMTBIT_S16_BE,
1097 .rates = SNDRV_PCM_RATE_KNOT |
1098 SNDRV_PCM_RATE_8000_48000,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 .rate_min = 5510,
1100 .rate_max = 48000,
1101 .channels_min = 1,
1102 .channels_max = 2,
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001103 .buffer_bytes_max = 32 * 1024,
David S. Millerf9af1d92007-01-03 18:51:54 -08001104 .period_bytes_min = 64,
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001105 .period_bytes_max = 32 * 1024,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 .periods_min = 1,
1107 .periods_max = 1024,
1108};
1109
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001110static struct snd_pcm_hardware snd_cs4231_capture = {
1111 .info = SNDRV_PCM_INFO_MMAP |
1112 SNDRV_PCM_INFO_INTERLEAVED |
1113 SNDRV_PCM_INFO_MMAP_VALID |
1114 SNDRV_PCM_INFO_SYNC_START,
1115 .formats = SNDRV_PCM_FMTBIT_MU_LAW |
1116 SNDRV_PCM_FMTBIT_A_LAW |
1117 SNDRV_PCM_FMTBIT_IMA_ADPCM |
1118 SNDRV_PCM_FMTBIT_U8 |
1119 SNDRV_PCM_FMTBIT_S16_LE |
1120 SNDRV_PCM_FMTBIT_S16_BE,
1121 .rates = SNDRV_PCM_RATE_KNOT |
1122 SNDRV_PCM_RATE_8000_48000,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 .rate_min = 5510,
1124 .rate_max = 48000,
1125 .channels_min = 1,
1126 .channels_max = 2,
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001127 .buffer_bytes_max = 32 * 1024,
David S. Millerf9af1d92007-01-03 18:51:54 -08001128 .period_bytes_min = 64,
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001129 .period_bytes_max = 32 * 1024,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 .periods_min = 1,
1131 .periods_max = 1024,
1132};
1133
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001134static int snd_cs4231_playback_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135{
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001136 struct snd_cs4231 *chip = snd_pcm_substream_chip(substream);
1137 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 int err;
1139
1140 runtime->hw = snd_cs4231_playback;
1141
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001142 err = snd_cs4231_open(chip, CS4231_MODE_PLAY);
1143 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 snd_free_pages(runtime->dma_area, runtime->dma_bytes);
1145 return err;
1146 }
1147 chip->playback_substream = substream;
1148 chip->p_periods_sent = 0;
1149 snd_pcm_set_sync(substream);
1150 snd_cs4231_xrate(runtime);
1151
1152 return 0;
1153}
1154
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001155static int snd_cs4231_capture_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156{
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001157 struct snd_cs4231 *chip = snd_pcm_substream_chip(substream);
1158 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 int err;
1160
1161 runtime->hw = snd_cs4231_capture;
1162
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001163 err = snd_cs4231_open(chip, CS4231_MODE_RECORD);
1164 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 snd_free_pages(runtime->dma_area, runtime->dma_bytes);
1166 return err;
1167 }
1168 chip->capture_substream = substream;
1169 chip->c_periods_sent = 0;
1170 snd_pcm_set_sync(substream);
1171 snd_cs4231_xrate(runtime);
1172
1173 return 0;
1174}
1175
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001176static int snd_cs4231_playback_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177{
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001178 struct snd_cs4231 *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 snd_cs4231_close(chip, CS4231_MODE_PLAY);
Georg Chinib1282542005-11-07 14:09:19 -08001181 chip->playback_substream = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182
1183 return 0;
1184}
1185
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001186static int snd_cs4231_capture_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187{
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001188 struct snd_cs4231 *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 snd_cs4231_close(chip, CS4231_MODE_RECORD);
Georg Chinib1282542005-11-07 14:09:19 -08001191 chip->capture_substream = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192
1193 return 0;
1194}
1195
1196/* XXX We can do some power-management, in particular on EBUS using
1197 * XXX the audio AUXIO register...
1198 */
1199
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001200static struct snd_pcm_ops snd_cs4231_playback_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 .open = snd_cs4231_playback_open,
1202 .close = snd_cs4231_playback_close,
1203 .ioctl = snd_pcm_lib_ioctl,
1204 .hw_params = snd_cs4231_playback_hw_params,
Krzysztof Heltc6c2d572007-09-04 13:08:24 +02001205 .hw_free = snd_pcm_lib_free_pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 .prepare = snd_cs4231_playback_prepare,
1207 .trigger = snd_cs4231_trigger,
1208 .pointer = snd_cs4231_playback_pointer,
1209};
1210
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001211static struct snd_pcm_ops snd_cs4231_capture_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 .open = snd_cs4231_capture_open,
1213 .close = snd_cs4231_capture_close,
1214 .ioctl = snd_pcm_lib_ioctl,
1215 .hw_params = snd_cs4231_capture_hw_params,
Krzysztof Heltc6c2d572007-09-04 13:08:24 +02001216 .hw_free = snd_pcm_lib_free_pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 .prepare = snd_cs4231_capture_prepare,
1218 .trigger = snd_cs4231_trigger,
1219 .pointer = snd_cs4231_capture_pointer,
1220};
1221
David S. Miller78162382009-04-07 00:45:51 -07001222static int __devinit snd_cs4231_pcm(struct snd_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223{
Krzysztof Heltc6c2d572007-09-04 13:08:24 +02001224 struct snd_cs4231 *chip = card->private_data;
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001225 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 int err;
1227
Krzysztof Heltc6c2d572007-09-04 13:08:24 +02001228 err = snd_pcm_new(card, "CS4231", 0, 1, 1, &pcm);
1229 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 return err;
1231
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001232 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
1233 &snd_cs4231_playback_ops);
1234 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
1235 &snd_cs4231_capture_ops);
1236
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 /* global setup */
1238 pcm->private_data = chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 pcm->info_flags = SNDRV_PCM_INFO_JOINT_DUPLEX;
1240 strcpy(pcm->name, "CS4231");
1241
David S. Millerafc88ad2008-08-30 00:13:55 -07001242 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1243 &chip->op->dev,
1244 64 * 1024, 128 * 1024);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245
1246 chip->pcm = pcm;
1247
1248 return 0;
1249}
1250
David S. Miller78162382009-04-07 00:45:51 -07001251static int __devinit snd_cs4231_timer(struct snd_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252{
Krzysztof Heltc6c2d572007-09-04 13:08:24 +02001253 struct snd_cs4231 *chip = card->private_data;
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001254 struct snd_timer *timer;
1255 struct snd_timer_id tid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 int err;
1257
1258 /* Timer initialization */
1259 tid.dev_class = SNDRV_TIMER_CLASS_CARD;
1260 tid.dev_sclass = SNDRV_TIMER_SCLASS_NONE;
Krzysztof Heltc6c2d572007-09-04 13:08:24 +02001261 tid.card = card->number;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 tid.device = 0;
1263 tid.subdevice = 0;
Krzysztof Heltc6c2d572007-09-04 13:08:24 +02001264 err = snd_timer_new(card, "CS4231", &tid, &timer);
1265 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 return err;
1267 strcpy(timer->name, "CS4231");
1268 timer->private_data = chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 timer->hw = snd_cs4231_timer_table;
1270 chip->timer = timer;
1271
1272 return 0;
1273}
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001274
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275/*
1276 * MIXER part
1277 */
1278
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001279static int snd_cs4231_info_mux(struct snd_kcontrol *kcontrol,
1280 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281{
1282 static char *texts[4] = {
1283 "Line", "CD", "Mic", "Mix"
1284 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1287 uinfo->count = 2;
1288 uinfo->value.enumerated.items = 4;
1289 if (uinfo->value.enumerated.item > 3)
1290 uinfo->value.enumerated.item = 3;
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001291 strcpy(uinfo->value.enumerated.name,
1292 texts[uinfo->value.enumerated.item]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293
1294 return 0;
1295}
1296
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001297static int snd_cs4231_get_mux(struct snd_kcontrol *kcontrol,
1298 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299{
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001300 struct snd_cs4231 *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 unsigned long flags;
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001302
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 spin_lock_irqsave(&chip->lock, flags);
1304 ucontrol->value.enumerated.item[0] =
1305 (chip->image[CS4231_LEFT_INPUT] & CS4231_MIXS_ALL) >> 6;
1306 ucontrol->value.enumerated.item[1] =
1307 (chip->image[CS4231_RIGHT_INPUT] & CS4231_MIXS_ALL) >> 6;
1308 spin_unlock_irqrestore(&chip->lock, flags);
1309
1310 return 0;
1311}
1312
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001313static int snd_cs4231_put_mux(struct snd_kcontrol *kcontrol,
1314 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315{
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001316 struct snd_cs4231 *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 unsigned long flags;
1318 unsigned short left, right;
1319 int change;
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001320
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 if (ucontrol->value.enumerated.item[0] > 3 ||
1322 ucontrol->value.enumerated.item[1] > 3)
1323 return -EINVAL;
1324 left = ucontrol->value.enumerated.item[0] << 6;
1325 right = ucontrol->value.enumerated.item[1] << 6;
1326
1327 spin_lock_irqsave(&chip->lock, flags);
1328
1329 left = (chip->image[CS4231_LEFT_INPUT] & ~CS4231_MIXS_ALL) | left;
1330 right = (chip->image[CS4231_RIGHT_INPUT] & ~CS4231_MIXS_ALL) | right;
1331 change = left != chip->image[CS4231_LEFT_INPUT] ||
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001332 right != chip->image[CS4231_RIGHT_INPUT];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 snd_cs4231_out(chip, CS4231_LEFT_INPUT, left);
1334 snd_cs4231_out(chip, CS4231_RIGHT_INPUT, right);
1335
1336 spin_unlock_irqrestore(&chip->lock, flags);
1337
1338 return change;
1339}
1340
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001341static int snd_cs4231_info_single(struct snd_kcontrol *kcontrol,
1342 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343{
1344 int mask = (kcontrol->private_value >> 16) & 0xff;
1345
1346 uinfo->type = (mask == 1) ?
1347 SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
1348 uinfo->count = 1;
1349 uinfo->value.integer.min = 0;
1350 uinfo->value.integer.max = mask;
1351
1352 return 0;
1353}
1354
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001355static int snd_cs4231_get_single(struct snd_kcontrol *kcontrol,
1356 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357{
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001358 struct snd_cs4231 *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 unsigned long flags;
1360 int reg = kcontrol->private_value & 0xff;
1361 int shift = (kcontrol->private_value >> 8) & 0xff;
1362 int mask = (kcontrol->private_value >> 16) & 0xff;
1363 int invert = (kcontrol->private_value >> 24) & 0xff;
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001364
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 spin_lock_irqsave(&chip->lock, flags);
1366
1367 ucontrol->value.integer.value[0] = (chip->image[reg] >> shift) & mask;
1368
1369 spin_unlock_irqrestore(&chip->lock, flags);
1370
1371 if (invert)
1372 ucontrol->value.integer.value[0] =
1373 (mask - ucontrol->value.integer.value[0]);
1374
1375 return 0;
1376}
1377
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001378static int snd_cs4231_put_single(struct snd_kcontrol *kcontrol,
1379 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380{
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001381 struct snd_cs4231 *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 unsigned long flags;
1383 int reg = kcontrol->private_value & 0xff;
1384 int shift = (kcontrol->private_value >> 8) & 0xff;
1385 int mask = (kcontrol->private_value >> 16) & 0xff;
1386 int invert = (kcontrol->private_value >> 24) & 0xff;
1387 int change;
1388 unsigned short val;
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001389
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 val = (ucontrol->value.integer.value[0] & mask);
1391 if (invert)
1392 val = mask - val;
1393 val <<= shift;
1394
1395 spin_lock_irqsave(&chip->lock, flags);
1396
1397 val = (chip->image[reg] & ~(mask << shift)) | val;
1398 change = val != chip->image[reg];
1399 snd_cs4231_out(chip, reg, val);
1400
1401 spin_unlock_irqrestore(&chip->lock, flags);
1402
1403 return change;
1404}
1405
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001406static int snd_cs4231_info_double(struct snd_kcontrol *kcontrol,
1407 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408{
1409 int mask = (kcontrol->private_value >> 24) & 0xff;
1410
1411 uinfo->type = mask == 1 ?
1412 SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
1413 uinfo->count = 2;
1414 uinfo->value.integer.min = 0;
1415 uinfo->value.integer.max = mask;
1416
1417 return 0;
1418}
1419
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001420static int snd_cs4231_get_double(struct snd_kcontrol *kcontrol,
1421 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422{
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001423 struct snd_cs4231 *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 unsigned long flags;
1425 int left_reg = kcontrol->private_value & 0xff;
1426 int right_reg = (kcontrol->private_value >> 8) & 0xff;
1427 int shift_left = (kcontrol->private_value >> 16) & 0x07;
1428 int shift_right = (kcontrol->private_value >> 19) & 0x07;
1429 int mask = (kcontrol->private_value >> 24) & 0xff;
1430 int invert = (kcontrol->private_value >> 22) & 1;
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001431
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 spin_lock_irqsave(&chip->lock, flags);
1433
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001434 ucontrol->value.integer.value[0] =
1435 (chip->image[left_reg] >> shift_left) & mask;
1436 ucontrol->value.integer.value[1] =
1437 (chip->image[right_reg] >> shift_right) & mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438
1439 spin_unlock_irqrestore(&chip->lock, flags);
1440
1441 if (invert) {
1442 ucontrol->value.integer.value[0] =
1443 (mask - ucontrol->value.integer.value[0]);
1444 ucontrol->value.integer.value[1] =
1445 (mask - ucontrol->value.integer.value[1]);
1446 }
1447
1448 return 0;
1449}
1450
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001451static int snd_cs4231_put_double(struct snd_kcontrol *kcontrol,
1452 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453{
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001454 struct snd_cs4231 *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 unsigned long flags;
1456 int left_reg = kcontrol->private_value & 0xff;
1457 int right_reg = (kcontrol->private_value >> 8) & 0xff;
1458 int shift_left = (kcontrol->private_value >> 16) & 0x07;
1459 int shift_right = (kcontrol->private_value >> 19) & 0x07;
1460 int mask = (kcontrol->private_value >> 24) & 0xff;
1461 int invert = (kcontrol->private_value >> 22) & 1;
1462 int change;
1463 unsigned short val1, val2;
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001464
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 val1 = ucontrol->value.integer.value[0] & mask;
1466 val2 = ucontrol->value.integer.value[1] & mask;
1467 if (invert) {
1468 val1 = mask - val1;
1469 val2 = mask - val2;
1470 }
1471 val1 <<= shift_left;
1472 val2 <<= shift_right;
1473
1474 spin_lock_irqsave(&chip->lock, flags);
1475
1476 val1 = (chip->image[left_reg] & ~(mask << shift_left)) | val1;
1477 val2 = (chip->image[right_reg] & ~(mask << shift_right)) | val2;
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001478 change = val1 != chip->image[left_reg];
1479 change |= val2 != chip->image[right_reg];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 snd_cs4231_out(chip, left_reg, val1);
1481 snd_cs4231_out(chip, right_reg, val2);
1482
1483 spin_unlock_irqrestore(&chip->lock, flags);
1484
1485 return change;
1486}
1487
1488#define CS4231_SINGLE(xname, xindex, reg, shift, mask, invert) \
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001489{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = (xname), .index = (xindex), \
1490 .info = snd_cs4231_info_single, \
1491 .get = snd_cs4231_get_single, .put = snd_cs4231_put_single, \
1492 .private_value = (reg) | ((shift) << 8) | ((mask) << 16) | ((invert) << 24) }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001494#define CS4231_DOUBLE(xname, xindex, left_reg, right_reg, shift_left, \
1495 shift_right, mask, invert) \
1496{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = (xname), .index = (xindex), \
1497 .info = snd_cs4231_info_double, \
1498 .get = snd_cs4231_get_double, .put = snd_cs4231_put_double, \
1499 .private_value = (left_reg) | ((right_reg) << 8) | ((shift_left) << 16) | \
1500 ((shift_right) << 19) | ((mask) << 24) | ((invert) << 22) }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501
David S. Miller78162382009-04-07 00:45:51 -07001502static struct snd_kcontrol_new snd_cs4231_controls[] __devinitdata = {
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001503CS4231_DOUBLE("PCM Playback Switch", 0, CS4231_LEFT_OUTPUT,
1504 CS4231_RIGHT_OUTPUT, 7, 7, 1, 1),
1505CS4231_DOUBLE("PCM Playback Volume", 0, CS4231_LEFT_OUTPUT,
1506 CS4231_RIGHT_OUTPUT, 0, 0, 63, 1),
1507CS4231_DOUBLE("Line Playback Switch", 0, CS4231_LEFT_LINE_IN,
1508 CS4231_RIGHT_LINE_IN, 7, 7, 1, 1),
1509CS4231_DOUBLE("Line Playback Volume", 0, CS4231_LEFT_LINE_IN,
1510 CS4231_RIGHT_LINE_IN, 0, 0, 31, 1),
1511CS4231_DOUBLE("Aux Playback Switch", 0, CS4231_AUX1_LEFT_INPUT,
1512 CS4231_AUX1_RIGHT_INPUT, 7, 7, 1, 1),
1513CS4231_DOUBLE("Aux Playback Volume", 0, CS4231_AUX1_LEFT_INPUT,
1514 CS4231_AUX1_RIGHT_INPUT, 0, 0, 31, 1),
1515CS4231_DOUBLE("Aux Playback Switch", 1, CS4231_AUX2_LEFT_INPUT,
1516 CS4231_AUX2_RIGHT_INPUT, 7, 7, 1, 1),
1517CS4231_DOUBLE("Aux Playback Volume", 1, CS4231_AUX2_LEFT_INPUT,
1518 CS4231_AUX2_RIGHT_INPUT, 0, 0, 31, 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519CS4231_SINGLE("Mono Playback Switch", 0, CS4231_MONO_CTRL, 7, 1, 1),
1520CS4231_SINGLE("Mono Playback Volume", 0, CS4231_MONO_CTRL, 0, 15, 1),
1521CS4231_SINGLE("Mono Output Playback Switch", 0, CS4231_MONO_CTRL, 6, 1, 1),
1522CS4231_SINGLE("Mono Output Playback Bypass", 0, CS4231_MONO_CTRL, 5, 1, 0),
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001523CS4231_DOUBLE("Capture Volume", 0, CS4231_LEFT_INPUT, CS4231_RIGHT_INPUT, 0, 0,
1524 15, 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525{
1526 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1527 .name = "Capture Source",
1528 .info = snd_cs4231_info_mux,
1529 .get = snd_cs4231_get_mux,
1530 .put = snd_cs4231_put_mux,
1531},
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001532CS4231_DOUBLE("Mic Boost", 0, CS4231_LEFT_INPUT, CS4231_RIGHT_INPUT, 5, 5,
1533 1, 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534CS4231_SINGLE("Loopback Capture Switch", 0, CS4231_LOOPBACK, 0, 1, 0),
1535CS4231_SINGLE("Loopback Capture Volume", 0, CS4231_LOOPBACK, 2, 63, 1),
1536/* SPARC specific uses of XCTL{0,1} general purpose outputs. */
1537CS4231_SINGLE("Line Out Switch", 0, CS4231_PIN_CTRL, 6, 1, 1),
1538CS4231_SINGLE("Headphone Out Switch", 0, CS4231_PIN_CTRL, 7, 1, 1)
1539};
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001540
David S. Miller78162382009-04-07 00:45:51 -07001541static int __devinit snd_cs4231_mixer(struct snd_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542{
Krzysztof Heltc6c2d572007-09-04 13:08:24 +02001543 struct snd_cs4231 *chip = card->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 int err, idx;
1545
Takashi Iwai5e246b82008-08-08 17:12:47 +02001546 if (snd_BUG_ON(!chip || !chip->pcm))
1547 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 strcpy(card->mixername, chip->pcm->name);
1550
1551 for (idx = 0; idx < ARRAY_SIZE(snd_cs4231_controls); idx++) {
Krzysztof Heltc6c2d572007-09-04 13:08:24 +02001552 err = snd_ctl_add(card,
1553 snd_ctl_new1(&snd_cs4231_controls[idx], chip));
1554 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 return err;
1556 }
1557 return 0;
1558}
1559
1560static int dev;
1561
David S. Miller78162382009-04-07 00:45:51 -07001562static int __devinit cs4231_attach_begin(struct snd_card **rcard)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563{
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001564 struct snd_card *card;
Krzysztof Heltc6c2d572007-09-04 13:08:24 +02001565 struct snd_cs4231 *chip;
Takashi Iwaibd7dd772008-12-28 16:45:02 +01001566 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567
1568 *rcard = NULL;
1569
1570 if (dev >= SNDRV_CARDS)
1571 return -ENODEV;
1572
1573 if (!enable[dev]) {
1574 dev++;
1575 return -ENOENT;
1576 }
1577
Takashi Iwaibd7dd772008-12-28 16:45:02 +01001578 err = snd_card_create(index[dev], id[dev], THIS_MODULE,
1579 sizeof(struct snd_cs4231), &card);
1580 if (err < 0)
1581 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582
1583 strcpy(card->driver, "CS4231");
1584 strcpy(card->shortname, "Sun CS4231");
1585
Krzysztof Heltc6c2d572007-09-04 13:08:24 +02001586 chip = card->private_data;
1587 chip->card = card;
1588
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 *rcard = card;
1590 return 0;
1591}
1592
David S. Miller78162382009-04-07 00:45:51 -07001593static int __devinit cs4231_attach_finish(struct snd_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594{
Krzysztof Heltc6c2d572007-09-04 13:08:24 +02001595 struct snd_cs4231 *chip = card->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 int err;
1597
Krzysztof Heltc6c2d572007-09-04 13:08:24 +02001598 err = snd_cs4231_pcm(card);
1599 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 goto out_err;
1601
Krzysztof Heltc6c2d572007-09-04 13:08:24 +02001602 err = snd_cs4231_mixer(card);
1603 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 goto out_err;
1605
Krzysztof Heltc6c2d572007-09-04 13:08:24 +02001606 err = snd_cs4231_timer(card);
1607 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 goto out_err;
1609
Krzysztof Heltc6c2d572007-09-04 13:08:24 +02001610 err = snd_card_register(card);
1611 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 goto out_err;
1613
David S. Millerafc88ad2008-08-30 00:13:55 -07001614 dev_set_drvdata(&chip->op->dev, chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615
1616 dev++;
1617 return 0;
1618
1619out_err:
1620 snd_card_free(card);
1621 return err;
1622}
1623
1624#ifdef SBUS_SUPPORT
Georg Chinib1282542005-11-07 14:09:19 -08001625
David Howells7d12e782006-10-05 14:55:46 +01001626static irqreturn_t snd_cs4231_sbus_interrupt(int irq, void *dev_id)
Georg Chinib1282542005-11-07 14:09:19 -08001627{
1628 unsigned long flags;
1629 unsigned char status;
1630 u32 csr;
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001631 struct snd_cs4231 *chip = dev_id;
Georg Chinib1282542005-11-07 14:09:19 -08001632
1633 /*This is IRQ is not raised by the cs4231*/
Krzysztof Helt7e52f3d2007-09-05 15:08:23 +02001634 if (!(__cs4231_readb(chip, CS4231U(chip, STATUS)) & CS4231_GLOBALIRQ))
Georg Chinib1282542005-11-07 14:09:19 -08001635 return IRQ_NONE;
1636
1637 /* ACK the APC interrupt. */
1638 csr = sbus_readl(chip->port + APCCSR);
1639
1640 sbus_writel(csr, chip->port + APCCSR);
1641
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001642 if ((csr & APC_PDMA_READY) &&
1643 (csr & APC_PLAY_INT) &&
Georg Chinib1282542005-11-07 14:09:19 -08001644 (csr & APC_XINT_PNVA) &&
1645 !(csr & APC_XINT_EMPT))
1646 snd_cs4231_play_callback(chip);
1647
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001648 if ((csr & APC_CDMA_READY) &&
1649 (csr & APC_CAPT_INT) &&
Georg Chinib1282542005-11-07 14:09:19 -08001650 (csr & APC_XINT_CNVA) &&
1651 !(csr & APC_XINT_EMPT))
1652 snd_cs4231_capture_callback(chip);
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001653
Georg Chinib1282542005-11-07 14:09:19 -08001654 status = snd_cs4231_in(chip, CS4231_IRQ_STATUS);
1655
1656 if (status & CS4231_TIMER_IRQ) {
1657 if (chip->timer)
1658 snd_timer_interrupt(chip->timer, chip->timer->sticks);
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001659 }
Georg Chinib1282542005-11-07 14:09:19 -08001660
1661 if ((status & CS4231_RECORD_IRQ) && (csr & APC_CDMA_READY))
1662 snd_cs4231_overrange(chip);
1663
1664 /* ACK the CS4231 interrupt. */
1665 spin_lock_irqsave(&chip->lock, flags);
1666 snd_cs4231_outm(chip, CS4231_IRQ_STATUS, ~CS4231_ALL_IRQS | ~status, 0);
1667 spin_unlock_irqrestore(&chip->lock, flags);
1668
Georg Chinid35a1b92007-01-02 21:28:17 -08001669 return IRQ_HANDLED;
Georg Chinib1282542005-11-07 14:09:19 -08001670}
1671
1672/*
1673 * SBUS DMA routines
1674 */
1675
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001676static int sbus_dma_request(struct cs4231_dma_control *dma_cont,
1677 dma_addr_t bus_addr, size_t len)
Georg Chinib1282542005-11-07 14:09:19 -08001678{
1679 unsigned long flags;
1680 u32 test, csr;
1681 int err;
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001682 struct sbus_dma_info *base = &dma_cont->sbus_info;
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001683
Georg Chinib1282542005-11-07 14:09:19 -08001684 if (len >= (1 << 24))
1685 return -EINVAL;
1686 spin_lock_irqsave(&base->lock, flags);
1687 csr = sbus_readl(base->regs + APCCSR);
1688 err = -EINVAL;
1689 test = APC_CDMA_READY;
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001690 if (base->dir == APC_PLAY)
Georg Chinib1282542005-11-07 14:09:19 -08001691 test = APC_PDMA_READY;
1692 if (!(csr & test))
1693 goto out;
1694 err = -EBUSY;
Georg Chinib1282542005-11-07 14:09:19 -08001695 test = APC_XINT_CNVA;
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001696 if (base->dir == APC_PLAY)
Georg Chinib1282542005-11-07 14:09:19 -08001697 test = APC_XINT_PNVA;
1698 if (!(csr & test))
1699 goto out;
1700 err = 0;
1701 sbus_writel(bus_addr, base->regs + base->dir + APCNVA);
1702 sbus_writel(len, base->regs + base->dir + APCNC);
1703out:
1704 spin_unlock_irqrestore(&base->lock, flags);
1705 return err;
1706}
1707
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001708static void sbus_dma_prepare(struct cs4231_dma_control *dma_cont, int d)
Georg Chinib1282542005-11-07 14:09:19 -08001709{
1710 unsigned long flags;
1711 u32 csr, test;
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001712 struct sbus_dma_info *base = &dma_cont->sbus_info;
Georg Chinib1282542005-11-07 14:09:19 -08001713
1714 spin_lock_irqsave(&base->lock, flags);
1715 csr = sbus_readl(base->regs + APCCSR);
1716 test = APC_GENL_INT | APC_PLAY_INT | APC_XINT_ENA |
1717 APC_XINT_PLAY | APC_XINT_PEMP | APC_XINT_GENL |
1718 APC_XINT_PENA;
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001719 if (base->dir == APC_RECORD)
Georg Chinib1282542005-11-07 14:09:19 -08001720 test = APC_GENL_INT | APC_CAPT_INT | APC_XINT_ENA |
1721 APC_XINT_CAPT | APC_XINT_CEMP | APC_XINT_GENL;
1722 csr |= test;
1723 sbus_writel(csr, base->regs + APCCSR);
1724 spin_unlock_irqrestore(&base->lock, flags);
1725}
1726
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001727static void sbus_dma_enable(struct cs4231_dma_control *dma_cont, int on)
Georg Chinib1282542005-11-07 14:09:19 -08001728{
1729 unsigned long flags;
1730 u32 csr, shift;
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001731 struct sbus_dma_info *base = &dma_cont->sbus_info;
Georg Chinib1282542005-11-07 14:09:19 -08001732
1733 spin_lock_irqsave(&base->lock, flags);
1734 if (!on) {
Georg Chinid35a1b92007-01-02 21:28:17 -08001735 sbus_writel(0, base->regs + base->dir + APCNC);
1736 sbus_writel(0, base->regs + base->dir + APCNVA);
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001737 if (base->dir == APC_PLAY) {
Georg Chini3daadf32007-08-01 21:55:58 -07001738 sbus_writel(0, base->regs + base->dir + APCC);
1739 sbus_writel(0, base->regs + base->dir + APCVA);
1740 }
Georg Chinid35a1b92007-01-02 21:28:17 -08001741
Georg Chini3daadf32007-08-01 21:55:58 -07001742 udelay(1200);
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001743 }
Georg Chinib1282542005-11-07 14:09:19 -08001744 csr = sbus_readl(base->regs + APCCSR);
1745 shift = 0;
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001746 if (base->dir == APC_PLAY)
Georg Chinib1282542005-11-07 14:09:19 -08001747 shift = 1;
1748 if (on)
1749 csr &= ~(APC_CPAUSE << shift);
1750 else
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001751 csr |= (APC_CPAUSE << shift);
Georg Chinib1282542005-11-07 14:09:19 -08001752 sbus_writel(csr, base->regs + APCCSR);
1753 if (on)
1754 csr |= (APC_CDMA_READY << shift);
1755 else
1756 csr &= ~(APC_CDMA_READY << shift);
1757 sbus_writel(csr, base->regs + APCCSR);
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001758
Georg Chinib1282542005-11-07 14:09:19 -08001759 spin_unlock_irqrestore(&base->lock, flags);
1760}
1761
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001762static unsigned int sbus_dma_addr(struct cs4231_dma_control *dma_cont)
Georg Chinib1282542005-11-07 14:09:19 -08001763{
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001764 struct sbus_dma_info *base = &dma_cont->sbus_info;
Georg Chinib1282542005-11-07 14:09:19 -08001765
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001766 return sbus_readl(base->regs + base->dir + APCVA);
Georg Chinib1282542005-11-07 14:09:19 -08001767}
1768
Georg Chinib1282542005-11-07 14:09:19 -08001769/*
1770 * Init and exit routines
1771 */
1772
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001773static int snd_cs4231_sbus_free(struct snd_cs4231 *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774{
David S. Millerafc88ad2008-08-30 00:13:55 -07001775 struct of_device *op = chip->op;
David S. Millerae251032008-08-27 18:24:01 -07001776
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 if (chip->irq[0])
1778 free_irq(chip->irq[0], chip);
1779
1780 if (chip->port)
David S. Millerae251032008-08-27 18:24:01 -07001781 of_iounmap(&op->resource[0], chip->port, chip->regs_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 return 0;
1784}
1785
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001786static int snd_cs4231_sbus_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787{
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001788 struct snd_cs4231 *cp = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789
1790 return snd_cs4231_sbus_free(cp);
1791}
1792
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001793static struct snd_device_ops snd_cs4231_sbus_dev_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 .dev_free = snd_cs4231_sbus_dev_free,
1795};
1796
David S. Miller78162382009-04-07 00:45:51 -07001797static int __devinit snd_cs4231_sbus_create(struct snd_card *card,
1798 struct of_device *op,
1799 int dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800{
Krzysztof Heltc6c2d572007-09-04 13:08:24 +02001801 struct snd_cs4231 *chip = card->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 int err;
1803
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 spin_lock_init(&chip->lock);
Georg Chinib1282542005-11-07 14:09:19 -08001805 spin_lock_init(&chip->c_dma.sbus_info.lock);
1806 spin_lock_init(&chip->p_dma.sbus_info.lock);
Ingo Molnar12aa7572006-01-16 16:36:05 +01001807 mutex_init(&chip->mce_mutex);
1808 mutex_init(&chip->open_mutex);
David S. Millerafc88ad2008-08-30 00:13:55 -07001809 chip->op = op;
David S. Millerae251032008-08-27 18:24:01 -07001810 chip->regs_size = resource_size(&op->resource[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 memcpy(&chip->image, &snd_cs4231_original_image,
1812 sizeof(snd_cs4231_original_image));
1813
David S. Millerae251032008-08-27 18:24:01 -07001814 chip->port = of_ioremap(&op->resource[0], 0,
1815 chip->regs_size, "cs4231");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 if (!chip->port) {
Christopher Zimmermanna1314302005-09-21 00:41:22 -07001817 snd_printdd("cs4231-%d: Unable to map chip registers.\n", dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 return -EIO;
1819 }
1820
Georg Chinib1282542005-11-07 14:09:19 -08001821 chip->c_dma.sbus_info.regs = chip->port;
1822 chip->p_dma.sbus_info.regs = chip->port;
1823 chip->c_dma.sbus_info.dir = APC_RECORD;
1824 chip->p_dma.sbus_info.dir = APC_PLAY;
1825
1826 chip->p_dma.prepare = sbus_dma_prepare;
1827 chip->p_dma.enable = sbus_dma_enable;
1828 chip->p_dma.request = sbus_dma_request;
1829 chip->p_dma.address = sbus_dma_addr;
Georg Chinib1282542005-11-07 14:09:19 -08001830
1831 chip->c_dma.prepare = sbus_dma_prepare;
1832 chip->c_dma.enable = sbus_dma_enable;
1833 chip->c_dma.request = sbus_dma_request;
1834 chip->c_dma.address = sbus_dma_addr;
Georg Chini5a820fa2005-11-07 14:08:25 -08001835
David S. Millerae251032008-08-27 18:24:01 -07001836 if (request_irq(op->irqs[0], snd_cs4231_sbus_interrupt,
Thomas Gleixner65ca68b2006-07-01 19:29:46 -07001837 IRQF_SHARED, "cs4231", chip)) {
David S. Millerc6387a42006-06-20 01:21:29 -07001838 snd_printdd("cs4231-%d: Unable to grab SBUS IRQ %d\n",
David S. Millerae251032008-08-27 18:24:01 -07001839 dev, op->irqs[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 snd_cs4231_sbus_free(chip);
1841 return -EBUSY;
1842 }
David S. Millerae251032008-08-27 18:24:01 -07001843 chip->irq[0] = op->irqs[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844
1845 if (snd_cs4231_probe(chip) < 0) {
1846 snd_cs4231_sbus_free(chip);
1847 return -ENODEV;
1848 }
1849 snd_cs4231_init(chip);
1850
1851 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL,
1852 chip, &snd_cs4231_sbus_dev_ops)) < 0) {
1853 snd_cs4231_sbus_free(chip);
1854 return err;
1855 }
1856
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 return 0;
1858}
1859
Linus Torvalds33b07db2008-12-01 07:55:14 -08001860static int __devinit cs4231_sbus_probe(struct of_device *op, const struct of_device_id *match)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861{
David S. Millerae251032008-08-27 18:24:01 -07001862 struct resource *rp = &op->resource[0];
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001863 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 int err;
1865
1866 err = cs4231_attach_begin(&card);
1867 if (err)
1868 return err;
1869
Andrew Morton5863aa62006-07-03 00:24:22 -07001870 sprintf(card->longname, "%s at 0x%02lx:0x%016Lx, irq %d",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 card->shortname,
1872 rp->flags & 0xffL,
Greg Kroah-Hartmanaa0a2dd2006-06-12 14:50:27 -07001873 (unsigned long long)rp->start,
David S. Millerae251032008-08-27 18:24:01 -07001874 op->irqs[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875
David S. Millerae251032008-08-27 18:24:01 -07001876 err = snd_cs4231_sbus_create(card, op, dev);
Krzysztof Heltc6c2d572007-09-04 13:08:24 +02001877 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 snd_card_free(card);
1879 return err;
1880 }
1881
Krzysztof Heltc6c2d572007-09-04 13:08:24 +02001882 return cs4231_attach_finish(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883}
1884#endif
1885
1886#ifdef EBUS_SUPPORT
Georg Chinib1282542005-11-07 14:09:19 -08001887
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001888static void snd_cs4231_ebus_play_callback(struct ebus_dma_info *p, int event,
1889 void *cookie)
Georg Chinib1282542005-11-07 14:09:19 -08001890{
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001891 struct snd_cs4231 *chip = cookie;
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001892
Georg Chinib1282542005-11-07 14:09:19 -08001893 snd_cs4231_play_callback(chip);
1894}
1895
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001896static void snd_cs4231_ebus_capture_callback(struct ebus_dma_info *p,
1897 int event, void *cookie)
Georg Chinib1282542005-11-07 14:09:19 -08001898{
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001899 struct snd_cs4231 *chip = cookie;
Georg Chinib1282542005-11-07 14:09:19 -08001900
1901 snd_cs4231_capture_callback(chip);
1902}
1903
1904/*
1905 * EBUS DMA wrappers
1906 */
1907
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001908static int _ebus_dma_request(struct cs4231_dma_control *dma_cont,
1909 dma_addr_t bus_addr, size_t len)
Georg Chinib1282542005-11-07 14:09:19 -08001910{
1911 return ebus_dma_request(&dma_cont->ebus_info, bus_addr, len);
1912}
1913
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001914static void _ebus_dma_enable(struct cs4231_dma_control *dma_cont, int on)
Georg Chinib1282542005-11-07 14:09:19 -08001915{
1916 ebus_dma_enable(&dma_cont->ebus_info, on);
1917}
1918
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001919static void _ebus_dma_prepare(struct cs4231_dma_control *dma_cont, int dir)
Georg Chinib1282542005-11-07 14:09:19 -08001920{
1921 ebus_dma_prepare(&dma_cont->ebus_info, dir);
1922}
1923
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001924static unsigned int _ebus_dma_addr(struct cs4231_dma_control *dma_cont)
Georg Chinib1282542005-11-07 14:09:19 -08001925{
1926 return ebus_dma_addr(&dma_cont->ebus_info);
1927}
1928
Georg Chinib1282542005-11-07 14:09:19 -08001929/*
1930 * Init and exit routines
1931 */
1932
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001933static int snd_cs4231_ebus_free(struct snd_cs4231 *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934{
David S. Millerafc88ad2008-08-30 00:13:55 -07001935 struct of_device *op = chip->op;
1936
Georg Chinib1282542005-11-07 14:09:19 -08001937 if (chip->c_dma.ebus_info.regs) {
1938 ebus_dma_unregister(&chip->c_dma.ebus_info);
David S. Millerafc88ad2008-08-30 00:13:55 -07001939 of_iounmap(&op->resource[2], chip->c_dma.ebus_info.regs, 0x10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 }
Georg Chinib1282542005-11-07 14:09:19 -08001941 if (chip->p_dma.ebus_info.regs) {
1942 ebus_dma_unregister(&chip->p_dma.ebus_info);
David S. Millerafc88ad2008-08-30 00:13:55 -07001943 of_iounmap(&op->resource[1], chip->p_dma.ebus_info.regs, 0x10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 }
1945
1946 if (chip->port)
David S. Millerafc88ad2008-08-30 00:13:55 -07001947 of_iounmap(&op->resource[0], chip->port, 0x10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 return 0;
1950}
1951
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001952static int snd_cs4231_ebus_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953{
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001954 struct snd_cs4231 *cp = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955
1956 return snd_cs4231_ebus_free(cp);
1957}
1958
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001959static struct snd_device_ops snd_cs4231_ebus_dev_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 .dev_free = snd_cs4231_ebus_dev_free,
1961};
1962
David S. Miller78162382009-04-07 00:45:51 -07001963static int __devinit snd_cs4231_ebus_create(struct snd_card *card,
1964 struct of_device *op,
1965 int dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966{
Krzysztof Heltc6c2d572007-09-04 13:08:24 +02001967 struct snd_cs4231 *chip = card->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 int err;
1969
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 spin_lock_init(&chip->lock);
Georg Chinib1282542005-11-07 14:09:19 -08001971 spin_lock_init(&chip->c_dma.ebus_info.lock);
1972 spin_lock_init(&chip->p_dma.ebus_info.lock);
Ingo Molnar12aa7572006-01-16 16:36:05 +01001973 mutex_init(&chip->mce_mutex);
1974 mutex_init(&chip->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 chip->flags |= CS4231_FLAG_EBUS;
David S. Millerafc88ad2008-08-30 00:13:55 -07001976 chip->op = op;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 memcpy(&chip->image, &snd_cs4231_original_image,
1978 sizeof(snd_cs4231_original_image));
Georg Chinib1282542005-11-07 14:09:19 -08001979 strcpy(chip->c_dma.ebus_info.name, "cs4231(capture)");
1980 chip->c_dma.ebus_info.flags = EBUS_DMA_FLAG_USE_EBDMA_HANDLER;
1981 chip->c_dma.ebus_info.callback = snd_cs4231_ebus_capture_callback;
1982 chip->c_dma.ebus_info.client_cookie = chip;
David S. Millerafc88ad2008-08-30 00:13:55 -07001983 chip->c_dma.ebus_info.irq = op->irqs[0];
Georg Chinib1282542005-11-07 14:09:19 -08001984 strcpy(chip->p_dma.ebus_info.name, "cs4231(play)");
1985 chip->p_dma.ebus_info.flags = EBUS_DMA_FLAG_USE_EBDMA_HANDLER;
1986 chip->p_dma.ebus_info.callback = snd_cs4231_ebus_play_callback;
1987 chip->p_dma.ebus_info.client_cookie = chip;
David S. Millerafc88ad2008-08-30 00:13:55 -07001988 chip->p_dma.ebus_info.irq = op->irqs[1];
Georg Chinib1282542005-11-07 14:09:19 -08001989
1990 chip->p_dma.prepare = _ebus_dma_prepare;
1991 chip->p_dma.enable = _ebus_dma_enable;
1992 chip->p_dma.request = _ebus_dma_request;
1993 chip->p_dma.address = _ebus_dma_addr;
Georg Chinib1282542005-11-07 14:09:19 -08001994
1995 chip->c_dma.prepare = _ebus_dma_prepare;
1996 chip->c_dma.enable = _ebus_dma_enable;
1997 chip->c_dma.request = _ebus_dma_request;
1998 chip->c_dma.address = _ebus_dma_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999
David S. Millerafc88ad2008-08-30 00:13:55 -07002000 chip->port = of_ioremap(&op->resource[0], 0, 0x10, "cs4231");
2001 chip->p_dma.ebus_info.regs =
2002 of_ioremap(&op->resource[1], 0, 0x10, "cs4231_pdma");
2003 chip->c_dma.ebus_info.regs =
2004 of_ioremap(&op->resource[2], 0, 0x10, "cs4231_cdma");
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02002005 if (!chip->port || !chip->p_dma.ebus_info.regs ||
2006 !chip->c_dma.ebus_info.regs) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 snd_cs4231_ebus_free(chip);
Christopher Zimmermanna1314302005-09-21 00:41:22 -07002008 snd_printdd("cs4231-%d: Unable to map chip registers.\n", dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 return -EIO;
2010 }
2011
Georg Chinib1282542005-11-07 14:09:19 -08002012 if (ebus_dma_register(&chip->c_dma.ebus_info)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 snd_cs4231_ebus_free(chip);
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02002014 snd_printdd("cs4231-%d: Unable to register EBUS capture DMA\n",
2015 dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 return -EBUSY;
2017 }
Georg Chinib1282542005-11-07 14:09:19 -08002018 if (ebus_dma_irq_enable(&chip->c_dma.ebus_info, 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 snd_cs4231_ebus_free(chip);
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02002020 snd_printdd("cs4231-%d: Unable to enable EBUS capture IRQ\n",
2021 dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 return -EBUSY;
2023 }
2024
Georg Chinib1282542005-11-07 14:09:19 -08002025 if (ebus_dma_register(&chip->p_dma.ebus_info)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 snd_cs4231_ebus_free(chip);
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02002027 snd_printdd("cs4231-%d: Unable to register EBUS play DMA\n",
2028 dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 return -EBUSY;
2030 }
Georg Chinib1282542005-11-07 14:09:19 -08002031 if (ebus_dma_irq_enable(&chip->p_dma.ebus_info, 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 snd_cs4231_ebus_free(chip);
Christopher Zimmermanna1314302005-09-21 00:41:22 -07002033 snd_printdd("cs4231-%d: Unable to enable EBUS play IRQ\n", dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 return -EBUSY;
2035 }
2036
2037 if (snd_cs4231_probe(chip) < 0) {
2038 snd_cs4231_ebus_free(chip);
2039 return -ENODEV;
2040 }
2041 snd_cs4231_init(chip);
2042
2043 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL,
2044 chip, &snd_cs4231_ebus_dev_ops)) < 0) {
2045 snd_cs4231_ebus_free(chip);
2046 return err;
2047 }
2048
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 return 0;
2050}
2051
Linus Torvalds33b07db2008-12-01 07:55:14 -08002052static int __devinit cs4231_ebus_probe(struct of_device *op, const struct of_device_id *match)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053{
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01002054 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 int err;
2056
2057 err = cs4231_attach_begin(&card);
2058 if (err)
2059 return err;
2060
Sam Ravnborg3f4528d2009-01-06 13:20:38 -08002061 sprintf(card->longname, "%s at 0x%llx, irq %d",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 card->shortname,
David S. Millerafc88ad2008-08-30 00:13:55 -07002063 op->resource[0].start,
2064 op->irqs[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065
David S. Millerafc88ad2008-08-30 00:13:55 -07002066 err = snd_cs4231_ebus_create(card, op, dev);
Krzysztof Heltc6c2d572007-09-04 13:08:24 +02002067 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 snd_card_free(card);
2069 return err;
2070 }
2071
Krzysztof Heltc6c2d572007-09-04 13:08:24 +02002072 return cs4231_attach_finish(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073}
2074#endif
2075
Linus Torvalds33b07db2008-12-01 07:55:14 -08002076static int __devinit cs4231_probe(struct of_device *op, const struct of_device_id *match)
David S. Millerafc88ad2008-08-30 00:13:55 -07002077{
2078#ifdef EBUS_SUPPORT
2079 if (!strcmp(op->node->parent->name, "ebus"))
2080 return cs4231_ebus_probe(op, match);
2081#endif
David S. Millerae251032008-08-27 18:24:01 -07002082#ifdef SBUS_SUPPORT
David S. Millerafc88ad2008-08-30 00:13:55 -07002083 if (!strcmp(op->node->parent->name, "sbus") ||
2084 !strcmp(op->node->parent->name, "sbi"))
2085 return cs4231_sbus_probe(op, match);
2086#endif
2087 return -ENODEV;
2088}
2089
Linus Torvalds33b07db2008-12-01 07:55:14 -08002090static int __devexit cs4231_remove(struct of_device *op)
David S. Millerafc88ad2008-08-30 00:13:55 -07002091{
2092 struct snd_cs4231 *chip = dev_get_drvdata(&op->dev);
2093
2094 snd_card_free(chip->card);
2095
2096 return 0;
2097}
2098
David S. Millerfd098312008-08-31 01:23:17 -07002099static const struct of_device_id cs4231_match[] = {
David S. Millerae251032008-08-27 18:24:01 -07002100 {
2101 .name = "SUNW,CS4231",
2102 },
David S. Millerafc88ad2008-08-30 00:13:55 -07002103 {
2104 .name = "audio",
2105 .compatible = "SUNW,CS4231",
2106 },
David S. Millerae251032008-08-27 18:24:01 -07002107 {},
2108};
2109
2110MODULE_DEVICE_TABLE(of, cs4231_match);
2111
2112static struct of_platform_driver cs4231_driver = {
2113 .name = "audio",
2114 .match_table = cs4231_match,
2115 .probe = cs4231_probe,
Linus Torvalds33b07db2008-12-01 07:55:14 -08002116 .remove = __devexit_p(cs4231_remove),
David S. Millerae251032008-08-27 18:24:01 -07002117};
David S. Millerae251032008-08-27 18:24:01 -07002118
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119static int __init cs4231_init(void)
2120{
David S. Millerafc88ad2008-08-30 00:13:55 -07002121 return of_register_driver(&cs4231_driver, &of_bus_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122}
2123
2124static void __exit cs4231_exit(void)
2125{
David S. Millerae251032008-08-27 18:24:01 -07002126 of_unregister_driver(&cs4231_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127}
2128
2129module_init(cs4231_init);
2130module_exit(cs4231_exit);