ali5451.c 56 KB

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