ali5451.c 55 KB

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