ali5451.c 53 KB

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