blob: 0eed5f79a2bf9954163a5c902eb4f0b986e53e0b [file] [log] [blame]
Thomas Gleixner09c434b2019-05-19 13:08:20 +01001// SPDX-License-Identifier: GPL-2.0-only
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * Driver for CS4231 sound chips found on Sparcs.
David S. Millerae251032008-08-27 18:24:01 -07004 * Copyright (C) 2002, 2008 David S. Miller <davem@davemloft.net>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
6 * Based entirely upon drivers/sbus/audio/cs4231.c which is:
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02007 * Copyright (C) 1996, 1997, 1998 Derrick J Brashear (shadow@andrew.cmu.edu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * and also sound/isa/cs423x/cs4231_lib.c which is:
Jaroslav Kyselac1017a42007-10-15 09:50:19 +02009 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 */
11
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/module.h>
13#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#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 */
Rusty Russella67ff6a2011-12-15 13:49:36 +103044static bool 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
Grant Likely2dc11582010-08-06 09:25:50 -0600115 struct platform_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
Takashi Iwai121f46b2020-01-05 15:47:52 +0100187static const unsigned char freq_bits[14] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 /* 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
Takashi Iwai4e4b7ea2017-06-07 14:23:22 +0200204static const unsigned int rates[14] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 5510, 6620, 8000, 9600, 11025, 16000, 18900, 22050,
206 27042, 32000, 33075, 37800, 44100, 48000
207};
208
Takashi Iwai4e4b7ea2017-06-07 14:23:22 +0200209static const 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
Takashi Iwai121f46b2020-01-05 15:47:52 +0100221static const unsigned char snd_cs4231_original_image[32] =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222{
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
Takashi Iwai5a19b172013-11-05 15:02:42 +0100433 if (WARN_ON(period_size >= (1 << 24)))
434 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
Krzysztof Helt9e9abb42007-09-10 23:06:55 +0200436 if (dma_cont->request(dma_cont,
437 runtime->dma_addr + offset, period_size))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 (*periods_sent) = ((*periods_sent) + 1) % runtime->periods;
440 }
441}
Christopher Zimmermanna1314302005-09-21 00:41:22 -0700442
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100443static void cs4231_dma_trigger(struct snd_pcm_substream *substream,
444 unsigned int what, int on)
Christopher Zimmermanna1314302005-09-21 00:41:22 -0700445{
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100446 struct snd_cs4231 *chip = snd_pcm_substream_chip(substream);
447 struct cs4231_dma_control *dma_cont;
Christopher Zimmermanna1314302005-09-21 00:41:22 -0700448
Georg Chini5a820fa2005-11-07 14:08:25 -0800449 if (what & CS4231_PLAYBACK_ENABLE) {
Georg Chinib1282542005-11-07 14:09:19 -0800450 dma_cont = &chip->p_dma;
Christopher Zimmermanna1314302005-09-21 00:41:22 -0700451 if (on) {
Georg Chinib1282542005-11-07 14:09:19 -0800452 dma_cont->prepare(dma_cont, 0);
453 dma_cont->enable(dma_cont, 1);
454 snd_cs4231_advance_dma(dma_cont,
Georg Chini5a820fa2005-11-07 14:08:25 -0800455 chip->playback_substream,
456 &chip->p_periods_sent);
Christopher Zimmermanna1314302005-09-21 00:41:22 -0700457 } else {
Georg Chinib1282542005-11-07 14:09:19 -0800458 dma_cont->enable(dma_cont, 0);
Christopher Zimmermanna1314302005-09-21 00:41:22 -0700459 }
Georg Chini5a820fa2005-11-07 14:08:25 -0800460 }
461 if (what & CS4231_RECORD_ENABLE) {
Georg Chinib1282542005-11-07 14:09:19 -0800462 dma_cont = &chip->c_dma;
Christopher Zimmermanna1314302005-09-21 00:41:22 -0700463 if (on) {
Georg Chinib1282542005-11-07 14:09:19 -0800464 dma_cont->prepare(dma_cont, 1);
465 dma_cont->enable(dma_cont, 1);
466 snd_cs4231_advance_dma(dma_cont,
Georg Chini5a820fa2005-11-07 14:08:25 -0800467 chip->capture_substream,
468 &chip->c_periods_sent);
Christopher Zimmermanna1314302005-09-21 00:41:22 -0700469 } else {
Georg Chinib1282542005-11-07 14:09:19 -0800470 dma_cont->enable(dma_cont, 0);
Christopher Zimmermanna1314302005-09-21 00:41:22 -0700471 }
Christopher Zimmermanna1314302005-09-21 00:41:22 -0700472 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473}
474
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100475static int snd_cs4231_trigger(struct snd_pcm_substream *substream, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476{
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100477 struct snd_cs4231 *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 int result = 0;
479
480 switch (cmd) {
481 case SNDRV_PCM_TRIGGER_START:
482 case SNDRV_PCM_TRIGGER_STOP:
483 {
484 unsigned int what = 0;
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100485 struct snd_pcm_substream *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 unsigned long flags;
487
Takashi Iwaief991b92007-02-22 12:52:53 +0100488 snd_pcm_group_for_each_entry(s, substream) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 if (s == chip->playback_substream) {
490 what |= CS4231_PLAYBACK_ENABLE;
491 snd_pcm_trigger_done(s, substream);
492 } else if (s == chip->capture_substream) {
493 what |= CS4231_RECORD_ENABLE;
494 snd_pcm_trigger_done(s, substream);
495 }
496 }
497
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 spin_lock_irqsave(&chip->lock, flags);
499 if (cmd == SNDRV_PCM_TRIGGER_START) {
Christopher Zimmermanna1314302005-09-21 00:41:22 -0700500 cs4231_dma_trigger(substream, what, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 chip->image[CS4231_IFACE_CTRL] |= what;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 } else {
Christopher Zimmermanna1314302005-09-21 00:41:22 -0700503 cs4231_dma_trigger(substream, what, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 chip->image[CS4231_IFACE_CTRL] &= ~what;
505 }
506 snd_cs4231_out(chip, CS4231_IFACE_CTRL,
507 chip->image[CS4231_IFACE_CTRL]);
508 spin_unlock_irqrestore(&chip->lock, flags);
509 break;
510 }
511 default:
512 result = -EINVAL;
513 break;
514 }
Christopher Zimmermanna1314302005-09-21 00:41:22 -0700515
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 return result;
517}
518
519/*
520 * CODEC I/O
521 */
522
523static unsigned char snd_cs4231_get_rate(unsigned int rate)
524{
525 int i;
526
527 for (i = 0; i < 14; i++)
528 if (rate == rates[i])
529 return freq_bits[i];
Krzysztof Helt9e9abb42007-09-10 23:06:55 +0200530
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 return freq_bits[13];
532}
533
Krzysztof Helt9e9abb42007-09-10 23:06:55 +0200534static unsigned char snd_cs4231_get_format(struct snd_cs4231 *chip, int format,
535 int channels)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536{
537 unsigned char rformat;
538
539 rformat = CS4231_LINEAR_8;
540 switch (format) {
Krzysztof Helt9e9abb42007-09-10 23:06:55 +0200541 case SNDRV_PCM_FORMAT_MU_LAW:
542 rformat = CS4231_ULAW_8;
543 break;
544 case SNDRV_PCM_FORMAT_A_LAW:
545 rformat = CS4231_ALAW_8;
546 break;
547 case SNDRV_PCM_FORMAT_S16_LE:
548 rformat = CS4231_LINEAR_16;
549 break;
550 case SNDRV_PCM_FORMAT_S16_BE:
551 rformat = CS4231_LINEAR_16_BIG;
552 break;
553 case SNDRV_PCM_FORMAT_IMA_ADPCM:
554 rformat = CS4231_ADPCM_16;
555 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 }
557 if (channels > 1)
558 rformat |= CS4231_STEREO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 return rformat;
560}
561
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100562static void snd_cs4231_calibrate_mute(struct snd_cs4231 *chip, int mute)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563{
564 unsigned long flags;
565
566 mute = mute ? 1 : 0;
567 spin_lock_irqsave(&chip->lock, flags);
568 if (chip->calibrate_mute == mute) {
569 spin_unlock_irqrestore(&chip->lock, flags);
570 return;
571 }
572 if (!mute) {
573 snd_cs4231_dout(chip, CS4231_LEFT_INPUT,
574 chip->image[CS4231_LEFT_INPUT]);
575 snd_cs4231_dout(chip, CS4231_RIGHT_INPUT,
576 chip->image[CS4231_RIGHT_INPUT]);
577 snd_cs4231_dout(chip, CS4231_LOOPBACK,
578 chip->image[CS4231_LOOPBACK]);
579 }
580 snd_cs4231_dout(chip, CS4231_AUX1_LEFT_INPUT,
581 mute ? 0x80 : chip->image[CS4231_AUX1_LEFT_INPUT]);
582 snd_cs4231_dout(chip, CS4231_AUX1_RIGHT_INPUT,
583 mute ? 0x80 : chip->image[CS4231_AUX1_RIGHT_INPUT]);
584 snd_cs4231_dout(chip, CS4231_AUX2_LEFT_INPUT,
585 mute ? 0x80 : chip->image[CS4231_AUX2_LEFT_INPUT]);
586 snd_cs4231_dout(chip, CS4231_AUX2_RIGHT_INPUT,
587 mute ? 0x80 : chip->image[CS4231_AUX2_RIGHT_INPUT]);
588 snd_cs4231_dout(chip, CS4231_LEFT_OUTPUT,
589 mute ? 0x80 : chip->image[CS4231_LEFT_OUTPUT]);
590 snd_cs4231_dout(chip, CS4231_RIGHT_OUTPUT,
591 mute ? 0x80 : chip->image[CS4231_RIGHT_OUTPUT]);
592 snd_cs4231_dout(chip, CS4231_LEFT_LINE_IN,
593 mute ? 0x80 : chip->image[CS4231_LEFT_LINE_IN]);
594 snd_cs4231_dout(chip, CS4231_RIGHT_LINE_IN,
595 mute ? 0x80 : chip->image[CS4231_RIGHT_LINE_IN]);
596 snd_cs4231_dout(chip, CS4231_MONO_CTRL,
597 mute ? 0xc0 : chip->image[CS4231_MONO_CTRL]);
598 chip->calibrate_mute = mute;
599 spin_unlock_irqrestore(&chip->lock, flags);
600}
601
Krzysztof Helt9e9abb42007-09-10 23:06:55 +0200602static void snd_cs4231_playback_format(struct snd_cs4231 *chip,
603 struct snd_pcm_hw_params *params,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 unsigned char pdfr)
605{
606 unsigned long flags;
607
Ingo Molnar12aa7572006-01-16 16:36:05 +0100608 mutex_lock(&chip->mce_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 snd_cs4231_calibrate_mute(chip, 1);
610
611 snd_cs4231_mce_up(chip);
612
613 spin_lock_irqsave(&chip->lock, flags);
614 snd_cs4231_out(chip, CS4231_PLAYBK_FORMAT,
615 (chip->image[CS4231_IFACE_CTRL] & CS4231_RECORD_ENABLE) ?
616 (pdfr & 0xf0) | (chip->image[CS4231_REC_FORMAT] & 0x0f) :
617 pdfr);
618 spin_unlock_irqrestore(&chip->lock, flags);
619
620 snd_cs4231_mce_down(chip);
621
622 snd_cs4231_calibrate_mute(chip, 0);
Ingo Molnar12aa7572006-01-16 16:36:05 +0100623 mutex_unlock(&chip->mce_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624}
625
Krzysztof Helt9e9abb42007-09-10 23:06:55 +0200626static void snd_cs4231_capture_format(struct snd_cs4231 *chip,
627 struct snd_pcm_hw_params *params,
628 unsigned char cdfr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629{
630 unsigned long flags;
631
Ingo Molnar12aa7572006-01-16 16:36:05 +0100632 mutex_lock(&chip->mce_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 snd_cs4231_calibrate_mute(chip, 1);
634
635 snd_cs4231_mce_up(chip);
636
637 spin_lock_irqsave(&chip->lock, flags);
638 if (!(chip->image[CS4231_IFACE_CTRL] & CS4231_PLAYBACK_ENABLE)) {
639 snd_cs4231_out(chip, CS4231_PLAYBK_FORMAT,
640 ((chip->image[CS4231_PLAYBK_FORMAT]) & 0xf0) |
641 (cdfr & 0x0f));
642 spin_unlock_irqrestore(&chip->lock, flags);
643 snd_cs4231_mce_down(chip);
644 snd_cs4231_mce_up(chip);
645 spin_lock_irqsave(&chip->lock, flags);
646 }
647 snd_cs4231_out(chip, CS4231_REC_FORMAT, cdfr);
648 spin_unlock_irqrestore(&chip->lock, flags);
649
650 snd_cs4231_mce_down(chip);
651
652 snd_cs4231_calibrate_mute(chip, 0);
Ingo Molnar12aa7572006-01-16 16:36:05 +0100653 mutex_unlock(&chip->mce_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654}
655
656/*
657 * Timer interface
658 */
659
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100660static unsigned long snd_cs4231_timer_resolution(struct snd_timer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661{
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100662 struct snd_cs4231 *chip = snd_timer_chip(timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663
664 return chip->image[CS4231_PLAYBK_FORMAT] & 1 ? 9969 : 9920;
665}
666
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100667static int snd_cs4231_timer_start(struct snd_timer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668{
669 unsigned long flags;
670 unsigned int ticks;
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100671 struct snd_cs4231 *chip = snd_timer_chip(timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
673 spin_lock_irqsave(&chip->lock, flags);
674 ticks = timer->sticks;
675 if ((chip->image[CS4231_ALT_FEATURE_1] & CS4231_TIMER_ENABLE) == 0 ||
676 (unsigned char)(ticks >> 8) != chip->image[CS4231_TIMER_HIGH] ||
677 (unsigned char)ticks != chip->image[CS4231_TIMER_LOW]) {
678 snd_cs4231_out(chip, CS4231_TIMER_HIGH,
679 chip->image[CS4231_TIMER_HIGH] =
680 (unsigned char) (ticks >> 8));
681 snd_cs4231_out(chip, CS4231_TIMER_LOW,
682 chip->image[CS4231_TIMER_LOW] =
683 (unsigned char) ticks);
684 snd_cs4231_out(chip, CS4231_ALT_FEATURE_1,
Krzysztof Helt9e9abb42007-09-10 23:06:55 +0200685 chip->image[CS4231_ALT_FEATURE_1] |
686 CS4231_TIMER_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 }
688 spin_unlock_irqrestore(&chip->lock, flags);
689
690 return 0;
691}
692
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100693static int snd_cs4231_timer_stop(struct snd_timer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694{
695 unsigned long flags;
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100696 struct snd_cs4231 *chip = snd_timer_chip(timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
698 spin_lock_irqsave(&chip->lock, flags);
Krzysztof Helt9e9abb42007-09-10 23:06:55 +0200699 chip->image[CS4231_ALT_FEATURE_1] &= ~CS4231_TIMER_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 snd_cs4231_out(chip, CS4231_ALT_FEATURE_1,
Krzysztof Helt9e9abb42007-09-10 23:06:55 +0200701 chip->image[CS4231_ALT_FEATURE_1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 spin_unlock_irqrestore(&chip->lock, flags);
703
704 return 0;
705}
706
Bill Pemberton32e02a72012-12-06 12:35:25 -0500707static void snd_cs4231_init(struct snd_cs4231 *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708{
709 unsigned long flags;
710
711 snd_cs4231_mce_down(chip);
712
713#ifdef SNDRV_DEBUG_MCE
Christopher Zimmermanna1314302005-09-21 00:41:22 -0700714 snd_printdd("init: (1)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715#endif
716 snd_cs4231_mce_up(chip);
717 spin_lock_irqsave(&chip->lock, flags);
Krzysztof Helt9e9abb42007-09-10 23:06:55 +0200718 chip->image[CS4231_IFACE_CTRL] &= ~(CS4231_PLAYBACK_ENABLE |
719 CS4231_PLAYBACK_PIO |
720 CS4231_RECORD_ENABLE |
721 CS4231_RECORD_PIO |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 CS4231_CALIB_MODE);
723 chip->image[CS4231_IFACE_CTRL] |= CS4231_AUTOCALIB;
724 snd_cs4231_out(chip, CS4231_IFACE_CTRL, chip->image[CS4231_IFACE_CTRL]);
725 spin_unlock_irqrestore(&chip->lock, flags);
726 snd_cs4231_mce_down(chip);
727
728#ifdef SNDRV_DEBUG_MCE
Christopher Zimmermanna1314302005-09-21 00:41:22 -0700729 snd_printdd("init: (2)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730#endif
731
732 snd_cs4231_mce_up(chip);
733 spin_lock_irqsave(&chip->lock, flags);
Krzysztof Helt9e9abb42007-09-10 23:06:55 +0200734 snd_cs4231_out(chip, CS4231_ALT_FEATURE_1,
735 chip->image[CS4231_ALT_FEATURE_1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 spin_unlock_irqrestore(&chip->lock, flags);
737 snd_cs4231_mce_down(chip);
738
739#ifdef SNDRV_DEBUG_MCE
Krzysztof Helt9e9abb42007-09-10 23:06:55 +0200740 snd_printdd("init: (3) - afei = 0x%x\n",
741 chip->image[CS4231_ALT_FEATURE_1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742#endif
743
744 spin_lock_irqsave(&chip->lock, flags);
Krzysztof Helt9e9abb42007-09-10 23:06:55 +0200745 snd_cs4231_out(chip, CS4231_ALT_FEATURE_2,
746 chip->image[CS4231_ALT_FEATURE_2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 spin_unlock_irqrestore(&chip->lock, flags);
748
749 snd_cs4231_mce_up(chip);
750 spin_lock_irqsave(&chip->lock, flags);
Krzysztof Helt9e9abb42007-09-10 23:06:55 +0200751 snd_cs4231_out(chip, CS4231_PLAYBK_FORMAT,
752 chip->image[CS4231_PLAYBK_FORMAT]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 spin_unlock_irqrestore(&chip->lock, flags);
754 snd_cs4231_mce_down(chip);
755
756#ifdef SNDRV_DEBUG_MCE
Christopher Zimmermanna1314302005-09-21 00:41:22 -0700757 snd_printdd("init: (4)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758#endif
759
760 snd_cs4231_mce_up(chip);
761 spin_lock_irqsave(&chip->lock, flags);
762 snd_cs4231_out(chip, CS4231_REC_FORMAT, chip->image[CS4231_REC_FORMAT]);
763 spin_unlock_irqrestore(&chip->lock, flags);
764 snd_cs4231_mce_down(chip);
765
766#ifdef SNDRV_DEBUG_MCE
Christopher Zimmermanna1314302005-09-21 00:41:22 -0700767 snd_printdd("init: (5)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768#endif
769}
770
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100771static int snd_cs4231_open(struct snd_cs4231 *chip, unsigned int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772{
773 unsigned long flags;
774
Ingo Molnar12aa7572006-01-16 16:36:05 +0100775 mutex_lock(&chip->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 if ((chip->mode & mode)) {
Ingo Molnar12aa7572006-01-16 16:36:05 +0100777 mutex_unlock(&chip->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 return -EAGAIN;
779 }
780 if (chip->mode & CS4231_MODE_OPEN) {
781 chip->mode |= mode;
Ingo Molnar12aa7572006-01-16 16:36:05 +0100782 mutex_unlock(&chip->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 return 0;
784 }
785 /* ok. now enable and ack CODEC IRQ */
786 spin_lock_irqsave(&chip->lock, flags);
787 snd_cs4231_out(chip, CS4231_IRQ_STATUS, CS4231_PLAYBACK_IRQ |
788 CS4231_RECORD_IRQ |
789 CS4231_TIMER_IRQ);
790 snd_cs4231_out(chip, CS4231_IRQ_STATUS, 0);
Krzysztof Helt7e52f3d2007-09-05 15:08:23 +0200791 __cs4231_writeb(chip, 0, CS4231U(chip, STATUS)); /* clear IRQ */
792 __cs4231_writeb(chip, 0, CS4231U(chip, STATUS)); /* clear IRQ */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793
794 snd_cs4231_out(chip, CS4231_IRQ_STATUS, CS4231_PLAYBACK_IRQ |
795 CS4231_RECORD_IRQ |
796 CS4231_TIMER_IRQ);
797 snd_cs4231_out(chip, CS4231_IRQ_STATUS, 0);
Christopher Zimmermanna1314302005-09-21 00:41:22 -0700798
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 spin_unlock_irqrestore(&chip->lock, flags);
800
801 chip->mode = mode;
Ingo Molnar12aa7572006-01-16 16:36:05 +0100802 mutex_unlock(&chip->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 return 0;
804}
805
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100806static void snd_cs4231_close(struct snd_cs4231 *chip, unsigned int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807{
808 unsigned long flags;
809
Ingo Molnar12aa7572006-01-16 16:36:05 +0100810 mutex_lock(&chip->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 chip->mode &= ~mode;
812 if (chip->mode & CS4231_MODE_OPEN) {
Ingo Molnar12aa7572006-01-16 16:36:05 +0100813 mutex_unlock(&chip->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 return;
815 }
816 snd_cs4231_calibrate_mute(chip, 1);
817
818 /* disable IRQ */
819 spin_lock_irqsave(&chip->lock, flags);
820 snd_cs4231_out(chip, CS4231_IRQ_STATUS, 0);
Krzysztof Helt7e52f3d2007-09-05 15:08:23 +0200821 __cs4231_writeb(chip, 0, CS4231U(chip, STATUS)); /* clear IRQ */
822 __cs4231_writeb(chip, 0, CS4231U(chip, STATUS)); /* clear IRQ */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823
824 /* now disable record & playback */
825
826 if (chip->image[CS4231_IFACE_CTRL] &
827 (CS4231_PLAYBACK_ENABLE | CS4231_PLAYBACK_PIO |
828 CS4231_RECORD_ENABLE | CS4231_RECORD_PIO)) {
829 spin_unlock_irqrestore(&chip->lock, flags);
830 snd_cs4231_mce_up(chip);
831 spin_lock_irqsave(&chip->lock, flags);
832 chip->image[CS4231_IFACE_CTRL] &=
833 ~(CS4231_PLAYBACK_ENABLE | CS4231_PLAYBACK_PIO |
834 CS4231_RECORD_ENABLE | CS4231_RECORD_PIO);
Krzysztof Helt9e9abb42007-09-10 23:06:55 +0200835 snd_cs4231_out(chip, CS4231_IFACE_CTRL,
836 chip->image[CS4231_IFACE_CTRL]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 spin_unlock_irqrestore(&chip->lock, flags);
838 snd_cs4231_mce_down(chip);
839 spin_lock_irqsave(&chip->lock, flags);
840 }
841
842 /* clear IRQ again */
843 snd_cs4231_out(chip, CS4231_IRQ_STATUS, 0);
Krzysztof Helt7e52f3d2007-09-05 15:08:23 +0200844 __cs4231_writeb(chip, 0, CS4231U(chip, STATUS)); /* clear IRQ */
845 __cs4231_writeb(chip, 0, CS4231U(chip, STATUS)); /* clear IRQ */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 spin_unlock_irqrestore(&chip->lock, flags);
847
848 snd_cs4231_calibrate_mute(chip, 0);
849
850 chip->mode = 0;
Ingo Molnar12aa7572006-01-16 16:36:05 +0100851 mutex_unlock(&chip->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852}
853
854/*
855 * timer open/close
856 */
857
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100858static int snd_cs4231_timer_open(struct snd_timer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859{
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100860 struct snd_cs4231 *chip = snd_timer_chip(timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 snd_cs4231_open(chip, CS4231_MODE_TIMER);
862 return 0;
863}
864
Krzysztof Helt9e9abb42007-09-10 23:06:55 +0200865static int snd_cs4231_timer_close(struct snd_timer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866{
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100867 struct snd_cs4231 *chip = snd_timer_chip(timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 snd_cs4231_close(chip, CS4231_MODE_TIMER);
869 return 0;
870}
871
Takashi Iwai5ff16a32020-01-03 09:16:37 +0100872static const struct snd_timer_hardware snd_cs4231_timer_table = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 .flags = SNDRV_TIMER_HW_AUTO,
874 .resolution = 9945,
875 .ticks = 65535,
876 .open = snd_cs4231_timer_open,
877 .close = snd_cs4231_timer_close,
878 .c_resolution = snd_cs4231_timer_resolution,
879 .start = snd_cs4231_timer_start,
880 .stop = snd_cs4231_timer_stop,
881};
882
883/*
884 * ok.. exported functions..
885 */
886
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100887static int snd_cs4231_playback_hw_params(struct snd_pcm_substream *substream,
888 struct snd_pcm_hw_params *hw_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889{
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100890 struct snd_cs4231 *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 unsigned char new_pdfr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 new_pdfr = snd_cs4231_get_format(chip, params_format(hw_params),
894 params_channels(hw_params)) |
895 snd_cs4231_get_rate(params_rate(hw_params));
896 snd_cs4231_playback_format(chip, hw_params, new_pdfr);
897
898 return 0;
899}
900
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100901static int snd_cs4231_playback_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902{
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100903 struct snd_cs4231 *chip = snd_pcm_substream_chip(substream);
904 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 unsigned long flags;
Wei Yongjuna5224092013-11-11 22:21:33 +0800906 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907
908 spin_lock_irqsave(&chip->lock, flags);
Christopher Zimmermanna1314302005-09-21 00:41:22 -0700909
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 chip->image[CS4231_IFACE_CTRL] &= ~(CS4231_PLAYBACK_ENABLE |
911 CS4231_PLAYBACK_PIO);
Christopher Zimmermanna1314302005-09-21 00:41:22 -0700912
Wei Yongjuna5224092013-11-11 22:21:33 +0800913 if (WARN_ON(runtime->period_size > 0xffff + 1)) {
914 ret = -EINVAL;
915 goto out;
916 }
Christopher Zimmermanna1314302005-09-21 00:41:22 -0700917
Christopher Zimmermanna1314302005-09-21 00:41:22 -0700918 chip->p_periods_sent = 0;
Wei Yongjuna5224092013-11-11 22:21:33 +0800919
920out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 spin_unlock_irqrestore(&chip->lock, flags);
922
Wei Yongjuna5224092013-11-11 22:21:33 +0800923 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924}
925
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100926static int snd_cs4231_capture_hw_params(struct snd_pcm_substream *substream,
927 struct snd_pcm_hw_params *hw_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928{
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100929 struct snd_cs4231 *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 unsigned char new_cdfr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 new_cdfr = snd_cs4231_get_format(chip, params_format(hw_params),
933 params_channels(hw_params)) |
934 snd_cs4231_get_rate(params_rate(hw_params));
935 snd_cs4231_capture_format(chip, hw_params, new_cdfr);
936
937 return 0;
938}
939
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100940static int snd_cs4231_capture_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941{
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100942 struct snd_cs4231 *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 unsigned long flags;
944
945 spin_lock_irqsave(&chip->lock, flags);
946 chip->image[CS4231_IFACE_CTRL] &= ~(CS4231_RECORD_ENABLE |
947 CS4231_RECORD_PIO);
948
Christopher Zimmermanna1314302005-09-21 00:41:22 -0700949
Georg Chini5a820fa2005-11-07 14:08:25 -0800950 chip->c_periods_sent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 spin_unlock_irqrestore(&chip->lock, flags);
952
953 return 0;
954}
955
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100956static void snd_cs4231_overrange(struct snd_cs4231 *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957{
958 unsigned long flags;
959 unsigned char res;
960
961 spin_lock_irqsave(&chip->lock, flags);
962 res = snd_cs4231_in(chip, CS4231_TEST_INIT);
963 spin_unlock_irqrestore(&chip->lock, flags);
964
Krzysztof Helt9e9abb42007-09-10 23:06:55 +0200965 /* detect overrange only above 0dB; may be user selectable? */
966 if (res & (0x08 | 0x02))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 chip->capture_substream->runtime->overrange++;
968}
969
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100970static void snd_cs4231_play_callback(struct snd_cs4231 *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 if (chip->image[CS4231_IFACE_CTRL] & CS4231_PLAYBACK_ENABLE) {
973 snd_pcm_period_elapsed(chip->playback_substream);
Georg Chinib1282542005-11-07 14:09:19 -0800974 snd_cs4231_advance_dma(&chip->p_dma, chip->playback_substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 &chip->p_periods_sent);
976 }
977}
978
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100979static void snd_cs4231_capture_callback(struct snd_cs4231 *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 if (chip->image[CS4231_IFACE_CTRL] & CS4231_RECORD_ENABLE) {
982 snd_pcm_period_elapsed(chip->capture_substream);
Georg Chinib1282542005-11-07 14:09:19 -0800983 snd_cs4231_advance_dma(&chip->c_dma, chip->capture_substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 &chip->c_periods_sent);
985 }
986}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987
Krzysztof Helt9e9abb42007-09-10 23:06:55 +0200988static snd_pcm_uframes_t snd_cs4231_playback_pointer(
989 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990{
Takashi Iwaibe9b7e82006-01-03 13:42:38 +0100991 struct snd_cs4231 *chip = snd_pcm_substream_chip(substream);
992 struct cs4231_dma_control *dma_cont = &chip->p_dma;
Georg Chini5a820fa2005-11-07 14:08:25 -0800993 size_t ptr;
Krzysztof Helt9e9abb42007-09-10 23:06:55 +0200994
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 if (!(chip->image[CS4231_IFACE_CTRL] & CS4231_PLAYBACK_ENABLE))
996 return 0;
Georg Chinib1282542005-11-07 14:09:19 -0800997 ptr = dma_cont->address(dma_cont);
998 if (ptr != 0)
999 ptr -= substream->runtime->dma_addr;
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001000
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 return bytes_to_frames(substream->runtime, ptr);
1002}
1003
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001004static snd_pcm_uframes_t snd_cs4231_capture_pointer(
1005 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006{
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001007 struct snd_cs4231 *chip = snd_pcm_substream_chip(substream);
1008 struct cs4231_dma_control *dma_cont = &chip->c_dma;
Georg Chini5a820fa2005-11-07 14:08:25 -08001009 size_t ptr;
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001010
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 if (!(chip->image[CS4231_IFACE_CTRL] & CS4231_RECORD_ENABLE))
1012 return 0;
Georg Chinib1282542005-11-07 14:09:19 -08001013 ptr = dma_cont->address(dma_cont);
1014 if (ptr != 0)
1015 ptr -= substream->runtime->dma_addr;
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001016
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 return bytes_to_frames(substream->runtime, ptr);
1018}
1019
Bill Pemberton32e02a72012-12-06 12:35:25 -05001020static int snd_cs4231_probe(struct snd_cs4231 *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021{
1022 unsigned long flags;
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001023 int i;
1024 int id = 0;
1025 int vers = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 unsigned char *ptr;
1027
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 for (i = 0; i < 50; i++) {
1029 mb();
Krzysztof Helt7e52f3d2007-09-05 15:08:23 +02001030 if (__cs4231_readb(chip, CS4231U(chip, REGSEL)) & CS4231_INIT)
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001031 msleep(2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 else {
1033 spin_lock_irqsave(&chip->lock, flags);
1034 snd_cs4231_out(chip, CS4231_MISC_INFO, CS4231_MODE2);
1035 id = snd_cs4231_in(chip, CS4231_MISC_INFO) & 0x0f;
1036 vers = snd_cs4231_in(chip, CS4231_VERSION);
1037 spin_unlock_irqrestore(&chip->lock, flags);
1038 if (id == 0x0a)
1039 break; /* this is valid value */
1040 }
1041 }
1042 snd_printdd("cs4231: port = %p, id = 0x%x\n", chip->port, id);
1043 if (id != 0x0a)
1044 return -ENODEV; /* no valid device found */
1045
1046 spin_lock_irqsave(&chip->lock, flags);
1047
Krzysztof Helt7e52f3d2007-09-05 15:08:23 +02001048 /* clear any pendings IRQ */
1049 __cs4231_readb(chip, CS4231U(chip, STATUS));
1050 __cs4231_writeb(chip, 0, CS4231U(chip, STATUS));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 mb();
1052
1053 spin_unlock_irqrestore(&chip->lock, flags);
1054
1055 chip->image[CS4231_MISC_INFO] = CS4231_MODE2;
1056 chip->image[CS4231_IFACE_CTRL] =
1057 chip->image[CS4231_IFACE_CTRL] & ~CS4231_SINGLE_DMA;
1058 chip->image[CS4231_ALT_FEATURE_1] = 0x80;
1059 chip->image[CS4231_ALT_FEATURE_2] = 0x01;
1060 if (vers & 0x20)
1061 chip->image[CS4231_ALT_FEATURE_2] |= 0x02;
1062
1063 ptr = (unsigned char *) &chip->image;
1064
1065 snd_cs4231_mce_down(chip);
1066
1067 spin_lock_irqsave(&chip->lock, flags);
1068
1069 for (i = 0; i < 32; i++) /* ok.. fill all CS4231 registers */
1070 snd_cs4231_out(chip, i, *ptr++);
1071
1072 spin_unlock_irqrestore(&chip->lock, flags);
1073
1074 snd_cs4231_mce_up(chip);
1075
1076 snd_cs4231_mce_down(chip);
1077
1078 mdelay(2);
1079
1080 return 0; /* all things are ok.. */
1081}
1082
Bhumika Goyal688ed202017-08-17 14:45:58 +05301083static const struct snd_pcm_hardware snd_cs4231_playback = {
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001084 .info = SNDRV_PCM_INFO_MMAP |
1085 SNDRV_PCM_INFO_INTERLEAVED |
1086 SNDRV_PCM_INFO_MMAP_VALID |
1087 SNDRV_PCM_INFO_SYNC_START,
1088 .formats = SNDRV_PCM_FMTBIT_MU_LAW |
1089 SNDRV_PCM_FMTBIT_A_LAW |
1090 SNDRV_PCM_FMTBIT_IMA_ADPCM |
1091 SNDRV_PCM_FMTBIT_U8 |
1092 SNDRV_PCM_FMTBIT_S16_LE |
1093 SNDRV_PCM_FMTBIT_S16_BE,
1094 .rates = SNDRV_PCM_RATE_KNOT |
1095 SNDRV_PCM_RATE_8000_48000,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 .rate_min = 5510,
1097 .rate_max = 48000,
1098 .channels_min = 1,
1099 .channels_max = 2,
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001100 .buffer_bytes_max = 32 * 1024,
David S. Millerf9af1d92007-01-03 18:51:54 -08001101 .period_bytes_min = 64,
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001102 .period_bytes_max = 32 * 1024,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 .periods_min = 1,
1104 .periods_max = 1024,
1105};
1106
Bhumika Goyal688ed202017-08-17 14:45:58 +05301107static const struct snd_pcm_hardware snd_cs4231_capture = {
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001108 .info = SNDRV_PCM_INFO_MMAP |
1109 SNDRV_PCM_INFO_INTERLEAVED |
1110 SNDRV_PCM_INFO_MMAP_VALID |
1111 SNDRV_PCM_INFO_SYNC_START,
1112 .formats = SNDRV_PCM_FMTBIT_MU_LAW |
1113 SNDRV_PCM_FMTBIT_A_LAW |
1114 SNDRV_PCM_FMTBIT_IMA_ADPCM |
1115 SNDRV_PCM_FMTBIT_U8 |
1116 SNDRV_PCM_FMTBIT_S16_LE |
1117 SNDRV_PCM_FMTBIT_S16_BE,
1118 .rates = SNDRV_PCM_RATE_KNOT |
1119 SNDRV_PCM_RATE_8000_48000,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 .rate_min = 5510,
1121 .rate_max = 48000,
1122 .channels_min = 1,
1123 .channels_max = 2,
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001124 .buffer_bytes_max = 32 * 1024,
David S. Millerf9af1d92007-01-03 18:51:54 -08001125 .period_bytes_min = 64,
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001126 .period_bytes_max = 32 * 1024,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 .periods_min = 1,
1128 .periods_max = 1024,
1129};
1130
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001131static int snd_cs4231_playback_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132{
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001133 struct snd_cs4231 *chip = snd_pcm_substream_chip(substream);
1134 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 int err;
1136
1137 runtime->hw = snd_cs4231_playback;
1138
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001139 err = snd_cs4231_open(chip, CS4231_MODE_PLAY);
Takashi Iwai9a203322018-11-23 18:18:30 +01001140 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 chip->playback_substream = substream;
1143 chip->p_periods_sent = 0;
1144 snd_pcm_set_sync(substream);
1145 snd_cs4231_xrate(runtime);
1146
1147 return 0;
1148}
1149
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001150static int snd_cs4231_capture_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151{
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001152 struct snd_cs4231 *chip = snd_pcm_substream_chip(substream);
1153 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 int err;
1155
1156 runtime->hw = snd_cs4231_capture;
1157
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001158 err = snd_cs4231_open(chip, CS4231_MODE_RECORD);
Takashi Iwai9a203322018-11-23 18:18:30 +01001159 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 chip->capture_substream = substream;
1162 chip->c_periods_sent = 0;
1163 snd_pcm_set_sync(substream);
1164 snd_cs4231_xrate(runtime);
1165
1166 return 0;
1167}
1168
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001169static int snd_cs4231_playback_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170{
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001171 struct snd_cs4231 *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 snd_cs4231_close(chip, CS4231_MODE_PLAY);
Georg Chinib1282542005-11-07 14:09:19 -08001174 chip->playback_substream = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175
1176 return 0;
1177}
1178
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001179static int snd_cs4231_capture_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180{
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001181 struct snd_cs4231 *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 snd_cs4231_close(chip, CS4231_MODE_RECORD);
Georg Chinib1282542005-11-07 14:09:19 -08001184 chip->capture_substream = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185
1186 return 0;
1187}
1188
1189/* XXX We can do some power-management, in particular on EBUS using
1190 * XXX the audio AUXIO register...
1191 */
1192
Arvind Yadav544d6272017-08-18 13:15:19 +05301193static const struct snd_pcm_ops snd_cs4231_playback_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 .open = snd_cs4231_playback_open,
1195 .close = snd_cs4231_playback_close,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 .hw_params = snd_cs4231_playback_hw_params,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 .prepare = snd_cs4231_playback_prepare,
1198 .trigger = snd_cs4231_trigger,
1199 .pointer = snd_cs4231_playback_pointer,
1200};
1201
Arvind Yadav544d6272017-08-18 13:15:19 +05301202static const struct snd_pcm_ops snd_cs4231_capture_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 .open = snd_cs4231_capture_open,
1204 .close = snd_cs4231_capture_close,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 .hw_params = snd_cs4231_capture_hw_params,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 .prepare = snd_cs4231_capture_prepare,
1207 .trigger = snd_cs4231_trigger,
1208 .pointer = snd_cs4231_capture_pointer,
1209};
1210
Bill Pemberton32e02a72012-12-06 12:35:25 -05001211static int snd_cs4231_pcm(struct snd_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212{
Krzysztof Heltc6c2d572007-09-04 13:08:24 +02001213 struct snd_cs4231 *chip = card->private_data;
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001214 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 int err;
1216
Krzysztof Heltc6c2d572007-09-04 13:08:24 +02001217 err = snd_pcm_new(card, "CS4231", 0, 1, 1, &pcm);
1218 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 return err;
1220
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001221 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
1222 &snd_cs4231_playback_ops);
1223 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
1224 &snd_cs4231_capture_ops);
1225
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 /* global setup */
1227 pcm->private_data = chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 pcm->info_flags = SNDRV_PCM_INFO_JOINT_DUPLEX;
1229 strcpy(pcm->name, "CS4231");
1230
Takashi Iwai786e90b2019-12-09 10:49:34 +01001231 snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV,
1232 &chip->op->dev, 64 * 1024, 128 * 1024);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233
1234 chip->pcm = pcm;
1235
1236 return 0;
1237}
1238
Bill Pemberton32e02a72012-12-06 12:35:25 -05001239static int snd_cs4231_timer(struct snd_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240{
Krzysztof Heltc6c2d572007-09-04 13:08:24 +02001241 struct snd_cs4231 *chip = card->private_data;
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001242 struct snd_timer *timer;
1243 struct snd_timer_id tid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 int err;
1245
1246 /* Timer initialization */
1247 tid.dev_class = SNDRV_TIMER_CLASS_CARD;
1248 tid.dev_sclass = SNDRV_TIMER_SCLASS_NONE;
Krzysztof Heltc6c2d572007-09-04 13:08:24 +02001249 tid.card = card->number;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 tid.device = 0;
1251 tid.subdevice = 0;
Krzysztof Heltc6c2d572007-09-04 13:08:24 +02001252 err = snd_timer_new(card, "CS4231", &tid, &timer);
1253 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 return err;
1255 strcpy(timer->name, "CS4231");
1256 timer->private_data = chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 timer->hw = snd_cs4231_timer_table;
1258 chip->timer = timer;
1259
1260 return 0;
1261}
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001262
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263/*
1264 * MIXER part
1265 */
1266
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001267static int snd_cs4231_info_mux(struct snd_kcontrol *kcontrol,
1268 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269{
Takashi Iwai5fe0b0e2014-10-20 18:21:15 +02001270 static const char * const texts[4] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 "Line", "CD", "Mic", "Mix"
1272 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273
Takashi Iwai5fe0b0e2014-10-20 18:21:15 +02001274 return snd_ctl_enum_info(uinfo, 2, 4, texts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275}
1276
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001277static int snd_cs4231_get_mux(struct snd_kcontrol *kcontrol,
1278 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279{
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001280 struct snd_cs4231 *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 unsigned long flags;
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001282
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 spin_lock_irqsave(&chip->lock, flags);
1284 ucontrol->value.enumerated.item[0] =
1285 (chip->image[CS4231_LEFT_INPUT] & CS4231_MIXS_ALL) >> 6;
1286 ucontrol->value.enumerated.item[1] =
1287 (chip->image[CS4231_RIGHT_INPUT] & CS4231_MIXS_ALL) >> 6;
1288 spin_unlock_irqrestore(&chip->lock, flags);
1289
1290 return 0;
1291}
1292
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001293static int snd_cs4231_put_mux(struct snd_kcontrol *kcontrol,
1294 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295{
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001296 struct snd_cs4231 *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 unsigned long flags;
1298 unsigned short left, right;
1299 int change;
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001300
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 if (ucontrol->value.enumerated.item[0] > 3 ||
1302 ucontrol->value.enumerated.item[1] > 3)
1303 return -EINVAL;
1304 left = ucontrol->value.enumerated.item[0] << 6;
1305 right = ucontrol->value.enumerated.item[1] << 6;
1306
1307 spin_lock_irqsave(&chip->lock, flags);
1308
1309 left = (chip->image[CS4231_LEFT_INPUT] & ~CS4231_MIXS_ALL) | left;
1310 right = (chip->image[CS4231_RIGHT_INPUT] & ~CS4231_MIXS_ALL) | right;
1311 change = left != chip->image[CS4231_LEFT_INPUT] ||
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001312 right != chip->image[CS4231_RIGHT_INPUT];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 snd_cs4231_out(chip, CS4231_LEFT_INPUT, left);
1314 snd_cs4231_out(chip, CS4231_RIGHT_INPUT, right);
1315
1316 spin_unlock_irqrestore(&chip->lock, flags);
1317
1318 return change;
1319}
1320
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001321static int snd_cs4231_info_single(struct snd_kcontrol *kcontrol,
1322 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323{
1324 int mask = (kcontrol->private_value >> 16) & 0xff;
1325
1326 uinfo->type = (mask == 1) ?
1327 SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
1328 uinfo->count = 1;
1329 uinfo->value.integer.min = 0;
1330 uinfo->value.integer.max = mask;
1331
1332 return 0;
1333}
1334
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001335static int snd_cs4231_get_single(struct snd_kcontrol *kcontrol,
1336 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337{
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001338 struct snd_cs4231 *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 unsigned long flags;
1340 int reg = kcontrol->private_value & 0xff;
1341 int shift = (kcontrol->private_value >> 8) & 0xff;
1342 int mask = (kcontrol->private_value >> 16) & 0xff;
1343 int invert = (kcontrol->private_value >> 24) & 0xff;
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001344
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 spin_lock_irqsave(&chip->lock, flags);
1346
1347 ucontrol->value.integer.value[0] = (chip->image[reg] >> shift) & mask;
1348
1349 spin_unlock_irqrestore(&chip->lock, flags);
1350
1351 if (invert)
1352 ucontrol->value.integer.value[0] =
1353 (mask - ucontrol->value.integer.value[0]);
1354
1355 return 0;
1356}
1357
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001358static int snd_cs4231_put_single(struct snd_kcontrol *kcontrol,
1359 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360{
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001361 struct snd_cs4231 *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 unsigned long flags;
1363 int reg = kcontrol->private_value & 0xff;
1364 int shift = (kcontrol->private_value >> 8) & 0xff;
1365 int mask = (kcontrol->private_value >> 16) & 0xff;
1366 int invert = (kcontrol->private_value >> 24) & 0xff;
1367 int change;
1368 unsigned short val;
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001369
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 val = (ucontrol->value.integer.value[0] & mask);
1371 if (invert)
1372 val = mask - val;
1373 val <<= shift;
1374
1375 spin_lock_irqsave(&chip->lock, flags);
1376
1377 val = (chip->image[reg] & ~(mask << shift)) | val;
1378 change = val != chip->image[reg];
1379 snd_cs4231_out(chip, reg, val);
1380
1381 spin_unlock_irqrestore(&chip->lock, flags);
1382
1383 return change;
1384}
1385
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001386static int snd_cs4231_info_double(struct snd_kcontrol *kcontrol,
1387 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388{
1389 int mask = (kcontrol->private_value >> 24) & 0xff;
1390
1391 uinfo->type = mask == 1 ?
1392 SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
1393 uinfo->count = 2;
1394 uinfo->value.integer.min = 0;
1395 uinfo->value.integer.max = mask;
1396
1397 return 0;
1398}
1399
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001400static int snd_cs4231_get_double(struct snd_kcontrol *kcontrol,
1401 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402{
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001403 struct snd_cs4231 *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 unsigned long flags;
1405 int left_reg = kcontrol->private_value & 0xff;
1406 int right_reg = (kcontrol->private_value >> 8) & 0xff;
1407 int shift_left = (kcontrol->private_value >> 16) & 0x07;
1408 int shift_right = (kcontrol->private_value >> 19) & 0x07;
1409 int mask = (kcontrol->private_value >> 24) & 0xff;
1410 int invert = (kcontrol->private_value >> 22) & 1;
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001411
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 spin_lock_irqsave(&chip->lock, flags);
1413
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001414 ucontrol->value.integer.value[0] =
1415 (chip->image[left_reg] >> shift_left) & mask;
1416 ucontrol->value.integer.value[1] =
1417 (chip->image[right_reg] >> shift_right) & mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418
1419 spin_unlock_irqrestore(&chip->lock, flags);
1420
1421 if (invert) {
1422 ucontrol->value.integer.value[0] =
1423 (mask - ucontrol->value.integer.value[0]);
1424 ucontrol->value.integer.value[1] =
1425 (mask - ucontrol->value.integer.value[1]);
1426 }
1427
1428 return 0;
1429}
1430
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001431static int snd_cs4231_put_double(struct snd_kcontrol *kcontrol,
1432 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433{
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001434 struct snd_cs4231 *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 unsigned long flags;
1436 int left_reg = kcontrol->private_value & 0xff;
1437 int right_reg = (kcontrol->private_value >> 8) & 0xff;
1438 int shift_left = (kcontrol->private_value >> 16) & 0x07;
1439 int shift_right = (kcontrol->private_value >> 19) & 0x07;
1440 int mask = (kcontrol->private_value >> 24) & 0xff;
1441 int invert = (kcontrol->private_value >> 22) & 1;
1442 int change;
1443 unsigned short val1, val2;
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001444
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 val1 = ucontrol->value.integer.value[0] & mask;
1446 val2 = ucontrol->value.integer.value[1] & mask;
1447 if (invert) {
1448 val1 = mask - val1;
1449 val2 = mask - val2;
1450 }
1451 val1 <<= shift_left;
1452 val2 <<= shift_right;
1453
1454 spin_lock_irqsave(&chip->lock, flags);
1455
1456 val1 = (chip->image[left_reg] & ~(mask << shift_left)) | val1;
1457 val2 = (chip->image[right_reg] & ~(mask << shift_right)) | val2;
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001458 change = val1 != chip->image[left_reg];
1459 change |= val2 != chip->image[right_reg];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 snd_cs4231_out(chip, left_reg, val1);
1461 snd_cs4231_out(chip, right_reg, val2);
1462
1463 spin_unlock_irqrestore(&chip->lock, flags);
1464
1465 return change;
1466}
1467
1468#define CS4231_SINGLE(xname, xindex, reg, shift, mask, invert) \
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001469{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = (xname), .index = (xindex), \
1470 .info = snd_cs4231_info_single, \
1471 .get = snd_cs4231_get_single, .put = snd_cs4231_put_single, \
1472 .private_value = (reg) | ((shift) << 8) | ((mask) << 16) | ((invert) << 24) }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001474#define CS4231_DOUBLE(xname, xindex, left_reg, right_reg, shift_left, \
1475 shift_right, mask, invert) \
1476{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = (xname), .index = (xindex), \
1477 .info = snd_cs4231_info_double, \
1478 .get = snd_cs4231_get_double, .put = snd_cs4231_put_double, \
1479 .private_value = (left_reg) | ((right_reg) << 8) | ((shift_left) << 16) | \
1480 ((shift_right) << 19) | ((mask) << 24) | ((invert) << 22) }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481
Takashi Iwaif8a32d92020-01-03 09:16:55 +01001482static const struct snd_kcontrol_new snd_cs4231_controls[] = {
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001483CS4231_DOUBLE("PCM Playback Switch", 0, CS4231_LEFT_OUTPUT,
1484 CS4231_RIGHT_OUTPUT, 7, 7, 1, 1),
1485CS4231_DOUBLE("PCM Playback Volume", 0, CS4231_LEFT_OUTPUT,
1486 CS4231_RIGHT_OUTPUT, 0, 0, 63, 1),
1487CS4231_DOUBLE("Line Playback Switch", 0, CS4231_LEFT_LINE_IN,
1488 CS4231_RIGHT_LINE_IN, 7, 7, 1, 1),
1489CS4231_DOUBLE("Line Playback Volume", 0, CS4231_LEFT_LINE_IN,
1490 CS4231_RIGHT_LINE_IN, 0, 0, 31, 1),
1491CS4231_DOUBLE("Aux Playback Switch", 0, CS4231_AUX1_LEFT_INPUT,
1492 CS4231_AUX1_RIGHT_INPUT, 7, 7, 1, 1),
1493CS4231_DOUBLE("Aux Playback Volume", 0, CS4231_AUX1_LEFT_INPUT,
1494 CS4231_AUX1_RIGHT_INPUT, 0, 0, 31, 1),
1495CS4231_DOUBLE("Aux Playback Switch", 1, CS4231_AUX2_LEFT_INPUT,
1496 CS4231_AUX2_RIGHT_INPUT, 7, 7, 1, 1),
1497CS4231_DOUBLE("Aux Playback Volume", 1, CS4231_AUX2_LEFT_INPUT,
1498 CS4231_AUX2_RIGHT_INPUT, 0, 0, 31, 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499CS4231_SINGLE("Mono Playback Switch", 0, CS4231_MONO_CTRL, 7, 1, 1),
1500CS4231_SINGLE("Mono Playback Volume", 0, CS4231_MONO_CTRL, 0, 15, 1),
1501CS4231_SINGLE("Mono Output Playback Switch", 0, CS4231_MONO_CTRL, 6, 1, 1),
1502CS4231_SINGLE("Mono Output Playback Bypass", 0, CS4231_MONO_CTRL, 5, 1, 0),
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001503CS4231_DOUBLE("Capture Volume", 0, CS4231_LEFT_INPUT, CS4231_RIGHT_INPUT, 0, 0,
1504 15, 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505{
1506 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1507 .name = "Capture Source",
1508 .info = snd_cs4231_info_mux,
1509 .get = snd_cs4231_get_mux,
1510 .put = snd_cs4231_put_mux,
1511},
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001512CS4231_DOUBLE("Mic Boost", 0, CS4231_LEFT_INPUT, CS4231_RIGHT_INPUT, 5, 5,
1513 1, 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514CS4231_SINGLE("Loopback Capture Switch", 0, CS4231_LOOPBACK, 0, 1, 0),
1515CS4231_SINGLE("Loopback Capture Volume", 0, CS4231_LOOPBACK, 2, 63, 1),
1516/* SPARC specific uses of XCTL{0,1} general purpose outputs. */
1517CS4231_SINGLE("Line Out Switch", 0, CS4231_PIN_CTRL, 6, 1, 1),
1518CS4231_SINGLE("Headphone Out Switch", 0, CS4231_PIN_CTRL, 7, 1, 1)
1519};
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001520
Bill Pemberton32e02a72012-12-06 12:35:25 -05001521static int snd_cs4231_mixer(struct snd_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522{
Krzysztof Heltc6c2d572007-09-04 13:08:24 +02001523 struct snd_cs4231 *chip = card->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 int err, idx;
1525
Takashi Iwai5e246b82008-08-08 17:12:47 +02001526 if (snd_BUG_ON(!chip || !chip->pcm))
1527 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 strcpy(card->mixername, chip->pcm->name);
1530
1531 for (idx = 0; idx < ARRAY_SIZE(snd_cs4231_controls); idx++) {
Krzysztof Heltc6c2d572007-09-04 13:08:24 +02001532 err = snd_ctl_add(card,
1533 snd_ctl_new1(&snd_cs4231_controls[idx], chip));
1534 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 return err;
1536 }
1537 return 0;
1538}
1539
1540static int dev;
1541
Takashi Iwaia2fefc32014-01-29 14:41:09 +01001542static int cs4231_attach_begin(struct platform_device *op,
1543 struct snd_card **rcard)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544{
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001545 struct snd_card *card;
Krzysztof Heltc6c2d572007-09-04 13:08:24 +02001546 struct snd_cs4231 *chip;
Takashi Iwaibd7dd772008-12-28 16:45:02 +01001547 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548
1549 *rcard = NULL;
1550
1551 if (dev >= SNDRV_CARDS)
1552 return -ENODEV;
1553
1554 if (!enable[dev]) {
1555 dev++;
1556 return -ENOENT;
1557 }
1558
Takashi Iwaia2fefc32014-01-29 14:41:09 +01001559 err = snd_card_new(&op->dev, index[dev], id[dev], THIS_MODULE,
1560 sizeof(struct snd_cs4231), &card);
Takashi Iwaibd7dd772008-12-28 16:45:02 +01001561 if (err < 0)
1562 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563
1564 strcpy(card->driver, "CS4231");
1565 strcpy(card->shortname, "Sun CS4231");
1566
Krzysztof Heltc6c2d572007-09-04 13:08:24 +02001567 chip = card->private_data;
1568 chip->card = card;
1569
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 *rcard = card;
1571 return 0;
1572}
1573
Bill Pemberton32e02a72012-12-06 12:35:25 -05001574static int cs4231_attach_finish(struct snd_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575{
Krzysztof Heltc6c2d572007-09-04 13:08:24 +02001576 struct snd_cs4231 *chip = card->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 int err;
1578
Krzysztof Heltc6c2d572007-09-04 13:08:24 +02001579 err = snd_cs4231_pcm(card);
1580 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 goto out_err;
1582
Krzysztof Heltc6c2d572007-09-04 13:08:24 +02001583 err = snd_cs4231_mixer(card);
1584 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 goto out_err;
1586
Krzysztof Heltc6c2d572007-09-04 13:08:24 +02001587 err = snd_cs4231_timer(card);
1588 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 goto out_err;
1590
Krzysztof Heltc6c2d572007-09-04 13:08:24 +02001591 err = snd_card_register(card);
1592 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 goto out_err;
1594
David S. Millerafc88ad2008-08-30 00:13:55 -07001595 dev_set_drvdata(&chip->op->dev, chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596
1597 dev++;
1598 return 0;
1599
1600out_err:
1601 snd_card_free(card);
1602 return err;
1603}
1604
1605#ifdef SBUS_SUPPORT
Georg Chinib1282542005-11-07 14:09:19 -08001606
David Howells7d12e782006-10-05 14:55:46 +01001607static irqreturn_t snd_cs4231_sbus_interrupt(int irq, void *dev_id)
Georg Chinib1282542005-11-07 14:09:19 -08001608{
1609 unsigned long flags;
1610 unsigned char status;
1611 u32 csr;
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001612 struct snd_cs4231 *chip = dev_id;
Georg Chinib1282542005-11-07 14:09:19 -08001613
1614 /*This is IRQ is not raised by the cs4231*/
Krzysztof Helt7e52f3d2007-09-05 15:08:23 +02001615 if (!(__cs4231_readb(chip, CS4231U(chip, STATUS)) & CS4231_GLOBALIRQ))
Georg Chinib1282542005-11-07 14:09:19 -08001616 return IRQ_NONE;
1617
1618 /* ACK the APC interrupt. */
1619 csr = sbus_readl(chip->port + APCCSR);
1620
1621 sbus_writel(csr, chip->port + APCCSR);
1622
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001623 if ((csr & APC_PDMA_READY) &&
1624 (csr & APC_PLAY_INT) &&
Georg Chinib1282542005-11-07 14:09:19 -08001625 (csr & APC_XINT_PNVA) &&
1626 !(csr & APC_XINT_EMPT))
1627 snd_cs4231_play_callback(chip);
1628
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001629 if ((csr & APC_CDMA_READY) &&
1630 (csr & APC_CAPT_INT) &&
Georg Chinib1282542005-11-07 14:09:19 -08001631 (csr & APC_XINT_CNVA) &&
1632 !(csr & APC_XINT_EMPT))
1633 snd_cs4231_capture_callback(chip);
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001634
Georg Chinib1282542005-11-07 14:09:19 -08001635 status = snd_cs4231_in(chip, CS4231_IRQ_STATUS);
1636
1637 if (status & CS4231_TIMER_IRQ) {
1638 if (chip->timer)
1639 snd_timer_interrupt(chip->timer, chip->timer->sticks);
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001640 }
Georg Chinib1282542005-11-07 14:09:19 -08001641
1642 if ((status & CS4231_RECORD_IRQ) && (csr & APC_CDMA_READY))
1643 snd_cs4231_overrange(chip);
1644
1645 /* ACK the CS4231 interrupt. */
1646 spin_lock_irqsave(&chip->lock, flags);
1647 snd_cs4231_outm(chip, CS4231_IRQ_STATUS, ~CS4231_ALL_IRQS | ~status, 0);
1648 spin_unlock_irqrestore(&chip->lock, flags);
1649
Georg Chinid35a1b92007-01-02 21:28:17 -08001650 return IRQ_HANDLED;
Georg Chinib1282542005-11-07 14:09:19 -08001651}
1652
1653/*
1654 * SBUS DMA routines
1655 */
1656
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001657static int sbus_dma_request(struct cs4231_dma_control *dma_cont,
1658 dma_addr_t bus_addr, size_t len)
Georg Chinib1282542005-11-07 14:09:19 -08001659{
1660 unsigned long flags;
1661 u32 test, csr;
1662 int err;
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001663 struct sbus_dma_info *base = &dma_cont->sbus_info;
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001664
Georg Chinib1282542005-11-07 14:09:19 -08001665 if (len >= (1 << 24))
1666 return -EINVAL;
1667 spin_lock_irqsave(&base->lock, flags);
1668 csr = sbus_readl(base->regs + APCCSR);
1669 err = -EINVAL;
1670 test = APC_CDMA_READY;
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001671 if (base->dir == APC_PLAY)
Georg Chinib1282542005-11-07 14:09:19 -08001672 test = APC_PDMA_READY;
1673 if (!(csr & test))
1674 goto out;
1675 err = -EBUSY;
Georg Chinib1282542005-11-07 14:09:19 -08001676 test = APC_XINT_CNVA;
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001677 if (base->dir == APC_PLAY)
Georg Chinib1282542005-11-07 14:09:19 -08001678 test = APC_XINT_PNVA;
1679 if (!(csr & test))
1680 goto out;
1681 err = 0;
1682 sbus_writel(bus_addr, base->regs + base->dir + APCNVA);
1683 sbus_writel(len, base->regs + base->dir + APCNC);
1684out:
1685 spin_unlock_irqrestore(&base->lock, flags);
1686 return err;
1687}
1688
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001689static void sbus_dma_prepare(struct cs4231_dma_control *dma_cont, int d)
Georg Chinib1282542005-11-07 14:09:19 -08001690{
1691 unsigned long flags;
1692 u32 csr, test;
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001693 struct sbus_dma_info *base = &dma_cont->sbus_info;
Georg Chinib1282542005-11-07 14:09:19 -08001694
1695 spin_lock_irqsave(&base->lock, flags);
1696 csr = sbus_readl(base->regs + APCCSR);
1697 test = APC_GENL_INT | APC_PLAY_INT | APC_XINT_ENA |
1698 APC_XINT_PLAY | APC_XINT_PEMP | APC_XINT_GENL |
1699 APC_XINT_PENA;
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001700 if (base->dir == APC_RECORD)
Georg Chinib1282542005-11-07 14:09:19 -08001701 test = APC_GENL_INT | APC_CAPT_INT | APC_XINT_ENA |
1702 APC_XINT_CAPT | APC_XINT_CEMP | APC_XINT_GENL;
1703 csr |= test;
1704 sbus_writel(csr, base->regs + APCCSR);
1705 spin_unlock_irqrestore(&base->lock, flags);
1706}
1707
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001708static void sbus_dma_enable(struct cs4231_dma_control *dma_cont, int on)
Georg Chinib1282542005-11-07 14:09:19 -08001709{
1710 unsigned long flags;
1711 u32 csr, shift;
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 if (!on) {
Georg Chinid35a1b92007-01-02 21:28:17 -08001716 sbus_writel(0, base->regs + base->dir + APCNC);
1717 sbus_writel(0, base->regs + base->dir + APCNVA);
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001718 if (base->dir == APC_PLAY) {
Georg Chini3daadf32007-08-01 21:55:58 -07001719 sbus_writel(0, base->regs + base->dir + APCC);
1720 sbus_writel(0, base->regs + base->dir + APCVA);
1721 }
Georg Chinid35a1b92007-01-02 21:28:17 -08001722
Georg Chini3daadf32007-08-01 21:55:58 -07001723 udelay(1200);
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001724 }
Georg Chinib1282542005-11-07 14:09:19 -08001725 csr = sbus_readl(base->regs + APCCSR);
1726 shift = 0;
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001727 if (base->dir == APC_PLAY)
Georg Chinib1282542005-11-07 14:09:19 -08001728 shift = 1;
1729 if (on)
1730 csr &= ~(APC_CPAUSE << shift);
1731 else
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001732 csr |= (APC_CPAUSE << shift);
Georg Chinib1282542005-11-07 14:09:19 -08001733 sbus_writel(csr, base->regs + APCCSR);
1734 if (on)
1735 csr |= (APC_CDMA_READY << shift);
1736 else
1737 csr &= ~(APC_CDMA_READY << shift);
1738 sbus_writel(csr, base->regs + APCCSR);
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001739
Georg Chinib1282542005-11-07 14:09:19 -08001740 spin_unlock_irqrestore(&base->lock, flags);
1741}
1742
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001743static unsigned int sbus_dma_addr(struct cs4231_dma_control *dma_cont)
Georg Chinib1282542005-11-07 14:09:19 -08001744{
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001745 struct sbus_dma_info *base = &dma_cont->sbus_info;
Georg Chinib1282542005-11-07 14:09:19 -08001746
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001747 return sbus_readl(base->regs + base->dir + APCVA);
Georg Chinib1282542005-11-07 14:09:19 -08001748}
1749
Georg Chinib1282542005-11-07 14:09:19 -08001750/*
1751 * Init and exit routines
1752 */
1753
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001754static int snd_cs4231_sbus_free(struct snd_cs4231 *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755{
Grant Likely2dc11582010-08-06 09:25:50 -06001756 struct platform_device *op = chip->op;
David S. Millerae251032008-08-27 18:24:01 -07001757
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 if (chip->irq[0])
1759 free_irq(chip->irq[0], chip);
1760
1761 if (chip->port)
David S. Millerae251032008-08-27 18:24:01 -07001762 of_iounmap(&op->resource[0], chip->port, chip->regs_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 return 0;
1765}
1766
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001767static int snd_cs4231_sbus_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768{
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001769 struct snd_cs4231 *cp = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770
1771 return snd_cs4231_sbus_free(cp);
1772}
1773
Takashi Iwaib75851d2020-01-03 09:16:32 +01001774static const struct snd_device_ops snd_cs4231_sbus_dev_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 .dev_free = snd_cs4231_sbus_dev_free,
1776};
1777
Bill Pemberton32e02a72012-12-06 12:35:25 -05001778static int snd_cs4231_sbus_create(struct snd_card *card,
1779 struct platform_device *op,
1780 int dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781{
Krzysztof Heltc6c2d572007-09-04 13:08:24 +02001782 struct snd_cs4231 *chip = card->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 int err;
1784
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 spin_lock_init(&chip->lock);
Georg Chinib1282542005-11-07 14:09:19 -08001786 spin_lock_init(&chip->c_dma.sbus_info.lock);
1787 spin_lock_init(&chip->p_dma.sbus_info.lock);
Ingo Molnar12aa7572006-01-16 16:36:05 +01001788 mutex_init(&chip->mce_mutex);
1789 mutex_init(&chip->open_mutex);
David S. Millerafc88ad2008-08-30 00:13:55 -07001790 chip->op = op;
David S. Millerae251032008-08-27 18:24:01 -07001791 chip->regs_size = resource_size(&op->resource[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 memcpy(&chip->image, &snd_cs4231_original_image,
1793 sizeof(snd_cs4231_original_image));
1794
David S. Millerae251032008-08-27 18:24:01 -07001795 chip->port = of_ioremap(&op->resource[0], 0,
1796 chip->regs_size, "cs4231");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 if (!chip->port) {
Christopher Zimmermanna1314302005-09-21 00:41:22 -07001798 snd_printdd("cs4231-%d: Unable to map chip registers.\n", dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 return -EIO;
1800 }
1801
Georg Chinib1282542005-11-07 14:09:19 -08001802 chip->c_dma.sbus_info.regs = chip->port;
1803 chip->p_dma.sbus_info.regs = chip->port;
1804 chip->c_dma.sbus_info.dir = APC_RECORD;
1805 chip->p_dma.sbus_info.dir = APC_PLAY;
1806
1807 chip->p_dma.prepare = sbus_dma_prepare;
1808 chip->p_dma.enable = sbus_dma_enable;
1809 chip->p_dma.request = sbus_dma_request;
1810 chip->p_dma.address = sbus_dma_addr;
Georg Chinib1282542005-11-07 14:09:19 -08001811
1812 chip->c_dma.prepare = sbus_dma_prepare;
1813 chip->c_dma.enable = sbus_dma_enable;
1814 chip->c_dma.request = sbus_dma_request;
1815 chip->c_dma.address = sbus_dma_addr;
Georg Chini5a820fa2005-11-07 14:08:25 -08001816
Grant Likely1636f8a2010-06-18 11:09:58 -06001817 if (request_irq(op->archdata.irqs[0], snd_cs4231_sbus_interrupt,
Thomas Gleixner65ca68b2006-07-01 19:29:46 -07001818 IRQF_SHARED, "cs4231", chip)) {
David S. Millerc6387a42006-06-20 01:21:29 -07001819 snd_printdd("cs4231-%d: Unable to grab SBUS IRQ %d\n",
Grant Likely1636f8a2010-06-18 11:09:58 -06001820 dev, op->archdata.irqs[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 snd_cs4231_sbus_free(chip);
1822 return -EBUSY;
1823 }
Grant Likely1636f8a2010-06-18 11:09:58 -06001824 chip->irq[0] = op->archdata.irqs[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825
1826 if (snd_cs4231_probe(chip) < 0) {
1827 snd_cs4231_sbus_free(chip);
1828 return -ENODEV;
1829 }
1830 snd_cs4231_init(chip);
1831
1832 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL,
1833 chip, &snd_cs4231_sbus_dev_ops)) < 0) {
1834 snd_cs4231_sbus_free(chip);
1835 return err;
1836 }
1837
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 return 0;
1839}
1840
Bill Pemberton32e02a72012-12-06 12:35:25 -05001841static int cs4231_sbus_probe(struct platform_device *op)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842{
David S. Millerae251032008-08-27 18:24:01 -07001843 struct resource *rp = &op->resource[0];
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001844 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 int err;
1846
Takashi Iwaia2fefc32014-01-29 14:41:09 +01001847 err = cs4231_attach_begin(op, &card);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 if (err)
1849 return err;
1850
Andrew Morton5863aa62006-07-03 00:24:22 -07001851 sprintf(card->longname, "%s at 0x%02lx:0x%016Lx, irq %d",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 card->shortname,
1853 rp->flags & 0xffL,
Greg Kroah-Hartmanaa0a2dd2006-06-12 14:50:27 -07001854 (unsigned long long)rp->start,
Grant Likely1636f8a2010-06-18 11:09:58 -06001855 op->archdata.irqs[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856
David S. Millerae251032008-08-27 18:24:01 -07001857 err = snd_cs4231_sbus_create(card, op, dev);
Krzysztof Heltc6c2d572007-09-04 13:08:24 +02001858 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 snd_card_free(card);
1860 return err;
1861 }
1862
Krzysztof Heltc6c2d572007-09-04 13:08:24 +02001863 return cs4231_attach_finish(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864}
1865#endif
1866
1867#ifdef EBUS_SUPPORT
Georg Chinib1282542005-11-07 14:09:19 -08001868
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001869static void snd_cs4231_ebus_play_callback(struct ebus_dma_info *p, int event,
1870 void *cookie)
Georg Chinib1282542005-11-07 14:09:19 -08001871{
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001872 struct snd_cs4231 *chip = cookie;
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001873
Georg Chinib1282542005-11-07 14:09:19 -08001874 snd_cs4231_play_callback(chip);
1875}
1876
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001877static void snd_cs4231_ebus_capture_callback(struct ebus_dma_info *p,
1878 int event, void *cookie)
Georg Chinib1282542005-11-07 14:09:19 -08001879{
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001880 struct snd_cs4231 *chip = cookie;
Georg Chinib1282542005-11-07 14:09:19 -08001881
1882 snd_cs4231_capture_callback(chip);
1883}
1884
1885/*
1886 * EBUS DMA wrappers
1887 */
1888
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001889static int _ebus_dma_request(struct cs4231_dma_control *dma_cont,
1890 dma_addr_t bus_addr, size_t len)
Georg Chinib1282542005-11-07 14:09:19 -08001891{
1892 return ebus_dma_request(&dma_cont->ebus_info, bus_addr, len);
1893}
1894
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001895static void _ebus_dma_enable(struct cs4231_dma_control *dma_cont, int on)
Georg Chinib1282542005-11-07 14:09:19 -08001896{
1897 ebus_dma_enable(&dma_cont->ebus_info, on);
1898}
1899
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001900static void _ebus_dma_prepare(struct cs4231_dma_control *dma_cont, int dir)
Georg Chinib1282542005-11-07 14:09:19 -08001901{
1902 ebus_dma_prepare(&dma_cont->ebus_info, dir);
1903}
1904
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001905static unsigned int _ebus_dma_addr(struct cs4231_dma_control *dma_cont)
Georg Chinib1282542005-11-07 14:09:19 -08001906{
1907 return ebus_dma_addr(&dma_cont->ebus_info);
1908}
1909
Georg Chinib1282542005-11-07 14:09:19 -08001910/*
1911 * Init and exit routines
1912 */
1913
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001914static int snd_cs4231_ebus_free(struct snd_cs4231 *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915{
Grant Likely2dc11582010-08-06 09:25:50 -06001916 struct platform_device *op = chip->op;
David S. Millerafc88ad2008-08-30 00:13:55 -07001917
Georg Chinib1282542005-11-07 14:09:19 -08001918 if (chip->c_dma.ebus_info.regs) {
1919 ebus_dma_unregister(&chip->c_dma.ebus_info);
David S. Millerafc88ad2008-08-30 00:13:55 -07001920 of_iounmap(&op->resource[2], chip->c_dma.ebus_info.regs, 0x10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 }
Georg Chinib1282542005-11-07 14:09:19 -08001922 if (chip->p_dma.ebus_info.regs) {
1923 ebus_dma_unregister(&chip->p_dma.ebus_info);
David S. Millerafc88ad2008-08-30 00:13:55 -07001924 of_iounmap(&op->resource[1], chip->p_dma.ebus_info.regs, 0x10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 }
1926
1927 if (chip->port)
David S. Millerafc88ad2008-08-30 00:13:55 -07001928 of_iounmap(&op->resource[0], chip->port, 0x10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 return 0;
1931}
1932
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001933static int snd_cs4231_ebus_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934{
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01001935 struct snd_cs4231 *cp = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936
1937 return snd_cs4231_ebus_free(cp);
1938}
1939
Takashi Iwaib75851d2020-01-03 09:16:32 +01001940static const struct snd_device_ops snd_cs4231_ebus_dev_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 .dev_free = snd_cs4231_ebus_dev_free,
1942};
1943
Bill Pemberton32e02a72012-12-06 12:35:25 -05001944static int snd_cs4231_ebus_create(struct snd_card *card,
1945 struct platform_device *op,
1946 int dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947{
Krzysztof Heltc6c2d572007-09-04 13:08:24 +02001948 struct snd_cs4231 *chip = card->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 int err;
1950
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 spin_lock_init(&chip->lock);
Georg Chinib1282542005-11-07 14:09:19 -08001952 spin_lock_init(&chip->c_dma.ebus_info.lock);
1953 spin_lock_init(&chip->p_dma.ebus_info.lock);
Ingo Molnar12aa7572006-01-16 16:36:05 +01001954 mutex_init(&chip->mce_mutex);
1955 mutex_init(&chip->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 chip->flags |= CS4231_FLAG_EBUS;
David S. Millerafc88ad2008-08-30 00:13:55 -07001957 chip->op = op;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 memcpy(&chip->image, &snd_cs4231_original_image,
1959 sizeof(snd_cs4231_original_image));
Georg Chinib1282542005-11-07 14:09:19 -08001960 strcpy(chip->c_dma.ebus_info.name, "cs4231(capture)");
1961 chip->c_dma.ebus_info.flags = EBUS_DMA_FLAG_USE_EBDMA_HANDLER;
1962 chip->c_dma.ebus_info.callback = snd_cs4231_ebus_capture_callback;
1963 chip->c_dma.ebus_info.client_cookie = chip;
Grant Likely1636f8a2010-06-18 11:09:58 -06001964 chip->c_dma.ebus_info.irq = op->archdata.irqs[0];
Georg Chinib1282542005-11-07 14:09:19 -08001965 strcpy(chip->p_dma.ebus_info.name, "cs4231(play)");
1966 chip->p_dma.ebus_info.flags = EBUS_DMA_FLAG_USE_EBDMA_HANDLER;
1967 chip->p_dma.ebus_info.callback = snd_cs4231_ebus_play_callback;
1968 chip->p_dma.ebus_info.client_cookie = chip;
Grant Likely1636f8a2010-06-18 11:09:58 -06001969 chip->p_dma.ebus_info.irq = op->archdata.irqs[1];
Georg Chinib1282542005-11-07 14:09:19 -08001970
1971 chip->p_dma.prepare = _ebus_dma_prepare;
1972 chip->p_dma.enable = _ebus_dma_enable;
1973 chip->p_dma.request = _ebus_dma_request;
1974 chip->p_dma.address = _ebus_dma_addr;
Georg Chinib1282542005-11-07 14:09:19 -08001975
1976 chip->c_dma.prepare = _ebus_dma_prepare;
1977 chip->c_dma.enable = _ebus_dma_enable;
1978 chip->c_dma.request = _ebus_dma_request;
1979 chip->c_dma.address = _ebus_dma_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980
David S. Millerafc88ad2008-08-30 00:13:55 -07001981 chip->port = of_ioremap(&op->resource[0], 0, 0x10, "cs4231");
1982 chip->p_dma.ebus_info.regs =
1983 of_ioremap(&op->resource[1], 0, 0x10, "cs4231_pdma");
1984 chip->c_dma.ebus_info.regs =
1985 of_ioremap(&op->resource[2], 0, 0x10, "cs4231_cdma");
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001986 if (!chip->port || !chip->p_dma.ebus_info.regs ||
1987 !chip->c_dma.ebus_info.regs) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 snd_cs4231_ebus_free(chip);
Christopher Zimmermanna1314302005-09-21 00:41:22 -07001989 snd_printdd("cs4231-%d: Unable to map chip registers.\n", dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 return -EIO;
1991 }
1992
Georg Chinib1282542005-11-07 14:09:19 -08001993 if (ebus_dma_register(&chip->c_dma.ebus_info)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 snd_cs4231_ebus_free(chip);
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02001995 snd_printdd("cs4231-%d: Unable to register EBUS capture DMA\n",
1996 dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 return -EBUSY;
1998 }
Georg Chinib1282542005-11-07 14:09:19 -08001999 if (ebus_dma_irq_enable(&chip->c_dma.ebus_info, 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 snd_cs4231_ebus_free(chip);
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02002001 snd_printdd("cs4231-%d: Unable to enable EBUS capture IRQ\n",
2002 dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 return -EBUSY;
2004 }
2005
Georg Chinib1282542005-11-07 14:09:19 -08002006 if (ebus_dma_register(&chip->p_dma.ebus_info)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 snd_cs4231_ebus_free(chip);
Krzysztof Helt9e9abb42007-09-10 23:06:55 +02002008 snd_printdd("cs4231-%d: Unable to register EBUS play DMA\n",
2009 dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 return -EBUSY;
2011 }
Georg Chinib1282542005-11-07 14:09:19 -08002012 if (ebus_dma_irq_enable(&chip->p_dma.ebus_info, 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 snd_cs4231_ebus_free(chip);
Christopher Zimmermanna1314302005-09-21 00:41:22 -07002014 snd_printdd("cs4231-%d: Unable to enable EBUS play IRQ\n", dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 return -EBUSY;
2016 }
2017
2018 if (snd_cs4231_probe(chip) < 0) {
2019 snd_cs4231_ebus_free(chip);
2020 return -ENODEV;
2021 }
2022 snd_cs4231_init(chip);
2023
2024 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL,
2025 chip, &snd_cs4231_ebus_dev_ops)) < 0) {
2026 snd_cs4231_ebus_free(chip);
2027 return err;
2028 }
2029
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 return 0;
2031}
2032
Bill Pemberton32e02a72012-12-06 12:35:25 -05002033static int cs4231_ebus_probe(struct platform_device *op)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034{
Takashi Iwaibe9b7e82006-01-03 13:42:38 +01002035 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 int err;
2037
Takashi Iwaia2fefc32014-01-29 14:41:09 +01002038 err = cs4231_attach_begin(op, &card);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 if (err)
2040 return err;
2041
Sam Ravnborg3f4528d2009-01-06 13:20:38 -08002042 sprintf(card->longname, "%s at 0x%llx, irq %d",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 card->shortname,
David S. Millerafc88ad2008-08-30 00:13:55 -07002044 op->resource[0].start,
Grant Likely1636f8a2010-06-18 11:09:58 -06002045 op->archdata.irqs[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046
David S. Millerafc88ad2008-08-30 00:13:55 -07002047 err = snd_cs4231_ebus_create(card, op, dev);
Krzysztof Heltc6c2d572007-09-04 13:08:24 +02002048 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 snd_card_free(card);
2050 return err;
2051 }
2052
Krzysztof Heltc6c2d572007-09-04 13:08:24 +02002053 return cs4231_attach_finish(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054}
2055#endif
2056
Bill Pemberton32e02a72012-12-06 12:35:25 -05002057static int cs4231_probe(struct platform_device *op)
David S. Millerafc88ad2008-08-30 00:13:55 -07002058{
2059#ifdef EBUS_SUPPORT
Rob Herringc23b8e72018-12-05 13:50:47 -06002060 if (of_node_name_eq(op->dev.of_node->parent, "ebus"))
Grant Likelyf07eb222011-02-22 21:05:04 -07002061 return cs4231_ebus_probe(op);
David S. Millerafc88ad2008-08-30 00:13:55 -07002062#endif
David S. Millerae251032008-08-27 18:24:01 -07002063#ifdef SBUS_SUPPORT
Rob Herringc23b8e72018-12-05 13:50:47 -06002064 if (of_node_name_eq(op->dev.of_node->parent, "sbus") ||
2065 of_node_name_eq(op->dev.of_node->parent, "sbi"))
Grant Likelyf07eb222011-02-22 21:05:04 -07002066 return cs4231_sbus_probe(op);
David S. Millerafc88ad2008-08-30 00:13:55 -07002067#endif
2068 return -ENODEV;
2069}
2070
Bill Pemberton32e02a72012-12-06 12:35:25 -05002071static int cs4231_remove(struct platform_device *op)
David S. Millerafc88ad2008-08-30 00:13:55 -07002072{
2073 struct snd_cs4231 *chip = dev_get_drvdata(&op->dev);
2074
2075 snd_card_free(chip->card);
2076
2077 return 0;
2078}
2079
David S. Millerfd098312008-08-31 01:23:17 -07002080static const struct of_device_id cs4231_match[] = {
David S. Millerae251032008-08-27 18:24:01 -07002081 {
2082 .name = "SUNW,CS4231",
2083 },
David S. Millerafc88ad2008-08-30 00:13:55 -07002084 {
2085 .name = "audio",
2086 .compatible = "SUNW,CS4231",
2087 },
David S. Millerae251032008-08-27 18:24:01 -07002088 {},
2089};
2090
2091MODULE_DEVICE_TABLE(of, cs4231_match);
2092
Grant Likelyf07eb222011-02-22 21:05:04 -07002093static struct platform_driver cs4231_driver = {
Grant Likely40182942010-04-13 16:13:02 -07002094 .driver = {
2095 .name = "audio",
Grant Likely40182942010-04-13 16:13:02 -07002096 .of_match_table = cs4231_match,
2097 },
David S. Millerae251032008-08-27 18:24:01 -07002098 .probe = cs4231_probe,
Bill Pemberton32e02a72012-12-06 12:35:25 -05002099 .remove = cs4231_remove,
David S. Millerae251032008-08-27 18:24:01 -07002100};
David S. Millerae251032008-08-27 18:24:01 -07002101
Axel Lina09452e2011-11-27 16:36:04 +08002102module_platform_driver(cs4231_driver);