xref: /openbmc/linux/sound/pci/riptide/riptide.c (revision 2ac55daffee5af04b13f96e6acef63a8e4bfff85)
11a59d1b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2109a9638SPeter Gruber /*
3109a9638SPeter Gruber  *   Driver for the Conexant Riptide Soundchip
4109a9638SPeter Gruber  *
5109a9638SPeter Gruber  *	Copyright (c) 2004 Peter Gruber <nokos@gmx.net>
6109a9638SPeter Gruber  */
7109a9638SPeter Gruber /*
8109a9638SPeter Gruber   History:
9109a9638SPeter Gruber    - 02/15/2004 first release
10109a9638SPeter Gruber 
11109a9638SPeter Gruber   This Driver is based on the OSS Driver version from Linuxant (riptide-0.6lnxtbeta03111100)
12109a9638SPeter Gruber   credits from the original files:
13109a9638SPeter Gruber 
14109a9638SPeter Gruber   MODULE NAME:        cnxt_rt.h
15109a9638SPeter Gruber   AUTHOR:             K. Lazarev  (Transcribed by KNL)
16109a9638SPeter Gruber   HISTORY:         Major Revision               Date        By
17109a9638SPeter Gruber             -----------------------------     --------     -----
18109a9638SPeter Gruber             Created                           02/1/2000     KNL
19109a9638SPeter Gruber 
20109a9638SPeter Gruber   MODULE NAME:     int_mdl.c
21109a9638SPeter Gruber   AUTHOR:          Konstantin Lazarev    (Transcribed by KNL)
22109a9638SPeter Gruber   HISTORY:         Major Revision               Date        By
23109a9638SPeter Gruber             -----------------------------     --------     -----
24109a9638SPeter Gruber             Created                           10/01/99      KNL
25109a9638SPeter Gruber 
26109a9638SPeter Gruber   MODULE NAME:        riptide.h
27109a9638SPeter Gruber   AUTHOR:             O. Druzhinin  (Transcribed by OLD)
28109a9638SPeter Gruber   HISTORY:         Major Revision               Date        By
29109a9638SPeter Gruber             -----------------------------     --------     -----
30109a9638SPeter Gruber             Created                           10/16/97      OLD
31109a9638SPeter Gruber 
32109a9638SPeter Gruber   MODULE NAME:        Rp_Cmdif.cpp
33109a9638SPeter Gruber   AUTHOR:             O. Druzhinin  (Transcribed by OLD)
34109a9638SPeter Gruber                       K. Lazarev    (Transcribed by KNL)
35109a9638SPeter Gruber   HISTORY:         Major Revision               Date        By
36109a9638SPeter Gruber             -----------------------------     --------     -----
37109a9638SPeter Gruber             Adopted from NT4 driver            6/22/99      OLD
38109a9638SPeter Gruber             Ported to Linux                    9/01/99      KNL
39109a9638SPeter Gruber 
40109a9638SPeter Gruber   MODULE NAME:        rt_hw.c
41109a9638SPeter Gruber   AUTHOR:             O. Druzhinin  (Transcribed by OLD)
42109a9638SPeter Gruber                       C. Lazarev    (Transcribed by CNL)
43109a9638SPeter Gruber   HISTORY:         Major Revision               Date        By
44109a9638SPeter Gruber             -----------------------------     --------     -----
45109a9638SPeter Gruber             Created                           11/18/97      OLD
46109a9638SPeter Gruber             Hardware functions for RipTide    11/24/97      CNL
47109a9638SPeter Gruber             (ES1) are coded
48109a9638SPeter Gruber             Hardware functions for RipTide    12/24/97      CNL
49109a9638SPeter Gruber             (A0) are coded
50109a9638SPeter Gruber             Hardware functions for RipTide    03/20/98      CNL
51109a9638SPeter Gruber             (A1) are coded
52109a9638SPeter Gruber             Boot loader is included           05/07/98      CNL
53109a9638SPeter Gruber             Redesigned for WDM                07/27/98      CNL
54109a9638SPeter Gruber             Redesigned for Linux              09/01/99      CNL
55109a9638SPeter Gruber 
56109a9638SPeter Gruber   MODULE NAME:        rt_hw.h
57109a9638SPeter Gruber   AUTHOR:             C. Lazarev    (Transcribed by CNL)
58109a9638SPeter Gruber   HISTORY:         Major Revision               Date        By
59109a9638SPeter Gruber             -----------------------------     --------     -----
60109a9638SPeter Gruber             Created                           11/18/97      CNL
61109a9638SPeter Gruber 
62109a9638SPeter Gruber   MODULE NAME:     rt_mdl.c
63109a9638SPeter Gruber   AUTHOR:          Konstantin Lazarev    (Transcribed by KNL)
64109a9638SPeter Gruber   HISTORY:         Major Revision               Date        By
65109a9638SPeter Gruber             -----------------------------     --------     -----
66109a9638SPeter Gruber             Created                           10/01/99      KNL
67109a9638SPeter Gruber 
68109a9638SPeter Gruber   MODULE NAME:        mixer.h
69109a9638SPeter Gruber   AUTHOR:             K. Kenney
70109a9638SPeter Gruber   HISTORY:         Major Revision                   Date          By
71109a9638SPeter Gruber             -----------------------------          --------     -----
72109a9638SPeter Gruber             Created from MS W95 Sample             11/28/95      KRS
73109a9638SPeter Gruber             RipTide                                10/15/97      KRS
74109a9638SPeter Gruber             Adopted for Windows NT driver          01/20/98      CNL
75109a9638SPeter Gruber */
76109a9638SPeter Gruber 
77109a9638SPeter Gruber #include <linux/delay.h>
78109a9638SPeter Gruber #include <linux/init.h>
79109a9638SPeter Gruber #include <linux/interrupt.h>
80109a9638SPeter Gruber #include <linux/pci.h>
81109a9638SPeter Gruber #include <linux/slab.h>
82109a9638SPeter Gruber #include <linux/wait.h>
83109a9638SPeter Gruber #include <linux/gameport.h>
84109a9638SPeter Gruber #include <linux/device.h>
85109a9638SPeter Gruber #include <linux/firmware.h>
86c9ff921aSAndy Shevchenko #include <linux/kernel.h>
87da155d5bSPaul Gortmaker #include <linux/module.h>
886cbbfe1cSTakashi Iwai #include <linux/io.h>
89109a9638SPeter Gruber #include <sound/core.h>
90109a9638SPeter Gruber #include <sound/info.h>
91109a9638SPeter Gruber #include <sound/control.h>
92109a9638SPeter Gruber #include <sound/pcm.h>
93109a9638SPeter Gruber #include <sound/pcm_params.h>
94109a9638SPeter Gruber #include <sound/ac97_codec.h>
95109a9638SPeter Gruber #include <sound/mpu401.h>
96109a9638SPeter Gruber #include <sound/opl3.h>
97109a9638SPeter Gruber #include <sound/initval.h>
98109a9638SPeter Gruber 
99b2fac073SFabian Frederick #if IS_REACHABLE(CONFIG_GAMEPORT)
100109a9638SPeter Gruber #define SUPPORT_JOYSTICK 1
101109a9638SPeter Gruber #endif
102109a9638SPeter Gruber 
103109a9638SPeter Gruber MODULE_AUTHOR("Peter Gruber <nokos@gmx.net>");
104109a9638SPeter Gruber MODULE_DESCRIPTION("riptide");
105109a9638SPeter Gruber MODULE_LICENSE("GPL");
106109a9638SPeter Gruber MODULE_SUPPORTED_DEVICE("{{Conexant,Riptide}}");
1077e0af29dSClemens Ladisch MODULE_FIRMWARE("riptide.hex");
108109a9638SPeter Gruber 
109109a9638SPeter Gruber static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
110109a9638SPeter Gruber static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
111a67ff6a5SRusty Russell static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE;
112109a9638SPeter Gruber 
113109a9638SPeter Gruber #ifdef SUPPORT_JOYSTICK
114109a9638SPeter Gruber static int joystick_port[SNDRV_CARDS] = { [0 ... (SNDRV_CARDS - 1)] = 0x200 };
115109a9638SPeter Gruber #endif
116109a9638SPeter Gruber static int mpu_port[SNDRV_CARDS] = { [0 ... (SNDRV_CARDS - 1)] = 0x330 };
117109a9638SPeter Gruber static int opl3_port[SNDRV_CARDS] = { [0 ... (SNDRV_CARDS - 1)] = 0x388 };
118109a9638SPeter Gruber 
119109a9638SPeter Gruber module_param_array(index, int, NULL, 0444);
120109a9638SPeter Gruber MODULE_PARM_DESC(index, "Index value for Riptide soundcard.");
121109a9638SPeter Gruber module_param_array(id, charp, NULL, 0444);
122109a9638SPeter Gruber MODULE_PARM_DESC(id, "ID string for Riptide soundcard.");
123109a9638SPeter Gruber module_param_array(enable, bool, NULL, 0444);
124109a9638SPeter Gruber MODULE_PARM_DESC(enable, "Enable Riptide soundcard.");
125109a9638SPeter Gruber #ifdef SUPPORT_JOYSTICK
1266192c41fSDavid Howells module_param_hw_array(joystick_port, int, ioport, NULL, 0444);
127109a9638SPeter Gruber MODULE_PARM_DESC(joystick_port, "Joystick port # for Riptide soundcard.");
128109a9638SPeter Gruber #endif
1296192c41fSDavid Howells module_param_hw_array(mpu_port, int, ioport, NULL, 0444);
130109a9638SPeter Gruber MODULE_PARM_DESC(mpu_port, "MPU401 port # for Riptide driver.");
1316192c41fSDavid Howells module_param_hw_array(opl3_port, int, ioport, NULL, 0444);
132109a9638SPeter Gruber MODULE_PARM_DESC(opl3_port, "OPL3 port # for Riptide driver.");
133109a9638SPeter Gruber 
134109a9638SPeter Gruber /*
135109a9638SPeter Gruber  */
136109a9638SPeter Gruber 
137109a9638SPeter Gruber #define MPU401_HW_RIPTIDE MPU401_HW_MPU401
138109a9638SPeter Gruber #define OPL3_HW_RIPTIDE   OPL3_HW_OPL3
139109a9638SPeter Gruber 
140109a9638SPeter Gruber #define PCI_EXT_CapId       0x40
141109a9638SPeter Gruber #define PCI_EXT_NextCapPrt  0x41
142109a9638SPeter Gruber #define PCI_EXT_PWMC        0x42
143109a9638SPeter Gruber #define PCI_EXT_PWSCR       0x44
144109a9638SPeter Gruber #define PCI_EXT_Data00      0x46
145109a9638SPeter Gruber #define PCI_EXT_PMSCR_BSE   0x47
146109a9638SPeter Gruber #define PCI_EXT_SB_Base     0x48
147109a9638SPeter Gruber #define PCI_EXT_FM_Base     0x4a
148109a9638SPeter Gruber #define PCI_EXT_MPU_Base    0x4C
149109a9638SPeter Gruber #define PCI_EXT_Game_Base   0x4E
150109a9638SPeter Gruber #define PCI_EXT_Legacy_Mask 0x50
151109a9638SPeter Gruber #define PCI_EXT_AsicRev     0x52
152109a9638SPeter Gruber #define PCI_EXT_Reserved3   0x53
153109a9638SPeter Gruber 
154109a9638SPeter Gruber #define LEGACY_ENABLE_ALL      0x8000	/* legacy device options */
155109a9638SPeter Gruber #define LEGACY_ENABLE_SB       0x4000
156109a9638SPeter Gruber #define LEGACY_ENABLE_FM       0x2000
157109a9638SPeter Gruber #define LEGACY_ENABLE_MPU_INT  0x1000
158109a9638SPeter Gruber #define LEGACY_ENABLE_MPU      0x0800
159109a9638SPeter Gruber #define LEGACY_ENABLE_GAMEPORT 0x0400
160109a9638SPeter Gruber 
161109a9638SPeter Gruber #define MAX_WRITE_RETRY  10	/* cmd interface limits */
162109a9638SPeter Gruber #define MAX_ERROR_COUNT  10
1630f620830SPeter Gruber #define CMDIF_TIMEOUT    50000
164109a9638SPeter Gruber #define RESET_TRIES      5
165109a9638SPeter Gruber 
166109a9638SPeter Gruber #define READ_PORT_ULONG(p)     inl((unsigned long)&(p))
167109a9638SPeter Gruber #define WRITE_PORT_ULONG(p,x)  outl(x,(unsigned long)&(p))
168109a9638SPeter Gruber 
169109a9638SPeter Gruber #define READ_AUDIO_CONTROL(p)     READ_PORT_ULONG(p->audio_control)
170109a9638SPeter Gruber #define WRITE_AUDIO_CONTROL(p,x)  WRITE_PORT_ULONG(p->audio_control,x)
171109a9638SPeter Gruber #define UMASK_AUDIO_CONTROL(p,x)  WRITE_PORT_ULONG(p->audio_control,READ_PORT_ULONG(p->audio_control)|x)
172109a9638SPeter Gruber #define MASK_AUDIO_CONTROL(p,x)   WRITE_PORT_ULONG(p->audio_control,READ_PORT_ULONG(p->audio_control)&x)
173109a9638SPeter Gruber #define READ_AUDIO_STATUS(p)      READ_PORT_ULONG(p->audio_status)
174109a9638SPeter Gruber 
175109a9638SPeter Gruber #define SET_GRESET(p)     UMASK_AUDIO_CONTROL(p,0x0001)	/* global reset switch */
176109a9638SPeter Gruber #define UNSET_GRESET(p)   MASK_AUDIO_CONTROL(p,~0x0001)
177109a9638SPeter Gruber #define SET_AIE(p)        UMASK_AUDIO_CONTROL(p,0x0004)	/* interrupt enable */
178109a9638SPeter Gruber #define UNSET_AIE(p)      MASK_AUDIO_CONTROL(p,~0x0004)
179109a9638SPeter Gruber #define SET_AIACK(p)      UMASK_AUDIO_CONTROL(p,0x0008)	/* interrupt acknowledge */
180109a9638SPeter Gruber #define UNSET_AIACKT(p)   MASKAUDIO_CONTROL(p,~0x0008)
181109a9638SPeter Gruber #define SET_ECMDAE(p)     UMASK_AUDIO_CONTROL(p,0x0010)
182109a9638SPeter Gruber #define UNSET_ECMDAE(p)   MASK_AUDIO_CONTROL(p,~0x0010)
183109a9638SPeter Gruber #define SET_ECMDBE(p)     UMASK_AUDIO_CONTROL(p,0x0020)
184109a9638SPeter Gruber #define UNSET_ECMDBE(p)   MASK_AUDIO_CONTROL(p,~0x0020)
185109a9638SPeter Gruber #define SET_EDATAF(p)     UMASK_AUDIO_CONTROL(p,0x0040)
186109a9638SPeter Gruber #define UNSET_EDATAF(p)   MASK_AUDIO_CONTROL(p,~0x0040)
187109a9638SPeter Gruber #define SET_EDATBF(p)     UMASK_AUDIO_CONTROL(p,0x0080)
188109a9638SPeter Gruber #define UNSET_EDATBF(p)   MASK_AUDIO_CONTROL(p,~0x0080)
189109a9638SPeter Gruber #define SET_ESBIRQON(p)   UMASK_AUDIO_CONTROL(p,0x0100)
190109a9638SPeter Gruber #define UNSET_ESBIRQON(p) MASK_AUDIO_CONTROL(p,~0x0100)
191109a9638SPeter Gruber #define SET_EMPUIRQ(p)    UMASK_AUDIO_CONTROL(p,0x0200)
192109a9638SPeter Gruber #define UNSET_EMPUIRQ(p)  MASK_AUDIO_CONTROL(p,~0x0200)
193109a9638SPeter Gruber #define IS_CMDE(a)        (READ_PORT_ULONG(a->stat)&0x1)	/* cmd empty */
194109a9638SPeter Gruber #define IS_DATF(a)        (READ_PORT_ULONG(a->stat)&0x2)	/* data filled */
195109a9638SPeter Gruber #define IS_READY(p)       (READ_AUDIO_STATUS(p)&0x0001)
196109a9638SPeter Gruber #define IS_DLREADY(p)     (READ_AUDIO_STATUS(p)&0x0002)
197109a9638SPeter Gruber #define IS_DLERR(p)       (READ_AUDIO_STATUS(p)&0x0004)
198109a9638SPeter Gruber #define IS_GERR(p)        (READ_AUDIO_STATUS(p)&0x0008)	/* error ! */
199109a9638SPeter Gruber #define IS_CMDAEIRQ(p)    (READ_AUDIO_STATUS(p)&0x0010)
200109a9638SPeter Gruber #define IS_CMDBEIRQ(p)    (READ_AUDIO_STATUS(p)&0x0020)
201109a9638SPeter Gruber #define IS_DATAFIRQ(p)    (READ_AUDIO_STATUS(p)&0x0040)
202109a9638SPeter Gruber #define IS_DATBFIRQ(p)    (READ_AUDIO_STATUS(p)&0x0080)
203109a9638SPeter Gruber #define IS_EOBIRQ(p)      (READ_AUDIO_STATUS(p)&0x0100)	/* interrupt status */
204109a9638SPeter Gruber #define IS_EOSIRQ(p)      (READ_AUDIO_STATUS(p)&0x0200)
205109a9638SPeter Gruber #define IS_EOCIRQ(p)      (READ_AUDIO_STATUS(p)&0x0400)
206109a9638SPeter Gruber #define IS_UNSLIRQ(p)     (READ_AUDIO_STATUS(p)&0x0800)
207109a9638SPeter Gruber #define IS_SBIRQ(p)       (READ_AUDIO_STATUS(p)&0x1000)
208109a9638SPeter Gruber #define IS_MPUIRQ(p)      (READ_AUDIO_STATUS(p)&0x2000)
209109a9638SPeter Gruber 
210109a9638SPeter Gruber #define RESP 0x00000001		/* command flags */
211109a9638SPeter Gruber #define PARM 0x00000002
212109a9638SPeter Gruber #define CMDA 0x00000004
213109a9638SPeter Gruber #define CMDB 0x00000008
214109a9638SPeter Gruber #define NILL 0x00000000
215109a9638SPeter Gruber 
216109a9638SPeter Gruber #define LONG0(a)   ((u32)a)	/* shifts and masks */
217109a9638SPeter Gruber #define BYTE0(a)   (LONG0(a)&0xff)
218109a9638SPeter Gruber #define BYTE1(a)   (BYTE0(a)<<8)
219109a9638SPeter Gruber #define BYTE2(a)   (BYTE0(a)<<16)
220109a9638SPeter Gruber #define BYTE3(a)   (BYTE0(a)<<24)
221109a9638SPeter Gruber #define WORD0(a)   (LONG0(a)&0xffff)
222109a9638SPeter Gruber #define WORD1(a)   (WORD0(a)<<8)
223109a9638SPeter Gruber #define WORD2(a)   (WORD0(a)<<16)
224109a9638SPeter Gruber #define TRINIB0(a) (LONG0(a)&0xffffff)
225109a9638SPeter Gruber #define TRINIB1(a) (TRINIB0(a)<<8)
226109a9638SPeter Gruber 
227109a9638SPeter Gruber #define RET(a)     ((union cmdret *)(a))
228109a9638SPeter Gruber 
229109a9638SPeter Gruber #define SEND_GETV(p,b)             sendcmd(p,RESP,GETV,0,RET(b))	/* get version */
230109a9638SPeter Gruber #define SEND_GETC(p,b,c)           sendcmd(p,PARM|RESP,GETC,c,RET(b))
231109a9638SPeter Gruber #define SEND_GUNS(p,b)             sendcmd(p,RESP,GUNS,0,RET(b))
232109a9638SPeter Gruber #define SEND_SCID(p,b)             sendcmd(p,RESP,SCID,0,RET(b))
233109a9638SPeter Gruber #define SEND_RMEM(p,b,c,d)         sendcmd(p,PARM|RESP,RMEM|BYTE1(b),LONG0(c),RET(d))	/* memory access for firmware write */
234109a9638SPeter Gruber #define SEND_SMEM(p,b,c)           sendcmd(p,PARM,SMEM|BYTE1(b),LONG0(c),RET(0))	/* memory access for firmware write */
235109a9638SPeter Gruber #define SEND_WMEM(p,b,c)           sendcmd(p,PARM,WMEM|BYTE1(b),LONG0(c),RET(0))	/* memory access for firmware write */
236109a9638SPeter Gruber #define SEND_SDTM(p,b,c)           sendcmd(p,PARM|RESP,SDTM|TRINIB1(b),0,RET(c))	/* memory access for firmware write */
237109a9638SPeter Gruber #define SEND_GOTO(p,b)             sendcmd(p,PARM,GOTO,LONG0(b),RET(0))	/* memory access for firmware write */
238109a9638SPeter Gruber #define SEND_SETDPLL(p)	           sendcmd(p,0,ARM_SETDPLL,0,RET(0))
239109a9638SPeter Gruber #define SEND_SSTR(p,b,c)           sendcmd(p,PARM,SSTR|BYTE3(b),LONG0(c),RET(0))	/* start stream */
240109a9638SPeter Gruber #define SEND_PSTR(p,b)             sendcmd(p,PARM,PSTR,BYTE3(b),RET(0))	/* pause stream */
241109a9638SPeter Gruber #define SEND_KSTR(p,b)             sendcmd(p,PARM,KSTR,BYTE3(b),RET(0))	/* stop stream */
242109a9638SPeter Gruber #define SEND_KDMA(p)               sendcmd(p,0,KDMA,0,RET(0))	/* stop all dma */
243109a9638SPeter Gruber #define SEND_GPOS(p,b,c,d)         sendcmd(p,PARM|RESP,GPOS,BYTE3(c)|BYTE2(b),RET(d))	/* get position in dma */
244109a9638SPeter Gruber #define SEND_SETF(p,b,c,d,e,f,g)   sendcmd(p,PARM,SETF|WORD1(b)|BYTE3(c),d|BYTE1(e)|BYTE2(f)|BYTE3(g),RET(0))	/* set sample format at mixer */
245109a9638SPeter Gruber #define SEND_GSTS(p,b,c,d)         sendcmd(p,PARM|RESP,GSTS,BYTE3(c)|BYTE2(b),RET(d))
246109a9638SPeter Gruber #define SEND_NGPOS(p,b,c,d)        sendcmd(p,PARM|RESP,NGPOS,BYTE3(c)|BYTE2(b),RET(d))
247109a9638SPeter Gruber #define SEND_PSEL(p,b,c)           sendcmd(p,PARM,PSEL,BYTE2(b)|BYTE3(c),RET(0))	/* activate lbus path */
248109a9638SPeter Gruber #define SEND_PCLR(p,b,c)           sendcmd(p,PARM,PCLR,BYTE2(b)|BYTE3(c),RET(0))	/* deactivate lbus path */
249109a9638SPeter Gruber #define SEND_PLST(p,b)             sendcmd(p,PARM,PLST,BYTE3(b),RET(0))
250109a9638SPeter Gruber #define SEND_RSSV(p,b,c,d)         sendcmd(p,PARM|RESP,RSSV,BYTE2(b)|BYTE3(c),RET(d))
251109a9638SPeter Gruber #define SEND_LSEL(p,b,c,d,e,f,g,h) sendcmd(p,PARM,LSEL|BYTE1(b)|BYTE2(c)|BYTE3(d),BYTE0(e)|BYTE1(f)|BYTE2(g)|BYTE3(h),RET(0))	/* select paths for internal connections */
252109a9638SPeter Gruber #define SEND_SSRC(p,b,c,d,e)       sendcmd(p,PARM,SSRC|BYTE1(b)|WORD2(c),WORD0(d)|WORD2(e),RET(0))	/* configure source */
253109a9638SPeter Gruber #define SEND_SLST(p,b)             sendcmd(p,PARM,SLST,BYTE3(b),RET(0))
254109a9638SPeter Gruber #define SEND_RSRC(p,b,c)           sendcmd(p,RESP,RSRC|BYTE1(b),0,RET(c))	/* read source config */
255109a9638SPeter Gruber #define SEND_SSRB(p,b,c)           sendcmd(p,PARM,SSRB|BYTE1(b),WORD2(c),RET(0))
256109a9638SPeter Gruber #define SEND_SDGV(p,b,c,d,e)       sendcmd(p,PARM,SDGV|BYTE2(b)|BYTE3(c),WORD0(d)|WORD2(e),RET(0))	/* set digital mixer */
257109a9638SPeter Gruber #define SEND_RDGV(p,b,c,d)         sendcmd(p,PARM|RESP,RDGV|BYTE2(b)|BYTE3(c),0,RET(d))	/* read digital mixer */
258109a9638SPeter Gruber #define SEND_DLST(p,b)             sendcmd(p,PARM,DLST,BYTE3(b),RET(0))
259109a9638SPeter Gruber #define SEND_SACR(p,b,c)           sendcmd(p,PARM,SACR,WORD0(b)|WORD2(c),RET(0))	/* set AC97 register */
260109a9638SPeter Gruber #define SEND_RACR(p,b,c)           sendcmd(p,PARM|RESP,RACR,WORD2(b),RET(c))	/* get AC97 register */
261109a9638SPeter Gruber #define SEND_ALST(p,b)             sendcmd(p,PARM,ALST,BYTE3(b),RET(0))
262109a9638SPeter Gruber #define SEND_TXAC(p,b,c,d,e,f)     sendcmd(p,PARM,TXAC|BYTE1(b)|WORD2(c),WORD0(d)|BYTE2(e)|BYTE3(f),RET(0))
263109a9638SPeter Gruber #define SEND_RXAC(p,b,c,d)         sendcmd(p,PARM|RESP,RXAC,BYTE2(b)|BYTE3(c),RET(d))
264109a9638SPeter Gruber #define SEND_SI2S(p,b)             sendcmd(p,PARM,SI2S,WORD2(b),RET(0))
265109a9638SPeter Gruber 
266109a9638SPeter Gruber #define EOB_STATUS         0x80000000	/* status flags : block boundary */
267109a9638SPeter Gruber #define EOS_STATUS         0x40000000	/*              : stoppped */
268109a9638SPeter Gruber #define EOC_STATUS         0x20000000	/*              : stream end */
269109a9638SPeter Gruber #define ERR_STATUS         0x10000000
270109a9638SPeter Gruber #define EMPTY_STATUS       0x08000000
271109a9638SPeter Gruber 
272109a9638SPeter Gruber #define IEOB_ENABLE        0x1	/* enable interrupts for status notification above */
273109a9638SPeter Gruber #define IEOS_ENABLE        0x2
274109a9638SPeter Gruber #define IEOC_ENABLE        0x4
275109a9638SPeter Gruber #define RDONCE             0x8
276109a9638SPeter Gruber #define DESC_MAX_MASK      0xff
277109a9638SPeter Gruber 
278109a9638SPeter Gruber #define ST_PLAY  0x1		/* stream states */
279109a9638SPeter Gruber #define ST_STOP  0x2
280109a9638SPeter Gruber #define ST_PAUSE 0x4
281109a9638SPeter Gruber 
282109a9638SPeter Gruber #define I2S_INTDEC     3	/* config for I2S link */
283109a9638SPeter Gruber #define I2S_MERGER     0
284109a9638SPeter Gruber #define I2S_SPLITTER   0
285109a9638SPeter Gruber #define I2S_MIXER      7
286109a9638SPeter Gruber #define I2S_RATE       44100
287109a9638SPeter Gruber 
288109a9638SPeter Gruber #define MODEM_INTDEC   4	/* config for modem link */
289109a9638SPeter Gruber #define MODEM_MERGER   3
290109a9638SPeter Gruber #define MODEM_SPLITTER 0
291109a9638SPeter Gruber #define MODEM_MIXER    11
292109a9638SPeter Gruber 
293109a9638SPeter Gruber #define FM_INTDEC      3	/* config for FM/OPL3 link */
294109a9638SPeter Gruber #define FM_MERGER      0
295109a9638SPeter Gruber #define FM_SPLITTER    0
296109a9638SPeter Gruber #define FM_MIXER       9
297109a9638SPeter Gruber 
298109a9638SPeter Gruber #define SPLIT_PATH  0x80	/* path splitting flag */
299109a9638SPeter Gruber 
300109a9638SPeter Gruber enum FIRMWARE {
301109a9638SPeter Gruber 	DATA_REC = 0, EXT_END_OF_FILE, EXT_SEG_ADDR_REC, EXT_GOTO_CMD_REC,
302109a9638SPeter Gruber 	EXT_LIN_ADDR_REC,
303109a9638SPeter Gruber };
304109a9638SPeter Gruber 
305109a9638SPeter Gruber enum CMDS {
306109a9638SPeter Gruber 	GETV = 0x00, GETC, GUNS, SCID, RMEM =
307109a9638SPeter Gruber 	    0x10, SMEM, WMEM, SDTM, GOTO, SSTR =
308109a9638SPeter Gruber 	    0x20, PSTR, KSTR, KDMA, GPOS, SETF, GSTS, NGPOS, PSEL =
309109a9638SPeter Gruber 	    0x30, PCLR, PLST, RSSV, LSEL, SSRC = 0x40, SLST, RSRC, SSRB, SDGV =
310109a9638SPeter Gruber 	    0x50, RDGV, DLST, SACR = 0x60, RACR, ALST, TXAC, RXAC, SI2S =
311109a9638SPeter Gruber 	    0x70, ARM_SETDPLL = 0x72,
312109a9638SPeter Gruber };
313109a9638SPeter Gruber 
314109a9638SPeter Gruber enum E1SOURCE {
315109a9638SPeter Gruber 	ARM2LBUS_FIFO0 = 0, ARM2LBUS_FIFO1, ARM2LBUS_FIFO2, ARM2LBUS_FIFO3,
316109a9638SPeter Gruber 	ARM2LBUS_FIFO4, ARM2LBUS_FIFO5, ARM2LBUS_FIFO6, ARM2LBUS_FIFO7,
317109a9638SPeter Gruber 	ARM2LBUS_FIFO8, ARM2LBUS_FIFO9, ARM2LBUS_FIFO10, ARM2LBUS_FIFO11,
318109a9638SPeter Gruber 	ARM2LBUS_FIFO12, ARM2LBUS_FIFO13, ARM2LBUS_FIFO14, ARM2LBUS_FIFO15,
319109a9638SPeter Gruber 	INTER0_OUT, INTER1_OUT, INTER2_OUT, INTER3_OUT, INTER4_OUT,
320109a9638SPeter Gruber 	INTERM0_OUT, INTERM1_OUT, INTERM2_OUT, INTERM3_OUT, INTERM4_OUT,
321109a9638SPeter Gruber 	INTERM5_OUT, INTERM6_OUT, DECIMM0_OUT, DECIMM1_OUT, DECIMM2_OUT,
322109a9638SPeter Gruber 	DECIMM3_OUT, DECIM0_OUT, SR3_4_OUT, OPL3_SAMPLE, ASRC0, ASRC1,
323109a9638SPeter Gruber 	ACLNK2PADC, ACLNK2MODEM0RX, ACLNK2MIC, ACLNK2MODEM1RX, ACLNK2HNDMIC,
324109a9638SPeter Gruber 	DIGITAL_MIXER_OUT0, GAINFUNC0_OUT, GAINFUNC1_OUT, GAINFUNC2_OUT,
325109a9638SPeter Gruber 	GAINFUNC3_OUT, GAINFUNC4_OUT, SOFTMODEMTX, SPLITTER0_OUTL,
326109a9638SPeter Gruber 	SPLITTER0_OUTR, SPLITTER1_OUTL, SPLITTER1_OUTR, SPLITTER2_OUTL,
327109a9638SPeter Gruber 	SPLITTER2_OUTR, SPLITTER3_OUTL, SPLITTER3_OUTR, MERGER0_OUT,
328109a9638SPeter Gruber 	MERGER1_OUT, MERGER2_OUT, MERGER3_OUT, ARM2LBUS_FIFO_DIRECT, NO_OUT
329109a9638SPeter Gruber };
330109a9638SPeter Gruber 
331109a9638SPeter Gruber enum E2SINK {
332109a9638SPeter Gruber 	LBUS2ARM_FIFO0 = 0, LBUS2ARM_FIFO1, LBUS2ARM_FIFO2, LBUS2ARM_FIFO3,
333109a9638SPeter Gruber 	LBUS2ARM_FIFO4, LBUS2ARM_FIFO5, LBUS2ARM_FIFO6, LBUS2ARM_FIFO7,
334109a9638SPeter Gruber 	INTER0_IN, INTER1_IN, INTER2_IN, INTER3_IN, INTER4_IN, INTERM0_IN,
335109a9638SPeter Gruber 	INTERM1_IN, INTERM2_IN, INTERM3_IN, INTERM4_IN, INTERM5_IN, INTERM6_IN,
336109a9638SPeter Gruber 	DECIMM0_IN, DECIMM1_IN, DECIMM2_IN, DECIMM3_IN, DECIM0_IN, SR3_4_IN,
337109a9638SPeter Gruber 	PDAC2ACLNK, MODEM0TX2ACLNK, MODEM1TX2ACLNK, HNDSPK2ACLNK,
338109a9638SPeter Gruber 	DIGITAL_MIXER_IN0, DIGITAL_MIXER_IN1, DIGITAL_MIXER_IN2,
339109a9638SPeter Gruber 	DIGITAL_MIXER_IN3, DIGITAL_MIXER_IN4, DIGITAL_MIXER_IN5,
340109a9638SPeter Gruber 	DIGITAL_MIXER_IN6, DIGITAL_MIXER_IN7, DIGITAL_MIXER_IN8,
341109a9638SPeter Gruber 	DIGITAL_MIXER_IN9, DIGITAL_MIXER_IN10, DIGITAL_MIXER_IN11,
342109a9638SPeter Gruber 	GAINFUNC0_IN, GAINFUNC1_IN, GAINFUNC2_IN, GAINFUNC3_IN, GAINFUNC4_IN,
343109a9638SPeter Gruber 	SOFTMODEMRX, SPLITTER0_IN, SPLITTER1_IN, SPLITTER2_IN, SPLITTER3_IN,
344109a9638SPeter Gruber 	MERGER0_INL, MERGER0_INR, MERGER1_INL, MERGER1_INR, MERGER2_INL,
345109a9638SPeter Gruber 	MERGER2_INR, MERGER3_INL, MERGER3_INR, E2SINK_MAX
346109a9638SPeter Gruber };
347109a9638SPeter Gruber 
348109a9638SPeter Gruber enum LBUS_SINK {
349109a9638SPeter Gruber 	LS_SRC_INTERPOLATOR = 0, LS_SRC_INTERPOLATORM, LS_SRC_DECIMATOR,
350109a9638SPeter Gruber 	LS_SRC_DECIMATORM, LS_MIXER_IN, LS_MIXER_GAIN_FUNCTION,
351109a9638SPeter Gruber 	LS_SRC_SPLITTER, LS_SRC_MERGER, LS_NONE1, LS_NONE2,
352109a9638SPeter Gruber };
353109a9638SPeter Gruber 
354109a9638SPeter Gruber enum RT_CHANNEL_IDS {
355109a9638SPeter Gruber 	M0TX = 0, M1TX, TAMTX, HSSPKR, PDAC, DSNDTX0, DSNDTX1, DSNDTX2,
356109a9638SPeter Gruber 	DSNDTX3, DSNDTX4, DSNDTX5, DSNDTX6, DSNDTX7, WVSTRTX, COP3DTX, SPARE,
357109a9638SPeter Gruber 	M0RX, HSMIC, M1RX, CLEANRX, MICADC, PADC, COPRX1, COPRX2,
358109a9638SPeter Gruber 	CHANNEL_ID_COUNTER
359109a9638SPeter Gruber };
360109a9638SPeter Gruber 
361109a9638SPeter Gruber enum { SB_CMD = 0, MODEM_CMD, I2S_CMD0, I2S_CMD1, FM_CMD, MAX_CMD };
362109a9638SPeter Gruber 
363109a9638SPeter Gruber struct lbuspath {
364bf6f3d74STakashi Iwai 	const unsigned char *noconv;
365bf6f3d74STakashi Iwai 	const unsigned char *stereo;
366bf6f3d74STakashi Iwai 	const unsigned char *mono;
367109a9638SPeter Gruber };
368109a9638SPeter Gruber 
369109a9638SPeter Gruber struct cmdport {
370109a9638SPeter Gruber 	u32 data1;		/* cmd,param */
371109a9638SPeter Gruber 	u32 data2;		/* param */
372109a9638SPeter Gruber 	u32 stat;		/* status */
373109a9638SPeter Gruber 	u32 pad[5];
374109a9638SPeter Gruber };
375109a9638SPeter Gruber 
376109a9638SPeter Gruber struct riptideport {
377109a9638SPeter Gruber 	u32 audio_control;	/* status registers */
378109a9638SPeter Gruber 	u32 audio_status;
379109a9638SPeter Gruber 	u32 pad[2];
380109a9638SPeter Gruber 	struct cmdport port[2];	/* command ports */
381109a9638SPeter Gruber };
382109a9638SPeter Gruber 
383109a9638SPeter Gruber struct cmdif {
384109a9638SPeter Gruber 	struct riptideport *hwport;
385109a9638SPeter Gruber 	spinlock_t lock;
386109a9638SPeter Gruber 	unsigned int cmdcnt;	/* cmd statistics */
387109a9638SPeter Gruber 	unsigned int cmdtime;
388109a9638SPeter Gruber 	unsigned int cmdtimemax;
389109a9638SPeter Gruber 	unsigned int cmdtimemin;
390109a9638SPeter Gruber 	unsigned int errcnt;
391109a9638SPeter Gruber 	int is_reset;
392109a9638SPeter Gruber };
393109a9638SPeter Gruber 
394109a9638SPeter Gruber struct riptide_firmware {
395109a9638SPeter Gruber 	u16 ASIC;
396109a9638SPeter Gruber 	u16 CODEC;
397109a9638SPeter Gruber 	u16 AUXDSP;
398109a9638SPeter Gruber 	u16 PROG;
399109a9638SPeter Gruber };
400109a9638SPeter Gruber 
401109a9638SPeter Gruber union cmdret {
402109a9638SPeter Gruber 	u8 retbytes[8];
403109a9638SPeter Gruber 	u16 retwords[4];
404109a9638SPeter Gruber 	u32 retlongs[2];
405109a9638SPeter Gruber };
406109a9638SPeter Gruber 
407109a9638SPeter Gruber union firmware_version {
408109a9638SPeter Gruber 	union cmdret ret;
409109a9638SPeter Gruber 	struct riptide_firmware firmware;
410109a9638SPeter Gruber };
411109a9638SPeter Gruber 
412109a9638SPeter Gruber #define get_pcmhwdev(substream) (struct pcmhw *)(substream->runtime->private_data)
413109a9638SPeter Gruber 
414109a9638SPeter Gruber #define PLAYBACK_SUBSTREAMS 3
415109a9638SPeter Gruber struct snd_riptide {
416109a9638SPeter Gruber 	struct snd_card *card;
417109a9638SPeter Gruber 	struct pci_dev *pci;
418109a9638SPeter Gruber 	const struct firmware *fw_entry;
419109a9638SPeter Gruber 
420109a9638SPeter Gruber 	struct cmdif *cif;
421109a9638SPeter Gruber 
422109a9638SPeter Gruber 	struct snd_pcm *pcm;
423109a9638SPeter Gruber 	struct snd_pcm *pcm_i2s;
424109a9638SPeter Gruber 	struct snd_rawmidi *rmidi;
425109a9638SPeter Gruber 	struct snd_opl3 *opl3;
426109a9638SPeter Gruber 	struct snd_ac97 *ac97;
427109a9638SPeter Gruber 	struct snd_ac97_bus *ac97_bus;
428109a9638SPeter Gruber 
429109a9638SPeter Gruber 	struct snd_pcm_substream *playback_substream[PLAYBACK_SUBSTREAMS];
430109a9638SPeter Gruber 	struct snd_pcm_substream *capture_substream;
431109a9638SPeter Gruber 
432109a9638SPeter Gruber 	int openstreams;
433109a9638SPeter Gruber 
434109a9638SPeter Gruber 	int irq;
435109a9638SPeter Gruber 	unsigned long port;
436109a9638SPeter Gruber 	unsigned short mpuaddr;
437109a9638SPeter Gruber 	unsigned short opladdr;
438109a9638SPeter Gruber #ifdef SUPPORT_JOYSTICK
439109a9638SPeter Gruber 	unsigned short gameaddr;
440109a9638SPeter Gruber #endif
441109a9638SPeter Gruber 	struct resource *res_port;
442109a9638SPeter Gruber 
443109a9638SPeter Gruber 	unsigned short device_id;
444109a9638SPeter Gruber 
445109a9638SPeter Gruber 	union firmware_version firmware;
446109a9638SPeter Gruber 
447109a9638SPeter Gruber 	spinlock_t lock;
448109a9638SPeter Gruber 	struct snd_info_entry *proc_entry;
449109a9638SPeter Gruber 
450109a9638SPeter Gruber 	unsigned long received_irqs;
451109a9638SPeter Gruber 	unsigned long handled_irqs;
452c7561cd8STakashi Iwai #ifdef CONFIG_PM_SLEEP
453109a9638SPeter Gruber 	int in_suspend;
454109a9638SPeter Gruber #endif
455109a9638SPeter Gruber };
456109a9638SPeter Gruber 
457109a9638SPeter Gruber struct sgd {			/* scatter gather desriptor */
458be05e3deSTakashi Iwai 	__le32 dwNextLink;
459be05e3deSTakashi Iwai 	__le32 dwSegPtrPhys;
460be05e3deSTakashi Iwai 	__le32 dwSegLen;
461be05e3deSTakashi Iwai 	__le32 dwStat_Ctl;
462109a9638SPeter Gruber };
463109a9638SPeter Gruber 
464109a9638SPeter Gruber struct pcmhw {			/* pcm descriptor */
465109a9638SPeter Gruber 	struct lbuspath paths;
466bf6f3d74STakashi Iwai 	const unsigned char *lbuspath;
467109a9638SPeter Gruber 	unsigned char source;
468109a9638SPeter Gruber 	unsigned char intdec[2];
469109a9638SPeter Gruber 	unsigned char mixer;
470109a9638SPeter Gruber 	unsigned char id;
471109a9638SPeter Gruber 	unsigned char state;
472109a9638SPeter Gruber 	unsigned int rate;
473109a9638SPeter Gruber 	unsigned int channels;
474109a9638SPeter Gruber 	snd_pcm_format_t format;
475109a9638SPeter Gruber 	struct snd_dma_buffer sgdlist;
476109a9638SPeter Gruber 	struct sgd *sgdbuf;
477109a9638SPeter Gruber 	unsigned int size;
478109a9638SPeter Gruber 	unsigned int pages;
479109a9638SPeter Gruber 	unsigned int oldpos;
480109a9638SPeter Gruber 	unsigned int pointer;
481109a9638SPeter Gruber };
482109a9638SPeter Gruber 
483109a9638SPeter Gruber #define CMDRET_ZERO (union cmdret){{(u32)0, (u32) 0}}
484109a9638SPeter Gruber 
485109a9638SPeter Gruber static int sendcmd(struct cmdif *cif, u32 flags, u32 cmd, u32 parm,
486109a9638SPeter Gruber 		   union cmdret *ret);
487109a9638SPeter Gruber static int getsourcesink(struct cmdif *cif, unsigned char source,
488109a9638SPeter Gruber 			 unsigned char sink, unsigned char *a,
489109a9638SPeter Gruber 			 unsigned char *b);
490109a9638SPeter Gruber static int snd_riptide_initialize(struct snd_riptide *chip);
491109a9638SPeter Gruber static int riptide_reset(struct cmdif *cif, struct snd_riptide *chip);
492109a9638SPeter Gruber 
493109a9638SPeter Gruber /*
494109a9638SPeter Gruber  */
495109a9638SPeter Gruber 
4969baa3c34SBenoit Taine static const struct pci_device_id snd_riptide_ids[] = {
497a693a26fSTakashi Iwai 	{ PCI_DEVICE(0x127a, 0x4310) },
498a693a26fSTakashi Iwai 	{ PCI_DEVICE(0x127a, 0x4320) },
499a693a26fSTakashi Iwai 	{ PCI_DEVICE(0x127a, 0x4330) },
500a693a26fSTakashi Iwai 	{ PCI_DEVICE(0x127a, 0x4340) },
501109a9638SPeter Gruber 	{0,},
502109a9638SPeter Gruber };
503109a9638SPeter Gruber 
504109a9638SPeter Gruber #ifdef SUPPORT_JOYSTICK
5059baa3c34SBenoit Taine static const struct pci_device_id snd_riptide_joystick_ids[] = {
506a693a26fSTakashi Iwai 	{ PCI_DEVICE(0x127a, 0x4312) },
507a693a26fSTakashi Iwai 	{ PCI_DEVICE(0x127a, 0x4322) },
508a693a26fSTakashi Iwai 	{ PCI_DEVICE(0x127a, 0x4332) },
509a693a26fSTakashi Iwai 	{ PCI_DEVICE(0x127a, 0x4342) },
510109a9638SPeter Gruber 	{0,},
511109a9638SPeter Gruber };
512109a9638SPeter Gruber #endif
513109a9638SPeter Gruber 
514109a9638SPeter Gruber MODULE_DEVICE_TABLE(pci, snd_riptide_ids);
515109a9638SPeter Gruber 
516109a9638SPeter Gruber /*
517109a9638SPeter Gruber  */
518109a9638SPeter Gruber 
519bf6f3d74STakashi Iwai static const unsigned char lbusin2out[E2SINK_MAX + 1][2] = {
520109a9638SPeter Gruber 	{NO_OUT, LS_NONE1}, {NO_OUT, LS_NONE2}, {NO_OUT, LS_NONE1}, {NO_OUT,
521109a9638SPeter Gruber 								     LS_NONE2},
522109a9638SPeter Gruber 	{NO_OUT, LS_NONE1}, {NO_OUT, LS_NONE2}, {NO_OUT, LS_NONE1}, {NO_OUT,
523109a9638SPeter Gruber 								     LS_NONE2},
524109a9638SPeter Gruber 	{INTER0_OUT, LS_SRC_INTERPOLATOR}, {INTER1_OUT, LS_SRC_INTERPOLATOR},
525109a9638SPeter Gruber 	{INTER2_OUT, LS_SRC_INTERPOLATOR}, {INTER3_OUT, LS_SRC_INTERPOLATOR},
526109a9638SPeter Gruber 	{INTER4_OUT, LS_SRC_INTERPOLATOR}, {INTERM0_OUT, LS_SRC_INTERPOLATORM},
527109a9638SPeter Gruber 	{INTERM1_OUT, LS_SRC_INTERPOLATORM}, {INTERM2_OUT,
528109a9638SPeter Gruber 					      LS_SRC_INTERPOLATORM},
529109a9638SPeter Gruber 	{INTERM3_OUT, LS_SRC_INTERPOLATORM}, {INTERM4_OUT,
530109a9638SPeter Gruber 					      LS_SRC_INTERPOLATORM},
531109a9638SPeter Gruber 	{INTERM5_OUT, LS_SRC_INTERPOLATORM}, {INTERM6_OUT,
532109a9638SPeter Gruber 					      LS_SRC_INTERPOLATORM},
533109a9638SPeter Gruber 	{DECIMM0_OUT, LS_SRC_DECIMATORM}, {DECIMM1_OUT, LS_SRC_DECIMATORM},
534109a9638SPeter Gruber 	{DECIMM2_OUT, LS_SRC_DECIMATORM}, {DECIMM3_OUT, LS_SRC_DECIMATORM},
535109a9638SPeter Gruber 	{DECIM0_OUT, LS_SRC_DECIMATOR}, {SR3_4_OUT, LS_NONE1}, {NO_OUT,
536109a9638SPeter Gruber 								LS_NONE2},
537109a9638SPeter Gruber 	{NO_OUT, LS_NONE1}, {NO_OUT, LS_NONE2}, {NO_OUT, LS_NONE1},
538109a9638SPeter Gruber 	{DIGITAL_MIXER_OUT0, LS_MIXER_IN}, {DIGITAL_MIXER_OUT0, LS_MIXER_IN},
539109a9638SPeter Gruber 	{DIGITAL_MIXER_OUT0, LS_MIXER_IN}, {DIGITAL_MIXER_OUT0, LS_MIXER_IN},
540109a9638SPeter Gruber 	{DIGITAL_MIXER_OUT0, LS_MIXER_IN}, {DIGITAL_MIXER_OUT0, LS_MIXER_IN},
541109a9638SPeter Gruber 	{DIGITAL_MIXER_OUT0, LS_MIXER_IN}, {DIGITAL_MIXER_OUT0, LS_MIXER_IN},
542109a9638SPeter Gruber 	{DIGITAL_MIXER_OUT0, LS_MIXER_IN}, {DIGITAL_MIXER_OUT0, LS_MIXER_IN},
543109a9638SPeter Gruber 	{DIGITAL_MIXER_OUT0, LS_MIXER_IN}, {DIGITAL_MIXER_OUT0, LS_MIXER_IN},
544109a9638SPeter Gruber 	{GAINFUNC0_OUT, LS_MIXER_GAIN_FUNCTION}, {GAINFUNC1_OUT,
545109a9638SPeter Gruber 						  LS_MIXER_GAIN_FUNCTION},
546109a9638SPeter Gruber 	{GAINFUNC2_OUT, LS_MIXER_GAIN_FUNCTION}, {GAINFUNC3_OUT,
547109a9638SPeter Gruber 						  LS_MIXER_GAIN_FUNCTION},
548109a9638SPeter Gruber 	{GAINFUNC4_OUT, LS_MIXER_GAIN_FUNCTION}, {SOFTMODEMTX, LS_NONE1},
549109a9638SPeter Gruber 	{SPLITTER0_OUTL, LS_SRC_SPLITTER}, {SPLITTER1_OUTL, LS_SRC_SPLITTER},
550109a9638SPeter Gruber 	{SPLITTER2_OUTL, LS_SRC_SPLITTER}, {SPLITTER3_OUTL, LS_SRC_SPLITTER},
551109a9638SPeter Gruber 	{MERGER0_OUT, LS_SRC_MERGER}, {MERGER0_OUT, LS_SRC_MERGER},
552109a9638SPeter Gruber 	{MERGER1_OUT, LS_SRC_MERGER},
553109a9638SPeter Gruber 	{MERGER1_OUT, LS_SRC_MERGER}, {MERGER2_OUT, LS_SRC_MERGER},
554109a9638SPeter Gruber 	{MERGER2_OUT, LS_SRC_MERGER},
555109a9638SPeter Gruber 	{MERGER3_OUT, LS_SRC_MERGER}, {MERGER3_OUT, LS_SRC_MERGER}, {NO_OUT,
556109a9638SPeter Gruber 								     LS_NONE2},
557109a9638SPeter Gruber };
558109a9638SPeter Gruber 
559bf6f3d74STakashi Iwai static const unsigned char lbus_play_opl3[] = {
560109a9638SPeter Gruber 	DIGITAL_MIXER_IN0 + FM_MIXER, 0xff
561109a9638SPeter Gruber };
562bf6f3d74STakashi Iwai static const unsigned char lbus_play_modem[] = {
563109a9638SPeter Gruber 	DIGITAL_MIXER_IN0 + MODEM_MIXER, 0xff
564109a9638SPeter Gruber };
565bf6f3d74STakashi Iwai static const unsigned char lbus_play_i2s[] = {
566109a9638SPeter Gruber 	INTER0_IN + I2S_INTDEC, DIGITAL_MIXER_IN0 + I2S_MIXER, 0xff
567109a9638SPeter Gruber };
568bf6f3d74STakashi Iwai static const unsigned char lbus_play_out[] = {
569109a9638SPeter Gruber 	PDAC2ACLNK, 0xff
570109a9638SPeter Gruber };
571bf6f3d74STakashi Iwai static const unsigned char lbus_play_outhp[] = {
572109a9638SPeter Gruber 	HNDSPK2ACLNK, 0xff
573109a9638SPeter Gruber };
574bf6f3d74STakashi Iwai static const unsigned char lbus_play_noconv1[] = {
575109a9638SPeter Gruber 	DIGITAL_MIXER_IN0, 0xff
576109a9638SPeter Gruber };
577bf6f3d74STakashi Iwai static const unsigned char lbus_play_stereo1[] = {
578109a9638SPeter Gruber 	INTER0_IN, DIGITAL_MIXER_IN0, 0xff
579109a9638SPeter Gruber };
580bf6f3d74STakashi Iwai static const unsigned char lbus_play_mono1[] = {
581109a9638SPeter Gruber 	INTERM0_IN, DIGITAL_MIXER_IN0, 0xff
582109a9638SPeter Gruber };
583bf6f3d74STakashi Iwai static const unsigned char lbus_play_noconv2[] = {
584109a9638SPeter Gruber 	DIGITAL_MIXER_IN1, 0xff
585109a9638SPeter Gruber };
586bf6f3d74STakashi Iwai static const unsigned char lbus_play_stereo2[] = {
587109a9638SPeter Gruber 	INTER1_IN, DIGITAL_MIXER_IN1, 0xff
588109a9638SPeter Gruber };
589bf6f3d74STakashi Iwai static const unsigned char lbus_play_mono2[] = {
590109a9638SPeter Gruber 	INTERM1_IN, DIGITAL_MIXER_IN1, 0xff
591109a9638SPeter Gruber };
592bf6f3d74STakashi Iwai static const unsigned char lbus_play_noconv3[] = {
593109a9638SPeter Gruber 	DIGITAL_MIXER_IN2, 0xff
594109a9638SPeter Gruber };
595bf6f3d74STakashi Iwai static const unsigned char lbus_play_stereo3[] = {
596109a9638SPeter Gruber 	INTER2_IN, DIGITAL_MIXER_IN2, 0xff
597109a9638SPeter Gruber };
598bf6f3d74STakashi Iwai static const unsigned char lbus_play_mono3[] = {
599109a9638SPeter Gruber 	INTERM2_IN, DIGITAL_MIXER_IN2, 0xff
600109a9638SPeter Gruber };
601bf6f3d74STakashi Iwai static const unsigned char lbus_rec_noconv1[] = {
602109a9638SPeter Gruber 	LBUS2ARM_FIFO5, 0xff
603109a9638SPeter Gruber };
604bf6f3d74STakashi Iwai static const unsigned char lbus_rec_stereo1[] = {
605109a9638SPeter Gruber 	DECIM0_IN, LBUS2ARM_FIFO5, 0xff
606109a9638SPeter Gruber };
607bf6f3d74STakashi Iwai static const unsigned char lbus_rec_mono1[] = {
608109a9638SPeter Gruber 	DECIMM3_IN, LBUS2ARM_FIFO5, 0xff
609109a9638SPeter Gruber };
610109a9638SPeter Gruber 
611bf6f3d74STakashi Iwai static const unsigned char play_ids[] = { 4, 1, 2, };
612bf6f3d74STakashi Iwai static const unsigned char play_sources[] = {
613109a9638SPeter Gruber 	ARM2LBUS_FIFO4, ARM2LBUS_FIFO1, ARM2LBUS_FIFO2,
614109a9638SPeter Gruber };
615bf6f3d74STakashi Iwai static const struct lbuspath lbus_play_paths[] = {
616109a9638SPeter Gruber 	{
617109a9638SPeter Gruber 	 .noconv = lbus_play_noconv1,
618109a9638SPeter Gruber 	 .stereo = lbus_play_stereo1,
619109a9638SPeter Gruber 	 .mono = lbus_play_mono1,
620109a9638SPeter Gruber 	 },
621109a9638SPeter Gruber 	{
622109a9638SPeter Gruber 	 .noconv = lbus_play_noconv2,
623109a9638SPeter Gruber 	 .stereo = lbus_play_stereo2,
624109a9638SPeter Gruber 	 .mono = lbus_play_mono2,
625109a9638SPeter Gruber 	 },
626109a9638SPeter Gruber 	{
627109a9638SPeter Gruber 	 .noconv = lbus_play_noconv3,
628109a9638SPeter Gruber 	 .stereo = lbus_play_stereo3,
629109a9638SPeter Gruber 	 .mono = lbus_play_mono3,
630109a9638SPeter Gruber 	 },
631109a9638SPeter Gruber };
632071f1344SJulia Lawall static const struct lbuspath lbus_rec_path = {
633109a9638SPeter Gruber 	.noconv = lbus_rec_noconv1,
634109a9638SPeter Gruber 	.stereo = lbus_rec_stereo1,
635109a9638SPeter Gruber 	.mono = lbus_rec_mono1,
636109a9638SPeter Gruber };
637109a9638SPeter Gruber 
638109a9638SPeter Gruber #define FIRMWARE_VERSIONS 1
639109a9638SPeter Gruber static union firmware_version firmware_versions[] = {
640109a9638SPeter Gruber 	{
641adf75dcaSClemens Ladisch 		.firmware = {
642adf75dcaSClemens Ladisch 			.ASIC = 3,
643adf75dcaSClemens Ladisch 			.CODEC = 2,
644adf75dcaSClemens Ladisch 			.AUXDSP = 3,
645adf75dcaSClemens Ladisch 			.PROG = 773,
646adf75dcaSClemens Ladisch 		},
647109a9638SPeter Gruber 	},
648109a9638SPeter Gruber };
649109a9638SPeter Gruber 
65093a9c901SDavid Woodhouse static u32 atoh(const unsigned char *in, unsigned int len)
651109a9638SPeter Gruber {
652109a9638SPeter Gruber 	u32 sum = 0;
653109a9638SPeter Gruber 	unsigned int mult = 1;
654109a9638SPeter Gruber 	unsigned char c;
655109a9638SPeter Gruber 
656109a9638SPeter Gruber 	while (len) {
657c9ff921aSAndy Shevchenko 		int value;
658c9ff921aSAndy Shevchenko 
659109a9638SPeter Gruber 		c = in[len - 1];
660c9ff921aSAndy Shevchenko 		value = hex_to_bin(c);
661c9ff921aSAndy Shevchenko 		if (value >= 0)
662c9ff921aSAndy Shevchenko 			sum += mult * value;
663109a9638SPeter Gruber 		mult *= 16;
664109a9638SPeter Gruber 		--len;
665109a9638SPeter Gruber 	}
666109a9638SPeter Gruber 	return sum;
667109a9638SPeter Gruber }
668109a9638SPeter Gruber 
66993a9c901SDavid Woodhouse static int senddata(struct cmdif *cif, const unsigned char *in, u32 offset)
670109a9638SPeter Gruber {
671109a9638SPeter Gruber 	u32 addr;
672109a9638SPeter Gruber 	u32 data;
673109a9638SPeter Gruber 	u32 i;
67493a9c901SDavid Woodhouse 	const unsigned char *p;
675109a9638SPeter Gruber 
676109a9638SPeter Gruber 	i = atoh(&in[1], 2);
677109a9638SPeter Gruber 	addr = offset + atoh(&in[3], 4);
678109a9638SPeter Gruber 	if (SEND_SMEM(cif, 0, addr) != 0)
679109a9638SPeter Gruber 		return -EACCES;
680109a9638SPeter Gruber 	p = in + 9;
681109a9638SPeter Gruber 	while (i) {
682109a9638SPeter Gruber 		data = atoh(p, 8);
683109a9638SPeter Gruber 		if (SEND_WMEM(cif, 2,
684109a9638SPeter Gruber 			      ((data & 0x0f0f0f0f) << 4) | ((data & 0xf0f0f0f0)
685109a9638SPeter Gruber 							    >> 4)))
686109a9638SPeter Gruber 			return -EACCES;
687109a9638SPeter Gruber 		i -= 4;
688109a9638SPeter Gruber 		p += 8;
689109a9638SPeter Gruber 	}
690109a9638SPeter Gruber 	return 0;
691109a9638SPeter Gruber }
692109a9638SPeter Gruber 
69393a9c901SDavid Woodhouse static int loadfirmware(struct cmdif *cif, const unsigned char *img,
694109a9638SPeter Gruber 			unsigned int size)
695109a9638SPeter Gruber {
69693a9c901SDavid Woodhouse 	const unsigned char *in;
697109a9638SPeter Gruber 	u32 laddr, saddr, t, val;
698109a9638SPeter Gruber 	int err = 0;
699109a9638SPeter Gruber 
700109a9638SPeter Gruber 	laddr = saddr = 0;
701109a9638SPeter Gruber 	while (size > 0 && err == 0) {
702109a9638SPeter Gruber 		in = img;
703109a9638SPeter Gruber 		if (in[0] == ':') {
704109a9638SPeter Gruber 			t = atoh(&in[7], 2);
705109a9638SPeter Gruber 			switch (t) {
706109a9638SPeter Gruber 			case DATA_REC:
707109a9638SPeter Gruber 				err = senddata(cif, in, laddr + saddr);
708109a9638SPeter Gruber 				break;
709109a9638SPeter Gruber 			case EXT_SEG_ADDR_REC:
710109a9638SPeter Gruber 				saddr = atoh(&in[9], 4) << 4;
711109a9638SPeter Gruber 				break;
712109a9638SPeter Gruber 			case EXT_LIN_ADDR_REC:
713109a9638SPeter Gruber 				laddr = atoh(&in[9], 4) << 16;
714109a9638SPeter Gruber 				break;
715109a9638SPeter Gruber 			case EXT_GOTO_CMD_REC:
716109a9638SPeter Gruber 				val = atoh(&in[9], 8);
717109a9638SPeter Gruber 				if (SEND_GOTO(cif, val) != 0)
718109a9638SPeter Gruber 					err = -EACCES;
719109a9638SPeter Gruber 				break;
720109a9638SPeter Gruber 			case EXT_END_OF_FILE:
721109a9638SPeter Gruber 				size = 0;
722109a9638SPeter Gruber 				break;
723109a9638SPeter Gruber 			default:
724109a9638SPeter Gruber 				break;
725109a9638SPeter Gruber 			}
726109a9638SPeter Gruber 			while (size > 0) {
727109a9638SPeter Gruber 				size--;
728109a9638SPeter Gruber 				if (*img++ == '\n')
729109a9638SPeter Gruber 					break;
730109a9638SPeter Gruber 			}
731109a9638SPeter Gruber 		}
732109a9638SPeter Gruber 	}
733109a9638SPeter Gruber 	snd_printdd("load firmware return %d\n", err);
734109a9638SPeter Gruber 	return err;
735109a9638SPeter Gruber }
736109a9638SPeter Gruber 
737109a9638SPeter Gruber static void
738109a9638SPeter Gruber alloclbuspath(struct cmdif *cif, unsigned char source,
739bf6f3d74STakashi Iwai 	      const unsigned char *path, unsigned char *mixer, unsigned char *s)
740109a9638SPeter Gruber {
741109a9638SPeter Gruber 	while (*path != 0xff) {
742109a9638SPeter Gruber 		unsigned char sink, type;
743109a9638SPeter Gruber 
744109a9638SPeter Gruber 		sink = *path & (~SPLIT_PATH);
745109a9638SPeter Gruber 		if (sink != E2SINK_MAX) {
746109a9638SPeter Gruber 			snd_printdd("alloc path 0x%x->0x%x\n", source, sink);
747109a9638SPeter Gruber 			SEND_PSEL(cif, source, sink);
748109a9638SPeter Gruber 			source = lbusin2out[sink][0];
749109a9638SPeter Gruber 			type = lbusin2out[sink][1];
750109a9638SPeter Gruber 			if (type == LS_MIXER_IN) {
751109a9638SPeter Gruber 				if (mixer)
752109a9638SPeter Gruber 					*mixer = sink - DIGITAL_MIXER_IN0;
753109a9638SPeter Gruber 			}
754109a9638SPeter Gruber 			if (type == LS_SRC_DECIMATORM ||
755109a9638SPeter Gruber 			    type == LS_SRC_DECIMATOR ||
756109a9638SPeter Gruber 			    type == LS_SRC_INTERPOLATORM ||
757109a9638SPeter Gruber 			    type == LS_SRC_INTERPOLATOR) {
758109a9638SPeter Gruber 				if (s) {
759109a9638SPeter Gruber 					if (s[0] != 0xff)
760109a9638SPeter Gruber 						s[1] = sink;
761109a9638SPeter Gruber 					else
762109a9638SPeter Gruber 						s[0] = sink;
763109a9638SPeter Gruber 				}
764109a9638SPeter Gruber 			}
765109a9638SPeter Gruber 		}
766109a9638SPeter Gruber 		if (*path++ & SPLIT_PATH) {
767bf6f3d74STakashi Iwai 			const unsigned char *npath = path;
768109a9638SPeter Gruber 
769109a9638SPeter Gruber 			while (*npath != 0xff)
770109a9638SPeter Gruber 				npath++;
771109a9638SPeter Gruber 			alloclbuspath(cif, source + 1, ++npath, mixer, s);
772109a9638SPeter Gruber 		}
773109a9638SPeter Gruber 	}
774109a9638SPeter Gruber }
775109a9638SPeter Gruber 
776109a9638SPeter Gruber static void
777bf6f3d74STakashi Iwai freelbuspath(struct cmdif *cif, unsigned char source, const unsigned char *path)
778109a9638SPeter Gruber {
779109a9638SPeter Gruber 	while (*path != 0xff) {
780109a9638SPeter Gruber 		unsigned char sink;
781109a9638SPeter Gruber 
782109a9638SPeter Gruber 		sink = *path & (~SPLIT_PATH);
783109a9638SPeter Gruber 		if (sink != E2SINK_MAX) {
784109a9638SPeter Gruber 			snd_printdd("free path 0x%x->0x%x\n", source, sink);
785109a9638SPeter Gruber 			SEND_PCLR(cif, source, sink);
786109a9638SPeter Gruber 			source = lbusin2out[sink][0];
787109a9638SPeter Gruber 		}
788109a9638SPeter Gruber 		if (*path++ & SPLIT_PATH) {
789bf6f3d74STakashi Iwai 			const unsigned char *npath = path;
790109a9638SPeter Gruber 
791109a9638SPeter Gruber 			while (*npath != 0xff)
792109a9638SPeter Gruber 				npath++;
793109a9638SPeter Gruber 			freelbuspath(cif, source + 1, ++npath);
794109a9638SPeter Gruber 		}
795109a9638SPeter Gruber 	}
796109a9638SPeter Gruber }
797109a9638SPeter Gruber 
798109a9638SPeter Gruber static int writearm(struct cmdif *cif, u32 addr, u32 data, u32 mask)
799109a9638SPeter Gruber {
800109a9638SPeter Gruber 	union cmdret rptr = CMDRET_ZERO;
801109a9638SPeter Gruber 	unsigned int i = MAX_WRITE_RETRY;
802109a9638SPeter Gruber 	int flag = 1;
803109a9638SPeter Gruber 
804109a9638SPeter Gruber 	SEND_RMEM(cif, 0x02, addr, &rptr);
805109a9638SPeter Gruber 	rptr.retlongs[0] &= (~mask);
806109a9638SPeter Gruber 
807109a9638SPeter Gruber 	while (--i) {
808109a9638SPeter Gruber 		SEND_SMEM(cif, 0x01, addr);
809109a9638SPeter Gruber 		SEND_WMEM(cif, 0x02, (rptr.retlongs[0] | data));
810109a9638SPeter Gruber 		SEND_RMEM(cif, 0x02, addr, &rptr);
811109a9638SPeter Gruber 		if ((rptr.retlongs[0] & data) == data) {
812109a9638SPeter Gruber 			flag = 0;
813109a9638SPeter Gruber 			break;
814109a9638SPeter Gruber 		} else
815109a9638SPeter Gruber 			rptr.retlongs[0] &= ~mask;
816109a9638SPeter Gruber 	}
817109a9638SPeter Gruber 	snd_printdd("send arm 0x%x 0x%x 0x%x return %d\n", addr, data, mask,
818109a9638SPeter Gruber 		    flag);
819109a9638SPeter Gruber 	return flag;
820109a9638SPeter Gruber }
821109a9638SPeter Gruber 
822109a9638SPeter Gruber static int sendcmd(struct cmdif *cif, u32 flags, u32 cmd, u32 parm,
823109a9638SPeter Gruber 		   union cmdret *ret)
824109a9638SPeter Gruber {
825109a9638SPeter Gruber 	int i, j;
826109a9638SPeter Gruber 	int err;
827109a9638SPeter Gruber 	unsigned int time = 0;
828109a9638SPeter Gruber 	unsigned long irqflags;
829109a9638SPeter Gruber 	struct riptideport *hwport;
830109a9638SPeter Gruber 	struct cmdport *cmdport = NULL;
831109a9638SPeter Gruber 
832da3cec35STakashi Iwai 	if (snd_BUG_ON(!cif))
833da3cec35STakashi Iwai 		return -EINVAL;
834109a9638SPeter Gruber 
835109a9638SPeter Gruber 	hwport = cif->hwport;
836109a9638SPeter Gruber 	if (cif->errcnt > MAX_ERROR_COUNT) {
837109a9638SPeter Gruber 		if (cif->is_reset) {
838109a9638SPeter Gruber 			snd_printk(KERN_ERR
839109a9638SPeter Gruber 				   "Riptide: Too many failed cmds, reinitializing\n");
840109a9638SPeter Gruber 			if (riptide_reset(cif, NULL) == 0) {
841109a9638SPeter Gruber 				cif->errcnt = 0;
842109a9638SPeter Gruber 				return -EIO;
843109a9638SPeter Gruber 			}
844109a9638SPeter Gruber 		}
845109a9638SPeter Gruber 		snd_printk(KERN_ERR "Riptide: Initialization failed.\n");
846109a9638SPeter Gruber 		return -EINVAL;
847109a9638SPeter Gruber 	}
848109a9638SPeter Gruber 	if (ret) {
849109a9638SPeter Gruber 		ret->retlongs[0] = 0;
850109a9638SPeter Gruber 		ret->retlongs[1] = 0;
851109a9638SPeter Gruber 	}
852109a9638SPeter Gruber 	i = 0;
853109a9638SPeter Gruber 	spin_lock_irqsave(&cif->lock, irqflags);
854109a9638SPeter Gruber 	while (i++ < CMDIF_TIMEOUT && !IS_READY(cif->hwport))
855109a9638SPeter Gruber 		udelay(10);
8561b1cc7f2SRoel Kluin 	if (i > CMDIF_TIMEOUT) {
857109a9638SPeter Gruber 		err = -EBUSY;
858109a9638SPeter Gruber 		goto errout;
859109a9638SPeter Gruber 	}
860109a9638SPeter Gruber 
861109a9638SPeter Gruber 	err = 0;
862109a9638SPeter Gruber 	for (j = 0, time = 0; time < CMDIF_TIMEOUT; j++, time += 2) {
863109a9638SPeter Gruber 		cmdport = &(hwport->port[j % 2]);
864109a9638SPeter Gruber 		if (IS_DATF(cmdport)) {	/* free pending data */
865109a9638SPeter Gruber 			READ_PORT_ULONG(cmdport->data1);
866109a9638SPeter Gruber 			READ_PORT_ULONG(cmdport->data2);
867109a9638SPeter Gruber 		}
868109a9638SPeter Gruber 		if (IS_CMDE(cmdport)) {
869109a9638SPeter Gruber 			if (flags & PARM)	/* put data */
870109a9638SPeter Gruber 				WRITE_PORT_ULONG(cmdport->data2, parm);
871109a9638SPeter Gruber 			WRITE_PORT_ULONG(cmdport->data1, cmd);	/* write cmd */
872109a9638SPeter Gruber 			if ((flags & RESP) && ret) {
873109a9638SPeter Gruber 				while (!IS_DATF(cmdport) &&
8741b1cc7f2SRoel Kluin 				       time < CMDIF_TIMEOUT) {
875109a9638SPeter Gruber 					udelay(10);
8761b1cc7f2SRoel Kluin 					time++;
8771b1cc7f2SRoel Kluin 				}
878109a9638SPeter Gruber 				if (time < CMDIF_TIMEOUT) {	/* read response */
879109a9638SPeter Gruber 					ret->retlongs[0] =
880109a9638SPeter Gruber 					    READ_PORT_ULONG(cmdport->data1);
881109a9638SPeter Gruber 					ret->retlongs[1] =
882109a9638SPeter Gruber 					    READ_PORT_ULONG(cmdport->data2);
883109a9638SPeter Gruber 				} else {
884109a9638SPeter Gruber 					err = -ENOSYS;
885109a9638SPeter Gruber 					goto errout;
886109a9638SPeter Gruber 				}
887109a9638SPeter Gruber 			}
888109a9638SPeter Gruber 			break;
889109a9638SPeter Gruber 		}
890109a9638SPeter Gruber 		udelay(20);
891109a9638SPeter Gruber 	}
892109a9638SPeter Gruber 	if (time == CMDIF_TIMEOUT) {
893109a9638SPeter Gruber 		err = -ENODATA;
894109a9638SPeter Gruber 		goto errout;
895109a9638SPeter Gruber 	}
896109a9638SPeter Gruber 	spin_unlock_irqrestore(&cif->lock, irqflags);
897109a9638SPeter Gruber 
898109a9638SPeter Gruber 	cif->cmdcnt++;		/* update command statistics */
899109a9638SPeter Gruber 	cif->cmdtime += time;
900109a9638SPeter Gruber 	if (time > cif->cmdtimemax)
901109a9638SPeter Gruber 		cif->cmdtimemax = time;
902109a9638SPeter Gruber 	if (time < cif->cmdtimemin)
903109a9638SPeter Gruber 		cif->cmdtimemin = time;
904109a9638SPeter Gruber 	if ((cif->cmdcnt) % 1000 == 0)
905109a9638SPeter Gruber 		snd_printdd
906109a9638SPeter Gruber 		    ("send cmd %d time: %d mintime: %d maxtime %d err: %d\n",
907109a9638SPeter Gruber 		     cif->cmdcnt, cif->cmdtime, cif->cmdtimemin,
908109a9638SPeter Gruber 		     cif->cmdtimemax, cif->errcnt);
909109a9638SPeter Gruber 	return 0;
910109a9638SPeter Gruber 
911109a9638SPeter Gruber       errout:
912109a9638SPeter Gruber 	cif->errcnt++;
913109a9638SPeter Gruber 	spin_unlock_irqrestore(&cif->lock, irqflags);
914109a9638SPeter Gruber 	snd_printdd
915109a9638SPeter Gruber 	    ("send cmd %d hw: 0x%x flag: 0x%x cmd: 0x%x parm: 0x%x ret: 0x%x 0x%x CMDE: %d DATF: %d failed %d\n",
916109a9638SPeter Gruber 	     cif->cmdcnt, (int)((void *)&(cmdport->stat) - (void *)hwport),
917109a9638SPeter Gruber 	     flags, cmd, parm, ret ? ret->retlongs[0] : 0,
918109a9638SPeter Gruber 	     ret ? ret->retlongs[1] : 0, IS_CMDE(cmdport), IS_DATF(cmdport),
919109a9638SPeter Gruber 	     err);
920109a9638SPeter Gruber 	return err;
921109a9638SPeter Gruber }
922109a9638SPeter Gruber 
923109a9638SPeter Gruber static int
924109a9638SPeter Gruber setmixer(struct cmdif *cif, short num, unsigned short rval, unsigned short lval)
925109a9638SPeter Gruber {
926109a9638SPeter Gruber 	union cmdret rptr = CMDRET_ZERO;
927109a9638SPeter Gruber 	int i = 0;
928109a9638SPeter Gruber 
92947efe636SHans Wennborg 	snd_printdd("sent mixer %d: 0x%x 0x%x\n", num, rval, lval);
930109a9638SPeter Gruber 	do {
931109a9638SPeter Gruber 		SEND_SDGV(cif, num, num, rval, lval);
932109a9638SPeter Gruber 		SEND_RDGV(cif, num, num, &rptr);
933109a9638SPeter Gruber 		if (rptr.retwords[0] == lval && rptr.retwords[1] == rval)
934109a9638SPeter Gruber 			return 0;
935109a9638SPeter Gruber 	} while (i++ < MAX_WRITE_RETRY);
936109a9638SPeter Gruber 	snd_printdd("sent mixer failed\n");
937109a9638SPeter Gruber 	return -EIO;
938109a9638SPeter Gruber }
939109a9638SPeter Gruber 
940109a9638SPeter Gruber static int getpaths(struct cmdif *cif, unsigned char *o)
941109a9638SPeter Gruber {
942109a9638SPeter Gruber 	unsigned char src[E2SINK_MAX];
943109a9638SPeter Gruber 	unsigned char sink[E2SINK_MAX];
944109a9638SPeter Gruber 	int i, j = 0;
945109a9638SPeter Gruber 
946109a9638SPeter Gruber 	for (i = 0; i < E2SINK_MAX; i++) {
947109a9638SPeter Gruber 		getsourcesink(cif, i, i, &src[i], &sink[i]);
948109a9638SPeter Gruber 		if (sink[i] < E2SINK_MAX) {
949109a9638SPeter Gruber 			o[j++] = sink[i];
950109a9638SPeter Gruber 			o[j++] = i;
951109a9638SPeter Gruber 		}
952109a9638SPeter Gruber 	}
953109a9638SPeter Gruber 	return j;
954109a9638SPeter Gruber }
955109a9638SPeter Gruber 
956109a9638SPeter Gruber static int
957109a9638SPeter Gruber getsourcesink(struct cmdif *cif, unsigned char source, unsigned char sink,
958109a9638SPeter Gruber 	      unsigned char *a, unsigned char *b)
959109a9638SPeter Gruber {
960109a9638SPeter Gruber 	union cmdret rptr = CMDRET_ZERO;
961109a9638SPeter Gruber 
962109a9638SPeter Gruber 	if (SEND_RSSV(cif, source, sink, &rptr) &&
963109a9638SPeter Gruber 	    SEND_RSSV(cif, source, sink, &rptr))
964109a9638SPeter Gruber 		return -EIO;
965109a9638SPeter Gruber 	*a = rptr.retbytes[0];
966109a9638SPeter Gruber 	*b = rptr.retbytes[1];
967109a9638SPeter Gruber 	snd_printdd("getsourcesink 0x%x 0x%x\n", *a, *b);
968109a9638SPeter Gruber 	return 0;
969109a9638SPeter Gruber }
970109a9638SPeter Gruber 
971109a9638SPeter Gruber static int
972109a9638SPeter Gruber getsamplerate(struct cmdif *cif, unsigned char *intdec, unsigned int *rate)
973109a9638SPeter Gruber {
974109a9638SPeter Gruber 	unsigned char *s;
975109a9638SPeter Gruber 	unsigned int p[2] = { 0, 0 };
976109a9638SPeter Gruber 	int i;
977109a9638SPeter Gruber 	union cmdret rptr = CMDRET_ZERO;
978109a9638SPeter Gruber 
979109a9638SPeter Gruber 	s = intdec;
980109a9638SPeter Gruber 	for (i = 0; i < 2; i++) {
981109a9638SPeter Gruber 		if (*s != 0xff) {
982109a9638SPeter Gruber 			if (SEND_RSRC(cif, *s, &rptr) &&
983109a9638SPeter Gruber 			    SEND_RSRC(cif, *s, &rptr))
984109a9638SPeter Gruber 				return -EIO;
985109a9638SPeter Gruber 			p[i] += rptr.retwords[1];
986109a9638SPeter Gruber 			p[i] *= rptr.retwords[2];
987109a9638SPeter Gruber 			p[i] += rptr.retwords[3];
988109a9638SPeter Gruber 			p[i] /= 65536;
989109a9638SPeter Gruber 		}
990109a9638SPeter Gruber 		s++;
991109a9638SPeter Gruber 	}
992109a9638SPeter Gruber 	if (p[0]) {
993109a9638SPeter Gruber 		if (p[1] != p[0])
994109a9638SPeter Gruber 			snd_printdd("rates differ %d %d\n", p[0], p[1]);
995109a9638SPeter Gruber 		*rate = (unsigned int)p[0];
996109a9638SPeter Gruber 	} else
997109a9638SPeter Gruber 		*rate = (unsigned int)p[1];
998109a9638SPeter Gruber 	snd_printdd("getsampleformat %d %d %d\n", intdec[0], intdec[1], *rate);
999109a9638SPeter Gruber 	return 0;
1000109a9638SPeter Gruber }
1001109a9638SPeter Gruber 
1002109a9638SPeter Gruber static int
1003109a9638SPeter Gruber setsampleformat(struct cmdif *cif,
1004109a9638SPeter Gruber 		unsigned char mixer, unsigned char id,
100594dfee0cSTakashi Iwai 		unsigned char channels, snd_pcm_format_t format)
1006109a9638SPeter Gruber {
1007109a9638SPeter Gruber 	unsigned char w, ch, sig, order;
1008109a9638SPeter Gruber 
1009109a9638SPeter Gruber 	snd_printdd
1010109a9638SPeter Gruber 	    ("setsampleformat mixer: %d id: %d channels: %d format: %d\n",
1011109a9638SPeter Gruber 	     mixer, id, channels, format);
1012109a9638SPeter Gruber 	ch = channels == 1;
1013109a9638SPeter Gruber 	w = snd_pcm_format_width(format) == 8;
1014109a9638SPeter Gruber 	sig = snd_pcm_format_unsigned(format) != 0;
1015109a9638SPeter Gruber 	order = snd_pcm_format_big_endian(format) != 0;
1016109a9638SPeter Gruber 
1017109a9638SPeter Gruber 	if (SEND_SETF(cif, mixer, w, ch, order, sig, id) &&
1018109a9638SPeter Gruber 	    SEND_SETF(cif, mixer, w, ch, order, sig, id)) {
1019109a9638SPeter Gruber 		snd_printdd("setsampleformat failed\n");
1020109a9638SPeter Gruber 		return -EIO;
1021109a9638SPeter Gruber 	}
1022109a9638SPeter Gruber 	return 0;
1023109a9638SPeter Gruber }
1024109a9638SPeter Gruber 
1025109a9638SPeter Gruber static int
1026109a9638SPeter Gruber setsamplerate(struct cmdif *cif, unsigned char *intdec, unsigned int rate)
1027109a9638SPeter Gruber {
1028109a9638SPeter Gruber 	u32 D, M, N;
1029109a9638SPeter Gruber 	union cmdret rptr = CMDRET_ZERO;
1030109a9638SPeter Gruber 	int i;
1031109a9638SPeter Gruber 
1032109a9638SPeter Gruber 	snd_printdd("setsamplerate intdec: %d,%d rate: %d\n", intdec[0],
1033109a9638SPeter Gruber 		    intdec[1], rate);
1034109a9638SPeter Gruber 	D = 48000;
1035109a9638SPeter Gruber 	M = ((rate == 48000) ? 47999 : rate) * 65536;
1036109a9638SPeter Gruber 	N = M % D;
1037109a9638SPeter Gruber 	M /= D;
1038109a9638SPeter Gruber 	for (i = 0; i < 2; i++) {
1039109a9638SPeter Gruber 		if (*intdec != 0xff) {
1040109a9638SPeter Gruber 			do {
1041109a9638SPeter Gruber 				SEND_SSRC(cif, *intdec, D, M, N);
1042109a9638SPeter Gruber 				SEND_RSRC(cif, *intdec, &rptr);
1043109a9638SPeter Gruber 			} while (rptr.retwords[1] != D &&
1044109a9638SPeter Gruber 				 rptr.retwords[2] != M &&
1045109a9638SPeter Gruber 				 rptr.retwords[3] != N &&
1046109a9638SPeter Gruber 				 i++ < MAX_WRITE_RETRY);
10479980c620SRoel Kluin 			if (i > MAX_WRITE_RETRY) {
1048109a9638SPeter Gruber 				snd_printdd("sent samplerate %d: %d failed\n",
1049109a9638SPeter Gruber 					    *intdec, rate);
1050109a9638SPeter Gruber 				return -EIO;
1051109a9638SPeter Gruber 			}
1052109a9638SPeter Gruber 		}
1053109a9638SPeter Gruber 		intdec++;
1054109a9638SPeter Gruber 	}
1055109a9638SPeter Gruber 	return 0;
1056109a9638SPeter Gruber }
1057109a9638SPeter Gruber 
1058109a9638SPeter Gruber static int
1059109a9638SPeter Gruber getmixer(struct cmdif *cif, short num, unsigned short *rval,
1060109a9638SPeter Gruber 	 unsigned short *lval)
1061109a9638SPeter Gruber {
1062109a9638SPeter Gruber 	union cmdret rptr = CMDRET_ZERO;
1063109a9638SPeter Gruber 
1064109a9638SPeter Gruber 	if (SEND_RDGV(cif, num, num, &rptr) && SEND_RDGV(cif, num, num, &rptr))
1065109a9638SPeter Gruber 		return -EIO;
1066109a9638SPeter Gruber 	*rval = rptr.retwords[0];
1067109a9638SPeter Gruber 	*lval = rptr.retwords[1];
106847efe636SHans Wennborg 	snd_printdd("got mixer %d: 0x%x 0x%x\n", num, *rval, *lval);
1069109a9638SPeter Gruber 	return 0;
1070109a9638SPeter Gruber }
1071109a9638SPeter Gruber 
1072*2ac55dafSTakashi Iwai static irqreturn_t riptide_handleirq(int irq, void *dev_id)
1073109a9638SPeter Gruber {
1074*2ac55dafSTakashi Iwai 	struct snd_riptide *chip = dev_id;
1075109a9638SPeter Gruber 	struct cmdif *cif = chip->cif;
1076109a9638SPeter Gruber 	struct snd_pcm_substream *substream[PLAYBACK_SUBSTREAMS + 1];
1077109a9638SPeter Gruber 	struct snd_pcm_runtime *runtime;
1078109a9638SPeter Gruber 	struct pcmhw *data = NULL;
1079109a9638SPeter Gruber 	unsigned int pos, period_bytes;
1080109a9638SPeter Gruber 	struct sgd *c;
1081109a9638SPeter Gruber 	int i, j;
1082109a9638SPeter Gruber 	unsigned int flag;
1083109a9638SPeter Gruber 
1084109a9638SPeter Gruber 	if (!cif)
1085*2ac55dafSTakashi Iwai 		return IRQ_HANDLED;
1086109a9638SPeter Gruber 
1087109a9638SPeter Gruber 	for (i = 0; i < PLAYBACK_SUBSTREAMS; i++)
1088109a9638SPeter Gruber 		substream[i] = chip->playback_substream[i];
1089109a9638SPeter Gruber 	substream[i] = chip->capture_substream;
1090109a9638SPeter Gruber 	for (i = 0; i < PLAYBACK_SUBSTREAMS + 1; i++) {
1091109a9638SPeter Gruber 		if (substream[i] &&
1092109a9638SPeter Gruber 		    (runtime = substream[i]->runtime) &&
1093109a9638SPeter Gruber 		    (data = runtime->private_data) && data->state != ST_STOP) {
1094109a9638SPeter Gruber 			pos = 0;
1095109a9638SPeter Gruber 			for (j = 0; j < data->pages; j++) {
1096109a9638SPeter Gruber 				c = &data->sgdbuf[j];
1097109a9638SPeter Gruber 				flag = le32_to_cpu(c->dwStat_Ctl);
1098109a9638SPeter Gruber 				if (flag & EOB_STATUS)
1099109a9638SPeter Gruber 					pos += le32_to_cpu(c->dwSegLen);
1100109a9638SPeter Gruber 				if (flag & EOC_STATUS)
1101109a9638SPeter Gruber 					pos += le32_to_cpu(c->dwSegLen);
1102109a9638SPeter Gruber 				if ((flag & EOS_STATUS)
1103109a9638SPeter Gruber 				    && (data->state == ST_PLAY)) {
1104109a9638SPeter Gruber 					data->state = ST_STOP;
1105109a9638SPeter Gruber 					snd_printk(KERN_ERR
1106109a9638SPeter Gruber 						   "Riptide: DMA stopped unexpectedly\n");
1107109a9638SPeter Gruber 				}
1108109a9638SPeter Gruber 				c->dwStat_Ctl =
1109109a9638SPeter Gruber 				    cpu_to_le32(flag &
1110109a9638SPeter Gruber 						~(EOS_STATUS | EOB_STATUS |
1111109a9638SPeter Gruber 						  EOC_STATUS));
1112109a9638SPeter Gruber 			}
1113109a9638SPeter Gruber 			data->pointer += pos;
1114109a9638SPeter Gruber 			pos += data->oldpos;
1115109a9638SPeter Gruber 			if (data->state != ST_STOP) {
1116109a9638SPeter Gruber 				period_bytes =
1117109a9638SPeter Gruber 				    frames_to_bytes(runtime,
1118109a9638SPeter Gruber 						    runtime->period_size);
1119109a9638SPeter Gruber 				snd_printdd
1120109a9638SPeter Gruber 				    ("interrupt 0x%x after 0x%lx of 0x%lx frames in period\n",
1121109a9638SPeter Gruber 				     READ_AUDIO_STATUS(cif->hwport),
1122109a9638SPeter Gruber 				     bytes_to_frames(runtime, pos),
1123109a9638SPeter Gruber 				     runtime->period_size);
1124109a9638SPeter Gruber 				j = 0;
1125109a9638SPeter Gruber 				if (pos >= period_bytes) {
1126109a9638SPeter Gruber 					j++;
1127109a9638SPeter Gruber 					while (pos >= period_bytes)
1128109a9638SPeter Gruber 						pos -= period_bytes;
1129109a9638SPeter Gruber 				}
1130109a9638SPeter Gruber 				data->oldpos = pos;
1131109a9638SPeter Gruber 				if (j > 0)
1132109a9638SPeter Gruber 					snd_pcm_period_elapsed(substream[i]);
1133109a9638SPeter Gruber 			}
1134109a9638SPeter Gruber 		}
1135109a9638SPeter Gruber 	}
1136*2ac55dafSTakashi Iwai 
1137*2ac55dafSTakashi Iwai 	return IRQ_HANDLED;
1138109a9638SPeter Gruber }
1139109a9638SPeter Gruber 
1140c7561cd8STakashi Iwai #ifdef CONFIG_PM_SLEEP
114168cb2b55STakashi Iwai static int riptide_suspend(struct device *dev)
1142109a9638SPeter Gruber {
114368cb2b55STakashi Iwai 	struct snd_card *card = dev_get_drvdata(dev);
1144109a9638SPeter Gruber 	struct snd_riptide *chip = card->private_data;
1145109a9638SPeter Gruber 
1146109a9638SPeter Gruber 	chip->in_suspend = 1;
1147109a9638SPeter Gruber 	snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
1148109a9638SPeter Gruber 	snd_ac97_suspend(chip->ac97);
1149109a9638SPeter Gruber 	return 0;
1150109a9638SPeter Gruber }
1151109a9638SPeter Gruber 
115268cb2b55STakashi Iwai static int riptide_resume(struct device *dev)
1153109a9638SPeter Gruber {
115468cb2b55STakashi Iwai 	struct snd_card *card = dev_get_drvdata(dev);
1155109a9638SPeter Gruber 	struct snd_riptide *chip = card->private_data;
1156109a9638SPeter Gruber 
1157109a9638SPeter Gruber 	snd_riptide_initialize(chip);
1158109a9638SPeter Gruber 	snd_ac97_resume(chip->ac97);
1159109a9638SPeter Gruber 	snd_power_change_state(card, SNDRV_CTL_POWER_D0);
1160109a9638SPeter Gruber 	chip->in_suspend = 0;
1161109a9638SPeter Gruber 	return 0;
1162109a9638SPeter Gruber }
116368cb2b55STakashi Iwai 
116468cb2b55STakashi Iwai static SIMPLE_DEV_PM_OPS(riptide_pm, riptide_suspend, riptide_resume);
116568cb2b55STakashi Iwai #define RIPTIDE_PM_OPS	&riptide_pm
116668cb2b55STakashi Iwai #else
116768cb2b55STakashi Iwai #define RIPTIDE_PM_OPS	NULL
1168c7561cd8STakashi Iwai #endif /* CONFIG_PM_SLEEP */
1169109a9638SPeter Gruber 
1170a693a26fSTakashi Iwai static int try_to_load_firmware(struct cmdif *cif, struct snd_riptide *chip)
1171109a9638SPeter Gruber {
1172a693a26fSTakashi Iwai 	union firmware_version firmware = { .ret = CMDRET_ZERO };
1173a693a26fSTakashi Iwai 	int i, timeout, err;
1174109a9638SPeter Gruber 
1175109a9638SPeter Gruber 	for (i = 0; i < 2; i++) {
1176109a9638SPeter Gruber 		WRITE_PORT_ULONG(cif->hwport->port[i].data1, 0);
1177109a9638SPeter Gruber 		WRITE_PORT_ULONG(cif->hwport->port[i].data2, 0);
1178109a9638SPeter Gruber 	}
1179109a9638SPeter Gruber 	SET_GRESET(cif->hwport);
1180109a9638SPeter Gruber 	udelay(100);
1181109a9638SPeter Gruber 	UNSET_GRESET(cif->hwport);
1182109a9638SPeter Gruber 	udelay(100);
1183109a9638SPeter Gruber 
1184109a9638SPeter Gruber 	for (timeout = 100000; --timeout; udelay(10)) {
1185109a9638SPeter Gruber 		if (IS_READY(cif->hwport) && !IS_GERR(cif->hwport))
1186109a9638SPeter Gruber 			break;
1187109a9638SPeter Gruber 	}
1188a693a26fSTakashi Iwai 	if (!timeout) {
1189109a9638SPeter Gruber 		snd_printk(KERN_ERR
1190a693a26fSTakashi Iwai 			   "Riptide: device not ready, audio status: 0x%x "
1191a693a26fSTakashi Iwai 			   "ready: %d gerr: %d\n",
1192109a9638SPeter Gruber 			   READ_AUDIO_STATUS(cif->hwport),
1193109a9638SPeter Gruber 			   IS_READY(cif->hwport), IS_GERR(cif->hwport));
1194109a9638SPeter Gruber 		return -EIO;
1195109a9638SPeter Gruber 	} else {
1196109a9638SPeter Gruber 		snd_printdd
1197109a9638SPeter Gruber 			("Riptide: audio status: 0x%x ready: %d gerr: %d\n",
1198109a9638SPeter Gruber 			 READ_AUDIO_STATUS(cif->hwport),
1199109a9638SPeter Gruber 			 IS_READY(cif->hwport), IS_GERR(cif->hwport));
1200109a9638SPeter Gruber 	}
1201109a9638SPeter Gruber 
1202a693a26fSTakashi Iwai 	SEND_GETV(cif, &firmware.ret);
1203a693a26fSTakashi Iwai 	snd_printdd("Firmware version: ASIC: %d CODEC %d AUXDSP %d PROG %d\n",
1204109a9638SPeter Gruber 		    firmware.firmware.ASIC, firmware.firmware.CODEC,
1205109a9638SPeter Gruber 		    firmware.firmware.AUXDSP, firmware.firmware.PROG);
1206109a9638SPeter Gruber 
1207a5ba6bebSTakashi Iwai 	if (!chip)
1208a5ba6bebSTakashi Iwai 		return 1;
1209a5ba6bebSTakashi Iwai 
1210a693a26fSTakashi Iwai 	for (i = 0; i < FIRMWARE_VERSIONS; i++) {
1211a693a26fSTakashi Iwai 		if (!memcmp(&firmware_versions[i], &firmware, sizeof(firmware)))
1212a693a26fSTakashi Iwai 			return 1; /* OK */
1213a693a26fSTakashi Iwai 
1214a5ba6bebSTakashi Iwai 	}
1215a5ba6bebSTakashi Iwai 
1216109a9638SPeter Gruber 	snd_printdd("Writing Firmware\n");
1217109a9638SPeter Gruber 	if (!chip->fw_entry) {
1218a693a26fSTakashi Iwai 		err = request_firmware(&chip->fw_entry, "riptide.hex",
1219a693a26fSTakashi Iwai 				       &chip->pci->dev);
1220a693a26fSTakashi Iwai 		if (err) {
1221109a9638SPeter Gruber 			snd_printk(KERN_ERR
1222a693a26fSTakashi Iwai 				   "Riptide: Firmware not available %d\n", err);
1223109a9638SPeter Gruber 			return -EIO;
1224109a9638SPeter Gruber 		}
1225109a9638SPeter Gruber 	}
1226a693a26fSTakashi Iwai 	err = loadfirmware(cif, chip->fw_entry->data, chip->fw_entry->size);
1227a693a26fSTakashi Iwai 	if (err) {
1228109a9638SPeter Gruber 		snd_printk(KERN_ERR
1229a693a26fSTakashi Iwai 			   "Riptide: Could not load firmware %d\n", err);
1230a693a26fSTakashi Iwai 		return err;
1231109a9638SPeter Gruber 	}
1232a693a26fSTakashi Iwai 
1233a693a26fSTakashi Iwai 	chip->firmware = firmware;
1234a693a26fSTakashi Iwai 
1235a693a26fSTakashi Iwai 	return 1; /* OK */
1236109a9638SPeter Gruber }
1237109a9638SPeter Gruber 
1238a693a26fSTakashi Iwai static int riptide_reset(struct cmdif *cif, struct snd_riptide *chip)
1239a693a26fSTakashi Iwai {
1240a693a26fSTakashi Iwai 	union cmdret rptr = CMDRET_ZERO;
1241a693a26fSTakashi Iwai 	int err, tries;
1242a693a26fSTakashi Iwai 
1243a693a26fSTakashi Iwai 	if (!cif)
1244a693a26fSTakashi Iwai 		return -EINVAL;
1245a693a26fSTakashi Iwai 
1246a693a26fSTakashi Iwai 	cif->cmdcnt = 0;
1247a693a26fSTakashi Iwai 	cif->cmdtime = 0;
1248a693a26fSTakashi Iwai 	cif->cmdtimemax = 0;
1249a693a26fSTakashi Iwai 	cif->cmdtimemin = 0xffffffff;
1250a693a26fSTakashi Iwai 	cif->errcnt = 0;
1251a693a26fSTakashi Iwai 	cif->is_reset = 0;
1252a693a26fSTakashi Iwai 
1253a693a26fSTakashi Iwai 	tries = RESET_TRIES;
1254a693a26fSTakashi Iwai 	do {
1255a693a26fSTakashi Iwai 		err = try_to_load_firmware(cif, chip);
1256a693a26fSTakashi Iwai 		if (err < 0)
1257a693a26fSTakashi Iwai 			return err;
1258a693a26fSTakashi Iwai 	} while (!err && --tries);
1259a693a26fSTakashi Iwai 
1260109a9638SPeter Gruber 	SEND_SACR(cif, 0, AC97_RESET);
1261109a9638SPeter Gruber 	SEND_RACR(cif, AC97_RESET, &rptr);
1262109a9638SPeter Gruber 	snd_printdd("AC97: 0x%x 0x%x\n", rptr.retlongs[0], rptr.retlongs[1]);
1263109a9638SPeter Gruber 
1264109a9638SPeter Gruber 	SEND_PLST(cif, 0);
1265109a9638SPeter Gruber 	SEND_SLST(cif, 0);
1266109a9638SPeter Gruber 	SEND_DLST(cif, 0);
1267109a9638SPeter Gruber 	SEND_ALST(cif, 0);
1268109a9638SPeter Gruber 	SEND_KDMA(cif);
1269109a9638SPeter Gruber 
1270109a9638SPeter Gruber 	writearm(cif, 0x301F8, 1, 1);
1271109a9638SPeter Gruber 	writearm(cif, 0x301F4, 1, 1);
1272109a9638SPeter Gruber 
1273109a9638SPeter Gruber 	SEND_LSEL(cif, MODEM_CMD, 0, 0, MODEM_INTDEC, MODEM_MERGER,
1274109a9638SPeter Gruber 		  MODEM_SPLITTER, MODEM_MIXER);
1275109a9638SPeter Gruber 	setmixer(cif, MODEM_MIXER, 0x7fff, 0x7fff);
1276109a9638SPeter Gruber 	alloclbuspath(cif, ARM2LBUS_FIFO13, lbus_play_modem, NULL, NULL);
1277109a9638SPeter Gruber 
1278109a9638SPeter Gruber 	SEND_LSEL(cif, FM_CMD, 0, 0, FM_INTDEC, FM_MERGER, FM_SPLITTER,
1279109a9638SPeter Gruber 		  FM_MIXER);
1280109a9638SPeter Gruber 	setmixer(cif, FM_MIXER, 0x7fff, 0x7fff);
1281109a9638SPeter Gruber 	writearm(cif, 0x30648 + FM_MIXER * 4, 0x01, 0x00000005);
1282109a9638SPeter Gruber 	writearm(cif, 0x301A8, 0x02, 0x00000002);
1283109a9638SPeter Gruber 	writearm(cif, 0x30264, 0x08, 0xffffffff);
1284109a9638SPeter Gruber 	alloclbuspath(cif, OPL3_SAMPLE, lbus_play_opl3, NULL, NULL);
1285109a9638SPeter Gruber 
1286109a9638SPeter Gruber 	SEND_SSRC(cif, I2S_INTDEC, 48000,
1287109a9638SPeter Gruber 		  ((u32) I2S_RATE * 65536) / 48000,
1288109a9638SPeter Gruber 		  ((u32) I2S_RATE * 65536) % 48000);
1289109a9638SPeter Gruber 	SEND_LSEL(cif, I2S_CMD0, 0, 0, I2S_INTDEC, I2S_MERGER, I2S_SPLITTER,
1290109a9638SPeter Gruber 		  I2S_MIXER);
1291109a9638SPeter Gruber 	SEND_SI2S(cif, 1);
1292109a9638SPeter Gruber 	alloclbuspath(cif, ARM2LBUS_FIFO0, lbus_play_i2s, NULL, NULL);
1293109a9638SPeter Gruber 	alloclbuspath(cif, DIGITAL_MIXER_OUT0, lbus_play_out, NULL, NULL);
1294109a9638SPeter Gruber 	alloclbuspath(cif, DIGITAL_MIXER_OUT0, lbus_play_outhp, NULL, NULL);
1295109a9638SPeter Gruber 
1296109a9638SPeter Gruber 	SET_AIACK(cif->hwport);
1297109a9638SPeter Gruber 	SET_AIE(cif->hwport);
1298109a9638SPeter Gruber 	SET_AIACK(cif->hwport);
1299109a9638SPeter Gruber 	cif->is_reset = 1;
1300109a9638SPeter Gruber 
1301109a9638SPeter Gruber 	return 0;
1302109a9638SPeter Gruber }
1303109a9638SPeter Gruber 
130414ff46fdSBhumika Goyal static const struct snd_pcm_hardware snd_riptide_playback = {
1305109a9638SPeter Gruber 	.info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1306109a9638SPeter Gruber 		 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1307109a9638SPeter Gruber 		 SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_MMAP_VALID),
1308109a9638SPeter Gruber 	.formats =
1309109a9638SPeter Gruber 	    SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S8
1310109a9638SPeter Gruber 	    | SNDRV_PCM_FMTBIT_U16_LE,
1311109a9638SPeter Gruber 	.rates = SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_8000_48000,
1312109a9638SPeter Gruber 	.rate_min = 5500,
1313109a9638SPeter Gruber 	.rate_max = 48000,
1314109a9638SPeter Gruber 	.channels_min = 1,
1315109a9638SPeter Gruber 	.channels_max = 2,
1316109a9638SPeter Gruber 	.buffer_bytes_max = (64 * 1024),
1317109a9638SPeter Gruber 	.period_bytes_min = PAGE_SIZE >> 1,
1318109a9638SPeter Gruber 	.period_bytes_max = PAGE_SIZE << 8,
1319109a9638SPeter Gruber 	.periods_min = 2,
1320109a9638SPeter Gruber 	.periods_max = 64,
1321109a9638SPeter Gruber 	.fifo_size = 0,
1322109a9638SPeter Gruber };
132314ff46fdSBhumika Goyal static const struct snd_pcm_hardware snd_riptide_capture = {
1324109a9638SPeter Gruber 	.info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1325109a9638SPeter Gruber 		 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1326109a9638SPeter Gruber 		 SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_MMAP_VALID),
1327109a9638SPeter Gruber 	.formats =
1328109a9638SPeter Gruber 	    SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S8
1329109a9638SPeter Gruber 	    | SNDRV_PCM_FMTBIT_U16_LE,
1330109a9638SPeter Gruber 	.rates = SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_8000_48000,
1331109a9638SPeter Gruber 	.rate_min = 5500,
1332109a9638SPeter Gruber 	.rate_max = 48000,
1333109a9638SPeter Gruber 	.channels_min = 1,
1334109a9638SPeter Gruber 	.channels_max = 2,
1335109a9638SPeter Gruber 	.buffer_bytes_max = (64 * 1024),
1336109a9638SPeter Gruber 	.period_bytes_min = PAGE_SIZE >> 1,
1337109a9638SPeter Gruber 	.period_bytes_max = PAGE_SIZE << 3,
1338109a9638SPeter Gruber 	.periods_min = 2,
1339109a9638SPeter Gruber 	.periods_max = 64,
1340109a9638SPeter Gruber 	.fifo_size = 0,
1341109a9638SPeter Gruber };
1342109a9638SPeter Gruber 
1343109a9638SPeter Gruber static snd_pcm_uframes_t snd_riptide_pointer(struct snd_pcm_substream
1344109a9638SPeter Gruber 					     *substream)
1345109a9638SPeter Gruber {
1346109a9638SPeter Gruber 	struct snd_riptide *chip = snd_pcm_substream_chip(substream);
1347109a9638SPeter Gruber 	struct snd_pcm_runtime *runtime = substream->runtime;
1348109a9638SPeter Gruber 	struct pcmhw *data = get_pcmhwdev(substream);
1349109a9638SPeter Gruber 	struct cmdif *cif = chip->cif;
1350109a9638SPeter Gruber 	union cmdret rptr = CMDRET_ZERO;
1351109a9638SPeter Gruber 	snd_pcm_uframes_t ret;
1352109a9638SPeter Gruber 
1353109a9638SPeter Gruber 	SEND_GPOS(cif, 0, data->id, &rptr);
1354109a9638SPeter Gruber 	if (data->size && runtime->period_size) {
1355109a9638SPeter Gruber 		snd_printdd
1356109a9638SPeter Gruber 		    ("pointer stream %d position 0x%x(0x%x in buffer) bytes 0x%lx(0x%lx in period) frames\n",
1357109a9638SPeter Gruber 		     data->id, rptr.retlongs[1], rptr.retlongs[1] % data->size,
1358109a9638SPeter Gruber 		     bytes_to_frames(runtime, rptr.retlongs[1]),
1359109a9638SPeter Gruber 		     bytes_to_frames(runtime,
1360109a9638SPeter Gruber 				     rptr.retlongs[1]) % runtime->period_size);
1361109a9638SPeter Gruber 		if (rptr.retlongs[1] > data->pointer)
1362109a9638SPeter Gruber 			ret =
1363109a9638SPeter Gruber 			    bytes_to_frames(runtime,
1364109a9638SPeter Gruber 					    rptr.retlongs[1] % data->size);
1365109a9638SPeter Gruber 		else
1366109a9638SPeter Gruber 			ret =
1367109a9638SPeter Gruber 			    bytes_to_frames(runtime,
1368109a9638SPeter Gruber 					    data->pointer % data->size);
1369109a9638SPeter Gruber 	} else {
1370109a9638SPeter Gruber 		snd_printdd("stream not started or strange parms (%d %ld)\n",
1371109a9638SPeter Gruber 			    data->size, runtime->period_size);
1372109a9638SPeter Gruber 		ret = bytes_to_frames(runtime, 0);
1373109a9638SPeter Gruber 	}
1374109a9638SPeter Gruber 	return ret;
1375109a9638SPeter Gruber }
1376109a9638SPeter Gruber 
1377109a9638SPeter Gruber static int snd_riptide_trigger(struct snd_pcm_substream *substream, int cmd)
1378109a9638SPeter Gruber {
1379109a9638SPeter Gruber 	int i, j;
1380109a9638SPeter Gruber 	struct snd_riptide *chip = snd_pcm_substream_chip(substream);
1381109a9638SPeter Gruber 	struct pcmhw *data = get_pcmhwdev(substream);
1382109a9638SPeter Gruber 	struct cmdif *cif = chip->cif;
1383109a9638SPeter Gruber 	union cmdret rptr = CMDRET_ZERO;
1384109a9638SPeter Gruber 
1385109a9638SPeter Gruber 	spin_lock(&chip->lock);
1386109a9638SPeter Gruber 	switch (cmd) {
1387109a9638SPeter Gruber 	case SNDRV_PCM_TRIGGER_START:
1388109a9638SPeter Gruber 	case SNDRV_PCM_TRIGGER_RESUME:
1389109a9638SPeter Gruber 		if (!(data->state & ST_PLAY)) {
1390109a9638SPeter Gruber 			SEND_SSTR(cif, data->id, data->sgdlist.addr);
1391109a9638SPeter Gruber 			SET_AIE(cif->hwport);
1392109a9638SPeter Gruber 			data->state = ST_PLAY;
1393109a9638SPeter Gruber 			if (data->mixer != 0xff)
1394109a9638SPeter Gruber 				setmixer(cif, data->mixer, 0x7fff, 0x7fff);
1395109a9638SPeter Gruber 			chip->openstreams++;
1396109a9638SPeter Gruber 			data->oldpos = 0;
1397109a9638SPeter Gruber 			data->pointer = 0;
1398109a9638SPeter Gruber 		}
1399109a9638SPeter Gruber 		break;
1400109a9638SPeter Gruber 	case SNDRV_PCM_TRIGGER_STOP:
1401109a9638SPeter Gruber 	case SNDRV_PCM_TRIGGER_SUSPEND:
1402109a9638SPeter Gruber 		if (data->mixer != 0xff)
1403109a9638SPeter Gruber 			setmixer(cif, data->mixer, 0, 0);
1404109a9638SPeter Gruber 		setmixer(cif, data->mixer, 0, 0);
1405109a9638SPeter Gruber 		SEND_KSTR(cif, data->id);
1406109a9638SPeter Gruber 		data->state = ST_STOP;
1407109a9638SPeter Gruber 		chip->openstreams--;
1408109a9638SPeter Gruber 		j = 0;
1409109a9638SPeter Gruber 		do {
1410109a9638SPeter Gruber 			i = rptr.retlongs[1];
1411109a9638SPeter Gruber 			SEND_GPOS(cif, 0, data->id, &rptr);
1412109a9638SPeter Gruber 			udelay(1);
1413109a9638SPeter Gruber 		} while (i != rptr.retlongs[1] && j++ < MAX_WRITE_RETRY);
14141b1cc7f2SRoel Kluin 		if (j > MAX_WRITE_RETRY)
1415109a9638SPeter Gruber 			snd_printk(KERN_ERR "Riptide: Could not stop stream!");
1416109a9638SPeter Gruber 		break;
1417109a9638SPeter Gruber 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1418109a9638SPeter Gruber 		if (!(data->state & ST_PAUSE)) {
1419109a9638SPeter Gruber 			SEND_PSTR(cif, data->id);
1420109a9638SPeter Gruber 			data->state |= ST_PAUSE;
1421109a9638SPeter Gruber 			chip->openstreams--;
1422109a9638SPeter Gruber 		}
1423109a9638SPeter Gruber 		break;
1424109a9638SPeter Gruber 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1425109a9638SPeter Gruber 		if (data->state & ST_PAUSE) {
1426109a9638SPeter Gruber 			SEND_SSTR(cif, data->id, data->sgdlist.addr);
1427109a9638SPeter Gruber 			data->state &= ~ST_PAUSE;
1428109a9638SPeter Gruber 			chip->openstreams++;
1429109a9638SPeter Gruber 		}
1430109a9638SPeter Gruber 		break;
1431109a9638SPeter Gruber 	default:
1432109a9638SPeter Gruber 		spin_unlock(&chip->lock);
1433109a9638SPeter Gruber 		return -EINVAL;
1434109a9638SPeter Gruber 	}
1435109a9638SPeter Gruber 	spin_unlock(&chip->lock);
1436109a9638SPeter Gruber 	return 0;
1437109a9638SPeter Gruber }
1438109a9638SPeter Gruber 
1439109a9638SPeter Gruber static int snd_riptide_prepare(struct snd_pcm_substream *substream)
1440109a9638SPeter Gruber {
1441109a9638SPeter Gruber 	struct snd_riptide *chip = snd_pcm_substream_chip(substream);
1442109a9638SPeter Gruber 	struct snd_pcm_runtime *runtime = substream->runtime;
1443109a9638SPeter Gruber 	struct pcmhw *data = get_pcmhwdev(substream);
1444109a9638SPeter Gruber 	struct cmdif *cif = chip->cif;
1445bf6f3d74STakashi Iwai 	const unsigned char *lbuspath = NULL;
1446109a9638SPeter Gruber 	unsigned int rate, channels;
1447109a9638SPeter Gruber 	int err = 0;
1448109a9638SPeter Gruber 	snd_pcm_format_t format;
1449109a9638SPeter Gruber 
1450da3cec35STakashi Iwai 	if (snd_BUG_ON(!cif || !data))
1451da3cec35STakashi Iwai 		return -EINVAL;
1452109a9638SPeter Gruber 
1453109a9638SPeter Gruber 	snd_printdd("prepare id %d ch: %d f:0x%x r:%d\n", data->id,
1454109a9638SPeter Gruber 		    runtime->channels, runtime->format, runtime->rate);
1455109a9638SPeter Gruber 
1456109a9638SPeter Gruber 	spin_lock_irq(&chip->lock);
1457109a9638SPeter Gruber 	channels = runtime->channels;
1458109a9638SPeter Gruber 	format = runtime->format;
1459109a9638SPeter Gruber 	rate = runtime->rate;
1460109a9638SPeter Gruber 	switch (channels) {
1461109a9638SPeter Gruber 	case 1:
1462109a9638SPeter Gruber 		if (rate == 48000 && format == SNDRV_PCM_FORMAT_S16_LE)
1463109a9638SPeter Gruber 			lbuspath = data->paths.noconv;
1464109a9638SPeter Gruber 		else
1465109a9638SPeter Gruber 			lbuspath = data->paths.mono;
1466109a9638SPeter Gruber 		break;
1467109a9638SPeter Gruber 	case 2:
1468109a9638SPeter Gruber 		if (rate == 48000 && format == SNDRV_PCM_FORMAT_S16_LE)
1469109a9638SPeter Gruber 			lbuspath = data->paths.noconv;
1470109a9638SPeter Gruber 		else
1471109a9638SPeter Gruber 			lbuspath = data->paths.stereo;
1472109a9638SPeter Gruber 		break;
1473109a9638SPeter Gruber 	}
147477a23f26STakashi Iwai 	snd_printdd("use sgdlist at 0x%p\n",
147577a23f26STakashi Iwai 		    data->sgdlist.area);
147677a23f26STakashi Iwai 	if (data->sgdlist.area) {
1477109a9638SPeter Gruber 		unsigned int i, j, size, pages, f, pt, period;
1478109a9638SPeter Gruber 		struct sgd *c, *p = NULL;
1479109a9638SPeter Gruber 
1480109a9638SPeter Gruber 		size = frames_to_bytes(runtime, runtime->buffer_size);
1481109a9638SPeter Gruber 		period = frames_to_bytes(runtime, runtime->period_size);
1482109a9638SPeter Gruber 		f = PAGE_SIZE;
1483109a9638SPeter Gruber 		while ((size + (f >> 1) - 1) <= (f << 7) && (f << 1) > period)
1484109a9638SPeter Gruber 			f = f >> 1;
1485cf7d7edcSAmitoj Kaur Chawla 		pages = DIV_ROUND_UP(size, f);
1486109a9638SPeter Gruber 		data->size = size;
1487109a9638SPeter Gruber 		data->pages = pages;
1488109a9638SPeter Gruber 		snd_printdd
1489109a9638SPeter Gruber 		    ("create sgd size: 0x%x pages %d of size 0x%x for period 0x%x\n",
1490109a9638SPeter Gruber 		     size, pages, f, period);
1491109a9638SPeter Gruber 		pt = 0;
1492109a9638SPeter Gruber 		j = 0;
1493109a9638SPeter Gruber 		for (i = 0; i < pages; i++) {
149477a23f26STakashi Iwai 			unsigned int ofs, addr;
1495109a9638SPeter Gruber 			c = &data->sgdbuf[i];
1496109a9638SPeter Gruber 			if (p)
1497109a9638SPeter Gruber 				p->dwNextLink = cpu_to_le32(data->sgdlist.addr +
1498109a9638SPeter Gruber 							    (i *
1499109a9638SPeter Gruber 							     sizeof(struct
1500109a9638SPeter Gruber 								    sgd)));
1501109a9638SPeter Gruber 			c->dwNextLink = cpu_to_le32(data->sgdlist.addr);
150277a23f26STakashi Iwai 			ofs = j << PAGE_SHIFT;
150377a23f26STakashi Iwai 			addr = snd_pcm_sgbuf_get_addr(substream, ofs) + pt;
150477a23f26STakashi Iwai 			c->dwSegPtrPhys = cpu_to_le32(addr);
1505109a9638SPeter Gruber 			pt = (pt + f) % PAGE_SIZE;
1506109a9638SPeter Gruber 			if (pt == 0)
1507109a9638SPeter Gruber 				j++;
1508109a9638SPeter Gruber 			c->dwSegLen = cpu_to_le32(f);
1509109a9638SPeter Gruber 			c->dwStat_Ctl =
1510109a9638SPeter Gruber 			    cpu_to_le32(IEOB_ENABLE | IEOS_ENABLE |
1511109a9638SPeter Gruber 					IEOC_ENABLE);
1512109a9638SPeter Gruber 			p = c;
1513109a9638SPeter Gruber 			size -= f;
1514109a9638SPeter Gruber 		}
1515109a9638SPeter Gruber 		data->sgdbuf[i].dwSegLen = cpu_to_le32(size);
1516109a9638SPeter Gruber 	}
1517109a9638SPeter Gruber 	if (lbuspath && lbuspath != data->lbuspath) {
1518109a9638SPeter Gruber 		if (data->lbuspath)
1519109a9638SPeter Gruber 			freelbuspath(cif, data->source, data->lbuspath);
1520109a9638SPeter Gruber 		alloclbuspath(cif, data->source, lbuspath,
1521109a9638SPeter Gruber 			      &data->mixer, data->intdec);
1522109a9638SPeter Gruber 		data->lbuspath = lbuspath;
1523109a9638SPeter Gruber 		data->rate = 0;
1524109a9638SPeter Gruber 	}
1525109a9638SPeter Gruber 	if (data->rate != rate || data->format != format ||
1526109a9638SPeter Gruber 	    data->channels != channels) {
1527109a9638SPeter Gruber 		data->rate = rate;
1528109a9638SPeter Gruber 		data->format = format;
1529109a9638SPeter Gruber 		data->channels = channels;
1530109a9638SPeter Gruber 		if (setsampleformat
1531109a9638SPeter Gruber 		    (cif, data->mixer, data->id, channels, format)
1532109a9638SPeter Gruber 		    || setsamplerate(cif, data->intdec, rate))
1533109a9638SPeter Gruber 			err = -EIO;
1534109a9638SPeter Gruber 	}
1535109a9638SPeter Gruber 	spin_unlock_irq(&chip->lock);
1536109a9638SPeter Gruber 	return err;
1537109a9638SPeter Gruber }
1538109a9638SPeter Gruber 
1539109a9638SPeter Gruber static int
1540109a9638SPeter Gruber snd_riptide_hw_params(struct snd_pcm_substream *substream,
1541109a9638SPeter Gruber 		      struct snd_pcm_hw_params *hw_params)
1542109a9638SPeter Gruber {
1543109a9638SPeter Gruber 	struct snd_riptide *chip = snd_pcm_substream_chip(substream);
1544109a9638SPeter Gruber 	struct pcmhw *data = get_pcmhwdev(substream);
1545109a9638SPeter Gruber 	struct snd_dma_buffer *sgdlist = &data->sgdlist;
1546109a9638SPeter Gruber 	int err;
1547109a9638SPeter Gruber 
1548109a9638SPeter Gruber 	snd_printdd("hw params id %d (sgdlist: 0x%p 0x%lx %d)\n", data->id,
1549109a9638SPeter Gruber 		    sgdlist->area, (unsigned long)sgdlist->addr,
1550109a9638SPeter Gruber 		    (int)sgdlist->bytes);
1551109a9638SPeter Gruber 	if (sgdlist->area)
1552109a9638SPeter Gruber 		snd_dma_free_pages(sgdlist);
1553109a9638SPeter Gruber 	if ((err = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV,
15546974f8adSTakashi Iwai 				       &chip->pci->dev,
1555109a9638SPeter Gruber 				       sizeof(struct sgd) * (DESC_MAX_MASK + 1),
1556109a9638SPeter Gruber 				       sgdlist)) < 0) {
1557109a9638SPeter Gruber 		snd_printk(KERN_ERR "Riptide: failed to alloc %d dma bytes\n",
1558109a9638SPeter Gruber 			   (int)sizeof(struct sgd) * (DESC_MAX_MASK + 1));
1559109a9638SPeter Gruber 		return err;
1560109a9638SPeter Gruber 	}
1561109a9638SPeter Gruber 	data->sgdbuf = (struct sgd *)sgdlist->area;
15620dd32369STakashi Iwai 	return 0;
1563109a9638SPeter Gruber }
1564109a9638SPeter Gruber 
1565109a9638SPeter Gruber static int snd_riptide_hw_free(struct snd_pcm_substream *substream)
1566109a9638SPeter Gruber {
1567109a9638SPeter Gruber 	struct snd_riptide *chip = snd_pcm_substream_chip(substream);
1568109a9638SPeter Gruber 	struct pcmhw *data = get_pcmhwdev(substream);
1569109a9638SPeter Gruber 	struct cmdif *cif = chip->cif;
1570109a9638SPeter Gruber 
1571109a9638SPeter Gruber 	if (cif && data) {
1572109a9638SPeter Gruber 		if (data->lbuspath)
1573109a9638SPeter Gruber 			freelbuspath(cif, data->source, data->lbuspath);
1574109a9638SPeter Gruber 		data->lbuspath = NULL;
1575109a9638SPeter Gruber 		data->source = 0xff;
1576109a9638SPeter Gruber 		data->intdec[0] = 0xff;
1577109a9638SPeter Gruber 		data->intdec[1] = 0xff;
1578109a9638SPeter Gruber 
1579109a9638SPeter Gruber 		if (data->sgdlist.area) {
1580109a9638SPeter Gruber 			snd_dma_free_pages(&data->sgdlist);
1581109a9638SPeter Gruber 			data->sgdlist.area = NULL;
1582109a9638SPeter Gruber 		}
1583109a9638SPeter Gruber 	}
15840dd32369STakashi Iwai 	return 0;
1585109a9638SPeter Gruber }
1586109a9638SPeter Gruber 
1587109a9638SPeter Gruber static int snd_riptide_playback_open(struct snd_pcm_substream *substream)
1588109a9638SPeter Gruber {
1589109a9638SPeter Gruber 	struct snd_riptide *chip = snd_pcm_substream_chip(substream);
1590109a9638SPeter Gruber 	struct snd_pcm_runtime *runtime = substream->runtime;
1591109a9638SPeter Gruber 	struct pcmhw *data;
15920cd87b10SHarvey Harrison 	int sub_num = substream->number;
1593109a9638SPeter Gruber 
15940cd87b10SHarvey Harrison 	chip->playback_substream[sub_num] = substream;
1595109a9638SPeter Gruber 	runtime->hw = snd_riptide_playback;
159668bf5700SKulikov Vasiliy 
1597109a9638SPeter Gruber 	data = kzalloc(sizeof(struct pcmhw), GFP_KERNEL);
159868bf5700SKulikov Vasiliy 	if (data == NULL)
159968bf5700SKulikov Vasiliy 		return -ENOMEM;
16000cd87b10SHarvey Harrison 	data->paths = lbus_play_paths[sub_num];
16010cd87b10SHarvey Harrison 	data->id = play_ids[sub_num];
16020cd87b10SHarvey Harrison 	data->source = play_sources[sub_num];
1603109a9638SPeter Gruber 	data->intdec[0] = 0xff;
1604109a9638SPeter Gruber 	data->intdec[1] = 0xff;
1605109a9638SPeter Gruber 	data->state = ST_STOP;
1606109a9638SPeter Gruber 	runtime->private_data = data;
1607109a9638SPeter Gruber 	return snd_pcm_hw_constraint_integer(runtime,
1608109a9638SPeter Gruber 					     SNDRV_PCM_HW_PARAM_PERIODS);
1609109a9638SPeter Gruber }
1610109a9638SPeter Gruber 
1611109a9638SPeter Gruber static int snd_riptide_capture_open(struct snd_pcm_substream *substream)
1612109a9638SPeter Gruber {
1613109a9638SPeter Gruber 	struct snd_riptide *chip = snd_pcm_substream_chip(substream);
1614109a9638SPeter Gruber 	struct snd_pcm_runtime *runtime = substream->runtime;
1615109a9638SPeter Gruber 	struct pcmhw *data;
1616109a9638SPeter Gruber 
1617109a9638SPeter Gruber 	chip->capture_substream = substream;
1618109a9638SPeter Gruber 	runtime->hw = snd_riptide_capture;
161968bf5700SKulikov Vasiliy 
1620109a9638SPeter Gruber 	data = kzalloc(sizeof(struct pcmhw), GFP_KERNEL);
162168bf5700SKulikov Vasiliy 	if (data == NULL)
162268bf5700SKulikov Vasiliy 		return -ENOMEM;
1623109a9638SPeter Gruber 	data->paths = lbus_rec_path;
1624109a9638SPeter Gruber 	data->id = PADC;
1625109a9638SPeter Gruber 	data->source = ACLNK2PADC;
1626109a9638SPeter Gruber 	data->intdec[0] = 0xff;
1627109a9638SPeter Gruber 	data->intdec[1] = 0xff;
1628109a9638SPeter Gruber 	data->state = ST_STOP;
1629109a9638SPeter Gruber 	runtime->private_data = data;
1630109a9638SPeter Gruber 	return snd_pcm_hw_constraint_integer(runtime,
1631109a9638SPeter Gruber 					     SNDRV_PCM_HW_PARAM_PERIODS);
1632109a9638SPeter Gruber }
1633109a9638SPeter Gruber 
1634109a9638SPeter Gruber static int snd_riptide_playback_close(struct snd_pcm_substream *substream)
1635109a9638SPeter Gruber {
1636109a9638SPeter Gruber 	struct snd_riptide *chip = snd_pcm_substream_chip(substream);
1637109a9638SPeter Gruber 	struct pcmhw *data = get_pcmhwdev(substream);
16380cd87b10SHarvey Harrison 	int sub_num = substream->number;
1639109a9638SPeter Gruber 
1640109a9638SPeter Gruber 	substream->runtime->private_data = NULL;
16410cd87b10SHarvey Harrison 	chip->playback_substream[sub_num] = NULL;
1642109a9638SPeter Gruber 	kfree(data);
1643109a9638SPeter Gruber 	return 0;
1644109a9638SPeter Gruber }
1645109a9638SPeter Gruber 
1646109a9638SPeter Gruber static int snd_riptide_capture_close(struct snd_pcm_substream *substream)
1647109a9638SPeter Gruber {
1648109a9638SPeter Gruber 	struct snd_riptide *chip = snd_pcm_substream_chip(substream);
1649109a9638SPeter Gruber 	struct pcmhw *data = get_pcmhwdev(substream);
1650109a9638SPeter Gruber 
1651109a9638SPeter Gruber 	substream->runtime->private_data = NULL;
1652109a9638SPeter Gruber 	chip->capture_substream = NULL;
1653109a9638SPeter Gruber 	kfree(data);
1654109a9638SPeter Gruber 	return 0;
1655109a9638SPeter Gruber }
1656109a9638SPeter Gruber 
16576769e988SJulia Lawall static const struct snd_pcm_ops snd_riptide_playback_ops = {
1658109a9638SPeter Gruber 	.open = snd_riptide_playback_open,
1659109a9638SPeter Gruber 	.close = snd_riptide_playback_close,
1660109a9638SPeter Gruber 	.hw_params = snd_riptide_hw_params,
1661109a9638SPeter Gruber 	.hw_free = snd_riptide_hw_free,
1662109a9638SPeter Gruber 	.prepare = snd_riptide_prepare,
1663109a9638SPeter Gruber 	.trigger = snd_riptide_trigger,
1664109a9638SPeter Gruber 	.pointer = snd_riptide_pointer,
1665109a9638SPeter Gruber };
16666769e988SJulia Lawall static const struct snd_pcm_ops snd_riptide_capture_ops = {
1667109a9638SPeter Gruber 	.open = snd_riptide_capture_open,
1668109a9638SPeter Gruber 	.close = snd_riptide_capture_close,
1669109a9638SPeter Gruber 	.hw_params = snd_riptide_hw_params,
1670109a9638SPeter Gruber 	.hw_free = snd_riptide_hw_free,
1671109a9638SPeter Gruber 	.prepare = snd_riptide_prepare,
1672109a9638SPeter Gruber 	.trigger = snd_riptide_trigger,
1673109a9638SPeter Gruber 	.pointer = snd_riptide_pointer,
1674109a9638SPeter Gruber };
1675109a9638SPeter Gruber 
167615aded4cSLars-Peter Clausen static int snd_riptide_pcm(struct snd_riptide *chip, int device)
1677109a9638SPeter Gruber {
1678109a9638SPeter Gruber 	struct snd_pcm *pcm;
1679109a9638SPeter Gruber 	int err;
1680109a9638SPeter Gruber 
1681109a9638SPeter Gruber 	if ((err =
1682109a9638SPeter Gruber 	     snd_pcm_new(chip->card, "RIPTIDE", device, PLAYBACK_SUBSTREAMS, 1,
1683109a9638SPeter Gruber 			 &pcm)) < 0)
1684109a9638SPeter Gruber 		return err;
1685109a9638SPeter Gruber 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
1686109a9638SPeter Gruber 			&snd_riptide_playback_ops);
1687109a9638SPeter Gruber 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
1688109a9638SPeter Gruber 			&snd_riptide_capture_ops);
1689109a9638SPeter Gruber 	pcm->private_data = chip;
1690109a9638SPeter Gruber 	pcm->info_flags = 0;
1691109a9638SPeter Gruber 	strcpy(pcm->name, "RIPTIDE");
1692109a9638SPeter Gruber 	chip->pcm = pcm;
16930dd32369STakashi Iwai 	snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV_SG,
16940dd32369STakashi Iwai 				       &chip->pci->dev, 64 * 1024, 128 * 1024);
1695109a9638SPeter Gruber 	return 0;
1696109a9638SPeter Gruber }
1697109a9638SPeter Gruber 
1698109a9638SPeter Gruber static irqreturn_t
16997d12e780SDavid Howells snd_riptide_interrupt(int irq, void *dev_id)
1700109a9638SPeter Gruber {
1701109a9638SPeter Gruber 	struct snd_riptide *chip = dev_id;
1702109a9638SPeter Gruber 	struct cmdif *cif = chip->cif;
1703*2ac55dafSTakashi Iwai 	irqreturn_t ret = IRQ_HANDLED;
1704109a9638SPeter Gruber 
1705109a9638SPeter Gruber 	if (cif) {
1706109a9638SPeter Gruber 		chip->received_irqs++;
1707109a9638SPeter Gruber 		if (IS_EOBIRQ(cif->hwport) || IS_EOSIRQ(cif->hwport) ||
1708109a9638SPeter Gruber 		    IS_EOCIRQ(cif->hwport)) {
1709109a9638SPeter Gruber 			chip->handled_irqs++;
1710*2ac55dafSTakashi Iwai 			ret = IRQ_WAKE_THREAD;
1711109a9638SPeter Gruber 		}
1712109a9638SPeter Gruber 		if (chip->rmidi && IS_MPUIRQ(cif->hwport)) {
1713109a9638SPeter Gruber 			chip->handled_irqs++;
1714109a9638SPeter Gruber 			snd_mpu401_uart_interrupt(irq,
17157d12e780SDavid Howells 						  chip->rmidi->private_data);
1716109a9638SPeter Gruber 		}
1717109a9638SPeter Gruber 		SET_AIACK(cif->hwport);
1718109a9638SPeter Gruber 	}
1719*2ac55dafSTakashi Iwai 	return ret;
1720109a9638SPeter Gruber }
1721109a9638SPeter Gruber 
1722109a9638SPeter Gruber static void
1723109a9638SPeter Gruber snd_riptide_codec_write(struct snd_ac97 *ac97, unsigned short reg,
1724109a9638SPeter Gruber 			unsigned short val)
1725109a9638SPeter Gruber {
1726109a9638SPeter Gruber 	struct snd_riptide *chip = ac97->private_data;
1727109a9638SPeter Gruber 	struct cmdif *cif = chip->cif;
1728109a9638SPeter Gruber 	union cmdret rptr = CMDRET_ZERO;
1729109a9638SPeter Gruber 	int i = 0;
1730109a9638SPeter Gruber 
1731da3cec35STakashi Iwai 	if (snd_BUG_ON(!cif))
1732da3cec35STakashi Iwai 		return;
1733109a9638SPeter Gruber 
1734109a9638SPeter Gruber 	snd_printdd("Write AC97 reg 0x%x 0x%x\n", reg, val);
1735109a9638SPeter Gruber 	do {
1736109a9638SPeter Gruber 		SEND_SACR(cif, val, reg);
1737109a9638SPeter Gruber 		SEND_RACR(cif, reg, &rptr);
1738109a9638SPeter Gruber 	} while (rptr.retwords[1] != val && i++ < MAX_WRITE_RETRY);
17391b1cc7f2SRoel Kluin 	if (i > MAX_WRITE_RETRY)
1740109a9638SPeter Gruber 		snd_printdd("Write AC97 reg failed\n");
1741109a9638SPeter Gruber }
1742109a9638SPeter Gruber 
1743109a9638SPeter Gruber static unsigned short snd_riptide_codec_read(struct snd_ac97 *ac97,
1744109a9638SPeter Gruber 					     unsigned short reg)
1745109a9638SPeter Gruber {
1746109a9638SPeter Gruber 	struct snd_riptide *chip = ac97->private_data;
1747109a9638SPeter Gruber 	struct cmdif *cif = chip->cif;
1748109a9638SPeter Gruber 	union cmdret rptr = CMDRET_ZERO;
1749109a9638SPeter Gruber 
1750da3cec35STakashi Iwai 	if (snd_BUG_ON(!cif))
1751da3cec35STakashi Iwai 		return 0;
1752109a9638SPeter Gruber 
1753109a9638SPeter Gruber 	if (SEND_RACR(cif, reg, &rptr) != 0)
1754109a9638SPeter Gruber 		SEND_RACR(cif, reg, &rptr);
1755109a9638SPeter Gruber 	snd_printdd("Read AC97 reg 0x%x got 0x%x\n", reg, rptr.retwords[1]);
1756109a9638SPeter Gruber 	return rptr.retwords[1];
1757109a9638SPeter Gruber }
1758109a9638SPeter Gruber 
1759109a9638SPeter Gruber static int snd_riptide_initialize(struct snd_riptide *chip)
1760109a9638SPeter Gruber {
1761109a9638SPeter Gruber 	struct cmdif *cif;
1762109a9638SPeter Gruber 	unsigned int device_id;
1763109a9638SPeter Gruber 	int err;
1764109a9638SPeter Gruber 
1765da3cec35STakashi Iwai 	if (snd_BUG_ON(!chip))
1766da3cec35STakashi Iwai 		return -EINVAL;
1767109a9638SPeter Gruber 
1768109a9638SPeter Gruber 	cif = chip->cif;
1769109a9638SPeter Gruber 	if (!cif) {
1770109a9638SPeter Gruber 		if ((cif = kzalloc(sizeof(struct cmdif), GFP_KERNEL)) == NULL)
1771109a9638SPeter Gruber 			return -ENOMEM;
1772109a9638SPeter Gruber 		cif->hwport = (struct riptideport *)chip->port;
1773109a9638SPeter Gruber 		spin_lock_init(&cif->lock);
1774109a9638SPeter Gruber 		chip->cif = cif;
1775109a9638SPeter Gruber 	}
1776109a9638SPeter Gruber 	cif->is_reset = 0;
1777109a9638SPeter Gruber 	if ((err = riptide_reset(cif, chip)) != 0)
1778109a9638SPeter Gruber 		return err;
1779109a9638SPeter Gruber 	device_id = chip->device_id;
1780109a9638SPeter Gruber 	switch (device_id) {
1781109a9638SPeter Gruber 	case 0x4310:
1782109a9638SPeter Gruber 	case 0x4320:
1783109a9638SPeter Gruber 	case 0x4330:
1784109a9638SPeter Gruber 		snd_printdd("Modem enable?\n");
1785109a9638SPeter Gruber 		SEND_SETDPLL(cif);
1786109a9638SPeter Gruber 		break;
1787109a9638SPeter Gruber 	}
1788109a9638SPeter Gruber 	snd_printdd("Enabling MPU IRQs\n");
1789109a9638SPeter Gruber 	if (chip->rmidi)
1790109a9638SPeter Gruber 		SET_EMPUIRQ(cif->hwport);
1791109a9638SPeter Gruber 	return err;
1792109a9638SPeter Gruber }
1793109a9638SPeter Gruber 
1794109a9638SPeter Gruber static int snd_riptide_free(struct snd_riptide *chip)
1795109a9638SPeter Gruber {
1796109a9638SPeter Gruber 	struct cmdif *cif;
1797109a9638SPeter Gruber 
1798da3cec35STakashi Iwai 	if (!chip)
1799da3cec35STakashi Iwai 		return 0;
1800109a9638SPeter Gruber 
1801109a9638SPeter Gruber 	if ((cif = chip->cif)) {
1802109a9638SPeter Gruber 		SET_GRESET(cif->hwport);
1803109a9638SPeter Gruber 		udelay(100);
1804109a9638SPeter Gruber 		UNSET_GRESET(cif->hwport);
1805109a9638SPeter Gruber 		kfree(chip->cif);
1806109a9638SPeter Gruber 	}
18070a50d2b2STakashi Iwai 	if (chip->irq >= 0)
18080a50d2b2STakashi Iwai 		free_irq(chip->irq, chip);
1809109a9638SPeter Gruber 	release_firmware(chip->fw_entry);
181043560116STakashi Iwai 	release_and_free_resource(chip->res_port);
1811109a9638SPeter Gruber 	kfree(chip);
1812109a9638SPeter Gruber 	return 0;
1813109a9638SPeter Gruber }
1814109a9638SPeter Gruber 
1815109a9638SPeter Gruber static int snd_riptide_dev_free(struct snd_device *device)
1816109a9638SPeter Gruber {
1817109a9638SPeter Gruber 	struct snd_riptide *chip = device->device_data;
1818109a9638SPeter Gruber 
1819109a9638SPeter Gruber 	return snd_riptide_free(chip);
1820109a9638SPeter Gruber }
1821109a9638SPeter Gruber 
1822e23e7a14SBill Pemberton static int
1823109a9638SPeter Gruber snd_riptide_create(struct snd_card *card, struct pci_dev *pci,
1824109a9638SPeter Gruber 		   struct snd_riptide **rchip)
1825109a9638SPeter Gruber {
1826109a9638SPeter Gruber 	struct snd_riptide *chip;
1827109a9638SPeter Gruber 	struct riptideport *hwport;
1828109a9638SPeter Gruber 	int err;
1829efb0ad25STakashi Iwai 	static const struct snd_device_ops ops = {
1830109a9638SPeter Gruber 		.dev_free = snd_riptide_dev_free,
1831109a9638SPeter Gruber 	};
1832109a9638SPeter Gruber 
1833109a9638SPeter Gruber 	*rchip = NULL;
1834109a9638SPeter Gruber 	if ((err = pci_enable_device(pci)) < 0)
1835109a9638SPeter Gruber 		return err;
1836109a9638SPeter Gruber 	if (!(chip = kzalloc(sizeof(struct snd_riptide), GFP_KERNEL)))
1837109a9638SPeter Gruber 		return -ENOMEM;
1838109a9638SPeter Gruber 
1839109a9638SPeter Gruber 	spin_lock_init(&chip->lock);
1840109a9638SPeter Gruber 	chip->card = card;
1841109a9638SPeter Gruber 	chip->pci = pci;
1842109a9638SPeter Gruber 	chip->irq = -1;
1843109a9638SPeter Gruber 	chip->openstreams = 0;
1844109a9638SPeter Gruber 	chip->port = pci_resource_start(pci, 0);
1845109a9638SPeter Gruber 	chip->received_irqs = 0;
1846109a9638SPeter Gruber 	chip->handled_irqs = 0;
1847109a9638SPeter Gruber 	chip->cif = NULL;
1848109a9638SPeter Gruber 
1849109a9638SPeter Gruber 	if ((chip->res_port =
1850109a9638SPeter Gruber 	     request_region(chip->port, 64, "RIPTIDE")) == NULL) {
1851109a9638SPeter Gruber 		snd_printk(KERN_ERR
1852109a9638SPeter Gruber 			   "Riptide: unable to grab region 0x%lx-0x%lx\n",
1853109a9638SPeter Gruber 			   chip->port, chip->port + 64 - 1);
1854109a9638SPeter Gruber 		snd_riptide_free(chip);
1855109a9638SPeter Gruber 		return -EBUSY;
1856109a9638SPeter Gruber 	}
1857109a9638SPeter Gruber 	hwport = (struct riptideport *)chip->port;
1858109a9638SPeter Gruber 	UNSET_AIE(hwport);
1859109a9638SPeter Gruber 
1860*2ac55dafSTakashi Iwai 	if (request_threaded_irq(pci->irq, snd_riptide_interrupt,
1861*2ac55dafSTakashi Iwai 				 riptide_handleirq, IRQF_SHARED,
1862934c2b6dSTakashi Iwai 				 KBUILD_MODNAME, chip)) {
1863109a9638SPeter Gruber 		snd_printk(KERN_ERR "Riptide: unable to grab IRQ %d\n",
1864109a9638SPeter Gruber 			   pci->irq);
1865109a9638SPeter Gruber 		snd_riptide_free(chip);
1866109a9638SPeter Gruber 		return -EBUSY;
1867109a9638SPeter Gruber 	}
1868109a9638SPeter Gruber 	chip->irq = pci->irq;
18693992f78fSTakashi Iwai 	card->sync_irq = chip->irq;
1870109a9638SPeter Gruber 	chip->device_id = pci->device;
1871109a9638SPeter Gruber 	pci_set_master(pci);
1872109a9638SPeter Gruber 	if ((err = snd_riptide_initialize(chip)) < 0) {
1873109a9638SPeter Gruber 		snd_riptide_free(chip);
1874109a9638SPeter Gruber 		return err;
1875109a9638SPeter Gruber 	}
1876109a9638SPeter Gruber 
1877109a9638SPeter Gruber 	if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
1878109a9638SPeter Gruber 		snd_riptide_free(chip);
1879109a9638SPeter Gruber 		return err;
1880109a9638SPeter Gruber 	}
1881109a9638SPeter Gruber 
1882109a9638SPeter Gruber 	*rchip = chip;
1883109a9638SPeter Gruber 	return 0;
1884109a9638SPeter Gruber }
1885109a9638SPeter Gruber 
1886109a9638SPeter Gruber static void
1887109a9638SPeter Gruber snd_riptide_proc_read(struct snd_info_entry *entry,
1888109a9638SPeter Gruber 		      struct snd_info_buffer *buffer)
1889109a9638SPeter Gruber {
1890109a9638SPeter Gruber 	struct snd_riptide *chip = entry->private_data;
1891109a9638SPeter Gruber 	struct pcmhw *data;
1892109a9638SPeter Gruber 	int i;
1893109a9638SPeter Gruber 	struct cmdif *cif = NULL;
1894109a9638SPeter Gruber 	unsigned char p[256];
1895109a9638SPeter Gruber 	unsigned short rval = 0, lval = 0;
1896109a9638SPeter Gruber 	unsigned int rate;
1897109a9638SPeter Gruber 
1898109a9638SPeter Gruber 	if (!chip)
1899109a9638SPeter Gruber 		return;
1900109a9638SPeter Gruber 
1901109a9638SPeter Gruber 	snd_iprintf(buffer, "%s\n\n", chip->card->longname);
1902109a9638SPeter Gruber 	snd_iprintf(buffer, "Device ID: 0x%x\nReceived IRQs: (%ld)%ld\nPorts:",
1903109a9638SPeter Gruber 		    chip->device_id, chip->handled_irqs, chip->received_irqs);
1904109a9638SPeter Gruber 	for (i = 0; i < 64; i += 4)
1905109a9638SPeter Gruber 		snd_iprintf(buffer, "%c%02x: %08x",
1906109a9638SPeter Gruber 			    (i % 16) ? ' ' : '\n', i, inl(chip->port + i));
1907109a9638SPeter Gruber 	if ((cif = chip->cif)) {
1908109a9638SPeter Gruber 		snd_iprintf(buffer,
1909109a9638SPeter Gruber 			    "\nVersion: ASIC: %d CODEC: %d AUXDSP: %d PROG: %d",
1910109a9638SPeter Gruber 			    chip->firmware.firmware.ASIC,
1911109a9638SPeter Gruber 			    chip->firmware.firmware.CODEC,
1912109a9638SPeter Gruber 			    chip->firmware.firmware.AUXDSP,
1913109a9638SPeter Gruber 			    chip->firmware.firmware.PROG);
1914109a9638SPeter Gruber 		snd_iprintf(buffer, "\nDigital mixer:");
1915109a9638SPeter Gruber 		for (i = 0; i < 12; i++) {
1916109a9638SPeter Gruber 			getmixer(cif, i, &rval, &lval);
1917109a9638SPeter Gruber 			snd_iprintf(buffer, "\n %d: %d %d", i, rval, lval);
1918109a9638SPeter Gruber 		}
1919109a9638SPeter Gruber 		snd_iprintf(buffer,
1920109a9638SPeter Gruber 			    "\nARM Commands num: %d failed: %d time: %d max: %d min: %d",
1921109a9638SPeter Gruber 			    cif->cmdcnt, cif->errcnt,
1922109a9638SPeter Gruber 			    cif->cmdtime, cif->cmdtimemax, cif->cmdtimemin);
1923109a9638SPeter Gruber 	}
1924109a9638SPeter Gruber 	snd_iprintf(buffer, "\nOpen streams %d:\n", chip->openstreams);
1925109a9638SPeter Gruber 	for (i = 0; i < PLAYBACK_SUBSTREAMS; i++) {
1926109a9638SPeter Gruber 		if (chip->playback_substream[i]
1927109a9638SPeter Gruber 		    && chip->playback_substream[i]->runtime
1928109a9638SPeter Gruber 		    && (data =
1929109a9638SPeter Gruber 			chip->playback_substream[i]->runtime->private_data)) {
1930109a9638SPeter Gruber 			snd_iprintf(buffer,
1931109a9638SPeter Gruber 				    "stream: %d mixer: %d source: %d (%d,%d)\n",
1932109a9638SPeter Gruber 				    data->id, data->mixer, data->source,
1933109a9638SPeter Gruber 				    data->intdec[0], data->intdec[1]);
1934109a9638SPeter Gruber 			if (!(getsamplerate(cif, data->intdec, &rate)))
1935109a9638SPeter Gruber 				snd_iprintf(buffer, "rate: %d\n", rate);
1936109a9638SPeter Gruber 		}
1937109a9638SPeter Gruber 	}
1938109a9638SPeter Gruber 	if (chip->capture_substream
1939109a9638SPeter Gruber 	    && chip->capture_substream->runtime
1940109a9638SPeter Gruber 	    && (data = chip->capture_substream->runtime->private_data)) {
1941109a9638SPeter Gruber 		snd_iprintf(buffer,
1942109a9638SPeter Gruber 			    "stream: %d mixer: %d source: %d (%d,%d)\n",
1943109a9638SPeter Gruber 			    data->id, data->mixer,
1944109a9638SPeter Gruber 			    data->source, data->intdec[0], data->intdec[1]);
1945109a9638SPeter Gruber 		if (!(getsamplerate(cif, data->intdec, &rate)))
1946109a9638SPeter Gruber 			snd_iprintf(buffer, "rate: %d\n", rate);
1947109a9638SPeter Gruber 	}
1948109a9638SPeter Gruber 	snd_iprintf(buffer, "Paths:\n");
1949109a9638SPeter Gruber 	i = getpaths(cif, p);
1950282572b5SDan Carpenter 	while (i >= 2) {
1951282572b5SDan Carpenter 		i -= 2;
1952282572b5SDan Carpenter 		snd_iprintf(buffer, "%x->%x ", p[i], p[i + 1]);
1953109a9638SPeter Gruber 	}
1954109a9638SPeter Gruber 	snd_iprintf(buffer, "\n");
1955109a9638SPeter Gruber }
1956109a9638SPeter Gruber 
1957e23e7a14SBill Pemberton static void snd_riptide_proc_init(struct snd_riptide *chip)
1958109a9638SPeter Gruber {
195947f2769bSTakashi Iwai 	snd_card_ro_proc_new(chip->card, "riptide", chip,
196047f2769bSTakashi Iwai 			     snd_riptide_proc_read);
1961109a9638SPeter Gruber }
1962109a9638SPeter Gruber 
1963e23e7a14SBill Pemberton static int snd_riptide_mixer(struct snd_riptide *chip)
1964109a9638SPeter Gruber {
1965109a9638SPeter Gruber 	struct snd_ac97_bus *pbus;
1966109a9638SPeter Gruber 	struct snd_ac97_template ac97;
1967109a9638SPeter Gruber 	int err = 0;
196851055da5STakashi Iwai 	static const struct snd_ac97_bus_ops ops = {
1969109a9638SPeter Gruber 		.write = snd_riptide_codec_write,
1970109a9638SPeter Gruber 		.read = snd_riptide_codec_read,
1971109a9638SPeter Gruber 	};
1972109a9638SPeter Gruber 
1973109a9638SPeter Gruber 	memset(&ac97, 0, sizeof(ac97));
1974109a9638SPeter Gruber 	ac97.private_data = chip;
1975109a9638SPeter Gruber 	ac97.scaps = AC97_SCAP_SKIP_MODEM;
1976109a9638SPeter Gruber 
1977109a9638SPeter Gruber 	if ((err = snd_ac97_bus(chip->card, 0, &ops, chip, &pbus)) < 0)
1978109a9638SPeter Gruber 		return err;
1979109a9638SPeter Gruber 
1980109a9638SPeter Gruber 	chip->ac97_bus = pbus;
1981109a9638SPeter Gruber 	ac97.pci = chip->pci;
1982109a9638SPeter Gruber 	if ((err = snd_ac97_mixer(pbus, &ac97, &chip->ac97)) < 0)
1983109a9638SPeter Gruber 		return err;
1984109a9638SPeter Gruber 	return err;
1985109a9638SPeter Gruber }
1986109a9638SPeter Gruber 
1987109a9638SPeter Gruber #ifdef SUPPORT_JOYSTICK
1988109a9638SPeter Gruber 
1989e23e7a14SBill Pemberton static int
1990109a9638SPeter Gruber snd_riptide_joystick_probe(struct pci_dev *pci, const struct pci_device_id *id)
1991109a9638SPeter Gruber {
1992109a9638SPeter Gruber 	static int dev;
1993db1005ecSTakashi Iwai 	struct gameport *gameport;
1994e4940626SDan Carpenter 	int ret;
1995109a9638SPeter Gruber 
1996109a9638SPeter Gruber 	if (dev >= SNDRV_CARDS)
1997109a9638SPeter Gruber 		return -ENODEV;
1998e4940626SDan Carpenter 
1999109a9638SPeter Gruber 	if (!enable[dev]) {
2000e4940626SDan Carpenter 		ret = -ENOENT;
2001e4940626SDan Carpenter 		goto inc_dev;
2002109a9638SPeter Gruber 	}
2003109a9638SPeter Gruber 
2004e4940626SDan Carpenter 	if (!joystick_port[dev]) {
2005e4940626SDan Carpenter 		ret = 0;
2006e4940626SDan Carpenter 		goto inc_dev;
2007e4940626SDan Carpenter 	}
2008db1005ecSTakashi Iwai 
2009db1005ecSTakashi Iwai 	gameport = gameport_allocate_port();
2010e4940626SDan Carpenter 	if (!gameport) {
2011e4940626SDan Carpenter 		ret = -ENOMEM;
2012e4940626SDan Carpenter 		goto inc_dev;
2013e4940626SDan Carpenter 	}
2014db1005ecSTakashi Iwai 	if (!request_region(joystick_port[dev], 8, "Riptide gameport")) {
2015109a9638SPeter Gruber 		snd_printk(KERN_WARNING
2016109a9638SPeter Gruber 			   "Riptide: cannot grab gameport 0x%x\n",
2017109a9638SPeter Gruber 			   joystick_port[dev]);
2018db1005ecSTakashi Iwai 		gameport_free_port(gameport);
2019e4940626SDan Carpenter 		ret = -EBUSY;
2020e4940626SDan Carpenter 		goto inc_dev;
2021109a9638SPeter Gruber 	}
2022db1005ecSTakashi Iwai 
2023db1005ecSTakashi Iwai 	gameport->io = joystick_port[dev];
2024db1005ecSTakashi Iwai 	gameport_register_port(gameport);
2025db1005ecSTakashi Iwai 	pci_set_drvdata(pci, gameport);
2026e4940626SDan Carpenter 
2027e4940626SDan Carpenter 	ret = 0;
2028e4940626SDan Carpenter inc_dev:
2029e4940626SDan Carpenter 	dev++;
2030e4940626SDan Carpenter 	return ret;
2031109a9638SPeter Gruber }
2032109a9638SPeter Gruber 
2033e23e7a14SBill Pemberton static void snd_riptide_joystick_remove(struct pci_dev *pci)
2034109a9638SPeter Gruber {
2035db1005ecSTakashi Iwai 	struct gameport *gameport = pci_get_drvdata(pci);
2036db1005ecSTakashi Iwai 	if (gameport) {
2037db1005ecSTakashi Iwai 		release_region(gameport->io, 8);
2038db1005ecSTakashi Iwai 		gameport_unregister_port(gameport);
2039109a9638SPeter Gruber 	}
2040109a9638SPeter Gruber }
2041109a9638SPeter Gruber #endif
2042109a9638SPeter Gruber 
2043e23e7a14SBill Pemberton static int
2044109a9638SPeter Gruber snd_card_riptide_probe(struct pci_dev *pci, const struct pci_device_id *pci_id)
2045109a9638SPeter Gruber {
2046109a9638SPeter Gruber 	static int dev;
2047109a9638SPeter Gruber 	struct snd_card *card;
2048109a9638SPeter Gruber 	struct snd_riptide *chip;
2049a693a26fSTakashi Iwai 	unsigned short val;
2050a693a26fSTakashi Iwai 	int err;
2051109a9638SPeter Gruber 
2052109a9638SPeter Gruber 	if (dev >= SNDRV_CARDS)
2053109a9638SPeter Gruber 		return -ENODEV;
2054109a9638SPeter Gruber 	if (!enable[dev]) {
2055109a9638SPeter Gruber 		dev++;
2056109a9638SPeter Gruber 		return -ENOENT;
2057109a9638SPeter Gruber 	}
2058109a9638SPeter Gruber 
205960c5772bSTakashi Iwai 	err = snd_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE,
206060c5772bSTakashi Iwai 			   0, &card);
2061e58de7baSTakashi Iwai 	if (err < 0)
2062e58de7baSTakashi Iwai 		return err;
2063a693a26fSTakashi Iwai 	err = snd_riptide_create(card, pci, &chip);
2064a693a26fSTakashi Iwai 	if (err < 0)
2065a693a26fSTakashi Iwai 		goto error;
2066109a9638SPeter Gruber 	card->private_data = chip;
206715aded4cSLars-Peter Clausen 	err = snd_riptide_pcm(chip, 0);
2068a693a26fSTakashi Iwai 	if (err < 0)
2069a693a26fSTakashi Iwai 		goto error;
2070a693a26fSTakashi Iwai 	err = snd_riptide_mixer(chip);
2071a693a26fSTakashi Iwai 	if (err < 0)
2072a693a26fSTakashi Iwai 		goto error;
2073a693a26fSTakashi Iwai 
2074a693a26fSTakashi Iwai 	val = LEGACY_ENABLE_ALL;
2075a693a26fSTakashi Iwai 	if (opl3_port[dev])
2076a693a26fSTakashi Iwai 		val |= LEGACY_ENABLE_FM;
2077109a9638SPeter Gruber #ifdef SUPPORT_JOYSTICK
2078a693a26fSTakashi Iwai 	if (joystick_port[dev])
2079a693a26fSTakashi Iwai 		val |= LEGACY_ENABLE_GAMEPORT;
2080109a9638SPeter Gruber #endif
2081a693a26fSTakashi Iwai 	if (mpu_port[dev])
2082a693a26fSTakashi Iwai 		val |= LEGACY_ENABLE_MPU_INT | LEGACY_ENABLE_MPU;
2083a693a26fSTakashi Iwai 	val |= (chip->irq << 4) & 0xf0;
2084a693a26fSTakashi Iwai 	pci_write_config_word(chip->pci, PCI_EXT_Legacy_Mask, val);
2085a693a26fSTakashi Iwai 	if (mpu_port[dev]) {
2086a693a26fSTakashi Iwai 		val = mpu_port[dev];
2087a693a26fSTakashi Iwai 		pci_write_config_word(chip->pci, PCI_EXT_MPU_Base, val);
2088a693a26fSTakashi Iwai 		err = snd_mpu401_uart_new(card, 0, MPU401_HW_RIPTIDE,
2089dba8b469SClemens Ladisch 					  val, MPU401_INFO_IRQ_HOOK, -1,
2090a693a26fSTakashi Iwai 					  &chip->rmidi);
2091a693a26fSTakashi Iwai 		if (err < 0)
2092109a9638SPeter Gruber 			snd_printk(KERN_WARNING
2093109a9638SPeter Gruber 				   "Riptide: Can't Allocate MPU at 0x%x\n",
2094a693a26fSTakashi Iwai 				   val);
2095109a9638SPeter Gruber 		else
2096a693a26fSTakashi Iwai 			chip->mpuaddr = val;
2097109a9638SPeter Gruber 	}
2098a693a26fSTakashi Iwai 	if (opl3_port[dev]) {
2099a693a26fSTakashi Iwai 		val = opl3_port[dev];
2100a693a26fSTakashi Iwai 		pci_write_config_word(chip->pci, PCI_EXT_FM_Base, val);
2101a693a26fSTakashi Iwai 		err = snd_opl3_create(card, val, val + 2,
2102a693a26fSTakashi Iwai 				      OPL3_HW_RIPTIDE, 0, &chip->opl3);
2103a693a26fSTakashi Iwai 		if (err < 0)
2104109a9638SPeter Gruber 			snd_printk(KERN_WARNING
2105109a9638SPeter Gruber 				   "Riptide: Can't Allocate OPL3 at 0x%x\n",
2106a693a26fSTakashi Iwai 				   val);
2107109a9638SPeter Gruber 		else {
2108a693a26fSTakashi Iwai 			chip->opladdr = val;
2109a693a26fSTakashi Iwai 			err = snd_opl3_hwdep_new(chip->opl3, 0, 1, NULL);
2110a693a26fSTakashi Iwai 			if (err < 0)
2111109a9638SPeter Gruber 				snd_printk(KERN_WARNING
2112109a9638SPeter Gruber 					   "Riptide: Can't Allocate OPL3-HWDEP\n");
2113109a9638SPeter Gruber 		}
2114109a9638SPeter Gruber 	}
2115109a9638SPeter Gruber #ifdef SUPPORT_JOYSTICK
2116a693a26fSTakashi Iwai 	if (joystick_port[dev]) {
2117a693a26fSTakashi Iwai 		val = joystick_port[dev];
2118a693a26fSTakashi Iwai 		pci_write_config_word(chip->pci, PCI_EXT_Game_Base, val);
2119a693a26fSTakashi Iwai 		chip->gameaddr = val;
2120109a9638SPeter Gruber 	}
2121109a9638SPeter Gruber #endif
2122109a9638SPeter Gruber 
2123109a9638SPeter Gruber 	strcpy(card->driver, "RIPTIDE");
2124109a9638SPeter Gruber 	strcpy(card->shortname, "Riptide");
2125109a9638SPeter Gruber #ifdef SUPPORT_JOYSTICK
2126109a9638SPeter Gruber 	snprintf(card->longname, sizeof(card->longname),
2127109a9638SPeter Gruber 		 "%s at 0x%lx, irq %i mpu 0x%x opl3 0x%x gameport 0x%x",
2128109a9638SPeter Gruber 		 card->shortname, chip->port, chip->irq, chip->mpuaddr,
2129109a9638SPeter Gruber 		 chip->opladdr, chip->gameaddr);
2130109a9638SPeter Gruber #else
2131109a9638SPeter Gruber 	snprintf(card->longname, sizeof(card->longname),
2132109a9638SPeter Gruber 		 "%s at 0x%lx, irq %i mpu 0x%x opl3 0x%x",
2133109a9638SPeter Gruber 		 card->shortname, chip->port, chip->irq, chip->mpuaddr,
2134109a9638SPeter Gruber 		 chip->opladdr);
2135109a9638SPeter Gruber #endif
2136109a9638SPeter Gruber 	snd_riptide_proc_init(chip);
2137a693a26fSTakashi Iwai 	err = snd_card_register(card);
2138a693a26fSTakashi Iwai 	if (err < 0)
2139a693a26fSTakashi Iwai 		goto error;
2140109a9638SPeter Gruber 	pci_set_drvdata(pci, card);
2141109a9638SPeter Gruber 	dev++;
2142109a9638SPeter Gruber 	return 0;
2143a693a26fSTakashi Iwai 
2144a693a26fSTakashi Iwai  error:
2145a693a26fSTakashi Iwai 	snd_card_free(card);
2146a693a26fSTakashi Iwai 	return err;
2147109a9638SPeter Gruber }
2148109a9638SPeter Gruber 
2149e23e7a14SBill Pemberton static void snd_card_riptide_remove(struct pci_dev *pci)
2150109a9638SPeter Gruber {
2151109a9638SPeter Gruber 	snd_card_free(pci_get_drvdata(pci));
2152109a9638SPeter Gruber }
2153109a9638SPeter Gruber 
2154109a9638SPeter Gruber static struct pci_driver driver = {
21553733e424STakashi Iwai 	.name = KBUILD_MODNAME,
2156109a9638SPeter Gruber 	.id_table = snd_riptide_ids,
2157109a9638SPeter Gruber 	.probe = snd_card_riptide_probe,
2158e23e7a14SBill Pemberton 	.remove = snd_card_riptide_remove,
215968cb2b55STakashi Iwai 	.driver = {
216068cb2b55STakashi Iwai 		.pm = RIPTIDE_PM_OPS,
216168cb2b55STakashi Iwai 	},
2162109a9638SPeter Gruber };
2163109a9638SPeter Gruber 
2164109a9638SPeter Gruber #ifdef SUPPORT_JOYSTICK
2165109a9638SPeter Gruber static struct pci_driver joystick_driver = {
21663733e424STakashi Iwai 	.name = KBUILD_MODNAME "-joystick",
2167109a9638SPeter Gruber 	.id_table = snd_riptide_joystick_ids,
2168109a9638SPeter Gruber 	.probe = snd_riptide_joystick_probe,
2169e23e7a14SBill Pemberton 	.remove = snd_riptide_joystick_remove,
2170109a9638SPeter Gruber };
2171109a9638SPeter Gruber #endif
2172109a9638SPeter Gruber 
2173109a9638SPeter Gruber static int __init alsa_card_riptide_init(void)
2174109a9638SPeter Gruber {
2175109a9638SPeter Gruber 	int err;
2176db1005ecSTakashi Iwai 	err = pci_register_driver(&driver);
2177db1005ecSTakashi Iwai 	if (err < 0)
2178109a9638SPeter Gruber 		return err;
2179109a9638SPeter Gruber #if defined(SUPPORT_JOYSTICK)
2180cb65c873SJaswinder Singh Rajput 	err = pci_register_driver(&joystick_driver);
2181cb65c873SJaswinder Singh Rajput 	/* On failure unregister formerly registered audio driver */
2182cb65c873SJaswinder Singh Rajput 	if (err < 0)
2183cb65c873SJaswinder Singh Rajput 		pci_unregister_driver(&driver);
2184109a9638SPeter Gruber #endif
2185cb65c873SJaswinder Singh Rajput 	return err;
2186109a9638SPeter Gruber }
2187109a9638SPeter Gruber 
2188109a9638SPeter Gruber static void __exit alsa_card_riptide_exit(void)
2189109a9638SPeter Gruber {
2190109a9638SPeter Gruber 	pci_unregister_driver(&driver);
2191109a9638SPeter Gruber #if defined(SUPPORT_JOYSTICK)
2192109a9638SPeter Gruber 	pci_unregister_driver(&joystick_driver);
2193109a9638SPeter Gruber #endif
2194109a9638SPeter Gruber }
2195109a9638SPeter Gruber 
2196109a9638SPeter Gruber module_init(alsa_card_riptide_init);
2197109a9638SPeter Gruber module_exit(alsa_card_riptide_exit);
2198