ali5451.c 56 KB

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