ali5451.c 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297
  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 <asm/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 pci_dev *pci = to_pci_dev(dev);
  1576. struct snd_card *card = dev_get_drvdata(dev);
  1577. struct snd_ali *chip = card->private_data;
  1578. struct snd_ali_image *im;
  1579. int i, j;
  1580. im = chip->image;
  1581. if (!im)
  1582. return 0;
  1583. snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
  1584. for (i = 0; i < chip->num_of_codecs; i++) {
  1585. snd_pcm_suspend_all(chip->pcm[i]);
  1586. snd_ac97_suspend(chip->ac97[i]);
  1587. }
  1588. spin_lock_irq(&chip->reg_lock);
  1589. im->regs[ALI_MISCINT >> 2] = inl(ALI_REG(chip, ALI_MISCINT));
  1590. /* im->regs[ALI_START >> 2] = inl(ALI_REG(chip, ALI_START)); */
  1591. im->regs[ALI_STOP >> 2] = inl(ALI_REG(chip, ALI_STOP));
  1592. /* disable all IRQ bits */
  1593. outl(0, ALI_REG(chip, ALI_MISCINT));
  1594. for (i = 0; i < ALI_GLOBAL_REGS; i++) {
  1595. if ((i*4 == ALI_MISCINT) || (i*4 == ALI_STOP))
  1596. continue;
  1597. im->regs[i] = inl(ALI_REG(chip, i*4));
  1598. }
  1599. for (i = 0; i < ALI_CHANNELS; i++) {
  1600. outb(i, ALI_REG(chip, ALI_GC_CIR));
  1601. for (j = 0; j < ALI_CHANNEL_REGS; j++)
  1602. im->channel_regs[i][j] = inl(ALI_REG(chip, j*4 + 0xe0));
  1603. }
  1604. /* stop all HW channel */
  1605. outl(0xffffffff, ALI_REG(chip, ALI_STOP));
  1606. spin_unlock_irq(&chip->reg_lock);
  1607. pci_disable_device(pci);
  1608. pci_save_state(pci);
  1609. pci_set_power_state(pci, PCI_D3hot);
  1610. return 0;
  1611. }
  1612. static int ali_resume(struct device *dev)
  1613. {
  1614. struct pci_dev *pci = to_pci_dev(dev);
  1615. struct snd_card *card = dev_get_drvdata(dev);
  1616. struct snd_ali *chip = card->private_data;
  1617. struct snd_ali_image *im;
  1618. int i, j;
  1619. im = chip->image;
  1620. if (!im)
  1621. return 0;
  1622. pci_set_power_state(pci, PCI_D0);
  1623. pci_restore_state(pci);
  1624. if (pci_enable_device(pci) < 0) {
  1625. dev_err(dev, "pci_enable_device failed, disabling device\n");
  1626. snd_card_disconnect(card);
  1627. return -EIO;
  1628. }
  1629. pci_set_master(pci);
  1630. spin_lock_irq(&chip->reg_lock);
  1631. for (i = 0; i < ALI_CHANNELS; i++) {
  1632. outb(i, ALI_REG(chip, ALI_GC_CIR));
  1633. for (j = 0; j < ALI_CHANNEL_REGS; j++)
  1634. outl(im->channel_regs[i][j], ALI_REG(chip, j*4 + 0xe0));
  1635. }
  1636. for (i = 0; i < ALI_GLOBAL_REGS; i++) {
  1637. if ((i*4 == ALI_MISCINT) || (i*4 == ALI_STOP) ||
  1638. (i*4 == ALI_START))
  1639. continue;
  1640. outl(im->regs[i], ALI_REG(chip, i*4));
  1641. }
  1642. /* start HW channel */
  1643. outl(im->regs[ALI_START >> 2], ALI_REG(chip, ALI_START));
  1644. /* restore IRQ enable bits */
  1645. outl(im->regs[ALI_MISCINT >> 2], ALI_REG(chip, ALI_MISCINT));
  1646. spin_unlock_irq(&chip->reg_lock);
  1647. for (i = 0 ; i < chip->num_of_codecs; i++)
  1648. snd_ac97_resume(chip->ac97[i]);
  1649. snd_power_change_state(card, SNDRV_CTL_POWER_D0);
  1650. return 0;
  1651. }
  1652. static SIMPLE_DEV_PM_OPS(ali_pm, ali_suspend, ali_resume);
  1653. #define ALI_PM_OPS &ali_pm
  1654. #else
  1655. #define ALI_PM_OPS NULL
  1656. #endif /* CONFIG_PM_SLEEP */
  1657. static int snd_ali_free(struct snd_ali * codec)
  1658. {
  1659. if (codec->hw_initialized)
  1660. snd_ali_disable_address_interrupt(codec);
  1661. if (codec->irq >= 0)
  1662. free_irq(codec->irq, codec);
  1663. if (codec->port)
  1664. pci_release_regions(codec->pci);
  1665. pci_disable_device(codec->pci);
  1666. #ifdef CONFIG_PM_SLEEP
  1667. kfree(codec->image);
  1668. #endif
  1669. pci_dev_put(codec->pci_m1533);
  1670. pci_dev_put(codec->pci_m7101);
  1671. kfree(codec);
  1672. return 0;
  1673. }
  1674. static int snd_ali_chip_init(struct snd_ali *codec)
  1675. {
  1676. unsigned int legacy;
  1677. unsigned char temp;
  1678. struct pci_dev *pci_dev;
  1679. dev_dbg(codec->card->dev, "chip initializing ...\n");
  1680. if (snd_ali_reset_5451(codec)) {
  1681. dev_err(codec->card->dev, "ali_chip_init: reset 5451 error.\n");
  1682. return -1;
  1683. }
  1684. if (codec->revision == ALI_5451_V02) {
  1685. pci_dev = codec->pci_m1533;
  1686. pci_read_config_byte(pci_dev, 0x59, &temp);
  1687. temp |= 0x80;
  1688. pci_write_config_byte(pci_dev, 0x59, temp);
  1689. pci_dev = codec->pci_m7101;
  1690. pci_read_config_byte(pci_dev, 0xb8, &temp);
  1691. temp |= 0x20;
  1692. pci_write_config_byte(pci_dev, 0xB8, temp);
  1693. }
  1694. pci_read_config_dword(codec->pci, 0x44, &legacy);
  1695. legacy &= 0xff00ff00;
  1696. legacy |= 0x000800aa;
  1697. pci_write_config_dword(codec->pci, 0x44, legacy);
  1698. outl(0x80000001, ALI_REG(codec, ALI_GLOBAL_CONTROL));
  1699. outl(0x00000000, ALI_REG(codec, ALI_AINTEN));
  1700. outl(0xffffffff, ALI_REG(codec, ALI_AINT));
  1701. outl(0x00000000, ALI_REG(codec, ALI_VOLUME));
  1702. outb(0x10, ALI_REG(codec, ALI_MPUR2));
  1703. codec->ac97_ext_id = snd_ali_codec_peek(codec, 0, AC97_EXTENDED_ID);
  1704. codec->ac97_ext_status = snd_ali_codec_peek(codec, 0,
  1705. AC97_EXTENDED_STATUS);
  1706. if (codec->spdif_support) {
  1707. snd_ali_enable_spdif_out(codec);
  1708. codec->spdif_mask = 0x00000002;
  1709. }
  1710. codec->num_of_codecs = 1;
  1711. /* secondary codec - modem */
  1712. if (inl(ALI_REG(codec, ALI_SCTRL)) & ALI_SCTRL_CODEC2_READY) {
  1713. codec->num_of_codecs++;
  1714. outl(inl(ALI_REG(codec, ALI_SCTRL)) |
  1715. (ALI_SCTRL_LINE_IN2 | ALI_SCTRL_GPIO_IN2 |
  1716. ALI_SCTRL_LINE_OUT_EN),
  1717. ALI_REG(codec, ALI_SCTRL));
  1718. }
  1719. dev_dbg(codec->card->dev, "chip initialize succeed.\n");
  1720. return 0;
  1721. }
  1722. /* proc for register dump */
  1723. static void snd_ali_proc_read(struct snd_info_entry *entry,
  1724. struct snd_info_buffer *buf)
  1725. {
  1726. struct snd_ali *codec = entry->private_data;
  1727. int i;
  1728. for (i = 0; i < 256 ; i+= 4)
  1729. snd_iprintf(buf, "%02x: %08x\n", i, inl(ALI_REG(codec, i)));
  1730. }
  1731. static void snd_ali_proc_init(struct snd_ali *codec)
  1732. {
  1733. struct snd_info_entry *entry;
  1734. if (!snd_card_proc_new(codec->card, "ali5451", &entry))
  1735. snd_info_set_text_ops(entry, codec, snd_ali_proc_read);
  1736. }
  1737. static int snd_ali_resources(struct snd_ali *codec)
  1738. {
  1739. int err;
  1740. dev_dbg(codec->card->dev, "resources allocation ...\n");
  1741. err = pci_request_regions(codec->pci, "ALI 5451");
  1742. if (err < 0)
  1743. return err;
  1744. codec->port = pci_resource_start(codec->pci, 0);
  1745. if (request_irq(codec->pci->irq, snd_ali_card_interrupt,
  1746. IRQF_SHARED, KBUILD_MODNAME, codec)) {
  1747. dev_err(codec->card->dev, "Unable to request irq.\n");
  1748. return -EBUSY;
  1749. }
  1750. codec->irq = codec->pci->irq;
  1751. dev_dbg(codec->card->dev, "resources allocated.\n");
  1752. return 0;
  1753. }
  1754. static int snd_ali_dev_free(struct snd_device *device)
  1755. {
  1756. struct snd_ali *codec = device->device_data;
  1757. snd_ali_free(codec);
  1758. return 0;
  1759. }
  1760. static int snd_ali_create(struct snd_card *card,
  1761. struct pci_dev *pci,
  1762. int pcm_streams,
  1763. int spdif_support,
  1764. struct snd_ali **r_ali)
  1765. {
  1766. struct snd_ali *codec;
  1767. int i, err;
  1768. unsigned short cmdw;
  1769. static struct snd_device_ops ops = {
  1770. .dev_free = snd_ali_dev_free,
  1771. };
  1772. *r_ali = NULL;
  1773. dev_dbg(card->dev, "creating ...\n");
  1774. /* enable PCI device */
  1775. err = pci_enable_device(pci);
  1776. if (err < 0)
  1777. return err;
  1778. /* check, if we can restrict PCI DMA transfers to 31 bits */
  1779. if (pci_set_dma_mask(pci, DMA_BIT_MASK(31)) < 0 ||
  1780. pci_set_consistent_dma_mask(pci, DMA_BIT_MASK(31)) < 0) {
  1781. dev_err(card->dev,
  1782. "architecture does not support 31bit PCI busmaster DMA\n");
  1783. pci_disable_device(pci);
  1784. return -ENXIO;
  1785. }
  1786. codec = kzalloc(sizeof(*codec), GFP_KERNEL);
  1787. if (!codec) {
  1788. pci_disable_device(pci);
  1789. return -ENOMEM;
  1790. }
  1791. spin_lock_init(&codec->reg_lock);
  1792. spin_lock_init(&codec->voice_alloc);
  1793. codec->card = card;
  1794. codec->pci = pci;
  1795. codec->irq = -1;
  1796. codec->revision = pci->revision;
  1797. codec->spdif_support = spdif_support;
  1798. if (pcm_streams < 1)
  1799. pcm_streams = 1;
  1800. if (pcm_streams > 32)
  1801. pcm_streams = 32;
  1802. pci_set_master(pci);
  1803. pci_read_config_word(pci, PCI_COMMAND, &cmdw);
  1804. if ((cmdw & PCI_COMMAND_IO) != PCI_COMMAND_IO) {
  1805. cmdw |= PCI_COMMAND_IO;
  1806. pci_write_config_word(pci, PCI_COMMAND, cmdw);
  1807. }
  1808. pci_set_master(pci);
  1809. if (snd_ali_resources(codec)) {
  1810. snd_ali_free(codec);
  1811. return -EBUSY;
  1812. }
  1813. synchronize_irq(pci->irq);
  1814. codec->synth.chmap = 0;
  1815. codec->synth.chcnt = 0;
  1816. codec->spdif_mask = 0;
  1817. codec->synth.synthcount = 0;
  1818. if (codec->revision == ALI_5451_V02)
  1819. codec->chregs.regs.ac97read = ALI_AC97_WRITE;
  1820. else
  1821. codec->chregs.regs.ac97read = ALI_AC97_READ;
  1822. codec->chregs.regs.ac97write = ALI_AC97_WRITE;
  1823. codec->chregs.regs.start = ALI_START;
  1824. codec->chregs.regs.stop = ALI_STOP;
  1825. codec->chregs.regs.aint = ALI_AINT;
  1826. codec->chregs.regs.ainten = ALI_AINTEN;
  1827. codec->chregs.data.start = 0x00;
  1828. codec->chregs.data.stop = 0x00;
  1829. codec->chregs.data.aint = 0x00;
  1830. codec->chregs.data.ainten = 0x00;
  1831. /* M1533: southbridge */
  1832. codec->pci_m1533 = pci_get_device(0x10b9, 0x1533, NULL);
  1833. if (!codec->pci_m1533) {
  1834. dev_err(card->dev, "cannot find ALi 1533 chip.\n");
  1835. snd_ali_free(codec);
  1836. return -ENODEV;
  1837. }
  1838. /* M7101: power management */
  1839. codec->pci_m7101 = pci_get_device(0x10b9, 0x7101, NULL);
  1840. if (!codec->pci_m7101 && codec->revision == ALI_5451_V02) {
  1841. dev_err(card->dev, "cannot find ALi 7101 chip.\n");
  1842. snd_ali_free(codec);
  1843. return -ENODEV;
  1844. }
  1845. dev_dbg(card->dev, "snd_device_new is called.\n");
  1846. err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, codec, &ops);
  1847. if (err < 0) {
  1848. snd_ali_free(codec);
  1849. return err;
  1850. }
  1851. /* initialise synth voices*/
  1852. for (i = 0; i < ALI_CHANNELS; i++)
  1853. codec->synth.voices[i].number = i;
  1854. err = snd_ali_chip_init(codec);
  1855. if (err < 0) {
  1856. dev_err(card->dev, "ali create: chip init error.\n");
  1857. return err;
  1858. }
  1859. #ifdef CONFIG_PM_SLEEP
  1860. codec->image = kmalloc(sizeof(*codec->image), GFP_KERNEL);
  1861. if (!codec->image)
  1862. dev_warn(card->dev, "can't allocate apm buffer\n");
  1863. #endif
  1864. snd_ali_enable_address_interrupt(codec);
  1865. codec->hw_initialized = 1;
  1866. *r_ali = codec;
  1867. dev_dbg(card->dev, "created.\n");
  1868. return 0;
  1869. }
  1870. static int snd_ali_probe(struct pci_dev *pci,
  1871. const struct pci_device_id *pci_id)
  1872. {
  1873. struct snd_card *card;
  1874. struct snd_ali *codec;
  1875. int err;
  1876. dev_dbg(&pci->dev, "probe ...\n");
  1877. err = snd_card_new(&pci->dev, index, id, THIS_MODULE, 0, &card);
  1878. if (err < 0)
  1879. return err;
  1880. err = snd_ali_create(card, pci, pcm_channels, spdif, &codec);
  1881. if (err < 0)
  1882. goto error;
  1883. card->private_data = codec;
  1884. dev_dbg(&pci->dev, "mixer building ...\n");
  1885. err = snd_ali_mixer(codec);
  1886. if (err < 0)
  1887. goto error;
  1888. dev_dbg(&pci->dev, "pcm building ...\n");
  1889. err = snd_ali_build_pcms(codec);
  1890. if (err < 0)
  1891. goto error;
  1892. snd_ali_proc_init(codec);
  1893. strcpy(card->driver, "ALI5451");
  1894. strcpy(card->shortname, "ALI 5451");
  1895. sprintf(card->longname, "%s at 0x%lx, irq %i",
  1896. card->shortname, codec->port, codec->irq);
  1897. dev_dbg(&pci->dev, "register card.\n");
  1898. err = snd_card_register(card);
  1899. if (err < 0)
  1900. goto error;
  1901. pci_set_drvdata(pci, card);
  1902. return 0;
  1903. error:
  1904. snd_card_free(card);
  1905. return err;
  1906. }
  1907. static void snd_ali_remove(struct pci_dev *pci)
  1908. {
  1909. snd_card_free(pci_get_drvdata(pci));
  1910. }
  1911. static struct pci_driver ali5451_driver = {
  1912. .name = KBUILD_MODNAME,
  1913. .id_table = snd_ali_ids,
  1914. .probe = snd_ali_probe,
  1915. .remove = snd_ali_remove,
  1916. .driver = {
  1917. .pm = ALI_PM_OPS,
  1918. },
  1919. };
  1920. module_pci_driver(ali5451_driver);