xref: /openbmc/linux/sound/pci/riptide/riptide.c (revision 2af5acbaa74c91266457fa1494685729d6f9e540)
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");
1067e0af29dSClemens Ladisch MODULE_FIRMWARE("riptide.hex");
107109a9638SPeter Gruber 
108109a9638SPeter Gruber static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
109109a9638SPeter Gruber static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
110a67ff6a5SRusty Russell static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE;
111109a9638SPeter Gruber 
112109a9638SPeter Gruber #ifdef SUPPORT_JOYSTICK
113109a9638SPeter Gruber static int joystick_port[SNDRV_CARDS] = { [0 ... (SNDRV_CARDS - 1)] = 0x200 };
114109a9638SPeter Gruber #endif
115109a9638SPeter Gruber static int mpu_port[SNDRV_CARDS] = { [0 ... (SNDRV_CARDS - 1)] = 0x330 };
116109a9638SPeter Gruber static int opl3_port[SNDRV_CARDS] = { [0 ... (SNDRV_CARDS - 1)] = 0x388 };
117109a9638SPeter Gruber 
118109a9638SPeter Gruber module_param_array(index, int, NULL, 0444);
119109a9638SPeter Gruber MODULE_PARM_DESC(index, "Index value for Riptide soundcard.");
120109a9638SPeter Gruber module_param_array(id, charp, NULL, 0444);
121109a9638SPeter Gruber MODULE_PARM_DESC(id, "ID string for Riptide soundcard.");
122109a9638SPeter Gruber module_param_array(enable, bool, NULL, 0444);
123109a9638SPeter Gruber MODULE_PARM_DESC(enable, "Enable Riptide soundcard.");
124109a9638SPeter Gruber #ifdef SUPPORT_JOYSTICK
1256192c41fSDavid Howells module_param_hw_array(joystick_port, int, ioport, NULL, 0444);
126109a9638SPeter Gruber MODULE_PARM_DESC(joystick_port, "Joystick port # for Riptide soundcard.");
127109a9638SPeter Gruber #endif
1286192c41fSDavid Howells module_param_hw_array(mpu_port, int, ioport, NULL, 0444);
129109a9638SPeter Gruber MODULE_PARM_DESC(mpu_port, "MPU401 port # for Riptide driver.");
1306192c41fSDavid Howells module_param_hw_array(opl3_port, int, ioport, NULL, 0444);
131109a9638SPeter Gruber MODULE_PARM_DESC(opl3_port, "OPL3 port # for Riptide driver.");
132109a9638SPeter Gruber 
133109a9638SPeter Gruber /*
134109a9638SPeter Gruber  */
135109a9638SPeter Gruber 
136109a9638SPeter Gruber #define MPU401_HW_RIPTIDE MPU401_HW_MPU401
137109a9638SPeter Gruber #define OPL3_HW_RIPTIDE   OPL3_HW_OPL3
138109a9638SPeter Gruber 
139109a9638SPeter Gruber #define PCI_EXT_CapId       0x40
140109a9638SPeter Gruber #define PCI_EXT_NextCapPrt  0x41
141109a9638SPeter Gruber #define PCI_EXT_PWMC        0x42
142109a9638SPeter Gruber #define PCI_EXT_PWSCR       0x44
143109a9638SPeter Gruber #define PCI_EXT_Data00      0x46
144109a9638SPeter Gruber #define PCI_EXT_PMSCR_BSE   0x47
145109a9638SPeter Gruber #define PCI_EXT_SB_Base     0x48
146109a9638SPeter Gruber #define PCI_EXT_FM_Base     0x4a
147109a9638SPeter Gruber #define PCI_EXT_MPU_Base    0x4C
148109a9638SPeter Gruber #define PCI_EXT_Game_Base   0x4E
149109a9638SPeter Gruber #define PCI_EXT_Legacy_Mask 0x50
150109a9638SPeter Gruber #define PCI_EXT_AsicRev     0x52
151109a9638SPeter Gruber #define PCI_EXT_Reserved3   0x53
152109a9638SPeter Gruber 
153109a9638SPeter Gruber #define LEGACY_ENABLE_ALL      0x8000	/* legacy device options */
154109a9638SPeter Gruber #define LEGACY_ENABLE_SB       0x4000
155109a9638SPeter Gruber #define LEGACY_ENABLE_FM       0x2000
156109a9638SPeter Gruber #define LEGACY_ENABLE_MPU_INT  0x1000
157109a9638SPeter Gruber #define LEGACY_ENABLE_MPU      0x0800
158109a9638SPeter Gruber #define LEGACY_ENABLE_GAMEPORT 0x0400
159109a9638SPeter Gruber 
160109a9638SPeter Gruber #define MAX_WRITE_RETRY  10	/* cmd interface limits */
161109a9638SPeter Gruber #define MAX_ERROR_COUNT  10
1620f620830SPeter Gruber #define CMDIF_TIMEOUT    50000
163109a9638SPeter Gruber #define RESET_TRIES      5
164109a9638SPeter Gruber 
165109a9638SPeter Gruber #define READ_PORT_ULONG(p)     inl((unsigned long)&(p))
166109a9638SPeter Gruber #define WRITE_PORT_ULONG(p,x)  outl(x,(unsigned long)&(p))
167109a9638SPeter Gruber 
168109a9638SPeter Gruber #define READ_AUDIO_CONTROL(p)     READ_PORT_ULONG(p->audio_control)
169109a9638SPeter Gruber #define WRITE_AUDIO_CONTROL(p,x)  WRITE_PORT_ULONG(p->audio_control,x)
170109a9638SPeter Gruber #define UMASK_AUDIO_CONTROL(p,x)  WRITE_PORT_ULONG(p->audio_control,READ_PORT_ULONG(p->audio_control)|x)
171109a9638SPeter Gruber #define MASK_AUDIO_CONTROL(p,x)   WRITE_PORT_ULONG(p->audio_control,READ_PORT_ULONG(p->audio_control)&x)
172109a9638SPeter Gruber #define READ_AUDIO_STATUS(p)      READ_PORT_ULONG(p->audio_status)
173109a9638SPeter Gruber 
174109a9638SPeter Gruber #define SET_GRESET(p)     UMASK_AUDIO_CONTROL(p,0x0001)	/* global reset switch */
175109a9638SPeter Gruber #define UNSET_GRESET(p)   MASK_AUDIO_CONTROL(p,~0x0001)
176109a9638SPeter Gruber #define SET_AIE(p)        UMASK_AUDIO_CONTROL(p,0x0004)	/* interrupt enable */
177109a9638SPeter Gruber #define UNSET_AIE(p)      MASK_AUDIO_CONTROL(p,~0x0004)
178109a9638SPeter Gruber #define SET_AIACK(p)      UMASK_AUDIO_CONTROL(p,0x0008)	/* interrupt acknowledge */
179109a9638SPeter Gruber #define UNSET_AIACKT(p)   MASKAUDIO_CONTROL(p,~0x0008)
180109a9638SPeter Gruber #define SET_ECMDAE(p)     UMASK_AUDIO_CONTROL(p,0x0010)
181109a9638SPeter Gruber #define UNSET_ECMDAE(p)   MASK_AUDIO_CONTROL(p,~0x0010)
182109a9638SPeter Gruber #define SET_ECMDBE(p)     UMASK_AUDIO_CONTROL(p,0x0020)
183109a9638SPeter Gruber #define UNSET_ECMDBE(p)   MASK_AUDIO_CONTROL(p,~0x0020)
184109a9638SPeter Gruber #define SET_EDATAF(p)     UMASK_AUDIO_CONTROL(p,0x0040)
185109a9638SPeter Gruber #define UNSET_EDATAF(p)   MASK_AUDIO_CONTROL(p,~0x0040)
186109a9638SPeter Gruber #define SET_EDATBF(p)     UMASK_AUDIO_CONTROL(p,0x0080)
187109a9638SPeter Gruber #define UNSET_EDATBF(p)   MASK_AUDIO_CONTROL(p,~0x0080)
188109a9638SPeter Gruber #define SET_ESBIRQON(p)   UMASK_AUDIO_CONTROL(p,0x0100)
189109a9638SPeter Gruber #define UNSET_ESBIRQON(p) MASK_AUDIO_CONTROL(p,~0x0100)
190109a9638SPeter Gruber #define SET_EMPUIRQ(p)    UMASK_AUDIO_CONTROL(p,0x0200)
191109a9638SPeter Gruber #define UNSET_EMPUIRQ(p)  MASK_AUDIO_CONTROL(p,~0x0200)
192109a9638SPeter Gruber #define IS_CMDE(a)        (READ_PORT_ULONG(a->stat)&0x1)	/* cmd empty */
193109a9638SPeter Gruber #define IS_DATF(a)        (READ_PORT_ULONG(a->stat)&0x2)	/* data filled */
194109a9638SPeter Gruber #define IS_READY(p)       (READ_AUDIO_STATUS(p)&0x0001)
195109a9638SPeter Gruber #define IS_DLREADY(p)     (READ_AUDIO_STATUS(p)&0x0002)
196109a9638SPeter Gruber #define IS_DLERR(p)       (READ_AUDIO_STATUS(p)&0x0004)
197109a9638SPeter Gruber #define IS_GERR(p)        (READ_AUDIO_STATUS(p)&0x0008)	/* error ! */
198109a9638SPeter Gruber #define IS_CMDAEIRQ(p)    (READ_AUDIO_STATUS(p)&0x0010)
199109a9638SPeter Gruber #define IS_CMDBEIRQ(p)    (READ_AUDIO_STATUS(p)&0x0020)
200109a9638SPeter Gruber #define IS_DATAFIRQ(p)    (READ_AUDIO_STATUS(p)&0x0040)
201109a9638SPeter Gruber #define IS_DATBFIRQ(p)    (READ_AUDIO_STATUS(p)&0x0080)
202109a9638SPeter Gruber #define IS_EOBIRQ(p)      (READ_AUDIO_STATUS(p)&0x0100)	/* interrupt status */
203109a9638SPeter Gruber #define IS_EOSIRQ(p)      (READ_AUDIO_STATUS(p)&0x0200)
204109a9638SPeter Gruber #define IS_EOCIRQ(p)      (READ_AUDIO_STATUS(p)&0x0400)
205109a9638SPeter Gruber #define IS_UNSLIRQ(p)     (READ_AUDIO_STATUS(p)&0x0800)
206109a9638SPeter Gruber #define IS_SBIRQ(p)       (READ_AUDIO_STATUS(p)&0x1000)
207109a9638SPeter Gruber #define IS_MPUIRQ(p)      (READ_AUDIO_STATUS(p)&0x2000)
208109a9638SPeter Gruber 
209109a9638SPeter Gruber #define RESP 0x00000001		/* command flags */
210109a9638SPeter Gruber #define PARM 0x00000002
211109a9638SPeter Gruber #define CMDA 0x00000004
212109a9638SPeter Gruber #define CMDB 0x00000008
213109a9638SPeter Gruber #define NILL 0x00000000
214109a9638SPeter Gruber 
215109a9638SPeter Gruber #define LONG0(a)   ((u32)a)	/* shifts and masks */
216109a9638SPeter Gruber #define BYTE0(a)   (LONG0(a)&0xff)
217109a9638SPeter Gruber #define BYTE1(a)   (BYTE0(a)<<8)
218109a9638SPeter Gruber #define BYTE2(a)   (BYTE0(a)<<16)
219109a9638SPeter Gruber #define BYTE3(a)   (BYTE0(a)<<24)
220109a9638SPeter Gruber #define WORD0(a)   (LONG0(a)&0xffff)
221109a9638SPeter Gruber #define WORD1(a)   (WORD0(a)<<8)
222109a9638SPeter Gruber #define WORD2(a)   (WORD0(a)<<16)
223109a9638SPeter Gruber #define TRINIB0(a) (LONG0(a)&0xffffff)
224109a9638SPeter Gruber #define TRINIB1(a) (TRINIB0(a)<<8)
225109a9638SPeter Gruber 
226109a9638SPeter Gruber #define RET(a)     ((union cmdret *)(a))
227109a9638SPeter Gruber 
228109a9638SPeter Gruber #define SEND_GETV(p,b)             sendcmd(p,RESP,GETV,0,RET(b))	/* get version */
229109a9638SPeter Gruber #define SEND_GETC(p,b,c)           sendcmd(p,PARM|RESP,GETC,c,RET(b))
230109a9638SPeter Gruber #define SEND_GUNS(p,b)             sendcmd(p,RESP,GUNS,0,RET(b))
231109a9638SPeter Gruber #define SEND_SCID(p,b)             sendcmd(p,RESP,SCID,0,RET(b))
232109a9638SPeter Gruber #define SEND_RMEM(p,b,c,d)         sendcmd(p,PARM|RESP,RMEM|BYTE1(b),LONG0(c),RET(d))	/* memory access for firmware write */
233109a9638SPeter Gruber #define SEND_SMEM(p,b,c)           sendcmd(p,PARM,SMEM|BYTE1(b),LONG0(c),RET(0))	/* memory access for firmware write */
234109a9638SPeter Gruber #define SEND_WMEM(p,b,c)           sendcmd(p,PARM,WMEM|BYTE1(b),LONG0(c),RET(0))	/* memory access for firmware write */
235109a9638SPeter Gruber #define SEND_SDTM(p,b,c)           sendcmd(p,PARM|RESP,SDTM|TRINIB1(b),0,RET(c))	/* memory access for firmware write */
236109a9638SPeter Gruber #define SEND_GOTO(p,b)             sendcmd(p,PARM,GOTO,LONG0(b),RET(0))	/* memory access for firmware write */
237109a9638SPeter Gruber #define SEND_SETDPLL(p)	           sendcmd(p,0,ARM_SETDPLL,0,RET(0))
238109a9638SPeter Gruber #define SEND_SSTR(p,b,c)           sendcmd(p,PARM,SSTR|BYTE3(b),LONG0(c),RET(0))	/* start stream */
239109a9638SPeter Gruber #define SEND_PSTR(p,b)             sendcmd(p,PARM,PSTR,BYTE3(b),RET(0))	/* pause stream */
240109a9638SPeter Gruber #define SEND_KSTR(p,b)             sendcmd(p,PARM,KSTR,BYTE3(b),RET(0))	/* stop stream */
241109a9638SPeter Gruber #define SEND_KDMA(p)               sendcmd(p,0,KDMA,0,RET(0))	/* stop all dma */
242109a9638SPeter Gruber #define SEND_GPOS(p,b,c,d)         sendcmd(p,PARM|RESP,GPOS,BYTE3(c)|BYTE2(b),RET(d))	/* get position in dma */
243109a9638SPeter 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 */
244109a9638SPeter Gruber #define SEND_GSTS(p,b,c,d)         sendcmd(p,PARM|RESP,GSTS,BYTE3(c)|BYTE2(b),RET(d))
245109a9638SPeter Gruber #define SEND_NGPOS(p,b,c,d)        sendcmd(p,PARM|RESP,NGPOS,BYTE3(c)|BYTE2(b),RET(d))
246109a9638SPeter Gruber #define SEND_PSEL(p,b,c)           sendcmd(p,PARM,PSEL,BYTE2(b)|BYTE3(c),RET(0))	/* activate lbus path */
247109a9638SPeter Gruber #define SEND_PCLR(p,b,c)           sendcmd(p,PARM,PCLR,BYTE2(b)|BYTE3(c),RET(0))	/* deactivate lbus path */
248109a9638SPeter Gruber #define SEND_PLST(p,b)             sendcmd(p,PARM,PLST,BYTE3(b),RET(0))
249109a9638SPeter Gruber #define SEND_RSSV(p,b,c,d)         sendcmd(p,PARM|RESP,RSSV,BYTE2(b)|BYTE3(c),RET(d))
250109a9638SPeter 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 */
251109a9638SPeter 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 */
252109a9638SPeter Gruber #define SEND_SLST(p,b)             sendcmd(p,PARM,SLST,BYTE3(b),RET(0))
253109a9638SPeter Gruber #define SEND_RSRC(p,b,c)           sendcmd(p,RESP,RSRC|BYTE1(b),0,RET(c))	/* read source config */
254109a9638SPeter Gruber #define SEND_SSRB(p,b,c)           sendcmd(p,PARM,SSRB|BYTE1(b),WORD2(c),RET(0))
255109a9638SPeter 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 */
256109a9638SPeter Gruber #define SEND_RDGV(p,b,c,d)         sendcmd(p,PARM|RESP,RDGV|BYTE2(b)|BYTE3(c),0,RET(d))	/* read digital mixer */
257109a9638SPeter Gruber #define SEND_DLST(p,b)             sendcmd(p,PARM,DLST,BYTE3(b),RET(0))
258109a9638SPeter Gruber #define SEND_SACR(p,b,c)           sendcmd(p,PARM,SACR,WORD0(b)|WORD2(c),RET(0))	/* set AC97 register */
259109a9638SPeter Gruber #define SEND_RACR(p,b,c)           sendcmd(p,PARM|RESP,RACR,WORD2(b),RET(c))	/* get AC97 register */
260109a9638SPeter Gruber #define SEND_ALST(p,b)             sendcmd(p,PARM,ALST,BYTE3(b),RET(0))
261109a9638SPeter 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))
262109a9638SPeter Gruber #define SEND_RXAC(p,b,c,d)         sendcmd(p,PARM|RESP,RXAC,BYTE2(b)|BYTE3(c),RET(d))
263109a9638SPeter Gruber #define SEND_SI2S(p,b)             sendcmd(p,PARM,SI2S,WORD2(b),RET(0))
264109a9638SPeter Gruber 
265109a9638SPeter Gruber #define EOB_STATUS         0x80000000	/* status flags : block boundary */
266109a9638SPeter Gruber #define EOS_STATUS         0x40000000	/*              : stoppped */
267109a9638SPeter Gruber #define EOC_STATUS         0x20000000	/*              : stream end */
268109a9638SPeter Gruber #define ERR_STATUS         0x10000000
269109a9638SPeter Gruber #define EMPTY_STATUS       0x08000000
270109a9638SPeter Gruber 
271109a9638SPeter Gruber #define IEOB_ENABLE        0x1	/* enable interrupts for status notification above */
272109a9638SPeter Gruber #define IEOS_ENABLE        0x2
273109a9638SPeter Gruber #define IEOC_ENABLE        0x4
274109a9638SPeter Gruber #define RDONCE             0x8
275109a9638SPeter Gruber #define DESC_MAX_MASK      0xff
276109a9638SPeter Gruber 
277109a9638SPeter Gruber #define ST_PLAY  0x1		/* stream states */
278109a9638SPeter Gruber #define ST_STOP  0x2
279109a9638SPeter Gruber #define ST_PAUSE 0x4
280109a9638SPeter Gruber 
281109a9638SPeter Gruber #define I2S_INTDEC     3	/* config for I2S link */
282109a9638SPeter Gruber #define I2S_MERGER     0
283109a9638SPeter Gruber #define I2S_SPLITTER   0
284109a9638SPeter Gruber #define I2S_MIXER      7
285109a9638SPeter Gruber #define I2S_RATE       44100
286109a9638SPeter Gruber 
287109a9638SPeter Gruber #define MODEM_INTDEC   4	/* config for modem link */
288109a9638SPeter Gruber #define MODEM_MERGER   3
289109a9638SPeter Gruber #define MODEM_SPLITTER 0
290109a9638SPeter Gruber #define MODEM_MIXER    11
291109a9638SPeter Gruber 
292109a9638SPeter Gruber #define FM_INTDEC      3	/* config for FM/OPL3 link */
293109a9638SPeter Gruber #define FM_MERGER      0
294109a9638SPeter Gruber #define FM_SPLITTER    0
295109a9638SPeter Gruber #define FM_MIXER       9
296109a9638SPeter Gruber 
297109a9638SPeter Gruber #define SPLIT_PATH  0x80	/* path splitting flag */
298109a9638SPeter Gruber 
299109a9638SPeter Gruber enum FIRMWARE {
300109a9638SPeter Gruber 	DATA_REC = 0, EXT_END_OF_FILE, EXT_SEG_ADDR_REC, EXT_GOTO_CMD_REC,
301109a9638SPeter Gruber 	EXT_LIN_ADDR_REC,
302109a9638SPeter Gruber };
303109a9638SPeter Gruber 
304109a9638SPeter Gruber enum CMDS {
305109a9638SPeter Gruber 	GETV = 0x00, GETC, GUNS, SCID, RMEM =
306109a9638SPeter Gruber 	    0x10, SMEM, WMEM, SDTM, GOTO, SSTR =
307109a9638SPeter Gruber 	    0x20, PSTR, KSTR, KDMA, GPOS, SETF, GSTS, NGPOS, PSEL =
308109a9638SPeter Gruber 	    0x30, PCLR, PLST, RSSV, LSEL, SSRC = 0x40, SLST, RSRC, SSRB, SDGV =
309109a9638SPeter Gruber 	    0x50, RDGV, DLST, SACR = 0x60, RACR, ALST, TXAC, RXAC, SI2S =
310109a9638SPeter Gruber 	    0x70, ARM_SETDPLL = 0x72,
311109a9638SPeter Gruber };
312109a9638SPeter Gruber 
313109a9638SPeter Gruber enum E1SOURCE {
314109a9638SPeter Gruber 	ARM2LBUS_FIFO0 = 0, ARM2LBUS_FIFO1, ARM2LBUS_FIFO2, ARM2LBUS_FIFO3,
315109a9638SPeter Gruber 	ARM2LBUS_FIFO4, ARM2LBUS_FIFO5, ARM2LBUS_FIFO6, ARM2LBUS_FIFO7,
316109a9638SPeter Gruber 	ARM2LBUS_FIFO8, ARM2LBUS_FIFO9, ARM2LBUS_FIFO10, ARM2LBUS_FIFO11,
317109a9638SPeter Gruber 	ARM2LBUS_FIFO12, ARM2LBUS_FIFO13, ARM2LBUS_FIFO14, ARM2LBUS_FIFO15,
318109a9638SPeter Gruber 	INTER0_OUT, INTER1_OUT, INTER2_OUT, INTER3_OUT, INTER4_OUT,
319109a9638SPeter Gruber 	INTERM0_OUT, INTERM1_OUT, INTERM2_OUT, INTERM3_OUT, INTERM4_OUT,
320109a9638SPeter Gruber 	INTERM5_OUT, INTERM6_OUT, DECIMM0_OUT, DECIMM1_OUT, DECIMM2_OUT,
321109a9638SPeter Gruber 	DECIMM3_OUT, DECIM0_OUT, SR3_4_OUT, OPL3_SAMPLE, ASRC0, ASRC1,
322109a9638SPeter Gruber 	ACLNK2PADC, ACLNK2MODEM0RX, ACLNK2MIC, ACLNK2MODEM1RX, ACLNK2HNDMIC,
323109a9638SPeter Gruber 	DIGITAL_MIXER_OUT0, GAINFUNC0_OUT, GAINFUNC1_OUT, GAINFUNC2_OUT,
324109a9638SPeter Gruber 	GAINFUNC3_OUT, GAINFUNC4_OUT, SOFTMODEMTX, SPLITTER0_OUTL,
325109a9638SPeter Gruber 	SPLITTER0_OUTR, SPLITTER1_OUTL, SPLITTER1_OUTR, SPLITTER2_OUTL,
326109a9638SPeter Gruber 	SPLITTER2_OUTR, SPLITTER3_OUTL, SPLITTER3_OUTR, MERGER0_OUT,
327109a9638SPeter Gruber 	MERGER1_OUT, MERGER2_OUT, MERGER3_OUT, ARM2LBUS_FIFO_DIRECT, NO_OUT
328109a9638SPeter Gruber };
329109a9638SPeter Gruber 
330109a9638SPeter Gruber enum E2SINK {
331109a9638SPeter Gruber 	LBUS2ARM_FIFO0 = 0, LBUS2ARM_FIFO1, LBUS2ARM_FIFO2, LBUS2ARM_FIFO3,
332109a9638SPeter Gruber 	LBUS2ARM_FIFO4, LBUS2ARM_FIFO5, LBUS2ARM_FIFO6, LBUS2ARM_FIFO7,
333109a9638SPeter Gruber 	INTER0_IN, INTER1_IN, INTER2_IN, INTER3_IN, INTER4_IN, INTERM0_IN,
334109a9638SPeter Gruber 	INTERM1_IN, INTERM2_IN, INTERM3_IN, INTERM4_IN, INTERM5_IN, INTERM6_IN,
335109a9638SPeter Gruber 	DECIMM0_IN, DECIMM1_IN, DECIMM2_IN, DECIMM3_IN, DECIM0_IN, SR3_4_IN,
336109a9638SPeter Gruber 	PDAC2ACLNK, MODEM0TX2ACLNK, MODEM1TX2ACLNK, HNDSPK2ACLNK,
337109a9638SPeter Gruber 	DIGITAL_MIXER_IN0, DIGITAL_MIXER_IN1, DIGITAL_MIXER_IN2,
338109a9638SPeter Gruber 	DIGITAL_MIXER_IN3, DIGITAL_MIXER_IN4, DIGITAL_MIXER_IN5,
339109a9638SPeter Gruber 	DIGITAL_MIXER_IN6, DIGITAL_MIXER_IN7, DIGITAL_MIXER_IN8,
340109a9638SPeter Gruber 	DIGITAL_MIXER_IN9, DIGITAL_MIXER_IN10, DIGITAL_MIXER_IN11,
341109a9638SPeter Gruber 	GAINFUNC0_IN, GAINFUNC1_IN, GAINFUNC2_IN, GAINFUNC3_IN, GAINFUNC4_IN,
342109a9638SPeter Gruber 	SOFTMODEMRX, SPLITTER0_IN, SPLITTER1_IN, SPLITTER2_IN, SPLITTER3_IN,
343109a9638SPeter Gruber 	MERGER0_INL, MERGER0_INR, MERGER1_INL, MERGER1_INR, MERGER2_INL,
344109a9638SPeter Gruber 	MERGER2_INR, MERGER3_INL, MERGER3_INR, E2SINK_MAX
345109a9638SPeter Gruber };
346109a9638SPeter Gruber 
347109a9638SPeter Gruber enum LBUS_SINK {
348109a9638SPeter Gruber 	LS_SRC_INTERPOLATOR = 0, LS_SRC_INTERPOLATORM, LS_SRC_DECIMATOR,
349109a9638SPeter Gruber 	LS_SRC_DECIMATORM, LS_MIXER_IN, LS_MIXER_GAIN_FUNCTION,
350109a9638SPeter Gruber 	LS_SRC_SPLITTER, LS_SRC_MERGER, LS_NONE1, LS_NONE2,
351109a9638SPeter Gruber };
352109a9638SPeter Gruber 
353109a9638SPeter Gruber enum RT_CHANNEL_IDS {
354109a9638SPeter Gruber 	M0TX = 0, M1TX, TAMTX, HSSPKR, PDAC, DSNDTX0, DSNDTX1, DSNDTX2,
355109a9638SPeter Gruber 	DSNDTX3, DSNDTX4, DSNDTX5, DSNDTX6, DSNDTX7, WVSTRTX, COP3DTX, SPARE,
356109a9638SPeter Gruber 	M0RX, HSMIC, M1RX, CLEANRX, MICADC, PADC, COPRX1, COPRX2,
357109a9638SPeter Gruber 	CHANNEL_ID_COUNTER
358109a9638SPeter Gruber };
359109a9638SPeter Gruber 
360109a9638SPeter Gruber enum { SB_CMD = 0, MODEM_CMD, I2S_CMD0, I2S_CMD1, FM_CMD, MAX_CMD };
361109a9638SPeter Gruber 
362109a9638SPeter Gruber struct lbuspath {
363bf6f3d74STakashi Iwai 	const unsigned char *noconv;
364bf6f3d74STakashi Iwai 	const unsigned char *stereo;
365bf6f3d74STakashi Iwai 	const unsigned char *mono;
366109a9638SPeter Gruber };
367109a9638SPeter Gruber 
368109a9638SPeter Gruber struct cmdport {
369109a9638SPeter Gruber 	u32 data1;		/* cmd,param */
370109a9638SPeter Gruber 	u32 data2;		/* param */
371109a9638SPeter Gruber 	u32 stat;		/* status */
372109a9638SPeter Gruber 	u32 pad[5];
373109a9638SPeter Gruber };
374109a9638SPeter Gruber 
375109a9638SPeter Gruber struct riptideport {
376109a9638SPeter Gruber 	u32 audio_control;	/* status registers */
377109a9638SPeter Gruber 	u32 audio_status;
378109a9638SPeter Gruber 	u32 pad[2];
379109a9638SPeter Gruber 	struct cmdport port[2];	/* command ports */
380109a9638SPeter Gruber };
381109a9638SPeter Gruber 
382109a9638SPeter Gruber struct cmdif {
383109a9638SPeter Gruber 	struct riptideport *hwport;
384109a9638SPeter Gruber 	spinlock_t lock;
385109a9638SPeter Gruber 	unsigned int cmdcnt;	/* cmd statistics */
386109a9638SPeter Gruber 	unsigned int cmdtime;
387109a9638SPeter Gruber 	unsigned int cmdtimemax;
388109a9638SPeter Gruber 	unsigned int cmdtimemin;
389109a9638SPeter Gruber 	unsigned int errcnt;
390109a9638SPeter Gruber 	int is_reset;
391109a9638SPeter Gruber };
392109a9638SPeter Gruber 
393109a9638SPeter Gruber struct riptide_firmware {
394109a9638SPeter Gruber 	u16 ASIC;
395109a9638SPeter Gruber 	u16 CODEC;
396109a9638SPeter Gruber 	u16 AUXDSP;
397109a9638SPeter Gruber 	u16 PROG;
398109a9638SPeter Gruber };
399109a9638SPeter Gruber 
400109a9638SPeter Gruber union cmdret {
401109a9638SPeter Gruber 	u8 retbytes[8];
402109a9638SPeter Gruber 	u16 retwords[4];
403109a9638SPeter Gruber 	u32 retlongs[2];
404109a9638SPeter Gruber };
405109a9638SPeter Gruber 
406109a9638SPeter Gruber union firmware_version {
407109a9638SPeter Gruber 	union cmdret ret;
408109a9638SPeter Gruber 	struct riptide_firmware firmware;
409109a9638SPeter Gruber };
410109a9638SPeter Gruber 
411109a9638SPeter Gruber #define get_pcmhwdev(substream) (struct pcmhw *)(substream->runtime->private_data)
412109a9638SPeter Gruber 
413109a9638SPeter Gruber #define PLAYBACK_SUBSTREAMS 3
414109a9638SPeter Gruber struct snd_riptide {
415109a9638SPeter Gruber 	struct snd_card *card;
416109a9638SPeter Gruber 	struct pci_dev *pci;
417109a9638SPeter Gruber 	const struct firmware *fw_entry;
418109a9638SPeter Gruber 
419109a9638SPeter Gruber 	struct cmdif *cif;
420109a9638SPeter Gruber 
421109a9638SPeter Gruber 	struct snd_pcm *pcm;
422109a9638SPeter Gruber 	struct snd_pcm *pcm_i2s;
423109a9638SPeter Gruber 	struct snd_rawmidi *rmidi;
424109a9638SPeter Gruber 	struct snd_opl3 *opl3;
425109a9638SPeter Gruber 	struct snd_ac97 *ac97;
426109a9638SPeter Gruber 	struct snd_ac97_bus *ac97_bus;
427109a9638SPeter Gruber 
428109a9638SPeter Gruber 	struct snd_pcm_substream *playback_substream[PLAYBACK_SUBSTREAMS];
429109a9638SPeter Gruber 	struct snd_pcm_substream *capture_substream;
430109a9638SPeter Gruber 
431109a9638SPeter Gruber 	int openstreams;
432109a9638SPeter Gruber 
433109a9638SPeter Gruber 	int irq;
434109a9638SPeter Gruber 	unsigned long port;
435109a9638SPeter Gruber 	unsigned short mpuaddr;
436109a9638SPeter Gruber 	unsigned short opladdr;
437109a9638SPeter Gruber #ifdef SUPPORT_JOYSTICK
438109a9638SPeter Gruber 	unsigned short gameaddr;
439109a9638SPeter Gruber #endif
440109a9638SPeter Gruber 	struct resource *res_port;
441109a9638SPeter Gruber 
442109a9638SPeter Gruber 	unsigned short device_id;
443109a9638SPeter Gruber 
444109a9638SPeter Gruber 	union firmware_version firmware;
445109a9638SPeter Gruber 
446109a9638SPeter Gruber 	spinlock_t lock;
447109a9638SPeter Gruber 	struct snd_info_entry *proc_entry;
448109a9638SPeter Gruber 
449109a9638SPeter Gruber 	unsigned long received_irqs;
450109a9638SPeter Gruber 	unsigned long handled_irqs;
451c7561cd8STakashi Iwai #ifdef CONFIG_PM_SLEEP
452109a9638SPeter Gruber 	int in_suspend;
453109a9638SPeter Gruber #endif
454109a9638SPeter Gruber };
455109a9638SPeter Gruber 
456109a9638SPeter Gruber struct sgd {			/* scatter gather desriptor */
457be05e3deSTakashi Iwai 	__le32 dwNextLink;
458be05e3deSTakashi Iwai 	__le32 dwSegPtrPhys;
459be05e3deSTakashi Iwai 	__le32 dwSegLen;
460be05e3deSTakashi Iwai 	__le32 dwStat_Ctl;
461109a9638SPeter Gruber };
462109a9638SPeter Gruber 
463109a9638SPeter Gruber struct pcmhw {			/* pcm descriptor */
464109a9638SPeter Gruber 	struct lbuspath paths;
465bf6f3d74STakashi Iwai 	const unsigned char *lbuspath;
466109a9638SPeter Gruber 	unsigned char source;
467109a9638SPeter Gruber 	unsigned char intdec[2];
468109a9638SPeter Gruber 	unsigned char mixer;
469109a9638SPeter Gruber 	unsigned char id;
470109a9638SPeter Gruber 	unsigned char state;
471109a9638SPeter Gruber 	unsigned int rate;
472109a9638SPeter Gruber 	unsigned int channels;
473109a9638SPeter Gruber 	snd_pcm_format_t format;
474109a9638SPeter Gruber 	struct snd_dma_buffer sgdlist;
475109a9638SPeter Gruber 	struct sgd *sgdbuf;
476109a9638SPeter Gruber 	unsigned int size;
477109a9638SPeter Gruber 	unsigned int pages;
478109a9638SPeter Gruber 	unsigned int oldpos;
479109a9638SPeter Gruber 	unsigned int pointer;
480109a9638SPeter Gruber };
481109a9638SPeter Gruber 
482109a9638SPeter Gruber #define CMDRET_ZERO (union cmdret){{(u32)0, (u32) 0}}
483109a9638SPeter Gruber 
484109a9638SPeter Gruber static int sendcmd(struct cmdif *cif, u32 flags, u32 cmd, u32 parm,
485109a9638SPeter Gruber 		   union cmdret *ret);
486109a9638SPeter Gruber static int getsourcesink(struct cmdif *cif, unsigned char source,
487109a9638SPeter Gruber 			 unsigned char sink, unsigned char *a,
488109a9638SPeter Gruber 			 unsigned char *b);
489109a9638SPeter Gruber static int snd_riptide_initialize(struct snd_riptide *chip);
490109a9638SPeter Gruber static int riptide_reset(struct cmdif *cif, struct snd_riptide *chip);
491109a9638SPeter Gruber 
492109a9638SPeter Gruber /*
493109a9638SPeter Gruber  */
494109a9638SPeter Gruber 
4959baa3c34SBenoit Taine static const struct pci_device_id snd_riptide_ids[] = {
496a693a26fSTakashi Iwai 	{ PCI_DEVICE(0x127a, 0x4310) },
497a693a26fSTakashi Iwai 	{ PCI_DEVICE(0x127a, 0x4320) },
498a693a26fSTakashi Iwai 	{ PCI_DEVICE(0x127a, 0x4330) },
499a693a26fSTakashi Iwai 	{ PCI_DEVICE(0x127a, 0x4340) },
500109a9638SPeter Gruber 	{0,},
501109a9638SPeter Gruber };
502109a9638SPeter Gruber 
503109a9638SPeter Gruber #ifdef SUPPORT_JOYSTICK
5049baa3c34SBenoit Taine static const struct pci_device_id snd_riptide_joystick_ids[] = {
505a693a26fSTakashi Iwai 	{ PCI_DEVICE(0x127a, 0x4312) },
506a693a26fSTakashi Iwai 	{ PCI_DEVICE(0x127a, 0x4322) },
507a693a26fSTakashi Iwai 	{ PCI_DEVICE(0x127a, 0x4332) },
508a693a26fSTakashi Iwai 	{ PCI_DEVICE(0x127a, 0x4342) },
509109a9638SPeter Gruber 	{0,},
510109a9638SPeter Gruber };
511109a9638SPeter Gruber #endif
512109a9638SPeter Gruber 
513109a9638SPeter Gruber MODULE_DEVICE_TABLE(pci, snd_riptide_ids);
514109a9638SPeter Gruber 
515109a9638SPeter Gruber /*
516109a9638SPeter Gruber  */
517109a9638SPeter Gruber 
518bf6f3d74STakashi Iwai static const unsigned char lbusin2out[E2SINK_MAX + 1][2] = {
519109a9638SPeter Gruber 	{NO_OUT, LS_NONE1}, {NO_OUT, LS_NONE2}, {NO_OUT, LS_NONE1}, {NO_OUT,
520109a9638SPeter Gruber 								     LS_NONE2},
521109a9638SPeter Gruber 	{NO_OUT, LS_NONE1}, {NO_OUT, LS_NONE2}, {NO_OUT, LS_NONE1}, {NO_OUT,
522109a9638SPeter Gruber 								     LS_NONE2},
523109a9638SPeter Gruber 	{INTER0_OUT, LS_SRC_INTERPOLATOR}, {INTER1_OUT, LS_SRC_INTERPOLATOR},
524109a9638SPeter Gruber 	{INTER2_OUT, LS_SRC_INTERPOLATOR}, {INTER3_OUT, LS_SRC_INTERPOLATOR},
525109a9638SPeter Gruber 	{INTER4_OUT, LS_SRC_INTERPOLATOR}, {INTERM0_OUT, LS_SRC_INTERPOLATORM},
526109a9638SPeter Gruber 	{INTERM1_OUT, LS_SRC_INTERPOLATORM}, {INTERM2_OUT,
527109a9638SPeter Gruber 					      LS_SRC_INTERPOLATORM},
528109a9638SPeter Gruber 	{INTERM3_OUT, LS_SRC_INTERPOLATORM}, {INTERM4_OUT,
529109a9638SPeter Gruber 					      LS_SRC_INTERPOLATORM},
530109a9638SPeter Gruber 	{INTERM5_OUT, LS_SRC_INTERPOLATORM}, {INTERM6_OUT,
531109a9638SPeter Gruber 					      LS_SRC_INTERPOLATORM},
532109a9638SPeter Gruber 	{DECIMM0_OUT, LS_SRC_DECIMATORM}, {DECIMM1_OUT, LS_SRC_DECIMATORM},
533109a9638SPeter Gruber 	{DECIMM2_OUT, LS_SRC_DECIMATORM}, {DECIMM3_OUT, LS_SRC_DECIMATORM},
534109a9638SPeter Gruber 	{DECIM0_OUT, LS_SRC_DECIMATOR}, {SR3_4_OUT, LS_NONE1}, {NO_OUT,
535109a9638SPeter Gruber 								LS_NONE2},
536109a9638SPeter Gruber 	{NO_OUT, LS_NONE1}, {NO_OUT, LS_NONE2}, {NO_OUT, LS_NONE1},
537109a9638SPeter Gruber 	{DIGITAL_MIXER_OUT0, LS_MIXER_IN}, {DIGITAL_MIXER_OUT0, LS_MIXER_IN},
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 	{GAINFUNC0_OUT, LS_MIXER_GAIN_FUNCTION}, {GAINFUNC1_OUT,
544109a9638SPeter Gruber 						  LS_MIXER_GAIN_FUNCTION},
545109a9638SPeter Gruber 	{GAINFUNC2_OUT, LS_MIXER_GAIN_FUNCTION}, {GAINFUNC3_OUT,
546109a9638SPeter Gruber 						  LS_MIXER_GAIN_FUNCTION},
547109a9638SPeter Gruber 	{GAINFUNC4_OUT, LS_MIXER_GAIN_FUNCTION}, {SOFTMODEMTX, LS_NONE1},
548109a9638SPeter Gruber 	{SPLITTER0_OUTL, LS_SRC_SPLITTER}, {SPLITTER1_OUTL, LS_SRC_SPLITTER},
549109a9638SPeter Gruber 	{SPLITTER2_OUTL, LS_SRC_SPLITTER}, {SPLITTER3_OUTL, LS_SRC_SPLITTER},
550109a9638SPeter Gruber 	{MERGER0_OUT, LS_SRC_MERGER}, {MERGER0_OUT, LS_SRC_MERGER},
551109a9638SPeter Gruber 	{MERGER1_OUT, LS_SRC_MERGER},
552109a9638SPeter Gruber 	{MERGER1_OUT, LS_SRC_MERGER}, {MERGER2_OUT, LS_SRC_MERGER},
553109a9638SPeter Gruber 	{MERGER2_OUT, LS_SRC_MERGER},
554109a9638SPeter Gruber 	{MERGER3_OUT, LS_SRC_MERGER}, {MERGER3_OUT, LS_SRC_MERGER}, {NO_OUT,
555109a9638SPeter Gruber 								     LS_NONE2},
556109a9638SPeter Gruber };
557109a9638SPeter Gruber 
558bf6f3d74STakashi Iwai static const unsigned char lbus_play_opl3[] = {
559109a9638SPeter Gruber 	DIGITAL_MIXER_IN0 + FM_MIXER, 0xff
560109a9638SPeter Gruber };
561bf6f3d74STakashi Iwai static const unsigned char lbus_play_modem[] = {
562109a9638SPeter Gruber 	DIGITAL_MIXER_IN0 + MODEM_MIXER, 0xff
563109a9638SPeter Gruber };
564bf6f3d74STakashi Iwai static const unsigned char lbus_play_i2s[] = {
565109a9638SPeter Gruber 	INTER0_IN + I2S_INTDEC, DIGITAL_MIXER_IN0 + I2S_MIXER, 0xff
566109a9638SPeter Gruber };
567bf6f3d74STakashi Iwai static const unsigned char lbus_play_out[] = {
568109a9638SPeter Gruber 	PDAC2ACLNK, 0xff
569109a9638SPeter Gruber };
570bf6f3d74STakashi Iwai static const unsigned char lbus_play_outhp[] = {
571109a9638SPeter Gruber 	HNDSPK2ACLNK, 0xff
572109a9638SPeter Gruber };
573bf6f3d74STakashi Iwai static const unsigned char lbus_play_noconv1[] = {
574109a9638SPeter Gruber 	DIGITAL_MIXER_IN0, 0xff
575109a9638SPeter Gruber };
576bf6f3d74STakashi Iwai static const unsigned char lbus_play_stereo1[] = {
577109a9638SPeter Gruber 	INTER0_IN, DIGITAL_MIXER_IN0, 0xff
578109a9638SPeter Gruber };
579bf6f3d74STakashi Iwai static const unsigned char lbus_play_mono1[] = {
580109a9638SPeter Gruber 	INTERM0_IN, DIGITAL_MIXER_IN0, 0xff
581109a9638SPeter Gruber };
582bf6f3d74STakashi Iwai static const unsigned char lbus_play_noconv2[] = {
583109a9638SPeter Gruber 	DIGITAL_MIXER_IN1, 0xff
584109a9638SPeter Gruber };
585bf6f3d74STakashi Iwai static const unsigned char lbus_play_stereo2[] = {
586109a9638SPeter Gruber 	INTER1_IN, DIGITAL_MIXER_IN1, 0xff
587109a9638SPeter Gruber };
588bf6f3d74STakashi Iwai static const unsigned char lbus_play_mono2[] = {
589109a9638SPeter Gruber 	INTERM1_IN, DIGITAL_MIXER_IN1, 0xff
590109a9638SPeter Gruber };
591bf6f3d74STakashi Iwai static const unsigned char lbus_play_noconv3[] = {
592109a9638SPeter Gruber 	DIGITAL_MIXER_IN2, 0xff
593109a9638SPeter Gruber };
594bf6f3d74STakashi Iwai static const unsigned char lbus_play_stereo3[] = {
595109a9638SPeter Gruber 	INTER2_IN, DIGITAL_MIXER_IN2, 0xff
596109a9638SPeter Gruber };
597bf6f3d74STakashi Iwai static const unsigned char lbus_play_mono3[] = {
598109a9638SPeter Gruber 	INTERM2_IN, DIGITAL_MIXER_IN2, 0xff
599109a9638SPeter Gruber };
600bf6f3d74STakashi Iwai static const unsigned char lbus_rec_noconv1[] = {
601109a9638SPeter Gruber 	LBUS2ARM_FIFO5, 0xff
602109a9638SPeter Gruber };
603bf6f3d74STakashi Iwai static const unsigned char lbus_rec_stereo1[] = {
604109a9638SPeter Gruber 	DECIM0_IN, LBUS2ARM_FIFO5, 0xff
605109a9638SPeter Gruber };
606bf6f3d74STakashi Iwai static const unsigned char lbus_rec_mono1[] = {
607109a9638SPeter Gruber 	DECIMM3_IN, LBUS2ARM_FIFO5, 0xff
608109a9638SPeter Gruber };
609109a9638SPeter Gruber 
610bf6f3d74STakashi Iwai static const unsigned char play_ids[] = { 4, 1, 2, };
611bf6f3d74STakashi Iwai static const unsigned char play_sources[] = {
612109a9638SPeter Gruber 	ARM2LBUS_FIFO4, ARM2LBUS_FIFO1, ARM2LBUS_FIFO2,
613109a9638SPeter Gruber };
614bf6f3d74STakashi Iwai static const struct lbuspath lbus_play_paths[] = {
615109a9638SPeter Gruber 	{
616109a9638SPeter Gruber 	 .noconv = lbus_play_noconv1,
617109a9638SPeter Gruber 	 .stereo = lbus_play_stereo1,
618109a9638SPeter Gruber 	 .mono = lbus_play_mono1,
619109a9638SPeter Gruber 	 },
620109a9638SPeter Gruber 	{
621109a9638SPeter Gruber 	 .noconv = lbus_play_noconv2,
622109a9638SPeter Gruber 	 .stereo = lbus_play_stereo2,
623109a9638SPeter Gruber 	 .mono = lbus_play_mono2,
624109a9638SPeter Gruber 	 },
625109a9638SPeter Gruber 	{
626109a9638SPeter Gruber 	 .noconv = lbus_play_noconv3,
627109a9638SPeter Gruber 	 .stereo = lbus_play_stereo3,
628109a9638SPeter Gruber 	 .mono = lbus_play_mono3,
629109a9638SPeter Gruber 	 },
630109a9638SPeter Gruber };
631071f1344SJulia Lawall static const struct lbuspath lbus_rec_path = {
632109a9638SPeter Gruber 	.noconv = lbus_rec_noconv1,
633109a9638SPeter Gruber 	.stereo = lbus_rec_stereo1,
634109a9638SPeter Gruber 	.mono = lbus_rec_mono1,
635109a9638SPeter Gruber };
636109a9638SPeter Gruber 
637109a9638SPeter Gruber #define FIRMWARE_VERSIONS 1
638109a9638SPeter Gruber static union firmware_version firmware_versions[] = {
639109a9638SPeter Gruber 	{
640adf75dcaSClemens Ladisch 		.firmware = {
641adf75dcaSClemens Ladisch 			.ASIC = 3,
642adf75dcaSClemens Ladisch 			.CODEC = 2,
643adf75dcaSClemens Ladisch 			.AUXDSP = 3,
644adf75dcaSClemens Ladisch 			.PROG = 773,
645adf75dcaSClemens Ladisch 		},
646109a9638SPeter Gruber 	},
647109a9638SPeter Gruber };
648109a9638SPeter Gruber 
atoh(const unsigned char * in,unsigned int len)64993a9c901SDavid Woodhouse static u32 atoh(const unsigned char *in, unsigned int len)
650109a9638SPeter Gruber {
651109a9638SPeter Gruber 	u32 sum = 0;
652109a9638SPeter Gruber 	unsigned int mult = 1;
653109a9638SPeter Gruber 	unsigned char c;
654109a9638SPeter Gruber 
655109a9638SPeter Gruber 	while (len) {
656c9ff921aSAndy Shevchenko 		int value;
657c9ff921aSAndy Shevchenko 
658109a9638SPeter Gruber 		c = in[len - 1];
659c9ff921aSAndy Shevchenko 		value = hex_to_bin(c);
660c9ff921aSAndy Shevchenko 		if (value >= 0)
661c9ff921aSAndy Shevchenko 			sum += mult * value;
662109a9638SPeter Gruber 		mult *= 16;
663109a9638SPeter Gruber 		--len;
664109a9638SPeter Gruber 	}
665109a9638SPeter Gruber 	return sum;
666109a9638SPeter Gruber }
667109a9638SPeter Gruber 
senddata(struct cmdif * cif,const unsigned char * in,u32 offset)66893a9c901SDavid Woodhouse static int senddata(struct cmdif *cif, const unsigned char *in, u32 offset)
669109a9638SPeter Gruber {
670109a9638SPeter Gruber 	u32 addr;
671109a9638SPeter Gruber 	u32 data;
672109a9638SPeter Gruber 	u32 i;
67393a9c901SDavid Woodhouse 	const unsigned char *p;
674109a9638SPeter Gruber 
675109a9638SPeter Gruber 	i = atoh(&in[1], 2);
676109a9638SPeter Gruber 	addr = offset + atoh(&in[3], 4);
677109a9638SPeter Gruber 	if (SEND_SMEM(cif, 0, addr) != 0)
678109a9638SPeter Gruber 		return -EACCES;
679109a9638SPeter Gruber 	p = in + 9;
680109a9638SPeter Gruber 	while (i) {
681109a9638SPeter Gruber 		data = atoh(p, 8);
682109a9638SPeter Gruber 		if (SEND_WMEM(cif, 2,
683109a9638SPeter Gruber 			      ((data & 0x0f0f0f0f) << 4) | ((data & 0xf0f0f0f0)
684109a9638SPeter Gruber 							    >> 4)))
685109a9638SPeter Gruber 			return -EACCES;
686109a9638SPeter Gruber 		i -= 4;
687109a9638SPeter Gruber 		p += 8;
688109a9638SPeter Gruber 	}
689109a9638SPeter Gruber 	return 0;
690109a9638SPeter Gruber }
691109a9638SPeter Gruber 
loadfirmware(struct cmdif * cif,const unsigned char * img,unsigned int size)69293a9c901SDavid Woodhouse static int loadfirmware(struct cmdif *cif, const unsigned char *img,
693109a9638SPeter Gruber 			unsigned int size)
694109a9638SPeter Gruber {
69593a9c901SDavid Woodhouse 	const unsigned char *in;
696109a9638SPeter Gruber 	u32 laddr, saddr, t, val;
697109a9638SPeter Gruber 	int err = 0;
698109a9638SPeter Gruber 
699109a9638SPeter Gruber 	laddr = saddr = 0;
700109a9638SPeter Gruber 	while (size > 0 && err == 0) {
701109a9638SPeter Gruber 		in = img;
702109a9638SPeter Gruber 		if (in[0] == ':') {
703109a9638SPeter Gruber 			t = atoh(&in[7], 2);
704109a9638SPeter Gruber 			switch (t) {
705109a9638SPeter Gruber 			case DATA_REC:
706109a9638SPeter Gruber 				err = senddata(cif, in, laddr + saddr);
707109a9638SPeter Gruber 				break;
708109a9638SPeter Gruber 			case EXT_SEG_ADDR_REC:
709109a9638SPeter Gruber 				saddr = atoh(&in[9], 4) << 4;
710109a9638SPeter Gruber 				break;
711109a9638SPeter Gruber 			case EXT_LIN_ADDR_REC:
712109a9638SPeter Gruber 				laddr = atoh(&in[9], 4) << 16;
713109a9638SPeter Gruber 				break;
714109a9638SPeter Gruber 			case EXT_GOTO_CMD_REC:
715109a9638SPeter Gruber 				val = atoh(&in[9], 8);
716109a9638SPeter Gruber 				if (SEND_GOTO(cif, val) != 0)
717109a9638SPeter Gruber 					err = -EACCES;
718109a9638SPeter Gruber 				break;
719109a9638SPeter Gruber 			case EXT_END_OF_FILE:
720109a9638SPeter Gruber 				size = 0;
721109a9638SPeter Gruber 				break;
722109a9638SPeter Gruber 			default:
723109a9638SPeter Gruber 				break;
724109a9638SPeter Gruber 			}
725109a9638SPeter Gruber 			while (size > 0) {
726109a9638SPeter Gruber 				size--;
727109a9638SPeter Gruber 				if (*img++ == '\n')
728109a9638SPeter Gruber 					break;
729109a9638SPeter Gruber 			}
730109a9638SPeter Gruber 		}
731109a9638SPeter Gruber 	}
732109a9638SPeter Gruber 	snd_printdd("load firmware return %d\n", err);
733109a9638SPeter Gruber 	return err;
734109a9638SPeter Gruber }
735109a9638SPeter Gruber 
736109a9638SPeter Gruber static void
alloclbuspath(struct cmdif * cif,unsigned char source,const unsigned char * path,unsigned char * mixer,unsigned char * s)737109a9638SPeter Gruber alloclbuspath(struct cmdif *cif, unsigned char source,
738bf6f3d74STakashi Iwai 	      const unsigned char *path, unsigned char *mixer, unsigned char *s)
739109a9638SPeter Gruber {
740109a9638SPeter Gruber 	while (*path != 0xff) {
741109a9638SPeter Gruber 		unsigned char sink, type;
742109a9638SPeter Gruber 
743109a9638SPeter Gruber 		sink = *path & (~SPLIT_PATH);
744109a9638SPeter Gruber 		if (sink != E2SINK_MAX) {
745109a9638SPeter Gruber 			snd_printdd("alloc path 0x%x->0x%x\n", source, sink);
746109a9638SPeter Gruber 			SEND_PSEL(cif, source, sink);
747109a9638SPeter Gruber 			source = lbusin2out[sink][0];
748109a9638SPeter Gruber 			type = lbusin2out[sink][1];
749109a9638SPeter Gruber 			if (type == LS_MIXER_IN) {
750109a9638SPeter Gruber 				if (mixer)
751109a9638SPeter Gruber 					*mixer = sink - DIGITAL_MIXER_IN0;
752109a9638SPeter Gruber 			}
753109a9638SPeter Gruber 			if (type == LS_SRC_DECIMATORM ||
754109a9638SPeter Gruber 			    type == LS_SRC_DECIMATOR ||
755109a9638SPeter Gruber 			    type == LS_SRC_INTERPOLATORM ||
756109a9638SPeter Gruber 			    type == LS_SRC_INTERPOLATOR) {
757109a9638SPeter Gruber 				if (s) {
758109a9638SPeter Gruber 					if (s[0] != 0xff)
759109a9638SPeter Gruber 						s[1] = sink;
760109a9638SPeter Gruber 					else
761109a9638SPeter Gruber 						s[0] = sink;
762109a9638SPeter Gruber 				}
763109a9638SPeter Gruber 			}
764109a9638SPeter Gruber 		}
765109a9638SPeter Gruber 		if (*path++ & SPLIT_PATH) {
766bf6f3d74STakashi Iwai 			const unsigned char *npath = path;
767109a9638SPeter Gruber 
768109a9638SPeter Gruber 			while (*npath != 0xff)
769109a9638SPeter Gruber 				npath++;
770109a9638SPeter Gruber 			alloclbuspath(cif, source + 1, ++npath, mixer, s);
771109a9638SPeter Gruber 		}
772109a9638SPeter Gruber 	}
773109a9638SPeter Gruber }
774109a9638SPeter Gruber 
775109a9638SPeter Gruber static void
freelbuspath(struct cmdif * cif,unsigned char source,const unsigned char * path)776bf6f3d74STakashi Iwai freelbuspath(struct cmdif *cif, unsigned char source, const unsigned char *path)
777109a9638SPeter Gruber {
778109a9638SPeter Gruber 	while (*path != 0xff) {
779109a9638SPeter Gruber 		unsigned char sink;
780109a9638SPeter Gruber 
781109a9638SPeter Gruber 		sink = *path & (~SPLIT_PATH);
782109a9638SPeter Gruber 		if (sink != E2SINK_MAX) {
783109a9638SPeter Gruber 			snd_printdd("free path 0x%x->0x%x\n", source, sink);
784109a9638SPeter Gruber 			SEND_PCLR(cif, source, sink);
785109a9638SPeter Gruber 			source = lbusin2out[sink][0];
786109a9638SPeter Gruber 		}
787109a9638SPeter Gruber 		if (*path++ & SPLIT_PATH) {
788bf6f3d74STakashi Iwai 			const unsigned char *npath = path;
789109a9638SPeter Gruber 
790109a9638SPeter Gruber 			while (*npath != 0xff)
791109a9638SPeter Gruber 				npath++;
792109a9638SPeter Gruber 			freelbuspath(cif, source + 1, ++npath);
793109a9638SPeter Gruber 		}
794109a9638SPeter Gruber 	}
795109a9638SPeter Gruber }
796109a9638SPeter Gruber 
writearm(struct cmdif * cif,u32 addr,u32 data,u32 mask)797109a9638SPeter Gruber static int writearm(struct cmdif *cif, u32 addr, u32 data, u32 mask)
798109a9638SPeter Gruber {
799109a9638SPeter Gruber 	union cmdret rptr = CMDRET_ZERO;
800109a9638SPeter Gruber 	unsigned int i = MAX_WRITE_RETRY;
801109a9638SPeter Gruber 	int flag = 1;
802109a9638SPeter Gruber 
803109a9638SPeter Gruber 	SEND_RMEM(cif, 0x02, addr, &rptr);
804109a9638SPeter Gruber 	rptr.retlongs[0] &= (~mask);
805109a9638SPeter Gruber 
806109a9638SPeter Gruber 	while (--i) {
807109a9638SPeter Gruber 		SEND_SMEM(cif, 0x01, addr);
808109a9638SPeter Gruber 		SEND_WMEM(cif, 0x02, (rptr.retlongs[0] | data));
809109a9638SPeter Gruber 		SEND_RMEM(cif, 0x02, addr, &rptr);
810109a9638SPeter Gruber 		if ((rptr.retlongs[0] & data) == data) {
811109a9638SPeter Gruber 			flag = 0;
812109a9638SPeter Gruber 			break;
813109a9638SPeter Gruber 		} else
814109a9638SPeter Gruber 			rptr.retlongs[0] &= ~mask;
815109a9638SPeter Gruber 	}
816109a9638SPeter Gruber 	snd_printdd("send arm 0x%x 0x%x 0x%x return %d\n", addr, data, mask,
817109a9638SPeter Gruber 		    flag);
818109a9638SPeter Gruber 	return flag;
819109a9638SPeter Gruber }
820109a9638SPeter Gruber 
sendcmd(struct cmdif * cif,u32 flags,u32 cmd,u32 parm,union cmdret * ret)821109a9638SPeter Gruber static int sendcmd(struct cmdif *cif, u32 flags, u32 cmd, u32 parm,
822109a9638SPeter Gruber 		   union cmdret *ret)
823109a9638SPeter Gruber {
824109a9638SPeter Gruber 	int i, j;
825109a9638SPeter Gruber 	int err;
826109a9638SPeter Gruber 	unsigned int time = 0;
827109a9638SPeter Gruber 	unsigned long irqflags;
828109a9638SPeter Gruber 	struct riptideport *hwport;
829109a9638SPeter Gruber 	struct cmdport *cmdport = NULL;
830109a9638SPeter Gruber 
831da3cec35STakashi Iwai 	if (snd_BUG_ON(!cif))
832da3cec35STakashi Iwai 		return -EINVAL;
833109a9638SPeter Gruber 
834109a9638SPeter Gruber 	hwport = cif->hwport;
835109a9638SPeter Gruber 	if (cif->errcnt > MAX_ERROR_COUNT) {
836109a9638SPeter Gruber 		if (cif->is_reset) {
837109a9638SPeter Gruber 			snd_printk(KERN_ERR
838109a9638SPeter Gruber 				   "Riptide: Too many failed cmds, reinitializing\n");
839109a9638SPeter Gruber 			if (riptide_reset(cif, NULL) == 0) {
840109a9638SPeter Gruber 				cif->errcnt = 0;
841109a9638SPeter Gruber 				return -EIO;
842109a9638SPeter Gruber 			}
843109a9638SPeter Gruber 		}
844109a9638SPeter Gruber 		snd_printk(KERN_ERR "Riptide: Initialization failed.\n");
845109a9638SPeter Gruber 		return -EINVAL;
846109a9638SPeter Gruber 	}
847109a9638SPeter Gruber 	if (ret) {
848109a9638SPeter Gruber 		ret->retlongs[0] = 0;
849109a9638SPeter Gruber 		ret->retlongs[1] = 0;
850109a9638SPeter Gruber 	}
851109a9638SPeter Gruber 	i = 0;
852109a9638SPeter Gruber 	spin_lock_irqsave(&cif->lock, irqflags);
853109a9638SPeter Gruber 	while (i++ < CMDIF_TIMEOUT && !IS_READY(cif->hwport))
854109a9638SPeter Gruber 		udelay(10);
8551b1cc7f2SRoel Kluin 	if (i > CMDIF_TIMEOUT) {
856109a9638SPeter Gruber 		err = -EBUSY;
857109a9638SPeter Gruber 		goto errout;
858109a9638SPeter Gruber 	}
859109a9638SPeter Gruber 
860109a9638SPeter Gruber 	err = 0;
861109a9638SPeter Gruber 	for (j = 0, time = 0; time < CMDIF_TIMEOUT; j++, time += 2) {
862109a9638SPeter Gruber 		cmdport = &(hwport->port[j % 2]);
863109a9638SPeter Gruber 		if (IS_DATF(cmdport)) {	/* free pending data */
864109a9638SPeter Gruber 			READ_PORT_ULONG(cmdport->data1);
865109a9638SPeter Gruber 			READ_PORT_ULONG(cmdport->data2);
866109a9638SPeter Gruber 		}
867109a9638SPeter Gruber 		if (IS_CMDE(cmdport)) {
868109a9638SPeter Gruber 			if (flags & PARM)	/* put data */
869109a9638SPeter Gruber 				WRITE_PORT_ULONG(cmdport->data2, parm);
870109a9638SPeter Gruber 			WRITE_PORT_ULONG(cmdport->data1, cmd);	/* write cmd */
871109a9638SPeter Gruber 			if ((flags & RESP) && ret) {
872109a9638SPeter Gruber 				while (!IS_DATF(cmdport) &&
8731b1cc7f2SRoel Kluin 				       time < CMDIF_TIMEOUT) {
874109a9638SPeter Gruber 					udelay(10);
8751b1cc7f2SRoel Kluin 					time++;
8761b1cc7f2SRoel Kluin 				}
877109a9638SPeter Gruber 				if (time < CMDIF_TIMEOUT) {	/* read response */
878109a9638SPeter Gruber 					ret->retlongs[0] =
879109a9638SPeter Gruber 					    READ_PORT_ULONG(cmdport->data1);
880109a9638SPeter Gruber 					ret->retlongs[1] =
881109a9638SPeter Gruber 					    READ_PORT_ULONG(cmdport->data2);
882109a9638SPeter Gruber 				} else {
883109a9638SPeter Gruber 					err = -ENOSYS;
884109a9638SPeter Gruber 					goto errout;
885109a9638SPeter Gruber 				}
886109a9638SPeter Gruber 			}
887109a9638SPeter Gruber 			break;
888109a9638SPeter Gruber 		}
889109a9638SPeter Gruber 		udelay(20);
890109a9638SPeter Gruber 	}
891109a9638SPeter Gruber 	if (time == CMDIF_TIMEOUT) {
892109a9638SPeter Gruber 		err = -ENODATA;
893109a9638SPeter Gruber 		goto errout;
894109a9638SPeter Gruber 	}
895109a9638SPeter Gruber 	spin_unlock_irqrestore(&cif->lock, irqflags);
896109a9638SPeter Gruber 
897109a9638SPeter Gruber 	cif->cmdcnt++;		/* update command statistics */
898109a9638SPeter Gruber 	cif->cmdtime += time;
899109a9638SPeter Gruber 	if (time > cif->cmdtimemax)
900109a9638SPeter Gruber 		cif->cmdtimemax = time;
901109a9638SPeter Gruber 	if (time < cif->cmdtimemin)
902109a9638SPeter Gruber 		cif->cmdtimemin = time;
903109a9638SPeter Gruber 	if ((cif->cmdcnt) % 1000 == 0)
904109a9638SPeter Gruber 		snd_printdd
905109a9638SPeter Gruber 		    ("send cmd %d time: %d mintime: %d maxtime %d err: %d\n",
906109a9638SPeter Gruber 		     cif->cmdcnt, cif->cmdtime, cif->cmdtimemin,
907109a9638SPeter Gruber 		     cif->cmdtimemax, cif->errcnt);
908109a9638SPeter Gruber 	return 0;
909109a9638SPeter Gruber 
910109a9638SPeter Gruber       errout:
911109a9638SPeter Gruber 	cif->errcnt++;
912109a9638SPeter Gruber 	spin_unlock_irqrestore(&cif->lock, irqflags);
913109a9638SPeter Gruber 	snd_printdd
914109a9638SPeter 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",
915109a9638SPeter Gruber 	     cif->cmdcnt, (int)((void *)&(cmdport->stat) - (void *)hwport),
916109a9638SPeter Gruber 	     flags, cmd, parm, ret ? ret->retlongs[0] : 0,
917109a9638SPeter Gruber 	     ret ? ret->retlongs[1] : 0, IS_CMDE(cmdport), IS_DATF(cmdport),
918109a9638SPeter Gruber 	     err);
919109a9638SPeter Gruber 	return err;
920109a9638SPeter Gruber }
921109a9638SPeter Gruber 
922109a9638SPeter Gruber static int
setmixer(struct cmdif * cif,short num,unsigned short rval,unsigned short lval)923109a9638SPeter Gruber setmixer(struct cmdif *cif, short num, unsigned short rval, unsigned short lval)
924109a9638SPeter Gruber {
925109a9638SPeter Gruber 	union cmdret rptr = CMDRET_ZERO;
926109a9638SPeter Gruber 	int i = 0;
927109a9638SPeter Gruber 
92847efe636SHans Wennborg 	snd_printdd("sent mixer %d: 0x%x 0x%x\n", num, rval, lval);
929109a9638SPeter Gruber 	do {
930109a9638SPeter Gruber 		SEND_SDGV(cif, num, num, rval, lval);
931109a9638SPeter Gruber 		SEND_RDGV(cif, num, num, &rptr);
932109a9638SPeter Gruber 		if (rptr.retwords[0] == lval && rptr.retwords[1] == rval)
933109a9638SPeter Gruber 			return 0;
934109a9638SPeter Gruber 	} while (i++ < MAX_WRITE_RETRY);
935109a9638SPeter Gruber 	snd_printdd("sent mixer failed\n");
936109a9638SPeter Gruber 	return -EIO;
937109a9638SPeter Gruber }
938109a9638SPeter Gruber 
getpaths(struct cmdif * cif,unsigned char * o)939109a9638SPeter Gruber static int getpaths(struct cmdif *cif, unsigned char *o)
940109a9638SPeter Gruber {
941109a9638SPeter Gruber 	unsigned char src[E2SINK_MAX];
942109a9638SPeter Gruber 	unsigned char sink[E2SINK_MAX];
943109a9638SPeter Gruber 	int i, j = 0;
944109a9638SPeter Gruber 
945109a9638SPeter Gruber 	for (i = 0; i < E2SINK_MAX; i++) {
946109a9638SPeter Gruber 		getsourcesink(cif, i, i, &src[i], &sink[i]);
947109a9638SPeter Gruber 		if (sink[i] < E2SINK_MAX) {
948109a9638SPeter Gruber 			o[j++] = sink[i];
949109a9638SPeter Gruber 			o[j++] = i;
950109a9638SPeter Gruber 		}
951109a9638SPeter Gruber 	}
952109a9638SPeter Gruber 	return j;
953109a9638SPeter Gruber }
954109a9638SPeter Gruber 
955109a9638SPeter Gruber static int
getsourcesink(struct cmdif * cif,unsigned char source,unsigned char sink,unsigned char * a,unsigned char * b)956109a9638SPeter Gruber getsourcesink(struct cmdif *cif, unsigned char source, unsigned char sink,
957109a9638SPeter Gruber 	      unsigned char *a, unsigned char *b)
958109a9638SPeter Gruber {
959109a9638SPeter Gruber 	union cmdret rptr = CMDRET_ZERO;
960109a9638SPeter Gruber 
961109a9638SPeter Gruber 	if (SEND_RSSV(cif, source, sink, &rptr) &&
962109a9638SPeter Gruber 	    SEND_RSSV(cif, source, sink, &rptr))
963109a9638SPeter Gruber 		return -EIO;
964109a9638SPeter Gruber 	*a = rptr.retbytes[0];
965109a9638SPeter Gruber 	*b = rptr.retbytes[1];
966109a9638SPeter Gruber 	snd_printdd("getsourcesink 0x%x 0x%x\n", *a, *b);
967109a9638SPeter Gruber 	return 0;
968109a9638SPeter Gruber }
969109a9638SPeter Gruber 
970109a9638SPeter Gruber static int
getsamplerate(struct cmdif * cif,unsigned char * intdec,unsigned int * rate)971109a9638SPeter Gruber getsamplerate(struct cmdif *cif, unsigned char *intdec, unsigned int *rate)
972109a9638SPeter Gruber {
973109a9638SPeter Gruber 	unsigned char *s;
974109a9638SPeter Gruber 	unsigned int p[2] = { 0, 0 };
975109a9638SPeter Gruber 	int i;
976109a9638SPeter Gruber 	union cmdret rptr = CMDRET_ZERO;
977109a9638SPeter Gruber 
978109a9638SPeter Gruber 	s = intdec;
979109a9638SPeter Gruber 	for (i = 0; i < 2; i++) {
980109a9638SPeter Gruber 		if (*s != 0xff) {
981109a9638SPeter Gruber 			if (SEND_RSRC(cif, *s, &rptr) &&
982109a9638SPeter Gruber 			    SEND_RSRC(cif, *s, &rptr))
983109a9638SPeter Gruber 				return -EIO;
984109a9638SPeter Gruber 			p[i] += rptr.retwords[1];
985109a9638SPeter Gruber 			p[i] *= rptr.retwords[2];
986109a9638SPeter Gruber 			p[i] += rptr.retwords[3];
987109a9638SPeter Gruber 			p[i] /= 65536;
988109a9638SPeter Gruber 		}
989109a9638SPeter Gruber 		s++;
990109a9638SPeter Gruber 	}
991109a9638SPeter Gruber 	if (p[0]) {
992109a9638SPeter Gruber 		if (p[1] != p[0])
993109a9638SPeter Gruber 			snd_printdd("rates differ %d %d\n", p[0], p[1]);
994109a9638SPeter Gruber 		*rate = (unsigned int)p[0];
995109a9638SPeter Gruber 	} else
996109a9638SPeter Gruber 		*rate = (unsigned int)p[1];
997109a9638SPeter Gruber 	snd_printdd("getsampleformat %d %d %d\n", intdec[0], intdec[1], *rate);
998109a9638SPeter Gruber 	return 0;
999109a9638SPeter Gruber }
1000109a9638SPeter Gruber 
1001109a9638SPeter Gruber static int
setsampleformat(struct cmdif * cif,unsigned char mixer,unsigned char id,unsigned char channels,snd_pcm_format_t format)1002109a9638SPeter Gruber setsampleformat(struct cmdif *cif,
1003109a9638SPeter Gruber 		unsigned char mixer, unsigned char id,
100494dfee0cSTakashi Iwai 		unsigned char channels, snd_pcm_format_t format)
1005109a9638SPeter Gruber {
1006109a9638SPeter Gruber 	unsigned char w, ch, sig, order;
1007109a9638SPeter Gruber 
1008109a9638SPeter Gruber 	snd_printdd
1009109a9638SPeter Gruber 	    ("setsampleformat mixer: %d id: %d channels: %d format: %d\n",
1010109a9638SPeter Gruber 	     mixer, id, channels, format);
1011109a9638SPeter Gruber 	ch = channels == 1;
1012109a9638SPeter Gruber 	w = snd_pcm_format_width(format) == 8;
1013109a9638SPeter Gruber 	sig = snd_pcm_format_unsigned(format) != 0;
1014109a9638SPeter Gruber 	order = snd_pcm_format_big_endian(format) != 0;
1015109a9638SPeter Gruber 
1016109a9638SPeter Gruber 	if (SEND_SETF(cif, mixer, w, ch, order, sig, id) &&
1017109a9638SPeter Gruber 	    SEND_SETF(cif, mixer, w, ch, order, sig, id)) {
1018109a9638SPeter Gruber 		snd_printdd("setsampleformat failed\n");
1019109a9638SPeter Gruber 		return -EIO;
1020109a9638SPeter Gruber 	}
1021109a9638SPeter Gruber 	return 0;
1022109a9638SPeter Gruber }
1023109a9638SPeter Gruber 
1024109a9638SPeter Gruber static int
setsamplerate(struct cmdif * cif,unsigned char * intdec,unsigned int rate)1025109a9638SPeter Gruber setsamplerate(struct cmdif *cif, unsigned char *intdec, unsigned int rate)
1026109a9638SPeter Gruber {
1027109a9638SPeter Gruber 	u32 D, M, N;
1028109a9638SPeter Gruber 	union cmdret rptr = CMDRET_ZERO;
1029109a9638SPeter Gruber 	int i;
1030109a9638SPeter Gruber 
1031109a9638SPeter Gruber 	snd_printdd("setsamplerate intdec: %d,%d rate: %d\n", intdec[0],
1032109a9638SPeter Gruber 		    intdec[1], rate);
1033109a9638SPeter Gruber 	D = 48000;
1034109a9638SPeter Gruber 	M = ((rate == 48000) ? 47999 : rate) * 65536;
1035109a9638SPeter Gruber 	N = M % D;
1036109a9638SPeter Gruber 	M /= D;
1037109a9638SPeter Gruber 	for (i = 0; i < 2; i++) {
1038109a9638SPeter Gruber 		if (*intdec != 0xff) {
1039109a9638SPeter Gruber 			do {
1040109a9638SPeter Gruber 				SEND_SSRC(cif, *intdec, D, M, N);
1041109a9638SPeter Gruber 				SEND_RSRC(cif, *intdec, &rptr);
1042109a9638SPeter Gruber 			} while (rptr.retwords[1] != D &&
1043109a9638SPeter Gruber 				 rptr.retwords[2] != M &&
1044109a9638SPeter Gruber 				 rptr.retwords[3] != N &&
1045109a9638SPeter Gruber 				 i++ < MAX_WRITE_RETRY);
10469980c620SRoel Kluin 			if (i > MAX_WRITE_RETRY) {
1047109a9638SPeter Gruber 				snd_printdd("sent samplerate %d: %d failed\n",
1048109a9638SPeter Gruber 					    *intdec, rate);
1049109a9638SPeter Gruber 				return -EIO;
1050109a9638SPeter Gruber 			}
1051109a9638SPeter Gruber 		}
1052109a9638SPeter Gruber 		intdec++;
1053109a9638SPeter Gruber 	}
1054109a9638SPeter Gruber 	return 0;
1055109a9638SPeter Gruber }
1056109a9638SPeter Gruber 
1057109a9638SPeter Gruber static int
getmixer(struct cmdif * cif,short num,unsigned short * rval,unsigned short * lval)1058109a9638SPeter Gruber getmixer(struct cmdif *cif, short num, unsigned short *rval,
1059109a9638SPeter Gruber 	 unsigned short *lval)
1060109a9638SPeter Gruber {
1061109a9638SPeter Gruber 	union cmdret rptr = CMDRET_ZERO;
1062109a9638SPeter Gruber 
1063109a9638SPeter Gruber 	if (SEND_RDGV(cif, num, num, &rptr) && SEND_RDGV(cif, num, num, &rptr))
1064109a9638SPeter Gruber 		return -EIO;
1065109a9638SPeter Gruber 	*rval = rptr.retwords[0];
1066109a9638SPeter Gruber 	*lval = rptr.retwords[1];
106747efe636SHans Wennborg 	snd_printdd("got mixer %d: 0x%x 0x%x\n", num, *rval, *lval);
1068109a9638SPeter Gruber 	return 0;
1069109a9638SPeter Gruber }
1070109a9638SPeter Gruber 
riptide_handleirq(int irq,void * dev_id)10712ac55dafSTakashi Iwai static irqreturn_t riptide_handleirq(int irq, void *dev_id)
1072109a9638SPeter Gruber {
10732ac55dafSTakashi Iwai 	struct snd_riptide *chip = dev_id;
1074109a9638SPeter Gruber 	struct cmdif *cif = chip->cif;
1075109a9638SPeter Gruber 	struct snd_pcm_substream *substream[PLAYBACK_SUBSTREAMS + 1];
1076109a9638SPeter Gruber 	struct snd_pcm_runtime *runtime;
1077109a9638SPeter Gruber 	struct pcmhw *data = NULL;
1078109a9638SPeter Gruber 	unsigned int pos, period_bytes;
1079109a9638SPeter Gruber 	struct sgd *c;
1080109a9638SPeter Gruber 	int i, j;
1081109a9638SPeter Gruber 	unsigned int flag;
1082109a9638SPeter Gruber 
1083109a9638SPeter Gruber 	if (!cif)
10842ac55dafSTakashi Iwai 		return IRQ_HANDLED;
1085109a9638SPeter Gruber 
1086109a9638SPeter Gruber 	for (i = 0; i < PLAYBACK_SUBSTREAMS; i++)
1087109a9638SPeter Gruber 		substream[i] = chip->playback_substream[i];
1088109a9638SPeter Gruber 	substream[i] = chip->capture_substream;
1089109a9638SPeter Gruber 	for (i = 0; i < PLAYBACK_SUBSTREAMS + 1; i++) {
109028c07099STakashi Iwai 		if (!substream[i])
109128c07099STakashi Iwai 			continue;
109228c07099STakashi Iwai 		runtime = substream[i]->runtime;
109328c07099STakashi Iwai 		if (!runtime)
109428c07099STakashi Iwai 			continue;
109528c07099STakashi Iwai 		data = runtime->private_data;
109628c07099STakashi Iwai 		if (!data)
109728c07099STakashi Iwai 			continue;
109828c07099STakashi Iwai 		if (data->state != ST_STOP) {
1099109a9638SPeter Gruber 			pos = 0;
1100109a9638SPeter Gruber 			for (j = 0; j < data->pages; j++) {
1101109a9638SPeter Gruber 				c = &data->sgdbuf[j];
1102109a9638SPeter Gruber 				flag = le32_to_cpu(c->dwStat_Ctl);
1103109a9638SPeter Gruber 				if (flag & EOB_STATUS)
1104109a9638SPeter Gruber 					pos += le32_to_cpu(c->dwSegLen);
1105109a9638SPeter Gruber 				if (flag & EOC_STATUS)
1106109a9638SPeter Gruber 					pos += le32_to_cpu(c->dwSegLen);
1107109a9638SPeter Gruber 				if ((flag & EOS_STATUS)
1108109a9638SPeter Gruber 				    && (data->state == ST_PLAY)) {
1109109a9638SPeter Gruber 					data->state = ST_STOP;
1110109a9638SPeter Gruber 					snd_printk(KERN_ERR
1111109a9638SPeter Gruber 						   "Riptide: DMA stopped unexpectedly\n");
1112109a9638SPeter Gruber 				}
1113109a9638SPeter Gruber 				c->dwStat_Ctl =
1114109a9638SPeter Gruber 				    cpu_to_le32(flag &
1115109a9638SPeter Gruber 						~(EOS_STATUS | EOB_STATUS |
1116109a9638SPeter Gruber 						  EOC_STATUS));
1117109a9638SPeter Gruber 			}
1118109a9638SPeter Gruber 			data->pointer += pos;
1119109a9638SPeter Gruber 			pos += data->oldpos;
1120109a9638SPeter Gruber 			if (data->state != ST_STOP) {
1121109a9638SPeter Gruber 				period_bytes =
1122109a9638SPeter Gruber 				    frames_to_bytes(runtime,
1123109a9638SPeter Gruber 						    runtime->period_size);
1124109a9638SPeter Gruber 				snd_printdd
1125109a9638SPeter Gruber 				    ("interrupt 0x%x after 0x%lx of 0x%lx frames in period\n",
1126109a9638SPeter Gruber 				     READ_AUDIO_STATUS(cif->hwport),
1127109a9638SPeter Gruber 				     bytes_to_frames(runtime, pos),
1128109a9638SPeter Gruber 				     runtime->period_size);
1129109a9638SPeter Gruber 				j = 0;
1130109a9638SPeter Gruber 				if (pos >= period_bytes) {
1131109a9638SPeter Gruber 					j++;
1132109a9638SPeter Gruber 					while (pos >= period_bytes)
1133109a9638SPeter Gruber 						pos -= period_bytes;
1134109a9638SPeter Gruber 				}
1135109a9638SPeter Gruber 				data->oldpos = pos;
1136109a9638SPeter Gruber 				if (j > 0)
1137109a9638SPeter Gruber 					snd_pcm_period_elapsed(substream[i]);
1138109a9638SPeter Gruber 			}
1139109a9638SPeter Gruber 		}
1140109a9638SPeter Gruber 	}
11412ac55dafSTakashi Iwai 
11422ac55dafSTakashi Iwai 	return IRQ_HANDLED;
1143109a9638SPeter Gruber }
1144109a9638SPeter Gruber 
1145c7561cd8STakashi Iwai #ifdef CONFIG_PM_SLEEP
riptide_suspend(struct device * dev)114668cb2b55STakashi Iwai static int riptide_suspend(struct device *dev)
1147109a9638SPeter Gruber {
114868cb2b55STakashi Iwai 	struct snd_card *card = dev_get_drvdata(dev);
1149109a9638SPeter Gruber 	struct snd_riptide *chip = card->private_data;
1150109a9638SPeter Gruber 
1151109a9638SPeter Gruber 	chip->in_suspend = 1;
1152109a9638SPeter Gruber 	snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
1153109a9638SPeter Gruber 	snd_ac97_suspend(chip->ac97);
1154109a9638SPeter Gruber 	return 0;
1155109a9638SPeter Gruber }
1156109a9638SPeter Gruber 
riptide_resume(struct device * dev)115768cb2b55STakashi Iwai static int riptide_resume(struct device *dev)
1158109a9638SPeter Gruber {
115968cb2b55STakashi Iwai 	struct snd_card *card = dev_get_drvdata(dev);
1160109a9638SPeter Gruber 	struct snd_riptide *chip = card->private_data;
1161109a9638SPeter Gruber 
1162109a9638SPeter Gruber 	snd_riptide_initialize(chip);
1163109a9638SPeter Gruber 	snd_ac97_resume(chip->ac97);
1164109a9638SPeter Gruber 	snd_power_change_state(card, SNDRV_CTL_POWER_D0);
1165109a9638SPeter Gruber 	chip->in_suspend = 0;
1166109a9638SPeter Gruber 	return 0;
1167109a9638SPeter Gruber }
116868cb2b55STakashi Iwai 
116968cb2b55STakashi Iwai static SIMPLE_DEV_PM_OPS(riptide_pm, riptide_suspend, riptide_resume);
117068cb2b55STakashi Iwai #define RIPTIDE_PM_OPS	&riptide_pm
117168cb2b55STakashi Iwai #else
117268cb2b55STakashi Iwai #define RIPTIDE_PM_OPS	NULL
1173c7561cd8STakashi Iwai #endif /* CONFIG_PM_SLEEP */
1174109a9638SPeter Gruber 
try_to_load_firmware(struct cmdif * cif,struct snd_riptide * chip)1175a693a26fSTakashi Iwai static int try_to_load_firmware(struct cmdif *cif, struct snd_riptide *chip)
1176109a9638SPeter Gruber {
1177a693a26fSTakashi Iwai 	union firmware_version firmware = { .ret = CMDRET_ZERO };
1178a693a26fSTakashi Iwai 	int i, timeout, err;
1179109a9638SPeter Gruber 
1180109a9638SPeter Gruber 	for (i = 0; i < 2; i++) {
1181109a9638SPeter Gruber 		WRITE_PORT_ULONG(cif->hwport->port[i].data1, 0);
1182109a9638SPeter Gruber 		WRITE_PORT_ULONG(cif->hwport->port[i].data2, 0);
1183109a9638SPeter Gruber 	}
1184109a9638SPeter Gruber 	SET_GRESET(cif->hwport);
1185109a9638SPeter Gruber 	udelay(100);
1186109a9638SPeter Gruber 	UNSET_GRESET(cif->hwport);
1187109a9638SPeter Gruber 	udelay(100);
1188109a9638SPeter Gruber 
1189109a9638SPeter Gruber 	for (timeout = 100000; --timeout; udelay(10)) {
1190109a9638SPeter Gruber 		if (IS_READY(cif->hwport) && !IS_GERR(cif->hwport))
1191109a9638SPeter Gruber 			break;
1192109a9638SPeter Gruber 	}
1193a693a26fSTakashi Iwai 	if (!timeout) {
1194109a9638SPeter Gruber 		snd_printk(KERN_ERR
1195a693a26fSTakashi Iwai 			   "Riptide: device not ready, audio status: 0x%x "
1196a693a26fSTakashi Iwai 			   "ready: %d gerr: %d\n",
1197109a9638SPeter Gruber 			   READ_AUDIO_STATUS(cif->hwport),
1198109a9638SPeter Gruber 			   IS_READY(cif->hwport), IS_GERR(cif->hwport));
1199109a9638SPeter Gruber 		return -EIO;
1200109a9638SPeter Gruber 	} else {
1201109a9638SPeter Gruber 		snd_printdd
1202109a9638SPeter Gruber 			("Riptide: audio status: 0x%x ready: %d gerr: %d\n",
1203109a9638SPeter Gruber 			 READ_AUDIO_STATUS(cif->hwport),
1204109a9638SPeter Gruber 			 IS_READY(cif->hwport), IS_GERR(cif->hwport));
1205109a9638SPeter Gruber 	}
1206109a9638SPeter Gruber 
1207a693a26fSTakashi Iwai 	SEND_GETV(cif, &firmware.ret);
1208a693a26fSTakashi Iwai 	snd_printdd("Firmware version: ASIC: %d CODEC %d AUXDSP %d PROG %d\n",
1209109a9638SPeter Gruber 		    firmware.firmware.ASIC, firmware.firmware.CODEC,
1210109a9638SPeter Gruber 		    firmware.firmware.AUXDSP, firmware.firmware.PROG);
1211109a9638SPeter Gruber 
1212a5ba6bebSTakashi Iwai 	if (!chip)
1213a5ba6bebSTakashi Iwai 		return 1;
1214a5ba6bebSTakashi Iwai 
1215a693a26fSTakashi Iwai 	for (i = 0; i < FIRMWARE_VERSIONS; i++) {
1216a693a26fSTakashi Iwai 		if (!memcmp(&firmware_versions[i], &firmware, sizeof(firmware)))
1217a693a26fSTakashi Iwai 			return 1; /* OK */
1218a693a26fSTakashi Iwai 
1219a5ba6bebSTakashi Iwai 	}
1220a5ba6bebSTakashi Iwai 
1221109a9638SPeter Gruber 	snd_printdd("Writing Firmware\n");
1222109a9638SPeter Gruber 	if (!chip->fw_entry) {
1223a693a26fSTakashi Iwai 		err = request_firmware(&chip->fw_entry, "riptide.hex",
1224a693a26fSTakashi Iwai 				       &chip->pci->dev);
1225a693a26fSTakashi Iwai 		if (err) {
1226109a9638SPeter Gruber 			snd_printk(KERN_ERR
1227a693a26fSTakashi Iwai 				   "Riptide: Firmware not available %d\n", err);
1228109a9638SPeter Gruber 			return -EIO;
1229109a9638SPeter Gruber 		}
1230109a9638SPeter Gruber 	}
1231a693a26fSTakashi Iwai 	err = loadfirmware(cif, chip->fw_entry->data, chip->fw_entry->size);
1232a693a26fSTakashi Iwai 	if (err) {
1233109a9638SPeter Gruber 		snd_printk(KERN_ERR
1234a693a26fSTakashi Iwai 			   "Riptide: Could not load firmware %d\n", err);
1235a693a26fSTakashi Iwai 		return err;
1236109a9638SPeter Gruber 	}
1237a693a26fSTakashi Iwai 
1238a693a26fSTakashi Iwai 	chip->firmware = firmware;
1239a693a26fSTakashi Iwai 
1240a693a26fSTakashi Iwai 	return 1; /* OK */
1241109a9638SPeter Gruber }
1242109a9638SPeter Gruber 
riptide_reset(struct cmdif * cif,struct snd_riptide * chip)1243a693a26fSTakashi Iwai static int riptide_reset(struct cmdif *cif, struct snd_riptide *chip)
1244a693a26fSTakashi Iwai {
1245a693a26fSTakashi Iwai 	union cmdret rptr = CMDRET_ZERO;
1246a693a26fSTakashi Iwai 	int err, tries;
1247a693a26fSTakashi Iwai 
1248a693a26fSTakashi Iwai 	if (!cif)
1249a693a26fSTakashi Iwai 		return -EINVAL;
1250a693a26fSTakashi Iwai 
1251a693a26fSTakashi Iwai 	cif->cmdcnt = 0;
1252a693a26fSTakashi Iwai 	cif->cmdtime = 0;
1253a693a26fSTakashi Iwai 	cif->cmdtimemax = 0;
1254a693a26fSTakashi Iwai 	cif->cmdtimemin = 0xffffffff;
1255a693a26fSTakashi Iwai 	cif->errcnt = 0;
1256a693a26fSTakashi Iwai 	cif->is_reset = 0;
1257a693a26fSTakashi Iwai 
1258a693a26fSTakashi Iwai 	tries = RESET_TRIES;
1259a693a26fSTakashi Iwai 	do {
1260a693a26fSTakashi Iwai 		err = try_to_load_firmware(cif, chip);
1261a693a26fSTakashi Iwai 		if (err < 0)
1262a693a26fSTakashi Iwai 			return err;
1263a693a26fSTakashi Iwai 	} while (!err && --tries);
1264a693a26fSTakashi Iwai 
1265109a9638SPeter Gruber 	SEND_SACR(cif, 0, AC97_RESET);
1266109a9638SPeter Gruber 	SEND_RACR(cif, AC97_RESET, &rptr);
1267109a9638SPeter Gruber 	snd_printdd("AC97: 0x%x 0x%x\n", rptr.retlongs[0], rptr.retlongs[1]);
1268109a9638SPeter Gruber 
1269109a9638SPeter Gruber 	SEND_PLST(cif, 0);
1270109a9638SPeter Gruber 	SEND_SLST(cif, 0);
1271109a9638SPeter Gruber 	SEND_DLST(cif, 0);
1272109a9638SPeter Gruber 	SEND_ALST(cif, 0);
1273109a9638SPeter Gruber 	SEND_KDMA(cif);
1274109a9638SPeter Gruber 
1275109a9638SPeter Gruber 	writearm(cif, 0x301F8, 1, 1);
1276109a9638SPeter Gruber 	writearm(cif, 0x301F4, 1, 1);
1277109a9638SPeter Gruber 
1278109a9638SPeter Gruber 	SEND_LSEL(cif, MODEM_CMD, 0, 0, MODEM_INTDEC, MODEM_MERGER,
1279109a9638SPeter Gruber 		  MODEM_SPLITTER, MODEM_MIXER);
1280109a9638SPeter Gruber 	setmixer(cif, MODEM_MIXER, 0x7fff, 0x7fff);
1281109a9638SPeter Gruber 	alloclbuspath(cif, ARM2LBUS_FIFO13, lbus_play_modem, NULL, NULL);
1282109a9638SPeter Gruber 
1283109a9638SPeter Gruber 	SEND_LSEL(cif, FM_CMD, 0, 0, FM_INTDEC, FM_MERGER, FM_SPLITTER,
1284109a9638SPeter Gruber 		  FM_MIXER);
1285109a9638SPeter Gruber 	setmixer(cif, FM_MIXER, 0x7fff, 0x7fff);
1286109a9638SPeter Gruber 	writearm(cif, 0x30648 + FM_MIXER * 4, 0x01, 0x00000005);
1287109a9638SPeter Gruber 	writearm(cif, 0x301A8, 0x02, 0x00000002);
1288109a9638SPeter Gruber 	writearm(cif, 0x30264, 0x08, 0xffffffff);
1289109a9638SPeter Gruber 	alloclbuspath(cif, OPL3_SAMPLE, lbus_play_opl3, NULL, NULL);
1290109a9638SPeter Gruber 
1291109a9638SPeter Gruber 	SEND_SSRC(cif, I2S_INTDEC, 48000,
1292109a9638SPeter Gruber 		  ((u32) I2S_RATE * 65536) / 48000,
1293109a9638SPeter Gruber 		  ((u32) I2S_RATE * 65536) % 48000);
1294109a9638SPeter Gruber 	SEND_LSEL(cif, I2S_CMD0, 0, 0, I2S_INTDEC, I2S_MERGER, I2S_SPLITTER,
1295109a9638SPeter Gruber 		  I2S_MIXER);
1296109a9638SPeter Gruber 	SEND_SI2S(cif, 1);
1297109a9638SPeter Gruber 	alloclbuspath(cif, ARM2LBUS_FIFO0, lbus_play_i2s, NULL, NULL);
1298109a9638SPeter Gruber 	alloclbuspath(cif, DIGITAL_MIXER_OUT0, lbus_play_out, NULL, NULL);
1299109a9638SPeter Gruber 	alloclbuspath(cif, DIGITAL_MIXER_OUT0, lbus_play_outhp, NULL, NULL);
1300109a9638SPeter Gruber 
1301109a9638SPeter Gruber 	SET_AIACK(cif->hwport);
1302109a9638SPeter Gruber 	SET_AIE(cif->hwport);
1303109a9638SPeter Gruber 	SET_AIACK(cif->hwport);
1304109a9638SPeter Gruber 	cif->is_reset = 1;
1305109a9638SPeter Gruber 
1306109a9638SPeter Gruber 	return 0;
1307109a9638SPeter Gruber }
1308109a9638SPeter Gruber 
130914ff46fdSBhumika Goyal static const struct snd_pcm_hardware snd_riptide_playback = {
1310109a9638SPeter Gruber 	.info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1311109a9638SPeter Gruber 		 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1312109a9638SPeter Gruber 		 SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_MMAP_VALID),
1313109a9638SPeter Gruber 	.formats =
1314109a9638SPeter Gruber 	    SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S8
1315109a9638SPeter Gruber 	    | SNDRV_PCM_FMTBIT_U16_LE,
1316109a9638SPeter Gruber 	.rates = SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_8000_48000,
1317109a9638SPeter Gruber 	.rate_min = 5500,
1318109a9638SPeter Gruber 	.rate_max = 48000,
1319109a9638SPeter Gruber 	.channels_min = 1,
1320109a9638SPeter Gruber 	.channels_max = 2,
1321109a9638SPeter Gruber 	.buffer_bytes_max = (64 * 1024),
1322109a9638SPeter Gruber 	.period_bytes_min = PAGE_SIZE >> 1,
1323109a9638SPeter Gruber 	.period_bytes_max = PAGE_SIZE << 8,
1324109a9638SPeter Gruber 	.periods_min = 2,
1325109a9638SPeter Gruber 	.periods_max = 64,
1326109a9638SPeter Gruber 	.fifo_size = 0,
1327109a9638SPeter Gruber };
132814ff46fdSBhumika Goyal static const struct snd_pcm_hardware snd_riptide_capture = {
1329109a9638SPeter Gruber 	.info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1330109a9638SPeter Gruber 		 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1331109a9638SPeter Gruber 		 SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_MMAP_VALID),
1332109a9638SPeter Gruber 	.formats =
1333109a9638SPeter Gruber 	    SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S8
1334109a9638SPeter Gruber 	    | SNDRV_PCM_FMTBIT_U16_LE,
1335109a9638SPeter Gruber 	.rates = SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_8000_48000,
1336109a9638SPeter Gruber 	.rate_min = 5500,
1337109a9638SPeter Gruber 	.rate_max = 48000,
1338109a9638SPeter Gruber 	.channels_min = 1,
1339109a9638SPeter Gruber 	.channels_max = 2,
1340109a9638SPeter Gruber 	.buffer_bytes_max = (64 * 1024),
1341109a9638SPeter Gruber 	.period_bytes_min = PAGE_SIZE >> 1,
1342109a9638SPeter Gruber 	.period_bytes_max = PAGE_SIZE << 3,
1343109a9638SPeter Gruber 	.periods_min = 2,
1344109a9638SPeter Gruber 	.periods_max = 64,
1345109a9638SPeter Gruber 	.fifo_size = 0,
1346109a9638SPeter Gruber };
1347109a9638SPeter Gruber 
snd_riptide_pointer(struct snd_pcm_substream * substream)1348109a9638SPeter Gruber static snd_pcm_uframes_t snd_riptide_pointer(struct snd_pcm_substream
1349109a9638SPeter Gruber 					     *substream)
1350109a9638SPeter Gruber {
1351109a9638SPeter Gruber 	struct snd_riptide *chip = snd_pcm_substream_chip(substream);
1352109a9638SPeter Gruber 	struct snd_pcm_runtime *runtime = substream->runtime;
1353109a9638SPeter Gruber 	struct pcmhw *data = get_pcmhwdev(substream);
1354109a9638SPeter Gruber 	struct cmdif *cif = chip->cif;
1355109a9638SPeter Gruber 	union cmdret rptr = CMDRET_ZERO;
1356109a9638SPeter Gruber 	snd_pcm_uframes_t ret;
1357109a9638SPeter Gruber 
1358109a9638SPeter Gruber 	SEND_GPOS(cif, 0, data->id, &rptr);
1359109a9638SPeter Gruber 	if (data->size && runtime->period_size) {
1360109a9638SPeter Gruber 		snd_printdd
1361109a9638SPeter Gruber 		    ("pointer stream %d position 0x%x(0x%x in buffer) bytes 0x%lx(0x%lx in period) frames\n",
1362109a9638SPeter Gruber 		     data->id, rptr.retlongs[1], rptr.retlongs[1] % data->size,
1363109a9638SPeter Gruber 		     bytes_to_frames(runtime, rptr.retlongs[1]),
1364109a9638SPeter Gruber 		     bytes_to_frames(runtime,
1365109a9638SPeter Gruber 				     rptr.retlongs[1]) % runtime->period_size);
1366109a9638SPeter Gruber 		if (rptr.retlongs[1] > data->pointer)
1367109a9638SPeter Gruber 			ret =
1368109a9638SPeter Gruber 			    bytes_to_frames(runtime,
1369109a9638SPeter Gruber 					    rptr.retlongs[1] % data->size);
1370109a9638SPeter Gruber 		else
1371109a9638SPeter Gruber 			ret =
1372109a9638SPeter Gruber 			    bytes_to_frames(runtime,
1373109a9638SPeter Gruber 					    data->pointer % data->size);
1374109a9638SPeter Gruber 	} else {
1375109a9638SPeter Gruber 		snd_printdd("stream not started or strange parms (%d %ld)\n",
1376109a9638SPeter Gruber 			    data->size, runtime->period_size);
1377109a9638SPeter Gruber 		ret = bytes_to_frames(runtime, 0);
1378109a9638SPeter Gruber 	}
1379109a9638SPeter Gruber 	return ret;
1380109a9638SPeter Gruber }
1381109a9638SPeter Gruber 
snd_riptide_trigger(struct snd_pcm_substream * substream,int cmd)1382109a9638SPeter Gruber static int snd_riptide_trigger(struct snd_pcm_substream *substream, int cmd)
1383109a9638SPeter Gruber {
1384109a9638SPeter Gruber 	int i, j;
1385109a9638SPeter Gruber 	struct snd_riptide *chip = snd_pcm_substream_chip(substream);
1386109a9638SPeter Gruber 	struct pcmhw *data = get_pcmhwdev(substream);
1387109a9638SPeter Gruber 	struct cmdif *cif = chip->cif;
1388109a9638SPeter Gruber 	union cmdret rptr = CMDRET_ZERO;
1389109a9638SPeter Gruber 
1390109a9638SPeter Gruber 	spin_lock(&chip->lock);
1391109a9638SPeter Gruber 	switch (cmd) {
1392109a9638SPeter Gruber 	case SNDRV_PCM_TRIGGER_START:
1393109a9638SPeter Gruber 	case SNDRV_PCM_TRIGGER_RESUME:
1394109a9638SPeter Gruber 		if (!(data->state & ST_PLAY)) {
1395109a9638SPeter Gruber 			SEND_SSTR(cif, data->id, data->sgdlist.addr);
1396109a9638SPeter Gruber 			SET_AIE(cif->hwport);
1397109a9638SPeter Gruber 			data->state = ST_PLAY;
1398109a9638SPeter Gruber 			if (data->mixer != 0xff)
1399109a9638SPeter Gruber 				setmixer(cif, data->mixer, 0x7fff, 0x7fff);
1400109a9638SPeter Gruber 			chip->openstreams++;
1401109a9638SPeter Gruber 			data->oldpos = 0;
1402109a9638SPeter Gruber 			data->pointer = 0;
1403109a9638SPeter Gruber 		}
1404109a9638SPeter Gruber 		break;
1405109a9638SPeter Gruber 	case SNDRV_PCM_TRIGGER_STOP:
1406109a9638SPeter Gruber 	case SNDRV_PCM_TRIGGER_SUSPEND:
1407109a9638SPeter Gruber 		if (data->mixer != 0xff)
1408109a9638SPeter Gruber 			setmixer(cif, data->mixer, 0, 0);
1409109a9638SPeter Gruber 		setmixer(cif, data->mixer, 0, 0);
1410109a9638SPeter Gruber 		SEND_KSTR(cif, data->id);
1411109a9638SPeter Gruber 		data->state = ST_STOP;
1412109a9638SPeter Gruber 		chip->openstreams--;
1413109a9638SPeter Gruber 		j = 0;
1414109a9638SPeter Gruber 		do {
1415109a9638SPeter Gruber 			i = rptr.retlongs[1];
1416109a9638SPeter Gruber 			SEND_GPOS(cif, 0, data->id, &rptr);
1417109a9638SPeter Gruber 			udelay(1);
1418109a9638SPeter Gruber 		} while (i != rptr.retlongs[1] && j++ < MAX_WRITE_RETRY);
14191b1cc7f2SRoel Kluin 		if (j > MAX_WRITE_RETRY)
1420109a9638SPeter Gruber 			snd_printk(KERN_ERR "Riptide: Could not stop stream!");
1421109a9638SPeter Gruber 		break;
1422109a9638SPeter Gruber 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1423109a9638SPeter Gruber 		if (!(data->state & ST_PAUSE)) {
1424109a9638SPeter Gruber 			SEND_PSTR(cif, data->id);
1425109a9638SPeter Gruber 			data->state |= ST_PAUSE;
1426109a9638SPeter Gruber 			chip->openstreams--;
1427109a9638SPeter Gruber 		}
1428109a9638SPeter Gruber 		break;
1429109a9638SPeter Gruber 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1430109a9638SPeter Gruber 		if (data->state & ST_PAUSE) {
1431109a9638SPeter Gruber 			SEND_SSTR(cif, data->id, data->sgdlist.addr);
1432109a9638SPeter Gruber 			data->state &= ~ST_PAUSE;
1433109a9638SPeter Gruber 			chip->openstreams++;
1434109a9638SPeter Gruber 		}
1435109a9638SPeter Gruber 		break;
1436109a9638SPeter Gruber 	default:
1437109a9638SPeter Gruber 		spin_unlock(&chip->lock);
1438109a9638SPeter Gruber 		return -EINVAL;
1439109a9638SPeter Gruber 	}
1440109a9638SPeter Gruber 	spin_unlock(&chip->lock);
1441109a9638SPeter Gruber 	return 0;
1442109a9638SPeter Gruber }
1443109a9638SPeter Gruber 
snd_riptide_prepare(struct snd_pcm_substream * substream)1444109a9638SPeter Gruber static int snd_riptide_prepare(struct snd_pcm_substream *substream)
1445109a9638SPeter Gruber {
1446109a9638SPeter Gruber 	struct snd_riptide *chip = snd_pcm_substream_chip(substream);
1447109a9638SPeter Gruber 	struct snd_pcm_runtime *runtime = substream->runtime;
1448109a9638SPeter Gruber 	struct pcmhw *data = get_pcmhwdev(substream);
1449109a9638SPeter Gruber 	struct cmdif *cif = chip->cif;
1450bf6f3d74STakashi Iwai 	const unsigned char *lbuspath = NULL;
1451109a9638SPeter Gruber 	unsigned int rate, channels;
1452109a9638SPeter Gruber 	int err = 0;
1453109a9638SPeter Gruber 	snd_pcm_format_t format;
1454109a9638SPeter Gruber 
1455da3cec35STakashi Iwai 	if (snd_BUG_ON(!cif || !data))
1456da3cec35STakashi Iwai 		return -EINVAL;
1457109a9638SPeter Gruber 
1458109a9638SPeter Gruber 	snd_printdd("prepare id %d ch: %d f:0x%x r:%d\n", data->id,
1459109a9638SPeter Gruber 		    runtime->channels, runtime->format, runtime->rate);
1460109a9638SPeter Gruber 
1461109a9638SPeter Gruber 	spin_lock_irq(&chip->lock);
1462109a9638SPeter Gruber 	channels = runtime->channels;
1463109a9638SPeter Gruber 	format = runtime->format;
1464109a9638SPeter Gruber 	rate = runtime->rate;
1465109a9638SPeter Gruber 	switch (channels) {
1466109a9638SPeter Gruber 	case 1:
1467109a9638SPeter Gruber 		if (rate == 48000 && format == SNDRV_PCM_FORMAT_S16_LE)
1468109a9638SPeter Gruber 			lbuspath = data->paths.noconv;
1469109a9638SPeter Gruber 		else
1470109a9638SPeter Gruber 			lbuspath = data->paths.mono;
1471109a9638SPeter Gruber 		break;
1472109a9638SPeter Gruber 	case 2:
1473109a9638SPeter Gruber 		if (rate == 48000 && format == SNDRV_PCM_FORMAT_S16_LE)
1474109a9638SPeter Gruber 			lbuspath = data->paths.noconv;
1475109a9638SPeter Gruber 		else
1476109a9638SPeter Gruber 			lbuspath = data->paths.stereo;
1477109a9638SPeter Gruber 		break;
1478109a9638SPeter Gruber 	}
147977a23f26STakashi Iwai 	snd_printdd("use sgdlist at 0x%p\n",
148077a23f26STakashi Iwai 		    data->sgdlist.area);
148177a23f26STakashi Iwai 	if (data->sgdlist.area) {
1482109a9638SPeter Gruber 		unsigned int i, j, size, pages, f, pt, period;
1483109a9638SPeter Gruber 		struct sgd *c, *p = NULL;
1484109a9638SPeter Gruber 
1485109a9638SPeter Gruber 		size = frames_to_bytes(runtime, runtime->buffer_size);
1486109a9638SPeter Gruber 		period = frames_to_bytes(runtime, runtime->period_size);
1487109a9638SPeter Gruber 		f = PAGE_SIZE;
1488109a9638SPeter Gruber 		while ((size + (f >> 1) - 1) <= (f << 7) && (f << 1) > period)
1489109a9638SPeter Gruber 			f = f >> 1;
1490cf7d7edcSAmitoj Kaur Chawla 		pages = DIV_ROUND_UP(size, f);
1491109a9638SPeter Gruber 		data->size = size;
1492109a9638SPeter Gruber 		data->pages = pages;
1493109a9638SPeter Gruber 		snd_printdd
1494109a9638SPeter Gruber 		    ("create sgd size: 0x%x pages %d of size 0x%x for period 0x%x\n",
1495109a9638SPeter Gruber 		     size, pages, f, period);
1496109a9638SPeter Gruber 		pt = 0;
1497109a9638SPeter Gruber 		j = 0;
1498109a9638SPeter Gruber 		for (i = 0; i < pages; i++) {
149977a23f26STakashi Iwai 			unsigned int ofs, addr;
1500109a9638SPeter Gruber 			c = &data->sgdbuf[i];
1501109a9638SPeter Gruber 			if (p)
1502109a9638SPeter Gruber 				p->dwNextLink = cpu_to_le32(data->sgdlist.addr +
1503109a9638SPeter Gruber 							    (i *
1504109a9638SPeter Gruber 							     sizeof(struct
1505109a9638SPeter Gruber 								    sgd)));
1506109a9638SPeter Gruber 			c->dwNextLink = cpu_to_le32(data->sgdlist.addr);
150777a23f26STakashi Iwai 			ofs = j << PAGE_SHIFT;
150877a23f26STakashi Iwai 			addr = snd_pcm_sgbuf_get_addr(substream, ofs) + pt;
150977a23f26STakashi Iwai 			c->dwSegPtrPhys = cpu_to_le32(addr);
1510109a9638SPeter Gruber 			pt = (pt + f) % PAGE_SIZE;
1511109a9638SPeter Gruber 			if (pt == 0)
1512109a9638SPeter Gruber 				j++;
1513109a9638SPeter Gruber 			c->dwSegLen = cpu_to_le32(f);
1514109a9638SPeter Gruber 			c->dwStat_Ctl =
1515109a9638SPeter Gruber 			    cpu_to_le32(IEOB_ENABLE | IEOS_ENABLE |
1516109a9638SPeter Gruber 					IEOC_ENABLE);
1517109a9638SPeter Gruber 			p = c;
1518109a9638SPeter Gruber 			size -= f;
1519109a9638SPeter Gruber 		}
1520109a9638SPeter Gruber 		data->sgdbuf[i].dwSegLen = cpu_to_le32(size);
1521109a9638SPeter Gruber 	}
1522109a9638SPeter Gruber 	if (lbuspath && lbuspath != data->lbuspath) {
1523109a9638SPeter Gruber 		if (data->lbuspath)
1524109a9638SPeter Gruber 			freelbuspath(cif, data->source, data->lbuspath);
1525109a9638SPeter Gruber 		alloclbuspath(cif, data->source, lbuspath,
1526109a9638SPeter Gruber 			      &data->mixer, data->intdec);
1527109a9638SPeter Gruber 		data->lbuspath = lbuspath;
1528109a9638SPeter Gruber 		data->rate = 0;
1529109a9638SPeter Gruber 	}
1530109a9638SPeter Gruber 	if (data->rate != rate || data->format != format ||
1531109a9638SPeter Gruber 	    data->channels != channels) {
1532109a9638SPeter Gruber 		data->rate = rate;
1533109a9638SPeter Gruber 		data->format = format;
1534109a9638SPeter Gruber 		data->channels = channels;
1535109a9638SPeter Gruber 		if (setsampleformat
1536109a9638SPeter Gruber 		    (cif, data->mixer, data->id, channels, format)
1537109a9638SPeter Gruber 		    || setsamplerate(cif, data->intdec, rate))
1538109a9638SPeter Gruber 			err = -EIO;
1539109a9638SPeter Gruber 	}
1540109a9638SPeter Gruber 	spin_unlock_irq(&chip->lock);
1541109a9638SPeter Gruber 	return err;
1542109a9638SPeter Gruber }
1543109a9638SPeter Gruber 
1544109a9638SPeter Gruber static int
snd_riptide_hw_params(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * hw_params)1545109a9638SPeter Gruber snd_riptide_hw_params(struct snd_pcm_substream *substream,
1546109a9638SPeter Gruber 		      struct snd_pcm_hw_params *hw_params)
1547109a9638SPeter Gruber {
1548109a9638SPeter Gruber 	struct snd_riptide *chip = snd_pcm_substream_chip(substream);
1549109a9638SPeter Gruber 	struct pcmhw *data = get_pcmhwdev(substream);
1550109a9638SPeter Gruber 	struct snd_dma_buffer *sgdlist = &data->sgdlist;
1551109a9638SPeter Gruber 	int err;
1552109a9638SPeter Gruber 
1553109a9638SPeter Gruber 	snd_printdd("hw params id %d (sgdlist: 0x%p 0x%lx %d)\n", data->id,
1554109a9638SPeter Gruber 		    sgdlist->area, (unsigned long)sgdlist->addr,
1555109a9638SPeter Gruber 		    (int)sgdlist->bytes);
1556109a9638SPeter Gruber 	if (sgdlist->area)
1557109a9638SPeter Gruber 		snd_dma_free_pages(sgdlist);
155828c07099STakashi Iwai 	err = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, &chip->pci->dev,
1559109a9638SPeter Gruber 				  sizeof(struct sgd) * (DESC_MAX_MASK + 1),
156028c07099STakashi Iwai 				  sgdlist);
156128c07099STakashi Iwai 	if (err < 0) {
1562109a9638SPeter Gruber 		snd_printk(KERN_ERR "Riptide: failed to alloc %d dma bytes\n",
1563109a9638SPeter Gruber 			   (int)sizeof(struct sgd) * (DESC_MAX_MASK + 1));
1564109a9638SPeter Gruber 		return err;
1565109a9638SPeter Gruber 	}
1566109a9638SPeter Gruber 	data->sgdbuf = (struct sgd *)sgdlist->area;
15670dd32369STakashi Iwai 	return 0;
1568109a9638SPeter Gruber }
1569109a9638SPeter Gruber 
snd_riptide_hw_free(struct snd_pcm_substream * substream)1570109a9638SPeter Gruber static int snd_riptide_hw_free(struct snd_pcm_substream *substream)
1571109a9638SPeter Gruber {
1572109a9638SPeter Gruber 	struct snd_riptide *chip = snd_pcm_substream_chip(substream);
1573109a9638SPeter Gruber 	struct pcmhw *data = get_pcmhwdev(substream);
1574109a9638SPeter Gruber 	struct cmdif *cif = chip->cif;
1575109a9638SPeter Gruber 
1576109a9638SPeter Gruber 	if (cif && data) {
1577109a9638SPeter Gruber 		if (data->lbuspath)
1578109a9638SPeter Gruber 			freelbuspath(cif, data->source, data->lbuspath);
1579109a9638SPeter Gruber 		data->lbuspath = NULL;
1580109a9638SPeter Gruber 		data->source = 0xff;
1581109a9638SPeter Gruber 		data->intdec[0] = 0xff;
1582109a9638SPeter Gruber 		data->intdec[1] = 0xff;
1583109a9638SPeter Gruber 
1584109a9638SPeter Gruber 		if (data->sgdlist.area) {
1585109a9638SPeter Gruber 			snd_dma_free_pages(&data->sgdlist);
1586109a9638SPeter Gruber 			data->sgdlist.area = NULL;
1587109a9638SPeter Gruber 		}
1588109a9638SPeter Gruber 	}
15890dd32369STakashi Iwai 	return 0;
1590109a9638SPeter Gruber }
1591109a9638SPeter Gruber 
snd_riptide_playback_open(struct snd_pcm_substream * substream)1592109a9638SPeter Gruber static int snd_riptide_playback_open(struct snd_pcm_substream *substream)
1593109a9638SPeter Gruber {
1594109a9638SPeter Gruber 	struct snd_riptide *chip = snd_pcm_substream_chip(substream);
1595109a9638SPeter Gruber 	struct snd_pcm_runtime *runtime = substream->runtime;
1596109a9638SPeter Gruber 	struct pcmhw *data;
15970cd87b10SHarvey Harrison 	int sub_num = substream->number;
1598109a9638SPeter Gruber 
15990cd87b10SHarvey Harrison 	chip->playback_substream[sub_num] = substream;
1600109a9638SPeter Gruber 	runtime->hw = snd_riptide_playback;
160168bf5700SKulikov Vasiliy 
1602109a9638SPeter Gruber 	data = kzalloc(sizeof(struct pcmhw), GFP_KERNEL);
160368bf5700SKulikov Vasiliy 	if (data == NULL)
160468bf5700SKulikov Vasiliy 		return -ENOMEM;
16050cd87b10SHarvey Harrison 	data->paths = lbus_play_paths[sub_num];
16060cd87b10SHarvey Harrison 	data->id = play_ids[sub_num];
16070cd87b10SHarvey Harrison 	data->source = play_sources[sub_num];
1608109a9638SPeter Gruber 	data->intdec[0] = 0xff;
1609109a9638SPeter Gruber 	data->intdec[1] = 0xff;
1610109a9638SPeter Gruber 	data->state = ST_STOP;
1611109a9638SPeter Gruber 	runtime->private_data = data;
1612109a9638SPeter Gruber 	return snd_pcm_hw_constraint_integer(runtime,
1613109a9638SPeter Gruber 					     SNDRV_PCM_HW_PARAM_PERIODS);
1614109a9638SPeter Gruber }
1615109a9638SPeter Gruber 
snd_riptide_capture_open(struct snd_pcm_substream * substream)1616109a9638SPeter Gruber static int snd_riptide_capture_open(struct snd_pcm_substream *substream)
1617109a9638SPeter Gruber {
1618109a9638SPeter Gruber 	struct snd_riptide *chip = snd_pcm_substream_chip(substream);
1619109a9638SPeter Gruber 	struct snd_pcm_runtime *runtime = substream->runtime;
1620109a9638SPeter Gruber 	struct pcmhw *data;
1621109a9638SPeter Gruber 
1622109a9638SPeter Gruber 	chip->capture_substream = substream;
1623109a9638SPeter Gruber 	runtime->hw = snd_riptide_capture;
162468bf5700SKulikov Vasiliy 
1625109a9638SPeter Gruber 	data = kzalloc(sizeof(struct pcmhw), GFP_KERNEL);
162668bf5700SKulikov Vasiliy 	if (data == NULL)
162768bf5700SKulikov Vasiliy 		return -ENOMEM;
1628109a9638SPeter Gruber 	data->paths = lbus_rec_path;
1629109a9638SPeter Gruber 	data->id = PADC;
1630109a9638SPeter Gruber 	data->source = ACLNK2PADC;
1631109a9638SPeter Gruber 	data->intdec[0] = 0xff;
1632109a9638SPeter Gruber 	data->intdec[1] = 0xff;
1633109a9638SPeter Gruber 	data->state = ST_STOP;
1634109a9638SPeter Gruber 	runtime->private_data = data;
1635109a9638SPeter Gruber 	return snd_pcm_hw_constraint_integer(runtime,
1636109a9638SPeter Gruber 					     SNDRV_PCM_HW_PARAM_PERIODS);
1637109a9638SPeter Gruber }
1638109a9638SPeter Gruber 
snd_riptide_playback_close(struct snd_pcm_substream * substream)1639109a9638SPeter Gruber static int snd_riptide_playback_close(struct snd_pcm_substream *substream)
1640109a9638SPeter Gruber {
1641109a9638SPeter Gruber 	struct snd_riptide *chip = snd_pcm_substream_chip(substream);
1642109a9638SPeter Gruber 	struct pcmhw *data = get_pcmhwdev(substream);
16430cd87b10SHarvey Harrison 	int sub_num = substream->number;
1644109a9638SPeter Gruber 
1645109a9638SPeter Gruber 	substream->runtime->private_data = NULL;
16460cd87b10SHarvey Harrison 	chip->playback_substream[sub_num] = NULL;
1647109a9638SPeter Gruber 	kfree(data);
1648109a9638SPeter Gruber 	return 0;
1649109a9638SPeter Gruber }
1650109a9638SPeter Gruber 
snd_riptide_capture_close(struct snd_pcm_substream * substream)1651109a9638SPeter Gruber static int snd_riptide_capture_close(struct snd_pcm_substream *substream)
1652109a9638SPeter Gruber {
1653109a9638SPeter Gruber 	struct snd_riptide *chip = snd_pcm_substream_chip(substream);
1654109a9638SPeter Gruber 	struct pcmhw *data = get_pcmhwdev(substream);
1655109a9638SPeter Gruber 
1656109a9638SPeter Gruber 	substream->runtime->private_data = NULL;
1657109a9638SPeter Gruber 	chip->capture_substream = NULL;
1658109a9638SPeter Gruber 	kfree(data);
1659109a9638SPeter Gruber 	return 0;
1660109a9638SPeter Gruber }
1661109a9638SPeter Gruber 
16626769e988SJulia Lawall static const struct snd_pcm_ops snd_riptide_playback_ops = {
1663109a9638SPeter Gruber 	.open = snd_riptide_playback_open,
1664109a9638SPeter Gruber 	.close = snd_riptide_playback_close,
1665109a9638SPeter Gruber 	.hw_params = snd_riptide_hw_params,
1666109a9638SPeter Gruber 	.hw_free = snd_riptide_hw_free,
1667109a9638SPeter Gruber 	.prepare = snd_riptide_prepare,
1668109a9638SPeter Gruber 	.trigger = snd_riptide_trigger,
1669109a9638SPeter Gruber 	.pointer = snd_riptide_pointer,
1670109a9638SPeter Gruber };
16716769e988SJulia Lawall static const struct snd_pcm_ops snd_riptide_capture_ops = {
1672109a9638SPeter Gruber 	.open = snd_riptide_capture_open,
1673109a9638SPeter Gruber 	.close = snd_riptide_capture_close,
1674109a9638SPeter Gruber 	.hw_params = snd_riptide_hw_params,
1675109a9638SPeter Gruber 	.hw_free = snd_riptide_hw_free,
1676109a9638SPeter Gruber 	.prepare = snd_riptide_prepare,
1677109a9638SPeter Gruber 	.trigger = snd_riptide_trigger,
1678109a9638SPeter Gruber 	.pointer = snd_riptide_pointer,
1679109a9638SPeter Gruber };
1680109a9638SPeter Gruber 
snd_riptide_pcm(struct snd_riptide * chip,int device)168115aded4cSLars-Peter Clausen static int snd_riptide_pcm(struct snd_riptide *chip, int device)
1682109a9638SPeter Gruber {
1683109a9638SPeter Gruber 	struct snd_pcm *pcm;
1684109a9638SPeter Gruber 	int err;
1685109a9638SPeter Gruber 
168628c07099STakashi Iwai 	err = snd_pcm_new(chip->card, "RIPTIDE", device, PLAYBACK_SUBSTREAMS, 1,
168728c07099STakashi Iwai 			  &pcm);
168828c07099STakashi Iwai 	if (err < 0)
1689109a9638SPeter Gruber 		return err;
1690109a9638SPeter Gruber 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
1691109a9638SPeter Gruber 			&snd_riptide_playback_ops);
1692109a9638SPeter Gruber 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
1693109a9638SPeter Gruber 			&snd_riptide_capture_ops);
1694109a9638SPeter Gruber 	pcm->private_data = chip;
1695109a9638SPeter Gruber 	pcm->info_flags = 0;
1696109a9638SPeter Gruber 	strcpy(pcm->name, "RIPTIDE");
1697109a9638SPeter Gruber 	chip->pcm = pcm;
16980dd32369STakashi Iwai 	snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV_SG,
16990dd32369STakashi Iwai 				       &chip->pci->dev, 64 * 1024, 128 * 1024);
1700109a9638SPeter Gruber 	return 0;
1701109a9638SPeter Gruber }
1702109a9638SPeter Gruber 
1703109a9638SPeter Gruber static irqreturn_t
snd_riptide_interrupt(int irq,void * dev_id)17047d12e780SDavid Howells snd_riptide_interrupt(int irq, void *dev_id)
1705109a9638SPeter Gruber {
1706109a9638SPeter Gruber 	struct snd_riptide *chip = dev_id;
1707109a9638SPeter Gruber 	struct cmdif *cif = chip->cif;
17082ac55dafSTakashi Iwai 	irqreturn_t ret = IRQ_HANDLED;
1709109a9638SPeter Gruber 
1710109a9638SPeter Gruber 	if (cif) {
1711109a9638SPeter Gruber 		chip->received_irqs++;
1712109a9638SPeter Gruber 		if (IS_EOBIRQ(cif->hwport) || IS_EOSIRQ(cif->hwport) ||
1713109a9638SPeter Gruber 		    IS_EOCIRQ(cif->hwport)) {
1714109a9638SPeter Gruber 			chip->handled_irqs++;
17152ac55dafSTakashi Iwai 			ret = IRQ_WAKE_THREAD;
1716109a9638SPeter Gruber 		}
1717109a9638SPeter Gruber 		if (chip->rmidi && IS_MPUIRQ(cif->hwport)) {
1718109a9638SPeter Gruber 			chip->handled_irqs++;
1719109a9638SPeter Gruber 			snd_mpu401_uart_interrupt(irq,
17207d12e780SDavid Howells 						  chip->rmidi->private_data);
1721109a9638SPeter Gruber 		}
1722109a9638SPeter Gruber 		SET_AIACK(cif->hwport);
1723109a9638SPeter Gruber 	}
17242ac55dafSTakashi Iwai 	return ret;
1725109a9638SPeter Gruber }
1726109a9638SPeter Gruber 
1727109a9638SPeter Gruber static void
snd_riptide_codec_write(struct snd_ac97 * ac97,unsigned short reg,unsigned short val)1728109a9638SPeter Gruber snd_riptide_codec_write(struct snd_ac97 *ac97, unsigned short reg,
1729109a9638SPeter Gruber 			unsigned short val)
1730109a9638SPeter Gruber {
1731109a9638SPeter Gruber 	struct snd_riptide *chip = ac97->private_data;
1732109a9638SPeter Gruber 	struct cmdif *cif = chip->cif;
1733109a9638SPeter Gruber 	union cmdret rptr = CMDRET_ZERO;
1734109a9638SPeter Gruber 	int i = 0;
1735109a9638SPeter Gruber 
1736da3cec35STakashi Iwai 	if (snd_BUG_ON(!cif))
1737da3cec35STakashi Iwai 		return;
1738109a9638SPeter Gruber 
1739109a9638SPeter Gruber 	snd_printdd("Write AC97 reg 0x%x 0x%x\n", reg, val);
1740109a9638SPeter Gruber 	do {
1741109a9638SPeter Gruber 		SEND_SACR(cif, val, reg);
1742109a9638SPeter Gruber 		SEND_RACR(cif, reg, &rptr);
1743109a9638SPeter Gruber 	} while (rptr.retwords[1] != val && i++ < MAX_WRITE_RETRY);
17441b1cc7f2SRoel Kluin 	if (i > MAX_WRITE_RETRY)
1745109a9638SPeter Gruber 		snd_printdd("Write AC97 reg failed\n");
1746109a9638SPeter Gruber }
1747109a9638SPeter Gruber 
snd_riptide_codec_read(struct snd_ac97 * ac97,unsigned short reg)1748109a9638SPeter Gruber static unsigned short snd_riptide_codec_read(struct snd_ac97 *ac97,
1749109a9638SPeter Gruber 					     unsigned short reg)
1750109a9638SPeter Gruber {
1751109a9638SPeter Gruber 	struct snd_riptide *chip = ac97->private_data;
1752109a9638SPeter Gruber 	struct cmdif *cif = chip->cif;
1753109a9638SPeter Gruber 	union cmdret rptr = CMDRET_ZERO;
1754109a9638SPeter Gruber 
1755da3cec35STakashi Iwai 	if (snd_BUG_ON(!cif))
1756da3cec35STakashi Iwai 		return 0;
1757109a9638SPeter Gruber 
1758109a9638SPeter Gruber 	if (SEND_RACR(cif, reg, &rptr) != 0)
1759109a9638SPeter Gruber 		SEND_RACR(cif, reg, &rptr);
1760109a9638SPeter Gruber 	snd_printdd("Read AC97 reg 0x%x got 0x%x\n", reg, rptr.retwords[1]);
1761109a9638SPeter Gruber 	return rptr.retwords[1];
1762109a9638SPeter Gruber }
1763109a9638SPeter Gruber 
snd_riptide_initialize(struct snd_riptide * chip)1764109a9638SPeter Gruber static int snd_riptide_initialize(struct snd_riptide *chip)
1765109a9638SPeter Gruber {
1766109a9638SPeter Gruber 	struct cmdif *cif;
1767109a9638SPeter Gruber 	unsigned int device_id;
1768109a9638SPeter Gruber 	int err;
1769109a9638SPeter Gruber 
1770da3cec35STakashi Iwai 	if (snd_BUG_ON(!chip))
1771da3cec35STakashi Iwai 		return -EINVAL;
1772109a9638SPeter Gruber 
1773109a9638SPeter Gruber 	cif = chip->cif;
1774109a9638SPeter Gruber 	if (!cif) {
177528c07099STakashi Iwai 		cif = kzalloc(sizeof(struct cmdif), GFP_KERNEL);
177628c07099STakashi Iwai 		if (!cif)
1777109a9638SPeter Gruber 			return -ENOMEM;
1778109a9638SPeter Gruber 		cif->hwport = (struct riptideport *)chip->port;
1779109a9638SPeter Gruber 		spin_lock_init(&cif->lock);
1780109a9638SPeter Gruber 		chip->cif = cif;
1781109a9638SPeter Gruber 	}
1782109a9638SPeter Gruber 	cif->is_reset = 0;
178328c07099STakashi Iwai 	err = riptide_reset(cif, chip);
178428c07099STakashi Iwai 	if (err)
1785109a9638SPeter Gruber 		return err;
1786109a9638SPeter Gruber 	device_id = chip->device_id;
1787109a9638SPeter Gruber 	switch (device_id) {
1788109a9638SPeter Gruber 	case 0x4310:
1789109a9638SPeter Gruber 	case 0x4320:
1790109a9638SPeter Gruber 	case 0x4330:
1791109a9638SPeter Gruber 		snd_printdd("Modem enable?\n");
1792109a9638SPeter Gruber 		SEND_SETDPLL(cif);
1793109a9638SPeter Gruber 		break;
1794109a9638SPeter Gruber 	}
1795109a9638SPeter Gruber 	snd_printdd("Enabling MPU IRQs\n");
1796109a9638SPeter Gruber 	if (chip->rmidi)
1797109a9638SPeter Gruber 		SET_EMPUIRQ(cif->hwport);
1798109a9638SPeter Gruber 	return err;
1799109a9638SPeter Gruber }
1800109a9638SPeter Gruber 
snd_riptide_free(struct snd_card * card)1801546c201aSTakashi Iwai static void snd_riptide_free(struct snd_card *card)
1802109a9638SPeter Gruber {
1803546c201aSTakashi Iwai 	struct snd_riptide *chip = card->private_data;
1804109a9638SPeter Gruber 	struct cmdif *cif;
1805109a9638SPeter Gruber 
180628c07099STakashi Iwai 	cif = chip->cif;
180728c07099STakashi Iwai 	if (cif) {
1808109a9638SPeter Gruber 		SET_GRESET(cif->hwport);
1809109a9638SPeter Gruber 		udelay(100);
1810109a9638SPeter Gruber 		UNSET_GRESET(cif->hwport);
1811109a9638SPeter Gruber 		kfree(chip->cif);
1812109a9638SPeter Gruber 	}
1813109a9638SPeter Gruber 	release_firmware(chip->fw_entry);
1814109a9638SPeter Gruber }
1815109a9638SPeter Gruber 
1816e23e7a14SBill Pemberton static int
snd_riptide_create(struct snd_card * card,struct pci_dev * pci)1817546c201aSTakashi Iwai snd_riptide_create(struct snd_card *card, struct pci_dev *pci)
1818109a9638SPeter Gruber {
1819546c201aSTakashi Iwai 	struct snd_riptide *chip = card->private_data;
1820109a9638SPeter Gruber 	struct riptideport *hwport;
1821109a9638SPeter Gruber 	int err;
1822109a9638SPeter Gruber 
1823546c201aSTakashi Iwai 	err = pcim_enable_device(pci);
182428c07099STakashi Iwai 	if (err < 0)
1825109a9638SPeter Gruber 		return err;
1826109a9638SPeter Gruber 
1827109a9638SPeter Gruber 	spin_lock_init(&chip->lock);
1828109a9638SPeter Gruber 	chip->card = card;
1829109a9638SPeter Gruber 	chip->pci = pci;
1830109a9638SPeter Gruber 	chip->irq = -1;
1831109a9638SPeter Gruber 	chip->openstreams = 0;
1832109a9638SPeter Gruber 	chip->port = pci_resource_start(pci, 0);
1833109a9638SPeter Gruber 	chip->received_irqs = 0;
1834109a9638SPeter Gruber 	chip->handled_irqs = 0;
1835109a9638SPeter Gruber 	chip->cif = NULL;
1836546c201aSTakashi Iwai 	card->private_free = snd_riptide_free;
1837109a9638SPeter Gruber 
1838546c201aSTakashi Iwai 	err = pci_request_regions(pci, "RIPTIDE");
1839546c201aSTakashi Iwai 	if (err < 0)
1840546c201aSTakashi Iwai 		return err;
1841109a9638SPeter Gruber 	hwport = (struct riptideport *)chip->port;
1842109a9638SPeter Gruber 	UNSET_AIE(hwport);
1843109a9638SPeter Gruber 
1844546c201aSTakashi Iwai 	if (devm_request_threaded_irq(&pci->dev, pci->irq,
1845546c201aSTakashi Iwai 				      snd_riptide_interrupt,
18462ac55dafSTakashi Iwai 				      riptide_handleirq, IRQF_SHARED,
1847934c2b6dSTakashi Iwai 				      KBUILD_MODNAME, chip)) {
1848109a9638SPeter Gruber 		snd_printk(KERN_ERR "Riptide: unable to grab IRQ %d\n",
1849109a9638SPeter Gruber 			   pci->irq);
1850109a9638SPeter Gruber 		return -EBUSY;
1851109a9638SPeter Gruber 	}
1852109a9638SPeter Gruber 	chip->irq = pci->irq;
18533992f78fSTakashi Iwai 	card->sync_irq = chip->irq;
1854109a9638SPeter Gruber 	chip->device_id = pci->device;
1855109a9638SPeter Gruber 	pci_set_master(pci);
185628c07099STakashi Iwai 	err = snd_riptide_initialize(chip);
1857546c201aSTakashi Iwai 	if (err < 0)
1858109a9638SPeter Gruber 		return err;
1859109a9638SPeter Gruber 
1860109a9638SPeter Gruber 	return 0;
1861109a9638SPeter Gruber }
1862109a9638SPeter Gruber 
1863109a9638SPeter Gruber static void
snd_riptide_proc_read(struct snd_info_entry * entry,struct snd_info_buffer * buffer)1864109a9638SPeter Gruber snd_riptide_proc_read(struct snd_info_entry *entry,
1865109a9638SPeter Gruber 		      struct snd_info_buffer *buffer)
1866109a9638SPeter Gruber {
1867109a9638SPeter Gruber 	struct snd_riptide *chip = entry->private_data;
1868109a9638SPeter Gruber 	struct pcmhw *data;
1869109a9638SPeter Gruber 	int i;
1870109a9638SPeter Gruber 	struct cmdif *cif = NULL;
1871109a9638SPeter Gruber 	unsigned char p[256];
1872109a9638SPeter Gruber 	unsigned short rval = 0, lval = 0;
1873109a9638SPeter Gruber 	unsigned int rate;
1874109a9638SPeter Gruber 
1875109a9638SPeter Gruber 	if (!chip)
1876109a9638SPeter Gruber 		return;
1877109a9638SPeter Gruber 
1878109a9638SPeter Gruber 	snd_iprintf(buffer, "%s\n\n", chip->card->longname);
1879109a9638SPeter Gruber 	snd_iprintf(buffer, "Device ID: 0x%x\nReceived IRQs: (%ld)%ld\nPorts:",
1880109a9638SPeter Gruber 		    chip->device_id, chip->handled_irqs, chip->received_irqs);
1881109a9638SPeter Gruber 	for (i = 0; i < 64; i += 4)
1882109a9638SPeter Gruber 		snd_iprintf(buffer, "%c%02x: %08x",
1883109a9638SPeter Gruber 			    (i % 16) ? ' ' : '\n', i, inl(chip->port + i));
188428c07099STakashi Iwai 	cif = chip->cif;
188528c07099STakashi Iwai 	if (cif) {
1886109a9638SPeter Gruber 		snd_iprintf(buffer,
1887109a9638SPeter Gruber 			    "\nVersion: ASIC: %d CODEC: %d AUXDSP: %d PROG: %d",
1888109a9638SPeter Gruber 			    chip->firmware.firmware.ASIC,
1889109a9638SPeter Gruber 			    chip->firmware.firmware.CODEC,
1890109a9638SPeter Gruber 			    chip->firmware.firmware.AUXDSP,
1891109a9638SPeter Gruber 			    chip->firmware.firmware.PROG);
1892109a9638SPeter Gruber 		snd_iprintf(buffer, "\nDigital mixer:");
1893109a9638SPeter Gruber 		for (i = 0; i < 12; i++) {
1894109a9638SPeter Gruber 			getmixer(cif, i, &rval, &lval);
1895109a9638SPeter Gruber 			snd_iprintf(buffer, "\n %d: %d %d", i, rval, lval);
1896109a9638SPeter Gruber 		}
1897109a9638SPeter Gruber 		snd_iprintf(buffer,
1898109a9638SPeter Gruber 			    "\nARM Commands num: %d failed: %d time: %d max: %d min: %d",
1899109a9638SPeter Gruber 			    cif->cmdcnt, cif->errcnt,
1900109a9638SPeter Gruber 			    cif->cmdtime, cif->cmdtimemax, cif->cmdtimemin);
1901109a9638SPeter Gruber 	}
1902109a9638SPeter Gruber 	snd_iprintf(buffer, "\nOpen streams %d:\n", chip->openstreams);
1903109a9638SPeter Gruber 	for (i = 0; i < PLAYBACK_SUBSTREAMS; i++) {
190428c07099STakashi Iwai 		if (!chip->playback_substream[i] ||
190528c07099STakashi Iwai 		    !chip->playback_substream[i]->runtime)
190628c07099STakashi Iwai 			continue;
190728c07099STakashi Iwai 		data = chip->playback_substream[i]->runtime->private_data;
190828c07099STakashi Iwai 		if (data) {
1909109a9638SPeter Gruber 			snd_iprintf(buffer,
1910109a9638SPeter Gruber 				    "stream: %d mixer: %d source: %d (%d,%d)\n",
1911109a9638SPeter Gruber 				    data->id, data->mixer, data->source,
1912109a9638SPeter Gruber 				    data->intdec[0], data->intdec[1]);
1913109a9638SPeter Gruber 			if (!(getsamplerate(cif, data->intdec, &rate)))
1914109a9638SPeter Gruber 				snd_iprintf(buffer, "rate: %d\n", rate);
1915109a9638SPeter Gruber 		}
1916109a9638SPeter Gruber 	}
191728c07099STakashi Iwai 	if (chip->capture_substream && chip->capture_substream->runtime) {
191828c07099STakashi Iwai 		data = chip->capture_substream->runtime->private_data;
191928c07099STakashi Iwai 		if (data) {
1920109a9638SPeter Gruber 			snd_iprintf(buffer,
1921109a9638SPeter Gruber 				    "stream: %d mixer: %d source: %d (%d,%d)\n",
1922109a9638SPeter Gruber 				    data->id, data->mixer,
1923109a9638SPeter Gruber 				    data->source, data->intdec[0], data->intdec[1]);
1924109a9638SPeter Gruber 			if (!(getsamplerate(cif, data->intdec, &rate)))
1925109a9638SPeter Gruber 				snd_iprintf(buffer, "rate: %d\n", rate);
1926109a9638SPeter Gruber 		}
192728c07099STakashi Iwai 	}
1928109a9638SPeter Gruber 	snd_iprintf(buffer, "Paths:\n");
1929109a9638SPeter Gruber 	i = getpaths(cif, p);
1930282572b5SDan Carpenter 	while (i >= 2) {
1931282572b5SDan Carpenter 		i -= 2;
1932282572b5SDan Carpenter 		snd_iprintf(buffer, "%x->%x ", p[i], p[i + 1]);
1933109a9638SPeter Gruber 	}
1934109a9638SPeter Gruber 	snd_iprintf(buffer, "\n");
1935109a9638SPeter Gruber }
1936109a9638SPeter Gruber 
snd_riptide_proc_init(struct snd_riptide * chip)1937e23e7a14SBill Pemberton static void snd_riptide_proc_init(struct snd_riptide *chip)
1938109a9638SPeter Gruber {
193947f2769bSTakashi Iwai 	snd_card_ro_proc_new(chip->card, "riptide", chip,
194047f2769bSTakashi Iwai 			     snd_riptide_proc_read);
1941109a9638SPeter Gruber }
1942109a9638SPeter Gruber 
snd_riptide_mixer(struct snd_riptide * chip)1943e23e7a14SBill Pemberton static int snd_riptide_mixer(struct snd_riptide *chip)
1944109a9638SPeter Gruber {
1945109a9638SPeter Gruber 	struct snd_ac97_bus *pbus;
1946109a9638SPeter Gruber 	struct snd_ac97_template ac97;
1947109a9638SPeter Gruber 	int err = 0;
194851055da5STakashi Iwai 	static const struct snd_ac97_bus_ops ops = {
1949109a9638SPeter Gruber 		.write = snd_riptide_codec_write,
1950109a9638SPeter Gruber 		.read = snd_riptide_codec_read,
1951109a9638SPeter Gruber 	};
1952109a9638SPeter Gruber 
1953109a9638SPeter Gruber 	memset(&ac97, 0, sizeof(ac97));
1954109a9638SPeter Gruber 	ac97.private_data = chip;
1955109a9638SPeter Gruber 	ac97.scaps = AC97_SCAP_SKIP_MODEM;
1956109a9638SPeter Gruber 
195728c07099STakashi Iwai 	err = snd_ac97_bus(chip->card, 0, &ops, chip, &pbus);
195828c07099STakashi Iwai 	if (err < 0)
1959109a9638SPeter Gruber 		return err;
1960109a9638SPeter Gruber 
1961109a9638SPeter Gruber 	chip->ac97_bus = pbus;
1962109a9638SPeter Gruber 	ac97.pci = chip->pci;
196328c07099STakashi Iwai 	err = snd_ac97_mixer(pbus, &ac97, &chip->ac97);
196428c07099STakashi Iwai 	if (err < 0)
1965109a9638SPeter Gruber 		return err;
1966109a9638SPeter Gruber 	return err;
1967109a9638SPeter Gruber }
1968109a9638SPeter Gruber 
1969109a9638SPeter Gruber #ifdef SUPPORT_JOYSTICK
1970109a9638SPeter Gruber 
1971e23e7a14SBill Pemberton static int
snd_riptide_joystick_probe(struct pci_dev * pci,const struct pci_device_id * id)1972109a9638SPeter Gruber snd_riptide_joystick_probe(struct pci_dev *pci, const struct pci_device_id *id)
1973109a9638SPeter Gruber {
1974109a9638SPeter Gruber 	static int dev;
1975db1005ecSTakashi Iwai 	struct gameport *gameport;
1976e4940626SDan Carpenter 	int ret;
1977109a9638SPeter Gruber 
1978109a9638SPeter Gruber 	if (dev >= SNDRV_CARDS)
1979109a9638SPeter Gruber 		return -ENODEV;
1980e4940626SDan Carpenter 
1981109a9638SPeter Gruber 	if (!enable[dev]) {
1982e4940626SDan Carpenter 		ret = -ENOENT;
1983e4940626SDan Carpenter 		goto inc_dev;
1984109a9638SPeter Gruber 	}
1985109a9638SPeter Gruber 
1986e4940626SDan Carpenter 	if (!joystick_port[dev]) {
1987e4940626SDan Carpenter 		ret = 0;
1988e4940626SDan Carpenter 		goto inc_dev;
1989e4940626SDan Carpenter 	}
1990db1005ecSTakashi Iwai 
1991db1005ecSTakashi Iwai 	gameport = gameport_allocate_port();
1992e4940626SDan Carpenter 	if (!gameport) {
1993e4940626SDan Carpenter 		ret = -ENOMEM;
1994e4940626SDan Carpenter 		goto inc_dev;
1995e4940626SDan Carpenter 	}
1996db1005ecSTakashi Iwai 	if (!request_region(joystick_port[dev], 8, "Riptide gameport")) {
1997109a9638SPeter Gruber 		snd_printk(KERN_WARNING
1998109a9638SPeter Gruber 			   "Riptide: cannot grab gameport 0x%x\n",
1999109a9638SPeter Gruber 			   joystick_port[dev]);
2000db1005ecSTakashi Iwai 		gameport_free_port(gameport);
2001e4940626SDan Carpenter 		ret = -EBUSY;
2002e4940626SDan Carpenter 		goto inc_dev;
2003109a9638SPeter Gruber 	}
2004db1005ecSTakashi Iwai 
2005db1005ecSTakashi Iwai 	gameport->io = joystick_port[dev];
2006db1005ecSTakashi Iwai 	gameport_register_port(gameport);
2007db1005ecSTakashi Iwai 	pci_set_drvdata(pci, gameport);
2008e4940626SDan Carpenter 
2009e4940626SDan Carpenter 	ret = 0;
2010e4940626SDan Carpenter inc_dev:
2011e4940626SDan Carpenter 	dev++;
2012e4940626SDan Carpenter 	return ret;
2013109a9638SPeter Gruber }
2014109a9638SPeter Gruber 
snd_riptide_joystick_remove(struct pci_dev * pci)2015e23e7a14SBill Pemberton static void snd_riptide_joystick_remove(struct pci_dev *pci)
2016109a9638SPeter Gruber {
2017db1005ecSTakashi Iwai 	struct gameport *gameport = pci_get_drvdata(pci);
2018db1005ecSTakashi Iwai 	if (gameport) {
2019db1005ecSTakashi Iwai 		release_region(gameport->io, 8);
2020db1005ecSTakashi Iwai 		gameport_unregister_port(gameport);
2021109a9638SPeter Gruber 	}
2022109a9638SPeter Gruber }
2023109a9638SPeter Gruber #endif
2024109a9638SPeter Gruber 
2025e23e7a14SBill Pemberton static int
__snd_card_riptide_probe(struct pci_dev * pci,const struct pci_device_id * pci_id)2026348f08deSTakashi Iwai __snd_card_riptide_probe(struct pci_dev *pci, const struct pci_device_id *pci_id)
2027109a9638SPeter Gruber {
2028109a9638SPeter Gruber 	static int dev;
2029109a9638SPeter Gruber 	struct snd_card *card;
2030109a9638SPeter Gruber 	struct snd_riptide *chip;
2031a693a26fSTakashi Iwai 	unsigned short val;
2032a693a26fSTakashi Iwai 	int err;
2033109a9638SPeter Gruber 
2034109a9638SPeter Gruber 	if (dev >= SNDRV_CARDS)
2035109a9638SPeter Gruber 		return -ENODEV;
2036109a9638SPeter Gruber 	if (!enable[dev]) {
2037109a9638SPeter Gruber 		dev++;
2038109a9638SPeter Gruber 		return -ENOENT;
2039109a9638SPeter Gruber 	}
2040109a9638SPeter Gruber 
2041546c201aSTakashi Iwai 	err = snd_devm_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE,
2042546c201aSTakashi Iwai 				sizeof(*chip), &card);
2043e58de7baSTakashi Iwai 	if (err < 0)
2044e58de7baSTakashi Iwai 		return err;
2045546c201aSTakashi Iwai 	chip = card->private_data;
2046546c201aSTakashi Iwai 	err = snd_riptide_create(card, pci);
2047a693a26fSTakashi Iwai 	if (err < 0)
2048546c201aSTakashi Iwai 		return err;
204915aded4cSLars-Peter Clausen 	err = snd_riptide_pcm(chip, 0);
2050a693a26fSTakashi Iwai 	if (err < 0)
2051546c201aSTakashi Iwai 		return err;
2052a693a26fSTakashi Iwai 	err = snd_riptide_mixer(chip);
2053a693a26fSTakashi Iwai 	if (err < 0)
2054546c201aSTakashi Iwai 		return err;
2055a693a26fSTakashi Iwai 
2056a693a26fSTakashi Iwai 	val = LEGACY_ENABLE_ALL;
2057a693a26fSTakashi Iwai 	if (opl3_port[dev])
2058a693a26fSTakashi Iwai 		val |= LEGACY_ENABLE_FM;
2059109a9638SPeter Gruber #ifdef SUPPORT_JOYSTICK
2060a693a26fSTakashi Iwai 	if (joystick_port[dev])
2061a693a26fSTakashi Iwai 		val |= LEGACY_ENABLE_GAMEPORT;
2062109a9638SPeter Gruber #endif
2063a693a26fSTakashi Iwai 	if (mpu_port[dev])
2064a693a26fSTakashi Iwai 		val |= LEGACY_ENABLE_MPU_INT | LEGACY_ENABLE_MPU;
2065a693a26fSTakashi Iwai 	val |= (chip->irq << 4) & 0xf0;
2066a693a26fSTakashi Iwai 	pci_write_config_word(chip->pci, PCI_EXT_Legacy_Mask, val);
2067a693a26fSTakashi Iwai 	if (mpu_port[dev]) {
2068a693a26fSTakashi Iwai 		val = mpu_port[dev];
2069a693a26fSTakashi Iwai 		pci_write_config_word(chip->pci, PCI_EXT_MPU_Base, val);
2070a693a26fSTakashi Iwai 		err = snd_mpu401_uart_new(card, 0, MPU401_HW_RIPTIDE,
2071dba8b469SClemens Ladisch 					  val, MPU401_INFO_IRQ_HOOK, -1,
2072a693a26fSTakashi Iwai 					  &chip->rmidi);
2073a693a26fSTakashi Iwai 		if (err < 0)
2074109a9638SPeter Gruber 			snd_printk(KERN_WARNING
2075109a9638SPeter Gruber 				   "Riptide: Can't Allocate MPU at 0x%x\n",
2076a693a26fSTakashi Iwai 				   val);
2077109a9638SPeter Gruber 		else
2078a693a26fSTakashi Iwai 			chip->mpuaddr = val;
2079109a9638SPeter Gruber 	}
2080a693a26fSTakashi Iwai 	if (opl3_port[dev]) {
2081a693a26fSTakashi Iwai 		val = opl3_port[dev];
2082a693a26fSTakashi Iwai 		pci_write_config_word(chip->pci, PCI_EXT_FM_Base, val);
2083a693a26fSTakashi Iwai 		err = snd_opl3_create(card, val, val + 2,
2084a693a26fSTakashi Iwai 				      OPL3_HW_RIPTIDE, 0, &chip->opl3);
2085a693a26fSTakashi Iwai 		if (err < 0)
2086109a9638SPeter Gruber 			snd_printk(KERN_WARNING
2087109a9638SPeter Gruber 				   "Riptide: Can't Allocate OPL3 at 0x%x\n",
2088a693a26fSTakashi Iwai 				   val);
2089109a9638SPeter Gruber 		else {
2090a693a26fSTakashi Iwai 			chip->opladdr = val;
2091a693a26fSTakashi Iwai 			err = snd_opl3_hwdep_new(chip->opl3, 0, 1, NULL);
2092a693a26fSTakashi Iwai 			if (err < 0)
2093109a9638SPeter Gruber 				snd_printk(KERN_WARNING
2094109a9638SPeter Gruber 					   "Riptide: Can't Allocate OPL3-HWDEP\n");
2095109a9638SPeter Gruber 		}
2096109a9638SPeter Gruber 	}
2097109a9638SPeter Gruber #ifdef SUPPORT_JOYSTICK
2098a693a26fSTakashi Iwai 	if (joystick_port[dev]) {
2099a693a26fSTakashi Iwai 		val = joystick_port[dev];
2100a693a26fSTakashi Iwai 		pci_write_config_word(chip->pci, PCI_EXT_Game_Base, val);
2101a693a26fSTakashi Iwai 		chip->gameaddr = val;
2102109a9638SPeter Gruber 	}
2103109a9638SPeter Gruber #endif
2104109a9638SPeter Gruber 
2105109a9638SPeter Gruber 	strcpy(card->driver, "RIPTIDE");
2106109a9638SPeter Gruber 	strcpy(card->shortname, "Riptide");
2107109a9638SPeter Gruber #ifdef SUPPORT_JOYSTICK
2108*322e0c50STakashi Iwai 	scnprintf(card->longname, sizeof(card->longname),
2109109a9638SPeter Gruber 		  "%s at 0x%lx, irq %i mpu 0x%x opl3 0x%x gameport 0x%x",
2110109a9638SPeter Gruber 		  card->shortname, chip->port, chip->irq, chip->mpuaddr,
2111109a9638SPeter Gruber 		  chip->opladdr, chip->gameaddr);
2112109a9638SPeter Gruber #else
2113*322e0c50STakashi Iwai 	scnprintf(card->longname, sizeof(card->longname),
2114109a9638SPeter Gruber 		  "%s at 0x%lx, irq %i mpu 0x%x opl3 0x%x",
2115109a9638SPeter Gruber 		  card->shortname, chip->port, chip->irq, chip->mpuaddr,
2116109a9638SPeter Gruber 		  chip->opladdr);
2117109a9638SPeter Gruber #endif
2118109a9638SPeter Gruber 	snd_riptide_proc_init(chip);
2119a693a26fSTakashi Iwai 	err = snd_card_register(card);
2120a693a26fSTakashi Iwai 	if (err < 0)
2121546c201aSTakashi Iwai 		return err;
2122109a9638SPeter Gruber 	pci_set_drvdata(pci, card);
2123109a9638SPeter Gruber 	dev++;
2124109a9638SPeter Gruber 	return 0;
2125109a9638SPeter Gruber }
2126109a9638SPeter Gruber 
2127348f08deSTakashi Iwai static int
snd_card_riptide_probe(struct pci_dev * pci,const struct pci_device_id * pci_id)2128348f08deSTakashi Iwai snd_card_riptide_probe(struct pci_dev *pci, const struct pci_device_id *pci_id)
2129348f08deSTakashi Iwai {
2130348f08deSTakashi Iwai 	return snd_card_free_on_error(&pci->dev, __snd_card_riptide_probe(pci, pci_id));
2131348f08deSTakashi Iwai }
2132348f08deSTakashi Iwai 
2133109a9638SPeter Gruber static struct pci_driver driver = {
21343733e424STakashi Iwai 	.name = KBUILD_MODNAME,
2135109a9638SPeter Gruber 	.id_table = snd_riptide_ids,
2136109a9638SPeter Gruber 	.probe = snd_card_riptide_probe,
213768cb2b55STakashi Iwai 	.driver = {
213868cb2b55STakashi Iwai 		.pm = RIPTIDE_PM_OPS,
213968cb2b55STakashi Iwai 	},
2140109a9638SPeter Gruber };
2141109a9638SPeter Gruber 
2142109a9638SPeter Gruber #ifdef SUPPORT_JOYSTICK
2143109a9638SPeter Gruber static struct pci_driver joystick_driver = {
21443733e424STakashi Iwai 	.name = KBUILD_MODNAME "-joystick",
2145109a9638SPeter Gruber 	.id_table = snd_riptide_joystick_ids,
2146109a9638SPeter Gruber 	.probe = snd_riptide_joystick_probe,
2147e23e7a14SBill Pemberton 	.remove = snd_riptide_joystick_remove,
2148109a9638SPeter Gruber };
2149109a9638SPeter Gruber #endif
2150109a9638SPeter Gruber 
alsa_card_riptide_init(void)2151109a9638SPeter Gruber static int __init alsa_card_riptide_init(void)
2152109a9638SPeter Gruber {
2153109a9638SPeter Gruber 	int err;
2154db1005ecSTakashi Iwai 	err = pci_register_driver(&driver);
2155db1005ecSTakashi Iwai 	if (err < 0)
2156109a9638SPeter Gruber 		return err;
2157109a9638SPeter Gruber #if defined(SUPPORT_JOYSTICK)
2158cb65c873SJaswinder Singh Rajput 	err = pci_register_driver(&joystick_driver);
2159cb65c873SJaswinder Singh Rajput 	/* On failure unregister formerly registered audio driver */
2160cb65c873SJaswinder Singh Rajput 	if (err < 0)
2161cb65c873SJaswinder Singh Rajput 		pci_unregister_driver(&driver);
2162109a9638SPeter Gruber #endif
2163cb65c873SJaswinder Singh Rajput 	return err;
2164109a9638SPeter Gruber }
2165109a9638SPeter Gruber 
alsa_card_riptide_exit(void)2166109a9638SPeter Gruber static void __exit alsa_card_riptide_exit(void)
2167109a9638SPeter Gruber {
2168109a9638SPeter Gruber 	pci_unregister_driver(&driver);
2169109a9638SPeter Gruber #if defined(SUPPORT_JOYSTICK)
2170109a9638SPeter Gruber 	pci_unregister_driver(&joystick_driver);
2171109a9638SPeter Gruber #endif
2172109a9638SPeter Gruber }
2173109a9638SPeter Gruber 
2174109a9638SPeter Gruber module_init(alsa_card_riptide_init);
2175109a9638SPeter Gruber module_exit(alsa_card_riptide_exit);
2176