18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci *   Driver for the Conexant Riptide Soundchip
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci *	Copyright (c) 2004 Peter Gruber <nokos@gmx.net>
68c2ecf20Sopenharmony_ci */
78c2ecf20Sopenharmony_ci/*
88c2ecf20Sopenharmony_ci  History:
98c2ecf20Sopenharmony_ci   - 02/15/2004 first release
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_ci  This Driver is based on the OSS Driver version from Linuxant (riptide-0.6lnxtbeta03111100)
128c2ecf20Sopenharmony_ci  credits from the original files:
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_ci  MODULE NAME:        cnxt_rt.h
158c2ecf20Sopenharmony_ci  AUTHOR:             K. Lazarev  (Transcribed by KNL)
168c2ecf20Sopenharmony_ci  HISTORY:         Major Revision               Date        By
178c2ecf20Sopenharmony_ci            -----------------------------     --------     -----
188c2ecf20Sopenharmony_ci            Created                           02/1/2000     KNL
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_ci  MODULE NAME:     int_mdl.c
218c2ecf20Sopenharmony_ci  AUTHOR:          Konstantin Lazarev    (Transcribed by KNL)
228c2ecf20Sopenharmony_ci  HISTORY:         Major Revision               Date        By
238c2ecf20Sopenharmony_ci            -----------------------------     --------     -----
248c2ecf20Sopenharmony_ci            Created                           10/01/99      KNL
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_ci  MODULE NAME:        riptide.h
278c2ecf20Sopenharmony_ci  AUTHOR:             O. Druzhinin  (Transcribed by OLD)
288c2ecf20Sopenharmony_ci  HISTORY:         Major Revision               Date        By
298c2ecf20Sopenharmony_ci            -----------------------------     --------     -----
308c2ecf20Sopenharmony_ci            Created                           10/16/97      OLD
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_ci  MODULE NAME:        Rp_Cmdif.cpp
338c2ecf20Sopenharmony_ci  AUTHOR:             O. Druzhinin  (Transcribed by OLD)
348c2ecf20Sopenharmony_ci                      K. Lazarev    (Transcribed by KNL)
358c2ecf20Sopenharmony_ci  HISTORY:         Major Revision               Date        By
368c2ecf20Sopenharmony_ci            -----------------------------     --------     -----
378c2ecf20Sopenharmony_ci            Adopted from NT4 driver            6/22/99      OLD
388c2ecf20Sopenharmony_ci            Ported to Linux                    9/01/99      KNL
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_ci  MODULE NAME:        rt_hw.c
418c2ecf20Sopenharmony_ci  AUTHOR:             O. Druzhinin  (Transcribed by OLD)
428c2ecf20Sopenharmony_ci                      C. Lazarev    (Transcribed by CNL)
438c2ecf20Sopenharmony_ci  HISTORY:         Major Revision               Date        By
448c2ecf20Sopenharmony_ci            -----------------------------     --------     -----
458c2ecf20Sopenharmony_ci            Created                           11/18/97      OLD
468c2ecf20Sopenharmony_ci            Hardware functions for RipTide    11/24/97      CNL
478c2ecf20Sopenharmony_ci            (ES1) are coded
488c2ecf20Sopenharmony_ci            Hardware functions for RipTide    12/24/97      CNL
498c2ecf20Sopenharmony_ci            (A0) are coded
508c2ecf20Sopenharmony_ci            Hardware functions for RipTide    03/20/98      CNL
518c2ecf20Sopenharmony_ci            (A1) are coded
528c2ecf20Sopenharmony_ci            Boot loader is included           05/07/98      CNL
538c2ecf20Sopenharmony_ci            Redesigned for WDM                07/27/98      CNL
548c2ecf20Sopenharmony_ci            Redesigned for Linux              09/01/99      CNL
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_ci  MODULE NAME:        rt_hw.h
578c2ecf20Sopenharmony_ci  AUTHOR:             C. Lazarev    (Transcribed by CNL)
588c2ecf20Sopenharmony_ci  HISTORY:         Major Revision               Date        By
598c2ecf20Sopenharmony_ci            -----------------------------     --------     -----
608c2ecf20Sopenharmony_ci            Created                           11/18/97      CNL
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_ci  MODULE NAME:     rt_mdl.c
638c2ecf20Sopenharmony_ci  AUTHOR:          Konstantin Lazarev    (Transcribed by KNL)
648c2ecf20Sopenharmony_ci  HISTORY:         Major Revision               Date        By
658c2ecf20Sopenharmony_ci            -----------------------------     --------     -----
668c2ecf20Sopenharmony_ci            Created                           10/01/99      KNL
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_ci  MODULE NAME:        mixer.h
698c2ecf20Sopenharmony_ci  AUTHOR:             K. Kenney
708c2ecf20Sopenharmony_ci  HISTORY:         Major Revision                   Date          By
718c2ecf20Sopenharmony_ci            -----------------------------          --------     -----
728c2ecf20Sopenharmony_ci            Created from MS W95 Sample             11/28/95      KRS
738c2ecf20Sopenharmony_ci            RipTide                                10/15/97      KRS
748c2ecf20Sopenharmony_ci            Adopted for Windows NT driver          01/20/98      CNL
758c2ecf20Sopenharmony_ci*/
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_ci#include <linux/delay.h>
788c2ecf20Sopenharmony_ci#include <linux/init.h>
798c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
808c2ecf20Sopenharmony_ci#include <linux/pci.h>
818c2ecf20Sopenharmony_ci#include <linux/slab.h>
828c2ecf20Sopenharmony_ci#include <linux/wait.h>
838c2ecf20Sopenharmony_ci#include <linux/gameport.h>
848c2ecf20Sopenharmony_ci#include <linux/device.h>
858c2ecf20Sopenharmony_ci#include <linux/firmware.h>
868c2ecf20Sopenharmony_ci#include <linux/kernel.h>
878c2ecf20Sopenharmony_ci#include <linux/module.h>
888c2ecf20Sopenharmony_ci#include <linux/io.h>
898c2ecf20Sopenharmony_ci#include <sound/core.h>
908c2ecf20Sopenharmony_ci#include <sound/info.h>
918c2ecf20Sopenharmony_ci#include <sound/control.h>
928c2ecf20Sopenharmony_ci#include <sound/pcm.h>
938c2ecf20Sopenharmony_ci#include <sound/pcm_params.h>
948c2ecf20Sopenharmony_ci#include <sound/ac97_codec.h>
958c2ecf20Sopenharmony_ci#include <sound/mpu401.h>
968c2ecf20Sopenharmony_ci#include <sound/opl3.h>
978c2ecf20Sopenharmony_ci#include <sound/initval.h>
988c2ecf20Sopenharmony_ci
998c2ecf20Sopenharmony_ci#if IS_REACHABLE(CONFIG_GAMEPORT)
1008c2ecf20Sopenharmony_ci#define SUPPORT_JOYSTICK 1
1018c2ecf20Sopenharmony_ci#endif
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_ciMODULE_AUTHOR("Peter Gruber <nokos@gmx.net>");
1048c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("riptide");
1058c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
1068c2ecf20Sopenharmony_ciMODULE_SUPPORTED_DEVICE("{{Conexant,Riptide}}");
1078c2ecf20Sopenharmony_ciMODULE_FIRMWARE("riptide.hex");
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_cistatic int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
1108c2ecf20Sopenharmony_cistatic char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
1118c2ecf20Sopenharmony_cistatic bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE;
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_ci#ifdef SUPPORT_JOYSTICK
1148c2ecf20Sopenharmony_cistatic int joystick_port[SNDRV_CARDS] = { [0 ... (SNDRV_CARDS - 1)] = 0x200 };
1158c2ecf20Sopenharmony_ci#endif
1168c2ecf20Sopenharmony_cistatic int mpu_port[SNDRV_CARDS] = { [0 ... (SNDRV_CARDS - 1)] = 0x330 };
1178c2ecf20Sopenharmony_cistatic int opl3_port[SNDRV_CARDS] = { [0 ... (SNDRV_CARDS - 1)] = 0x388 };
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_cimodule_param_array(index, int, NULL, 0444);
1208c2ecf20Sopenharmony_ciMODULE_PARM_DESC(index, "Index value for Riptide soundcard.");
1218c2ecf20Sopenharmony_cimodule_param_array(id, charp, NULL, 0444);
1228c2ecf20Sopenharmony_ciMODULE_PARM_DESC(id, "ID string for Riptide soundcard.");
1238c2ecf20Sopenharmony_cimodule_param_array(enable, bool, NULL, 0444);
1248c2ecf20Sopenharmony_ciMODULE_PARM_DESC(enable, "Enable Riptide soundcard.");
1258c2ecf20Sopenharmony_ci#ifdef SUPPORT_JOYSTICK
1268c2ecf20Sopenharmony_cimodule_param_hw_array(joystick_port, int, ioport, NULL, 0444);
1278c2ecf20Sopenharmony_ciMODULE_PARM_DESC(joystick_port, "Joystick port # for Riptide soundcard.");
1288c2ecf20Sopenharmony_ci#endif
1298c2ecf20Sopenharmony_cimodule_param_hw_array(mpu_port, int, ioport, NULL, 0444);
1308c2ecf20Sopenharmony_ciMODULE_PARM_DESC(mpu_port, "MPU401 port # for Riptide driver.");
1318c2ecf20Sopenharmony_cimodule_param_hw_array(opl3_port, int, ioport, NULL, 0444);
1328c2ecf20Sopenharmony_ciMODULE_PARM_DESC(opl3_port, "OPL3 port # for Riptide driver.");
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_ci/*
1358c2ecf20Sopenharmony_ci */
1368c2ecf20Sopenharmony_ci
1378c2ecf20Sopenharmony_ci#define MPU401_HW_RIPTIDE MPU401_HW_MPU401
1388c2ecf20Sopenharmony_ci#define OPL3_HW_RIPTIDE   OPL3_HW_OPL3
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_ci#define PCI_EXT_CapId       0x40
1418c2ecf20Sopenharmony_ci#define PCI_EXT_NextCapPrt  0x41
1428c2ecf20Sopenharmony_ci#define PCI_EXT_PWMC        0x42
1438c2ecf20Sopenharmony_ci#define PCI_EXT_PWSCR       0x44
1448c2ecf20Sopenharmony_ci#define PCI_EXT_Data00      0x46
1458c2ecf20Sopenharmony_ci#define PCI_EXT_PMSCR_BSE   0x47
1468c2ecf20Sopenharmony_ci#define PCI_EXT_SB_Base     0x48
1478c2ecf20Sopenharmony_ci#define PCI_EXT_FM_Base     0x4a
1488c2ecf20Sopenharmony_ci#define PCI_EXT_MPU_Base    0x4C
1498c2ecf20Sopenharmony_ci#define PCI_EXT_Game_Base   0x4E
1508c2ecf20Sopenharmony_ci#define PCI_EXT_Legacy_Mask 0x50
1518c2ecf20Sopenharmony_ci#define PCI_EXT_AsicRev     0x52
1528c2ecf20Sopenharmony_ci#define PCI_EXT_Reserved3   0x53
1538c2ecf20Sopenharmony_ci
1548c2ecf20Sopenharmony_ci#define LEGACY_ENABLE_ALL      0x8000	/* legacy device options */
1558c2ecf20Sopenharmony_ci#define LEGACY_ENABLE_SB       0x4000
1568c2ecf20Sopenharmony_ci#define LEGACY_ENABLE_FM       0x2000
1578c2ecf20Sopenharmony_ci#define LEGACY_ENABLE_MPU_INT  0x1000
1588c2ecf20Sopenharmony_ci#define LEGACY_ENABLE_MPU      0x0800
1598c2ecf20Sopenharmony_ci#define LEGACY_ENABLE_GAMEPORT 0x0400
1608c2ecf20Sopenharmony_ci
1618c2ecf20Sopenharmony_ci#define MAX_WRITE_RETRY  10	/* cmd interface limits */
1628c2ecf20Sopenharmony_ci#define MAX_ERROR_COUNT  10
1638c2ecf20Sopenharmony_ci#define CMDIF_TIMEOUT    50000
1648c2ecf20Sopenharmony_ci#define RESET_TRIES      5
1658c2ecf20Sopenharmony_ci
1668c2ecf20Sopenharmony_ci#define READ_PORT_ULONG(p)     inl((unsigned long)&(p))
1678c2ecf20Sopenharmony_ci#define WRITE_PORT_ULONG(p,x)  outl(x,(unsigned long)&(p))
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_ci#define READ_AUDIO_CONTROL(p)     READ_PORT_ULONG(p->audio_control)
1708c2ecf20Sopenharmony_ci#define WRITE_AUDIO_CONTROL(p,x)  WRITE_PORT_ULONG(p->audio_control,x)
1718c2ecf20Sopenharmony_ci#define UMASK_AUDIO_CONTROL(p,x)  WRITE_PORT_ULONG(p->audio_control,READ_PORT_ULONG(p->audio_control)|x)
1728c2ecf20Sopenharmony_ci#define MASK_AUDIO_CONTROL(p,x)   WRITE_PORT_ULONG(p->audio_control,READ_PORT_ULONG(p->audio_control)&x)
1738c2ecf20Sopenharmony_ci#define READ_AUDIO_STATUS(p)      READ_PORT_ULONG(p->audio_status)
1748c2ecf20Sopenharmony_ci
1758c2ecf20Sopenharmony_ci#define SET_GRESET(p)     UMASK_AUDIO_CONTROL(p,0x0001)	/* global reset switch */
1768c2ecf20Sopenharmony_ci#define UNSET_GRESET(p)   MASK_AUDIO_CONTROL(p,~0x0001)
1778c2ecf20Sopenharmony_ci#define SET_AIE(p)        UMASK_AUDIO_CONTROL(p,0x0004)	/* interrupt enable */
1788c2ecf20Sopenharmony_ci#define UNSET_AIE(p)      MASK_AUDIO_CONTROL(p,~0x0004)
1798c2ecf20Sopenharmony_ci#define SET_AIACK(p)      UMASK_AUDIO_CONTROL(p,0x0008)	/* interrupt acknowledge */
1808c2ecf20Sopenharmony_ci#define UNSET_AIACKT(p)   MASKAUDIO_CONTROL(p,~0x0008)
1818c2ecf20Sopenharmony_ci#define SET_ECMDAE(p)     UMASK_AUDIO_CONTROL(p,0x0010)
1828c2ecf20Sopenharmony_ci#define UNSET_ECMDAE(p)   MASK_AUDIO_CONTROL(p,~0x0010)
1838c2ecf20Sopenharmony_ci#define SET_ECMDBE(p)     UMASK_AUDIO_CONTROL(p,0x0020)
1848c2ecf20Sopenharmony_ci#define UNSET_ECMDBE(p)   MASK_AUDIO_CONTROL(p,~0x0020)
1858c2ecf20Sopenharmony_ci#define SET_EDATAF(p)     UMASK_AUDIO_CONTROL(p,0x0040)
1868c2ecf20Sopenharmony_ci#define UNSET_EDATAF(p)   MASK_AUDIO_CONTROL(p,~0x0040)
1878c2ecf20Sopenharmony_ci#define SET_EDATBF(p)     UMASK_AUDIO_CONTROL(p,0x0080)
1888c2ecf20Sopenharmony_ci#define UNSET_EDATBF(p)   MASK_AUDIO_CONTROL(p,~0x0080)
1898c2ecf20Sopenharmony_ci#define SET_ESBIRQON(p)   UMASK_AUDIO_CONTROL(p,0x0100)
1908c2ecf20Sopenharmony_ci#define UNSET_ESBIRQON(p) MASK_AUDIO_CONTROL(p,~0x0100)
1918c2ecf20Sopenharmony_ci#define SET_EMPUIRQ(p)    UMASK_AUDIO_CONTROL(p,0x0200)
1928c2ecf20Sopenharmony_ci#define UNSET_EMPUIRQ(p)  MASK_AUDIO_CONTROL(p,~0x0200)
1938c2ecf20Sopenharmony_ci#define IS_CMDE(a)        (READ_PORT_ULONG(a->stat)&0x1)	/* cmd empty */
1948c2ecf20Sopenharmony_ci#define IS_DATF(a)        (READ_PORT_ULONG(a->stat)&0x2)	/* data filled */
1958c2ecf20Sopenharmony_ci#define IS_READY(p)       (READ_AUDIO_STATUS(p)&0x0001)
1968c2ecf20Sopenharmony_ci#define IS_DLREADY(p)     (READ_AUDIO_STATUS(p)&0x0002)
1978c2ecf20Sopenharmony_ci#define IS_DLERR(p)       (READ_AUDIO_STATUS(p)&0x0004)
1988c2ecf20Sopenharmony_ci#define IS_GERR(p)        (READ_AUDIO_STATUS(p)&0x0008)	/* error ! */
1998c2ecf20Sopenharmony_ci#define IS_CMDAEIRQ(p)    (READ_AUDIO_STATUS(p)&0x0010)
2008c2ecf20Sopenharmony_ci#define IS_CMDBEIRQ(p)    (READ_AUDIO_STATUS(p)&0x0020)
2018c2ecf20Sopenharmony_ci#define IS_DATAFIRQ(p)    (READ_AUDIO_STATUS(p)&0x0040)
2028c2ecf20Sopenharmony_ci#define IS_DATBFIRQ(p)    (READ_AUDIO_STATUS(p)&0x0080)
2038c2ecf20Sopenharmony_ci#define IS_EOBIRQ(p)      (READ_AUDIO_STATUS(p)&0x0100)	/* interrupt status */
2048c2ecf20Sopenharmony_ci#define IS_EOSIRQ(p)      (READ_AUDIO_STATUS(p)&0x0200)
2058c2ecf20Sopenharmony_ci#define IS_EOCIRQ(p)      (READ_AUDIO_STATUS(p)&0x0400)
2068c2ecf20Sopenharmony_ci#define IS_UNSLIRQ(p)     (READ_AUDIO_STATUS(p)&0x0800)
2078c2ecf20Sopenharmony_ci#define IS_SBIRQ(p)       (READ_AUDIO_STATUS(p)&0x1000)
2088c2ecf20Sopenharmony_ci#define IS_MPUIRQ(p)      (READ_AUDIO_STATUS(p)&0x2000)
2098c2ecf20Sopenharmony_ci
2108c2ecf20Sopenharmony_ci#define RESP 0x00000001		/* command flags */
2118c2ecf20Sopenharmony_ci#define PARM 0x00000002
2128c2ecf20Sopenharmony_ci#define CMDA 0x00000004
2138c2ecf20Sopenharmony_ci#define CMDB 0x00000008
2148c2ecf20Sopenharmony_ci#define NILL 0x00000000
2158c2ecf20Sopenharmony_ci
2168c2ecf20Sopenharmony_ci#define LONG0(a)   ((u32)a)	/* shifts and masks */
2178c2ecf20Sopenharmony_ci#define BYTE0(a)   (LONG0(a)&0xff)
2188c2ecf20Sopenharmony_ci#define BYTE1(a)   (BYTE0(a)<<8)
2198c2ecf20Sopenharmony_ci#define BYTE2(a)   (BYTE0(a)<<16)
2208c2ecf20Sopenharmony_ci#define BYTE3(a)   (BYTE0(a)<<24)
2218c2ecf20Sopenharmony_ci#define WORD0(a)   (LONG0(a)&0xffff)
2228c2ecf20Sopenharmony_ci#define WORD1(a)   (WORD0(a)<<8)
2238c2ecf20Sopenharmony_ci#define WORD2(a)   (WORD0(a)<<16)
2248c2ecf20Sopenharmony_ci#define TRINIB0(a) (LONG0(a)&0xffffff)
2258c2ecf20Sopenharmony_ci#define TRINIB1(a) (TRINIB0(a)<<8)
2268c2ecf20Sopenharmony_ci
2278c2ecf20Sopenharmony_ci#define RET(a)     ((union cmdret *)(a))
2288c2ecf20Sopenharmony_ci
2298c2ecf20Sopenharmony_ci#define SEND_GETV(p,b)             sendcmd(p,RESP,GETV,0,RET(b))	/* get version */
2308c2ecf20Sopenharmony_ci#define SEND_GETC(p,b,c)           sendcmd(p,PARM|RESP,GETC,c,RET(b))
2318c2ecf20Sopenharmony_ci#define SEND_GUNS(p,b)             sendcmd(p,RESP,GUNS,0,RET(b))
2328c2ecf20Sopenharmony_ci#define SEND_SCID(p,b)             sendcmd(p,RESP,SCID,0,RET(b))
2338c2ecf20Sopenharmony_ci#define SEND_RMEM(p,b,c,d)         sendcmd(p,PARM|RESP,RMEM|BYTE1(b),LONG0(c),RET(d))	/* memory access for firmware write */
2348c2ecf20Sopenharmony_ci#define SEND_SMEM(p,b,c)           sendcmd(p,PARM,SMEM|BYTE1(b),LONG0(c),RET(0))	/* memory access for firmware write */
2358c2ecf20Sopenharmony_ci#define SEND_WMEM(p,b,c)           sendcmd(p,PARM,WMEM|BYTE1(b),LONG0(c),RET(0))	/* memory access for firmware write */
2368c2ecf20Sopenharmony_ci#define SEND_SDTM(p,b,c)           sendcmd(p,PARM|RESP,SDTM|TRINIB1(b),0,RET(c))	/* memory access for firmware write */
2378c2ecf20Sopenharmony_ci#define SEND_GOTO(p,b)             sendcmd(p,PARM,GOTO,LONG0(b),RET(0))	/* memory access for firmware write */
2388c2ecf20Sopenharmony_ci#define SEND_SETDPLL(p)	           sendcmd(p,0,ARM_SETDPLL,0,RET(0))
2398c2ecf20Sopenharmony_ci#define SEND_SSTR(p,b,c)           sendcmd(p,PARM,SSTR|BYTE3(b),LONG0(c),RET(0))	/* start stream */
2408c2ecf20Sopenharmony_ci#define SEND_PSTR(p,b)             sendcmd(p,PARM,PSTR,BYTE3(b),RET(0))	/* pause stream */
2418c2ecf20Sopenharmony_ci#define SEND_KSTR(p,b)             sendcmd(p,PARM,KSTR,BYTE3(b),RET(0))	/* stop stream */
2428c2ecf20Sopenharmony_ci#define SEND_KDMA(p)               sendcmd(p,0,KDMA,0,RET(0))	/* stop all dma */
2438c2ecf20Sopenharmony_ci#define SEND_GPOS(p,b,c,d)         sendcmd(p,PARM|RESP,GPOS,BYTE3(c)|BYTE2(b),RET(d))	/* get position in dma */
2448c2ecf20Sopenharmony_ci#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 */
2458c2ecf20Sopenharmony_ci#define SEND_GSTS(p,b,c,d)         sendcmd(p,PARM|RESP,GSTS,BYTE3(c)|BYTE2(b),RET(d))
2468c2ecf20Sopenharmony_ci#define SEND_NGPOS(p,b,c,d)        sendcmd(p,PARM|RESP,NGPOS,BYTE3(c)|BYTE2(b),RET(d))
2478c2ecf20Sopenharmony_ci#define SEND_PSEL(p,b,c)           sendcmd(p,PARM,PSEL,BYTE2(b)|BYTE3(c),RET(0))	/* activate lbus path */
2488c2ecf20Sopenharmony_ci#define SEND_PCLR(p,b,c)           sendcmd(p,PARM,PCLR,BYTE2(b)|BYTE3(c),RET(0))	/* deactivate lbus path */
2498c2ecf20Sopenharmony_ci#define SEND_PLST(p,b)             sendcmd(p,PARM,PLST,BYTE3(b),RET(0))
2508c2ecf20Sopenharmony_ci#define SEND_RSSV(p,b,c,d)         sendcmd(p,PARM|RESP,RSSV,BYTE2(b)|BYTE3(c),RET(d))
2518c2ecf20Sopenharmony_ci#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 */
2528c2ecf20Sopenharmony_ci#define SEND_SSRC(p,b,c,d,e)       sendcmd(p,PARM,SSRC|BYTE1(b)|WORD2(c),WORD0(d)|WORD2(e),RET(0))	/* configure source */
2538c2ecf20Sopenharmony_ci#define SEND_SLST(p,b)             sendcmd(p,PARM,SLST,BYTE3(b),RET(0))
2548c2ecf20Sopenharmony_ci#define SEND_RSRC(p,b,c)           sendcmd(p,RESP,RSRC|BYTE1(b),0,RET(c))	/* read source config */
2558c2ecf20Sopenharmony_ci#define SEND_SSRB(p,b,c)           sendcmd(p,PARM,SSRB|BYTE1(b),WORD2(c),RET(0))
2568c2ecf20Sopenharmony_ci#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 */
2578c2ecf20Sopenharmony_ci#define SEND_RDGV(p,b,c,d)         sendcmd(p,PARM|RESP,RDGV|BYTE2(b)|BYTE3(c),0,RET(d))	/* read digital mixer */
2588c2ecf20Sopenharmony_ci#define SEND_DLST(p,b)             sendcmd(p,PARM,DLST,BYTE3(b),RET(0))
2598c2ecf20Sopenharmony_ci#define SEND_SACR(p,b,c)           sendcmd(p,PARM,SACR,WORD0(b)|WORD2(c),RET(0))	/* set AC97 register */
2608c2ecf20Sopenharmony_ci#define SEND_RACR(p,b,c)           sendcmd(p,PARM|RESP,RACR,WORD2(b),RET(c))	/* get AC97 register */
2618c2ecf20Sopenharmony_ci#define SEND_ALST(p,b)             sendcmd(p,PARM,ALST,BYTE3(b),RET(0))
2628c2ecf20Sopenharmony_ci#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))
2638c2ecf20Sopenharmony_ci#define SEND_RXAC(p,b,c,d)         sendcmd(p,PARM|RESP,RXAC,BYTE2(b)|BYTE3(c),RET(d))
2648c2ecf20Sopenharmony_ci#define SEND_SI2S(p,b)             sendcmd(p,PARM,SI2S,WORD2(b),RET(0))
2658c2ecf20Sopenharmony_ci
2668c2ecf20Sopenharmony_ci#define EOB_STATUS         0x80000000	/* status flags : block boundary */
2678c2ecf20Sopenharmony_ci#define EOS_STATUS         0x40000000	/*              : stoppped */
2688c2ecf20Sopenharmony_ci#define EOC_STATUS         0x20000000	/*              : stream end */
2698c2ecf20Sopenharmony_ci#define ERR_STATUS         0x10000000
2708c2ecf20Sopenharmony_ci#define EMPTY_STATUS       0x08000000
2718c2ecf20Sopenharmony_ci
2728c2ecf20Sopenharmony_ci#define IEOB_ENABLE        0x1	/* enable interrupts for status notification above */
2738c2ecf20Sopenharmony_ci#define IEOS_ENABLE        0x2
2748c2ecf20Sopenharmony_ci#define IEOC_ENABLE        0x4
2758c2ecf20Sopenharmony_ci#define RDONCE             0x8
2768c2ecf20Sopenharmony_ci#define DESC_MAX_MASK      0xff
2778c2ecf20Sopenharmony_ci
2788c2ecf20Sopenharmony_ci#define ST_PLAY  0x1		/* stream states */
2798c2ecf20Sopenharmony_ci#define ST_STOP  0x2
2808c2ecf20Sopenharmony_ci#define ST_PAUSE 0x4
2818c2ecf20Sopenharmony_ci
2828c2ecf20Sopenharmony_ci#define I2S_INTDEC     3	/* config for I2S link */
2838c2ecf20Sopenharmony_ci#define I2S_MERGER     0
2848c2ecf20Sopenharmony_ci#define I2S_SPLITTER   0
2858c2ecf20Sopenharmony_ci#define I2S_MIXER      7
2868c2ecf20Sopenharmony_ci#define I2S_RATE       44100
2878c2ecf20Sopenharmony_ci
2888c2ecf20Sopenharmony_ci#define MODEM_INTDEC   4	/* config for modem link */
2898c2ecf20Sopenharmony_ci#define MODEM_MERGER   3
2908c2ecf20Sopenharmony_ci#define MODEM_SPLITTER 0
2918c2ecf20Sopenharmony_ci#define MODEM_MIXER    11
2928c2ecf20Sopenharmony_ci
2938c2ecf20Sopenharmony_ci#define FM_INTDEC      3	/* config for FM/OPL3 link */
2948c2ecf20Sopenharmony_ci#define FM_MERGER      0
2958c2ecf20Sopenharmony_ci#define FM_SPLITTER    0
2968c2ecf20Sopenharmony_ci#define FM_MIXER       9
2978c2ecf20Sopenharmony_ci
2988c2ecf20Sopenharmony_ci#define SPLIT_PATH  0x80	/* path splitting flag */
2998c2ecf20Sopenharmony_ci
3008c2ecf20Sopenharmony_cienum FIRMWARE {
3018c2ecf20Sopenharmony_ci	DATA_REC = 0, EXT_END_OF_FILE, EXT_SEG_ADDR_REC, EXT_GOTO_CMD_REC,
3028c2ecf20Sopenharmony_ci	EXT_LIN_ADDR_REC,
3038c2ecf20Sopenharmony_ci};
3048c2ecf20Sopenharmony_ci
3058c2ecf20Sopenharmony_cienum CMDS {
3068c2ecf20Sopenharmony_ci	GETV = 0x00, GETC, GUNS, SCID, RMEM =
3078c2ecf20Sopenharmony_ci	    0x10, SMEM, WMEM, SDTM, GOTO, SSTR =
3088c2ecf20Sopenharmony_ci	    0x20, PSTR, KSTR, KDMA, GPOS, SETF, GSTS, NGPOS, PSEL =
3098c2ecf20Sopenharmony_ci	    0x30, PCLR, PLST, RSSV, LSEL, SSRC = 0x40, SLST, RSRC, SSRB, SDGV =
3108c2ecf20Sopenharmony_ci	    0x50, RDGV, DLST, SACR = 0x60, RACR, ALST, TXAC, RXAC, SI2S =
3118c2ecf20Sopenharmony_ci	    0x70, ARM_SETDPLL = 0x72,
3128c2ecf20Sopenharmony_ci};
3138c2ecf20Sopenharmony_ci
3148c2ecf20Sopenharmony_cienum E1SOURCE {
3158c2ecf20Sopenharmony_ci	ARM2LBUS_FIFO0 = 0, ARM2LBUS_FIFO1, ARM2LBUS_FIFO2, ARM2LBUS_FIFO3,
3168c2ecf20Sopenharmony_ci	ARM2LBUS_FIFO4, ARM2LBUS_FIFO5, ARM2LBUS_FIFO6, ARM2LBUS_FIFO7,
3178c2ecf20Sopenharmony_ci	ARM2LBUS_FIFO8, ARM2LBUS_FIFO9, ARM2LBUS_FIFO10, ARM2LBUS_FIFO11,
3188c2ecf20Sopenharmony_ci	ARM2LBUS_FIFO12, ARM2LBUS_FIFO13, ARM2LBUS_FIFO14, ARM2LBUS_FIFO15,
3198c2ecf20Sopenharmony_ci	INTER0_OUT, INTER1_OUT, INTER2_OUT, INTER3_OUT, INTER4_OUT,
3208c2ecf20Sopenharmony_ci	INTERM0_OUT, INTERM1_OUT, INTERM2_OUT, INTERM3_OUT, INTERM4_OUT,
3218c2ecf20Sopenharmony_ci	INTERM5_OUT, INTERM6_OUT, DECIMM0_OUT, DECIMM1_OUT, DECIMM2_OUT,
3228c2ecf20Sopenharmony_ci	DECIMM3_OUT, DECIM0_OUT, SR3_4_OUT, OPL3_SAMPLE, ASRC0, ASRC1,
3238c2ecf20Sopenharmony_ci	ACLNK2PADC, ACLNK2MODEM0RX, ACLNK2MIC, ACLNK2MODEM1RX, ACLNK2HNDMIC,
3248c2ecf20Sopenharmony_ci	DIGITAL_MIXER_OUT0, GAINFUNC0_OUT, GAINFUNC1_OUT, GAINFUNC2_OUT,
3258c2ecf20Sopenharmony_ci	GAINFUNC3_OUT, GAINFUNC4_OUT, SOFTMODEMTX, SPLITTER0_OUTL,
3268c2ecf20Sopenharmony_ci	SPLITTER0_OUTR, SPLITTER1_OUTL, SPLITTER1_OUTR, SPLITTER2_OUTL,
3278c2ecf20Sopenharmony_ci	SPLITTER2_OUTR, SPLITTER3_OUTL, SPLITTER3_OUTR, MERGER0_OUT,
3288c2ecf20Sopenharmony_ci	MERGER1_OUT, MERGER2_OUT, MERGER3_OUT, ARM2LBUS_FIFO_DIRECT, NO_OUT
3298c2ecf20Sopenharmony_ci};
3308c2ecf20Sopenharmony_ci
3318c2ecf20Sopenharmony_cienum E2SINK {
3328c2ecf20Sopenharmony_ci	LBUS2ARM_FIFO0 = 0, LBUS2ARM_FIFO1, LBUS2ARM_FIFO2, LBUS2ARM_FIFO3,
3338c2ecf20Sopenharmony_ci	LBUS2ARM_FIFO4, LBUS2ARM_FIFO5, LBUS2ARM_FIFO6, LBUS2ARM_FIFO7,
3348c2ecf20Sopenharmony_ci	INTER0_IN, INTER1_IN, INTER2_IN, INTER3_IN, INTER4_IN, INTERM0_IN,
3358c2ecf20Sopenharmony_ci	INTERM1_IN, INTERM2_IN, INTERM3_IN, INTERM4_IN, INTERM5_IN, INTERM6_IN,
3368c2ecf20Sopenharmony_ci	DECIMM0_IN, DECIMM1_IN, DECIMM2_IN, DECIMM3_IN, DECIM0_IN, SR3_4_IN,
3378c2ecf20Sopenharmony_ci	PDAC2ACLNK, MODEM0TX2ACLNK, MODEM1TX2ACLNK, HNDSPK2ACLNK,
3388c2ecf20Sopenharmony_ci	DIGITAL_MIXER_IN0, DIGITAL_MIXER_IN1, DIGITAL_MIXER_IN2,
3398c2ecf20Sopenharmony_ci	DIGITAL_MIXER_IN3, DIGITAL_MIXER_IN4, DIGITAL_MIXER_IN5,
3408c2ecf20Sopenharmony_ci	DIGITAL_MIXER_IN6, DIGITAL_MIXER_IN7, DIGITAL_MIXER_IN8,
3418c2ecf20Sopenharmony_ci	DIGITAL_MIXER_IN9, DIGITAL_MIXER_IN10, DIGITAL_MIXER_IN11,
3428c2ecf20Sopenharmony_ci	GAINFUNC0_IN, GAINFUNC1_IN, GAINFUNC2_IN, GAINFUNC3_IN, GAINFUNC4_IN,
3438c2ecf20Sopenharmony_ci	SOFTMODEMRX, SPLITTER0_IN, SPLITTER1_IN, SPLITTER2_IN, SPLITTER3_IN,
3448c2ecf20Sopenharmony_ci	MERGER0_INL, MERGER0_INR, MERGER1_INL, MERGER1_INR, MERGER2_INL,
3458c2ecf20Sopenharmony_ci	MERGER2_INR, MERGER3_INL, MERGER3_INR, E2SINK_MAX
3468c2ecf20Sopenharmony_ci};
3478c2ecf20Sopenharmony_ci
3488c2ecf20Sopenharmony_cienum LBUS_SINK {
3498c2ecf20Sopenharmony_ci	LS_SRC_INTERPOLATOR = 0, LS_SRC_INTERPOLATORM, LS_SRC_DECIMATOR,
3508c2ecf20Sopenharmony_ci	LS_SRC_DECIMATORM, LS_MIXER_IN, LS_MIXER_GAIN_FUNCTION,
3518c2ecf20Sopenharmony_ci	LS_SRC_SPLITTER, LS_SRC_MERGER, LS_NONE1, LS_NONE2,
3528c2ecf20Sopenharmony_ci};
3538c2ecf20Sopenharmony_ci
3548c2ecf20Sopenharmony_cienum RT_CHANNEL_IDS {
3558c2ecf20Sopenharmony_ci	M0TX = 0, M1TX, TAMTX, HSSPKR, PDAC, DSNDTX0, DSNDTX1, DSNDTX2,
3568c2ecf20Sopenharmony_ci	DSNDTX3, DSNDTX4, DSNDTX5, DSNDTX6, DSNDTX7, WVSTRTX, COP3DTX, SPARE,
3578c2ecf20Sopenharmony_ci	M0RX, HSMIC, M1RX, CLEANRX, MICADC, PADC, COPRX1, COPRX2,
3588c2ecf20Sopenharmony_ci	CHANNEL_ID_COUNTER
3598c2ecf20Sopenharmony_ci};
3608c2ecf20Sopenharmony_ci
3618c2ecf20Sopenharmony_cienum { SB_CMD = 0, MODEM_CMD, I2S_CMD0, I2S_CMD1, FM_CMD, MAX_CMD };
3628c2ecf20Sopenharmony_ci
3638c2ecf20Sopenharmony_cistruct lbuspath {
3648c2ecf20Sopenharmony_ci	const unsigned char *noconv;
3658c2ecf20Sopenharmony_ci	const unsigned char *stereo;
3668c2ecf20Sopenharmony_ci	const unsigned char *mono;
3678c2ecf20Sopenharmony_ci};
3688c2ecf20Sopenharmony_ci
3698c2ecf20Sopenharmony_cistruct cmdport {
3708c2ecf20Sopenharmony_ci	u32 data1;		/* cmd,param */
3718c2ecf20Sopenharmony_ci	u32 data2;		/* param */
3728c2ecf20Sopenharmony_ci	u32 stat;		/* status */
3738c2ecf20Sopenharmony_ci	u32 pad[5];
3748c2ecf20Sopenharmony_ci};
3758c2ecf20Sopenharmony_ci
3768c2ecf20Sopenharmony_cistruct riptideport {
3778c2ecf20Sopenharmony_ci	u32 audio_control;	/* status registers */
3788c2ecf20Sopenharmony_ci	u32 audio_status;
3798c2ecf20Sopenharmony_ci	u32 pad[2];
3808c2ecf20Sopenharmony_ci	struct cmdport port[2];	/* command ports */
3818c2ecf20Sopenharmony_ci};
3828c2ecf20Sopenharmony_ci
3838c2ecf20Sopenharmony_cistruct cmdif {
3848c2ecf20Sopenharmony_ci	struct riptideport *hwport;
3858c2ecf20Sopenharmony_ci	spinlock_t lock;
3868c2ecf20Sopenharmony_ci	unsigned int cmdcnt;	/* cmd statistics */
3878c2ecf20Sopenharmony_ci	unsigned int cmdtime;
3888c2ecf20Sopenharmony_ci	unsigned int cmdtimemax;
3898c2ecf20Sopenharmony_ci	unsigned int cmdtimemin;
3908c2ecf20Sopenharmony_ci	unsigned int errcnt;
3918c2ecf20Sopenharmony_ci	int is_reset;
3928c2ecf20Sopenharmony_ci};
3938c2ecf20Sopenharmony_ci
3948c2ecf20Sopenharmony_cistruct riptide_firmware {
3958c2ecf20Sopenharmony_ci	u16 ASIC;
3968c2ecf20Sopenharmony_ci	u16 CODEC;
3978c2ecf20Sopenharmony_ci	u16 AUXDSP;
3988c2ecf20Sopenharmony_ci	u16 PROG;
3998c2ecf20Sopenharmony_ci};
4008c2ecf20Sopenharmony_ci
4018c2ecf20Sopenharmony_ciunion cmdret {
4028c2ecf20Sopenharmony_ci	u8 retbytes[8];
4038c2ecf20Sopenharmony_ci	u16 retwords[4];
4048c2ecf20Sopenharmony_ci	u32 retlongs[2];
4058c2ecf20Sopenharmony_ci};
4068c2ecf20Sopenharmony_ci
4078c2ecf20Sopenharmony_ciunion firmware_version {
4088c2ecf20Sopenharmony_ci	union cmdret ret;
4098c2ecf20Sopenharmony_ci	struct riptide_firmware firmware;
4108c2ecf20Sopenharmony_ci};
4118c2ecf20Sopenharmony_ci
4128c2ecf20Sopenharmony_ci#define get_pcmhwdev(substream) (struct pcmhw *)(substream->runtime->private_data)
4138c2ecf20Sopenharmony_ci
4148c2ecf20Sopenharmony_ci#define PLAYBACK_SUBSTREAMS 3
4158c2ecf20Sopenharmony_cistruct snd_riptide {
4168c2ecf20Sopenharmony_ci	struct snd_card *card;
4178c2ecf20Sopenharmony_ci	struct pci_dev *pci;
4188c2ecf20Sopenharmony_ci	const struct firmware *fw_entry;
4198c2ecf20Sopenharmony_ci
4208c2ecf20Sopenharmony_ci	struct cmdif *cif;
4218c2ecf20Sopenharmony_ci
4228c2ecf20Sopenharmony_ci	struct snd_pcm *pcm;
4238c2ecf20Sopenharmony_ci	struct snd_pcm *pcm_i2s;
4248c2ecf20Sopenharmony_ci	struct snd_rawmidi *rmidi;
4258c2ecf20Sopenharmony_ci	struct snd_opl3 *opl3;
4268c2ecf20Sopenharmony_ci	struct snd_ac97 *ac97;
4278c2ecf20Sopenharmony_ci	struct snd_ac97_bus *ac97_bus;
4288c2ecf20Sopenharmony_ci
4298c2ecf20Sopenharmony_ci	struct snd_pcm_substream *playback_substream[PLAYBACK_SUBSTREAMS];
4308c2ecf20Sopenharmony_ci	struct snd_pcm_substream *capture_substream;
4318c2ecf20Sopenharmony_ci
4328c2ecf20Sopenharmony_ci	int openstreams;
4338c2ecf20Sopenharmony_ci
4348c2ecf20Sopenharmony_ci	int irq;
4358c2ecf20Sopenharmony_ci	unsigned long port;
4368c2ecf20Sopenharmony_ci	unsigned short mpuaddr;
4378c2ecf20Sopenharmony_ci	unsigned short opladdr;
4388c2ecf20Sopenharmony_ci#ifdef SUPPORT_JOYSTICK
4398c2ecf20Sopenharmony_ci	unsigned short gameaddr;
4408c2ecf20Sopenharmony_ci#endif
4418c2ecf20Sopenharmony_ci	struct resource *res_port;
4428c2ecf20Sopenharmony_ci
4438c2ecf20Sopenharmony_ci	unsigned short device_id;
4448c2ecf20Sopenharmony_ci
4458c2ecf20Sopenharmony_ci	union firmware_version firmware;
4468c2ecf20Sopenharmony_ci
4478c2ecf20Sopenharmony_ci	spinlock_t lock;
4488c2ecf20Sopenharmony_ci	struct snd_info_entry *proc_entry;
4498c2ecf20Sopenharmony_ci
4508c2ecf20Sopenharmony_ci	unsigned long received_irqs;
4518c2ecf20Sopenharmony_ci	unsigned long handled_irqs;
4528c2ecf20Sopenharmony_ci#ifdef CONFIG_PM_SLEEP
4538c2ecf20Sopenharmony_ci	int in_suspend;
4548c2ecf20Sopenharmony_ci#endif
4558c2ecf20Sopenharmony_ci};
4568c2ecf20Sopenharmony_ci
4578c2ecf20Sopenharmony_cistruct sgd {			/* scatter gather desriptor */
4588c2ecf20Sopenharmony_ci	__le32 dwNextLink;
4598c2ecf20Sopenharmony_ci	__le32 dwSegPtrPhys;
4608c2ecf20Sopenharmony_ci	__le32 dwSegLen;
4618c2ecf20Sopenharmony_ci	__le32 dwStat_Ctl;
4628c2ecf20Sopenharmony_ci};
4638c2ecf20Sopenharmony_ci
4648c2ecf20Sopenharmony_cistruct pcmhw {			/* pcm descriptor */
4658c2ecf20Sopenharmony_ci	struct lbuspath paths;
4668c2ecf20Sopenharmony_ci	const unsigned char *lbuspath;
4678c2ecf20Sopenharmony_ci	unsigned char source;
4688c2ecf20Sopenharmony_ci	unsigned char intdec[2];
4698c2ecf20Sopenharmony_ci	unsigned char mixer;
4708c2ecf20Sopenharmony_ci	unsigned char id;
4718c2ecf20Sopenharmony_ci	unsigned char state;
4728c2ecf20Sopenharmony_ci	unsigned int rate;
4738c2ecf20Sopenharmony_ci	unsigned int channels;
4748c2ecf20Sopenharmony_ci	snd_pcm_format_t format;
4758c2ecf20Sopenharmony_ci	struct snd_dma_buffer sgdlist;
4768c2ecf20Sopenharmony_ci	struct sgd *sgdbuf;
4778c2ecf20Sopenharmony_ci	unsigned int size;
4788c2ecf20Sopenharmony_ci	unsigned int pages;
4798c2ecf20Sopenharmony_ci	unsigned int oldpos;
4808c2ecf20Sopenharmony_ci	unsigned int pointer;
4818c2ecf20Sopenharmony_ci};
4828c2ecf20Sopenharmony_ci
4838c2ecf20Sopenharmony_ci#define CMDRET_ZERO (union cmdret){{(u32)0, (u32) 0}}
4848c2ecf20Sopenharmony_ci
4858c2ecf20Sopenharmony_cistatic int sendcmd(struct cmdif *cif, u32 flags, u32 cmd, u32 parm,
4868c2ecf20Sopenharmony_ci		   union cmdret *ret);
4878c2ecf20Sopenharmony_cistatic int getsourcesink(struct cmdif *cif, unsigned char source,
4888c2ecf20Sopenharmony_ci			 unsigned char sink, unsigned char *a,
4898c2ecf20Sopenharmony_ci			 unsigned char *b);
4908c2ecf20Sopenharmony_cistatic int snd_riptide_initialize(struct snd_riptide *chip);
4918c2ecf20Sopenharmony_cistatic int riptide_reset(struct cmdif *cif, struct snd_riptide *chip);
4928c2ecf20Sopenharmony_ci
4938c2ecf20Sopenharmony_ci/*
4948c2ecf20Sopenharmony_ci */
4958c2ecf20Sopenharmony_ci
4968c2ecf20Sopenharmony_cistatic const struct pci_device_id snd_riptide_ids[] = {
4978c2ecf20Sopenharmony_ci	{ PCI_DEVICE(0x127a, 0x4310) },
4988c2ecf20Sopenharmony_ci	{ PCI_DEVICE(0x127a, 0x4320) },
4998c2ecf20Sopenharmony_ci	{ PCI_DEVICE(0x127a, 0x4330) },
5008c2ecf20Sopenharmony_ci	{ PCI_DEVICE(0x127a, 0x4340) },
5018c2ecf20Sopenharmony_ci	{0,},
5028c2ecf20Sopenharmony_ci};
5038c2ecf20Sopenharmony_ci
5048c2ecf20Sopenharmony_ci#ifdef SUPPORT_JOYSTICK
5058c2ecf20Sopenharmony_cistatic const struct pci_device_id snd_riptide_joystick_ids[] = {
5068c2ecf20Sopenharmony_ci	{ PCI_DEVICE(0x127a, 0x4312) },
5078c2ecf20Sopenharmony_ci	{ PCI_DEVICE(0x127a, 0x4322) },
5088c2ecf20Sopenharmony_ci	{ PCI_DEVICE(0x127a, 0x4332) },
5098c2ecf20Sopenharmony_ci	{ PCI_DEVICE(0x127a, 0x4342) },
5108c2ecf20Sopenharmony_ci	{0,},
5118c2ecf20Sopenharmony_ci};
5128c2ecf20Sopenharmony_ci#endif
5138c2ecf20Sopenharmony_ci
5148c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(pci, snd_riptide_ids);
5158c2ecf20Sopenharmony_ci
5168c2ecf20Sopenharmony_ci/*
5178c2ecf20Sopenharmony_ci */
5188c2ecf20Sopenharmony_ci
5198c2ecf20Sopenharmony_cistatic const unsigned char lbusin2out[E2SINK_MAX + 1][2] = {
5208c2ecf20Sopenharmony_ci	{NO_OUT, LS_NONE1}, {NO_OUT, LS_NONE2}, {NO_OUT, LS_NONE1}, {NO_OUT,
5218c2ecf20Sopenharmony_ci								     LS_NONE2},
5228c2ecf20Sopenharmony_ci	{NO_OUT, LS_NONE1}, {NO_OUT, LS_NONE2}, {NO_OUT, LS_NONE1}, {NO_OUT,
5238c2ecf20Sopenharmony_ci								     LS_NONE2},
5248c2ecf20Sopenharmony_ci	{INTER0_OUT, LS_SRC_INTERPOLATOR}, {INTER1_OUT, LS_SRC_INTERPOLATOR},
5258c2ecf20Sopenharmony_ci	{INTER2_OUT, LS_SRC_INTERPOLATOR}, {INTER3_OUT, LS_SRC_INTERPOLATOR},
5268c2ecf20Sopenharmony_ci	{INTER4_OUT, LS_SRC_INTERPOLATOR}, {INTERM0_OUT, LS_SRC_INTERPOLATORM},
5278c2ecf20Sopenharmony_ci	{INTERM1_OUT, LS_SRC_INTERPOLATORM}, {INTERM2_OUT,
5288c2ecf20Sopenharmony_ci					      LS_SRC_INTERPOLATORM},
5298c2ecf20Sopenharmony_ci	{INTERM3_OUT, LS_SRC_INTERPOLATORM}, {INTERM4_OUT,
5308c2ecf20Sopenharmony_ci					      LS_SRC_INTERPOLATORM},
5318c2ecf20Sopenharmony_ci	{INTERM5_OUT, LS_SRC_INTERPOLATORM}, {INTERM6_OUT,
5328c2ecf20Sopenharmony_ci					      LS_SRC_INTERPOLATORM},
5338c2ecf20Sopenharmony_ci	{DECIMM0_OUT, LS_SRC_DECIMATORM}, {DECIMM1_OUT, LS_SRC_DECIMATORM},
5348c2ecf20Sopenharmony_ci	{DECIMM2_OUT, LS_SRC_DECIMATORM}, {DECIMM3_OUT, LS_SRC_DECIMATORM},
5358c2ecf20Sopenharmony_ci	{DECIM0_OUT, LS_SRC_DECIMATOR}, {SR3_4_OUT, LS_NONE1}, {NO_OUT,
5368c2ecf20Sopenharmony_ci								LS_NONE2},
5378c2ecf20Sopenharmony_ci	{NO_OUT, LS_NONE1}, {NO_OUT, LS_NONE2}, {NO_OUT, LS_NONE1},
5388c2ecf20Sopenharmony_ci	{DIGITAL_MIXER_OUT0, LS_MIXER_IN}, {DIGITAL_MIXER_OUT0, LS_MIXER_IN},
5398c2ecf20Sopenharmony_ci	{DIGITAL_MIXER_OUT0, LS_MIXER_IN}, {DIGITAL_MIXER_OUT0, LS_MIXER_IN},
5408c2ecf20Sopenharmony_ci	{DIGITAL_MIXER_OUT0, LS_MIXER_IN}, {DIGITAL_MIXER_OUT0, LS_MIXER_IN},
5418c2ecf20Sopenharmony_ci	{DIGITAL_MIXER_OUT0, LS_MIXER_IN}, {DIGITAL_MIXER_OUT0, LS_MIXER_IN},
5428c2ecf20Sopenharmony_ci	{DIGITAL_MIXER_OUT0, LS_MIXER_IN}, {DIGITAL_MIXER_OUT0, LS_MIXER_IN},
5438c2ecf20Sopenharmony_ci	{DIGITAL_MIXER_OUT0, LS_MIXER_IN}, {DIGITAL_MIXER_OUT0, LS_MIXER_IN},
5448c2ecf20Sopenharmony_ci	{GAINFUNC0_OUT, LS_MIXER_GAIN_FUNCTION}, {GAINFUNC1_OUT,
5458c2ecf20Sopenharmony_ci						  LS_MIXER_GAIN_FUNCTION},
5468c2ecf20Sopenharmony_ci	{GAINFUNC2_OUT, LS_MIXER_GAIN_FUNCTION}, {GAINFUNC3_OUT,
5478c2ecf20Sopenharmony_ci						  LS_MIXER_GAIN_FUNCTION},
5488c2ecf20Sopenharmony_ci	{GAINFUNC4_OUT, LS_MIXER_GAIN_FUNCTION}, {SOFTMODEMTX, LS_NONE1},
5498c2ecf20Sopenharmony_ci	{SPLITTER0_OUTL, LS_SRC_SPLITTER}, {SPLITTER1_OUTL, LS_SRC_SPLITTER},
5508c2ecf20Sopenharmony_ci	{SPLITTER2_OUTL, LS_SRC_SPLITTER}, {SPLITTER3_OUTL, LS_SRC_SPLITTER},
5518c2ecf20Sopenharmony_ci	{MERGER0_OUT, LS_SRC_MERGER}, {MERGER0_OUT, LS_SRC_MERGER},
5528c2ecf20Sopenharmony_ci	{MERGER1_OUT, LS_SRC_MERGER},
5538c2ecf20Sopenharmony_ci	{MERGER1_OUT, LS_SRC_MERGER}, {MERGER2_OUT, LS_SRC_MERGER},
5548c2ecf20Sopenharmony_ci	{MERGER2_OUT, LS_SRC_MERGER},
5558c2ecf20Sopenharmony_ci	{MERGER3_OUT, LS_SRC_MERGER}, {MERGER3_OUT, LS_SRC_MERGER}, {NO_OUT,
5568c2ecf20Sopenharmony_ci								     LS_NONE2},
5578c2ecf20Sopenharmony_ci};
5588c2ecf20Sopenharmony_ci
5598c2ecf20Sopenharmony_cistatic const unsigned char lbus_play_opl3[] = {
5608c2ecf20Sopenharmony_ci	DIGITAL_MIXER_IN0 + FM_MIXER, 0xff
5618c2ecf20Sopenharmony_ci};
5628c2ecf20Sopenharmony_cistatic const unsigned char lbus_play_modem[] = {
5638c2ecf20Sopenharmony_ci	DIGITAL_MIXER_IN0 + MODEM_MIXER, 0xff
5648c2ecf20Sopenharmony_ci};
5658c2ecf20Sopenharmony_cistatic const unsigned char lbus_play_i2s[] = {
5668c2ecf20Sopenharmony_ci	INTER0_IN + I2S_INTDEC, DIGITAL_MIXER_IN0 + I2S_MIXER, 0xff
5678c2ecf20Sopenharmony_ci};
5688c2ecf20Sopenharmony_cistatic const unsigned char lbus_play_out[] = {
5698c2ecf20Sopenharmony_ci	PDAC2ACLNK, 0xff
5708c2ecf20Sopenharmony_ci};
5718c2ecf20Sopenharmony_cistatic const unsigned char lbus_play_outhp[] = {
5728c2ecf20Sopenharmony_ci	HNDSPK2ACLNK, 0xff
5738c2ecf20Sopenharmony_ci};
5748c2ecf20Sopenharmony_cistatic const unsigned char lbus_play_noconv1[] = {
5758c2ecf20Sopenharmony_ci	DIGITAL_MIXER_IN0, 0xff
5768c2ecf20Sopenharmony_ci};
5778c2ecf20Sopenharmony_cistatic const unsigned char lbus_play_stereo1[] = {
5788c2ecf20Sopenharmony_ci	INTER0_IN, DIGITAL_MIXER_IN0, 0xff
5798c2ecf20Sopenharmony_ci};
5808c2ecf20Sopenharmony_cistatic const unsigned char lbus_play_mono1[] = {
5818c2ecf20Sopenharmony_ci	INTERM0_IN, DIGITAL_MIXER_IN0, 0xff
5828c2ecf20Sopenharmony_ci};
5838c2ecf20Sopenharmony_cistatic const unsigned char lbus_play_noconv2[] = {
5848c2ecf20Sopenharmony_ci	DIGITAL_MIXER_IN1, 0xff
5858c2ecf20Sopenharmony_ci};
5868c2ecf20Sopenharmony_cistatic const unsigned char lbus_play_stereo2[] = {
5878c2ecf20Sopenharmony_ci	INTER1_IN, DIGITAL_MIXER_IN1, 0xff
5888c2ecf20Sopenharmony_ci};
5898c2ecf20Sopenharmony_cistatic const unsigned char lbus_play_mono2[] = {
5908c2ecf20Sopenharmony_ci	INTERM1_IN, DIGITAL_MIXER_IN1, 0xff
5918c2ecf20Sopenharmony_ci};
5928c2ecf20Sopenharmony_cistatic const unsigned char lbus_play_noconv3[] = {
5938c2ecf20Sopenharmony_ci	DIGITAL_MIXER_IN2, 0xff
5948c2ecf20Sopenharmony_ci};
5958c2ecf20Sopenharmony_cistatic const unsigned char lbus_play_stereo3[] = {
5968c2ecf20Sopenharmony_ci	INTER2_IN, DIGITAL_MIXER_IN2, 0xff
5978c2ecf20Sopenharmony_ci};
5988c2ecf20Sopenharmony_cistatic const unsigned char lbus_play_mono3[] = {
5998c2ecf20Sopenharmony_ci	INTERM2_IN, DIGITAL_MIXER_IN2, 0xff
6008c2ecf20Sopenharmony_ci};
6018c2ecf20Sopenharmony_cistatic const unsigned char lbus_rec_noconv1[] = {
6028c2ecf20Sopenharmony_ci	LBUS2ARM_FIFO5, 0xff
6038c2ecf20Sopenharmony_ci};
6048c2ecf20Sopenharmony_cistatic const unsigned char lbus_rec_stereo1[] = {
6058c2ecf20Sopenharmony_ci	DECIM0_IN, LBUS2ARM_FIFO5, 0xff
6068c2ecf20Sopenharmony_ci};
6078c2ecf20Sopenharmony_cistatic const unsigned char lbus_rec_mono1[] = {
6088c2ecf20Sopenharmony_ci	DECIMM3_IN, LBUS2ARM_FIFO5, 0xff
6098c2ecf20Sopenharmony_ci};
6108c2ecf20Sopenharmony_ci
6118c2ecf20Sopenharmony_cistatic const unsigned char play_ids[] = { 4, 1, 2, };
6128c2ecf20Sopenharmony_cistatic const unsigned char play_sources[] = {
6138c2ecf20Sopenharmony_ci	ARM2LBUS_FIFO4, ARM2LBUS_FIFO1, ARM2LBUS_FIFO2,
6148c2ecf20Sopenharmony_ci};
6158c2ecf20Sopenharmony_cistatic const struct lbuspath lbus_play_paths[] = {
6168c2ecf20Sopenharmony_ci	{
6178c2ecf20Sopenharmony_ci	 .noconv = lbus_play_noconv1,
6188c2ecf20Sopenharmony_ci	 .stereo = lbus_play_stereo1,
6198c2ecf20Sopenharmony_ci	 .mono = lbus_play_mono1,
6208c2ecf20Sopenharmony_ci	 },
6218c2ecf20Sopenharmony_ci	{
6228c2ecf20Sopenharmony_ci	 .noconv = lbus_play_noconv2,
6238c2ecf20Sopenharmony_ci	 .stereo = lbus_play_stereo2,
6248c2ecf20Sopenharmony_ci	 .mono = lbus_play_mono2,
6258c2ecf20Sopenharmony_ci	 },
6268c2ecf20Sopenharmony_ci	{
6278c2ecf20Sopenharmony_ci	 .noconv = lbus_play_noconv3,
6288c2ecf20Sopenharmony_ci	 .stereo = lbus_play_stereo3,
6298c2ecf20Sopenharmony_ci	 .mono = lbus_play_mono3,
6308c2ecf20Sopenharmony_ci	 },
6318c2ecf20Sopenharmony_ci};
6328c2ecf20Sopenharmony_cistatic const struct lbuspath lbus_rec_path = {
6338c2ecf20Sopenharmony_ci	.noconv = lbus_rec_noconv1,
6348c2ecf20Sopenharmony_ci	.stereo = lbus_rec_stereo1,
6358c2ecf20Sopenharmony_ci	.mono = lbus_rec_mono1,
6368c2ecf20Sopenharmony_ci};
6378c2ecf20Sopenharmony_ci
6388c2ecf20Sopenharmony_ci#define FIRMWARE_VERSIONS 1
6398c2ecf20Sopenharmony_cistatic union firmware_version firmware_versions[] = {
6408c2ecf20Sopenharmony_ci	{
6418c2ecf20Sopenharmony_ci		.firmware = {
6428c2ecf20Sopenharmony_ci			.ASIC = 3,
6438c2ecf20Sopenharmony_ci			.CODEC = 2,
6448c2ecf20Sopenharmony_ci			.AUXDSP = 3,
6458c2ecf20Sopenharmony_ci			.PROG = 773,
6468c2ecf20Sopenharmony_ci		},
6478c2ecf20Sopenharmony_ci	},
6488c2ecf20Sopenharmony_ci};
6498c2ecf20Sopenharmony_ci
6508c2ecf20Sopenharmony_cistatic u32 atoh(const unsigned char *in, unsigned int len)
6518c2ecf20Sopenharmony_ci{
6528c2ecf20Sopenharmony_ci	u32 sum = 0;
6538c2ecf20Sopenharmony_ci	unsigned int mult = 1;
6548c2ecf20Sopenharmony_ci	unsigned char c;
6558c2ecf20Sopenharmony_ci
6568c2ecf20Sopenharmony_ci	while (len) {
6578c2ecf20Sopenharmony_ci		int value;
6588c2ecf20Sopenharmony_ci
6598c2ecf20Sopenharmony_ci		c = in[len - 1];
6608c2ecf20Sopenharmony_ci		value = hex_to_bin(c);
6618c2ecf20Sopenharmony_ci		if (value >= 0)
6628c2ecf20Sopenharmony_ci			sum += mult * value;
6638c2ecf20Sopenharmony_ci		mult *= 16;
6648c2ecf20Sopenharmony_ci		--len;
6658c2ecf20Sopenharmony_ci	}
6668c2ecf20Sopenharmony_ci	return sum;
6678c2ecf20Sopenharmony_ci}
6688c2ecf20Sopenharmony_ci
6698c2ecf20Sopenharmony_cistatic int senddata(struct cmdif *cif, const unsigned char *in, u32 offset)
6708c2ecf20Sopenharmony_ci{
6718c2ecf20Sopenharmony_ci	u32 addr;
6728c2ecf20Sopenharmony_ci	u32 data;
6738c2ecf20Sopenharmony_ci	u32 i;
6748c2ecf20Sopenharmony_ci	const unsigned char *p;
6758c2ecf20Sopenharmony_ci
6768c2ecf20Sopenharmony_ci	i = atoh(&in[1], 2);
6778c2ecf20Sopenharmony_ci	addr = offset + atoh(&in[3], 4);
6788c2ecf20Sopenharmony_ci	if (SEND_SMEM(cif, 0, addr) != 0)
6798c2ecf20Sopenharmony_ci		return -EACCES;
6808c2ecf20Sopenharmony_ci	p = in + 9;
6818c2ecf20Sopenharmony_ci	while (i) {
6828c2ecf20Sopenharmony_ci		data = atoh(p, 8);
6838c2ecf20Sopenharmony_ci		if (SEND_WMEM(cif, 2,
6848c2ecf20Sopenharmony_ci			      ((data & 0x0f0f0f0f) << 4) | ((data & 0xf0f0f0f0)
6858c2ecf20Sopenharmony_ci							    >> 4)))
6868c2ecf20Sopenharmony_ci			return -EACCES;
6878c2ecf20Sopenharmony_ci		i -= 4;
6888c2ecf20Sopenharmony_ci		p += 8;
6898c2ecf20Sopenharmony_ci	}
6908c2ecf20Sopenharmony_ci	return 0;
6918c2ecf20Sopenharmony_ci}
6928c2ecf20Sopenharmony_ci
6938c2ecf20Sopenharmony_cistatic int loadfirmware(struct cmdif *cif, const unsigned char *img,
6948c2ecf20Sopenharmony_ci			unsigned int size)
6958c2ecf20Sopenharmony_ci{
6968c2ecf20Sopenharmony_ci	const unsigned char *in;
6978c2ecf20Sopenharmony_ci	u32 laddr, saddr, t, val;
6988c2ecf20Sopenharmony_ci	int err = 0;
6998c2ecf20Sopenharmony_ci
7008c2ecf20Sopenharmony_ci	laddr = saddr = 0;
7018c2ecf20Sopenharmony_ci	while (size > 0 && err == 0) {
7028c2ecf20Sopenharmony_ci		in = img;
7038c2ecf20Sopenharmony_ci		if (in[0] == ':') {
7048c2ecf20Sopenharmony_ci			t = atoh(&in[7], 2);
7058c2ecf20Sopenharmony_ci			switch (t) {
7068c2ecf20Sopenharmony_ci			case DATA_REC:
7078c2ecf20Sopenharmony_ci				err = senddata(cif, in, laddr + saddr);
7088c2ecf20Sopenharmony_ci				break;
7098c2ecf20Sopenharmony_ci			case EXT_SEG_ADDR_REC:
7108c2ecf20Sopenharmony_ci				saddr = atoh(&in[9], 4) << 4;
7118c2ecf20Sopenharmony_ci				break;
7128c2ecf20Sopenharmony_ci			case EXT_LIN_ADDR_REC:
7138c2ecf20Sopenharmony_ci				laddr = atoh(&in[9], 4) << 16;
7148c2ecf20Sopenharmony_ci				break;
7158c2ecf20Sopenharmony_ci			case EXT_GOTO_CMD_REC:
7168c2ecf20Sopenharmony_ci				val = atoh(&in[9], 8);
7178c2ecf20Sopenharmony_ci				if (SEND_GOTO(cif, val) != 0)
7188c2ecf20Sopenharmony_ci					err = -EACCES;
7198c2ecf20Sopenharmony_ci				break;
7208c2ecf20Sopenharmony_ci			case EXT_END_OF_FILE:
7218c2ecf20Sopenharmony_ci				size = 0;
7228c2ecf20Sopenharmony_ci				break;
7238c2ecf20Sopenharmony_ci			default:
7248c2ecf20Sopenharmony_ci				break;
7258c2ecf20Sopenharmony_ci			}
7268c2ecf20Sopenharmony_ci			while (size > 0) {
7278c2ecf20Sopenharmony_ci				size--;
7288c2ecf20Sopenharmony_ci				if (*img++ == '\n')
7298c2ecf20Sopenharmony_ci					break;
7308c2ecf20Sopenharmony_ci			}
7318c2ecf20Sopenharmony_ci		}
7328c2ecf20Sopenharmony_ci	}
7338c2ecf20Sopenharmony_ci	snd_printdd("load firmware return %d\n", err);
7348c2ecf20Sopenharmony_ci	return err;
7358c2ecf20Sopenharmony_ci}
7368c2ecf20Sopenharmony_ci
7378c2ecf20Sopenharmony_cistatic void
7388c2ecf20Sopenharmony_cialloclbuspath(struct cmdif *cif, unsigned char source,
7398c2ecf20Sopenharmony_ci	      const unsigned char *path, unsigned char *mixer, unsigned char *s)
7408c2ecf20Sopenharmony_ci{
7418c2ecf20Sopenharmony_ci	while (*path != 0xff) {
7428c2ecf20Sopenharmony_ci		unsigned char sink, type;
7438c2ecf20Sopenharmony_ci
7448c2ecf20Sopenharmony_ci		sink = *path & (~SPLIT_PATH);
7458c2ecf20Sopenharmony_ci		if (sink != E2SINK_MAX) {
7468c2ecf20Sopenharmony_ci			snd_printdd("alloc path 0x%x->0x%x\n", source, sink);
7478c2ecf20Sopenharmony_ci			SEND_PSEL(cif, source, sink);
7488c2ecf20Sopenharmony_ci			source = lbusin2out[sink][0];
7498c2ecf20Sopenharmony_ci			type = lbusin2out[sink][1];
7508c2ecf20Sopenharmony_ci			if (type == LS_MIXER_IN) {
7518c2ecf20Sopenharmony_ci				if (mixer)
7528c2ecf20Sopenharmony_ci					*mixer = sink - DIGITAL_MIXER_IN0;
7538c2ecf20Sopenharmony_ci			}
7548c2ecf20Sopenharmony_ci			if (type == LS_SRC_DECIMATORM ||
7558c2ecf20Sopenharmony_ci			    type == LS_SRC_DECIMATOR ||
7568c2ecf20Sopenharmony_ci			    type == LS_SRC_INTERPOLATORM ||
7578c2ecf20Sopenharmony_ci			    type == LS_SRC_INTERPOLATOR) {
7588c2ecf20Sopenharmony_ci				if (s) {
7598c2ecf20Sopenharmony_ci					if (s[0] != 0xff)
7608c2ecf20Sopenharmony_ci						s[1] = sink;
7618c2ecf20Sopenharmony_ci					else
7628c2ecf20Sopenharmony_ci						s[0] = sink;
7638c2ecf20Sopenharmony_ci				}
7648c2ecf20Sopenharmony_ci			}
7658c2ecf20Sopenharmony_ci		}
7668c2ecf20Sopenharmony_ci		if (*path++ & SPLIT_PATH) {
7678c2ecf20Sopenharmony_ci			const unsigned char *npath = path;
7688c2ecf20Sopenharmony_ci
7698c2ecf20Sopenharmony_ci			while (*npath != 0xff)
7708c2ecf20Sopenharmony_ci				npath++;
7718c2ecf20Sopenharmony_ci			alloclbuspath(cif, source + 1, ++npath, mixer, s);
7728c2ecf20Sopenharmony_ci		}
7738c2ecf20Sopenharmony_ci	}
7748c2ecf20Sopenharmony_ci}
7758c2ecf20Sopenharmony_ci
7768c2ecf20Sopenharmony_cistatic void
7778c2ecf20Sopenharmony_cifreelbuspath(struct cmdif *cif, unsigned char source, const unsigned char *path)
7788c2ecf20Sopenharmony_ci{
7798c2ecf20Sopenharmony_ci	while (*path != 0xff) {
7808c2ecf20Sopenharmony_ci		unsigned char sink;
7818c2ecf20Sopenharmony_ci
7828c2ecf20Sopenharmony_ci		sink = *path & (~SPLIT_PATH);
7838c2ecf20Sopenharmony_ci		if (sink != E2SINK_MAX) {
7848c2ecf20Sopenharmony_ci			snd_printdd("free path 0x%x->0x%x\n", source, sink);
7858c2ecf20Sopenharmony_ci			SEND_PCLR(cif, source, sink);
7868c2ecf20Sopenharmony_ci			source = lbusin2out[sink][0];
7878c2ecf20Sopenharmony_ci		}
7888c2ecf20Sopenharmony_ci		if (*path++ & SPLIT_PATH) {
7898c2ecf20Sopenharmony_ci			const unsigned char *npath = path;
7908c2ecf20Sopenharmony_ci
7918c2ecf20Sopenharmony_ci			while (*npath != 0xff)
7928c2ecf20Sopenharmony_ci				npath++;
7938c2ecf20Sopenharmony_ci			freelbuspath(cif, source + 1, ++npath);
7948c2ecf20Sopenharmony_ci		}
7958c2ecf20Sopenharmony_ci	}
7968c2ecf20Sopenharmony_ci}
7978c2ecf20Sopenharmony_ci
7988c2ecf20Sopenharmony_cistatic int writearm(struct cmdif *cif, u32 addr, u32 data, u32 mask)
7998c2ecf20Sopenharmony_ci{
8008c2ecf20Sopenharmony_ci	union cmdret rptr = CMDRET_ZERO;
8018c2ecf20Sopenharmony_ci	unsigned int i = MAX_WRITE_RETRY;
8028c2ecf20Sopenharmony_ci	int flag = 1;
8038c2ecf20Sopenharmony_ci
8048c2ecf20Sopenharmony_ci	SEND_RMEM(cif, 0x02, addr, &rptr);
8058c2ecf20Sopenharmony_ci	rptr.retlongs[0] &= (~mask);
8068c2ecf20Sopenharmony_ci
8078c2ecf20Sopenharmony_ci	while (--i) {
8088c2ecf20Sopenharmony_ci		SEND_SMEM(cif, 0x01, addr);
8098c2ecf20Sopenharmony_ci		SEND_WMEM(cif, 0x02, (rptr.retlongs[0] | data));
8108c2ecf20Sopenharmony_ci		SEND_RMEM(cif, 0x02, addr, &rptr);
8118c2ecf20Sopenharmony_ci		if ((rptr.retlongs[0] & data) == data) {
8128c2ecf20Sopenharmony_ci			flag = 0;
8138c2ecf20Sopenharmony_ci			break;
8148c2ecf20Sopenharmony_ci		} else
8158c2ecf20Sopenharmony_ci			rptr.retlongs[0] &= ~mask;
8168c2ecf20Sopenharmony_ci	}
8178c2ecf20Sopenharmony_ci	snd_printdd("send arm 0x%x 0x%x 0x%x return %d\n", addr, data, mask,
8188c2ecf20Sopenharmony_ci		    flag);
8198c2ecf20Sopenharmony_ci	return flag;
8208c2ecf20Sopenharmony_ci}
8218c2ecf20Sopenharmony_ci
8228c2ecf20Sopenharmony_cistatic int sendcmd(struct cmdif *cif, u32 flags, u32 cmd, u32 parm,
8238c2ecf20Sopenharmony_ci		   union cmdret *ret)
8248c2ecf20Sopenharmony_ci{
8258c2ecf20Sopenharmony_ci	int i, j;
8268c2ecf20Sopenharmony_ci	int err;
8278c2ecf20Sopenharmony_ci	unsigned int time = 0;
8288c2ecf20Sopenharmony_ci	unsigned long irqflags;
8298c2ecf20Sopenharmony_ci	struct riptideport *hwport;
8308c2ecf20Sopenharmony_ci	struct cmdport *cmdport = NULL;
8318c2ecf20Sopenharmony_ci
8328c2ecf20Sopenharmony_ci	if (snd_BUG_ON(!cif))
8338c2ecf20Sopenharmony_ci		return -EINVAL;
8348c2ecf20Sopenharmony_ci
8358c2ecf20Sopenharmony_ci	hwport = cif->hwport;
8368c2ecf20Sopenharmony_ci	if (cif->errcnt > MAX_ERROR_COUNT) {
8378c2ecf20Sopenharmony_ci		if (cif->is_reset) {
8388c2ecf20Sopenharmony_ci			snd_printk(KERN_ERR
8398c2ecf20Sopenharmony_ci				   "Riptide: Too many failed cmds, reinitializing\n");
8408c2ecf20Sopenharmony_ci			if (riptide_reset(cif, NULL) == 0) {
8418c2ecf20Sopenharmony_ci				cif->errcnt = 0;
8428c2ecf20Sopenharmony_ci				return -EIO;
8438c2ecf20Sopenharmony_ci			}
8448c2ecf20Sopenharmony_ci		}
8458c2ecf20Sopenharmony_ci		snd_printk(KERN_ERR "Riptide: Initialization failed.\n");
8468c2ecf20Sopenharmony_ci		return -EINVAL;
8478c2ecf20Sopenharmony_ci	}
8488c2ecf20Sopenharmony_ci	if (ret) {
8498c2ecf20Sopenharmony_ci		ret->retlongs[0] = 0;
8508c2ecf20Sopenharmony_ci		ret->retlongs[1] = 0;
8518c2ecf20Sopenharmony_ci	}
8528c2ecf20Sopenharmony_ci	i = 0;
8538c2ecf20Sopenharmony_ci	spin_lock_irqsave(&cif->lock, irqflags);
8548c2ecf20Sopenharmony_ci	while (i++ < CMDIF_TIMEOUT && !IS_READY(cif->hwport))
8558c2ecf20Sopenharmony_ci		udelay(10);
8568c2ecf20Sopenharmony_ci	if (i > CMDIF_TIMEOUT) {
8578c2ecf20Sopenharmony_ci		err = -EBUSY;
8588c2ecf20Sopenharmony_ci		goto errout;
8598c2ecf20Sopenharmony_ci	}
8608c2ecf20Sopenharmony_ci
8618c2ecf20Sopenharmony_ci	err = 0;
8628c2ecf20Sopenharmony_ci	for (j = 0, time = 0; time < CMDIF_TIMEOUT; j++, time += 2) {
8638c2ecf20Sopenharmony_ci		cmdport = &(hwport->port[j % 2]);
8648c2ecf20Sopenharmony_ci		if (IS_DATF(cmdport)) {	/* free pending data */
8658c2ecf20Sopenharmony_ci			READ_PORT_ULONG(cmdport->data1);
8668c2ecf20Sopenharmony_ci			READ_PORT_ULONG(cmdport->data2);
8678c2ecf20Sopenharmony_ci		}
8688c2ecf20Sopenharmony_ci		if (IS_CMDE(cmdport)) {
8698c2ecf20Sopenharmony_ci			if (flags & PARM)	/* put data */
8708c2ecf20Sopenharmony_ci				WRITE_PORT_ULONG(cmdport->data2, parm);
8718c2ecf20Sopenharmony_ci			WRITE_PORT_ULONG(cmdport->data1, cmd);	/* write cmd */
8728c2ecf20Sopenharmony_ci			if ((flags & RESP) && ret) {
8738c2ecf20Sopenharmony_ci				while (!IS_DATF(cmdport) &&
8748c2ecf20Sopenharmony_ci				       time < CMDIF_TIMEOUT) {
8758c2ecf20Sopenharmony_ci					udelay(10);
8768c2ecf20Sopenharmony_ci					time++;
8778c2ecf20Sopenharmony_ci				}
8788c2ecf20Sopenharmony_ci				if (time < CMDIF_TIMEOUT) {	/* read response */
8798c2ecf20Sopenharmony_ci					ret->retlongs[0] =
8808c2ecf20Sopenharmony_ci					    READ_PORT_ULONG(cmdport->data1);
8818c2ecf20Sopenharmony_ci					ret->retlongs[1] =
8828c2ecf20Sopenharmony_ci					    READ_PORT_ULONG(cmdport->data2);
8838c2ecf20Sopenharmony_ci				} else {
8848c2ecf20Sopenharmony_ci					err = -ENOSYS;
8858c2ecf20Sopenharmony_ci					goto errout;
8868c2ecf20Sopenharmony_ci				}
8878c2ecf20Sopenharmony_ci			}
8888c2ecf20Sopenharmony_ci			break;
8898c2ecf20Sopenharmony_ci		}
8908c2ecf20Sopenharmony_ci		udelay(20);
8918c2ecf20Sopenharmony_ci	}
8928c2ecf20Sopenharmony_ci	if (time == CMDIF_TIMEOUT) {
8938c2ecf20Sopenharmony_ci		err = -ENODATA;
8948c2ecf20Sopenharmony_ci		goto errout;
8958c2ecf20Sopenharmony_ci	}
8968c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&cif->lock, irqflags);
8978c2ecf20Sopenharmony_ci
8988c2ecf20Sopenharmony_ci	cif->cmdcnt++;		/* update command statistics */
8998c2ecf20Sopenharmony_ci	cif->cmdtime += time;
9008c2ecf20Sopenharmony_ci	if (time > cif->cmdtimemax)
9018c2ecf20Sopenharmony_ci		cif->cmdtimemax = time;
9028c2ecf20Sopenharmony_ci	if (time < cif->cmdtimemin)
9038c2ecf20Sopenharmony_ci		cif->cmdtimemin = time;
9048c2ecf20Sopenharmony_ci	if ((cif->cmdcnt) % 1000 == 0)
9058c2ecf20Sopenharmony_ci		snd_printdd
9068c2ecf20Sopenharmony_ci		    ("send cmd %d time: %d mintime: %d maxtime %d err: %d\n",
9078c2ecf20Sopenharmony_ci		     cif->cmdcnt, cif->cmdtime, cif->cmdtimemin,
9088c2ecf20Sopenharmony_ci		     cif->cmdtimemax, cif->errcnt);
9098c2ecf20Sopenharmony_ci	return 0;
9108c2ecf20Sopenharmony_ci
9118c2ecf20Sopenharmony_ci      errout:
9128c2ecf20Sopenharmony_ci	cif->errcnt++;
9138c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&cif->lock, irqflags);
9148c2ecf20Sopenharmony_ci	snd_printdd
9158c2ecf20Sopenharmony_ci	    ("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",
9168c2ecf20Sopenharmony_ci	     cif->cmdcnt, (int)((void *)&(cmdport->stat) - (void *)hwport),
9178c2ecf20Sopenharmony_ci	     flags, cmd, parm, ret ? ret->retlongs[0] : 0,
9188c2ecf20Sopenharmony_ci	     ret ? ret->retlongs[1] : 0, IS_CMDE(cmdport), IS_DATF(cmdport),
9198c2ecf20Sopenharmony_ci	     err);
9208c2ecf20Sopenharmony_ci	return err;
9218c2ecf20Sopenharmony_ci}
9228c2ecf20Sopenharmony_ci
9238c2ecf20Sopenharmony_cistatic int
9248c2ecf20Sopenharmony_cisetmixer(struct cmdif *cif, short num, unsigned short rval, unsigned short lval)
9258c2ecf20Sopenharmony_ci{
9268c2ecf20Sopenharmony_ci	union cmdret rptr = CMDRET_ZERO;
9278c2ecf20Sopenharmony_ci	int i = 0;
9288c2ecf20Sopenharmony_ci
9298c2ecf20Sopenharmony_ci	snd_printdd("sent mixer %d: 0x%x 0x%x\n", num, rval, lval);
9308c2ecf20Sopenharmony_ci	do {
9318c2ecf20Sopenharmony_ci		SEND_SDGV(cif, num, num, rval, lval);
9328c2ecf20Sopenharmony_ci		SEND_RDGV(cif, num, num, &rptr);
9338c2ecf20Sopenharmony_ci		if (rptr.retwords[0] == lval && rptr.retwords[1] == rval)
9348c2ecf20Sopenharmony_ci			return 0;
9358c2ecf20Sopenharmony_ci	} while (i++ < MAX_WRITE_RETRY);
9368c2ecf20Sopenharmony_ci	snd_printdd("sent mixer failed\n");
9378c2ecf20Sopenharmony_ci	return -EIO;
9388c2ecf20Sopenharmony_ci}
9398c2ecf20Sopenharmony_ci
9408c2ecf20Sopenharmony_cistatic int getpaths(struct cmdif *cif, unsigned char *o)
9418c2ecf20Sopenharmony_ci{
9428c2ecf20Sopenharmony_ci	unsigned char src[E2SINK_MAX];
9438c2ecf20Sopenharmony_ci	unsigned char sink[E2SINK_MAX];
9448c2ecf20Sopenharmony_ci	int i, j = 0;
9458c2ecf20Sopenharmony_ci
9468c2ecf20Sopenharmony_ci	for (i = 0; i < E2SINK_MAX; i++) {
9478c2ecf20Sopenharmony_ci		getsourcesink(cif, i, i, &src[i], &sink[i]);
9488c2ecf20Sopenharmony_ci		if (sink[i] < E2SINK_MAX) {
9498c2ecf20Sopenharmony_ci			o[j++] = sink[i];
9508c2ecf20Sopenharmony_ci			o[j++] = i;
9518c2ecf20Sopenharmony_ci		}
9528c2ecf20Sopenharmony_ci	}
9538c2ecf20Sopenharmony_ci	return j;
9548c2ecf20Sopenharmony_ci}
9558c2ecf20Sopenharmony_ci
9568c2ecf20Sopenharmony_cistatic int
9578c2ecf20Sopenharmony_cigetsourcesink(struct cmdif *cif, unsigned char source, unsigned char sink,
9588c2ecf20Sopenharmony_ci	      unsigned char *a, unsigned char *b)
9598c2ecf20Sopenharmony_ci{
9608c2ecf20Sopenharmony_ci	union cmdret rptr = CMDRET_ZERO;
9618c2ecf20Sopenharmony_ci
9628c2ecf20Sopenharmony_ci	if (SEND_RSSV(cif, source, sink, &rptr) &&
9638c2ecf20Sopenharmony_ci	    SEND_RSSV(cif, source, sink, &rptr))
9648c2ecf20Sopenharmony_ci		return -EIO;
9658c2ecf20Sopenharmony_ci	*a = rptr.retbytes[0];
9668c2ecf20Sopenharmony_ci	*b = rptr.retbytes[1];
9678c2ecf20Sopenharmony_ci	snd_printdd("getsourcesink 0x%x 0x%x\n", *a, *b);
9688c2ecf20Sopenharmony_ci	return 0;
9698c2ecf20Sopenharmony_ci}
9708c2ecf20Sopenharmony_ci
9718c2ecf20Sopenharmony_cistatic int
9728c2ecf20Sopenharmony_cigetsamplerate(struct cmdif *cif, unsigned char *intdec, unsigned int *rate)
9738c2ecf20Sopenharmony_ci{
9748c2ecf20Sopenharmony_ci	unsigned char *s;
9758c2ecf20Sopenharmony_ci	unsigned int p[2] = { 0, 0 };
9768c2ecf20Sopenharmony_ci	int i;
9778c2ecf20Sopenharmony_ci	union cmdret rptr = CMDRET_ZERO;
9788c2ecf20Sopenharmony_ci
9798c2ecf20Sopenharmony_ci	s = intdec;
9808c2ecf20Sopenharmony_ci	for (i = 0; i < 2; i++) {
9818c2ecf20Sopenharmony_ci		if (*s != 0xff) {
9828c2ecf20Sopenharmony_ci			if (SEND_RSRC(cif, *s, &rptr) &&
9838c2ecf20Sopenharmony_ci			    SEND_RSRC(cif, *s, &rptr))
9848c2ecf20Sopenharmony_ci				return -EIO;
9858c2ecf20Sopenharmony_ci			p[i] += rptr.retwords[1];
9868c2ecf20Sopenharmony_ci			p[i] *= rptr.retwords[2];
9878c2ecf20Sopenharmony_ci			p[i] += rptr.retwords[3];
9888c2ecf20Sopenharmony_ci			p[i] /= 65536;
9898c2ecf20Sopenharmony_ci		}
9908c2ecf20Sopenharmony_ci		s++;
9918c2ecf20Sopenharmony_ci	}
9928c2ecf20Sopenharmony_ci	if (p[0]) {
9938c2ecf20Sopenharmony_ci		if (p[1] != p[0])
9948c2ecf20Sopenharmony_ci			snd_printdd("rates differ %d %d\n", p[0], p[1]);
9958c2ecf20Sopenharmony_ci		*rate = (unsigned int)p[0];
9968c2ecf20Sopenharmony_ci	} else
9978c2ecf20Sopenharmony_ci		*rate = (unsigned int)p[1];
9988c2ecf20Sopenharmony_ci	snd_printdd("getsampleformat %d %d %d\n", intdec[0], intdec[1], *rate);
9998c2ecf20Sopenharmony_ci	return 0;
10008c2ecf20Sopenharmony_ci}
10018c2ecf20Sopenharmony_ci
10028c2ecf20Sopenharmony_cistatic int
10038c2ecf20Sopenharmony_cisetsampleformat(struct cmdif *cif,
10048c2ecf20Sopenharmony_ci		unsigned char mixer, unsigned char id,
10058c2ecf20Sopenharmony_ci		unsigned char channels, snd_pcm_format_t format)
10068c2ecf20Sopenharmony_ci{
10078c2ecf20Sopenharmony_ci	unsigned char w, ch, sig, order;
10088c2ecf20Sopenharmony_ci
10098c2ecf20Sopenharmony_ci	snd_printdd
10108c2ecf20Sopenharmony_ci	    ("setsampleformat mixer: %d id: %d channels: %d format: %d\n",
10118c2ecf20Sopenharmony_ci	     mixer, id, channels, format);
10128c2ecf20Sopenharmony_ci	ch = channels == 1;
10138c2ecf20Sopenharmony_ci	w = snd_pcm_format_width(format) == 8;
10148c2ecf20Sopenharmony_ci	sig = snd_pcm_format_unsigned(format) != 0;
10158c2ecf20Sopenharmony_ci	order = snd_pcm_format_big_endian(format) != 0;
10168c2ecf20Sopenharmony_ci
10178c2ecf20Sopenharmony_ci	if (SEND_SETF(cif, mixer, w, ch, order, sig, id) &&
10188c2ecf20Sopenharmony_ci	    SEND_SETF(cif, mixer, w, ch, order, sig, id)) {
10198c2ecf20Sopenharmony_ci		snd_printdd("setsampleformat failed\n");
10208c2ecf20Sopenharmony_ci		return -EIO;
10218c2ecf20Sopenharmony_ci	}
10228c2ecf20Sopenharmony_ci	return 0;
10238c2ecf20Sopenharmony_ci}
10248c2ecf20Sopenharmony_ci
10258c2ecf20Sopenharmony_cistatic int
10268c2ecf20Sopenharmony_cisetsamplerate(struct cmdif *cif, unsigned char *intdec, unsigned int rate)
10278c2ecf20Sopenharmony_ci{
10288c2ecf20Sopenharmony_ci	u32 D, M, N;
10298c2ecf20Sopenharmony_ci	union cmdret rptr = CMDRET_ZERO;
10308c2ecf20Sopenharmony_ci	int i;
10318c2ecf20Sopenharmony_ci
10328c2ecf20Sopenharmony_ci	snd_printdd("setsamplerate intdec: %d,%d rate: %d\n", intdec[0],
10338c2ecf20Sopenharmony_ci		    intdec[1], rate);
10348c2ecf20Sopenharmony_ci	D = 48000;
10358c2ecf20Sopenharmony_ci	M = ((rate == 48000) ? 47999 : rate) * 65536;
10368c2ecf20Sopenharmony_ci	N = M % D;
10378c2ecf20Sopenharmony_ci	M /= D;
10388c2ecf20Sopenharmony_ci	for (i = 0; i < 2; i++) {
10398c2ecf20Sopenharmony_ci		if (*intdec != 0xff) {
10408c2ecf20Sopenharmony_ci			do {
10418c2ecf20Sopenharmony_ci				SEND_SSRC(cif, *intdec, D, M, N);
10428c2ecf20Sopenharmony_ci				SEND_RSRC(cif, *intdec, &rptr);
10438c2ecf20Sopenharmony_ci			} while (rptr.retwords[1] != D &&
10448c2ecf20Sopenharmony_ci				 rptr.retwords[2] != M &&
10458c2ecf20Sopenharmony_ci				 rptr.retwords[3] != N &&
10468c2ecf20Sopenharmony_ci				 i++ < MAX_WRITE_RETRY);
10478c2ecf20Sopenharmony_ci			if (i > MAX_WRITE_RETRY) {
10488c2ecf20Sopenharmony_ci				snd_printdd("sent samplerate %d: %d failed\n",
10498c2ecf20Sopenharmony_ci					    *intdec, rate);
10508c2ecf20Sopenharmony_ci				return -EIO;
10518c2ecf20Sopenharmony_ci			}
10528c2ecf20Sopenharmony_ci		}
10538c2ecf20Sopenharmony_ci		intdec++;
10548c2ecf20Sopenharmony_ci	}
10558c2ecf20Sopenharmony_ci	return 0;
10568c2ecf20Sopenharmony_ci}
10578c2ecf20Sopenharmony_ci
10588c2ecf20Sopenharmony_cistatic int
10598c2ecf20Sopenharmony_cigetmixer(struct cmdif *cif, short num, unsigned short *rval,
10608c2ecf20Sopenharmony_ci	 unsigned short *lval)
10618c2ecf20Sopenharmony_ci{
10628c2ecf20Sopenharmony_ci	union cmdret rptr = CMDRET_ZERO;
10638c2ecf20Sopenharmony_ci
10648c2ecf20Sopenharmony_ci	if (SEND_RDGV(cif, num, num, &rptr) && SEND_RDGV(cif, num, num, &rptr))
10658c2ecf20Sopenharmony_ci		return -EIO;
10668c2ecf20Sopenharmony_ci	*rval = rptr.retwords[0];
10678c2ecf20Sopenharmony_ci	*lval = rptr.retwords[1];
10688c2ecf20Sopenharmony_ci	snd_printdd("got mixer %d: 0x%x 0x%x\n", num, *rval, *lval);
10698c2ecf20Sopenharmony_ci	return 0;
10708c2ecf20Sopenharmony_ci}
10718c2ecf20Sopenharmony_ci
10728c2ecf20Sopenharmony_cistatic irqreturn_t riptide_handleirq(int irq, void *dev_id)
10738c2ecf20Sopenharmony_ci{
10748c2ecf20Sopenharmony_ci	struct snd_riptide *chip = dev_id;
10758c2ecf20Sopenharmony_ci	struct cmdif *cif = chip->cif;
10768c2ecf20Sopenharmony_ci	struct snd_pcm_substream *substream[PLAYBACK_SUBSTREAMS + 1];
10778c2ecf20Sopenharmony_ci	struct snd_pcm_runtime *runtime;
10788c2ecf20Sopenharmony_ci	struct pcmhw *data = NULL;
10798c2ecf20Sopenharmony_ci	unsigned int pos, period_bytes;
10808c2ecf20Sopenharmony_ci	struct sgd *c;
10818c2ecf20Sopenharmony_ci	int i, j;
10828c2ecf20Sopenharmony_ci	unsigned int flag;
10838c2ecf20Sopenharmony_ci
10848c2ecf20Sopenharmony_ci	if (!cif)
10858c2ecf20Sopenharmony_ci		return IRQ_HANDLED;
10868c2ecf20Sopenharmony_ci
10878c2ecf20Sopenharmony_ci	for (i = 0; i < PLAYBACK_SUBSTREAMS; i++)
10888c2ecf20Sopenharmony_ci		substream[i] = chip->playback_substream[i];
10898c2ecf20Sopenharmony_ci	substream[i] = chip->capture_substream;
10908c2ecf20Sopenharmony_ci	for (i = 0; i < PLAYBACK_SUBSTREAMS + 1; i++) {
10918c2ecf20Sopenharmony_ci		if (substream[i] &&
10928c2ecf20Sopenharmony_ci		    (runtime = substream[i]->runtime) &&
10938c2ecf20Sopenharmony_ci		    (data = runtime->private_data) && data->state != ST_STOP) {
10948c2ecf20Sopenharmony_ci			pos = 0;
10958c2ecf20Sopenharmony_ci			for (j = 0; j < data->pages; j++) {
10968c2ecf20Sopenharmony_ci				c = &data->sgdbuf[j];
10978c2ecf20Sopenharmony_ci				flag = le32_to_cpu(c->dwStat_Ctl);
10988c2ecf20Sopenharmony_ci				if (flag & EOB_STATUS)
10998c2ecf20Sopenharmony_ci					pos += le32_to_cpu(c->dwSegLen);
11008c2ecf20Sopenharmony_ci				if (flag & EOC_STATUS)
11018c2ecf20Sopenharmony_ci					pos += le32_to_cpu(c->dwSegLen);
11028c2ecf20Sopenharmony_ci				if ((flag & EOS_STATUS)
11038c2ecf20Sopenharmony_ci				    && (data->state == ST_PLAY)) {
11048c2ecf20Sopenharmony_ci					data->state = ST_STOP;
11058c2ecf20Sopenharmony_ci					snd_printk(KERN_ERR
11068c2ecf20Sopenharmony_ci						   "Riptide: DMA stopped unexpectedly\n");
11078c2ecf20Sopenharmony_ci				}
11088c2ecf20Sopenharmony_ci				c->dwStat_Ctl =
11098c2ecf20Sopenharmony_ci				    cpu_to_le32(flag &
11108c2ecf20Sopenharmony_ci						~(EOS_STATUS | EOB_STATUS |
11118c2ecf20Sopenharmony_ci						  EOC_STATUS));
11128c2ecf20Sopenharmony_ci			}
11138c2ecf20Sopenharmony_ci			data->pointer += pos;
11148c2ecf20Sopenharmony_ci			pos += data->oldpos;
11158c2ecf20Sopenharmony_ci			if (data->state != ST_STOP) {
11168c2ecf20Sopenharmony_ci				period_bytes =
11178c2ecf20Sopenharmony_ci				    frames_to_bytes(runtime,
11188c2ecf20Sopenharmony_ci						    runtime->period_size);
11198c2ecf20Sopenharmony_ci				snd_printdd
11208c2ecf20Sopenharmony_ci				    ("interrupt 0x%x after 0x%lx of 0x%lx frames in period\n",
11218c2ecf20Sopenharmony_ci				     READ_AUDIO_STATUS(cif->hwport),
11228c2ecf20Sopenharmony_ci				     bytes_to_frames(runtime, pos),
11238c2ecf20Sopenharmony_ci				     runtime->period_size);
11248c2ecf20Sopenharmony_ci				j = 0;
11258c2ecf20Sopenharmony_ci				if (pos >= period_bytes) {
11268c2ecf20Sopenharmony_ci					j++;
11278c2ecf20Sopenharmony_ci					while (pos >= period_bytes)
11288c2ecf20Sopenharmony_ci						pos -= period_bytes;
11298c2ecf20Sopenharmony_ci				}
11308c2ecf20Sopenharmony_ci				data->oldpos = pos;
11318c2ecf20Sopenharmony_ci				if (j > 0)
11328c2ecf20Sopenharmony_ci					snd_pcm_period_elapsed(substream[i]);
11338c2ecf20Sopenharmony_ci			}
11348c2ecf20Sopenharmony_ci		}
11358c2ecf20Sopenharmony_ci	}
11368c2ecf20Sopenharmony_ci
11378c2ecf20Sopenharmony_ci	return IRQ_HANDLED;
11388c2ecf20Sopenharmony_ci}
11398c2ecf20Sopenharmony_ci
11408c2ecf20Sopenharmony_ci#ifdef CONFIG_PM_SLEEP
11418c2ecf20Sopenharmony_cistatic int riptide_suspend(struct device *dev)
11428c2ecf20Sopenharmony_ci{
11438c2ecf20Sopenharmony_ci	struct snd_card *card = dev_get_drvdata(dev);
11448c2ecf20Sopenharmony_ci	struct snd_riptide *chip = card->private_data;
11458c2ecf20Sopenharmony_ci
11468c2ecf20Sopenharmony_ci	chip->in_suspend = 1;
11478c2ecf20Sopenharmony_ci	snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
11488c2ecf20Sopenharmony_ci	snd_ac97_suspend(chip->ac97);
11498c2ecf20Sopenharmony_ci	return 0;
11508c2ecf20Sopenharmony_ci}
11518c2ecf20Sopenharmony_ci
11528c2ecf20Sopenharmony_cistatic int riptide_resume(struct device *dev)
11538c2ecf20Sopenharmony_ci{
11548c2ecf20Sopenharmony_ci	struct snd_card *card = dev_get_drvdata(dev);
11558c2ecf20Sopenharmony_ci	struct snd_riptide *chip = card->private_data;
11568c2ecf20Sopenharmony_ci
11578c2ecf20Sopenharmony_ci	snd_riptide_initialize(chip);
11588c2ecf20Sopenharmony_ci	snd_ac97_resume(chip->ac97);
11598c2ecf20Sopenharmony_ci	snd_power_change_state(card, SNDRV_CTL_POWER_D0);
11608c2ecf20Sopenharmony_ci	chip->in_suspend = 0;
11618c2ecf20Sopenharmony_ci	return 0;
11628c2ecf20Sopenharmony_ci}
11638c2ecf20Sopenharmony_ci
11648c2ecf20Sopenharmony_cistatic SIMPLE_DEV_PM_OPS(riptide_pm, riptide_suspend, riptide_resume);
11658c2ecf20Sopenharmony_ci#define RIPTIDE_PM_OPS	&riptide_pm
11668c2ecf20Sopenharmony_ci#else
11678c2ecf20Sopenharmony_ci#define RIPTIDE_PM_OPS	NULL
11688c2ecf20Sopenharmony_ci#endif /* CONFIG_PM_SLEEP */
11698c2ecf20Sopenharmony_ci
11708c2ecf20Sopenharmony_cistatic int try_to_load_firmware(struct cmdif *cif, struct snd_riptide *chip)
11718c2ecf20Sopenharmony_ci{
11728c2ecf20Sopenharmony_ci	union firmware_version firmware = { .ret = CMDRET_ZERO };
11738c2ecf20Sopenharmony_ci	int i, timeout, err;
11748c2ecf20Sopenharmony_ci
11758c2ecf20Sopenharmony_ci	for (i = 0; i < 2; i++) {
11768c2ecf20Sopenharmony_ci		WRITE_PORT_ULONG(cif->hwport->port[i].data1, 0);
11778c2ecf20Sopenharmony_ci		WRITE_PORT_ULONG(cif->hwport->port[i].data2, 0);
11788c2ecf20Sopenharmony_ci	}
11798c2ecf20Sopenharmony_ci	SET_GRESET(cif->hwport);
11808c2ecf20Sopenharmony_ci	udelay(100);
11818c2ecf20Sopenharmony_ci	UNSET_GRESET(cif->hwport);
11828c2ecf20Sopenharmony_ci	udelay(100);
11838c2ecf20Sopenharmony_ci
11848c2ecf20Sopenharmony_ci	for (timeout = 100000; --timeout; udelay(10)) {
11858c2ecf20Sopenharmony_ci		if (IS_READY(cif->hwport) && !IS_GERR(cif->hwport))
11868c2ecf20Sopenharmony_ci			break;
11878c2ecf20Sopenharmony_ci	}
11888c2ecf20Sopenharmony_ci	if (!timeout) {
11898c2ecf20Sopenharmony_ci		snd_printk(KERN_ERR
11908c2ecf20Sopenharmony_ci			   "Riptide: device not ready, audio status: 0x%x "
11918c2ecf20Sopenharmony_ci			   "ready: %d gerr: %d\n",
11928c2ecf20Sopenharmony_ci			   READ_AUDIO_STATUS(cif->hwport),
11938c2ecf20Sopenharmony_ci			   IS_READY(cif->hwport), IS_GERR(cif->hwport));
11948c2ecf20Sopenharmony_ci		return -EIO;
11958c2ecf20Sopenharmony_ci	} else {
11968c2ecf20Sopenharmony_ci		snd_printdd
11978c2ecf20Sopenharmony_ci			("Riptide: audio status: 0x%x ready: %d gerr: %d\n",
11988c2ecf20Sopenharmony_ci			 READ_AUDIO_STATUS(cif->hwport),
11998c2ecf20Sopenharmony_ci			 IS_READY(cif->hwport), IS_GERR(cif->hwport));
12008c2ecf20Sopenharmony_ci	}
12018c2ecf20Sopenharmony_ci
12028c2ecf20Sopenharmony_ci	SEND_GETV(cif, &firmware.ret);
12038c2ecf20Sopenharmony_ci	snd_printdd("Firmware version: ASIC: %d CODEC %d AUXDSP %d PROG %d\n",
12048c2ecf20Sopenharmony_ci		    firmware.firmware.ASIC, firmware.firmware.CODEC,
12058c2ecf20Sopenharmony_ci		    firmware.firmware.AUXDSP, firmware.firmware.PROG);
12068c2ecf20Sopenharmony_ci
12078c2ecf20Sopenharmony_ci	if (!chip)
12088c2ecf20Sopenharmony_ci		return 1;
12098c2ecf20Sopenharmony_ci
12108c2ecf20Sopenharmony_ci	for (i = 0; i < FIRMWARE_VERSIONS; i++) {
12118c2ecf20Sopenharmony_ci		if (!memcmp(&firmware_versions[i], &firmware, sizeof(firmware)))
12128c2ecf20Sopenharmony_ci			return 1; /* OK */
12138c2ecf20Sopenharmony_ci
12148c2ecf20Sopenharmony_ci	}
12158c2ecf20Sopenharmony_ci
12168c2ecf20Sopenharmony_ci	snd_printdd("Writing Firmware\n");
12178c2ecf20Sopenharmony_ci	if (!chip->fw_entry) {
12188c2ecf20Sopenharmony_ci		err = request_firmware(&chip->fw_entry, "riptide.hex",
12198c2ecf20Sopenharmony_ci				       &chip->pci->dev);
12208c2ecf20Sopenharmony_ci		if (err) {
12218c2ecf20Sopenharmony_ci			snd_printk(KERN_ERR
12228c2ecf20Sopenharmony_ci				   "Riptide: Firmware not available %d\n", err);
12238c2ecf20Sopenharmony_ci			return -EIO;
12248c2ecf20Sopenharmony_ci		}
12258c2ecf20Sopenharmony_ci	}
12268c2ecf20Sopenharmony_ci	err = loadfirmware(cif, chip->fw_entry->data, chip->fw_entry->size);
12278c2ecf20Sopenharmony_ci	if (err) {
12288c2ecf20Sopenharmony_ci		snd_printk(KERN_ERR
12298c2ecf20Sopenharmony_ci			   "Riptide: Could not load firmware %d\n", err);
12308c2ecf20Sopenharmony_ci		return err;
12318c2ecf20Sopenharmony_ci	}
12328c2ecf20Sopenharmony_ci
12338c2ecf20Sopenharmony_ci	chip->firmware = firmware;
12348c2ecf20Sopenharmony_ci
12358c2ecf20Sopenharmony_ci	return 1; /* OK */
12368c2ecf20Sopenharmony_ci}
12378c2ecf20Sopenharmony_ci
12388c2ecf20Sopenharmony_cistatic int riptide_reset(struct cmdif *cif, struct snd_riptide *chip)
12398c2ecf20Sopenharmony_ci{
12408c2ecf20Sopenharmony_ci	union cmdret rptr = CMDRET_ZERO;
12418c2ecf20Sopenharmony_ci	int err, tries;
12428c2ecf20Sopenharmony_ci
12438c2ecf20Sopenharmony_ci	if (!cif)
12448c2ecf20Sopenharmony_ci		return -EINVAL;
12458c2ecf20Sopenharmony_ci
12468c2ecf20Sopenharmony_ci	cif->cmdcnt = 0;
12478c2ecf20Sopenharmony_ci	cif->cmdtime = 0;
12488c2ecf20Sopenharmony_ci	cif->cmdtimemax = 0;
12498c2ecf20Sopenharmony_ci	cif->cmdtimemin = 0xffffffff;
12508c2ecf20Sopenharmony_ci	cif->errcnt = 0;
12518c2ecf20Sopenharmony_ci	cif->is_reset = 0;
12528c2ecf20Sopenharmony_ci
12538c2ecf20Sopenharmony_ci	tries = RESET_TRIES;
12548c2ecf20Sopenharmony_ci	do {
12558c2ecf20Sopenharmony_ci		err = try_to_load_firmware(cif, chip);
12568c2ecf20Sopenharmony_ci		if (err < 0)
12578c2ecf20Sopenharmony_ci			return err;
12588c2ecf20Sopenharmony_ci	} while (!err && --tries);
12598c2ecf20Sopenharmony_ci
12608c2ecf20Sopenharmony_ci	SEND_SACR(cif, 0, AC97_RESET);
12618c2ecf20Sopenharmony_ci	SEND_RACR(cif, AC97_RESET, &rptr);
12628c2ecf20Sopenharmony_ci	snd_printdd("AC97: 0x%x 0x%x\n", rptr.retlongs[0], rptr.retlongs[1]);
12638c2ecf20Sopenharmony_ci
12648c2ecf20Sopenharmony_ci	SEND_PLST(cif, 0);
12658c2ecf20Sopenharmony_ci	SEND_SLST(cif, 0);
12668c2ecf20Sopenharmony_ci	SEND_DLST(cif, 0);
12678c2ecf20Sopenharmony_ci	SEND_ALST(cif, 0);
12688c2ecf20Sopenharmony_ci	SEND_KDMA(cif);
12698c2ecf20Sopenharmony_ci
12708c2ecf20Sopenharmony_ci	writearm(cif, 0x301F8, 1, 1);
12718c2ecf20Sopenharmony_ci	writearm(cif, 0x301F4, 1, 1);
12728c2ecf20Sopenharmony_ci
12738c2ecf20Sopenharmony_ci	SEND_LSEL(cif, MODEM_CMD, 0, 0, MODEM_INTDEC, MODEM_MERGER,
12748c2ecf20Sopenharmony_ci		  MODEM_SPLITTER, MODEM_MIXER);
12758c2ecf20Sopenharmony_ci	setmixer(cif, MODEM_MIXER, 0x7fff, 0x7fff);
12768c2ecf20Sopenharmony_ci	alloclbuspath(cif, ARM2LBUS_FIFO13, lbus_play_modem, NULL, NULL);
12778c2ecf20Sopenharmony_ci
12788c2ecf20Sopenharmony_ci	SEND_LSEL(cif, FM_CMD, 0, 0, FM_INTDEC, FM_MERGER, FM_SPLITTER,
12798c2ecf20Sopenharmony_ci		  FM_MIXER);
12808c2ecf20Sopenharmony_ci	setmixer(cif, FM_MIXER, 0x7fff, 0x7fff);
12818c2ecf20Sopenharmony_ci	writearm(cif, 0x30648 + FM_MIXER * 4, 0x01, 0x00000005);
12828c2ecf20Sopenharmony_ci	writearm(cif, 0x301A8, 0x02, 0x00000002);
12838c2ecf20Sopenharmony_ci	writearm(cif, 0x30264, 0x08, 0xffffffff);
12848c2ecf20Sopenharmony_ci	alloclbuspath(cif, OPL3_SAMPLE, lbus_play_opl3, NULL, NULL);
12858c2ecf20Sopenharmony_ci
12868c2ecf20Sopenharmony_ci	SEND_SSRC(cif, I2S_INTDEC, 48000,
12878c2ecf20Sopenharmony_ci		  ((u32) I2S_RATE * 65536) / 48000,
12888c2ecf20Sopenharmony_ci		  ((u32) I2S_RATE * 65536) % 48000);
12898c2ecf20Sopenharmony_ci	SEND_LSEL(cif, I2S_CMD0, 0, 0, I2S_INTDEC, I2S_MERGER, I2S_SPLITTER,
12908c2ecf20Sopenharmony_ci		  I2S_MIXER);
12918c2ecf20Sopenharmony_ci	SEND_SI2S(cif, 1);
12928c2ecf20Sopenharmony_ci	alloclbuspath(cif, ARM2LBUS_FIFO0, lbus_play_i2s, NULL, NULL);
12938c2ecf20Sopenharmony_ci	alloclbuspath(cif, DIGITAL_MIXER_OUT0, lbus_play_out, NULL, NULL);
12948c2ecf20Sopenharmony_ci	alloclbuspath(cif, DIGITAL_MIXER_OUT0, lbus_play_outhp, NULL, NULL);
12958c2ecf20Sopenharmony_ci
12968c2ecf20Sopenharmony_ci	SET_AIACK(cif->hwport);
12978c2ecf20Sopenharmony_ci	SET_AIE(cif->hwport);
12988c2ecf20Sopenharmony_ci	SET_AIACK(cif->hwport);
12998c2ecf20Sopenharmony_ci	cif->is_reset = 1;
13008c2ecf20Sopenharmony_ci
13018c2ecf20Sopenharmony_ci	return 0;
13028c2ecf20Sopenharmony_ci}
13038c2ecf20Sopenharmony_ci
13048c2ecf20Sopenharmony_cistatic const struct snd_pcm_hardware snd_riptide_playback = {
13058c2ecf20Sopenharmony_ci	.info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
13068c2ecf20Sopenharmony_ci		 SNDRV_PCM_INFO_BLOCK_TRANSFER |
13078c2ecf20Sopenharmony_ci		 SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_MMAP_VALID),
13088c2ecf20Sopenharmony_ci	.formats =
13098c2ecf20Sopenharmony_ci	    SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S8
13108c2ecf20Sopenharmony_ci	    | SNDRV_PCM_FMTBIT_U16_LE,
13118c2ecf20Sopenharmony_ci	.rates = SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_8000_48000,
13128c2ecf20Sopenharmony_ci	.rate_min = 5500,
13138c2ecf20Sopenharmony_ci	.rate_max = 48000,
13148c2ecf20Sopenharmony_ci	.channels_min = 1,
13158c2ecf20Sopenharmony_ci	.channels_max = 2,
13168c2ecf20Sopenharmony_ci	.buffer_bytes_max = (64 * 1024),
13178c2ecf20Sopenharmony_ci	.period_bytes_min = PAGE_SIZE >> 1,
13188c2ecf20Sopenharmony_ci	.period_bytes_max = PAGE_SIZE << 8,
13198c2ecf20Sopenharmony_ci	.periods_min = 2,
13208c2ecf20Sopenharmony_ci	.periods_max = 64,
13218c2ecf20Sopenharmony_ci	.fifo_size = 0,
13228c2ecf20Sopenharmony_ci};
13238c2ecf20Sopenharmony_cistatic const struct snd_pcm_hardware snd_riptide_capture = {
13248c2ecf20Sopenharmony_ci	.info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
13258c2ecf20Sopenharmony_ci		 SNDRV_PCM_INFO_BLOCK_TRANSFER |
13268c2ecf20Sopenharmony_ci		 SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_MMAP_VALID),
13278c2ecf20Sopenharmony_ci	.formats =
13288c2ecf20Sopenharmony_ci	    SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S8
13298c2ecf20Sopenharmony_ci	    | SNDRV_PCM_FMTBIT_U16_LE,
13308c2ecf20Sopenharmony_ci	.rates = SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_8000_48000,
13318c2ecf20Sopenharmony_ci	.rate_min = 5500,
13328c2ecf20Sopenharmony_ci	.rate_max = 48000,
13338c2ecf20Sopenharmony_ci	.channels_min = 1,
13348c2ecf20Sopenharmony_ci	.channels_max = 2,
13358c2ecf20Sopenharmony_ci	.buffer_bytes_max = (64 * 1024),
13368c2ecf20Sopenharmony_ci	.period_bytes_min = PAGE_SIZE >> 1,
13378c2ecf20Sopenharmony_ci	.period_bytes_max = PAGE_SIZE << 3,
13388c2ecf20Sopenharmony_ci	.periods_min = 2,
13398c2ecf20Sopenharmony_ci	.periods_max = 64,
13408c2ecf20Sopenharmony_ci	.fifo_size = 0,
13418c2ecf20Sopenharmony_ci};
13428c2ecf20Sopenharmony_ci
13438c2ecf20Sopenharmony_cistatic snd_pcm_uframes_t snd_riptide_pointer(struct snd_pcm_substream
13448c2ecf20Sopenharmony_ci					     *substream)
13458c2ecf20Sopenharmony_ci{
13468c2ecf20Sopenharmony_ci	struct snd_riptide *chip = snd_pcm_substream_chip(substream);
13478c2ecf20Sopenharmony_ci	struct snd_pcm_runtime *runtime = substream->runtime;
13488c2ecf20Sopenharmony_ci	struct pcmhw *data = get_pcmhwdev(substream);
13498c2ecf20Sopenharmony_ci	struct cmdif *cif = chip->cif;
13508c2ecf20Sopenharmony_ci	union cmdret rptr = CMDRET_ZERO;
13518c2ecf20Sopenharmony_ci	snd_pcm_uframes_t ret;
13528c2ecf20Sopenharmony_ci
13538c2ecf20Sopenharmony_ci	SEND_GPOS(cif, 0, data->id, &rptr);
13548c2ecf20Sopenharmony_ci	if (data->size && runtime->period_size) {
13558c2ecf20Sopenharmony_ci		snd_printdd
13568c2ecf20Sopenharmony_ci		    ("pointer stream %d position 0x%x(0x%x in buffer) bytes 0x%lx(0x%lx in period) frames\n",
13578c2ecf20Sopenharmony_ci		     data->id, rptr.retlongs[1], rptr.retlongs[1] % data->size,
13588c2ecf20Sopenharmony_ci		     bytes_to_frames(runtime, rptr.retlongs[1]),
13598c2ecf20Sopenharmony_ci		     bytes_to_frames(runtime,
13608c2ecf20Sopenharmony_ci				     rptr.retlongs[1]) % runtime->period_size);
13618c2ecf20Sopenharmony_ci		if (rptr.retlongs[1] > data->pointer)
13628c2ecf20Sopenharmony_ci			ret =
13638c2ecf20Sopenharmony_ci			    bytes_to_frames(runtime,
13648c2ecf20Sopenharmony_ci					    rptr.retlongs[1] % data->size);
13658c2ecf20Sopenharmony_ci		else
13668c2ecf20Sopenharmony_ci			ret =
13678c2ecf20Sopenharmony_ci			    bytes_to_frames(runtime,
13688c2ecf20Sopenharmony_ci					    data->pointer % data->size);
13698c2ecf20Sopenharmony_ci	} else {
13708c2ecf20Sopenharmony_ci		snd_printdd("stream not started or strange parms (%d %ld)\n",
13718c2ecf20Sopenharmony_ci			    data->size, runtime->period_size);
13728c2ecf20Sopenharmony_ci		ret = bytes_to_frames(runtime, 0);
13738c2ecf20Sopenharmony_ci	}
13748c2ecf20Sopenharmony_ci	return ret;
13758c2ecf20Sopenharmony_ci}
13768c2ecf20Sopenharmony_ci
13778c2ecf20Sopenharmony_cistatic int snd_riptide_trigger(struct snd_pcm_substream *substream, int cmd)
13788c2ecf20Sopenharmony_ci{
13798c2ecf20Sopenharmony_ci	int i, j;
13808c2ecf20Sopenharmony_ci	struct snd_riptide *chip = snd_pcm_substream_chip(substream);
13818c2ecf20Sopenharmony_ci	struct pcmhw *data = get_pcmhwdev(substream);
13828c2ecf20Sopenharmony_ci	struct cmdif *cif = chip->cif;
13838c2ecf20Sopenharmony_ci	union cmdret rptr = CMDRET_ZERO;
13848c2ecf20Sopenharmony_ci
13858c2ecf20Sopenharmony_ci	spin_lock(&chip->lock);
13868c2ecf20Sopenharmony_ci	switch (cmd) {
13878c2ecf20Sopenharmony_ci	case SNDRV_PCM_TRIGGER_START:
13888c2ecf20Sopenharmony_ci	case SNDRV_PCM_TRIGGER_RESUME:
13898c2ecf20Sopenharmony_ci		if (!(data->state & ST_PLAY)) {
13908c2ecf20Sopenharmony_ci			SEND_SSTR(cif, data->id, data->sgdlist.addr);
13918c2ecf20Sopenharmony_ci			SET_AIE(cif->hwport);
13928c2ecf20Sopenharmony_ci			data->state = ST_PLAY;
13938c2ecf20Sopenharmony_ci			if (data->mixer != 0xff)
13948c2ecf20Sopenharmony_ci				setmixer(cif, data->mixer, 0x7fff, 0x7fff);
13958c2ecf20Sopenharmony_ci			chip->openstreams++;
13968c2ecf20Sopenharmony_ci			data->oldpos = 0;
13978c2ecf20Sopenharmony_ci			data->pointer = 0;
13988c2ecf20Sopenharmony_ci		}
13998c2ecf20Sopenharmony_ci		break;
14008c2ecf20Sopenharmony_ci	case SNDRV_PCM_TRIGGER_STOP:
14018c2ecf20Sopenharmony_ci	case SNDRV_PCM_TRIGGER_SUSPEND:
14028c2ecf20Sopenharmony_ci		if (data->mixer != 0xff)
14038c2ecf20Sopenharmony_ci			setmixer(cif, data->mixer, 0, 0);
14048c2ecf20Sopenharmony_ci		setmixer(cif, data->mixer, 0, 0);
14058c2ecf20Sopenharmony_ci		SEND_KSTR(cif, data->id);
14068c2ecf20Sopenharmony_ci		data->state = ST_STOP;
14078c2ecf20Sopenharmony_ci		chip->openstreams--;
14088c2ecf20Sopenharmony_ci		j = 0;
14098c2ecf20Sopenharmony_ci		do {
14108c2ecf20Sopenharmony_ci			i = rptr.retlongs[1];
14118c2ecf20Sopenharmony_ci			SEND_GPOS(cif, 0, data->id, &rptr);
14128c2ecf20Sopenharmony_ci			udelay(1);
14138c2ecf20Sopenharmony_ci		} while (i != rptr.retlongs[1] && j++ < MAX_WRITE_RETRY);
14148c2ecf20Sopenharmony_ci		if (j > MAX_WRITE_RETRY)
14158c2ecf20Sopenharmony_ci			snd_printk(KERN_ERR "Riptide: Could not stop stream!");
14168c2ecf20Sopenharmony_ci		break;
14178c2ecf20Sopenharmony_ci	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
14188c2ecf20Sopenharmony_ci		if (!(data->state & ST_PAUSE)) {
14198c2ecf20Sopenharmony_ci			SEND_PSTR(cif, data->id);
14208c2ecf20Sopenharmony_ci			data->state |= ST_PAUSE;
14218c2ecf20Sopenharmony_ci			chip->openstreams--;
14228c2ecf20Sopenharmony_ci		}
14238c2ecf20Sopenharmony_ci		break;
14248c2ecf20Sopenharmony_ci	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
14258c2ecf20Sopenharmony_ci		if (data->state & ST_PAUSE) {
14268c2ecf20Sopenharmony_ci			SEND_SSTR(cif, data->id, data->sgdlist.addr);
14278c2ecf20Sopenharmony_ci			data->state &= ~ST_PAUSE;
14288c2ecf20Sopenharmony_ci			chip->openstreams++;
14298c2ecf20Sopenharmony_ci		}
14308c2ecf20Sopenharmony_ci		break;
14318c2ecf20Sopenharmony_ci	default:
14328c2ecf20Sopenharmony_ci		spin_unlock(&chip->lock);
14338c2ecf20Sopenharmony_ci		return -EINVAL;
14348c2ecf20Sopenharmony_ci	}
14358c2ecf20Sopenharmony_ci	spin_unlock(&chip->lock);
14368c2ecf20Sopenharmony_ci	return 0;
14378c2ecf20Sopenharmony_ci}
14388c2ecf20Sopenharmony_ci
14398c2ecf20Sopenharmony_cistatic int snd_riptide_prepare(struct snd_pcm_substream *substream)
14408c2ecf20Sopenharmony_ci{
14418c2ecf20Sopenharmony_ci	struct snd_riptide *chip = snd_pcm_substream_chip(substream);
14428c2ecf20Sopenharmony_ci	struct snd_pcm_runtime *runtime = substream->runtime;
14438c2ecf20Sopenharmony_ci	struct pcmhw *data = get_pcmhwdev(substream);
14448c2ecf20Sopenharmony_ci	struct cmdif *cif = chip->cif;
14458c2ecf20Sopenharmony_ci	const unsigned char *lbuspath = NULL;
14468c2ecf20Sopenharmony_ci	unsigned int rate, channels;
14478c2ecf20Sopenharmony_ci	int err = 0;
14488c2ecf20Sopenharmony_ci	snd_pcm_format_t format;
14498c2ecf20Sopenharmony_ci
14508c2ecf20Sopenharmony_ci	if (snd_BUG_ON(!cif || !data))
14518c2ecf20Sopenharmony_ci		return -EINVAL;
14528c2ecf20Sopenharmony_ci
14538c2ecf20Sopenharmony_ci	snd_printdd("prepare id %d ch: %d f:0x%x r:%d\n", data->id,
14548c2ecf20Sopenharmony_ci		    runtime->channels, runtime->format, runtime->rate);
14558c2ecf20Sopenharmony_ci
14568c2ecf20Sopenharmony_ci	spin_lock_irq(&chip->lock);
14578c2ecf20Sopenharmony_ci	channels = runtime->channels;
14588c2ecf20Sopenharmony_ci	format = runtime->format;
14598c2ecf20Sopenharmony_ci	rate = runtime->rate;
14608c2ecf20Sopenharmony_ci	switch (channels) {
14618c2ecf20Sopenharmony_ci	case 1:
14628c2ecf20Sopenharmony_ci		if (rate == 48000 && format == SNDRV_PCM_FORMAT_S16_LE)
14638c2ecf20Sopenharmony_ci			lbuspath = data->paths.noconv;
14648c2ecf20Sopenharmony_ci		else
14658c2ecf20Sopenharmony_ci			lbuspath = data->paths.mono;
14668c2ecf20Sopenharmony_ci		break;
14678c2ecf20Sopenharmony_ci	case 2:
14688c2ecf20Sopenharmony_ci		if (rate == 48000 && format == SNDRV_PCM_FORMAT_S16_LE)
14698c2ecf20Sopenharmony_ci			lbuspath = data->paths.noconv;
14708c2ecf20Sopenharmony_ci		else
14718c2ecf20Sopenharmony_ci			lbuspath = data->paths.stereo;
14728c2ecf20Sopenharmony_ci		break;
14738c2ecf20Sopenharmony_ci	}
14748c2ecf20Sopenharmony_ci	snd_printdd("use sgdlist at 0x%p\n",
14758c2ecf20Sopenharmony_ci		    data->sgdlist.area);
14768c2ecf20Sopenharmony_ci	if (data->sgdlist.area) {
14778c2ecf20Sopenharmony_ci		unsigned int i, j, size, pages, f, pt, period;
14788c2ecf20Sopenharmony_ci		struct sgd *c, *p = NULL;
14798c2ecf20Sopenharmony_ci
14808c2ecf20Sopenharmony_ci		size = frames_to_bytes(runtime, runtime->buffer_size);
14818c2ecf20Sopenharmony_ci		period = frames_to_bytes(runtime, runtime->period_size);
14828c2ecf20Sopenharmony_ci		f = PAGE_SIZE;
14838c2ecf20Sopenharmony_ci		while ((size + (f >> 1) - 1) <= (f << 7) && (f << 1) > period)
14848c2ecf20Sopenharmony_ci			f = f >> 1;
14858c2ecf20Sopenharmony_ci		pages = DIV_ROUND_UP(size, f);
14868c2ecf20Sopenharmony_ci		data->size = size;
14878c2ecf20Sopenharmony_ci		data->pages = pages;
14888c2ecf20Sopenharmony_ci		snd_printdd
14898c2ecf20Sopenharmony_ci		    ("create sgd size: 0x%x pages %d of size 0x%x for period 0x%x\n",
14908c2ecf20Sopenharmony_ci		     size, pages, f, period);
14918c2ecf20Sopenharmony_ci		pt = 0;
14928c2ecf20Sopenharmony_ci		j = 0;
14938c2ecf20Sopenharmony_ci		for (i = 0; i < pages; i++) {
14948c2ecf20Sopenharmony_ci			unsigned int ofs, addr;
14958c2ecf20Sopenharmony_ci			c = &data->sgdbuf[i];
14968c2ecf20Sopenharmony_ci			if (p)
14978c2ecf20Sopenharmony_ci				p->dwNextLink = cpu_to_le32(data->sgdlist.addr +
14988c2ecf20Sopenharmony_ci							    (i *
14998c2ecf20Sopenharmony_ci							     sizeof(struct
15008c2ecf20Sopenharmony_ci								    sgd)));
15018c2ecf20Sopenharmony_ci			c->dwNextLink = cpu_to_le32(data->sgdlist.addr);
15028c2ecf20Sopenharmony_ci			ofs = j << PAGE_SHIFT;
15038c2ecf20Sopenharmony_ci			addr = snd_pcm_sgbuf_get_addr(substream, ofs) + pt;
15048c2ecf20Sopenharmony_ci			c->dwSegPtrPhys = cpu_to_le32(addr);
15058c2ecf20Sopenharmony_ci			pt = (pt + f) % PAGE_SIZE;
15068c2ecf20Sopenharmony_ci			if (pt == 0)
15078c2ecf20Sopenharmony_ci				j++;
15088c2ecf20Sopenharmony_ci			c->dwSegLen = cpu_to_le32(f);
15098c2ecf20Sopenharmony_ci			c->dwStat_Ctl =
15108c2ecf20Sopenharmony_ci			    cpu_to_le32(IEOB_ENABLE | IEOS_ENABLE |
15118c2ecf20Sopenharmony_ci					IEOC_ENABLE);
15128c2ecf20Sopenharmony_ci			p = c;
15138c2ecf20Sopenharmony_ci			size -= f;
15148c2ecf20Sopenharmony_ci		}
15158c2ecf20Sopenharmony_ci		data->sgdbuf[i].dwSegLen = cpu_to_le32(size);
15168c2ecf20Sopenharmony_ci	}
15178c2ecf20Sopenharmony_ci	if (lbuspath && lbuspath != data->lbuspath) {
15188c2ecf20Sopenharmony_ci		if (data->lbuspath)
15198c2ecf20Sopenharmony_ci			freelbuspath(cif, data->source, data->lbuspath);
15208c2ecf20Sopenharmony_ci		alloclbuspath(cif, data->source, lbuspath,
15218c2ecf20Sopenharmony_ci			      &data->mixer, data->intdec);
15228c2ecf20Sopenharmony_ci		data->lbuspath = lbuspath;
15238c2ecf20Sopenharmony_ci		data->rate = 0;
15248c2ecf20Sopenharmony_ci	}
15258c2ecf20Sopenharmony_ci	if (data->rate != rate || data->format != format ||
15268c2ecf20Sopenharmony_ci	    data->channels != channels) {
15278c2ecf20Sopenharmony_ci		data->rate = rate;
15288c2ecf20Sopenharmony_ci		data->format = format;
15298c2ecf20Sopenharmony_ci		data->channels = channels;
15308c2ecf20Sopenharmony_ci		if (setsampleformat
15318c2ecf20Sopenharmony_ci		    (cif, data->mixer, data->id, channels, format)
15328c2ecf20Sopenharmony_ci		    || setsamplerate(cif, data->intdec, rate))
15338c2ecf20Sopenharmony_ci			err = -EIO;
15348c2ecf20Sopenharmony_ci	}
15358c2ecf20Sopenharmony_ci	spin_unlock_irq(&chip->lock);
15368c2ecf20Sopenharmony_ci	return err;
15378c2ecf20Sopenharmony_ci}
15388c2ecf20Sopenharmony_ci
15398c2ecf20Sopenharmony_cistatic int
15408c2ecf20Sopenharmony_cisnd_riptide_hw_params(struct snd_pcm_substream *substream,
15418c2ecf20Sopenharmony_ci		      struct snd_pcm_hw_params *hw_params)
15428c2ecf20Sopenharmony_ci{
15438c2ecf20Sopenharmony_ci	struct snd_riptide *chip = snd_pcm_substream_chip(substream);
15448c2ecf20Sopenharmony_ci	struct pcmhw *data = get_pcmhwdev(substream);
15458c2ecf20Sopenharmony_ci	struct snd_dma_buffer *sgdlist = &data->sgdlist;
15468c2ecf20Sopenharmony_ci	int err;
15478c2ecf20Sopenharmony_ci
15488c2ecf20Sopenharmony_ci	snd_printdd("hw params id %d (sgdlist: 0x%p 0x%lx %d)\n", data->id,
15498c2ecf20Sopenharmony_ci		    sgdlist->area, (unsigned long)sgdlist->addr,
15508c2ecf20Sopenharmony_ci		    (int)sgdlist->bytes);
15518c2ecf20Sopenharmony_ci	if (sgdlist->area)
15528c2ecf20Sopenharmony_ci		snd_dma_free_pages(sgdlist);
15538c2ecf20Sopenharmony_ci	if ((err = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV,
15548c2ecf20Sopenharmony_ci				       &chip->pci->dev,
15558c2ecf20Sopenharmony_ci				       sizeof(struct sgd) * (DESC_MAX_MASK + 1),
15568c2ecf20Sopenharmony_ci				       sgdlist)) < 0) {
15578c2ecf20Sopenharmony_ci		snd_printk(KERN_ERR "Riptide: failed to alloc %d dma bytes\n",
15588c2ecf20Sopenharmony_ci			   (int)sizeof(struct sgd) * (DESC_MAX_MASK + 1));
15598c2ecf20Sopenharmony_ci		return err;
15608c2ecf20Sopenharmony_ci	}
15618c2ecf20Sopenharmony_ci	data->sgdbuf = (struct sgd *)sgdlist->area;
15628c2ecf20Sopenharmony_ci	return 0;
15638c2ecf20Sopenharmony_ci}
15648c2ecf20Sopenharmony_ci
15658c2ecf20Sopenharmony_cistatic int snd_riptide_hw_free(struct snd_pcm_substream *substream)
15668c2ecf20Sopenharmony_ci{
15678c2ecf20Sopenharmony_ci	struct snd_riptide *chip = snd_pcm_substream_chip(substream);
15688c2ecf20Sopenharmony_ci	struct pcmhw *data = get_pcmhwdev(substream);
15698c2ecf20Sopenharmony_ci	struct cmdif *cif = chip->cif;
15708c2ecf20Sopenharmony_ci
15718c2ecf20Sopenharmony_ci	if (cif && data) {
15728c2ecf20Sopenharmony_ci		if (data->lbuspath)
15738c2ecf20Sopenharmony_ci			freelbuspath(cif, data->source, data->lbuspath);
15748c2ecf20Sopenharmony_ci		data->lbuspath = NULL;
15758c2ecf20Sopenharmony_ci		data->source = 0xff;
15768c2ecf20Sopenharmony_ci		data->intdec[0] = 0xff;
15778c2ecf20Sopenharmony_ci		data->intdec[1] = 0xff;
15788c2ecf20Sopenharmony_ci
15798c2ecf20Sopenharmony_ci		if (data->sgdlist.area) {
15808c2ecf20Sopenharmony_ci			snd_dma_free_pages(&data->sgdlist);
15818c2ecf20Sopenharmony_ci			data->sgdlist.area = NULL;
15828c2ecf20Sopenharmony_ci		}
15838c2ecf20Sopenharmony_ci	}
15848c2ecf20Sopenharmony_ci	return 0;
15858c2ecf20Sopenharmony_ci}
15868c2ecf20Sopenharmony_ci
15878c2ecf20Sopenharmony_cistatic int snd_riptide_playback_open(struct snd_pcm_substream *substream)
15888c2ecf20Sopenharmony_ci{
15898c2ecf20Sopenharmony_ci	struct snd_riptide *chip = snd_pcm_substream_chip(substream);
15908c2ecf20Sopenharmony_ci	struct snd_pcm_runtime *runtime = substream->runtime;
15918c2ecf20Sopenharmony_ci	struct pcmhw *data;
15928c2ecf20Sopenharmony_ci	int sub_num = substream->number;
15938c2ecf20Sopenharmony_ci
15948c2ecf20Sopenharmony_ci	chip->playback_substream[sub_num] = substream;
15958c2ecf20Sopenharmony_ci	runtime->hw = snd_riptide_playback;
15968c2ecf20Sopenharmony_ci
15978c2ecf20Sopenharmony_ci	data = kzalloc(sizeof(struct pcmhw), GFP_KERNEL);
15988c2ecf20Sopenharmony_ci	if (data == NULL)
15998c2ecf20Sopenharmony_ci		return -ENOMEM;
16008c2ecf20Sopenharmony_ci	data->paths = lbus_play_paths[sub_num];
16018c2ecf20Sopenharmony_ci	data->id = play_ids[sub_num];
16028c2ecf20Sopenharmony_ci	data->source = play_sources[sub_num];
16038c2ecf20Sopenharmony_ci	data->intdec[0] = 0xff;
16048c2ecf20Sopenharmony_ci	data->intdec[1] = 0xff;
16058c2ecf20Sopenharmony_ci	data->state = ST_STOP;
16068c2ecf20Sopenharmony_ci	runtime->private_data = data;
16078c2ecf20Sopenharmony_ci	return snd_pcm_hw_constraint_integer(runtime,
16088c2ecf20Sopenharmony_ci					     SNDRV_PCM_HW_PARAM_PERIODS);
16098c2ecf20Sopenharmony_ci}
16108c2ecf20Sopenharmony_ci
16118c2ecf20Sopenharmony_cistatic int snd_riptide_capture_open(struct snd_pcm_substream *substream)
16128c2ecf20Sopenharmony_ci{
16138c2ecf20Sopenharmony_ci	struct snd_riptide *chip = snd_pcm_substream_chip(substream);
16148c2ecf20Sopenharmony_ci	struct snd_pcm_runtime *runtime = substream->runtime;
16158c2ecf20Sopenharmony_ci	struct pcmhw *data;
16168c2ecf20Sopenharmony_ci
16178c2ecf20Sopenharmony_ci	chip->capture_substream = substream;
16188c2ecf20Sopenharmony_ci	runtime->hw = snd_riptide_capture;
16198c2ecf20Sopenharmony_ci
16208c2ecf20Sopenharmony_ci	data = kzalloc(sizeof(struct pcmhw), GFP_KERNEL);
16218c2ecf20Sopenharmony_ci	if (data == NULL)
16228c2ecf20Sopenharmony_ci		return -ENOMEM;
16238c2ecf20Sopenharmony_ci	data->paths = lbus_rec_path;
16248c2ecf20Sopenharmony_ci	data->id = PADC;
16258c2ecf20Sopenharmony_ci	data->source = ACLNK2PADC;
16268c2ecf20Sopenharmony_ci	data->intdec[0] = 0xff;
16278c2ecf20Sopenharmony_ci	data->intdec[1] = 0xff;
16288c2ecf20Sopenharmony_ci	data->state = ST_STOP;
16298c2ecf20Sopenharmony_ci	runtime->private_data = data;
16308c2ecf20Sopenharmony_ci	return snd_pcm_hw_constraint_integer(runtime,
16318c2ecf20Sopenharmony_ci					     SNDRV_PCM_HW_PARAM_PERIODS);
16328c2ecf20Sopenharmony_ci}
16338c2ecf20Sopenharmony_ci
16348c2ecf20Sopenharmony_cistatic int snd_riptide_playback_close(struct snd_pcm_substream *substream)
16358c2ecf20Sopenharmony_ci{
16368c2ecf20Sopenharmony_ci	struct snd_riptide *chip = snd_pcm_substream_chip(substream);
16378c2ecf20Sopenharmony_ci	struct pcmhw *data = get_pcmhwdev(substream);
16388c2ecf20Sopenharmony_ci	int sub_num = substream->number;
16398c2ecf20Sopenharmony_ci
16408c2ecf20Sopenharmony_ci	substream->runtime->private_data = NULL;
16418c2ecf20Sopenharmony_ci	chip->playback_substream[sub_num] = NULL;
16428c2ecf20Sopenharmony_ci	kfree(data);
16438c2ecf20Sopenharmony_ci	return 0;
16448c2ecf20Sopenharmony_ci}
16458c2ecf20Sopenharmony_ci
16468c2ecf20Sopenharmony_cistatic int snd_riptide_capture_close(struct snd_pcm_substream *substream)
16478c2ecf20Sopenharmony_ci{
16488c2ecf20Sopenharmony_ci	struct snd_riptide *chip = snd_pcm_substream_chip(substream);
16498c2ecf20Sopenharmony_ci	struct pcmhw *data = get_pcmhwdev(substream);
16508c2ecf20Sopenharmony_ci
16518c2ecf20Sopenharmony_ci	substream->runtime->private_data = NULL;
16528c2ecf20Sopenharmony_ci	chip->capture_substream = NULL;
16538c2ecf20Sopenharmony_ci	kfree(data);
16548c2ecf20Sopenharmony_ci	return 0;
16558c2ecf20Sopenharmony_ci}
16568c2ecf20Sopenharmony_ci
16578c2ecf20Sopenharmony_cistatic const struct snd_pcm_ops snd_riptide_playback_ops = {
16588c2ecf20Sopenharmony_ci	.open = snd_riptide_playback_open,
16598c2ecf20Sopenharmony_ci	.close = snd_riptide_playback_close,
16608c2ecf20Sopenharmony_ci	.hw_params = snd_riptide_hw_params,
16618c2ecf20Sopenharmony_ci	.hw_free = snd_riptide_hw_free,
16628c2ecf20Sopenharmony_ci	.prepare = snd_riptide_prepare,
16638c2ecf20Sopenharmony_ci	.trigger = snd_riptide_trigger,
16648c2ecf20Sopenharmony_ci	.pointer = snd_riptide_pointer,
16658c2ecf20Sopenharmony_ci};
16668c2ecf20Sopenharmony_cistatic const struct snd_pcm_ops snd_riptide_capture_ops = {
16678c2ecf20Sopenharmony_ci	.open = snd_riptide_capture_open,
16688c2ecf20Sopenharmony_ci	.close = snd_riptide_capture_close,
16698c2ecf20Sopenharmony_ci	.hw_params = snd_riptide_hw_params,
16708c2ecf20Sopenharmony_ci	.hw_free = snd_riptide_hw_free,
16718c2ecf20Sopenharmony_ci	.prepare = snd_riptide_prepare,
16728c2ecf20Sopenharmony_ci	.trigger = snd_riptide_trigger,
16738c2ecf20Sopenharmony_ci	.pointer = snd_riptide_pointer,
16748c2ecf20Sopenharmony_ci};
16758c2ecf20Sopenharmony_ci
16768c2ecf20Sopenharmony_cistatic int snd_riptide_pcm(struct snd_riptide *chip, int device)
16778c2ecf20Sopenharmony_ci{
16788c2ecf20Sopenharmony_ci	struct snd_pcm *pcm;
16798c2ecf20Sopenharmony_ci	int err;
16808c2ecf20Sopenharmony_ci
16818c2ecf20Sopenharmony_ci	if ((err =
16828c2ecf20Sopenharmony_ci	     snd_pcm_new(chip->card, "RIPTIDE", device, PLAYBACK_SUBSTREAMS, 1,
16838c2ecf20Sopenharmony_ci			 &pcm)) < 0)
16848c2ecf20Sopenharmony_ci		return err;
16858c2ecf20Sopenharmony_ci	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
16868c2ecf20Sopenharmony_ci			&snd_riptide_playback_ops);
16878c2ecf20Sopenharmony_ci	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
16888c2ecf20Sopenharmony_ci			&snd_riptide_capture_ops);
16898c2ecf20Sopenharmony_ci	pcm->private_data = chip;
16908c2ecf20Sopenharmony_ci	pcm->info_flags = 0;
16918c2ecf20Sopenharmony_ci	strcpy(pcm->name, "RIPTIDE");
16928c2ecf20Sopenharmony_ci	chip->pcm = pcm;
16938c2ecf20Sopenharmony_ci	snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV_SG,
16948c2ecf20Sopenharmony_ci				       &chip->pci->dev, 64 * 1024, 128 * 1024);
16958c2ecf20Sopenharmony_ci	return 0;
16968c2ecf20Sopenharmony_ci}
16978c2ecf20Sopenharmony_ci
16988c2ecf20Sopenharmony_cistatic irqreturn_t
16998c2ecf20Sopenharmony_cisnd_riptide_interrupt(int irq, void *dev_id)
17008c2ecf20Sopenharmony_ci{
17018c2ecf20Sopenharmony_ci	struct snd_riptide *chip = dev_id;
17028c2ecf20Sopenharmony_ci	struct cmdif *cif = chip->cif;
17038c2ecf20Sopenharmony_ci	irqreturn_t ret = IRQ_HANDLED;
17048c2ecf20Sopenharmony_ci
17058c2ecf20Sopenharmony_ci	if (cif) {
17068c2ecf20Sopenharmony_ci		chip->received_irqs++;
17078c2ecf20Sopenharmony_ci		if (IS_EOBIRQ(cif->hwport) || IS_EOSIRQ(cif->hwport) ||
17088c2ecf20Sopenharmony_ci		    IS_EOCIRQ(cif->hwport)) {
17098c2ecf20Sopenharmony_ci			chip->handled_irqs++;
17108c2ecf20Sopenharmony_ci			ret = IRQ_WAKE_THREAD;
17118c2ecf20Sopenharmony_ci		}
17128c2ecf20Sopenharmony_ci		if (chip->rmidi && IS_MPUIRQ(cif->hwport)) {
17138c2ecf20Sopenharmony_ci			chip->handled_irqs++;
17148c2ecf20Sopenharmony_ci			snd_mpu401_uart_interrupt(irq,
17158c2ecf20Sopenharmony_ci						  chip->rmidi->private_data);
17168c2ecf20Sopenharmony_ci		}
17178c2ecf20Sopenharmony_ci		SET_AIACK(cif->hwport);
17188c2ecf20Sopenharmony_ci	}
17198c2ecf20Sopenharmony_ci	return ret;
17208c2ecf20Sopenharmony_ci}
17218c2ecf20Sopenharmony_ci
17228c2ecf20Sopenharmony_cistatic void
17238c2ecf20Sopenharmony_cisnd_riptide_codec_write(struct snd_ac97 *ac97, unsigned short reg,
17248c2ecf20Sopenharmony_ci			unsigned short val)
17258c2ecf20Sopenharmony_ci{
17268c2ecf20Sopenharmony_ci	struct snd_riptide *chip = ac97->private_data;
17278c2ecf20Sopenharmony_ci	struct cmdif *cif = chip->cif;
17288c2ecf20Sopenharmony_ci	union cmdret rptr = CMDRET_ZERO;
17298c2ecf20Sopenharmony_ci	int i = 0;
17308c2ecf20Sopenharmony_ci
17318c2ecf20Sopenharmony_ci	if (snd_BUG_ON(!cif))
17328c2ecf20Sopenharmony_ci		return;
17338c2ecf20Sopenharmony_ci
17348c2ecf20Sopenharmony_ci	snd_printdd("Write AC97 reg 0x%x 0x%x\n", reg, val);
17358c2ecf20Sopenharmony_ci	do {
17368c2ecf20Sopenharmony_ci		SEND_SACR(cif, val, reg);
17378c2ecf20Sopenharmony_ci		SEND_RACR(cif, reg, &rptr);
17388c2ecf20Sopenharmony_ci	} while (rptr.retwords[1] != val && i++ < MAX_WRITE_RETRY);
17398c2ecf20Sopenharmony_ci	if (i > MAX_WRITE_RETRY)
17408c2ecf20Sopenharmony_ci		snd_printdd("Write AC97 reg failed\n");
17418c2ecf20Sopenharmony_ci}
17428c2ecf20Sopenharmony_ci
17438c2ecf20Sopenharmony_cistatic unsigned short snd_riptide_codec_read(struct snd_ac97 *ac97,
17448c2ecf20Sopenharmony_ci					     unsigned short reg)
17458c2ecf20Sopenharmony_ci{
17468c2ecf20Sopenharmony_ci	struct snd_riptide *chip = ac97->private_data;
17478c2ecf20Sopenharmony_ci	struct cmdif *cif = chip->cif;
17488c2ecf20Sopenharmony_ci	union cmdret rptr = CMDRET_ZERO;
17498c2ecf20Sopenharmony_ci
17508c2ecf20Sopenharmony_ci	if (snd_BUG_ON(!cif))
17518c2ecf20Sopenharmony_ci		return 0;
17528c2ecf20Sopenharmony_ci
17538c2ecf20Sopenharmony_ci	if (SEND_RACR(cif, reg, &rptr) != 0)
17548c2ecf20Sopenharmony_ci		SEND_RACR(cif, reg, &rptr);
17558c2ecf20Sopenharmony_ci	snd_printdd("Read AC97 reg 0x%x got 0x%x\n", reg, rptr.retwords[1]);
17568c2ecf20Sopenharmony_ci	return rptr.retwords[1];
17578c2ecf20Sopenharmony_ci}
17588c2ecf20Sopenharmony_ci
17598c2ecf20Sopenharmony_cistatic int snd_riptide_initialize(struct snd_riptide *chip)
17608c2ecf20Sopenharmony_ci{
17618c2ecf20Sopenharmony_ci	struct cmdif *cif;
17628c2ecf20Sopenharmony_ci	unsigned int device_id;
17638c2ecf20Sopenharmony_ci	int err;
17648c2ecf20Sopenharmony_ci
17658c2ecf20Sopenharmony_ci	if (snd_BUG_ON(!chip))
17668c2ecf20Sopenharmony_ci		return -EINVAL;
17678c2ecf20Sopenharmony_ci
17688c2ecf20Sopenharmony_ci	cif = chip->cif;
17698c2ecf20Sopenharmony_ci	if (!cif) {
17708c2ecf20Sopenharmony_ci		if ((cif = kzalloc(sizeof(struct cmdif), GFP_KERNEL)) == NULL)
17718c2ecf20Sopenharmony_ci			return -ENOMEM;
17728c2ecf20Sopenharmony_ci		cif->hwport = (struct riptideport *)chip->port;
17738c2ecf20Sopenharmony_ci		spin_lock_init(&cif->lock);
17748c2ecf20Sopenharmony_ci		chip->cif = cif;
17758c2ecf20Sopenharmony_ci	}
17768c2ecf20Sopenharmony_ci	cif->is_reset = 0;
17778c2ecf20Sopenharmony_ci	if ((err = riptide_reset(cif, chip)) != 0)
17788c2ecf20Sopenharmony_ci		return err;
17798c2ecf20Sopenharmony_ci	device_id = chip->device_id;
17808c2ecf20Sopenharmony_ci	switch (device_id) {
17818c2ecf20Sopenharmony_ci	case 0x4310:
17828c2ecf20Sopenharmony_ci	case 0x4320:
17838c2ecf20Sopenharmony_ci	case 0x4330:
17848c2ecf20Sopenharmony_ci		snd_printdd("Modem enable?\n");
17858c2ecf20Sopenharmony_ci		SEND_SETDPLL(cif);
17868c2ecf20Sopenharmony_ci		break;
17878c2ecf20Sopenharmony_ci	}
17888c2ecf20Sopenharmony_ci	snd_printdd("Enabling MPU IRQs\n");
17898c2ecf20Sopenharmony_ci	if (chip->rmidi)
17908c2ecf20Sopenharmony_ci		SET_EMPUIRQ(cif->hwport);
17918c2ecf20Sopenharmony_ci	return err;
17928c2ecf20Sopenharmony_ci}
17938c2ecf20Sopenharmony_ci
17948c2ecf20Sopenharmony_cistatic int snd_riptide_free(struct snd_riptide *chip)
17958c2ecf20Sopenharmony_ci{
17968c2ecf20Sopenharmony_ci	struct cmdif *cif;
17978c2ecf20Sopenharmony_ci
17988c2ecf20Sopenharmony_ci	if (!chip)
17998c2ecf20Sopenharmony_ci		return 0;
18008c2ecf20Sopenharmony_ci
18018c2ecf20Sopenharmony_ci	if ((cif = chip->cif)) {
18028c2ecf20Sopenharmony_ci		SET_GRESET(cif->hwport);
18038c2ecf20Sopenharmony_ci		udelay(100);
18048c2ecf20Sopenharmony_ci		UNSET_GRESET(cif->hwport);
18058c2ecf20Sopenharmony_ci		kfree(chip->cif);
18068c2ecf20Sopenharmony_ci	}
18078c2ecf20Sopenharmony_ci	if (chip->irq >= 0)
18088c2ecf20Sopenharmony_ci		free_irq(chip->irq, chip);
18098c2ecf20Sopenharmony_ci	release_firmware(chip->fw_entry);
18108c2ecf20Sopenharmony_ci	release_and_free_resource(chip->res_port);
18118c2ecf20Sopenharmony_ci	kfree(chip);
18128c2ecf20Sopenharmony_ci	return 0;
18138c2ecf20Sopenharmony_ci}
18148c2ecf20Sopenharmony_ci
18158c2ecf20Sopenharmony_cistatic int snd_riptide_dev_free(struct snd_device *device)
18168c2ecf20Sopenharmony_ci{
18178c2ecf20Sopenharmony_ci	struct snd_riptide *chip = device->device_data;
18188c2ecf20Sopenharmony_ci
18198c2ecf20Sopenharmony_ci	return snd_riptide_free(chip);
18208c2ecf20Sopenharmony_ci}
18218c2ecf20Sopenharmony_ci
18228c2ecf20Sopenharmony_cistatic int
18238c2ecf20Sopenharmony_cisnd_riptide_create(struct snd_card *card, struct pci_dev *pci,
18248c2ecf20Sopenharmony_ci		   struct snd_riptide **rchip)
18258c2ecf20Sopenharmony_ci{
18268c2ecf20Sopenharmony_ci	struct snd_riptide *chip;
18278c2ecf20Sopenharmony_ci	struct riptideport *hwport;
18288c2ecf20Sopenharmony_ci	int err;
18298c2ecf20Sopenharmony_ci	static const struct snd_device_ops ops = {
18308c2ecf20Sopenharmony_ci		.dev_free = snd_riptide_dev_free,
18318c2ecf20Sopenharmony_ci	};
18328c2ecf20Sopenharmony_ci
18338c2ecf20Sopenharmony_ci	*rchip = NULL;
18348c2ecf20Sopenharmony_ci	if ((err = pci_enable_device(pci)) < 0)
18358c2ecf20Sopenharmony_ci		return err;
18368c2ecf20Sopenharmony_ci	if (!(chip = kzalloc(sizeof(struct snd_riptide), GFP_KERNEL)))
18378c2ecf20Sopenharmony_ci		return -ENOMEM;
18388c2ecf20Sopenharmony_ci
18398c2ecf20Sopenharmony_ci	spin_lock_init(&chip->lock);
18408c2ecf20Sopenharmony_ci	chip->card = card;
18418c2ecf20Sopenharmony_ci	chip->pci = pci;
18428c2ecf20Sopenharmony_ci	chip->irq = -1;
18438c2ecf20Sopenharmony_ci	chip->openstreams = 0;
18448c2ecf20Sopenharmony_ci	chip->port = pci_resource_start(pci, 0);
18458c2ecf20Sopenharmony_ci	chip->received_irqs = 0;
18468c2ecf20Sopenharmony_ci	chip->handled_irqs = 0;
18478c2ecf20Sopenharmony_ci	chip->cif = NULL;
18488c2ecf20Sopenharmony_ci
18498c2ecf20Sopenharmony_ci	if ((chip->res_port =
18508c2ecf20Sopenharmony_ci	     request_region(chip->port, 64, "RIPTIDE")) == NULL) {
18518c2ecf20Sopenharmony_ci		snd_printk(KERN_ERR
18528c2ecf20Sopenharmony_ci			   "Riptide: unable to grab region 0x%lx-0x%lx\n",
18538c2ecf20Sopenharmony_ci			   chip->port, chip->port + 64 - 1);
18548c2ecf20Sopenharmony_ci		snd_riptide_free(chip);
18558c2ecf20Sopenharmony_ci		return -EBUSY;
18568c2ecf20Sopenharmony_ci	}
18578c2ecf20Sopenharmony_ci	hwport = (struct riptideport *)chip->port;
18588c2ecf20Sopenharmony_ci	UNSET_AIE(hwport);
18598c2ecf20Sopenharmony_ci
18608c2ecf20Sopenharmony_ci	if (request_threaded_irq(pci->irq, snd_riptide_interrupt,
18618c2ecf20Sopenharmony_ci				 riptide_handleirq, IRQF_SHARED,
18628c2ecf20Sopenharmony_ci				 KBUILD_MODNAME, chip)) {
18638c2ecf20Sopenharmony_ci		snd_printk(KERN_ERR "Riptide: unable to grab IRQ %d\n",
18648c2ecf20Sopenharmony_ci			   pci->irq);
18658c2ecf20Sopenharmony_ci		snd_riptide_free(chip);
18668c2ecf20Sopenharmony_ci		return -EBUSY;
18678c2ecf20Sopenharmony_ci	}
18688c2ecf20Sopenharmony_ci	chip->irq = pci->irq;
18698c2ecf20Sopenharmony_ci	card->sync_irq = chip->irq;
18708c2ecf20Sopenharmony_ci	chip->device_id = pci->device;
18718c2ecf20Sopenharmony_ci	pci_set_master(pci);
18728c2ecf20Sopenharmony_ci	if ((err = snd_riptide_initialize(chip)) < 0) {
18738c2ecf20Sopenharmony_ci		snd_riptide_free(chip);
18748c2ecf20Sopenharmony_ci		return err;
18758c2ecf20Sopenharmony_ci	}
18768c2ecf20Sopenharmony_ci
18778c2ecf20Sopenharmony_ci	if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
18788c2ecf20Sopenharmony_ci		snd_riptide_free(chip);
18798c2ecf20Sopenharmony_ci		return err;
18808c2ecf20Sopenharmony_ci	}
18818c2ecf20Sopenharmony_ci
18828c2ecf20Sopenharmony_ci	*rchip = chip;
18838c2ecf20Sopenharmony_ci	return 0;
18848c2ecf20Sopenharmony_ci}
18858c2ecf20Sopenharmony_ci
18868c2ecf20Sopenharmony_cistatic void
18878c2ecf20Sopenharmony_cisnd_riptide_proc_read(struct snd_info_entry *entry,
18888c2ecf20Sopenharmony_ci		      struct snd_info_buffer *buffer)
18898c2ecf20Sopenharmony_ci{
18908c2ecf20Sopenharmony_ci	struct snd_riptide *chip = entry->private_data;
18918c2ecf20Sopenharmony_ci	struct pcmhw *data;
18928c2ecf20Sopenharmony_ci	int i;
18938c2ecf20Sopenharmony_ci	struct cmdif *cif = NULL;
18948c2ecf20Sopenharmony_ci	unsigned char p[256];
18958c2ecf20Sopenharmony_ci	unsigned short rval = 0, lval = 0;
18968c2ecf20Sopenharmony_ci	unsigned int rate;
18978c2ecf20Sopenharmony_ci
18988c2ecf20Sopenharmony_ci	if (!chip)
18998c2ecf20Sopenharmony_ci		return;
19008c2ecf20Sopenharmony_ci
19018c2ecf20Sopenharmony_ci	snd_iprintf(buffer, "%s\n\n", chip->card->longname);
19028c2ecf20Sopenharmony_ci	snd_iprintf(buffer, "Device ID: 0x%x\nReceived IRQs: (%ld)%ld\nPorts:",
19038c2ecf20Sopenharmony_ci		    chip->device_id, chip->handled_irqs, chip->received_irqs);
19048c2ecf20Sopenharmony_ci	for (i = 0; i < 64; i += 4)
19058c2ecf20Sopenharmony_ci		snd_iprintf(buffer, "%c%02x: %08x",
19068c2ecf20Sopenharmony_ci			    (i % 16) ? ' ' : '\n', i, inl(chip->port + i));
19078c2ecf20Sopenharmony_ci	if ((cif = chip->cif)) {
19088c2ecf20Sopenharmony_ci		snd_iprintf(buffer,
19098c2ecf20Sopenharmony_ci			    "\nVersion: ASIC: %d CODEC: %d AUXDSP: %d PROG: %d",
19108c2ecf20Sopenharmony_ci			    chip->firmware.firmware.ASIC,
19118c2ecf20Sopenharmony_ci			    chip->firmware.firmware.CODEC,
19128c2ecf20Sopenharmony_ci			    chip->firmware.firmware.AUXDSP,
19138c2ecf20Sopenharmony_ci			    chip->firmware.firmware.PROG);
19148c2ecf20Sopenharmony_ci		snd_iprintf(buffer, "\nDigital mixer:");
19158c2ecf20Sopenharmony_ci		for (i = 0; i < 12; i++) {
19168c2ecf20Sopenharmony_ci			getmixer(cif, i, &rval, &lval);
19178c2ecf20Sopenharmony_ci			snd_iprintf(buffer, "\n %d: %d %d", i, rval, lval);
19188c2ecf20Sopenharmony_ci		}
19198c2ecf20Sopenharmony_ci		snd_iprintf(buffer,
19208c2ecf20Sopenharmony_ci			    "\nARM Commands num: %d failed: %d time: %d max: %d min: %d",
19218c2ecf20Sopenharmony_ci			    cif->cmdcnt, cif->errcnt,
19228c2ecf20Sopenharmony_ci			    cif->cmdtime, cif->cmdtimemax, cif->cmdtimemin);
19238c2ecf20Sopenharmony_ci	}
19248c2ecf20Sopenharmony_ci	snd_iprintf(buffer, "\nOpen streams %d:\n", chip->openstreams);
19258c2ecf20Sopenharmony_ci	for (i = 0; i < PLAYBACK_SUBSTREAMS; i++) {
19268c2ecf20Sopenharmony_ci		if (chip->playback_substream[i]
19278c2ecf20Sopenharmony_ci		    && chip->playback_substream[i]->runtime
19288c2ecf20Sopenharmony_ci		    && (data =
19298c2ecf20Sopenharmony_ci			chip->playback_substream[i]->runtime->private_data)) {
19308c2ecf20Sopenharmony_ci			snd_iprintf(buffer,
19318c2ecf20Sopenharmony_ci				    "stream: %d mixer: %d source: %d (%d,%d)\n",
19328c2ecf20Sopenharmony_ci				    data->id, data->mixer, data->source,
19338c2ecf20Sopenharmony_ci				    data->intdec[0], data->intdec[1]);
19348c2ecf20Sopenharmony_ci			if (!(getsamplerate(cif, data->intdec, &rate)))
19358c2ecf20Sopenharmony_ci				snd_iprintf(buffer, "rate: %d\n", rate);
19368c2ecf20Sopenharmony_ci		}
19378c2ecf20Sopenharmony_ci	}
19388c2ecf20Sopenharmony_ci	if (chip->capture_substream
19398c2ecf20Sopenharmony_ci	    && chip->capture_substream->runtime
19408c2ecf20Sopenharmony_ci	    && (data = chip->capture_substream->runtime->private_data)) {
19418c2ecf20Sopenharmony_ci		snd_iprintf(buffer,
19428c2ecf20Sopenharmony_ci			    "stream: %d mixer: %d source: %d (%d,%d)\n",
19438c2ecf20Sopenharmony_ci			    data->id, data->mixer,
19448c2ecf20Sopenharmony_ci			    data->source, data->intdec[0], data->intdec[1]);
19458c2ecf20Sopenharmony_ci		if (!(getsamplerate(cif, data->intdec, &rate)))
19468c2ecf20Sopenharmony_ci			snd_iprintf(buffer, "rate: %d\n", rate);
19478c2ecf20Sopenharmony_ci	}
19488c2ecf20Sopenharmony_ci	snd_iprintf(buffer, "Paths:\n");
19498c2ecf20Sopenharmony_ci	i = getpaths(cif, p);
19508c2ecf20Sopenharmony_ci	while (i >= 2) {
19518c2ecf20Sopenharmony_ci		i -= 2;
19528c2ecf20Sopenharmony_ci		snd_iprintf(buffer, "%x->%x ", p[i], p[i + 1]);
19538c2ecf20Sopenharmony_ci	}
19548c2ecf20Sopenharmony_ci	snd_iprintf(buffer, "\n");
19558c2ecf20Sopenharmony_ci}
19568c2ecf20Sopenharmony_ci
19578c2ecf20Sopenharmony_cistatic void snd_riptide_proc_init(struct snd_riptide *chip)
19588c2ecf20Sopenharmony_ci{
19598c2ecf20Sopenharmony_ci	snd_card_ro_proc_new(chip->card, "riptide", chip,
19608c2ecf20Sopenharmony_ci			     snd_riptide_proc_read);
19618c2ecf20Sopenharmony_ci}
19628c2ecf20Sopenharmony_ci
19638c2ecf20Sopenharmony_cistatic int snd_riptide_mixer(struct snd_riptide *chip)
19648c2ecf20Sopenharmony_ci{
19658c2ecf20Sopenharmony_ci	struct snd_ac97_bus *pbus;
19668c2ecf20Sopenharmony_ci	struct snd_ac97_template ac97;
19678c2ecf20Sopenharmony_ci	int err = 0;
19688c2ecf20Sopenharmony_ci	static const struct snd_ac97_bus_ops ops = {
19698c2ecf20Sopenharmony_ci		.write = snd_riptide_codec_write,
19708c2ecf20Sopenharmony_ci		.read = snd_riptide_codec_read,
19718c2ecf20Sopenharmony_ci	};
19728c2ecf20Sopenharmony_ci
19738c2ecf20Sopenharmony_ci	memset(&ac97, 0, sizeof(ac97));
19748c2ecf20Sopenharmony_ci	ac97.private_data = chip;
19758c2ecf20Sopenharmony_ci	ac97.scaps = AC97_SCAP_SKIP_MODEM;
19768c2ecf20Sopenharmony_ci
19778c2ecf20Sopenharmony_ci	if ((err = snd_ac97_bus(chip->card, 0, &ops, chip, &pbus)) < 0)
19788c2ecf20Sopenharmony_ci		return err;
19798c2ecf20Sopenharmony_ci
19808c2ecf20Sopenharmony_ci	chip->ac97_bus = pbus;
19818c2ecf20Sopenharmony_ci	ac97.pci = chip->pci;
19828c2ecf20Sopenharmony_ci	if ((err = snd_ac97_mixer(pbus, &ac97, &chip->ac97)) < 0)
19838c2ecf20Sopenharmony_ci		return err;
19848c2ecf20Sopenharmony_ci	return err;
19858c2ecf20Sopenharmony_ci}
19868c2ecf20Sopenharmony_ci
19878c2ecf20Sopenharmony_ci#ifdef SUPPORT_JOYSTICK
19888c2ecf20Sopenharmony_ci
19898c2ecf20Sopenharmony_cistatic int
19908c2ecf20Sopenharmony_cisnd_riptide_joystick_probe(struct pci_dev *pci, const struct pci_device_id *id)
19918c2ecf20Sopenharmony_ci{
19928c2ecf20Sopenharmony_ci	static int dev;
19938c2ecf20Sopenharmony_ci	struct gameport *gameport;
19948c2ecf20Sopenharmony_ci	int ret;
19958c2ecf20Sopenharmony_ci
19968c2ecf20Sopenharmony_ci	if (dev >= SNDRV_CARDS)
19978c2ecf20Sopenharmony_ci		return -ENODEV;
19988c2ecf20Sopenharmony_ci
19998c2ecf20Sopenharmony_ci	if (!enable[dev]) {
20008c2ecf20Sopenharmony_ci		ret = -ENOENT;
20018c2ecf20Sopenharmony_ci		goto inc_dev;
20028c2ecf20Sopenharmony_ci	}
20038c2ecf20Sopenharmony_ci
20048c2ecf20Sopenharmony_ci	if (!joystick_port[dev]) {
20058c2ecf20Sopenharmony_ci		ret = 0;
20068c2ecf20Sopenharmony_ci		goto inc_dev;
20078c2ecf20Sopenharmony_ci	}
20088c2ecf20Sopenharmony_ci
20098c2ecf20Sopenharmony_ci	gameport = gameport_allocate_port();
20108c2ecf20Sopenharmony_ci	if (!gameport) {
20118c2ecf20Sopenharmony_ci		ret = -ENOMEM;
20128c2ecf20Sopenharmony_ci		goto inc_dev;
20138c2ecf20Sopenharmony_ci	}
20148c2ecf20Sopenharmony_ci	if (!request_region(joystick_port[dev], 8, "Riptide gameport")) {
20158c2ecf20Sopenharmony_ci		snd_printk(KERN_WARNING
20168c2ecf20Sopenharmony_ci			   "Riptide: cannot grab gameport 0x%x\n",
20178c2ecf20Sopenharmony_ci			   joystick_port[dev]);
20188c2ecf20Sopenharmony_ci		gameport_free_port(gameport);
20198c2ecf20Sopenharmony_ci		ret = -EBUSY;
20208c2ecf20Sopenharmony_ci		goto inc_dev;
20218c2ecf20Sopenharmony_ci	}
20228c2ecf20Sopenharmony_ci
20238c2ecf20Sopenharmony_ci	gameport->io = joystick_port[dev];
20248c2ecf20Sopenharmony_ci	gameport_register_port(gameport);
20258c2ecf20Sopenharmony_ci	pci_set_drvdata(pci, gameport);
20268c2ecf20Sopenharmony_ci
20278c2ecf20Sopenharmony_ci	ret = 0;
20288c2ecf20Sopenharmony_ciinc_dev:
20298c2ecf20Sopenharmony_ci	dev++;
20308c2ecf20Sopenharmony_ci	return ret;
20318c2ecf20Sopenharmony_ci}
20328c2ecf20Sopenharmony_ci
20338c2ecf20Sopenharmony_cistatic void snd_riptide_joystick_remove(struct pci_dev *pci)
20348c2ecf20Sopenharmony_ci{
20358c2ecf20Sopenharmony_ci	struct gameport *gameport = pci_get_drvdata(pci);
20368c2ecf20Sopenharmony_ci	if (gameport) {
20378c2ecf20Sopenharmony_ci		release_region(gameport->io, 8);
20388c2ecf20Sopenharmony_ci		gameport_unregister_port(gameport);
20398c2ecf20Sopenharmony_ci	}
20408c2ecf20Sopenharmony_ci}
20418c2ecf20Sopenharmony_ci#endif
20428c2ecf20Sopenharmony_ci
20438c2ecf20Sopenharmony_cistatic int
20448c2ecf20Sopenharmony_cisnd_card_riptide_probe(struct pci_dev *pci, const struct pci_device_id *pci_id)
20458c2ecf20Sopenharmony_ci{
20468c2ecf20Sopenharmony_ci	static int dev;
20478c2ecf20Sopenharmony_ci	struct snd_card *card;
20488c2ecf20Sopenharmony_ci	struct snd_riptide *chip;
20498c2ecf20Sopenharmony_ci	unsigned short val;
20508c2ecf20Sopenharmony_ci	int err;
20518c2ecf20Sopenharmony_ci
20528c2ecf20Sopenharmony_ci	if (dev >= SNDRV_CARDS)
20538c2ecf20Sopenharmony_ci		return -ENODEV;
20548c2ecf20Sopenharmony_ci	if (!enable[dev]) {
20558c2ecf20Sopenharmony_ci		dev++;
20568c2ecf20Sopenharmony_ci		return -ENOENT;
20578c2ecf20Sopenharmony_ci	}
20588c2ecf20Sopenharmony_ci
20598c2ecf20Sopenharmony_ci	err = snd_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE,
20608c2ecf20Sopenharmony_ci			   0, &card);
20618c2ecf20Sopenharmony_ci	if (err < 0)
20628c2ecf20Sopenharmony_ci		return err;
20638c2ecf20Sopenharmony_ci	err = snd_riptide_create(card, pci, &chip);
20648c2ecf20Sopenharmony_ci	if (err < 0)
20658c2ecf20Sopenharmony_ci		goto error;
20668c2ecf20Sopenharmony_ci	card->private_data = chip;
20678c2ecf20Sopenharmony_ci	err = snd_riptide_pcm(chip, 0);
20688c2ecf20Sopenharmony_ci	if (err < 0)
20698c2ecf20Sopenharmony_ci		goto error;
20708c2ecf20Sopenharmony_ci	err = snd_riptide_mixer(chip);
20718c2ecf20Sopenharmony_ci	if (err < 0)
20728c2ecf20Sopenharmony_ci		goto error;
20738c2ecf20Sopenharmony_ci
20748c2ecf20Sopenharmony_ci	val = LEGACY_ENABLE_ALL;
20758c2ecf20Sopenharmony_ci	if (opl3_port[dev])
20768c2ecf20Sopenharmony_ci		val |= LEGACY_ENABLE_FM;
20778c2ecf20Sopenharmony_ci#ifdef SUPPORT_JOYSTICK
20788c2ecf20Sopenharmony_ci	if (joystick_port[dev])
20798c2ecf20Sopenharmony_ci		val |= LEGACY_ENABLE_GAMEPORT;
20808c2ecf20Sopenharmony_ci#endif
20818c2ecf20Sopenharmony_ci	if (mpu_port[dev])
20828c2ecf20Sopenharmony_ci		val |= LEGACY_ENABLE_MPU_INT | LEGACY_ENABLE_MPU;
20838c2ecf20Sopenharmony_ci	val |= (chip->irq << 4) & 0xf0;
20848c2ecf20Sopenharmony_ci	pci_write_config_word(chip->pci, PCI_EXT_Legacy_Mask, val);
20858c2ecf20Sopenharmony_ci	if (mpu_port[dev]) {
20868c2ecf20Sopenharmony_ci		val = mpu_port[dev];
20878c2ecf20Sopenharmony_ci		pci_write_config_word(chip->pci, PCI_EXT_MPU_Base, val);
20888c2ecf20Sopenharmony_ci		err = snd_mpu401_uart_new(card, 0, MPU401_HW_RIPTIDE,
20898c2ecf20Sopenharmony_ci					  val, MPU401_INFO_IRQ_HOOK, -1,
20908c2ecf20Sopenharmony_ci					  &chip->rmidi);
20918c2ecf20Sopenharmony_ci		if (err < 0)
20928c2ecf20Sopenharmony_ci			snd_printk(KERN_WARNING
20938c2ecf20Sopenharmony_ci				   "Riptide: Can't Allocate MPU at 0x%x\n",
20948c2ecf20Sopenharmony_ci				   val);
20958c2ecf20Sopenharmony_ci		else
20968c2ecf20Sopenharmony_ci			chip->mpuaddr = val;
20978c2ecf20Sopenharmony_ci	}
20988c2ecf20Sopenharmony_ci	if (opl3_port[dev]) {
20998c2ecf20Sopenharmony_ci		val = opl3_port[dev];
21008c2ecf20Sopenharmony_ci		pci_write_config_word(chip->pci, PCI_EXT_FM_Base, val);
21018c2ecf20Sopenharmony_ci		err = snd_opl3_create(card, val, val + 2,
21028c2ecf20Sopenharmony_ci				      OPL3_HW_RIPTIDE, 0, &chip->opl3);
21038c2ecf20Sopenharmony_ci		if (err < 0)
21048c2ecf20Sopenharmony_ci			snd_printk(KERN_WARNING
21058c2ecf20Sopenharmony_ci				   "Riptide: Can't Allocate OPL3 at 0x%x\n",
21068c2ecf20Sopenharmony_ci				   val);
21078c2ecf20Sopenharmony_ci		else {
21088c2ecf20Sopenharmony_ci			chip->opladdr = val;
21098c2ecf20Sopenharmony_ci			err = snd_opl3_hwdep_new(chip->opl3, 0, 1, NULL);
21108c2ecf20Sopenharmony_ci			if (err < 0)
21118c2ecf20Sopenharmony_ci				snd_printk(KERN_WARNING
21128c2ecf20Sopenharmony_ci					   "Riptide: Can't Allocate OPL3-HWDEP\n");
21138c2ecf20Sopenharmony_ci		}
21148c2ecf20Sopenharmony_ci	}
21158c2ecf20Sopenharmony_ci#ifdef SUPPORT_JOYSTICK
21168c2ecf20Sopenharmony_ci	if (joystick_port[dev]) {
21178c2ecf20Sopenharmony_ci		val = joystick_port[dev];
21188c2ecf20Sopenharmony_ci		pci_write_config_word(chip->pci, PCI_EXT_Game_Base, val);
21198c2ecf20Sopenharmony_ci		chip->gameaddr = val;
21208c2ecf20Sopenharmony_ci	}
21218c2ecf20Sopenharmony_ci#endif
21228c2ecf20Sopenharmony_ci
21238c2ecf20Sopenharmony_ci	strcpy(card->driver, "RIPTIDE");
21248c2ecf20Sopenharmony_ci	strcpy(card->shortname, "Riptide");
21258c2ecf20Sopenharmony_ci#ifdef SUPPORT_JOYSTICK
21268c2ecf20Sopenharmony_ci	snprintf(card->longname, sizeof(card->longname),
21278c2ecf20Sopenharmony_ci		 "%s at 0x%lx, irq %i mpu 0x%x opl3 0x%x gameport 0x%x",
21288c2ecf20Sopenharmony_ci		 card->shortname, chip->port, chip->irq, chip->mpuaddr,
21298c2ecf20Sopenharmony_ci		 chip->opladdr, chip->gameaddr);
21308c2ecf20Sopenharmony_ci#else
21318c2ecf20Sopenharmony_ci	snprintf(card->longname, sizeof(card->longname),
21328c2ecf20Sopenharmony_ci		 "%s at 0x%lx, irq %i mpu 0x%x opl3 0x%x",
21338c2ecf20Sopenharmony_ci		 card->shortname, chip->port, chip->irq, chip->mpuaddr,
21348c2ecf20Sopenharmony_ci		 chip->opladdr);
21358c2ecf20Sopenharmony_ci#endif
21368c2ecf20Sopenharmony_ci	snd_riptide_proc_init(chip);
21378c2ecf20Sopenharmony_ci	err = snd_card_register(card);
21388c2ecf20Sopenharmony_ci	if (err < 0)
21398c2ecf20Sopenharmony_ci		goto error;
21408c2ecf20Sopenharmony_ci	pci_set_drvdata(pci, card);
21418c2ecf20Sopenharmony_ci	dev++;
21428c2ecf20Sopenharmony_ci	return 0;
21438c2ecf20Sopenharmony_ci
21448c2ecf20Sopenharmony_ci error:
21458c2ecf20Sopenharmony_ci	snd_card_free(card);
21468c2ecf20Sopenharmony_ci	return err;
21478c2ecf20Sopenharmony_ci}
21488c2ecf20Sopenharmony_ci
21498c2ecf20Sopenharmony_cistatic void snd_card_riptide_remove(struct pci_dev *pci)
21508c2ecf20Sopenharmony_ci{
21518c2ecf20Sopenharmony_ci	snd_card_free(pci_get_drvdata(pci));
21528c2ecf20Sopenharmony_ci}
21538c2ecf20Sopenharmony_ci
21548c2ecf20Sopenharmony_cistatic struct pci_driver driver = {
21558c2ecf20Sopenharmony_ci	.name = KBUILD_MODNAME,
21568c2ecf20Sopenharmony_ci	.id_table = snd_riptide_ids,
21578c2ecf20Sopenharmony_ci	.probe = snd_card_riptide_probe,
21588c2ecf20Sopenharmony_ci	.remove = snd_card_riptide_remove,
21598c2ecf20Sopenharmony_ci	.driver = {
21608c2ecf20Sopenharmony_ci		.pm = RIPTIDE_PM_OPS,
21618c2ecf20Sopenharmony_ci	},
21628c2ecf20Sopenharmony_ci};
21638c2ecf20Sopenharmony_ci
21648c2ecf20Sopenharmony_ci#ifdef SUPPORT_JOYSTICK
21658c2ecf20Sopenharmony_cistatic struct pci_driver joystick_driver = {
21668c2ecf20Sopenharmony_ci	.name = KBUILD_MODNAME "-joystick",
21678c2ecf20Sopenharmony_ci	.id_table = snd_riptide_joystick_ids,
21688c2ecf20Sopenharmony_ci	.probe = snd_riptide_joystick_probe,
21698c2ecf20Sopenharmony_ci	.remove = snd_riptide_joystick_remove,
21708c2ecf20Sopenharmony_ci};
21718c2ecf20Sopenharmony_ci#endif
21728c2ecf20Sopenharmony_ci
21738c2ecf20Sopenharmony_cistatic int __init alsa_card_riptide_init(void)
21748c2ecf20Sopenharmony_ci{
21758c2ecf20Sopenharmony_ci	int err;
21768c2ecf20Sopenharmony_ci	err = pci_register_driver(&driver);
21778c2ecf20Sopenharmony_ci	if (err < 0)
21788c2ecf20Sopenharmony_ci		return err;
21798c2ecf20Sopenharmony_ci#if defined(SUPPORT_JOYSTICK)
21808c2ecf20Sopenharmony_ci	err = pci_register_driver(&joystick_driver);
21818c2ecf20Sopenharmony_ci	/* On failure unregister formerly registered audio driver */
21828c2ecf20Sopenharmony_ci	if (err < 0)
21838c2ecf20Sopenharmony_ci		pci_unregister_driver(&driver);
21848c2ecf20Sopenharmony_ci#endif
21858c2ecf20Sopenharmony_ci	return err;
21868c2ecf20Sopenharmony_ci}
21878c2ecf20Sopenharmony_ci
21888c2ecf20Sopenharmony_cistatic void __exit alsa_card_riptide_exit(void)
21898c2ecf20Sopenharmony_ci{
21908c2ecf20Sopenharmony_ci	pci_unregister_driver(&driver);
21918c2ecf20Sopenharmony_ci#if defined(SUPPORT_JOYSTICK)
21928c2ecf20Sopenharmony_ci	pci_unregister_driver(&joystick_driver);
21938c2ecf20Sopenharmony_ci#endif
21948c2ecf20Sopenharmony_ci}
21958c2ecf20Sopenharmony_ci
21968c2ecf20Sopenharmony_cimodule_init(alsa_card_riptide_init);
21978c2ecf20Sopenharmony_cimodule_exit(alsa_card_riptide_exit);
2198