ali5451.c 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283
  1. /*
  2. * Matt Wu <Matt_Wu@acersoftech.com.cn>
  3. * Apr 26, 2001
  4. * Routines for control of ALi pci audio M5451
  5. *
  6. * BUGS:
  7. * --
  8. *
  9. * TODO:
  10. * --
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public Lcodecnse as published by
  14. * the Free Software Foundation; either version 2 of the Lcodecnse, or
  15. * (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public Lcodecnse for more details.
  21. *
  22. * You should have received a copy of the GNU General Public Lcodecnse
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  25. *
  26. */
  27. #include <linux/io.h>
  28. #include <linux/delay.h>
  29. #include <linux/interrupt.h>
  30. #include <linux/init.h>
  31. #include <linux/pci.h>
  32. #include <linux/slab.h>
  33. #include <linux/module.h>
  34. #include <linux/dma-mapping.h>
  35. #include <sound/core.h>
  36. #include <sound/pcm.h>
  37. #include <sound/info.h>
  38. #include <sound/ac97_codec.h>
  39. #include <sound/mpu401.h>
  40. #include <sound/initval.h>
  41. MODULE_AUTHOR("Matt Wu <Matt_Wu@acersoftech.com.cn>");
  42. MODULE_DESCRIPTION("ALI M5451");
  43. MODULE_LICENSE("GPL");
  44. MODULE_SUPPORTED_DEVICE("{{ALI,M5451,pci},{ALI,M5451}}");
  45. static int index = SNDRV_DEFAULT_IDX1; /* Index */
  46. static char *id = SNDRV_DEFAULT_STR1; /* ID for this card */
  47. static int pcm_channels = 32;
  48. static bool spdif;
  49. module_param(index, int, 0444);
  50. MODULE_PARM_DESC(index, "Index value for ALI M5451 PCI Audio.");
  51. module_param(id, charp, 0444);
  52. MODULE_PARM_DESC(id, "ID string for ALI M5451 PCI Audio.");
  53. module_param(pcm_channels, int, 0444);
  54. MODULE_PARM_DESC(pcm_channels, "PCM Channels");
  55. module_param(spdif, bool, 0444);
  56. MODULE_PARM_DESC(spdif, "Support SPDIF I/O");
  57. /* just for backward compatibility */
  58. static bool enable;
  59. module_param(enable, bool, 0444);
  60. /*
  61. * Constants definition
  62. */
  63. #define DEVICE_ID_ALI5451 ((PCI_VENDOR_ID_AL<<16)|PCI_DEVICE_ID_AL_M5451)
  64. #define ALI_CHANNELS 32
  65. #define ALI_PCM_IN_CHANNEL 31
  66. #define ALI_SPDIF_IN_CHANNEL 19
  67. #define ALI_SPDIF_OUT_CHANNEL 15
  68. #define ALI_CENTER_CHANNEL 24
  69. #define ALI_LEF_CHANNEL 23
  70. #define ALI_SURR_LEFT_CHANNEL 26
  71. #define ALI_SURR_RIGHT_CHANNEL 25
  72. #define ALI_MODEM_IN_CHANNEL 21
  73. #define ALI_MODEM_OUT_CHANNEL 20
  74. #define SNDRV_ALI_VOICE_TYPE_PCM 01
  75. #define SNDRV_ALI_VOICE_TYPE_OTH 02
  76. #define ALI_5451_V02 0x02
  77. /*
  78. * Direct Registers
  79. */
  80. #define ALI_LEGACY_DMAR0 0x00 /* ADR0 */
  81. #define ALI_LEGACY_DMAR4 0x04 /* CNT0 */
  82. #define ALI_LEGACY_DMAR11 0x0b /* MOD */
  83. #define ALI_LEGACY_DMAR15 0x0f /* MMR */
  84. #define ALI_MPUR0 0x20
  85. #define ALI_MPUR1 0x21
  86. #define ALI_MPUR2 0x22
  87. #define ALI_MPUR3 0x23
  88. #define ALI_AC97_WRITE 0x40
  89. #define ALI_AC97_READ 0x44
  90. #define ALI_SCTRL 0x48
  91. #define ALI_SPDIF_OUT_ENABLE 0x20
  92. #define ALI_SCTRL_LINE_IN2 (1 << 9)
  93. #define ALI_SCTRL_GPIO_IN2 (1 << 13)
  94. #define ALI_SCTRL_LINE_OUT_EN (1 << 20)
  95. #define ALI_SCTRL_GPIO_OUT_EN (1 << 23)
  96. #define ALI_SCTRL_CODEC1_READY (1 << 24)
  97. #define ALI_SCTRL_CODEC2_READY (1 << 25)
  98. #define ALI_AC97_GPIO 0x4c
  99. #define ALI_AC97_GPIO_ENABLE 0x8000
  100. #define ALI_AC97_GPIO_DATA_SHIFT 16
  101. #define ALI_SPDIF_CS 0x70
  102. #define ALI_SPDIF_CTRL 0x74
  103. #define ALI_SPDIF_IN_FUNC_ENABLE 0x02
  104. #define ALI_SPDIF_IN_CH_STATUS 0x40
  105. #define ALI_SPDIF_OUT_CH_STATUS 0xbf
  106. #define ALI_START 0x80
  107. #define ALI_STOP 0x84
  108. #define ALI_CSPF 0x90
  109. #define ALI_AINT 0x98
  110. #define ALI_GC_CIR 0xa0
  111. #define ENDLP_IE 0x00001000
  112. #define MIDLP_IE 0x00002000
  113. #define ALI_AINTEN 0xa4
  114. #define ALI_VOLUME 0xa8
  115. #define ALI_SBDELTA_DELTA_R 0xac
  116. #define ALI_MISCINT 0xb0
  117. #define ADDRESS_IRQ 0x00000020
  118. #define TARGET_REACHED 0x00008000
  119. #define MIXER_OVERFLOW 0x00000800
  120. #define MIXER_UNDERFLOW 0x00000400
  121. #define GPIO_IRQ 0x01000000
  122. #define ALI_SBBL_SBCL 0xc0
  123. #define ALI_SBCTRL_SBE2R_SBDD 0xc4
  124. #define ALI_STIMER 0xc8
  125. #define ALI_GLOBAL_CONTROL 0xd4
  126. #define ALI_SPDIF_OUT_SEL_PCM 0x00000400 /* bit 10 */
  127. #define ALI_SPDIF_IN_SUPPORT 0x00000800 /* bit 11 */
  128. #define ALI_SPDIF_OUT_CH_ENABLE 0x00008000 /* bit 15 */
  129. #define ALI_SPDIF_IN_CH_ENABLE 0x00080000 /* bit 19 */
  130. #define ALI_PCM_IN_ENABLE 0x80000000 /* bit 31 */
  131. #define ALI_CSO_ALPHA_FMS 0xe0
  132. #define ALI_LBA 0xe4
  133. #define ALI_ESO_DELTA 0xe8
  134. #define ALI_GVSEL_PAN_VOC_CTRL_EC 0xf0
  135. #define ALI_EBUF1 0xf4
  136. #define ALI_EBUF2 0xf8
  137. #define ALI_REG(codec, x) ((codec)->port + x)
  138. #define MAX_CODECS 2
  139. struct snd_ali;
  140. struct snd_ali_voice;
  141. struct snd_ali_channel_control {
  142. /* register data */
  143. struct REGDATA {
  144. unsigned int start;
  145. unsigned int stop;
  146. unsigned int aint;
  147. unsigned int ainten;
  148. } data;
  149. /* register addresses */
  150. struct REGS {
  151. unsigned int start;
  152. unsigned int stop;
  153. unsigned int aint;
  154. unsigned int ainten;
  155. unsigned int ac97read;
  156. unsigned int ac97write;
  157. } regs;
  158. };
  159. struct snd_ali_voice {
  160. unsigned int number;
  161. unsigned int use :1,
  162. pcm :1,
  163. midi :1,
  164. mode :1,
  165. synth :1,
  166. running :1;
  167. /* PCM data */
  168. struct snd_ali *codec;
  169. struct snd_pcm_substream *substream;
  170. struct snd_ali_voice *extra;
  171. int eso; /* final ESO value for channel */
  172. int count; /* runtime->period_size */
  173. /* --- */
  174. void *private_data;
  175. void (*private_free)(void *private_data);
  176. };
  177. struct snd_alidev {
  178. struct snd_ali_voice voices[ALI_CHANNELS];
  179. unsigned int chcnt; /* num of opened channels */
  180. unsigned int chmap; /* bitmap for opened channels */
  181. unsigned int synthcount;
  182. };
  183. #define ALI_GLOBAL_REGS 56
  184. #define ALI_CHANNEL_REGS 8
  185. struct snd_ali_image {
  186. u32 regs[ALI_GLOBAL_REGS];
  187. u32 channel_regs[ALI_CHANNELS][ALI_CHANNEL_REGS];
  188. };
  189. struct snd_ali {
  190. int irq;
  191. unsigned long port;
  192. unsigned char revision;
  193. unsigned int hw_initialized :1;
  194. unsigned int spdif_support :1;
  195. struct pci_dev *pci;
  196. struct pci_dev *pci_m1533;
  197. struct pci_dev *pci_m7101;
  198. struct snd_card *card;
  199. struct snd_pcm *pcm[MAX_CODECS];
  200. struct snd_alidev synth;
  201. struct snd_ali_channel_control chregs;
  202. /* S/PDIF Mask */
  203. unsigned int spdif_mask;
  204. unsigned int spurious_irq_count;
  205. unsigned int spurious_irq_max_delta;
  206. unsigned int num_of_codecs;
  207. struct snd_ac97_bus *ac97_bus;
  208. struct snd_ac97 *ac97[MAX_CODECS];
  209. unsigned short ac97_ext_id;
  210. unsigned short ac97_ext_status;
  211. spinlock_t reg_lock;
  212. spinlock_t voice_alloc;
  213. #ifdef CONFIG_PM_SLEEP
  214. struct snd_ali_image *image;
  215. #endif
  216. };
  217. static const struct pci_device_id snd_ali_ids[] = {
  218. {PCI_DEVICE(PCI_VENDOR_ID_AL, PCI_DEVICE_ID_AL_M5451), 0, 0, 0},
  219. {0, }
  220. };
  221. MODULE_DEVICE_TABLE(pci, snd_ali_ids);
  222. static void snd_ali_clear_voices(struct snd_ali *, unsigned int, unsigned int);
  223. static unsigned short snd_ali_codec_peek(struct snd_ali *, int, unsigned short);
  224. static void snd_ali_codec_poke(struct snd_ali *, int, unsigned short,
  225. unsigned short);
  226. /*
  227. * AC97 ACCESS
  228. */
  229. static inline unsigned int snd_ali_5451_peek(struct snd_ali *codec,
  230. unsigned int port)
  231. {
  232. return (unsigned int)inl(ALI_REG(codec, port));
  233. }
  234. static inline void snd_ali_5451_poke(struct snd_ali *codec,
  235. unsigned int port,
  236. unsigned int val)
  237. {
  238. outl((unsigned int)val, ALI_REG(codec, port));
  239. }
  240. static int snd_ali_codec_ready(struct snd_ali *codec,
  241. unsigned int port)
  242. {
  243. unsigned long end_time;
  244. unsigned int res;
  245. end_time = jiffies + msecs_to_jiffies(250);
  246. for (;;) {
  247. res = snd_ali_5451_peek(codec,port);
  248. if (!(res & 0x8000))
  249. return 0;
  250. if (!time_after_eq(end_time, jiffies))
  251. break;
  252. schedule_timeout_uninterruptible(1);
  253. }
  254. snd_ali_5451_poke(codec, port, res & ~0x8000);
  255. dev_dbg(codec->card->dev, "ali_codec_ready: codec is not ready.\n ");
  256. return -EIO;
  257. }
  258. static int snd_ali_stimer_ready(struct snd_ali *codec)
  259. {
  260. unsigned long end_time;
  261. unsigned long dwChk1,dwChk2;
  262. dwChk1 = snd_ali_5451_peek(codec, ALI_STIMER);
  263. end_time = jiffies + msecs_to_jiffies(250);
  264. for (;;) {
  265. dwChk2 = snd_ali_5451_peek(codec, ALI_STIMER);
  266. if (dwChk2 != dwChk1)
  267. return 0;
  268. if (!time_after_eq(end_time, jiffies))
  269. break;
  270. schedule_timeout_uninterruptible(1);
  271. }
  272. dev_err(codec->card->dev, "ali_stimer_read: stimer is not ready.\n");
  273. return -EIO;
  274. }
  275. static void snd_ali_codec_poke(struct snd_ali *codec,int secondary,
  276. unsigned short reg,
  277. unsigned short val)
  278. {
  279. unsigned int dwVal;
  280. unsigned int port;
  281. if (reg >= 0x80) {
  282. dev_err(codec->card->dev,
  283. "ali_codec_poke: reg(%xh) invalid.\n", reg);
  284. return;
  285. }
  286. port = codec->chregs.regs.ac97write;
  287. if (snd_ali_codec_ready(codec, port) < 0)
  288. return;
  289. if (snd_ali_stimer_ready(codec) < 0)
  290. return;
  291. dwVal = (unsigned int) (reg & 0xff);
  292. dwVal |= 0x8000 | (val << 16);
  293. if (secondary)
  294. dwVal |= 0x0080;
  295. if (codec->revision == ALI_5451_V02)
  296. dwVal |= 0x0100;
  297. snd_ali_5451_poke(codec, port, dwVal);
  298. return ;
  299. }
  300. static unsigned short snd_ali_codec_peek(struct snd_ali *codec,
  301. int secondary,
  302. unsigned short reg)
  303. {
  304. unsigned int dwVal;
  305. unsigned int port;
  306. if (reg >= 0x80) {
  307. dev_err(codec->card->dev,
  308. "ali_codec_peek: reg(%xh) invalid.\n", reg);
  309. return ~0;
  310. }
  311. port = codec->chregs.regs.ac97read;
  312. if (snd_ali_codec_ready(codec, port) < 0)
  313. return ~0;
  314. if (snd_ali_stimer_ready(codec) < 0)
  315. return ~0;
  316. dwVal = (unsigned int) (reg & 0xff);
  317. dwVal |= 0x8000; /* bit 15*/
  318. if (secondary)
  319. dwVal |= 0x0080;
  320. snd_ali_5451_poke(codec, port, dwVal);
  321. if (snd_ali_stimer_ready(codec) < 0)
  322. return ~0;
  323. if (snd_ali_codec_ready(codec, port) < 0)
  324. return ~0;
  325. return (snd_ali_5451_peek(codec, port) & 0xffff0000) >> 16;
  326. }
  327. static void snd_ali_codec_write(struct snd_ac97 *ac97,
  328. unsigned short reg,
  329. unsigned short val )
  330. {
  331. struct snd_ali *codec = ac97->private_data;
  332. dev_dbg(codec->card->dev, "codec_write: reg=%xh data=%xh.\n", reg, val);
  333. if (reg == AC97_GPIO_STATUS) {
  334. outl((val << ALI_AC97_GPIO_DATA_SHIFT) | ALI_AC97_GPIO_ENABLE,
  335. ALI_REG(codec, ALI_AC97_GPIO));
  336. return;
  337. }
  338. snd_ali_codec_poke(codec, ac97->num, reg, val);
  339. return ;
  340. }
  341. static unsigned short snd_ali_codec_read(struct snd_ac97 *ac97,
  342. unsigned short reg)
  343. {
  344. struct snd_ali *codec = ac97->private_data;
  345. dev_dbg(codec->card->dev, "codec_read reg=%xh.\n", reg);
  346. return snd_ali_codec_peek(codec, ac97->num, reg);
  347. }
  348. /*
  349. * AC97 Reset
  350. */
  351. static int snd_ali_reset_5451(struct snd_ali *codec)
  352. {
  353. struct pci_dev *pci_dev;
  354. unsigned short wCount, wReg;
  355. unsigned int dwVal;
  356. pci_dev = codec->pci_m1533;
  357. if (pci_dev) {
  358. pci_read_config_dword(pci_dev, 0x7c, &dwVal);
  359. pci_write_config_dword(pci_dev, 0x7c, dwVal | 0x08000000);
  360. mdelay(5);
  361. pci_read_config_dword(pci_dev, 0x7c, &dwVal);
  362. pci_write_config_dword(pci_dev, 0x7c, dwVal & 0xf7ffffff);
  363. mdelay(5);
  364. }
  365. pci_dev = codec->pci;
  366. pci_read_config_dword(pci_dev, 0x44, &dwVal);
  367. pci_write_config_dword(pci_dev, 0x44, dwVal | 0x000c0000);
  368. udelay(500);
  369. pci_read_config_dword(pci_dev, 0x44, &dwVal);
  370. pci_write_config_dword(pci_dev, 0x44, dwVal & 0xfffbffff);
  371. mdelay(5);
  372. wCount = 200;
  373. while(wCount--) {
  374. wReg = snd_ali_codec_peek(codec, 0, AC97_POWERDOWN);
  375. if ((wReg & 0x000f) == 0x000f)
  376. return 0;
  377. mdelay(5);
  378. }
  379. /* non-fatal if you have a non PM capable codec */
  380. /* dev_warn(codec->card->dev, "ali5451: reset time out\n"); */
  381. return 0;
  382. }
  383. /*
  384. * ALI 5451 Controller
  385. */
  386. static void snd_ali_enable_special_channel(struct snd_ali *codec,
  387. unsigned int channel)
  388. {
  389. unsigned long dwVal;
  390. dwVal = inl(ALI_REG(codec, ALI_GLOBAL_CONTROL));
  391. dwVal |= 1 << (channel & 0x0000001f);
  392. outl(dwVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
  393. }
  394. static void snd_ali_disable_special_channel(struct snd_ali *codec,
  395. unsigned int channel)
  396. {
  397. unsigned long dwVal;
  398. dwVal = inl(ALI_REG(codec, ALI_GLOBAL_CONTROL));
  399. dwVal &= ~(1 << (channel & 0x0000001f));
  400. outl(dwVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
  401. }
  402. static void snd_ali_enable_address_interrupt(struct snd_ali *codec)
  403. {
  404. unsigned int gc;
  405. gc = inl(ALI_REG(codec, ALI_GC_CIR));
  406. gc |= ENDLP_IE;
  407. gc |= MIDLP_IE;
  408. outl( gc, ALI_REG(codec, ALI_GC_CIR));
  409. }
  410. static void snd_ali_disable_address_interrupt(struct snd_ali *codec)
  411. {
  412. unsigned int gc;
  413. gc = inl(ALI_REG(codec, ALI_GC_CIR));
  414. gc &= ~ENDLP_IE;
  415. gc &= ~MIDLP_IE;
  416. outl(gc, ALI_REG(codec, ALI_GC_CIR));
  417. }
  418. static void snd_ali_disable_voice_irq(struct snd_ali *codec,
  419. unsigned int channel)
  420. {
  421. unsigned int mask;
  422. struct snd_ali_channel_control *pchregs = &(codec->chregs);
  423. dev_dbg(codec->card->dev, "disable_voice_irq channel=%d\n", channel);
  424. mask = 1 << (channel & 0x1f);
  425. pchregs->data.ainten = inl(ALI_REG(codec, pchregs->regs.ainten));
  426. pchregs->data.ainten &= ~mask;
  427. outl(pchregs->data.ainten, ALI_REG(codec, pchregs->regs.ainten));
  428. }
  429. static int snd_ali_alloc_pcm_channel(struct snd_ali *codec, int channel)
  430. {
  431. unsigned int idx = channel & 0x1f;
  432. if (codec->synth.chcnt >= ALI_CHANNELS){
  433. dev_err(codec->card->dev,
  434. "ali_alloc_pcm_channel: no free channels.\n");
  435. return -1;
  436. }
  437. if (!(codec->synth.chmap & (1 << idx))) {
  438. codec->synth.chmap |= 1 << idx;
  439. codec->synth.chcnt++;
  440. dev_dbg(codec->card->dev, "alloc_pcm_channel no. %d.\n", idx);
  441. return idx;
  442. }
  443. return -1;
  444. }
  445. static int snd_ali_find_free_channel(struct snd_ali * codec, int rec)
  446. {
  447. int idx;
  448. int result = -1;
  449. dev_dbg(codec->card->dev,
  450. "find_free_channel: for %s\n", rec ? "rec" : "pcm");
  451. /* recording */
  452. if (rec) {
  453. if (codec->spdif_support &&
  454. (inl(ALI_REG(codec, ALI_GLOBAL_CONTROL)) &
  455. ALI_SPDIF_IN_SUPPORT))
  456. idx = ALI_SPDIF_IN_CHANNEL;
  457. else
  458. idx = ALI_PCM_IN_CHANNEL;
  459. result = snd_ali_alloc_pcm_channel(codec, idx);
  460. if (result >= 0)
  461. return result;
  462. else {
  463. dev_err(codec->card->dev,
  464. "ali_find_free_channel: record channel is busy now.\n");
  465. return -1;
  466. }
  467. }
  468. /* playback... */
  469. if (codec->spdif_support &&
  470. (inl(ALI_REG(codec, ALI_GLOBAL_CONTROL)) &
  471. ALI_SPDIF_OUT_CH_ENABLE)) {
  472. idx = ALI_SPDIF_OUT_CHANNEL;
  473. result = snd_ali_alloc_pcm_channel(codec, idx);
  474. if (result >= 0)
  475. return result;
  476. else
  477. dev_err(codec->card->dev,
  478. "ali_find_free_channel: S/PDIF out channel is in busy now.\n");
  479. }
  480. for (idx = 0; idx < ALI_CHANNELS; idx++) {
  481. result = snd_ali_alloc_pcm_channel(codec, idx);
  482. if (result >= 0)
  483. return result;
  484. }
  485. dev_err(codec->card->dev, "ali_find_free_channel: no free channels.\n");
  486. return -1;
  487. }
  488. static void snd_ali_free_channel_pcm(struct snd_ali *codec, int channel)
  489. {
  490. unsigned int idx = channel & 0x0000001f;
  491. dev_dbg(codec->card->dev, "free_channel_pcm channel=%d\n", channel);
  492. if (channel < 0 || channel >= ALI_CHANNELS)
  493. return;
  494. if (!(codec->synth.chmap & (1 << idx))) {
  495. dev_err(codec->card->dev,
  496. "ali_free_channel_pcm: channel %d is not in use.\n",
  497. channel);
  498. return;
  499. } else {
  500. codec->synth.chmap &= ~(1 << idx);
  501. codec->synth.chcnt--;
  502. }
  503. }
  504. static void snd_ali_stop_voice(struct snd_ali *codec, unsigned int channel)
  505. {
  506. unsigned int mask = 1 << (channel & 0x1f);
  507. dev_dbg(codec->card->dev, "stop_voice: channel=%d\n", channel);
  508. outl(mask, ALI_REG(codec, codec->chregs.regs.stop));
  509. }
  510. /*
  511. * S/PDIF Part
  512. */
  513. static void snd_ali_delay(struct snd_ali *codec,int interval)
  514. {
  515. unsigned long begintimer,currenttimer;
  516. begintimer = inl(ALI_REG(codec, ALI_STIMER));
  517. currenttimer = inl(ALI_REG(codec, ALI_STIMER));
  518. while (currenttimer < begintimer + interval) {
  519. if (snd_ali_stimer_ready(codec) < 0)
  520. break;
  521. currenttimer = inl(ALI_REG(codec, ALI_STIMER));
  522. cpu_relax();
  523. }
  524. }
  525. static void snd_ali_detect_spdif_rate(struct snd_ali *codec)
  526. {
  527. u16 wval;
  528. u16 count = 0;
  529. u8 bval, R1 = 0, R2;
  530. bval = inb(ALI_REG(codec, ALI_SPDIF_CTRL + 1));
  531. bval |= 0x1F;
  532. outb(bval, ALI_REG(codec, ALI_SPDIF_CTRL + 1));
  533. while ((R1 < 0x0b || R1 > 0x0e) && R1 != 0x12 && count <= 50000) {
  534. count ++;
  535. snd_ali_delay(codec, 6);
  536. bval = inb(ALI_REG(codec, ALI_SPDIF_CTRL + 1));
  537. R1 = bval & 0x1F;
  538. }
  539. if (count > 50000) {
  540. dev_err(codec->card->dev, "ali_detect_spdif_rate: timeout!\n");
  541. return;
  542. }
  543. for (count = 0; count <= 50000; count++) {
  544. snd_ali_delay(codec, 6);
  545. bval = inb(ALI_REG(codec,ALI_SPDIF_CTRL + 1));
  546. R2 = bval & 0x1F;
  547. if (R2 != R1)
  548. R1 = R2;
  549. else
  550. break;
  551. }
  552. if (count > 50000) {
  553. dev_err(codec->card->dev, "ali_detect_spdif_rate: timeout!\n");
  554. return;
  555. }
  556. if (R2 >= 0x0b && R2 <= 0x0e) {
  557. wval = inw(ALI_REG(codec, ALI_SPDIF_CTRL + 2));
  558. wval &= 0xe0f0;
  559. wval |= (0x09 << 8) | 0x05;
  560. outw(wval, ALI_REG(codec, ALI_SPDIF_CTRL + 2));
  561. bval = inb(ALI_REG(codec, ALI_SPDIF_CS + 3)) & 0xf0;
  562. outb(bval | 0x02, ALI_REG(codec, ALI_SPDIF_CS + 3));
  563. } else if (R2 == 0x12) {
  564. wval = inw(ALI_REG(codec, ALI_SPDIF_CTRL + 2));
  565. wval &= 0xe0f0;
  566. wval |= (0x0e << 8) | 0x08;
  567. outw(wval, ALI_REG(codec, ALI_SPDIF_CTRL + 2));
  568. bval = inb(ALI_REG(codec,ALI_SPDIF_CS + 3)) & 0xf0;
  569. outb(bval | 0x03, ALI_REG(codec, ALI_SPDIF_CS + 3));
  570. }
  571. }
  572. static unsigned int snd_ali_get_spdif_in_rate(struct snd_ali *codec)
  573. {
  574. u32 dwRate;
  575. u8 bval;
  576. bval = inb(ALI_REG(codec, ALI_SPDIF_CTRL));
  577. bval &= 0x7f;
  578. bval |= 0x40;
  579. outb(bval, ALI_REG(codec, ALI_SPDIF_CTRL));
  580. snd_ali_detect_spdif_rate(codec);
  581. bval = inb(ALI_REG(codec, ALI_SPDIF_CS + 3));
  582. bval &= 0x0f;
  583. switch (bval) {
  584. case 0: dwRate = 44100; break;
  585. case 1: dwRate = 48000; break;
  586. case 2: dwRate = 32000; break;
  587. default: dwRate = 0; break;
  588. }
  589. return dwRate;
  590. }
  591. static void snd_ali_enable_spdif_in(struct snd_ali *codec)
  592. {
  593. unsigned int dwVal;
  594. dwVal = inl(ALI_REG(codec, ALI_GLOBAL_CONTROL));
  595. dwVal |= ALI_SPDIF_IN_SUPPORT;
  596. outl(dwVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
  597. dwVal = inb(ALI_REG(codec, ALI_SPDIF_CTRL));
  598. dwVal |= 0x02;
  599. outb(dwVal, ALI_REG(codec, ALI_SPDIF_CTRL));
  600. snd_ali_enable_special_channel(codec, ALI_SPDIF_IN_CHANNEL);
  601. }
  602. static void snd_ali_disable_spdif_in(struct snd_ali *codec)
  603. {
  604. unsigned int dwVal;
  605. dwVal = inl(ALI_REG(codec, ALI_GLOBAL_CONTROL));
  606. dwVal &= ~ALI_SPDIF_IN_SUPPORT;
  607. outl(dwVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
  608. snd_ali_disable_special_channel(codec, ALI_SPDIF_IN_CHANNEL);
  609. }
  610. static void snd_ali_set_spdif_out_rate(struct snd_ali *codec, unsigned int rate)
  611. {
  612. unsigned char bVal;
  613. unsigned int dwRate;
  614. switch (rate) {
  615. case 32000: dwRate = 0x300; break;
  616. case 48000: dwRate = 0x200; break;
  617. default: dwRate = 0; break;
  618. }
  619. bVal = inb(ALI_REG(codec, ALI_SPDIF_CTRL));
  620. bVal &= (unsigned char)(~(1<<6));
  621. bVal |= 0x80; /* select right */
  622. outb(bVal, ALI_REG(codec, ALI_SPDIF_CTRL));
  623. outb(dwRate | 0x20, ALI_REG(codec, ALI_SPDIF_CS + 2));
  624. bVal &= ~0x80; /* select left */
  625. outb(bVal, ALI_REG(codec, ALI_SPDIF_CTRL));
  626. outw(rate | 0x10, ALI_REG(codec, ALI_SPDIF_CS + 2));
  627. }
  628. static void snd_ali_enable_spdif_out(struct snd_ali *codec)
  629. {
  630. unsigned short wVal;
  631. unsigned char bVal;
  632. struct pci_dev *pci_dev;
  633. pci_dev = codec->pci_m1533;
  634. if (pci_dev == NULL)
  635. return;
  636. pci_read_config_byte(pci_dev, 0x61, &bVal);
  637. bVal |= 0x40;
  638. pci_write_config_byte(pci_dev, 0x61, bVal);
  639. pci_read_config_byte(pci_dev, 0x7d, &bVal);
  640. bVal |= 0x01;
  641. pci_write_config_byte(pci_dev, 0x7d, bVal);
  642. pci_read_config_byte(pci_dev, 0x7e, &bVal);
  643. bVal &= (~0x20);
  644. bVal |= 0x10;
  645. pci_write_config_byte(pci_dev, 0x7e, bVal);
  646. bVal = inb(ALI_REG(codec, ALI_SCTRL));
  647. outb(bVal | ALI_SPDIF_OUT_ENABLE, ALI_REG(codec, ALI_SCTRL));
  648. bVal = inb(ALI_REG(codec, ALI_SPDIF_CTRL));
  649. outb(bVal & ALI_SPDIF_OUT_CH_STATUS, ALI_REG(codec, ALI_SPDIF_CTRL));
  650. wVal = inw(ALI_REG(codec, ALI_GLOBAL_CONTROL));
  651. wVal |= ALI_SPDIF_OUT_SEL_PCM;
  652. outw(wVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
  653. snd_ali_disable_special_channel(codec, ALI_SPDIF_OUT_CHANNEL);
  654. }
  655. static void snd_ali_enable_spdif_chnout(struct snd_ali *codec)
  656. {
  657. unsigned short wVal;
  658. wVal = inw(ALI_REG(codec, ALI_GLOBAL_CONTROL));
  659. wVal &= ~ALI_SPDIF_OUT_SEL_PCM;
  660. outw(wVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
  661. /*
  662. wVal = inw(ALI_REG(codec, ALI_SPDIF_CS));
  663. if (flag & ALI_SPDIF_OUT_NON_PCM)
  664. wVal |= 0x0002;
  665. else
  666. wVal &= (~0x0002);
  667. outw(wVal, ALI_REG(codec, ALI_SPDIF_CS));
  668. */
  669. snd_ali_enable_special_channel(codec, ALI_SPDIF_OUT_CHANNEL);
  670. }
  671. static void snd_ali_disable_spdif_chnout(struct snd_ali *codec)
  672. {
  673. unsigned short wVal;
  674. wVal = inw(ALI_REG(codec, ALI_GLOBAL_CONTROL));
  675. wVal |= ALI_SPDIF_OUT_SEL_PCM;
  676. outw(wVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
  677. snd_ali_enable_special_channel(codec, ALI_SPDIF_OUT_CHANNEL);
  678. }
  679. static void snd_ali_disable_spdif_out(struct snd_ali *codec)
  680. {
  681. unsigned char bVal;
  682. bVal = inb(ALI_REG(codec, ALI_SCTRL));
  683. outb(bVal & ~ALI_SPDIF_OUT_ENABLE, ALI_REG(codec, ALI_SCTRL));
  684. snd_ali_disable_spdif_chnout(codec);
  685. }
  686. static void snd_ali_update_ptr(struct snd_ali *codec, int channel)
  687. {
  688. struct snd_ali_voice *pvoice;
  689. struct snd_ali_channel_control *pchregs;
  690. unsigned int old, mask;
  691. pchregs = &(codec->chregs);
  692. /* check if interrupt occurred for channel */
  693. old = pchregs->data.aint;
  694. mask = 1U << (channel & 0x1f);
  695. if (!(old & mask))
  696. return;
  697. pvoice = &codec->synth.voices[channel];
  698. udelay(100);
  699. spin_lock(&codec->reg_lock);
  700. if (pvoice->pcm && pvoice->substream) {
  701. /* pcm interrupt */
  702. if (pvoice->running) {
  703. dev_dbg(codec->card->dev,
  704. "update_ptr: cso=%4.4x cspf=%d.\n",
  705. inw(ALI_REG(codec, ALI_CSO_ALPHA_FMS + 2)),
  706. (inl(ALI_REG(codec, ALI_CSPF)) & mask) == mask);
  707. spin_unlock(&codec->reg_lock);
  708. snd_pcm_period_elapsed(pvoice->substream);
  709. spin_lock(&codec->reg_lock);
  710. } else {
  711. snd_ali_stop_voice(codec, channel);
  712. snd_ali_disable_voice_irq(codec, channel);
  713. }
  714. } else if (codec->synth.voices[channel].synth) {
  715. /* synth interrupt */
  716. } else if (codec->synth.voices[channel].midi) {
  717. /* midi interrupt */
  718. } else {
  719. /* unknown interrupt */
  720. snd_ali_stop_voice(codec, channel);
  721. snd_ali_disable_voice_irq(codec, channel);
  722. }
  723. spin_unlock(&codec->reg_lock);
  724. outl(mask,ALI_REG(codec,pchregs->regs.aint));
  725. pchregs->data.aint = old & (~mask);
  726. }
  727. static irqreturn_t snd_ali_card_interrupt(int irq, void *dev_id)
  728. {
  729. struct snd_ali *codec = dev_id;
  730. int channel;
  731. unsigned int audio_int;
  732. struct snd_ali_channel_control *pchregs;
  733. if (codec == NULL || !codec->hw_initialized)
  734. return IRQ_NONE;
  735. audio_int = inl(ALI_REG(codec, ALI_MISCINT));
  736. if (!audio_int)
  737. return IRQ_NONE;
  738. pchregs = &(codec->chregs);
  739. if (audio_int & ADDRESS_IRQ) {
  740. /* get interrupt status for all channels */
  741. pchregs->data.aint = inl(ALI_REG(codec, pchregs->regs.aint));
  742. for (channel = 0; channel < ALI_CHANNELS; channel++)
  743. snd_ali_update_ptr(codec, channel);
  744. }
  745. outl((TARGET_REACHED | MIXER_OVERFLOW | MIXER_UNDERFLOW),
  746. ALI_REG(codec, ALI_MISCINT));
  747. return IRQ_HANDLED;
  748. }
  749. static struct snd_ali_voice *snd_ali_alloc_voice(struct snd_ali * codec,
  750. int type, int rec, int channel)
  751. {
  752. struct snd_ali_voice *pvoice;
  753. int idx;
  754. dev_dbg(codec->card->dev, "alloc_voice: type=%d rec=%d\n", type, rec);
  755. spin_lock_irq(&codec->voice_alloc);
  756. if (type == SNDRV_ALI_VOICE_TYPE_PCM) {
  757. idx = channel > 0 ? snd_ali_alloc_pcm_channel(codec, channel) :
  758. snd_ali_find_free_channel(codec,rec);
  759. if (idx < 0) {
  760. dev_err(codec->card->dev, "ali_alloc_voice: err.\n");
  761. spin_unlock_irq(&codec->voice_alloc);
  762. return NULL;
  763. }
  764. pvoice = &(codec->synth.voices[idx]);
  765. pvoice->codec = codec;
  766. pvoice->use = 1;
  767. pvoice->pcm = 1;
  768. pvoice->mode = rec;
  769. spin_unlock_irq(&codec->voice_alloc);
  770. return pvoice;
  771. }
  772. spin_unlock_irq(&codec->voice_alloc);
  773. return NULL;
  774. }
  775. static void snd_ali_free_voice(struct snd_ali * codec,
  776. struct snd_ali_voice *pvoice)
  777. {
  778. void (*private_free)(void *);
  779. void *private_data;
  780. dev_dbg(codec->card->dev, "free_voice: channel=%d\n", pvoice->number);
  781. if (!pvoice->use)
  782. return;
  783. snd_ali_clear_voices(codec, pvoice->number, pvoice->number);
  784. spin_lock_irq(&codec->voice_alloc);
  785. private_free = pvoice->private_free;
  786. private_data = pvoice->private_data;
  787. pvoice->private_free = NULL;
  788. pvoice->private_data = NULL;
  789. if (pvoice->pcm)
  790. snd_ali_free_channel_pcm(codec, pvoice->number);
  791. pvoice->use = pvoice->pcm = pvoice->synth = 0;
  792. pvoice->substream = NULL;
  793. spin_unlock_irq(&codec->voice_alloc);
  794. if (private_free)
  795. private_free(private_data);
  796. }
  797. static void snd_ali_clear_voices(struct snd_ali *codec,
  798. unsigned int v_min,
  799. unsigned int v_max)
  800. {
  801. unsigned int i;
  802. for (i = v_min; i <= v_max; i++) {
  803. snd_ali_stop_voice(codec, i);
  804. snd_ali_disable_voice_irq(codec, i);
  805. }
  806. }
  807. static void snd_ali_write_voice_regs(struct snd_ali *codec,
  808. unsigned int Channel,
  809. unsigned int LBA,
  810. unsigned int CSO,
  811. unsigned int ESO,
  812. unsigned int DELTA,
  813. unsigned int ALPHA_FMS,
  814. unsigned int GVSEL,
  815. unsigned int PAN,
  816. unsigned int VOL,
  817. unsigned int CTRL,
  818. unsigned int EC)
  819. {
  820. unsigned int ctlcmds[4];
  821. outb((unsigned char)(Channel & 0x001f), ALI_REG(codec, ALI_GC_CIR));
  822. ctlcmds[0] = (CSO << 16) | (ALPHA_FMS & 0x0000ffff);
  823. ctlcmds[1] = LBA;
  824. ctlcmds[2] = (ESO << 16) | (DELTA & 0x0ffff);
  825. ctlcmds[3] = (GVSEL << 31) |
  826. ((PAN & 0x0000007f) << 24) |
  827. ((VOL & 0x000000ff) << 16) |
  828. ((CTRL & 0x0000000f) << 12) |
  829. (EC & 0x00000fff);
  830. outb(Channel, ALI_REG(codec, ALI_GC_CIR));
  831. outl(ctlcmds[0], ALI_REG(codec, ALI_CSO_ALPHA_FMS));
  832. outl(ctlcmds[1], ALI_REG(codec, ALI_LBA));
  833. outl(ctlcmds[2], ALI_REG(codec, ALI_ESO_DELTA));
  834. outl(ctlcmds[3], ALI_REG(codec, ALI_GVSEL_PAN_VOC_CTRL_EC));
  835. outl(0x30000000, ALI_REG(codec, ALI_EBUF1)); /* Still Mode */
  836. outl(0x30000000, ALI_REG(codec, ALI_EBUF2)); /* Still Mode */
  837. }
  838. static unsigned int snd_ali_convert_rate(unsigned int rate, int rec)
  839. {
  840. unsigned int delta;
  841. if (rate < 4000)
  842. rate = 4000;
  843. if (rate > 48000)
  844. rate = 48000;
  845. if (rec) {
  846. if (rate == 44100)
  847. delta = 0x116a;
  848. else if (rate == 8000)
  849. delta = 0x6000;
  850. else if (rate == 48000)
  851. delta = 0x1000;
  852. else
  853. delta = ((48000 << 12) / rate) & 0x0000ffff;
  854. } else {
  855. if (rate == 44100)
  856. delta = 0xeb3;
  857. else if (rate == 8000)
  858. delta = 0x2ab;
  859. else if (rate == 48000)
  860. delta = 0x1000;
  861. else
  862. delta = (((rate << 12) + rate) / 48000) & 0x0000ffff;
  863. }
  864. return delta;
  865. }
  866. static unsigned int snd_ali_control_mode(struct snd_pcm_substream *substream)
  867. {
  868. unsigned int CTRL;
  869. struct snd_pcm_runtime *runtime = substream->runtime;
  870. /* set ctrl mode
  871. CTRL default: 8-bit (unsigned) mono, loop mode enabled
  872. */
  873. CTRL = 0x00000001;
  874. if (snd_pcm_format_width(runtime->format) == 16)
  875. CTRL |= 0x00000008; /* 16-bit data */
  876. if (!snd_pcm_format_unsigned(runtime->format))
  877. CTRL |= 0x00000002; /* signed data */
  878. if (runtime->channels > 1)
  879. CTRL |= 0x00000004; /* stereo data */
  880. return CTRL;
  881. }
  882. /*
  883. * PCM part
  884. */
  885. static int snd_ali_trigger(struct snd_pcm_substream *substream,
  886. int cmd)
  887. {
  888. struct snd_ali *codec = snd_pcm_substream_chip(substream);
  889. struct snd_pcm_substream *s;
  890. unsigned int what, whati, capture_flag;
  891. struct snd_ali_voice *pvoice, *evoice;
  892. unsigned int val;
  893. int do_start;
  894. switch (cmd) {
  895. case SNDRV_PCM_TRIGGER_START:
  896. case SNDRV_PCM_TRIGGER_RESUME:
  897. do_start = 1;
  898. break;
  899. case SNDRV_PCM_TRIGGER_STOP:
  900. case SNDRV_PCM_TRIGGER_SUSPEND:
  901. do_start = 0;
  902. break;
  903. default:
  904. return -EINVAL;
  905. }
  906. what = whati = capture_flag = 0;
  907. snd_pcm_group_for_each_entry(s, substream) {
  908. if ((struct snd_ali *) snd_pcm_substream_chip(s) == codec) {
  909. pvoice = s->runtime->private_data;
  910. evoice = pvoice->extra;
  911. what |= 1 << (pvoice->number & 0x1f);
  912. if (evoice == NULL)
  913. whati |= 1 << (pvoice->number & 0x1f);
  914. else {
  915. whati |= 1 << (evoice->number & 0x1f);
  916. what |= 1 << (evoice->number & 0x1f);
  917. }
  918. if (do_start) {
  919. pvoice->running = 1;
  920. if (evoice != NULL)
  921. evoice->running = 1;
  922. } else {
  923. pvoice->running = 0;
  924. if (evoice != NULL)
  925. evoice->running = 0;
  926. }
  927. snd_pcm_trigger_done(s, substream);
  928. if (pvoice->mode)
  929. capture_flag = 1;
  930. }
  931. }
  932. spin_lock(&codec->reg_lock);
  933. if (!do_start)
  934. outl(what, ALI_REG(codec, ALI_STOP));
  935. val = inl(ALI_REG(codec, ALI_AINTEN));
  936. if (do_start)
  937. val |= whati;
  938. else
  939. val &= ~whati;
  940. outl(val, ALI_REG(codec, ALI_AINTEN));
  941. if (do_start)
  942. outl(what, ALI_REG(codec, ALI_START));
  943. dev_dbg(codec->card->dev, "trigger: what=%xh whati=%xh\n", what, whati);
  944. spin_unlock(&codec->reg_lock);
  945. return 0;
  946. }
  947. static int snd_ali_playback_hw_params(struct snd_pcm_substream *substream,
  948. struct snd_pcm_hw_params *hw_params)
  949. {
  950. struct snd_ali *codec = snd_pcm_substream_chip(substream);
  951. struct snd_pcm_runtime *runtime = substream->runtime;
  952. struct snd_ali_voice *pvoice = runtime->private_data;
  953. struct snd_ali_voice *evoice = pvoice->extra;
  954. int err;
  955. err = snd_pcm_lib_malloc_pages(substream,
  956. params_buffer_bytes(hw_params));
  957. if (err < 0)
  958. return err;
  959. /* voice management */
  960. if (params_buffer_size(hw_params) / 2 !=
  961. params_period_size(hw_params)) {
  962. if (!evoice) {
  963. evoice = snd_ali_alloc_voice(codec,
  964. SNDRV_ALI_VOICE_TYPE_PCM,
  965. 0, -1);
  966. if (!evoice)
  967. return -ENOMEM;
  968. pvoice->extra = evoice;
  969. evoice->substream = substream;
  970. }
  971. } else {
  972. if (evoice) {
  973. snd_ali_free_voice(codec, evoice);
  974. pvoice->extra = evoice = NULL;
  975. }
  976. }
  977. return 0;
  978. }
  979. static int snd_ali_playback_hw_free(struct snd_pcm_substream *substream)
  980. {
  981. struct snd_ali *codec = snd_pcm_substream_chip(substream);
  982. struct snd_pcm_runtime *runtime = substream->runtime;
  983. struct snd_ali_voice *pvoice = runtime->private_data;
  984. struct snd_ali_voice *evoice = pvoice ? pvoice->extra : NULL;
  985. snd_pcm_lib_free_pages(substream);
  986. if (evoice) {
  987. snd_ali_free_voice(codec, evoice);
  988. pvoice->extra = NULL;
  989. }
  990. return 0;
  991. }
  992. static int snd_ali_hw_params(struct snd_pcm_substream *substream,
  993. struct snd_pcm_hw_params *hw_params)
  994. {
  995. return snd_pcm_lib_malloc_pages(substream,
  996. params_buffer_bytes(hw_params));
  997. }
  998. static int snd_ali_hw_free(struct snd_pcm_substream *substream)
  999. {
  1000. return snd_pcm_lib_free_pages(substream);
  1001. }
  1002. static int snd_ali_playback_prepare(struct snd_pcm_substream *substream)
  1003. {
  1004. struct snd_ali *codec = snd_pcm_substream_chip(substream);
  1005. struct snd_pcm_runtime *runtime = substream->runtime;
  1006. struct snd_ali_voice *pvoice = runtime->private_data;
  1007. struct snd_ali_voice *evoice = pvoice->extra;
  1008. unsigned int LBA;
  1009. unsigned int Delta;
  1010. unsigned int ESO;
  1011. unsigned int CTRL;
  1012. unsigned int GVSEL;
  1013. unsigned int PAN;
  1014. unsigned int VOL;
  1015. unsigned int EC;
  1016. dev_dbg(codec->card->dev, "playback_prepare ...\n");
  1017. spin_lock_irq(&codec->reg_lock);
  1018. /* set Delta (rate) value */
  1019. Delta = snd_ali_convert_rate(runtime->rate, 0);
  1020. if (pvoice->number == ALI_SPDIF_IN_CHANNEL ||
  1021. pvoice->number == ALI_PCM_IN_CHANNEL)
  1022. snd_ali_disable_special_channel(codec, pvoice->number);
  1023. else if (codec->spdif_support &&
  1024. (inl(ALI_REG(codec, ALI_GLOBAL_CONTROL)) &
  1025. ALI_SPDIF_OUT_CH_ENABLE)
  1026. && pvoice->number == ALI_SPDIF_OUT_CHANNEL) {
  1027. snd_ali_set_spdif_out_rate(codec, runtime->rate);
  1028. Delta = 0x1000;
  1029. }
  1030. /* set Loop Back Address */
  1031. LBA = runtime->dma_addr;
  1032. /* set interrupt count size */
  1033. pvoice->count = runtime->period_size;
  1034. /* set target ESO for channel */
  1035. pvoice->eso = runtime->buffer_size;
  1036. dev_dbg(codec->card->dev, "playback_prepare: eso=%xh count=%xh\n",
  1037. pvoice->eso, pvoice->count);
  1038. /* set ESO to capture first MIDLP interrupt */
  1039. ESO = pvoice->eso -1;
  1040. /* set ctrl mode */
  1041. CTRL = snd_ali_control_mode(substream);
  1042. GVSEL = 1;
  1043. PAN = 0;
  1044. VOL = 0;
  1045. EC = 0;
  1046. dev_dbg(codec->card->dev, "playback_prepare:\n");
  1047. dev_dbg(codec->card->dev,
  1048. "ch=%d, Rate=%d Delta=%xh,GVSEL=%xh,PAN=%xh,CTRL=%xh\n",
  1049. pvoice->number,runtime->rate,Delta,GVSEL,PAN,CTRL);
  1050. snd_ali_write_voice_regs(codec,
  1051. pvoice->number,
  1052. LBA,
  1053. 0, /* cso */
  1054. ESO,
  1055. Delta,
  1056. 0, /* alpha */
  1057. GVSEL,
  1058. PAN,
  1059. VOL,
  1060. CTRL,
  1061. EC);
  1062. if (evoice) {
  1063. evoice->count = pvoice->count;
  1064. evoice->eso = pvoice->count << 1;
  1065. ESO = evoice->eso - 1;
  1066. snd_ali_write_voice_regs(codec,
  1067. evoice->number,
  1068. LBA,
  1069. 0, /* cso */
  1070. ESO,
  1071. Delta,
  1072. 0, /* alpha */
  1073. GVSEL,
  1074. 0x7f,
  1075. 0x3ff,
  1076. CTRL,
  1077. EC);
  1078. }
  1079. spin_unlock_irq(&codec->reg_lock);
  1080. return 0;
  1081. }
  1082. static int snd_ali_prepare(struct snd_pcm_substream *substream)
  1083. {
  1084. struct snd_ali *codec = snd_pcm_substream_chip(substream);
  1085. struct snd_pcm_runtime *runtime = substream->runtime;
  1086. struct snd_ali_voice *pvoice = runtime->private_data;
  1087. unsigned int LBA;
  1088. unsigned int Delta;
  1089. unsigned int ESO;
  1090. unsigned int CTRL;
  1091. unsigned int GVSEL;
  1092. unsigned int PAN;
  1093. unsigned int VOL;
  1094. unsigned int EC;
  1095. u8 bValue;
  1096. spin_lock_irq(&codec->reg_lock);
  1097. dev_dbg(codec->card->dev, "ali_prepare...\n");
  1098. snd_ali_enable_special_channel(codec,pvoice->number);
  1099. Delta = (pvoice->number == ALI_MODEM_IN_CHANNEL ||
  1100. pvoice->number == ALI_MODEM_OUT_CHANNEL) ?
  1101. 0x1000 : snd_ali_convert_rate(runtime->rate, pvoice->mode);
  1102. /* Prepare capture intr channel */
  1103. if (pvoice->number == ALI_SPDIF_IN_CHANNEL) {
  1104. unsigned int rate;
  1105. spin_unlock_irq(&codec->reg_lock);
  1106. if (codec->revision != ALI_5451_V02)
  1107. return -1;
  1108. rate = snd_ali_get_spdif_in_rate(codec);
  1109. if (rate == 0) {
  1110. dev_warn(codec->card->dev,
  1111. "ali_capture_preapre: spdif rate detect err!\n");
  1112. rate = 48000;
  1113. }
  1114. spin_lock_irq(&codec->reg_lock);
  1115. bValue = inb(ALI_REG(codec,ALI_SPDIF_CTRL));
  1116. if (bValue & 0x10) {
  1117. outb(bValue,ALI_REG(codec,ALI_SPDIF_CTRL));
  1118. dev_warn(codec->card->dev,
  1119. "clear SPDIF parity error flag.\n");
  1120. }
  1121. if (rate != 48000)
  1122. Delta = ((rate << 12) / runtime->rate) & 0x00ffff;
  1123. }
  1124. /* set target ESO for channel */
  1125. pvoice->eso = runtime->buffer_size;
  1126. /* set interrupt count size */
  1127. pvoice->count = runtime->period_size;
  1128. /* set Loop Back Address */
  1129. LBA = runtime->dma_addr;
  1130. /* set ESO to capture first MIDLP interrupt */
  1131. ESO = pvoice->eso - 1;
  1132. CTRL = snd_ali_control_mode(substream);
  1133. GVSEL = 0;
  1134. PAN = 0x00;
  1135. VOL = 0x00;
  1136. EC = 0;
  1137. snd_ali_write_voice_regs( codec,
  1138. pvoice->number,
  1139. LBA,
  1140. 0, /* cso */
  1141. ESO,
  1142. Delta,
  1143. 0, /* alpha */
  1144. GVSEL,
  1145. PAN,
  1146. VOL,
  1147. CTRL,
  1148. EC);
  1149. spin_unlock_irq(&codec->reg_lock);
  1150. return 0;
  1151. }
  1152. static snd_pcm_uframes_t
  1153. snd_ali_playback_pointer(struct snd_pcm_substream *substream)
  1154. {
  1155. struct snd_ali *codec = snd_pcm_substream_chip(substream);
  1156. struct snd_pcm_runtime *runtime = substream->runtime;
  1157. struct snd_ali_voice *pvoice = runtime->private_data;
  1158. unsigned int cso;
  1159. spin_lock(&codec->reg_lock);
  1160. if (!pvoice->running) {
  1161. spin_unlock(&codec->reg_lock);
  1162. return 0;
  1163. }
  1164. outb(pvoice->number, ALI_REG(codec, ALI_GC_CIR));
  1165. cso = inw(ALI_REG(codec, ALI_CSO_ALPHA_FMS + 2));
  1166. spin_unlock(&codec->reg_lock);
  1167. dev_dbg(codec->card->dev, "playback pointer returned cso=%xh.\n", cso);
  1168. return cso;
  1169. }
  1170. static snd_pcm_uframes_t snd_ali_pointer(struct snd_pcm_substream *substream)
  1171. {
  1172. struct snd_ali *codec = snd_pcm_substream_chip(substream);
  1173. struct snd_pcm_runtime *runtime = substream->runtime;
  1174. struct snd_ali_voice *pvoice = runtime->private_data;
  1175. unsigned int cso;
  1176. spin_lock(&codec->reg_lock);
  1177. if (!pvoice->running) {
  1178. spin_unlock(&codec->reg_lock);
  1179. return 0;
  1180. }
  1181. outb(pvoice->number, ALI_REG(codec, ALI_GC_CIR));
  1182. cso = inw(ALI_REG(codec, ALI_CSO_ALPHA_FMS + 2));
  1183. spin_unlock(&codec->reg_lock);
  1184. return cso;
  1185. }
  1186. static struct snd_pcm_hardware snd_ali_playback =
  1187. {
  1188. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  1189. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  1190. SNDRV_PCM_INFO_MMAP_VALID |
  1191. SNDRV_PCM_INFO_RESUME |
  1192. SNDRV_PCM_INFO_SYNC_START),
  1193. .formats = (SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE |
  1194. SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U16_LE),
  1195. .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
  1196. .rate_min = 4000,
  1197. .rate_max = 48000,
  1198. .channels_min = 1,
  1199. .channels_max = 2,
  1200. .buffer_bytes_max = (256*1024),
  1201. .period_bytes_min = 64,
  1202. .period_bytes_max = (256*1024),
  1203. .periods_min = 1,
  1204. .periods_max = 1024,
  1205. .fifo_size = 0,
  1206. };
  1207. /*
  1208. * Capture support device description
  1209. */
  1210. static struct snd_pcm_hardware snd_ali_capture =
  1211. {
  1212. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  1213. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  1214. SNDRV_PCM_INFO_MMAP_VALID |
  1215. SNDRV_PCM_INFO_RESUME |
  1216. SNDRV_PCM_INFO_SYNC_START),
  1217. .formats = (SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE |
  1218. SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U16_LE),
  1219. .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
  1220. .rate_min = 4000,
  1221. .rate_max = 48000,
  1222. .channels_min = 1,
  1223. .channels_max = 2,
  1224. .buffer_bytes_max = (128*1024),
  1225. .period_bytes_min = 64,
  1226. .period_bytes_max = (128*1024),
  1227. .periods_min = 1,
  1228. .periods_max = 1024,
  1229. .fifo_size = 0,
  1230. };
  1231. static void snd_ali_pcm_free_substream(struct snd_pcm_runtime *runtime)
  1232. {
  1233. struct snd_ali_voice *pvoice = runtime->private_data;
  1234. struct snd_ali *codec;
  1235. if (pvoice) {
  1236. codec = pvoice->codec;
  1237. snd_ali_free_voice(pvoice->codec, pvoice);
  1238. }
  1239. }
  1240. static int snd_ali_open(struct snd_pcm_substream *substream, int rec,
  1241. int channel, struct snd_pcm_hardware *phw)
  1242. {
  1243. struct snd_ali *codec = snd_pcm_substream_chip(substream);
  1244. struct snd_pcm_runtime *runtime = substream->runtime;
  1245. struct snd_ali_voice *pvoice;
  1246. pvoice = snd_ali_alloc_voice(codec, SNDRV_ALI_VOICE_TYPE_PCM, rec,
  1247. channel);
  1248. if (!pvoice)
  1249. return -EAGAIN;
  1250. pvoice->substream = substream;
  1251. runtime->private_data = pvoice;
  1252. runtime->private_free = snd_ali_pcm_free_substream;
  1253. runtime->hw = *phw;
  1254. snd_pcm_set_sync(substream);
  1255. snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
  1256. 0, 64*1024);
  1257. return 0;
  1258. }
  1259. static int snd_ali_playback_open(struct snd_pcm_substream *substream)
  1260. {
  1261. return snd_ali_open(substream, 0, -1, &snd_ali_playback);
  1262. }
  1263. static int snd_ali_capture_open(struct snd_pcm_substream *substream)
  1264. {
  1265. return snd_ali_open(substream, 1, -1, &snd_ali_capture);
  1266. }
  1267. static int snd_ali_playback_close(struct snd_pcm_substream *substream)
  1268. {
  1269. return 0;
  1270. }
  1271. static int snd_ali_close(struct snd_pcm_substream *substream)
  1272. {
  1273. struct snd_ali *codec = snd_pcm_substream_chip(substream);
  1274. struct snd_ali_voice *pvoice = substream->runtime->private_data;
  1275. snd_ali_disable_special_channel(codec,pvoice->number);
  1276. return 0;
  1277. }
  1278. static struct snd_pcm_ops snd_ali_playback_ops = {
  1279. .open = snd_ali_playback_open,
  1280. .close = snd_ali_playback_close,
  1281. .ioctl = snd_pcm_lib_ioctl,
  1282. .hw_params = snd_ali_playback_hw_params,
  1283. .hw_free = snd_ali_playback_hw_free,
  1284. .prepare = snd_ali_playback_prepare,
  1285. .trigger = snd_ali_trigger,
  1286. .pointer = snd_ali_playback_pointer,
  1287. };
  1288. static struct snd_pcm_ops snd_ali_capture_ops = {
  1289. .open = snd_ali_capture_open,
  1290. .close = snd_ali_close,
  1291. .ioctl = snd_pcm_lib_ioctl,
  1292. .hw_params = snd_ali_hw_params,
  1293. .hw_free = snd_ali_hw_free,
  1294. .prepare = snd_ali_prepare,
  1295. .trigger = snd_ali_trigger,
  1296. .pointer = snd_ali_pointer,
  1297. };
  1298. /*
  1299. * Modem PCM
  1300. */
  1301. static int snd_ali_modem_hw_params(struct snd_pcm_substream *substream,
  1302. struct snd_pcm_hw_params *hw_params)
  1303. {
  1304. struct snd_ali *chip = snd_pcm_substream_chip(substream);
  1305. unsigned int modem_num = chip->num_of_codecs - 1;
  1306. snd_ac97_write(chip->ac97[modem_num], AC97_LINE1_RATE,
  1307. params_rate(hw_params));
  1308. snd_ac97_write(chip->ac97[modem_num], AC97_LINE1_LEVEL, 0);
  1309. return snd_ali_hw_params(substream, hw_params);
  1310. }
  1311. static struct snd_pcm_hardware snd_ali_modem =
  1312. {
  1313. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  1314. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  1315. SNDRV_PCM_INFO_MMAP_VALID |
  1316. SNDRV_PCM_INFO_RESUME |
  1317. SNDRV_PCM_INFO_SYNC_START),
  1318. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  1319. .rates = (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_8000 |
  1320. SNDRV_PCM_RATE_16000),
  1321. .rate_min = 8000,
  1322. .rate_max = 16000,
  1323. .channels_min = 1,
  1324. .channels_max = 1,
  1325. .buffer_bytes_max = (256*1024),
  1326. .period_bytes_min = 64,
  1327. .period_bytes_max = (256*1024),
  1328. .periods_min = 1,
  1329. .periods_max = 1024,
  1330. .fifo_size = 0,
  1331. };
  1332. static int snd_ali_modem_open(struct snd_pcm_substream *substream, int rec,
  1333. int channel)
  1334. {
  1335. static unsigned int rates[] = {8000, 9600, 12000, 16000};
  1336. static struct snd_pcm_hw_constraint_list hw_constraint_rates = {
  1337. .count = ARRAY_SIZE(rates),
  1338. .list = rates,
  1339. .mask = 0,
  1340. };
  1341. int err = snd_ali_open(substream, rec, channel, &snd_ali_modem);
  1342. if (err)
  1343. return err;
  1344. return snd_pcm_hw_constraint_list(substream->runtime, 0,
  1345. SNDRV_PCM_HW_PARAM_RATE, &hw_constraint_rates);
  1346. }
  1347. static int snd_ali_modem_playback_open(struct snd_pcm_substream *substream)
  1348. {
  1349. return snd_ali_modem_open(substream, 0, ALI_MODEM_OUT_CHANNEL);
  1350. }
  1351. static int snd_ali_modem_capture_open(struct snd_pcm_substream *substream)
  1352. {
  1353. return snd_ali_modem_open(substream, 1, ALI_MODEM_IN_CHANNEL);
  1354. }
  1355. static struct snd_pcm_ops snd_ali_modem_playback_ops = {
  1356. .open = snd_ali_modem_playback_open,
  1357. .close = snd_ali_close,
  1358. .ioctl = snd_pcm_lib_ioctl,
  1359. .hw_params = snd_ali_modem_hw_params,
  1360. .hw_free = snd_ali_hw_free,
  1361. .prepare = snd_ali_prepare,
  1362. .trigger = snd_ali_trigger,
  1363. .pointer = snd_ali_pointer,
  1364. };
  1365. static struct snd_pcm_ops snd_ali_modem_capture_ops = {
  1366. .open = snd_ali_modem_capture_open,
  1367. .close = snd_ali_close,
  1368. .ioctl = snd_pcm_lib_ioctl,
  1369. .hw_params = snd_ali_modem_hw_params,
  1370. .hw_free = snd_ali_hw_free,
  1371. .prepare = snd_ali_prepare,
  1372. .trigger = snd_ali_trigger,
  1373. .pointer = snd_ali_pointer,
  1374. };
  1375. struct ali_pcm_description {
  1376. char *name;
  1377. unsigned int playback_num;
  1378. unsigned int capture_num;
  1379. struct snd_pcm_ops *playback_ops;
  1380. struct snd_pcm_ops *capture_ops;
  1381. unsigned short class;
  1382. };
  1383. static void snd_ali_pcm_free(struct snd_pcm *pcm)
  1384. {
  1385. struct snd_ali *codec = pcm->private_data;
  1386. codec->pcm[pcm->device] = NULL;
  1387. }
  1388. static int snd_ali_pcm(struct snd_ali *codec, int device,
  1389. struct ali_pcm_description *desc)
  1390. {
  1391. struct snd_pcm *pcm;
  1392. int err;
  1393. err = snd_pcm_new(codec->card, desc->name, device,
  1394. desc->playback_num, desc->capture_num, &pcm);
  1395. if (err < 0) {
  1396. dev_err(codec->card->dev,
  1397. "snd_ali_pcm: err called snd_pcm_new.\n");
  1398. return err;
  1399. }
  1400. pcm->private_data = codec;
  1401. pcm->private_free = snd_ali_pcm_free;
  1402. if (desc->playback_ops)
  1403. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
  1404. desc->playback_ops);
  1405. if (desc->capture_ops)
  1406. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
  1407. desc->capture_ops);
  1408. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
  1409. snd_dma_pci_data(codec->pci),
  1410. 64*1024, 128*1024);
  1411. pcm->info_flags = 0;
  1412. pcm->dev_class = desc->class;
  1413. pcm->dev_subclass = SNDRV_PCM_SUBCLASS_GENERIC_MIX;
  1414. strcpy(pcm->name, desc->name);
  1415. codec->pcm[0] = pcm;
  1416. return 0;
  1417. }
  1418. static struct ali_pcm_description ali_pcms[] = {
  1419. { .name = "ALI 5451",
  1420. .playback_num = ALI_CHANNELS,
  1421. .capture_num = 1,
  1422. .playback_ops = &snd_ali_playback_ops,
  1423. .capture_ops = &snd_ali_capture_ops
  1424. },
  1425. { .name = "ALI 5451 modem",
  1426. .playback_num = 1,
  1427. .capture_num = 1,
  1428. .playback_ops = &snd_ali_modem_playback_ops,
  1429. .capture_ops = &snd_ali_modem_capture_ops,
  1430. .class = SNDRV_PCM_CLASS_MODEM
  1431. }
  1432. };
  1433. static int snd_ali_build_pcms(struct snd_ali *codec)
  1434. {
  1435. int i, err;
  1436. for (i = 0; i < codec->num_of_codecs && i < ARRAY_SIZE(ali_pcms); i++) {
  1437. err = snd_ali_pcm(codec, i, &ali_pcms[i]);
  1438. if (err < 0)
  1439. return err;
  1440. }
  1441. return 0;
  1442. }
  1443. #define ALI5451_SPDIF(xname, xindex, value) \
  1444. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex,\
  1445. .info = snd_ali5451_spdif_info, .get = snd_ali5451_spdif_get, \
  1446. .put = snd_ali5451_spdif_put, .private_value = value}
  1447. #define snd_ali5451_spdif_info snd_ctl_boolean_mono_info
  1448. static int snd_ali5451_spdif_get(struct snd_kcontrol *kcontrol,
  1449. struct snd_ctl_elem_value *ucontrol)
  1450. {
  1451. struct snd_ali *codec = kcontrol->private_data;
  1452. unsigned int spdif_enable;
  1453. spdif_enable = ucontrol->value.integer.value[0] ? 1 : 0;
  1454. spin_lock_irq(&codec->reg_lock);
  1455. switch (kcontrol->private_value) {
  1456. case 0:
  1457. spdif_enable = (codec->spdif_mask & 0x02) ? 1 : 0;
  1458. break;
  1459. case 1:
  1460. spdif_enable = ((codec->spdif_mask & 0x02) &&
  1461. (codec->spdif_mask & 0x04)) ? 1 : 0;
  1462. break;
  1463. case 2:
  1464. spdif_enable = (codec->spdif_mask & 0x01) ? 1 : 0;
  1465. break;
  1466. default:
  1467. break;
  1468. }
  1469. ucontrol->value.integer.value[0] = spdif_enable;
  1470. spin_unlock_irq(&codec->reg_lock);
  1471. return 0;
  1472. }
  1473. static int snd_ali5451_spdif_put(struct snd_kcontrol *kcontrol,
  1474. struct snd_ctl_elem_value *ucontrol)
  1475. {
  1476. struct snd_ali *codec = kcontrol->private_data;
  1477. unsigned int change = 0, spdif_enable = 0;
  1478. spdif_enable = ucontrol->value.integer.value[0] ? 1 : 0;
  1479. spin_lock_irq(&codec->reg_lock);
  1480. switch (kcontrol->private_value) {
  1481. case 0:
  1482. change = (codec->spdif_mask & 0x02) ? 1 : 0;
  1483. change = change ^ spdif_enable;
  1484. if (change) {
  1485. if (spdif_enable) {
  1486. codec->spdif_mask |= 0x02;
  1487. snd_ali_enable_spdif_out(codec);
  1488. } else {
  1489. codec->spdif_mask &= ~(0x02);
  1490. codec->spdif_mask &= ~(0x04);
  1491. snd_ali_disable_spdif_out(codec);
  1492. }
  1493. }
  1494. break;
  1495. case 1:
  1496. change = (codec->spdif_mask & 0x04) ? 1 : 0;
  1497. change = change ^ spdif_enable;
  1498. if (change && (codec->spdif_mask & 0x02)) {
  1499. if (spdif_enable) {
  1500. codec->spdif_mask |= 0x04;
  1501. snd_ali_enable_spdif_chnout(codec);
  1502. } else {
  1503. codec->spdif_mask &= ~(0x04);
  1504. snd_ali_disable_spdif_chnout(codec);
  1505. }
  1506. }
  1507. break;
  1508. case 2:
  1509. change = (codec->spdif_mask & 0x01) ? 1 : 0;
  1510. change = change ^ spdif_enable;
  1511. if (change) {
  1512. if (spdif_enable) {
  1513. codec->spdif_mask |= 0x01;
  1514. snd_ali_enable_spdif_in(codec);
  1515. } else {
  1516. codec->spdif_mask &= ~(0x01);
  1517. snd_ali_disable_spdif_in(codec);
  1518. }
  1519. }
  1520. break;
  1521. default:
  1522. break;
  1523. }
  1524. spin_unlock_irq(&codec->reg_lock);
  1525. return change;
  1526. }
  1527. static struct snd_kcontrol_new snd_ali5451_mixer_spdif[] = {
  1528. /* spdif aplayback switch */
  1529. /* FIXME: "IEC958 Playback Switch" may conflict with one on ac97_codec */
  1530. ALI5451_SPDIF(SNDRV_CTL_NAME_IEC958("Output ",NONE,SWITCH), 0, 0),
  1531. /* spdif out to spdif channel */
  1532. ALI5451_SPDIF(SNDRV_CTL_NAME_IEC958("Channel Output ",NONE,SWITCH), 0, 1),
  1533. /* spdif in from spdif channel */
  1534. ALI5451_SPDIF(SNDRV_CTL_NAME_IEC958("",CAPTURE,SWITCH), 0, 2)
  1535. };
  1536. static int snd_ali_mixer(struct snd_ali *codec)
  1537. {
  1538. struct snd_ac97_template ac97;
  1539. unsigned int idx;
  1540. int i, err;
  1541. static struct snd_ac97_bus_ops ops = {
  1542. .write = snd_ali_codec_write,
  1543. .read = snd_ali_codec_read,
  1544. };
  1545. err = snd_ac97_bus(codec->card, 0, &ops, codec, &codec->ac97_bus);
  1546. if (err < 0)
  1547. return err;
  1548. memset(&ac97, 0, sizeof(ac97));
  1549. ac97.private_data = codec;
  1550. for (i = 0; i < codec->num_of_codecs; i++) {
  1551. ac97.num = i;
  1552. err = snd_ac97_mixer(codec->ac97_bus, &ac97, &codec->ac97[i]);
  1553. if (err < 0) {
  1554. dev_err(codec->card->dev,
  1555. "ali mixer %d creating error.\n", i);
  1556. if (i == 0)
  1557. return err;
  1558. codec->num_of_codecs = 1;
  1559. break;
  1560. }
  1561. }
  1562. if (codec->spdif_support) {
  1563. for (idx = 0; idx < ARRAY_SIZE(snd_ali5451_mixer_spdif); idx++) {
  1564. err = snd_ctl_add(codec->card,
  1565. snd_ctl_new1(&snd_ali5451_mixer_spdif[idx], codec));
  1566. if (err < 0)
  1567. return err;
  1568. }
  1569. }
  1570. return 0;
  1571. }
  1572. #ifdef CONFIG_PM_SLEEP
  1573. static int ali_suspend(struct device *dev)
  1574. {
  1575. struct snd_card *card = dev_get_drvdata(dev);
  1576. struct snd_ali *chip = card->private_data;
  1577. struct snd_ali_image *im;
  1578. int i, j;
  1579. im = chip->image;
  1580. if (!im)
  1581. return 0;
  1582. snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
  1583. for (i = 0; i < chip->num_of_codecs; i++) {
  1584. snd_pcm_suspend_all(chip->pcm[i]);
  1585. snd_ac97_suspend(chip->ac97[i]);
  1586. }
  1587. spin_lock_irq(&chip->reg_lock);
  1588. im->regs[ALI_MISCINT >> 2] = inl(ALI_REG(chip, ALI_MISCINT));
  1589. /* im->regs[ALI_START >> 2] = inl(ALI_REG(chip, ALI_START)); */
  1590. im->regs[ALI_STOP >> 2] = inl(ALI_REG(chip, ALI_STOP));
  1591. /* disable all IRQ bits */
  1592. outl(0, ALI_REG(chip, ALI_MISCINT));
  1593. for (i = 0; i < ALI_GLOBAL_REGS; i++) {
  1594. if ((i*4 == ALI_MISCINT) || (i*4 == ALI_STOP))
  1595. continue;
  1596. im->regs[i] = inl(ALI_REG(chip, i*4));
  1597. }
  1598. for (i = 0; i < ALI_CHANNELS; i++) {
  1599. outb(i, ALI_REG(chip, ALI_GC_CIR));
  1600. for (j = 0; j < ALI_CHANNEL_REGS; j++)
  1601. im->channel_regs[i][j] = inl(ALI_REG(chip, j*4 + 0xe0));
  1602. }
  1603. /* stop all HW channel */
  1604. outl(0xffffffff, ALI_REG(chip, ALI_STOP));
  1605. spin_unlock_irq(&chip->reg_lock);
  1606. return 0;
  1607. }
  1608. static int ali_resume(struct device *dev)
  1609. {
  1610. struct snd_card *card = dev_get_drvdata(dev);
  1611. struct snd_ali *chip = card->private_data;
  1612. struct snd_ali_image *im;
  1613. int i, j;
  1614. im = chip->image;
  1615. if (!im)
  1616. return 0;
  1617. spin_lock_irq(&chip->reg_lock);
  1618. for (i = 0; i < ALI_CHANNELS; i++) {
  1619. outb(i, ALI_REG(chip, ALI_GC_CIR));
  1620. for (j = 0; j < ALI_CHANNEL_REGS; j++)
  1621. outl(im->channel_regs[i][j], ALI_REG(chip, j*4 + 0xe0));
  1622. }
  1623. for (i = 0; i < ALI_GLOBAL_REGS; i++) {
  1624. if ((i*4 == ALI_MISCINT) || (i*4 == ALI_STOP) ||
  1625. (i*4 == ALI_START))
  1626. continue;
  1627. outl(im->regs[i], ALI_REG(chip, i*4));
  1628. }
  1629. /* start HW channel */
  1630. outl(im->regs[ALI_START >> 2], ALI_REG(chip, ALI_START));
  1631. /* restore IRQ enable bits */
  1632. outl(im->regs[ALI_MISCINT >> 2], ALI_REG(chip, ALI_MISCINT));
  1633. spin_unlock_irq(&chip->reg_lock);
  1634. for (i = 0 ; i < chip->num_of_codecs; i++)
  1635. snd_ac97_resume(chip->ac97[i]);
  1636. snd_power_change_state(card, SNDRV_CTL_POWER_D0);
  1637. return 0;
  1638. }
  1639. static SIMPLE_DEV_PM_OPS(ali_pm, ali_suspend, ali_resume);
  1640. #define ALI_PM_OPS &ali_pm
  1641. #else
  1642. #define ALI_PM_OPS NULL
  1643. #endif /* CONFIG_PM_SLEEP */
  1644. static int snd_ali_free(struct snd_ali * codec)
  1645. {
  1646. if (codec->hw_initialized)
  1647. snd_ali_disable_address_interrupt(codec);
  1648. if (codec->irq >= 0)
  1649. free_irq(codec->irq, codec);
  1650. if (codec->port)
  1651. pci_release_regions(codec->pci);
  1652. pci_disable_device(codec->pci);
  1653. #ifdef CONFIG_PM_SLEEP
  1654. kfree(codec->image);
  1655. #endif
  1656. pci_dev_put(codec->pci_m1533);
  1657. pci_dev_put(codec->pci_m7101);
  1658. kfree(codec);
  1659. return 0;
  1660. }
  1661. static int snd_ali_chip_init(struct snd_ali *codec)
  1662. {
  1663. unsigned int legacy;
  1664. unsigned char temp;
  1665. struct pci_dev *pci_dev;
  1666. dev_dbg(codec->card->dev, "chip initializing ...\n");
  1667. if (snd_ali_reset_5451(codec)) {
  1668. dev_err(codec->card->dev, "ali_chip_init: reset 5451 error.\n");
  1669. return -1;
  1670. }
  1671. if (codec->revision == ALI_5451_V02) {
  1672. pci_dev = codec->pci_m1533;
  1673. pci_read_config_byte(pci_dev, 0x59, &temp);
  1674. temp |= 0x80;
  1675. pci_write_config_byte(pci_dev, 0x59, temp);
  1676. pci_dev = codec->pci_m7101;
  1677. pci_read_config_byte(pci_dev, 0xb8, &temp);
  1678. temp |= 0x20;
  1679. pci_write_config_byte(pci_dev, 0xB8, temp);
  1680. }
  1681. pci_read_config_dword(codec->pci, 0x44, &legacy);
  1682. legacy &= 0xff00ff00;
  1683. legacy |= 0x000800aa;
  1684. pci_write_config_dword(codec->pci, 0x44, legacy);
  1685. outl(0x80000001, ALI_REG(codec, ALI_GLOBAL_CONTROL));
  1686. outl(0x00000000, ALI_REG(codec, ALI_AINTEN));
  1687. outl(0xffffffff, ALI_REG(codec, ALI_AINT));
  1688. outl(0x00000000, ALI_REG(codec, ALI_VOLUME));
  1689. outb(0x10, ALI_REG(codec, ALI_MPUR2));
  1690. codec->ac97_ext_id = snd_ali_codec_peek(codec, 0, AC97_EXTENDED_ID);
  1691. codec->ac97_ext_status = snd_ali_codec_peek(codec, 0,
  1692. AC97_EXTENDED_STATUS);
  1693. if (codec->spdif_support) {
  1694. snd_ali_enable_spdif_out(codec);
  1695. codec->spdif_mask = 0x00000002;
  1696. }
  1697. codec->num_of_codecs = 1;
  1698. /* secondary codec - modem */
  1699. if (inl(ALI_REG(codec, ALI_SCTRL)) & ALI_SCTRL_CODEC2_READY) {
  1700. codec->num_of_codecs++;
  1701. outl(inl(ALI_REG(codec, ALI_SCTRL)) |
  1702. (ALI_SCTRL_LINE_IN2 | ALI_SCTRL_GPIO_IN2 |
  1703. ALI_SCTRL_LINE_OUT_EN),
  1704. ALI_REG(codec, ALI_SCTRL));
  1705. }
  1706. dev_dbg(codec->card->dev, "chip initialize succeed.\n");
  1707. return 0;
  1708. }
  1709. /* proc for register dump */
  1710. static void snd_ali_proc_read(struct snd_info_entry *entry,
  1711. struct snd_info_buffer *buf)
  1712. {
  1713. struct snd_ali *codec = entry->private_data;
  1714. int i;
  1715. for (i = 0; i < 256 ; i+= 4)
  1716. snd_iprintf(buf, "%02x: %08x\n", i, inl(ALI_REG(codec, i)));
  1717. }
  1718. static void snd_ali_proc_init(struct snd_ali *codec)
  1719. {
  1720. struct snd_info_entry *entry;
  1721. if (!snd_card_proc_new(codec->card, "ali5451", &entry))
  1722. snd_info_set_text_ops(entry, codec, snd_ali_proc_read);
  1723. }
  1724. static int snd_ali_resources(struct snd_ali *codec)
  1725. {
  1726. int err;
  1727. dev_dbg(codec->card->dev, "resources allocation ...\n");
  1728. err = pci_request_regions(codec->pci, "ALI 5451");
  1729. if (err < 0)
  1730. return err;
  1731. codec->port = pci_resource_start(codec->pci, 0);
  1732. if (request_irq(codec->pci->irq, snd_ali_card_interrupt,
  1733. IRQF_SHARED, KBUILD_MODNAME, codec)) {
  1734. dev_err(codec->card->dev, "Unable to request irq.\n");
  1735. return -EBUSY;
  1736. }
  1737. codec->irq = codec->pci->irq;
  1738. dev_dbg(codec->card->dev, "resources allocated.\n");
  1739. return 0;
  1740. }
  1741. static int snd_ali_dev_free(struct snd_device *device)
  1742. {
  1743. struct snd_ali *codec = device->device_data;
  1744. snd_ali_free(codec);
  1745. return 0;
  1746. }
  1747. static int snd_ali_create(struct snd_card *card,
  1748. struct pci_dev *pci,
  1749. int pcm_streams,
  1750. int spdif_support,
  1751. struct snd_ali **r_ali)
  1752. {
  1753. struct snd_ali *codec;
  1754. int i, err;
  1755. unsigned short cmdw;
  1756. static struct snd_device_ops ops = {
  1757. .dev_free = snd_ali_dev_free,
  1758. };
  1759. *r_ali = NULL;
  1760. dev_dbg(card->dev, "creating ...\n");
  1761. /* enable PCI device */
  1762. err = pci_enable_device(pci);
  1763. if (err < 0)
  1764. return err;
  1765. /* check, if we can restrict PCI DMA transfers to 31 bits */
  1766. if (dma_set_mask(&pci->dev, DMA_BIT_MASK(31)) < 0 ||
  1767. dma_set_coherent_mask(&pci->dev, DMA_BIT_MASK(31)) < 0) {
  1768. dev_err(card->dev,
  1769. "architecture does not support 31bit PCI busmaster DMA\n");
  1770. pci_disable_device(pci);
  1771. return -ENXIO;
  1772. }
  1773. codec = kzalloc(sizeof(*codec), GFP_KERNEL);
  1774. if (!codec) {
  1775. pci_disable_device(pci);
  1776. return -ENOMEM;
  1777. }
  1778. spin_lock_init(&codec->reg_lock);
  1779. spin_lock_init(&codec->voice_alloc);
  1780. codec->card = card;
  1781. codec->pci = pci;
  1782. codec->irq = -1;
  1783. codec->revision = pci->revision;
  1784. codec->spdif_support = spdif_support;
  1785. if (pcm_streams < 1)
  1786. pcm_streams = 1;
  1787. if (pcm_streams > 32)
  1788. pcm_streams = 32;
  1789. pci_set_master(pci);
  1790. pci_read_config_word(pci, PCI_COMMAND, &cmdw);
  1791. if ((cmdw & PCI_COMMAND_IO) != PCI_COMMAND_IO) {
  1792. cmdw |= PCI_COMMAND_IO;
  1793. pci_write_config_word(pci, PCI_COMMAND, cmdw);
  1794. }
  1795. pci_set_master(pci);
  1796. if (snd_ali_resources(codec)) {
  1797. snd_ali_free(codec);
  1798. return -EBUSY;
  1799. }
  1800. synchronize_irq(pci->irq);
  1801. codec->synth.chmap = 0;
  1802. codec->synth.chcnt = 0;
  1803. codec->spdif_mask = 0;
  1804. codec->synth.synthcount = 0;
  1805. if (codec->revision == ALI_5451_V02)
  1806. codec->chregs.regs.ac97read = ALI_AC97_WRITE;
  1807. else
  1808. codec->chregs.regs.ac97read = ALI_AC97_READ;
  1809. codec->chregs.regs.ac97write = ALI_AC97_WRITE;
  1810. codec->chregs.regs.start = ALI_START;
  1811. codec->chregs.regs.stop = ALI_STOP;
  1812. codec->chregs.regs.aint = ALI_AINT;
  1813. codec->chregs.regs.ainten = ALI_AINTEN;
  1814. codec->chregs.data.start = 0x00;
  1815. codec->chregs.data.stop = 0x00;
  1816. codec->chregs.data.aint = 0x00;
  1817. codec->chregs.data.ainten = 0x00;
  1818. /* M1533: southbridge */
  1819. codec->pci_m1533 = pci_get_device(0x10b9, 0x1533, NULL);
  1820. if (!codec->pci_m1533) {
  1821. dev_err(card->dev, "cannot find ALi 1533 chip.\n");
  1822. snd_ali_free(codec);
  1823. return -ENODEV;
  1824. }
  1825. /* M7101: power management */
  1826. codec->pci_m7101 = pci_get_device(0x10b9, 0x7101, NULL);
  1827. if (!codec->pci_m7101 && codec->revision == ALI_5451_V02) {
  1828. dev_err(card->dev, "cannot find ALi 7101 chip.\n");
  1829. snd_ali_free(codec);
  1830. return -ENODEV;
  1831. }
  1832. dev_dbg(card->dev, "snd_device_new is called.\n");
  1833. err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, codec, &ops);
  1834. if (err < 0) {
  1835. snd_ali_free(codec);
  1836. return err;
  1837. }
  1838. /* initialise synth voices*/
  1839. for (i = 0; i < ALI_CHANNELS; i++)
  1840. codec->synth.voices[i].number = i;
  1841. err = snd_ali_chip_init(codec);
  1842. if (err < 0) {
  1843. dev_err(card->dev, "ali create: chip init error.\n");
  1844. return err;
  1845. }
  1846. #ifdef CONFIG_PM_SLEEP
  1847. codec->image = kmalloc(sizeof(*codec->image), GFP_KERNEL);
  1848. if (!codec->image)
  1849. dev_warn(card->dev, "can't allocate apm buffer\n");
  1850. #endif
  1851. snd_ali_enable_address_interrupt(codec);
  1852. codec->hw_initialized = 1;
  1853. *r_ali = codec;
  1854. dev_dbg(card->dev, "created.\n");
  1855. return 0;
  1856. }
  1857. static int snd_ali_probe(struct pci_dev *pci,
  1858. const struct pci_device_id *pci_id)
  1859. {
  1860. struct snd_card *card;
  1861. struct snd_ali *codec;
  1862. int err;
  1863. dev_dbg(&pci->dev, "probe ...\n");
  1864. err = snd_card_new(&pci->dev, index, id, THIS_MODULE, 0, &card);
  1865. if (err < 0)
  1866. return err;
  1867. err = snd_ali_create(card, pci, pcm_channels, spdif, &codec);
  1868. if (err < 0)
  1869. goto error;
  1870. card->private_data = codec;
  1871. dev_dbg(&pci->dev, "mixer building ...\n");
  1872. err = snd_ali_mixer(codec);
  1873. if (err < 0)
  1874. goto error;
  1875. dev_dbg(&pci->dev, "pcm building ...\n");
  1876. err = snd_ali_build_pcms(codec);
  1877. if (err < 0)
  1878. goto error;
  1879. snd_ali_proc_init(codec);
  1880. strcpy(card->driver, "ALI5451");
  1881. strcpy(card->shortname, "ALI 5451");
  1882. sprintf(card->longname, "%s at 0x%lx, irq %i",
  1883. card->shortname, codec->port, codec->irq);
  1884. dev_dbg(&pci->dev, "register card.\n");
  1885. err = snd_card_register(card);
  1886. if (err < 0)
  1887. goto error;
  1888. pci_set_drvdata(pci, card);
  1889. return 0;
  1890. error:
  1891. snd_card_free(card);
  1892. return err;
  1893. }
  1894. static void snd_ali_remove(struct pci_dev *pci)
  1895. {
  1896. snd_card_free(pci_get_drvdata(pci));
  1897. }
  1898. static struct pci_driver ali5451_driver = {
  1899. .name = KBUILD_MODNAME,
  1900. .id_table = snd_ali_ids,
  1901. .probe = snd_ali_probe,
  1902. .remove = snd_ali_remove,
  1903. .driver = {
  1904. .pm = ALI_PM_OPS,
  1905. },
  1906. };
  1907. module_pci_driver(ali5451_driver);