ali5451.c 56 KB

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